gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
40namespace gem5
41{
42
43namespace X86ISA
44{
45
47static 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
55static void
56getMem(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
79template <typename T, size_t N>
80static void
81getPackedMem(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
88template <size_t N>
89static void
90getMem(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
108static 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
127template <typename T, size_t N>
128static Fault
129readPackedMemAtomic(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
146template <size_t N>
147static 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:
157 break;
158 case 8:
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
169template <typename T, size_t N>
170static Fault
171writePackedMem(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
185static 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
198template <size_t N>
199static 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
217static 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
233template <size_t N>
234static 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...
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:295
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
void setData(std::array< T, N > d)
STL vector class.
Definition stl.hh:37
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
uint8_t flags
Definition helpers.cc:87
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)
static Fault writeMemAtomic(ExecContext *xc, trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Bitfield< 3 > addr
Definition types.hh:84
static Fault readPackedMemAtomic(ExecContext *xc, Addr addr, std::array< uint64_t, N > &mem, unsigned flags)
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)
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)
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
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 Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0