gem5  v22.1.0.0
memhelpers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Google
3  * Copyright (c) 2015 Advanced Micro Devices, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __ARCH_X86_MEMHELPERS_HH__
31 #define __ARCH_X86_MEMHELPERS_HH__
32 
33 #include <array>
34 
35 #include "base/types.hh"
36 #include "cpu/exec_context.hh"
37 #include "sim/byteswap.hh"
38 #include "sim/insttracer.hh"
39 
40 namespace gem5
41 {
42 
43 namespace X86ISA
44 {
45 
47 static Fault
49  unsigned dataSize, Request::Flags flags)
50 {
51  const std::vector<bool> byte_enable(dataSize, true);
52  return xc->initiateMemRead(addr, dataSize, flags, byte_enable);
53 }
54 
55 static void
56 getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize,
57  trace::InstRecord *traceData)
58 {
59  switch (dataSize) {
60  case 1:
61  mem = pkt->getLE<uint8_t>();
62  break;
63  case 2:
64  mem = pkt->getLE<uint16_t>();
65  break;
66  case 4:
67  mem = pkt->getLE<uint32_t>();
68  break;
69  case 8:
70  mem = pkt->getLE<uint64_t>();
71  break;
72  default:
73  panic("Unhandled size in getMem.\n");
74  }
75  if (traceData)
76  traceData->setData(mem);
77 }
78 
79 template <typename T, size_t N>
80 static void
81 getPackedMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize)
82 {
83  std::array<T, N> real_mem = pkt->getLE<std::array<T, N> >();
84  for (int i = 0; i < N; i++)
85  mem[i] = real_mem[i];
86 }
87 
88 template <size_t N>
89 static void
90 getMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize,
91  trace::InstRecord *traceData)
92 {
93  switch (dataSize) {
94  case 4:
95  getPackedMem<uint32_t, N>(pkt, mem, dataSize);
96  break;
97  case 8:
98  getPackedMem<uint64_t, N>(pkt, mem, dataSize);
99  break;
100  default:
101  panic("Unhandled element size in getMem.\n");
102  }
103  if (traceData)
104  traceData->setData(mem[0]);
105 }
106 
107 
108 static Fault
110  uint64_t &mem, unsigned dataSize, Request::Flags flags)
111 {
112  memset(&mem, 0, sizeof(mem));
113  const std::vector<bool> byte_enable(dataSize, true);
114  Fault fault = xc->readMem(addr, (uint8_t *)&mem, dataSize,
115  flags, byte_enable);
116  if (fault == NoFault) {
117  // If LE to LE, this is a nop, if LE to BE, the actual data ends up
118  // in the right place because the LSBs where at the low addresses on
119  // access. This doesn't work for BE guests.
120  mem = letoh(mem);
121  if (traceData)
122  traceData->setData(mem);
123  }
124  return fault;
125 }
126 
127 template <typename T, size_t N>
128 static Fault
129 readPackedMemAtomic(ExecContext *xc, Addr addr, std::array<uint64_t, N> &mem,
130  unsigned flags)
131 {
132  std::array<T, N> real_mem;
133  // Size is fixed at compilation time. Make a static vector.
134  constexpr auto size = sizeof(T) * N;
135  static const std::vector<bool> byte_enable(size, true);
136  Fault fault = xc->readMem(addr, (uint8_t *)&real_mem,
137  size, flags, byte_enable);
138  if (fault == NoFault) {
139  real_mem = letoh(real_mem);
140  for (int i = 0; i < N; i++)
141  mem[i] = real_mem[i];
142  }
143  return fault;
144 }
145 
146 template <size_t N>
147 static Fault
149  std::array<uint64_t, N> &mem, unsigned dataSize,
150  unsigned flags)
151 {
152  Fault fault = NoFault;
153 
154  switch (dataSize) {
155  case 4:
156  fault = readPackedMemAtomic<uint32_t, N>(xc, addr, mem, flags);
157  break;
158  case 8:
159  fault = readPackedMemAtomic<uint64_t, N>(xc, addr, mem, flags);
160  break;
161  default:
162  panic("Unhandled element size in readMemAtomic\n");
163  }
164  if (fault == NoFault && traceData)
165  traceData->setData(mem[0]);
166  return fault;
167 }
168 
169 template <typename T, size_t N>
170 static Fault
171 writePackedMem(ExecContext *xc, std::array<uint64_t, N> &mem, Addr addr,
172  unsigned flags, uint64_t *res)
173 {
174  std::array<T, N> real_mem;
175  for (int i = 0; i < N; i++)
176  real_mem[i] = mem[i];
177  real_mem = htole(real_mem);
178  // Size is fixed at compilation time. Make a static vector.
179  constexpr auto size = sizeof(T) * N;
180  static const std::vector<bool> byte_enable(size, true);
181  return xc->writeMem((uint8_t *)&real_mem, size,
182  addr, flags, res, byte_enable);
183 }
184 
185 static Fault
187  unsigned dataSize, Addr addr, Request::Flags flags,
188  uint64_t *res)
189 {
190  if (traceData)
191  traceData->setData(mem);
192  mem = htole(mem);
193  const std::vector<bool> byte_enable(dataSize, true);
194  return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags,
195  res, byte_enable);
196 }
197 
198 template <size_t N>
199 static Fault
201  std::array<uint64_t, N> &mem, unsigned dataSize,
202  Addr addr, unsigned flags, uint64_t *res)
203 {
204  if (traceData)
205  traceData->setData(mem[0]);
206 
207  switch (dataSize) {
208  case 4:
209  return writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
210  case 8:
211  return writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
212  default:
213  panic("Unhandled element size in writeMemTiming.\n");
214  }
215 }
216 
217 static Fault
219  unsigned dataSize, Addr addr, Request::Flags flags,
220  uint64_t *res)
221 {
222  if (traceData)
223  traceData->setData(mem);
224  uint64_t host_mem = htole(mem);
225  const std::vector<bool> byte_enable(dataSize, true);
226  Fault fault = xc->writeMem((uint8_t *)&host_mem, dataSize, addr,
227  flags, res, byte_enable);
228  if (fault == NoFault && res)
229  *res = letoh(*res);
230  return fault;
231 }
232 
233 template <size_t N>
234 static Fault
236  std::array<uint64_t, N> &mem, unsigned dataSize,
237  Addr addr, unsigned flags, uint64_t *res)
238 {
239  if (traceData)
240  traceData->setData(mem[0]);
241 
242  Fault fault;
243  switch (dataSize) {
244  case 4:
245  fault = writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
246  break;
247  case 8:
248  fault = writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
249  break;
250  default:
251  panic("Unhandled element size in writeMemAtomic.\n");
252  }
253 
254  if (fault == NoFault && res)
255  *res = letoh(*res);
256 
257  return fault;
258 }
259 
260 } // namespace X86ISA
261 } // namespace gem5
262 
263 #endif
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
The ExecContext is an abstract base class the provides the interface used by the ISA to manipulate th...
Definition: exec_context.hh:72
virtual Fault initiateMemRead(Addr addr, unsigned int size, Request::Flags flags, const std::vector< bool > &byte_enable)
Initiate a timing memory read operation.
virtual Fault readMem(Addr addr, uint8_t *data, unsigned int size, Request::Flags flags, const std::vector< bool > &byte_enable)
Perform an atomic memory read operation.
virtual Fault writeMem(uint8_t *data, unsigned int size, Addr addr, Request::Flags flags, uint64_t *res, const std::vector< bool > &byte_enable)=0
For atomic-mode contexts, perform an atomic memory write operation.
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
void setData(std::array< T, N > d)
Definition: insttracer.hh:184
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
uint8_t flags
Definition: helpers.cc:66
Bitfield< 7 > i
Definition: misc_types.hh:67
static void getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize, trace::InstRecord *traceData)
Definition: memhelpers.hh:56
static Fault writePackedMem(ExecContext *xc, std::array< uint64_t, N > &mem, Addr addr, unsigned flags, uint64_t *res)
Definition: memhelpers.hh:171
static Fault writeMemAtomic(ExecContext *xc, trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:218
Bitfield< 3 > addr
Definition: types.hh:84
static Fault readPackedMemAtomic(ExecContext *xc, Addr addr, std::array< uint64_t, N > &mem, unsigned flags)
Definition: memhelpers.hh:129
static void getPackedMem(PacketPtr pkt, std::array< uint64_t, N > &mem, unsigned dataSize)
Definition: memhelpers.hh:81
static Fault readMemAtomic(ExecContext *xc, trace::InstRecord *traceData, Addr addr, uint64_t &mem, unsigned dataSize, Request::Flags flags)
Definition: memhelpers.hh:109
static Fault initiateMemRead(ExecContext *xc, trace::InstRecord *traceData, Addr addr, unsigned dataSize, Request::Flags flags)
Initiate a read from memory in timing mode.
Definition: memhelpers.hh:48
static Fault writeMemTiming(ExecContext *xc, trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:186
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
std::shared_ptr< FaultBase > Fault
Definition: types.hh:248
T letoh(T value)
Definition: byteswap.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
T htole(T value)
Definition: byteswap.hh:172
constexpr decltype(nullptr) NoFault
Definition: types.hh:253
bool_vector8 mem[]
Definition: reset_stim.h:43

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