physical.cc revision 2497
1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/types.h>
30#include <sys/mman.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <zlib.h>
35
36#include <cstdio>
37#include <iostream>
38#include <string>
39
40
41#include "base/misc.hh"
42#include "config/full_system.hh"
43#include "mem/physical.hh"
44#include "sim/host.hh"
45#include "sim/builder.hh"
46#include "sim/eventq.hh"
47#include "arch/isa_traits.hh"
48
49
50using namespace std;
51using namespace TheISA;
52
53PhysicalMemory::MemResponseEvent::MemResponseEvent(Packet &pkt, MemoryPort* _m)
54    : Event(&mainEventQueue, CPU_Tick_Pri), pkt(pkt), memoryPort(_m)
55{
56
57    this->setFlags(AutoDelete);
58}
59
60void
61PhysicalMemory::MemResponseEvent::process()
62{
63    memoryPort->sendTiming(pkt);
64}
65
66const char *
67PhysicalMemory::MemResponseEvent::description()
68{
69    return "Physical Memory Timing Access respnse event";
70}
71
72PhysicalMemory::PhysicalMemory(const string &n, MemObject *bus)
73    : MemObject(n), memPort(this), base_addr(0), pmem_addr(NULL)
74{
75    // Hardcoded to 128 MB for now.
76    pmem_size = 1 << 27;
77
78    if (pmem_size % TheISA::PageBytes != 0)
79        panic("Memory Size not divisible by page size\n");
80
81    int map_flags = MAP_ANON | MAP_PRIVATE;
82    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
83                                map_flags, -1, 0);
84
85    if (pmem_addr == (void *)MAP_FAILED) {
86        perror("mmap");
87        fatal("Could not mmap!\n");
88    }
89
90    page_ptr = 0;
91
92    Port *peer_port;
93    peer_port = bus->getPort();
94    memPort.setPeer(peer_port);
95    peer_port->setPeer(&memPort);
96
97
98
99}
100
101PhysicalMemory::~PhysicalMemory()
102{
103    if (pmem_addr)
104        munmap(pmem_addr, pmem_size);
105    //Remove memPorts?
106}
107
108Addr
109PhysicalMemory::new_page()
110{
111    Addr return_addr = page_ptr << LogVMPageSize;
112    return_addr += base_addr;
113
114    ++page_ptr;
115    return return_addr;
116}
117
118int
119PhysicalMemory::deviceBlockSize()
120{
121    //Can accept anysize request
122    return 0;
123}
124
125bool
126PhysicalMemory::doTimingAccess (Packet &pkt, MemoryPort* memoryPort)
127{
128    doFunctionalAccess(pkt);
129
130    MemResponseEvent* response = new MemResponseEvent(pkt, memoryPort);
131    response->schedule(curTick + lat);
132
133    return true;
134}
135
136Tick
137PhysicalMemory::doAtomicAccess(Packet &pkt)
138{
139    doFunctionalAccess(pkt);
140    return curTick + lat;
141}
142
143void
144PhysicalMemory::doFunctionalAccess(Packet &pkt)
145{
146    assert(pkt.addr + pkt.size < pmem_size);
147
148    switch (pkt.cmd) {
149      case Read:
150        memcpy(pkt.data, pmem_addr + pkt.addr - base_addr, pkt.size);
151        break;
152      case Write:
153        memcpy(pmem_addr + pkt.addr - base_addr, pkt.data, pkt.size);
154        break;
155      default:
156        panic("unimplemented");
157    }
158
159    pkt.result = Success;
160}
161
162Port *
163PhysicalMemory::getPort(const char *if_name)
164{
165    if (if_name == NULL) {
166        return new MemoryPort(this);
167    } else {
168        panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
169    }
170}
171
172void
173PhysicalMemory::recvStatusChange(Port::Status status)
174{
175    panic("??");
176}
177
178PhysicalMemory::MemoryPort::MemoryPort(PhysicalMemory *_memory)
179    : memory(_memory)
180{ }
181
182void
183PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
184{
185    memory->recvStatusChange(status);
186}
187
188void
189PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &range_list,
190                                           bool &owner)
191{
192    memory->getAddressRanges(range_list, owner);
193}
194
195void
196PhysicalMemory::getAddressRanges(AddrRangeList &range_list, bool &owner)
197{
198    owner = true;
199    range_list.clear();
200    range_list.push_back(RangeSize(base_addr, pmem_size));
201}
202
203int
204PhysicalMemory::MemoryPort::deviceBlockSize()
205{
206    return memory->deviceBlockSize();
207}
208
209bool
210PhysicalMemory::MemoryPort::recvTiming(Packet &pkt)
211{
212    return memory->doTimingAccess(pkt, this);
213}
214
215Tick
216PhysicalMemory::MemoryPort::recvAtomic(Packet &pkt)
217{
218    return memory->doAtomicAccess(pkt);
219}
220
221void
222PhysicalMemory::MemoryPort::recvFunctional(Packet &pkt)
223{
224    memory->doFunctionalAccess(pkt);
225}
226
227
228
229void
230PhysicalMemory::serialize(ostream &os)
231{
232    gzFile compressedMem;
233    string filename = name() + ".physmem";
234
235    SERIALIZE_SCALAR(pmem_size);
236    SERIALIZE_SCALAR(filename);
237
238    // write memory file
239    string thefile = Checkpoint::dir() + "/" + filename.c_str();
240    int fd = creat(thefile.c_str(), 0664);
241    if (fd < 0) {
242        perror("creat");
243        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
244    }
245
246    compressedMem = gzdopen(fd, "wb");
247    if (compressedMem == NULL)
248        fatal("Insufficient memory to allocate compression state for %s\n",
249                filename);
250
251    if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) {
252        fatal("Write failed on physical memory checkpoint file '%s'\n",
253              filename);
254    }
255
256    if (gzclose(compressedMem))
257        fatal("Close failed on physical memory checkpoint file '%s'\n",
258              filename);
259}
260
261void
262PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
263{
264    gzFile compressedMem;
265    long *tempPage;
266    long *pmem_current;
267    uint64_t curSize;
268    uint32_t bytesRead;
269    const int chunkSize = 16384;
270
271
272    // unmap file that was mmaped in the constructor
273    munmap(pmem_addr, pmem_size);
274
275    string filename;
276
277    UNSERIALIZE_SCALAR(pmem_size);
278    UNSERIALIZE_SCALAR(filename);
279
280    filename = cp->cptDir + "/" + filename;
281
282    // mmap memoryfile
283    int fd = open(filename.c_str(), O_RDONLY);
284    if (fd < 0) {
285        perror("open");
286        fatal("Can't open physical memory checkpoint file '%s'", filename);
287    }
288
289    compressedMem = gzdopen(fd, "rb");
290    if (compressedMem == NULL)
291        fatal("Insufficient memory to allocate compression state for %s\n",
292                filename);
293
294
295    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
296                                MAP_ANON | MAP_PRIVATE, -1, 0);
297
298    if (pmem_addr == (void *)MAP_FAILED) {
299        perror("mmap");
300        fatal("Could not mmap physical memory!\n");
301    }
302
303    curSize = 0;
304    tempPage = (long*)malloc(chunkSize);
305    if (tempPage == NULL)
306        fatal("Unable to malloc memory to read file %s\n", filename);
307
308    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
309    while (curSize < pmem_size) {
310        bytesRead = gzread(compressedMem, tempPage, chunkSize);
311        if (bytesRead != chunkSize && bytesRead != pmem_size - curSize)
312            fatal("Read failed on physical memory checkpoint file '%s'"
313                  " got %d bytes, expected %d or %d bytes\n",
314                  filename, bytesRead, chunkSize, pmem_size-curSize);
315
316        assert(bytesRead % sizeof(long) == 0);
317
318        for (int x = 0; x < bytesRead/sizeof(long); x++)
319        {
320             if (*(tempPage+x) != 0) {
321                 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long));
322                 *pmem_current = *(tempPage+x);
323             }
324        }
325        curSize += bytesRead;
326    }
327
328    free(tempPage);
329
330    if (gzclose(compressedMem))
331        fatal("Close failed on physical memory checkpoint file '%s'\n",
332              filename);
333
334}
335
336
337BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
338
339    Param<string> file;
340#if FULL_SYSTEM
341    SimObjectParam<MemoryController *> mmu;
342#endif
343    Param<Range<Addr> > range;
344    SimObjectParam<MemObject*> bus;
345
346END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
347
348BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
349
350    INIT_PARAM_DFLT(file, "memory mapped file", ""),
351#if FULL_SYSTEM
352    INIT_PARAM(mmu, "Memory Controller"),
353#endif
354    INIT_PARAM(range, "Device Address Range"),
355    INIT_PARAM(bus, "bus object memory connects to")
356
357END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
358
359CREATE_SIM_OBJECT(PhysicalMemory)
360{
361#if FULL_SYSTEM
362    if (mmu) {
363        return new PhysicalMemory(getInstanceName(), range, mmu, file);
364    }
365#endif
366
367    return new PhysicalMemory(getInstanceName(), bus);
368}
369
370REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
371