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