abstract_mem.cc revision 3012
12SN/A/* 21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski 292665Ssaidi@eecs.umich.edu * Ali Saidi 302SN/A */ 312SN/A 322SN/A#include <sys/types.h> 332SN/A#include <sys/mman.h> 342SN/A#include <errno.h> 352SN/A#include <fcntl.h> 365882Snate@binkert.org#include <unistd.h> 371492SN/A#include <zlib.h> 381717SN/A 398229Snate@binkert.org#include <iostream> 402680Sktlim@umich.edu#include <string> 418232Snate@binkert.org 424167Sbinkertn@umich.edu 432190SN/A#include "base/misc.hh" 442SN/A#include "config/full_system.hh" 452SN/A#include "mem/packet_impl.hh" 462SN/A#include "mem/physical.hh" 472SN/A#include "sim/host.hh" 482SN/A#include "sim/builder.hh" 492SN/A#include "sim/eventq.hh" 502SN/A#include "arch/isa_traits.hh" 512SN/A 522SN/A 532SN/Ausing namespace std; 542SN/Ausing namespace TheISA; 552SN/A 562SN/A 572SN/APhysicalMemory::PhysicalMemory(Params *p) 588991SAli.Saidi@ARM.com : MemObject(p->name), pmemAddr(NULL), port(NULL), lat(p->latency), _params(p) 598991SAli.Saidi@ARM.com{ 608991SAli.Saidi@ARM.com if (params()->addrRange.size() % TheISA::PageBytes != 0) 612SN/A panic("Memory Size not divisible by page size\n"); 622SN/A 632SN/A int map_flags = MAP_ANON | MAP_PRIVATE; 648991SAli.Saidi@ARM.com pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE, 652SN/A map_flags, -1, 0); 668991SAli.Saidi@ARM.com 678991SAli.Saidi@ARM.com if (pmemAddr == (void *)MAP_FAILED) { 682SN/A perror("mmap"); 698991SAli.Saidi@ARM.com fatal("Could not mmap!\n"); 702SN/A } 712SN/A 722SN/A pagePtr = 0; 732SN/A} 742SN/A 752SN/Avoid 762SN/APhysicalMemory::init() 772SN/A{ 782SN/A if (!port) 792SN/A panic("PhysicalMemory not connected to anything!"); 802SN/A port->sendStatusChange(Port::RangeChange); 812SN/A} 822SN/A 832SN/APhysicalMemory::~PhysicalMemory() 842SN/A{ 852SN/A if (pmemAddr) 862SN/A munmap(pmemAddr, params()->addrRange.size()); 872SN/A //Remove memPorts? 882680Sktlim@umich.edu} 892SN/A 908670Ss052838@student.dtu.dkAddr 918670Ss052838@student.dtu.dkPhysicalMemory::new_page() 928670Ss052838@student.dtu.dk{ 932SN/A Addr return_addr = pagePtr << LogVMPageSize; 942SN/A return_addr += params()->addrRange.start; 952SN/A 962SN/A ++pagePtr; 972SN/A return return_addr; 982SN/A} 992SN/A 1008670Ss052838@student.dtu.dkint 1012SN/APhysicalMemory::deviceBlockSize() 1022SN/A{ 1032SN/A //Can accept anysize request 1042SN/A return 0; 1052SN/A} 1062680Sktlim@umich.edu 1072SN/ATick 1082SN/APhysicalMemory::calculateLatency(Packet *pkt) 1092SN/A{ 1102SN/A return lat; 1112SN/A} 1122SN/A 1132SN/ATick 1142SN/APhysicalMemory::doFunctionalAccess(Packet *pkt) 1152SN/A{ 1162SN/A assert(pkt->getAddr() + pkt->getSize() < params()->addrRange.size()); 1172SN/A 1182SN/A switch (pkt->cmd) { 1192SN/A case Packet::ReadReq: 1207823Ssteve.reinhardt@amd.com memcpy(pkt->getPtr<uint8_t>(), 1212SN/A pmemAddr + pkt->getAddr() - params()->addrRange.start, 1222SN/A pkt->getSize()); 1232SN/A break; 1242SN/A case Packet::WriteReq: 1252SN/A memcpy(pmemAddr + pkt->getAddr() - params()->addrRange.start, 1262SN/A pkt->getPtr<uint8_t>(), 1272SN/A pkt->getSize()); 1282SN/A // temporary hack: will need to add real LL/SC implementation 1292SN/A // for cacheless systems later. 1301885SN/A if (pkt->req->getFlags() & LOCKED) { 1311885SN/A pkt->req->setScResult(1); 1321885SN/A } 1332SN/A break; 1342SN/A default: 1352SN/A panic("unimplemented"); 1362SN/A } 1372680Sktlim@umich.edu 1382SN/A pkt->result = Packet::Success; 1392680Sktlim@umich.edu return calculateLatency(pkt); 1401646SN/A} 1418231Snate@binkert.org 1422SN/APort * 1432SN/APhysicalMemory::getPort(const std::string &if_name, int idx) 1442SN/A{ 1452SN/A if (if_name == "port" && idx == -1) { 1462SN/A if (port != NULL) 1472130SN/A panic("PhysicalMemory::getPort: additional port requested to memory!"); 1482SN/A port = new MemoryPort(name() + "-port", this); 1491885SN/A return port; 1502SN/A } else if (if_name == "functional") { 1512SN/A /* special port for functional writes at startup. */ 1522SN/A return new MemoryPort(name() + "-funcport", this); 1532130SN/A } else { 1542SN/A panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 1552SN/A } 1562SN/A} 1572SN/A 1582SN/Avoid 1592SN/APhysicalMemory::recvStatusChange(Port::Status status) 1602SN/A{ 1619649SAndreas.Sandberg@ARM.com} 1629649SAndreas.Sandberg@ARM.com 1639649SAndreas.Sandberg@ARM.comPhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 1649649SAndreas.Sandberg@ARM.com PhysicalMemory *_memory) 1659649SAndreas.Sandberg@ARM.com : SimpleTimingPort(_name), memory(_memory) 1669649SAndreas.Sandberg@ARM.com{ } 1679649SAndreas.Sandberg@ARM.com 1689649SAndreas.Sandberg@ARM.comvoid 1699649SAndreas.Sandberg@ARM.comPhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 1709649SAndreas.Sandberg@ARM.com{ 1719649SAndreas.Sandberg@ARM.com memory->recvStatusChange(status); 1729649SAndreas.Sandberg@ARM.com} 173 174void 175PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 176 AddrRangeList &snoop) 177{ 178 memory->getAddressRanges(resp, snoop); 179} 180 181void 182PhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 183{ 184 snoop.clear(); 185 resp.clear(); 186 resp.push_back(RangeSize(params()->addrRange.start, params()->addrRange.size())); 187} 188 189int 190PhysicalMemory::MemoryPort::deviceBlockSize() 191{ 192 return memory->deviceBlockSize(); 193} 194 195bool 196PhysicalMemory::MemoryPort::recvTiming(Packet *pkt) 197{ 198 assert(pkt->result != Packet::Nacked); 199 200 Tick latency = memory->doFunctionalAccess(pkt); 201 202 pkt->makeTimingResponse(); 203 sendTiming(pkt, latency); 204 205 return true; 206} 207 208Tick 209PhysicalMemory::MemoryPort::recvAtomic(Packet *pkt) 210{ 211 return memory->doFunctionalAccess(pkt); 212} 213 214void 215PhysicalMemory::MemoryPort::recvFunctional(Packet *pkt) 216{ 217 memory->doFunctionalAccess(pkt); 218} 219 220unsigned int 221PhysicalMemory::drain(Event *de) 222{ 223 int count = port->drain(de); 224 if (count) 225 changeState(Draining); 226 else 227 changeState(Drained); 228 return count; 229} 230 231void 232PhysicalMemory::serialize(ostream &os) 233{ 234 gzFile compressedMem; 235 string filename = name() + ".physmem"; 236 237 SERIALIZE_SCALAR(filename); 238 239 // write memory file 240 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 241 int fd = creat(thefile.c_str(), 0664); 242 if (fd < 0) { 243 perror("creat"); 244 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 245 } 246 247 compressedMem = gzdopen(fd, "wb"); 248 if (compressedMem == NULL) 249 fatal("Insufficient memory to allocate compression state for %s\n", 250 filename); 251 252 if (gzwrite(compressedMem, pmemAddr, params()->addrRange.size()) != params()->addrRange.size()) { 253 fatal("Write failed on physical memory checkpoint file '%s'\n", 254 filename); 255 } 256 257 if (gzclose(compressedMem)) 258 fatal("Close failed on physical memory checkpoint file '%s'\n", 259 filename); 260} 261 262void 263PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 264{ 265 gzFile compressedMem; 266 long *tempPage; 267 long *pmem_current; 268 uint64_t curSize; 269 uint32_t bytesRead; 270 const int chunkSize = 16384; 271 272 273 string filename; 274 275 UNSERIALIZE_SCALAR(filename); 276 277 filename = cp->cptDir + "/" + filename; 278 279 // mmap memoryfile 280 int fd = open(filename.c_str(), O_RDONLY); 281 if (fd < 0) { 282 perror("open"); 283 fatal("Can't open physical memory checkpoint file '%s'", filename); 284 } 285 286 compressedMem = gzdopen(fd, "rb"); 287 if (compressedMem == NULL) 288 fatal("Insufficient memory to allocate compression state for %s\n", 289 filename); 290 291 // unmap file that was mmaped in the constructor 292 // This is done here to make sure that gzip and open don't muck with our 293 // nice large space of memory before we reallocate it 294 munmap(pmemAddr, params()->addrRange.size()); 295 296 pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE, 297 MAP_ANON | MAP_PRIVATE, -1, 0); 298 299 if (pmemAddr == (void *)MAP_FAILED) { 300 perror("mmap"); 301 fatal("Could not mmap physical memory!\n"); 302 } 303 304 curSize = 0; 305 tempPage = (long*)malloc(chunkSize); 306 if (tempPage == NULL) 307 fatal("Unable to malloc memory to read file %s\n", filename); 308 309 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 310 while (curSize < params()->addrRange.size()) { 311 bytesRead = gzread(compressedMem, tempPage, chunkSize); 312 if (bytesRead != chunkSize && bytesRead != params()->addrRange.size() - curSize) 313 fatal("Read failed on physical memory checkpoint file '%s'" 314 " got %d bytes, expected %d or %d bytes\n", 315 filename, bytesRead, chunkSize, params()->addrRange.size()-curSize); 316 317 assert(bytesRead % sizeof(long) == 0); 318 319 for (int x = 0; x < bytesRead/sizeof(long); x++) 320 { 321 if (*(tempPage+x) != 0) { 322 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 323 *pmem_current = *(tempPage+x); 324 } 325 } 326 curSize += bytesRead; 327 } 328 329 free(tempPage); 330 331 if (gzclose(compressedMem)) 332 fatal("Close failed on physical memory checkpoint file '%s'\n", 333 filename); 334 335} 336 337 338BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 339 340 Param<string> file; 341 Param<Range<Addr> > range; 342 Param<Tick> latency; 343 344END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 345 346BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 347 348 INIT_PARAM_DFLT(file, "memory mapped file", ""), 349 INIT_PARAM(range, "Device Address Range"), 350 INIT_PARAM(latency, "Memory access latency") 351 352END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 353 354CREATE_SIM_OBJECT(PhysicalMemory) 355{ 356 PhysicalMemory::Params *p = new PhysicalMemory::Params; 357 p->name = getInstanceName(); 358 p->addrRange = range; 359 p->latency = latency; 360 return new PhysicalMemory(p); 361} 362 363REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 364