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
), port(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
;
61 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
64 if (pmemAddr
== (void *)MAP_FAILED
) {
66 fatal("Could not mmap!\n");
69 //If requested, initialize all the memory to 0
71 memset(pmemAddr
, 0, params()->addrRange
.size());
77 PhysicalMemory::init()
80 panic("PhysicalMemory not connected to anything!");
81 port
->sendStatusChange(Port::RangeChange
);
84 PhysicalMemory::~PhysicalMemory()
87 munmap((char*)pmemAddr
, params()->addrRange
.size());
92 PhysicalMemory::new_page()
94 Addr return_addr
= pagePtr
<< LogVMPageSize
;
95 return_addr
+= start();
102 PhysicalMemory::deviceBlockSize()
104 //Can accept anysize request
109 PhysicalMemory::calculateLatency(PacketPtr pkt
)
116 // Add load-locked to tracking list. Should only be called if the
117 // operation is a load and the LOCKED flag is set.
119 PhysicalMemory::trackLoadLocked(Request
*req
)
121 Addr paddr
= LockedAddr::mask(req
->getPaddr());
123 // first we check if we already have a locked addr for this
124 // xc. Since each xc only gets one, we just update the
125 // existing record with the new address.
126 list
<LockedAddr
>::iterator i
;
128 for (i
= lockedAddrList
.begin(); i
!= lockedAddrList
.end(); ++i
) {
129 if (i
->matchesContext(req
)) {
130 DPRINTF(LLSC
, "Modifying lock record: cpu %d thread %d addr %#x\n",
131 req
->getCpuNum(), req
->getThreadNum(), paddr
);
137 // no record for this xc: need to allocate a new one
138 DPRINTF(LLSC
, "Adding lock record: cpu %d thread %d addr %#x\n",
139 req
->getCpuNum(), req
->getThreadNum(), paddr
);
140 lockedAddrList
.push_front(LockedAddr(req
));
144 // Called on *writes* only... both regular stores and
145 // store-conditional operations. Check for conventional stores which
146 // conflict with locked addresses, and for success/failure of store
149 PhysicalMemory::checkLockedAddrList(Request
*req
)
151 Addr paddr
= LockedAddr::mask(req
->getPaddr());
152 bool isLocked
= req
->isLocked();
154 // Initialize return value. Non-conditional stores always
155 // succeed. Assume conditional stores will fail until proven
157 bool success
= !isLocked
;
159 // Iterate over list. Note that there could be multiple matching
160 // records, as more than one context could have done a load locked
162 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
164 while (i
!= lockedAddrList
.end()) {
166 if (i
->addr
== paddr
) {
167 // we have a matching address
169 if (isLocked
&& i
->matchesContext(req
)) {
170 // it's a store conditional, and as far as the memory
171 // system can tell, the requesting context's lock is
173 DPRINTF(LLSC
, "StCond success: cpu %d thread %d addr %#x\n",
174 req
->getCpuNum(), req
->getThreadNum(), paddr
);
178 // Get rid of our record of this lock and advance to next
179 DPRINTF(LLSC
, "Erasing lock record: cpu %d thread %d addr %#x\n",
180 i
->cpuNum
, i
->threadNum
, paddr
);
181 i
= lockedAddrList
.erase(i
);
184 // no match: advance to next record
190 req
->setExtraData(success
? 1 : 0);
197 PhysicalMemory::doFunctionalAccess(PacketPtr pkt
)
199 assert(pkt
->getAddr() >= start() &&
200 pkt
->getAddr() + pkt
->getSize() <= start() + size());
203 if (pkt
->req
->isLocked()) {
204 trackLoadLocked(pkt
->req
);
206 memcpy(pkt
->getPtr
<uint8_t>(), pmemAddr
+ pkt
->getAddr() - start(),
209 switch (pkt
->getSize()) {
210 case sizeof(uint64_t):
211 DPRINTF(MemoryAccess
, "Read of size %i on address 0x%x data 0x%x\n",
212 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint64_t>());
214 case sizeof(uint32_t):
215 DPRINTF(MemoryAccess
, "Read of size %i on address 0x%x data 0x%x\n",
216 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint32_t>());
218 case sizeof(uint16_t):
219 DPRINTF(MemoryAccess
, "Read of size %i on address 0x%x data 0x%x\n",
220 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint16_t>());
222 case sizeof(uint8_t):
223 DPRINTF(MemoryAccess
, "Read of size %i on address 0x%x data 0x%x\n",
224 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint8_t>());
227 DPRINTF(MemoryAccess
, "Read of size %i on address 0x%x\n",
228 pkt
->getSize(), pkt
->getAddr());
232 else if (pkt
->isWrite()) {
233 if (writeOK(pkt
->req
)) {
234 memcpy(pmemAddr
+ pkt
->getAddr() - start(), pkt
->getPtr
<uint8_t>(),
237 switch (pkt
->getSize()) {
238 case sizeof(uint64_t):
239 DPRINTF(MemoryAccess
, "Write of size %i on address 0x%x data 0x%x\n",
240 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint64_t>());
242 case sizeof(uint32_t):
243 DPRINTF(MemoryAccess
, "Write of size %i on address 0x%x data 0x%x\n",
244 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint32_t>());
246 case sizeof(uint16_t):
247 DPRINTF(MemoryAccess
, "Write of size %i on address 0x%x data 0x%x\n",
248 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint16_t>());
250 case sizeof(uint8_t):
251 DPRINTF(MemoryAccess
, "Write of size %i on address 0x%x data 0x%x\n",
252 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint8_t>());
255 DPRINTF(MemoryAccess
, "Write of size %i on address 0x%x\n",
256 pkt
->getSize(), pkt
->getAddr());
260 } else if (pkt
->isInvalidate()) {
261 //upgrade or invalidate
262 pkt
->flags
|= SATISFIED
;
263 } else if (pkt
->isReadWrite()) {
264 IntReg overwrite_val
;
266 uint64_t condition_val64
;
267 uint32_t condition_val32
;
269 assert(sizeof(IntReg
) >= pkt
->getSize());
271 overwrite_mem
= true;
272 // keep a copy of our possible write value, and copy what is at the
273 // memory address into the packet
274 std::memcpy(&overwrite_val
, pkt
->getPtr
<uint8_t>(), pkt
->getSize());
275 std::memcpy(pkt
->getPtr
<uint8_t>(), pmemAddr
+ pkt
->getAddr() - start(),
278 if (pkt
->req
->isCondSwap()) {
279 if (pkt
->getSize() == sizeof(uint64_t)) {
280 condition_val64
= pkt
->req
->getExtraData();
281 overwrite_mem
= !std::memcmp(&condition_val64
, pmemAddr
+
282 pkt
->getAddr() - start(), sizeof(uint64_t));
283 } else if (pkt
->getSize() == sizeof(uint32_t)) {
284 condition_val32
= (uint32_t)pkt
->req
->getExtraData();
285 overwrite_mem
= !std::memcmp(&condition_val32
, pmemAddr
+
286 pkt
->getAddr() - start(), sizeof(uint32_t));
288 panic("Invalid size for conditional read/write\n");
292 std::memcpy(pmemAddr
+ pkt
->getAddr() - start(),
293 &overwrite_val
, pkt
->getSize());
296 switch (pkt
->getSize()) {
297 case sizeof(uint64_t):
298 DPRINTF(MemoryAccess
, "Read/Write of size %i on address 0x%x old data 0x%x\n",
299 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint64_t>());
300 DPRINTF(MemoryAccess
, "New Data 0x%x %s conditional (0x%x) and %s \n",
301 overwrite_mem
, pkt
->req
->isCondSwap() ? "was" : "wasn't",
302 condition_val64
, overwrite_mem
? "happened" : "didn't happen");
304 case sizeof(uint32_t):
305 DPRINTF(MemoryAccess
, "Read/Write of size %i on address 0x%x old data 0x%x\n",
306 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint32_t>());
307 DPRINTF(MemoryAccess
, "New Data 0x%x %s conditional (0x%x) and %s \n",
308 overwrite_mem
, pkt
->req
->isCondSwap() ? "was" : "wasn't",
309 condition_val32
, overwrite_mem
? "happened" : "didn't happen");
311 case sizeof(uint16_t):
312 DPRINTF(MemoryAccess
, "Read/Write of size %i on address 0x%x old data 0x%x\n",
313 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint16_t>());
314 DPRINTF(MemoryAccess
, "New Data 0x%x wasn't conditional and happned\n",
317 case sizeof(uint8_t):
318 DPRINTF(MemoryAccess
, "Read/Write of size %i on address 0x%x old data 0x%x\n",
319 pkt
->getSize(), pkt
->getAddr(),pkt
->get
<uint8_t>());
320 DPRINTF(MemoryAccess
, "New Data 0x%x wasn't conditional and happned\n",
324 DPRINTF(MemoryAccess
, "Read/Write of size %i on address 0x%x\n",
325 pkt
->getSize(), pkt
->getAddr());
329 panic("unimplemented");
332 pkt
->result
= Packet::Success
;
336 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
338 if (if_name
== "port" && idx
== -1) {
340 panic("PhysicalMemory::getPort: additional port requested to memory!");
341 port
= new MemoryPort(name() + "-port", this);
343 } else if (if_name
== "functional") {
344 /* special port for functional writes at startup. And for memtester */
345 return new MemoryPort(name() + "-funcport", this);
347 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
352 PhysicalMemory::recvStatusChange(Port::Status status
)
356 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
357 PhysicalMemory
*_memory
)
358 : SimpleTimingPort(_name
), memory(_memory
)
362 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
364 memory
->recvStatusChange(status
);
368 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
369 AddrRangeList
&snoop
)
371 memory
->getAddressRanges(resp
, snoop
);
375 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
379 resp
.push_back(RangeSize(start(),
380 params()->addrRange
.size()));
384 PhysicalMemory::MemoryPort::deviceBlockSize()
386 return memory
->deviceBlockSize();
390 PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt
)
392 memory
->doFunctionalAccess(pkt
);
393 return memory
->calculateLatency(pkt
);
397 PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt
)
399 //Since we are overriding the function, make sure to have the impl of the
400 //check or functional accesses here.
401 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator i
= transmitList
.begin();
402 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator end
= transmitList
.end();
405 while (i
!= end
&& notDone
) {
406 PacketPtr target
= i
->second
;
407 // If the target contains data, and it overlaps the
408 // probed request, need to update data
409 if (target
->intersect(pkt
))
410 notDone
= fixPacket(pkt
, target
);
414 // Default implementation of SimpleTimingPort::recvFunctional()
415 // calls recvAtomic() and throws away the latency; we can save a
416 // little here by just not calculating the latency.
417 memory
->doFunctionalAccess(pkt
);
421 PhysicalMemory::drain(Event
*de
)
423 int count
= port
->drain(de
);
425 changeState(Draining
);
427 changeState(Drained
);
432 PhysicalMemory::serialize(ostream
&os
)
434 gzFile compressedMem
;
435 string filename
= name() + ".physmem";
437 SERIALIZE_SCALAR(filename
);
440 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
441 int fd
= creat(thefile
.c_str(), 0664);
444 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
447 compressedMem
= gzdopen(fd
, "wb");
448 if (compressedMem
== NULL
)
449 fatal("Insufficient memory to allocate compression state for %s\n",
452 if (gzwrite(compressedMem
, pmemAddr
, params()->addrRange
.size()) != params()->addrRange
.size()) {
453 fatal("Write failed on physical memory checkpoint file '%s'\n",
457 if (gzclose(compressedMem
))
458 fatal("Close failed on physical memory checkpoint file '%s'\n",
463 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
465 gzFile compressedMem
;
470 const int chunkSize
= 16384;
475 UNSERIALIZE_SCALAR(filename
);
477 filename
= cp
->cptDir
+ "/" + filename
;
480 int fd
= open(filename
.c_str(), O_RDONLY
);
483 fatal("Can't open physical memory checkpoint file '%s'", filename
);
486 compressedMem
= gzdopen(fd
, "rb");
487 if (compressedMem
== NULL
)
488 fatal("Insufficient memory to allocate compression state for %s\n",
491 // unmap file that was mmaped in the constructor
492 // This is done here to make sure that gzip and open don't muck with our
493 // nice large space of memory before we reallocate it
494 munmap((char*)pmemAddr
, params()->addrRange
.size());
496 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
497 MAP_ANON
| MAP_PRIVATE
, -1, 0);
499 if (pmemAddr
== (void *)MAP_FAILED
) {
501 fatal("Could not mmap physical memory!\n");
505 tempPage
= (long*)malloc(chunkSize
);
506 if (tempPage
== NULL
)
507 fatal("Unable to malloc memory to read file %s\n", filename
);
509 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
510 while (curSize
< params()->addrRange
.size()) {
511 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
512 if (bytesRead
!= chunkSize
&& bytesRead
!= params()->addrRange
.size() - curSize
)
513 fatal("Read failed on physical memory checkpoint file '%s'"
514 " got %d bytes, expected %d or %d bytes\n",
515 filename
, bytesRead
, chunkSize
, params()->addrRange
.size()-curSize
);
517 assert(bytesRead
% sizeof(long) == 0);
519 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
521 if (*(tempPage
+x
) != 0) {
522 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
523 *pmem_current
= *(tempPage
+x
);
526 curSize
+= bytesRead
;
531 if (gzclose(compressedMem
))
532 fatal("Close failed on physical memory checkpoint file '%s'\n",
538 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
541 Param
<Range
<Addr
> > range
;
545 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
547 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
549 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
550 INIT_PARAM(range
, "Device Address Range"),
551 INIT_PARAM(latency
, "Memory access latency"),
552 INIT_PARAM(zero
, "Zero initialize memory")
554 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
556 CREATE_SIM_OBJECT(PhysicalMemory
)
558 PhysicalMemory::Params
*p
= new PhysicalMemory::Params
;
559 p
->name
= getInstanceName();
560 p
->addrRange
= range
;
561 p
->latency
= latency
;
563 return new PhysicalMemory(p
);
566 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)