1/*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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;

--- 40 unchanged lines hidden (view full) ---

49#include "sim/eventq.hh"
50#include "arch/isa_traits.hh"
51
52
53using namespace std;
54using namespace TheISA;
55
56
57PhysicalMemory::PhysicalMemory(const string &n, Tick latency)
58 : MemObject(n),base_addr(0), pmem_addr(NULL), port(NULL), lat(latency)
57PhysicalMemory::PhysicalMemory(Params *p)
58 : MemObject(p->name), pmemAddr(NULL), port(NULL), lat(p->latency), _params(p)
59{
60 // Hardcoded to 128 MB for now.
61 pmem_size = 1 << 27;
62
63 if (pmem_size % TheISA::PageBytes != 0)
60 if (params()->addrRange.size() % TheISA::PageBytes != 0)
61 panic("Memory Size not divisible by page size\n");
62
63 int map_flags = MAP_ANON | MAP_PRIVATE;
67 pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
64 pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
65 map_flags, -1, 0);
66
70 if (pmem_addr == (void *)MAP_FAILED) {
67 if (pmemAddr == (void *)MAP_FAILED) {
68 perror("mmap");
69 fatal("Could not mmap!\n");
70 }
71
75 page_ptr = 0;
72 pagePtr = 0;
73}
74
75void
76PhysicalMemory::init()
77{
78 if (!port)
79 panic("PhysicalMemory not connected to anything!");
80 port->sendStatusChange(Port::RangeChange);
81}
82
83PhysicalMemory::~PhysicalMemory()
84{
88 if (pmem_addr)
89 munmap(pmem_addr, pmem_size);
85 if (pmemAddr)
86 munmap(pmemAddr, params()->addrRange.size());
87 //Remove memPorts?
88}
89
90Addr
91PhysicalMemory::new_page()
92{
96 Addr return_addr = page_ptr << LogVMPageSize;
97 return_addr += base_addr;
93 Addr return_addr = pagePtr << LogVMPageSize;
94 return_addr += params()->addrRange.start;
95
99 ++page_ptr;
96 ++pagePtr;
97 return return_addr;
98}
99
100int
101PhysicalMemory::deviceBlockSize()
102{
103 //Can accept anysize request
104 return 0;
105}
106
107Tick
108PhysicalMemory::calculateLatency(Packet *pkt)
109{
110 return lat;
111}
112
113Tick
114PhysicalMemory::doFunctionalAccess(Packet *pkt)
115{
114 assert(pkt->getAddr() + pkt->getSize() < pmem_size);
116 assert(pkt->getAddr() + pkt->getSize() < params()->addrRange.size());
117
118 switch (pkt->cmd) {
119 case Packet::ReadReq:
120 memcpy(pkt->getPtr<uint8_t>(),
119 pmem_addr + pkt->getAddr() - base_addr,
121 pmemAddr + pkt->getAddr() - params()->addrRange.start,
122 pkt->getSize());
123 break;
124 case Packet::WriteReq:
123 memcpy(pmem_addr + pkt->getAddr() - base_addr,
125 memcpy(pmemAddr + pkt->getAddr() - params()->addrRange.start,
126 pkt->getPtr<uint8_t>(),
127 pkt->getSize());
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);
132 }
133 break;
134 default:
135 panic("unimplemented");
136 }
137
138 pkt->result = Packet::Success;
137 return lat;
139 return calculateLatency(pkt);
140}
141
142Port *
143PhysicalMemory::getPort(const std::string &if_name, int idx)
144{
145 if (if_name == "port" && idx == -1) {
146 if (port != NULL)
147 panic("PhysicalMemory::getPort: additional port requested to memory!");

--- 30 unchanged lines hidden (view full) ---

178 memory->getAddressRanges(resp, snoop);
179}
180
181void
182PhysicalMemory::getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
183{
184 snoop.clear();
185 resp.clear();
184 resp.push_back(RangeSize(base_addr, pmem_size));
186 resp.push_back(RangeSize(params()->addrRange.start, params()->addrRange.size()));
187}
188
189int
190PhysicalMemory::MemoryPort::deviceBlockSize()
191{
192 return memory->deviceBlockSize();
193}
194

--- 34 unchanged lines hidden (view full) ---

229}
230
231void
232PhysicalMemory::serialize(ostream &os)
233{
234 gzFile compressedMem;
235 string filename = name() + ".physmem";
236
235 SERIALIZE_SCALAR(pmem_size);
237 SERIALIZE_SCALAR(filename);
238
239 // write memory file
240 string thefile = Checkpoint::dir() + "/" + filename.c_str();
241 int fd = creat(thefile.c_str(), 0664);
242 if (fd < 0) {
243 perror("creat");
244 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
245 }
246
247 compressedMem = gzdopen(fd, "wb");
248 if (compressedMem == NULL)
249 fatal("Insufficient memory to allocate compression state for %s\n",
250 filename);
251
251 if (gzwrite(compressedMem, pmem_addr, pmem_size) != pmem_size) {
252 if (gzwrite(compressedMem, pmemAddr, params()->addrRange.size()) != params()->addrRange.size()) {
253 fatal("Write failed on physical memory checkpoint file '%s'\n",
254 filename);
255 }
256
257 if (gzclose(compressedMem))
258 fatal("Close failed on physical memory checkpoint file '%s'\n",
259 filename);
260}

--- 4 unchanged lines hidden (view full) ---

265 gzFile compressedMem;
266 long *tempPage;
267 long *pmem_current;
268 uint64_t curSize;
269 uint32_t bytesRead;
270 const int chunkSize = 16384;
271
272
272 // unmap file that was mmaped in the constructor
273 munmap(pmem_addr, pmem_size);
274
273 string filename;
274
277 UNSERIALIZE_SCALAR(pmem_size);
275 UNSERIALIZE_SCALAR(filename);
276
277 filename = cp->cptDir + "/" + filename;
278
279 // mmap memoryfile
280 int fd = open(filename.c_str(), O_RDONLY);
281 if (fd < 0) {
282 perror("open");
283 fatal("Can't open physical memory checkpoint file '%s'", filename);
284 }
285
286 compressedMem = gzdopen(fd, "rb");
287 if (compressedMem == NULL)
288 fatal("Insufficient memory to allocate compression state for %s\n",
289 filename);
290
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());
295
295 pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
296 pmemAddr = (uint8_t *)mmap(NULL, params()->addrRange.size(), PROT_READ | PROT_WRITE,
297 MAP_ANON | MAP_PRIVATE, -1, 0);
298
298 if (pmem_addr == (void *)MAP_FAILED) {
299 if (pmemAddr == (void *)MAP_FAILED) {
300 perror("mmap");
301 fatal("Could not mmap physical memory!\n");
302 }
303
304 curSize = 0;
305 tempPage = (long*)malloc(chunkSize);
306 if (tempPage == NULL)
307 fatal("Unable to malloc memory to read file %s\n", filename);
308
309 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
309 while (curSize < pmem_size) {
310 while (curSize < params()->addrRange.size()) {
311 bytesRead = gzread(compressedMem, tempPage, chunkSize);
311 if (bytesRead != chunkSize && bytesRead != pmem_size - curSize)
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",
314 filename, bytesRead, chunkSize, pmem_size-curSize);
315 filename, bytesRead, chunkSize, params()->addrRange.size()-curSize);
316
317 assert(bytesRead % sizeof(long) == 0);
318
319 for (int x = 0; x < bytesRead/sizeof(long); x++)
320 {
321 if (*(tempPage+x) != 0) {
321 pmem_current = (long*)(pmem_addr + curSize + x * sizeof(long));
322 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
323 *pmem_current = *(tempPage+x);
324 }
325 }
326 curSize += bytesRead;
327 }
328
329 free(tempPage);
330

--- 17 unchanged lines hidden (view full) ---

348 INIT_PARAM_DFLT(file, "memory mapped file", ""),
349 INIT_PARAM(range, "Device Address Range"),
350 INIT_PARAM(latency, "Memory access latency")
351
352END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
353
354CREATE_SIM_OBJECT(PhysicalMemory)
355{
355
356 return new PhysicalMemory(getInstanceName(), latency);
356 PhysicalMemory::Params *p = new PhysicalMemory::Params;
357 p->name = getInstanceName();
358 p->addrRange = range;
359 p->latency = latency;
360 return new PhysicalMemory(p);
361}
362
363REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)