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