abstract_mem.cc revision 6820
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 * Authors: Ron Dreslinski 29 * Ali Saidi 30 */ 31 32#include <sys/types.h> 33#include <sys/mman.h> 34#include <errno.h> 35#include <fcntl.h> 36#include <unistd.h> 37#include <zlib.h> 38 39#include <cstdio> 40#include <iostream> 41#include <string> 42 43#include "arch/registers.hh" 44#include "base/misc.hh" 45#include "base/random.hh" 46#include "base/types.hh" 47#include "config/full_system.hh" 48#include "config/the_isa.hh" 49#include "mem/packet_access.hh" 50#include "mem/physical.hh" 51#include "sim/eventq.hh" 52 53using namespace std; 54using namespace TheISA; 55 56PhysicalMemory::PhysicalMemory(const Params *p) 57 : MemObject(p), pmemAddr(NULL), pagePtr(0), 58 lat(p->latency), lat_var(p->latency_var), 59 cachedSize(params()->range.size()), cachedStart(params()->range.start) 60{ 61 if (params()->range.size() % TheISA::PageBytes != 0) 62 panic("Memory Size not divisible by page size\n"); 63 64 if (params()->null) 65 return; 66 67 int map_flags = MAP_ANON | MAP_PRIVATE; 68 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 69 PROT_READ | PROT_WRITE, map_flags, -1, 0); 70 71 if (pmemAddr == (void *)MAP_FAILED) { 72 perror("mmap"); 73 fatal("Could not mmap!\n"); 74 } 75 76 //If requested, initialize all the memory to 0 77 if (p->zero) 78 memset(pmemAddr, 0, p->range.size()); 79} 80 81void 82PhysicalMemory::init() 83{ 84 if (ports.size() == 0) { 85 fatal("PhysicalMemory object %s is unconnected!", name()); 86 } 87 88 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 89 if (*pi) 90 (*pi)->sendStatusChange(Port::RangeChange); 91 } 92} 93 94PhysicalMemory::~PhysicalMemory() 95{ 96 if (pmemAddr) 97 munmap((char*)pmemAddr, params()->range.size()); 98 //Remove memPorts? 99} 100 101Addr 102PhysicalMemory::new_page() 103{ 104 Addr return_addr = pagePtr << LogVMPageSize; 105 return_addr += start(); 106 107 ++pagePtr; 108 return return_addr; 109} 110 111unsigned 112PhysicalMemory::deviceBlockSize() const 113{ 114 //Can accept anysize request 115 return 0; 116} 117 118Tick 119PhysicalMemory::calculateLatency(PacketPtr pkt) 120{ 121 Tick latency = lat; 122 if (lat_var != 0) 123 latency += random_mt.random<Tick>(0, lat_var); 124 return latency; 125} 126 127 128 129// Add load-locked to tracking list. Should only be called if the 130// operation is a load and the LLSC flag is set. 131void 132PhysicalMemory::trackLoadLocked(PacketPtr pkt) 133{ 134 Request *req = pkt->req; 135 Addr paddr = LockedAddr::mask(req->getPaddr()); 136 137 // first we check if we already have a locked addr for this 138 // xc. Since each xc only gets one, we just update the 139 // existing record with the new address. 140 list<LockedAddr>::iterator i; 141 142 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) { 143 if (i->matchesContext(req)) { 144 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n", 145 req->contextId(), paddr); 146 i->addr = paddr; 147 return; 148 } 149 } 150 151 // no record for this xc: need to allocate a new one 152 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n", 153 req->contextId(), paddr); 154 lockedAddrList.push_front(LockedAddr(req)); 155} 156 157 158// Called on *writes* only... both regular stores and 159// store-conditional operations. Check for conventional stores which 160// conflict with locked addresses, and for success/failure of store 161// conditionals. 162bool 163PhysicalMemory::checkLockedAddrList(PacketPtr pkt) 164{ 165 Request *req = pkt->req; 166 Addr paddr = LockedAddr::mask(req->getPaddr()); 167 bool isLLSC = pkt->isLLSC(); 168 169 // Initialize return value. Non-conditional stores always 170 // succeed. Assume conditional stores will fail until proven 171 // otherwise. 172 bool success = !isLLSC; 173 174 // Iterate over list. Note that there could be multiple matching 175 // records, as more than one context could have done a load locked 176 // to this location. 177 list<LockedAddr>::iterator i = lockedAddrList.begin(); 178 179 while (i != lockedAddrList.end()) { 180 181 if (i->addr == paddr) { 182 // we have a matching address 183 184 if (isLLSC && i->matchesContext(req)) { 185 // it's a store conditional, and as far as the memory 186 // system can tell, the requesting context's lock is 187 // still valid. 188 DPRINTF(LLSC, "StCond success: context %d addr %#x\n", 189 req->contextId(), paddr); 190 success = true; 191 } 192 193 // Get rid of our record of this lock and advance to next 194 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n", 195 i->contextId, paddr); 196 i = lockedAddrList.erase(i); 197 } 198 else { 199 // no match: advance to next record 200 ++i; 201 } 202 } 203 204 if (isLLSC) { 205 req->setExtraData(success ? 1 : 0); 206 } 207 208 return success; 209} 210 211 212#if TRACING_ON 213 214#define CASE(A, T) \ 215 case sizeof(T): \ 216 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \ 217 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \ 218 break 219 220 221#define TRACE_PACKET(A) \ 222 do { \ 223 switch (pkt->getSize()) { \ 224 CASE(A, uint64_t); \ 225 CASE(A, uint32_t); \ 226 CASE(A, uint16_t); \ 227 CASE(A, uint8_t); \ 228 default: \ 229 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \ 230 A, pkt->getSize(), pkt->getAddr()); \ 231 } \ 232 } while (0) 233 234#else 235 236#define TRACE_PACKET(A) 237 238#endif 239 240Tick 241PhysicalMemory::doAtomicAccess(PacketPtr pkt) 242{ 243 assert(pkt->getAddr() >= start() && 244 pkt->getAddr() + pkt->getSize() <= start() + size()); 245 246 if (pkt->memInhibitAsserted()) { 247 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n", 248 pkt->getAddr()); 249 return 0; 250 } 251 252 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 253 254 if (pkt->cmd == MemCmd::SwapReq) { 255 IntReg overwrite_val; 256 bool overwrite_mem; 257 uint64_t condition_val64; 258 uint32_t condition_val32; 259 260 if (!pmemAddr) 261 panic("Swap only works if there is real memory (i.e. null=False)"); 262 assert(sizeof(IntReg) >= pkt->getSize()); 263 264 overwrite_mem = true; 265 // keep a copy of our possible write value, and copy what is at the 266 // memory address into the packet 267 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize()); 268 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 269 270 if (pkt->req->isCondSwap()) { 271 if (pkt->getSize() == sizeof(uint64_t)) { 272 condition_val64 = pkt->req->getExtraData(); 273 overwrite_mem = !std::memcmp(&condition_val64, hostAddr, 274 sizeof(uint64_t)); 275 } else if (pkt->getSize() == sizeof(uint32_t)) { 276 condition_val32 = (uint32_t)pkt->req->getExtraData(); 277 overwrite_mem = !std::memcmp(&condition_val32, hostAddr, 278 sizeof(uint32_t)); 279 } else 280 panic("Invalid size for conditional read/write\n"); 281 } 282 283 if (overwrite_mem) 284 std::memcpy(hostAddr, &overwrite_val, pkt->getSize()); 285 286 assert(!pkt->req->isInstFetch()); 287 TRACE_PACKET("Read/Write"); 288 } else if (pkt->isRead()) { 289 assert(!pkt->isWrite()); 290 if (pkt->isLLSC()) { 291 trackLoadLocked(pkt); 292 } 293 if (pmemAddr) 294 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 295 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read"); 296 } else if (pkt->isWrite()) { 297 if (writeOK(pkt)) { 298 if (pmemAddr) 299 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 300 assert(!pkt->req->isInstFetch()); 301 TRACE_PACKET("Write"); 302 } 303 } else if (pkt->isInvalidate()) { 304 //upgrade or invalidate 305 if (pkt->needsResponse()) { 306 pkt->makeAtomicResponse(); 307 } 308 } else { 309 panic("unimplemented"); 310 } 311 312 if (pkt->needsResponse()) { 313 pkt->makeAtomicResponse(); 314 } 315 return calculateLatency(pkt); 316} 317 318 319void 320PhysicalMemory::doFunctionalAccess(PacketPtr pkt) 321{ 322 assert(pkt->getAddr() >= start() && 323 pkt->getAddr() + pkt->getSize() <= start() + size()); 324 325 326 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 327 328 if (pkt->isRead()) { 329 if (pmemAddr) 330 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 331 TRACE_PACKET("Read"); 332 pkt->makeAtomicResponse(); 333 } else if (pkt->isWrite()) { 334 if (pmemAddr) 335 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 336 TRACE_PACKET("Write"); 337 pkt->makeAtomicResponse(); 338 } else if (pkt->isPrint()) { 339 Packet::PrintReqState *prs = 340 dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 341 // Need to call printLabels() explicitly since we're not going 342 // through printObj(). 343 prs->printLabels(); 344 // Right now we just print the single byte at the specified address. 345 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 346 } else { 347 panic("PhysicalMemory: unimplemented functional command %s", 348 pkt->cmdString()); 349 } 350} 351 352 353Port * 354PhysicalMemory::getPort(const std::string &if_name, int idx) 355{ 356 // Accept request for "functional" port for backwards compatibility 357 // with places where this function is called from C++. I'd prefer 358 // to move all these into Python someday. 359 if (if_name == "functional") { 360 return new MemoryPort(csprintf("%s-functional", name()), this); 361 } 362 363 if (if_name != "port") { 364 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 365 } 366 367 if (idx >= (int)ports.size()) { 368 ports.resize(idx + 1); 369 } 370 371 if (ports[idx] != NULL) { 372 panic("PhysicalMemory::getPort: port %d already assigned", idx); 373 } 374 375 MemoryPort *port = 376 new MemoryPort(csprintf("%s-port%d", name(), idx), this); 377 378 ports[idx] = port; 379 return port; 380} 381 382 383void 384PhysicalMemory::recvStatusChange(Port::Status status) 385{ 386} 387 388PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 389 PhysicalMemory *_memory) 390 : SimpleTimingPort(_name, _memory), memory(_memory) 391{ } 392 393void 394PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 395{ 396 memory->recvStatusChange(status); 397} 398 399void 400PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 401 bool &snoop) 402{ 403 memory->getAddressRanges(resp, snoop); 404} 405 406void 407PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 408{ 409 snoop = false; 410 resp.clear(); 411 resp.push_back(RangeSize(start(), params()->range.size())); 412} 413 414unsigned 415PhysicalMemory::MemoryPort::deviceBlockSize() const 416{ 417 return memory->deviceBlockSize(); 418} 419 420Tick 421PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 422{ 423 return memory->doAtomicAccess(pkt); 424} 425 426void 427PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 428{ 429 pkt->pushLabel(memory->name()); 430 431 if (!checkFunctional(pkt)) { 432 // Default implementation of SimpleTimingPort::recvFunctional() 433 // calls recvAtomic() and throws away the latency; we can save a 434 // little here by just not calculating the latency. 435 memory->doFunctionalAccess(pkt); 436 } 437 438 pkt->popLabel(); 439} 440 441unsigned int 442PhysicalMemory::drain(Event *de) 443{ 444 int count = 0; 445 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 446 count += (*pi)->drain(de); 447 } 448 449 if (count) 450 changeState(Draining); 451 else 452 changeState(Drained); 453 return count; 454} 455 456void 457PhysicalMemory::serialize(ostream &os) 458{ 459 if (!pmemAddr) 460 return; 461 462 gzFile compressedMem; 463 string filename = name() + ".physmem"; 464 465 SERIALIZE_SCALAR(filename); 466 467 // write memory file 468 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 469 int fd = creat(thefile.c_str(), 0664); 470 if (fd < 0) { 471 perror("creat"); 472 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 473 } 474 475 compressedMem = gzdopen(fd, "wb"); 476 if (compressedMem == NULL) 477 fatal("Insufficient memory to allocate compression state for %s\n", 478 filename); 479 480 if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 481 (int)params()->range.size()) { 482 fatal("Write failed on physical memory checkpoint file '%s'\n", 483 filename); 484 } 485 486 if (gzclose(compressedMem)) 487 fatal("Close failed on physical memory checkpoint file '%s'\n", 488 filename); 489} 490 491void 492PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 493{ 494 if (!pmemAddr) 495 return; 496 497 gzFile compressedMem; 498 long *tempPage; 499 long *pmem_current; 500 uint64_t curSize; 501 uint32_t bytesRead; 502 const uint32_t chunkSize = 16384; 503 504 string filename; 505 506 UNSERIALIZE_SCALAR(filename); 507 508 filename = cp->cptDir + "/" + filename; 509 510 // mmap memoryfile 511 int fd = open(filename.c_str(), O_RDONLY); 512 if (fd < 0) { 513 perror("open"); 514 fatal("Can't open physical memory checkpoint file '%s'", filename); 515 } 516 517 compressedMem = gzdopen(fd, "rb"); 518 if (compressedMem == NULL) 519 fatal("Insufficient memory to allocate compression state for %s\n", 520 filename); 521 522 // unmap file that was mmaped in the constructor 523 // This is done here to make sure that gzip and open don't muck with our 524 // nice large space of memory before we reallocate it 525 munmap((char*)pmemAddr, params()->range.size()); 526 527 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 528 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 529 530 if (pmemAddr == (void *)MAP_FAILED) { 531 perror("mmap"); 532 fatal("Could not mmap physical memory!\n"); 533 } 534 535 curSize = 0; 536 tempPage = (long*)malloc(chunkSize); 537 if (tempPage == NULL) 538 fatal("Unable to malloc memory to read file %s\n", filename); 539 540 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 541 while (curSize < params()->range.size()) { 542 bytesRead = gzread(compressedMem, tempPage, chunkSize); 543 if (bytesRead == 0) 544 break; 545 546 assert(bytesRead % sizeof(long) == 0); 547 548 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++) 549 { 550 if (*(tempPage+x) != 0) { 551 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 552 *pmem_current = *(tempPage+x); 553 } 554 } 555 curSize += bytesRead; 556 } 557 558 free(tempPage); 559 560 if (gzclose(compressedMem)) 561 fatal("Close failed on physical memory checkpoint file '%s'\n", 562 filename); 563 564} 565 566PhysicalMemory * 567PhysicalMemoryParams::create() 568{ 569 return new PhysicalMemory(this); 570} 571