abstract_mem.cc revision 2414
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#if FULL_SYSTEM
44#include "mem/functional/memory_control.hh"
45#endif
46#include "mem/physical.hh"
47#include "sim/host.hh"
48#include "sim/builder.hh"
49#include "targetarch/isa_traits.hh"
50
51
52using namespace std;
53
54#if FULL_SYSTEM
55PhysicalMemory::PhysicalMemory(const string &n, Range<Addr> range,
56                               MemoryController *mmu, const std::string &fname)
57    : Memory(n), base_addr(range.start), pmem_size(range.size()),
58      pmem_addr(NULL)
59{
60    if (pmem_size % TheISA::PageBytes != 0)
61        panic("Memory Size not divisible by page size\n");
62
63    mmu->add_child(this, range);
64
65    int fd = -1;
66
67    if (!fname.empty()) {
68        fd = open(fname.c_str(), O_RDWR | O_CREAT, 0644);
69        if (fd == -1) {
70            perror("open");
71            fatal("Could not open physical memory file: %s\n", fname);
72        }
73        ftruncate(fd, pmem_size);
74    }
75
76    int map_flags = (fd == -1) ? (MAP_ANON | MAP_PRIVATE) : MAP_SHARED;
77    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
78                                map_flags, fd, 0);
79
80    if (fd != -1)
81        close(fd);
82
83    if (pmem_addr == (void *)MAP_FAILED) {
84        perror("mmap");
85        fatal("Could not mmap!\n");
86    }
87
88    page_ptr = 0;
89}
90#endif
91
92PhysicalMemory::PhysicalMemory(const string &n)
93    : Memory(n), memoryPort(this), base_addr(0), pmem_addr(NULL)
94{
95    // Hardcoded to 128 MB for now.
96    pmem_size = 1 << 27;
97
98    if (pmem_size % TheISA::PageBytes != 0)
99        panic("Memory Size not divisible by page size\n");
100
101    int map_flags = MAP_ANON | MAP_PRIVATE;
102    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
103                                map_flags, -1, 0);
104
105    if (pmem_addr == (void *)MAP_FAILED) {
106        perror("mmap");
107        fatal("Could not mmap!\n");
108    }
109
110    page_ptr = 0;
111}
112
113PhysicalMemory::~PhysicalMemory()
114{
115    if (pmem_addr)
116        munmap(pmem_addr, pmem_size);
117}
118
119Addr
120PhysicalMemory::new_page()
121{
122    Addr return_addr = page_ptr << LogVMPageSize;
123    return_addr += base_addr;
124
125    ++page_ptr;
126    return return_addr;
127}
128
129//
130// little helper for better prot_* error messages
131//
132void
133PhysicalMemory::prot_access_error(Addr addr, int size, Command func)
134{
135    panic("invalid physical memory access!\n"
136          "%s: %i(addr=%#x, size=%d) out of range (max=%#x)\n",
137          name(), func, addr, size, pmem_size - 1);
138}
139
140void
141PhysicalMemory::prot_memset(Addr addr, uint8_t val, int size)
142{
143    if (addr + size >= pmem_size)
144        prot_access_error(addr, size, Write);
145
146    memset(pmem_addr + addr - base_addr, val, size);
147}
148
149int
150PhysicalMemory::deviceBlockSize()
151{
152    //Can accept anysize request
153    return 0;
154}
155
156bool
157PhysicalMemory::doTimingAccess (Packet &pkt)
158{
159    doFunctionalAccess(pkt);
160    //Schedule a response event at curTick + lat;
161    return true;
162}
163
164Tick
165PhysicalMemory::doAtomicAccess(Packet &pkt)
166{
167    doFunctionalAccess(pkt);
168    return curTick + lat;
169}
170
171void
172PhysicalMemory::doFunctionalAccess(Packet &pkt)
173{
174    if (pkt.addr + pkt.size >= pmem_size)
175            prot_access_error(pkt.addr, pkt.size, pkt.cmd);
176
177    switch (pkt.cmd) {
178      case Read:
179        memcpy(pkt.data, pmem_addr + pkt.addr - base_addr, pkt.size);
180      case Write:
181        memcpy(pmem_addr + pkt.addr - base_addr, pkt.data, pkt.size);
182      default:
183        panic("unimplemented");
184    }
185}
186
187Port *
188PhysicalMemory::getPort(const char *if_name)
189{
190    return &memoryPort;
191}
192
193void
194PhysicalMemory::recvStatusChange(Port::Status status)
195{
196    panic("??");
197}
198
199PhysicalMemory::MemoryPort::MemoryPort(PhysicalMemory *_memory)
200    : memory(_memory)
201{ }
202
203void
204PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
205{
206    memory->recvStatusChange(status);
207}
208
209void
210PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &range_list,
211                                           bool &owner)
212{
213    panic("??");
214}
215
216
217bool
218PhysicalMemory::MemoryPort::recvTiming(Packet &pkt)
219{
220    return memory->doTimingAccess(pkt);
221}
222
223Tick
224PhysicalMemory::MemoryPort::recvAtomic(Packet &pkt)
225{
226    return memory->doAtomicAccess(pkt);
227}
228
229void
230PhysicalMemory::MemoryPort::recvFunctional(Packet &pkt)
231{
232    memory->doFunctionalAccess(pkt);
233}
234
235
236
237void
238PhysicalMemory::serialize(ostream &os)
239{
240    gzFile compressedMem;
241    string filename = name() + ".physmem";
242
243    SERIALIZE_SCALAR(pmem_size);
244    SERIALIZE_SCALAR(filename);
245
246    // write memory file
247    string thefile = Checkpoint::dir() + "/" + filename.c_str();
248    int fd = creat(thefile.c_str(), 0664);
249    if (fd < 0) {
250        perror("creat");
251        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
252    }
253
254    compressedMem = gzdopen(fd, "wb");
255    if (compressedMem == NULL)
256        fatal("Insufficient memory to allocate compression state for %s\n",
257                filename);
258
259    if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) {
260        fatal("Write failed on physical memory checkpoint file '%s'\n",
261              filename);
262    }
263
264    if (gzclose(compressedMem))
265        fatal("Close failed on physical memory checkpoint file '%s'\n",
266              filename);
267}
268
269void
270PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
271{
272    gzFile compressedMem;
273    long *tempPage;
274    long *pmem_current;
275    uint64_t curSize;
276    uint32_t bytesRead;
277    const int chunkSize = 16384;
278
279
280    // unmap file that was mmaped in the constructor
281    munmap(pmem_addr, pmem_size);
282
283    string filename;
284
285    UNSERIALIZE_SCALAR(pmem_size);
286    UNSERIALIZE_SCALAR(filename);
287
288    filename = cp->cptDir + "/" + filename;
289
290    // mmap memoryfile
291    int fd = open(filename.c_str(), O_RDONLY);
292    if (fd < 0) {
293        perror("open");
294        fatal("Can't open physical memory checkpoint file '%s'", filename);
295    }
296
297    compressedMem = gzdopen(fd, "rb");
298    if (compressedMem == NULL)
299        fatal("Insufficient memory to allocate compression state for %s\n",
300                filename);
301
302
303    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
304                                MAP_ANON | MAP_PRIVATE, -1, 0);
305
306    if (pmem_addr == (void *)MAP_FAILED) {
307        perror("mmap");
308        fatal("Could not mmap physical memory!\n");
309    }
310
311    curSize = 0;
312    tempPage = (long*)malloc(chunkSize);
313    if (tempPage == NULL)
314        fatal("Unable to malloc memory to read file %s\n", filename);
315
316    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
317    while (curSize < pmem_size) {
318        bytesRead = gzread(compressedMem, tempPage, chunkSize);
319        if (bytesRead != chunkSize && bytesRead != pmem_size - curSize)
320            fatal("Read failed on physical memory checkpoint file '%s'"
321                  " got %d bytes, expected %d or %d bytes\n",
322                  filename, bytesRead, chunkSize, pmem_size-curSize);
323
324        assert(bytesRead % sizeof(long) == 0);
325
326        for (int x = 0; x < bytesRead/sizeof(long); x++)
327        {
328             if (*(tempPage+x) != 0) {
329                 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long));
330                 *pmem_current = *(tempPage+x);
331             }
332        }
333        curSize += bytesRead;
334    }
335
336    free(tempPage);
337
338    if (gzclose(compressedMem))
339        fatal("Close failed on physical memory checkpoint file '%s'\n",
340              filename);
341
342}
343
344
345BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
346
347    Param<string> file;
348#if FULL_SYSTEM
349    SimObjectParam<MemoryController *> mmu;
350#endif
351    Param<Range<Addr> > range;
352
353END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
354
355BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
356
357    INIT_PARAM_DFLT(file, "memory mapped file", ""),
358#if FULL_SYSTEM
359    INIT_PARAM(mmu, "Memory Controller"),
360#endif
361    INIT_PARAM(range, "Device Address Range")
362
363END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
364
365CREATE_SIM_OBJECT(PhysicalMemory)
366{
367#if FULL_SYSTEM
368    if (mmu) {
369        return new PhysicalMemory(getInstanceName(), range, mmu, file);
370    }
371#endif
372
373    return new PhysicalMemory(getInstanceName());
374}
375
376REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
377