abstract_mem.cc revision 6076
16019Shines@cs.fsu.edu/*
26019Shines@cs.fsu.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan
37178Sgblack@eecs.umich.edu * All rights reserved.
47178Sgblack@eecs.umich.edu *
57178Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67178Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77178Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87178Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97178Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107178Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117178Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127178Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137178Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147178Sgblack@eecs.umich.edu * this software without specific prior written permission.
156019Shines@cs.fsu.edu *
166019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * Authors: Ron Dreslinski
296019Shines@cs.fsu.edu *          Ali Saidi
306019Shines@cs.fsu.edu */
316019Shines@cs.fsu.edu
326019Shines@cs.fsu.edu#include <sys/types.h>
336019Shines@cs.fsu.edu#include <sys/mman.h>
346019Shines@cs.fsu.edu#include <errno.h>
356019Shines@cs.fsu.edu#include <fcntl.h>
366019Shines@cs.fsu.edu#include <unistd.h>
376019Shines@cs.fsu.edu#include <zlib.h>
386019Shines@cs.fsu.edu
396019Shines@cs.fsu.edu#include <iostream>
406019Shines@cs.fsu.edu#include <string>
416019Shines@cs.fsu.edu
426019Shines@cs.fsu.edu#include "arch/isa_traits.hh"
436019Shines@cs.fsu.edu#include "base/misc.hh"
446019Shines@cs.fsu.edu#include "base/random.hh"
456019Shines@cs.fsu.edu#include "config/full_system.hh"
466019Shines@cs.fsu.edu#include "mem/packet_access.hh"
476019Shines@cs.fsu.edu#include "mem/physical.hh"
486019Shines@cs.fsu.edu#include "sim/eventq.hh"
496019Shines@cs.fsu.edu#include "sim/host.hh"
506019Shines@cs.fsu.edu
516019Shines@cs.fsu.eduusing namespace std;
526019Shines@cs.fsu.eduusing namespace TheISA;
536019Shines@cs.fsu.edu
546019Shines@cs.fsu.eduPhysicalMemory::PhysicalMemory(const Params *p)
556019Shines@cs.fsu.edu    : MemObject(p), pmemAddr(NULL), pagePtr(0),
566019Shines@cs.fsu.edu      lat(p->latency), lat_var(p->latency_var),
576019Shines@cs.fsu.edu      cachedSize(params()->range.size()), cachedStart(params()->range.start)
586243Sgblack@eecs.umich.edu{
596243Sgblack@eecs.umich.edu    if (params()->range.size() % TheISA::PageBytes != 0)
606243Sgblack@eecs.umich.edu        panic("Memory Size not divisible by page size\n");
616243Sgblack@eecs.umich.edu
626243Sgblack@eecs.umich.edu    if (params()->null)
636019Shines@cs.fsu.edu        return;
646019Shines@cs.fsu.edu
656019Shines@cs.fsu.edu    int map_flags = MAP_ANON | MAP_PRIVATE;
666019Shines@cs.fsu.edu    pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(),
676019Shines@cs.fsu.edu                               PROT_READ | PROT_WRITE, map_flags, -1, 0);
686019Shines@cs.fsu.edu
696019Shines@cs.fsu.edu    if (pmemAddr == (void *)MAP_FAILED) {
706019Shines@cs.fsu.edu        perror("mmap");
716019Shines@cs.fsu.edu        fatal("Could not mmap!\n");
726019Shines@cs.fsu.edu    }
736019Shines@cs.fsu.edu
746019Shines@cs.fsu.edu    //If requested, initialize all the memory to 0
756019Shines@cs.fsu.edu    if (p->zero)
766019Shines@cs.fsu.edu        memset(pmemAddr, 0, p->range.size());
776019Shines@cs.fsu.edu}
786019Shines@cs.fsu.edu
796019Shines@cs.fsu.eduvoid
806019Shines@cs.fsu.eduPhysicalMemory::init()
816019Shines@cs.fsu.edu{
826019Shines@cs.fsu.edu    if (ports.size() == 0) {
836019Shines@cs.fsu.edu        fatal("PhysicalMemory object %s is unconnected!", name());
846019Shines@cs.fsu.edu    }
856019Shines@cs.fsu.edu
866019Shines@cs.fsu.edu    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
876019Shines@cs.fsu.edu        if (*pi)
886019Shines@cs.fsu.edu            (*pi)->sendStatusChange(Port::RangeChange);
896019Shines@cs.fsu.edu    }
906019Shines@cs.fsu.edu}
916019Shines@cs.fsu.edu
926019Shines@cs.fsu.eduPhysicalMemory::~PhysicalMemory()
936019Shines@cs.fsu.edu{
946252Sgblack@eecs.umich.edu    if (pmemAddr)
956243Sgblack@eecs.umich.edu        munmap((char*)pmemAddr, params()->range.size());
966243Sgblack@eecs.umich.edu    //Remove memPorts?
976243Sgblack@eecs.umich.edu}
986019Shines@cs.fsu.edu
996019Shines@cs.fsu.eduAddr
1006019Shines@cs.fsu.eduPhysicalMemory::new_page()
1016019Shines@cs.fsu.edu{
1026019Shines@cs.fsu.edu    Addr return_addr = pagePtr << LogVMPageSize;
1036252Sgblack@eecs.umich.edu    return_addr += start();
1046243Sgblack@eecs.umich.edu
1056243Sgblack@eecs.umich.edu    ++pagePtr;
1066243Sgblack@eecs.umich.edu    return return_addr;
1076019Shines@cs.fsu.edu}
1086019Shines@cs.fsu.edu
1096019Shines@cs.fsu.eduint
1106019Shines@cs.fsu.eduPhysicalMemory::deviceBlockSize()
1116019Shines@cs.fsu.edu{
1126019Shines@cs.fsu.edu    //Can accept anysize request
1136019Shines@cs.fsu.edu    return 0;
1146252Sgblack@eecs.umich.edu}
1156243Sgblack@eecs.umich.edu
1166243Sgblack@eecs.umich.eduTick
1176243Sgblack@eecs.umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt)
1186019Shines@cs.fsu.edu{
1196019Shines@cs.fsu.edu    Tick latency = lat;
1206019Shines@cs.fsu.edu    if (lat_var != 0)
1216019Shines@cs.fsu.edu        latency += random_mt.random<Tick>(0, lat_var);
1226019Shines@cs.fsu.edu    return latency;
1236019Shines@cs.fsu.edu}
1246019Shines@cs.fsu.edu
1256019Shines@cs.fsu.edu
1266019Shines@cs.fsu.edu
1276019Shines@cs.fsu.edu// Add load-locked to tracking list.  Should only be called if the
1286019Shines@cs.fsu.edu// operation is a load and the LLSC flag is set.
1296019Shines@cs.fsu.eduvoid
1306019Shines@cs.fsu.eduPhysicalMemory::trackLoadLocked(PacketPtr pkt)
1316019Shines@cs.fsu.edu{
1326019Shines@cs.fsu.edu    Request *req = pkt->req;
1336019Shines@cs.fsu.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1346724Sgblack@eecs.umich.edu
1356724Sgblack@eecs.umich.edu    // first we check if we already have a locked addr for this
1366019Shines@cs.fsu.edu    // xc.  Since each xc only gets one, we just update the
1376019Shines@cs.fsu.edu    // existing record with the new address.
1386019Shines@cs.fsu.edu    list<LockedAddr>::iterator i;
1396019Shines@cs.fsu.edu
1406019Shines@cs.fsu.edu    for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
1416252Sgblack@eecs.umich.edu        if (i->matchesContext(req)) {
1426243Sgblack@eecs.umich.edu            DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
1436243Sgblack@eecs.umich.edu                    req->contextId(), paddr);
1446243Sgblack@eecs.umich.edu            i->addr = paddr;
1456019Shines@cs.fsu.edu            return;
1466019Shines@cs.fsu.edu        }
1476019Shines@cs.fsu.edu    }
1486019Shines@cs.fsu.edu
1496019Shines@cs.fsu.edu    // no record for this xc: need to allocate a new one
1506019Shines@cs.fsu.edu    DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
1517178Sgblack@eecs.umich.edu            req->contextId(), paddr);
1527178Sgblack@eecs.umich.edu    lockedAddrList.push_front(LockedAddr(req));
1537178Sgblack@eecs.umich.edu}
1547178Sgblack@eecs.umich.edu
1557178Sgblack@eecs.umich.edu
1567178Sgblack@eecs.umich.edu// Called on *writes* only... both regular stores and
1577178Sgblack@eecs.umich.edu// store-conditional operations.  Check for conventional stores which
1587178Sgblack@eecs.umich.edu// conflict with locked addresses, and for success/failure of store
1597178Sgblack@eecs.umich.edu// conditionals.
1607178Sgblack@eecs.umich.edubool
1617178Sgblack@eecs.umich.eduPhysicalMemory::checkLockedAddrList(PacketPtr pkt)
1627178Sgblack@eecs.umich.edu{
1637178Sgblack@eecs.umich.edu    Request *req = pkt->req;
1647178Sgblack@eecs.umich.edu    Addr paddr = LockedAddr::mask(req->getPaddr());
1657178Sgblack@eecs.umich.edu    bool isLlsc = pkt->isLlsc();
1667178Sgblack@eecs.umich.edu
1677178Sgblack@eecs.umich.edu    // Initialize return value.  Non-conditional stores always
1687178Sgblack@eecs.umich.edu    // succeed.  Assume conditional stores will fail until proven
1697178Sgblack@eecs.umich.edu    // otherwise.
1707178Sgblack@eecs.umich.edu    bool success = !isLlsc;
1717178Sgblack@eecs.umich.edu
1727178Sgblack@eecs.umich.edu    // Iterate over list.  Note that there could be multiple matching
1737178Sgblack@eecs.umich.edu    // records, as more than one context could have done a load locked
1747178Sgblack@eecs.umich.edu    // to this location.
1757178Sgblack@eecs.umich.edu    list<LockedAddr>::iterator i = lockedAddrList.begin();
1767178Sgblack@eecs.umich.edu
1777178Sgblack@eecs.umich.edu    while (i != lockedAddrList.end()) {
1787178Sgblack@eecs.umich.edu
1797178Sgblack@eecs.umich.edu        if (i->addr == paddr) {
1807178Sgblack@eecs.umich.edu            // we have a matching address
1817178Sgblack@eecs.umich.edu
1827178Sgblack@eecs.umich.edu            if (isLlsc && i->matchesContext(req)) {
1837178Sgblack@eecs.umich.edu                // it's a store conditional, and as far as the memory
1847178Sgblack@eecs.umich.edu                // system can tell, the requesting context's lock is
1857178Sgblack@eecs.umich.edu                // still valid.
1867178Sgblack@eecs.umich.edu                DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
1877178Sgblack@eecs.umich.edu                        req->contextId(), paddr);
1887178Sgblack@eecs.umich.edu                success = true;
1897178Sgblack@eecs.umich.edu            }
1907178Sgblack@eecs.umich.edu
1917178Sgblack@eecs.umich.edu            // Get rid of our record of this lock and advance to next
1927178Sgblack@eecs.umich.edu            DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
1937178Sgblack@eecs.umich.edu                    i->contextId, paddr);
1947178Sgblack@eecs.umich.edu            i = lockedAddrList.erase(i);
1957178Sgblack@eecs.umich.edu        }
1967178Sgblack@eecs.umich.edu        else {
1977178Sgblack@eecs.umich.edu            // no match: advance to next record
1987178Sgblack@eecs.umich.edu            ++i;
1997178Sgblack@eecs.umich.edu        }
2007178Sgblack@eecs.umich.edu    }
2017178Sgblack@eecs.umich.edu
2027178Sgblack@eecs.umich.edu    if (isLlsc) {
2037178Sgblack@eecs.umich.edu        req->setExtraData(success ? 1 : 0);
2047178Sgblack@eecs.umich.edu    }
2057178Sgblack@eecs.umich.edu
2067178Sgblack@eecs.umich.edu    return success;
2077178Sgblack@eecs.umich.edu}
2087178Sgblack@eecs.umich.edu
2097178Sgblack@eecs.umich.edu
210#if TRACING_ON
211
212#define CASE(A, T)                                                      \
213  case sizeof(T):                                                       \
214    DPRINTF(MemoryAccess, A " of size %i on address 0x%x data 0x%x\n",  \
215            pkt->getSize(), pkt->getAddr(), pkt->get<T>());             \
216  break
217
218
219#define TRACE_PACKET(A)                                                 \
220    do {                                                                \
221        switch (pkt->getSize()) {                                       \
222          CASE(A, uint64_t);                                            \
223          CASE(A, uint32_t);                                            \
224          CASE(A, uint16_t);                                            \
225          CASE(A, uint8_t);                                             \
226          default:                                                      \
227            DPRINTF(MemoryAccess, A " of size %i on address 0x%x\n",    \
228                    pkt->getSize(), pkt->getAddr());                    \
229        }                                                               \
230    } while (0)
231
232#else
233
234#define TRACE_PACKET(A)
235
236#endif
237
238Tick
239PhysicalMemory::doAtomicAccess(PacketPtr pkt)
240{
241    assert(pkt->getAddr() >= start() &&
242           pkt->getAddr() + pkt->getSize() <= start() + size());
243
244    if (pkt->memInhibitAsserted()) {
245        DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
246                pkt->getAddr());
247        return 0;
248    }
249
250    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
251
252    if (pkt->cmd == MemCmd::SwapReq) {
253        IntReg overwrite_val;
254        bool overwrite_mem;
255        uint64_t condition_val64;
256        uint32_t condition_val32;
257
258        if (!pmemAddr)
259            panic("Swap only works if there is real memory (i.e. null=False)");
260        assert(sizeof(IntReg) >= pkt->getSize());
261
262        overwrite_mem = true;
263        // keep a copy of our possible write value, and copy what is at the
264        // memory address into the packet
265        std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
266        std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
267
268        if (pkt->req->isCondSwap()) {
269            if (pkt->getSize() == sizeof(uint64_t)) {
270                condition_val64 = pkt->req->getExtraData();
271                overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
272                                             sizeof(uint64_t));
273            } else if (pkt->getSize() == sizeof(uint32_t)) {
274                condition_val32 = (uint32_t)pkt->req->getExtraData();
275                overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
276                                             sizeof(uint32_t));
277            } else
278                panic("Invalid size for conditional read/write\n");
279        }
280
281        if (overwrite_mem)
282            std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
283
284        TRACE_PACKET("Read/Write");
285    } else if (pkt->isRead()) {
286        assert(!pkt->isWrite());
287        if (pkt->isLlsc()) {
288            trackLoadLocked(pkt);
289        }
290        if (pmemAddr)
291            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
292        TRACE_PACKET("Read");
293    } else if (pkt->isWrite()) {
294        if (writeOK(pkt)) {
295            if (pmemAddr)
296                memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
297            TRACE_PACKET("Write");
298        }
299    } else if (pkt->isInvalidate()) {
300        //upgrade or invalidate
301        if (pkt->needsResponse()) {
302            pkt->makeAtomicResponse();
303        }
304    } else {
305        panic("unimplemented");
306    }
307
308    if (pkt->needsResponse()) {
309        pkt->makeAtomicResponse();
310    }
311    return calculateLatency(pkt);
312}
313
314
315void
316PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
317{
318    assert(pkt->getAddr() >= start() &&
319           pkt->getAddr() + pkt->getSize() <= start() + size());
320
321
322    uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
323
324    if (pkt->isRead()) {
325        if (pmemAddr)
326            memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
327        TRACE_PACKET("Read");
328        pkt->makeAtomicResponse();
329    } else if (pkt->isWrite()) {
330        if (pmemAddr)
331            memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
332        TRACE_PACKET("Write");
333        pkt->makeAtomicResponse();
334    } else if (pkt->isPrint()) {
335        Packet::PrintReqState *prs =
336            dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
337        // Need to call printLabels() explicitly since we're not going
338        // through printObj().
339        prs->printLabels();
340        // Right now we just print the single byte at the specified address.
341        ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
342    } else {
343        panic("PhysicalMemory: unimplemented functional command %s",
344              pkt->cmdString());
345    }
346}
347
348
349Port *
350PhysicalMemory::getPort(const std::string &if_name, int idx)
351{
352    // Accept request for "functional" port for backwards compatibility
353    // with places where this function is called from C++.  I'd prefer
354    // to move all these into Python someday.
355    if (if_name == "functional") {
356        return new MemoryPort(csprintf("%s-functional", name()), this);
357    }
358
359    if (if_name != "port") {
360        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
361    }
362
363    if (idx >= ports.size()) {
364        ports.resize(idx+1);
365    }
366
367    if (ports[idx] != NULL) {
368        panic("PhysicalMemory::getPort: port %d already assigned", idx);
369    }
370
371    MemoryPort *port =
372        new MemoryPort(csprintf("%s-port%d", name(), idx), this);
373
374    ports[idx] = port;
375    return port;
376}
377
378
379void
380PhysicalMemory::recvStatusChange(Port::Status status)
381{
382}
383
384PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
385                                       PhysicalMemory *_memory)
386    : SimpleTimingPort(_name, _memory), memory(_memory)
387{ }
388
389void
390PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
391{
392    memory->recvStatusChange(status);
393}
394
395void
396PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
397                                                   bool &snoop)
398{
399    memory->getAddressRanges(resp, snoop);
400}
401
402void
403PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
404{
405    snoop = false;
406    resp.clear();
407    resp.push_back(RangeSize(start(), params()->range.size()));
408}
409
410int
411PhysicalMemory::MemoryPort::deviceBlockSize()
412{
413    return memory->deviceBlockSize();
414}
415
416Tick
417PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
418{
419    return memory->doAtomicAccess(pkt);
420}
421
422void
423PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
424{
425    pkt->pushLabel(memory->name());
426
427    if (!checkFunctional(pkt)) {
428        // Default implementation of SimpleTimingPort::recvFunctional()
429        // calls recvAtomic() and throws away the latency; we can save a
430        // little here by just not calculating the latency.
431        memory->doFunctionalAccess(pkt);
432    }
433
434    pkt->popLabel();
435}
436
437unsigned int
438PhysicalMemory::drain(Event *de)
439{
440    int count = 0;
441    for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
442        count += (*pi)->drain(de);
443    }
444
445    if (count)
446        changeState(Draining);
447    else
448        changeState(Drained);
449    return count;
450}
451
452void
453PhysicalMemory::serialize(ostream &os)
454{
455    if (!pmemAddr)
456        return;
457
458    gzFile compressedMem;
459    string filename = name() + ".physmem";
460
461    SERIALIZE_SCALAR(filename);
462
463    // write memory file
464    string thefile = Checkpoint::dir() + "/" + filename.c_str();
465    int fd = creat(thefile.c_str(), 0664);
466    if (fd < 0) {
467        perror("creat");
468        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
469    }
470
471    compressedMem = gzdopen(fd, "wb");
472    if (compressedMem == NULL)
473        fatal("Insufficient memory to allocate compression state for %s\n",
474                filename);
475
476    if (gzwrite(compressedMem, pmemAddr, params()->range.size()) !=
477        params()->range.size()) {
478        fatal("Write failed on physical memory checkpoint file '%s'\n",
479              filename);
480    }
481
482    if (gzclose(compressedMem))
483        fatal("Close failed on physical memory checkpoint file '%s'\n",
484              filename);
485}
486
487void
488PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
489{
490    if (!pmemAddr)
491        return;
492
493    gzFile compressedMem;
494    long *tempPage;
495    long *pmem_current;
496    uint64_t curSize;
497    uint32_t bytesRead;
498    const int chunkSize = 16384;
499
500    string filename;
501
502    UNSERIALIZE_SCALAR(filename);
503
504    filename = cp->cptDir + "/" + filename;
505
506    // mmap memoryfile
507    int fd = open(filename.c_str(), O_RDONLY);
508    if (fd < 0) {
509        perror("open");
510        fatal("Can't open physical memory checkpoint file '%s'", filename);
511    }
512
513    compressedMem = gzdopen(fd, "rb");
514    if (compressedMem == NULL)
515        fatal("Insufficient memory to allocate compression state for %s\n",
516                filename);
517
518    // unmap file that was mmaped in the constructor
519    // This is done here to make sure that gzip and open don't muck with our
520    // nice large space of memory before we reallocate it
521    munmap((char*)pmemAddr, params()->range.size());
522
523    pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(),
524        PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
525
526    if (pmemAddr == (void *)MAP_FAILED) {
527        perror("mmap");
528        fatal("Could not mmap physical memory!\n");
529    }
530
531    curSize = 0;
532    tempPage = (long*)malloc(chunkSize);
533    if (tempPage == NULL)
534        fatal("Unable to malloc memory to read file %s\n", filename);
535
536    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
537    while (curSize < params()->range.size()) {
538        bytesRead = gzread(compressedMem, tempPage, chunkSize);
539        if (bytesRead != chunkSize &&
540            bytesRead != params()->range.size() - curSize)
541            fatal("Read failed on physical memory checkpoint file '%s'"
542                  " got %d bytes, expected %d or %d bytes\n",
543                  filename, bytesRead, chunkSize,
544                  params()->range.size() - curSize);
545
546        assert(bytesRead % sizeof(long) == 0);
547
548        for (int x = 0; x < bytesRead/sizeof(long); x++)
549        {
550             if (*(tempPage+x) != 0) {
551                 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
552                 *pmem_current = *(tempPage+x);
553             }
554        }
555        curSize += bytesRead;
556    }
557
558    free(tempPage);
559
560    if (gzclose(compressedMem))
561        fatal("Close failed on physical memory checkpoint file '%s'\n",
562              filename);
563
564}
565
566PhysicalMemory *
567PhysicalMemoryParams::create()
568{
569    return new PhysicalMemory(this);
570}
571