gem5  v22.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 
46 namespace gem5
47 {
48 
52 class DiskImage : public SimObject
53 {
54  protected:
56 
57  public:
58  typedef DiskImageParams Params;
59  DiskImage(const Params &p) : SimObject(p), initialized(false) {}
60  virtual ~DiskImage() {}
61 
62  virtual std::streampos size() const = 0;
63 
64  virtual std::streampos read(uint8_t *data,
65  std::streampos offset) const = 0;
66  virtual std::streampos write(const uint8_t *data,
67  std::streampos offset) = 0;
68 };
69 
73 class RawDiskImage : public DiskImage
74 {
75  protected:
76  mutable std::fstream stream;
77  std::string file;
78  bool readonly;
79  mutable std::streampos disk_size;
80 
81  public:
82  typedef RawDiskImageParams Params;
83  RawDiskImage(const Params &p);
84  ~RawDiskImage();
85 
86  void notifyFork() override;
87 
88  void close();
89  void open(const std::string &filename, bool rd_only = false);
90 
91  std::streampos size() const override;
92 
93  std::streampos read(uint8_t *data, std::streampos offset) const override;
94  std::streampos write(const uint8_t *data, std::streampos offset) override;
95 };
96 
107 class CowDiskImage : public DiskImage
108 {
109  public:
110  static const uint32_t VersionMajor;
111  static const uint32_t VersionMinor;
112 
113  protected:
114  struct Sector
115  {
116  uint8_t data[SectorSize];
117  };
118  typedef std::unordered_map<uint64_t, Sector *> SectorTable;
119 
120  protected:
121  std::string filename;
124 
125  public:
126  typedef CowDiskImageParams Params;
127  CowDiskImage(const Params &p);
128  ~CowDiskImage();
129 
130  void notifyFork() override;
131 
132  void initSectorTable(int hash_size);
133  bool open(const std::string &file);
134  void save() const;
135  void save(const std::string &file) const;
136  void writeback();
137 
138  void serialize(CheckpointOut &cp) const override;
139  void unserialize(CheckpointIn &cp) override;
140 
141  std::streampos size() const override;
142 
143  std::streampos read(uint8_t *data, std::streampos offset) const override;
144  std::streampos write(const uint8_t *data, std::streampos offset) override;
145 };
146 
147 void SafeRead(std::ifstream &stream, void *data, int count);
148 
149 template<class T>
150 void SafeRead(std::ifstream &stream, T &data);
151 
152 template<class T>
153 void SafeReadSwap(std::ifstream &stream, T &data);
154 
155 void SafeWrite(std::ofstream &stream, const void *data, int count);
156 
157 template<class T>
158 void SafeWrite(std::ofstream &stream, const T &data);
159 
160 template<class T>
161 void SafeWriteSwap(std::ofstream &stream, const T &data);
162 
163 } // namespace gem5
164 
165 
166 #endif // __DEV_STORAGE_DISK_IMAGE_HH__
const char data[]
Specialization for accessing a copy-on-write disk image layer.
Definition: disk_image.hh:108
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:200
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:401
std::streampos size() const override
Definition: disk_image.cc:377
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: disk_image.cc:433
static const uint32_t VersionMajor
Definition: disk_image.hh:110
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:381
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: disk_image.cc:425
std::unordered_map< uint64_t, Sector * > SectorTable
Definition: disk_image.hh:118
void save() const
Definition: disk_image.cc:321
bool open(const std::string &file)
Definition: disk_image.cc:239
CowDiskImageParams Params
Definition: disk_image.hh:126
SectorTable * table
Definition: disk_image.hh:123
static const uint32_t VersionMinor
Definition: disk_image.hh:111
void initSectorTable(int hash_size)
Definition: disk_image.cc:285
CowDiskImage(const Params &p)
Definition: disk_image.cc:171
std::string filename
Definition: disk_image.hh:121
DiskImage * child
Definition: disk_image.hh:122
Basic interface for accessing a disk image.
Definition: disk_image.hh:53
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
DiskImageParams Params
Definition: disk_image.hh:58
virtual std::streampos size() const =0
virtual ~DiskImage()
Definition: disk_image.hh:60
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
DiskImage(const Params &p)
Definition: disk_image.hh:59
Specialization for accessing a raw disk image.
Definition: disk_image.hh:74
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:72
void open(const std::string &filename, bool rd_only=false)
Definition: disk_image.cc:83
std::streampos size() const override
Definition: disk_image.cc:106
std::fstream stream
Definition: disk_image.hh:76
std::streampos disk_size
Definition: disk_image.hh:79
RawDiskImage(const Params &p)
Definition: disk_image.cc:60
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:119
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:141
RawDiskImageParams Params
Definition: disk_image.hh:82
std::string file
Definition: disk_image.hh:77
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
#define SectorSize
Definition: disk_image.hh:44
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 54 > p
Definition: pagetable.hh:70
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
void SafeWriteSwap(std::ofstream &stream, const T &data)
Definition: disk_image.cc:315
std::ostream CheckpointOut
Definition: serialize.hh:66
void SafeRead(std::ifstream &stream, void *data, int count)
Definition: disk_image.cc:210
void SafeWrite(std::ofstream &stream, const void *data, int count)
Definition: disk_image.cc:293
void SafeReadSwap(std::ifstream &stream, T &data)
Definition: disk_image.cc:232
uint8_t data[SectorSize]
Definition: disk_image.hh:116

Generated on Wed Dec 21 2022 10:22:35 for gem5 by doxygen 1.9.1