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/isa_traits.hh"
43 #include "base/misc.hh"
44 #include "config/full_system.hh"
45 #include "mem/packet_access.hh"
46 #include "mem/physical.hh"
47 #include "sim/builder.hh"
48 #include "sim/eventq.hh"
49 #include "sim/host.hh"
52 using namespace TheISA
;
54 PhysicalMemory::PhysicalMemory(Params
*p
)
55 : MemObject(p
->name
), pmemAddr(NULL
), lat(p
->latency
), _params(p
)
57 if (params()->addrRange
.size() % TheISA::PageBytes
!= 0)
58 panic("Memory Size not divisible by page size\n");
60 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
62 (uint8_t *)mmap(NULL
, params()->addrRange
.size(),
63 PROT_READ
| PROT_WRITE
, map_flags
, -1, 0);
65 if (pmemAddr
== (void *)MAP_FAILED
) {
67 fatal("Could not mmap!\n");
70 //If requested, initialize all the memory to 0
72 memset(pmemAddr
, 0, params()->addrRange
.size());
78 PhysicalMemory::init()
80 if (ports
.size() == 0) {
81 fatal("PhysicalMemory object %s is unconnected!", name());
84 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
86 (*pi
)->sendStatusChange(Port::RangeChange
);
90 PhysicalMemory::~PhysicalMemory()
93 munmap((char*)pmemAddr
, params()->addrRange
.size());
98 PhysicalMemory::new_page()
100 Addr return_addr
= pagePtr
<< LogVMPageSize
;
101 return_addr
+= start();
108 PhysicalMemory::deviceBlockSize()
110 //Can accept anysize request
115 PhysicalMemory::calculateLatency(PacketPtr pkt
)
122 // Add load-locked to tracking list. Should only be called if the
123 // operation is a load and the LOCKED flag is set.
125 PhysicalMemory::trackLoadLocked(PacketPtr pkt
)
127 Request
*req
= pkt
->req
;
128 Addr paddr
= LockedAddr::mask(req
->getPaddr());
130 // first we check if we already have a locked addr for this
131 // xc. Since each xc only gets one, we just update the
132 // existing record with the new address.
133 list
<LockedAddr
>::iterator i
;
135 for (i
= lockedAddrList
.begin(); i
!= lockedAddrList
.end(); ++i
) {
136 if (i
->matchesContext(req
)) {
137 DPRINTF(LLSC
, "Modifying lock record: cpu %d thread %d addr %#x\n",
138 req
->getCpuNum(), req
->getThreadNum(), paddr
);
144 // no record for this xc: need to allocate a new one
145 DPRINTF(LLSC
, "Adding lock record: cpu %d thread %d addr %#x\n",
146 req
->getCpuNum(), req
->getThreadNum(), paddr
);
147 lockedAddrList
.push_front(LockedAddr(req
));
151 // Called on *writes* only... both regular stores and
152 // store-conditional operations. Check for conventional stores which
153 // conflict with locked addresses, and for success/failure of store
156 PhysicalMemory::checkLockedAddrList(PacketPtr pkt
)
158 Request
*req
= pkt
->req
;
159 Addr paddr
= LockedAddr::mask(req
->getPaddr());
160 bool isLocked
= pkt
->isLocked();
162 // Initialize return value. Non-conditional stores always
163 // succeed. Assume conditional stores will fail until proven
165 bool success
= !isLocked
;
167 // Iterate over list. Note that there could be multiple matching
168 // records, as more than one context could have done a load locked
170 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
172 while (i
!= lockedAddrList
.end()) {
174 if (i
->addr
== paddr
) {
175 // we have a matching address
177 if (isLocked
&& i
->matchesContext(req
)) {
178 // it's a store conditional, and as far as the memory
179 // system can tell, the requesting context's lock is
181 DPRINTF(LLSC
, "StCond success: cpu %d thread %d addr %#x\n",
182 req
->getCpuNum(), req
->getThreadNum(), paddr
);
186 // Get rid of our record of this lock and advance to next
187 DPRINTF(LLSC
, "Erasing lock record: cpu %d thread %d addr %#x\n",
188 i
->cpuNum
, i
->threadNum
, paddr
);
189 i
= lockedAddrList
.erase(i
);
192 // no match: advance to next record
198 req
->setExtraData(success
? 1 : 0);
209 DPRINTF(MemoryAccess, A " of size %i on address 0x%x data 0x%x\n", \
210 pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
214 #define TRACE_PACKET(A) \
216 switch (pkt->getSize()) { \
222 DPRINTF(MemoryAccess, A " of size %i on address 0x%x\n", \
223 pkt->getSize(), pkt->getAddr()); \
229 #define TRACE_PACKET(A)
234 PhysicalMemory::doAtomicAccess(PacketPtr pkt
)
236 assert(pkt
->getAddr() >= start() &&
237 pkt
->getAddr() + pkt
->getSize() <= start() + size());
239 if (pkt
->memInhibitAsserted()) {
240 DPRINTF(MemoryAccess
, "mem inhibited on 0x%x: not responding\n",
245 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
247 if (pkt
->cmd
== MemCmd::SwapReq
) {
248 IntReg overwrite_val
;
250 uint64_t condition_val64
;
251 uint32_t condition_val32
;
253 assert(sizeof(IntReg
) >= pkt
->getSize());
255 overwrite_mem
= true;
256 // keep a copy of our possible write value, and copy what is at the
257 // memory address into the packet
258 std::memcpy(&overwrite_val
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
259 std::memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
261 if (pkt
->req
->isCondSwap()) {
262 if (pkt
->getSize() == sizeof(uint64_t)) {
263 condition_val64
= pkt
->req
->getExtraData();
264 overwrite_mem
= !std::memcmp(&condition_val64
, hostAddr
,
266 } else if (pkt
->getSize() == sizeof(uint32_t)) {
267 condition_val32
= (uint32_t)pkt
->req
->getExtraData();
268 overwrite_mem
= !std::memcmp(&condition_val32
, hostAddr
,
271 panic("Invalid size for conditional read/write\n");
275 std::memcpy(hostAddr
, &overwrite_val
, pkt
->getSize());
277 TRACE_PACKET("Read/Write");
278 } else if (pkt
->isRead()) {
279 assert(!pkt
->isWrite());
280 if (pkt
->isLocked()) {
281 trackLoadLocked(pkt
);
283 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
284 TRACE_PACKET("Read");
285 } else if (pkt
->isWrite()) {
287 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
288 TRACE_PACKET("Write");
290 } else if (pkt
->isInvalidate()) {
291 //upgrade or invalidate
292 if (pkt
->needsResponse()) {
293 pkt
->makeAtomicResponse();
296 panic("unimplemented");
299 if (pkt
->needsResponse()) {
300 pkt
->makeAtomicResponse();
302 return calculateLatency(pkt
);
307 PhysicalMemory::doFunctionalAccess(PacketPtr pkt
)
309 assert(pkt
->getAddr() >= start() &&
310 pkt
->getAddr() + pkt
->getSize() <= start() + size());
312 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
314 if (pkt
->cmd
== MemCmd::ReadReq
) {
315 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
316 TRACE_PACKET("Read");
317 } else if (pkt
->cmd
== MemCmd::WriteReq
) {
318 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
319 TRACE_PACKET("Write");
321 panic("PhysicalMemory: unimplemented functional command %s",
325 pkt
->result
= Packet::Success
;
330 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
332 // Accept request for "functional" port for backwards compatibility
333 // with places where this function is called from C++. I'd prefer
334 // to move all these into Python someday.
335 if (if_name
== "functional") {
336 return new MemoryPort(csprintf("%s-functional", name()), this);
339 if (if_name
!= "port") {
340 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
343 if (idx
>= ports
.size()) {
347 if (ports
[idx
] != NULL
) {
348 panic("PhysicalMemory::getPort: port %d already assigned", idx
);
352 new MemoryPort(csprintf("%s-port%d", name(), idx
), this);
360 PhysicalMemory::recvStatusChange(Port::Status status
)
364 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
365 PhysicalMemory
*_memory
)
366 : SimpleTimingPort(_name
), memory(_memory
)
370 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
372 memory
->recvStatusChange(status
);
376 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
379 memory
->getAddressRanges(resp
, snoop
);
383 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, bool &snoop
)
387 resp
.push_back(RangeSize(start(), params()->addrRange
.size()));
391 PhysicalMemory::MemoryPort::deviceBlockSize()
393 return memory
->deviceBlockSize();
397 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
399 return memory
->doAtomicAccess(pkt
);
403 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
405 checkFunctional(pkt
);
407 // Default implementation of SimpleTimingPort::recvFunctional()
408 // calls recvAtomic() and throws away the latency; we can save a
409 // little here by just not calculating the latency.
410 memory
->doFunctionalAccess(pkt
);
414 PhysicalMemory::drain(Event
*de
)
417 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
418 count
+= (*pi
)->drain(de
);
422 changeState(Draining
);
424 changeState(Drained
);
429 PhysicalMemory::serialize(ostream
&os
)
431 gzFile compressedMem
;
432 string filename
= name() + ".physmem";
434 SERIALIZE_SCALAR(filename
);
437 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
438 int fd
= creat(thefile
.c_str(), 0664);
441 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
444 compressedMem
= gzdopen(fd
, "wb");
445 if (compressedMem
== NULL
)
446 fatal("Insufficient memory to allocate compression state for %s\n",
449 if (gzwrite(compressedMem
, pmemAddr
, params()->addrRange
.size()) != params()->addrRange
.size()) {
450 fatal("Write failed on physical memory checkpoint file '%s'\n",
454 if (gzclose(compressedMem
))
455 fatal("Close failed on physical memory checkpoint file '%s'\n",
460 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
462 gzFile compressedMem
;
467 const int chunkSize
= 16384;
472 UNSERIALIZE_SCALAR(filename
);
474 filename
= cp
->cptDir
+ "/" + filename
;
477 int fd
= open(filename
.c_str(), O_RDONLY
);
480 fatal("Can't open physical memory checkpoint file '%s'", filename
);
483 compressedMem
= gzdopen(fd
, "rb");
484 if (compressedMem
== NULL
)
485 fatal("Insufficient memory to allocate compression state for %s\n",
488 // unmap file that was mmaped in the constructor
489 // This is done here to make sure that gzip and open don't muck with our
490 // nice large space of memory before we reallocate it
491 munmap((char*)pmemAddr
, params()->addrRange
.size());
493 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
494 MAP_ANON
| MAP_PRIVATE
, -1, 0);
496 if (pmemAddr
== (void *)MAP_FAILED
) {
498 fatal("Could not mmap physical memory!\n");
502 tempPage
= (long*)malloc(chunkSize
);
503 if (tempPage
== NULL
)
504 fatal("Unable to malloc memory to read file %s\n", filename
);
506 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
507 while (curSize
< params()->addrRange
.size()) {
508 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
509 if (bytesRead
!= chunkSize
&& bytesRead
!= params()->addrRange
.size() - curSize
)
510 fatal("Read failed on physical memory checkpoint file '%s'"
511 " got %d bytes, expected %d or %d bytes\n",
512 filename
, bytesRead
, chunkSize
, params()->addrRange
.size()-curSize
);
514 assert(bytesRead
% sizeof(long) == 0);
516 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
518 if (*(tempPage
+x
) != 0) {
519 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
520 *pmem_current
= *(tempPage
+x
);
523 curSize
+= bytesRead
;
528 if (gzclose(compressedMem
))
529 fatal("Close failed on physical memory checkpoint file '%s'\n",
535 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
538 Param
<Range
<Addr
> > range
;
542 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
544 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
546 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
547 INIT_PARAM(range
, "Device Address Range"),
548 INIT_PARAM(latency
, "Memory access latency"),
549 INIT_PARAM(zero
, "Zero initialize memory")
551 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
553 CREATE_SIM_OBJECT(PhysicalMemory
)
555 PhysicalMemory::Params
*p
= new PhysicalMemory::Params
;
556 p
->name
= getInstanceName();
557 p
->addrRange
= range
;
558 p
->latency
= latency
;
560 return new PhysicalMemory(p
);
563 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)