gem5  v22.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 
39 #include "base/compiler.hh"
41 #include "base/logging.hh"
42 #include "base/types.hh"
43 
44 namespace gem5
45 {
46 
47 class PortProxy;
48 
49 GEM5_DEPRECATED_NAMESPACE(Loader, loader);
50 namespace loader
51 {
52 
54 {
55  public:
56  struct Segment
57  {
58  Segment(const std::string &_name, Addr _base,
59  const uint8_t *_data, size_t _size) :
60  name(_name), base(_base), data(_data), size(_size)
61  {}
62 
63  Segment(const std::string &_name, Addr _base, size_t _size) :
64  name(_name), base(_base), size(_size)
65  {}
66 
67  Segment(const std::string &_name, Addr _base,
68  const ImageFileDataPtr &_ifd, Addr offset, size_t _size) :
69  ifd(_ifd), name(_name), base(_base), size(_size)
70  {
71  panic_if(offset + size > ifd->len(),
72  "Segment outside the bounds of the image data");
73  data = ifd->data() + offset;
74  }
75 
76  Segment(const std::string &_name, const ImageFileDataPtr &_ifd) :
77  Segment(_name, 0, _ifd, 0, _ifd->len())
78  {}
79 
81  std::string name;
82  Addr base = 0;
83  const uint8_t *data = nullptr;
84  size_t size = 0;
85  };
86 
88 
90  {
91  addSegment(seg);
92  }
93 
94  MemoryImage(std::initializer_list<Segment> segs)
95  {
96  addSegments(segs);
97  }
98 
99  private:
101  bool writeSegment(const Segment &seg, const PortProxy &proxy) const;
102 
103  public:
104  const std::vector<Segment> &
105  segments() const
106  {
107  return _segments;
108  }
109 
110  void
112  {
113  _segments.emplace_back(seg);
114  }
115 
116  void
117  addSegments(std::initializer_list<Segment> segs)
118  {
119  for (auto &seg: segs)
120  addSegment(seg);
121  }
122 
123  bool write(const PortProxy &proxy) const;
124  MemoryImage &move(std::function<Addr(Addr)> mapper);
125  MemoryImage &
127  {
128  return move([by](Addr a){ return by + a; });
129  }
130  MemoryImage &
132  return move([m](Addr a) { return a & m; });
133  }
134 
135  Addr
136  maxAddr() const
137  {
138  Addr max = 0;
139  for (auto &seg: _segments)
140  max = std::max(max, seg.base + seg.size);
141  return max;
142  }
143 
144  Addr
145  minAddr() const
146  {
147  Addr min = MaxAddr;
148  for (auto &seg: _segments)
149  min = std::min(min, seg.base);
150  return min;
151  }
152 
153  bool
155  {
156  for (auto &seg: _segments) {
157  Addr start = seg.base;
158  Addr end = seg.base + seg.size;
159  if (addr >= start && addr < end)
160  return true;
161  }
162  return false;
163  }
164 };
165 
166 static inline std::ostream &
167 operator << (std::ostream &os, const MemoryImage::Segment &seg)
168 {
169  ccprintf(os, "%s: %#x %d", seg.name, seg.base, seg.size);
170  return os;
171 }
172 
173 } // namespace loader
174 } // namespace gem5
175 
176 #endif // __BASE_LOADER_MEMORY_IMAGE_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:87
MemoryImage & offset(Addr by)
MemoryImage(const Segment &seg)
Definition: memory_image.hh:89
void addSegments(std::initializer_list< Segment > segs)
MemoryImage & mask(Addr m)
std::vector< Segment > _segments
const std::vector< Segment > & segments() const
void addSegment(const Segment &seg)
bool contains(Addr addr) const
bool write(const PortProxy &proxy) const
Definition: memory_image.cc:54
bool writeSegment(const Segment &seg, const PortProxy &proxy) const
Definition: memory_image.cc:40
MemoryImage(std::initializer_list< Segment > segs)
Definition: memory_image.hh:94
MemoryImage & move(std::function< Addr(Addr)> mapper)
Definition: memory_image.cc:63
STL vector class.
Definition: stl.hh:37
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
uint16_t len
Definition: helpers.cc:62
Bitfield< 8 > a
Definition: misc_types.hh:66
Bitfield< 17 > os
Definition: misc.hh:810
Bitfield< 3 > addr
Definition: types.hh:84
Bitfield< 2, 0 > seg
Definition: types.hh:87
static std::ostream & operator<<(std::ostream &os, const MemoryImage::Segment &seg)
std::shared_ptr< ImageFileData > ImageFileDataPtr
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
const Addr MaxAddr
Definition: types.hh:171
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
Segment(const std::string &_name, Addr _base, size_t _size)
Definition: memory_image.hh:63
Segment(const std::string &_name, const ImageFileDataPtr &_ifd)
Definition: memory_image.hh:76
Segment(const std::string &_name, Addr _base, const ImageFileDataPtr &_ifd, Addr offset, size_t _size)
Definition: memory_image.hh:67
Segment(const std::string &_name, Addr _base, const uint8_t *_data, size_t _size)
Definition: memory_image.hh:58

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