atomic.cc (10030:b531e328342d) atomic.cc (10031:79d034cd6ba3)
1/*
2 * Copyright (c) 2012-2013 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 *
14 * Copyright (c) 2002-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.
39 *
40 * Authors: Steve Reinhardt
41 */
42
43#include "arch/locked_mem.hh"
44#include "arch/mmapped_ipr.hh"
45#include "arch/utility.hh"
46#include "base/bigint.hh"
47#include "base/output.hh"
48#include "config/the_isa.hh"
49#include "cpu/simple/atomic.hh"
50#include "cpu/exetrace.hh"
51#include "debug/Drain.hh"
52#include "debug/ExecFaulting.hh"
53#include "debug/SimpleCPU.hh"
54#include "mem/packet.hh"
55#include "mem/packet_access.hh"
56#include "mem/physical.hh"
57#include "params/AtomicSimpleCPU.hh"
58#include "sim/faults.hh"
59#include "sim/system.hh"
60#include "sim/full_system.hh"
61
62using namespace std;
63using namespace TheISA;
64
65AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU *c)
66 : Event(CPU_Tick_Pri), cpu(c)
67{
68}
69
70
71void
72AtomicSimpleCPU::TickEvent::process()
73{
74 cpu->tick();
75}
76
77const char *
78AtomicSimpleCPU::TickEvent::description() const
79{
80 return "AtomicSimpleCPU tick";
81}
82
83void
84AtomicSimpleCPU::init()
85{
86 BaseCPU::init();
87
88 // Initialise the ThreadContext's memory proxies
89 tcBase()->initMemProxies(tcBase());
90
91 if (FullSystem && !params()->switched_out) {
92 ThreadID size = threadContexts.size();
93 for (ThreadID i = 0; i < size; ++i) {
94 ThreadContext *tc = threadContexts[i];
95 // initialize CPU, including PC
96 TheISA::initCPU(tc, tc->contextId());
97 }
98 }
99
100 // Atomic doesn't do MT right now, so contextId == threadId
101 ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
102 data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
103 data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
104}
105
106AtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams *p)
107 : BaseSimpleCPU(p), tickEvent(this), width(p->width), locked(false),
108 simulate_data_stalls(p->simulate_data_stalls),
109 simulate_inst_stalls(p->simulate_inst_stalls),
110 drain_manager(NULL),
111 icachePort(name() + ".icache_port", this),
112 dcachePort(name() + ".dcache_port", this),
113 fastmem(p->fastmem),
114 simpoint(p->simpoint_profile),
115 intervalSize(p->simpoint_interval),
116 intervalCount(0),
117 intervalDrift(0),
118 simpointStream(NULL),
119 currentBBV(0, 0),
120 currentBBVInstCount(0)
121{
122 _status = Idle;
123
124 if (simpoint) {
125 simpointStream = simout.create(p->simpoint_profile_file, false);
126 }
127}
128
129
130AtomicSimpleCPU::~AtomicSimpleCPU()
131{
132 if (tickEvent.scheduled()) {
133 deschedule(tickEvent);
134 }
135 if (simpointStream) {
136 simout.close(simpointStream);
137 }
138}
139
140unsigned int
141AtomicSimpleCPU::drain(DrainManager *dm)
142{
143 assert(!drain_manager);
144 if (switchedOut())
145 return 0;
146
147 if (!isDrained()) {
148 DPRINTF(Drain, "Requesting drain: %s\n", pcState());
149 drain_manager = dm;
150 return 1;
151 } else {
152 if (tickEvent.scheduled())
153 deschedule(tickEvent);
154
155 DPRINTF(Drain, "Not executing microcode, no need to drain.\n");
156 return 0;
157 }
158}
159
160void
161AtomicSimpleCPU::drainResume()
162{
163 assert(!tickEvent.scheduled());
164 assert(!drain_manager);
165 if (switchedOut())
166 return;
167
168 DPRINTF(SimpleCPU, "Resume\n");
169 verifyMemoryMode();
170
171 assert(!threadContexts.empty());
172 if (threadContexts.size() > 1)
173 fatal("The atomic CPU only supports one thread.\n");
174
175 if (thread->status() == ThreadContext::Active) {
176 schedule(tickEvent, nextCycle());
177 _status = BaseSimpleCPU::Running;
178 notIdleFraction = 1;
179 } else {
180 _status = BaseSimpleCPU::Idle;
181 notIdleFraction = 0;
182 }
183
184 system->totalNumInsts = 0;
185}
186
187bool
188AtomicSimpleCPU::tryCompleteDrain()
189{
190 if (!drain_manager)
191 return false;
192
193 DPRINTF(Drain, "tryCompleteDrain: %s\n", pcState());
194 if (!isDrained())
195 return false;
196
197 DPRINTF(Drain, "CPU done draining, processing drain event\n");
198 drain_manager->signalDrainDone();
199 drain_manager = NULL;
200
201 return true;
202}
203
204
205void
206AtomicSimpleCPU::switchOut()
207{
208 BaseSimpleCPU::switchOut();
209
210 assert(!tickEvent.scheduled());
211 assert(_status == BaseSimpleCPU::Running || _status == Idle);
212 assert(isDrained());
213}
214
215
216void
217AtomicSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
218{
219 BaseSimpleCPU::takeOverFrom(oldCPU);
220
221 // The tick event should have been descheduled by drain()
222 assert(!tickEvent.scheduled());
223
224 ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
225 data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
226 data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
227}
228
229void
230AtomicSimpleCPU::verifyMemoryMode() const
231{
232 if (!system->isAtomicMode()) {
233 fatal("The atomic CPU requires the memory system to be in "
234 "'atomic' mode.\n");
235 }
236}
237
238void
239AtomicSimpleCPU::activateContext(ThreadID thread_num, Cycles delay)
240{
241 DPRINTF(SimpleCPU, "ActivateContext %d (%d cycles)\n", thread_num, delay);
242
243 assert(thread_num == 0);
244 assert(thread);
245
246 assert(_status == Idle);
247 assert(!tickEvent.scheduled());
248
249 notIdleFraction = 1;
250 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
251
252 //Make sure ticks are still on multiples of cycles
253 schedule(tickEvent, clockEdge(delay));
254 _status = BaseSimpleCPU::Running;
255}
256
257
258void
259AtomicSimpleCPU::suspendContext(ThreadID thread_num)
260{
261 DPRINTF(SimpleCPU, "SuspendContext %d\n", thread_num);
262
263 assert(thread_num == 0);
264 assert(thread);
265
266 if (_status == Idle)
267 return;
268
269 assert(_status == BaseSimpleCPU::Running);
270
271 // tick event may not be scheduled if this gets called from inside
272 // an instruction's execution, e.g. "quiesce"
273 if (tickEvent.scheduled())
274 deschedule(tickEvent);
275
276 notIdleFraction = 0;
277 _status = Idle;
278}
279
280
281Tick
282AtomicSimpleCPU::AtomicCPUDPort::recvAtomicSnoop(PacketPtr pkt)
283{
284 DPRINTF(SimpleCPU, "received snoop pkt for addr:%#x %s\n", pkt->getAddr(),
285 pkt->cmdString());
286
287 // if snoop invalidates, release any associated locks
288 if (pkt->isInvalidate()) {
289 DPRINTF(SimpleCPU, "received invalidation for addr:%#x\n",
290 pkt->getAddr());
291 TheISA::handleLockedSnoop(cpu->thread, pkt, cacheBlockMask);
292 }
293
294 return 0;
295}
296
297void
298AtomicSimpleCPU::AtomicCPUDPort::recvFunctionalSnoop(PacketPtr pkt)
299{
300 DPRINTF(SimpleCPU, "received snoop pkt for addr:%#x %s\n", pkt->getAddr(),
301 pkt->cmdString());
302
303 // if snoop invalidates, release any associated locks
304 if (pkt->isInvalidate()) {
305 DPRINTF(SimpleCPU, "received invalidation for addr:%#x\n",
306 pkt->getAddr());
307 TheISA::handleLockedSnoop(cpu->thread, pkt, cacheBlockMask);
308 }
309}
310
311Fault
312AtomicSimpleCPU::readMem(Addr addr, uint8_t * data,
313 unsigned size, unsigned flags)
314{
315 // use the CPU's statically allocated read request and packet objects
316 Request *req = &data_read_req;
317
318 if (traceData) {
319 traceData->setAddr(addr);
320 }
321
322 //The size of the data we're trying to read.
323 int fullSize = size;
324
325 //The address of the second part of this access if it needs to be split
326 //across a cache line boundary.
327 Addr secondAddr = roundDown(addr + size - 1, cacheLineSize());
328
329 if (secondAddr > addr)
330 size = secondAddr - addr;
331
332 dcache_latency = 0;
333
334 req->taskId(taskId());
335 while (1) {
336 req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
337
338 // translate to physical address
339 Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Read);
340
341 // Now do the access.
342 if (fault == NoFault && !req->getFlags().isSet(Request::NO_ACCESS)) {
343 Packet pkt = Packet(req,
344 req->isLLSC() ? MemCmd::LoadLockedReq :
345 MemCmd::ReadReq);
346 pkt.dataStatic(data);
347
348 if (req->isMmappedIpr())
349 dcache_latency += TheISA::handleIprRead(thread->getTC(), &pkt);
350 else {
351 if (fastmem && system->isMemAddr(pkt.getAddr()))
352 system->getPhysMem().access(&pkt);
353 else
354 dcache_latency += dcachePort.sendAtomic(&pkt);
355 }
356 dcache_access = true;
357
358 assert(!pkt.isError());
359
360 if (req->isLLSC()) {
361 TheISA::handleLockedRead(thread, req);
362 }
363 }
364
365 //If there's a fault, return it
366 if (fault != NoFault) {
367 if (req->isPrefetch()) {
368 return NoFault;
369 } else {
370 return fault;
371 }
372 }
373
374 //If we don't need to access a second cache line, stop now.
375 if (secondAddr <= addr)
376 {
377 if (req->isLocked() && fault == NoFault) {
378 assert(!locked);
379 locked = true;
380 }
381 return fault;
382 }
383
384 /*
385 * Set up for accessing the second cache line.
386 */
387
388 //Move the pointer we're reading into to the correct location.
389 data += size;
390 //Adjust the size to get the remaining bytes.
391 size = addr + fullSize - secondAddr;
392 //And access the right address.
393 addr = secondAddr;
394 }
395}
396
397
398Fault
399AtomicSimpleCPU::writeMem(uint8_t *data, unsigned size,
400 Addr addr, unsigned flags, uint64_t *res)
401{
1/*
2 * Copyright (c) 2012-2013 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 *
14 * Copyright (c) 2002-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.
39 *
40 * Authors: Steve Reinhardt
41 */
42
43#include "arch/locked_mem.hh"
44#include "arch/mmapped_ipr.hh"
45#include "arch/utility.hh"
46#include "base/bigint.hh"
47#include "base/output.hh"
48#include "config/the_isa.hh"
49#include "cpu/simple/atomic.hh"
50#include "cpu/exetrace.hh"
51#include "debug/Drain.hh"
52#include "debug/ExecFaulting.hh"
53#include "debug/SimpleCPU.hh"
54#include "mem/packet.hh"
55#include "mem/packet_access.hh"
56#include "mem/physical.hh"
57#include "params/AtomicSimpleCPU.hh"
58#include "sim/faults.hh"
59#include "sim/system.hh"
60#include "sim/full_system.hh"
61
62using namespace std;
63using namespace TheISA;
64
65AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU *c)
66 : Event(CPU_Tick_Pri), cpu(c)
67{
68}
69
70
71void
72AtomicSimpleCPU::TickEvent::process()
73{
74 cpu->tick();
75}
76
77const char *
78AtomicSimpleCPU::TickEvent::description() const
79{
80 return "AtomicSimpleCPU tick";
81}
82
83void
84AtomicSimpleCPU::init()
85{
86 BaseCPU::init();
87
88 // Initialise the ThreadContext's memory proxies
89 tcBase()->initMemProxies(tcBase());
90
91 if (FullSystem && !params()->switched_out) {
92 ThreadID size = threadContexts.size();
93 for (ThreadID i = 0; i < size; ++i) {
94 ThreadContext *tc = threadContexts[i];
95 // initialize CPU, including PC
96 TheISA::initCPU(tc, tc->contextId());
97 }
98 }
99
100 // Atomic doesn't do MT right now, so contextId == threadId
101 ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
102 data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
103 data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
104}
105
106AtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams *p)
107 : BaseSimpleCPU(p), tickEvent(this), width(p->width), locked(false),
108 simulate_data_stalls(p->simulate_data_stalls),
109 simulate_inst_stalls(p->simulate_inst_stalls),
110 drain_manager(NULL),
111 icachePort(name() + ".icache_port", this),
112 dcachePort(name() + ".dcache_port", this),
113 fastmem(p->fastmem),
114 simpoint(p->simpoint_profile),
115 intervalSize(p->simpoint_interval),
116 intervalCount(0),
117 intervalDrift(0),
118 simpointStream(NULL),
119 currentBBV(0, 0),
120 currentBBVInstCount(0)
121{
122 _status = Idle;
123
124 if (simpoint) {
125 simpointStream = simout.create(p->simpoint_profile_file, false);
126 }
127}
128
129
130AtomicSimpleCPU::~AtomicSimpleCPU()
131{
132 if (tickEvent.scheduled()) {
133 deschedule(tickEvent);
134 }
135 if (simpointStream) {
136 simout.close(simpointStream);
137 }
138}
139
140unsigned int
141AtomicSimpleCPU::drain(DrainManager *dm)
142{
143 assert(!drain_manager);
144 if (switchedOut())
145 return 0;
146
147 if (!isDrained()) {
148 DPRINTF(Drain, "Requesting drain: %s\n", pcState());
149 drain_manager = dm;
150 return 1;
151 } else {
152 if (tickEvent.scheduled())
153 deschedule(tickEvent);
154
155 DPRINTF(Drain, "Not executing microcode, no need to drain.\n");
156 return 0;
157 }
158}
159
160void
161AtomicSimpleCPU::drainResume()
162{
163 assert(!tickEvent.scheduled());
164 assert(!drain_manager);
165 if (switchedOut())
166 return;
167
168 DPRINTF(SimpleCPU, "Resume\n");
169 verifyMemoryMode();
170
171 assert(!threadContexts.empty());
172 if (threadContexts.size() > 1)
173 fatal("The atomic CPU only supports one thread.\n");
174
175 if (thread->status() == ThreadContext::Active) {
176 schedule(tickEvent, nextCycle());
177 _status = BaseSimpleCPU::Running;
178 notIdleFraction = 1;
179 } else {
180 _status = BaseSimpleCPU::Idle;
181 notIdleFraction = 0;
182 }
183
184 system->totalNumInsts = 0;
185}
186
187bool
188AtomicSimpleCPU::tryCompleteDrain()
189{
190 if (!drain_manager)
191 return false;
192
193 DPRINTF(Drain, "tryCompleteDrain: %s\n", pcState());
194 if (!isDrained())
195 return false;
196
197 DPRINTF(Drain, "CPU done draining, processing drain event\n");
198 drain_manager->signalDrainDone();
199 drain_manager = NULL;
200
201 return true;
202}
203
204
205void
206AtomicSimpleCPU::switchOut()
207{
208 BaseSimpleCPU::switchOut();
209
210 assert(!tickEvent.scheduled());
211 assert(_status == BaseSimpleCPU::Running || _status == Idle);
212 assert(isDrained());
213}
214
215
216void
217AtomicSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
218{
219 BaseSimpleCPU::takeOverFrom(oldCPU);
220
221 // The tick event should have been descheduled by drain()
222 assert(!tickEvent.scheduled());
223
224 ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
225 data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
226 data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
227}
228
229void
230AtomicSimpleCPU::verifyMemoryMode() const
231{
232 if (!system->isAtomicMode()) {
233 fatal("The atomic CPU requires the memory system to be in "
234 "'atomic' mode.\n");
235 }
236}
237
238void
239AtomicSimpleCPU::activateContext(ThreadID thread_num, Cycles delay)
240{
241 DPRINTF(SimpleCPU, "ActivateContext %d (%d cycles)\n", thread_num, delay);
242
243 assert(thread_num == 0);
244 assert(thread);
245
246 assert(_status == Idle);
247 assert(!tickEvent.scheduled());
248
249 notIdleFraction = 1;
250 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
251
252 //Make sure ticks are still on multiples of cycles
253 schedule(tickEvent, clockEdge(delay));
254 _status = BaseSimpleCPU::Running;
255}
256
257
258void
259AtomicSimpleCPU::suspendContext(ThreadID thread_num)
260{
261 DPRINTF(SimpleCPU, "SuspendContext %d\n", thread_num);
262
263 assert(thread_num == 0);
264 assert(thread);
265
266 if (_status == Idle)
267 return;
268
269 assert(_status == BaseSimpleCPU::Running);
270
271 // tick event may not be scheduled if this gets called from inside
272 // an instruction's execution, e.g. "quiesce"
273 if (tickEvent.scheduled())
274 deschedule(tickEvent);
275
276 notIdleFraction = 0;
277 _status = Idle;
278}
279
280
281Tick
282AtomicSimpleCPU::AtomicCPUDPort::recvAtomicSnoop(PacketPtr pkt)
283{
284 DPRINTF(SimpleCPU, "received snoop pkt for addr:%#x %s\n", pkt->getAddr(),
285 pkt->cmdString());
286
287 // if snoop invalidates, release any associated locks
288 if (pkt->isInvalidate()) {
289 DPRINTF(SimpleCPU, "received invalidation for addr:%#x\n",
290 pkt->getAddr());
291 TheISA::handleLockedSnoop(cpu->thread, pkt, cacheBlockMask);
292 }
293
294 return 0;
295}
296
297void
298AtomicSimpleCPU::AtomicCPUDPort::recvFunctionalSnoop(PacketPtr pkt)
299{
300 DPRINTF(SimpleCPU, "received snoop pkt for addr:%#x %s\n", pkt->getAddr(),
301 pkt->cmdString());
302
303 // if snoop invalidates, release any associated locks
304 if (pkt->isInvalidate()) {
305 DPRINTF(SimpleCPU, "received invalidation for addr:%#x\n",
306 pkt->getAddr());
307 TheISA::handleLockedSnoop(cpu->thread, pkt, cacheBlockMask);
308 }
309}
310
311Fault
312AtomicSimpleCPU::readMem(Addr addr, uint8_t * data,
313 unsigned size, unsigned flags)
314{
315 // use the CPU's statically allocated read request and packet objects
316 Request *req = &data_read_req;
317
318 if (traceData) {
319 traceData->setAddr(addr);
320 }
321
322 //The size of the data we're trying to read.
323 int fullSize = size;
324
325 //The address of the second part of this access if it needs to be split
326 //across a cache line boundary.
327 Addr secondAddr = roundDown(addr + size - 1, cacheLineSize());
328
329 if (secondAddr > addr)
330 size = secondAddr - addr;
331
332 dcache_latency = 0;
333
334 req->taskId(taskId());
335 while (1) {
336 req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
337
338 // translate to physical address
339 Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Read);
340
341 // Now do the access.
342 if (fault == NoFault && !req->getFlags().isSet(Request::NO_ACCESS)) {
343 Packet pkt = Packet(req,
344 req->isLLSC() ? MemCmd::LoadLockedReq :
345 MemCmd::ReadReq);
346 pkt.dataStatic(data);
347
348 if (req->isMmappedIpr())
349 dcache_latency += TheISA::handleIprRead(thread->getTC(), &pkt);
350 else {
351 if (fastmem && system->isMemAddr(pkt.getAddr()))
352 system->getPhysMem().access(&pkt);
353 else
354 dcache_latency += dcachePort.sendAtomic(&pkt);
355 }
356 dcache_access = true;
357
358 assert(!pkt.isError());
359
360 if (req->isLLSC()) {
361 TheISA::handleLockedRead(thread, req);
362 }
363 }
364
365 //If there's a fault, return it
366 if (fault != NoFault) {
367 if (req->isPrefetch()) {
368 return NoFault;
369 } else {
370 return fault;
371 }
372 }
373
374 //If we don't need to access a second cache line, stop now.
375 if (secondAddr <= addr)
376 {
377 if (req->isLocked() && fault == NoFault) {
378 assert(!locked);
379 locked = true;
380 }
381 return fault;
382 }
383
384 /*
385 * Set up for accessing the second cache line.
386 */
387
388 //Move the pointer we're reading into to the correct location.
389 data += size;
390 //Adjust the size to get the remaining bytes.
391 size = addr + fullSize - secondAddr;
392 //And access the right address.
393 addr = secondAddr;
394 }
395}
396
397
398Fault
399AtomicSimpleCPU::writeMem(uint8_t *data, unsigned size,
400 Addr addr, unsigned flags, uint64_t *res)
401{
402
403 static uint8_t zero_array[64] = {};
404
405 if (data == NULL) {
406 assert(size <= 64);
407 assert(flags & Request::CACHE_BLOCK_ZERO);
408 // This must be a cache block cleaning request
409 data = zero_array;
410 }
411
402 // use the CPU's statically allocated write request and packet objects
403 Request *req = &data_write_req;
404
405 if (traceData) {
406 traceData->setAddr(addr);
407 }
408
409 //The size of the data we're trying to read.
410 int fullSize = size;
411
412 //The address of the second part of this access if it needs to be split
413 //across a cache line boundary.
414 Addr secondAddr = roundDown(addr + size - 1, cacheLineSize());
415
416 if(secondAddr > addr)
417 size = secondAddr - addr;
418
419 dcache_latency = 0;
420
421 req->taskId(taskId());
422 while(1) {
423 req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
424
425 // translate to physical address
426 Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Write);
427
428 // Now do the access.
429 if (fault == NoFault) {
430 MemCmd cmd = MemCmd::WriteReq; // default
431 bool do_access = true; // flag to suppress cache access
432
433 if (req->isLLSC()) {
434 cmd = MemCmd::StoreCondReq;
435 do_access = TheISA::handleLockedWrite(thread, req, dcachePort.cacheBlockMask);
436 } else if (req->isSwap()) {
437 cmd = MemCmd::SwapReq;
438 if (req->isCondSwap()) {
439 assert(res);
440 req->setExtraData(*res);
441 }
442 }
443
444 if (do_access && !req->getFlags().isSet(Request::NO_ACCESS)) {
445 Packet pkt = Packet(req, cmd);
446 pkt.dataStatic(data);
447
448 if (req->isMmappedIpr()) {
449 dcache_latency +=
450 TheISA::handleIprWrite(thread->getTC(), &pkt);
451 } else {
452 if (fastmem && system->isMemAddr(pkt.getAddr()))
453 system->getPhysMem().access(&pkt);
454 else
455 dcache_latency += dcachePort.sendAtomic(&pkt);
456 }
457 dcache_access = true;
458 assert(!pkt.isError());
459
460 if (req->isSwap()) {
461 assert(res);
462 memcpy(res, pkt.getPtr<uint8_t>(), fullSize);
463 }
464 }
465
466 if (res && !req->isSwap()) {
467 *res = req->getExtraData();
468 }
469 }
470
471 //If there's a fault or we don't need to access a second cache line,
472 //stop now.
473 if (fault != NoFault || secondAddr <= addr)
474 {
475 if (req->isLocked() && fault == NoFault) {
476 assert(locked);
477 locked = false;
478 }
479 if (fault != NoFault && req->isPrefetch()) {
480 return NoFault;
481 } else {
482 return fault;
483 }
484 }
485
486 /*
487 * Set up for accessing the second cache line.
488 */
489
490 //Move the pointer we're reading into to the correct location.
491 data += size;
492 //Adjust the size to get the remaining bytes.
493 size = addr + fullSize - secondAddr;
494 //And access the right address.
495 addr = secondAddr;
496 }
497}
498
499
500void
501AtomicSimpleCPU::tick()
502{
503 DPRINTF(SimpleCPU, "Tick\n");
504
505 Tick latency = 0;
506
507 for (int i = 0; i < width || locked; ++i) {
508 numCycles++;
509
510 if (!curStaticInst || !curStaticInst->isDelayedCommit())
511 checkForInterrupts();
512
513 checkPcEventQueue();
514 // We must have just got suspended by a PC event
515 if (_status == Idle) {
516 tryCompleteDrain();
517 return;
518 }
519
520 Fault fault = NoFault;
521
522 TheISA::PCState pcState = thread->pcState();
523
524 bool needToFetch = !isRomMicroPC(pcState.microPC()) &&
525 !curMacroStaticInst;
526 if (needToFetch) {
527 ifetch_req.taskId(taskId());
528 setupFetchRequest(&ifetch_req);
529 fault = thread->itb->translateAtomic(&ifetch_req, tc,
530 BaseTLB::Execute);
531 }
532
533 if (fault == NoFault) {
534 Tick icache_latency = 0;
535 bool icache_access = false;
536 dcache_access = false; // assume no dcache access
537
538 if (needToFetch) {
539 // This is commented out because the decoder would act like
540 // a tiny cache otherwise. It wouldn't be flushed when needed
541 // like the I cache. It should be flushed, and when that works
542 // this code should be uncommented.
543 //Fetch more instruction memory if necessary
544 //if(decoder.needMoreBytes())
545 //{
546 icache_access = true;
547 Packet ifetch_pkt = Packet(&ifetch_req, MemCmd::ReadReq);
548 ifetch_pkt.dataStatic(&inst);
549
550 if (fastmem && system->isMemAddr(ifetch_pkt.getAddr()))
551 system->getPhysMem().access(&ifetch_pkt);
552 else
553 icache_latency = icachePort.sendAtomic(&ifetch_pkt);
554
555 assert(!ifetch_pkt.isError());
556
557 // ifetch_req is initialized to read the instruction directly
558 // into the CPU object's inst field.
559 //}
560 }
561
562 preExecute();
563
564 if (curStaticInst) {
565 fault = curStaticInst->execute(this, traceData);
566
567 // keep an instruction count
568 if (fault == NoFault)
569 countInst();
570 else if (traceData && !DTRACE(ExecFaulting)) {
571 delete traceData;
572 traceData = NULL;
573 }
574
575 postExecute();
576 }
577
578 // @todo remove me after debugging with legion done
579 if (curStaticInst && (!curStaticInst->isMicroop() ||
580 curStaticInst->isFirstMicroop()))
581 instCnt++;
582
583 // profile for SimPoints if enabled and macro inst is finished
584 if (simpoint && curStaticInst && (fault == NoFault) &&
585 (!curStaticInst->isMicroop() ||
586 curStaticInst->isLastMicroop())) {
587 profileSimPoint();
588 }
589
590 Tick stall_ticks = 0;
591 if (simulate_inst_stalls && icache_access)
592 stall_ticks += icache_latency;
593
594 if (simulate_data_stalls && dcache_access)
595 stall_ticks += dcache_latency;
596
597 if (stall_ticks) {
598 // the atomic cpu does its accounting in ticks, so
599 // keep counting in ticks but round to the clock
600 // period
601 latency += divCeil(stall_ticks, clockPeriod()) *
602 clockPeriod();
603 }
604
605 }
606 if(fault != NoFault || !stayAtPC)
607 advancePC(fault);
608 }
609
610 if (tryCompleteDrain())
611 return;
612
613 // instruction takes at least one cycle
614 if (latency < clockPeriod())
615 latency = clockPeriod();
616
617 if (_status != Idle)
618 schedule(tickEvent, curTick() + latency);
619}
620
621
622void
623AtomicSimpleCPU::printAddr(Addr a)
624{
625 dcachePort.printAddr(a);
626}
627
628void
629AtomicSimpleCPU::profileSimPoint()
630{
631 if (!currentBBVInstCount)
632 currentBBV.first = thread->pcState().instAddr();
633
634 ++intervalCount;
635 ++currentBBVInstCount;
636
637 // If inst is control inst, assume end of basic block.
638 if (curStaticInst->isControl()) {
639 currentBBV.second = thread->pcState().instAddr();
640
641 auto map_itr = bbMap.find(currentBBV);
642 if (map_itr == bbMap.end()){
643 // If a new (previously unseen) basic block is found,
644 // add a new unique id, record num of insts and insert into bbMap.
645 BBInfo info;
646 info.id = bbMap.size() + 1;
647 info.insts = currentBBVInstCount;
648 info.count = currentBBVInstCount;
649 bbMap.insert(std::make_pair(currentBBV, info));
650 } else {
651 // If basic block is seen before, just increment the count by the
652 // number of insts in basic block.
653 BBInfo& info = map_itr->second;
654 info.count += currentBBVInstCount;
655 }
656 currentBBVInstCount = 0;
657
658 // Reached end of interval if the sum of the current inst count
659 // (intervalCount) and the excessive inst count from the previous
660 // interval (intervalDrift) is greater than/equal to the interval size.
661 if (intervalCount + intervalDrift >= intervalSize) {
662 // summarize interval and display BBV info
663 std::vector<pair<uint64_t, uint64_t> > counts;
664 for (auto map_itr = bbMap.begin(); map_itr != bbMap.end();
665 ++map_itr) {
666 BBInfo& info = map_itr->second;
667 if (info.count != 0) {
668 counts.push_back(std::make_pair(info.id, info.count));
669 info.count = 0;
670 }
671 }
672 std::sort(counts.begin(), counts.end());
673
674 // Print output BBV info
675 *simpointStream << "T";
676 for (auto cnt_itr = counts.begin(); cnt_itr != counts.end();
677 ++cnt_itr) {
678 *simpointStream << ":" << cnt_itr->first
679 << ":" << cnt_itr->second << " ";
680 }
681 *simpointStream << "\n";
682
683 intervalDrift = (intervalCount + intervalDrift) - intervalSize;
684 intervalCount = 0;
685 }
686 }
687}
688
689////////////////////////////////////////////////////////////////////////
690//
691// AtomicSimpleCPU Simulation Object
692//
693AtomicSimpleCPU *
694AtomicSimpleCPUParams::create()
695{
696 numThreads = 1;
697 if (!FullSystem && workload.size() != 1)
698 panic("only one workload allowed");
699 return new AtomicSimpleCPU(this);
700}
412 // use the CPU's statically allocated write request and packet objects
413 Request *req = &data_write_req;
414
415 if (traceData) {
416 traceData->setAddr(addr);
417 }
418
419 //The size of the data we're trying to read.
420 int fullSize = size;
421
422 //The address of the second part of this access if it needs to be split
423 //across a cache line boundary.
424 Addr secondAddr = roundDown(addr + size - 1, cacheLineSize());
425
426 if(secondAddr > addr)
427 size = secondAddr - addr;
428
429 dcache_latency = 0;
430
431 req->taskId(taskId());
432 while(1) {
433 req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
434
435 // translate to physical address
436 Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Write);
437
438 // Now do the access.
439 if (fault == NoFault) {
440 MemCmd cmd = MemCmd::WriteReq; // default
441 bool do_access = true; // flag to suppress cache access
442
443 if (req->isLLSC()) {
444 cmd = MemCmd::StoreCondReq;
445 do_access = TheISA::handleLockedWrite(thread, req, dcachePort.cacheBlockMask);
446 } else if (req->isSwap()) {
447 cmd = MemCmd::SwapReq;
448 if (req->isCondSwap()) {
449 assert(res);
450 req->setExtraData(*res);
451 }
452 }
453
454 if (do_access && !req->getFlags().isSet(Request::NO_ACCESS)) {
455 Packet pkt = Packet(req, cmd);
456 pkt.dataStatic(data);
457
458 if (req->isMmappedIpr()) {
459 dcache_latency +=
460 TheISA::handleIprWrite(thread->getTC(), &pkt);
461 } else {
462 if (fastmem && system->isMemAddr(pkt.getAddr()))
463 system->getPhysMem().access(&pkt);
464 else
465 dcache_latency += dcachePort.sendAtomic(&pkt);
466 }
467 dcache_access = true;
468 assert(!pkt.isError());
469
470 if (req->isSwap()) {
471 assert(res);
472 memcpy(res, pkt.getPtr<uint8_t>(), fullSize);
473 }
474 }
475
476 if (res && !req->isSwap()) {
477 *res = req->getExtraData();
478 }
479 }
480
481 //If there's a fault or we don't need to access a second cache line,
482 //stop now.
483 if (fault != NoFault || secondAddr <= addr)
484 {
485 if (req->isLocked() && fault == NoFault) {
486 assert(locked);
487 locked = false;
488 }
489 if (fault != NoFault && req->isPrefetch()) {
490 return NoFault;
491 } else {
492 return fault;
493 }
494 }
495
496 /*
497 * Set up for accessing the second cache line.
498 */
499
500 //Move the pointer we're reading into to the correct location.
501 data += size;
502 //Adjust the size to get the remaining bytes.
503 size = addr + fullSize - secondAddr;
504 //And access the right address.
505 addr = secondAddr;
506 }
507}
508
509
510void
511AtomicSimpleCPU::tick()
512{
513 DPRINTF(SimpleCPU, "Tick\n");
514
515 Tick latency = 0;
516
517 for (int i = 0; i < width || locked; ++i) {
518 numCycles++;
519
520 if (!curStaticInst || !curStaticInst->isDelayedCommit())
521 checkForInterrupts();
522
523 checkPcEventQueue();
524 // We must have just got suspended by a PC event
525 if (_status == Idle) {
526 tryCompleteDrain();
527 return;
528 }
529
530 Fault fault = NoFault;
531
532 TheISA::PCState pcState = thread->pcState();
533
534 bool needToFetch = !isRomMicroPC(pcState.microPC()) &&
535 !curMacroStaticInst;
536 if (needToFetch) {
537 ifetch_req.taskId(taskId());
538 setupFetchRequest(&ifetch_req);
539 fault = thread->itb->translateAtomic(&ifetch_req, tc,
540 BaseTLB::Execute);
541 }
542
543 if (fault == NoFault) {
544 Tick icache_latency = 0;
545 bool icache_access = false;
546 dcache_access = false; // assume no dcache access
547
548 if (needToFetch) {
549 // This is commented out because the decoder would act like
550 // a tiny cache otherwise. It wouldn't be flushed when needed
551 // like the I cache. It should be flushed, and when that works
552 // this code should be uncommented.
553 //Fetch more instruction memory if necessary
554 //if(decoder.needMoreBytes())
555 //{
556 icache_access = true;
557 Packet ifetch_pkt = Packet(&ifetch_req, MemCmd::ReadReq);
558 ifetch_pkt.dataStatic(&inst);
559
560 if (fastmem && system->isMemAddr(ifetch_pkt.getAddr()))
561 system->getPhysMem().access(&ifetch_pkt);
562 else
563 icache_latency = icachePort.sendAtomic(&ifetch_pkt);
564
565 assert(!ifetch_pkt.isError());
566
567 // ifetch_req is initialized to read the instruction directly
568 // into the CPU object's inst field.
569 //}
570 }
571
572 preExecute();
573
574 if (curStaticInst) {
575 fault = curStaticInst->execute(this, traceData);
576
577 // keep an instruction count
578 if (fault == NoFault)
579 countInst();
580 else if (traceData && !DTRACE(ExecFaulting)) {
581 delete traceData;
582 traceData = NULL;
583 }
584
585 postExecute();
586 }
587
588 // @todo remove me after debugging with legion done
589 if (curStaticInst && (!curStaticInst->isMicroop() ||
590 curStaticInst->isFirstMicroop()))
591 instCnt++;
592
593 // profile for SimPoints if enabled and macro inst is finished
594 if (simpoint && curStaticInst && (fault == NoFault) &&
595 (!curStaticInst->isMicroop() ||
596 curStaticInst->isLastMicroop())) {
597 profileSimPoint();
598 }
599
600 Tick stall_ticks = 0;
601 if (simulate_inst_stalls && icache_access)
602 stall_ticks += icache_latency;
603
604 if (simulate_data_stalls && dcache_access)
605 stall_ticks += dcache_latency;
606
607 if (stall_ticks) {
608 // the atomic cpu does its accounting in ticks, so
609 // keep counting in ticks but round to the clock
610 // period
611 latency += divCeil(stall_ticks, clockPeriod()) *
612 clockPeriod();
613 }
614
615 }
616 if(fault != NoFault || !stayAtPC)
617 advancePC(fault);
618 }
619
620 if (tryCompleteDrain())
621 return;
622
623 // instruction takes at least one cycle
624 if (latency < clockPeriod())
625 latency = clockPeriod();
626
627 if (_status != Idle)
628 schedule(tickEvent, curTick() + latency);
629}
630
631
632void
633AtomicSimpleCPU::printAddr(Addr a)
634{
635 dcachePort.printAddr(a);
636}
637
638void
639AtomicSimpleCPU::profileSimPoint()
640{
641 if (!currentBBVInstCount)
642 currentBBV.first = thread->pcState().instAddr();
643
644 ++intervalCount;
645 ++currentBBVInstCount;
646
647 // If inst is control inst, assume end of basic block.
648 if (curStaticInst->isControl()) {
649 currentBBV.second = thread->pcState().instAddr();
650
651 auto map_itr = bbMap.find(currentBBV);
652 if (map_itr == bbMap.end()){
653 // If a new (previously unseen) basic block is found,
654 // add a new unique id, record num of insts and insert into bbMap.
655 BBInfo info;
656 info.id = bbMap.size() + 1;
657 info.insts = currentBBVInstCount;
658 info.count = currentBBVInstCount;
659 bbMap.insert(std::make_pair(currentBBV, info));
660 } else {
661 // If basic block is seen before, just increment the count by the
662 // number of insts in basic block.
663 BBInfo& info = map_itr->second;
664 info.count += currentBBVInstCount;
665 }
666 currentBBVInstCount = 0;
667
668 // Reached end of interval if the sum of the current inst count
669 // (intervalCount) and the excessive inst count from the previous
670 // interval (intervalDrift) is greater than/equal to the interval size.
671 if (intervalCount + intervalDrift >= intervalSize) {
672 // summarize interval and display BBV info
673 std::vector<pair<uint64_t, uint64_t> > counts;
674 for (auto map_itr = bbMap.begin(); map_itr != bbMap.end();
675 ++map_itr) {
676 BBInfo& info = map_itr->second;
677 if (info.count != 0) {
678 counts.push_back(std::make_pair(info.id, info.count));
679 info.count = 0;
680 }
681 }
682 std::sort(counts.begin(), counts.end());
683
684 // Print output BBV info
685 *simpointStream << "T";
686 for (auto cnt_itr = counts.begin(); cnt_itr != counts.end();
687 ++cnt_itr) {
688 *simpointStream << ":" << cnt_itr->first
689 << ":" << cnt_itr->second << " ";
690 }
691 *simpointStream << "\n";
692
693 intervalDrift = (intervalCount + intervalDrift) - intervalSize;
694 intervalCount = 0;
695 }
696 }
697}
698
699////////////////////////////////////////////////////////////////////////
700//
701// AtomicSimpleCPU Simulation Object
702//
703AtomicSimpleCPU *
704AtomicSimpleCPUParams::create()
705{
706 numThreads = 1;
707 if (!FullSystem && workload.size() != 1)
708 panic("only one workload allowed");
709 return new AtomicSimpleCPU(this);
710}