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