physical.cc revision 7730
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>
347730SAli.Saidi@ARM.com#include <sys/user.h>
352391SN/A#include <errno.h>
362391SN/A#include <fcntl.h>
372391SN/A#include <unistd.h>
382391SN/A#include <zlib.h>
392391SN/A
406712Snate@binkert.org#include <cstdio>
412391SN/A#include <iostream>
422391SN/A#include <string>
432391SN/A
446329Sgblack@eecs.umich.edu#include "arch/registers.hh"
457730SAli.Saidi@ARM.com#include "base/intmath.hh"
462391SN/A#include "base/misc.hh"
475399Ssaidi@eecs.umich.edu#include "base/random.hh"
486216Snate@binkert.org#include "base/types.hh"
492391SN/A#include "config/full_system.hh"
506658Snate@binkert.org#include "config/the_isa.hh"
513879Ssaidi@eecs.umich.edu#include "mem/packet_access.hh"
522394SN/A#include "mem/physical.hh"
532415SN/A#include "sim/eventq.hh"
542394SN/A
552391SN/Ausing namespace std;
562423SN/Ausing namespace TheISA;
572391SN/A
584762Snate@binkert.orgPhysicalMemory::PhysicalMemory(const Params *p)
595477Snate@binkert.org    : MemObject(p), pmemAddr(NULL), pagePtr(0),
605477Snate@binkert.org      lat(p->latency), lat_var(p->latency_var),
617730SAli.Saidi@ARM.com      _size(params()->range.size()), _start(params()->range.start)
622391SN/A{
637730SAli.Saidi@ARM.com    if (size() % TheISA::PageBytes != 0)
642391SN/A        panic("Memory Size not divisible by page size\n");
652391SN/A
665477Snate@binkert.org    if (params()->null)
675477Snate@binkert.org        return;
685477Snate@binkert.org
697730SAli.Saidi@ARM.com
707730SAli.Saidi@ARM.com    if (params()->file == "") {
717730SAli.Saidi@ARM.com        int map_flags = MAP_ANON | MAP_PRIVATE;
727730SAli.Saidi@ARM.com        pmemAddr = (uint8_t *)mmap(NULL, size(),
737730SAli.Saidi@ARM.com                                   PROT_READ | PROT_WRITE, map_flags, -1, 0);
747730SAli.Saidi@ARM.com    } else {
757730SAli.Saidi@ARM.com        int map_flags = MAP_PRIVATE;
767730SAli.Saidi@ARM.com        int fd = open(params()->file.c_str(), O_RDONLY);
777730SAli.Saidi@ARM.com        _size = lseek(fd, 0, SEEK_END);
787730SAli.Saidi@ARM.com        lseek(fd, 0, SEEK_SET);
797730SAli.Saidi@ARM.com        pmemAddr = (uint8_t *)mmap(NULL, roundUp(size(), PAGE_SIZE),
807730SAli.Saidi@ARM.com                                   PROT_READ | PROT_WRITE, map_flags, fd, 0);
817730SAli.Saidi@ARM.com    }
822391SN/A
833012Ssaidi@eecs.umich.edu    if (pmemAddr == (void *)MAP_FAILED) {
842391SN/A        perror("mmap");
857730SAli.Saidi@ARM.com        if (params()->file == "")
867730SAli.Saidi@ARM.com            fatal("Could not mmap!\n");
877730SAli.Saidi@ARM.com        else
887730SAli.Saidi@ARM.com            fatal("Could not find file: %s\n", params()->file);
892391SN/A    }
902391SN/A
913751Sgblack@eecs.umich.edu    //If requested, initialize all the memory to 0
924762Snate@binkert.org    if (p->zero)
937730SAli.Saidi@ARM.com        memset(pmemAddr, 0, size());
942391SN/A}
952391SN/A
962541SN/Avoid
972541SN/APhysicalMemory::init()
982541SN/A{
994470Sstever@eecs.umich.edu    if (ports.size() == 0) {
1004470Sstever@eecs.umich.edu        fatal("PhysicalMemory object %s is unconnected!", name());
1014470Sstever@eecs.umich.edu    }
1024470Sstever@eecs.umich.edu
1034467Sstever@eecs.umich.edu    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
1044467Sstever@eecs.umich.edu        if (*pi)
1054467Sstever@eecs.umich.edu            (*pi)->sendStatusChange(Port::RangeChange);
1064467Sstever@eecs.umich.edu    }
1072541SN/A}
1082541SN/A
1092391SN/APhysicalMemory::~PhysicalMemory()
1102391SN/A{
1113012Ssaidi@eecs.umich.edu    if (pmemAddr)
1127730SAli.Saidi@ARM.com        munmap((char*)pmemAddr, size());
1132391SN/A}
1142391SN/A
1152391SN/AAddr
1162391SN/APhysicalMemory::new_page()
1172391SN/A{
1183012Ssaidi@eecs.umich.edu    Addr return_addr = pagePtr << LogVMPageSize;
1194040Ssaidi@eecs.umich.edu    return_addr += start();
1202391SN/A
1213012Ssaidi@eecs.umich.edu    ++pagePtr;
1222391SN/A    return return_addr;
1232391SN/A}
1242391SN/A
1256227Snate@binkert.orgunsigned
1266227Snate@binkert.orgPhysicalMemory::deviceBlockSize() const
1272408SN/A{
1282409SN/A    //Can accept anysize request
1292409SN/A    return 0;
1302408SN/A}
1312408SN/A
1323012Ssaidi@eecs.umich.eduTick
1333349Sbinkertn@umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt)
1343012Ssaidi@eecs.umich.edu{
1355399Ssaidi@eecs.umich.edu    Tick latency = lat;
1365399Ssaidi@eecs.umich.edu    if (lat_var != 0)
1375399Ssaidi@eecs.umich.edu        latency += random_mt.random<Tick>(0, lat_var);
1385399Ssaidi@eecs.umich.edu    return latency;
1393012Ssaidi@eecs.umich.edu}
1402413SN/A
1413170Sstever@eecs.umich.edu
1423170Sstever@eecs.umich.edu
1433170Sstever@eecs.umich.edu// Add load-locked to tracking list.  Should only be called if the
1446076Sgblack@eecs.umich.edu// operation is a load and the LLSC flag is set.
1453170Sstever@eecs.umich.eduvoid
1464626Sstever@eecs.umich.eduPhysicalMemory::trackLoadLocked(PacketPtr pkt)
1473170Sstever@eecs.umich.edu{
1484626Sstever@eecs.umich.edu    Request *req = pkt->req;
1493170Sstever@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1503170Sstever@eecs.umich.edu
1513170Sstever@eecs.umich.edu    // first we check if we already have a locked addr for this
1523170Sstever@eecs.umich.edu    // xc.  Since each xc only gets one, we just update the
1533170Sstever@eecs.umich.edu    // existing record with the new address.
1543170Sstever@eecs.umich.edu    list<LockedAddr>::iterator i;
1553170Sstever@eecs.umich.edu
1563170Sstever@eecs.umich.edu    for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
1573170Sstever@eecs.umich.edu        if (i->matchesContext(req)) {
1585714Shsul@eecs.umich.edu            DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
1595714Shsul@eecs.umich.edu                    req->contextId(), paddr);
1603170Sstever@eecs.umich.edu            i->addr = paddr;
1613170Sstever@eecs.umich.edu            return;
1623170Sstever@eecs.umich.edu        }
1633170Sstever@eecs.umich.edu    }
1643170Sstever@eecs.umich.edu
1653170Sstever@eecs.umich.edu    // no record for this xc: need to allocate a new one
1665714Shsul@eecs.umich.edu    DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
1675714Shsul@eecs.umich.edu            req->contextId(), paddr);
1683170Sstever@eecs.umich.edu    lockedAddrList.push_front(LockedAddr(req));
1693170Sstever@eecs.umich.edu}
1703170Sstever@eecs.umich.edu
1713170Sstever@eecs.umich.edu
1723170Sstever@eecs.umich.edu// Called on *writes* only... both regular stores and
1733170Sstever@eecs.umich.edu// store-conditional operations.  Check for conventional stores which
1743170Sstever@eecs.umich.edu// conflict with locked addresses, and for success/failure of store
1753170Sstever@eecs.umich.edu// conditionals.
1763170Sstever@eecs.umich.edubool
1774626Sstever@eecs.umich.eduPhysicalMemory::checkLockedAddrList(PacketPtr pkt)
1783170Sstever@eecs.umich.edu{
1794626Sstever@eecs.umich.edu    Request *req = pkt->req;
1803170Sstever@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1816102Sgblack@eecs.umich.edu    bool isLLSC = pkt->isLLSC();
1823170Sstever@eecs.umich.edu
1833170Sstever@eecs.umich.edu    // Initialize return value.  Non-conditional stores always
1843170Sstever@eecs.umich.edu    // succeed.  Assume conditional stores will fail until proven
1853170Sstever@eecs.umich.edu    // otherwise.
1866102Sgblack@eecs.umich.edu    bool success = !isLLSC;
1873170Sstever@eecs.umich.edu
1883170Sstever@eecs.umich.edu    // Iterate over list.  Note that there could be multiple matching
1893170Sstever@eecs.umich.edu    // records, as more than one context could have done a load locked
1903170Sstever@eecs.umich.edu    // to this location.
1913170Sstever@eecs.umich.edu    list<LockedAddr>::iterator i = lockedAddrList.begin();
1923170Sstever@eecs.umich.edu
1933170Sstever@eecs.umich.edu    while (i != lockedAddrList.end()) {
1943170Sstever@eecs.umich.edu
1953170Sstever@eecs.umich.edu        if (i->addr == paddr) {
1963170Sstever@eecs.umich.edu            // we have a matching address
1973170Sstever@eecs.umich.edu
1986102Sgblack@eecs.umich.edu            if (isLLSC && i->matchesContext(req)) {
1993170Sstever@eecs.umich.edu                // it's a store conditional, and as far as the memory
2003170Sstever@eecs.umich.edu                // system can tell, the requesting context's lock is
2013170Sstever@eecs.umich.edu                // still valid.
2025714Shsul@eecs.umich.edu                DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
2035714Shsul@eecs.umich.edu                        req->contextId(), paddr);
2043170Sstever@eecs.umich.edu                success = true;
2053170Sstever@eecs.umich.edu            }
2063170Sstever@eecs.umich.edu
2073170Sstever@eecs.umich.edu            // Get rid of our record of this lock and advance to next
2085714Shsul@eecs.umich.edu            DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
2095714Shsul@eecs.umich.edu                    i->contextId, paddr);
2103170Sstever@eecs.umich.edu            i = lockedAddrList.erase(i);
2113170Sstever@eecs.umich.edu        }
2123170Sstever@eecs.umich.edu        else {
2133170Sstever@eecs.umich.edu            // no match: advance to next record
2143170Sstever@eecs.umich.edu            ++i;
2153170Sstever@eecs.umich.edu        }
2163170Sstever@eecs.umich.edu    }
2173170Sstever@eecs.umich.edu
2186102Sgblack@eecs.umich.edu    if (isLLSC) {
2194040Ssaidi@eecs.umich.edu        req->setExtraData(success ? 1 : 0);
2203170Sstever@eecs.umich.edu    }
2213170Sstever@eecs.umich.edu
2223170Sstever@eecs.umich.edu    return success;
2233170Sstever@eecs.umich.edu}
2243170Sstever@eecs.umich.edu
2254626Sstever@eecs.umich.edu
2264626Sstever@eecs.umich.edu#if TRACING_ON
2274626Sstever@eecs.umich.edu
2284626Sstever@eecs.umich.edu#define CASE(A, T)                                                      \
2294626Sstever@eecs.umich.edu  case sizeof(T):                                                       \
2306429Ssteve.reinhardt@amd.com    DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n",   \
2316429Ssteve.reinhardt@amd.com            A, pkt->getSize(), pkt->getAddr(), pkt->get<T>());          \
2324626Sstever@eecs.umich.edu  break
2334626Sstever@eecs.umich.edu
2344626Sstever@eecs.umich.edu
2354626Sstever@eecs.umich.edu#define TRACE_PACKET(A)                                                 \
2364626Sstever@eecs.umich.edu    do {                                                                \
2374626Sstever@eecs.umich.edu        switch (pkt->getSize()) {                                       \
2384626Sstever@eecs.umich.edu          CASE(A, uint64_t);                                            \
2394626Sstever@eecs.umich.edu          CASE(A, uint32_t);                                            \
2404626Sstever@eecs.umich.edu          CASE(A, uint16_t);                                            \
2414626Sstever@eecs.umich.edu          CASE(A, uint8_t);                                             \
2424626Sstever@eecs.umich.edu          default:                                                      \
2436429Ssteve.reinhardt@amd.com            DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n",    \
2446429Ssteve.reinhardt@amd.com                    A, pkt->getSize(), pkt->getAddr());                 \
2454626Sstever@eecs.umich.edu        }                                                               \
2464626Sstever@eecs.umich.edu    } while (0)
2474626Sstever@eecs.umich.edu
2484626Sstever@eecs.umich.edu#else
2494626Sstever@eecs.umich.edu
2504626Sstever@eecs.umich.edu#define TRACE_PACKET(A)
2514626Sstever@eecs.umich.edu
2524626Sstever@eecs.umich.edu#endif
2534626Sstever@eecs.umich.edu
2544626Sstever@eecs.umich.eduTick
2554626Sstever@eecs.umich.eduPhysicalMemory::doAtomicAccess(PacketPtr pkt)
2562413SN/A{
2574040Ssaidi@eecs.umich.edu    assert(pkt->getAddr() >= start() &&
2584040Ssaidi@eecs.umich.edu           pkt->getAddr() + pkt->getSize() <= start() + size());
2592414SN/A
2604626Sstever@eecs.umich.edu    if (pkt->memInhibitAsserted()) {
2614626Sstever@eecs.umich.edu        DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
2624626Sstever@eecs.umich.edu                pkt->getAddr());
2634626Sstever@eecs.umich.edu        return 0;
2643175Srdreslin@umich.edu    }
2654626Sstever@eecs.umich.edu
2664626Sstever@eecs.umich.edu    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
2674626Sstever@eecs.umich.edu
2684626Sstever@eecs.umich.edu    if (pkt->cmd == MemCmd::SwapReq) {
2694040Ssaidi@eecs.umich.edu        IntReg overwrite_val;
2704040Ssaidi@eecs.umich.edu        bool overwrite_mem;
2714040Ssaidi@eecs.umich.edu        uint64_t condition_val64;
2724040Ssaidi@eecs.umich.edu        uint32_t condition_val32;
2734040Ssaidi@eecs.umich.edu
2745477Snate@binkert.org        if (!pmemAddr)
2755477Snate@binkert.org            panic("Swap only works if there is real memory (i.e. null=False)");
2764040Ssaidi@eecs.umich.edu        assert(sizeof(IntReg) >= pkt->getSize());
2774040Ssaidi@eecs.umich.edu
2784040Ssaidi@eecs.umich.edu        overwrite_mem = true;
2794040Ssaidi@eecs.umich.edu        // keep a copy of our possible write value, and copy what is at the
2804040Ssaidi@eecs.umich.edu        // memory address into the packet
2814052Ssaidi@eecs.umich.edu        std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
2824626Sstever@eecs.umich.edu        std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
2834040Ssaidi@eecs.umich.edu
2844040Ssaidi@eecs.umich.edu        if (pkt->req->isCondSwap()) {
2854040Ssaidi@eecs.umich.edu            if (pkt->getSize() == sizeof(uint64_t)) {
2864052Ssaidi@eecs.umich.edu                condition_val64 = pkt->req->getExtraData();
2874626Sstever@eecs.umich.edu                overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
2884626Sstever@eecs.umich.edu                                             sizeof(uint64_t));
2894040Ssaidi@eecs.umich.edu            } else if (pkt->getSize() == sizeof(uint32_t)) {
2904052Ssaidi@eecs.umich.edu                condition_val32 = (uint32_t)pkt->req->getExtraData();
2914626Sstever@eecs.umich.edu                overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
2924626Sstever@eecs.umich.edu                                             sizeof(uint32_t));
2934040Ssaidi@eecs.umich.edu            } else
2944040Ssaidi@eecs.umich.edu                panic("Invalid size for conditional read/write\n");
2954040Ssaidi@eecs.umich.edu        }
2964040Ssaidi@eecs.umich.edu
2974040Ssaidi@eecs.umich.edu        if (overwrite_mem)
2984626Sstever@eecs.umich.edu            std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
2994040Ssaidi@eecs.umich.edu
3006429Ssteve.reinhardt@amd.com        assert(!pkt->req->isInstFetch());
3014626Sstever@eecs.umich.edu        TRACE_PACKET("Read/Write");
3024626Sstever@eecs.umich.edu    } else if (pkt->isRead()) {
3034626Sstever@eecs.umich.edu        assert(!pkt->isWrite());
3046102Sgblack@eecs.umich.edu        if (pkt->isLLSC()) {
3054626Sstever@eecs.umich.edu            trackLoadLocked(pkt);
3064040Ssaidi@eecs.umich.edu        }
3075477Snate@binkert.org        if (pmemAddr)
3085477Snate@binkert.org            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
3096429Ssteve.reinhardt@amd.com        TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
3104626Sstever@eecs.umich.edu    } else if (pkt->isWrite()) {
3114626Sstever@eecs.umich.edu        if (writeOK(pkt)) {
3125477Snate@binkert.org            if (pmemAddr)
3135477Snate@binkert.org                memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
3146429Ssteve.reinhardt@amd.com            assert(!pkt->req->isInstFetch());
3154626Sstever@eecs.umich.edu            TRACE_PACKET("Write");
3164626Sstever@eecs.umich.edu        }
3174626Sstever@eecs.umich.edu    } else if (pkt->isInvalidate()) {
3184626Sstever@eecs.umich.edu        //upgrade or invalidate
3194626Sstever@eecs.umich.edu        if (pkt->needsResponse()) {
3204626Sstever@eecs.umich.edu            pkt->makeAtomicResponse();
3214626Sstever@eecs.umich.edu        }
3224040Ssaidi@eecs.umich.edu    } else {
3232413SN/A        panic("unimplemented");
3242413SN/A    }
3252420SN/A
3264626Sstever@eecs.umich.edu    if (pkt->needsResponse()) {
3274626Sstever@eecs.umich.edu        pkt->makeAtomicResponse();
3284626Sstever@eecs.umich.edu    }
3294626Sstever@eecs.umich.edu    return calculateLatency(pkt);
3302413SN/A}
3312413SN/A
3324626Sstever@eecs.umich.edu
3334626Sstever@eecs.umich.eduvoid
3344626Sstever@eecs.umich.eduPhysicalMemory::doFunctionalAccess(PacketPtr pkt)
3354626Sstever@eecs.umich.edu{
3364626Sstever@eecs.umich.edu    assert(pkt->getAddr() >= start() &&
3374626Sstever@eecs.umich.edu           pkt->getAddr() + pkt->getSize() <= start() + size());
3384626Sstever@eecs.umich.edu
3395222Sksewell@umich.edu
3404626Sstever@eecs.umich.edu    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
3414626Sstever@eecs.umich.edu
3425314Sstever@gmail.com    if (pkt->isRead()) {
3435477Snate@binkert.org        if (pmemAddr)
3445477Snate@binkert.org            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
3454626Sstever@eecs.umich.edu        TRACE_PACKET("Read");
3465314Sstever@gmail.com        pkt->makeAtomicResponse();
3475314Sstever@gmail.com    } else if (pkt->isWrite()) {
3485477Snate@binkert.org        if (pmemAddr)
3495477Snate@binkert.org            memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
3504626Sstever@eecs.umich.edu        TRACE_PACKET("Write");
3515314Sstever@gmail.com        pkt->makeAtomicResponse();
3525314Sstever@gmail.com    } else if (pkt->isPrint()) {
3535315Sstever@gmail.com        Packet::PrintReqState *prs =
3545315Sstever@gmail.com            dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
3555315Sstever@gmail.com        // Need to call printLabels() explicitly since we're not going
3565315Sstever@gmail.com        // through printObj().
3575314Sstever@gmail.com        prs->printLabels();
3585315Sstever@gmail.com        // Right now we just print the single byte at the specified address.
3595314Sstever@gmail.com        ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
3604626Sstever@eecs.umich.edu    } else {
3614626Sstever@eecs.umich.edu        panic("PhysicalMemory: unimplemented functional command %s",
3624626Sstever@eecs.umich.edu              pkt->cmdString());
3634626Sstever@eecs.umich.edu    }
3644490Sstever@eecs.umich.edu}
3654490Sstever@eecs.umich.edu
3664626Sstever@eecs.umich.edu
3672413SN/APort *
3682738Sstever@eecs.umich.eduPhysicalMemory::getPort(const std::string &if_name, int idx)
3692413SN/A{
3704468Sstever@eecs.umich.edu    // Accept request for "functional" port for backwards compatibility
3714468Sstever@eecs.umich.edu    // with places where this function is called from C++.  I'd prefer
3724468Sstever@eecs.umich.edu    // to move all these into Python someday.
3734468Sstever@eecs.umich.edu    if (if_name == "functional") {
3744468Sstever@eecs.umich.edu        return new MemoryPort(csprintf("%s-functional", name()), this);
3754468Sstever@eecs.umich.edu    }
3764468Sstever@eecs.umich.edu
3774467Sstever@eecs.umich.edu    if (if_name != "port") {
3782462SN/A        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
3792462SN/A    }
3804467Sstever@eecs.umich.edu
3816227Snate@binkert.org    if (idx >= (int)ports.size()) {
3826227Snate@binkert.org        ports.resize(idx + 1);
3834467Sstever@eecs.umich.edu    }
3844467Sstever@eecs.umich.edu
3854467Sstever@eecs.umich.edu    if (ports[idx] != NULL) {
3864467Sstever@eecs.umich.edu        panic("PhysicalMemory::getPort: port %d already assigned", idx);
3874467Sstever@eecs.umich.edu    }
3884467Sstever@eecs.umich.edu
3894467Sstever@eecs.umich.edu    MemoryPort *port =
3904467Sstever@eecs.umich.edu        new MemoryPort(csprintf("%s-port%d", name(), idx), this);
3914467Sstever@eecs.umich.edu
3924467Sstever@eecs.umich.edu    ports[idx] = port;
3934467Sstever@eecs.umich.edu    return port;
3942413SN/A}
3952413SN/A
3964467Sstever@eecs.umich.edu
3972413SN/Avoid
3982413SN/APhysicalMemory::recvStatusChange(Port::Status status)
3992413SN/A{
4002413SN/A}
4012413SN/A
4022640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
4032640Sstever@eecs.umich.edu                                       PhysicalMemory *_memory)
4045606Snate@binkert.org    : SimpleTimingPort(_name, _memory), memory(_memory)
4052413SN/A{ }
4062413SN/A
4072413SN/Avoid
4082413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
4092413SN/A{
4102413SN/A    memory->recvStatusChange(status);
4112413SN/A}
4122413SN/A
4132413SN/Avoid
4142522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
4154475Sstever@eecs.umich.edu                                                   bool &snoop)
4162413SN/A{
4172522SN/A    memory->getAddressRanges(resp, snoop);
4182497SN/A}
4192497SN/A
4202497SN/Avoid
4214475Sstever@eecs.umich.eduPhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
4222497SN/A{
4234475Sstever@eecs.umich.edu    snoop = false;
4242522SN/A    resp.clear();
4257730SAli.Saidi@ARM.com    resp.push_back(RangeSize(start(), size()));
4262413SN/A}
4272413SN/A
4286227Snate@binkert.orgunsigned
4296227Snate@binkert.orgPhysicalMemory::MemoryPort::deviceBlockSize() const
4302415SN/A{
4312415SN/A    return memory->deviceBlockSize();
4322415SN/A}
4332413SN/A
4342413SN/ATick
4353349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
4362413SN/A{
4374626Sstever@eecs.umich.edu    return memory->doAtomicAccess(pkt);
4382413SN/A}
4392413SN/A
4402413SN/Avoid
4413349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
4422413SN/A{
4435314Sstever@gmail.com    pkt->pushLabel(memory->name());
4445314Sstever@gmail.com
4454929Sstever@gmail.com    if (!checkFunctional(pkt)) {
4464929Sstever@gmail.com        // Default implementation of SimpleTimingPort::recvFunctional()
4474929Sstever@gmail.com        // calls recvAtomic() and throws away the latency; we can save a
4484929Sstever@gmail.com        // little here by just not calculating the latency.
4494929Sstever@gmail.com        memory->doFunctionalAccess(pkt);
4504929Sstever@gmail.com    }
4515314Sstever@gmail.com
4525314Sstever@gmail.com    pkt->popLabel();
4532413SN/A}
4542413SN/A
4552914Ssaidi@eecs.umich.eduunsigned int
4562914Ssaidi@eecs.umich.eduPhysicalMemory::drain(Event *de)
4572914Ssaidi@eecs.umich.edu{
4584467Sstever@eecs.umich.edu    int count = 0;
4594467Sstever@eecs.umich.edu    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
4604467Sstever@eecs.umich.edu        count += (*pi)->drain(de);
4614467Sstever@eecs.umich.edu    }
4624467Sstever@eecs.umich.edu
4632914Ssaidi@eecs.umich.edu    if (count)
4642914Ssaidi@eecs.umich.edu        changeState(Draining);
4652914Ssaidi@eecs.umich.edu    else
4662914Ssaidi@eecs.umich.edu        changeState(Drained);
4672914Ssaidi@eecs.umich.edu    return count;
4682914Ssaidi@eecs.umich.edu}
4692413SN/A
4702391SN/Avoid
4712391SN/APhysicalMemory::serialize(ostream &os)
4722391SN/A{
4735477Snate@binkert.org    if (!pmemAddr)
4745477Snate@binkert.org        return;
4755477Snate@binkert.org
4762391SN/A    gzFile compressedMem;
4772391SN/A    string filename = name() + ".physmem";
4782391SN/A
4792391SN/A    SERIALIZE_SCALAR(filename);
4807730SAli.Saidi@ARM.com    SERIALIZE_SCALAR(_size);
4812391SN/A
4822391SN/A    // write memory file
4832391SN/A    string thefile = Checkpoint::dir() + "/" + filename.c_str();
4842391SN/A    int fd = creat(thefile.c_str(), 0664);
4852391SN/A    if (fd < 0) {
4862391SN/A        perror("creat");
4872391SN/A        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
4882391SN/A    }
4892391SN/A
4902391SN/A    compressedMem = gzdopen(fd, "wb");
4912391SN/A    if (compressedMem == NULL)
4922391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
4932391SN/A                filename);
4942391SN/A
4957730SAli.Saidi@ARM.com    if (gzwrite(compressedMem, pmemAddr, size()) != (int)size()) {
4962391SN/A        fatal("Write failed on physical memory checkpoint file '%s'\n",
4972391SN/A              filename);
4982391SN/A    }
4992391SN/A
5002391SN/A    if (gzclose(compressedMem))
5012391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
5022391SN/A              filename);
5032391SN/A}
5042391SN/A
5052391SN/Avoid
5062391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string &section)
5072391SN/A{
5085477Snate@binkert.org    if (!pmemAddr)
5095477Snate@binkert.org        return;
5105477Snate@binkert.org
5112391SN/A    gzFile compressedMem;
5122391SN/A    long *tempPage;
5132391SN/A    long *pmem_current;
5142391SN/A    uint64_t curSize;
5152391SN/A    uint32_t bytesRead;
5166227Snate@binkert.org    const uint32_t chunkSize = 16384;
5172391SN/A
5182391SN/A    string filename;
5192391SN/A
5202391SN/A    UNSERIALIZE_SCALAR(filename);
5212391SN/A
5222391SN/A    filename = cp->cptDir + "/" + filename;
5232391SN/A
5242391SN/A    // mmap memoryfile
5252391SN/A    int fd = open(filename.c_str(), O_RDONLY);
5262391SN/A    if (fd < 0) {
5272391SN/A        perror("open");
5282391SN/A        fatal("Can't open physical memory checkpoint file '%s'", filename);
5292391SN/A    }
5302391SN/A
5312391SN/A    compressedMem = gzdopen(fd, "rb");
5322391SN/A    if (compressedMem == NULL)
5332391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
5342391SN/A                filename);
5352391SN/A
5363012Ssaidi@eecs.umich.edu    // unmap file that was mmaped in the constructor
5373012Ssaidi@eecs.umich.edu    // This is done here to make sure that gzip and open don't muck with our
5383012Ssaidi@eecs.umich.edu    // nice large space of memory before we reallocate it
5397730SAli.Saidi@ARM.com    munmap((char*)pmemAddr, size());
5402391SN/A
5417730SAli.Saidi@ARM.com    UNSERIALIZE_SCALAR(_size);
5427730SAli.Saidi@ARM.com    if (size() > params()->range.size())
5437730SAli.Saidi@ARM.com        fatal("Memory size has changed!\n");
5447730SAli.Saidi@ARM.com
5457730SAli.Saidi@ARM.com    pmemAddr = (uint8_t *)mmap(NULL, size(),
5464762Snate@binkert.org        PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
5472391SN/A
5483012Ssaidi@eecs.umich.edu    if (pmemAddr == (void *)MAP_FAILED) {
5492391SN/A        perror("mmap");
5502391SN/A        fatal("Could not mmap physical memory!\n");
5512391SN/A    }
5522391SN/A
5532391SN/A    curSize = 0;
5542391SN/A    tempPage = (long*)malloc(chunkSize);
5552391SN/A    if (tempPage == NULL)
5562391SN/A        fatal("Unable to malloc memory to read file %s\n", filename);
5572391SN/A
5582391SN/A    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
5597730SAli.Saidi@ARM.com    while (curSize < size()) {
5602391SN/A        bytesRead = gzread(compressedMem, tempPage, chunkSize);
5616820SLisa.Hsu@amd.com        if (bytesRead == 0)
5626820SLisa.Hsu@amd.com            break;
5632391SN/A
5642391SN/A        assert(bytesRead % sizeof(long) == 0);
5652391SN/A
5666227Snate@binkert.org        for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
5672391SN/A        {
5682391SN/A             if (*(tempPage+x) != 0) {
5693012Ssaidi@eecs.umich.edu                 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
5702391SN/A                 *pmem_current = *(tempPage+x);
5712391SN/A             }
5722391SN/A        }
5732391SN/A        curSize += bytesRead;
5742391SN/A    }
5752391SN/A
5762391SN/A    free(tempPage);
5772391SN/A
5782391SN/A    if (gzclose(compressedMem))
5792391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
5802391SN/A              filename);
5812391SN/A
5822391SN/A}
5832391SN/A
5844762Snate@binkert.orgPhysicalMemory *
5854762Snate@binkert.orgPhysicalMemoryParams::create()
5862391SN/A{
5874762Snate@binkert.org    return new PhysicalMemory(this);
5882391SN/A}
589