abstract_mem.cc revision 5314
19020Sgblack@eecs.umich.edu/* 29020Sgblack@eecs.umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan 39020Sgblack@eecs.umich.edu * All rights reserved. 49020Sgblack@eecs.umich.edu * 59020Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 69020Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are 79020Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright 89020Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 99020Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 109020Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 119020Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution; 129020Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its 139020Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 149020Sgblack@eecs.umich.edu * this software without specific prior written permission. 159020Sgblack@eecs.umich.edu * 169020Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179020Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189020Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199020Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209020Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219020Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229020Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239020Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249020Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259020Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269020Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279020Sgblack@eecs.umich.edu * 289020Sgblack@eecs.umich.edu * Authors: Ron Dreslinski 299020Sgblack@eecs.umich.edu * Ali Saidi 309020Sgblack@eecs.umich.edu */ 319020Sgblack@eecs.umich.edu 329020Sgblack@eecs.umich.edu#include <sys/types.h> 339020Sgblack@eecs.umich.edu#include <sys/mman.h> 349024Sgblack@eecs.umich.edu#include <errno.h> 359022Sgblack@eecs.umich.edu#include <fcntl.h> 369024Sgblack@eecs.umich.edu#include <unistd.h> 379024Sgblack@eecs.umich.edu#include <zlib.h> 389020Sgblack@eecs.umich.edu 399020Sgblack@eecs.umich.edu#include <iostream> 409020Sgblack@eecs.umich.edu#include <string> 419022Sgblack@eecs.umich.edu 429022Sgblack@eecs.umich.edu#include "arch/isa_traits.hh" 439022Sgblack@eecs.umich.edu#include "base/misc.hh" 449023Sgblack@eecs.umich.edu#include "config/full_system.hh" 459023Sgblack@eecs.umich.edu#include "mem/packet_access.hh" 469023Sgblack@eecs.umich.edu#include "mem/physical.hh" 479023Sgblack@eecs.umich.edu#include "sim/eventq.hh" 489023Sgblack@eecs.umich.edu#include "sim/host.hh" 499377Sgblack@eecs.umich.edu 509023Sgblack@eecs.umich.eduusing namespace std; 519023Sgblack@eecs.umich.eduusing namespace TheISA; 529023Sgblack@eecs.umich.edu 539023Sgblack@eecs.umich.eduPhysicalMemory::PhysicalMemory(const Params *p) 549023Sgblack@eecs.umich.edu : MemObject(p), pmemAddr(NULL), lat(p->latency) 559023Sgblack@eecs.umich.edu{ 569023Sgblack@eecs.umich.edu if (params()->range.size() % TheISA::PageBytes != 0) 579023Sgblack@eecs.umich.edu panic("Memory Size not divisible by page size\n"); 589023Sgblack@eecs.umich.edu 599023Sgblack@eecs.umich.edu int map_flags = MAP_ANON | MAP_PRIVATE; 609023Sgblack@eecs.umich.edu pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 619023Sgblack@eecs.umich.edu PROT_READ | PROT_WRITE, map_flags, -1, 0); 629023Sgblack@eecs.umich.edu 639023Sgblack@eecs.umich.edu if (pmemAddr == (void *)MAP_FAILED) { 649023Sgblack@eecs.umich.edu perror("mmap"); 659023Sgblack@eecs.umich.edu fatal("Could not mmap!\n"); 669023Sgblack@eecs.umich.edu } 679023Sgblack@eecs.umich.edu 689023Sgblack@eecs.umich.edu //If requested, initialize all the memory to 0 699023Sgblack@eecs.umich.edu if (p->zero) 709023Sgblack@eecs.umich.edu memset(pmemAddr, 0, p->range.size()); 719023Sgblack@eecs.umich.edu 729023Sgblack@eecs.umich.edu pagePtr = 0; 739023Sgblack@eecs.umich.edu 749023Sgblack@eecs.umich.edu cachedSize = params()->range.size(); 759023Sgblack@eecs.umich.edu cachedStart = params()->range.start; 769023Sgblack@eecs.umich.edu 779023Sgblack@eecs.umich.edu} 789023Sgblack@eecs.umich.edu 799023Sgblack@eecs.umich.eduvoid 809023Sgblack@eecs.umich.eduPhysicalMemory::init() 819023Sgblack@eecs.umich.edu{ 829023Sgblack@eecs.umich.edu if (ports.size() == 0) { 839023Sgblack@eecs.umich.edu fatal("PhysicalMemory object %s is unconnected!", name()); 849023Sgblack@eecs.umich.edu } 859023Sgblack@eecs.umich.edu 869023Sgblack@eecs.umich.edu for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 879023Sgblack@eecs.umich.edu if (*pi) 889023Sgblack@eecs.umich.edu (*pi)->sendStatusChange(Port::RangeChange); 899023Sgblack@eecs.umich.edu } 909023Sgblack@eecs.umich.edu} 919023Sgblack@eecs.umich.edu 929023Sgblack@eecs.umich.eduPhysicalMemory::~PhysicalMemory() 939022Sgblack@eecs.umich.edu{ 949024Sgblack@eecs.umich.edu if (pmemAddr) 959022Sgblack@eecs.umich.edu munmap((char*)pmemAddr, params()->range.size()); 969022Sgblack@eecs.umich.edu //Remove memPorts? 979022Sgblack@eecs.umich.edu} 989022Sgblack@eecs.umich.edu 999022Sgblack@eecs.umich.eduAddr 1009022Sgblack@eecs.umich.eduPhysicalMemory::new_page() 1019022Sgblack@eecs.umich.edu{ 1029022Sgblack@eecs.umich.edu Addr return_addr = pagePtr << LogVMPageSize; 1039022Sgblack@eecs.umich.edu return_addr += start(); 1049022Sgblack@eecs.umich.edu 1059022Sgblack@eecs.umich.edu ++pagePtr; 1069022Sgblack@eecs.umich.edu return return_addr; 1079023Sgblack@eecs.umich.edu} 1089023Sgblack@eecs.umich.edu 1099023Sgblack@eecs.umich.eduint 1109023Sgblack@eecs.umich.eduPhysicalMemory::deviceBlockSize() 1119023Sgblack@eecs.umich.edu{ 1129023Sgblack@eecs.umich.edu //Can accept anysize request 1139023Sgblack@eecs.umich.edu return 0; 1149023Sgblack@eecs.umich.edu} 1159023Sgblack@eecs.umich.edu 1169022Sgblack@eecs.umich.eduTick 1179020Sgblack@eecs.umich.eduPhysicalMemory::calculateLatency(PacketPtr pkt) 1189020Sgblack@eecs.umich.edu{ 1199020Sgblack@eecs.umich.edu return lat; 1209020Sgblack@eecs.umich.edu} 121 122 123 124// Add load-locked to tracking list. Should only be called if the 125// operation is a load and the LOCKED flag is set. 126void 127PhysicalMemory::trackLoadLocked(PacketPtr pkt) 128{ 129 Request *req = pkt->req; 130 Addr paddr = LockedAddr::mask(req->getPaddr()); 131 132 // first we check if we already have a locked addr for this 133 // xc. Since each xc only gets one, we just update the 134 // existing record with the new address. 135 list<LockedAddr>::iterator i; 136 137 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) { 138 if (i->matchesContext(req)) { 139 DPRINTF(LLSC, "Modifying lock record: cpu %d thread %d addr %#x\n", 140 req->getCpuNum(), req->getThreadNum(), paddr); 141 i->addr = paddr; 142 return; 143 } 144 } 145 146 // no record for this xc: need to allocate a new one 147 DPRINTF(LLSC, "Adding lock record: cpu %d thread %d addr %#x\n", 148 req->getCpuNum(), req->getThreadNum(), paddr); 149 lockedAddrList.push_front(LockedAddr(req)); 150} 151 152 153// Called on *writes* only... both regular stores and 154// store-conditional operations. Check for conventional stores which 155// conflict with locked addresses, and for success/failure of store 156// conditionals. 157bool 158PhysicalMemory::checkLockedAddrList(PacketPtr pkt) 159{ 160 Request *req = pkt->req; 161 Addr paddr = LockedAddr::mask(req->getPaddr()); 162 bool isLocked = pkt->isLocked(); 163 164 // Initialize return value. Non-conditional stores always 165 // succeed. Assume conditional stores will fail until proven 166 // otherwise. 167 bool success = !isLocked; 168 169 // Iterate over list. Note that there could be multiple matching 170 // records, as more than one context could have done a load locked 171 // to this location. 172 list<LockedAddr>::iterator i = lockedAddrList.begin(); 173 174 while (i != lockedAddrList.end()) { 175 176 if (i->addr == paddr) { 177 // we have a matching address 178 179 if (isLocked && i->matchesContext(req)) { 180 // it's a store conditional, and as far as the memory 181 // system can tell, the requesting context's lock is 182 // still valid. 183 DPRINTF(LLSC, "StCond success: cpu %d thread %d addr %#x\n", 184 req->getCpuNum(), req->getThreadNum(), paddr); 185 success = true; 186 } 187 188 // Get rid of our record of this lock and advance to next 189 DPRINTF(LLSC, "Erasing lock record: cpu %d thread %d addr %#x\n", 190 i->cpuNum, i->threadNum, paddr); 191 i = lockedAddrList.erase(i); 192 } 193 else { 194 // no match: advance to next record 195 ++i; 196 } 197 } 198 199 if (isLocked) { 200 req->setExtraData(success ? 1 : 0); 201 } 202 203 return success; 204} 205 206 207#if TRACING_ON 208 209#define CASE(A, T) \ 210 case sizeof(T): \ 211 DPRINTF(MemoryAccess, A " of size %i on address 0x%x data 0x%x\n", \ 212 pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \ 213 break 214 215 216#define TRACE_PACKET(A) \ 217 do { \ 218 switch (pkt->getSize()) { \ 219 CASE(A, uint64_t); \ 220 CASE(A, uint32_t); \ 221 CASE(A, uint16_t); \ 222 CASE(A, uint8_t); \ 223 default: \ 224 DPRINTF(MemoryAccess, A " of size %i on address 0x%x\n", \ 225 pkt->getSize(), pkt->getAddr()); \ 226 } \ 227 } while (0) 228 229#else 230 231#define TRACE_PACKET(A) 232 233#endif 234 235Tick 236PhysicalMemory::doAtomicAccess(PacketPtr pkt) 237{ 238 assert(pkt->getAddr() >= start() && 239 pkt->getAddr() + pkt->getSize() <= start() + size()); 240 241 if (pkt->memInhibitAsserted()) { 242 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n", 243 pkt->getAddr()); 244 return 0; 245 } 246 247 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 248 249 if (pkt->cmd == MemCmd::SwapReq) { 250 IntReg overwrite_val; 251 bool overwrite_mem; 252 uint64_t condition_val64; 253 uint32_t condition_val32; 254 255 assert(sizeof(IntReg) >= pkt->getSize()); 256 257 overwrite_mem = true; 258 // keep a copy of our possible write value, and copy what is at the 259 // memory address into the packet 260 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize()); 261 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 262 263 if (pkt->req->isCondSwap()) { 264 if (pkt->getSize() == sizeof(uint64_t)) { 265 condition_val64 = pkt->req->getExtraData(); 266 overwrite_mem = !std::memcmp(&condition_val64, hostAddr, 267 sizeof(uint64_t)); 268 } else if (pkt->getSize() == sizeof(uint32_t)) { 269 condition_val32 = (uint32_t)pkt->req->getExtraData(); 270 overwrite_mem = !std::memcmp(&condition_val32, hostAddr, 271 sizeof(uint32_t)); 272 } else 273 panic("Invalid size for conditional read/write\n"); 274 } 275 276 if (overwrite_mem) 277 std::memcpy(hostAddr, &overwrite_val, pkt->getSize()); 278 279 TRACE_PACKET("Read/Write"); 280 } else if (pkt->isRead()) { 281 assert(!pkt->isWrite()); 282 if (pkt->isLocked()) { 283 trackLoadLocked(pkt); 284 } 285 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 286 TRACE_PACKET("Read"); 287 } else if (pkt->isWrite()) { 288 if (writeOK(pkt)) { 289 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 290 TRACE_PACKET("Write"); 291 } 292 } else if (pkt->isInvalidate()) { 293 //upgrade or invalidate 294 if (pkt->needsResponse()) { 295 pkt->makeAtomicResponse(); 296 } 297 } else { 298 panic("unimplemented"); 299 } 300 301 if (pkt->needsResponse()) { 302 pkt->makeAtomicResponse(); 303 } 304 return calculateLatency(pkt); 305} 306 307 308void 309PhysicalMemory::doFunctionalAccess(PacketPtr pkt) 310{ 311 assert(pkt->getAddr() >= start() && 312 pkt->getAddr() + pkt->getSize() <= start() + size()); 313 314 315 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 316 317 if (pkt->isRead()) { 318 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 319 TRACE_PACKET("Read"); 320 pkt->makeAtomicResponse(); 321 } else if (pkt->isWrite()) { 322 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 323 TRACE_PACKET("Write"); 324 pkt->makeAtomicResponse(); 325 } else if (pkt->isPrint()) { 326 Packet::PrintReqState *prs = dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 327 prs->printLabels(); 328 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 329 } else { 330 panic("PhysicalMemory: unimplemented functional command %s", 331 pkt->cmdString()); 332 } 333} 334 335 336Port * 337PhysicalMemory::getPort(const std::string &if_name, int idx) 338{ 339 // Accept request for "functional" port for backwards compatibility 340 // with places where this function is called from C++. I'd prefer 341 // to move all these into Python someday. 342 if (if_name == "functional") { 343 return new MemoryPort(csprintf("%s-functional", name()), this); 344 } 345 346 if (if_name != "port") { 347 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 348 } 349 350 if (idx >= ports.size()) { 351 ports.resize(idx+1); 352 } 353 354 if (ports[idx] != NULL) { 355 panic("PhysicalMemory::getPort: port %d already assigned", idx); 356 } 357 358 MemoryPort *port = 359 new MemoryPort(csprintf("%s-port%d", name(), idx), this); 360 361 ports[idx] = port; 362 return port; 363} 364 365 366void 367PhysicalMemory::recvStatusChange(Port::Status status) 368{ 369} 370 371PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 372 PhysicalMemory *_memory) 373 : SimpleTimingPort(_name), memory(_memory) 374{ } 375 376void 377PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 378{ 379 memory->recvStatusChange(status); 380} 381 382void 383PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 384 bool &snoop) 385{ 386 memory->getAddressRanges(resp, snoop); 387} 388 389void 390PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 391{ 392 snoop = false; 393 resp.clear(); 394 resp.push_back(RangeSize(start(), params()->range.size())); 395} 396 397int 398PhysicalMemory::MemoryPort::deviceBlockSize() 399{ 400 return memory->deviceBlockSize(); 401} 402 403Tick 404PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 405{ 406 return memory->doAtomicAccess(pkt); 407} 408 409void 410PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 411{ 412 pkt->pushLabel(memory->name()); 413 414 if (!checkFunctional(pkt)) { 415 // Default implementation of SimpleTimingPort::recvFunctional() 416 // calls recvAtomic() and throws away the latency; we can save a 417 // little here by just not calculating the latency. 418 memory->doFunctionalAccess(pkt); 419 } 420 421 pkt->popLabel(); 422} 423 424unsigned int 425PhysicalMemory::drain(Event *de) 426{ 427 int count = 0; 428 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 429 count += (*pi)->drain(de); 430 } 431 432 if (count) 433 changeState(Draining); 434 else 435 changeState(Drained); 436 return count; 437} 438 439void 440PhysicalMemory::serialize(ostream &os) 441{ 442 gzFile compressedMem; 443 string filename = name() + ".physmem"; 444 445 SERIALIZE_SCALAR(filename); 446 447 // write memory file 448 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 449 int fd = creat(thefile.c_str(), 0664); 450 if (fd < 0) { 451 perror("creat"); 452 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 453 } 454 455 compressedMem = gzdopen(fd, "wb"); 456 if (compressedMem == NULL) 457 fatal("Insufficient memory to allocate compression state for %s\n", 458 filename); 459 460 if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 461 params()->range.size()) { 462 fatal("Write failed on physical memory checkpoint file '%s'\n", 463 filename); 464 } 465 466 if (gzclose(compressedMem)) 467 fatal("Close failed on physical memory checkpoint file '%s'\n", 468 filename); 469} 470 471void 472PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 473{ 474 gzFile compressedMem; 475 long *tempPage; 476 long *pmem_current; 477 uint64_t curSize; 478 uint32_t bytesRead; 479 const int chunkSize = 16384; 480 481 482 string filename; 483 484 UNSERIALIZE_SCALAR(filename); 485 486 filename = cp->cptDir + "/" + filename; 487 488 // mmap memoryfile 489 int fd = open(filename.c_str(), O_RDONLY); 490 if (fd < 0) { 491 perror("open"); 492 fatal("Can't open physical memory checkpoint file '%s'", filename); 493 } 494 495 compressedMem = gzdopen(fd, "rb"); 496 if (compressedMem == NULL) 497 fatal("Insufficient memory to allocate compression state for %s\n", 498 filename); 499 500 // unmap file that was mmaped in the constructor 501 // This is done here to make sure that gzip and open don't muck with our 502 // nice large space of memory before we reallocate it 503 munmap((char*)pmemAddr, params()->range.size()); 504 505 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 506 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 507 508 if (pmemAddr == (void *)MAP_FAILED) { 509 perror("mmap"); 510 fatal("Could not mmap physical memory!\n"); 511 } 512 513 curSize = 0; 514 tempPage = (long*)malloc(chunkSize); 515 if (tempPage == NULL) 516 fatal("Unable to malloc memory to read file %s\n", filename); 517 518 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 519 while (curSize < params()->range.size()) { 520 bytesRead = gzread(compressedMem, tempPage, chunkSize); 521 if (bytesRead != chunkSize && 522 bytesRead != params()->range.size() - curSize) 523 fatal("Read failed on physical memory checkpoint file '%s'" 524 " got %d bytes, expected %d or %d bytes\n", 525 filename, bytesRead, chunkSize, 526 params()->range.size() - curSize); 527 528 assert(bytesRead % sizeof(long) == 0); 529 530 for (int x = 0; x < bytesRead/sizeof(long); x++) 531 { 532 if (*(tempPage+x) != 0) { 533 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 534 *pmem_current = *(tempPage+x); 535 } 536 } 537 curSize += bytesRead; 538 } 539 540 free(tempPage); 541 542 if (gzclose(compressedMem)) 543 fatal("Close failed on physical memory checkpoint file '%s'\n", 544 filename); 545 546} 547 548PhysicalMemory * 549PhysicalMemoryParams::create() 550{ 551 return new PhysicalMemory(this); 552} 553