physical.cc revision 2640
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!"); 1782640Sstever@eecs.umich.edu port = new MemoryPort(name() + "-port", this); 1792499SN/A return port; 1802519SN/A } else if (if_name == "functional") { 1812519SN/A /* special port for functional writes at startup. */ 1822640Sstever@eecs.umich.edu return new MemoryPort(name() + "-funcport", 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 1932640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 1942640Sstever@eecs.umich.edu PhysicalMemory *_memory) 1952640Sstever@eecs.umich.edu : Port(_name), memory(_memory) 1962413SN/A{ } 1972413SN/A 1982413SN/Avoid 1992413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 2002413SN/A{ 2012413SN/A memory->recvStatusChange(status); 2022413SN/A} 2032413SN/A 2042413SN/Avoid 2052522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 2062522SN/A AddrRangeList &snoop) 2072413SN/A{ 2082522SN/A memory->getAddressRanges(resp, snoop); 2092497SN/A} 2102497SN/A 2112497SN/Avoid 2122522SN/APhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 2132497SN/A{ 2142522SN/A snoop.clear(); 2152522SN/A resp.clear(); 2162522SN/A resp.push_back(RangeSize(base_addr, pmem_size)); 2172413SN/A} 2182413SN/A 2192415SN/Aint 2202415SN/APhysicalMemory::MemoryPort::deviceBlockSize() 2212415SN/A{ 2222415SN/A return memory->deviceBlockSize(); 2232415SN/A} 2242413SN/A 2252413SN/Abool 2262630SN/APhysicalMemory::MemoryPort::recvTiming(Packet *pkt) 2272413SN/A{ 2282416SN/A return memory->doTimingAccess(pkt, this); 2292413SN/A} 2302413SN/A 2312413SN/ATick 2322630SN/APhysicalMemory::MemoryPort::recvAtomic(Packet *pkt) 2332413SN/A{ 2342413SN/A return memory->doAtomicAccess(pkt); 2352413SN/A} 2362413SN/A 2372413SN/Avoid 2382630SN/APhysicalMemory::MemoryPort::recvFunctional(Packet *pkt) 2392413SN/A{ 2402413SN/A memory->doFunctionalAccess(pkt); 2412413SN/A} 2422413SN/A 2432413SN/A 2442413SN/A 2452391SN/Avoid 2462391SN/APhysicalMemory::serialize(ostream &os) 2472391SN/A{ 2482391SN/A gzFile compressedMem; 2492391SN/A string filename = name() + ".physmem"; 2502391SN/A 2512391SN/A SERIALIZE_SCALAR(pmem_size); 2522391SN/A SERIALIZE_SCALAR(filename); 2532391SN/A 2542391SN/A // write memory file 2552391SN/A string thefile = Checkpoint::dir() + "/" + filename.c_str(); 2562391SN/A int fd = creat(thefile.c_str(), 0664); 2572391SN/A if (fd < 0) { 2582391SN/A perror("creat"); 2592391SN/A fatal("Can't open physical memory checkpoint file '%s'\n", filename); 2602391SN/A } 2612391SN/A 2622391SN/A compressedMem = gzdopen(fd, "wb"); 2632391SN/A if (compressedMem == NULL) 2642391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 2652391SN/A filename); 2662391SN/A 2672391SN/A if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) { 2682391SN/A fatal("Write failed on physical memory checkpoint file '%s'\n", 2692391SN/A filename); 2702391SN/A } 2712391SN/A 2722391SN/A if (gzclose(compressedMem)) 2732391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 2742391SN/A filename); 2752391SN/A} 2762391SN/A 2772391SN/Avoid 2782391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 2792391SN/A{ 2802391SN/A gzFile compressedMem; 2812391SN/A long *tempPage; 2822391SN/A long *pmem_current; 2832391SN/A uint64_t curSize; 2842391SN/A uint32_t bytesRead; 2852391SN/A const int chunkSize = 16384; 2862391SN/A 2872391SN/A 2882391SN/A // unmap file that was mmaped in the constructor 2892391SN/A munmap(pmem_addr, pmem_size); 2902391SN/A 2912391SN/A string filename; 2922391SN/A 2932391SN/A UNSERIALIZE_SCALAR(pmem_size); 2942391SN/A UNSERIALIZE_SCALAR(filename); 2952391SN/A 2962391SN/A filename = cp->cptDir + "/" + filename; 2972391SN/A 2982391SN/A // mmap memoryfile 2992391SN/A int fd = open(filename.c_str(), O_RDONLY); 3002391SN/A if (fd < 0) { 3012391SN/A perror("open"); 3022391SN/A fatal("Can't open physical memory checkpoint file '%s'", filename); 3032391SN/A } 3042391SN/A 3052391SN/A compressedMem = gzdopen(fd, "rb"); 3062391SN/A if (compressedMem == NULL) 3072391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 3082391SN/A filename); 3092391SN/A 3102391SN/A 3112391SN/A pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 3122391SN/A MAP_ANON | MAP_PRIVATE, -1, 0); 3132391SN/A 3142391SN/A if (pmem_addr == (void *)MAP_FAILED) { 3152391SN/A perror("mmap"); 3162391SN/A fatal("Could not mmap physical memory!\n"); 3172391SN/A } 3182391SN/A 3192391SN/A curSize = 0; 3202391SN/A tempPage = (long*)malloc(chunkSize); 3212391SN/A if (tempPage == NULL) 3222391SN/A fatal("Unable to malloc memory to read file %s\n", filename); 3232391SN/A 3242391SN/A /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 3252391SN/A while (curSize < pmem_size) { 3262391SN/A bytesRead = gzread(compressedMem, tempPage, chunkSize); 3272391SN/A if (bytesRead != chunkSize && bytesRead != pmem_size - curSize) 3282391SN/A fatal("Read failed on physical memory checkpoint file '%s'" 3292391SN/A " got %d bytes, expected %d or %d bytes\n", 3302391SN/A filename, bytesRead, chunkSize, pmem_size-curSize); 3312391SN/A 3322391SN/A assert(bytesRead % sizeof(long) == 0); 3332391SN/A 3342391SN/A for (int x = 0; x < bytesRead/sizeof(long); x++) 3352391SN/A { 3362391SN/A if (*(tempPage+x) != 0) { 3372391SN/A pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long)); 3382391SN/A *pmem_current = *(tempPage+x); 3392391SN/A } 3402391SN/A } 3412391SN/A curSize += bytesRead; 3422391SN/A } 3432391SN/A 3442391SN/A free(tempPage); 3452391SN/A 3462391SN/A if (gzclose(compressedMem)) 3472391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 3482391SN/A filename); 3492391SN/A 3502391SN/A} 3512391SN/A 3522413SN/A 3532391SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3542391SN/A 3552391SN/A Param<string> file; 3562391SN/A Param<Range<Addr> > range; 3572565SN/A Param<Tick> latency; 3582391SN/A 3592391SN/AEND_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3602391SN/A 3612391SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3622391SN/A 3632391SN/A INIT_PARAM_DFLT(file, "memory mapped file", ""), 3642565SN/A INIT_PARAM(range, "Device Address Range"), 3652565SN/A INIT_PARAM(latency, "Memory access latency") 3662391SN/A 3672391SN/AEND_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3682391SN/A 3692391SN/ACREATE_SIM_OBJECT(PhysicalMemory) 3702391SN/A{ 3712391SN/A 3722565SN/A return new PhysicalMemory(getInstanceName(), latency); 3732391SN/A} 3742391SN/A 3752391SN/AREGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 376