physical.cc (6820:2980bd04e6df) physical.cc (7730:982b4c6c1470)
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;
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.
15 *
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.
27 *
28 * Authors: Ron Dreslinski
29 * Ali Saidi
30 */
31
32#include <sys/types.h>
33#include <sys/mman.h>
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;
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.
15 *
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.
27 *
28 * Authors: Ron Dreslinski
29 * Ali Saidi
30 */
31
32#include <sys/types.h>
33#include <sys/mman.h>
34#include <sys/user.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <zlib.h>
38
39#include <cstdio>
40#include <iostream>
41#include <string>
42
43#include "arch/registers.hh"
35#include <errno.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <zlib.h>
39
40#include <cstdio>
41#include <iostream>
42#include <string>
43
44#include "arch/registers.hh"
45#include "base/intmath.hh"
44#include "base/misc.hh"
45#include "base/random.hh"
46#include "base/types.hh"
47#include "config/full_system.hh"
48#include "config/the_isa.hh"
49#include "mem/packet_access.hh"
50#include "mem/physical.hh"
51#include "sim/eventq.hh"
52
53using namespace std;
54using namespace TheISA;
55
56PhysicalMemory::PhysicalMemory(const Params *p)
57 : MemObject(p), pmemAddr(NULL), pagePtr(0),
58 lat(p->latency), lat_var(p->latency_var),
46#include "base/misc.hh"
47#include "base/random.hh"
48#include "base/types.hh"
49#include "config/full_system.hh"
50#include "config/the_isa.hh"
51#include "mem/packet_access.hh"
52#include "mem/physical.hh"
53#include "sim/eventq.hh"
54
55using namespace std;
56using namespace TheISA;
57
58PhysicalMemory::PhysicalMemory(const Params *p)
59 : MemObject(p), pmemAddr(NULL), pagePtr(0),
60 lat(p->latency), lat_var(p->latency_var),
59 cachedSize(params()->range.size()), cachedStart(params()->range.start)
61 _size(params()->range.size()), _start(params()->range.start)
60{
62{
61 if (params()->range.size() % TheISA::PageBytes != 0)
63 if (size() % TheISA::PageBytes != 0)
62 panic("Memory Size not divisible by page size\n");
63
64 if (params()->null)
65 return;
66
64 panic("Memory Size not divisible by page size\n");
65
66 if (params()->null)
67 return;
68
67 int map_flags = MAP_ANON | MAP_PRIVATE;
68 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(),
69 PROT_READ | PROT_WRITE, map_flags, -1, 0);
70
69
70 if (params()->file == "") {
71 int map_flags = MAP_ANON | MAP_PRIVATE;
72 pmemAddr = (uint8_t *)mmap(NULL, size(),
73 PROT_READ | PROT_WRITE, map_flags, -1, 0);
74 } else {
75 int map_flags = MAP_PRIVATE;
76 int fd = open(params()->file.c_str(), O_RDONLY);
77 _size = lseek(fd, 0, SEEK_END);
78 lseek(fd, 0, SEEK_SET);
79 pmemAddr = (uint8_t *)mmap(NULL, roundUp(size(), PAGE_SIZE),
80 PROT_READ | PROT_WRITE, map_flags, fd, 0);
81 }
82
71 if (pmemAddr == (void *)MAP_FAILED) {
72 perror("mmap");
83 if (pmemAddr == (void *)MAP_FAILED) {
84 perror("mmap");
73 fatal("Could not mmap!\n");
85 if (params()->file == "")
86 fatal("Could not mmap!\n");
87 else
88 fatal("Could not find file: %s\n", params()->file);
74 }
75
76 //If requested, initialize all the memory to 0
77 if (p->zero)
89 }
90
91 //If requested, initialize all the memory to 0
92 if (p->zero)
78 memset(pmemAddr, 0, p->range.size());
93 memset(pmemAddr, 0, size());
79}
80
81void
82PhysicalMemory::init()
83{
84 if (ports.size() == 0) {
85 fatal("PhysicalMemory object %s is unconnected!", name());
86 }
87
88 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
89 if (*pi)
90 (*pi)->sendStatusChange(Port::RangeChange);
91 }
92}
93
94PhysicalMemory::~PhysicalMemory()
95{
96 if (pmemAddr)
94}
95
96void
97PhysicalMemory::init()
98{
99 if (ports.size() == 0) {
100 fatal("PhysicalMemory object %s is unconnected!", name());
101 }
102
103 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
104 if (*pi)
105 (*pi)->sendStatusChange(Port::RangeChange);
106 }
107}
108
109PhysicalMemory::~PhysicalMemory()
110{
111 if (pmemAddr)
97 munmap((char*)pmemAddr, params()->range.size());
98 //Remove memPorts?
112 munmap((char*)pmemAddr, size());
99}
100
101Addr
102PhysicalMemory::new_page()
103{
104 Addr return_addr = pagePtr << LogVMPageSize;
105 return_addr += start();
106
107 ++pagePtr;
108 return return_addr;
109}
110
111unsigned
112PhysicalMemory::deviceBlockSize() const
113{
114 //Can accept anysize request
115 return 0;
116}
117
118Tick
119PhysicalMemory::calculateLatency(PacketPtr pkt)
120{
121 Tick latency = lat;
122 if (lat_var != 0)
123 latency += random_mt.random<Tick>(0, lat_var);
124 return latency;
125}
126
127
128
129// Add load-locked to tracking list. Should only be called if the
130// operation is a load and the LLSC flag is set.
131void
132PhysicalMemory::trackLoadLocked(PacketPtr pkt)
133{
134 Request *req = pkt->req;
135 Addr paddr = LockedAddr::mask(req->getPaddr());
136
137 // first we check if we already have a locked addr for this
138 // xc. Since each xc only gets one, we just update the
139 // existing record with the new address.
140 list<LockedAddr>::iterator i;
141
142 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
143 if (i->matchesContext(req)) {
144 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
145 req->contextId(), paddr);
146 i->addr = paddr;
147 return;
148 }
149 }
150
151 // no record for this xc: need to allocate a new one
152 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
153 req->contextId(), paddr);
154 lockedAddrList.push_front(LockedAddr(req));
155}
156
157
158// Called on *writes* only... both regular stores and
159// store-conditional operations. Check for conventional stores which
160// conflict with locked addresses, and for success/failure of store
161// conditionals.
162bool
163PhysicalMemory::checkLockedAddrList(PacketPtr pkt)
164{
165 Request *req = pkt->req;
166 Addr paddr = LockedAddr::mask(req->getPaddr());
167 bool isLLSC = pkt->isLLSC();
168
169 // Initialize return value. Non-conditional stores always
170 // succeed. Assume conditional stores will fail until proven
171 // otherwise.
172 bool success = !isLLSC;
173
174 // Iterate over list. Note that there could be multiple matching
175 // records, as more than one context could have done a load locked
176 // to this location.
177 list<LockedAddr>::iterator i = lockedAddrList.begin();
178
179 while (i != lockedAddrList.end()) {
180
181 if (i->addr == paddr) {
182 // we have a matching address
183
184 if (isLLSC && i->matchesContext(req)) {
185 // it's a store conditional, and as far as the memory
186 // system can tell, the requesting context's lock is
187 // still valid.
188 DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
189 req->contextId(), paddr);
190 success = true;
191 }
192
193 // Get rid of our record of this lock and advance to next
194 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
195 i->contextId, paddr);
196 i = lockedAddrList.erase(i);
197 }
198 else {
199 // no match: advance to next record
200 ++i;
201 }
202 }
203
204 if (isLLSC) {
205 req->setExtraData(success ? 1 : 0);
206 }
207
208 return success;
209}
210
211
212#if TRACING_ON
213
214#define CASE(A, T) \
215 case sizeof(T): \
216 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
217 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
218 break
219
220
221#define TRACE_PACKET(A) \
222 do { \
223 switch (pkt->getSize()) { \
224 CASE(A, uint64_t); \
225 CASE(A, uint32_t); \
226 CASE(A, uint16_t); \
227 CASE(A, uint8_t); \
228 default: \
229 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
230 A, pkt->getSize(), pkt->getAddr()); \
231 } \
232 } while (0)
233
234#else
235
236#define TRACE_PACKET(A)
237
238#endif
239
240Tick
241PhysicalMemory::doAtomicAccess(PacketPtr pkt)
242{
243 assert(pkt->getAddr() >= start() &&
244 pkt->getAddr() + pkt->getSize() <= start() + size());
245
246 if (pkt->memInhibitAsserted()) {
247 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
248 pkt->getAddr());
249 return 0;
250 }
251
252 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
253
254 if (pkt->cmd == MemCmd::SwapReq) {
255 IntReg overwrite_val;
256 bool overwrite_mem;
257 uint64_t condition_val64;
258 uint32_t condition_val32;
259
260 if (!pmemAddr)
261 panic("Swap only works if there is real memory (i.e. null=False)");
262 assert(sizeof(IntReg) >= pkt->getSize());
263
264 overwrite_mem = true;
265 // keep a copy of our possible write value, and copy what is at the
266 // memory address into the packet
267 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
268 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
269
270 if (pkt->req->isCondSwap()) {
271 if (pkt->getSize() == sizeof(uint64_t)) {
272 condition_val64 = pkt->req->getExtraData();
273 overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
274 sizeof(uint64_t));
275 } else if (pkt->getSize() == sizeof(uint32_t)) {
276 condition_val32 = (uint32_t)pkt->req->getExtraData();
277 overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
278 sizeof(uint32_t));
279 } else
280 panic("Invalid size for conditional read/write\n");
281 }
282
283 if (overwrite_mem)
284 std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
285
286 assert(!pkt->req->isInstFetch());
287 TRACE_PACKET("Read/Write");
288 } else if (pkt->isRead()) {
289 assert(!pkt->isWrite());
290 if (pkt->isLLSC()) {
291 trackLoadLocked(pkt);
292 }
293 if (pmemAddr)
294 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
295 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
296 } else if (pkt->isWrite()) {
297 if (writeOK(pkt)) {
298 if (pmemAddr)
299 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
300 assert(!pkt->req->isInstFetch());
301 TRACE_PACKET("Write");
302 }
303 } else if (pkt->isInvalidate()) {
304 //upgrade or invalidate
305 if (pkt->needsResponse()) {
306 pkt->makeAtomicResponse();
307 }
308 } else {
309 panic("unimplemented");
310 }
311
312 if (pkt->needsResponse()) {
313 pkt->makeAtomicResponse();
314 }
315 return calculateLatency(pkt);
316}
317
318
319void
320PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
321{
322 assert(pkt->getAddr() >= start() &&
323 pkt->getAddr() + pkt->getSize() <= start() + size());
324
325
326 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
327
328 if (pkt->isRead()) {
329 if (pmemAddr)
330 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
331 TRACE_PACKET("Read");
332 pkt->makeAtomicResponse();
333 } else if (pkt->isWrite()) {
334 if (pmemAddr)
335 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
336 TRACE_PACKET("Write");
337 pkt->makeAtomicResponse();
338 } else if (pkt->isPrint()) {
339 Packet::PrintReqState *prs =
340 dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
341 // Need to call printLabels() explicitly since we're not going
342 // through printObj().
343 prs->printLabels();
344 // Right now we just print the single byte at the specified address.
345 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
346 } else {
347 panic("PhysicalMemory: unimplemented functional command %s",
348 pkt->cmdString());
349 }
350}
351
352
353Port *
354PhysicalMemory::getPort(const std::string &if_name, int idx)
355{
356 // Accept request for "functional" port for backwards compatibility
357 // with places where this function is called from C++. I'd prefer
358 // to move all these into Python someday.
359 if (if_name == "functional") {
360 return new MemoryPort(csprintf("%s-functional", name()), this);
361 }
362
363 if (if_name != "port") {
364 panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
365 }
366
367 if (idx >= (int)ports.size()) {
368 ports.resize(idx + 1);
369 }
370
371 if (ports[idx] != NULL) {
372 panic("PhysicalMemory::getPort: port %d already assigned", idx);
373 }
374
375 MemoryPort *port =
376 new MemoryPort(csprintf("%s-port%d", name(), idx), this);
377
378 ports[idx] = port;
379 return port;
380}
381
382
383void
384PhysicalMemory::recvStatusChange(Port::Status status)
385{
386}
387
388PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
389 PhysicalMemory *_memory)
390 : SimpleTimingPort(_name, _memory), memory(_memory)
391{ }
392
393void
394PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
395{
396 memory->recvStatusChange(status);
397}
398
399void
400PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
401 bool &snoop)
402{
403 memory->getAddressRanges(resp, snoop);
404}
405
406void
407PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
408{
409 snoop = false;
410 resp.clear();
113}
114
115Addr
116PhysicalMemory::new_page()
117{
118 Addr return_addr = pagePtr << LogVMPageSize;
119 return_addr += start();
120
121 ++pagePtr;
122 return return_addr;
123}
124
125unsigned
126PhysicalMemory::deviceBlockSize() const
127{
128 //Can accept anysize request
129 return 0;
130}
131
132Tick
133PhysicalMemory::calculateLatency(PacketPtr pkt)
134{
135 Tick latency = lat;
136 if (lat_var != 0)
137 latency += random_mt.random<Tick>(0, lat_var);
138 return latency;
139}
140
141
142
143// Add load-locked to tracking list. Should only be called if the
144// operation is a load and the LLSC flag is set.
145void
146PhysicalMemory::trackLoadLocked(PacketPtr pkt)
147{
148 Request *req = pkt->req;
149 Addr paddr = LockedAddr::mask(req->getPaddr());
150
151 // first we check if we already have a locked addr for this
152 // xc. Since each xc only gets one, we just update the
153 // existing record with the new address.
154 list<LockedAddr>::iterator i;
155
156 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
157 if (i->matchesContext(req)) {
158 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
159 req->contextId(), paddr);
160 i->addr = paddr;
161 return;
162 }
163 }
164
165 // no record for this xc: need to allocate a new one
166 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
167 req->contextId(), paddr);
168 lockedAddrList.push_front(LockedAddr(req));
169}
170
171
172// Called on *writes* only... both regular stores and
173// store-conditional operations. Check for conventional stores which
174// conflict with locked addresses, and for success/failure of store
175// conditionals.
176bool
177PhysicalMemory::checkLockedAddrList(PacketPtr pkt)
178{
179 Request *req = pkt->req;
180 Addr paddr = LockedAddr::mask(req->getPaddr());
181 bool isLLSC = pkt->isLLSC();
182
183 // Initialize return value. Non-conditional stores always
184 // succeed. Assume conditional stores will fail until proven
185 // otherwise.
186 bool success = !isLLSC;
187
188 // Iterate over list. Note that there could be multiple matching
189 // records, as more than one context could have done a load locked
190 // to this location.
191 list<LockedAddr>::iterator i = lockedAddrList.begin();
192
193 while (i != lockedAddrList.end()) {
194
195 if (i->addr == paddr) {
196 // we have a matching address
197
198 if (isLLSC && i->matchesContext(req)) {
199 // it's a store conditional, and as far as the memory
200 // system can tell, the requesting context's lock is
201 // still valid.
202 DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
203 req->contextId(), paddr);
204 success = true;
205 }
206
207 // Get rid of our record of this lock and advance to next
208 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
209 i->contextId, paddr);
210 i = lockedAddrList.erase(i);
211 }
212 else {
213 // no match: advance to next record
214 ++i;
215 }
216 }
217
218 if (isLLSC) {
219 req->setExtraData(success ? 1 : 0);
220 }
221
222 return success;
223}
224
225
226#if TRACING_ON
227
228#define CASE(A, T) \
229 case sizeof(T): \
230 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
231 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
232 break
233
234
235#define TRACE_PACKET(A) \
236 do { \
237 switch (pkt->getSize()) { \
238 CASE(A, uint64_t); \
239 CASE(A, uint32_t); \
240 CASE(A, uint16_t); \
241 CASE(A, uint8_t); \
242 default: \
243 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
244 A, pkt->getSize(), pkt->getAddr()); \
245 } \
246 } while (0)
247
248#else
249
250#define TRACE_PACKET(A)
251
252#endif
253
254Tick
255PhysicalMemory::doAtomicAccess(PacketPtr pkt)
256{
257 assert(pkt->getAddr() >= start() &&
258 pkt->getAddr() + pkt->getSize() <= start() + size());
259
260 if (pkt->memInhibitAsserted()) {
261 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
262 pkt->getAddr());
263 return 0;
264 }
265
266 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
267
268 if (pkt->cmd == MemCmd::SwapReq) {
269 IntReg overwrite_val;
270 bool overwrite_mem;
271 uint64_t condition_val64;
272 uint32_t condition_val32;
273
274 if (!pmemAddr)
275 panic("Swap only works if there is real memory (i.e. null=False)");
276 assert(sizeof(IntReg) >= pkt->getSize());
277
278 overwrite_mem = true;
279 // keep a copy of our possible write value, and copy what is at the
280 // memory address into the packet
281 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
282 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
283
284 if (pkt->req->isCondSwap()) {
285 if (pkt->getSize() == sizeof(uint64_t)) {
286 condition_val64 = pkt->req->getExtraData();
287 overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
288 sizeof(uint64_t));
289 } else if (pkt->getSize() == sizeof(uint32_t)) {
290 condition_val32 = (uint32_t)pkt->req->getExtraData();
291 overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
292 sizeof(uint32_t));
293 } else
294 panic("Invalid size for conditional read/write\n");
295 }
296
297 if (overwrite_mem)
298 std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
299
300 assert(!pkt->req->isInstFetch());
301 TRACE_PACKET("Read/Write");
302 } else if (pkt->isRead()) {
303 assert(!pkt->isWrite());
304 if (pkt->isLLSC()) {
305 trackLoadLocked(pkt);
306 }
307 if (pmemAddr)
308 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
309 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
310 } else if (pkt->isWrite()) {
311 if (writeOK(pkt)) {
312 if (pmemAddr)
313 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
314 assert(!pkt->req->isInstFetch());
315 TRACE_PACKET("Write");
316 }
317 } else if (pkt->isInvalidate()) {
318 //upgrade or invalidate
319 if (pkt->needsResponse()) {
320 pkt->makeAtomicResponse();
321 }
322 } else {
323 panic("unimplemented");
324 }
325
326 if (pkt->needsResponse()) {
327 pkt->makeAtomicResponse();
328 }
329 return calculateLatency(pkt);
330}
331
332
333void
334PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
335{
336 assert(pkt->getAddr() >= start() &&
337 pkt->getAddr() + pkt->getSize() <= start() + size());
338
339
340 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
341
342 if (pkt->isRead()) {
343 if (pmemAddr)
344 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
345 TRACE_PACKET("Read");
346 pkt->makeAtomicResponse();
347 } else if (pkt->isWrite()) {
348 if (pmemAddr)
349 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
350 TRACE_PACKET("Write");
351 pkt->makeAtomicResponse();
352 } else if (pkt->isPrint()) {
353 Packet::PrintReqState *prs =
354 dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
355 // Need to call printLabels() explicitly since we're not going
356 // through printObj().
357 prs->printLabels();
358 // Right now we just print the single byte at the specified address.
359 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
360 } else {
361 panic("PhysicalMemory: unimplemented functional command %s",
362 pkt->cmdString());
363 }
364}
365
366
367Port *
368PhysicalMemory::getPort(const std::string &if_name, int idx)
369{
370 // Accept request for "functional" port for backwards compatibility
371 // with places where this function is called from C++. I'd prefer
372 // to move all these into Python someday.
373 if (if_name == "functional") {
374 return new MemoryPort(csprintf("%s-functional", name()), this);
375 }
376
377 if (if_name != "port") {
378 panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
379 }
380
381 if (idx >= (int)ports.size()) {
382 ports.resize(idx + 1);
383 }
384
385 if (ports[idx] != NULL) {
386 panic("PhysicalMemory::getPort: port %d already assigned", idx);
387 }
388
389 MemoryPort *port =
390 new MemoryPort(csprintf("%s-port%d", name(), idx), this);
391
392 ports[idx] = port;
393 return port;
394}
395
396
397void
398PhysicalMemory::recvStatusChange(Port::Status status)
399{
400}
401
402PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
403 PhysicalMemory *_memory)
404 : SimpleTimingPort(_name, _memory), memory(_memory)
405{ }
406
407void
408PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
409{
410 memory->recvStatusChange(status);
411}
412
413void
414PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
415 bool &snoop)
416{
417 memory->getAddressRanges(resp, snoop);
418}
419
420void
421PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
422{
423 snoop = false;
424 resp.clear();
411 resp.push_back(RangeSize(start(), params()->range.size()));
425 resp.push_back(RangeSize(start(), size()));
412}
413
414unsigned
415PhysicalMemory::MemoryPort::deviceBlockSize() const
416{
417 return memory->deviceBlockSize();
418}
419
420Tick
421PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
422{
423 return memory->doAtomicAccess(pkt);
424}
425
426void
427PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
428{
429 pkt->pushLabel(memory->name());
430
431 if (!checkFunctional(pkt)) {
432 // Default implementation of SimpleTimingPort::recvFunctional()
433 // calls recvAtomic() and throws away the latency; we can save a
434 // little here by just not calculating the latency.
435 memory->doFunctionalAccess(pkt);
436 }
437
438 pkt->popLabel();
439}
440
441unsigned int
442PhysicalMemory::drain(Event *de)
443{
444 int count = 0;
445 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
446 count += (*pi)->drain(de);
447 }
448
449 if (count)
450 changeState(Draining);
451 else
452 changeState(Drained);
453 return count;
454}
455
456void
457PhysicalMemory::serialize(ostream &os)
458{
459 if (!pmemAddr)
460 return;
461
462 gzFile compressedMem;
463 string filename = name() + ".physmem";
464
465 SERIALIZE_SCALAR(filename);
426}
427
428unsigned
429PhysicalMemory::MemoryPort::deviceBlockSize() const
430{
431 return memory->deviceBlockSize();
432}
433
434Tick
435PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
436{
437 return memory->doAtomicAccess(pkt);
438}
439
440void
441PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
442{
443 pkt->pushLabel(memory->name());
444
445 if (!checkFunctional(pkt)) {
446 // Default implementation of SimpleTimingPort::recvFunctional()
447 // calls recvAtomic() and throws away the latency; we can save a
448 // little here by just not calculating the latency.
449 memory->doFunctionalAccess(pkt);
450 }
451
452 pkt->popLabel();
453}
454
455unsigned int
456PhysicalMemory::drain(Event *de)
457{
458 int count = 0;
459 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
460 count += (*pi)->drain(de);
461 }
462
463 if (count)
464 changeState(Draining);
465 else
466 changeState(Drained);
467 return count;
468}
469
470void
471PhysicalMemory::serialize(ostream &os)
472{
473 if (!pmemAddr)
474 return;
475
476 gzFile compressedMem;
477 string filename = name() + ".physmem";
478
479 SERIALIZE_SCALAR(filename);
480 SERIALIZE_SCALAR(_size);
466
467 // write memory file
468 string thefile = Checkpoint::dir() + "/" + filename.c_str();
469 int fd = creat(thefile.c_str(), 0664);
470 if (fd < 0) {
471 perror("creat");
472 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
473 }
474
475 compressedMem = gzdopen(fd, "wb");
476 if (compressedMem == NULL)
477 fatal("Insufficient memory to allocate compression state for %s\n",
478 filename);
479
481
482 // write memory file
483 string thefile = Checkpoint::dir() + "/" + filename.c_str();
484 int fd = creat(thefile.c_str(), 0664);
485 if (fd < 0) {
486 perror("creat");
487 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
488 }
489
490 compressedMem = gzdopen(fd, "wb");
491 if (compressedMem == NULL)
492 fatal("Insufficient memory to allocate compression state for %s\n",
493 filename);
494
480 if (gzwrite(compressedMem, pmemAddr, params()->range.size()) !=
481 (int)params()->range.size()) {
495 if (gzwrite(compressedMem, pmemAddr, size()) != (int)size()) {
482 fatal("Write failed on physical memory checkpoint file '%s'\n",
483 filename);
484 }
485
486 if (gzclose(compressedMem))
487 fatal("Close failed on physical memory checkpoint file '%s'\n",
488 filename);
489}
490
491void
492PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
493{
494 if (!pmemAddr)
495 return;
496
497 gzFile compressedMem;
498 long *tempPage;
499 long *pmem_current;
500 uint64_t curSize;
501 uint32_t bytesRead;
502 const uint32_t chunkSize = 16384;
503
504 string filename;
505
506 UNSERIALIZE_SCALAR(filename);
507
508 filename = cp->cptDir + "/" + filename;
509
510 // mmap memoryfile
511 int fd = open(filename.c_str(), O_RDONLY);
512 if (fd < 0) {
513 perror("open");
514 fatal("Can't open physical memory checkpoint file '%s'", filename);
515 }
516
517 compressedMem = gzdopen(fd, "rb");
518 if (compressedMem == NULL)
519 fatal("Insufficient memory to allocate compression state for %s\n",
520 filename);
521
522 // unmap file that was mmaped in the constructor
523 // This is done here to make sure that gzip and open don't muck with our
524 // nice large space of memory before we reallocate it
496 fatal("Write failed on physical memory checkpoint file '%s'\n",
497 filename);
498 }
499
500 if (gzclose(compressedMem))
501 fatal("Close failed on physical memory checkpoint file '%s'\n",
502 filename);
503}
504
505void
506PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
507{
508 if (!pmemAddr)
509 return;
510
511 gzFile compressedMem;
512 long *tempPage;
513 long *pmem_current;
514 uint64_t curSize;
515 uint32_t bytesRead;
516 const uint32_t chunkSize = 16384;
517
518 string filename;
519
520 UNSERIALIZE_SCALAR(filename);
521
522 filename = cp->cptDir + "/" + filename;
523
524 // mmap memoryfile
525 int fd = open(filename.c_str(), O_RDONLY);
526 if (fd < 0) {
527 perror("open");
528 fatal("Can't open physical memory checkpoint file '%s'", filename);
529 }
530
531 compressedMem = gzdopen(fd, "rb");
532 if (compressedMem == NULL)
533 fatal("Insufficient memory to allocate compression state for %s\n",
534 filename);
535
536 // unmap file that was mmaped in the constructor
537 // This is done here to make sure that gzip and open don't muck with our
538 // nice large space of memory before we reallocate it
525 munmap((char*)pmemAddr, params()->range.size());
539 munmap((char*)pmemAddr, size());
526
540
527 pmemAddr = (uint8_t *)mmap(NULL, params()->range.size(),
541 UNSERIALIZE_SCALAR(_size);
542 if (size() > params()->range.size())
543 fatal("Memory size has changed!\n");
544
545 pmemAddr = (uint8_t *)mmap(NULL, size(),
528 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
529
530 if (pmemAddr == (void *)MAP_FAILED) {
531 perror("mmap");
532 fatal("Could not mmap physical memory!\n");
533 }
534
535 curSize = 0;
536 tempPage = (long*)malloc(chunkSize);
537 if (tempPage == NULL)
538 fatal("Unable to malloc memory to read file %s\n", filename);
539
540 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
546 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
547
548 if (pmemAddr == (void *)MAP_FAILED) {
549 perror("mmap");
550 fatal("Could not mmap physical memory!\n");
551 }
552
553 curSize = 0;
554 tempPage = (long*)malloc(chunkSize);
555 if (tempPage == NULL)
556 fatal("Unable to malloc memory to read file %s\n", filename);
557
558 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
541 while (curSize < params()->range.size()) {
559 while (curSize < size()) {
542 bytesRead = gzread(compressedMem, tempPage, chunkSize);
543 if (bytesRead == 0)
544 break;
545
546 assert(bytesRead % sizeof(long) == 0);
547
548 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
549 {
550 if (*(tempPage+x) != 0) {
551 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
552 *pmem_current = *(tempPage+x);
553 }
554 }
555 curSize += bytesRead;
556 }
557
558 free(tempPage);
559
560 if (gzclose(compressedMem))
561 fatal("Close failed on physical memory checkpoint file '%s'\n",
562 filename);
563
564}
565
566PhysicalMemory *
567PhysicalMemoryParams::create()
568{
569 return new PhysicalMemory(this);
570}
560 bytesRead = gzread(compressedMem, tempPage, chunkSize);
561 if (bytesRead == 0)
562 break;
563
564 assert(bytesRead % sizeof(long) == 0);
565
566 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
567 {
568 if (*(tempPage+x) != 0) {
569 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
570 *pmem_current = *(tempPage+x);
571 }
572 }
573 curSize += bytesRead;
574 }
575
576 free(tempPage);
577
578 if (gzclose(compressedMem))
579 fatal("Close failed on physical memory checkpoint file '%s'\n",
580 filename);
581
582}
583
584PhysicalMemory *
585PhysicalMemoryParams::create()
586{
587 return new PhysicalMemory(this);
588}