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.
29 #include <sys/types.h>
40 #include "base/misc.hh"
41 #include "config/full_system.hh"
42 #include "mem/packet_impl.hh"
43 #include "mem/physical.hh"
44 #include "sim/host.hh"
45 #include "sim/builder.hh"
46 #include "sim/eventq.hh"
47 #include "arch/isa_traits.hh"
51 using namespace TheISA
;
53 PhysicalMemory::MemResponseEvent::MemResponseEvent(Packet
*pkt
, MemoryPort
* _m
)
54 : Event(&mainEventQueue
, CPU_Tick_Pri
), pkt(pkt
), memoryPort(_m
)
57 this->setFlags(AutoDelete
);
61 PhysicalMemory::MemResponseEvent::process()
63 memoryPort
->sendTiming(pkt
);
67 PhysicalMemory::MemResponseEvent::description()
69 return "Physical Memory Timing Access respnse event";
72 PhysicalMemory::PhysicalMemory(const string
&n
, Tick latency
)
73 : MemObject(n
),base_addr(0), pmem_addr(NULL
), port(NULL
), lat(latency
)
75 // Hardcoded to 128 MB for now.
78 if (pmem_size
% TheISA::PageBytes
!= 0)
79 panic("Memory Size not divisible by page size\n");
81 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
82 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
85 if (pmem_addr
== (void *)MAP_FAILED
) {
87 fatal("Could not mmap!\n");
94 PhysicalMemory::init()
97 panic("PhysicalMemory not connected to anything!");
98 port
->sendStatusChange(Port::RangeChange
);
101 PhysicalMemory::~PhysicalMemory()
104 munmap(pmem_addr
, pmem_size
);
109 PhysicalMemory::new_page()
111 Addr return_addr
= page_ptr
<< LogVMPageSize
;
112 return_addr
+= base_addr
;
119 PhysicalMemory::deviceBlockSize()
121 //Can accept anysize request
126 PhysicalMemory::doTimingAccess (Packet
*pkt
, MemoryPort
* memoryPort
)
128 doFunctionalAccess(pkt
);
130 // turn packet around to go back to requester
131 pkt
->makeTimingResponse();
132 MemResponseEvent
* response
= new MemResponseEvent(pkt
, memoryPort
);
133 response
->schedule(curTick
+ lat
);
139 PhysicalMemory::doAtomicAccess(Packet
*pkt
)
141 doFunctionalAccess(pkt
);
146 PhysicalMemory::doFunctionalAccess(Packet
*pkt
)
148 assert(pkt
->getAddr() + pkt
->getSize() < pmem_size
);
151 case Packet::ReadReq
:
152 memcpy(pkt
->getPtr
<uint8_t>(),
153 pmem_addr
+ pkt
->getAddr() - base_addr
,
156 case Packet::WriteReq
:
157 memcpy(pmem_addr
+ pkt
->getAddr() - base_addr
,
158 pkt
->getPtr
<uint8_t>(),
160 // temporary hack: will need to add real LL/SC implementation
161 // for cacheless systems later.
162 if (pkt
->req
->getFlags() & LOCKED
) {
163 pkt
->req
->setScResult(1);
167 panic("unimplemented");
170 pkt
->result
= Packet::Success
;
174 PhysicalMemory::getPort(const std::string
&if_name
)
178 panic("PhysicalMemory::getPort: additional port requested to memory!");
179 port
= new MemoryPort(name() + "-port", this);
181 } else if (if_name
== "functional") {
182 /* special port for functional writes at startup. */
183 return new MemoryPort(name() + "-funcport", this);
185 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
190 PhysicalMemory::recvStatusChange(Port::Status status
)
194 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
195 PhysicalMemory
*_memory
)
196 : Port(_name
), memory(_memory
)
200 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
202 memory
->recvStatusChange(status
);
206 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
207 AddrRangeList
&snoop
)
209 memory
->getAddressRanges(resp
, snoop
);
213 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
217 resp
.push_back(RangeSize(base_addr
, pmem_size
));
221 PhysicalMemory::MemoryPort::deviceBlockSize()
223 return memory
->deviceBlockSize();
227 PhysicalMemory::MemoryPort::recvTiming(Packet
*pkt
)
229 return memory
->doTimingAccess(pkt
, this);
233 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
235 return memory
->doAtomicAccess(pkt
);
239 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
241 memory
->doFunctionalAccess(pkt
);
247 PhysicalMemory::serialize(ostream
&os
)
249 gzFile compressedMem
;
250 string filename
= name() + ".physmem";
252 SERIALIZE_SCALAR(pmem_size
);
253 SERIALIZE_SCALAR(filename
);
256 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
257 int fd
= creat(thefile
.c_str(), 0664);
260 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
263 compressedMem
= gzdopen(fd
, "wb");
264 if (compressedMem
== NULL
)
265 fatal("Insufficient memory to allocate compression state for %s\n",
268 if (gzwrite(compressedMem
, pmem_addr
, pmem_size
) != pmem_size
) {
269 fatal("Write failed on physical memory checkpoint file '%s'\n",
273 if (gzclose(compressedMem
))
274 fatal("Close failed on physical memory checkpoint file '%s'\n",
279 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
281 gzFile compressedMem
;
286 const int chunkSize
= 16384;
289 // unmap file that was mmaped in the constructor
290 munmap(pmem_addr
, pmem_size
);
294 UNSERIALIZE_SCALAR(pmem_size
);
295 UNSERIALIZE_SCALAR(filename
);
297 filename
= cp
->cptDir
+ "/" + filename
;
300 int fd
= open(filename
.c_str(), O_RDONLY
);
303 fatal("Can't open physical memory checkpoint file '%s'", filename
);
306 compressedMem
= gzdopen(fd
, "rb");
307 if (compressedMem
== NULL
)
308 fatal("Insufficient memory to allocate compression state for %s\n",
312 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
313 MAP_ANON
| MAP_PRIVATE
, -1, 0);
315 if (pmem_addr
== (void *)MAP_FAILED
) {
317 fatal("Could not mmap physical memory!\n");
321 tempPage
= (long*)malloc(chunkSize
);
322 if (tempPage
== NULL
)
323 fatal("Unable to malloc memory to read file %s\n", filename
);
325 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
326 while (curSize
< pmem_size
) {
327 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
328 if (bytesRead
!= chunkSize
&& bytesRead
!= pmem_size
- curSize
)
329 fatal("Read failed on physical memory checkpoint file '%s'"
330 " got %d bytes, expected %d or %d bytes\n",
331 filename
, bytesRead
, chunkSize
, pmem_size
-curSize
);
333 assert(bytesRead
% sizeof(long) == 0);
335 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
337 if (*(tempPage
+x
) != 0) {
338 pmem_current
= (long*)(pmem_addr
+ curSize
+ x
* sizeof(long));
339 *pmem_current
= *(tempPage
+x
);
342 curSize
+= bytesRead
;
347 if (gzclose(compressedMem
))
348 fatal("Close failed on physical memory checkpoint file '%s'\n",
354 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
357 Param
<Range
<Addr
> > range
;
360 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
362 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
364 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
365 INIT_PARAM(range
, "Device Address Range"),
366 INIT_PARAM(latency
, "Memory access latency")
368 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
370 CREATE_SIM_OBJECT(PhysicalMemory
)
373 return new PhysicalMemory(getInstanceName(), latency
);
376 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)