Deleted Added
sdiff udiff text old ( 2914:2c524dc023d2 ) new ( 3012:1d5e18f6a100 )
full compact
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)
59{
60 // Hardcoded to 128 MB for now.
61 pmem_size = 1 << 27;
62
63 if (pmem_size % TheISA::PageBytes != 0)
64 panic("Memory Size not divisible by page size\n");
65
66 int map_flags = MAP_ANON | MAP_PRIVATE;
67 pmem_addr = (uint8_t *)mmap(NULL, pmem_size, PROT_READ | PROT_WRITE,
68 map_flags, -1, 0);
69
70 if (pmem_addr == (void *)MAP_FAILED) {
71 perror("mmap");
72 fatal("Could not mmap!\n");
73 }
74
75 page_ptr = 0;
76}
77
78void
79PhysicalMemory::init()
80{
81 if (!port)
82 panic("PhysicalMemory not connected to anything!");
83 port->sendStatusChange(Port::RangeChange);
84}
85
86PhysicalMemory::~PhysicalMemory()
87{
88 if (pmem_addr)
89 munmap(pmem_addr, pmem_size);
90 //Remove memPorts?
91}
92
93Addr
94PhysicalMemory::new_page()
95{
96 Addr return_addr = page_ptr << LogVMPageSize;
97 return_addr += base_addr;
98
99 ++page_ptr;
100 return return_addr;
101}
102
103int
104PhysicalMemory::deviceBlockSize()
105{
106 //Can accept anysize request
107 return 0;
108}
109
110
111Tick
112PhysicalMemory::doFunctionalAccess(Packet *pkt)
113{
114 assert(pkt->getAddr() + pkt->getSize() < pmem_size);
115
116 switch (pkt->cmd) {
117 case Packet::ReadReq:
118 memcpy(pkt->getPtr<uint8_t>(),
119 pmem_addr + pkt->getAddr() - base_addr,
120 pkt->getSize());
121 break;
122 case Packet::WriteReq:
123 memcpy(pmem_addr + pkt->getAddr() - base_addr,
124 pkt->getPtr<uint8_t>(),
125 pkt->getSize());
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);
130 }
131 break;
132 default:
133 panic("unimplemented");
134 }
135
136 pkt->result = Packet::Success;
137 return lat;
138}
139
140Port *
141PhysicalMemory::getPort(const std::string &if_name, int idx)
142{
143 if (if_name == "port" && idx == -1) {
144 if (port != NULL)
145 panic("PhysicalMemory::getPort: additional port requested to memory!");

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

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

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

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

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

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

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

347 INIT_PARAM_DFLT(file, "memory mapped file", ""),
348 INIT_PARAM(range, "Device Address Range"),
349 INIT_PARAM(latency, "Memory access latency")
350
351END_INIT_SIM_OBJECT_PARAMS(PhysicalMemory)
352
353CREATE_SIM_OBJECT(PhysicalMemory)
354{
355
356 return new PhysicalMemory(getInstanceName(), latency);
357}
358
359REGISTER_SIM_OBJECT("PhysicalMemory", PhysicalMemory)