memhelpers.hh revision 12384
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2011 Google
36145Snate@binkert.org * Copyright (c) 2015 Advanced Micro Devices, Inc.
46145Snate@binkert.org * All rights reserved.
56145Snate@binkert.org *
66145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org * modification, are permitted provided that the following conditions are
86145Snate@binkert.org * met: redistributions of source code must retain the above copyright
96145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org * documentation and/or other materials provided with the distribution;
136145Snate@binkert.org * neither the name of the copyright holders nor the names of its
146145Snate@binkert.org * contributors may be used to endorse or promote products derived from
156145Snate@binkert.org * this software without specific prior written permission.
166145Snate@binkert.org *
176145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org *
296145Snate@binkert.org * Authors: Gabe Black
306145Snate@binkert.org */
316284Snate@binkert.org
326145Snate@binkert.org#ifndef __ARCH_X86_MEMHELPERS_HH__
336145Snate@binkert.org#define __ARCH_X86_MEMHELPERS_HH__
346145Snate@binkert.org
356145Snate@binkert.org#include <array>
366145Snate@binkert.org
376145Snate@binkert.org#include "base/types.hh"
386145Snate@binkert.org#include "cpu/exec_context.hh"
396145Snate@binkert.org#include "sim/byteswap.hh"
406154Snate@binkert.org#include "sim/insttracer.hh"
416154Snate@binkert.org
426154Snate@binkert.orgnamespace X86ISA
436154Snate@binkert.org{
446154Snate@binkert.org
456154Snate@binkert.org/// Initiate a read from memory in timing mode.
466285Snate@binkert.orgstatic Fault
476154Snate@binkert.orginitiateMemRead(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
486285Snate@binkert.org                unsigned dataSize, Request::Flags flags)
496145Snate@binkert.org{
506145Snate@binkert.org    return xc->initiateMemRead(addr, dataSize, flags);
516145Snate@binkert.org}
526145Snate@binkert.org
536285Snate@binkert.orgstatic void
546876Ssteve.reinhardt@amd.comgetMem(PacketPtr pkt, uint64_t &mem, unsigned dataSize,
556876Ssteve.reinhardt@amd.com       Trace::InstRecord *traceData)
566145Snate@binkert.org{
576285Snate@binkert.org    switch (dataSize) {
586285Snate@binkert.org      case 1:
596285Snate@binkert.org        mem = pkt->get<uint8_t>();
606285Snate@binkert.org        break;
616285Snate@binkert.org      case 2:
626285Snate@binkert.org        mem = pkt->get<uint16_t>();
636285Snate@binkert.org        break;
646285Snate@binkert.org      case 4:
656285Snate@binkert.org        mem = pkt->get<uint32_t>();
666285Snate@binkert.org        break;
676763SBrad.Beckmann@amd.com      case 8:
686763SBrad.Beckmann@amd.com        mem = pkt->get<uint64_t>();
696876Ssteve.reinhardt@amd.com        break;
706145Snate@binkert.org      default:
716876Ssteve.reinhardt@amd.com        panic("Unhandled size in getMem.\n");
726145Snate@binkert.org    }
736876Ssteve.reinhardt@amd.com    if (traceData)
746145Snate@binkert.org        traceData->setData(mem);
756145Snate@binkert.org}
766145Snate@binkert.org
776145Snate@binkert.orgtemplate <typename T, size_t N>
786145Snate@binkert.orgstatic void
796145Snate@binkert.orggetPackedMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize)
806145Snate@binkert.org{
816285Snate@binkert.org    std::array<T, N> real_mem = pkt->get<std::array<T, N> >();
826145Snate@binkert.org    for (int i = 0; i < N; i++)
836145Snate@binkert.org        mem[i] = real_mem[i];
846145Snate@binkert.org}
856145Snate@binkert.org
866145Snate@binkert.orgtemplate <size_t N>
876145Snate@binkert.orgstatic void
886145Snate@binkert.orggetMem(PacketPtr pkt, std::array<uint64_t, N> &mem, unsigned dataSize,
896285Snate@binkert.org       Trace::InstRecord *traceData)
906845Sdrh5@cs.wisc.edu{
916145Snate@binkert.org    switch (dataSize) {
926145Snate@binkert.org      case 4:
936145Snate@binkert.org        getPackedMem<uint32_t, N>(pkt, mem, dataSize);
946859Sdrh5@cs.wisc.edu        break;
956145Snate@binkert.org      case 8:
966145Snate@binkert.org        getPackedMem<uint64_t, N>(pkt, mem, dataSize);
976285Snate@binkert.org        break;
986285Snate@binkert.org      default:
996145Snate@binkert.org        panic("Unhandled element size in getMem.\n");
1006285Snate@binkert.org    }
1016145Snate@binkert.org    if (traceData)
1026145Snate@binkert.org        traceData->setData(mem[0]);
1036145Snate@binkert.org}
1046285Snate@binkert.org
1056285Snate@binkert.org
1066285Snate@binkert.orgstatic Fault
1076285Snate@binkert.orgreadMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
1086145Snate@binkert.org              uint64_t &mem, unsigned dataSize, Request::Flags flags)
1096145Snate@binkert.org{
1106145Snate@binkert.org    memset(&mem, 0, sizeof(mem));
1116145Snate@binkert.org    Fault fault = xc->readMem(addr, (uint8_t *)&mem, dataSize, flags);
1126145Snate@binkert.org    if (fault == NoFault) {
1136145Snate@binkert.org        // If LE to LE, this is a nop, if LE to BE, the actual data ends up
1146285Snate@binkert.org        // in the right place because the LSBs where at the low addresses on
1156285Snate@binkert.org        // access. This doesn't work for BE guests.
1166285Snate@binkert.org        mem = gtoh(mem);
1176285Snate@binkert.org        if (traceData)
1186285Snate@binkert.org            traceData->setData(mem);
1196285Snate@binkert.org    }
1206145Snate@binkert.org    return fault;
1216145Snate@binkert.org}
1226285Snate@binkert.org
1236145Snate@binkert.orgtemplate <typename T, size_t N>
1246285Snate@binkert.orgstatic Fault
1256285Snate@binkert.orgreadPackedMemAtomic(ExecContext *xc, Addr addr, std::array<uint64_t, N> &mem,
1266145Snate@binkert.org                    unsigned flags)
1276145Snate@binkert.org{
1286145Snate@binkert.org    std::array<T, N> real_mem;
1296859Sdrh5@cs.wisc.edu    Fault fault = xc->readMem(addr, (uint8_t *)&real_mem,
1306859Sdrh5@cs.wisc.edu                              sizeof(T) * N, flags);
1316859Sdrh5@cs.wisc.edu    if (fault == NoFault) {
1326859Sdrh5@cs.wisc.edu        real_mem = gtoh(real_mem);
1336859Sdrh5@cs.wisc.edu        for (int i = 0; i < N; i++)
1346145Snate@binkert.org            mem[i] = real_mem[i];
1356145Snate@binkert.org    }
1366145Snate@binkert.org    return fault;
1376145Snate@binkert.org}
1386145Snate@binkert.org
1396145Snate@binkert.orgtemplate <size_t N>
1406145Snate@binkert.orgstatic Fault
1416145Snate@binkert.orgreadMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, Addr addr,
1426145Snate@binkert.org              std::array<uint64_t, N> &mem, unsigned dataSize,
1436145Snate@binkert.org              unsigned flags)
1446145Snate@binkert.org{
1456145Snate@binkert.org    Fault fault = NoFault;
1466145Snate@binkert.org
1476145Snate@binkert.org    switch (dataSize) {
1486145Snate@binkert.org      case 4:
1496145Snate@binkert.org        fault = readPackedMemAtomic<uint32_t, N>(xc, addr, mem, flags);
1506145Snate@binkert.org        break;
1516145Snate@binkert.org      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
162template <typename T, size_t N>
163static Fault
164writePackedMem(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 = htog(real_mem);
171    return xc->writeMem((uint8_t *)&real_mem, sizeof(T) * N,
172                        addr, flags, res);
173}
174
175static Fault
176writeMemTiming(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem,
177               unsigned dataSize, Addr addr, Request::Flags flags,
178               uint64_t *res)
179{
180    if (traceData)
181        traceData->setData(mem);
182    mem = TheISA::htog(mem);
183    return xc->writeMem((uint8_t *)&mem, dataSize, addr, flags, res);
184}
185
186template <size_t N>
187static Fault
188writeMemTiming(ExecContext *xc, Trace::InstRecord *traceData,
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
205static Fault
206writeMemAtomic(ExecContext *xc, Trace::InstRecord *traceData, uint64_t mem,
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 = TheISA::htog(mem);
213    Fault fault =
214          xc->writeMem((uint8_t *)&host_mem, dataSize, addr, flags, res);
215    if (fault == NoFault && res)
216        *res = gtoh(*res);
217    return fault;
218}
219
220template <size_t N>
221static Fault
222writeMemAtomic(ExecContext *xc, Trace::InstRecord *traceData,
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 = gtoh(*res);
243
244    return fault;
245}
246
247}
248
249#endif
250