2 * Copyright (c) 2011 Google
3 * Copyright (c) 2015 Advanced Micro Devices, Inc.
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.
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.
32 #ifndef __ARCH_X86_MEMHELPERS_HH__
33 #define __ARCH_X86_MEMHELPERS_HH__
37 #include "base/types.hh"
38 #include "cpu/exec_context.hh"
39 #include "sim/byteswap.hh"
40 #include "sim/insttracer.hh"
45 /// Initiate a read from memory in timing mode.
47 initiateMemRead(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
48 unsigned dataSize, Request::Flags flags)
50 return xc->initiateMemRead(addr, dataSize, flags);
54 getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize,
55 Trace::InstRecord *traceData)
59 mem = pkt->getLE<uint8_t>();
62 mem = pkt->getLE<uint16_t>();
65 mem = pkt->getLE<uint32_t>();
68 mem = pkt->getLE<uint64_t>();
71 panic("Unhandled size in getMem.\n");
74 traceData->setData(mem);
77 template <typename T, size_t N>
79 getPackedMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize)
81 std::array<T, N> real_mem = pkt->getLE<std::array<T, N> >();
82 for (int i = 0; i < N; i++)
88 getMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize,
89 Trace::InstRecord *traceData)
93 getPackedMem<uint32_t, N>(pkt, mem, dataSize);
96 getPackedMem<uint64_t, N>(pkt, mem, dataSize);
99 panic("Unhandled element size in getMem.\n");
102 traceData->setData(mem[0]);
107 readMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
108 uint64_t &mem, unsigned dataSize, Request::Flags flags)
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.
118 traceData->setData(mem);
123 template <typename T, size_t N>
125 readPackedMemAtomic(ExecContext *xc, Addr addr, std::array<uint64_t, N> &mem,
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 = gtoh(real_mem);
133 for (int i = 0; i < N; i++)
134 mem[i] = real_mem[i];
141 readMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
142 std::array<uint64_t, N> &mem, unsigned dataSize,
145 Fault fault = NoFault;
149 fault = readPackedMemAtomic<uint32_t, N>(xc, addr, mem, flags);
152 fault = readPackedMemAtomic<uint64_t, N>(xc, addr, mem, flags);
155 panic("Unhandled element size in readMemAtomic\n");
157 if (fault == NoFault && traceData)
158 traceData->setData(mem[0]);
162 template <typename T, size_t N>
164 writePackedMem(ExecContext *xc, std::array<uint64_t, N> &mem, Addr addr,
165 unsigned flags, uint64_t *res)
167 std::array<T, N> real_mem;
168 for (int i = 0; i < N; i++)
169 real_mem[i] = mem[i];
170 real_mem = htog(real_mem);
171 return xc->writeMem((uint8_t *)&real_mem, sizeof(T) * N,
176 writeMemTiming(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem,
177 unsigned dataSize, Addr addr, Request::Flags flags,
181 traceData->setData(mem);
183 return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags, res);
188 writeMemTiming(ExecContext *xc, Trace::InstRecord *traceData,
189 std::array<uint64_t, N> &mem, unsigned dataSize,
190 Addr addr, unsigned flags, uint64_t *res)
193 traceData->setData(mem[0]);
197 return writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
199 return writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
201 panic("Unhandled element size in writeMemTiming.\n");
206 writeMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem,
207 unsigned dataSize, Addr addr, Request::Flags flags,
211 traceData->setData(mem);
212 uint64_t host_mem = htog(mem);
214 xc->writeMem((uint8_t *)&host_mem, dataSize, addr, flags, res);
215 if (fault == NoFault && res)
222 writeMemAtomic(ExecContext *xc, Trace::InstRecord *traceData,
223 std::array<uint64_t, N> &mem, unsigned dataSize,
224 Addr addr, unsigned flags, uint64_t *res)
227 traceData->setData(mem[0]);
232 fault = writePackedMem<uint32_t, N>(xc, mem, addr, flags, res);
235 fault = writePackedMem<uint64_t, N>(xc, mem, addr, flags, res);
238 panic("Unhandled element size in writeMemAtomic.\n");
241 if (fault == NoFault && res)