tests: log_call is not returning any value
[gem5.git] / src / base / loader / memory_image.hh
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/loader/image_file_data.hh"
40 #include "base/logging.hh"
41 #include "base/types.hh"
42
43 class PortProxy;
44
45 namespace Loader
46 {
47
48 class MemoryImage
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
75 ImageFileDataPtr ifd;
76 std::string name;
77 Addr base = 0;
78 const uint8_t *data = nullptr;
79 size_t size = 0;
80 };
81
82 MemoryImage() {}
83
84 MemoryImage(const Segment &seg)
85 {
86 addSegment(seg);
87 }
88
89 MemoryImage(std::initializer_list<Segment> segs)
90 {
91 addSegments(segs);
92 }
93
94 private:
95 std::vector<Segment> _segments;
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
106 addSegment(const Segment &seg)
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 &
121 offset(Addr by)
122 {
123 return move([by](Addr a){ return by + a; });
124 }
125 MemoryImage &
126 mask(Addr m) {
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
149 contains(Addr addr) const
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__