gem5  v21.1.0.2
memhelpers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, 2019 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2011 Google
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #ifndef __ARCH_GENERIC_MEMHELPERS_HH__
42 #define __ARCH_GENERIC_MEMHELPERS_HH__
43 
44 #include "base/types.hh"
45 #include "mem/packet.hh"
46 #include "mem/request.hh"
47 #include "sim/byteswap.hh"
48 #include "sim/insttracer.hh"
49 
50 namespace gem5
51 {
52 
53 template <class XC>
54 Fault
55 initiateMemRead(XC *xc, Addr addr, std::size_t size,
56  Request::Flags flags,
57  const std::vector<bool> &byte_enable)
58 {
59  return xc->initiateMemRead(addr, size, flags, byte_enable);
60 }
61 
65 template <class XC, class MemT>
66 Fault
68  MemT &mem, Request::Flags flags)
69 {
70  static const std::vector<bool> byte_enable(sizeof(MemT), true);
71  return initiateMemRead(xc, addr, sizeof(MemT),
72  flags, byte_enable);
73 }
74 
76 template <ByteOrder Order, class MemT>
77 void
78 getMem(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
79 {
80  mem = pkt->get<MemT>(Order);
81  if (traceData)
82  traceData->setData(mem);
83 }
84 
85 template <class MemT>
86 void
87 getMemLE(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
88 {
89  getMem<ByteOrder::little>(pkt, mem, traceData);
90 }
91 
92 template <class MemT>
93 void
94 getMemBE(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
95 {
96  getMem<ByteOrder::big>(pkt, mem, traceData);
97 }
98 
100 template <class XC>
101 Fault
102 readMemAtomic(XC *xc, Addr addr, uint8_t *mem,
103  std::size_t size, Request::Flags flags,
104  const std::vector<bool> &byte_enable)
105 {
106  return xc->readMem(addr, mem, size, flags, byte_enable);
107 }
108 
110 template <ByteOrder Order, class XC, class MemT>
111 Fault
112 readMemAtomic(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem,
113  Request::Flags flags)
114 {
115  memset(&mem, 0, sizeof(mem));
116  static const std::vector<bool> byte_enable(sizeof(MemT), true);
117  Fault fault = readMemAtomic(xc, addr, (uint8_t*)&mem,
118  sizeof(MemT), flags, byte_enable);
119  if (fault == NoFault) {
120  mem = gtoh(mem, Order);
121  if (traceData)
122  traceData->setData(mem);
123  }
124  return fault;
125 }
126 
127 template <class XC, class MemT>
128 Fault
129 readMemAtomicLE(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem,
130  Request::Flags flags)
131 {
132  return readMemAtomic<ByteOrder::little>(
133  xc, traceData, addr, mem, flags);
134 }
135 
136 template <class XC, class MemT>
137 Fault
138 readMemAtomicBE(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem,
139  Request::Flags flags)
140 {
141  return readMemAtomic<ByteOrder::big>(xc, traceData, addr, mem, flags);
142 }
143 
145 template <class XC>
146 Fault
147 writeMemTiming(XC *xc, uint8_t *mem, Addr addr,
148  std::size_t size, Request::Flags flags, uint64_t *res,
149  const std::vector<bool> &byte_enable)
150 {
151  return xc->writeMem(mem, size, addr, flags, res, byte_enable);
152 }
153 
154 template <ByteOrder Order, class XC, class MemT>
155 Fault
156 writeMemTiming(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr,
157  Request::Flags flags, uint64_t *res)
158 {
159  if (traceData) {
160  traceData->setData(mem);
161  }
162  mem = htog(mem, Order);
163  static const std::vector<bool> byte_enable(sizeof(MemT), true);
164  return writeMemTiming(xc, (uint8_t*)&mem, addr,
165  sizeof(MemT), flags, res, byte_enable);
166 }
167 
168 template <class XC, class MemT>
169 Fault
170 writeMemTimingLE(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr,
171  Request::Flags flags, uint64_t *res)
172 {
173  return writeMemTiming<ByteOrder::little>(
174  xc, traceData, mem, addr, flags, res);
175 }
176 
177 template <class XC, class MemT>
178 Fault
179 writeMemTimingBE(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr,
180  Request::Flags flags, uint64_t *res)
181 {
182  return writeMemTiming<ByteOrder::big>(
183  xc, traceData, mem, addr, flags, res);
184 }
185 
187 template <class XC>
188 Fault
189 writeMemAtomic(XC *xc, uint8_t *mem, Addr addr,
190  std::size_t size, Request::Flags flags,
191  uint64_t *res, const std::vector<bool> &byte_enable)
192 {
193  return xc->writeMem(mem, size, addr, flags, res, byte_enable);
194 }
195 
196 template <ByteOrder Order, class XC, class MemT>
197 Fault
198 writeMemAtomic(XC *xc, Trace::InstRecord *traceData, const MemT &mem,
199  Addr addr, Request::Flags flags, uint64_t *res)
200 {
201  if (traceData) {
202  traceData->setData(mem);
203  }
204  MemT host_mem = htog(mem, Order);
205  static const std::vector<bool> byte_enable(sizeof(MemT), true);
206  Fault fault = writeMemAtomic(xc, (uint8_t*)&host_mem,
207  addr, sizeof(MemT), flags, res, byte_enable);
208  if (fault == NoFault && res != NULL) {
209  if (flags & Request::MEM_SWAP || flags & Request::MEM_SWAP_COND)
210  *(MemT *)res = gtoh(*(MemT *)res, Order);
211  else
212  *res = gtoh(*res, Order);
213  }
214  return fault;
215 }
216 
217 template <class XC, class MemT>
218 Fault
219 writeMemAtomicLE(XC *xc, Trace::InstRecord *traceData, const MemT &mem,
220  Addr addr, Request::Flags flags, uint64_t *res)
221 {
222  return writeMemAtomic<ByteOrder::little>(
223  xc, traceData, mem, addr, flags, res);
224 }
225 
226 template <class XC, class MemT>
227 Fault
228 writeMemAtomicBE(XC *xc, Trace::InstRecord *traceData, const MemT &mem,
229  Addr addr, Request::Flags flags, uint64_t *res)
230 {
231  return writeMemAtomic<ByteOrder::big>(
232  xc, traceData, mem, addr, flags, res);
233 }
234 
236 template <ByteOrder Order, class XC, class MemT>
237 Fault
238 amoMemAtomic(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr,
239  Request::Flags flags, AtomicOpFunctor *_amo_op)
240 {
241  assert(_amo_op);
242 
243  // mem will hold the previous value at addr after the AMO completes
244  memset(&mem, 0, sizeof(mem));
245 
246  AtomicOpFunctorPtr amo_op = AtomicOpFunctorPtr(_amo_op);
247  Fault fault = xc->amoMem(addr, (uint8_t *)&mem, sizeof(MemT), flags,
248  std::move(amo_op));
249 
250  if (fault == NoFault) {
251  mem = gtoh(mem, Order);
252  if (traceData)
253  traceData->setData(mem);
254  }
255  return fault;
256 }
257 
258 template <class XC, class MemT>
259 Fault
260 amoMemAtomicLE(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr,
261  Request::Flags flags, AtomicOpFunctor *_amo_op)
262 {
263  return amoMemAtomic<ByteOrder::little>(
264  xc, traceData, mem, addr, flags, _amo_op);
265 }
266 
267 template <class XC, class MemT>
268 Fault
269 amoMemAtomicBE(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr,
270  Request::Flags flags, AtomicOpFunctor *_amo_op)
271 {
272  return amoMemAtomic<ByteOrder::big>(
273  xc, traceData, mem, addr, flags, _amo_op);
274 }
275 
277 template <class XC, class MemT>
278 Fault
279 initiateMemAMO(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT& mem,
280  Request::Flags flags, AtomicOpFunctor *_amo_op)
281 {
282  assert(_amo_op);
283  AtomicOpFunctorPtr amo_op = AtomicOpFunctorPtr(_amo_op);
284  return xc->initiateMemAMO(addr, sizeof(MemT), flags, std::move(amo_op));
285 }
286 
287 } // namespace gem5
288 
289 #endif
gem5::NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:260
gem5::initiateMemRead
Fault initiateMemRead(XC *xc, Addr addr, std::size_t size, Request::Flags flags, const std::vector< bool > &byte_enable)
Definition: memhelpers.hh:55
gem5::readMemAtomic
Fault readMemAtomic(XC *xc, Addr addr, uint8_t *mem, std::size_t size, Request::Flags flags, const std::vector< bool > &byte_enable)
Read from memory in atomic mode.
Definition: memhelpers.hh:102
gem5::AtomicOpFunctor
Definition: amo.hh:43
gem5::readMemAtomicLE
Fault readMemAtomicLE(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem, Request::Flags flags)
Definition: memhelpers.hh:129
insttracer.hh
gem5::writeMemAtomicLE
Fault writeMemAtomicLE(XC *xc, Trace::InstRecord *traceData, const MemT &mem, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:219
gem5::getMemBE
void getMemBE(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
Definition: memhelpers.hh:94
gem5::initiateMemAMO
Fault initiateMemAMO(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem, Request::Flags flags, AtomicOpFunctor *_amo_op)
Do atomic read-modify-wrote (AMO) in timing mode.
Definition: memhelpers.hh:279
gem5::Request::MEM_SWAP
@ MEM_SWAP
This request is for a memory swap.
Definition: request.hh:158
gem5::writeMemTimingLE
Fault writeMemTimingLE(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:170
gem5::Trace::InstRecord::setData
void setData(std::array< T, N > d)
Definition: insttracer.hh:187
gem5::writeMemTimingBE
Fault writeMemTimingBE(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:179
std::vector< bool >
gem5::amoMemAtomic
Fault amoMemAtomic(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr, Request::Flags flags, AtomicOpFunctor *_amo_op)
Do atomic read-modify-write (AMO) in atomic mode.
Definition: memhelpers.hh:238
request.hh
packet.hh
gem5::Flags< FlagsType >
gem5::writeMemAtomic
Fault writeMemAtomic(XC *xc, uint8_t *mem, Addr addr, std::size_t size, Request::Flags flags, uint64_t *res, const std::vector< bool > &byte_enable)
Write to memory in atomic mode.
Definition: memhelpers.hh:189
gem5::Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:255
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:283
gem5::gtoh
T gtoh(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:194
gem5::htog
T htog(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:187
gem5::writeMemAtomicBE
Fault writeMemAtomicBE(XC *xc, Trace::InstRecord *traceData, const MemT &mem, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:228
gem5::amoMemAtomicBE
Fault amoMemAtomicBE(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr, Request::Flags flags, AtomicOpFunctor *_amo_op)
Definition: memhelpers.hh:269
gem5::amoMemAtomicLE
Fault amoMemAtomicLE(XC *xc, Trace::InstRecord *traceData, MemT &mem, Addr addr, Request::Flags flags, AtomicOpFunctor *_amo_op)
Definition: memhelpers.hh:260
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::readMemAtomicBE
Fault readMemAtomicBE(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem, Request::Flags flags)
Definition: memhelpers.hh:138
gem5::Request::MEM_SWAP_COND
@ MEM_SWAP_COND
Definition: request.hh:159
gem5::Packet::get
T get(ByteOrder endian) const
Get the data in the packet byte swapped from the specified endianness.
Definition: packet_access.hh:85
types.hh
gem5::getMemLE
void getMemLE(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
Definition: memhelpers.hh:87
mem
bool_vector8 mem[]
Definition: reset_stim.h:43
gem5::Trace::InstRecord
Definition: insttracer.hh:58
gem5::AtomicOpFunctorPtr
std::unique_ptr< AtomicOpFunctor > AtomicOpFunctorPtr
Definition: amo.hh:242
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::writeMemTiming
Fault writeMemTiming(XC *xc, uint8_t *mem, Addr addr, std::size_t size, Request::Flags flags, uint64_t *res, const std::vector< bool > &byte_enable)
Write to memory in timing mode.
Definition: memhelpers.hh:147
byteswap.hh
gem5::getMem
void getMem(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData)
Extract the data returned from a timing mode read.
Definition: memhelpers.hh:78
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Tue Sep 21 2021 12:24:47 for gem5 by doxygen 1.8.17