2 * Copyright (c) 2001-2004 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.
30 * Disk Image Definitions
33 #include <sys/types.h>
43 #include "base/callback.hh"
44 #include "base/misc.hh"
45 #include "base/trace.hh"
46 #include "dev/disk_image.hh"
47 #include "sim/builder.hh"
48 #include "sim/sim_exit.hh"
49 #include "targetarch/byte_swap.hh"
53 ////////////////////////////////////////////////////////////////////////
57 RawDiskImage::RawDiskImage(const string
&name
, const string
&filename
,
59 : DiskImage(name
), disk_size(0)
60 { open(filename
, rd_only
); }
62 RawDiskImage::~RawDiskImage()
66 RawDiskImage::open(const string
&filename
, bool rd_only
)
68 if (!filename
.empty()) {
73 ios::openmode mode
= ios::in
| ios::binary
;
76 stream
.open(file
.c_str(), mode
);
77 if (!stream
.is_open())
78 panic("Error opening %s", filename
);
89 RawDiskImage::size() const
92 if (!stream
.is_open())
93 panic("file not open!\n");
94 stream
.seekg(0, ios::end
);
95 disk_size
= stream
.tellg();
98 return disk_size
/ SectorSize
;
102 RawDiskImage::read(uint8_t *data
, off_t offset
) const
105 panic("RawDiskImage not initialized");
107 if (!stream
.is_open())
108 panic("file not open!\n");
110 if (stream
.seekg(offset
* SectorSize
, ios::beg
) < 0)
111 panic("Could not seek to location in file");
113 streampos pos
= stream
.tellg();
114 stream
.read((char *)data
, SectorSize
);
116 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
117 DDUMP(DiskImageRead
, data
, SectorSize
);
119 return stream
.tellg() - pos
;
123 RawDiskImage::write(const uint8_t *data
, off_t offset
)
126 panic("RawDiskImage not initialized");
129 panic("Cannot write to a read only disk image");
131 if (!stream
.is_open())
132 panic("file not open!\n");
134 if (stream
.seekp(offset
* SectorSize
, ios::beg
) < 0)
135 panic("Could not seek to location in file");
137 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
138 DDUMP(DiskImageWrite
, data
, SectorSize
);
140 streampos pos
= stream
.tellp();
141 stream
.write((const char *)data
, SectorSize
);
142 return stream
.tellp() - pos
;
145 DEFINE_SIM_OBJECT_CLASS_NAME("DiskImage", DiskImage
)
147 BEGIN_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
149 Param
<string
> image_file
;
150 Param
<bool> read_only
;
152 END_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
154 BEGIN_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
156 INIT_PARAM(image_file
, "disk image file"),
157 INIT_PARAM_DFLT(read_only
, "read only image", false)
159 END_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
162 CREATE_SIM_OBJECT(RawDiskImage
)
164 return new RawDiskImage(getInstanceName(), image_file
, read_only
);
167 REGISTER_SIM_OBJECT("RawDiskImage", RawDiskImage
)
169 ////////////////////////////////////////////////////////////////////////
171 // Copy on Write Disk image
173 const int CowDiskImage::VersionMajor
= 1;
174 const int CowDiskImage::VersionMinor
= 0;
176 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
)
177 : DiskImage(name
), child(kid
), table(NULL
)
180 class CowDiskCallback
: public Callback
186 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
187 void process() { image
->save(); delete this; }
190 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
,
191 const string
&file
, bool read_only
)
192 : DiskImage(name
), filename(file
), child(kid
), table(NULL
)
194 if (!open(filename
)) {
195 assert(!read_only
&& "why have a non-existent read only file?");
200 registerExitCallback(new CowDiskCallback(this));
203 CowDiskImage::~CowDiskImage()
205 SectorTable::iterator i
= table
->begin();
206 SectorTable::iterator end
= table
->end();
215 SafeRead(ifstream
&stream
, void *data
, int count
)
217 stream
.read((char *)data
, count
);
218 if (!stream
.is_open())
219 panic("file not open");
222 panic("premature end-of-file");
224 if (stream
.bad() || stream
.fail())
225 panic("error reading cowdisk image");
230 SafeRead(ifstream
&stream
, T
&data
)
232 SafeRead(stream
, &data
, sizeof(data
));
237 SafeReadSwap(ifstream
&stream
, T
&data
)
239 SafeRead(stream
, &data
, sizeof(data
));
244 CowDiskImage::open(const string
&file
)
246 ifstream
stream(file
.c_str());
247 if (!stream
.is_open())
250 if (stream
.fail() || stream
.bad())
251 panic("Error opening %s", file
);
254 SafeRead(stream
, magic
);
256 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
257 panic("Could not open %s: Invalid magic", file
);
259 uint32_t major
, minor
;
260 SafeReadSwap(stream
, major
);
261 SafeReadSwap(stream
, minor
);
263 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
264 panic("Could not open %s: invalid version %d.%d != %d.%d",
265 file
, major
, minor
, VersionMajor
, VersionMinor
);
267 uint64_t sector_count
;
268 SafeReadSwap(stream
, sector_count
);
269 table
= new SectorTable(sector_count
);
272 for (uint64_t i
= 0; i
< sector_count
; i
++) {
274 SafeReadSwap(stream
, offset
);
276 Sector
*sector
= new Sector
;
277 SafeRead(stream
, sector
, sizeof(Sector
));
279 assert(table
->find(offset
) == table
->end());
280 (*table
)[offset
] = sector
;
290 CowDiskImage::init(int hash_size
)
292 table
= new SectorTable(hash_size
);
298 SafeWrite(ofstream
&stream
, const void *data
, int count
)
300 stream
.write((const char *)data
, count
);
301 if (!stream
.is_open())
302 panic("file not open");
305 panic("premature end-of-file");
307 if (stream
.bad() || stream
.fail())
308 panic("error reading cowdisk image");
313 SafeWrite(ofstream
&stream
, const T
&data
)
315 SafeWrite(stream
, &data
, sizeof(data
));
320 SafeWriteSwap(ofstream
&stream
, const T
&data
)
322 T swappeddata
= htoa(data
);
323 SafeWrite(stream
, &swappeddata
, sizeof(data
));
332 CowDiskImage::save(const string
&file
)
335 panic("RawDiskImage not initialized");
337 ofstream
stream(file
.c_str());
338 if (!stream
.is_open() || stream
.fail() || stream
.bad())
339 panic("Error opening %s", file
);
342 memcpy(&magic
, "COWDISK!", sizeof(magic
));
343 SafeWrite(stream
, magic
);
345 SafeWriteSwap(stream
, (uint32_t)VersionMajor
);
346 SafeWriteSwap(stream
, (uint32_t)VersionMinor
);
347 SafeWriteSwap(stream
, (uint64_t)table
->size());
349 uint64_t size
= table
->size();
350 SectorTable::iterator iter
= table
->begin();
351 SectorTable::iterator end
= table
->end();
353 for (uint64_t i
= 0; i
< size
; i
++) {
355 panic("Incorrect Table Size during save of COW disk image");
357 SafeWriteSwap(stream
, (uint64_t)(*iter
).first
);
358 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
366 CowDiskImage::writeback()
368 SectorTable::iterator i
= table
->begin();
369 SectorTable::iterator end
= table
->end();
372 child
->write((*i
).second
->data
, (*i
).first
);
378 CowDiskImage::size() const
379 { return child
->size(); }
382 CowDiskImage::read(uint8_t *data
, off_t offset
) const
385 panic("CowDiskImage not initialized");
388 panic("access out of bounds");
390 SectorTable::const_iterator i
= table
->find(offset
);
391 if (i
== table
->end())
392 return child
->read(data
, offset
);
394 memcpy(data
, (*i
).second
->data
, SectorSize
);
395 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
396 DDUMP(DiskImageRead
, data
, SectorSize
);
402 CowDiskImage::write(const uint8_t *data
, off_t offset
)
405 panic("RawDiskImage not initialized");
408 panic("access out of bounds");
410 SectorTable::iterator i
= table
->find(offset
);
411 if (i
== table
->end()) {
412 Sector
*sector
= new Sector
;
413 memcpy(sector
, data
, SectorSize
);
414 table
->insert(make_pair(offset
, sector
));
416 memcpy((*i
).second
->data
, data
, SectorSize
);
419 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
420 DDUMP(DiskImageWrite
, data
, SectorSize
);
426 CowDiskImage::serialize(ostream
&os
)
428 string cowFilename
= name() + ".cow";
429 SERIALIZE_SCALAR(cowFilename
);
430 save(Checkpoint::dir() + "/" + cowFilename
);
434 CowDiskImage::unserialize(Checkpoint
*cp
, const string
§ion
)
437 UNSERIALIZE_SCALAR(cowFilename
);
438 cowFilename
= cp
->cptDir
+ "/" + cowFilename
;
442 BEGIN_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
444 SimObjectParam
<DiskImage
*> child
;
445 Param
<string
> image_file
;
446 Param
<int> table_size
;
447 Param
<bool> read_only
;
449 END_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
451 BEGIN_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
453 INIT_PARAM(child
, "child image"),
454 INIT_PARAM_DFLT(image_file
, "disk image file", ""),
455 INIT_PARAM_DFLT(table_size
, "initial table size", 65536),
456 INIT_PARAM_DFLT(read_only
, "don't write back to the copy-on-write file",
459 END_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
462 CREATE_SIM_OBJECT(CowDiskImage
)
464 if (((string
)image_file
).empty())
465 return new CowDiskImage(getInstanceName(), child
, table_size
);
467 return new CowDiskImage(getInstanceName(), child
, table_size
,
468 image_file
, read_only
);
471 REGISTER_SIM_OBJECT("CowDiskImage", CowDiskImage
)