MEM: Move port creation to the memory object(s) construction
[gem5.git] / src / mem / physical.cc
1 /*
2 * Copyright (c) 2010-2011 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 // create the appropriate number of ports
80 for (int i = 0; i < p->port_port_connection_count; ++i) {
81 ports.push_back(new MemoryPort(csprintf("%s-port%d", name(), i),
82 this));
83 }
84
85 if (params()->null)
86 return;
87
88
89 if (params()->file == "") {
90 int map_flags = MAP_ANON | MAP_PRIVATE;
91 pmemAddr = (uint8_t *)mmap(NULL, size(),
92 PROT_READ | PROT_WRITE, map_flags, -1, 0);
93 } else {
94 int map_flags = MAP_PRIVATE;
95 int fd = open(params()->file.c_str(), O_RDONLY);
96 _size = lseek(fd, 0, SEEK_END);
97 lseek(fd, 0, SEEK_SET);
98 pmemAddr = (uint8_t *)mmap(NULL, roundUp(size(), sysconf(_SC_PAGESIZE)),
99 PROT_READ | PROT_WRITE, map_flags, fd, 0);
100 }
101
102 if (pmemAddr == (void *)MAP_FAILED) {
103 perror("mmap");
104 if (params()->file == "")
105 fatal("Could not mmap!\n");
106 else
107 fatal("Could not find file: %s\n", params()->file);
108 }
109
110 //If requested, initialize all the memory to 0
111 if (p->zero)
112 memset(pmemAddr, 0, size());
113 }
114
115 void
116 PhysicalMemory::init()
117 {
118 if (ports.empty()) {
119 fatal("PhysicalMemory object %s is unconnected!", name());
120 }
121
122 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
123 (*pi)->sendRangeChange();
124 }
125 }
126
127 PhysicalMemory::~PhysicalMemory()
128 {
129 if (pmemAddr)
130 munmap((char*)pmemAddr, size());
131 }
132
133 void
134 PhysicalMemory::regStats()
135 {
136 using namespace Stats;
137
138 bytesRead
139 .name(name() + ".bytes_read")
140 .desc("Number of bytes read from this memory")
141 ;
142 bytesInstRead
143 .name(name() + ".bytes_inst_read")
144 .desc("Number of instructions bytes read from this memory")
145 ;
146 bytesWritten
147 .name(name() + ".bytes_written")
148 .desc("Number of bytes written to this memory")
149 ;
150 numReads
151 .name(name() + ".num_reads")
152 .desc("Number of read requests responded to by this memory")
153 ;
154 numWrites
155 .name(name() + ".num_writes")
156 .desc("Number of write requests responded to by this memory")
157 ;
158 numOther
159 .name(name() + ".num_other")
160 .desc("Number of other requests responded to by this memory")
161 ;
162 bwRead
163 .name(name() + ".bw_read")
164 .desc("Total read bandwidth from this memory (bytes/s)")
165 .precision(0)
166 .prereq(bytesRead)
167 ;
168 bwInstRead
169 .name(name() + ".bw_inst_read")
170 .desc("Instruction read bandwidth from this memory (bytes/s)")
171 .precision(0)
172 .prereq(bytesInstRead)
173 ;
174 bwWrite
175 .name(name() + ".bw_write")
176 .desc("Write bandwidth from this memory (bytes/s)")
177 .precision(0)
178 .prereq(bytesWritten)
179 ;
180 bwTotal
181 .name(name() + ".bw_total")
182 .desc("Total bandwidth to/from this memory (bytes/s)")
183 .precision(0)
184 .prereq(bwTotal)
185 ;
186 bwRead = bytesRead / simSeconds;
187 bwInstRead = bytesInstRead / simSeconds;
188 bwWrite = bytesWritten / simSeconds;
189 bwTotal = (bytesRead + bytesWritten) / simSeconds;
190 }
191
192 unsigned
193 PhysicalMemory::deviceBlockSize() const
194 {
195 //Can accept anysize request
196 return 0;
197 }
198
199 Tick
200 PhysicalMemory::calculateLatency(PacketPtr pkt)
201 {
202 Tick latency = lat;
203 if (lat_var != 0)
204 latency += random_mt.random<Tick>(0, lat_var);
205 return latency;
206 }
207
208
209
210 // Add load-locked to tracking list. Should only be called if the
211 // operation is a load and the LLSC flag is set.
212 void
213 PhysicalMemory::trackLoadLocked(PacketPtr pkt)
214 {
215 Request *req = pkt->req;
216 Addr paddr = LockedAddr::mask(req->getPaddr());
217
218 // first we check if we already have a locked addr for this
219 // xc. Since each xc only gets one, we just update the
220 // existing record with the new address.
221 list<LockedAddr>::iterator i;
222
223 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
224 if (i->matchesContext(req)) {
225 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
226 req->contextId(), paddr);
227 i->addr = paddr;
228 return;
229 }
230 }
231
232 // no record for this xc: need to allocate a new one
233 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
234 req->contextId(), paddr);
235 lockedAddrList.push_front(LockedAddr(req));
236 }
237
238
239 // Called on *writes* only... both regular stores and
240 // store-conditional operations. Check for conventional stores which
241 // conflict with locked addresses, and for success/failure of store
242 // conditionals.
243 bool
244 PhysicalMemory::checkLockedAddrList(PacketPtr pkt)
245 {
246 Request *req = pkt->req;
247 Addr paddr = LockedAddr::mask(req->getPaddr());
248 bool isLLSC = pkt->isLLSC();
249
250 // Initialize return value. Non-conditional stores always
251 // succeed. Assume conditional stores will fail until proven
252 // otherwise.
253 bool success = !isLLSC;
254
255 // Iterate over list. Note that there could be multiple matching
256 // records, as more than one context could have done a load locked
257 // to this location.
258 list<LockedAddr>::iterator i = lockedAddrList.begin();
259
260 while (i != lockedAddrList.end()) {
261
262 if (i->addr == paddr) {
263 // we have a matching address
264
265 if (isLLSC && i->matchesContext(req)) {
266 // it's a store conditional, and as far as the memory
267 // system can tell, the requesting context's lock is
268 // still valid.
269 DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
270 req->contextId(), paddr);
271 success = true;
272 }
273
274 // Get rid of our record of this lock and advance to next
275 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
276 i->contextId, paddr);
277 i = lockedAddrList.erase(i);
278 }
279 else {
280 // no match: advance to next record
281 ++i;
282 }
283 }
284
285 if (isLLSC) {
286 req->setExtraData(success ? 1 : 0);
287 }
288
289 return success;
290 }
291
292
293 #if TRACING_ON
294
295 #define CASE(A, T) \
296 case sizeof(T): \
297 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
298 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
299 break
300
301
302 #define TRACE_PACKET(A) \
303 do { \
304 switch (pkt->getSize()) { \
305 CASE(A, uint64_t); \
306 CASE(A, uint32_t); \
307 CASE(A, uint16_t); \
308 CASE(A, uint8_t); \
309 default: \
310 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
311 A, pkt->getSize(), pkt->getAddr()); \
312 DDUMP(MemoryAccess, pkt->getPtr<uint8_t>(), pkt->getSize());\
313 } \
314 } while (0)
315
316 #else
317
318 #define TRACE_PACKET(A)
319
320 #endif
321
322 Tick
323 PhysicalMemory::doAtomicAccess(PacketPtr pkt)
324 {
325 assert(pkt->getAddr() >= start() &&
326 pkt->getAddr() + pkt->getSize() <= start() + size());
327
328 if (pkt->memInhibitAsserted()) {
329 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
330 pkt->getAddr());
331 return 0;
332 }
333
334 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
335
336 if (pkt->cmd == MemCmd::SwapReq) {
337 IntReg overwrite_val;
338 bool overwrite_mem;
339 uint64_t condition_val64;
340 uint32_t condition_val32;
341
342 if (!pmemAddr)
343 panic("Swap only works if there is real memory (i.e. null=False)");
344 assert(sizeof(IntReg) >= pkt->getSize());
345
346 overwrite_mem = true;
347 // keep a copy of our possible write value, and copy what is at the
348 // memory address into the packet
349 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
350 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
351
352 if (pkt->req->isCondSwap()) {
353 if (pkt->getSize() == sizeof(uint64_t)) {
354 condition_val64 = pkt->req->getExtraData();
355 overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
356 sizeof(uint64_t));
357 } else if (pkt->getSize() == sizeof(uint32_t)) {
358 condition_val32 = (uint32_t)pkt->req->getExtraData();
359 overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
360 sizeof(uint32_t));
361 } else
362 panic("Invalid size for conditional read/write\n");
363 }
364
365 if (overwrite_mem)
366 std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
367
368 assert(!pkt->req->isInstFetch());
369 TRACE_PACKET("Read/Write");
370 numOther++;
371 } else if (pkt->isRead()) {
372 assert(!pkt->isWrite());
373 if (pkt->isLLSC()) {
374 trackLoadLocked(pkt);
375 }
376 if (pmemAddr)
377 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
378 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
379 numReads++;
380 bytesRead += pkt->getSize();
381 if (pkt->req->isInstFetch())
382 bytesInstRead += pkt->getSize();
383 } else if (pkt->isWrite()) {
384 if (writeOK(pkt)) {
385 if (pmemAddr)
386 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
387 assert(!pkt->req->isInstFetch());
388 TRACE_PACKET("Write");
389 numWrites++;
390 bytesWritten += pkt->getSize();
391 }
392 } else if (pkt->isInvalidate()) {
393 //upgrade or invalidate
394 if (pkt->needsResponse()) {
395 pkt->makeAtomicResponse();
396 }
397 } else {
398 panic("unimplemented");
399 }
400
401 if (pkt->needsResponse()) {
402 pkt->makeAtomicResponse();
403 }
404 return calculateLatency(pkt);
405 }
406
407
408 void
409 PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
410 {
411 assert(pkt->getAddr() >= start() &&
412 pkt->getAddr() + pkt->getSize() <= start() + size());
413
414
415 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
416
417 if (pkt->isRead()) {
418 if (pmemAddr)
419 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
420 TRACE_PACKET("Read");
421 pkt->makeAtomicResponse();
422 } else if (pkt->isWrite()) {
423 if (pmemAddr)
424 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
425 TRACE_PACKET("Write");
426 pkt->makeAtomicResponse();
427 } else if (pkt->isPrint()) {
428 Packet::PrintReqState *prs =
429 dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
430 // Need to call printLabels() explicitly since we're not going
431 // through printObj().
432 prs->printLabels();
433 // Right now we just print the single byte at the specified address.
434 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
435 } else {
436 panic("PhysicalMemory: unimplemented functional command %s",
437 pkt->cmdString());
438 }
439 }
440
441
442 Port *
443 PhysicalMemory::getPort(const std::string &if_name, int idx)
444 {
445 if (if_name != "port") {
446 panic("PhysicalMemory::getPort: unknown port %s requested\n", if_name);
447 }
448
449 if (idx >= static_cast<int>(ports.size())) {
450 panic("PhysicalMemory::getPort: unknown index %d requested\n", idx);
451 }
452
453 return ports[idx];
454 }
455
456 PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
457 PhysicalMemory *_memory)
458 : SimpleTimingPort(_name, _memory), memory(_memory)
459 { }
460
461 void
462 PhysicalMemory::MemoryPort::recvRangeChange()
463 {
464 // memory is a slave and thus should never have to worry about its
465 // neighbours address ranges
466 }
467
468 AddrRangeList
469 PhysicalMemory::MemoryPort::getAddrRanges()
470 {
471 return memory->getAddrRanges();
472 }
473
474 AddrRangeList
475 PhysicalMemory::getAddrRanges()
476 {
477 AddrRangeList ranges;
478 ranges.push_back(RangeSize(start(), size()));
479 return ranges;
480 }
481
482 unsigned
483 PhysicalMemory::MemoryPort::deviceBlockSize() const
484 {
485 return memory->deviceBlockSize();
486 }
487
488 Tick
489 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
490 {
491 return memory->doAtomicAccess(pkt);
492 }
493
494 void
495 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
496 {
497 pkt->pushLabel(memory->name());
498
499 if (!checkFunctional(pkt)) {
500 // Default implementation of SimpleTimingPort::recvFunctional()
501 // calls recvAtomic() and throws away the latency; we can save a
502 // little here by just not calculating the latency.
503 memory->doFunctionalAccess(pkt);
504 }
505
506 pkt->popLabel();
507 }
508
509 unsigned int
510 PhysicalMemory::drain(Event *de)
511 {
512 int count = 0;
513 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
514 count += (*pi)->drain(de);
515 }
516
517 if (count)
518 changeState(Draining);
519 else
520 changeState(Drained);
521 return count;
522 }
523
524 void
525 PhysicalMemory::serialize(ostream &os)
526 {
527 if (!pmemAddr)
528 return;
529
530 gzFile compressedMem;
531 string filename = name() + ".physmem";
532
533 SERIALIZE_SCALAR(filename);
534 SERIALIZE_SCALAR(_size);
535
536 // write memory file
537 string thefile = Checkpoint::dir() + "/" + filename.c_str();
538 int fd = creat(thefile.c_str(), 0664);
539 if (fd < 0) {
540 perror("creat");
541 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
542 }
543
544 compressedMem = gzdopen(fd, "wb");
545 if (compressedMem == NULL)
546 fatal("Insufficient memory to allocate compression state for %s\n",
547 filename);
548
549 if (gzwrite(compressedMem, pmemAddr, size()) != (int)size()) {
550 fatal("Write failed on physical memory checkpoint file '%s'\n",
551 filename);
552 }
553
554 if (gzclose(compressedMem))
555 fatal("Close failed on physical memory checkpoint file '%s'\n",
556 filename);
557
558 list<LockedAddr>::iterator i = lockedAddrList.begin();
559
560 vector<Addr> lal_addr;
561 vector<int> lal_cid;
562 while (i != lockedAddrList.end()) {
563 lal_addr.push_back(i->addr);
564 lal_cid.push_back(i->contextId);
565 i++;
566 }
567 arrayParamOut(os, "lal_addr", lal_addr);
568 arrayParamOut(os, "lal_cid", lal_cid);
569 }
570
571 void
572 PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
573 {
574 if (!pmemAddr)
575 return;
576
577 gzFile compressedMem;
578 long *tempPage;
579 long *pmem_current;
580 uint64_t curSize;
581 uint32_t bytesRead;
582 const uint32_t chunkSize = 16384;
583
584 string filename;
585
586 UNSERIALIZE_SCALAR(filename);
587
588 filename = cp->cptDir + "/" + filename;
589
590 // mmap memoryfile
591 int fd = open(filename.c_str(), O_RDONLY);
592 if (fd < 0) {
593 perror("open");
594 fatal("Can't open physical memory checkpoint file '%s'", filename);
595 }
596
597 compressedMem = gzdopen(fd, "rb");
598 if (compressedMem == NULL)
599 fatal("Insufficient memory to allocate compression state for %s\n",
600 filename);
601
602 // unmap file that was mmapped in the constructor
603 // This is done here to make sure that gzip and open don't muck with our
604 // nice large space of memory before we reallocate it
605 munmap((char*)pmemAddr, size());
606
607 UNSERIALIZE_SCALAR(_size);
608 if (size() > params()->range.size())
609 fatal("Memory size has changed! size %lld, param size %lld\n",
610 size(), params()->range.size());
611
612 pmemAddr = (uint8_t *)mmap(NULL, size(),
613 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
614
615 if (pmemAddr == (void *)MAP_FAILED) {
616 perror("mmap");
617 fatal("Could not mmap physical memory!\n");
618 }
619
620 curSize = 0;
621 tempPage = (long*)malloc(chunkSize);
622 if (tempPage == NULL)
623 fatal("Unable to malloc memory to read file %s\n", filename);
624
625 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
626 while (curSize < size()) {
627 bytesRead = gzread(compressedMem, tempPage, chunkSize);
628 if (bytesRead == 0)
629 break;
630
631 assert(bytesRead % sizeof(long) == 0);
632
633 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
634 {
635 if (*(tempPage+x) != 0) {
636 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
637 *pmem_current = *(tempPage+x);
638 }
639 }
640 curSize += bytesRead;
641 }
642
643 free(tempPage);
644
645 if (gzclose(compressedMem))
646 fatal("Close failed on physical memory checkpoint file '%s'\n",
647 filename);
648
649 vector<Addr> lal_addr;
650 vector<int> lal_cid;
651 arrayParamIn(cp, section, "lal_addr", lal_addr);
652 arrayParamIn(cp, section, "lal_cid", lal_cid);
653 for(int i = 0; i < lal_addr.size(); i++)
654 lockedAddrList.push_front(LockedAddr(lal_addr[i], lal_cid[i]));
655 }
656
657 PhysicalMemory *
658 PhysicalMemoryParams::create()
659 {
660 return new PhysicalMemory(this);
661 }