gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  * Authors: Nathan Binkert
29  */
30 
35 #ifndef __DEV_STORAGE_DISK_IMAGE_HH__
36 #define __DEV_STORAGE_DISK_IMAGE_HH__
37 
38 #include <fstream>
39 #include <unordered_map>
40 
41 #include "params/CowDiskImage.hh"
42 #include "params/DiskImage.hh"
43 #include "params/RawDiskImage.hh"
44 #include "sim/sim_object.hh"
45 
46 #define SectorSize (512)
47 
51 class DiskImage : public SimObject
52 {
53  protected:
55 
56  public:
57  typedef DiskImageParams Params;
58  DiskImage(const Params *p) : SimObject(p), initialized(false) {}
59  virtual ~DiskImage() {}
60 
61  virtual std::streampos size() const = 0;
62 
63  virtual std::streampos read(uint8_t *data,
64  std::streampos offset) const = 0;
65  virtual std::streampos write(const uint8_t *data,
66  std::streampos offset) = 0;
67 };
68 
72 class RawDiskImage : public DiskImage
73 {
74  protected:
75  mutable std::fstream stream;
76  std::string file;
77  bool readonly;
78  mutable std::streampos disk_size;
79 
80  public:
81  typedef RawDiskImageParams Params;
82  RawDiskImage(const Params *p);
83  ~RawDiskImage();
84 
85  void notifyFork() override;
86 
87  void close();
88  void open(const std::string &filename, bool rd_only = false);
89 
90  std::streampos size() const override;
91 
92  std::streampos read(uint8_t *data, std::streampos offset) const override;
93  std::streampos write(const uint8_t *data, std::streampos offset) override;
94 };
95 
106 class CowDiskImage : public DiskImage
107 {
108  public:
109  static const uint32_t VersionMajor;
110  static const uint32_t VersionMinor;
111 
112  protected:
113  struct Sector {
114  uint8_t data[SectorSize];
115  };
116  typedef std::unordered_map<uint64_t, Sector *> SectorTable;
117 
118  protected:
119  std::string filename;
122 
123  public:
124  typedef CowDiskImageParams Params;
125  CowDiskImage(const Params *p);
126  ~CowDiskImage();
127 
128  void notifyFork() override;
129 
130  void initSectorTable(int hash_size);
131  bool open(const std::string &file);
132  void save() const;
133  void save(const std::string &file) const;
134  void writeback();
135 
136  void serialize(CheckpointOut &cp) const override;
137  void unserialize(CheckpointIn &cp) override;
138 
139  std::streampos size() const override;
140 
141  std::streampos read(uint8_t *data, std::streampos offset) const override;
142  std::streampos write(const uint8_t *data, std::streampos offset) override;
143 };
144 
145 void SafeRead(std::ifstream &stream, void *data, int count);
146 
147 template<class T>
148 void SafeRead(std::ifstream &stream, T &data);
149 
150 template<class T>
151 void SafeReadSwap(std::ifstream &stream, T &data);
152 
153 void SafeWrite(std::ofstream &stream, const void *data, int count);
154 
155 template<class T>
156 void SafeWrite(std::ofstream &stream, const T &data);
157 
158 template<class T>
159 void SafeWriteSwap(std::ofstream &stream, const T &data);
160 
161 #endif // __DEV_STORAGE_DISK_IMAGE_HH__
count
Definition: misc.hh:704
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:413
std::streampos size() const override
Definition: disk_image.cc:102
DiskImage(const Params *p)
Definition: disk_image.hh:58
CowDiskImage(const Params *p)
Definition: disk_image.cc:183
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:115
bool initialized
Definition: disk_image.hh:54
void SafeWriteSwap(std::ofstream &stream, const T &data)
std::streampos size() const override
Definition: disk_image.cc:389
Bitfield< 23, 0 > offset
Definition: types.hh:149
Specialization for accessing a copy-on-write disk image layer.
Definition: disk_image.hh:106
static const uint32_t VersionMinor
Definition: disk_image.hh:110
const char data[]
Definition: circlebuf.cc:43
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: disk_image.cc:437
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:68
virtual std::streampos size() const =0
std::string filename
Definition: disk_image.hh:119
void close()
Definition: disk_image.cc:96
void save() const
Definition: disk_image.cc:333
std::unordered_map< uint64_t, Sector * > SectorTable
Definition: disk_image.hh:116
RawDiskImageParams Params
Definition: disk_image.hh:81
std::string file
Definition: disk_image.hh:76
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
void SafeWrite(std::ofstream &stream, const void *data, int count)
#define SectorSize
Definition: disk_image.hh:46
Basic interface for accessing a disk image.
Definition: disk_image.hh:51
void open(const std::string &filename, bool rd_only=false)
Definition: disk_image.cc:79
std::streampos disk_size
Definition: disk_image.hh:78
RawDiskImage(const Params *p)
Definition: disk_image.cc:60
void SafeReadSwap(std::ifstream &stream, T &data)
static const uint32_t VersionMajor
Definition: disk_image.hh:109
CowDiskImageParams Params
Definition: disk_image.hh:124
void SafeRead(std::ifstream &stream, void *data, int count)
std::ostream CheckpointOut
Definition: serialize.hh:67
void writeback()
Definition: disk_image.cc:377
uint8_t data[SectorSize]
Definition: disk_image.hh:114
DiskImage * child
Definition: disk_image.hh:120
std::streampos read(uint8_t *data, std::streampos offset) const override
Definition: disk_image.cc:393
SectorTable * table
Definition: disk_image.hh:121
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
std::fstream stream
Definition: disk_image.hh:75
std::streampos write(const uint8_t *data, std::streampos offset) override
Definition: disk_image.cc:137
void initSectorTable(int hash_size)
Definition: disk_image.cc:297
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: disk_image.cc:445
virtual ~DiskImage()
Definition: disk_image.hh:59
bool open(const std::string &file)
Definition: disk_image.cc:251
Specialization for accessing a raw disk image.
Definition: disk_image.hh:72
DiskImageParams Params
Definition: disk_image.hh:57
Bitfield< 0 > p
Abstract superclass for simulation objects.
Definition: sim_object.hh:94
void notifyFork() override
Notify a child process of a fork.
Definition: disk_image.cc:212

Generated on Fri Jun 9 2017 13:03:47 for gem5 by doxygen 1.8.6