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>
43 #include "base/misc.hh"
44 #include "config/full_system.hh"
45 #include "mem/packet_impl.hh"
46 #include "mem/physical.hh"
47 #include "sim/host.hh"
48 #include "sim/builder.hh"
49 #include "sim/eventq.hh"
50 #include "arch/isa_traits.hh"
54 using namespace TheISA
;
57 PhysicalMemory::PhysicalMemory(Params
*p
)
58 : MemObject(p
->name
), pmemAddr(NULL
), port(NULL
), lat(p
->latency
), _params(p
)
60 if (params()->addrRange
.size() % TheISA::PageBytes
!= 0)
61 panic("Memory Size not divisible by page size\n");
63 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
64 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
67 if (pmemAddr
== (void *)MAP_FAILED
) {
69 fatal("Could not mmap!\n");
76 PhysicalMemory::init()
79 panic("PhysicalMemory not connected to anything!");
80 port
->sendStatusChange(Port::RangeChange
);
83 PhysicalMemory::~PhysicalMemory()
86 munmap(pmemAddr
, params()->addrRange
.size());
91 PhysicalMemory::new_page()
93 Addr return_addr
= pagePtr
<< LogVMPageSize
;
94 return_addr
+= params()->addrRange
.start
;
101 PhysicalMemory::deviceBlockSize()
103 //Can accept anysize request
108 PhysicalMemory::calculateLatency(Packet
*pkt
)
114 PhysicalMemory::doFunctionalAccess(Packet
*pkt
)
116 assert(pkt
->getAddr() + pkt
->getSize() < params()->addrRange
.size());
119 case Packet::ReadReq
:
120 memcpy(pkt
->getPtr
<uint8_t>(),
121 pmemAddr
+ pkt
->getAddr() - params()->addrRange
.start
,
124 case Packet::WriteReq
:
125 memcpy(pmemAddr
+ pkt
->getAddr() - params()->addrRange
.start
,
126 pkt
->getPtr
<uint8_t>(),
128 // temporary hack: will need to add real LL/SC implementation
129 // for cacheless systems later.
130 if (pkt
->req
->getFlags() & LOCKED
) {
131 pkt
->req
->setScResult(1);
135 panic("unimplemented");
138 pkt
->result
= Packet::Success
;
142 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
144 if (if_name
== "port" && idx
== -1) {
146 panic("PhysicalMemory::getPort: additional port requested to memory!");
147 port
= new MemoryPort(name() + "-port", this);
149 } else if (if_name
== "functional") {
150 /* special port for functional writes at startup. */
151 return new MemoryPort(name() + "-funcport", this);
153 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
158 PhysicalMemory::recvStatusChange(Port::Status status
)
162 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
163 PhysicalMemory
*_memory
)
164 : SimpleTimingPort(_name
), memory(_memory
)
168 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
170 memory
->recvStatusChange(status
);
174 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
175 AddrRangeList
&snoop
)
177 memory
->getAddressRanges(resp
, snoop
);
181 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
185 resp
.push_back(RangeSize(params()->addrRange
.start
, params()->addrRange
.size()));
189 PhysicalMemory::MemoryPort::deviceBlockSize()
191 return memory
->deviceBlockSize();
195 PhysicalMemory::MemoryPort::recvTiming(Packet
*pkt
)
197 assert(pkt
->result
!= Packet::Nacked
);
199 Tick latency
= memory
->calculateLatency(pkt
);
201 memory
->doFunctionalAccess(pkt
);
203 pkt
->makeTimingResponse();
204 sendTiming(pkt
, latency
);
210 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
212 memory
->doFunctionalAccess(pkt
);
213 return memory
->calculateLatency(pkt
);
217 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
219 memory
->doFunctionalAccess(pkt
);
223 PhysicalMemory::drain(Event
*de
)
225 int count
= port
->drain(de
);
227 changeState(Draining
);
229 changeState(Drained
);
234 PhysicalMemory::serialize(ostream
&os
)
236 gzFile compressedMem
;
237 string filename
= name() + ".physmem";
239 SERIALIZE_SCALAR(filename
);
242 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
243 int fd
= creat(thefile
.c_str(), 0664);
246 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
249 compressedMem
= gzdopen(fd
, "wb");
250 if (compressedMem
== NULL
)
251 fatal("Insufficient memory to allocate compression state for %s\n",
254 if (gzwrite(compressedMem
, pmemAddr
, params()->addrRange
.size()) != params()->addrRange
.size()) {
255 fatal("Write failed on physical memory checkpoint file '%s'\n",
259 if (gzclose(compressedMem
))
260 fatal("Close failed on physical memory checkpoint file '%s'\n",
265 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
267 gzFile compressedMem
;
272 const int chunkSize
= 16384;
277 UNSERIALIZE_SCALAR(filename
);
279 filename
= cp
->cptDir
+ "/" + filename
;
282 int fd
= open(filename
.c_str(), O_RDONLY
);
285 fatal("Can't open physical memory checkpoint file '%s'", filename
);
288 compressedMem
= gzdopen(fd
, "rb");
289 if (compressedMem
== NULL
)
290 fatal("Insufficient memory to allocate compression state for %s\n",
293 // unmap file that was mmaped in the constructor
294 // This is done here to make sure that gzip and open don't muck with our
295 // nice large space of memory before we reallocate it
296 munmap(pmemAddr
, params()->addrRange
.size());
298 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
299 MAP_ANON
| MAP_PRIVATE
, -1, 0);
301 if (pmemAddr
== (void *)MAP_FAILED
) {
303 fatal("Could not mmap physical memory!\n");
307 tempPage
= (long*)malloc(chunkSize
);
308 if (tempPage
== NULL
)
309 fatal("Unable to malloc memory to read file %s\n", filename
);
311 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
312 while (curSize
< params()->addrRange
.size()) {
313 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
314 if (bytesRead
!= chunkSize
&& bytesRead
!= params()->addrRange
.size() - curSize
)
315 fatal("Read failed on physical memory checkpoint file '%s'"
316 " got %d bytes, expected %d or %d bytes\n",
317 filename
, bytesRead
, chunkSize
, params()->addrRange
.size()-curSize
);
319 assert(bytesRead
% sizeof(long) == 0);
321 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
323 if (*(tempPage
+x
) != 0) {
324 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
325 *pmem_current
= *(tempPage
+x
);
328 curSize
+= bytesRead
;
333 if (gzclose(compressedMem
))
334 fatal("Close failed on physical memory checkpoint file '%s'\n",
340 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
343 Param
<Range
<Addr
> > range
;
346 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
348 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
350 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
351 INIT_PARAM(range
, "Device Address Range"),
352 INIT_PARAM(latency
, "Memory access latency")
354 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
356 CREATE_SIM_OBJECT(PhysicalMemory
)
358 PhysicalMemory::Params
*p
= new PhysicalMemory::Params
;
359 p
->name
= getInstanceName();
360 p
->addrRange
= range
;
361 p
->latency
= latency
;
362 return new PhysicalMemory(p
);
365 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)