physical.cc revision 2639
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. 272391SN/A */ 282391SN/A 292391SN/A#include <sys/types.h> 302391SN/A#include <sys/mman.h> 312391SN/A#include <errno.h> 322391SN/A#include <fcntl.h> 332391SN/A#include <unistd.h> 342391SN/A#include <zlib.h> 352391SN/A 362391SN/A#include <iostream> 372391SN/A#include <string> 382391SN/A 392391SN/A 402391SN/A#include "base/misc.hh" 412391SN/A#include "config/full_system.hh" 422592SN/A#include "mem/packet_impl.hh" 432394SN/A#include "mem/physical.hh" 442391SN/A#include "sim/host.hh" 452391SN/A#include "sim/builder.hh" 462415SN/A#include "sim/eventq.hh" 472423SN/A#include "arch/isa_traits.hh" 482391SN/A 492394SN/A 502391SN/Ausing namespace std; 512423SN/Ausing namespace TheISA; 522391SN/A 532630SN/APhysicalMemory::MemResponseEvent::MemResponseEvent(Packet *pkt, MemoryPort* _m) 542415SN/A : Event(&mainEventQueue, CPU_Tick_Pri), pkt(pkt), memoryPort(_m) 552415SN/A{ 562415SN/A 572415SN/A this->setFlags(AutoDelete); 582415SN/A} 592415SN/A 602415SN/Avoid 612415SN/APhysicalMemory::MemResponseEvent::process() 622415SN/A{ 632415SN/A memoryPort->sendTiming(pkt); 642415SN/A} 652415SN/A 662415SN/Aconst char * 672415SN/APhysicalMemory::MemResponseEvent::description() 682415SN/A{ 692415SN/A return "Physical Memory Timing Access respnse event"; 702415SN/A} 712415SN/A 722565SN/APhysicalMemory::PhysicalMemory(const string &n, Tick latency) 732565SN/A : MemObject(n),base_addr(0), pmem_addr(NULL), port(NULL), lat(latency) 742391SN/A{ 752391SN/A // Hardcoded to 128 MB for now. 762391SN/A pmem_size = 1 << 27; 772391SN/A 782391SN/A if (pmem_size % TheISA::PageBytes != 0) 792391SN/A panic("Memory Size not divisible by page size\n"); 802391SN/A 812391SN/A int map_flags = MAP_ANON | MAP_PRIVATE; 822391SN/A pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 832391SN/A map_flags, -1, 0); 842391SN/A 852391SN/A if (pmem_addr == (void *)MAP_FAILED) { 862391SN/A perror("mmap"); 872391SN/A fatal("Could not mmap!\n"); 882391SN/A } 892391SN/A 902391SN/A page_ptr = 0; 912391SN/A} 922391SN/A 932541SN/Avoid 942541SN/APhysicalMemory::init() 952541SN/A{ 962541SN/A if (!port) 972541SN/A panic("PhysicalMemory not connected to anything!"); 982541SN/A port->sendStatusChange(Port::RangeChange); 992541SN/A} 1002541SN/A 1012391SN/APhysicalMemory::~PhysicalMemory() 1022391SN/A{ 1032391SN/A if (pmem_addr) 1042391SN/A munmap(pmem_addr, pmem_size); 1052416SN/A //Remove memPorts? 1062391SN/A} 1072391SN/A 1082391SN/AAddr 1092391SN/APhysicalMemory::new_page() 1102391SN/A{ 1112391SN/A Addr return_addr = page_ptr << LogVMPageSize; 1122391SN/A return_addr += base_addr; 1132391SN/A 1142391SN/A ++page_ptr; 1152391SN/A return return_addr; 1162391SN/A} 1172391SN/A 1182408SN/Aint 1192408SN/APhysicalMemory::deviceBlockSize() 1202408SN/A{ 1212409SN/A //Can accept anysize request 1222409SN/A return 0; 1232408SN/A} 1242408SN/A 1252413SN/Abool 1262630SN/APhysicalMemory::doTimingAccess (Packet *pkt, MemoryPort* memoryPort) 1272413SN/A{ 1282413SN/A doFunctionalAccess(pkt); 1292415SN/A 1302639Sstever@eecs.umich.edu // turn packet around to go back to requester 1312630SN/A pkt->dest = pkt->src; 1322416SN/A MemResponseEvent* response = new MemResponseEvent(pkt, memoryPort); 1332415SN/A response->schedule(curTick + lat); 1342415SN/A 1352413SN/A return true; 1362413SN/A} 1372413SN/A 1382413SN/ATick 1392630SN/APhysicalMemory::doAtomicAccess(Packet *pkt) 1402413SN/A{ 1412413SN/A doFunctionalAccess(pkt); 1422630SN/A pkt->time = curTick + lat; 1432413SN/A return curTick + lat; 1442413SN/A} 1452413SN/A 1462413SN/Avoid 1472630SN/APhysicalMemory::doFunctionalAccess(Packet *pkt) 1482413SN/A{ 1492630SN/A assert(pkt->addr + pkt->size < pmem_size); 1502414SN/A 1512630SN/A switch (pkt->cmd) { 1522413SN/A case Read: 1532630SN/A memcpy(pkt->getPtr<uint8_t>(), pmem_addr + pkt->addr - base_addr, 1542630SN/A pkt->size); 1552418SN/A break; 1562413SN/A case Write: 1572630SN/A memcpy(pmem_addr + pkt->addr - base_addr, pkt->getPtr<uint8_t>(), 1582630SN/A pkt->size); 1592631SN/A // temporary hack: will need to add real LL/SC implementation 1602631SN/A // for cacheless systems later. 1612631SN/A if (pkt->req->getFlags() & LOCKED) { 1622631SN/A pkt->req->setScResult(1); 1632631SN/A } 1642418SN/A break; 1652413SN/A default: 1662413SN/A panic("unimplemented"); 1672413SN/A } 1682420SN/A 1692630SN/A pkt->result = Success; 1702413SN/A} 1712413SN/A 1722413SN/APort * 1732499SN/APhysicalMemory::getPort(const std::string &if_name) 1742413SN/A{ 1752499SN/A if (if_name == "") { 1762499SN/A if (port != NULL) 1772499SN/A panic("PhysicalMemory::getPort: additional port requested to memory!"); 1782499SN/A port = new MemoryPort(this); 1792499SN/A return port; 1802519SN/A } else if (if_name == "functional") { 1812519SN/A /* special port for functional writes at startup. */ 1822519SN/A return new MemoryPort(this); 1832462SN/A } else { 1842462SN/A panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 1852462SN/A } 1862413SN/A} 1872413SN/A 1882413SN/Avoid 1892413SN/APhysicalMemory::recvStatusChange(Port::Status status) 1902413SN/A{ 1912413SN/A} 1922413SN/A 1932413SN/APhysicalMemory::MemoryPort::MemoryPort(PhysicalMemory *_memory) 1942413SN/A : memory(_memory) 1952413SN/A{ } 1962413SN/A 1972413SN/Avoid 1982413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 1992413SN/A{ 2002413SN/A memory->recvStatusChange(status); 2012413SN/A} 2022413SN/A 2032413SN/Avoid 2042522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 2052522SN/A AddrRangeList &snoop) 2062413SN/A{ 2072522SN/A memory->getAddressRanges(resp, snoop); 2082497SN/A} 2092497SN/A 2102497SN/Avoid 2112522SN/APhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 2122497SN/A{ 2132522SN/A snoop.clear(); 2142522SN/A resp.clear(); 2152522SN/A resp.push_back(RangeSize(base_addr, pmem_size)); 2162413SN/A} 2172413SN/A 2182415SN/Aint 2192415SN/APhysicalMemory::MemoryPort::deviceBlockSize() 2202415SN/A{ 2212415SN/A return memory->deviceBlockSize(); 2222415SN/A} 2232413SN/A 2242413SN/Abool 2252630SN/APhysicalMemory::MemoryPort::recvTiming(Packet *pkt) 2262413SN/A{ 2272416SN/A return memory->doTimingAccess(pkt, this); 2282413SN/A} 2292413SN/A 2302413SN/ATick 2312630SN/APhysicalMemory::MemoryPort::recvAtomic(Packet *pkt) 2322413SN/A{ 2332413SN/A return memory->doAtomicAccess(pkt); 2342413SN/A} 2352413SN/A 2362413SN/Avoid 2372630SN/APhysicalMemory::MemoryPort::recvFunctional(Packet *pkt) 2382413SN/A{ 2392413SN/A memory->doFunctionalAccess(pkt); 2402413SN/A} 2412413SN/A 2422413SN/A 2432413SN/A 2442391SN/Avoid 2452391SN/APhysicalMemory::serialize(ostream &os) 2462391SN/A{ 2472391SN/A gzFile compressedMem; 2482391SN/A string filename = name() + ".physmem"; 2492391SN/A 2502391SN/A SERIALIZE_SCALAR(pmem_size); 2512391SN/A SERIALIZE_SCALAR(filename); 2522391SN/A 2532391SN/A // write memory file 2542391SN/A string thefile = Checkpoint::dir() + "/" + filename.c_str(); 2552391SN/A int fd = creat(thefile.c_str(), 0664); 2562391SN/A if (fd < 0) { 2572391SN/A perror("creat"); 2582391SN/A fatal("Can't open physical memory checkpoint file '%s'\n", filename); 2592391SN/A } 2602391SN/A 2612391SN/A compressedMem = gzdopen(fd, "wb"); 2622391SN/A if (compressedMem == NULL) 2632391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 2642391SN/A filename); 2652391SN/A 2662391SN/A if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) { 2672391SN/A fatal("Write failed on physical memory checkpoint file '%s'\n", 2682391SN/A filename); 2692391SN/A } 2702391SN/A 2712391SN/A if (gzclose(compressedMem)) 2722391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 2732391SN/A filename); 2742391SN/A} 2752391SN/A 2762391SN/Avoid 2772391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 2782391SN/A{ 2792391SN/A gzFile compressedMem; 2802391SN/A long *tempPage; 2812391SN/A long *pmem_current; 2822391SN/A uint64_t curSize; 2832391SN/A uint32_t bytesRead; 2842391SN/A const int chunkSize = 16384; 2852391SN/A 2862391SN/A 2872391SN/A // unmap file that was mmaped in the constructor 2882391SN/A munmap(pmem_addr, pmem_size); 2892391SN/A 2902391SN/A string filename; 2912391SN/A 2922391SN/A UNSERIALIZE_SCALAR(pmem_size); 2932391SN/A UNSERIALIZE_SCALAR(filename); 2942391SN/A 2952391SN/A filename = cp->cptDir + "/" + filename; 2962391SN/A 2972391SN/A // mmap memoryfile 2982391SN/A int fd = open(filename.c_str(), O_RDONLY); 2992391SN/A if (fd < 0) { 3002391SN/A perror("open"); 3012391SN/A fatal("Can't open physical memory checkpoint file '%s'", filename); 3022391SN/A } 3032391SN/A 3042391SN/A compressedMem = gzdopen(fd, "rb"); 3052391SN/A if (compressedMem == NULL) 3062391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 3072391SN/A filename); 3082391SN/A 3092391SN/A 3102391SN/A pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 3112391SN/A MAP_ANON | MAP_PRIVATE, -1, 0); 3122391SN/A 3132391SN/A if (pmem_addr == (void *)MAP_FAILED) { 3142391SN/A perror("mmap"); 3152391SN/A fatal("Could not mmap physical memory!\n"); 3162391SN/A } 3172391SN/A 3182391SN/A curSize = 0; 3192391SN/A tempPage = (long*)malloc(chunkSize); 3202391SN/A if (tempPage == NULL) 3212391SN/A fatal("Unable to malloc memory to read file %s\n", filename); 3222391SN/A 3232391SN/A /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 3242391SN/A while (curSize < pmem_size) { 3252391SN/A bytesRead = gzread(compressedMem, tempPage, chunkSize); 3262391SN/A if (bytesRead != chunkSize && bytesRead != pmem_size - curSize) 3272391SN/A fatal("Read failed on physical memory checkpoint file '%s'" 3282391SN/A " got %d bytes, expected %d or %d bytes\n", 3292391SN/A filename, bytesRead, chunkSize, pmem_size-curSize); 3302391SN/A 3312391SN/A assert(bytesRead % sizeof(long) == 0); 3322391SN/A 3332391SN/A for (int x = 0; x < bytesRead/sizeof(long); x++) 3342391SN/A { 3352391SN/A if (*(tempPage+x) != 0) { 3362391SN/A pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long)); 3372391SN/A *pmem_current = *(tempPage+x); 3382391SN/A } 3392391SN/A } 3402391SN/A curSize += bytesRead; 3412391SN/A } 3422391SN/A 3432391SN/A free(tempPage); 3442391SN/A 3452391SN/A if (gzclose(compressedMem)) 3462391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 3472391SN/A filename); 3482391SN/A 3492391SN/A} 3502391SN/A 3512413SN/A 3522391SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3532391SN/A 3542391SN/A Param<string> file; 3552391SN/A Param<Range<Addr> > range; 3562565SN/A Param<Tick> latency; 3572391SN/A 3582391SN/AEND_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3592391SN/A 3602391SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3612391SN/A 3622391SN/A INIT_PARAM_DFLT(file, "memory mapped file", ""), 3632565SN/A INIT_PARAM(range, "Device Address Range"), 3642565SN/A INIT_PARAM(latency, "Memory access latency") 3652391SN/A 3662391SN/AEND_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3672391SN/A 3682391SN/ACREATE_SIM_OBJECT(PhysicalMemory) 3692391SN/A{ 3702391SN/A 3712565SN/A return new PhysicalMemory(getInstanceName(), latency); 3722391SN/A} 3732391SN/A 3742391SN/AREGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 375