physical.cc revision 6658
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 426329Sgblack@eecs.umich.edu#include "arch/registers.hh" 432391SN/A#include "base/misc.hh" 445399Ssaidi@eecs.umich.edu#include "base/random.hh" 456216Snate@binkert.org#include "base/types.hh" 462391SN/A#include "config/full_system.hh" 476658Snate@binkert.org#include "config/the_isa.hh" 483879Ssaidi@eecs.umich.edu#include "mem/packet_access.hh" 492394SN/A#include "mem/physical.hh" 502415SN/A#include "sim/eventq.hh" 512394SN/A 522391SN/Ausing namespace std; 532423SN/Ausing namespace TheISA; 542391SN/A 554762Snate@binkert.orgPhysicalMemory::PhysicalMemory(const Params *p) 565477Snate@binkert.org : MemObject(p), pmemAddr(NULL), pagePtr(0), 575477Snate@binkert.org lat(p->latency), lat_var(p->latency_var), 585477Snate@binkert.org cachedSize(params()->range.size()), cachedStart(params()->range.start) 592391SN/A{ 604762Snate@binkert.org if (params()->range.size() % TheISA::PageBytes != 0) 612391SN/A panic("Memory Size not divisible by page size\n"); 622391SN/A 635477Snate@binkert.org if (params()->null) 645477Snate@binkert.org return; 655477Snate@binkert.org 662391SN/A int map_flags = MAP_ANON | MAP_PRIVATE; 674918Snate@binkert.org pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 684918Snate@binkert.org PROT_READ | PROT_WRITE, map_flags, -1, 0); 692391SN/A 703012Ssaidi@eecs.umich.edu if (pmemAddr == (void *)MAP_FAILED) { 712391SN/A perror("mmap"); 722391SN/A fatal("Could not mmap!\n"); 732391SN/A } 742391SN/A 753751Sgblack@eecs.umich.edu //If requested, initialize all the memory to 0 764762Snate@binkert.org if (p->zero) 774762Snate@binkert.org memset(pmemAddr, 0, p->range.size()); 782391SN/A} 792391SN/A 802541SN/Avoid 812541SN/APhysicalMemory::init() 822541SN/A{ 834470Sstever@eecs.umich.edu if (ports.size() == 0) { 844470Sstever@eecs.umich.edu fatal("PhysicalMemory object %s is unconnected!", name()); 854470Sstever@eecs.umich.edu } 864470Sstever@eecs.umich.edu 874467Sstever@eecs.umich.edu for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 884467Sstever@eecs.umich.edu if (*pi) 894467Sstever@eecs.umich.edu (*pi)->sendStatusChange(Port::RangeChange); 904467Sstever@eecs.umich.edu } 912541SN/A} 922541SN/A 932391SN/APhysicalMemory::~PhysicalMemory() 942391SN/A{ 953012Ssaidi@eecs.umich.edu if (pmemAddr) 964762Snate@binkert.org munmap((char*)pmemAddr, params()->range.size()); 972416SN/A //Remove memPorts? 982391SN/A} 992391SN/A 1002391SN/AAddr 1012391SN/APhysicalMemory::new_page() 1022391SN/A{ 1033012Ssaidi@eecs.umich.edu Addr return_addr = pagePtr << LogVMPageSize; 1044040Ssaidi@eecs.umich.edu return_addr += start(); 1052391SN/A 1063012Ssaidi@eecs.umich.edu ++pagePtr; 1072391SN/A return return_addr; 1082391SN/A} 1092391SN/A 1106227Snate@binkert.orgunsigned 1116227Snate@binkert.orgPhysicalMemory::deviceBlockSize() const 1122408SN/A{ 1132409SN/A //Can accept anysize request 1142409SN/A return 0; 1152408SN/A} 1162408SN/A 1173012Ssaidi@eecs.umich.eduTick 1183349Sbinkertn@umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt) 1193012Ssaidi@eecs.umich.edu{ 1205399Ssaidi@eecs.umich.edu Tick latency = lat; 1215399Ssaidi@eecs.umich.edu if (lat_var != 0) 1225399Ssaidi@eecs.umich.edu latency += random_mt.random<Tick>(0, lat_var); 1235399Ssaidi@eecs.umich.edu return latency; 1243012Ssaidi@eecs.umich.edu} 1252413SN/A 1263170Sstever@eecs.umich.edu 1273170Sstever@eecs.umich.edu 1283170Sstever@eecs.umich.edu// Add load-locked to tracking list. Should only be called if the 1296076Sgblack@eecs.umich.edu// operation is a load and the LLSC flag is set. 1303170Sstever@eecs.umich.eduvoid 1314626Sstever@eecs.umich.eduPhysicalMemory::trackLoadLocked(PacketPtr pkt) 1323170Sstever@eecs.umich.edu{ 1334626Sstever@eecs.umich.edu Request *req = pkt->req; 1343170Sstever@eecs.umich.edu Addr paddr = LockedAddr::mask(req->getPaddr()); 1353170Sstever@eecs.umich.edu 1363170Sstever@eecs.umich.edu // first we check if we already have a locked addr for this 1373170Sstever@eecs.umich.edu // xc. Since each xc only gets one, we just update the 1383170Sstever@eecs.umich.edu // existing record with the new address. 1393170Sstever@eecs.umich.edu list<LockedAddr>::iterator i; 1403170Sstever@eecs.umich.edu 1413170Sstever@eecs.umich.edu for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) { 1423170Sstever@eecs.umich.edu if (i->matchesContext(req)) { 1435714Shsul@eecs.umich.edu DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n", 1445714Shsul@eecs.umich.edu req->contextId(), paddr); 1453170Sstever@eecs.umich.edu i->addr = paddr; 1463170Sstever@eecs.umich.edu return; 1473170Sstever@eecs.umich.edu } 1483170Sstever@eecs.umich.edu } 1493170Sstever@eecs.umich.edu 1503170Sstever@eecs.umich.edu // no record for this xc: need to allocate a new one 1515714Shsul@eecs.umich.edu DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n", 1525714Shsul@eecs.umich.edu req->contextId(), paddr); 1533170Sstever@eecs.umich.edu lockedAddrList.push_front(LockedAddr(req)); 1543170Sstever@eecs.umich.edu} 1553170Sstever@eecs.umich.edu 1563170Sstever@eecs.umich.edu 1573170Sstever@eecs.umich.edu// Called on *writes* only... both regular stores and 1583170Sstever@eecs.umich.edu// store-conditional operations. Check for conventional stores which 1593170Sstever@eecs.umich.edu// conflict with locked addresses, and for success/failure of store 1603170Sstever@eecs.umich.edu// conditionals. 1613170Sstever@eecs.umich.edubool 1624626Sstever@eecs.umich.eduPhysicalMemory::checkLockedAddrList(PacketPtr pkt) 1633170Sstever@eecs.umich.edu{ 1644626Sstever@eecs.umich.edu Request *req = pkt->req; 1653170Sstever@eecs.umich.edu Addr paddr = LockedAddr::mask(req->getPaddr()); 1666102Sgblack@eecs.umich.edu bool isLLSC = pkt->isLLSC(); 1673170Sstever@eecs.umich.edu 1683170Sstever@eecs.umich.edu // Initialize return value. Non-conditional stores always 1693170Sstever@eecs.umich.edu // succeed. Assume conditional stores will fail until proven 1703170Sstever@eecs.umich.edu // otherwise. 1716102Sgblack@eecs.umich.edu bool success = !isLLSC; 1723170Sstever@eecs.umich.edu 1733170Sstever@eecs.umich.edu // Iterate over list. Note that there could be multiple matching 1743170Sstever@eecs.umich.edu // records, as more than one context could have done a load locked 1753170Sstever@eecs.umich.edu // to this location. 1763170Sstever@eecs.umich.edu list<LockedAddr>::iterator i = lockedAddrList.begin(); 1773170Sstever@eecs.umich.edu 1783170Sstever@eecs.umich.edu while (i != lockedAddrList.end()) { 1793170Sstever@eecs.umich.edu 1803170Sstever@eecs.umich.edu if (i->addr == paddr) { 1813170Sstever@eecs.umich.edu // we have a matching address 1823170Sstever@eecs.umich.edu 1836102Sgblack@eecs.umich.edu if (isLLSC && i->matchesContext(req)) { 1843170Sstever@eecs.umich.edu // it's a store conditional, and as far as the memory 1853170Sstever@eecs.umich.edu // system can tell, the requesting context's lock is 1863170Sstever@eecs.umich.edu // still valid. 1875714Shsul@eecs.umich.edu DPRINTF(LLSC, "StCond success: context %d addr %#x\n", 1885714Shsul@eecs.umich.edu req->contextId(), paddr); 1893170Sstever@eecs.umich.edu success = true; 1903170Sstever@eecs.umich.edu } 1913170Sstever@eecs.umich.edu 1923170Sstever@eecs.umich.edu // Get rid of our record of this lock and advance to next 1935714Shsul@eecs.umich.edu DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n", 1945714Shsul@eecs.umich.edu i->contextId, paddr); 1953170Sstever@eecs.umich.edu i = lockedAddrList.erase(i); 1963170Sstever@eecs.umich.edu } 1973170Sstever@eecs.umich.edu else { 1983170Sstever@eecs.umich.edu // no match: advance to next record 1993170Sstever@eecs.umich.edu ++i; 2003170Sstever@eecs.umich.edu } 2013170Sstever@eecs.umich.edu } 2023170Sstever@eecs.umich.edu 2036102Sgblack@eecs.umich.edu if (isLLSC) { 2044040Ssaidi@eecs.umich.edu req->setExtraData(success ? 1 : 0); 2053170Sstever@eecs.umich.edu } 2063170Sstever@eecs.umich.edu 2073170Sstever@eecs.umich.edu return success; 2083170Sstever@eecs.umich.edu} 2093170Sstever@eecs.umich.edu 2104626Sstever@eecs.umich.edu 2114626Sstever@eecs.umich.edu#if TRACING_ON 2124626Sstever@eecs.umich.edu 2134626Sstever@eecs.umich.edu#define CASE(A, T) \ 2144626Sstever@eecs.umich.edu case sizeof(T): \ 2156429Ssteve.reinhardt@amd.com DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \ 2166429Ssteve.reinhardt@amd.com A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \ 2174626Sstever@eecs.umich.edu break 2184626Sstever@eecs.umich.edu 2194626Sstever@eecs.umich.edu 2204626Sstever@eecs.umich.edu#define TRACE_PACKET(A) \ 2214626Sstever@eecs.umich.edu do { \ 2224626Sstever@eecs.umich.edu switch (pkt->getSize()) { \ 2234626Sstever@eecs.umich.edu CASE(A, uint64_t); \ 2244626Sstever@eecs.umich.edu CASE(A, uint32_t); \ 2254626Sstever@eecs.umich.edu CASE(A, uint16_t); \ 2264626Sstever@eecs.umich.edu CASE(A, uint8_t); \ 2274626Sstever@eecs.umich.edu default: \ 2286429Ssteve.reinhardt@amd.com DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \ 2296429Ssteve.reinhardt@amd.com A, pkt->getSize(), pkt->getAddr()); \ 2304626Sstever@eecs.umich.edu } \ 2314626Sstever@eecs.umich.edu } while (0) 2324626Sstever@eecs.umich.edu 2334626Sstever@eecs.umich.edu#else 2344626Sstever@eecs.umich.edu 2354626Sstever@eecs.umich.edu#define TRACE_PACKET(A) 2364626Sstever@eecs.umich.edu 2374626Sstever@eecs.umich.edu#endif 2384626Sstever@eecs.umich.edu 2394626Sstever@eecs.umich.eduTick 2404626Sstever@eecs.umich.eduPhysicalMemory::doAtomicAccess(PacketPtr pkt) 2412413SN/A{ 2424040Ssaidi@eecs.umich.edu assert(pkt->getAddr() >= start() && 2434040Ssaidi@eecs.umich.edu pkt->getAddr() + pkt->getSize() <= start() + size()); 2442414SN/A 2454626Sstever@eecs.umich.edu if (pkt->memInhibitAsserted()) { 2464626Sstever@eecs.umich.edu DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n", 2474626Sstever@eecs.umich.edu pkt->getAddr()); 2484626Sstever@eecs.umich.edu return 0; 2493175Srdreslin@umich.edu } 2504626Sstever@eecs.umich.edu 2514626Sstever@eecs.umich.edu uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 2524626Sstever@eecs.umich.edu 2534626Sstever@eecs.umich.edu if (pkt->cmd == MemCmd::SwapReq) { 2544040Ssaidi@eecs.umich.edu IntReg overwrite_val; 2554040Ssaidi@eecs.umich.edu bool overwrite_mem; 2564040Ssaidi@eecs.umich.edu uint64_t condition_val64; 2574040Ssaidi@eecs.umich.edu uint32_t condition_val32; 2584040Ssaidi@eecs.umich.edu 2595477Snate@binkert.org if (!pmemAddr) 2605477Snate@binkert.org panic("Swap only works if there is real memory (i.e. null=False)"); 2614040Ssaidi@eecs.umich.edu assert(sizeof(IntReg) >= pkt->getSize()); 2624040Ssaidi@eecs.umich.edu 2634040Ssaidi@eecs.umich.edu overwrite_mem = true; 2644040Ssaidi@eecs.umich.edu // keep a copy of our possible write value, and copy what is at the 2654040Ssaidi@eecs.umich.edu // memory address into the packet 2664052Ssaidi@eecs.umich.edu std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize()); 2674626Sstever@eecs.umich.edu std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 2684040Ssaidi@eecs.umich.edu 2694040Ssaidi@eecs.umich.edu if (pkt->req->isCondSwap()) { 2704040Ssaidi@eecs.umich.edu if (pkt->getSize() == sizeof(uint64_t)) { 2714052Ssaidi@eecs.umich.edu condition_val64 = pkt->req->getExtraData(); 2724626Sstever@eecs.umich.edu overwrite_mem = !std::memcmp(&condition_val64, hostAddr, 2734626Sstever@eecs.umich.edu sizeof(uint64_t)); 2744040Ssaidi@eecs.umich.edu } else if (pkt->getSize() == sizeof(uint32_t)) { 2754052Ssaidi@eecs.umich.edu condition_val32 = (uint32_t)pkt->req->getExtraData(); 2764626Sstever@eecs.umich.edu overwrite_mem = !std::memcmp(&condition_val32, hostAddr, 2774626Sstever@eecs.umich.edu sizeof(uint32_t)); 2784040Ssaidi@eecs.umich.edu } else 2794040Ssaidi@eecs.umich.edu panic("Invalid size for conditional read/write\n"); 2804040Ssaidi@eecs.umich.edu } 2814040Ssaidi@eecs.umich.edu 2824040Ssaidi@eecs.umich.edu if (overwrite_mem) 2834626Sstever@eecs.umich.edu std::memcpy(hostAddr, &overwrite_val, pkt->getSize()); 2844040Ssaidi@eecs.umich.edu 2856429Ssteve.reinhardt@amd.com assert(!pkt->req->isInstFetch()); 2864626Sstever@eecs.umich.edu TRACE_PACKET("Read/Write"); 2874626Sstever@eecs.umich.edu } else if (pkt->isRead()) { 2884626Sstever@eecs.umich.edu assert(!pkt->isWrite()); 2896102Sgblack@eecs.umich.edu if (pkt->isLLSC()) { 2904626Sstever@eecs.umich.edu trackLoadLocked(pkt); 2914040Ssaidi@eecs.umich.edu } 2925477Snate@binkert.org if (pmemAddr) 2935477Snate@binkert.org memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 2946429Ssteve.reinhardt@amd.com TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read"); 2954626Sstever@eecs.umich.edu } else if (pkt->isWrite()) { 2964626Sstever@eecs.umich.edu if (writeOK(pkt)) { 2975477Snate@binkert.org if (pmemAddr) 2985477Snate@binkert.org memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 2996429Ssteve.reinhardt@amd.com assert(!pkt->req->isInstFetch()); 3004626Sstever@eecs.umich.edu TRACE_PACKET("Write"); 3014626Sstever@eecs.umich.edu } 3024626Sstever@eecs.umich.edu } else if (pkt->isInvalidate()) { 3034626Sstever@eecs.umich.edu //upgrade or invalidate 3044626Sstever@eecs.umich.edu if (pkt->needsResponse()) { 3054626Sstever@eecs.umich.edu pkt->makeAtomicResponse(); 3064626Sstever@eecs.umich.edu } 3074040Ssaidi@eecs.umich.edu } else { 3082413SN/A panic("unimplemented"); 3092413SN/A } 3102420SN/A 3114626Sstever@eecs.umich.edu if (pkt->needsResponse()) { 3124626Sstever@eecs.umich.edu pkt->makeAtomicResponse(); 3134626Sstever@eecs.umich.edu } 3144626Sstever@eecs.umich.edu return calculateLatency(pkt); 3152413SN/A} 3162413SN/A 3174626Sstever@eecs.umich.edu 3184626Sstever@eecs.umich.eduvoid 3194626Sstever@eecs.umich.eduPhysicalMemory::doFunctionalAccess(PacketPtr pkt) 3204626Sstever@eecs.umich.edu{ 3214626Sstever@eecs.umich.edu assert(pkt->getAddr() >= start() && 3224626Sstever@eecs.umich.edu pkt->getAddr() + pkt->getSize() <= start() + size()); 3234626Sstever@eecs.umich.edu 3245222Sksewell@umich.edu 3254626Sstever@eecs.umich.edu uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 3264626Sstever@eecs.umich.edu 3275314Sstever@gmail.com if (pkt->isRead()) { 3285477Snate@binkert.org if (pmemAddr) 3295477Snate@binkert.org memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 3304626Sstever@eecs.umich.edu TRACE_PACKET("Read"); 3315314Sstever@gmail.com pkt->makeAtomicResponse(); 3325314Sstever@gmail.com } else if (pkt->isWrite()) { 3335477Snate@binkert.org if (pmemAddr) 3345477Snate@binkert.org memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 3354626Sstever@eecs.umich.edu TRACE_PACKET("Write"); 3365314Sstever@gmail.com pkt->makeAtomicResponse(); 3375314Sstever@gmail.com } else if (pkt->isPrint()) { 3385315Sstever@gmail.com Packet::PrintReqState *prs = 3395315Sstever@gmail.com dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 3405315Sstever@gmail.com // Need to call printLabels() explicitly since we're not going 3415315Sstever@gmail.com // through printObj(). 3425314Sstever@gmail.com prs->printLabels(); 3435315Sstever@gmail.com // Right now we just print the single byte at the specified address. 3445314Sstever@gmail.com ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 3454626Sstever@eecs.umich.edu } else { 3464626Sstever@eecs.umich.edu panic("PhysicalMemory: unimplemented functional command %s", 3474626Sstever@eecs.umich.edu pkt->cmdString()); 3484626Sstever@eecs.umich.edu } 3494490Sstever@eecs.umich.edu} 3504490Sstever@eecs.umich.edu 3514626Sstever@eecs.umich.edu 3522413SN/APort * 3532738Sstever@eecs.umich.eduPhysicalMemory::getPort(const std::string &if_name, int idx) 3542413SN/A{ 3554468Sstever@eecs.umich.edu // Accept request for "functional" port for backwards compatibility 3564468Sstever@eecs.umich.edu // with places where this function is called from C++. I'd prefer 3574468Sstever@eecs.umich.edu // to move all these into Python someday. 3584468Sstever@eecs.umich.edu if (if_name == "functional") { 3594468Sstever@eecs.umich.edu return new MemoryPort(csprintf("%s-functional", name()), this); 3604468Sstever@eecs.umich.edu } 3614468Sstever@eecs.umich.edu 3624467Sstever@eecs.umich.edu if (if_name != "port") { 3632462SN/A panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 3642462SN/A } 3654467Sstever@eecs.umich.edu 3666227Snate@binkert.org if (idx >= (int)ports.size()) { 3676227Snate@binkert.org ports.resize(idx + 1); 3684467Sstever@eecs.umich.edu } 3694467Sstever@eecs.umich.edu 3704467Sstever@eecs.umich.edu if (ports[idx] != NULL) { 3714467Sstever@eecs.umich.edu panic("PhysicalMemory::getPort: port %d already assigned", idx); 3724467Sstever@eecs.umich.edu } 3734467Sstever@eecs.umich.edu 3744467Sstever@eecs.umich.edu MemoryPort *port = 3754467Sstever@eecs.umich.edu new MemoryPort(csprintf("%s-port%d", name(), idx), this); 3764467Sstever@eecs.umich.edu 3774467Sstever@eecs.umich.edu ports[idx] = port; 3784467Sstever@eecs.umich.edu return port; 3792413SN/A} 3802413SN/A 3814467Sstever@eecs.umich.edu 3822413SN/Avoid 3832413SN/APhysicalMemory::recvStatusChange(Port::Status status) 3842413SN/A{ 3852413SN/A} 3862413SN/A 3872640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 3882640Sstever@eecs.umich.edu PhysicalMemory *_memory) 3895606Snate@binkert.org : SimpleTimingPort(_name, _memory), memory(_memory) 3902413SN/A{ } 3912413SN/A 3922413SN/Avoid 3932413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 3942413SN/A{ 3952413SN/A memory->recvStatusChange(status); 3962413SN/A} 3972413SN/A 3982413SN/Avoid 3992522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 4004475Sstever@eecs.umich.edu bool &snoop) 4012413SN/A{ 4022522SN/A memory->getAddressRanges(resp, snoop); 4032497SN/A} 4042497SN/A 4052497SN/Avoid 4064475Sstever@eecs.umich.eduPhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 4072497SN/A{ 4084475Sstever@eecs.umich.edu snoop = false; 4092522SN/A resp.clear(); 4104762Snate@binkert.org resp.push_back(RangeSize(start(), params()->range.size())); 4112413SN/A} 4122413SN/A 4136227Snate@binkert.orgunsigned 4146227Snate@binkert.orgPhysicalMemory::MemoryPort::deviceBlockSize() const 4152415SN/A{ 4162415SN/A return memory->deviceBlockSize(); 4172415SN/A} 4182413SN/A 4192413SN/ATick 4203349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 4212413SN/A{ 4224626Sstever@eecs.umich.edu return memory->doAtomicAccess(pkt); 4232413SN/A} 4242413SN/A 4252413SN/Avoid 4263349Sbinkertn@umich.eduPhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 4272413SN/A{ 4285314Sstever@gmail.com pkt->pushLabel(memory->name()); 4295314Sstever@gmail.com 4304929Sstever@gmail.com if (!checkFunctional(pkt)) { 4314929Sstever@gmail.com // Default implementation of SimpleTimingPort::recvFunctional() 4324929Sstever@gmail.com // calls recvAtomic() and throws away the latency; we can save a 4334929Sstever@gmail.com // little here by just not calculating the latency. 4344929Sstever@gmail.com memory->doFunctionalAccess(pkt); 4354929Sstever@gmail.com } 4365314Sstever@gmail.com 4375314Sstever@gmail.com pkt->popLabel(); 4382413SN/A} 4392413SN/A 4402914Ssaidi@eecs.umich.eduunsigned int 4412914Ssaidi@eecs.umich.eduPhysicalMemory::drain(Event *de) 4422914Ssaidi@eecs.umich.edu{ 4434467Sstever@eecs.umich.edu int count = 0; 4444467Sstever@eecs.umich.edu for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 4454467Sstever@eecs.umich.edu count += (*pi)->drain(de); 4464467Sstever@eecs.umich.edu } 4474467Sstever@eecs.umich.edu 4482914Ssaidi@eecs.umich.edu if (count) 4492914Ssaidi@eecs.umich.edu changeState(Draining); 4502914Ssaidi@eecs.umich.edu else 4512914Ssaidi@eecs.umich.edu changeState(Drained); 4522914Ssaidi@eecs.umich.edu return count; 4532914Ssaidi@eecs.umich.edu} 4542413SN/A 4552391SN/Avoid 4562391SN/APhysicalMemory::serialize(ostream &os) 4572391SN/A{ 4585477Snate@binkert.org if (!pmemAddr) 4595477Snate@binkert.org return; 4605477Snate@binkert.org 4612391SN/A gzFile compressedMem; 4622391SN/A string filename = name() + ".physmem"; 4632391SN/A 4642391SN/A SERIALIZE_SCALAR(filename); 4652391SN/A 4662391SN/A // write memory file 4672391SN/A string thefile = Checkpoint::dir() + "/" + filename.c_str(); 4682391SN/A int fd = creat(thefile.c_str(), 0664); 4692391SN/A if (fd < 0) { 4702391SN/A perror("creat"); 4712391SN/A fatal("Can't open physical memory checkpoint file '%s'\n", filename); 4722391SN/A } 4732391SN/A 4742391SN/A compressedMem = gzdopen(fd, "wb"); 4752391SN/A if (compressedMem == NULL) 4762391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 4772391SN/A filename); 4782391SN/A 4794762Snate@binkert.org if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 4806227Snate@binkert.org (int)params()->range.size()) { 4812391SN/A fatal("Write failed on physical memory checkpoint file '%s'\n", 4822391SN/A filename); 4832391SN/A } 4842391SN/A 4852391SN/A if (gzclose(compressedMem)) 4862391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 4872391SN/A filename); 4882391SN/A} 4892391SN/A 4902391SN/Avoid 4912391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 4922391SN/A{ 4935477Snate@binkert.org if (!pmemAddr) 4945477Snate@binkert.org return; 4955477Snate@binkert.org 4962391SN/A gzFile compressedMem; 4972391SN/A long *tempPage; 4982391SN/A long *pmem_current; 4992391SN/A uint64_t curSize; 5002391SN/A uint32_t bytesRead; 5016227Snate@binkert.org const uint32_t chunkSize = 16384; 5022391SN/A 5032391SN/A string filename; 5042391SN/A 5052391SN/A UNSERIALIZE_SCALAR(filename); 5062391SN/A 5072391SN/A filename = cp->cptDir + "/" + filename; 5082391SN/A 5092391SN/A // mmap memoryfile 5102391SN/A int fd = open(filename.c_str(), O_RDONLY); 5112391SN/A if (fd < 0) { 5122391SN/A perror("open"); 5132391SN/A fatal("Can't open physical memory checkpoint file '%s'", filename); 5142391SN/A } 5152391SN/A 5162391SN/A compressedMem = gzdopen(fd, "rb"); 5172391SN/A if (compressedMem == NULL) 5182391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 5192391SN/A filename); 5202391SN/A 5213012Ssaidi@eecs.umich.edu // unmap file that was mmaped in the constructor 5223012Ssaidi@eecs.umich.edu // This is done here to make sure that gzip and open don't muck with our 5233012Ssaidi@eecs.umich.edu // nice large space of memory before we reallocate it 5244762Snate@binkert.org munmap((char*)pmemAddr, params()->range.size()); 5252391SN/A 5264762Snate@binkert.org pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 5274762Snate@binkert.org PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 5282391SN/A 5293012Ssaidi@eecs.umich.edu if (pmemAddr == (void *)MAP_FAILED) { 5302391SN/A perror("mmap"); 5312391SN/A fatal("Could not mmap physical memory!\n"); 5322391SN/A } 5332391SN/A 5342391SN/A curSize = 0; 5352391SN/A tempPage = (long*)malloc(chunkSize); 5362391SN/A if (tempPage == NULL) 5372391SN/A fatal("Unable to malloc memory to read file %s\n", filename); 5382391SN/A 5392391SN/A /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 5404762Snate@binkert.org while (curSize < params()->range.size()) { 5412391SN/A bytesRead = gzread(compressedMem, tempPage, chunkSize); 5424762Snate@binkert.org if (bytesRead != chunkSize && 5434762Snate@binkert.org bytesRead != params()->range.size() - curSize) 5442391SN/A fatal("Read failed on physical memory checkpoint file '%s'" 5452391SN/A " got %d bytes, expected %d or %d bytes\n", 5464762Snate@binkert.org filename, bytesRead, chunkSize, 5474762Snate@binkert.org params()->range.size() - curSize); 5482391SN/A 5492391SN/A assert(bytesRead % sizeof(long) == 0); 5502391SN/A 5516227Snate@binkert.org for (uint32_t x = 0; x < bytesRead / sizeof(long); x++) 5522391SN/A { 5532391SN/A if (*(tempPage+x) != 0) { 5543012Ssaidi@eecs.umich.edu pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 5552391SN/A *pmem_current = *(tempPage+x); 5562391SN/A } 5572391SN/A } 5582391SN/A curSize += bytesRead; 5592391SN/A } 5602391SN/A 5612391SN/A free(tempPage); 5622391SN/A 5632391SN/A if (gzclose(compressedMem)) 5642391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 5652391SN/A filename); 5662391SN/A 5672391SN/A} 5682391SN/A 5694762Snate@binkert.orgPhysicalMemory * 5704762Snate@binkert.orgPhysicalMemoryParams::create() 5712391SN/A{ 5724762Snate@binkert.org return new PhysicalMemory(this); 5732391SN/A} 574