cpu: move initCPU calls from initState to init
[gem5.git] / src / cpu / base.cc
1 /*
2 * Copyright (c) 2011-2012,2016-2017, 2019 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 * Copyright (c) 2011 Regents of the University of California
16 * Copyright (c) 2013 Advanced Micro Devices, Inc.
17 * Copyright (c) 2013 Mark D. Hill and David A. Wood
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are
22 * met: redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer;
24 * redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution;
27 * neither the name of the copyright holders nor the names of its
28 * contributors may be used to endorse or promote products derived from
29 * this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * Authors: Steve Reinhardt
44 * Nathan Binkert
45 * Rick Strong
46 */
47
48 #include "cpu/base.hh"
49
50 #include <iostream>
51 #include <sstream>
52 #include <string>
53
54 #include "arch/generic/tlb.hh"
55 #include "base/cprintf.hh"
56 #include "base/loader/symtab.hh"
57 #include "base/logging.hh"
58 #include "base/output.hh"
59 #include "base/trace.hh"
60 #include "cpu/checker/cpu.hh"
61 #include "cpu/cpuevent.hh"
62 #include "cpu/profile.hh"
63 #include "cpu/thread_context.hh"
64 #include "debug/Mwait.hh"
65 #include "debug/SyscallVerbose.hh"
66 #include "debug/Thread.hh"
67 #include "mem/page_table.hh"
68 #include "params/BaseCPU.hh"
69 #include "sim/clocked_object.hh"
70 #include "sim/full_system.hh"
71 #include "sim/process.hh"
72 #include "sim/sim_events.hh"
73 #include "sim/sim_exit.hh"
74 #include "sim/system.hh"
75
76 // Hack
77 #include "sim/stat_control.hh"
78
79 using namespace std;
80
81 vector<BaseCPU *> BaseCPU::cpuList;
82
83 // This variable reflects the max number of threads in any CPU. Be
84 // careful to only use it once all the CPUs that you care about have
85 // been initialized
86 int maxThreadsPerCPU = 1;
87
88 CPUProgressEvent::CPUProgressEvent(BaseCPU *_cpu, Tick ival)
89 : Event(Event::Progress_Event_Pri), _interval(ival), lastNumInst(0),
90 cpu(_cpu), _repeatEvent(true)
91 {
92 if (_interval)
93 cpu->schedule(this, curTick() + _interval);
94 }
95
96 void
97 CPUProgressEvent::process()
98 {
99 Counter temp = cpu->totalOps();
100
101 if (_repeatEvent)
102 cpu->schedule(this, curTick() + _interval);
103
104 if (cpu->switchedOut()) {
105 return;
106 }
107
108 #ifndef NDEBUG
109 double ipc = double(temp - lastNumInst) / (_interval / cpu->clockPeriod());
110
111 DPRINTFN("%s progress event, total committed:%i, progress insts committed: "
112 "%lli, IPC: %0.8d\n", cpu->name(), temp, temp - lastNumInst,
113 ipc);
114 ipc = 0.0;
115 #else
116 cprintf("%lli: %s progress event, total committed:%i, progress insts "
117 "committed: %lli\n", curTick(), cpu->name(), temp,
118 temp - lastNumInst);
119 #endif
120 lastNumInst = temp;
121 }
122
123 const char *
124 CPUProgressEvent::description() const
125 {
126 return "CPU Progress";
127 }
128
129 BaseCPU::BaseCPU(Params *p, bool is_checker)
130 : ClockedObject(p), instCnt(0), _cpuId(p->cpu_id), _socketId(p->socket_id),
131 _instMasterId(p->system->getMasterId(this, "inst")),
132 _dataMasterId(p->system->getMasterId(this, "data")),
133 _taskId(ContextSwitchTaskId::Unknown), _pid(invldPid),
134 _switchedOut(p->switched_out), _cacheLineSize(p->system->cacheLineSize()),
135 interrupts(p->interrupts), profileEvent(NULL),
136 numThreads(p->numThreads), system(p->system),
137 previousCycle(0), previousState(CPU_STATE_SLEEP),
138 functionTraceStream(nullptr), currentFunctionStart(0),
139 currentFunctionEnd(0), functionEntryTick(0),
140 addressMonitor(p->numThreads),
141 syscallRetryLatency(p->syscallRetryLatency),
142 pwrGatingLatency(p->pwr_gating_latency),
143 powerGatingOnIdle(p->power_gating_on_idle),
144 enterPwrGatingEvent([this]{ enterPwrGating(); }, name())
145 {
146 // if Python did not provide a valid ID, do it here
147 if (_cpuId == -1 ) {
148 _cpuId = cpuList.size();
149 }
150
151 // add self to global list of CPUs
152 cpuList.push_back(this);
153
154 DPRINTF(SyscallVerbose, "Constructing CPU with id %d, socket id %d\n",
155 _cpuId, _socketId);
156
157 if (numThreads > maxThreadsPerCPU)
158 maxThreadsPerCPU = numThreads;
159
160 functionTracingEnabled = false;
161 if (p->function_trace) {
162 const string fname = csprintf("ftrace.%s", name());
163 functionTraceStream = simout.findOrCreate(fname)->stream();
164
165 currentFunctionStart = currentFunctionEnd = 0;
166 functionEntryTick = p->function_trace_start;
167
168 if (p->function_trace_start == 0) {
169 functionTracingEnabled = true;
170 } else {
171 Event *event = new EventFunctionWrapper(
172 [this]{ enableFunctionTrace(); }, name(), true);
173 schedule(event, p->function_trace_start);
174 }
175 }
176
177 // The interrupts should always be present unless this CPU is
178 // switched in later or in case it is a checker CPU
179 if (!params()->switched_out && !is_checker) {
180 fatal_if(interrupts.size() != numThreads,
181 "CPU %s has %i interrupt controllers, but is expecting one "
182 "per thread (%i)\n",
183 name(), interrupts.size(), numThreads);
184 for (ThreadID tid = 0; tid < numThreads; tid++)
185 interrupts[tid]->setCPU(this);
186 }
187
188 if (FullSystem) {
189 if (params()->profile)
190 profileEvent = new EventFunctionWrapper(
191 [this]{ processProfileEvent(); },
192 name());
193 }
194 tracer = params()->tracer;
195
196 if (params()->isa.size() != numThreads) {
197 fatal("Number of ISAs (%i) assigned to the CPU does not equal number "
198 "of threads (%i).\n", params()->isa.size(), numThreads);
199 }
200 }
201
202 void
203 BaseCPU::enableFunctionTrace()
204 {
205 functionTracingEnabled = true;
206 }
207
208 BaseCPU::~BaseCPU()
209 {
210 delete profileEvent;
211 }
212
213 void
214 BaseCPU::armMonitor(ThreadID tid, Addr address)
215 {
216 assert(tid < numThreads);
217 AddressMonitor &monitor = addressMonitor[tid];
218
219 monitor.armed = true;
220 monitor.vAddr = address;
221 monitor.pAddr = 0x0;
222 DPRINTF(Mwait,"[tid:%d] Armed monitor (vAddr=0x%lx)\n", tid, address);
223 }
224
225 bool
226 BaseCPU::mwait(ThreadID tid, PacketPtr pkt)
227 {
228 assert(tid < numThreads);
229 AddressMonitor &monitor = addressMonitor[tid];
230
231 if (!monitor.gotWakeup) {
232 int block_size = cacheLineSize();
233 uint64_t mask = ~((uint64_t)(block_size - 1));
234
235 assert(pkt->req->hasPaddr());
236 monitor.pAddr = pkt->getAddr() & mask;
237 monitor.waiting = true;
238
239 DPRINTF(Mwait,"[tid:%d] mwait called (vAddr=0x%lx, "
240 "line's paddr=0x%lx)\n", tid, monitor.vAddr, monitor.pAddr);
241 return true;
242 } else {
243 monitor.gotWakeup = false;
244 return false;
245 }
246 }
247
248 void
249 BaseCPU::mwaitAtomic(ThreadID tid, ThreadContext *tc, BaseTLB *dtb)
250 {
251 assert(tid < numThreads);
252 AddressMonitor &monitor = addressMonitor[tid];
253
254 RequestPtr req = std::make_shared<Request>();
255
256 Addr addr = monitor.vAddr;
257 int block_size = cacheLineSize();
258 uint64_t mask = ~((uint64_t)(block_size - 1));
259 int size = block_size;
260
261 //The address of the next line if it crosses a cache line boundary.
262 Addr secondAddr = roundDown(addr + size - 1, block_size);
263
264 if (secondAddr > addr)
265 size = secondAddr - addr;
266
267 req->setVirt(0, addr, size, 0x0, dataMasterId(), tc->instAddr());
268
269 // translate to physical address
270 Fault fault = dtb->translateAtomic(req, tc, BaseTLB::Read);
271 assert(fault == NoFault);
272
273 monitor.pAddr = req->getPaddr() & mask;
274 monitor.waiting = true;
275
276 DPRINTF(Mwait,"[tid:%d] mwait called (vAddr=0x%lx, line's paddr=0x%lx)\n",
277 tid, monitor.vAddr, monitor.pAddr);
278 }
279
280 void
281 BaseCPU::init()
282 {
283 // Set up instruction-count-based termination events, if any. This needs
284 // to happen after threadContexts has been constructed.
285 if (params()->max_insts_any_thread != 0) {
286 const char *cause = "a thread reached the max instruction count";
287 for (ThreadID tid = 0; tid < numThreads; ++tid)
288 scheduleInstStop(tid, params()->max_insts_any_thread, cause);
289 }
290
291 // Set up instruction-count-based termination events for SimPoints
292 // Typically, there are more than one action points.
293 // Simulation.py is responsible to take the necessary actions upon
294 // exitting the simulation loop.
295 if (!params()->simpoint_start_insts.empty()) {
296 const char *cause = "simpoint starting point found";
297 for (size_t i = 0; i < params()->simpoint_start_insts.size(); ++i)
298 scheduleInstStop(0, params()->simpoint_start_insts[i], cause);
299 }
300
301 if (params()->max_insts_all_threads != 0) {
302 const char *cause = "all threads reached the max instruction count";
303
304 // allocate & initialize shared downcounter: each event will
305 // decrement this when triggered; simulation will terminate
306 // when counter reaches 0
307 int *counter = new int;
308 *counter = numThreads;
309 for (ThreadID tid = 0; tid < numThreads; ++tid) {
310 Event *event = new CountedExitEvent(cause, *counter);
311 threadContexts[tid]->scheduleInstCountEvent(
312 event, params()->max_insts_all_threads);
313 }
314 }
315
316 if (!params()->switched_out) {
317 registerThreadContexts();
318
319 verifyMemoryMode();
320 }
321
322 //These calls eventually need to be moved to initState
323 if (FullSystem && !params()->switched_out) {
324 for (auto *tc: threadContexts)
325 TheISA::initCPU(tc, tc->contextId());
326 }
327 }
328
329 void
330 BaseCPU::startup()
331 {
332 if (FullSystem) {
333 if (!params()->switched_out && profileEvent)
334 schedule(profileEvent, curTick());
335 }
336
337 if (params()->progress_interval) {
338 new CPUProgressEvent(this, params()->progress_interval);
339 }
340
341 if (_switchedOut)
342 ClockedObject::pwrState(Enums::PwrState::OFF);
343
344 // Assumption CPU start to operate instantaneously without any latency
345 if (ClockedObject::pwrState() == Enums::PwrState::UNDEFINED)
346 ClockedObject::pwrState(Enums::PwrState::ON);
347
348 }
349
350 ProbePoints::PMUUPtr
351 BaseCPU::pmuProbePoint(const char *name)
352 {
353 ProbePoints::PMUUPtr ptr;
354 ptr.reset(new ProbePoints::PMU(getProbeManager(), name));
355
356 return ptr;
357 }
358
359 void
360 BaseCPU::regProbePoints()
361 {
362 ppAllCycles = pmuProbePoint("Cycles");
363 ppActiveCycles = pmuProbePoint("ActiveCycles");
364
365 ppRetiredInsts = pmuProbePoint("RetiredInsts");
366 ppRetiredInstsPC = pmuProbePoint("RetiredInstsPC");
367 ppRetiredLoads = pmuProbePoint("RetiredLoads");
368 ppRetiredStores = pmuProbePoint("RetiredStores");
369 ppRetiredBranches = pmuProbePoint("RetiredBranches");
370
371 ppSleeping = new ProbePointArg<bool>(this->getProbeManager(),
372 "Sleeping");
373 }
374
375 void
376 BaseCPU::probeInstCommit(const StaticInstPtr &inst, Addr pc)
377 {
378 if (!inst->isMicroop() || inst->isLastMicroop()) {
379 ppRetiredInsts->notify(1);
380 ppRetiredInstsPC->notify(pc);
381 }
382
383 if (inst->isLoad())
384 ppRetiredLoads->notify(1);
385
386 if (inst->isStore() || inst->isAtomic())
387 ppRetiredStores->notify(1);
388
389 if (inst->isControl())
390 ppRetiredBranches->notify(1);
391 }
392
393 void
394 BaseCPU::regStats()
395 {
396 ClockedObject::regStats();
397
398 using namespace Stats;
399
400 numCycles
401 .name(name() + ".numCycles")
402 .desc("number of cpu cycles simulated")
403 ;
404
405 numWorkItemsStarted
406 .name(name() + ".numWorkItemsStarted")
407 .desc("number of work items this cpu started")
408 ;
409
410 numWorkItemsCompleted
411 .name(name() + ".numWorkItemsCompleted")
412 .desc("number of work items this cpu completed")
413 ;
414
415 int size = threadContexts.size();
416 if (size > 1) {
417 for (int i = 0; i < size; ++i) {
418 stringstream namestr;
419 ccprintf(namestr, "%s.ctx%d", name(), i);
420 threadContexts[i]->regStats(namestr.str());
421 }
422 } else if (size == 1)
423 threadContexts[0]->regStats(name());
424 }
425
426 Port &
427 BaseCPU::getPort(const string &if_name, PortID idx)
428 {
429 // Get the right port based on name. This applies to all the
430 // subclasses of the base CPU and relies on their implementation
431 // of getDataPort and getInstPort.
432 if (if_name == "dcache_port")
433 return getDataPort();
434 else if (if_name == "icache_port")
435 return getInstPort();
436 else
437 return ClockedObject::getPort(if_name, idx);
438 }
439
440 void
441 BaseCPU::registerThreadContexts()
442 {
443 assert(system->multiThread || numThreads == 1);
444
445 ThreadID size = threadContexts.size();
446 for (ThreadID tid = 0; tid < size; ++tid) {
447 ThreadContext *tc = threadContexts[tid];
448
449 if (system->multiThread) {
450 tc->setContextId(system->registerThreadContext(tc));
451 } else {
452 tc->setContextId(system->registerThreadContext(tc, _cpuId));
453 }
454
455 if (!FullSystem)
456 tc->getProcessPtr()->assignThreadContext(tc->contextId());
457 }
458 }
459
460 void
461 BaseCPU::deschedulePowerGatingEvent()
462 {
463 if (enterPwrGatingEvent.scheduled()){
464 deschedule(enterPwrGatingEvent);
465 }
466 }
467
468 void
469 BaseCPU::schedulePowerGatingEvent()
470 {
471 for (auto tc : threadContexts) {
472 if (tc->status() == ThreadContext::Active)
473 return;
474 }
475
476 if (ClockedObject::pwrState() == Enums::PwrState::CLK_GATED &&
477 powerGatingOnIdle) {
478 assert(!enterPwrGatingEvent.scheduled());
479 // Schedule a power gating event when clock gated for the specified
480 // amount of time
481 schedule(enterPwrGatingEvent, clockEdge(pwrGatingLatency));
482 }
483 }
484
485 int
486 BaseCPU::findContext(ThreadContext *tc)
487 {
488 ThreadID size = threadContexts.size();
489 for (ThreadID tid = 0; tid < size; ++tid) {
490 if (tc == threadContexts[tid])
491 return tid;
492 }
493 return 0;
494 }
495
496 void
497 BaseCPU::activateContext(ThreadID thread_num)
498 {
499 DPRINTF(Thread, "activate contextId %d\n",
500 threadContexts[thread_num]->contextId());
501 // Squash enter power gating event while cpu gets activated
502 if (enterPwrGatingEvent.scheduled())
503 deschedule(enterPwrGatingEvent);
504 // For any active thread running, update CPU power state to active (ON)
505 ClockedObject::pwrState(Enums::PwrState::ON);
506
507 updateCycleCounters(CPU_STATE_WAKEUP);
508 }
509
510 void
511 BaseCPU::suspendContext(ThreadID thread_num)
512 {
513 DPRINTF(Thread, "suspend contextId %d\n",
514 threadContexts[thread_num]->contextId());
515 // Check if all threads are suspended
516 for (auto t : threadContexts) {
517 if (t->status() != ThreadContext::Suspended) {
518 return;
519 }
520 }
521
522 // All CPU thread are suspended, update cycle count
523 updateCycleCounters(CPU_STATE_SLEEP);
524
525 // All CPU threads suspended, enter lower power state for the CPU
526 ClockedObject::pwrState(Enums::PwrState::CLK_GATED);
527
528 // If pwrGatingLatency is set to 0 then this mechanism is disabled
529 if (powerGatingOnIdle) {
530 // Schedule power gating event when clock gated for pwrGatingLatency
531 // cycles
532 schedule(enterPwrGatingEvent, clockEdge(pwrGatingLatency));
533 }
534 }
535
536 void
537 BaseCPU::haltContext(ThreadID thread_num)
538 {
539 updateCycleCounters(BaseCPU::CPU_STATE_SLEEP);
540 }
541
542 void
543 BaseCPU::enterPwrGating(void)
544 {
545 ClockedObject::pwrState(Enums::PwrState::OFF);
546 }
547
548 void
549 BaseCPU::switchOut()
550 {
551 assert(!_switchedOut);
552 _switchedOut = true;
553 if (profileEvent && profileEvent->scheduled())
554 deschedule(profileEvent);
555
556 // Flush all TLBs in the CPU to avoid having stale translations if
557 // it gets switched in later.
558 flushTLBs();
559
560 // Go to the power gating state
561 ClockedObject::pwrState(Enums::PwrState::OFF);
562 }
563
564 void
565 BaseCPU::takeOverFrom(BaseCPU *oldCPU)
566 {
567 assert(threadContexts.size() == oldCPU->threadContexts.size());
568 assert(_cpuId == oldCPU->cpuId());
569 assert(_switchedOut);
570 assert(oldCPU != this);
571 _pid = oldCPU->getPid();
572 _taskId = oldCPU->taskId();
573 // Take over the power state of the switchedOut CPU
574 ClockedObject::pwrState(oldCPU->pwrState());
575
576 previousState = oldCPU->previousState;
577 previousCycle = oldCPU->previousCycle;
578
579 _switchedOut = false;
580
581 ThreadID size = threadContexts.size();
582 for (ThreadID i = 0; i < size; ++i) {
583 ThreadContext *newTC = threadContexts[i];
584 ThreadContext *oldTC = oldCPU->threadContexts[i];
585
586 newTC->takeOverFrom(oldTC);
587
588 CpuEvent::replaceThreadContext(oldTC, newTC);
589
590 assert(newTC->contextId() == oldTC->contextId());
591 assert(newTC->threadId() == oldTC->threadId());
592 system->replaceThreadContext(newTC, newTC->contextId());
593
594 /* This code no longer works since the zero register (e.g.,
595 * r31 on Alpha) doesn't necessarily contain zero at this
596 * point.
597 if (DTRACE(Context))
598 ThreadContext::compare(oldTC, newTC);
599 */
600
601 Port *old_itb_port = oldTC->getITBPtr()->getTableWalkerPort();
602 Port *old_dtb_port = oldTC->getDTBPtr()->getTableWalkerPort();
603 Port *new_itb_port = newTC->getITBPtr()->getTableWalkerPort();
604 Port *new_dtb_port = newTC->getDTBPtr()->getTableWalkerPort();
605
606 // Move over any table walker ports if they exist
607 if (new_itb_port)
608 new_itb_port->takeOverFrom(old_itb_port);
609 if (new_dtb_port)
610 new_dtb_port->takeOverFrom(old_dtb_port);
611 newTC->getITBPtr()->takeOverFrom(oldTC->getITBPtr());
612 newTC->getDTBPtr()->takeOverFrom(oldTC->getDTBPtr());
613
614 // Checker whether or not we have to transfer CheckerCPU
615 // objects over in the switch
616 CheckerCPU *oldChecker = oldTC->getCheckerCpuPtr();
617 CheckerCPU *newChecker = newTC->getCheckerCpuPtr();
618 if (oldChecker && newChecker) {
619 Port *old_checker_itb_port =
620 oldChecker->getITBPtr()->getTableWalkerPort();
621 Port *old_checker_dtb_port =
622 oldChecker->getDTBPtr()->getTableWalkerPort();
623 Port *new_checker_itb_port =
624 newChecker->getITBPtr()->getTableWalkerPort();
625 Port *new_checker_dtb_port =
626 newChecker->getDTBPtr()->getTableWalkerPort();
627
628 newChecker->getITBPtr()->takeOverFrom(oldChecker->getITBPtr());
629 newChecker->getDTBPtr()->takeOverFrom(oldChecker->getDTBPtr());
630
631 // Move over any table walker ports if they exist for checker
632 if (new_checker_itb_port)
633 new_checker_itb_port->takeOverFrom(old_checker_itb_port);
634 if (new_checker_dtb_port)
635 new_checker_dtb_port->takeOverFrom(old_checker_dtb_port);
636 }
637 }
638
639 interrupts = oldCPU->interrupts;
640 for (ThreadID tid = 0; tid < numThreads; tid++) {
641 interrupts[tid]->setCPU(this);
642 }
643 oldCPU->interrupts.clear();
644
645 if (FullSystem) {
646 for (ThreadID i = 0; i < size; ++i)
647 threadContexts[i]->profileClear();
648
649 if (profileEvent)
650 schedule(profileEvent, curTick());
651 }
652
653 // All CPUs have an instruction and a data port, and the new CPU's
654 // ports are dangling while the old CPU has its ports connected
655 // already. Unbind the old CPU and then bind the ports of the one
656 // we are switching to.
657 getInstPort().takeOverFrom(&oldCPU->getInstPort());
658 getDataPort().takeOverFrom(&oldCPU->getDataPort());
659 }
660
661 void
662 BaseCPU::flushTLBs()
663 {
664 for (ThreadID i = 0; i < threadContexts.size(); ++i) {
665 ThreadContext &tc(*threadContexts[i]);
666 CheckerCPU *checker(tc.getCheckerCpuPtr());
667
668 tc.getITBPtr()->flushAll();
669 tc.getDTBPtr()->flushAll();
670 if (checker) {
671 checker->getITBPtr()->flushAll();
672 checker->getDTBPtr()->flushAll();
673 }
674 }
675 }
676
677 void
678 BaseCPU::processProfileEvent()
679 {
680 ThreadID size = threadContexts.size();
681
682 for (ThreadID i = 0; i < size; ++i)
683 threadContexts[i]->profileSample();
684
685 schedule(profileEvent, curTick() + params()->profile);
686 }
687
688 void
689 BaseCPU::serialize(CheckpointOut &cp) const
690 {
691 SERIALIZE_SCALAR(instCnt);
692
693 if (!_switchedOut) {
694 /* Unlike _pid, _taskId is not serialized, as they are dynamically
695 * assigned unique ids that are only meaningful for the duration of
696 * a specific run. We will need to serialize the entire taskMap in
697 * system. */
698 SERIALIZE_SCALAR(_pid);
699
700 // Serialize the threads, this is done by the CPU implementation.
701 for (ThreadID i = 0; i < numThreads; ++i) {
702 ScopedCheckpointSection sec(cp, csprintf("xc.%i", i));
703 interrupts[i]->serialize(cp);
704 serializeThread(cp, i);
705 }
706 }
707 }
708
709 void
710 BaseCPU::unserialize(CheckpointIn &cp)
711 {
712 UNSERIALIZE_SCALAR(instCnt);
713
714 if (!_switchedOut) {
715 UNSERIALIZE_SCALAR(_pid);
716
717 // Unserialize the threads, this is done by the CPU implementation.
718 for (ThreadID i = 0; i < numThreads; ++i) {
719 ScopedCheckpointSection sec(cp, csprintf("xc.%i", i));
720 interrupts[i]->unserialize(cp);
721 unserializeThread(cp, i);
722 }
723 }
724 }
725
726 void
727 BaseCPU::scheduleInstStop(ThreadID tid, Counter insts, const char *cause)
728 {
729 const Tick now(getCurrentInstCount(tid));
730 Event *event(new LocalSimLoopExitEvent(cause, 0));
731
732 threadContexts[tid]->scheduleInstCountEvent(event, now + insts);
733 }
734
735 Tick
736 BaseCPU::getCurrentInstCount(ThreadID tid)
737 {
738 return threadContexts[tid]->getCurrentInstCount();
739 }
740
741 AddressMonitor::AddressMonitor() {
742 armed = false;
743 waiting = false;
744 gotWakeup = false;
745 }
746
747 bool AddressMonitor::doMonitor(PacketPtr pkt) {
748 assert(pkt->req->hasPaddr());
749 if (armed && waiting) {
750 if (pAddr == pkt->getAddr()) {
751 DPRINTF(Mwait,"pAddr=0x%lx invalidated: waking up core\n",
752 pkt->getAddr());
753 waiting = false;
754 return true;
755 }
756 }
757 return false;
758 }
759
760
761 void
762 BaseCPU::traceFunctionsInternal(Addr pc)
763 {
764 if (!debugSymbolTable)
765 return;
766
767 // if pc enters different function, print new function symbol and
768 // update saved range. Otherwise do nothing.
769 if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
770 string sym_str;
771 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
772 currentFunctionStart,
773 currentFunctionEnd);
774
775 if (!found) {
776 // no symbol found: use addr as label
777 sym_str = csprintf("0x%x", pc);
778 currentFunctionStart = pc;
779 currentFunctionEnd = pc + 1;
780 }
781
782 ccprintf(*functionTraceStream, " (%d)\n%d: %s",
783 curTick() - functionEntryTick, curTick(), sym_str);
784 functionEntryTick = curTick();
785 }
786 }
787
788 bool
789 BaseCPU::waitForRemoteGDB() const
790 {
791 return params()->wait_for_remote_gdb;
792 }