2 * Copyright (c) 2003 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"
52 ////////////////////////////////////////////////////////////////////////
56 RawDiskImage::RawDiskImage(const string
&name
, const string
&filename
,
58 : DiskImage(name
), disk_size(0)
59 { open(filename
, rd_only
); }
61 RawDiskImage::~RawDiskImage()
65 RawDiskImage::open(const string
&filename
, bool rd_only
)
67 if (!filename
.empty()) {
72 ios::openmode mode
= ios::in
| ios::binary
;
75 stream
.open(file
.c_str(), mode
);
76 if (!stream
.is_open())
77 panic("Error opening %s", filename
);
88 RawDiskImage::size() const
91 if (!stream
.is_open())
92 panic("file not open!\n");
93 stream
.seekg(0, ios::end
);
94 disk_size
= stream
.tellg();
97 return disk_size
/ SectorSize
;
101 RawDiskImage::read(uint8_t *data
, off_t offset
) const
104 panic("RawDiskImage not initialized");
106 if (!stream
.is_open())
107 panic("file not open!\n");
109 if (stream
.seekg(offset
* SectorSize
, ios::beg
) < 0)
110 panic("Could not seek to location in file");
112 streampos pos
= stream
.tellg();
113 stream
.read((char *)data
, SectorSize
);
115 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
116 DDUMP(DiskImageRead
, data
, SectorSize
);
118 return stream
.tellg() - pos
;
122 RawDiskImage::write(const uint8_t *data
, off_t offset
)
125 panic("RawDiskImage not initialized");
128 panic("Cannot write to a read only disk image");
130 if (!stream
.is_open())
131 panic("file not open!\n");
133 if (stream
.seekp(offset
* SectorSize
, ios::beg
) < 0)
134 panic("Could not seek to location in file");
136 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
137 DDUMP(DiskImageWrite
, data
, SectorSize
);
139 streampos pos
= stream
.tellp();
140 stream
.write((const char *)data
, SectorSize
);
141 return stream
.tellp() - pos
;
144 DEFINE_SIM_OBJECT_CLASS_NAME("DiskImage", DiskImage
)
146 BEGIN_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
148 Param
<string
> image_file
;
149 Param
<bool> read_only
;
151 END_DECLARE_SIM_OBJECT_PARAMS(RawDiskImage
)
153 BEGIN_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
155 INIT_PARAM(image_file
, "disk image file"),
156 INIT_PARAM_DFLT(read_only
, "read only image", false)
158 END_INIT_SIM_OBJECT_PARAMS(RawDiskImage
)
161 CREATE_SIM_OBJECT(RawDiskImage
)
163 return new RawDiskImage(getInstanceName(), image_file
, read_only
);
166 REGISTER_SIM_OBJECT("RawDiskImage", RawDiskImage
)
168 ////////////////////////////////////////////////////////////////////////
170 // Copy on Write Disk image
172 const int CowDiskImage::VersionMajor
= 1;
173 const int CowDiskImage::VersionMinor
= 0;
175 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
)
176 : DiskImage(name
), child(kid
), table(NULL
)
179 class CowDiskCallback
: public Callback
185 CowDiskCallback(CowDiskImage
*i
) : image(i
) {}
186 void process() { image
->save(); delete this; }
189 CowDiskImage::CowDiskImage(const string
&name
, DiskImage
*kid
, int hash_size
,
190 const string
&file
, bool read_only
)
191 : DiskImage(name
), filename(file
), child(kid
), table(NULL
)
193 if (!open(filename
)) {
194 assert(!read_only
&& "why have a non-existent read only file?");
199 registerExitCallback(new CowDiskCallback(this));
202 CowDiskImage::~CowDiskImage()
204 SectorTable::iterator i
= table
->begin();
205 SectorTable::iterator end
= table
->end();
214 SafeRead(ifstream
&stream
, void *data
, int count
)
216 stream
.read((char *)data
, count
);
217 if (!stream
.is_open())
218 panic("file not open");
221 panic("premature end-of-file");
223 if (stream
.bad() || stream
.fail())
224 panic("error reading cowdisk image");
229 SafeRead(ifstream
&stream
, T
&data
)
230 { SafeRead(stream
, &data
, sizeof(data
)); }
233 CowDiskImage::open(const string
&file
)
235 ifstream
stream(file
.c_str());
236 if (!stream
.is_open())
239 if (stream
.fail() || stream
.bad())
240 panic("Error opening %s", file
);
243 SafeRead(stream
, magic
);
245 if (memcmp(&magic
, "COWDISK!", sizeof(magic
)) != 0)
246 panic("Could not open %s: Invalid magic", file
);
248 uint32_t major
, minor
;
249 SafeRead(stream
, major
);
250 SafeRead(stream
, minor
);
252 if (major
!= VersionMajor
&& minor
!= VersionMinor
)
253 panic("Could not open %s: invalid version %d.%d != %d.%d",
254 file
, major
, minor
, VersionMajor
, VersionMinor
);
256 uint64_t sector_count
;
257 SafeRead(stream
, sector_count
);
258 table
= new SectorTable(sector_count
);
261 for (uint64_t i
= 0; i
< sector_count
; i
++) {
263 SafeRead(stream
, offset
);
265 Sector
*sector
= new Sector
;
266 SafeRead(stream
, sector
, sizeof(Sector
));
268 assert(table
->find(offset
) == table
->end());
269 (*table
)[offset
] = sector
;
279 CowDiskImage::init(int hash_size
)
281 table
= new SectorTable(hash_size
);
287 SafeWrite(ofstream
&stream
, const void *data
, int count
)
289 stream
.write((const char *)data
, count
);
290 if (!stream
.is_open())
291 panic("file not open");
294 panic("premature end-of-file");
296 if (stream
.bad() || stream
.fail())
297 panic("error reading cowdisk image");
302 SafeWrite(ofstream
&stream
, const T
&data
)
303 { SafeWrite(stream
, &data
, sizeof(data
)); }
312 CowDiskImage::save(const string
&file
)
315 panic("RawDiskImage not initialized");
317 ofstream
stream(file
.c_str());
318 if (!stream
.is_open() || stream
.fail() || stream
.bad())
319 panic("Error opening %s", file
);
322 memcpy(&magic
, "COWDISK!", sizeof(magic
));
323 SafeWrite(stream
, magic
);
325 SafeWrite(stream
, (uint32_t)VersionMajor
);
326 SafeWrite(stream
, (uint32_t)VersionMinor
);
327 SafeWrite(stream
, (uint64_t)table
->size());
329 uint64_t size
= table
->size();
330 SectorTable::iterator iter
= table
->begin();
331 SectorTable::iterator end
= table
->end();
333 for (uint64_t i
= 0; i
< size
; i
++) {
335 panic("Incorrect Table Size during save of COW disk image");
337 SafeWrite(stream
, (uint64_t)(*iter
).first
);
338 SafeWrite(stream
, (*iter
).second
->data
, sizeof(Sector
));
346 CowDiskImage::writeback()
348 SectorTable::iterator i
= table
->begin();
349 SectorTable::iterator end
= table
->end();
352 child
->write((*i
).second
->data
, (*i
).first
);
358 CowDiskImage::size() const
359 { return child
->size(); }
362 CowDiskImage::read(uint8_t *data
, off_t offset
) const
365 panic("CowDiskImage not initialized");
368 panic("access out of bounds");
370 SectorTable::const_iterator i
= table
->find(offset
);
371 if (i
== table
->end())
372 return child
->read(data
, offset
);
374 memcpy(data
, (*i
).second
->data
, SectorSize
);
375 DPRINTF(DiskImageRead
, "read: offset=%d\n", (uint64_t)offset
);
376 DDUMP(DiskImageRead
, data
, SectorSize
);
382 CowDiskImage::write(const uint8_t *data
, off_t offset
)
385 panic("RawDiskImage not initialized");
388 panic("access out of bounds");
390 SectorTable::iterator i
= table
->find(offset
);
391 if (i
== table
->end()) {
392 Sector
*sector
= new Sector
;
393 memcpy(sector
, data
, SectorSize
);
394 table
->insert(make_pair(offset
, sector
));
396 memcpy((*i
).second
->data
, data
, SectorSize
);
399 DPRINTF(DiskImageWrite
, "write: offset=%d\n", (uint64_t)offset
);
400 DDUMP(DiskImageWrite
, data
, SectorSize
);
406 CowDiskImage::serialize(ostream
&os
)
408 string cowFilename
= Checkpoint::dir() + name() + ".cow";
409 SERIALIZE_SCALAR(cowFilename
);
414 CowDiskImage::unserialize(Checkpoint
*cp
, const string
§ion
)
417 UNSERIALIZE_SCALAR(cowFilename
);
421 BEGIN_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
423 SimObjectParam
<DiskImage
*> child
;
424 Param
<string
> image_file
;
425 Param
<int> table_size
;
426 Param
<bool> read_only
;
428 END_DECLARE_SIM_OBJECT_PARAMS(CowDiskImage
)
430 BEGIN_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
432 INIT_PARAM(child
, "child image"),
433 INIT_PARAM_DFLT(image_file
, "disk image file", ""),
434 INIT_PARAM_DFLT(table_size
, "initial table size", 65536),
435 INIT_PARAM_DFLT(read_only
, "don't write back to the copy-on-write file",
438 END_INIT_SIM_OBJECT_PARAMS(CowDiskImage
)
441 CREATE_SIM_OBJECT(CowDiskImage
)
443 if (((string
)image_file
).empty())
444 return new CowDiskImage(getInstanceName(), child
, table_size
);
446 return new CowDiskImage(getInstanceName(), child
, table_size
,
447 image_file
, read_only
);
450 REGISTER_SIM_OBJECT("CowDiskImage", CowDiskImage
)