2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
32 * Disk Image Definitions
35 #include <sys/types.h>
44 #include "base/callback.hh"
45 #include "base/misc.hh"
46 #include "base/trace.hh"
47 #include "dev/disk_image.hh"
48 #include "sim/builder.hh"
49 #include "sim/sim_exit.hh"
50 #include "sim/byteswap.hh"
54 ////////////////////////////////////////////////////////////////////////
58 RawDiskImage::RawDiskImage(const string
&name
, const string
&filename
,
60 : DiskImage(name
), disk_size(0)
61 { open(filename
, rd_only
); }
63 RawDiskImage::~RawDiskImage()
67 RawDiskImage::open(const string
&filename
, bool rd_only
)
69 if (!filename
.empty()) {
74 ios::openmode mode
= ios::in
| ios::binary
;
77 stream
.open(file
.c_str(), mode
);
78 if (!stream
.is_open())
79 panic("Error opening %s", filename
);
90 RawDiskImage::size() const
93 if (!stream
.is_open())
94 panic("file not open!\n");
95 stream
.seekg(0, ios::end
);
96 disk_size
= stream
.tellg();
99 return disk_size
/ SectorSize
;
103 RawDiskImage::read(uint8_t *data
, off_t offset
) const
106 panic("RawDiskImage not initialized");
108 if (!stream
.is_open())
109 panic("file not open!\n");
111 if (stream
.seekg(offset
* SectorSize
, ios::beg
) < 0)
112 panic("Could not seek to location in file");
114 streampos pos
= stream
.tellg();
115 stream
.read((char *)data
, SectorSize
);
117 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
118 DDUMP(DiskImageRead
, data
, SectorSize
);
120 return stream
.tellg() - pos
;
124 RawDiskImage::write(const uint8_t *data
, off_t offset
)
127 panic("RawDiskImage not initialized");
130 panic("Cannot write to a read only disk image");
132 if (!stream
.is_open())
133 panic("file not open!\n");
135 if (stream
.seekp(offset
* SectorSize
, ios::beg
) < 0)
136 panic("Could not seek to location in file");
138 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
139 DDUMP(DiskImageWrite
, data
, SectorSize
);
141 streampos pos
= stream
.tellp();
142 stream
.write((const char *)data
, SectorSize
);
143 return stream
.tellp() - pos
;
146 DEFINE_SIM_OBJECT_CLASS_NAME("DiskImage", DiskImage
)
148 BEGIN_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
150 Param
<string
> image_file
;
151 Param
<bool> read_only
;
153 END_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
155 BEGIN_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
157 INIT_PARAM(image_file
, "disk image file"),
158 INIT_PARAM_DFLT(read_only
, "read only image", false)
160 END_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
163 CREATE_SIM_OBJECT(RawDiskImage
)
165 return new RawDiskImage(getInstanceName(), image_file
, read_only
);
168 REGISTER_SIM_OBJECT("RawDiskImage", RawDiskImage
)
170 ////////////////////////////////////////////////////////////////////////
172 // Copy on Write Disk image
174 const int CowDiskImage::VersionMajor
= 1;
175 const int CowDiskImage::VersionMinor
= 0;
177 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
)
178 : DiskImage(name
), child(kid
), table(NULL
)
181 class CowDiskCallback
: public Callback
187 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
188 void process() { image
->save(); delete this; }
191 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
,
192 const string
&file
, bool read_only
)
193 : DiskImage(name
), filename(file
), child(kid
), table(NULL
)
195 if (!open(filename
)) {
196 assert(!read_only
&& "why have a non-existent read only file?");
201 registerExitCallback(new CowDiskCallback(this));
204 CowDiskImage::~CowDiskImage()
206 SectorTable::iterator i
= table
->begin();
207 SectorTable::iterator end
= table
->end();
216 SafeRead(ifstream
&stream
, void *data
, int count
)
218 stream
.read((char *)data
, count
);
219 if (!stream
.is_open())
220 panic("file not open");
223 panic("premature end-of-file");
225 if (stream
.bad() || stream
.fail())
226 panic("error reading cowdisk image");
231 SafeRead(ifstream
&stream
, T
&data
)
233 SafeRead(stream
, &data
, sizeof(data
));
238 SafeReadSwap(ifstream
&stream
, T
&data
)
240 SafeRead(stream
, &data
, sizeof(data
));
241 data
= letoh(data
); //is this the proper byte order conversion?
245 CowDiskImage::open(const string
&file
)
247 ifstream
stream(file
.c_str());
248 if (!stream
.is_open())
251 if (stream
.fail() || stream
.bad())
252 panic("Error opening %s", file
);
255 SafeRead(stream
, magic
);
257 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
258 panic("Could not open %s: Invalid magic", file
);
260 uint32_t major
, minor
;
261 SafeReadSwap(stream
, major
);
262 SafeReadSwap(stream
, minor
);
264 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
265 panic("Could not open %s: invalid version %d.%d != %d.%d",
266 file
, major
, minor
, VersionMajor
, VersionMinor
);
268 uint64_t sector_count
;
269 SafeReadSwap(stream
, sector_count
);
270 table
= new SectorTable(sector_count
);
273 for (uint64_t i
= 0; i
< sector_count
; i
++) {
275 SafeReadSwap(stream
, offset
);
277 Sector
*sector
= new Sector
;
278 SafeRead(stream
, sector
, sizeof(Sector
));
280 assert(table
->find(offset
) == table
->end());
281 (*table
)[offset
] = sector
;
291 CowDiskImage::init(int hash_size
)
293 table
= new SectorTable(hash_size
);
299 SafeWrite(ofstream
&stream
, const void *data
, int count
)
301 stream
.write((const char *)data
, count
);
302 if (!stream
.is_open())
303 panic("file not open");
306 panic("premature end-of-file");
308 if (stream
.bad() || stream
.fail())
309 panic("error reading cowdisk image");
314 SafeWrite(ofstream
&stream
, const T
&data
)
316 SafeWrite(stream
, &data
, sizeof(data
));
321 SafeWriteSwap(ofstream
&stream
, const T
&data
)
323 T swappeddata
= letoh(data
); //is this the proper byte order conversion?
324 SafeWrite(stream
, &swappeddata
, sizeof(data
));
333 CowDiskImage::save(const string
&file
)
336 panic("RawDiskImage not initialized");
338 ofstream
stream(file
.c_str());
339 if (!stream
.is_open() || stream
.fail() || stream
.bad())
340 panic("Error opening %s", file
);
343 memcpy(&magic
, "COWDISK!", sizeof(magic
));
344 SafeWrite(stream
, magic
);
346 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
347 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
348 SafeWriteSwap(stream
, (uint64_t)table
->size());
350 uint64_t size
= table
->size();
351 SectorTable::iterator iter
= table
->begin();
352 SectorTable::iterator end
= table
->end();
354 for (uint64_t i
= 0; i
< size
; i
++) {
356 panic("Incorrect Table Size during save of COW disk image");
358 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
359 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
367 CowDiskImage::writeback()
369 SectorTable::iterator i
= table
->begin();
370 SectorTable::iterator end
= table
->end();
373 child
->write((*i
).second
->data
, (*i
).first
);
379 CowDiskImage::size() const
380 { return child
->size(); }
383 CowDiskImage::read(uint8_t *data
, off_t offset
) const
386 panic("CowDiskImage not initialized");
389 panic("access out of bounds");
391 SectorTable::const_iterator i
= table
->find(offset
);
392 if (i
== table
->end())
393 return child
->read(data
, offset
);
395 memcpy(data
, (*i
).second
->data
, SectorSize
);
396 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
397 DDUMP(DiskImageRead
, data
, SectorSize
);
403 CowDiskImage::write(const uint8_t *data
, off_t offset
)
406 panic("RawDiskImage not initialized");
409 panic("access out of bounds");
411 SectorTable::iterator i
= table
->find(offset
);
412 if (i
== table
->end()) {
413 Sector
*sector
= new Sector
;
414 memcpy(sector
, data
, SectorSize
);
415 table
->insert(make_pair(offset
, sector
));
417 memcpy((*i
).second
->data
, data
, SectorSize
);
420 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
421 DDUMP(DiskImageWrite
, data
, SectorSize
);
427 CowDiskImage::serialize(ostream
&os
)
429 string cowFilename
= name() + ".cow";
430 SERIALIZE_SCALAR(cowFilename
);
431 save(Checkpoint::dir() + "/" + cowFilename
);
435 CowDiskImage::unserialize(Checkpoint
*cp
, const string
§ion
)
438 UNSERIALIZE_SCALAR(cowFilename
);
439 cowFilename
= cp
->cptDir
+ "/" + cowFilename
;
443 BEGIN_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
445 SimObjectParam
<DiskImage
*> child
;
446 Param
<string
> image_file
;
447 Param
<int> table_size
;
448 Param
<bool> read_only
;
450 END_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
452 BEGIN_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
454 INIT_PARAM(child
, "child image"),
455 INIT_PARAM_DFLT(image_file
, "disk image file", ""),
456 INIT_PARAM_DFLT(table_size
, "initial table size", 65536),
457 INIT_PARAM_DFLT(read_only
, "don't write back to the copy-on-write file",
460 END_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
463 CREATE_SIM_OBJECT(CowDiskImage
)
465 if (((string
)image_file
).empty())
466 return new CowDiskImage(getInstanceName(), child
, table_size
);
468 return new CowDiskImage(getInstanceName(), child
, table_size
,
469 image_file
, read_only
);
472 REGISTER_SIM_OBJECT("CowDiskImage", CowDiskImage
)