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