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(const string
&n
, Tick latency
)
58 : MemObject(n
),base_addr(0), pmem_addr(NULL
), port(NULL
), lat(latency
)
60 // Hardcoded to 128 MB for now.
63 if (pmem_size
% TheISA::PageBytes
!= 0)
64 panic("Memory Size not divisible by page size\n");
66 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
67 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
70 if (pmem_addr
== (void *)MAP_FAILED
) {
72 fatal("Could not mmap!\n");
79 PhysicalMemory::init()
82 panic("PhysicalMemory not connected to anything!");
83 port
->sendStatusChange(Port::RangeChange
);
86 PhysicalMemory::~PhysicalMemory()
89 munmap(pmem_addr
, pmem_size
);
94 PhysicalMemory::new_page()
96 Addr return_addr
= page_ptr
<< LogVMPageSize
;
97 return_addr
+= base_addr
;
104 PhysicalMemory::deviceBlockSize()
106 //Can accept anysize request
112 PhysicalMemory::doFunctionalAccess(Packet
*pkt
)
114 assert(pkt
->getAddr() + pkt
->getSize() < pmem_size
);
117 case Packet::ReadReq
:
118 memcpy(pkt
->getPtr
<uint8_t>(),
119 pmem_addr
+ pkt
->getAddr() - base_addr
,
122 case Packet::WriteReq
:
123 memcpy(pmem_addr
+ pkt
->getAddr() - base_addr
,
124 pkt
->getPtr
<uint8_t>(),
126 // temporary hack: will need to add real LL/SC implementation
127 // for cacheless systems later.
128 if (pkt
->req
->getFlags() & LOCKED
) {
129 pkt
->req
->setScResult(1);
133 panic("unimplemented");
136 pkt
->result
= Packet::Success
;
141 PhysicalMemory::getPort(const std::string
&if_name
, int idx
)
143 if (if_name
== "port" && idx
== -1) {
145 panic("PhysicalMemory::getPort: additional port requested to memory!");
146 port
= new MemoryPort(name() + "-port", this);
148 } else if (if_name
== "functional") {
149 /* special port for functional writes at startup. */
150 return new MemoryPort(name() + "-funcport", this);
152 panic("PhysicalMemory::getPort: unknown port %s requested", if_name
);
157 PhysicalMemory::recvStatusChange(Port::Status status
)
161 PhysicalMemory::MemoryPort::MemoryPort(const std::string
&_name
,
162 PhysicalMemory
*_memory
)
163 : SimpleTimingPort(_name
), memory(_memory
)
167 PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status
)
169 memory
->recvStatusChange(status
);
173 PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList
&resp
,
174 AddrRangeList
&snoop
)
176 memory
->getAddressRanges(resp
, snoop
);
180 PhysicalMemory::getAddressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
)
184 resp
.push_back(RangeSize(base_addr
, pmem_size
));
188 PhysicalMemory::MemoryPort::deviceBlockSize()
190 return memory
->deviceBlockSize();
194 PhysicalMemory::MemoryPort::recvTiming(Packet
*pkt
)
196 assert(pkt
->result
!= Packet::Nacked
);
198 Tick latency
= memory
->doFunctionalAccess(pkt
);
200 pkt
->makeTimingResponse();
201 sendTiming(pkt
, latency
);
207 PhysicalMemory::MemoryPort::recvAtomic(Packet
*pkt
)
209 return memory
->doFunctionalAccess(pkt
);
213 PhysicalMemory::MemoryPort::recvFunctional(Packet
*pkt
)
215 memory
->doFunctionalAccess(pkt
);
219 PhysicalMemory::drain(Event
*de
)
221 int count
= port
->drain(de
);
223 changeState(Draining
);
225 changeState(Drained
);
230 PhysicalMemory::serialize(ostream
&os
)
232 gzFile compressedMem
;
233 string filename
= name() + ".physmem";
235 SERIALIZE_SCALAR(pmem_size
);
236 SERIALIZE_SCALAR(filename
);
239 string thefile
= Checkpoint::dir() + "/" + filename
.c_str();
240 int fd
= creat(thefile
.c_str(), 0664);
243 fatal("Can't open physical memory checkpoint file '%s'\n", filename
);
246 compressedMem
= gzdopen(fd
, "wb");
247 if (compressedMem
== NULL
)
248 fatal("Insufficient memory to allocate compression state for %s\n",
251 if (gzwrite(compressedMem
, pmem_addr
, pmem_size
) != pmem_size
) {
252 fatal("Write failed on physical memory checkpoint file '%s'\n",
256 if (gzclose(compressedMem
))
257 fatal("Close failed on physical memory checkpoint file '%s'\n",
262 PhysicalMemory::unserialize(Checkpoint
*cp
, const string
§ion
)
264 gzFile compressedMem
;
269 const int chunkSize
= 16384;
272 // unmap file that was mmaped in the constructor
273 munmap(pmem_addr
, pmem_size
);
277 UNSERIALIZE_SCALAR(pmem_size
);
278 UNSERIALIZE_SCALAR(filename
);
280 filename
= cp
->cptDir
+ "/" + filename
;
283 int fd
= open(filename
.c_str(), O_RDONLY
);
286 fatal("Can't open physical memory checkpoint file '%s'", filename
);
289 compressedMem
= gzdopen(fd
, "rb");
290 if (compressedMem
== NULL
)
291 fatal("Insufficient memory to allocate compression state for %s\n",
295 pmem_addr
= (uint8_t *)mmap(NULL
, pmem_size
, PROT_READ
| PROT_WRITE
,
296 MAP_ANON
| MAP_PRIVATE
, -1, 0);
298 if (pmem_addr
== (void *)MAP_FAILED
) {
300 fatal("Could not mmap physical memory!\n");
304 tempPage
= (long*)malloc(chunkSize
);
305 if (tempPage
== NULL
)
306 fatal("Unable to malloc memory to read file %s\n", filename
);
308 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
309 while (curSize
< pmem_size
) {
310 bytesRead
= gzread(compressedMem
, tempPage
, chunkSize
);
311 if (bytesRead
!= chunkSize
&& bytesRead
!= pmem_size
- curSize
)
312 fatal("Read failed on physical memory checkpoint file '%s'"
313 " got %d bytes, expected %d or %d bytes\n",
314 filename
, bytesRead
, chunkSize
, pmem_size
-curSize
);
316 assert(bytesRead
% sizeof(long) == 0);
318 for (int x
= 0; x
< bytesRead
/sizeof(long); x
++)
320 if (*(tempPage
+x
) != 0) {
321 pmem_current
= (long*)(pmem_addr
+ curSize
+ x
* sizeof(long));
322 *pmem_current
= *(tempPage
+x
);
325 curSize
+= bytesRead
;
330 if (gzclose(compressedMem
))
331 fatal("Close failed on physical memory checkpoint file '%s'\n",
337 BEGIN_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
340 Param
<Range
<Addr
> > range
;
343 END_DECLARE_SIM_OBJECT_PARAMS(PhysicalMemory
)
345 BEGIN_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
347 INIT_PARAM_DFLT(file
, "memory mapped file", ""),
348 INIT_PARAM(range
, "Device Address Range"),
349 INIT_PARAM(latency
, "Memory access latency")
351 END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory
)
353 CREATE_SIM_OBJECT(PhysicalMemory
)
356 return new PhysicalMemory(getInstanceName(), latency
);
359 REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory
)