gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "sim/byteswap.hh"
39 #include "sim/insttracer.hh"
40 
41 namespace X86ISA
42 {
43 
45 template <class XC>
46 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->get<uint8_t>();
60  break;
61  case 2:
62  mem = pkt->get<uint16_t>();
63  break;
64  case 4:
65  mem = pkt->get<uint32_t>();
66  break;
67  case 8:
68  mem = pkt->get<uint64_t>();
69  break;
70  default:
71  panic("Unhandled size in getMem.\n");
72  }
73  if (traceData)
74  traceData->setData(mem);
75 }
76 
77 
78 template <size_t N>
79 void
80 getMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize,
81  Trace::InstRecord *traceData)
82 {
83  assert(dataSize >= 8);
84  assert((dataSize % 8) == 0);
85 
86  int num_words = dataSize / 8;
87  assert(num_words <= N);
88 
89  auto pkt_data = pkt->getConstPtr<const uint64_t>();
90  for (int i = 0; i < num_words; ++i)
91  mem[i] = gtoh(pkt_data[i]);
92 
93  // traceData record only has space for 64 bits, so we just record
94  // the first qword
95  if (traceData)
96  traceData->setData(mem[0]);
97 }
98 
99 
100 template <class XC>
101 Fault
102 readMemAtomic(XC *xc, Trace::InstRecord *traceData, Addr addr, uint64_t &mem,
103  unsigned dataSize, Request::Flags flags)
104 {
105  memset(&mem, 0, sizeof(mem));
106  Fault fault = xc->readMem(addr, (uint8_t *)&mem, dataSize, flags);
107  if (fault == NoFault) {
108  // If LE to LE, this is a nop, if LE to BE, the actual data ends up
109  // in the right place because the LSBs where at the low addresses on
110  // access. This doesn't work for BE guests.
111  mem = gtoh(mem);
112  if (traceData)
113  traceData->setData(mem);
114  }
115  return fault;
116 }
117 
118 template <class XC, size_t N>
119 Fault
121  std::array<uint64_t, N> &mem, unsigned dataSize,
122  unsigned flags)
123 {
124  assert(dataSize >= 8);
125  assert((dataSize % 8) == 0);
126 
127  Fault fault = xc->readMem(addr, (uint8_t *)&mem, dataSize, flags);
128 
129  if (fault == NoFault) {
130  int num_words = dataSize / 8;
131  assert(num_words <= N);
132 
133  for (int i = 0; i < num_words; ++i)
134  mem[i] = gtoh(mem[i]);
135 
136  if (traceData)
137  traceData->setData(mem[0]);
138  }
139  return fault;
140 }
141 
142 template <class XC>
143 Fault
144 writeMemTiming(XC *xc, Trace::InstRecord *traceData, uint64_t mem,
145  unsigned dataSize, Addr addr, Request::Flags flags,
146  uint64_t *res)
147 {
148  if (traceData) {
149  traceData->setData(mem);
150  }
151  mem = TheISA::htog(mem);
152  return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags, res);
153 }
154 
155 template <class XC, size_t N>
156 Fault
158  std::array<uint64_t, N> &mem, unsigned dataSize,
159  Addr addr, unsigned flags, uint64_t *res)
160 {
161  assert(dataSize >= 8);
162  assert((dataSize % 8) == 0);
163 
164  if (traceData) {
165  traceData->setData(mem[0]);
166  }
167 
168  int num_words = dataSize / 8;
169  assert(num_words <= N);
170 
171  for (int i = 0; i < num_words; ++i)
172  mem[i] = htog(mem[i]);
173 
174  return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags, res);
175 }
176 
177 template <class XC>
178 Fault
179 writeMemAtomic(XC *xc, Trace::InstRecord *traceData, uint64_t mem,
180  unsigned dataSize, Addr addr, Request::Flags flags,
181  uint64_t *res)
182 {
183  if (traceData) {
184  traceData->setData(mem);
185  }
186  uint64_t host_mem = TheISA::htog(mem);
187  Fault fault =
188  xc->writeMem((uint8_t *)&host_mem, dataSize, addr, flags, res);
189  if (fault == NoFault && res != NULL) {
190  *res = gtoh(*res);
191  }
192  return fault;
193 }
194 
195 template <class XC, size_t N>
196 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 
205  int num_words = dataSize / 8;
206  assert(num_words <= N);
207 
208  for (int i = 0; i < num_words; ++i)
209  mem[i] = htog(mem[i]);
210 
211  Fault fault = xc->writeMem((uint8_t *)&mem, dataSize, addr, flags, res);
212 
213  if (fault == NoFault && res != NULL) {
214  *res = gtoh(*res);
215  }
216 
217  return fault;
218 }
219 
220 }
221 
222 #endif
T htog(T value)
Definition: byteswap.hh:177
decltype(nullptr) constexpr NoFault
Definition: types.hh:189
Bitfield< 7 > i
Definition: miscregs.hh:1378
#define panic(...)
Definition: misc.hh:153
void setData(Twin64_t d)
Definition: insttracer.hh:162
Fault readMemAtomic(XC *xc, Trace::InstRecord *traceData, Addr addr, uint64_t &mem, unsigned dataSize, Request::Flags flags)
Definition: memhelpers.hh:102
T gtoh(T value)
Definition: byteswap.hh:179
T get(ByteOrder endian) const
Get the data in the packet byte swapped from the specified endianness.
Fault writeMemTiming(XC *xc, Trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:144
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:245
static void getMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize, Trace::InstRecord *traceData)
Definition: memhelpers.hh:54
const T * getConstPtr() const
Definition: packet.hh:967
Fault writeMemAtomic(XC *xc, Trace::InstRecord *traceData, uint64_t mem, unsigned dataSize, Addr addr, Request::Flags flags, uint64_t *res)
Definition: memhelpers.hh:179
std::shared_ptr< FaultBase > Fault
Definition: types.hh:184
Bitfield< 3 > addr
Definition: types.hh:81
Fault initiateMemRead(XC *xc, Trace::InstRecord *traceData, Addr addr, unsigned dataSize, Request::Flags flags)
Initiate a read from memory in timing mode.
Definition: memhelpers.hh:47

Generated on Fri Jun 9 2017 13:03:39 for gem5 by doxygen 1.8.6