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