gem5  v20.1.0.0
memory_image.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2004 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 
29 #ifndef __BASE_LOADER_MEMORY_IMAGE_HH__
30 #define __BASE_LOADER_MEMORY_IMAGE_HH__
31 
32 #include <algorithm>
33 #include <functional>
34 #include <initializer_list>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
40 #include "base/logging.hh"
41 #include "base/types.hh"
42 
43 class PortProxy;
44 
45 namespace Loader
46 {
47 
49 {
50  public:
51  struct Segment
52  {
53  Segment(const std::string &_name, Addr _base,
54  const uint8_t *_data, size_t _size) :
55  name(_name), base(_base), data(_data), size(_size)
56  {}
57 
58  Segment(const std::string &_name, Addr _base, size_t _size) :
59  name(_name), base(_base), size(_size)
60  {}
61 
62  Segment(const std::string &_name, Addr _base,
63  const ImageFileDataPtr &_ifd, Addr offset, size_t _size) :
64  ifd(_ifd), name(_name), base(_base), size(_size)
65  {
66  panic_if(offset + size > ifd->len(),
67  "Segment outside the bounds of the image data");
68  data = ifd->data() + offset;
69  }
70 
71  Segment(const std::string &_name, const ImageFileDataPtr &_ifd) :
72  Segment(_name, 0, _ifd, 0, _ifd->len())
73  {}
74 
76  std::string name;
77  Addr base = 0;
78  const uint8_t *data = nullptr;
79  size_t size = 0;
80  };
81 
83 
85  {
86  addSegment(seg);
87  }
88 
89  MemoryImage(std::initializer_list<Segment> segs)
90  {
91  addSegments(segs);
92  }
93 
94  private:
96  bool writeSegment(const Segment &seg, const PortProxy &proxy) const;
97 
98  public:
99  const std::vector<Segment> &
100  segments() const
101  {
102  return _segments;
103  }
104 
105  void
107  {
108  _segments.emplace_back(seg);
109  }
110 
111  void
112  addSegments(std::initializer_list<Segment> segs)
113  {
114  for (auto &seg: segs)
115  addSegment(seg);
116  }
117 
118  bool write(const PortProxy &proxy) const;
119  MemoryImage &move(std::function<Addr(Addr)> mapper);
120  MemoryImage &
122  {
123  return move([by](Addr a){ return by + a; });
124  }
125  MemoryImage &
127  return move([m](Addr a) { return a & m; });
128  }
129 
130  Addr
131  maxAddr() const
132  {
133  Addr max = 0;
134  for (auto &seg: _segments)
135  max = std::max(max, seg.base + seg.size);
136  return max;
137  }
138 
139  Addr
140  minAddr() const
141  {
142  Addr min = MaxAddr;
143  for (auto &seg: _segments)
144  min = std::min(min, seg.base);
145  return min;
146  }
147 
148  bool
150  {
151  for (auto &seg: _segments) {
152  Addr start = seg.base;
153  Addr end = seg.base + seg.size;
154  if (addr >= start && addr < end)
155  return true;
156  }
157  return false;
158  }
159 };
160 
161 static inline std::ostream &
162 operator << (std::ostream &os, const MemoryImage::Segment &seg)
163 {
164  ccprintf(os, "%s: %#x %d", seg.name, seg.base, seg.size);
165  return os;
166 }
167 
168 } // namespace Loader
169 
170 #endif // __BASE_LOADER_MEMORY_IMAGE_HH__
Loader::MemoryImage::Segment::data
const uint8_t * data
Definition: memory_image.hh:78
Loader::MemoryImage::offset
MemoryImage & offset(Addr by)
Definition: memory_image.hh:121
Loader::MemoryImage::Segment::Segment
Segment(const std::string &_name, const ImageFileDataPtr &_ifd)
Definition: memory_image.hh:71
Loader::MemoryImage::contains
bool contains(Addr addr) const
Definition: memory_image.hh:149
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
Loader::MemoryImage::write
bool write(const PortProxy &proxy) const
Definition: memory_image.cc:50
Loader::MemoryImage::move
MemoryImage & move(std::function< Addr(Addr)> mapper)
Definition: memory_image.cc:59
Loader::MemoryImage::MemoryImage
MemoryImage(const Segment &seg)
Definition: memory_image.hh:84
Loader::MemoryImage::Segment::Segment
Segment(const std::string &_name, Addr _base, size_t _size)
Definition: memory_image.hh:58
std::vector
STL vector class.
Definition: stl.hh:37
MaxAddr
const Addr MaxAddr
Definition: types.hh:166
Loader::ImageFileDataPtr
std::shared_ptr< ImageFileData > ImageFileDataPtr
Definition: image_file_data.hh:55
image_file_data.hh
Loader::MemoryImage::maxAddr
Addr maxAddr() const
Definition: memory_image.hh:131
Loader::MemoryImage
Definition: memory_image.hh:48
Loader::MemoryImage::Segment::size
size_t size
Definition: memory_image.hh:79
Loader
Definition: process.hh:39
ArmISA::a
Bitfield< 8 > a
Definition: miscregs_types.hh:62
Loader::MemoryImage::_segments
std::vector< Segment > _segments
Definition: memory_image.hh:95
Loader::MemoryImage::Segment::Segment
Segment(const std::string &_name, Addr _base, const uint8_t *_data, size_t _size)
Definition: memory_image.hh:53
Loader::MemoryImage::addSegments
void addSegments(std::initializer_list< Segment > segs)
Definition: memory_image.hh:112
Loader::MemoryImage::MemoryImage
MemoryImage(std::initializer_list< Segment > segs)
Definition: memory_image.hh:89
Loader::MemoryImage::mask
MemoryImage & mask(Addr m)
Definition: memory_image.hh:126
Loader::MemoryImage::writeSegment
bool writeSegment(const Segment &seg, const PortProxy &proxy) const
Definition: memory_image.cc:36
Loader::MemoryImage::Segment::Segment
Segment(const std::string &_name, Addr _base, const ImageFileDataPtr &_ifd, Addr offset, size_t _size)
Definition: memory_image.hh:62
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Loader::MemoryImage::MemoryImage
MemoryImage()
Definition: memory_image.hh:82
Loader::MemoryImage::Segment::name
std::string name
Definition: memory_image.hh:76
Loader::MemoryImage::Segment::ifd
ImageFileDataPtr ifd
Definition: memory_image.hh:75
Loader::MemoryImage::Segment::base
Addr base
Definition: memory_image.hh:77
Loader::MemoryImage::minAddr
Addr minAddr() const
Definition: memory_image.hh:140
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
PortProxy
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:80
Loader::MemoryImage::segments
const std::vector< Segment > & segments() const
Definition: memory_image.hh:100
types.hh
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
addr
ip6_addr_t addr
Definition: inet.hh:423
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
logging.hh
Loader::operator<<
static std::ostream & operator<<(std::ostream &os, const MemoryImage::Segment &seg)
Definition: memory_image.hh:162
X86ISA::seg
Bitfield< 2, 0 > seg
Definition: types.hh:82
ArmISA::m
Bitfield< 0 > m
Definition: miscregs_types.hh:389
Loader::MemoryImage::Segment
Definition: memory_image.hh:51
Loader::MemoryImage::addSegment
void addSegment(const Segment &seg)
Definition: memory_image.hh:106

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