2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Ron Dreslinski
32 #include <sys/types.h>
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"
52 using namespace TheISA
;
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
)
59 if (params()->range
.size() % TheISA::PageBytes
!= 0)
60 panic("Memory Size not divisible by page size\n");
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);
69 if (pmemAddr
== (void *)MAP_FAILED
) {
71 fatal("Could not mmap!\n");
74 //If requested, initialize all the memory to 0
76 memset(pmemAddr
, 0, p
->range
.size());
80 PhysicalMemory::init()
82 if (ports
.size() == 0) {
83 fatal("PhysicalMemory object %s is unconnected!", name());
86 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
88 (*pi
)->sendStatusChange(Port::RangeChange
);
92 PhysicalMemory::~PhysicalMemory()
95 munmap((char*)pmemAddr
, params()->range
.size());
100 PhysicalMemory::new_page()
102 Addr return_addr
= pagePtr
<< LogVMPageSize
;
103 return_addr
+= start();
110 PhysicalMemory::deviceBlockSize() const
112 //Can accept anysize request
117 PhysicalMemory::calculateLatency(PacketPtr pkt
)
121 latency
+= random_mt
.random
<Tick
>(0, lat_var
);
127 // Add load-locked to tracking list. Should only be called if the
128 // operation is a load and the LLSC flag is set.
130 PhysicalMemory::trackLoadLocked(PacketPtr pkt
)
132 Request
*req
= pkt
->req
;
133 Addr paddr
= LockedAddr::mask(req
->getPaddr());
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
;
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
);
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
));
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
161 PhysicalMemory::checkLockedAddrList(PacketPtr pkt
)
163 Request
*req
= pkt
->req
;
164 Addr paddr
= LockedAddr::mask(req
->getPaddr());
165 bool isLLSC
= pkt
->isLLSC();
167 // Initialize return value. Non-conditional stores always
168 // succeed. Assume conditional stores will fail until proven
170 bool success
= !isLLSC
;
172 // Iterate over list. Note that there could be multiple matching
173 // records, as more than one context could have done a load locked
175 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
177 while (i
!= lockedAddrList
.end()) {
179 if (i
->addr
== paddr
) {
180 // we have a matching address
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
186 DPRINTF(LLSC
, "StCond success: context %d addr %#x\n",
187 req
->contextId(), paddr
);
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
);
197 // no match: advance to next record
203 req
->setExtraData(success
? 1 : 0);
214 DPRINTF(MemoryAccess, A " of size %i on address 0x%x data 0x%x\n", \
215 pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
219 #define TRACE_PACKET(A) \
221 switch (pkt->getSize()) { \
227 DPRINTF(MemoryAccess, A " of size %i on address 0x%x\n", \
228 pkt->getSize(), pkt->getAddr()); \
234 #define TRACE_PACKET(A)
239 PhysicalMemory::doAtomicAccess(PacketPtr pkt
)
241 assert(pkt
->getAddr() >= start() &&
242 pkt
->getAddr() + pkt
->getSize() <= start() + size());
244 if (pkt
->memInhibitAsserted()) {
245 DPRINTF(MemoryAccess
, "mem inhibited on 0x%x: not responding\n",
250 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
252 if (pkt
->cmd
== MemCmd::SwapReq
) {
253 IntReg overwrite_val
;
255 uint64_t condition_val64
;
256 uint32_t condition_val32
;
259 panic("Swap only works if there is real memory (i.e. null=False)");
260 assert(sizeof(IntReg
) >= pkt
->getSize());
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());
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
,
273 } else if (pkt
->getSize() == sizeof(uint32_t)) {
274 condition_val32
= (uint32_t)pkt
->req
->getExtraData();
275 overwrite_mem
= !std::memcmp(&condition_val32
, hostAddr
,
278 panic("Invalid size for conditional read/write\n");
282 std::memcpy(hostAddr
, &overwrite_val
, pkt
->getSize());
284 TRACE_PACKET("Read/Write");
285 } else if (pkt
->isRead()) {
286 assert(!pkt
->isWrite());
288 trackLoadLocked(pkt
);
291 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
292 TRACE_PACKET("Read");
293 } else if (pkt
->isWrite()) {
296 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
297 TRACE_PACKET("Write");
299 } else if (pkt
->isInvalidate()) {
300 //upgrade or invalidate
301 if (pkt
->needsResponse()) {
302 pkt
->makeAtomicResponse();
305 panic("unimplemented");
308 if (pkt
->needsResponse()) {
309 pkt
->makeAtomicResponse();
311 return calculateLatency(pkt
);
316 PhysicalMemory::doFunctionalAccess(PacketPtr pkt
)
318 assert(pkt
->getAddr() >= start() &&
319 pkt
->getAddr() + pkt
->getSize() <= start() + size());
322 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
326 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
327 TRACE_PACKET("Read");
328 pkt
->makeAtomicResponse();
329 } else if (pkt
->isWrite()) {
331 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
332 TRACE_PACKET("Write");
333 pkt
->makeAtomicResponse();
334 } else if (pkt
->isPrint()) {
335 Packet::PrintReqState
*prs
=
336 dynamic_cast<Packet::PrintReqState
*>(pkt
->senderState
);
337 // Need to call printLabels() explicitly since we're not going
338 // through printObj().
340 // Right now we just print the single byte at the specified address.
341 ccprintf(prs
->os
, "%s%#x\n", prs
->curPrefix(), *hostAddr
);
343 panic("PhysicalMemory: unimplemented functional command %s",
350 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
352 // Accept request for "functional" port for backwards compatibility
353 // with places where this function is called from C++. I'd prefer
354 // to move all these into Python someday.
355 if (if_name
== "functional") {
356 return new MemoryPort(csprintf("%s-functional", name()), this);
359 if (if_name
!= "port") {
360 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
363 if (idx
>= (int)ports
.size()) {
364 ports
.resize(idx
+ 1);
367 if (ports
[idx
] != NULL
) {
368 panic("PhysicalMemory::getPort: port %d already assigned", idx
);
372 new MemoryPort(csprintf("%s-port%d", name(), idx
), this);
380 PhysicalMemory::recvStatusChange(Port::Status status
)
384 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
385 PhysicalMemory
*_memory
)
386 : SimpleTimingPort(_name
, _memory
), memory(_memory
)
390 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
392 memory
->recvStatusChange(status
);
396 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
399 memory
->getAddressRanges(resp
, snoop
);
403 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, bool &snoop
)
407 resp
.push_back(RangeSize(start(), params()->range
.size()));
411 PhysicalMemory::MemoryPort::deviceBlockSize() const
413 return memory
->deviceBlockSize();
417 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
419 return memory
->doAtomicAccess(pkt
);
423 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
425 pkt
->pushLabel(memory
->name());
427 if (!checkFunctional(pkt
)) {
428 // Default implementation of SimpleTimingPort::recvFunctional()
429 // calls recvAtomic() and throws away the latency; we can save a
430 // little here by just not calculating the latency.
431 memory
->doFunctionalAccess(pkt
);
438 PhysicalMemory::drain(Event
*de
)
441 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
442 count
+= (*pi
)->drain(de
);
446 changeState(Draining
);
448 changeState(Drained
);
453 PhysicalMemory::serialize(ostream
&os
)
458 gzFile compressedMem
;
459 string filename
= name() + ".physmem";
461 SERIALIZE_SCALAR(filename
);
464 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
465 int fd
= creat(thefile
.c_str(), 0664);
468 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
471 compressedMem
= gzdopen(fd
, "wb");
472 if (compressedMem
== NULL
)
473 fatal("Insufficient memory to allocate compression state for %s\n",
476 if (gzwrite(compressedMem
, pmemAddr
, params()->range
.size()) !=
477 (int)params()->range
.size()) {
478 fatal("Write failed on physical memory checkpoint file '%s'\n",
482 if (gzclose(compressedMem
))
483 fatal("Close failed on physical memory checkpoint file '%s'\n",
488 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
493 gzFile compressedMem
;
498 const uint32_t chunkSize
= 16384;
502 UNSERIALIZE_SCALAR(filename
);
504 filename
= cp
->cptDir
+ "/" + filename
;
507 int fd
= open(filename
.c_str(), O_RDONLY
);
510 fatal("Can't open physical memory checkpoint file '%s'", filename
);
513 compressedMem
= gzdopen(fd
, "rb");
514 if (compressedMem
== NULL
)
515 fatal("Insufficient memory to allocate compression state for %s\n",
518 // unmap file that was mmaped in the constructor
519 // This is done here to make sure that gzip and open don't muck with our
520 // nice large space of memory before we reallocate it
521 munmap((char*)pmemAddr
, params()->range
.size());
523 pmemAddr
= (uint8_t *)mmap(NULL
, params()->range
.size(),
524 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
526 if (pmemAddr
== (void *)MAP_FAILED
) {
528 fatal("Could not mmap physical memory!\n");
532 tempPage
= (long*)malloc(chunkSize
);
533 if (tempPage
== NULL
)
534 fatal("Unable to malloc memory to read file %s\n", filename
);
536 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
537 while (curSize
< params()->range
.size()) {
538 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
539 if (bytesRead
!= chunkSize
&&
540 bytesRead
!= params()->range
.size() - curSize
)
541 fatal("Read failed on physical memory checkpoint file '%s'"
542 " got %d bytes, expected %d or %d bytes\n",
543 filename
, bytesRead
, chunkSize
,
544 params()->range
.size() - curSize
);
546 assert(bytesRead
% sizeof(long) == 0);
548 for (uint32_t x
= 0; x
< bytesRead
/ sizeof(long); x
++)
550 if (*(tempPage
+x
) != 0) {
551 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
552 *pmem_current
= *(tempPage
+x
);
555 curSize
+= bytesRead
;
560 if (gzclose(compressedMem
))
561 fatal("Close failed on physical memory checkpoint file '%s'\n",
567 PhysicalMemoryParams::create()
569 return new PhysicalMemory(this);