Merge with the main repository again.
[gem5.git] / src / mem / physical.cc
1 /*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ron Dreslinski
41 * Ali Saidi
42 */
43
44 #include <sys/mman.h>
45 #include <sys/types.h>
46 #include <sys/user.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <zlib.h>
50
51 #include <cerrno>
52 #include <cstdio>
53 #include <iostream>
54 #include <string>
55
56 #include "arch/isa_traits.hh"
57 #include "arch/registers.hh"
58 #include "base/intmath.hh"
59 #include "base/misc.hh"
60 #include "base/random.hh"
61 #include "base/types.hh"
62 #include "config/the_isa.hh"
63 #include "debug/LLSC.hh"
64 #include "debug/MemoryAccess.hh"
65 #include "mem/packet_access.hh"
66 #include "mem/physical.hh"
67 #include "sim/eventq.hh"
68
69 using namespace std;
70 using namespace TheISA;
71
72 PhysicalMemory::PhysicalMemory(const Params *p)
73 : MemObject(p), pmemAddr(NULL), lat(p->latency), lat_var(p->latency_var),
74 _size(params()->range.size()), _start(params()->range.start)
75 {
76 if (size() % TheISA::PageBytes != 0)
77 panic("Memory Size not divisible by page size\n");
78
79 if (params()->null)
80 return;
81
82
83 if (params()->file == "") {
84 int map_flags = MAP_ANON | MAP_PRIVATE;
85 pmemAddr = (uint8_t *)mmap(NULL, size(),
86 PROT_READ | PROT_WRITE, map_flags, -1, 0);
87 } else {
88 int map_flags = MAP_PRIVATE;
89 int fd = open(params()->file.c_str(), O_RDONLY);
90 _size = lseek(fd, 0, SEEK_END);
91 lseek(fd, 0, SEEK_SET);
92 pmemAddr = (uint8_t *)mmap(NULL, roundUp(size(), sysconf(_SC_PAGESIZE)),
93 PROT_READ | PROT_WRITE, map_flags, fd, 0);
94 }
95
96 if (pmemAddr == (void *)MAP_FAILED) {
97 perror("mmap");
98 if (params()->file == "")
99 fatal("Could not mmap!\n");
100 else
101 fatal("Could not find file: %s\n", params()->file);
102 }
103
104 //If requested, initialize all the memory to 0
105 if (p->zero)
106 memset(pmemAddr, 0, size());
107 }
108
109 void
110 PhysicalMemory::init()
111 {
112 if (ports.size() == 0) {
113 fatal("PhysicalMemory object %s is unconnected!", name());
114 }
115
116 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
117 if (*pi)
118 (*pi)->sendStatusChange(Port::RangeChange);
119 }
120 }
121
122 PhysicalMemory::~PhysicalMemory()
123 {
124 if (pmemAddr)
125 munmap((char*)pmemAddr, size());
126 }
127
128 unsigned
129 PhysicalMemory::deviceBlockSize() const
130 {
131 //Can accept anysize request
132 return 0;
133 }
134
135 Tick
136 PhysicalMemory::calculateLatency(PacketPtr pkt)
137 {
138 Tick latency = lat;
139 if (lat_var != 0)
140 latency += random_mt.random<Tick>(0, lat_var);
141 return latency;
142 }
143
144
145
146 // Add load-locked to tracking list. Should only be called if the
147 // operation is a load and the LLSC flag is set.
148 void
149 PhysicalMemory::trackLoadLocked(PacketPtr pkt)
150 {
151 Request *req = pkt->req;
152 Addr paddr = LockedAddr::mask(req->getPaddr());
153
154 // first we check if we already have a locked addr for this
155 // xc. Since each xc only gets one, we just update the
156 // existing record with the new address.
157 list<LockedAddr>::iterator i;
158
159 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
160 if (i->matchesContext(req)) {
161 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
162 req->contextId(), paddr);
163 i->addr = paddr;
164 return;
165 }
166 }
167
168 // no record for this xc: need to allocate a new one
169 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
170 req->contextId(), paddr);
171 lockedAddrList.push_front(LockedAddr(req));
172 }
173
174
175 // Called on *writes* only... both regular stores and
176 // store-conditional operations. Check for conventional stores which
177 // conflict with locked addresses, and for success/failure of store
178 // conditionals.
179 bool
180 PhysicalMemory::checkLockedAddrList(PacketPtr pkt)
181 {
182 Request *req = pkt->req;
183 Addr paddr = LockedAddr::mask(req->getPaddr());
184 bool isLLSC = pkt->isLLSC();
185
186 // Initialize return value. Non-conditional stores always
187 // succeed. Assume conditional stores will fail until proven
188 // otherwise.
189 bool success = !isLLSC;
190
191 // Iterate over list. Note that there could be multiple matching
192 // records, as more than one context could have done a load locked
193 // to this location.
194 list<LockedAddr>::iterator i = lockedAddrList.begin();
195
196 while (i != lockedAddrList.end()) {
197
198 if (i->addr == paddr) {
199 // we have a matching address
200
201 if (isLLSC && i->matchesContext(req)) {
202 // it's a store conditional, and as far as the memory
203 // system can tell, the requesting context's lock is
204 // still valid.
205 DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
206 req->contextId(), paddr);
207 success = true;
208 }
209
210 // Get rid of our record of this lock and advance to next
211 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
212 i->contextId, paddr);
213 i = lockedAddrList.erase(i);
214 }
215 else {
216 // no match: advance to next record
217 ++i;
218 }
219 }
220
221 if (isLLSC) {
222 req->setExtraData(success ? 1 : 0);
223 }
224
225 return success;
226 }
227
228
229 #if TRACING_ON
230
231 #define CASE(A, T) \
232 case sizeof(T): \
233 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
234 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
235 break
236
237
238 #define TRACE_PACKET(A) \
239 do { \
240 switch (pkt->getSize()) { \
241 CASE(A, uint64_t); \
242 CASE(A, uint32_t); \
243 CASE(A, uint16_t); \
244 CASE(A, uint8_t); \
245 default: \
246 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
247 A, pkt->getSize(), pkt->getAddr()); \
248 DDUMP(MemoryAccess, pkt->getPtr<uint8_t>(), pkt->getSize());\
249 } \
250 } while (0)
251
252 #else
253
254 #define TRACE_PACKET(A)
255
256 #endif
257
258 Tick
259 PhysicalMemory::doAtomicAccess(PacketPtr pkt)
260 {
261 assert(pkt->getAddr() >= start() &&
262 pkt->getAddr() + pkt->getSize() <= start() + size());
263
264 if (pkt->memInhibitAsserted()) {
265 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
266 pkt->getAddr());
267 return 0;
268 }
269
270 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
271
272 if (pkt->cmd == MemCmd::SwapReq) {
273 IntReg overwrite_val;
274 bool overwrite_mem;
275 uint64_t condition_val64;
276 uint32_t condition_val32;
277
278 if (!pmemAddr)
279 panic("Swap only works if there is real memory (i.e. null=False)");
280 assert(sizeof(IntReg) >= pkt->getSize());
281
282 overwrite_mem = true;
283 // keep a copy of our possible write value, and copy what is at the
284 // memory address into the packet
285 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
286 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
287
288 if (pkt->req->isCondSwap()) {
289 if (pkt->getSize() == sizeof(uint64_t)) {
290 condition_val64 = pkt->req->getExtraData();
291 overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
292 sizeof(uint64_t));
293 } else if (pkt->getSize() == sizeof(uint32_t)) {
294 condition_val32 = (uint32_t)pkt->req->getExtraData();
295 overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
296 sizeof(uint32_t));
297 } else
298 panic("Invalid size for conditional read/write\n");
299 }
300
301 if (overwrite_mem)
302 std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
303
304 assert(!pkt->req->isInstFetch());
305 TRACE_PACKET("Read/Write");
306 } else if (pkt->isRead()) {
307 assert(!pkt->isWrite());
308 if (pkt->isLLSC()) {
309 trackLoadLocked(pkt);
310 }
311 if (pmemAddr)
312 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
313 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
314 } else if (pkt->isWrite()) {
315 if (writeOK(pkt)) {
316 if (pmemAddr)
317 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
318 assert(!pkt->req->isInstFetch());
319 TRACE_PACKET("Write");
320 }
321 } else if (pkt->isInvalidate()) {
322 //upgrade or invalidate
323 if (pkt->needsResponse()) {
324 pkt->makeAtomicResponse();
325 }
326 } else {
327 panic("unimplemented");
328 }
329
330 if (pkt->needsResponse()) {
331 pkt->makeAtomicResponse();
332 }
333 return calculateLatency(pkt);
334 }
335
336
337 void
338 PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
339 {
340 assert(pkt->getAddr() >= start() &&
341 pkt->getAddr() + pkt->getSize() <= start() + size());
342
343
344 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
345
346 if (pkt->isRead()) {
347 if (pmemAddr)
348 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
349 TRACE_PACKET("Read");
350 pkt->makeAtomicResponse();
351 } else if (pkt->isWrite()) {
352 if (pmemAddr)
353 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
354 TRACE_PACKET("Write");
355 pkt->makeAtomicResponse();
356 } else if (pkt->isPrint()) {
357 Packet::PrintReqState *prs =
358 dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
359 // Need to call printLabels() explicitly since we're not going
360 // through printObj().
361 prs->printLabels();
362 // Right now we just print the single byte at the specified address.
363 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
364 } else {
365 panic("PhysicalMemory: unimplemented functional command %s",
366 pkt->cmdString());
367 }
368 }
369
370
371 Port *
372 PhysicalMemory::getPort(const std::string &if_name, int idx)
373 {
374 // Accept request for "functional" port for backwards compatibility
375 // with places where this function is called from C++. I'd prefer
376 // to move all these into Python someday.
377 if (if_name == "functional") {
378 return new MemoryPort(csprintf("%s-functional", name()), this);
379 }
380
381 if (if_name != "port") {
382 panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
383 }
384
385 if (idx >= (int)ports.size()) {
386 ports.resize(idx + 1);
387 }
388
389 if (ports[idx] != NULL) {
390 panic("PhysicalMemory::getPort: port %d already assigned", idx);
391 }
392
393 MemoryPort *port =
394 new MemoryPort(csprintf("%s-port%d", name(), idx), this);
395
396 ports[idx] = port;
397 return port;
398 }
399
400
401 void
402 PhysicalMemory::recvStatusChange(Port::Status status)
403 {
404 }
405
406 PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
407 PhysicalMemory *_memory)
408 : SimpleTimingPort(_name, _memory), memory(_memory)
409 { }
410
411 void
412 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
413 {
414 memory->recvStatusChange(status);
415 }
416
417 void
418 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
419 bool &snoop)
420 {
421 memory->getAddressRanges(resp, snoop);
422 }
423
424 void
425 PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
426 {
427 snoop = false;
428 resp.clear();
429 resp.push_back(RangeSize(start(), size()));
430 }
431
432 unsigned
433 PhysicalMemory::MemoryPort::deviceBlockSize() const
434 {
435 return memory->deviceBlockSize();
436 }
437
438 Tick
439 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
440 {
441 return memory->doAtomicAccess(pkt);
442 }
443
444 void
445 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
446 {
447 pkt->pushLabel(memory->name());
448
449 if (!checkFunctional(pkt)) {
450 // Default implementation of SimpleTimingPort::recvFunctional()
451 // calls recvAtomic() and throws away the latency; we can save a
452 // little here by just not calculating the latency.
453 memory->doFunctionalAccess(pkt);
454 }
455
456 pkt->popLabel();
457 }
458
459 unsigned int
460 PhysicalMemory::drain(Event *de)
461 {
462 int count = 0;
463 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
464 count += (*pi)->drain(de);
465 }
466
467 if (count)
468 changeState(Draining);
469 else
470 changeState(Drained);
471 return count;
472 }
473
474 void
475 PhysicalMemory::serialize(ostream &os)
476 {
477 if (!pmemAddr)
478 return;
479
480 gzFile compressedMem;
481 string filename = name() + ".physmem";
482
483 SERIALIZE_SCALAR(filename);
484 SERIALIZE_SCALAR(_size);
485
486 // write memory file
487 string thefile = Checkpoint::dir() + "/" + filename.c_str();
488 int fd = creat(thefile.c_str(), 0664);
489 if (fd < 0) {
490 perror("creat");
491 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
492 }
493
494 compressedMem = gzdopen(fd, "wb");
495 if (compressedMem == NULL)
496 fatal("Insufficient memory to allocate compression state for %s\n",
497 filename);
498
499 if (gzwrite(compressedMem, pmemAddr, size()) != (int)size()) {
500 fatal("Write failed on physical memory checkpoint file '%s'\n",
501 filename);
502 }
503
504 if (gzclose(compressedMem))
505 fatal("Close failed on physical memory checkpoint file '%s'\n",
506 filename);
507
508 list<LockedAddr>::iterator i = lockedAddrList.begin();
509
510 vector<Addr> lal_addr;
511 vector<int> lal_cid;
512 while (i != lockedAddrList.end()) {
513 lal_addr.push_back(i->addr);
514 lal_cid.push_back(i->contextId);
515 i++;
516 }
517 arrayParamOut(os, "lal_addr", lal_addr);
518 arrayParamOut(os, "lal_cid", lal_cid);
519 }
520
521 void
522 PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
523 {
524 if (!pmemAddr)
525 return;
526
527 gzFile compressedMem;
528 long *tempPage;
529 long *pmem_current;
530 uint64_t curSize;
531 uint32_t bytesRead;
532 const uint32_t chunkSize = 16384;
533
534 string filename;
535
536 UNSERIALIZE_SCALAR(filename);
537
538 filename = cp->cptDir + "/" + filename;
539
540 // mmap memoryfile
541 int fd = open(filename.c_str(), O_RDONLY);
542 if (fd < 0) {
543 perror("open");
544 fatal("Can't open physical memory checkpoint file '%s'", filename);
545 }
546
547 compressedMem = gzdopen(fd, "rb");
548 if (compressedMem == NULL)
549 fatal("Insufficient memory to allocate compression state for %s\n",
550 filename);
551
552 // unmap file that was mmapped in the constructor
553 // This is done here to make sure that gzip and open don't muck with our
554 // nice large space of memory before we reallocate it
555 munmap((char*)pmemAddr, size());
556
557 UNSERIALIZE_SCALAR(_size);
558 if (size() > params()->range.size())
559 fatal("Memory size has changed! size %lld, param size %lld\n",
560 size(), params()->range.size());
561
562 pmemAddr = (uint8_t *)mmap(NULL, size(),
563 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
564
565 if (pmemAddr == (void *)MAP_FAILED) {
566 perror("mmap");
567 fatal("Could not mmap physical memory!\n");
568 }
569
570 curSize = 0;
571 tempPage = (long*)malloc(chunkSize);
572 if (tempPage == NULL)
573 fatal("Unable to malloc memory to read file %s\n", filename);
574
575 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
576 while (curSize < size()) {
577 bytesRead = gzread(compressedMem, tempPage, chunkSize);
578 if (bytesRead == 0)
579 break;
580
581 assert(bytesRead % sizeof(long) == 0);
582
583 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
584 {
585 if (*(tempPage+x) != 0) {
586 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
587 *pmem_current = *(tempPage+x);
588 }
589 }
590 curSize += bytesRead;
591 }
592
593 free(tempPage);
594
595 if (gzclose(compressedMem))
596 fatal("Close failed on physical memory checkpoint file '%s'\n",
597 filename);
598
599 vector<Addr> lal_addr;
600 vector<int> lal_cid;
601 arrayParamIn(cp, section, "lal_addr", lal_addr);
602 arrayParamIn(cp, section, "lal_cid", lal_cid);
603 for(int i = 0; i < lal_addr.size(); i++)
604 lockedAddrList.push_front(LockedAddr(lal_addr[i], lal_cid[i]));
605 }
606
607 PhysicalMemory *
608 PhysicalMemoryParams::create()
609 {
610 return new PhysicalMemory(this);
611 }