abstract_mem.cc revision 4490
1545SN/A/*
22512SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
3545SN/A * All rights reserved.
4545SN/A *
5545SN/A * Redistribution and use in source and binary forms, with or without
6545SN/A * modification, are permitted provided that the following conditions are
7545SN/A * met: redistributions of source code must retain the above copyright
8545SN/A * notice, this list of conditions and the following disclaimer;
9545SN/A * redistributions in binary form must reproduce the above copyright
10545SN/A * notice, this list of conditions and the following disclaimer in the
11545SN/A * documentation and/or other materials provided with the distribution;
12545SN/A * neither the name of the copyright holders nor the names of its
13545SN/A * contributors may be used to endorse or promote products derived from
14545SN/A * this software without specific prior written permission.
15545SN/A *
16545SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17545SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18545SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19545SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20545SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21545SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22545SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23545SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24545SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25545SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26545SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski
292665Ssaidi@eecs.umich.edu *          Ali Saidi
30545SN/A */
31545SN/A
322657Ssaidi@eecs.umich.edu#include <sys/types.h>
33545SN/A#include <sys/mman.h>
34679SN/A#include <errno.h>
352901Ssaidi@eecs.umich.edu#include <fcntl.h>
36545SN/A#include <unistd.h>
372489SN/A#include <zlib.h>
382901Ssaidi@eecs.umich.edu
392901Ssaidi@eecs.umich.edu#include <iostream>
402901Ssaidi@eecs.umich.edu#include <string>
412489SN/A
422489SN/A#include "arch/isa_traits.hh"
432489SN/A#include "base/misc.hh"
442489SN/A#include "config/full_system.hh"
452630SN/A#include "mem/packet_access.hh"
462489SN/A#include "mem/physical.hh"
472489SN/A#include "sim/builder.hh"
482489SN/A#include "sim/eventq.hh"
492489SN/A#include "sim/host.hh"
502489SN/A
512630SN/Ausing namespace std;
522489SN/Ausing namespace TheISA;
532489SN/A
542489SN/APhysicalMemory::PhysicalMemory(Params *p)
552489SN/A    : MemObject(p->name), pmemAddr(NULL), lat(p->latency), _params(p)
562489SN/A{
572521SN/A    if (params()->addrRange.size() % TheISA::PageBytes != 0)
582489SN/A        panic("Memory Size not divisible by page size\n");
592521SN/A
602521SN/A    int map_flags = MAP_ANON | MAP_PRIVATE;
612489SN/A    pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
622489SN/A            map_flags, -1, 0);
632489SN/A
642657Ssaidi@eecs.umich.edu    if (pmemAddr == (void *)MAP_FAILED) {
652489SN/A        perror("mmap");
662489SN/A        fatal("Could not mmap!\n");
672784Ssaidi@eecs.umich.edu    }
682784Ssaidi@eecs.umich.edu
692784Ssaidi@eecs.umich.edu    //If requested, initialize all the memory to 0
702784Ssaidi@eecs.umich.edu    if(params()->zero)
712784Ssaidi@eecs.umich.edu        memset(pmemAddr, 0, params()->addrRange.size());
722657Ssaidi@eecs.umich.edu
732901Ssaidi@eecs.umich.edu    pagePtr = 0;
742901Ssaidi@eecs.umich.edu}
752901Ssaidi@eecs.umich.edu
762901Ssaidi@eecs.umich.eduvoid
772489SN/APhysicalMemory::init()
782489SN/A{
792384SN/A    if (ports.size() == 0) {
802384SN/A        fatal("PhysicalMemory object %s is unconnected!", name());
812384SN/A    }
822901Ssaidi@eecs.umich.edu
832901Ssaidi@eecs.umich.edu    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
842641Sstever@eecs.umich.edu        if (*pi)
852901Ssaidi@eecs.umich.edu            (*pi)->sendStatusChange(Port::RangeChange);
862901Ssaidi@eecs.umich.edu    }
872901Ssaidi@eecs.umich.edu}
882901Ssaidi@eecs.umich.edu
892901Ssaidi@eecs.umich.eduPhysicalMemory::~PhysicalMemory()
902384SN/A{
912630SN/A    if (pmemAddr)
922384SN/A        munmap((char*)pmemAddr, params()->addrRange.size());
932384SN/A    //Remove memPorts?
942846Ssaidi@eecs.umich.edu}
952846Ssaidi@eecs.umich.edu
962846Ssaidi@eecs.umich.eduAddr
972846Ssaidi@eecs.umich.eduPhysicalMemory::new_page()
982846Ssaidi@eecs.umich.edu{
992846Ssaidi@eecs.umich.edu    Addr return_addr = pagePtr << LogVMPageSize;
1002846Ssaidi@eecs.umich.edu    return_addr += start();
1012846Ssaidi@eecs.umich.edu
1022846Ssaidi@eecs.umich.edu    ++pagePtr;
1032846Ssaidi@eecs.umich.edu    return return_addr;
1042384SN/A}
1052657Ssaidi@eecs.umich.edu
1062384SN/Aint
1072630SN/APhysicalMemory::deviceBlockSize()
1082384SN/A{
1092784Ssaidi@eecs.umich.edu    //Can accept anysize request
1102846Ssaidi@eecs.umich.edu    return 0;
1112784Ssaidi@eecs.umich.edu}
1122784Ssaidi@eecs.umich.edu
1132784Ssaidi@eecs.umich.eduTick
1142784Ssaidi@eecs.umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt)
1152784Ssaidi@eecs.umich.edu{
1162784Ssaidi@eecs.umich.edu    return lat;
1172641Sstever@eecs.umich.edu}
1182384SN/A
119545SN/A
1202901Ssaidi@eecs.umich.edu
1212901Ssaidi@eecs.umich.edu// Add load-locked to tracking list.  Should only be called if the
1222901Ssaidi@eecs.umich.edu// operation is a load and the LOCKED flag is set.
1232901Ssaidi@eecs.umich.eduvoid
1242901Ssaidi@eecs.umich.eduPhysicalMemory::trackLoadLocked(Request *req)
1252901Ssaidi@eecs.umich.edu{
1262901Ssaidi@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1272901Ssaidi@eecs.umich.edu
1282901Ssaidi@eecs.umich.edu    // first we check if we already have a locked addr for this
129545SN/A    // xc.  Since each xc only gets one, we just update the
130545SN/A    // existing record with the new address.
1312384SN/A    list<LockedAddr>::iterator i;
1322489SN/A
133545SN/A    for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
134545SN/A        if (i->matchesContext(req)) {
1352542SN/A            DPRINTF(LLSC, "Modifying lock record: cpu %d thread %d addr %#x\n",
1362541SN/A                    req->getCpuNum(), req->getThreadNum(), paddr);
1372541SN/A            i->addr = paddr;
1382541SN/A            return;
1392541SN/A        }
1402541SN/A    }
1412541SN/A
1422541SN/A    // no record for this xc: need to allocate a new one
1432901Ssaidi@eecs.umich.edu    DPRINTF(LLSC, "Adding lock record: cpu %d thread %d addr %#x\n",
1442901Ssaidi@eecs.umich.edu            req->getCpuNum(), req->getThreadNum(), paddr);
1452901Ssaidi@eecs.umich.edu    lockedAddrList.push_front(LockedAddr(req));
1462901Ssaidi@eecs.umich.edu}
1472901Ssaidi@eecs.umich.edu
1482901Ssaidi@eecs.umich.edu
1492901Ssaidi@eecs.umich.edu// Called on *writes* only... both regular stores and
1502901Ssaidi@eecs.umich.edu// store-conditional operations.  Check for conventional stores which
1512901Ssaidi@eecs.umich.edu// conflict with locked addresses, and for success/failure of store
1522901Ssaidi@eecs.umich.edu// conditionals.
1532901Ssaidi@eecs.umich.edubool
1542901Ssaidi@eecs.umich.eduPhysicalMemory::checkLockedAddrList(Request *req)
1552901Ssaidi@eecs.umich.edu{
1562539SN/A    Addr paddr = LockedAddr::mask(req->getPaddr());
1572539SN/A    bool isLocked = req->isLocked();
1582539SN/A
1592539SN/A    // Initialize return value.  Non-conditional stores always
1602539SN/A    // succeed.  Assume conditional stores will fail until proven
1612539SN/A    // otherwise.
1622539SN/A    bool success = !isLocked;
1632539SN/A
1642489SN/A    // Iterate over list.  Note that there could be multiple matching
1652901Ssaidi@eecs.umich.edu    // records, as more than one context could have done a load locked
1662901Ssaidi@eecs.umich.edu    // to this location.
1672901Ssaidi@eecs.umich.edu    list<LockedAddr>::iterator i = lockedAddrList.begin();
1682489SN/A
1692489SN/A    while (i != lockedAddrList.end()) {
1702489SN/A
1712630SN/A        if (i->addr == paddr) {
1722384SN/A            // we have a matching address
1732685Ssaidi@eecs.umich.edu
1742685Ssaidi@eecs.umich.edu            if (isLocked && i->matchesContext(req)) {
1752685Ssaidi@eecs.umich.edu                // it's a store conditional, and as far as the memory
1762685Ssaidi@eecs.umich.edu                // system can tell, the requesting context's lock is
1772685Ssaidi@eecs.umich.edu                // still valid.
1782685Ssaidi@eecs.umich.edu                DPRINTF(LLSC, "StCond success: cpu %d thread %d addr %#x\n",
1792685Ssaidi@eecs.umich.edu                        req->getCpuNum(), req->getThreadNum(), paddr);
1802685Ssaidi@eecs.umich.edu                success = true;
1812565SN/A            }
1822685Ssaidi@eecs.umich.edu
1832685Ssaidi@eecs.umich.edu            // Get rid of our record of this lock and advance to next
1842641Sstever@eecs.umich.edu            DPRINTF(LLSC, "Erasing lock record: cpu %d thread %d addr %#x\n",
1852685Ssaidi@eecs.umich.edu                    i->cpuNum, i->threadNum, paddr);
1862685Ssaidi@eecs.umich.edu            i = lockedAddrList.erase(i);
1872685Ssaidi@eecs.umich.edu        }
1882657Ssaidi@eecs.umich.edu        else {
1892685Ssaidi@eecs.umich.edu            // no match: advance to next record
1902685Ssaidi@eecs.umich.edu            ++i;
1912685Ssaidi@eecs.umich.edu        }
1922685Ssaidi@eecs.umich.edu    }
1932685Ssaidi@eecs.umich.edu
1942685Ssaidi@eecs.umich.edu    if (isLocked) {
1952630SN/A        req->setExtraData(success ? 1 : 0);
1962630SN/A    }
1972901Ssaidi@eecs.umich.edu
1982901Ssaidi@eecs.umich.edu    return success;
1992901Ssaidi@eecs.umich.edu}
2002901Ssaidi@eecs.umich.edu
2012901Ssaidi@eecs.umich.eduvoid
2022569SN/APhysicalMemory::doFunctionalAccess(PacketPtr pkt)
2032685Ssaidi@eecs.umich.edu{
2042565SN/A    assert(pkt->getAddr() >= start() &&
2052569SN/A           pkt->getAddr() + pkt->getSize() <= start() + size());
2062657Ssaidi@eecs.umich.edu
2072384SN/A    if (pkt->isRead()) {
208679SN/A        if (pkt->req->isLocked()) {
2092521SN/A            trackLoadLocked(pkt->req);
2102565SN/A        }
2112565SN/A        memcpy(pkt->getPtr<uint8_t>(), pmemAddr + pkt->getAddr() - start(),
2122384SN/A               pkt->getSize());
2132901Ssaidi@eecs.umich.edu#if TRACING_ON
2142901Ssaidi@eecs.umich.edu        switch (pkt->getSize()) {
2152901Ssaidi@eecs.umich.edu          case sizeof(uint64_t):
2162901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2172901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint64_t>());
2182901Ssaidi@eecs.umich.edu            break;
2192901Ssaidi@eecs.umich.edu          case sizeof(uint32_t):
2202901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2212901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint32_t>());
2222901Ssaidi@eecs.umich.edu            break;
2232901Ssaidi@eecs.umich.edu          case sizeof(uint16_t):
2242901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2252901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint16_t>());
2262901Ssaidi@eecs.umich.edu            break;
2272901Ssaidi@eecs.umich.edu          case sizeof(uint8_t):
2282901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x data 0x%x\n",
2292901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint8_t>());
2302901Ssaidi@eecs.umich.edu            break;
2312901Ssaidi@eecs.umich.edu          default:
2322901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read of size %i on address 0x%x\n",
2332901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr());
2342901Ssaidi@eecs.umich.edu        }
2352901Ssaidi@eecs.umich.edu#endif
2362384SN/A    }
2372489SN/A    else if (pkt->isWrite()) {
2382489SN/A        if (writeOK(pkt->req)) {
2392489SN/A                memcpy(pmemAddr + pkt->getAddr() - start(), pkt->getPtr<uint8_t>(),
2402659Ssaidi@eecs.umich.edu                        pkt->getSize());
2412659Ssaidi@eecs.umich.edu#if TRACING_ON
2422659Ssaidi@eecs.umich.edu            switch (pkt->getSize()) {
2432659Ssaidi@eecs.umich.edu              case sizeof(uint64_t):
2442659Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2452659Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint64_t>());
2462659Ssaidi@eecs.umich.edu                break;
2472659Ssaidi@eecs.umich.edu              case sizeof(uint32_t):
2482659Ssaidi@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2492659Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint32_t>());
2502659Ssaidi@eecs.umich.edu                break;
2512657Ssaidi@eecs.umich.edu              case sizeof(uint16_t):
2522489SN/A                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2532641Sstever@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint16_t>());
2542641Sstever@eecs.umich.edu                break;
2552489SN/A              case sizeof(uint8_t):
2562641Sstever@eecs.umich.edu                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x data 0x%x\n",
2572641Sstever@eecs.umich.edu                        pkt->getSize(), pkt->getAddr(),pkt->get<uint8_t>());
2582384SN/A                break;
2592384SN/A              default:
2602384SN/A                DPRINTF(MemoryAccess, "Write of size %i on address 0x%x\n",
2612901Ssaidi@eecs.umich.edu                        pkt->getSize(), pkt->getAddr());
2622901Ssaidi@eecs.umich.edu            }
2632685Ssaidi@eecs.umich.edu#endif
2642384SN/A        }
2652406SN/A    } else if (pkt->isInvalidate()) {
2662406SN/A        //upgrade or invalidate
2672663Sstever@eecs.umich.edu        pkt->flags |= SATISFIED;
2682641Sstever@eecs.umich.edu    } else if (pkt->isReadWrite()) {
2692641Sstever@eecs.umich.edu        IntReg overwrite_val;
2702384SN/A        bool overwrite_mem;
2712566SN/A        uint64_t condition_val64;
2722685Ssaidi@eecs.umich.edu        uint32_t condition_val32;
2732641Sstever@eecs.umich.edu
2742685Ssaidi@eecs.umich.edu        assert(sizeof(IntReg) >= pkt->getSize());
2752641Sstever@eecs.umich.edu
2762565SN/A        overwrite_mem = true;
2772565SN/A        // keep a copy of our possible write value, and copy what is at the
2782566SN/A        // memory address into the packet
2792384SN/A        std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
2802901Ssaidi@eecs.umich.edu        std::memcpy(pkt->getPtr<uint8_t>(), pmemAddr + pkt->getAddr() - start(),
2812384SN/A               pkt->getSize());
2822384SN/A
2832384SN/A        if (pkt->req->isCondSwap()) {
2842384SN/A            if (pkt->getSize() == sizeof(uint64_t)) {
2852685Ssaidi@eecs.umich.edu                condition_val64 = pkt->req->getExtraData();
2862384SN/A                overwrite_mem = !std::memcmp(&condition_val64, pmemAddr +
2872901Ssaidi@eecs.umich.edu                        pkt->getAddr() - start(), sizeof(uint64_t));
2882901Ssaidi@eecs.umich.edu            } else if (pkt->getSize() == sizeof(uint32_t)) {
2892901Ssaidi@eecs.umich.edu                condition_val32 = (uint32_t)pkt->req->getExtraData();
2902902Ssaidi@eecs.umich.edu                overwrite_mem = !std::memcmp(&condition_val32, pmemAddr +
2912901Ssaidi@eecs.umich.edu                        pkt->getAddr() - start(), sizeof(uint32_t));
2922901Ssaidi@eecs.umich.edu            } else
2932901Ssaidi@eecs.umich.edu                panic("Invalid size for conditional read/write\n");
2942901Ssaidi@eecs.umich.edu        }
2952901Ssaidi@eecs.umich.edu
2962901Ssaidi@eecs.umich.edu        if (overwrite_mem)
2972901Ssaidi@eecs.umich.edu            std::memcpy(pmemAddr + pkt->getAddr() - start(),
2982901Ssaidi@eecs.umich.edu               &overwrite_val, pkt->getSize());
2992901Ssaidi@eecs.umich.edu
3002901Ssaidi@eecs.umich.edu#if TRACING_ON
3012901Ssaidi@eecs.umich.edu        switch (pkt->getSize()) {
3022901Ssaidi@eecs.umich.edu          case sizeof(uint64_t):
3032901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read/Write of size %i on address 0x%x old data 0x%x\n",
3042901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint64_t>());
3052902Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "New Data 0x%x %s conditional (0x%x) and %s \n",
3062902Ssaidi@eecs.umich.edu                    overwrite_mem, pkt->req->isCondSwap() ? "was" : "wasn't",
3072901Ssaidi@eecs.umich.edu                    condition_val64, overwrite_mem ? "happened" : "didn't happen");
3082901Ssaidi@eecs.umich.edu            break;
3092901Ssaidi@eecs.umich.edu          case sizeof(uint32_t):
3102384SN/A            DPRINTF(MemoryAccess, "Read/Write of size %i on address 0x%x old data 0x%x\n",
3112901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint32_t>());
3122901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "New Data 0x%x %s conditional (0x%x) and %s \n",
3132902Ssaidi@eecs.umich.edu                    overwrite_mem, pkt->req->isCondSwap() ? "was" : "wasn't",
3142901Ssaidi@eecs.umich.edu                    condition_val32, overwrite_mem ? "happened" : "didn't happen");
3152901Ssaidi@eecs.umich.edu            break;
3162901Ssaidi@eecs.umich.edu          case sizeof(uint16_t):
3172901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read/Write of size %i on address 0x%x old data 0x%x\n",
3182901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint16_t>());
3192901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "New Data 0x%x wasn't conditional and happned\n",
3202566SN/A                    overwrite_mem);
3212901Ssaidi@eecs.umich.edu            break;
3222901Ssaidi@eecs.umich.edu          case sizeof(uint8_t):
3232901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "Read/Write of size %i on address 0x%x old data 0x%x\n",
3242901Ssaidi@eecs.umich.edu                    pkt->getSize(), pkt->getAddr(),pkt->get<uint8_t>());
3252901Ssaidi@eecs.umich.edu            DPRINTF(MemoryAccess, "New Data 0x%x wasn't conditional and happned\n",
3262384SN/A                    overwrite_mem);
3272384SN/A            break;
3282384SN/A          default:
329545SN/A            DPRINTF(MemoryAccess, "Read/Write of size %i on address 0x%x\n",
330545SN/A                    pkt->getSize(), pkt->getAddr());
331545SN/A        }
3322489SN/A#endif
3332489SN/A    } else {
334545SN/A        panic("unimplemented");
335545SN/A    }
336679SN/A
337    pkt->result = Packet::Success;
338}
339
340Port *
341PhysicalMemory::getPort(const std::string &if_name, int idx)
342{
343    // Accept request for "functional" port for backwards compatibility
344    // with places where this function is called from C++.  I'd prefer
345    // to move all these into Python someday.
346    if (if_name == "functional") {
347        return new MemoryPort(csprintf("%s-functional", name()), this);
348    }
349
350    if (if_name != "port") {
351        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
352    }
353
354    if (idx >= ports.size()) {
355        ports.resize(idx+1);
356    }
357
358    if (ports[idx] != NULL) {
359        panic("PhysicalMemory::getPort: port %d already assigned", idx);
360    }
361
362    MemoryPort *port =
363        new MemoryPort(csprintf("%s-port%d", name(), idx), this);
364
365    ports[idx] = port;
366    return port;
367}
368
369
370void
371PhysicalMemory::recvStatusChange(Port::Status status)
372{
373}
374
375PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
376                                       PhysicalMemory *_memory)
377    : SimpleTimingPort(_name), memory(_memory)
378{ }
379
380void
381PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
382{
383    memory->recvStatusChange(status);
384}
385
386void
387PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
388                                                   bool &snoop)
389{
390    memory->getAddressRanges(resp, snoop);
391}
392
393void
394PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
395{
396    snoop = false;
397    resp.clear();
398    resp.push_back(RangeSize(start(), params()->addrRange.size()));
399}
400
401int
402PhysicalMemory::MemoryPort::deviceBlockSize()
403{
404    return memory->deviceBlockSize();
405}
406
407Tick
408PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
409{
410    memory->doFunctionalAccess(pkt);
411    return memory->calculateLatency(pkt);
412}
413
414void
415PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
416{
417    checkFunctional(pkt);
418
419    // Default implementation of SimpleTimingPort::recvFunctional()
420    // calls recvAtomic() and throws away the latency; we can save a
421    // little here by just not calculating the latency.
422    memory->doFunctionalAccess(pkt);
423}
424
425unsigned int
426PhysicalMemory::drain(Event *de)
427{
428    int count = 0;
429    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
430        count += (*pi)->drain(de);
431    }
432
433    if (count)
434        changeState(Draining);
435    else
436        changeState(Drained);
437    return count;
438}
439
440void
441PhysicalMemory::serialize(ostream &os)
442{
443    gzFile compressedMem;
444    string filename = name() + ".physmem";
445
446    SERIALIZE_SCALAR(filename);
447
448    // write memory file
449    string thefile = Checkpoint::dir() + "/" + filename.c_str();
450    int fd = creat(thefile.c_str(), 0664);
451    if (fd < 0) {
452        perror("creat");
453        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
454    }
455
456    compressedMem = gzdopen(fd, "wb");
457    if (compressedMem == NULL)
458        fatal("Insufficient memory to allocate compression state for %s\n",
459                filename);
460
461    if (gzwrite(compressedMem, pmemAddr, params()->addrRange.size()) != params()->addrRange.size()) {
462        fatal("Write failed on physical memory checkpoint file '%s'\n",
463              filename);
464    }
465
466    if (gzclose(compressedMem))
467        fatal("Close failed on physical memory checkpoint file '%s'\n",
468              filename);
469}
470
471void
472PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
473{
474    gzFile compressedMem;
475    long *tempPage;
476    long *pmem_current;
477    uint64_t curSize;
478    uint32_t bytesRead;
479    const int chunkSize = 16384;
480
481
482    string filename;
483
484    UNSERIALIZE_SCALAR(filename);
485
486    filename = cp->cptDir + "/" + filename;
487
488    // mmap memoryfile
489    int fd = open(filename.c_str(), O_RDONLY);
490    if (fd < 0) {
491        perror("open");
492        fatal("Can't open physical memory checkpoint file '%s'", filename);
493    }
494
495    compressedMem = gzdopen(fd, "rb");
496    if (compressedMem == NULL)
497        fatal("Insufficient memory to allocate compression state for %s\n",
498                filename);
499
500    // unmap file that was mmaped in the constructor
501    // This is done here to make sure that gzip and open don't muck with our
502    // nice large space of memory before we reallocate it
503    munmap((char*)pmemAddr, params()->addrRange.size());
504
505    pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
506                                MAP_ANON | MAP_PRIVATE, -1, 0);
507
508    if (pmemAddr == (void *)MAP_FAILED) {
509        perror("mmap");
510        fatal("Could not mmap physical memory!\n");
511    }
512
513    curSize = 0;
514    tempPage = (long*)malloc(chunkSize);
515    if (tempPage == NULL)
516        fatal("Unable to malloc memory to read file %s\n", filename);
517
518    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
519    while (curSize < params()->addrRange.size()) {
520        bytesRead = gzread(compressedMem, tempPage, chunkSize);
521        if (bytesRead != chunkSize && bytesRead != params()->addrRange.size() - curSize)
522            fatal("Read failed on physical memory checkpoint file '%s'"
523                  " got %d bytes, expected %d or %d bytes\n",
524                  filename, bytesRead, chunkSize, params()->addrRange.size()-curSize);
525
526        assert(bytesRead % sizeof(long) == 0);
527
528        for (int x = 0; x < bytesRead/sizeof(long); x++)
529        {
530             if (*(tempPage+x) != 0) {
531                 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
532                 *pmem_current = *(tempPage+x);
533             }
534        }
535        curSize += bytesRead;
536    }
537
538    free(tempPage);
539
540    if (gzclose(compressedMem))
541        fatal("Close failed on physical memory checkpoint file '%s'\n",
542              filename);
543
544}
545
546
547BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
548
549    Param<string> file;
550    Param<Range<Addr> > range;
551    Param<Tick> latency;
552    Param<bool> zero;
553
554END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
555
556BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
557
558    INIT_PARAM_DFLT(file, "memory mapped file", ""),
559    INIT_PARAM(range, "Device Address Range"),
560    INIT_PARAM(latency, "Memory access latency"),
561    INIT_PARAM(zero, "Zero initialize memory")
562
563END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
564
565CREATE_SIM_OBJECT(PhysicalMemory)
566{
567    PhysicalMemory::Params *p = new PhysicalMemory::Params;
568    p->name = getInstanceName();
569    p->addrRange = range;
570    p->latency = latency;
571    p->zero = zero;
572    return new PhysicalMemory(p);
573}
574
575REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
576