physical.cc revision 2914
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 422391SN/A 432391SN/A#include "base/misc.hh" 442391SN/A#include "config/full_system.hh" 452592SN/A#include "mem/packet_impl.hh" 462394SN/A#include "mem/physical.hh" 472391SN/A#include "sim/host.hh" 482391SN/A#include "sim/builder.hh" 492415SN/A#include "sim/eventq.hh" 502423SN/A#include "arch/isa_traits.hh" 512391SN/A 522394SN/A 532391SN/Ausing namespace std; 542423SN/Ausing namespace TheISA; 552391SN/A 562415SN/A 572565SN/APhysicalMemory::PhysicalMemory(const string &n, Tick latency) 582565SN/A : MemObject(n),base_addr(0), pmem_addr(NULL), port(NULL), lat(latency) 592391SN/A{ 602391SN/A // Hardcoded to 128 MB for now. 612391SN/A pmem_size = 1 << 27; 622391SN/A 632391SN/A if (pmem_size % TheISA::PageBytes != 0) 642391SN/A panic("Memory Size not divisible by page size\n"); 652391SN/A 662391SN/A int map_flags = MAP_ANON | MAP_PRIVATE; 672391SN/A pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 682391SN/A map_flags, -1, 0); 692391SN/A 702391SN/A if (pmem_addr == (void *)MAP_FAILED) { 712391SN/A perror("mmap"); 722391SN/A fatal("Could not mmap!\n"); 732391SN/A } 742391SN/A 752391SN/A page_ptr = 0; 762391SN/A} 772391SN/A 782541SN/Avoid 792541SN/APhysicalMemory::init() 802541SN/A{ 812541SN/A if (!port) 822541SN/A panic("PhysicalMemory not connected to anything!"); 832541SN/A port->sendStatusChange(Port::RangeChange); 842541SN/A} 852541SN/A 862391SN/APhysicalMemory::~PhysicalMemory() 872391SN/A{ 882391SN/A if (pmem_addr) 892391SN/A munmap(pmem_addr, pmem_size); 902416SN/A //Remove memPorts? 912391SN/A} 922391SN/A 932391SN/AAddr 942391SN/APhysicalMemory::new_page() 952391SN/A{ 962391SN/A Addr return_addr = page_ptr << LogVMPageSize; 972391SN/A return_addr += base_addr; 982391SN/A 992391SN/A ++page_ptr; 1002391SN/A return return_addr; 1012391SN/A} 1022391SN/A 1032408SN/Aint 1042408SN/APhysicalMemory::deviceBlockSize() 1052408SN/A{ 1062409SN/A //Can accept anysize request 1072409SN/A return 0; 1082408SN/A} 1092408SN/A 1102413SN/A 1112413SN/ATick 1122630SN/APhysicalMemory::doFunctionalAccess(Packet *pkt) 1132413SN/A{ 1142641Sstever@eecs.umich.edu assert(pkt->getAddr() + pkt->getSize() < pmem_size); 1152414SN/A 1162630SN/A switch (pkt->cmd) { 1172641Sstever@eecs.umich.edu case Packet::ReadReq: 1182641Sstever@eecs.umich.edu memcpy(pkt->getPtr<uint8_t>(), 1192641Sstever@eecs.umich.edu pmem_addr + pkt->getAddr() - base_addr, 1202641Sstever@eecs.umich.edu pkt->getSize()); 1212418SN/A break; 1222641Sstever@eecs.umich.edu case Packet::WriteReq: 1232641Sstever@eecs.umich.edu memcpy(pmem_addr + pkt->getAddr() - base_addr, 1242641Sstever@eecs.umich.edu pkt->getPtr<uint8_t>(), 1252641Sstever@eecs.umich.edu pkt->getSize()); 1262631SN/A // temporary hack: will need to add real LL/SC implementation 1272631SN/A // for cacheless systems later. 1282631SN/A if (pkt->req->getFlags() & LOCKED) { 1292631SN/A pkt->req->setScResult(1); 1302631SN/A } 1312418SN/A break; 1322413SN/A default: 1332413SN/A panic("unimplemented"); 1342413SN/A } 1352420SN/A 1362641Sstever@eecs.umich.edu pkt->result = Packet::Success; 1372914Ssaidi@eecs.umich.edu return lat; 1382413SN/A} 1392413SN/A 1402413SN/APort * 1412738Sstever@eecs.umich.eduPhysicalMemory::getPort(const std::string &if_name, int idx) 1422413SN/A{ 1432738Sstever@eecs.umich.edu if (if_name == "port" && idx == -1) { 1442499SN/A if (port != NULL) 1452499SN/A panic("PhysicalMemory::getPort: additional port requested to memory!"); 1462640Sstever@eecs.umich.edu port = new MemoryPort(name() + "-port", this); 1472499SN/A return port; 1482519SN/A } else if (if_name == "functional") { 1492519SN/A /* special port for functional writes at startup. */ 1502640Sstever@eecs.umich.edu return new MemoryPort(name() + "-funcport", this); 1512462SN/A } else { 1522462SN/A panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 1532462SN/A } 1542413SN/A} 1552413SN/A 1562413SN/Avoid 1572413SN/APhysicalMemory::recvStatusChange(Port::Status status) 1582413SN/A{ 1592413SN/A} 1602413SN/A 1612640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 1622640Sstever@eecs.umich.edu PhysicalMemory *_memory) 1632914Ssaidi@eecs.umich.edu : SimpleTimingPort(_name), memory(_memory) 1642413SN/A{ } 1652413SN/A 1662413SN/Avoid 1672413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 1682413SN/A{ 1692413SN/A memory->recvStatusChange(status); 1702413SN/A} 1712413SN/A 1722413SN/Avoid 1732522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 1742522SN/A AddrRangeList &snoop) 1752413SN/A{ 1762522SN/A memory->getAddressRanges(resp, snoop); 1772497SN/A} 1782497SN/A 1792497SN/Avoid 1802522SN/APhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 1812497SN/A{ 1822522SN/A snoop.clear(); 1832522SN/A resp.clear(); 1842522SN/A resp.push_back(RangeSize(base_addr, pmem_size)); 1852413SN/A} 1862413SN/A 1872415SN/Aint 1882415SN/APhysicalMemory::MemoryPort::deviceBlockSize() 1892415SN/A{ 1902415SN/A return memory->deviceBlockSize(); 1912415SN/A} 1922413SN/A 1932413SN/Abool 1942630SN/APhysicalMemory::MemoryPort::recvTiming(Packet *pkt) 1952413SN/A{ 1962914Ssaidi@eecs.umich.edu assert(pkt->result != Packet::Nacked); 1972914Ssaidi@eecs.umich.edu 1982914Ssaidi@eecs.umich.edu Tick latency = memory->doFunctionalAccess(pkt); 1992914Ssaidi@eecs.umich.edu 2002914Ssaidi@eecs.umich.edu pkt->makeTimingResponse(); 2012914Ssaidi@eecs.umich.edu sendTiming(pkt, latency); 2022914Ssaidi@eecs.umich.edu 2032914Ssaidi@eecs.umich.edu return true; 2042413SN/A} 2052413SN/A 2062413SN/ATick 2072630SN/APhysicalMemory::MemoryPort::recvAtomic(Packet *pkt) 2082413SN/A{ 2092914Ssaidi@eecs.umich.edu return memory->doFunctionalAccess(pkt); 2102413SN/A} 2112413SN/A 2122413SN/Avoid 2132630SN/APhysicalMemory::MemoryPort::recvFunctional(Packet *pkt) 2142413SN/A{ 2152413SN/A memory->doFunctionalAccess(pkt); 2162413SN/A} 2172413SN/A 2182914Ssaidi@eecs.umich.eduunsigned int 2192914Ssaidi@eecs.umich.eduPhysicalMemory::drain(Event *de) 2202914Ssaidi@eecs.umich.edu{ 2212914Ssaidi@eecs.umich.edu int count = port->drain(de); 2222914Ssaidi@eecs.umich.edu if (count) 2232914Ssaidi@eecs.umich.edu changeState(Draining); 2242914Ssaidi@eecs.umich.edu else 2252914Ssaidi@eecs.umich.edu changeState(Drained); 2262914Ssaidi@eecs.umich.edu return count; 2272914Ssaidi@eecs.umich.edu} 2282413SN/A 2292391SN/Avoid 2302391SN/APhysicalMemory::serialize(ostream &os) 2312391SN/A{ 2322391SN/A gzFile compressedMem; 2332391SN/A string filename = name() + ".physmem"; 2342391SN/A 2352391SN/A SERIALIZE_SCALAR(pmem_size); 2362391SN/A SERIALIZE_SCALAR(filename); 2372391SN/A 2382391SN/A // write memory file 2392391SN/A string thefile = Checkpoint::dir() + "/" + filename.c_str(); 2402391SN/A int fd = creat(thefile.c_str(), 0664); 2412391SN/A if (fd < 0) { 2422391SN/A perror("creat"); 2432391SN/A fatal("Can't open physical memory checkpoint file '%s'\n", filename); 2442391SN/A } 2452391SN/A 2462391SN/A compressedMem = gzdopen(fd, "wb"); 2472391SN/A if (compressedMem == NULL) 2482391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 2492391SN/A filename); 2502391SN/A 2512391SN/A if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) { 2522391SN/A fatal("Write failed on physical memory checkpoint file '%s'\n", 2532391SN/A filename); 2542391SN/A } 2552391SN/A 2562391SN/A if (gzclose(compressedMem)) 2572391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 2582391SN/A filename); 2592391SN/A} 2602391SN/A 2612391SN/Avoid 2622391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 2632391SN/A{ 2642391SN/A gzFile compressedMem; 2652391SN/A long *tempPage; 2662391SN/A long *pmem_current; 2672391SN/A uint64_t curSize; 2682391SN/A uint32_t bytesRead; 2692391SN/A const int chunkSize = 16384; 2702391SN/A 2712391SN/A 2722391SN/A // unmap file that was mmaped in the constructor 2732391SN/A munmap(pmem_addr, pmem_size); 2742391SN/A 2752391SN/A string filename; 2762391SN/A 2772391SN/A UNSERIALIZE_SCALAR(pmem_size); 2782391SN/A UNSERIALIZE_SCALAR(filename); 2792391SN/A 2802391SN/A filename = cp->cptDir + "/" + filename; 2812391SN/A 2822391SN/A // mmap memoryfile 2832391SN/A int fd = open(filename.c_str(), O_RDONLY); 2842391SN/A if (fd < 0) { 2852391SN/A perror("open"); 2862391SN/A fatal("Can't open physical memory checkpoint file '%s'", filename); 2872391SN/A } 2882391SN/A 2892391SN/A compressedMem = gzdopen(fd, "rb"); 2902391SN/A if (compressedMem == NULL) 2912391SN/A fatal("Insufficient memory to allocate compression state for %s\n", 2922391SN/A filename); 2932391SN/A 2942391SN/A 2952391SN/A pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 2962391SN/A MAP_ANON | MAP_PRIVATE, -1, 0); 2972391SN/A 2982391SN/A if (pmem_addr == (void *)MAP_FAILED) { 2992391SN/A perror("mmap"); 3002391SN/A fatal("Could not mmap physical memory!\n"); 3012391SN/A } 3022391SN/A 3032391SN/A curSize = 0; 3042391SN/A tempPage = (long*)malloc(chunkSize); 3052391SN/A if (tempPage == NULL) 3062391SN/A fatal("Unable to malloc memory to read file %s\n", filename); 3072391SN/A 3082391SN/A /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 3092391SN/A while (curSize < pmem_size) { 3102391SN/A bytesRead = gzread(compressedMem, tempPage, chunkSize); 3112391SN/A if (bytesRead != chunkSize && bytesRead != pmem_size - curSize) 3122391SN/A fatal("Read failed on physical memory checkpoint file '%s'" 3132391SN/A " got %d bytes, expected %d or %d bytes\n", 3142391SN/A filename, bytesRead, chunkSize, pmem_size-curSize); 3152391SN/A 3162391SN/A assert(bytesRead % sizeof(long) == 0); 3172391SN/A 3182391SN/A for (int x = 0; x < bytesRead/sizeof(long); x++) 3192391SN/A { 3202391SN/A if (*(tempPage+x) != 0) { 3212391SN/A pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long)); 3222391SN/A *pmem_current = *(tempPage+x); 3232391SN/A } 3242391SN/A } 3252391SN/A curSize += bytesRead; 3262391SN/A } 3272391SN/A 3282391SN/A free(tempPage); 3292391SN/A 3302391SN/A if (gzclose(compressedMem)) 3312391SN/A fatal("Close failed on physical memory checkpoint file '%s'\n", 3322391SN/A filename); 3332391SN/A 3342391SN/A} 3352391SN/A 3362413SN/A 3372391SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3382391SN/A 3392391SN/A Param<string> file; 3402391SN/A Param<Range<Addr> > range; 3412565SN/A Param<Tick> latency; 3422391SN/A 3432391SN/AEND_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 3442391SN/A 3452391SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3462391SN/A 3472391SN/A INIT_PARAM_DFLT(file, "memory mapped file", ""), 3482565SN/A INIT_PARAM(range, "Device Address Range"), 3492565SN/A INIT_PARAM(latency, "Memory access latency") 3502391SN/A 3512391SN/AEND_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 3522391SN/A 3532391SN/ACREATE_SIM_OBJECT(PhysicalMemory) 3542391SN/A{ 3552391SN/A 3562565SN/A return new PhysicalMemory(getInstanceName(), latency); 3572391SN/A} 3582391SN/A 3592391SN/AREGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 360