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 "base/trace.hh" 41#include "cpu/base.hh" 42#include "cpu/cpuevent.hh" 43#include "cpu/thread_context.hh" 44#include "cpu/profile.hh" 45#include "params/BaseCPU.hh" 46#include "sim/sim_exit.hh" 47#include "sim/process.hh" 48#include "sim/sim_events.hh" 49#include "sim/system.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(BaseCPU *_cpu, Tick ival) 64 : Event(Event::Progress_Event_Pri), interval(ival), lastNumInst(0), 65 cpu(_cpu) 66{ 67 if (interval) 68 cpu->schedule(this, curTick + interval); 69} 70 71void 72CPUProgressEvent::process() 73{ 74 Counter temp = cpu->totalInstructions(); 75#ifndef NDEBUG 76 double ipc = double(temp - lastNumInst) / (interval / cpu->ticks(1)); 77 78 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n", 79 cpu->name(), temp - lastNumInst, ipc); 80 ipc = 0.0; 81#else 82 cprintf("%lli: %s progress event, instructions committed: %lli\n", 83 curTick, cpu->name(), temp - lastNumInst); 84#endif 85 lastNumInst = temp; 86 cpu->schedule(this, curTick + interval); 87} 88 89const char * 90CPUProgressEvent::description() const 91{ 92 return "CPU Progress"; 93} 94 95#if FULL_SYSTEM 96BaseCPU::BaseCPU(Params *p)
|
97 : MemObject(p), clock(p->clock), instCnt(0), interrupts(p->interrupts),
|
97 : MemObject(p), clock(p->clock), instCnt(0), _cpuId(p->cpu_id), 98 interrupts(p->interrupts), |
99 number_of_threads(p->numThreads), system(p->system), 100 phase(p->phase) 101#else 102BaseCPU::BaseCPU(Params *p)
|
102 : MemObject(p), clock(p->clock),
|
103 : MemObject(p), clock(p->clock), _cpuId(p->cpu_id), |
104 number_of_threads(p->numThreads), system(p->system), 105 phase(p->phase) 106#endif 107{ 108// currentTick = curTick; 109
|
110 // if Python did not provide a valid ID, do it here 111 if (_cpuId == -1 ) { 112 _cpuId = cpuList.size(); 113 } 114 |
115 // add self to global list of CPUs 116 cpuList.push_back(this); 117
|
118 DPRINTF(SyscallVerbose, "Constructing CPU with id %d\n", _cpuId); 119 |
120 if (number_of_threads > maxThreadsPerCPU) 121 maxThreadsPerCPU = number_of_threads; 122 123 // allocate per-thread instruction-based event queues 124 comInstEventQueue = new EventQueue *[number_of_threads]; 125 for (int i = 0; i < number_of_threads; ++i) 126 comInstEventQueue[i] = new EventQueue("instruction-based event queue"); 127 128 // 129 // set up instruction-count-based termination events, if any 130 // 131 if (p->max_insts_any_thread != 0) { 132 const char *cause = "a thread reached the max instruction count"; 133 for (int i = 0; i < number_of_threads; ++i) { 134 Event *event = new SimLoopExitEvent(cause, 0); 135 comInstEventQueue[i]->schedule(event, p->max_insts_any_thread); 136 } 137 } 138 139 if (p->max_insts_all_threads != 0) { 140 const char *cause = "all threads reached the max instruction count"; 141 142 // allocate & initialize shared downcounter: each event will 143 // decrement this when triggered; simulation will terminate 144 // when counter reaches 0 145 int *counter = new int; 146 *counter = number_of_threads; 147 for (int i = 0; i < number_of_threads; ++i) { 148 Event *event = new CountedExitEvent(cause, *counter); 149 comInstEventQueue[i]->schedule(event, p->max_insts_any_thread); 150 } 151 } 152 153 // allocate per-thread load-based event queues 154 comLoadEventQueue = new EventQueue *[number_of_threads]; 155 for (int i = 0; i < number_of_threads; ++i) 156 comLoadEventQueue[i] = new EventQueue("load-based event queue"); 157 158 // 159 // set up instruction-count-based termination events, if any 160 // 161 if (p->max_loads_any_thread != 0) { 162 const char *cause = "a thread reached the max load count"; 163 for (int i = 0; i < number_of_threads; ++i) { 164 Event *event = new SimLoopExitEvent(cause, 0); 165 comLoadEventQueue[i]->schedule(event, p->max_loads_any_thread); 166 } 167 } 168 169 if (p->max_loads_all_threads != 0) { 170 const char *cause = "all threads reached the max load count"; 171 // allocate & initialize shared downcounter: each event will 172 // decrement this when triggered; simulation will terminate 173 // when counter reaches 0 174 int *counter = new int; 175 *counter = number_of_threads; 176 for (int i = 0; i < number_of_threads; ++i) { 177 Event *event = new CountedExitEvent(cause, *counter); 178 comLoadEventQueue[i]->schedule(event, p->max_loads_all_threads); 179 } 180 } 181 182 functionTracingEnabled = false; 183 if (p->function_trace) { 184 functionTraceStream = simout.find(csprintf("ftrace.%s", name())); 185 currentFunctionStart = currentFunctionEnd = 0; 186 functionEntryTick = p->function_trace_start; 187 188 if (p->function_trace_start == 0) { 189 functionTracingEnabled = true; 190 } else { 191 typedef EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace> wrap; 192 Event *event = new wrap(this, true); 193 schedule(event, p->function_trace_start); 194 } 195 } 196#if FULL_SYSTEM 197 profileEvent = NULL; 198 if (params()->profile) 199 profileEvent = new ProfileEvent(this, params()->profile); 200#endif 201 tracer = params()->tracer; 202} 203 204void 205BaseCPU::enableFunctionTrace() 206{ 207 functionTracingEnabled = true; 208} 209 210BaseCPU::~BaseCPU() 211{ 212} 213 214void 215BaseCPU::init() 216{ 217 if (!params()->defer_registration) 218 registerThreadContexts(); 219} 220 221void 222BaseCPU::startup() 223{ 224#if FULL_SYSTEM 225 if (!params()->defer_registration && profileEvent) 226 schedule(profileEvent, curTick); 227#endif 228 229 if (params()->progress_interval) { 230 Tick num_ticks = ticks(params()->progress_interval); 231 Event *event = new CPUProgressEvent(this, num_ticks); 232 schedule(event, curTick + num_ticks); 233 } 234} 235 236 237void 238BaseCPU::regStats() 239{ 240 using namespace Stats; 241 242 numCycles 243 .name(name() + ".numCycles") 244 .desc("number of cpu cycles simulated") 245 ; 246 247 int size = threadContexts.size(); 248 if (size > 1) { 249 for (int i = 0; i < size; ++i) { 250 stringstream namestr; 251 ccprintf(namestr, "%s.ctx%d", name(), i); 252 threadContexts[i]->regStats(namestr.str()); 253 } 254 } else if (size == 1) 255 threadContexts[0]->regStats(name()); 256 257#if FULL_SYSTEM 258#endif 259} 260 261Tick 262BaseCPU::nextCycle() 263{ 264 Tick next_tick = curTick - phase + clock - 1; 265 next_tick -= (next_tick % clock); 266 next_tick += phase; 267 return next_tick; 268} 269 270Tick 271BaseCPU::nextCycle(Tick begin_tick) 272{ 273 Tick next_tick = begin_tick; 274 if (next_tick % clock != 0) 275 next_tick = next_tick - (next_tick % clock) + clock; 276 next_tick += phase; 277 278 assert(next_tick >= curTick); 279 return next_tick; 280} 281 282void 283BaseCPU::registerThreadContexts() 284{ 285 for (int i = 0; i < threadContexts.size(); ++i) { 286 ThreadContext *tc = threadContexts[i]; 287 288#if FULL_SYSTEM
|
281 int id = params()->cpu_id;
282 if (id != -1)
283 id += i;
284
285 tc->setCpuId(system->registerThreadContext(tc, id));
|
289 system->registerThreadContext(tc); |
290#else
|
287 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc));
|
291 tc->getProcessPtr()->registerThreadContext(tc); |
292#endif 293 } 294} 295 296 297int 298BaseCPU::findContext(ThreadContext *tc) 299{ 300 for (int i = 0; i < threadContexts.size(); ++i) { 301 if (tc == threadContexts[i]) 302 return i; 303 } 304 return 0; 305} 306 307void 308BaseCPU::switchOut() 309{ 310// panic("This CPU doesn't support sampling!"); 311#if FULL_SYSTEM 312 if (profileEvent && profileEvent->scheduled()) 313 deschedule(profileEvent); 314#endif 315} 316 317void 318BaseCPU::takeOverFrom(BaseCPU *oldCPU, Port *ic, Port *dc) 319{ 320 assert(threadContexts.size() == oldCPU->threadContexts.size()); 321
|
322 _cpuId = oldCPU->cpuId(); 323 |
324 for (int i = 0; i < threadContexts.size(); ++i) { 325 ThreadContext *newTC = threadContexts[i]; 326 ThreadContext *oldTC = oldCPU->threadContexts[i]; 327 328 newTC->takeOverFrom(oldTC); 329 330 CpuEvent::replaceThreadContext(oldTC, newTC); 331
|
326 assert(newTC->readCpuId() == oldTC->readCpuId());
|
332 assert(newTC->cpuId() == oldTC->cpuId()); |
333#if FULL_SYSTEM
|
328 system->replaceThreadContext(newTC, newTC->readCpuId());
|
334 system->replaceThreadContext(newTC, newTC->cpuId()); |
335#else 336 assert(newTC->getProcessPtr() == oldTC->getProcessPtr());
|
331 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId());
|
337 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->cpuId()); |
338#endif 339 340 if (DTRACE(Context)) 341 ThreadContext::compare(oldTC, newTC); 342 } 343 344#if FULL_SYSTEM 345 interrupts = oldCPU->interrupts; 346 347 for (int i = 0; i < threadContexts.size(); ++i) 348 threadContexts[i]->profileClear(); 349 350 if (profileEvent) 351 schedule(profileEvent, curTick); 352#endif 353 354 // Connect new CPU to old CPU's memory only if new CPU isn't 355 // connected to anything. Also connect old CPU's memory to new 356 // CPU. 357 if (!ic->isConnected()) { 358 Port *peer = oldCPU->getPort("icache_port")->getPeer(); 359 ic->setPeer(peer); 360 peer->setPeer(ic); 361 } 362 363 if (!dc->isConnected()) { 364 Port *peer = oldCPU->getPort("dcache_port")->getPeer(); 365 dc->setPeer(peer); 366 peer->setPeer(dc); 367 } 368} 369 370 371#if FULL_SYSTEM 372BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, Tick _interval) 373 : cpu(_cpu), interval(_interval) 374{ } 375 376void 377BaseCPU::ProfileEvent::process() 378{ 379 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) { 380 ThreadContext *tc = cpu->threadContexts[i]; 381 tc->profileSample(); 382 } 383 384 cpu->schedule(this, curTick + interval); 385} 386 387void 388BaseCPU::postInterrupt(int int_num, int index) 389{ 390 interrupts->post(int_num, index); 391} 392 393void 394BaseCPU::clearInterrupt(int int_num, int index) 395{ 396 interrupts->clear(int_num, index); 397} 398 399void 400BaseCPU::clearInterrupts() 401{ 402 interrupts->clearAll(); 403} 404 405void 406BaseCPU::serialize(std::ostream &os) 407{ 408 SERIALIZE_SCALAR(instCnt); 409 interrupts->serialize(os); 410} 411 412void 413BaseCPU::unserialize(Checkpoint *cp, const std::string §ion) 414{ 415 UNSERIALIZE_SCALAR(instCnt); 416 interrupts->unserialize(cp, section); 417} 418 419#endif // FULL_SYSTEM 420 421void 422BaseCPU::traceFunctionsInternal(Addr pc) 423{ 424 if (!debugSymbolTable) 425 return; 426 427 // if pc enters different function, print new function symbol and 428 // update saved range. Otherwise do nothing. 429 if (pc < currentFunctionStart || pc >= currentFunctionEnd) { 430 string sym_str; 431 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str, 432 currentFunctionStart, 433 currentFunctionEnd); 434 435 if (!found) { 436 // no symbol found: use addr as label 437 sym_str = csprintf("0x%x", pc); 438 currentFunctionStart = pc; 439 currentFunctionEnd = pc + 1; 440 } 441 442 ccprintf(*functionTraceStream, " (%d)\n%d: %s", 443 curTick - functionEntryTick, curTick, sym_str); 444 functionEntryTick = curTick; 445 } 446}
|