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
31 #include <sys/types.h>
42 #include "base/misc.hh"
43 #include "config/full_system.hh"
44 #include "mem/packet_impl.hh"
45 #include "mem/physical.hh"
46 #include "sim/host.hh"
47 #include "sim/builder.hh"
48 #include "sim/eventq.hh"
49 #include "arch/isa_traits.hh"
53 using namespace TheISA
;
55 PhysicalMemory::MemResponseEvent::MemResponseEvent(Packet
*pkt
, MemoryPort
* _m
)
56 : Event(&mainEventQueue
, CPU_Tick_Pri
), pkt(pkt
), memoryPort(_m
)
59 this->setFlags(AutoDelete
);
63 PhysicalMemory::MemResponseEvent::process()
65 memoryPort
->sendTiming(pkt
);
69 PhysicalMemory::MemResponseEvent::description()
71 return "Physical Memory Timing Access respnse event";
74 PhysicalMemory::PhysicalMemory(const string
&n
, Tick latency
)
75 : MemObject(n
),base_addr(0), pmem_addr(NULL
), port(NULL
), lat(latency
)
77 // Hardcoded to 128 MB for now.
80 if (pmem_size
% TheISA::PageBytes
!= 0)
81 panic("Memory Size not divisible by page size\n");
83 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
84 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
87 if (pmem_addr
== (void *)MAP_FAILED
) {
89 fatal("Could not mmap!\n");
96 PhysicalMemory::init()
99 panic("PhysicalMemory not connected to anything!");
100 port
->sendStatusChange(Port::RangeChange
);
103 PhysicalMemory::~PhysicalMemory()
106 munmap(pmem_addr
, pmem_size
);
111 PhysicalMemory::new_page()
113 Addr return_addr
= page_ptr
<< LogVMPageSize
;
114 return_addr
+= base_addr
;
121 PhysicalMemory::deviceBlockSize()
123 //Can accept anysize request
128 PhysicalMemory::doTimingAccess (Packet
*pkt
, MemoryPort
* memoryPort
)
130 doFunctionalAccess(pkt
);
132 // turn packet around to go back to requester
133 pkt
->makeTimingResponse();
134 MemResponseEvent
* response
= new MemResponseEvent(pkt
, memoryPort
);
135 response
->schedule(curTick
+ lat
);
141 PhysicalMemory::doAtomicAccess(Packet
*pkt
)
143 doFunctionalAccess(pkt
);
148 PhysicalMemory::doFunctionalAccess(Packet
*pkt
)
150 assert(pkt
->getAddr() + pkt
->getSize() < pmem_size
);
153 case Packet::ReadReq
:
154 memcpy(pkt
->getPtr
<uint8_t>(),
155 pmem_addr
+ pkt
->getAddr() - base_addr
,
158 case Packet::WriteReq
:
159 memcpy(pmem_addr
+ pkt
->getAddr() - base_addr
,
160 pkt
->getPtr
<uint8_t>(),
162 // temporary hack: will need to add real LL/SC implementation
163 // for cacheless systems later.
164 if (pkt
->req
->getFlags() & LOCKED
) {
165 pkt
->req
->setScResult(1);
169 panic("unimplemented");
172 pkt
->result
= Packet::Success
;
176 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
178 if (if_name
== "port" && idx
== -1) {
180 panic("PhysicalMemory::getPort: additional port requested to memory!");
181 port
= new MemoryPort(name() + "-port", this);
183 } else if (if_name
== "functional") {
184 /* special port for functional writes at startup. */
185 return new MemoryPort(name() + "-funcport", this);
187 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
192 PhysicalMemory::recvStatusChange(Port::Status status
)
196 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
197 PhysicalMemory
*_memory
)
198 : Port(_name
), memory(_memory
)
202 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
204 memory
->recvStatusChange(status
);
208 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
209 AddrRangeList
&snoop
)
211 memory
->getAddressRanges(resp
, snoop
);
215 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
219 resp
.push_back(RangeSize(base_addr
, pmem_size
));
223 PhysicalMemory::MemoryPort::deviceBlockSize()
225 return memory
->deviceBlockSize();
229 PhysicalMemory::MemoryPort::recvTiming(Packet
*pkt
)
231 return memory
->doTimingAccess(pkt
, this);
235 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
237 return memory
->doAtomicAccess(pkt
);
241 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
243 memory
->doFunctionalAccess(pkt
);
249 PhysicalMemory::serialize(ostream
&os
)
251 gzFile compressedMem
;
252 string filename
= name() + ".physmem";
254 SERIALIZE_SCALAR(pmem_size
);
255 SERIALIZE_SCALAR(filename
);
258 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
259 int fd
= creat(thefile
.c_str(), 0664);
262 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
265 compressedMem
= gzdopen(fd
, "wb");
266 if (compressedMem
== NULL
)
267 fatal("Insufficient memory to allocate compression state for %s\n",
270 if (gzwrite(compressedMem
, pmem_addr
, pmem_size
) != pmem_size
) {
271 fatal("Write failed on physical memory checkpoint file '%s'\n",
275 if (gzclose(compressedMem
))
276 fatal("Close failed on physical memory checkpoint file '%s'\n",
281 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
283 gzFile compressedMem
;
288 const int chunkSize
= 16384;
291 // unmap file that was mmaped in the constructor
292 munmap(pmem_addr
, pmem_size
);
296 UNSERIALIZE_SCALAR(pmem_size
);
297 UNSERIALIZE_SCALAR(filename
);
299 filename
= cp
->cptDir
+ "/" + filename
;
302 int fd
= open(filename
.c_str(), O_RDONLY
);
305 fatal("Can't open physical memory checkpoint file '%s'", filename
);
308 compressedMem
= gzdopen(fd
, "rb");
309 if (compressedMem
== NULL
)
310 fatal("Insufficient memory to allocate compression state for %s\n",
314 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
315 MAP_ANON
| MAP_PRIVATE
, -1, 0);
317 if (pmem_addr
== (void *)MAP_FAILED
) {
319 fatal("Could not mmap physical memory!\n");
323 tempPage
= (long*)malloc(chunkSize
);
324 if (tempPage
== NULL
)
325 fatal("Unable to malloc memory to read file %s\n", filename
);
327 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
328 while (curSize
< pmem_size
) {
329 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
330 if (bytesRead
!= chunkSize
&& bytesRead
!= pmem_size
- curSize
)
331 fatal("Read failed on physical memory checkpoint file '%s'"
332 " got %d bytes, expected %d or %d bytes\n",
333 filename
, bytesRead
, chunkSize
, pmem_size
-curSize
);
335 assert(bytesRead
% sizeof(long) == 0);
337 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
339 if (*(tempPage
+x
) != 0) {
340 pmem_current
= (long*)(pmem_addr
+ curSize
+ x
* sizeof(long));
341 *pmem_current
= *(tempPage
+x
);
344 curSize
+= bytesRead
;
349 if (gzclose(compressedMem
))
350 fatal("Close failed on physical memory checkpoint file '%s'\n",
356 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
359 Param
<Range
<Addr
> > range
;
362 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
364 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
366 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
367 INIT_PARAM(range
, "Device Address Range"),
368 INIT_PARAM(latency
, "Memory access latency")
370 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
372 CREATE_SIM_OBJECT(PhysicalMemory
)
375 return new PhysicalMemory(getInstanceName(), latency
);
378 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)