physical.cc revision 2462
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) 73 : MemObject(n), 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 93PhysicalMemory::~PhysicalMemory() 94{ 95 if (pmem_addr) 96 munmap(pmem_addr, pmem_size); 97 //Remove memPorts? 98} 99 100Addr 101PhysicalMemory::new_page() 102{ 103 Addr return_addr = page_ptr << LogVMPageSize; 104 return_addr += base_addr; 105 106 ++page_ptr; 107 return return_addr; 108} 109 110int 111PhysicalMemory::deviceBlockSize() 112{ 113 //Can accept anysize request 114 return 0; 115} 116 117bool 118PhysicalMemory::doTimingAccess (Packet &pkt, MemoryPort* memoryPort) 119{ 120 doFunctionalAccess(pkt); 121 122 MemResponseEvent* response = new MemResponseEvent(pkt, memoryPort); 123 response->schedule(curTick + lat); 124 125 return true; 126} 127 128Tick 129PhysicalMemory::doAtomicAccess(Packet &pkt) 130{ 131 doFunctionalAccess(pkt); 132 return curTick + lat; 133} 134 135void 136PhysicalMemory::doFunctionalAccess(Packet &pkt) 137{ 138 assert(pkt.addr + pkt.size < pmem_size); 139 140 switch (pkt.cmd) { 141 case Read: 142 memcpy(pkt.data, pmem_addr + pkt.addr - base_addr, pkt.size); 143 break; 144 case Write: 145 memcpy(pmem_addr + pkt.addr - base_addr, pkt.data, pkt.size); 146 break; 147 default: 148 panic("unimplemented"); 149 } 150 151 pkt.result = Success; 152} 153 154Port * 155PhysicalMemory::getPort(const char *if_name) 156{ 157 if (if_name == NULL) { 158 return new MemoryPort(this); 159 } else { 160 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 161 } 162} 163 164void 165PhysicalMemory::recvStatusChange(Port::Status status) 166{ 167 panic("??"); 168} 169 170PhysicalMemory::MemoryPort::MemoryPort(PhysicalMemory *_memory) 171 : memory(_memory) 172{ } 173 174void 175PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 176{ 177 memory->recvStatusChange(status); 178} 179 180void 181PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &range_list, 182 bool &owner) 183{ 184 panic("??"); 185} 186 187int 188PhysicalMemory::MemoryPort::deviceBlockSize() 189{ 190 return memory->deviceBlockSize(); 191} 192 193bool 194PhysicalMemory::MemoryPort::recvTiming(Packet &pkt) 195{ 196 return memory->doTimingAccess(pkt, this); 197} 198 199Tick 200PhysicalMemory::MemoryPort::recvAtomic(Packet &pkt) 201{ 202 return memory->doAtomicAccess(pkt); 203} 204 205void 206PhysicalMemory::MemoryPort::recvFunctional(Packet &pkt) 207{ 208 memory->doFunctionalAccess(pkt); 209} 210 211 212 213void 214PhysicalMemory::serialize(ostream &os) 215{ 216 gzFile compressedMem; 217 string filename = name() + ".physmem"; 218 219 SERIALIZE_SCALAR(pmem_size); 220 SERIALIZE_SCALAR(filename); 221 222 // write memory file 223 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 224 int fd = creat(thefile.c_str(), 0664); 225 if (fd < 0) { 226 perror("creat"); 227 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 228 } 229 230 compressedMem = gzdopen(fd, "wb"); 231 if (compressedMem == NULL) 232 fatal("Insufficient memory to allocate compression state for %s\n", 233 filename); 234 235 if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) { 236 fatal("Write failed on physical memory checkpoint file '%s'\n", 237 filename); 238 } 239 240 if (gzclose(compressedMem)) 241 fatal("Close failed on physical memory checkpoint file '%s'\n", 242 filename); 243} 244 245void 246PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 247{ 248 gzFile compressedMem; 249 long *tempPage; 250 long *pmem_current; 251 uint64_t curSize; 252 uint32_t bytesRead; 253 const int chunkSize = 16384; 254 255 256 // unmap file that was mmaped in the constructor 257 munmap(pmem_addr, pmem_size); 258 259 string filename; 260 261 UNSERIALIZE_SCALAR(pmem_size); 262 UNSERIALIZE_SCALAR(filename); 263 264 filename = cp->cptDir + "/" + filename; 265 266 // mmap memoryfile 267 int fd = open(filename.c_str(), O_RDONLY); 268 if (fd < 0) { 269 perror("open"); 270 fatal("Can't open physical memory checkpoint file '%s'", filename); 271 } 272 273 compressedMem = gzdopen(fd, "rb"); 274 if (compressedMem == NULL) 275 fatal("Insufficient memory to allocate compression state for %s\n", 276 filename); 277 278 279 pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 280 MAP_ANON | MAP_PRIVATE, -1, 0); 281 282 if (pmem_addr == (void *)MAP_FAILED) { 283 perror("mmap"); 284 fatal("Could not mmap physical memory!\n"); 285 } 286 287 curSize = 0; 288 tempPage = (long*)malloc(chunkSize); 289 if (tempPage == NULL) 290 fatal("Unable to malloc memory to read file %s\n", filename); 291 292 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 293 while (curSize < pmem_size) { 294 bytesRead = gzread(compressedMem, tempPage, chunkSize); 295 if (bytesRead != chunkSize && bytesRead != pmem_size - curSize) 296 fatal("Read failed on physical memory checkpoint file '%s'" 297 " got %d bytes, expected %d or %d bytes\n", 298 filename, bytesRead, chunkSize, pmem_size-curSize); 299 300 assert(bytesRead % sizeof(long) == 0); 301 302 for (int x = 0; x < bytesRead/sizeof(long); x++) 303 { 304 if (*(tempPage+x) != 0) { 305 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long)); 306 *pmem_current = *(tempPage+x); 307 } 308 } 309 curSize += bytesRead; 310 } 311 312 free(tempPage); 313 314 if (gzclose(compressedMem)) 315 fatal("Close failed on physical memory checkpoint file '%s'\n", 316 filename); 317 318} 319 320 321BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 322 323 Param<string> file; 324#if FULL_SYSTEM 325 SimObjectParam<MemoryController *> mmu; 326#endif 327 Param<Range<Addr> > range; 328 329END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 330 331BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 332 333 INIT_PARAM_DFLT(file, "memory mapped file", ""), 334#if FULL_SYSTEM 335 INIT_PARAM(mmu, "Memory Controller"), 336#endif 337 INIT_PARAM(range, "Device Address Range") 338 339END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 340 341CREATE_SIM_OBJECT(PhysicalMemory) 342{ 343#if FULL_SYSTEM 344 if (mmu) { 345 return new PhysicalMemory(getInstanceName(), range, mmu, file); 346 } 347#endif 348 349 return new PhysicalMemory(getInstanceName()); 350} 351 352REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 353