memhelpers.hh revision 11881:f0a07f1e8ee2
16654Snate@binkert.org/* 26654Snate@binkert.org * Copyright (c) 2013 ARM Limited 35467Snate@binkert.org * All rights reserved 45467Snate@binkert.org * 55467Snate@binkert.org * The license below extends only to copyright in the software and shall 65467Snate@binkert.org * not be construed as granting a license to any other intellectual 75467Snate@binkert.org * property including but not limited to intellectual property relating 85467Snate@binkert.org * to a hardware implementation of the functionality of the software 95467Snate@binkert.org * licensed hereunder. You may use the software subject to the license 105467Snate@binkert.org * terms below provided that you ensure that this notice is replicated 115467Snate@binkert.org * unmodified and in its entirety in all distributions of the software, 125467Snate@binkert.org * modified or unmodified, in source code or in binary form. 135467Snate@binkert.org * 145467Snate@binkert.org * Copyright (c) 2011 Google 155467Snate@binkert.org * All rights reserved. 165467Snate@binkert.org * 175467Snate@binkert.org * Redistribution and use in source and binary forms, with or without 185467Snate@binkert.org * modification, are permitted provided that the following conditions are 195467Snate@binkert.org * met: redistributions of source code must retain the above copyright 205467Snate@binkert.org * notice, this list of conditions and the following disclaimer; 215467Snate@binkert.org * redistributions in binary form must reproduce the above copyright 225467Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 235467Snate@binkert.org * documentation and/or other materials provided with the distribution; 245467Snate@binkert.org * neither the name of the copyright holders nor the names of its 255467Snate@binkert.org * contributors may be used to endorse or promote products derived from 265467Snate@binkert.org * this software without specific prior written permission. 275467Snate@binkert.org * 285467Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 295467Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 306654Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 316654Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 326654Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 336654Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 346654Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 356654Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 366654Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 375467Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 386502Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 395467Snate@binkert.org * 406500Snate@binkert.org * Authors: Gabe Black 416654Snate@binkert.org */ 425873Snate@binkert.org 436654Snate@binkert.org#ifndef __ARCH_GENERIC_MEMHELPERS_HH__ 446654Snate@binkert.org#define __ARCH_GENERIC_MEMHELPERS_HH__ 456654Snate@binkert.org 466654Snate@binkert.org#include "arch/isa_traits.hh" 476654Snate@binkert.org#include "base/types.hh" 486654Snate@binkert.org#include "mem/packet.hh" 496654Snate@binkert.org#include "mem/request.hh" 506654Snate@binkert.org#include "sim/byteswap.hh" 516654Snate@binkert.org#include "sim/insttracer.hh" 526654Snate@binkert.org 536654Snate@binkert.org/// Initiate a read from memory in timing mode. Note that the 'mem' 546654Snate@binkert.org/// parameter is unused; only the type of that parameter is used 556654Snate@binkert.org/// to determine the size of the access. 566654Snate@binkert.orgtemplate <class XC, class MemT> 576654Snate@binkert.orgFault 586654Snate@binkert.orginitiateMemRead(XC *xc, Trace::InstRecord *traceData, Addr addr, 596654Snate@binkert.org MemT &mem, Request::Flags flags) 606654Snate@binkert.org{ 616654Snate@binkert.org return xc->initiateMemRead(addr, sizeof(MemT), flags); 626654Snate@binkert.org} 636654Snate@binkert.org 646654Snate@binkert.org/// Extract the data returned from a timing mode read. 656654Snate@binkert.orgtemplate <class MemT> 666654Snate@binkert.orgvoid 676654Snate@binkert.orggetMem(PacketPtr pkt, MemT &mem, Trace::InstRecord *traceData) 686654Snate@binkert.org{ 696654Snate@binkert.org mem = pkt->get<MemT>(); 706654Snate@binkert.org if (traceData) 716654Snate@binkert.org traceData->setData(mem); 726654Snate@binkert.org} 736654Snate@binkert.org 746654Snate@binkert.org/// Read from memory in atomic mode. 756654Snate@binkert.orgtemplate <class XC, class MemT> 766654Snate@binkert.orgFault 776654Snate@binkert.orgreadMemAtomic(XC *xc, Trace::InstRecord *traceData, Addr addr, MemT &mem, 786654Snate@binkert.org Request::Flags flags) 796654Snate@binkert.org{ 806654Snate@binkert.org memset(&mem, 0, sizeof(mem)); 816654Snate@binkert.org Fault fault = xc->readMem(addr, (uint8_t *)&mem, sizeof(MemT), flags); 826654Snate@binkert.org if (fault == NoFault) { 836654Snate@binkert.org mem = TheISA::gtoh(mem); 846654Snate@binkert.org if (traceData) 856654Snate@binkert.org traceData->setData(mem); 866654Snate@binkert.org } 876654Snate@binkert.org return fault; 886654Snate@binkert.org} 896654Snate@binkert.org 906654Snate@binkert.org/// Write to memory in timing mode. 916654Snate@binkert.orgtemplate <class XC, class MemT> 926654Snate@binkert.orgFault 936654Snate@binkert.orgwriteMemTiming(XC *xc, Trace::InstRecord *traceData, MemT mem, Addr addr, 946654Snate@binkert.org Request::Flags flags, uint64_t *res) 956654Snate@binkert.org{ 966654Snate@binkert.org if (traceData) { 976654Snate@binkert.org traceData->setData(mem); 986654Snate@binkert.org } 996654Snate@binkert.org mem = TheISA::htog(mem); 1006654Snate@binkert.org return xc->writeMem((uint8_t *)&mem, sizeof(MemT), addr, flags, res); 1016654Snate@binkert.org} 1026654Snate@binkert.org 1036654Snate@binkert.org/// Write to memory in atomic mode. 1046654Snate@binkert.orgtemplate <class XC, class MemT> 1056654Snate@binkert.orgFault 1066654Snate@binkert.orgwriteMemAtomic(XC *xc, Trace::InstRecord *traceData, const MemT &mem, 1076654Snate@binkert.org Addr addr, Request::Flags flags, uint64_t *res) 1086654Snate@binkert.org{ 1096654Snate@binkert.org if (traceData) { 1106654Snate@binkert.org traceData->setData(mem); 1116654Snate@binkert.org } 1126654Snate@binkert.org MemT host_mem = TheISA::htog(mem); 1136654Snate@binkert.org Fault fault = 1146654Snate@binkert.org xc->writeMem((uint8_t *)&host_mem, sizeof(MemT), addr, flags, res); 1156654Snate@binkert.org if (fault == NoFault && res != NULL) { 1166654Snate@binkert.org if (flags & Request::MEM_SWAP || flags & Request::MEM_SWAP_COND) 1176654Snate@binkert.org *res = TheISA::gtoh((MemT)*res); 1186654Snate@binkert.org else 1196654Snate@binkert.org *res = TheISA::gtoh(*res); 1206654Snate@binkert.org } 1216654Snate@binkert.org return fault; 1226654Snate@binkert.org} 1236654Snate@binkert.org 1246654Snate@binkert.org#endif 1256654Snate@binkert.org