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/physical.hh"
46 #include "sim/builder.hh"
47 #include "sim/eventq.hh"
48 #include "sim/host.hh"
51 using namespace TheISA
;
53 PhysicalMemory::PhysicalMemory(Params
*p
)
54 : MemObject(p
->name
), pmemAddr(NULL
), port(NULL
), lat(p
->latency
), _params(p
)
56 if (params()->addrRange
.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()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
63 if (pmemAddr
== (void *)MAP_FAILED
) {
65 fatal("Could not mmap!\n");
72 PhysicalMemory::init()
75 panic("PhysicalMemory not connected to anything!");
76 port
->sendStatusChange(Port::RangeChange
);
79 PhysicalMemory::~PhysicalMemory()
82 munmap(pmemAddr
, params()->addrRange
.size());
87 PhysicalMemory::new_page()
89 Addr return_addr
= pagePtr
<< LogVMPageSize
;
90 return_addr
+= params()->addrRange
.start
;
97 PhysicalMemory::deviceBlockSize()
99 //Can accept anysize request
104 PhysicalMemory::calculateLatency(Packet
*pkt
)
111 // Add load-locked to tracking list. Should only be called if the
112 // operation is a load and the LOCKED flag is set.
114 PhysicalMemory::trackLoadLocked(Request
*req
)
116 Addr paddr
= LockedAddr::mask(req
->getPaddr());
118 // first we check if we already have a locked addr for this
119 // xc. Since each xc only gets one, we just update the
120 // existing record with the new address.
121 list
<LockedAddr
>::iterator i
;
123 for (i
= lockedAddrList
.begin(); i
!= lockedAddrList
.end(); ++i
) {
124 if (i
->matchesContext(req
)) {
125 DPRINTF(LLSC
, "Modifying lock record: cpu %d thread %d addr %#x\n",
126 req
->getCpuNum(), req
->getThreadNum(), paddr
);
132 // no record for this xc: need to allocate a new one
133 DPRINTF(LLSC
, "Adding lock record: cpu %d thread %d addr %#x\n",
134 req
->getCpuNum(), req
->getThreadNum(), paddr
);
135 lockedAddrList
.push_front(LockedAddr(req
));
139 // Called on *writes* only... both regular stores and
140 // store-conditional operations. Check for conventional stores which
141 // conflict with locked addresses, and for success/failure of store
144 PhysicalMemory::checkLockedAddrList(Request
*req
)
146 Addr paddr
= LockedAddr::mask(req
->getPaddr());
147 bool isLocked
= req
->isLocked();
149 // Initialize return value. Non-conditional stores always
150 // succeed. Assume conditional stores will fail until proven
152 bool success
= !isLocked
;
154 // Iterate over list. Note that there could be multiple matching
155 // records, as more than one context could have done a load locked
157 list
<LockedAddr
>::iterator i
= lockedAddrList
.begin();
159 while (i
!= lockedAddrList
.end()) {
161 if (i
->addr
== paddr
) {
162 // we have a matching address
164 if (isLocked
&& i
->matchesContext(req
)) {
165 // it's a store conditional, and as far as the memory
166 // system can tell, the requesting context's lock is
168 DPRINTF(LLSC
, "StCond success: cpu %d thread %d addr %#x\n",
169 req
->getCpuNum(), req
->getThreadNum(), paddr
);
173 // Get rid of our record of this lock and advance to next
174 DPRINTF(LLSC
, "Erasing lock record: cpu %d thread %d addr %#x\n",
175 i
->cpuNum
, i
->threadNum
, paddr
);
176 i
= lockedAddrList
.erase(i
);
179 // no match: advance to next record
185 req
->setScResult(success
? 1 : 0);
192 PhysicalMemory::doFunctionalAccess(Packet
*pkt
)
194 assert(pkt
->getAddr() + pkt
->getSize() <= params()->addrRange
.size());
197 if (pkt
->req
->isLocked()) {
198 trackLoadLocked(pkt
->req
);
200 DPRINTF(MemoryAccess
, "Performing Read of size %i on address 0x%x\n",
201 pkt
->getSize(), pkt
->getAddr());
202 memcpy(pkt
->getPtr
<uint8_t>(),
203 pmemAddr
+ pkt
->getAddr() - params()->addrRange
.start
,
206 else if (pkt
->isWrite()) {
207 if (writeOK(pkt
->req
)) {
208 DPRINTF(MemoryAccess
, "Performing Write of size %i on address 0x%x\n",
209 pkt
->getSize(), pkt
->getAddr());
210 memcpy(pmemAddr
+ pkt
->getAddr() - params()->addrRange
.start
,
211 pkt
->getPtr
<uint8_t>(), pkt
->getSize());
214 else if (pkt
->isInvalidate()) {
215 //upgrade or invalidate
216 pkt
->flags
|= SATISFIED
;
219 panic("unimplemented");
222 pkt
->result
= Packet::Success
;
226 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
228 if (if_name
== "port" && idx
== -1) {
230 panic("PhysicalMemory::getPort: additional port requested to memory!");
231 port
= new MemoryPort(name() + "-port", this);
233 } else if (if_name
== "functional") {
234 /* special port for functional writes at startup. And for memtester */
235 return new MemoryPort(name() + "-funcport", this);
237 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
242 PhysicalMemory::recvStatusChange(Port::Status status
)
246 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
247 PhysicalMemory
*_memory
)
248 : SimpleTimingPort(_name
), memory(_memory
)
252 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
254 memory
->recvStatusChange(status
);
258 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
259 AddrRangeList
&snoop
)
261 memory
->getAddressRanges(resp
, snoop
);
265 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
269 resp
.push_back(RangeSize(params()->addrRange
.start
,
270 params()->addrRange
.size()));
274 PhysicalMemory::MemoryPort::deviceBlockSize()
276 return memory
->deviceBlockSize();
280 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
282 memory
->doFunctionalAccess(pkt
);
283 return memory
->calculateLatency(pkt
);
287 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
289 // Default implementation of SimpleTimingPort::recvFunctional()
290 // calls recvAtomic() and throws away the latency; we can save a
291 // little here by just not calculating the latency.
292 memory
->doFunctionalAccess(pkt
);
296 PhysicalMemory::drain(Event
*de
)
298 int count
= port
->drain(de
);
300 changeState(Draining
);
302 changeState(Drained
);
307 PhysicalMemory::serialize(ostream
&os
)
309 gzFile compressedMem
;
310 string filename
= name() + ".physmem";
312 SERIALIZE_SCALAR(filename
);
315 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
316 int fd
= creat(thefile
.c_str(), 0664);
319 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
322 compressedMem
= gzdopen(fd
, "wb");
323 if (compressedMem
== NULL
)
324 fatal("Insufficient memory to allocate compression state for %s\n",
327 if (gzwrite(compressedMem
, pmemAddr
, params()->addrRange
.size()) != params()->addrRange
.size()) {
328 fatal("Write failed on physical memory checkpoint file '%s'\n",
332 if (gzclose(compressedMem
))
333 fatal("Close failed on physical memory checkpoint file '%s'\n",
338 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
340 gzFile compressedMem
;
345 const int chunkSize
= 16384;
350 UNSERIALIZE_SCALAR(filename
);
352 filename
= cp
->cptDir
+ "/" + filename
;
355 int fd
= open(filename
.c_str(), O_RDONLY
);
358 fatal("Can't open physical memory checkpoint file '%s'", filename
);
361 compressedMem
= gzdopen(fd
, "rb");
362 if (compressedMem
== NULL
)
363 fatal("Insufficient memory to allocate compression state for %s\n",
366 // unmap file that was mmaped in the constructor
367 // This is done here to make sure that gzip and open don't muck with our
368 // nice large space of memory before we reallocate it
369 munmap(pmemAddr
, params()->addrRange
.size());
371 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
372 MAP_ANON
| MAP_PRIVATE
, -1, 0);
374 if (pmemAddr
== (void *)MAP_FAILED
) {
376 fatal("Could not mmap physical memory!\n");
380 tempPage
= (long*)malloc(chunkSize
);
381 if (tempPage
== NULL
)
382 fatal("Unable to malloc memory to read file %s\n", filename
);
384 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
385 while (curSize
< params()->addrRange
.size()) {
386 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
387 if (bytesRead
!= chunkSize
&& bytesRead
!= params()->addrRange
.size() - curSize
)
388 fatal("Read failed on physical memory checkpoint file '%s'"
389 " got %d bytes, expected %d or %d bytes\n",
390 filename
, bytesRead
, chunkSize
, params()->addrRange
.size()-curSize
);
392 assert(bytesRead
% sizeof(long) == 0);
394 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
396 if (*(tempPage
+x
) != 0) {
397 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
398 *pmem_current
= *(tempPage
+x
);
401 curSize
+= bytesRead
;
406 if (gzclose(compressedMem
))
407 fatal("Close failed on physical memory checkpoint file '%s'\n",
413 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
416 Param
<Range
<Addr
> > range
;
419 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
421 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
423 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
424 INIT_PARAM(range
, "Device Address Range"),
425 INIT_PARAM(latency
, "Memory access latency")
427 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
429 CREATE_SIM_OBJECT(PhysicalMemory
)
431 PhysicalMemory::Params
*p
= new PhysicalMemory::Params
;
432 p
->name
= getInstanceName();
433 p
->addrRange
= range
;
434 p
->latency
= latency
;
435 return new PhysicalMemory(p
);
438 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)