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/eventq.hh"
48 #include "sim/host.hh"
51 using namespace TheISA
;
53 PhysicalMemory::PhysicalMemory(const Params
*p
)
54 : MemObject(p
), pmemAddr(NULL
), lat(p
->latency
)
56 if (params()->range
.size() % TheISA::PageBytes
!= 0)
57 panic("Memory Size not divisible by page size\n");
59 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
60 pmemAddr
= (uint8_t *)mmap(NULL
, params()->range
.size(),
61 PROT_READ
| PROT_WRITE
, map_flags
, -1, 0);
63 if (pmemAddr
== (void *)MAP_FAILED
) {
65 fatal("Could not mmap!\n");
68 //If requested, initialize all the memory to 0
70 memset(pmemAddr
, 0, p
->range
.size());
76 PhysicalMemory::init()
78 if (ports
.size() == 0) {
79 fatal("PhysicalMemory object %s is unconnected!", name());
82 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
84 (*pi
)->sendStatusChange(Port::RangeChange
);
88 PhysicalMemory::~PhysicalMemory()
91 munmap((char*)pmemAddr
, params()->range
.size());
96 PhysicalMemory::new_page()
98 Addr return_addr
= pagePtr
<< LogVMPageSize
;
99 return_addr
+= start();
106 PhysicalMemory::deviceBlockSize()
108 //Can accept anysize request
113 PhysicalMemory::calculateLatency(PacketPtr pkt
)
120 // Add load-locked to tracking list. Should only be called if the
121 // operation is a load and the LOCKED flag is set.
123 PhysicalMemory::trackLoadLocked(PacketPtr pkt
)
125 Request
*req
= pkt
->req
;
126 Addr paddr
= LockedAddr::mask(req
->getPaddr());
128 // first we check if we already have a locked addr for this
129 // xc. Since each xc only gets one, we just update the
130 // existing record with the new address.
131 list
<LockedAddr
>::iterator i
;
133 for (i
= lockedAddrList
.begin(); i
!= lockedAddrList
.end(); ++i
) {
134 if (i
->matchesContext(req
)) {
135 DPRINTF(LLSC
, "Modifying lock record: cpu %d thread %d addr %#x\n",
136 req
->getCpuNum(), req
->getThreadNum(), paddr
);
142 // no record for this xc: need to allocate a new one
143 DPRINTF(LLSC
, "Adding lock record: cpu %d thread %d addr %#x\n",
144 req
->getCpuNum(), req
->getThreadNum(), paddr
);
145 lockedAddrList
.push_front(LockedAddr(req
));
149 // Called on *writes* only... both regular stores and
150 // store-conditional operations. Check for conventional stores which
151 // conflict with locked addresses, and for success/failure of store
154 PhysicalMemory::checkLockedAddrList(PacketPtr pkt
)
156 Request
*req
= pkt
->req
;
157 Addr paddr
= LockedAddr::mask(req
->getPaddr());
158 bool isLocked
= pkt
->isLocked();
160 // Initialize return value. Non-conditional stores always
161 // succeed. Assume conditional stores will fail until proven
163 bool success
= !isLocked
;
165 // Iterate over list. Note that there could be multiple matching
166 // records, as more than one context could have done a load locked
168 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
170 while (i
!= lockedAddrList
.end()) {
172 if (i
->addr
== paddr
) {
173 // we have a matching address
175 if (isLocked
&& i
->matchesContext(req
)) {
176 // it's a store conditional, and as far as the memory
177 // system can tell, the requesting context's lock is
179 DPRINTF(LLSC
, "StCond success: cpu %d thread %d addr %#x\n",
180 req
->getCpuNum(), req
->getThreadNum(), paddr
);
184 // Get rid of our record of this lock and advance to next
185 DPRINTF(LLSC
, "Erasing lock record: cpu %d thread %d addr %#x\n",
186 i
->cpuNum
, i
->threadNum
, paddr
);
187 i
= lockedAddrList
.erase(i
);
190 // no match: advance to next record
196 req
->setExtraData(success
? 1 : 0);
207 DPRINTF(MemoryAccess, A " of size %i on address 0x%x data 0x%x\n", \
208 pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
212 #define TRACE_PACKET(A) \
214 switch (pkt->getSize()) { \
220 DPRINTF(MemoryAccess, A " of size %i on address 0x%x\n", \
221 pkt->getSize(), pkt->getAddr()); \
227 #define TRACE_PACKET(A)
232 PhysicalMemory::doAtomicAccess(PacketPtr pkt
)
234 assert(pkt
->getAddr() >= start() &&
235 pkt
->getAddr() + pkt
->getSize() <= start() + size());
237 if (pkt
->memInhibitAsserted()) {
238 DPRINTF(MemoryAccess
, "mem inhibited on 0x%x: not responding\n",
243 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
245 if (pkt
->cmd
== MemCmd::SwapReq
) {
246 IntReg overwrite_val
;
248 uint64_t condition_val64
;
249 uint32_t condition_val32
;
251 assert(sizeof(IntReg
) >= pkt
->getSize());
253 overwrite_mem
= true;
254 // keep a copy of our possible write value, and copy what is at the
255 // memory address into the packet
256 std::memcpy(&overwrite_val
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
257 std::memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
259 if (pkt
->req
->isCondSwap()) {
260 if (pkt
->getSize() == sizeof(uint64_t)) {
261 condition_val64
= pkt
->req
->getExtraData();
262 overwrite_mem
= !std::memcmp(&condition_val64
, hostAddr
,
264 } else if (pkt
->getSize() == sizeof(uint32_t)) {
265 condition_val32
= (uint32_t)pkt
->req
->getExtraData();
266 overwrite_mem
= !std::memcmp(&condition_val32
, hostAddr
,
269 panic("Invalid size for conditional read/write\n");
273 std::memcpy(hostAddr
, &overwrite_val
, pkt
->getSize());
275 TRACE_PACKET("Read/Write");
276 } else if (pkt
->isRead()) {
277 assert(!pkt
->isWrite());
278 if (pkt
->isLocked()) {
279 trackLoadLocked(pkt
);
281 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
282 TRACE_PACKET("Read");
283 } else if (pkt
->isWrite()) {
285 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
286 TRACE_PACKET("Write");
288 } else if (pkt
->isInvalidate()) {
289 //upgrade or invalidate
290 if (pkt
->needsResponse()) {
291 pkt
->makeAtomicResponse();
294 panic("unimplemented");
297 if (pkt
->needsResponse()) {
298 pkt
->makeAtomicResponse();
300 return calculateLatency(pkt
);
305 PhysicalMemory::doFunctionalAccess(PacketPtr pkt
)
307 assert(pkt
->getAddr() >= start() &&
308 pkt
->getAddr() + pkt
->getSize() <= start() + size());
311 uint8_t *hostAddr
= pmemAddr
+ pkt
->getAddr() - start();
313 if (pkt
->cmd
== MemCmd::ReadReq
) {
314 memcpy(pkt
->getPtr
<uint8_t>(), hostAddr
, pkt
->getSize());
315 TRACE_PACKET("Read");
316 } else if (pkt
->cmd
== MemCmd::WriteReq
) {
317 memcpy(hostAddr
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
318 TRACE_PACKET("Write");
320 panic("PhysicalMemory: unimplemented functional command %s",
324 pkt
->makeAtomicResponse();
329 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
331 // Accept request for "functional" port for backwards compatibility
332 // with places where this function is called from C++. I'd prefer
333 // to move all these into Python someday.
334 if (if_name
== "functional") {
335 return new MemoryPort(csprintf("%s-functional", name()), this);
338 if (if_name
!= "port") {
339 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
342 if (idx
>= ports
.size()) {
346 if (ports
[idx
] != NULL
) {
347 panic("PhysicalMemory::getPort: port %d already assigned", idx
);
351 new MemoryPort(csprintf("%s-port%d", name(), idx
), this);
359 PhysicalMemory::recvStatusChange(Port::Status status
)
363 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
364 PhysicalMemory
*_memory
)
365 : SimpleTimingPort(_name
), memory(_memory
)
369 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
371 memory
->recvStatusChange(status
);
375 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
378 memory
->getAddressRanges(resp
, snoop
);
382 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, bool &snoop
)
386 resp
.push_back(RangeSize(start(), params()->range
.size()));
390 PhysicalMemory::MemoryPort::deviceBlockSize()
392 return memory
->deviceBlockSize();
396 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
398 return memory
->doAtomicAccess(pkt
);
402 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
404 if (!checkFunctional(pkt
)) {
405 // Default implementation of SimpleTimingPort::recvFunctional()
406 // calls recvAtomic() and throws away the latency; we can save a
407 // little here by just not calculating the latency.
408 memory
->doFunctionalAccess(pkt
);
413 PhysicalMemory::drain(Event
*de
)
416 for (PortIterator pi
= ports
.begin(); pi
!= ports
.end(); ++pi
) {
417 count
+= (*pi
)->drain(de
);
421 changeState(Draining
);
423 changeState(Drained
);
428 PhysicalMemory::serialize(ostream
&os
)
430 gzFile compressedMem
;
431 string filename
= name() + ".physmem";
433 SERIALIZE_SCALAR(filename
);
436 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
437 int fd
= creat(thefile
.c_str(), 0664);
440 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
443 compressedMem
= gzdopen(fd
, "wb");
444 if (compressedMem
== NULL
)
445 fatal("Insufficient memory to allocate compression state for %s\n",
448 if (gzwrite(compressedMem
, pmemAddr
, params()->range
.size()) !=
449 params()->range
.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()->range
.size());
493 pmemAddr
= (uint8_t *)mmap(NULL
, params()->range
.size(),
494 PROT_READ
| PROT_WRITE
, 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()->range
.size()) {
508 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
509 if (bytesRead
!= chunkSize
&&
510 bytesRead
!= params()->range
.size() - curSize
)
511 fatal("Read failed on physical memory checkpoint file '%s'"
512 " got %d bytes, expected %d or %d bytes\n",
513 filename
, bytesRead
, chunkSize
,
514 params()->range
.size() - curSize
);
516 assert(bytesRead
% sizeof(long) == 0);
518 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
520 if (*(tempPage
+x
) != 0) {
521 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
522 *pmem_current
= *(tempPage
+x
);
525 curSize
+= bytesRead
;
530 if (gzclose(compressedMem
))
531 fatal("Close failed on physical memory checkpoint file '%s'\n",
537 PhysicalMemoryParams::create()
539 return new PhysicalMemory(this);