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