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