blob: fa9ec7e2370342131bf51a66d06d0b7f17c687f5 [file] [log] [blame]
Ron Dreslinski4e366782006-02-03 14:54:37 -05001/*
Ali Saidia1e82252010-11-08 13:58:25 -06002 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
Ron Dreslinski4e366782006-02-03 14:54:37 -050014 * Copyright (c) 2001-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ali Saidicb0cf2d2006-05-31 19:26:56 -040039 *
40 * Authors: Ron Dreslinski
Ali Saidi851f91f2006-07-20 19:03:47 -040041 * Ali Saidi
Ron Dreslinski4e366782006-02-03 14:54:37 -050042 */
43
Ron Dreslinski4e366782006-02-03 14:54:37 -050044#include <sys/mman.h>
Nathan Binkert39a05562011-04-15 10:44:06 -070045#include <sys/types.h>
Ali Saidic779af42010-11-08 13:58:24 -060046#include <sys/user.h>
Ron Dreslinski4e366782006-02-03 14:54:37 -050047#include <fcntl.h>
48#include <unistd.h>
49#include <zlib.h>
50
Nathan Binkert39a05562011-04-15 10:44:06 -070051#include <cerrno>
Nathan Binkert2c5fe6f2009-11-04 16:57:01 -080052#include <cstdio>
Ron Dreslinski4e366782006-02-03 14:54:37 -050053#include <iostream>
54#include <string>
55
Ali Saidif05f35d2011-02-23 15:10:49 -060056#include "arch/isa_traits.hh"
Gabe Blackb398b8f2009-07-08 23:02:21 -070057#include "arch/registers.hh"
Ali Saidic779af42010-11-08 13:58:24 -060058#include "base/intmath.hh"
Ron Dreslinski4e366782006-02-03 14:54:37 -050059#include "base/misc.hh"
Ali Saidife12f382008-04-10 14:44:52 -040060#include "base/random.hh"
Nathan Binkert8d2e51c2009-05-17 14:34:52 -070061#include "base/types.hh"
Ron Dreslinski4e366782006-02-03 14:54:37 -050062#include "config/full_system.hh"
Nathan Binkertd9f39c82009-09-23 08:34:21 -070063#include "config/the_isa.hh"
Ali Saidib6dc9022006-12-27 14:32:26 -050064#include "mem/packet_access.hh"
Ron Dreslinski7f114ca2006-02-15 14:21:09 -050065#include "mem/physical.hh"
Ron Dreslinski8fc06582006-02-23 13:51:54 -050066#include "sim/eventq.hh"
Ron Dreslinski7f114ca2006-02-15 14:21:09 -050067
Ron Dreslinski4e366782006-02-03 14:54:37 -050068using namespace std;
Gabe Blackf1023652006-03-09 19:21:35 -050069using namespace TheISA;
Ron Dreslinski4e366782006-02-03 14:54:37 -050070
Nathan Binkertabc76f22007-07-23 21:51:38 -070071PhysicalMemory::PhysicalMemory(const Params *p)
Ali Saidi34a8e372010-11-19 18:01:01 -060072 : MemObject(p), pmemAddr(NULL), lat(p->latency), lat_var(p->latency_var),
Ali Saidic779af42010-11-08 13:58:24 -060073 _size(params()->range.size()), _start(params()->range.start)
Ron Dreslinski4e366782006-02-03 14:54:37 -050074{
Ali Saidic779af42010-11-08 13:58:24 -060075 if (size() % TheISA::PageBytes != 0)
Ron Dreslinski4e366782006-02-03 14:54:37 -050076 panic("Memory Size not divisible by page size\n");
77
Nathan Binkertfa8f91f2008-06-15 21:39:29 -070078 if (params()->null)
79 return;
80
Ali Saidic779af42010-11-08 13:58:24 -060081
82 if (params()->file == "") {
83 int map_flags = MAP_ANON | MAP_PRIVATE;
84 pmemAddr = (uint8_t *)mmap(NULL, size(),
85 PROT_READ | PROT_WRITE, map_flags, -1, 0);
86 } else {
87 int map_flags = MAP_PRIVATE;
88 int fd = open(params()->file.c_str(), O_RDONLY);
89 _size = lseek(fd, 0, SEEK_END);
90 lseek(fd, 0, SEEK_SET);
91 pmemAddr = (uint8_t *)mmap(NULL, roundUp(size(), PAGE_SIZE),
92 PROT_READ | PROT_WRITE, map_flags, fd, 0);
93 }
Ron Dreslinski4e366782006-02-03 14:54:37 -050094
Ali Saidi2f145ac2006-08-16 19:01:11 -040095 if (pmemAddr == (void *)MAP_FAILED) {
Ron Dreslinski4e366782006-02-03 14:54:37 -050096 perror("mmap");
Ali Saidic779af42010-11-08 13:58:24 -060097 if (params()->file == "")
98 fatal("Could not mmap!\n");
99 else
100 fatal("Could not find file: %s\n", params()->file);
Ron Dreslinski4e366782006-02-03 14:54:37 -0500101 }
102
Gabe Blackf85082e2006-11-22 23:09:27 -0500103 //If requested, initialize all the memory to 0
Nathan Binkertabc76f22007-07-23 21:51:38 -0700104 if (p->zero)
Ali Saidic779af42010-11-08 13:58:24 -0600105 memset(pmemAddr, 0, size());
Ron Dreslinski4e366782006-02-03 14:54:37 -0500106}
107
Ali Saidi93b27112006-04-10 14:40:22 -0400108void
109PhysicalMemory::init()
110{
Steve Reinhardt87adc372007-05-20 18:23:05 -0700111 if (ports.size() == 0) {
112 fatal("PhysicalMemory object %s is unconnected!", name());
113 }
114
Steve Reinhardt03051592007-05-19 00:24:34 -0400115 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
116 if (*pi)
117 (*pi)->sendStatusChange(Port::RangeChange);
118 }
Ali Saidi93b27112006-04-10 14:40:22 -0400119}
120
Ron Dreslinski4e366782006-02-03 14:54:37 -0500121PhysicalMemory::~PhysicalMemory()
122{
Ali Saidi2f145ac2006-08-16 19:01:11 -0400123 if (pmemAddr)
Ali Saidic779af42010-11-08 13:58:24 -0600124 munmap((char*)pmemAddr, size());
Ron Dreslinski4e366782006-02-03 14:54:37 -0500125}
126
Nathan Binkert6faf3772009-06-04 23:21:12 -0700127unsigned
128PhysicalMemory::deviceBlockSize() const
Ron Dreslinski4bd11c12006-02-21 13:39:01 -0500129{
Ron Dreslinski1fff9f52006-02-21 20:04:23 -0500130 //Can accept anysize request
131 return 0;
Ron Dreslinski4bd11c12006-02-21 13:39:01 -0500132}
133
Ali Saidi2f145ac2006-08-16 19:01:11 -0400134Tick
Nathan Binkerta4c6f0d2006-10-20 00:10:12 -0700135PhysicalMemory::calculateLatency(PacketPtr pkt)
Ali Saidi2f145ac2006-08-16 19:01:11 -0400136{
Ali Saidife12f382008-04-10 14:44:52 -0400137 Tick latency = lat;
138 if (lat_var != 0)
139 latency += random_mt.random<Tick>(0, lat_var);
140 return latency;
Ali Saidi2f145ac2006-08-16 19:01:11 -0400141}
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500142
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700143
144
145// Add load-locked to tracking list. Should only be called if the
Gabe Black3e5f4872009-04-19 04:25:01 -0700146// operation is a load and the LLSC flag is set.
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700147void
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700148PhysicalMemory::trackLoadLocked(PacketPtr pkt)
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700149{
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700150 Request *req = pkt->req;
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700151 Addr paddr = LockedAddr::mask(req->getPaddr());
152
153 // first we check if we already have a locked addr for this
154 // xc. Since each xc only gets one, we just update the
155 // existing record with the new address.
156 list<LockedAddr>::iterator i;
157
158 for (i = lockedAddrList.begin(); i != lockedAddrList.end(); ++i) {
159 if (i->matchesContext(req)) {
Lisa Hsud857faf2008-11-02 21:57:07 -0500160 DPRINTF(LLSC, "Modifying lock record: context %d addr %#x\n",
161 req->contextId(), paddr);
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700162 i->addr = paddr;
163 return;
164 }
165 }
166
167 // no record for this xc: need to allocate a new one
Lisa Hsud857faf2008-11-02 21:57:07 -0500168 DPRINTF(LLSC, "Adding lock record: context %d addr %#x\n",
169 req->contextId(), paddr);
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700170 lockedAddrList.push_front(LockedAddr(req));
171}
172
173
174// Called on *writes* only... both regular stores and
175// store-conditional operations. Check for conventional stores which
176// conflict with locked addresses, and for success/failure of store
177// conditionals.
178bool
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700179PhysicalMemory::checkLockedAddrList(PacketPtr pkt)
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700180{
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700181 Request *req = pkt->req;
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700182 Addr paddr = LockedAddr::mask(req->getPaddr());
Gabe Blackbd6f2bb2009-04-19 21:44:15 -0700183 bool isLLSC = pkt->isLLSC();
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700184
185 // Initialize return value. Non-conditional stores always
186 // succeed. Assume conditional stores will fail until proven
187 // otherwise.
Gabe Blackbd6f2bb2009-04-19 21:44:15 -0700188 bool success = !isLLSC;
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700189
190 // Iterate over list. Note that there could be multiple matching
191 // records, as more than one context could have done a load locked
192 // to this location.
193 list<LockedAddr>::iterator i = lockedAddrList.begin();
194
195 while (i != lockedAddrList.end()) {
196
197 if (i->addr == paddr) {
198 // we have a matching address
199
Gabe Blackbd6f2bb2009-04-19 21:44:15 -0700200 if (isLLSC && i->matchesContext(req)) {
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700201 // it's a store conditional, and as far as the memory
202 // system can tell, the requesting context's lock is
203 // still valid.
Lisa Hsud857faf2008-11-02 21:57:07 -0500204 DPRINTF(LLSC, "StCond success: context %d addr %#x\n",
205 req->contextId(), paddr);
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700206 success = true;
207 }
208
209 // Get rid of our record of this lock and advance to next
Lisa Hsud857faf2008-11-02 21:57:07 -0500210 DPRINTF(LLSC, "Erasing lock record: context %d addr %#x\n",
211 i->contextId, paddr);
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700212 i = lockedAddrList.erase(i);
213 }
214 else {
215 // no match: advance to next record
216 ++i;
217 }
218 }
219
Gabe Blackbd6f2bb2009-04-19 21:44:15 -0700220 if (isLLSC) {
Ali Saidib5a4d952007-02-12 13:06:30 -0500221 req->setExtraData(success ? 1 : 0);
Steve Reinhardtd3fba5a2006-10-08 10:53:24 -0700222 }
223
224 return success;
225}
226
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700227
228#if TRACING_ON
229
230#define CASE(A, T) \
231 case sizeof(T): \
Steve Reinhardta13a7062009-08-01 22:50:14 -0700232 DPRINTF(MemoryAccess,"%s of size %i on address 0x%x data 0x%x\n", \
233 A, pkt->getSize(), pkt->getAddr(), pkt->get<T>()); \
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700234 break
235
236
237#define TRACE_PACKET(A) \
238 do { \
239 switch (pkt->getSize()) { \
240 CASE(A, uint64_t); \
241 CASE(A, uint32_t); \
242 CASE(A, uint16_t); \
243 CASE(A, uint8_t); \
244 default: \
Steve Reinhardta13a7062009-08-01 22:50:14 -0700245 DPRINTF(MemoryAccess, "%s of size %i on address 0x%x\n", \
246 A, pkt->getSize(), pkt->getAddr()); \
Ali Saidi05759882011-02-23 15:10:50 -0600247 DDUMP(MemoryAccess, pkt->getPtr<uint8_t>(), pkt->getSize());\
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700248 } \
249 } while (0)
250
251#else
252
253#define TRACE_PACKET(A)
254
255#endif
256
257Tick
258PhysicalMemory::doAtomicAccess(PacketPtr pkt)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500259{
Ali Saidib5a4d952007-02-12 13:06:30 -0500260 assert(pkt->getAddr() >= start() &&
261 pkt->getAddr() + pkt->getSize() <= start() + size());
Ron Dreslinskiceac38e2006-02-22 17:43:08 -0500262
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700263 if (pkt->memInhibitAsserted()) {
264 DPRINTF(MemoryAccess, "mem inhibited on 0x%x: not responding\n",
265 pkt->getAddr());
266 return 0;
Ron Dreslinskie65f0ce2006-10-08 19:05:48 -0400267 }
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700268
269 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
270
271 if (pkt->cmd == MemCmd::SwapReq) {
Ali Saidib5a4d952007-02-12 13:06:30 -0500272 IntReg overwrite_val;
273 bool overwrite_mem;
274 uint64_t condition_val64;
275 uint32_t condition_val32;
Ali Saidib5a4d952007-02-12 13:06:30 -0500276
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700277 if (!pmemAddr)
278 panic("Swap only works if there is real memory (i.e. null=False)");
Ali Saidib5a4d952007-02-12 13:06:30 -0500279 assert(sizeof(IntReg) >= pkt->getSize());
280
281 overwrite_mem = true;
282 // keep a copy of our possible write value, and copy what is at the
283 // memory address into the packet
Ali Saidif72a9992007-02-12 18:40:08 -0500284 std::memcpy(&overwrite_val, pkt->getPtr<uint8_t>(), pkt->getSize());
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700285 std::memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
Ali Saidib5a4d952007-02-12 13:06:30 -0500286
287 if (pkt->req->isCondSwap()) {
288 if (pkt->getSize() == sizeof(uint64_t)) {
Ali Saidif72a9992007-02-12 18:40:08 -0500289 condition_val64 = pkt->req->getExtraData();
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700290 overwrite_mem = !std::memcmp(&condition_val64, hostAddr,
291 sizeof(uint64_t));
Ali Saidib5a4d952007-02-12 13:06:30 -0500292 } else if (pkt->getSize() == sizeof(uint32_t)) {
Ali Saidif72a9992007-02-12 18:40:08 -0500293 condition_val32 = (uint32_t)pkt->req->getExtraData();
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700294 overwrite_mem = !std::memcmp(&condition_val32, hostAddr,
295 sizeof(uint32_t));
Ali Saidib5a4d952007-02-12 13:06:30 -0500296 } else
297 panic("Invalid size for conditional read/write\n");
298 }
299
300 if (overwrite_mem)
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700301 std::memcpy(hostAddr, &overwrite_val, pkt->getSize());
Ali Saidib5a4d952007-02-12 13:06:30 -0500302
Steve Reinhardta13a7062009-08-01 22:50:14 -0700303 assert(!pkt->req->isInstFetch());
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700304 TRACE_PACKET("Read/Write");
305 } else if (pkt->isRead()) {
306 assert(!pkt->isWrite());
Gabe Blackbd6f2bb2009-04-19 21:44:15 -0700307 if (pkt->isLLSC()) {
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700308 trackLoadLocked(pkt);
Ali Saidib5a4d952007-02-12 13:06:30 -0500309 }
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700310 if (pmemAddr)
311 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
Steve Reinhardta13a7062009-08-01 22:50:14 -0700312 TRACE_PACKET(pkt->req->isInstFetch() ? "IFetch" : "Read");
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700313 } else if (pkt->isWrite()) {
314 if (writeOK(pkt)) {
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700315 if (pmemAddr)
316 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
Steve Reinhardta13a7062009-08-01 22:50:14 -0700317 assert(!pkt->req->isInstFetch());
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700318 TRACE_PACKET("Write");
319 }
320 } else if (pkt->isInvalidate()) {
321 //upgrade or invalidate
322 if (pkt->needsResponse()) {
323 pkt->makeAtomicResponse();
324 }
Ali Saidib5a4d952007-02-12 13:06:30 -0500325 } else {
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500326 panic("unimplemented");
327 }
Steve Reinhardte7f442d2006-03-02 10:31:48 -0500328
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700329 if (pkt->needsResponse()) {
330 pkt->makeAtomicResponse();
331 }
332 return calculateLatency(pkt);
333}
334
335
336void
337PhysicalMemory::doFunctionalAccess(PacketPtr pkt)
338{
339 assert(pkt->getAddr() >= start() &&
340 pkt->getAddr() + pkt->getSize() <= start() + size());
341
Korey Sewell26925902007-11-13 16:58:16 -0500342
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700343 uint8_t *hostAddr = pmemAddr + pkt->getAddr() - start();
344
Steve Reinhardt3952e412008-01-02 12:20:15 -0800345 if (pkt->isRead()) {
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700346 if (pmemAddr)
347 memcpy(pkt->getPtr<uint8_t>(), hostAddr, pkt->getSize());
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700348 TRACE_PACKET("Read");
Steve Reinhardt3952e412008-01-02 12:20:15 -0800349 pkt->makeAtomicResponse();
350 } else if (pkt->isWrite()) {
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700351 if (pmemAddr)
352 memcpy(hostAddr, pkt->getPtr<uint8_t>(), pkt->getSize());
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700353 TRACE_PACKET("Write");
Steve Reinhardt3952e412008-01-02 12:20:15 -0800354 pkt->makeAtomicResponse();
355 } else if (pkt->isPrint()) {
Steve Reinhardtcde5a792008-01-02 13:46:22 -0800356 Packet::PrintReqState *prs =
357 dynamic_cast<Packet::PrintReqState*>(pkt->senderState);
358 // Need to call printLabels() explicitly since we're not going
359 // through printObj().
Steve Reinhardt3952e412008-01-02 12:20:15 -0800360 prs->printLabels();
Steve Reinhardtcde5a792008-01-02 13:46:22 -0800361 // Right now we just print the single byte at the specified address.
Steve Reinhardt3952e412008-01-02 12:20:15 -0800362 ccprintf(prs->os, "%s%#x\n", prs->curPrefix(), *hostAddr);
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700363 } else {
364 panic("PhysicalMemory: unimplemented functional command %s",
365 pkt->cmdString());
366 }
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500367}
368
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700369
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500370Port *
Steve Reinhardte981a972006-06-13 23:19:28 -0400371PhysicalMemory::getPort(const std::string &if_name, int idx)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500372{
Steve Reinhardtaa5b5952007-05-19 01:20:58 -0400373 // Accept request for "functional" port for backwards compatibility
374 // with places where this function is called from C++. I'd prefer
375 // to move all these into Python someday.
376 if (if_name == "functional") {
377 return new MemoryPort(csprintf("%s-functional", name()), this);
378 }
379
Steve Reinhardt03051592007-05-19 00:24:34 -0400380 if (if_name != "port") {
Steve Reinhardte2b329d2006-03-12 17:21:59 -0500381 panic("PhysicalMemory::getPort: unknown port %s requested", if_name);
382 }
Steve Reinhardt03051592007-05-19 00:24:34 -0400383
Nathan Binkert6faf3772009-06-04 23:21:12 -0700384 if (idx >= (int)ports.size()) {
385 ports.resize(idx + 1);
Steve Reinhardt03051592007-05-19 00:24:34 -0400386 }
387
388 if (ports[idx] != NULL) {
389 panic("PhysicalMemory::getPort: port %d already assigned", idx);
390 }
391
392 MemoryPort *port =
393 new MemoryPort(csprintf("%s-port%d", name(), idx), this);
394
395 ports[idx] = port;
396 return port;
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500397}
398
Steve Reinhardt03051592007-05-19 00:24:34 -0400399
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500400void
401PhysicalMemory::recvStatusChange(Port::Status status)
402{
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500403}
404
Steve Reinhardtda6a7b12006-05-26 13:48:35 -0400405PhysicalMemory::MemoryPort::MemoryPort(const std::string &_name,
406 PhysicalMemory *_memory)
Nathan Binkerte0632102008-10-09 04:58:24 -0700407 : SimpleTimingPort(_name, _memory), memory(_memory)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500408{ }
409
410void
411PhysicalMemory::MemoryPort::recvStatusChange(Port::Status status)
412{
413 memory->recvStatusChange(status);
414}
415
416void
Ali Saidi6240f8c2006-04-06 14:57:51 -0400417PhysicalMemory::MemoryPort::getDeviceAddressRanges(AddrRangeList &resp,
Steve Reinhardt41241792007-05-21 23:36:09 -0700418 bool &snoop)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500419{
Ali Saidi6240f8c2006-04-06 14:57:51 -0400420 memory->getAddressRanges(resp, snoop);
Ali Saidib38f67d2006-03-25 18:31:20 -0500421}
422
423void
Steve Reinhardt41241792007-05-21 23:36:09 -0700424PhysicalMemory::getAddressRanges(AddrRangeList &resp, bool &snoop)
Ali Saidib38f67d2006-03-25 18:31:20 -0500425{
Steve Reinhardt41241792007-05-21 23:36:09 -0700426 snoop = false;
Ali Saidi6240f8c2006-04-06 14:57:51 -0400427 resp.clear();
Ali Saidic779af42010-11-08 13:58:24 -0600428 resp.push_back(RangeSize(start(), size()));
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500429}
430
Nathan Binkert6faf3772009-06-04 23:21:12 -0700431unsigned
432PhysicalMemory::MemoryPort::deviceBlockSize() const
Ron Dreslinski8fc06582006-02-23 13:51:54 -0500433{
434 return memory->deviceBlockSize();
435}
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500436
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500437Tick
Nathan Binkerta4c6f0d2006-10-20 00:10:12 -0700438PhysicalMemory::MemoryPort::recvAtomic(PacketPtr pkt)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500439{
Steve Reinhardt35cf19d2007-06-17 17:27:53 -0700440 return memory->doAtomicAccess(pkt);
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500441}
442
443void
Nathan Binkerta4c6f0d2006-10-20 00:10:12 -0700444PhysicalMemory::MemoryPort::recvFunctional(PacketPtr pkt)
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500445{
Steve Reinhardt3952e412008-01-02 12:20:15 -0800446 pkt->pushLabel(memory->name());
447
Steve Reinhardt2f93db62007-07-29 20:17:03 -0700448 if (!checkFunctional(pkt)) {
449 // Default implementation of SimpleTimingPort::recvFunctional()
450 // calls recvAtomic() and throws away the latency; we can save a
451 // little here by just not calculating the latency.
452 memory->doFunctionalAccess(pkt);
453 }
Steve Reinhardt3952e412008-01-02 12:20:15 -0800454
455 pkt->popLabel();
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500456}
457
Ali Saidi851f91f2006-07-20 19:03:47 -0400458unsigned int
459PhysicalMemory::drain(Event *de)
460{
Steve Reinhardt03051592007-05-19 00:24:34 -0400461 int count = 0;
462 for (PortIterator pi = ports.begin(); pi != ports.end(); ++pi) {
463 count += (*pi)->drain(de);
464 }
465
Ali Saidi851f91f2006-07-20 19:03:47 -0400466 if (count)
467 changeState(Draining);
468 else
469 changeState(Drained);
470 return count;
471}
Ron Dreslinskib403abf2006-02-22 17:29:04 -0500472
Ron Dreslinski4e366782006-02-03 14:54:37 -0500473void
474PhysicalMemory::serialize(ostream &os)
475{
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700476 if (!pmemAddr)
477 return;
478
Ron Dreslinski4e366782006-02-03 14:54:37 -0500479 gzFile compressedMem;
480 string filename = name() + ".physmem";
481
Ron Dreslinski4e366782006-02-03 14:54:37 -0500482 SERIALIZE_SCALAR(filename);
Ali Saidic779af42010-11-08 13:58:24 -0600483 SERIALIZE_SCALAR(_size);
Ron Dreslinski4e366782006-02-03 14:54:37 -0500484
485 // write memory file
486 string thefile = Checkpoint::dir() + "/" + filename.c_str();
487 int fd = creat(thefile.c_str(), 0664);
488 if (fd < 0) {
489 perror("creat");
490 fatal("Can't open physical memory checkpoint file '%s'\n", filename);
491 }
492
493 compressedMem = gzdopen(fd, "wb");
494 if (compressedMem == NULL)
495 fatal("Insufficient memory to allocate compression state for %s\n",
496 filename);
497
Ali Saidic779af42010-11-08 13:58:24 -0600498 if (gzwrite(compressedMem, pmemAddr, size()) != (int)size()) {
Ron Dreslinski4e366782006-02-03 14:54:37 -0500499 fatal("Write failed on physical memory checkpoint file '%s'\n",
500 filename);
501 }
502
503 if (gzclose(compressedMem))
504 fatal("Close failed on physical memory checkpoint file '%s'\n",
505 filename);
Ali Saidia1e82252010-11-08 13:58:25 -0600506
507 list<LockedAddr>::iterator i = lockedAddrList.begin();
508
509 vector<Addr> lal_addr;
510 vector<int> lal_cid;
511 while (i != lockedAddrList.end()) {
512 lal_addr.push_back(i->addr);
513 lal_cid.push_back(i->contextId);
514 i++;
515 }
516 arrayParamOut(os, "lal_addr", lal_addr);
517 arrayParamOut(os, "lal_cid", lal_cid);
Ron Dreslinski4e366782006-02-03 14:54:37 -0500518}
519
520void
521PhysicalMemory::unserialize(Checkpoint *cp, const string &section)
522{
Nathan Binkertfa8f91f2008-06-15 21:39:29 -0700523 if (!pmemAddr)
524 return;
525
Ron Dreslinski4e366782006-02-03 14:54:37 -0500526 gzFile compressedMem;
527 long *tempPage;
528 long *pmem_current;
529 uint64_t curSize;
530 uint32_t bytesRead;
Nathan Binkert6faf3772009-06-04 23:21:12 -0700531 const uint32_t chunkSize = 16384;
Ron Dreslinski4e366782006-02-03 14:54:37 -0500532
Ron Dreslinski4e366782006-02-03 14:54:37 -0500533 string filename;
534
Ron Dreslinski4e366782006-02-03 14:54:37 -0500535 UNSERIALIZE_SCALAR(filename);
536
537 filename = cp->cptDir + "/" + filename;
538
539 // mmap memoryfile
540 int fd = open(filename.c_str(), O_RDONLY);
541 if (fd < 0) {
542 perror("open");
543 fatal("Can't open physical memory checkpoint file '%s'", filename);
544 }
545
546 compressedMem = gzdopen(fd, "rb");
547 if (compressedMem == NULL)
548 fatal("Insufficient memory to allocate compression state for %s\n",
549 filename);
550
Gabe Black579c5f02011-03-01 23:18:47 -0800551 // unmap file that was mmapped in the constructor
Ali Saidi2f145ac2006-08-16 19:01:11 -0400552 // This is done here to make sure that gzip and open don't muck with our
553 // nice large space of memory before we reallocate it
Ali Saidic779af42010-11-08 13:58:24 -0600554 munmap((char*)pmemAddr, size());
Ron Dreslinski4e366782006-02-03 14:54:37 -0500555
Ali Saidic779af42010-11-08 13:58:24 -0600556 UNSERIALIZE_SCALAR(_size);
557 if (size() > params()->range.size())
558 fatal("Memory size has changed!\n");
559
560 pmemAddr = (uint8_t *)mmap(NULL, size(),
Nathan Binkertabc76f22007-07-23 21:51:38 -0700561 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
Ron Dreslinski4e366782006-02-03 14:54:37 -0500562
Ali Saidi2f145ac2006-08-16 19:01:11 -0400563 if (pmemAddr == (void *)MAP_FAILED) {
Ron Dreslinski4e366782006-02-03 14:54:37 -0500564 perror("mmap");
565 fatal("Could not mmap physical memory!\n");
566 }
567
568 curSize = 0;
569 tempPage = (long*)malloc(chunkSize);
570 if (tempPage == NULL)
571 fatal("Unable to malloc memory to read file %s\n", filename);
572
573 /* Only copy bytes that are non-zero, so we don't give the VM system hell */
Ali Saidic779af42010-11-08 13:58:24 -0600574 while (curSize < size()) {
Ron Dreslinski4e366782006-02-03 14:54:37 -0500575 bytesRead = gzread(compressedMem, tempPage, chunkSize);
Lisa Hsud6da1722010-01-19 22:03:44 -0800576 if (bytesRead == 0)
577 break;
Ron Dreslinski4e366782006-02-03 14:54:37 -0500578
579 assert(bytesRead % sizeof(long) == 0);
580
Nathan Binkert6faf3772009-06-04 23:21:12 -0700581 for (uint32_t x = 0; x < bytesRead / sizeof(long); x++)
Ron Dreslinski4e366782006-02-03 14:54:37 -0500582 {
583 if (*(tempPage+x) != 0) {
Ali Saidi2f145ac2006-08-16 19:01:11 -0400584 pmem_current = (long*)(pmemAddr + curSize + x * sizeof(long));
Ron Dreslinski4e366782006-02-03 14:54:37 -0500585 *pmem_current = *(tempPage+x);
586 }
587 }
588 curSize += bytesRead;
589 }
590
591 free(tempPage);
592
593 if (gzclose(compressedMem))
594 fatal("Close failed on physical memory checkpoint file '%s'\n",
595 filename);
596
Ali Saidia1e82252010-11-08 13:58:25 -0600597 vector<Addr> lal_addr;
598 vector<int> lal_cid;
599 arrayParamIn(cp, section, "lal_addr", lal_addr);
600 arrayParamIn(cp, section, "lal_cid", lal_cid);
601 for(int i = 0; i < lal_addr.size(); i++)
602 lockedAddrList.push_front(LockedAddr(lal_addr[i], lal_cid[i]));
Ron Dreslinski4e366782006-02-03 14:54:37 -0500603}
604
Nathan Binkertabc76f22007-07-23 21:51:38 -0700605PhysicalMemory *
606PhysicalMemoryParams::create()
Ron Dreslinski4e366782006-02-03 14:54:37 -0500607{
Nathan Binkertabc76f22007-07-23 21:51:38 -0700608 return new PhysicalMemory(this);
Ron Dreslinski4e366782006-02-03 14:54:37 -0500609}