abstract_mem.cc revision 9235
12391SN/A/*
28931Sandreas.hansson@arm.com * Copyright (c) 2010-2012 ARM Limited
37733SN/A * All rights reserved
47733SN/A *
57733SN/A * The license below extends only to copyright in the software and shall
67733SN/A * not be construed as granting a license to any other intellectual
77733SN/A * property including but not limited to intellectual property relating
87733SN/A * to a hardware implementation of the functionality of the software
97733SN/A * licensed hereunder.  You may use the software subject to the license
107733SN/A * terms below provided that you ensure that this notice is replicated
117733SN/A * unmodified and in its entirety in all distributions of the software,
127733SN/A * modified or unmodified, in source code or in binary form.
137733SN/A *
142391SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
152391SN/A * All rights reserved.
162391SN/A *
172391SN/A * Redistribution and use in source and binary forms, with or without
182391SN/A * modification, are permitted provided that the following conditions are
192391SN/A * met: redistributions of source code must retain the above copyright
202391SN/A * notice, this list of conditions and the following disclaimer;
212391SN/A * redistributions in binary form must reproduce the above copyright
222391SN/A * notice, this list of conditions and the following disclaimer in the
232391SN/A * documentation and/or other materials provided with the distribution;
242391SN/A * neither the name of the copyright holders nor the names of its
252391SN/A * contributors may be used to endorse or promote products derived from
262391SN/A * this software without specific prior written permission.
272391SN/A *
282391SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292391SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302391SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312391SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322391SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332391SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342391SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352391SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362391SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372391SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382391SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665SN/A *
402665SN/A * Authors: Ron Dreslinski
412914SN/A *          Ali Saidi
428931Sandreas.hansson@arm.com *          Andreas Hansson
432391SN/A */
442391SN/A
458229SN/A#include <sys/mman.h>
462391SN/A#include <sys/types.h>
477730SN/A#include <sys/user.h>
482391SN/A#include <fcntl.h>
492391SN/A#include <unistd.h>
502391SN/A#include <zlib.h>
512391SN/A
528229SN/A#include <cerrno>
536712SN/A#include <cstdio>
549203Smarco.elver@ed.ac.uk#include <climits>
552391SN/A#include <iostream>
562391SN/A#include <string>
572391SN/A
586329SN/A#include "arch/registers.hh"
596658SN/A#include "config/the_isa.hh"
608232SN/A#include "debug/LLSC.hh"
618232SN/A#include "debug/MemoryAccess.hh"
628931Sandreas.hansson@arm.com#include "mem/abstract_mem.hh"
633879SN/A#include "mem/packet_access.hh"
649053Sdam.sunwoo@arm.com#include "sim/system.hh"
652394SN/A
662391SN/Ausing namespace std;
672391SN/A
688931Sandreas.hansson@arm.comAbstractMemory::AbstractMemory(const Params *p) :
698931Sandreas.hansson@arm.com    MemObject(p), range(params()->range), pmemAddr(NULL),
709053Sdam.sunwoo@arm.com    confTableReported(p->conf_table_reported), inAddrMap(p->in_addr_map),
719053Sdam.sunwoo@arm.com    _system(NULL)
722391SN/A{
737730SN/A    if (size() % TheISA::PageBytes != 0)
742391SN/A        panic("Memory Size not divisible by page size\n");
752391SN/A
765477SN/A    if (params()->null)
775477SN/A        return;
785477SN/A
797730SN/A    if (params()->file == "") {
807730SN/A        int map_flags = MAP_ANON | MAP_PRIVATE;
817730SN/A        pmemAddr = (uint8_t *)mmap(NULL, size(),
827730SN/A                                   PROT_READ | PROT_WRITE, map_flags, -1, 0);
837730SN/A    } else {
847730SN/A        int map_flags = MAP_PRIVATE;
857730SN/A        int fd = open(params()->file.c_str(), O_RDONLY);
868931Sandreas.hansson@arm.com        long _size = lseek(fd, 0, SEEK_END);
878931Sandreas.hansson@arm.com        if (_size != range.size()) {
889235Sandreas.hansson@arm.com            fatal("Specified size %d does not match file %s %d\n",
899235Sandreas.hansson@arm.com                  range.size(), params()->file, _size);
908931Sandreas.hansson@arm.com        }
917730SN/A        lseek(fd, 0, SEEK_SET);
928931Sandreas.hansson@arm.com        pmemAddr = (uint8_t *)mmap(NULL, roundUp(_size, sysconf(_SC_PAGESIZE)),
937730SN/A                                   PROT_READ | PROT_WRITE, map_flags, fd, 0);
947730SN/A    }
952391SN/A
963012SN/A    if (pmemAddr == (void *)MAP_FAILED) {
972391SN/A        perror("mmap");
987730SN/A        if (params()->file == "")
997730SN/A            fatal("Could not mmap!\n");
1007730SN/A        else
1017730SN/A            fatal("Could not find file: %s\n", params()->file);
1022391SN/A    }
1032391SN/A
1043751SN/A    //If requested, initialize all the memory to 0
1054762SN/A    if (p->zero)
1067730SN/A        memset(pmemAddr, 0, size());
1072391SN/A}
1082391SN/A
1092541SN/A
1108931Sandreas.hansson@arm.comAbstractMemory::~AbstractMemory()
1112391SN/A{
1123012SN/A    if (pmemAddr)
1137730SN/A        munmap((char*)pmemAddr, size());
1142391SN/A}
1152391SN/A
1168719SN/Avoid
1178931Sandreas.hansson@arm.comAbstractMemory::regStats()
1188719SN/A{
1198719SN/A    using namespace Stats;
1208719SN/A
1219053Sdam.sunwoo@arm.com    assert(system());
1229053Sdam.sunwoo@arm.com
1238719SN/A    bytesRead
1249053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1258719SN/A        .name(name() + ".bytes_read")
1268719SN/A        .desc("Number of bytes read from this memory")
1279053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1288719SN/A        ;
1299053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1309053Sdam.sunwoo@arm.com        bytesRead.subname(i, system()->getMasterName(i));
1319053Sdam.sunwoo@arm.com    }
1328719SN/A    bytesInstRead
1339053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1348719SN/A        .name(name() + ".bytes_inst_read")
1358719SN/A        .desc("Number of instructions bytes read from this memory")
1369053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1378719SN/A        ;
1389053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1399053Sdam.sunwoo@arm.com        bytesInstRead.subname(i, system()->getMasterName(i));
1409053Sdam.sunwoo@arm.com    }
1418719SN/A    bytesWritten
1429053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1438719SN/A        .name(name() + ".bytes_written")
1448719SN/A        .desc("Number of bytes written to this memory")
1459053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1468719SN/A        ;
1479053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1489053Sdam.sunwoo@arm.com        bytesWritten.subname(i, system()->getMasterName(i));
1499053Sdam.sunwoo@arm.com    }
1508719SN/A    numReads
1519053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1528719SN/A        .name(name() + ".num_reads")
1538719SN/A        .desc("Number of read requests responded to by this memory")
1549053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1558719SN/A        ;
1569053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1579053Sdam.sunwoo@arm.com        numReads.subname(i, system()->getMasterName(i));
1589053Sdam.sunwoo@arm.com    }
1598719SN/A    numWrites
1609053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1618719SN/A        .name(name() + ".num_writes")
1628719SN/A        .desc("Number of write requests responded to by this memory")
1639053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1648719SN/A        ;
1659053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1669053Sdam.sunwoo@arm.com        numWrites.subname(i, system()->getMasterName(i));
1679053Sdam.sunwoo@arm.com    }
1688719SN/A    numOther
1699053Sdam.sunwoo@arm.com        .init(system()->maxMasters())
1708719SN/A        .name(name() + ".num_other")
1718719SN/A        .desc("Number of other requests responded to by this memory")
1729053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1738719SN/A        ;
1749053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1759053Sdam.sunwoo@arm.com        numOther.subname(i, system()->getMasterName(i));
1769053Sdam.sunwoo@arm.com    }
1778719SN/A    bwRead
1788719SN/A        .name(name() + ".bw_read")
1798719SN/A        .desc("Total read bandwidth from this memory (bytes/s)")
1808719SN/A        .precision(0)
1818719SN/A        .prereq(bytesRead)
1829053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1838719SN/A        ;
1849053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1859053Sdam.sunwoo@arm.com        bwRead.subname(i, system()->getMasterName(i));
1869053Sdam.sunwoo@arm.com    }
1879053Sdam.sunwoo@arm.com
1888719SN/A    bwInstRead
1898719SN/A        .name(name() + ".bw_inst_read")
1908719SN/A        .desc("Instruction read bandwidth from this memory (bytes/s)")
1918719SN/A        .precision(0)
1928719SN/A        .prereq(bytesInstRead)
1939053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
1948719SN/A        ;
1959053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
1969053Sdam.sunwoo@arm.com        bwInstRead.subname(i, system()->getMasterName(i));
1979053Sdam.sunwoo@arm.com    }
1988719SN/A    bwWrite
1998719SN/A        .name(name() + ".bw_write")
2008719SN/A        .desc("Write bandwidth from this memory (bytes/s)")
2018719SN/A        .precision(0)
2028719SN/A        .prereq(bytesWritten)
2039053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
2048719SN/A        ;
2059053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
2069053Sdam.sunwoo@arm.com        bwWrite.subname(i, system()->getMasterName(i));
2079053Sdam.sunwoo@arm.com    }
2088719SN/A    bwTotal
2098719SN/A        .name(name() + ".bw_total")
2108719SN/A        .desc("Total bandwidth to/from this memory (bytes/s)")
2118719SN/A        .precision(0)
2128719SN/A        .prereq(bwTotal)
2139053Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
2148719SN/A        ;
2159053Sdam.sunwoo@arm.com    for (int i = 0; i < system()->maxMasters(); i++) {
2169053Sdam.sunwoo@arm.com        bwTotal.subname(i, system()->getMasterName(i));
2179053Sdam.sunwoo@arm.com    }
2188719SN/A    bwRead = bytesRead / simSeconds;
2198719SN/A    bwInstRead = bytesInstRead / simSeconds;
2208719SN/A    bwWrite = bytesWritten / simSeconds;
2218719SN/A    bwTotal = (bytesRead + bytesWritten) / simSeconds;
2228719SN/A}
2238719SN/A
2249235Sandreas.hansson@arm.comAddrRange
2259098Sandreas.hansson@arm.comAbstractMemory::getAddrRange() const
2262408SN/A{
2278931Sandreas.hansson@arm.com    return range;
2282408SN/A}
2292408SN/A
2303170SN/A// Add load-locked to tracking list.  Should only be called if the
2316076SN/A// operation is a load and the LLSC flag is set.
2323170SN/Avoid
2338931Sandreas.hansson@arm.comAbstractMemory::trackLoadLocked(PacketPtr pkt)
2343170SN/A{
2354626SN/A    Request *req = pkt->req;
2363170SN/A    Addr paddr = LockedAddr::mask(req->getPaddr());
2373170SN/A
2383170SN/A    // first we check if we already have a locked addr for this
2393170SN/A    // xc.  Since each xc only gets one, we just update the
2403170SN/A    // existing record with the new address.
2413170SN/A    list<LockedAddr>::iterator i;
2423170SN/A
2433170SN/A    for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
2443170SN/A        if (i->matchesContext(req)) {
2455714SN/A            DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
2465714SN/A                    req->contextId(), paddr);
2473170SN/A            i->addr = paddr;
2483170SN/A            return;
2493170SN/A        }
2503170SN/A    }
2513170SN/A
2523170SN/A    // no record for this xc: need to allocate a new one
2535714SN/A    DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
2545714SN/A            req->contextId(), paddr);
2553170SN/A    lockedAddrList.push_front(LockedAddr(req));
2563170SN/A}
2573170SN/A
2583170SN/A
2593170SN/A// Called on *writes* only... both regular stores and
2603170SN/A// store-conditional operations.  Check for conventional stores which
2613170SN/A// conflict with locked addresses, and for success/failure of store
2623170SN/A// conditionals.
2633170SN/Abool
2648931Sandreas.hansson@arm.comAbstractMemory::checkLockedAddrList(PacketPtr pkt)
2653170SN/A{
2664626SN/A    Request *req = pkt->req;
2673170SN/A    Addr paddr = LockedAddr::mask(req->getPaddr());
2686102SN/A    bool isLLSC = pkt->isLLSC();
2693170SN/A
2703170SN/A    // Initialize return value.  Non-conditional stores always
2713170SN/A    // succeed.  Assume conditional stores will fail until proven
2723170SN/A    // otherwise.
2739080Smatt.evans@arm.com    bool allowStore = !isLLSC;
2743170SN/A
2759080Smatt.evans@arm.com    // Iterate over list.  Note that there could be multiple matching records,
2769080Smatt.evans@arm.com    // as more than one context could have done a load locked to this location.
2779080Smatt.evans@arm.com    // Only remove records when we succeed in finding a record for (xc, addr);
2789080Smatt.evans@arm.com    // then, remove all records with this address.  Failed store-conditionals do
2799080Smatt.evans@arm.com    // not blow unrelated reservations.
2803170SN/A    list<LockedAddr>::iterator i = lockedAddrList.begin();
2813170SN/A
2829080Smatt.evans@arm.com    if (isLLSC) {
2839080Smatt.evans@arm.com        while (i != lockedAddrList.end()) {
2849080Smatt.evans@arm.com            if (i->addr == paddr && i->matchesContext(req)) {
2859080Smatt.evans@arm.com                // it's a store conditional, and as far as the memory system can
2869080Smatt.evans@arm.com                // tell, the requesting context's lock is still valid.
2875714SN/A                DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
2885714SN/A                        req->contextId(), paddr);
2899080Smatt.evans@arm.com                allowStore = true;
2909080Smatt.evans@arm.com                break;
2913170SN/A            }
2929080Smatt.evans@arm.com            // If we didn't find a match, keep searching!  Someone else may well
2939080Smatt.evans@arm.com            // have a reservation on this line here but we may find ours in just
2949080Smatt.evans@arm.com            // a little while.
2959080Smatt.evans@arm.com            i++;
2963170SN/A        }
2979080Smatt.evans@arm.com        req->setExtraData(allowStore ? 1 : 0);
2989080Smatt.evans@arm.com    }
2999080Smatt.evans@arm.com    // LLSCs that succeeded AND non-LLSC stores both fall into here:
3009080Smatt.evans@arm.com    if (allowStore) {
3019080Smatt.evans@arm.com        // We write address paddr.  However, there may be several entries with a
3029080Smatt.evans@arm.com        // reservation on this address (for other contextIds) and they must all
3039080Smatt.evans@arm.com        // be removed.
3049080Smatt.evans@arm.com        i = lockedAddrList.begin();
3059080Smatt.evans@arm.com        while (i != lockedAddrList.end()) {
3069080Smatt.evans@arm.com            if (i->addr == paddr) {
3079080Smatt.evans@arm.com                DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
3089080Smatt.evans@arm.com                        i->contextId, paddr);
3099080Smatt.evans@arm.com                i = lockedAddrList.erase(i);
3109080Smatt.evans@arm.com            } else {
3119080Smatt.evans@arm.com                i++;
3129080Smatt.evans@arm.com            }
3133170SN/A        }
3143170SN/A    }
3153170SN/A
3169080Smatt.evans@arm.com    return allowStore;
3173170SN/A}
3183170SN/A
3194626SN/A
3204626SN/A#if TRACING_ON
3214626SN/A
3224626SN/A#define CASE(A, T)                                                      \
3234626SN/A  case sizeof(T):                                                       \
3246429SN/A    DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n",   \
3256429SN/A            A, pkt->getSize(), pkt->getAddr(), pkt->get<T>());          \
3264626SN/A  break
3274626SN/A
3284626SN/A
3294626SN/A#define TRACE_PACKET(A)                                                 \
3304626SN/A    do {                                                                \
3314626SN/A        switch (pkt->getSize()) {                                       \
3324626SN/A          CASE(A, uint64_t);                                            \
3334626SN/A          CASE(A, uint32_t);                                            \
3344626SN/A          CASE(A, uint16_t);                                            \
3354626SN/A          CASE(A, uint8_t);                                             \
3364626SN/A          default:                                                      \
3376429SN/A            DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n",    \
3386429SN/A                    A, pkt->getSize(), pkt->getAddr());                 \
3398077SN/A            DDUMP(MemoryAccess, pkt->getPtr<uint8_t>(), pkt->getSize());\
3404626SN/A        }                                                               \
3414626SN/A    } while (0)
3424626SN/A
3434626SN/A#else
3444626SN/A
3454626SN/A#define TRACE_PACKET(A)
3464626SN/A
3474626SN/A#endif
3484626SN/A
3498931Sandreas.hansson@arm.comvoid
3508931Sandreas.hansson@arm.comAbstractMemory::access(PacketPtr pkt)
3512413SN/A{
3528931Sandreas.hansson@arm.com    assert(pkt->getAddr() >= range.start &&
3538931Sandreas.hansson@arm.com           (pkt->getAddr() + pkt->getSize() - 1) <= range.end);
3542414SN/A
3554626SN/A    if (pkt->memInhibitAsserted()) {
3564626SN/A        DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
3574626SN/A                pkt->getAddr());
3588931Sandreas.hansson@arm.com        return;
3593175SN/A    }
3604626SN/A
3618931Sandreas.hansson@arm.com    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start;
3624626SN/A
3634626SN/A    if (pkt->cmd == MemCmd::SwapReq) {
3648931Sandreas.hansson@arm.com        TheISA::IntReg overwrite_val;
3654040SN/A        bool overwrite_mem;
3664040SN/A        uint64_t condition_val64;
3674040SN/A        uint32_t condition_val32;
3684040SN/A
3695477SN/A        if (!pmemAddr)
3705477SN/A            panic("Swap only works if there is real memory (i.e. null=False)");
3718931Sandreas.hansson@arm.com        assert(sizeof(TheISA::IntReg) >= pkt->getSize());
3724040SN/A
3734040SN/A        overwrite_mem = true;
3744040SN/A        // keep a copy of our possible write value, and copy what is at the
3754040SN/A        // memory address into the packet
3764052SN/A        std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
3774626SN/A        std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
3784040SN/A
3794040SN/A        if (pkt->req->isCondSwap()) {
3804040SN/A            if (pkt->getSize() == sizeof(uint64_t)) {
3814052SN/A                condition_val64 = pkt->req->getExtraData();
3824626SN/A                overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
3834626SN/A                                             sizeof(uint64_t));
3844040SN/A            } else if (pkt->getSize() == sizeof(uint32_t)) {
3854052SN/A                condition_val32 = (uint32_t)pkt->req->getExtraData();
3864626SN/A                overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
3874626SN/A                                             sizeof(uint32_t));
3884040SN/A            } else
3894040SN/A                panic("Invalid size for conditional read/write\n");
3904040SN/A        }
3914040SN/A
3924040SN/A        if (overwrite_mem)
3934626SN/A            std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
3944040SN/A
3956429SN/A        assert(!pkt->req->isInstFetch());
3964626SN/A        TRACE_PACKET("Read/Write");
3979053Sdam.sunwoo@arm.com        numOther[pkt->req->masterId()]++;
3984626SN/A    } else if (pkt->isRead()) {
3994626SN/A        assert(!pkt->isWrite());
4006102SN/A        if (pkt->isLLSC()) {
4014626SN/A            trackLoadLocked(pkt);
4024040SN/A        }
4035477SN/A        if (pmemAddr)
4045477SN/A            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
4056429SN/A        TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
4069053Sdam.sunwoo@arm.com        numReads[pkt->req->masterId()]++;
4079053Sdam.sunwoo@arm.com        bytesRead[pkt->req->masterId()] += pkt->getSize();
4088719SN/A        if (pkt->req->isInstFetch())
4099053Sdam.sunwoo@arm.com            bytesInstRead[pkt->req->masterId()] += pkt->getSize();
4104626SN/A    } else if (pkt->isWrite()) {
4114626SN/A        if (writeOK(pkt)) {
4125477SN/A            if (pmemAddr)
4135477SN/A                memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
4146429SN/A            assert(!pkt->req->isInstFetch());
4154626SN/A            TRACE_PACKET("Write");
4169053Sdam.sunwoo@arm.com            numWrites[pkt->req->masterId()]++;
4179053Sdam.sunwoo@arm.com            bytesWritten[pkt->req->masterId()] += pkt->getSize();
4184626SN/A        }
4194626SN/A    } else if (pkt->isInvalidate()) {
4208931Sandreas.hansson@arm.com        // no need to do anything
4214040SN/A    } else {
4222413SN/A        panic("unimplemented");
4232413SN/A    }
4242420SN/A
4254626SN/A    if (pkt->needsResponse()) {
4268931Sandreas.hansson@arm.com        pkt->makeResponse();
4274626SN/A    }
4282413SN/A}
4292413SN/A
4308931Sandreas.hansson@arm.comvoid
4318931Sandreas.hansson@arm.comAbstractMemory::functionalAccess(PacketPtr pkt)
4328931Sandreas.hansson@arm.com{
4338931Sandreas.hansson@arm.com    assert(pkt->getAddr() >= range.start &&
4348931Sandreas.hansson@arm.com           (pkt->getAddr() + pkt->getSize() - 1) <= range.end);
4354626SN/A
4368931Sandreas.hansson@arm.com    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - range.start;
4374626SN/A
4385314SN/A    if (pkt->isRead()) {
4395477SN/A        if (pmemAddr)
4405477SN/A            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
4414626SN/A        TRACE_PACKET("Read");
4428931Sandreas.hansson@arm.com        pkt->makeResponse();
4435314SN/A    } else if (pkt->isWrite()) {
4445477SN/A        if (pmemAddr)
4455477SN/A            memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
4464626SN/A        TRACE_PACKET("Write");
4478931Sandreas.hansson@arm.com        pkt->makeResponse();
4485314SN/A    } else if (pkt->isPrint()) {
4495315SN/A        Packet::PrintReqState *prs =
4505315SN/A            dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
4518992SAli.Saidi@ARM.com        assert(prs);
4525315SN/A        // Need to call printLabels() explicitly since we're not going
4535315SN/A        // through printObj().
4545314SN/A        prs->printLabels();
4555315SN/A        // Right now we just print the single byte at the specified address.
4565314SN/A        ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
4574626SN/A    } else {
4588931Sandreas.hansson@arm.com        panic("AbstractMemory: unimplemented functional command %s",
4594626SN/A              pkt->cmdString());
4604626SN/A    }
4614490SN/A}
4624490SN/A
4632413SN/Avoid
4648931Sandreas.hansson@arm.comAbstractMemory::serialize(ostream &os)
4652391SN/A{
4665477SN/A    if (!pmemAddr)
4675477SN/A        return;
4685477SN/A
4692391SN/A    gzFile compressedMem;
4702391SN/A    string filename = name() + ".physmem";
4718931Sandreas.hansson@arm.com    long _size = range.size();
4722391SN/A
4732391SN/A    SERIALIZE_SCALAR(filename);
4747730SN/A    SERIALIZE_SCALAR(_size);
4752391SN/A
4762391SN/A    // write memory file
4772391SN/A    string thefile = Checkpoint::dir() + "/" + filename.c_str();
4782391SN/A    int fd = creat(thefile.c_str(), 0664);
4792391SN/A    if (fd < 0) {
4802391SN/A        perror("creat");
4812391SN/A        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
4822391SN/A    }
4832391SN/A
4842391SN/A    compressedMem = gzdopen(fd, "wb");
4852391SN/A    if (compressedMem == NULL)
4862391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
4872391SN/A                filename);
4882391SN/A
4899203Smarco.elver@ed.ac.uk    uint64_t pass_size = 0;
4909203Smarco.elver@ed.ac.uk    // gzwrite fails if (int)len < 0 (gzwrite returns int)
4919203Smarco.elver@ed.ac.uk    for (uint64_t written = 0; written < size(); written += pass_size) {
4929203Smarco.elver@ed.ac.uk        pass_size = (uint64_t)INT_MAX < (size() - written) ?
4939203Smarco.elver@ed.ac.uk            (uint64_t)INT_MAX : (size() - written);
4949203Smarco.elver@ed.ac.uk
4959203Smarco.elver@ed.ac.uk        if (gzwrite(compressedMem, pmemAddr + written,
4969203Smarco.elver@ed.ac.uk                    (unsigned int) pass_size) != (int)pass_size) {
4979203Smarco.elver@ed.ac.uk            fatal("Write failed on physical memory checkpoint file '%s'\n",
4989203Smarco.elver@ed.ac.uk                  filename);
4999203Smarco.elver@ed.ac.uk        }
5002391SN/A    }
5012391SN/A
5022391SN/A    if (gzclose(compressedMem))
5032391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
5042391SN/A              filename);
5057733SN/A
5067733SN/A    list<LockedAddr>::iterator i = lockedAddrList.begin();
5077733SN/A
5087733SN/A    vector<Addr> lal_addr;
5097733SN/A    vector<int> lal_cid;
5107733SN/A    while (i != lockedAddrList.end()) {
5117733SN/A        lal_addr.push_back(i->addr);
5127733SN/A        lal_cid.push_back(i->contextId);
5137733SN/A        i++;
5147733SN/A    }
5157733SN/A    arrayParamOut(os, "lal_addr", lal_addr);
5167733SN/A    arrayParamOut(os, "lal_cid", lal_cid);
5172391SN/A}
5182391SN/A
5192391SN/Avoid
5208931Sandreas.hansson@arm.comAbstractMemory::unserialize(Checkpoint *cp, const string &section)
5212391SN/A{
5225477SN/A    if (!pmemAddr)
5235477SN/A        return;
5245477SN/A
5252391SN/A    gzFile compressedMem;
5262391SN/A    long *tempPage;
5272391SN/A    long *pmem_current;
5282391SN/A    uint64_t curSize;
5292391SN/A    uint32_t bytesRead;
5306227SN/A    const uint32_t chunkSize = 16384;
5312391SN/A
5322391SN/A    string filename;
5332391SN/A
5342391SN/A    UNSERIALIZE_SCALAR(filename);
5352391SN/A
5362391SN/A    filename = cp->cptDir + "/" + filename;
5372391SN/A
5382391SN/A    // mmap memoryfile
5392391SN/A    int fd = open(filename.c_str(), O_RDONLY);
5402391SN/A    if (fd < 0) {
5412391SN/A        perror("open");
5422391SN/A        fatal("Can't open physical memory checkpoint file '%s'", filename);
5432391SN/A    }
5442391SN/A
5452391SN/A    compressedMem = gzdopen(fd, "rb");
5462391SN/A    if (compressedMem == NULL)
5472391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
5482391SN/A                filename);
5492391SN/A
5508105SN/A    // unmap file that was mmapped in the constructor
5513012SN/A    // This is done here to make sure that gzip and open don't muck with our
5523012SN/A    // nice large space of memory before we reallocate it
5537730SN/A    munmap((char*)pmemAddr, size());
5542391SN/A
5558931Sandreas.hansson@arm.com    long _size;
5567730SN/A    UNSERIALIZE_SCALAR(_size);
5578931Sandreas.hansson@arm.com    if (_size > params()->range.size())
5588636SN/A        fatal("Memory size has changed! size %lld, param size %lld\n",
5598931Sandreas.hansson@arm.com              _size, params()->range.size());
5607730SN/A
5617730SN/A    pmemAddr = (uint8_t *)mmap(NULL, size(),
5624762SN/A        PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
5632391SN/A
5643012SN/A    if (pmemAddr == (void *)MAP_FAILED) {
5652391SN/A        perror("mmap");
5662391SN/A        fatal("Could not mmap physical memory!\n");
5672391SN/A    }
5682391SN/A
5692391SN/A    curSize = 0;
5702391SN/A    tempPage = (long*)malloc(chunkSize);
5712391SN/A    if (tempPage == NULL)
5722391SN/A        fatal("Unable to malloc memory to read file %s\n", filename);
5732391SN/A
5742391SN/A    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
5757730SN/A    while (curSize < size()) {
5762391SN/A        bytesRead = gzread(compressedMem, tempPage, chunkSize);
5776820SN/A        if (bytesRead == 0)
5786820SN/A            break;
5792391SN/A
5802391SN/A        assert(bytesRead % sizeof(long) == 0);
5812391SN/A
5826227SN/A        for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
5832391SN/A        {
5842391SN/A             if (*(tempPage+x) != 0) {
5853012SN/A                 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
5862391SN/A                 *pmem_current = *(tempPage+x);
5872391SN/A             }
5882391SN/A        }
5892391SN/A        curSize += bytesRead;
5902391SN/A    }
5912391SN/A
5922391SN/A    free(tempPage);
5932391SN/A
5942391SN/A    if (gzclose(compressedMem))
5952391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
5962391SN/A              filename);
5972391SN/A
5987733SN/A    vector<Addr> lal_addr;
5997733SN/A    vector<int> lal_cid;
6007733SN/A    arrayParamIn(cp, section, "lal_addr", lal_addr);
6017733SN/A    arrayParamIn(cp, section, "lal_cid", lal_cid);
6027733SN/A    for(int i = 0; i < lal_addr.size(); i++)
6037733SN/A        lockedAddrList.push_front(LockedAddr(lal_addr[i], lal_cid[i]));
6042391SN/A}
605