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