physical.cc revision 3281:d0f7a2e1573f
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 43#include "base/misc.hh" 44#include "config/full_system.hh" 45#include "mem/packet_impl.hh" 46#include "mem/physical.hh" 47#include "sim/host.hh" 48#include "sim/builder.hh" 49#include "sim/eventq.hh" 50#include "arch/isa_traits.hh" 51 52 53using namespace std; 54using namespace TheISA; 55 56 57PhysicalMemory::PhysicalMemory(Params *p) 58 : MemObject(p->name), pmemAddr(NULL), port(NULL), lat(p->latency), _params(p) 59{ 60 if (params()->addrRange.size() % TheISA::PageBytes != 0) 61 panic("Memory Size not divisible by page size\n"); 62 63 int map_flags = MAP_ANON | MAP_PRIVATE; 64 pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE, 65 map_flags, -1, 0); 66 67 if (pmemAddr == (void *)MAP_FAILED) { 68 perror("mmap"); 69 fatal("Could not mmap!\n"); 70 } 71 72 pagePtr = 0; 73} 74 75void 76PhysicalMemory::init() 77{ 78 if (!port) 79 panic("PhysicalMemory not connected to anything!"); 80 port->sendStatusChange(Port::RangeChange); 81} 82 83PhysicalMemory::~PhysicalMemory() 84{ 85 if (pmemAddr) 86 munmap(pmemAddr, params()->addrRange.size()); 87 //Remove memPorts? 88} 89 90Addr 91PhysicalMemory::new_page() 92{ 93 Addr return_addr = pagePtr << LogVMPageSize; 94 return_addr += params()->addrRange.start; 95 96 ++pagePtr; 97 return return_addr; 98} 99 100int 101PhysicalMemory::deviceBlockSize() 102{ 103 //Can accept anysize request 104 return 0; 105} 106 107Tick 108PhysicalMemory::calculateLatency(Packet *pkt) 109{ 110 return lat; 111} 112 113 114 115// Add load-locked to tracking list. Should only be called if the 116// operation is a load and the LOCKED flag is set. 117void 118PhysicalMemory::trackLoadLocked(Request *req) 119{ 120 Addr paddr = LockedAddr::mask(req->getPaddr()); 121 122 // first we check if we already have a locked addr for this 123 // xc. Since each xc only gets one, we just update the 124 // existing record with the new address. 125 list<LockedAddr>::iterator i; 126 127 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) { 128 if (i->matchesContext(req)) { 129 DPRINTF(LLSC, "Modifying lock record: cpu %d thread %d addr %#x\n", 130 req->getCpuNum(), req->getThreadNum(), paddr); 131 i->addr = paddr; 132 return; 133 } 134 } 135 136 // no record for this xc: need to allocate a new one 137 DPRINTF(LLSC, "Adding lock record: cpu %d thread %d addr %#x\n", 138 req->getCpuNum(), req->getThreadNum(), paddr); 139 lockedAddrList.push_front(LockedAddr(req)); 140} 141 142 143// Called on *writes* only... both regular stores and 144// store-conditional operations. Check for conventional stores which 145// conflict with locked addresses, and for success/failure of store 146// conditionals. 147bool 148PhysicalMemory::checkLockedAddrList(Request *req) 149{ 150 Addr paddr = LockedAddr::mask(req->getPaddr()); 151 bool isLocked = req->isLocked(); 152 153 // Initialize return value. Non-conditional stores always 154 // succeed. Assume conditional stores will fail until proven 155 // otherwise. 156 bool success = !isLocked; 157 158 // Iterate over list. Note that there could be multiple matching 159 // records, as more than one context could have done a load locked 160 // to this location. 161 list<LockedAddr>::iterator i = lockedAddrList.begin(); 162 163 while (i != lockedAddrList.end()) { 164 165 if (i->addr == paddr) { 166 // we have a matching address 167 168 if (isLocked && i->matchesContext(req)) { 169 // it's a store conditional, and as far as the memory 170 // system can tell, the requesting context's lock is 171 // still valid. 172 DPRINTF(LLSC, "StCond success: cpu %d thread %d addr %#x\n", 173 req->getCpuNum(), req->getThreadNum(), paddr); 174 success = true; 175 } 176 177 // Get rid of our record of this lock and advance to next 178 DPRINTF(LLSC, "Erasing lock record: cpu %d thread %d addr %#x\n", 179 i->cpuNum, i->threadNum, paddr); 180 i = lockedAddrList.erase(i); 181 } 182 else { 183 // no match: advance to next record 184 ++i; 185 } 186 } 187 188 if (isLocked) { 189 req->setScResult(success ? 1 : 0); 190 } 191 192 return success; 193} 194 195void 196PhysicalMemory::doFunctionalAccess(Packet *pkt) 197{ 198 assert(pkt->getAddr() + pkt->getSize() <= params()->addrRange.size()); 199 200 if (pkt->isRead()) { 201 if (pkt->req->isLocked()) { 202 trackLoadLocked(pkt->req); 203 } 204 DPRINTF(MemoryAccess, "Performing Read of size %i on address 0x%x\n", 205 pkt->getSize(), pkt->getAddr()); 206 memcpy(pkt->getPtr<uint8_t>(), 207 pmemAddr + pkt->getAddr() - params()->addrRange.start, 208 pkt->getSize()); 209 } 210 else if (pkt->isWrite()) { 211 if (writeOK(pkt->req)) { 212 DPRINTF(MemoryAccess, "Performing Write of size %i on address 0x%x\n", 213 pkt->getSize(), pkt->getAddr()); 214 memcpy(pmemAddr + pkt->getAddr() - params()->addrRange.start, 215 pkt->getPtr<uint8_t>(), pkt->getSize()); 216 } 217 } 218 else if (pkt->isInvalidate()) { 219 //upgrade or invalidate 220 pkt->flags |= SATISFIED; 221 } 222 else { 223 panic("unimplemented"); 224 } 225 226 pkt->result = Packet::Success; 227} 228 229Port * 230PhysicalMemory::getPort(const std::string &if_name, int idx) 231{ 232 if (if_name == "port" && idx == -1) { 233 if (port != NULL) 234 panic("PhysicalMemory::getPort: additional port requested to memory!"); 235 port = new MemoryPort(name() + "-port", this); 236 return port; 237 } else if (if_name == "functional") { 238 /* special port for functional writes at startup. And for memtester */ 239 return new MemoryPort(name() + "-funcport", this); 240 } else { 241 panic("PhysicalMemory::getPort: unknown port %s requested", if_name); 242 } 243} 244 245void 246PhysicalMemory::recvStatusChange(Port::Status status) 247{ 248} 249 250PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name, 251 PhysicalMemory *_memory) 252 : SimpleTimingPort(_name), memory(_memory) 253{ } 254 255void 256PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status) 257{ 258 memory->recvStatusChange(status); 259} 260 261void 262PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp, 263 AddrRangeList &snoop) 264{ 265 memory->getAddressRanges(resp, snoop); 266} 267 268void 269PhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop) 270{ 271 snoop.clear(); 272 resp.clear(); 273 resp.push_back(RangeSize(params()->addrRange.start, 274 params()->addrRange.size())); 275} 276 277int 278PhysicalMemory::MemoryPort::deviceBlockSize() 279{ 280 return memory->deviceBlockSize(); 281} 282 283Tick 284PhysicalMemory::MemoryPort::recvAtomic(Packet *pkt) 285{ 286 memory->doFunctionalAccess(pkt); 287 return memory->calculateLatency(pkt); 288} 289 290void 291PhysicalMemory::MemoryPort::recvFunctional(Packet *pkt) 292{ 293 // Default implementation of SimpleTimingPort::recvFunctional() 294 // calls recvAtomic() and throws away the latency; we can save a 295 // little here by just not calculating the latency. 296 memory->doFunctionalAccess(pkt); 297} 298 299unsigned int 300PhysicalMemory::drain(Event *de) 301{ 302 int count = port->drain(de); 303 if (count) 304 changeState(Draining); 305 else 306 changeState(Drained); 307 return count; 308} 309 310void 311PhysicalMemory::serialize(ostream &os) 312{ 313 gzFile compressedMem; 314 string filename = name() + ".physmem"; 315 316 SERIALIZE_SCALAR(filename); 317 318 // write memory file 319 string thefile = Checkpoint::dir() + "/" + filename.c_str(); 320 int fd = creat(thefile.c_str(), 0664); 321 if (fd < 0) { 322 perror("creat"); 323 fatal("Can't open physical memory checkpoint file '%s'\n", filename); 324 } 325 326 compressedMem = gzdopen(fd, "wb"); 327 if (compressedMem == NULL) 328 fatal("Insufficient memory to allocate compression state for %s\n", 329 filename); 330 331 if (gzwrite(compressedMem, pmemAddr, params()->addrRange.size()) != params()->addrRange.size()) { 332 fatal("Write failed on physical memory checkpoint file '%s'\n", 333 filename); 334 } 335 336 if (gzclose(compressedMem)) 337 fatal("Close failed on physical memory checkpoint file '%s'\n", 338 filename); 339} 340 341void 342PhysicalMemory::unserialize(Checkpoint *cp, const string §ion) 343{ 344 gzFile compressedMem; 345 long *tempPage; 346 long *pmem_current; 347 uint64_t curSize; 348 uint32_t bytesRead; 349 const int chunkSize = 16384; 350 351 352 string filename; 353 354 UNSERIALIZE_SCALAR(filename); 355 356 filename = cp->cptDir + "/" + filename; 357 358 // mmap memoryfile 359 int fd = open(filename.c_str(), O_RDONLY); 360 if (fd < 0) { 361 perror("open"); 362 fatal("Can't open physical memory checkpoint file '%s'", filename); 363 } 364 365 compressedMem = gzdopen(fd, "rb"); 366 if (compressedMem == NULL) 367 fatal("Insufficient memory to allocate compression state for %s\n", 368 filename); 369 370 // unmap file that was mmaped in the constructor 371 // This is done here to make sure that gzip and open don't muck with our 372 // nice large space of memory before we reallocate it 373 munmap(pmemAddr, params()->addrRange.size()); 374 375 pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE, 376 MAP_ANON | MAP_PRIVATE, -1, 0); 377 378 if (pmemAddr == (void *)MAP_FAILED) { 379 perror("mmap"); 380 fatal("Could not mmap physical memory!\n"); 381 } 382 383 curSize = 0; 384 tempPage = (long*)malloc(chunkSize); 385 if (tempPage == NULL) 386 fatal("Unable to malloc memory to read file %s\n", filename); 387 388 /* Only copy bytes that are non-zero, so we don't give the VM system hell */ 389 while (curSize < params()->addrRange.size()) { 390 bytesRead = gzread(compressedMem, tempPage, chunkSize); 391 if (bytesRead != chunkSize && bytesRead != params()->addrRange.size() - curSize) 392 fatal("Read failed on physical memory checkpoint file '%s'" 393 " got %d bytes, expected %d or %d bytes\n", 394 filename, bytesRead, chunkSize, params()->addrRange.size()-curSize); 395 396 assert(bytesRead % sizeof(long) == 0); 397 398 for (int x = 0; x < bytesRead/sizeof(long); x++) 399 { 400 if (*(tempPage+x) != 0) { 401 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long)); 402 *pmem_current = *(tempPage+x); 403 } 404 } 405 curSize += bytesRead; 406 } 407 408 free(tempPage); 409 410 if (gzclose(compressedMem)) 411 fatal("Close failed on physical memory checkpoint file '%s'\n", 412 filename); 413 414} 415 416 417BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 418 419 Param<string> file; 420 Param<Range<Addr> > range; 421 Param<Tick> latency; 422 423END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory) 424 425BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 426 427 INIT_PARAM_DFLT(file, "memory mapped file", ""), 428 INIT_PARAM(range, "Device Address Range"), 429 INIT_PARAM(latency, "Memory access latency") 430 431END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory) 432 433CREATE_SIM_OBJECT(PhysicalMemory) 434{ 435 PhysicalMemory::Params *p = new PhysicalMemory::Params; 436 p->name = getInstanceName(); 437 p->addrRange = range; 438 p->latency = latency; 439 return new PhysicalMemory(p); 440} 441 442REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory) 443