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

Generated on Fri Feb 28 2020 16:26:57 for gem5 by doxygen 1.8.13