2 * Copyright (c) 2010 ARM Limited
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.
14 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
40 * Authors: Ron Dreslinski
45 #include <sys/types.h>
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/full_system.hh"
63 #include "config/the_isa.hh"
64 #include "debug/LLSC.hh"
65 #include "debug/MemoryAccess.hh"
66 #include "mem/packet_access.hh"
67 #include "mem/physical.hh"
68 #include "sim/eventq.hh"
71 using namespace TheISA
;
73 PhysicalMemory::PhysicalMemory(const Params
*p
)
74 : MemObject(p
), pmemAddr(NULL
), lat(p
->latency
), lat_var(p
->latency_var
),
75 _size(params()->range
.size()), _start(params()->range
.start
)
77 if (size() % TheISA::PageBytes
!= 0)
78 panic("Memory Size not divisible by page size\n");
84 if (params()->file
== "") {
85 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
86 pmemAddr
= (uint8_t *)mmap(NULL
, size(),
87 PROT_READ
| PROT_WRITE
, map_flags
, -1, 0);
89 int map_flags
= MAP_PRIVATE
;
90 int fd
= open(params()->file
.c_str(), O_RDONLY
);
91 _size
= lseek(fd
, 0, SEEK_END
);
92 lseek(fd
, 0, SEEK_SET
);
93 pmemAddr
= (uint8_t *)mmap(NULL
, roundUp(size(), sysconf(_SC_PAGESIZE
)),
94 PROT_READ
| PROT_WRITE
, map_flags
, fd
, 0);
97 if (pmemAddr
== (void *)MAP_FAILED
) {
99 if (params()->file
== "")
100 fatal("Could not mmap!\n");
102 fatal("Could not find file: %s\n", params()->file
);
105 //If requested, initialize all the memory to 0
107 memset(pmemAddr
, 0, size());
111 PhysicalMemory::init()
113 if (ports
.size() == 0) {
114 fatal("PhysicalMemory object %s is unconnected!", name());
117 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
119 (*pi
)->sendStatusChange(Port::RangeChange
);
123 PhysicalMemory::~PhysicalMemory()
126 munmap((char*)pmemAddr
, size());
130 PhysicalMemory::deviceBlockSize() const
132 //Can accept anysize request
137 PhysicalMemory::calculateLatency(PacketPtr pkt
)
141 latency
+= random_mt
.random
<Tick
>(0, lat_var
);
147 // Add load-locked to tracking list. Should only be called if the
148 // operation is a load and the LLSC flag is set.
150 PhysicalMemory::trackLoadLocked(PacketPtr pkt
)
152 Request
*req
= pkt
->req
;
153 Addr paddr
= LockedAddr::mask(req
->getPaddr());
155 // first we check if we already have a locked addr for this
156 // xc. Since each xc only gets one, we just update the
157 // existing record with the new address.
158 list
<LockedAddr
>::iterator i
;
160 for (i
= lockedAddrList
.begin(); i
!= lockedAddrList
.end(); ++i
) {
161 if (i
->matchesContext(req
)) {
162 DPRINTF(LLSC
, "Modifying lock record: context %d addr %#x\n",
163 req
->contextId(), paddr
);
169 // no record for this xc: need to allocate a new one
170 DPRINTF(LLSC
, "Adding lock record: context %d addr %#x\n",
171 req
->contextId(), paddr
);
172 lockedAddrList
.push_front(LockedAddr(req
));
176 // Called on *writes* only... both regular stores and
177 // store-conditional operations. Check for conventional stores which
178 // conflict with locked addresses, and for success/failure of store
181 PhysicalMemory::checkLockedAddrList(PacketPtr pkt
)
183 Request
*req
= pkt
->req
;
184 Addr paddr
= LockedAddr::mask(req
->getPaddr());
185 bool isLLSC
= pkt
->isLLSC();
187 // Initialize return value. Non-conditional stores always
188 // succeed. Assume conditional stores will fail until proven
190 bool success
= !isLLSC
;
192 // Iterate over list. Note that there could be multiple matching
193 // records, as more than one context could have done a load locked
195 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
197 while (i
!= lockedAddrList
.end()) {
199 if (i
->addr
== paddr
) {
200 // we have a matching address
202 if (isLLSC
&& i
->matchesContext(req
)) {
203 // it's a store conditional, and as far as the memory
204 // system can tell, the requesting context's lock is
206 DPRINTF(LLSC
, "StCond success: context %d addr %#x\n",
207 req
->contextId(), paddr
);
211 // Get rid of our record of this lock and advance to next
212 DPRINTF(LLSC
, "Erasing lock record: context %d addr %#x\n",
213 i
->contextId
, paddr
);
214 i
= lockedAddrList
.erase(i
);
217 // no match: advance to next record
223 req
->setExtraData(success
? 1 : 0);
234 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
235 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
239 #define TRACE_PACKET(A) \
241 switch (pkt->getSize()) { \
247 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
248 A, pkt->getSize(), pkt->getAddr()); \
249 DDUMP(MemoryAccess, pkt->getPtr<uint8_t>(), pkt->getSize());\
255 #define TRACE_PACKET(A)
260 PhysicalMemory::doAtomicAccess(PacketPtr pkt
)
262 assert(pkt
->getAddr() >= start() &&
263 pkt
->getAddr() + pkt
->getSize() <= start() + size());
265 if (pkt
->memInhibitAsserted()) {
266 DPRINTF(MemoryAccess
, "mem inhibited on 0x%x: not responding\n",
271 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
273 if (pkt
->cmd
== MemCmd::SwapReq
) {
274 IntReg overwrite_val
;
276 uint64_t condition_val64
;
277 uint32_t condition_val32
;
280 panic("Swap only works if there is real memory (i.e. null=False)");
281 assert(sizeof(IntReg
) >= pkt
->getSize());
283 overwrite_mem
= true;
284 // keep a copy of our possible write value, and copy what is at the
285 // memory address into the packet
286 std::memcpy(&overwrite_val
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
287 std::memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
289 if (pkt
->req
->isCondSwap()) {
290 if (pkt
->getSize() == sizeof(uint64_t)) {
291 condition_val64
= pkt
->req
->getExtraData();
292 overwrite_mem
= !std::memcmp(&condition_val64
, hostAddr
,
294 } else if (pkt
->getSize() == sizeof(uint32_t)) {
295 condition_val32
= (uint32_t)pkt
->req
->getExtraData();
296 overwrite_mem
= !std::memcmp(&condition_val32
, hostAddr
,
299 panic("Invalid size for conditional read/write\n");
303 std::memcpy(hostAddr
, &overwrite_val
, pkt
->getSize());
305 assert(!pkt
->req
->isInstFetch());
306 TRACE_PACKET("Read/Write");
307 } else if (pkt
->isRead()) {
308 assert(!pkt
->isWrite());
310 trackLoadLocked(pkt
);
313 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
314 TRACE_PACKET(pkt
->req
->isInstFetch() ? "IFetch" : "Read");
315 } else if (pkt
->isWrite()) {
318 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
319 assert(!pkt
->req
->isInstFetch());
320 TRACE_PACKET("Write");
322 } else if (pkt
->isInvalidate()) {
323 //upgrade or invalidate
324 if (pkt
->needsResponse()) {
325 pkt
->makeAtomicResponse();
328 panic("unimplemented");
331 if (pkt
->needsResponse()) {
332 pkt
->makeAtomicResponse();
334 return calculateLatency(pkt
);
339 PhysicalMemory::doFunctionalAccess(PacketPtr pkt
)
341 assert(pkt
->getAddr() >= start() &&
342 pkt
->getAddr() + pkt
->getSize() <= start() + size());
345 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
349 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
350 TRACE_PACKET("Read");
351 pkt
->makeAtomicResponse();
352 } else if (pkt
->isWrite()) {
354 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
355 TRACE_PACKET("Write");
356 pkt
->makeAtomicResponse();
357 } else if (pkt
->isPrint()) {
358 Packet::PrintReqState
*prs
=
359 dynamic_cast<Packet::PrintReqState
*>(pkt
->senderState
);
360 // Need to call printLabels() explicitly since we're not going
361 // through printObj().
363 // Right now we just print the single byte at the specified address.
364 ccprintf(prs
->os
, "%s%#x\n", prs
->curPrefix(), *hostAddr
);
366 panic("PhysicalMemory: unimplemented functional command %s",
373 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
375 // Accept request for "functional" port for backwards compatibility
376 // with places where this function is called from C++. I'd prefer
377 // to move all these into Python someday.
378 if (if_name
== "functional") {
379 return new MemoryPort(csprintf("%s-functional", name()), this);
382 if (if_name
!= "port") {
383 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
386 if (idx
>= (int)ports
.size()) {
387 ports
.resize(idx
+ 1);
390 if (ports
[idx
] != NULL
) {
391 panic("PhysicalMemory::getPort: port %d already assigned", idx
);
395 new MemoryPort(csprintf("%s-port%d", name(), idx
), this);
403 PhysicalMemory::recvStatusChange(Port::Status status
)
407 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
408 PhysicalMemory
*_memory
)
409 : SimpleTimingPort(_name
, _memory
), memory(_memory
)
413 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
415 memory
->recvStatusChange(status
);
419 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
422 memory
->getAddressRanges(resp
, snoop
);
426 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, bool &snoop
)
430 resp
.push_back(RangeSize(start(), size()));
434 PhysicalMemory::MemoryPort::deviceBlockSize() const
436 return memory
->deviceBlockSize();
440 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
442 return memory
->doAtomicAccess(pkt
);
446 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
448 pkt
->pushLabel(memory
->name());
450 if (!checkFunctional(pkt
)) {
451 // Default implementation of SimpleTimingPort::recvFunctional()
452 // calls recvAtomic() and throws away the latency; we can save a
453 // little here by just not calculating the latency.
454 memory
->doFunctionalAccess(pkt
);
461 PhysicalMemory::drain(Event
*de
)
464 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
465 count
+= (*pi
)->drain(de
);
469 changeState(Draining
);
471 changeState(Drained
);
476 PhysicalMemory::serialize(ostream
&os
)
481 gzFile compressedMem
;
482 string filename
= name() + ".physmem";
484 SERIALIZE_SCALAR(filename
);
485 SERIALIZE_SCALAR(_size
);
488 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
489 int fd
= creat(thefile
.c_str(), 0664);
492 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
495 compressedMem
= gzdopen(fd
, "wb");
496 if (compressedMem
== NULL
)
497 fatal("Insufficient memory to allocate compression state for %s\n",
500 if (gzwrite(compressedMem
, pmemAddr
, size()) != (int)size()) {
501 fatal("Write failed on physical memory checkpoint file '%s'\n",
505 if (gzclose(compressedMem
))
506 fatal("Close failed on physical memory checkpoint file '%s'\n",
509 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
511 vector
<Addr
> lal_addr
;
513 while (i
!= lockedAddrList
.end()) {
514 lal_addr
.push_back(i
->addr
);
515 lal_cid
.push_back(i
->contextId
);
518 arrayParamOut(os
, "lal_addr", lal_addr
);
519 arrayParamOut(os
, "lal_cid", lal_cid
);
523 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
528 gzFile compressedMem
;
533 const uint32_t chunkSize
= 16384;
537 UNSERIALIZE_SCALAR(filename
);
539 filename
= cp
->cptDir
+ "/" + filename
;
542 int fd
= open(filename
.c_str(), O_RDONLY
);
545 fatal("Can't open physical memory checkpoint file '%s'", filename
);
548 compressedMem
= gzdopen(fd
, "rb");
549 if (compressedMem
== NULL
)
550 fatal("Insufficient memory to allocate compression state for %s\n",
553 // unmap file that was mmapped in the constructor
554 // This is done here to make sure that gzip and open don't muck with our
555 // nice large space of memory before we reallocate it
556 munmap((char*)pmemAddr
, size());
558 UNSERIALIZE_SCALAR(_size
);
559 if (size() > params()->range
.size())
560 fatal("Memory size has changed! size %lld, param size %lld\n",
561 size(), params()->range
.size());
563 pmemAddr
= (uint8_t *)mmap(NULL
, size(),
564 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
566 if (pmemAddr
== (void *)MAP_FAILED
) {
568 fatal("Could not mmap physical memory!\n");
572 tempPage
= (long*)malloc(chunkSize
);
573 if (tempPage
== NULL
)
574 fatal("Unable to malloc memory to read file %s\n", filename
);
576 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
577 while (curSize
< size()) {
578 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
582 assert(bytesRead
% sizeof(long) == 0);
584 for (uint32_t x
= 0; x
< bytesRead
/ sizeof(long); x
++)
586 if (*(tempPage
+x
) != 0) {
587 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
588 *pmem_current
= *(tempPage
+x
);
591 curSize
+= bytesRead
;
596 if (gzclose(compressedMem
))
597 fatal("Close failed on physical memory checkpoint file '%s'\n",
600 vector
<Addr
> lal_addr
;
602 arrayParamIn(cp
, section
, "lal_addr", lal_addr
);
603 arrayParamIn(cp
, section
, "lal_cid", lal_cid
);
604 for(int i
= 0; i
< lal_addr
.size(); i
++)
605 lockedAddrList
.push_front(LockedAddr(lal_addr
[i
], lal_cid
[i
]));
609 PhysicalMemoryParams::create()
611 return new PhysicalMemory(this);