gem5  v21.1.0.2
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__
gem5::RawDiskImage::write
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:141
gem5::RawDiskImage::Params
RawDiskImageParams Params
Definition: disk_image.hh:82
gem5::CowDiskImage::size
std::streampos size() const override
Definition: disk_image.cc:377
gem5::CowDiskImage::CowDiskImage
CowDiskImage(const Params &p)
Definition: disk_image.cc:171
gem5::CowDiskImage::Params
CowDiskImageParams Params
Definition: disk_image.hh:126
gem5::CowDiskImage::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: disk_image.cc:425
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::RawDiskImage::close
void close()
Definition: disk_image.cc:100
gem5::DiskImage::DiskImage
DiskImage(const Params &p)
Definition: disk_image.hh:59
gem5::RawDiskImage::~RawDiskImage
~RawDiskImage()
Definition: disk_image.cc:66
SectorSize
#define SectorSize
Definition: disk_image.hh:44
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::CowDiskImage::child
DiskImage * child
Definition: disk_image.hh:122
gem5::DiskImage::~DiskImage
virtual ~DiskImage()
Definition: disk_image.hh:60
gem5::RawDiskImage::RawDiskImage
RawDiskImage(const Params &p)
Definition: disk_image.cc:60
gem5::CowDiskImage::SectorTable
std::unordered_map< uint64_t, Sector * > SectorTable
Definition: disk_image.hh:118
gem5::CowDiskImage::~CowDiskImage
~CowDiskImage()
Definition: disk_image.cc:188
gem5::DiskImage::write
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
gem5::RawDiskImage::notifyFork
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:72
gem5::RawDiskImage::file
std::string file
Definition: disk_image.hh:77
gem5::DiskImage::initialized
bool initialized
Definition: disk_image.hh:55
gem5::CowDiskImage::notifyFork
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:200
gem5::CowDiskImage::Sector::data
uint8_t data[SectorSize]
Definition: disk_image.hh:116
gem5::RawDiskImage::read
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:119
gem5::DiskImage
Basic interface for accessing a disk image.
Definition: disk_image.hh:52
gem5::SafeWrite
void SafeWrite(std::ofstream &stream, const void *data, int count)
Definition: disk_image.cc:293
gem5::RawDiskImage::disk_size
std::streampos disk_size
Definition: disk_image.hh:79
gem5::RawDiskImage
Specialization for accessing a raw disk image.
Definition: disk_image.hh:73
gem5::SafeWriteSwap
void SafeWriteSwap(std::ofstream &stream, const T &data)
Definition: disk_image.cc:315
sim_object.hh
gem5::X86ISA::count
count
Definition: misc.hh:709
gem5::CowDiskImage::Sector
Definition: disk_image.hh:114
gem5::CowDiskImage::save
void save() const
Definition: disk_image.cc:321
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::CowDiskImage::table
SectorTable * table
Definition: disk_image.hh:123
gem5::SafeRead
void SafeRead(std::ifstream &stream, void *data, int count)
Definition: disk_image.cc:210
gem5::ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:144
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::CowDiskImage::VersionMajor
static const uint32_t VersionMajor
Definition: disk_image.hh:110
gem5::RawDiskImage::stream
std::fstream stream
Definition: disk_image.hh:76
gem5::CowDiskImage::initSectorTable
void initSectorTable(int hash_size)
Definition: disk_image.cc:285
gem5::CowDiskImage::read
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:381
gem5::RawDiskImage::open
void open(const std::string &filename, bool rd_only=false)
Definition: disk_image.cc:83
gem5::CowDiskImage::filename
std::string filename
Definition: disk_image.hh:121
gem5::RawDiskImage::readonly
bool readonly
Definition: disk_image.hh:78
gem5::DiskImage::size
virtual std::streampos size() const =0
gem5::RawDiskImage::size
std::streampos size() const override
Definition: disk_image.cc:106
gem5::CowDiskImage::writeback
void writeback()
Definition: disk_image.cc:365
gem5::DiskImage::read
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::CowDiskImage
Specialization for accessing a copy-on-write disk image layer.
Definition: disk_image.hh:107
gem5::SafeReadSwap
void SafeReadSwap(std::ifstream &stream, T &data)
Definition: disk_image.cc:232
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::CowDiskImage::VersionMinor
static const uint32_t VersionMinor
Definition: disk_image.hh:111
gem5::CowDiskImage::open
bool open(const std::string &file)
Definition: disk_image.cc:239
gem5::CowDiskImage::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: disk_image.cc:433
gem5::DiskImage::Params
DiskImageParams Params
Definition: disk_image.hh:58
gem5::CowDiskImage::write
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:401

Generated on Tue Sep 21 2021 12:25:19 for gem5 by doxygen 1.8.17