5eb89e1265f12858af2b739555a327eb8c4470ae
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
;
139 return calculateLatency(pkt
);
143 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
145 if (if_name
== "port" && idx
== -1) {
147 panic("PhysicalMemory::getPort: additional port requested to memory!");
148 port
= new MemoryPort(name() + "-port", this);
150 } else if (if_name
== "functional") {
151 /* special port for functional writes at startup. */
152 return new MemoryPort(name() + "-funcport", this);
154 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
159 PhysicalMemory::recvStatusChange(Port::Status status
)
163 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
164 PhysicalMemory
*_memory
)
165 : SimpleTimingPort(_name
), memory(_memory
)
169 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
171 memory
->recvStatusChange(status
);
175 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
176 AddrRangeList
&snoop
)
178 memory
->getAddressRanges(resp
, snoop
);
182 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
186 resp
.push_back(RangeSize(params()->addrRange
.start
, params()->addrRange
.size()));
190 PhysicalMemory::MemoryPort::deviceBlockSize()
192 return memory
->deviceBlockSize();
196 PhysicalMemory::MemoryPort::recvTiming(Packet
*pkt
)
198 assert(pkt
->result
!= Packet::Nacked
);
200 Tick latency
= memory
->doFunctionalAccess(pkt
);
202 pkt
->makeTimingResponse();
203 sendTiming(pkt
, latency
);
209 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
211 return memory
->doFunctionalAccess(pkt
);
215 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
217 memory
->doFunctionalAccess(pkt
);
221 PhysicalMemory::drain(Event
*de
)
223 int count
= port
->drain(de
);
225 changeState(Draining
);
227 changeState(Drained
);
232 PhysicalMemory::serialize(ostream
&os
)
234 gzFile compressedMem
;
235 string filename
= name() + ".physmem";
237 SERIALIZE_SCALAR(filename
);
240 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
241 int fd
= creat(thefile
.c_str(), 0664);
244 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
247 compressedMem
= gzdopen(fd
, "wb");
248 if (compressedMem
== NULL
)
249 fatal("Insufficient memory to allocate compression state for %s\n",
252 if (gzwrite(compressedMem
, pmemAddr
, params()->addrRange
.size()) != params()->addrRange
.size()) {
253 fatal("Write failed on physical memory checkpoint file '%s'\n",
257 if (gzclose(compressedMem
))
258 fatal("Close failed on physical memory checkpoint file '%s'\n",
263 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
265 gzFile compressedMem
;
270 const int chunkSize
= 16384;
275 UNSERIALIZE_SCALAR(filename
);
277 filename
= cp
->cptDir
+ "/" + filename
;
280 int fd
= open(filename
.c_str(), O_RDONLY
);
283 fatal("Can't open physical memory checkpoint file '%s'", filename
);
286 compressedMem
= gzdopen(fd
, "rb");
287 if (compressedMem
== NULL
)
288 fatal("Insufficient memory to allocate compression state for %s\n",
291 // unmap file that was mmaped in the constructor
292 // This is done here to make sure that gzip and open don't muck with our
293 // nice large space of memory before we reallocate it
294 munmap(pmemAddr
, params()->addrRange
.size());
296 pmemAddr
= (uint8_t *)mmap(NULL
, params()->addrRange
.size(), PROT_READ
| PROT_WRITE
,
297 MAP_ANON
| MAP_PRIVATE
, -1, 0);
299 if (pmemAddr
== (void *)MAP_FAILED
) {
301 fatal("Could not mmap physical memory!\n");
305 tempPage
= (long*)malloc(chunkSize
);
306 if (tempPage
== NULL
)
307 fatal("Unable to malloc memory to read file %s\n", filename
);
309 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
310 while (curSize
< params()->addrRange
.size()) {
311 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
312 if (bytesRead
!= chunkSize
&& bytesRead
!= params()->addrRange
.size() - curSize
)
313 fatal("Read failed on physical memory checkpoint file '%s'"
314 " got %d bytes, expected %d or %d bytes\n",
315 filename
, bytesRead
, chunkSize
, params()->addrRange
.size()-curSize
);
317 assert(bytesRead
% sizeof(long) == 0);
319 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
321 if (*(tempPage
+x
) != 0) {
322 pmem_current
= (long*)(pmemAddr
+ curSize
+ x
* sizeof(long));
323 *pmem_current
= *(tempPage
+x
);
326 curSize
+= bytesRead
;
331 if (gzclose(compressedMem
))
332 fatal("Close failed on physical memory checkpoint file '%s'\n",
338 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
341 Param
<Range
<Addr
> > range
;
344 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
346 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
348 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
349 INIT_PARAM(range
, "Device Address Range"),
350 INIT_PARAM(latency
, "Memory access latency")
352 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
354 CREATE_SIM_OBJECT(PhysicalMemory
)
356 PhysicalMemory::Params
*p
= new PhysicalMemory::Params
;
357 p
->name
= getInstanceName();
358 p
->addrRange
= range
;
359 p
->latency
= latency
;
360 return new PhysicalMemory(p
);
363 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)