gem5  v20.1.0.0
disk_image.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
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.
15  *
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.
27  */
28 
33 #ifndef __DEV_STORAGE_DISK_IMAGE_HH__
34 #define __DEV_STORAGE_DISK_IMAGE_HH__
35 
36 #include <fstream>
37 #include <unordered_map>
38 
39 #include "params/CowDiskImage.hh"
40 #include "params/DiskImage.hh"
41 #include "params/RawDiskImage.hh"
42 #include "sim/sim_object.hh"
43 
44 #define SectorSize (512)
45 
49 class DiskImage : public SimObject
50 {
51  protected:
53 
54  public:
55  typedef DiskImageParams Params;
56  DiskImage(const Params *p) : SimObject(p), initialized(false) {}
57  virtual ~DiskImage() {}
58 
59  virtual std::streampos size() const = 0;
60 
61  virtual std::streampos read(uint8_t *data,
62  std::streampos offset) const = 0;
63  virtual std::streampos write(const uint8_t *data,
64  std::streampos offset) = 0;
65 };
66 
70 class RawDiskImage : public DiskImage
71 {
72  protected:
73  mutable std::fstream stream;
74  std::string file;
75  bool readonly;
76  mutable std::streampos disk_size;
77 
78  public:
79  typedef RawDiskImageParams Params;
80  RawDiskImage(const Params *p);
81  ~RawDiskImage();
82 
83  void notifyFork() override;
84 
85  void close();
86  void open(const std::string &filename, bool rd_only = false);
87 
88  std::streampos size() const override;
89 
90  std::streampos read(uint8_t *data, std::streampos offset) const override;
91  std::streampos write(const uint8_t *data, std::streampos offset) override;
92 };
93 
104 class CowDiskImage : public DiskImage
105 {
106  public:
107  static const uint32_t VersionMajor;
108  static const uint32_t VersionMinor;
109 
110  protected:
111  struct Sector {
112  uint8_t data[SectorSize];
113  };
114  typedef std::unordered_map<uint64_t, Sector *> SectorTable;
115 
116  protected:
117  std::string filename;
120 
121  public:
122  typedef CowDiskImageParams Params;
123  CowDiskImage(const Params *p);
124  ~CowDiskImage();
125 
126  void notifyFork() override;
127 
128  void initSectorTable(int hash_size);
129  bool open(const std::string &file);
130  void save() const;
131  void save(const std::string &file) const;
132  void writeback();
133 
134  void serialize(CheckpointOut &cp) const override;
135  void unserialize(CheckpointIn &cp) override;
136 
137  std::streampos size() const override;
138 
139  std::streampos read(uint8_t *data, std::streampos offset) const override;
140  std::streampos write(const uint8_t *data, std::streampos offset) override;
141 };
142 
143 void SafeRead(std::ifstream &stream, void *data, int count);
144 
145 template<class T>
146 void SafeRead(std::ifstream &stream, T &data);
147 
148 template<class T>
149 void SafeReadSwap(std::ifstream &stream, T &data);
150 
151 void SafeWrite(std::ofstream &stream, const void *data, int count);
152 
153 template<class T>
154 void SafeWrite(std::ofstream &stream, const T &data);
155 
156 template<class T>
157 void SafeWriteSwap(std::ofstream &stream, const T &data);
158 
159 #endif // __DEV_STORAGE_DISK_IMAGE_HH__
RawDiskImage
Specialization for accessing a raw disk image.
Definition: disk_image.hh:70
SafeWrite
void SafeWrite(std::ofstream &stream, const void *data, int count)
DiskImage::write
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
CowDiskImage::write
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:401
CowDiskImage::Sector
Definition: disk_image.hh:111
data
const char data[]
Definition: circlebuf.test.cc:42
DiskImage::initialized
bool initialized
Definition: disk_image.hh:52
DiskImage::~DiskImage
virtual ~DiskImage()
Definition: disk_image.hh:57
SectorSize
#define SectorSize
Definition: disk_image.hh:44
RawDiskImage::readonly
bool readonly
Definition: disk_image.hh:75
CowDiskImage::notifyFork
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:200
RawDiskImage::size
std::streampos size() const override
Definition: disk_image.cc:100
X86ISA::count
count
Definition: misc.hh:703
DiskImage::size
virtual std::streampos size() const =0
RawDiskImage::file
std::string file
Definition: disk_image.hh:74
CowDiskImage::CowDiskImage
CowDiskImage(const Params *p)
Definition: disk_image.cc:171
SafeWriteSwap
void SafeWriteSwap(std::ofstream &stream, const T &data)
cp
Definition: cprintf.cc:40
CowDiskImage::table
SectorTable * table
Definition: disk_image.hh:119
SafeReadSwap
void SafeReadSwap(std::ifstream &stream, T &data)
DiskImage
Basic interface for accessing a disk image.
Definition: disk_image.hh:49
CowDiskImage::size
std::streampos size() const override
Definition: disk_image.cc:377
RawDiskImage::RawDiskImage
RawDiskImage(const Params *p)
Definition: disk_image.cc:58
sim_object.hh
CowDiskImage::child
DiskImage * child
Definition: disk_image.hh:118
CowDiskImage::read
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:381
CowDiskImage
Specialization for accessing a copy-on-write disk image layer.
Definition: disk_image.hh:104
CowDiskImage::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: disk_image.cc:433
CowDiskImage::writeback
void writeback()
Definition: disk_image.cc:365
RawDiskImage::~RawDiskImage
~RawDiskImage()
Definition: disk_image.cc:62
CowDiskImage::~CowDiskImage
~CowDiskImage()
Definition: disk_image.cc:188
RawDiskImage::stream
std::fstream stream
Definition: disk_image.hh:73
RawDiskImage::notifyFork
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:66
CowDiskImage::Sector::data
uint8_t data[SectorSize]
Definition: disk_image.hh:112
RawDiskImage::close
void close()
Definition: disk_image.cc:94
CowDiskImage::open
bool open(const std::string &file)
Definition: disk_image.cc:239
RawDiskImage::write
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:135
DiskImage::DiskImage
DiskImage(const Params *p)
Definition: disk_image.hh:56
RawDiskImage::Params
RawDiskImageParams Params
Definition: disk_image.hh:79
CowDiskImage::Params
CowDiskImageParams Params
Definition: disk_image.hh:122
CowDiskImage::VersionMajor
static const uint32_t VersionMajor
Definition: disk_image.hh:107
CowDiskImage::VersionMinor
static const uint32_t VersionMinor
Definition: disk_image.hh:108
DiskImage::read
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
RawDiskImage::open
void open(const std::string &filename, bool rd_only=false)
Definition: disk_image.cc:77
CowDiskImage::filename
std::string filename
Definition: disk_image.hh:117
RawDiskImage::disk_size
std::streampos disk_size
Definition: disk_image.hh:76
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
SafeRead
void SafeRead(std::ifstream &stream, void *data, int count)
CheckpointIn
Definition: serialize.hh:67
CowDiskImage::save
void save() const
Definition: disk_image.cc:321
CowDiskImage::SectorTable
std::unordered_map< uint64_t, Sector * > SectorTable
Definition: disk_image.hh:114
DiskImage::Params
DiskImageParams Params
Definition: disk_image.hh:55
CowDiskImage::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: disk_image.cc:425
RawDiskImage::read
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:113
CowDiskImage::initSectorTable
void initSectorTable(int hash_size)
Definition: disk_image.cc:285
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:11 for gem5 by doxygen 1.8.17