base.cc (3125:febd811bccc6) base.cc (3126:756092c6383c)
1/*
2 * Copyright (c) 2002-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: Steve Reinhardt
29 * Nathan Binkert
30 */
31
32#include <iostream>
33#include <string>
34#include <sstream>
35
36#include "base/cprintf.hh"
37#include "base/loader/symtab.hh"
38#include "base/misc.hh"
39#include "base/output.hh"
40#include "cpu/base.hh"
41#include "cpu/cpuevent.hh"
42#include "cpu/thread_context.hh"
43#include "cpu/profile.hh"
44#include "sim/param.hh"
45#include "sim/process.hh"
46#include "sim/sim_events.hh"
47#include "sim/system.hh"
48
49#include "base/trace.hh"
50
51// Hack
52#include "sim/stat_control.hh"
53
54using namespace std;
55
56vector<BaseCPU *> BaseCPU::cpuList;
57
58// This variable reflects the max number of threads in any CPU. Be
59// careful to only use it once all the CPUs that you care about have
60// been initialized
61int maxThreadsPerCPU = 1;
62
1/*
2 * Copyright (c) 2002-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: Steve Reinhardt
29 * Nathan Binkert
30 */
31
32#include <iostream>
33#include <string>
34#include <sstream>
35
36#include "base/cprintf.hh"
37#include "base/loader/symtab.hh"
38#include "base/misc.hh"
39#include "base/output.hh"
40#include "cpu/base.hh"
41#include "cpu/cpuevent.hh"
42#include "cpu/thread_context.hh"
43#include "cpu/profile.hh"
44#include "sim/param.hh"
45#include "sim/process.hh"
46#include "sim/sim_events.hh"
47#include "sim/system.hh"
48
49#include "base/trace.hh"
50
51// Hack
52#include "sim/stat_control.hh"
53
54using namespace std;
55
56vector<BaseCPU *> BaseCPU::cpuList;
57
58// This variable reflects the max number of threads in any CPU. Be
59// careful to only use it once all the CPUs that you care about have
60// been initialized
61int maxThreadsPerCPU = 1;
62
63CPUProgressEvent::CPUProgressEvent(EventQueue *q, Tick ival,
64 BaseCPU *_cpu)
65 : Event(q, Event::Stat_Event_Pri), interval(ival),
66 lastNumInst(0), cpu(_cpu)
67{
68 if (interval)
69 schedule(curTick + interval);
70}
71
63void
64CPUProgressEvent::process()
65{
66 Counter temp = cpu->totalInstructions();
67#ifndef NDEBUG
68 double ipc = double(temp - lastNumInst) / (interval / cpu->cycles(1));
69
70 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
71 cpu->name(), temp - lastNumInst, ipc);
72 ipc = 0.0;
73#else
74 cprintf("%lli: %s progress event, instructions committed: %lli\n",
75 curTick, cpu->name(), temp - lastNumInst);
76#endif
77 lastNumInst = temp;
78 schedule(curTick + interval);
79}
80
81const char *
82CPUProgressEvent::description()
83{
84 return "CPU Progress event";
85}
86
87#if FULL_SYSTEM
88BaseCPU::BaseCPU(Params *p)
89 : MemObject(p->name), clock(p->clock), checkInterrupts(true),
90 params(p), number_of_threads(p->numberOfThreads), system(p->system)
91#else
92BaseCPU::BaseCPU(Params *p)
93 : MemObject(p->name), clock(p->clock), params(p),
94 number_of_threads(p->numberOfThreads), system(p->system)
95#endif
96{
97// currentTick = curTick;
98 DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this);
99
100 // add self to global list of CPUs
101 cpuList.push_back(this);
102
103 DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
104 this);
105
106 if (number_of_threads > maxThreadsPerCPU)
107 maxThreadsPerCPU = number_of_threads;
108
109 // allocate per-thread instruction-based event queues
110 comInstEventQueue = new EventQueue *[number_of_threads];
111 for (int i = 0; i < number_of_threads; ++i)
112 comInstEventQueue[i] = new EventQueue("instruction-based event queue");
113
114 //
115 // set up instruction-count-based termination events, if any
116 //
117 if (p->max_insts_any_thread != 0)
118 for (int i = 0; i < number_of_threads; ++i)
119 new SimLoopExitEvent(comInstEventQueue[i], p->max_insts_any_thread,
120 "a thread reached the max instruction count");
121
122 if (p->max_insts_all_threads != 0) {
123 // allocate & initialize shared downcounter: each event will
124 // decrement this when triggered; simulation will terminate
125 // when counter reaches 0
126 int *counter = new int;
127 *counter = number_of_threads;
128 for (int i = 0; i < number_of_threads; ++i)
129 new CountedExitEvent(comInstEventQueue[i],
130 "all threads reached the max instruction count",
131 p->max_insts_all_threads, *counter);
132 }
133
134 // allocate per-thread load-based event queues
135 comLoadEventQueue = new EventQueue *[number_of_threads];
136 for (int i = 0; i < number_of_threads; ++i)
137 comLoadEventQueue[i] = new EventQueue("load-based event queue");
138
139 //
140 // set up instruction-count-based termination events, if any
141 //
142 if (p->max_loads_any_thread != 0)
143 for (int i = 0; i < number_of_threads; ++i)
144 new SimLoopExitEvent(comLoadEventQueue[i], p->max_loads_any_thread,
145 "a thread reached the max load count");
146
147 if (p->max_loads_all_threads != 0) {
148 // allocate & initialize shared downcounter: each event will
149 // decrement this when triggered; simulation will terminate
150 // when counter reaches 0
151 int *counter = new int;
152 *counter = number_of_threads;
153 for (int i = 0; i < number_of_threads; ++i)
154 new CountedExitEvent(comLoadEventQueue[i],
155 "all threads reached the max load count",
156 p->max_loads_all_threads, *counter);
157 }
158
72void
73CPUProgressEvent::process()
74{
75 Counter temp = cpu->totalInstructions();
76#ifndef NDEBUG
77 double ipc = double(temp - lastNumInst) / (interval / cpu->cycles(1));
78
79 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
80 cpu->name(), temp - lastNumInst, ipc);
81 ipc = 0.0;
82#else
83 cprintf("%lli: %s progress event, instructions committed: %lli\n",
84 curTick, cpu->name(), temp - lastNumInst);
85#endif
86 lastNumInst = temp;
87 schedule(curTick + interval);
88}
89
90const char *
91CPUProgressEvent::description()
92{
93 return "CPU Progress event";
94}
95
96#if FULL_SYSTEM
97BaseCPU::BaseCPU(Params *p)
98 : MemObject(p->name), clock(p->clock), checkInterrupts(true),
99 params(p), number_of_threads(p->numberOfThreads), system(p->system)
100#else
101BaseCPU::BaseCPU(Params *p)
102 : MemObject(p->name), clock(p->clock), params(p),
103 number_of_threads(p->numberOfThreads), system(p->system)
104#endif
105{
106// currentTick = curTick;
107 DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this);
108
109 // add self to global list of CPUs
110 cpuList.push_back(this);
111
112 DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
113 this);
114
115 if (number_of_threads > maxThreadsPerCPU)
116 maxThreadsPerCPU = number_of_threads;
117
118 // allocate per-thread instruction-based event queues
119 comInstEventQueue = new EventQueue *[number_of_threads];
120 for (int i = 0; i < number_of_threads; ++i)
121 comInstEventQueue[i] = new EventQueue("instruction-based event queue");
122
123 //
124 // set up instruction-count-based termination events, if any
125 //
126 if (p->max_insts_any_thread != 0)
127 for (int i = 0; i < number_of_threads; ++i)
128 new SimLoopExitEvent(comInstEventQueue[i], p->max_insts_any_thread,
129 "a thread reached the max instruction count");
130
131 if (p->max_insts_all_threads != 0) {
132 // allocate & initialize shared downcounter: each event will
133 // decrement this when triggered; simulation will terminate
134 // when counter reaches 0
135 int *counter = new int;
136 *counter = number_of_threads;
137 for (int i = 0; i < number_of_threads; ++i)
138 new CountedExitEvent(comInstEventQueue[i],
139 "all threads reached the max instruction count",
140 p->max_insts_all_threads, *counter);
141 }
142
143 // allocate per-thread load-based event queues
144 comLoadEventQueue = new EventQueue *[number_of_threads];
145 for (int i = 0; i < number_of_threads; ++i)
146 comLoadEventQueue[i] = new EventQueue("load-based event queue");
147
148 //
149 // set up instruction-count-based termination events, if any
150 //
151 if (p->max_loads_any_thread != 0)
152 for (int i = 0; i < number_of_threads; ++i)
153 new SimLoopExitEvent(comLoadEventQueue[i], p->max_loads_any_thread,
154 "a thread reached the max load count");
155
156 if (p->max_loads_all_threads != 0) {
157 // allocate & initialize shared downcounter: each event will
158 // decrement this when triggered; simulation will terminate
159 // when counter reaches 0
160 int *counter = new int;
161 *counter = number_of_threads;
162 for (int i = 0; i < number_of_threads; ++i)
163 new CountedExitEvent(comLoadEventQueue[i],
164 "all threads reached the max load count",
165 p->max_loads_all_threads, *counter);
166 }
167
159 if (p->stats_reset_inst != 0) {
160 Stats::SetupEvent(Stats::Reset, p->stats_reset_inst, 0, comInstEventQueue[0]);
161 cprintf("Stats reset event scheduled for %lli insts\n",
162 p->stats_reset_inst);
163 }
164
165#if FULL_SYSTEM
166 memset(interrupts, 0, sizeof(interrupts));
167 intstatus = 0;
168#endif
169
170 functionTracingEnabled = false;
171 if (p->functionTrace) {
172 functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
173 currentFunctionStart = currentFunctionEnd = 0;
174 functionEntryTick = p->functionTraceStart;
175
176 if (p->functionTraceStart == 0) {
177 functionTracingEnabled = true;
178 } else {
179 Event *e =
180 new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
181 true);
182 e->schedule(p->functionTraceStart);
183 }
184 }
185#if FULL_SYSTEM
186 profileEvent = NULL;
187 if (params->profile)
188 profileEvent = new ProfileEvent(this, params->profile);
189#endif
190}
191
192BaseCPU::Params::Params()
193{
194#if FULL_SYSTEM
195 profile = false;
196#endif
197 checker = NULL;
198}
199
200void
201BaseCPU::enableFunctionTrace()
202{
203 functionTracingEnabled = true;
204}
205
206BaseCPU::~BaseCPU()
207{
208}
209
210void
211BaseCPU::init()
212{
213 if (!params->deferRegistration)
214 registerThreadContexts();
215}
216
217void
218BaseCPU::startup()
219{
220#if FULL_SYSTEM
221 if (!params->deferRegistration && profileEvent)
222 profileEvent->schedule(curTick);
223#endif
224
225 if (params->progress_interval) {
226 new CPUProgressEvent(&mainEventQueue, params->progress_interval,
227 this);
228 }
229}
230
231
232void
233BaseCPU::regStats()
234{
235 using namespace Stats;
236
237 numCycles
238 .name(name() + ".numCycles")
239 .desc("number of cpu cycles simulated")
240 ;
241
242 int size = threadContexts.size();
243 if (size > 1) {
244 for (int i = 0; i < size; ++i) {
245 stringstream namestr;
246 ccprintf(namestr, "%s.ctx%d", name(), i);
247 threadContexts[i]->regStats(namestr.str());
248 }
249 } else if (size == 1)
250 threadContexts[0]->regStats(name());
251
252#if FULL_SYSTEM
253#endif
254}
255
256
257void
258BaseCPU::registerThreadContexts()
259{
260 for (int i = 0; i < threadContexts.size(); ++i) {
261 ThreadContext *tc = threadContexts[i];
262
263#if FULL_SYSTEM
264 int id = params->cpu_id;
265 if (id != -1)
266 id += i;
267
268 tc->setCpuId(system->registerThreadContext(tc, id));
269#else
270 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc));
271#endif
272 }
273}
274
275
276void
277BaseCPU::switchOut()
278{
279// panic("This CPU doesn't support sampling!");
280#if FULL_SYSTEM
281 if (profileEvent && profileEvent->scheduled())
282 profileEvent->deschedule();
283#endif
284}
285
286void
287BaseCPU::takeOverFrom(BaseCPU *oldCPU)
288{
289 assert(threadContexts.size() == oldCPU->threadContexts.size());
290
291 for (int i = 0; i < threadContexts.size(); ++i) {
292 ThreadContext *newTC = threadContexts[i];
293 ThreadContext *oldTC = oldCPU->threadContexts[i];
294
295 newTC->takeOverFrom(oldTC);
296
297 CpuEvent::replaceThreadContext(oldTC, newTC);
298
299 assert(newTC->readCpuId() == oldTC->readCpuId());
300#if FULL_SYSTEM
301 system->replaceThreadContext(newTC, newTC->readCpuId());
302#else
303 assert(newTC->getProcessPtr() == oldTC->getProcessPtr());
304 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId());
305#endif
306
307// TheISA::compareXCs(oldXC, newXC);
308 }
309
310#if FULL_SYSTEM
311 for (int i = 0; i < TheISA::NumInterruptLevels; ++i)
312 interrupts[i] = oldCPU->interrupts[i];
313 intstatus = oldCPU->intstatus;
314 checkInterrupts = oldCPU->checkInterrupts;
315
316 for (int i = 0; i < threadContexts.size(); ++i)
317 threadContexts[i]->profileClear();
318
319 // The Sampler must take care of this!
320// if (profileEvent)
321// profileEvent->schedule(curTick);
322#endif
323}
324
325
326#if FULL_SYSTEM
327BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
328 : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
329{ }
330
331void
332BaseCPU::ProfileEvent::process()
333{
334 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) {
335 ThreadContext *tc = cpu->threadContexts[i];
336 tc->profileSample();
337 }
338
339 schedule(curTick + interval);
340}
341
342void
343BaseCPU::post_interrupt(int int_num, int index)
344{
345 DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
346
347 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
348 panic("int_num out of bounds\n");
349
350 if (index < 0 || index >= sizeof(uint64_t) * 8)
351 panic("int_num out of bounds\n");
352
353 checkInterrupts = true;
354 interrupts[int_num] |= 1 << index;
355 intstatus |= (ULL(1) << int_num);
356}
357
358void
359BaseCPU::clear_interrupt(int int_num, int index)
360{
361 DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
362
363 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
364 panic("int_num out of bounds\n");
365
366 if (index < 0 || index >= sizeof(uint64_t) * 8)
367 panic("int_num out of bounds\n");
368
369 interrupts[int_num] &= ~(1 << index);
370 if (interrupts[int_num] == 0)
371 intstatus &= ~(ULL(1) << int_num);
372}
373
374void
375BaseCPU::clear_interrupts()
376{
377 DPRINTF(Interrupt, "Interrupts all cleared\n");
378
379 memset(interrupts, 0, sizeof(interrupts));
380 intstatus = 0;
381}
382
383
384void
385BaseCPU::serialize(std::ostream &os)
386{
387 SERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
388 SERIALIZE_SCALAR(intstatus);
389}
390
391void
392BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
393{
394 UNSERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
395 UNSERIALIZE_SCALAR(intstatus);
396}
397
398#endif // FULL_SYSTEM
399
400void
401BaseCPU::traceFunctionsInternal(Addr pc)
402{
403 if (!debugSymbolTable)
404 return;
405
406 // if pc enters different function, print new function symbol and
407 // update saved range. Otherwise do nothing.
408 if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
409 string sym_str;
410 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
411 currentFunctionStart,
412 currentFunctionEnd);
413
414 if (!found) {
415 // no symbol found: use addr as label
416 sym_str = csprintf("0x%x", pc);
417 currentFunctionStart = pc;
418 currentFunctionEnd = pc + 1;
419 }
420
421 ccprintf(*functionTraceStream, " (%d)\n%d: %s",
422 curTick - functionEntryTick, curTick, sym_str);
423 functionEntryTick = curTick;
424 }
425}
426
427
428DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU)
168#if FULL_SYSTEM
169 memset(interrupts, 0, sizeof(interrupts));
170 intstatus = 0;
171#endif
172
173 functionTracingEnabled = false;
174 if (p->functionTrace) {
175 functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
176 currentFunctionStart = currentFunctionEnd = 0;
177 functionEntryTick = p->functionTraceStart;
178
179 if (p->functionTraceStart == 0) {
180 functionTracingEnabled = true;
181 } else {
182 Event *e =
183 new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
184 true);
185 e->schedule(p->functionTraceStart);
186 }
187 }
188#if FULL_SYSTEM
189 profileEvent = NULL;
190 if (params->profile)
191 profileEvent = new ProfileEvent(this, params->profile);
192#endif
193}
194
195BaseCPU::Params::Params()
196{
197#if FULL_SYSTEM
198 profile = false;
199#endif
200 checker = NULL;
201}
202
203void
204BaseCPU::enableFunctionTrace()
205{
206 functionTracingEnabled = true;
207}
208
209BaseCPU::~BaseCPU()
210{
211}
212
213void
214BaseCPU::init()
215{
216 if (!params->deferRegistration)
217 registerThreadContexts();
218}
219
220void
221BaseCPU::startup()
222{
223#if FULL_SYSTEM
224 if (!params->deferRegistration && profileEvent)
225 profileEvent->schedule(curTick);
226#endif
227
228 if (params->progress_interval) {
229 new CPUProgressEvent(&mainEventQueue, params->progress_interval,
230 this);
231 }
232}
233
234
235void
236BaseCPU::regStats()
237{
238 using namespace Stats;
239
240 numCycles
241 .name(name() + ".numCycles")
242 .desc("number of cpu cycles simulated")
243 ;
244
245 int size = threadContexts.size();
246 if (size > 1) {
247 for (int i = 0; i < size; ++i) {
248 stringstream namestr;
249 ccprintf(namestr, "%s.ctx%d", name(), i);
250 threadContexts[i]->regStats(namestr.str());
251 }
252 } else if (size == 1)
253 threadContexts[0]->regStats(name());
254
255#if FULL_SYSTEM
256#endif
257}
258
259
260void
261BaseCPU::registerThreadContexts()
262{
263 for (int i = 0; i < threadContexts.size(); ++i) {
264 ThreadContext *tc = threadContexts[i];
265
266#if FULL_SYSTEM
267 int id = params->cpu_id;
268 if (id != -1)
269 id += i;
270
271 tc->setCpuId(system->registerThreadContext(tc, id));
272#else
273 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc));
274#endif
275 }
276}
277
278
279void
280BaseCPU::switchOut()
281{
282// panic("This CPU doesn't support sampling!");
283#if FULL_SYSTEM
284 if (profileEvent && profileEvent->scheduled())
285 profileEvent->deschedule();
286#endif
287}
288
289void
290BaseCPU::takeOverFrom(BaseCPU *oldCPU)
291{
292 assert(threadContexts.size() == oldCPU->threadContexts.size());
293
294 for (int i = 0; i < threadContexts.size(); ++i) {
295 ThreadContext *newTC = threadContexts[i];
296 ThreadContext *oldTC = oldCPU->threadContexts[i];
297
298 newTC->takeOverFrom(oldTC);
299
300 CpuEvent::replaceThreadContext(oldTC, newTC);
301
302 assert(newTC->readCpuId() == oldTC->readCpuId());
303#if FULL_SYSTEM
304 system->replaceThreadContext(newTC, newTC->readCpuId());
305#else
306 assert(newTC->getProcessPtr() == oldTC->getProcessPtr());
307 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId());
308#endif
309
310// TheISA::compareXCs(oldXC, newXC);
311 }
312
313#if FULL_SYSTEM
314 for (int i = 0; i < TheISA::NumInterruptLevels; ++i)
315 interrupts[i] = oldCPU->interrupts[i];
316 intstatus = oldCPU->intstatus;
317 checkInterrupts = oldCPU->checkInterrupts;
318
319 for (int i = 0; i < threadContexts.size(); ++i)
320 threadContexts[i]->profileClear();
321
322 // The Sampler must take care of this!
323// if (profileEvent)
324// profileEvent->schedule(curTick);
325#endif
326}
327
328
329#if FULL_SYSTEM
330BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
331 : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
332{ }
333
334void
335BaseCPU::ProfileEvent::process()
336{
337 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) {
338 ThreadContext *tc = cpu->threadContexts[i];
339 tc->profileSample();
340 }
341
342 schedule(curTick + interval);
343}
344
345void
346BaseCPU::post_interrupt(int int_num, int index)
347{
348 DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index);
349
350 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
351 panic("int_num out of bounds\n");
352
353 if (index < 0 || index >= sizeof(uint64_t) * 8)
354 panic("int_num out of bounds\n");
355
356 checkInterrupts = true;
357 interrupts[int_num] |= 1 << index;
358 intstatus |= (ULL(1) << int_num);
359}
360
361void
362BaseCPU::clear_interrupt(int int_num, int index)
363{
364 DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index);
365
366 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels)
367 panic("int_num out of bounds\n");
368
369 if (index < 0 || index >= sizeof(uint64_t) * 8)
370 panic("int_num out of bounds\n");
371
372 interrupts[int_num] &= ~(1 << index);
373 if (interrupts[int_num] == 0)
374 intstatus &= ~(ULL(1) << int_num);
375}
376
377void
378BaseCPU::clear_interrupts()
379{
380 DPRINTF(Interrupt, "Interrupts all cleared\n");
381
382 memset(interrupts, 0, sizeof(interrupts));
383 intstatus = 0;
384}
385
386
387void
388BaseCPU::serialize(std::ostream &os)
389{
390 SERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
391 SERIALIZE_SCALAR(intstatus);
392}
393
394void
395BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
396{
397 UNSERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels);
398 UNSERIALIZE_SCALAR(intstatus);
399}
400
401#endif // FULL_SYSTEM
402
403void
404BaseCPU::traceFunctionsInternal(Addr pc)
405{
406 if (!debugSymbolTable)
407 return;
408
409 // if pc enters different function, print new function symbol and
410 // update saved range. Otherwise do nothing.
411 if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
412 string sym_str;
413 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
414 currentFunctionStart,
415 currentFunctionEnd);
416
417 if (!found) {
418 // no symbol found: use addr as label
419 sym_str = csprintf("0x%x", pc);
420 currentFunctionStart = pc;
421 currentFunctionEnd = pc + 1;
422 }
423
424 ccprintf(*functionTraceStream, " (%d)\n%d: %s",
425 curTick - functionEntryTick, curTick, sym_str);
426 functionEntryTick = curTick;
427 }
428}
429
430
431DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU)