physical.cc revision 2641
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
1312641Sstever@eecs.umich.edu    pkt->makeTimingResponse();
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{
1492641Sstever@eecs.umich.edu    assert(pkt->getAddr() + pkt->getSize() < pmem_size);
1502414SN/A
1512630SN/A    switch (pkt->cmd) {
1522641Sstever@eecs.umich.edu      case Packet::ReadReq:
1532641Sstever@eecs.umich.edu        memcpy(pkt->getPtr<uint8_t>(),
1542641Sstever@eecs.umich.edu               pmem_addr + pkt->getAddr() - base_addr,
1552641Sstever@eecs.umich.edu               pkt->getSize());
1562418SN/A        break;
1572641Sstever@eecs.umich.edu      case Packet::WriteReq:
1582641Sstever@eecs.umich.edu        memcpy(pmem_addr + pkt->getAddr() - base_addr,
1592641Sstever@eecs.umich.edu               pkt->getPtr<uint8_t>(),
1602641Sstever@eecs.umich.edu               pkt->getSize());
1612631SN/A        // temporary hack: will need to add real LL/SC implementation
1622631SN/A        // for cacheless systems later.
1632631SN/A        if (pkt->req->getFlags() & LOCKED) {
1642631SN/A            pkt->req->setScResult(1);
1652631SN/A        }
1662418SN/A        break;
1672413SN/A      default:
1682413SN/A        panic("unimplemented");
1692413SN/A    }
1702420SN/A
1712641Sstever@eecs.umich.edu    pkt->result = Packet::Success;
1722413SN/A}
1732413SN/A
1742413SN/APort *
1752499SN/APhysicalMemory::getPort(const std::string &if_name)
1762413SN/A{
1772499SN/A    if (if_name == "") {
1782499SN/A        if (port != NULL)
1792499SN/A           panic("PhysicalMemory::getPort: additional port requested to memory!");
1802640Sstever@eecs.umich.edu        port = new MemoryPort(name() + "-port", this);
1812499SN/A        return port;
1822519SN/A    } else if (if_name == "functional") {
1832519SN/A        /* special port for functional writes at startup. */
1842640Sstever@eecs.umich.edu        return new MemoryPort(name() + "-funcport", this);
1852462SN/A    } else {
1862462SN/A        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
1872462SN/A    }
1882413SN/A}
1892413SN/A
1902413SN/Avoid
1912413SN/APhysicalMemory::recvStatusChange(Port::Status status)
1922413SN/A{
1932413SN/A}
1942413SN/A
1952640Sstever@eecs.umich.eduPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
1962640Sstever@eecs.umich.edu                                       PhysicalMemory *_memory)
1972640Sstever@eecs.umich.edu    : Port(_name), memory(_memory)
1982413SN/A{ }
1992413SN/A
2002413SN/Avoid
2012413SN/APhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
2022413SN/A{
2032413SN/A    memory->recvStatusChange(status);
2042413SN/A}
2052413SN/A
2062413SN/Avoid
2072522SN/APhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
2082522SN/A                                            AddrRangeList &snoop)
2092413SN/A{
2102522SN/A    memory->getAddressRanges(resp, snoop);
2112497SN/A}
2122497SN/A
2132497SN/Avoid
2142522SN/APhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
2152497SN/A{
2162522SN/A    snoop.clear();
2172522SN/A    resp.clear();
2182522SN/A    resp.push_back(RangeSize(base_addr, pmem_size));
2192413SN/A}
2202413SN/A
2212415SN/Aint
2222415SN/APhysicalMemory::MemoryPort::deviceBlockSize()
2232415SN/A{
2242415SN/A    return memory->deviceBlockSize();
2252415SN/A}
2262413SN/A
2272413SN/Abool
2282630SN/APhysicalMemory::MemoryPort::recvTiming(Packet *pkt)
2292413SN/A{
2302416SN/A    return memory->doTimingAccess(pkt, this);
2312413SN/A}
2322413SN/A
2332413SN/ATick
2342630SN/APhysicalMemory::MemoryPort::recvAtomic(Packet *pkt)
2352413SN/A{
2362413SN/A    return memory->doAtomicAccess(pkt);
2372413SN/A}
2382413SN/A
2392413SN/Avoid
2402630SN/APhysicalMemory::MemoryPort::recvFunctional(Packet *pkt)
2412413SN/A{
2422413SN/A    memory->doFunctionalAccess(pkt);
2432413SN/A}
2442413SN/A
2452413SN/A
2462413SN/A
2472391SN/Avoid
2482391SN/APhysicalMemory::serialize(ostream &os)
2492391SN/A{
2502391SN/A    gzFile compressedMem;
2512391SN/A    string filename = name() + ".physmem";
2522391SN/A
2532391SN/A    SERIALIZE_SCALAR(pmem_size);
2542391SN/A    SERIALIZE_SCALAR(filename);
2552391SN/A
2562391SN/A    // write memory file
2572391SN/A    string thefile = Checkpoint::dir() + "/" + filename.c_str();
2582391SN/A    int fd = creat(thefile.c_str(), 0664);
2592391SN/A    if (fd < 0) {
2602391SN/A        perror("creat");
2612391SN/A        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
2622391SN/A    }
2632391SN/A
2642391SN/A    compressedMem = gzdopen(fd, "wb");
2652391SN/A    if (compressedMem == NULL)
2662391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
2672391SN/A                filename);
2682391SN/A
2692391SN/A    if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) {
2702391SN/A        fatal("Write failed on physical memory checkpoint file '%s'\n",
2712391SN/A              filename);
2722391SN/A    }
2732391SN/A
2742391SN/A    if (gzclose(compressedMem))
2752391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
2762391SN/A              filename);
2772391SN/A}
2782391SN/A
2792391SN/Avoid
2802391SN/APhysicalMemory::unserialize(Checkpoint *cp, const string &section)
2812391SN/A{
2822391SN/A    gzFile compressedMem;
2832391SN/A    long *tempPage;
2842391SN/A    long *pmem_current;
2852391SN/A    uint64_t curSize;
2862391SN/A    uint32_t bytesRead;
2872391SN/A    const int chunkSize = 16384;
2882391SN/A
2892391SN/A
2902391SN/A    // unmap file that was mmaped in the constructor
2912391SN/A    munmap(pmem_addr, pmem_size);
2922391SN/A
2932391SN/A    string filename;
2942391SN/A
2952391SN/A    UNSERIALIZE_SCALAR(pmem_size);
2962391SN/A    UNSERIALIZE_SCALAR(filename);
2972391SN/A
2982391SN/A    filename = cp->cptDir + "/" + filename;
2992391SN/A
3002391SN/A    // mmap memoryfile
3012391SN/A    int fd = open(filename.c_str(), O_RDONLY);
3022391SN/A    if (fd < 0) {
3032391SN/A        perror("open");
3042391SN/A        fatal("Can't open physical memory checkpoint file '%s'", filename);
3052391SN/A    }
3062391SN/A
3072391SN/A    compressedMem = gzdopen(fd, "rb");
3082391SN/A    if (compressedMem == NULL)
3092391SN/A        fatal("Insufficient memory to allocate compression state for %s\n",
3102391SN/A                filename);
3112391SN/A
3122391SN/A
3132391SN/A    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
3142391SN/A                                MAP_ANON | MAP_PRIVATE, -1, 0);
3152391SN/A
3162391SN/A    if (pmem_addr == (void *)MAP_FAILED) {
3172391SN/A        perror("mmap");
3182391SN/A        fatal("Could not mmap physical memory!\n");
3192391SN/A    }
3202391SN/A
3212391SN/A    curSize = 0;
3222391SN/A    tempPage = (long*)malloc(chunkSize);
3232391SN/A    if (tempPage == NULL)
3242391SN/A        fatal("Unable to malloc memory to read file %s\n", filename);
3252391SN/A
3262391SN/A    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
3272391SN/A    while (curSize < pmem_size) {
3282391SN/A        bytesRead = gzread(compressedMem, tempPage, chunkSize);
3292391SN/A        if (bytesRead != chunkSize && bytesRead != pmem_size - curSize)
3302391SN/A            fatal("Read failed on physical memory checkpoint file '%s'"
3312391SN/A                  " got %d bytes, expected %d or %d bytes\n",
3322391SN/A                  filename, bytesRead, chunkSize, pmem_size-curSize);
3332391SN/A
3342391SN/A        assert(bytesRead % sizeof(long) == 0);
3352391SN/A
3362391SN/A        for (int x = 0; x < bytesRead/sizeof(long); x++)
3372391SN/A        {
3382391SN/A             if (*(tempPage+x) != 0) {
3392391SN/A                 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long));
3402391SN/A                 *pmem_current = *(tempPage+x);
3412391SN/A             }
3422391SN/A        }
3432391SN/A        curSize += bytesRead;
3442391SN/A    }
3452391SN/A
3462391SN/A    free(tempPage);
3472391SN/A
3482391SN/A    if (gzclose(compressedMem))
3492391SN/A        fatal("Close failed on physical memory checkpoint file '%s'\n",
3502391SN/A              filename);
3512391SN/A
3522391SN/A}
3532391SN/A
3542413SN/A
3552391SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
3562391SN/A
3572391SN/A    Param<string> file;
3582391SN/A    Param<Range<Addr> > range;
3592565SN/A    Param<Tick> latency;
3602391SN/A
3612391SN/AEND_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
3622391SN/A
3632391SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
3642391SN/A
3652391SN/A    INIT_PARAM_DFLT(file, "memory mapped file", ""),
3662565SN/A    INIT_PARAM(range, "Device Address Range"),
3672565SN/A    INIT_PARAM(latency, "Memory access latency")
3682391SN/A
3692391SN/AEND_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
3702391SN/A
3712391SN/ACREATE_SIM_OBJECT(PhysicalMemory)
3722391SN/A{
3732391SN/A
3742565SN/A    return new PhysicalMemory(getInstanceName(), latency);
3752391SN/A}
3762391SN/A
3772391SN/AREGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
378