physical.cc revision 3879
12391SN/A/*
22391SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
32391SN/A * All rights reserved.
42391SN/A *
52391SN/A * Redistribution and use in source and binary forms, with or without
62391SN/A * modification, are permitted provided that the following conditions are
72391SN/A * met: redistributions of source code must retain the above copyright
82391SN/A * notice, this list of conditions and the following disclaimer;
92391SN/A * redistributions in binary form must reproduce the above copyright
102391SN/A * notice, this list of conditions and the following disclaimer in the
112391SN/A * documentation and/or other materials provided with the distribution;
122391SN/A * neither the name of the copyright holders nor the names of its
132391SN/A * contributors may be used to endorse or promote products derived from
142391SN/A * this software without specific prior written permission.
152391SN/A *
162391SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172391SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182391SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192391SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202391SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212391SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222391SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232391SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242391SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252391SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262391SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski
292914Ssaidi@eecs.umich.edu *          Ali Saidi
302391SN/A */
312391SN/A
322391SN/A#include <sys/types.h>
332391SN/A#include <sys/mman.h>
342391SN/A#include <errno.h>
352391SN/A#include <fcntl.h>
362391SN/A#include <unistd.h>
372391SN/A#include <zlib.h>
382391SN/A
392391SN/A#include <iostream>
402391SN/A#include <string>
412391SN/A
423348Sbinkertn@umich.edu#include "arch/isa_traits.hh"
432391SN/A#include "base/misc.hh"
442391SN/A#include "config/full_system.hh"
453879Ssaidi@eecs.umich.edu#include "mem/packet_access.hh"
462394SN/A#include "mem/physical.hh"
472391SN/A#include "sim/builder.hh"
482415SN/A#include "sim/eventq.hh"
493348Sbinkertn@umich.edu#include "sim/host.hh"
502394SN/A
512391SN/Ausing namespace std;
522423SN/Ausing namespace TheISA;
532391SN/A
543012Ssaidi@eecs.umich.eduPhysicalMemory::PhysicalMemory(Params *p)
553012Ssaidi@eecs.umich.edu    : MemObject(p->name), pmemAddr(NULL), port(NULL), lat(p->latency), _params(p)
562391SN/A{
573012Ssaidi@eecs.umich.edu    if (params()->addrRange.size() % TheISA::PageBytes != 0)
582391SN/A        panic("Memory Size not divisible by page size\n");
592391SN/A
602391SN/A    int map_flags = MAP_ANON | MAP_PRIVATE;
613012Ssaidi@eecs.umich.edu    pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
622391SN/A                                map_flags, -1, 0);
632391SN/A
643012Ssaidi@eecs.umich.edu    if (pmemAddr == (void *)MAP_FAILED) {
652391SN/A        perror("mmap");
662391SN/A        fatal("Could not mmap!\n");
672391SN/A    }
682391SN/A
693751Sgblack@eecs.umich.edu    //If requested, initialize all the memory to 0
703751Sgblack@eecs.umich.edu    if(params()->zero)
713751Sgblack@eecs.umich.edu        memset(pmemAddr, 0, params()->addrRange.size());
723751Sgblack@eecs.umich.edu
733012Ssaidi@eecs.umich.edu    pagePtr = 0;
742391SN/A}
752391SN/A
762541SN/Avoid
772541SN/APhysicalMemory::init()
782541SN/A{
792541SN/A    if (!port)
802541SN/A        panic("PhysicalMemory not connected to anything!");
812541SN/A    port->sendStatusChange(Port::RangeChange);
822541SN/A}
832541SN/A
842391SN/APhysicalMemory::~PhysicalMemory()
852391SN/A{
863012Ssaidi@eecs.umich.edu    if (pmemAddr)
873012Ssaidi@eecs.umich.edu        munmap(pmemAddr, params()->addrRange.size());
882416SN/A    //Remove memPorts?
892391SN/A}
902391SN/A
912391SN/AAddr
922391SN/APhysicalMemory::new_page()
932391SN/A{
943012Ssaidi@eecs.umich.edu    Addr return_addr = pagePtr << LogVMPageSize;
953012Ssaidi@eecs.umich.edu    return_addr += params()->addrRange.start;
962391SN/A
973012Ssaidi@eecs.umich.edu    ++pagePtr;
982391SN/A    return return_addr;
992391SN/A}
1002391SN/A
1012408SN/Aint
1022408SN/APhysicalMemory::deviceBlockSize()
1032408SN/A{
1042409SN/A    //Can accept anysize request
1052409SN/A    return 0;
1062408SN/A}
1072408SN/A
1083012Ssaidi@eecs.umich.eduTick
1093349Sbinkertn@umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt)
1103012Ssaidi@eecs.umich.edu{
1113012Ssaidi@eecs.umich.edu    return lat;
1123012Ssaidi@eecs.umich.edu}
1132413SN/A
1143170Sstever@eecs.umich.edu
1153170Sstever@eecs.umich.edu
1163170Sstever@eecs.umich.edu// Add load-locked to tracking list.  Should only be called if the
1173170Sstever@eecs.umich.edu// operation is a load and the LOCKED flag is set.
1183170Sstever@eecs.umich.eduvoid
1193170Sstever@eecs.umich.eduPhysicalMemory::trackLoadLocked(Request *req)
1203170Sstever@eecs.umich.edu{
1213170Sstever@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1223170Sstever@eecs.umich.edu
1233170Sstever@eecs.umich.edu    // first we check if we already have a locked addr for this
1243170Sstever@eecs.umich.edu    // xc.  Since each xc only gets one, we just update the
1253170Sstever@eecs.umich.edu    // existing record with the new address.
1263170Sstever@eecs.umich.edu    list<LockedAddr>::iterator i;
1273170Sstever@eecs.umich.edu
1283170Sstever@eecs.umich.edu    for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
1293170Sstever@eecs.umich.edu        if (i->matchesContext(req)) {
1303170Sstever@eecs.umich.edu            DPRINTF(LLSC, "Modifying lock record: cpu %d thread %d addr %#x\n",
1313170Sstever@eecs.umich.edu                    req->getCpuNum(), req->getThreadNum(), paddr);
1323170Sstever@eecs.umich.edu            i->addr = paddr;
1333170Sstever@eecs.umich.edu            return;
1343170Sstever@eecs.umich.edu        }
1353170Sstever@eecs.umich.edu    }
1363170Sstever@eecs.umich.edu
1373170Sstever@eecs.umich.edu    // no record for this xc: need to allocate a new one
1383170Sstever@eecs.umich.edu    DPRINTF(LLSC, "Adding lock record: cpu %d thread %d addr %#x\n",
1393170Sstever@eecs.umich.edu            req->getCpuNum(), req->getThreadNum(), paddr);
1403170Sstever@eecs.umich.edu    lockedAddrList.push_front(LockedAddr(req));
1413170Sstever@eecs.umich.edu}
1423170Sstever@eecs.umich.edu
1433170Sstever@eecs.umich.edu
1443170Sstever@eecs.umich.edu// Called on *writes* only... both regular stores and
1453170Sstever@eecs.umich.edu// store-conditional operations.  Check for conventional stores which
1463170Sstever@eecs.umich.edu// conflict with locked addresses, and for success/failure of store
1473170Sstever@eecs.umich.edu// conditionals.
1483170Sstever@eecs.umich.edubool
1493170Sstever@eecs.umich.eduPhysicalMemory::checkLockedAddrList(Request *req)
1503170Sstever@eecs.umich.edu{
1513170Sstever@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1523170Sstever@eecs.umich.edu    bool isLocked = req->isLocked();
1533170Sstever@eecs.umich.edu
1543170Sstever@eecs.umich.edu    // Initialize return value.  Non-conditional stores always
1553170Sstever@eecs.umich.edu    // succeed.  Assume conditional stores will fail until proven
1563170Sstever@eecs.umich.edu    // otherwise.
1573170Sstever@eecs.umich.edu    bool success = !isLocked;
1583170Sstever@eecs.umich.edu
1593170Sstever@eecs.umich.edu    // Iterate over list.  Note that there could be multiple matching
1603170Sstever@eecs.umich.edu    // records, as more than one context could have done a load locked
1613170Sstever@eecs.umich.edu    // to this location.
1623170Sstever@eecs.umich.edu    list<LockedAddr>::iterator i = lockedAddrList.begin();
1633170Sstever@eecs.umich.edu
1643170Sstever@eecs.umich.edu    while (i != lockedAddrList.end()) {
1653170Sstever@eecs.umich.edu
1663170Sstever@eecs.umich.edu        if (i->addr == paddr) {
1673170Sstever@eecs.umich.edu            // we have a matching address
1683170Sstever@eecs.umich.edu
1693170Sstever@eecs.umich.edu            if (isLocked && i->matchesContext(req)) {
1703170Sstever@eecs.umich.edu                // it's a store conditional, and as far as the memory
1713170Sstever@eecs.umich.edu                // system can tell, the requesting context's lock is
1723170Sstever@eecs.umich.edu                // still valid.
1733170Sstever@eecs.umich.edu                DPRINTF(LLSC, "StCond success: cpu %d thread %d addr %#x\n",
1743170Sstever@eecs.umich.edu                        req->getCpuNum(), req->getThreadNum(), paddr);
1753170Sstever@eecs.umich.edu                success = true;
1763170Sstever@eecs.umich.edu            }
1773170Sstever@eecs.umich.edu
1783170Sstever@eecs.umich.edu            // Get rid of our record of this lock and advance to next
1793170Sstever@eecs.umich.edu            DPRINTF(LLSC, "Erasing lock record: cpu %d thread %d addr %#x\n",
1803170Sstever@eecs.umich.edu                    i->cpuNum, i->threadNum, paddr);
1813170Sstever@eecs.umich.edu            i = lockedAddrList.erase(i);
1823170Sstever@eecs.umich.edu        }
1833170Sstever@eecs.umich.edu        else {
1843170Sstever@eecs.umich.edu            // no match: advance to next record
1853170Sstever@eecs.umich.edu            ++i;
1863170Sstever@eecs.umich.edu        }
1873170Sstever@eecs.umich.edu    }
1883170Sstever@eecs.umich.edu
1893170Sstever@eecs.umich.edu    if (isLocked) {
1903170Sstever@eecs.umich.edu        req->setScResult(success ? 1 : 0);
1913170Sstever@eecs.umich.edu    }
1923170Sstever@eecs.umich.edu
1933170Sstever@eecs.umich.edu    return success;
1943170Sstever@eecs.umich.edu}
1953170Sstever@eecs.umich.edu
1963029Ssaidi@eecs.umich.eduvoid
1973349Sbinkertn@umich.eduPhysicalMemory::doFunctionalAccess(PacketPtr pkt)
1982413SN/A{
1993749Sgblack@eecs.umich.edu    assert(pkt->getAddr() >= params()->addrRange.start &&
2003584Ssaidi@eecs.umich.edu           pkt->getAddr() + pkt->getSize() <= params()->addrRange.start +
2013584Ssaidi@eecs.umich.edu           params()->addrRange.size());
2022414SN/A
2033175Srdreslin@umich.edu    if (pkt->isRead()) {
2043170Sstever@eecs.umich.edu        if (pkt->req->isLocked()) {
2053170Sstever@eecs.umich.edu            trackLoadLocked(pkt->req);
2063170Sstever@eecs.umich.edu        }
2072641Sstever@eecs.umich.edu        memcpy(pkt->getPtr<uint8_t>(),
2083012Ssaidi@eecs.umich.edu               pmemAddr + pkt->getAddr() - params()->addrRange.start,
2092641Sstever@eecs.umich.edu               pkt->getSize());
2103879Ssaidi@eecs.umich.edu#if TRACING_ON
2113879Ssaidi@eecs.umich.edu        switch (pkt->getSize()) {
2123879Ssaidi@eecs.umich.edu          case sizeof(uint64_t):
2133879Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2143879Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint64_t>());
2153879Ssaidi@eecs.umich.edu            break;
2163879Ssaidi@eecs.umich.edu          case sizeof(uint32_t):
2173879Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2183879Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint32_t>());
2193879Ssaidi@eecs.umich.edu            break;
2203879Ssaidi@eecs.umich.edu          case sizeof(uint16_t):
2213879Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2223879Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint16_t>());
2233879Ssaidi@eecs.umich.edu            break;
2243879Ssaidi@eecs.umich.edu          case sizeof(uint8_t):
2253879Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2263879Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint8_t>());
2273879Ssaidi@eecs.umich.edu            break;
2283879Ssaidi@eecs.umich.edu          default:
2293879Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x\n",
2303879Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr());
2313879Ssaidi@eecs.umich.edu        }
2323879Ssaidi@eecs.umich.edu#endif
2333175Srdreslin@umich.edu    }
2343175Srdreslin@umich.edu    else if (pkt->isWrite()) {
2353170Sstever@eecs.umich.edu        if (writeOK(pkt->req)) {
2363170Sstever@eecs.umich.edu            memcpy(pmemAddr + pkt->getAddr() - params()->addrRange.start,
2373170Sstever@eecs.umich.edu                   pkt->getPtr<uint8_t>(), pkt->getSize());
2383879Ssaidi@eecs.umich.edu#if TRACING_ON
2393879Ssaidi@eecs.umich.edu            switch (pkt->getSize()) {
2403879Ssaidi@eecs.umich.edu              case sizeof(uint64_t):
2413879Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2423879Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint64_t>());
2433879Ssaidi@eecs.umich.edu                break;
2443879Ssaidi@eecs.umich.edu              case sizeof(uint32_t):
2453879Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2463879Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint32_t>());
2473879Ssaidi@eecs.umich.edu                break;
2483879Ssaidi@eecs.umich.edu              case sizeof(uint16_t):
2493879Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2503879Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint16_t>());
2513879Ssaidi@eecs.umich.edu                break;
2523879Ssaidi@eecs.umich.edu              case sizeof(uint8_t):
2533879Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2543879Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint8_t>());
2553879Ssaidi@eecs.umich.edu                break;
2563879Ssaidi@eecs.umich.edu              default:
2573879Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x\n",
2583879Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr());
2593879Ssaidi@eecs.umich.edu            }
2603879Ssaidi@eecs.umich.edu#endif
2612631SN/A        }
2623175Srdreslin@umich.edu    }
2633175Srdreslin@umich.edu    else if (pkt->isInvalidate()) {
2643175Srdreslin@umich.edu        //upgrade or invalidate
2653175Srdreslin@umich.edu        pkt->flags |= SATISFIED;
2663175Srdreslin@umich.edu    }
2673175Srdreslin@umich.edu    else {
2682413SN/A        panic("unimplemented");
2692413SN/A    }
2702420SN/A
2712641Sstever@eecs.umich.edu    pkt->result = Packet::Success;
2722413SN/A}
2732413SN/A
2742413SN/APort *
2752738Sstever@eecs.umich.eduPhysicalMemory::getPort(const std::string &if_name, int idx)
2762413SN/A{
2772738Sstever@eecs.umich.edu    if (if_name == "port" && idx == -1) {
2782499SN/A        if (port != NULL)
2792499SN/A           panic("PhysicalMemory::getPort: additional port requested to memory!");
2802640Sstever@eecs.umich.edu        port = new MemoryPort(name() + "-port", this);
2812499SN/A        return port;
2822519SN/A    } else if (if_name == "functional") {
2833196Srdreslin@umich.edu        /* special port for functional writes at startup. And for memtester */
2842640Sstever@eecs.umich.edu        return new MemoryPort(name() + "-funcport", this);
2852462SN/A    } else {
2862462SN/A        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
2872462SN/A    }
2882413SN/A}
2892413SN/A
2902413SN/Avoid
2912413SN/APhysicalMemory::recvStatusChange(Port::Status status)
2922413SN/A{
2932413SN/A}
2942413SN/A
2952640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
2962640Sstever@eecs.umich.edu                                       PhysicalMemory *_memory)
2972914Ssaidi@eecs.umich.edu    : SimpleTimingPort(_name), memory(_memory)
2982413SN/A{ }
2992413SN/A
3002413SN/Avoid
3012413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
3022413SN/A{
3032413SN/A    memory->recvStatusChange(status);
3042413SN/A}
3052413SN/A
3062413SN/Avoid
3072522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
3082522SN/A                                            AddrRangeList &snoop)
3092413SN/A{
3102522SN/A    memory->getAddressRanges(resp, snoop);
3112497SN/A}
3122497SN/A
3132497SN/Avoid
3142522SN/APhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
3152497SN/A{
3162522SN/A    snoop.clear();
3172522SN/A    resp.clear();
3183091Sstever@eecs.umich.edu    resp.push_back(RangeSize(params()->addrRange.start,
3193091Sstever@eecs.umich.edu                             params()->addrRange.size()));
3202413SN/A}
3212413SN/A
3222415SN/Aint
3232415SN/APhysicalMemory::MemoryPort::deviceBlockSize()
3242415SN/A{
3252415SN/A    return memory->deviceBlockSize();
3262415SN/A}
3272413SN/A
3282413SN/ATick
3293349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
3302413SN/A{
3313029Ssaidi@eecs.umich.edu    memory->doFunctionalAccess(pkt);
3323029Ssaidi@eecs.umich.edu    return memory->calculateLatency(pkt);
3332413SN/A}
3342413SN/A
3352413SN/Avoid
3363349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
3372413SN/A{
3383612Srdreslin@umich.edu    //Since we are overriding the function, make sure to have the impl of the
3393612Srdreslin@umich.edu    //check or functional accesses here.
3403612Srdreslin@umich.edu    std::list<std::pair<Tick,PacketPtr> >::iterator i = transmitList.begin();
3413612Srdreslin@umich.edu    std::list<std::pair<Tick,PacketPtr> >::iterator end = transmitList.end();
3423612Srdreslin@umich.edu    bool notDone = true;
3433612Srdreslin@umich.edu
3443612Srdreslin@umich.edu    while (i != end && notDone) {
3453612Srdreslin@umich.edu        PacketPtr target = i->second;
3463612Srdreslin@umich.edu        // If the target contains data, and it overlaps the
3473612Srdreslin@umich.edu        // probed request, need to update data
3483612Srdreslin@umich.edu        if (target->intersect(pkt))
3493612Srdreslin@umich.edu            notDone = fixPacket(pkt, target);
3503612Srdreslin@umich.edu        i++;
3513612Srdreslin@umich.edu    }
3523612Srdreslin@umich.edu
3533091Sstever@eecs.umich.edu    // Default implementation of SimpleTimingPort::recvFunctional()
3543091Sstever@eecs.umich.edu    // calls recvAtomic() and throws away the latency; we can save a
3553091Sstever@eecs.umich.edu    // little here by just not calculating the latency.
3562413SN/A    memory->doFunctionalAccess(pkt);
3572413SN/A}
3582413SN/A
3592914Ssaidi@eecs.umich.eduunsigned int
3602914Ssaidi@eecs.umich.eduPhysicalMemory::drain(Event *de)
3612914Ssaidi@eecs.umich.edu{
3622914Ssaidi@eecs.umich.edu    int count = port->drain(de);
3632914Ssaidi@eecs.umich.edu    if (count)
3642914Ssaidi@eecs.umich.edu        changeState(Draining);
3652914Ssaidi@eecs.umich.edu    else
3662914Ssaidi@eecs.umich.edu        changeState(Drained);
3672914Ssaidi@eecs.umich.edu    return count;
3682914Ssaidi@eecs.umich.edu}
3692413SN/A
3702391SN/Avoid
3712391SN/APhysicalMemory::serialize(ostream &os)
3722391SN/A{
3732391SN/A    gzFile compressedMem;
3742391SN/A    string filename = name() + ".physmem";
3752391SN/A
3762391SN/A    SERIALIZE_SCALAR(filename);
3772391SN/A
3782391SN/A    // write memory file
3792391SN/A    string thefile = Checkpoint::dir() + "/" + filename.c_str();
3802391SN/A    int fd = creat(thefile.c_str(), 0664);
3812391SN/A    if (fd < 0) {
3822391SN/A        perror("creat");
3832391SN/A        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
3842391SN/A    }
3852391SN/A
3862391SN/A    compressedMem = gzdopen(fd, "wb");
3872391SN/A    if (compressedMem == NULL)
3882391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
3892391SN/A                filename);
3902391SN/A
3913012Ssaidi@eecs.umich.edu    if (gzwrite(compressedMem, pmemAddr, params()->addrRange.size()) != params()->addrRange.size()) {
3922391SN/A        fatal("Write failed on physical memory checkpoint file '%s'\n",
3932391SN/A              filename);
3942391SN/A    }
3952391SN/A
3962391SN/A    if (gzclose(compressedMem))
3972391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
3982391SN/A              filename);
3992391SN/A}
4002391SN/A
4012391SN/Avoid
4022391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string &section)
4032391SN/A{
4042391SN/A    gzFile compressedMem;
4052391SN/A    long *tempPage;
4062391SN/A    long *pmem_current;
4072391SN/A    uint64_t curSize;
4082391SN/A    uint32_t bytesRead;
4092391SN/A    const int chunkSize = 16384;
4102391SN/A
4112391SN/A
4122391SN/A    string filename;
4132391SN/A
4142391SN/A    UNSERIALIZE_SCALAR(filename);
4152391SN/A
4162391SN/A    filename = cp->cptDir + "/" + filename;
4172391SN/A
4182391SN/A    // mmap memoryfile
4192391SN/A    int fd = open(filename.c_str(), O_RDONLY);
4202391SN/A    if (fd < 0) {
4212391SN/A        perror("open");
4222391SN/A        fatal("Can't open physical memory checkpoint file '%s'", filename);
4232391SN/A    }
4242391SN/A
4252391SN/A    compressedMem = gzdopen(fd, "rb");
4262391SN/A    if (compressedMem == NULL)
4272391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
4282391SN/A                filename);
4292391SN/A
4303012Ssaidi@eecs.umich.edu    // unmap file that was mmaped in the constructor
4313012Ssaidi@eecs.umich.edu    // This is done here to make sure that gzip and open don't muck with our
4323012Ssaidi@eecs.umich.edu    // nice large space of memory before we reallocate it
4333012Ssaidi@eecs.umich.edu    munmap(pmemAddr, params()->addrRange.size());
4342391SN/A
4353012Ssaidi@eecs.umich.edu    pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
4362391SN/A                                MAP_ANON | MAP_PRIVATE, -1, 0);
4372391SN/A
4383012Ssaidi@eecs.umich.edu    if (pmemAddr == (void *)MAP_FAILED) {
4392391SN/A        perror("mmap");
4402391SN/A        fatal("Could not mmap physical memory!\n");
4412391SN/A    }
4422391SN/A
4432391SN/A    curSize = 0;
4442391SN/A    tempPage = (long*)malloc(chunkSize);
4452391SN/A    if (tempPage == NULL)
4462391SN/A        fatal("Unable to malloc memory to read file %s\n", filename);
4472391SN/A
4482391SN/A    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
4493012Ssaidi@eecs.umich.edu    while (curSize < params()->addrRange.size()) {
4502391SN/A        bytesRead = gzread(compressedMem, tempPage, chunkSize);
4513012Ssaidi@eecs.umich.edu        if (bytesRead != chunkSize && bytesRead != params()->addrRange.size() - curSize)
4522391SN/A            fatal("Read failed on physical memory checkpoint file '%s'"
4532391SN/A                  " got %d bytes, expected %d or %d bytes\n",
4543012Ssaidi@eecs.umich.edu                  filename, bytesRead, chunkSize, params()->addrRange.size()-curSize);
4552391SN/A
4562391SN/A        assert(bytesRead % sizeof(long) == 0);
4572391SN/A
4582391SN/A        for (int x = 0; x < bytesRead/sizeof(long); x++)
4592391SN/A        {
4602391SN/A             if (*(tempPage+x) != 0) {
4613012Ssaidi@eecs.umich.edu                 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
4622391SN/A                 *pmem_current = *(tempPage+x);
4632391SN/A             }
4642391SN/A        }
4652391SN/A        curSize += bytesRead;
4662391SN/A    }
4672391SN/A
4682391SN/A    free(tempPage);
4692391SN/A
4702391SN/A    if (gzclose(compressedMem))
4712391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
4722391SN/A              filename);
4732391SN/A
4742391SN/A}
4752391SN/A
4762413SN/A
4772391SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
4782391SN/A
4792391SN/A    Param<string> file;
4802391SN/A    Param<Range<Addr> > range;
4812565SN/A    Param<Tick> latency;
4823751Sgblack@eecs.umich.edu    Param<bool> zero;
4832391SN/A
4842391SN/AEND_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
4852391SN/A
4862391SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
4872391SN/A
4882391SN/A    INIT_PARAM_DFLT(file, "memory mapped file", ""),
4892565SN/A    INIT_PARAM(range, "Device Address Range"),
4903751Sgblack@eecs.umich.edu    INIT_PARAM(latency, "Memory access latency"),
4913751Sgblack@eecs.umich.edu    INIT_PARAM(zero, "Zero initialize memory")
4922391SN/A
4932391SN/AEND_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
4942391SN/A
4952391SN/ACREATE_SIM_OBJECT(PhysicalMemory)
4962391SN/A{
4973012Ssaidi@eecs.umich.edu    PhysicalMemory::Params *p = new PhysicalMemory::Params;
4983012Ssaidi@eecs.umich.edu    p->name = getInstanceName();
4993012Ssaidi@eecs.umich.edu    p->addrRange = range;
5003012Ssaidi@eecs.umich.edu    p->latency = latency;
5013751Sgblack@eecs.umich.edu    p->zero = zero;
5023012Ssaidi@eecs.umich.edu    return new PhysicalMemory(p);
5032391SN/A}
5042391SN/A
5052391SN/AREGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
506