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 <iostream> 40#include <string> 41 42#include "arch/isa_traits.hh" 43#include "base/misc.hh"
| 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 <iostream> 40#include <string> 41 42#include "arch/isa_traits.hh" 43#include "base/misc.hh"
|
120} 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 = 327 dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 328 // Need to call printLabels() explicitly since we're not going 329 // through printObj(). 330 prs->printLabels(); 331 // Right now we just print the single byte at the specified address. 332 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 333 } else { 334 panic("PhysicalMemory: unimplemented functional command %s", 335 pkt->cmdString()); 336 } 337} 338 339 340Port * 341PhysicalMemory::getPort(const std::string &if_name, int idx) 342{ 343 // Accept request for "functional" port for backwards compatibility 344 // with places where this function is called from C++. I'd prefer 345 // to move all these into Python someday. 346 if (if_name == "functional") { 347 return new MemoryPort(csprintf("%s-functional", name()), this); 348 } 349 350 if (if_name != "port") { 351 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 352 } 353 354 if (idx >= ports.size()) { 355 ports.resize(idx+1); 356 } 357 358 if (ports[idx] != NULL) { 359 panic("PhysicalMemory::getPort: port %d already assigned", idx); 360 } 361 362 MemoryPort *port = 363 new MemoryPort(csprintf("%s-port%d", name(), idx), this); 364 365 ports[idx] = port; 366 return port; 367} 368 369 370void 371PhysicalMemory::recvStatusChange(Port::Status status) 372{ 373} 374 375PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 376 PhysicalMemory *_memory) 377 : SimpleTimingPort(_name), memory(_memory) 378{ } 379 380void 381PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 382{ 383 memory->recvStatusChange(status); 384} 385 386void 387PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 388 bool &snoop) 389{ 390 memory->getAddressRanges(resp, snoop); 391} 392 393void 394PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 395{ 396 snoop = false; 397 resp.clear(); 398 resp.push_back(RangeSize(start(), params()->range.size())); 399} 400 401int 402PhysicalMemory::MemoryPort::deviceBlockSize() 403{ 404 return memory->deviceBlockSize(); 405} 406 407Tick 408PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 409{ 410 return memory->doAtomicAccess(pkt); 411} 412 413void 414PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 415{ 416 pkt->pushLabel(memory->name()); 417 418 if (!checkFunctional(pkt)) { 419 // Default implementation of SimpleTimingPort::recvFunctional() 420 // calls recvAtomic() and throws away the latency; we can save a 421 // little here by just not calculating the latency. 422 memory->doFunctionalAccess(pkt); 423 } 424 425 pkt->popLabel(); 426} 427 428unsigned int 429PhysicalMemory::drain(Event *de) 430{ 431 int count = 0; 432 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 433 count += (*pi)->drain(de); 434 } 435 436 if (count) 437 changeState(Draining); 438 else 439 changeState(Drained); 440 return count; 441} 442 443void 444PhysicalMemory::serialize(ostream &os) 445{ 446 gzFile compressedMem; 447 string filename = name() + ".physmem"; 448 449 SERIALIZE_SCALAR(filename); 450 451 // write memory file 452 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 453 int fd = creat(thefile.c_str(), 0664); 454 if (fd < 0) { 455 perror("creat"); 456 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 457 } 458 459 compressedMem = gzdopen(fd, "wb"); 460 if (compressedMem == NULL) 461 fatal("Insufficient memory to allocate compression state for %s\n", 462 filename); 463 464 if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 465 params()->range.size()) { 466 fatal("Write failed on physical memory checkpoint file '%s'\n", 467 filename); 468 } 469 470 if (gzclose(compressedMem)) 471 fatal("Close failed on physical memory checkpoint file '%s'\n", 472 filename); 473} 474 475void 476PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 477{ 478 gzFile compressedMem; 479 long *tempPage; 480 long *pmem_current; 481 uint64_t curSize; 482 uint32_t bytesRead; 483 const int chunkSize = 16384; 484 485 486 string filename; 487 488 UNSERIALIZE_SCALAR(filename); 489 490 filename = cp->cptDir + "/" + filename; 491 492 // mmap memoryfile 493 int fd = open(filename.c_str(), O_RDONLY); 494 if (fd < 0) { 495 perror("open"); 496 fatal("Can't open physical memory checkpoint file '%s'", filename); 497 } 498 499 compressedMem = gzdopen(fd, "rb"); 500 if (compressedMem == NULL) 501 fatal("Insufficient memory to allocate compression state for %s\n", 502 filename); 503 504 // unmap file that was mmaped in the constructor 505 // This is done here to make sure that gzip and open don't muck with our 506 // nice large space of memory before we reallocate it 507 munmap((char*)pmemAddr, params()->range.size()); 508 509 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 510 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 511 512 if (pmemAddr == (void *)MAP_FAILED) { 513 perror("mmap"); 514 fatal("Could not mmap physical memory!\n"); 515 } 516 517 curSize = 0; 518 tempPage = (long*)malloc(chunkSize); 519 if (tempPage == NULL) 520 fatal("Unable to malloc memory to read file %s\n", filename); 521 522 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 523 while (curSize < params()->range.size()) { 524 bytesRead = gzread(compressedMem, tempPage, chunkSize); 525 if (bytesRead != chunkSize && 526 bytesRead != params()->range.size() - curSize) 527 fatal("Read failed on physical memory checkpoint file '%s'" 528 " got %d bytes, expected %d or %d bytes\n", 529 filename, bytesRead, chunkSize, 530 params()->range.size() - curSize); 531 532 assert(bytesRead % sizeof(long) == 0); 533 534 for (int x = 0; x < bytesRead/sizeof(long); x++) 535 { 536 if (*(tempPage+x) != 0) { 537 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 538 *pmem_current = *(tempPage+x); 539 } 540 } 541 curSize += bytesRead; 542 } 543 544 free(tempPage); 545 546 if (gzclose(compressedMem)) 547 fatal("Close failed on physical memory checkpoint file '%s'\n", 548 filename); 549 550} 551 552PhysicalMemory * 553PhysicalMemoryParams::create() 554{ 555 return new PhysicalMemory(this); 556}
| 125} 126 127 128 129// Add load-locked to tracking list. Should only be called if the 130// operation is a load and the LOCKED 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: cpu %d thread %d addr %#x\n", 145 req->getCpuNum(), req->getThreadNum(), 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: cpu %d thread %d addr %#x\n", 153 req->getCpuNum(), req->getThreadNum(), 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 isLocked = pkt->isLocked(); 168 169 // Initialize return value. Non-conditional stores always 170 // succeed. Assume conditional stores will fail until proven 171 // otherwise. 172 bool success = !isLocked; 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 (isLocked && 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: cpu %d thread %d addr %#x\n", 189 req->getCpuNum(), req->getThreadNum(), 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: cpu %d thread %d addr %#x\n", 195 i->cpuNum, i->threadNum, paddr); 196 i = lockedAddrList.erase(i); 197 } 198 else { 199 // no match: advance to next record 200 ++i; 201 } 202 } 203 204 if (isLocked) { 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, A " of size %i on address 0x%x data 0x%x\n", \ 217 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, A " of size %i on address 0x%x\n", \ 230 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 assert(sizeof(IntReg) >= pkt->getSize()); 261 262 overwrite_mem = true; 263 // keep a copy of our possible write value, and copy what is at the 264 // memory address into the packet 265 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize()); 266 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 267 268 if (pkt->req->isCondSwap()) { 269 if (pkt->getSize() == sizeof(uint64_t)) { 270 condition_val64 = pkt->req->getExtraData(); 271 overwrite_mem = !std::memcmp(&condition_val64, hostAddr, 272 sizeof(uint64_t)); 273 } else if (pkt->getSize() == sizeof(uint32_t)) { 274 condition_val32 = (uint32_t)pkt->req->getExtraData(); 275 overwrite_mem = !std::memcmp(&condition_val32, hostAddr, 276 sizeof(uint32_t)); 277 } else 278 panic("Invalid size for conditional read/write\n"); 279 } 280 281 if (overwrite_mem) 282 std::memcpy(hostAddr, &overwrite_val, pkt->getSize()); 283 284 TRACE_PACKET("Read/Write"); 285 } else if (pkt->isRead()) { 286 assert(!pkt->isWrite()); 287 if (pkt->isLocked()) { 288 trackLoadLocked(pkt); 289 } 290 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 291 TRACE_PACKET("Read"); 292 } else if (pkt->isWrite()) { 293 if (writeOK(pkt)) { 294 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 295 TRACE_PACKET("Write"); 296 } 297 } else if (pkt->isInvalidate()) { 298 //upgrade or invalidate 299 if (pkt->needsResponse()) { 300 pkt->makeAtomicResponse(); 301 } 302 } else { 303 panic("unimplemented"); 304 } 305 306 if (pkt->needsResponse()) { 307 pkt->makeAtomicResponse(); 308 } 309 return calculateLatency(pkt); 310} 311 312 313void 314PhysicalMemory::doFunctionalAccess(PacketPtr pkt) 315{ 316 assert(pkt->getAddr() >= start() && 317 pkt->getAddr() + pkt->getSize() <= start() + size()); 318 319 320 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start(); 321 322 if (pkt->isRead()) { 323 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize()); 324 TRACE_PACKET("Read"); 325 pkt->makeAtomicResponse(); 326 } else if (pkt->isWrite()) { 327 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize()); 328 TRACE_PACKET("Write"); 329 pkt->makeAtomicResponse(); 330 } else if (pkt->isPrint()) { 331 Packet::PrintReqState *prs = 332 dynamic_cast<Packet::PrintReqState*>(pkt->senderState); 333 // Need to call printLabels() explicitly since we're not going 334 // through printObj(). 335 prs->printLabels(); 336 // Right now we just print the single byte at the specified address. 337 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr); 338 } else { 339 panic("PhysicalMemory: unimplemented functional command %s", 340 pkt->cmdString()); 341 } 342} 343 344 345Port * 346PhysicalMemory::getPort(const std::string &if_name, int idx) 347{ 348 // Accept request for "functional" port for backwards compatibility 349 // with places where this function is called from C++. I'd prefer 350 // to move all these into Python someday. 351 if (if_name == "functional") { 352 return new MemoryPort(csprintf("%s-functional", name()), this); 353 } 354 355 if (if_name != "port") { 356 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 357 } 358 359 if (idx >= ports.size()) { 360 ports.resize(idx+1); 361 } 362 363 if (ports[idx] != NULL) { 364 panic("PhysicalMemory::getPort: port %d already assigned", idx); 365 } 366 367 MemoryPort *port = 368 new MemoryPort(csprintf("%s-port%d", name(), idx), this); 369 370 ports[idx] = port; 371 return port; 372} 373 374 375void 376PhysicalMemory::recvStatusChange(Port::Status status) 377{ 378} 379 380PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 381 PhysicalMemory *_memory) 382 : SimpleTimingPort(_name), memory(_memory) 383{ } 384 385void 386PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 387{ 388 memory->recvStatusChange(status); 389} 390 391void 392PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 393 bool &snoop) 394{ 395 memory->getAddressRanges(resp, snoop); 396} 397 398void 399PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop) 400{ 401 snoop = false; 402 resp.clear(); 403 resp.push_back(RangeSize(start(), params()->range.size())); 404} 405 406int 407PhysicalMemory::MemoryPort::deviceBlockSize() 408{ 409 return memory->deviceBlockSize(); 410} 411 412Tick 413PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt) 414{ 415 return memory->doAtomicAccess(pkt); 416} 417 418void 419PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt) 420{ 421 pkt->pushLabel(memory->name()); 422 423 if (!checkFunctional(pkt)) { 424 // Default implementation of SimpleTimingPort::recvFunctional() 425 // calls recvAtomic() and throws away the latency; we can save a 426 // little here by just not calculating the latency. 427 memory->doFunctionalAccess(pkt); 428 } 429 430 pkt->popLabel(); 431} 432 433unsigned int 434PhysicalMemory::drain(Event *de) 435{ 436 int count = 0; 437 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) { 438 count += (*pi)->drain(de); 439 } 440 441 if (count) 442 changeState(Draining); 443 else 444 changeState(Drained); 445 return count; 446} 447 448void 449PhysicalMemory::serialize(ostream &os) 450{ 451 gzFile compressedMem; 452 string filename = name() + ".physmem"; 453 454 SERIALIZE_SCALAR(filename); 455 456 // write memory file 457 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 458 int fd = creat(thefile.c_str(), 0664); 459 if (fd < 0) { 460 perror("creat"); 461 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 462 } 463 464 compressedMem = gzdopen(fd, "wb"); 465 if (compressedMem == NULL) 466 fatal("Insufficient memory to allocate compression state for %s\n", 467 filename); 468 469 if (gzwrite(compressedMem, pmemAddr, params()->range.size()) != 470 params()->range.size()) { 471 fatal("Write failed on physical memory checkpoint file '%s'\n", 472 filename); 473 } 474 475 if (gzclose(compressedMem)) 476 fatal("Close failed on physical memory checkpoint file '%s'\n", 477 filename); 478} 479 480void 481PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 482{ 483 gzFile compressedMem; 484 long *tempPage; 485 long *pmem_current; 486 uint64_t curSize; 487 uint32_t bytesRead; 488 const int chunkSize = 16384; 489 490 491 string filename; 492 493 UNSERIALIZE_SCALAR(filename); 494 495 filename = cp->cptDir + "/" + filename; 496 497 // mmap memoryfile 498 int fd = open(filename.c_str(), O_RDONLY); 499 if (fd < 0) { 500 perror("open"); 501 fatal("Can't open physical memory checkpoint file '%s'", filename); 502 } 503 504 compressedMem = gzdopen(fd, "rb"); 505 if (compressedMem == NULL) 506 fatal("Insufficient memory to allocate compression state for %s\n", 507 filename); 508 509 // unmap file that was mmaped in the constructor 510 // This is done here to make sure that gzip and open don't muck with our 511 // nice large space of memory before we reallocate it 512 munmap((char*)pmemAddr, params()->range.size()); 513 514 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(), 515 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 516 517 if (pmemAddr == (void *)MAP_FAILED) { 518 perror("mmap"); 519 fatal("Could not mmap physical memory!\n"); 520 } 521 522 curSize = 0; 523 tempPage = (long*)malloc(chunkSize); 524 if (tempPage == NULL) 525 fatal("Unable to malloc memory to read file %s\n", filename); 526 527 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 528 while (curSize < params()->range.size()) { 529 bytesRead = gzread(compressedMem, tempPage, chunkSize); 530 if (bytesRead != chunkSize && 531 bytesRead != params()->range.size() - curSize) 532 fatal("Read failed on physical memory checkpoint file '%s'" 533 " got %d bytes, expected %d or %d bytes\n", 534 filename, bytesRead, chunkSize, 535 params()->range.size() - curSize); 536 537 assert(bytesRead % sizeof(long) == 0); 538 539 for (int x = 0; x < bytesRead/sizeof(long); x++) 540 { 541 if (*(tempPage+x) != 0) { 542 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 543 *pmem_current = *(tempPage+x); 544 } 545 } 546 curSize += bytesRead; 547 } 548 549 free(tempPage); 550 551 if (gzclose(compressedMem)) 552 fatal("Close failed on physical memory checkpoint file '%s'\n", 553 filename); 554 555} 556 557PhysicalMemory * 558PhysicalMemoryParams::create() 559{ 560 return new PhysicalMemory(this); 561}
|