physical.cc revision 2519
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 std::string &if_name) 156{ 157 if (if_name == "") { 158 if (port != NULL) 159 panic("PhysicalMemory::getPort: additional port requested to memory!"); 160 port = new MemoryPort(this); 161 return port; 162 } else if (if_name == "functional") { 163 /* special port for functional writes at startup. */ 164 return new MemoryPort(this); 165 } else { 166 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 167 } 168} 169 170void 171PhysicalMemory::recvStatusChange(Port::Status status) 172{ 173 panic("??"); 174} 175 176PhysicalMemory::MemoryPort::MemoryPort(PhysicalMemory *_memory) 177 : memory(_memory) 178{ } 179 180void 181PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 182{ 183 memory->recvStatusChange(status); 184} 185 186void 187PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &range_list, 188 bool &owner) 189{ 190 memory->getAddressRanges(range_list, owner); 191} 192 193void 194PhysicalMemory::getAddressRanges(AddrRangeList &range_list, bool &owner) 195{ 196 owner = true; 197 range_list.clear(); 198 range_list.push_back(RangeSize(base_addr, pmem_size)); 199} 200 201int 202PhysicalMemory::MemoryPort::deviceBlockSize() 203{ 204 return memory->deviceBlockSize(); 205} 206 207bool 208PhysicalMemory::MemoryPort::recvTiming(Packet &pkt) 209{ 210 return memory->doTimingAccess(pkt, this); 211} 212 213Tick 214PhysicalMemory::MemoryPort::recvAtomic(Packet &pkt) 215{ 216 return memory->doAtomicAccess(pkt); 217} 218 219void 220PhysicalMemory::MemoryPort::recvFunctional(Packet &pkt) 221{ 222 memory->doFunctionalAccess(pkt); 223} 224 225 226 227void 228PhysicalMemory::serialize(ostream &os) 229{ 230 gzFile compressedMem; 231 string filename = name() + ".physmem"; 232 233 SERIALIZE_SCALAR(pmem_size); 234 SERIALIZE_SCALAR(filename); 235 236 // write memory file 237 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 238 int fd = creat(thefile.c_str(), 0664); 239 if (fd < 0) { 240 perror("creat"); 241 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 242 } 243 244 compressedMem = gzdopen(fd, "wb"); 245 if (compressedMem == NULL) 246 fatal("Insufficient memory to allocate compression state for %s\n", 247 filename); 248 249 if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) { 250 fatal("Write failed on physical memory checkpoint file '%s'\n", 251 filename); 252 } 253 254 if (gzclose(compressedMem)) 255 fatal("Close failed on physical memory checkpoint file '%s'\n", 256 filename); 257} 258 259void 260PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 261{ 262 gzFile compressedMem; 263 long *tempPage; 264 long *pmem_current; 265 uint64_t curSize; 266 uint32_t bytesRead; 267 const int chunkSize = 16384; 268 269 270 // unmap file that was mmaped in the constructor 271 munmap(pmem_addr, pmem_size); 272 273 string filename; 274 275 UNSERIALIZE_SCALAR(pmem_size); 276 UNSERIALIZE_SCALAR(filename); 277 278 filename = cp->cptDir + "/" + filename; 279 280 // mmap memoryfile 281 int fd = open(filename.c_str(), O_RDONLY); 282 if (fd < 0) { 283 perror("open"); 284 fatal("Can't open physical memory checkpoint file '%s'", filename); 285 } 286 287 compressedMem = gzdopen(fd, "rb"); 288 if (compressedMem == NULL) 289 fatal("Insufficient memory to allocate compression state for %s\n", 290 filename); 291 292 293 pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE, 294 MAP_ANON | MAP_PRIVATE, -1, 0); 295 296 if (pmem_addr == (void *)MAP_FAILED) { 297 perror("mmap"); 298 fatal("Could not mmap physical memory!\n"); 299 } 300 301 curSize = 0; 302 tempPage = (long*)malloc(chunkSize); 303 if (tempPage == NULL) 304 fatal("Unable to malloc memory to read file %s\n", filename); 305 306 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 307 while (curSize < pmem_size) { 308 bytesRead = gzread(compressedMem, tempPage, chunkSize); 309 if (bytesRead != chunkSize && bytesRead != pmem_size - curSize) 310 fatal("Read failed on physical memory checkpoint file '%s'" 311 " got %d bytes, expected %d or %d bytes\n", 312 filename, bytesRead, chunkSize, pmem_size-curSize); 313 314 assert(bytesRead % sizeof(long) == 0); 315 316 for (int x = 0; x < bytesRead/sizeof(long); x++) 317 { 318 if (*(tempPage+x) != 0) { 319 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long)); 320 *pmem_current = *(tempPage+x); 321 } 322 } 323 curSize += bytesRead; 324 } 325 326 free(tempPage); 327 328 if (gzclose(compressedMem)) 329 fatal("Close failed on physical memory checkpoint file '%s'\n", 330 filename); 331 332} 333 334 335BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 336 337 Param<string> file; 338 Param<Range<Addr> > range; 339 340END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 341 342BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 343 344 INIT_PARAM_DFLT(file, "memory mapped file", ""), 345 INIT_PARAM(range, "Device Address Range") 346 347END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 348 349CREATE_SIM_OBJECT(PhysicalMemory) 350{ 351 352 return new PhysicalMemory(getInstanceName()); 353} 354 355REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 356