gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 X86ISA
41 {
42 
44 static Fault
46  unsigned dataSize, Request::Flags flags)
47 {
48  const std::vector<bool> byte_enable(dataSize, true);
49  return xc->initiateMemRead(addr, dataSize, flags, byte_enable);
50 }
51 
52 static void
53 getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize,
54  Trace::InstRecord *traceData)
55 {
56  switch (dataSize) {
57  case 1:
58  mem = pkt->getLE<uint8_t>();
59  break;
60  case 2:
61  mem = pkt->getLE<uint16_t>();
62  break;
63  case 4:
64  mem = pkt->getLE<uint32_t>();
65  break;
66  case 8:
67  mem = pkt->getLE<uint64_t>();
68  break;
69  default:
70  panic("Unhandled size in getMem.\n");
71  }
72  if (traceData)
73  traceData->setData(mem);
74 }
75 
76 template <typename T, size_t N>
77 static void
78 getPackedMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize)
79 {
80  std::array<T, N> real_mem = pkt->getLE<std::array<T, N> >();
81  for (int i = 0; i < N; i++)
82  mem[i] = real_mem[i];
83 }
84 
85 template <size_t N>
86 static void
87 getMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize,
88  Trace::InstRecord *traceData)
89 {
90  switch (dataSize) {
91  case 4:
92  getPackedMem<uint32_t, N>(pkt, mem, dataSize);
93  break;
94  case 8:
95  getPackedMem<uint64_t, N>(pkt, mem, dataSize);
96  break;
97  default:
98  panic("Unhandled element size in getMem.\n");
99  }
100  if (traceData)
101  traceData->setData(mem[0]);
102 }
103 
104 
105 static Fault
107  uint64_t &mem, unsigned dataSize, Request::Flags flags)
108 {
109  memset(&mem, 0, sizeof(mem));
110  const std::vector<bool> byte_enable(dataSize, true);
111  Fault fault = xc->readMem(addr, (uint8_t *)&mem, dataSize,
112  flags, byte_enable);
113  if (fault == NoFault) {
114  // If LE to LE, this is a nop, if LE to BE, the actual data ends up
115  // in the right place because the LSBs where at the low addresses on
116  // access. This doesn't work for BE guests.
117  mem = letoh(mem);
118  if (traceData)
119  traceData->setData(mem);
120  }
121  return fault;
122 }
123 
124 template <typename T, size_t N>
125 static Fault
126 readPackedMemAtomic(ExecContext *xc, Addr addr, std::array<uint64_t, N> &mem,
127  unsigned flags)
128 {
129  std::array<T, N> real_mem;
130  // Size is fixed at compilation time. Make a static vector.
131  constexpr auto size = sizeof(T) * N;
132  static const std::vector<bool> byte_enable(size, true);
133  Fault fault = xc->readMem(addr, (uint8_t *)&real_mem,
134  size, flags, byte_enable);
135  if (fault == NoFault) {
136  real_mem = letoh(real_mem);
137  for (int i = 0; i < N; i++)
138  mem[i] = real_mem[i];
139  }
140  return fault;
141 }
142 
143 template <size_t N>
144 static Fault
146  std::array<uint64_t, N> &mem, unsigned dataSize,
147  unsigned flags)
148 {
149  Fault fault = NoFault;
150 
151  switch (dataSize) {
152  case 4:
153  fault = readPackedMemAtomic<uint32_t, N>(xc, addr, mem, flags);
154  break;
155  case 8:
156  fault = readPackedMemAtomic<uint64_t, N>(xc, addr, mem, flags);
157  break;
158  default:
159  panic("Unhandled element size in readMemAtomic\n");
160  }
161  if (fault == NoFault && traceData)
162  traceData->setData(mem[0]);
163  return fault;
164 }
165 
166 template <typename T, size_t N>
167 static Fault
168 writePackedMem(ExecContext *xc, std::array<uint64_t, N> &mem, Addr addr,
169  unsigned flags, uint64_t *res)
170 {
171  std::array<T, N> real_mem;
172  for (int i = 0; i < N; i++)
173  real_mem[i] = mem[i];
174  real_mem = htole(real_mem);
175  // Size is fixed at compilation time. Make a static vector.
176  constexpr auto size = sizeof(T) * N;
177  static const std::vector<bool> byte_enable(size, true);
178  return xc->writeMem((uint8_t *)&real_mem, size,
179  addr, flags, res, byte_enable);
180 }
181 
182 static Fault
184  unsigned dataSize, Addr addr, Request::Flags flags,
185  uint64_t *res)
186 {
187  if (traceData)
188  traceData->setData(mem);
189  mem = htole(mem);
190  const std::vector<bool> byte_enable(dataSize, true);
191  return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags,
192  res, byte_enable);
193 }
194 
195 template <size_t N>
196 static Fault
198  std::array<uint64_t, N> &mem, unsigned dataSize,
199  Addr addr, unsigned flags, uint64_t *res)
200 {
201  if (traceData)
202  traceData->setData(mem[0]);
203 
204  switch (dataSize) {
205  case 4:
206  return writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
207  case 8:
208  return writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
209  default:
210  panic("Unhandled element size in writeMemTiming.\n");
211  }
212 }
213 
214 static Fault
216  unsigned dataSize, Addr addr, Request::Flags flags,
217  uint64_t *res)
218 {
219  if (traceData)
220  traceData->setData(mem);
221  uint64_t host_mem = htole(mem);
222  const std::vector<bool> byte_enable(dataSize, true);
223  Fault fault = xc->writeMem((uint8_t *)&host_mem, dataSize, addr,
224  flags, res, byte_enable);
225  if (fault == NoFault && res)
226  *res = letoh(*res);
227  return fault;
228 }
229 
230 template <size_t N>
231 static Fault
233  std::array<uint64_t, N> &mem, unsigned dataSize,
234  Addr addr, unsigned flags, uint64_t *res)
235 {
236  if (traceData)
237  traceData->setData(mem[0]);
238 
239  Fault fault;
240  switch (dataSize) {
241  case 4:
242  fault = writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
243  break;
244  case 8:
245  fault = writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
246  break;
247  default:
248  panic("Unhandled element size in writeMemAtomic.\n");
249  }
250 
251  if (fault == NoFault && res)
252  *res = letoh(*res);
253 
254  return fault;
255 }
256 
257 }
258 
259 #endif
ExecContext::readMem
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.
Definition: exec_context.hh:228
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
insttracer.hh
Flags< FlagsType >
htole
T htole(T value)
Definition: byteswap.hh:141
Trace::InstRecord
Definition: insttracer.hh:55
X86ISA::writeMemAtomic
static Fault writeMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:215
std::vector< bool >
X86ISA::initiateMemRead
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:45
X86ISA::getPackedMem
static void getPackedMem(PacketPtr pkt, std::array< uint64_t, N > &mem, unsigned dataSize)
Definition: memhelpers.hh:78
X86ISA::getMem
static void getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize, Trace::InstRecord *traceData)
Definition: memhelpers.hh:53
letoh
T letoh(T value)
Definition: byteswap.hh:142
X86ISA::writeMemTiming
static Fault writeMemTiming(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:183
Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:246
X86ISA::readPackedMemAtomic
static Fault readPackedMemAtomic(ExecContext *xc, Addr addr, std::array< uint64_t, N > &mem, unsigned flags)
Definition: memhelpers.hh:126
ExecContext
The ExecContext is an abstract base class the provides the interface used by the ISA to manipulate th...
Definition: exec_context.hh:70
Trace::InstRecord::setData
void setData(std::array< T, N > d)
Definition: insttracer.hh:183
NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:251
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
X86ISA::writePackedMem
static Fault writePackedMem(ExecContext *xc, std::array< uint64_t, N > &mem, Addr addr, unsigned flags, uint64_t *res)
Definition: memhelpers.hh:168
X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:80
Packet::getLE
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
Definition: packet_access.hh:75
types.hh
ExecContext::writeMem
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.
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:258
exec_context.hh
X86ISA::readMemAtomic
static Fault readMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, Addr addr, uint64_t &mem, unsigned dataSize, Request::Flags flags)
Definition: memhelpers.hh:106
ExecContext::initiateMemRead
virtual Fault initiateMemRead(Addr addr, unsigned int size, Request::Flags flags, const std::vector< bool > &byte_enable)
Initiate a timing memory read operation.
Definition: exec_context.hh:242
mem
bool_vector8 mem[]
Definition: reset_stim.h:43
byteswap.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Tue Mar 23 2021 19:41:23 for gem5 by doxygen 1.8.17