Merge with head.
[gem5.git] / src / cpu / base.cc
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/sim_exit.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
54 using namespace std;
55
56 vector<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
61 int maxThreadsPerCPU = 1;
62
63 CPUProgressEvent::CPUProgressEvent(EventQueue *q, Tick ival,
64 BaseCPU *_cpu)
65 : Event(q, Event::Progress_Event_Pri), interval(ival),
66 lastNumInst(0), cpu(_cpu)
67 {
68 if (interval)
69 schedule(curTick + interval);
70 }
71
72 void
73 CPUProgressEvent::process()
74 {
75 Counter temp = cpu->totalInstructions();
76 #ifndef NDEBUG
77 double ipc = double(temp - lastNumInst) / (interval / cpu->ticks(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
90 const char *
91 CPUProgressEvent::description()
92 {
93 return "CPU Progress";
94 }
95
96 #if FULL_SYSTEM
97 BaseCPU::BaseCPU(Params *p)
98 : MemObject(makeParams(p->name)), clock(p->clock), instCnt(0),
99 params(p), number_of_threads(p->numberOfThreads), system(p->system),
100 phase(p->phase)
101 #else
102 BaseCPU::BaseCPU(Params *p)
103 : MemObject(makeParams(p->name)), clock(p->clock), params(p),
104 number_of_threads(p->numberOfThreads), system(p->system),
105 phase(p->phase)
106 #endif
107 {
108 // currentTick = curTick;
109
110 // add self to global list of CPUs
111 cpuList.push_back(this);
112
113 if (number_of_threads > maxThreadsPerCPU)
114 maxThreadsPerCPU = number_of_threads;
115
116 // allocate per-thread instruction-based event queues
117 comInstEventQueue = new EventQueue *[number_of_threads];
118 for (int i = 0; i < number_of_threads; ++i)
119 comInstEventQueue[i] = new EventQueue("instruction-based event queue");
120
121 //
122 // set up instruction-count-based termination events, if any
123 //
124 if (p->max_insts_any_thread != 0)
125 for (int i = 0; i < number_of_threads; ++i)
126 schedExitSimLoop("a thread reached the max instruction count",
127 p->max_insts_any_thread, 0,
128 comInstEventQueue[i]);
129
130 if (p->max_insts_all_threads != 0) {
131 // allocate & initialize shared downcounter: each event will
132 // decrement this when triggered; simulation will terminate
133 // when counter reaches 0
134 int *counter = new int;
135 *counter = number_of_threads;
136 for (int i = 0; i < number_of_threads; ++i)
137 new CountedExitEvent(comInstEventQueue[i],
138 "all threads reached the max instruction count",
139 p->max_insts_all_threads, *counter);
140 }
141
142 // allocate per-thread load-based event queues
143 comLoadEventQueue = new EventQueue *[number_of_threads];
144 for (int i = 0; i < number_of_threads; ++i)
145 comLoadEventQueue[i] = new EventQueue("load-based event queue");
146
147 //
148 // set up instruction-count-based termination events, if any
149 //
150 if (p->max_loads_any_thread != 0)
151 for (int i = 0; i < number_of_threads; ++i)
152 schedExitSimLoop("a thread reached the max load count",
153 p->max_loads_any_thread, 0,
154 comLoadEventQueue[i]);
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
168 functionTracingEnabled = false;
169 if (p->functionTrace) {
170 functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
171 currentFunctionStart = currentFunctionEnd = 0;
172 functionEntryTick = p->functionTraceStart;
173
174 if (p->functionTraceStart == 0) {
175 functionTracingEnabled = true;
176 } else {
177 new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this,
178 p->functionTraceStart,
179 true);
180 }
181 }
182 #if FULL_SYSTEM
183 profileEvent = NULL;
184 if (params->profile)
185 profileEvent = new ProfileEvent(this, params->profile);
186 #endif
187 tracer = params->tracer;
188 }
189
190 BaseCPU::Params::Params()
191 {
192 #if FULL_SYSTEM
193 profile = false;
194 #endif
195 checker = NULL;
196 tracer = NULL;
197 }
198
199 void
200 BaseCPU::enableFunctionTrace()
201 {
202 functionTracingEnabled = true;
203 }
204
205 BaseCPU::~BaseCPU()
206 {
207 }
208
209 void
210 BaseCPU::init()
211 {
212 if (!params->deferRegistration)
213 registerThreadContexts();
214 }
215
216 void
217 BaseCPU::startup()
218 {
219 #if FULL_SYSTEM
220 if (!params->deferRegistration && profileEvent)
221 profileEvent->schedule(curTick);
222 #endif
223
224 if (params->progress_interval) {
225 new CPUProgressEvent(&mainEventQueue,
226 ticks(params->progress_interval),
227 this);
228 }
229 }
230
231
232 void
233 BaseCPU::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 Tick
257 BaseCPU::nextCycle()
258 {
259 Tick next_tick = curTick - phase + clock - 1;
260 next_tick -= (next_tick % clock);
261 next_tick += phase;
262 return next_tick;
263 }
264
265 Tick
266 BaseCPU::nextCycle(Tick begin_tick)
267 {
268 Tick next_tick = begin_tick;
269 if (next_tick % clock != 0)
270 next_tick = next_tick - (next_tick % clock) + clock;
271 next_tick += phase;
272
273 assert(next_tick >= curTick);
274 return next_tick;
275 }
276
277 void
278 BaseCPU::registerThreadContexts()
279 {
280 for (int i = 0; i < threadContexts.size(); ++i) {
281 ThreadContext *tc = threadContexts[i];
282
283 #if FULL_SYSTEM
284 int id = params->cpu_id;
285 if (id != -1)
286 id += i;
287
288 tc->setCpuId(system->registerThreadContext(tc, id));
289 #else
290 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc));
291 #endif
292 }
293 }
294
295
296 int
297 BaseCPU::findContext(ThreadContext *tc)
298 {
299 for (int i = 0; i < threadContexts.size(); ++i) {
300 if (tc == threadContexts[i])
301 return i;
302 }
303 return 0;
304 }
305
306 void
307 BaseCPU::switchOut()
308 {
309 // panic("This CPU doesn't support sampling!");
310 #if FULL_SYSTEM
311 if (profileEvent && profileEvent->scheduled())
312 profileEvent->deschedule();
313 #endif
314 }
315
316 void
317 BaseCPU::takeOverFrom(BaseCPU *oldCPU, Port *ic, Port *dc)
318 {
319 assert(threadContexts.size() == oldCPU->threadContexts.size());
320
321 for (int i = 0; i < threadContexts.size(); ++i) {
322 ThreadContext *newTC = threadContexts[i];
323 ThreadContext *oldTC = oldCPU->threadContexts[i];
324
325 newTC->takeOverFrom(oldTC);
326
327 CpuEvent::replaceThreadContext(oldTC, newTC);
328
329 assert(newTC->readCpuId() == oldTC->readCpuId());
330 #if FULL_SYSTEM
331 system->replaceThreadContext(newTC, newTC->readCpuId());
332 #else
333 assert(newTC->getProcessPtr() == oldTC->getProcessPtr());
334 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId());
335 #endif
336
337 // TheISA::compareXCs(oldXC, newXC);
338 }
339
340 #if FULL_SYSTEM
341 interrupts = oldCPU->interrupts;
342
343 for (int i = 0; i < threadContexts.size(); ++i)
344 threadContexts[i]->profileClear();
345
346 if (profileEvent)
347 profileEvent->schedule(curTick);
348 #endif
349
350 // Connect new CPU to old CPU's memory only if new CPU isn't
351 // connected to anything. Also connect old CPU's memory to new
352 // CPU.
353 Port *peer;
354 if (ic->getPeer() == NULL) {
355 peer = oldCPU->getPort("icache_port")->getPeer();
356 ic->setPeer(peer);
357 } else {
358 peer = ic->getPeer();
359 }
360 peer->setPeer(ic);
361
362 if (dc->getPeer() == NULL) {
363 peer = oldCPU->getPort("dcache_port")->getPeer();
364 dc->setPeer(peer);
365 } else {
366 peer = dc->getPeer();
367 }
368 peer->setPeer(dc);
369 }
370
371
372 #if FULL_SYSTEM
373 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval)
374 : Event(&mainEventQueue), cpu(_cpu), interval(_interval)
375 { }
376
377 void
378 BaseCPU::ProfileEvent::process()
379 {
380 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) {
381 ThreadContext *tc = cpu->threadContexts[i];
382 tc->profileSample();
383 }
384
385 schedule(curTick + interval);
386 }
387
388 void
389 BaseCPU::post_interrupt(int int_num, int index)
390 {
391 interrupts.post(int_num, index);
392 }
393
394 void
395 BaseCPU::clear_interrupt(int int_num, int index)
396 {
397 interrupts.clear(int_num, index);
398 }
399
400 void
401 BaseCPU::clear_interrupts()
402 {
403 interrupts.clear_all();
404 }
405
406 uint64_t
407 BaseCPU::get_interrupts(int int_num)
408 {
409 return interrupts.get_vec(int_num);
410 }
411
412 void
413 BaseCPU::serialize(std::ostream &os)
414 {
415 SERIALIZE_SCALAR(instCnt);
416 interrupts.serialize(os);
417 }
418
419 void
420 BaseCPU::unserialize(Checkpoint *cp, const std::string &section)
421 {
422 UNSERIALIZE_SCALAR(instCnt);
423 interrupts.unserialize(cp, section);
424 }
425
426 #endif // FULL_SYSTEM
427
428 void
429 BaseCPU::traceFunctionsInternal(Addr pc)
430 {
431 if (!debugSymbolTable)
432 return;
433
434 // if pc enters different function, print new function symbol and
435 // update saved range. Otherwise do nothing.
436 if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
437 string sym_str;
438 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
439 currentFunctionStart,
440 currentFunctionEnd);
441
442 if (!found) {
443 // no symbol found: use addr as label
444 sym_str = csprintf("0x%x", pc);
445 currentFunctionStart = pc;
446 currentFunctionEnd = pc + 1;
447 }
448
449 ccprintf(*functionTraceStream, " (%d)\n%d: %s",
450 curTick - functionEntryTick, curTick, sym_str);
451 functionEntryTick = curTick;
452 }
453 }