physical.cc revision 2408
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    : FunctionalMemory(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), 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, const string &func)
134{
135    panic("invalid physical memory access!\n"
136          "%s: %s(addr=%#x, size=%d) out of range (max=%#x)\n",
137          name(), func, addr, size, pmem_size - 1);
138}
139
140void
141PhysicalMemory::prot_read(Addr addr, uint8_t *p, int size)
142{
143    if (addr + size >= pmem_size)
144        prot_access_error(addr, size, "prot_read");
145
146    memcpy(p, pmem_addr + addr - base_addr, size);
147}
148
149void
150PhysicalMemory::prot_write(Addr addr, const uint8_t *p, int size)
151{
152    if (addr + size >= pmem_size)
153        prot_access_error(addr, size, "prot_write");
154
155    memcpy(pmem_addr + addr - base_addr, p, size);
156}
157
158void
159PhysicalMemory::prot_memset(Addr addr, uint8_t val, int size)
160{
161    if (addr + size >= pmem_size)
162        prot_access_error(addr, size, "prot_memset");
163
164    memset(pmem_addr + addr - base_addr, val, size);
165}
166
167int
168PhysicalMemory::deviceBlockSize()
169{
170    //For now the largest accesses we can take are Page Sized
171    return VMPageSize;
172}
173
174void
175PhysicalMemory::serialize(ostream &os)
176{
177    gzFile compressedMem;
178    string filename = name() + ".physmem";
179
180    SERIALIZE_SCALAR(pmem_size);
181    SERIALIZE_SCALAR(filename);
182
183    // write memory file
184    string thefile = Checkpoint::dir() + "/" + filename.c_str();
185    int fd = creat(thefile.c_str(), 0664);
186    if (fd < 0) {
187        perror("creat");
188        fatal("Can't open physical memory checkpoint file '%s'\n", filename);
189    }
190
191    compressedMem = gzdopen(fd, "wb");
192    if (compressedMem == NULL)
193        fatal("Insufficient memory to allocate compression state for %s\n",
194                filename);
195
196    if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) {
197        fatal("Write failed on physical memory checkpoint file '%s'\n",
198              filename);
199    }
200
201    if (gzclose(compressedMem))
202        fatal("Close failed on physical memory checkpoint file '%s'\n",
203              filename);
204}
205
206void
207PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
208{
209    gzFile compressedMem;
210    long *tempPage;
211    long *pmem_current;
212    uint64_t curSize;
213    uint32_t bytesRead;
214    const int chunkSize = 16384;
215
216
217    // unmap file that was mmaped in the constructor
218    munmap(pmem_addr, pmem_size);
219
220    string filename;
221
222    UNSERIALIZE_SCALAR(pmem_size);
223    UNSERIALIZE_SCALAR(filename);
224
225    filename = cp->cptDir + "/" + filename;
226
227    // mmap memoryfile
228    int fd = open(filename.c_str(), O_RDONLY);
229    if (fd < 0) {
230        perror("open");
231        fatal("Can't open physical memory checkpoint file '%s'", filename);
232    }
233
234    compressedMem = gzdopen(fd, "rb");
235    if (compressedMem == NULL)
236        fatal("Insufficient memory to allocate compression state for %s\n",
237                filename);
238
239
240    pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
241                                MAP_ANON | MAP_PRIVATE, -1, 0);
242
243    if (pmem_addr == (void *)MAP_FAILED) {
244        perror("mmap");
245        fatal("Could not mmap physical memory!\n");
246    }
247
248    curSize = 0;
249    tempPage = (long*)malloc(chunkSize);
250    if (tempPage == NULL)
251        fatal("Unable to malloc memory to read file %s\n", filename);
252
253    /* Only copy bytes that are non-zero, so we don't give the VM system hell */
254    while (curSize < pmem_size) {
255        bytesRead = gzread(compressedMem, tempPage, chunkSize);
256        if (bytesRead != chunkSize && bytesRead != pmem_size - curSize)
257            fatal("Read failed on physical memory checkpoint file '%s'"
258                  " got %d bytes, expected %d or %d bytes\n",
259                  filename, bytesRead, chunkSize, pmem_size-curSize);
260
261        assert(bytesRead % sizeof(long) == 0);
262
263        for (int x = 0; x < bytesRead/sizeof(long); x++)
264        {
265             if (*(tempPage+x) != 0) {
266                 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long));
267                 *pmem_current = *(tempPage+x);
268             }
269        }
270        curSize += bytesRead;
271    }
272
273    free(tempPage);
274
275    if (gzclose(compressedMem))
276        fatal("Close failed on physical memory checkpoint file '%s'\n",
277              filename);
278
279}
280
281BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
282
283    Param<string> file;
284#if FULL_SYSTEM
285    SimObjectParam<MemoryController *> mmu;
286#endif
287    Param<Range<Addr> > range;
288
289END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory)
290
291BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
292
293    INIT_PARAM_DFLT(file, "memory mapped file", ""),
294#if FULL_SYSTEM
295    INIT_PARAM(mmu, "Memory Controller"),
296#endif
297    INIT_PARAM(range, "Device Address Range")
298
299END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
300
301CREATE_SIM_OBJECT(PhysicalMemory)
302{
303#if FULL_SYSTEM
304    if (mmu) {
305        return new PhysicalMemory(getInstanceName(), range, mmu, file);
306    }
307#endif
308
309    return new PhysicalMemory(getInstanceName());
310}
311
312REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)
313