base.cc revision 1400
1/* 2 * Copyright (c) 2002-2004 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_cpu.hh" 38#include "cpu/exec_context.hh" 39#include "sim/param.hh" 40#include "sim/sim_events.hh" 41 42using namespace std; 43 44vector<BaseCPU *> BaseCPU::cpuList; 45 46// This variable reflects the max number of threads in any CPU. Be 47// careful to only use it once all the CPUs that you care about have 48// been initialized 49int maxThreadsPerCPU = 1; 50 51#ifdef FULL_SYSTEM 52BaseCPU::BaseCPU(Params *p) 53 : SimObject(p->name), frequency(p->freq), checkInterrupts(true), 54 params(p), number_of_threads(p->numberOfThreads), system(p->system) 55#else 56BaseCPU::BaseCPU(Params *p) 57 : SimObject(p->name), params(p), number_of_threads(p->numberOfThreads) 58#endif 59{ 60 // add self to global list of CPUs 61 cpuList.push_back(this); 62 63 if (number_of_threads > maxThreadsPerCPU) 64 maxThreadsPerCPU = number_of_threads; 65 66 // allocate per-thread instruction-based event queues 67 comInstEventQueue = new EventQueue *[number_of_threads]; 68 for (int i = 0; i < number_of_threads; ++i) 69 comInstEventQueue[i] = new EventQueue("instruction-based event queue"); 70 71 // 72 // set up instruction-count-based termination events, if any 73 // 74 if (p->max_insts_any_thread != 0) 75 for (int i = 0; i < number_of_threads; ++i) 76 new SimExitEvent(comInstEventQueue[i], p->max_insts_any_thread, 77 "a thread reached the max instruction count"); 78 79 if (p->max_insts_all_threads != 0) { 80 // allocate & initialize shared downcounter: each event will 81 // decrement this when triggered; simulation will terminate 82 // when counter reaches 0 83 int *counter = new int; 84 *counter = number_of_threads; 85 for (int i = 0; i < number_of_threads; ++i) 86 new CountedExitEvent(comInstEventQueue[i], 87 "all threads reached the max instruction count", 88 p->max_insts_all_threads, *counter); 89 } 90 91 // allocate per-thread load-based event queues 92 comLoadEventQueue = new EventQueue *[number_of_threads]; 93 for (int i = 0; i < number_of_threads; ++i) 94 comLoadEventQueue[i] = new EventQueue("load-based event queue"); 95 96 // 97 // set up instruction-count-based termination events, if any 98 // 99 if (p->max_loads_any_thread != 0) 100 for (int i = 0; i < number_of_threads; ++i) 101 new SimExitEvent(comLoadEventQueue[i], p->max_loads_any_thread, 102 "a thread reached the max load count"); 103 104 if (p->max_loads_all_threads != 0) { 105 // allocate & initialize shared downcounter: each event will 106 // decrement this when triggered; simulation will terminate 107 // when counter reaches 0 108 int *counter = new int; 109 *counter = number_of_threads; 110 for (int i = 0; i < number_of_threads; ++i) 111 new CountedExitEvent(comLoadEventQueue[i], 112 "all threads reached the max load count", 113 p->max_loads_all_threads, *counter); 114 } 115 116#ifdef FULL_SYSTEM 117 memset(interrupts, 0, sizeof(interrupts)); 118 intstatus = 0; 119#endif 120 121 functionTracingEnabled = false; 122 if (p->functionTrace) { 123 functionTraceStream = simout.find(csprintf("ftrace.%s", name())); 124 currentFunctionStart = currentFunctionEnd = 0; 125 functionEntryTick = p->functionTraceStart; 126 127 if (p->functionTraceStart == 0) { 128 functionTracingEnabled = true; 129 } else { 130 Event *e = 131 new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this, 132 true); 133 e->schedule(p->functionTraceStart); 134 } 135 } 136} 137 138 139void 140BaseCPU::enableFunctionTrace() 141{ 142 functionTracingEnabled = true; 143} 144 145BaseCPU::~BaseCPU() 146{ 147} 148 149void 150BaseCPU::init() 151{ 152 if (!params->deferRegistration) 153 registerExecContexts(); 154} 155 156void 157BaseCPU::regStats() 158{ 159 using namespace Stats; 160 161 numCycles 162 .name(name() + ".numCycles") 163 .desc("number of cpu cycles simulated") 164 ; 165 166 int size = execContexts.size(); 167 if (size > 1) { 168 for (int i = 0; i < size; ++i) { 169 stringstream namestr; 170 ccprintf(namestr, "%s.ctx%d", name(), i); 171 execContexts[i]->regStats(namestr.str()); 172 } 173 } else if (size == 1) 174 execContexts[0]->regStats(name()); 175} 176 177 178void 179BaseCPU::registerExecContexts() 180{ 181 for (int i = 0; i < execContexts.size(); ++i) { 182 ExecContext *xc = execContexts[i]; 183 int cpu_id; 184 185#ifdef FULL_SYSTEM 186 cpu_id = system->registerExecContext(xc); 187#else 188 cpu_id = xc->process->registerExecContext(xc); 189#endif 190 191 xc->cpu_id = cpu_id; 192 } 193} 194 195 196void 197BaseCPU::switchOut() 198{ 199 // default: do nothing 200} 201 202void 203BaseCPU::takeOverFrom(BaseCPU *oldCPU) 204{ 205 assert(execContexts.size() == oldCPU->execContexts.size()); 206 207 for (int i = 0; i < execContexts.size(); ++i) { 208 ExecContext *newXC = execContexts[i]; 209 ExecContext *oldXC = oldCPU->execContexts[i]; 210 211 newXC->takeOverFrom(oldXC); 212 assert(newXC->cpu_id == oldXC->cpu_id); 213#ifdef FULL_SYSTEM 214 system->replaceExecContext(newXC, newXC->cpu_id); 215#else 216 assert(newXC->process == oldXC->process); 217 newXC->process->replaceExecContext(newXC, newXC->cpu_id); 218#endif 219 } 220 221#ifdef FULL_SYSTEM 222 for (int i = 0; i < NumInterruptLevels; ++i) 223 interrupts[i] = oldCPU->interrupts[i]; 224 intstatus = oldCPU->intstatus; 225#endif 226} 227 228 229#ifdef FULL_SYSTEM 230void 231BaseCPU::post_interrupt(int int_num, int index) 232{ 233 DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index); 234 235 if (int_num < 0 || int_num >= NumInterruptLevels) 236 panic("int_num out of bounds\n"); 237 238 if (index < 0 || index >= sizeof(uint64_t) * 8) 239 panic("int_num out of bounds\n"); 240 241 checkInterrupts = true; 242 interrupts[int_num] |= 1 << index; 243 intstatus |= (ULL(1) << int_num); 244} 245 246void 247BaseCPU::clear_interrupt(int int_num, int index) 248{ 249 DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index); 250 251 if (int_num < 0 || int_num >= NumInterruptLevels) 252 panic("int_num out of bounds\n"); 253 254 if (index < 0 || index >= sizeof(uint64_t) * 8) 255 panic("int_num out of bounds\n"); 256 257 interrupts[int_num] &= ~(1 << index); 258 if (interrupts[int_num] == 0) 259 intstatus &= ~(ULL(1) << int_num); 260} 261 262void 263BaseCPU::clear_interrupts() 264{ 265 DPRINTF(Interrupt, "Interrupts all cleared\n"); 266 267 memset(interrupts, 0, sizeof(interrupts)); 268 intstatus = 0; 269} 270 271 272void 273BaseCPU::serialize(std::ostream &os) 274{ 275 SERIALIZE_ARRAY(interrupts, NumInterruptLevels); 276 SERIALIZE_SCALAR(intstatus); 277} 278 279void 280BaseCPU::unserialize(Checkpoint *cp, const std::string §ion) 281{ 282 UNSERIALIZE_ARRAY(interrupts, NumInterruptLevels); 283 UNSERIALIZE_SCALAR(intstatus); 284} 285 286#endif // FULL_SYSTEM 287 288void 289BaseCPU::traceFunctionsInternal(Addr pc) 290{ 291 if (!debugSymbolTable) 292 return; 293 294 // if pc enters different function, print new function symbol and 295 // update saved range. Otherwise do nothing. 296 if (pc < currentFunctionStart || pc >= currentFunctionEnd) { 297 string sym_str; 298 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str, 299 currentFunctionStart, 300 currentFunctionEnd); 301 302 if (!found) { 303 // no symbol found: use addr as label 304 sym_str = csprintf("0x%x", pc); 305 currentFunctionStart = pc; 306 currentFunctionEnd = pc + 1; 307 } 308 309 ccprintf(*functionTraceStream, " (%d)\n%d: %s", 310 curTick - functionEntryTick, curTick, sym_str); 311 functionEntryTick = curTick; 312 } 313} 314 315 316DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU) 317