base.cc revision 4776
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 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::Progress_Event_Pri), interval(ival), 66 lastNumInst(0), cpu(_cpu) 67{ 68 if (interval) 69 schedule(curTick + interval); 70} 71 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), instCnt(0), 99 params(p), number_of_threads(p->numberOfThreads), system(p->system), 100 phase(p->phase) 101#else 102BaseCPU::BaseCPU(Params *p) 103 : MemObject(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 DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this); 110 111 // add self to global list of CPUs 112 cpuList.push_back(this); 113 114 DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n", 115 this); 116 117 if (number_of_threads > maxThreadsPerCPU) 118 maxThreadsPerCPU = number_of_threads; 119 120 // allocate per-thread instruction-based event queues 121 comInstEventQueue = new EventQueue *[number_of_threads]; 122 for (int i = 0; i < number_of_threads; ++i) 123 comInstEventQueue[i] = new EventQueue("instruction-based event queue"); 124 125 // 126 // set up instruction-count-based termination events, if any 127 // 128 if (p->max_insts_any_thread != 0) 129 for (int i = 0; i < number_of_threads; ++i) 130 schedExitSimLoop("a thread reached the max instruction count", 131 p->max_insts_any_thread, 0, 132 comInstEventQueue[i]); 133 134 if (p->max_insts_all_threads != 0) { 135 // allocate & initialize shared downcounter: each event will 136 // decrement this when triggered; simulation will terminate 137 // when counter reaches 0 138 int *counter = new int; 139 *counter = number_of_threads; 140 for (int i = 0; i < number_of_threads; ++i) 141 new CountedExitEvent(comInstEventQueue[i], 142 "all threads reached the max instruction count", 143 p->max_insts_all_threads, *counter); 144 } 145 146 // allocate per-thread load-based event queues 147 comLoadEventQueue = new EventQueue *[number_of_threads]; 148 for (int i = 0; i < number_of_threads; ++i) 149 comLoadEventQueue[i] = new EventQueue("load-based event queue"); 150 151 // 152 // set up instruction-count-based termination events, if any 153 // 154 if (p->max_loads_any_thread != 0) 155 for (int i = 0; i < number_of_threads; ++i) 156 schedExitSimLoop("a thread reached the max load count", 157 p->max_loads_any_thread, 0, 158 comLoadEventQueue[i]); 159 160 if (p->max_loads_all_threads != 0) { 161 // allocate & initialize shared downcounter: each event will 162 // decrement this when triggered; simulation will terminate 163 // when counter reaches 0 164 int *counter = new int; 165 *counter = number_of_threads; 166 for (int i = 0; i < number_of_threads; ++i) 167 new CountedExitEvent(comLoadEventQueue[i], 168 "all threads reached the max load count", 169 p->max_loads_all_threads, *counter); 170 } 171 172 functionTracingEnabled = false; 173 if (p->functionTrace) { 174 functionTraceStream = simout.find(csprintf("ftrace.%s", name())); 175 currentFunctionStart = currentFunctionEnd = 0; 176 functionEntryTick = p->functionTraceStart; 177 178 if (p->functionTraceStart == 0) { 179 functionTracingEnabled = true; 180 } else { 181 new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this, 182 p->functionTraceStart, 183 true); 184 } 185 } 186#if FULL_SYSTEM 187 profileEvent = NULL; 188 if (params->profile) 189 profileEvent = new ProfileEvent(this, params->profile); 190#endif 191 tracer = params->tracer; 192} 193 194BaseCPU::Params::Params() 195{ 196#if FULL_SYSTEM 197 profile = false; 198#endif 199 checker = NULL; 200 tracer = 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, 230 cycles(params->progress_interval), 231 this); 232 } 233} 234 235 236void 237BaseCPU::regStats() 238{ 239 using namespace Stats; 240 241 numCycles 242 .name(name() + ".numCycles") 243 .desc("number of cpu cycles simulated") 244 ; 245 246 int size = threadContexts.size(); 247 if (size > 1) { 248 for (int i = 0; i < size; ++i) { 249 stringstream namestr; 250 ccprintf(namestr, "%s.ctx%d", name(), i); 251 threadContexts[i]->regStats(namestr.str()); 252 } 253 } else if (size == 1) 254 threadContexts[0]->regStats(name()); 255 256#if FULL_SYSTEM 257#endif 258} 259 260Tick 261BaseCPU::nextCycle() 262{ 263 Tick next_tick = curTick - phase + clock - 1; 264 next_tick -= (next_tick % clock); 265 next_tick += phase; 266 return next_tick; 267} 268 269Tick 270BaseCPU::nextCycle(Tick begin_tick) 271{ 272 Tick next_tick = begin_tick; 273 if (next_tick % clock != 0) 274 next_tick = next_tick - (next_tick % clock) + clock; 275 next_tick += phase; 276 277 assert(next_tick >= curTick); 278 return next_tick; 279} 280 281void 282BaseCPU::registerThreadContexts() 283{ 284 for (int i = 0; i < threadContexts.size(); ++i) { 285 ThreadContext *tc = threadContexts[i]; 286 287#if FULL_SYSTEM 288 int id = params->cpu_id; 289 if (id != -1) 290 id += i; 291 292 tc->setCpuId(system->registerThreadContext(tc, id)); 293#else 294 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc)); 295#endif 296 } 297} 298 299 300int 301BaseCPU::findContext(ThreadContext *tc) 302{ 303 for (int i = 0; i < threadContexts.size(); ++i) { 304 if (tc == threadContexts[i]) 305 return i; 306 } 307 return 0; 308} 309 310void 311BaseCPU::switchOut() 312{ 313// panic("This CPU doesn't support sampling!"); 314#if FULL_SYSTEM 315 if (profileEvent && profileEvent->scheduled()) 316 profileEvent->deschedule(); 317#endif 318} 319 320void 321BaseCPU::takeOverFrom(BaseCPU *oldCPU, Port *ic, Port *dc) 322{ 323 assert(threadContexts.size() == oldCPU->threadContexts.size()); 324 325 for (int i = 0; i < threadContexts.size(); ++i) { 326 ThreadContext *newTC = threadContexts[i]; 327 ThreadContext *oldTC = oldCPU->threadContexts[i]; 328 329 newTC->takeOverFrom(oldTC); 330 331 CpuEvent::replaceThreadContext(oldTC, newTC); 332 333 assert(newTC->readCpuId() == oldTC->readCpuId()); 334#if FULL_SYSTEM 335 system->replaceThreadContext(newTC, newTC->readCpuId()); 336#else 337 assert(newTC->getProcessPtr() == oldTC->getProcessPtr()); 338 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId()); 339#endif 340 341// TheISA::compareXCs(oldXC, newXC); 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 // The Sampler must take care of this! 351// if (profileEvent) 352// profileEvent->schedule(curTick); 353#endif 354 355 // Connect new CPU to old CPU's memory only if new CPU isn't 356 // connected to anything. Also connect old CPU's memory to new 357 // CPU. 358 Port *peer; 359 if (ic->getPeer() == NULL) { 360 peer = oldCPU->getPort("icache_port")->getPeer(); 361 ic->setPeer(peer); 362 } else { 363 peer = ic->getPeer(); 364 } 365 peer->setPeer(ic); 366 367 if (dc->getPeer() == NULL) { 368 peer = oldCPU->getPort("dcache_port")->getPeer(); 369 dc->setPeer(peer); 370 } else { 371 peer = dc->getPeer(); 372 } 373 peer->setPeer(dc); 374} 375 376 377#if FULL_SYSTEM 378BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval) 379 : Event(&mainEventQueue), cpu(_cpu), interval(_interval) 380{ } 381 382void 383BaseCPU::ProfileEvent::process() 384{ 385 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) { 386 ThreadContext *tc = cpu->threadContexts[i]; 387 tc->profileSample(); 388 } 389 390 schedule(curTick + interval); 391} 392 393void 394BaseCPU::post_interrupt(int int_num, int index) 395{ 396 interrupts.post(int_num, index); 397} 398 399void 400BaseCPU::clear_interrupt(int int_num, int index) 401{ 402 interrupts.clear(int_num, index); 403} 404 405void 406BaseCPU::clear_interrupts() 407{ 408 interrupts.clear_all(); 409} 410 411uint64_t 412BaseCPU::get_interrupts(int int_num) 413{ 414 return interrupts.get_vec(int_num); 415} 416 417void 418BaseCPU::serialize(std::ostream &os) 419{ 420 SERIALIZE_SCALAR(instCnt); 421 interrupts.serialize(os); 422} 423 424void 425BaseCPU::unserialize(Checkpoint *cp, const std::string §ion) 426{ 427 UNSERIALIZE_SCALAR(instCnt); 428 interrupts.unserialize(cp, section); 429} 430 431#endif // FULL_SYSTEM 432 433void 434BaseCPU::traceFunctionsInternal(Addr pc) 435{ 436 if (!debugSymbolTable) 437 return; 438 439 // if pc enters different function, print new function symbol and 440 // update saved range. Otherwise do nothing. 441 if (pc < currentFunctionStart || pc >= currentFunctionEnd) { 442 string sym_str; 443 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str, 444 currentFunctionStart, 445 currentFunctionEnd); 446 447 if (!found) { 448 // no symbol found: use addr as label 449 sym_str = csprintf("0x%x", pc); 450 currentFunctionStart = pc; 451 currentFunctionEnd = pc + 1; 452 } 453 454 ccprintf(*functionTraceStream, " (%d)\n%d: %s", 455 curTick - functionEntryTick, curTick, sym_str); 456 functionEntryTick = curTick; 457 } 458} 459