base.cc revision 2107
16691Stjones1@inf.ed.ac.uk/* 26691Stjones1@inf.ed.ac.uk * Copyright (c) 2002-2005 The Regents of The University of Michigan 36691Stjones1@inf.ed.ac.uk * All rights reserved. 46691Stjones1@inf.ed.ac.uk * 56691Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without 66691Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are 76691Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright 86691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer; 96691Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright 106691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the 116691Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution; 126691Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its 136691Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from 146691Stjones1@inf.ed.ac.uk * this software without specific prior written permission. 156691Stjones1@inf.ed.ac.uk * 166691Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176691Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186691Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196691Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206691Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216691Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226691Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236691Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246691Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256691Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266691Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276691Stjones1@inf.ed.ac.uk */ 286691Stjones1@inf.ed.ac.uk 296691Stjones1@inf.ed.ac.uk#include <iostream> 306691Stjones1@inf.ed.ac.uk#include <string> 316691Stjones1@inf.ed.ac.uk#include <sstream> 326691Stjones1@inf.ed.ac.uk 336691Stjones1@inf.ed.ac.uk#include "base/cprintf.hh" 346691Stjones1@inf.ed.ac.uk#include "base/loader/symtab.hh" 356691Stjones1@inf.ed.ac.uk#include "base/misc.hh" 366691Stjones1@inf.ed.ac.uk#include "base/output.hh" 376691Stjones1@inf.ed.ac.uk#include "cpu/base.hh" 3812334Sgabeblack@google.com#include "cpu/exec_context.hh" 3912106SRekai.GonzalezAlberquilla@arm.com#include "cpu/profile.hh" 409384SAndreas.Sandberg@arm.com#include "cpu/sampler/sampler.hh" 416691Stjones1@inf.ed.ac.uk#include "sim/param.hh" 429384SAndreas.Sandberg@arm.com#include "sim/sim_events.hh" 436691Stjones1@inf.ed.ac.uk 446691Stjones1@inf.ed.ac.uk#include "base/trace.hh" 456691Stjones1@inf.ed.ac.uk 466691Stjones1@inf.ed.ac.ukusing namespace std; 476691Stjones1@inf.ed.ac.uk 486691Stjones1@inf.ed.ac.ukvector<BaseCPU *> BaseCPU::cpuList; 496691Stjones1@inf.ed.ac.uk 509384SAndreas.Sandberg@arm.com// This variable reflects the max number of threads in any CPU. Be 516691Stjones1@inf.ed.ac.uk// careful to only use it once all the CPUs that you care about have 526691Stjones1@inf.ed.ac.uk// been initialized 5313617Sgabeblack@google.comint maxThreadsPerCPU = 1; 5413617Sgabeblack@google.com 556691Stjones1@inf.ed.ac.uk#if FULL_SYSTEM 566691Stjones1@inf.ed.ac.ukBaseCPU::BaseCPU(Params *p) 579384SAndreas.Sandberg@arm.com : SimObject(p->name), clock(p->clock), checkInterrupts(true), 589384SAndreas.Sandberg@arm.com params(p), number_of_threads(p->numberOfThreads), system(p->system) 596691Stjones1@inf.ed.ac.uk#else 606691Stjones1@inf.ed.ac.ukBaseCPU::BaseCPU(Params *p) 616691Stjones1@inf.ed.ac.uk : SimObject(p->name), clock(p->clock), params(p), 626691Stjones1@inf.ed.ac.uk number_of_threads(p->numberOfThreads) 636691Stjones1@inf.ed.ac.uk#endif 6413617Sgabeblack@google.com{ 6510698Sandreas.hansson@arm.com DPRINTF(FullCPU, "BaseCPU: Creating object, mem address %#x.\n", this); 666691Stjones1@inf.ed.ac.uk 676691Stjones1@inf.ed.ac.uk // add self to global list of CPUs 686691Stjones1@inf.ed.ac.uk cpuList.push_back(this); 696691Stjones1@inf.ed.ac.uk 706691Stjones1@inf.ed.ac.uk DPRINTF(FullCPU, "BaseCPU: CPU added to cpuList, mem address %#x.\n", 7113617Sgabeblack@google.com this); 726691Stjones1@inf.ed.ac.uk 736691Stjones1@inf.ed.ac.uk if (number_of_threads > maxThreadsPerCPU) 746691Stjones1@inf.ed.ac.uk maxThreadsPerCPU = number_of_threads; 756691Stjones1@inf.ed.ac.uk 766691Stjones1@inf.ed.ac.uk // allocate per-thread instruction-based event queues 776691Stjones1@inf.ed.ac.uk comInstEventQueue = new EventQueue *[number_of_threads]; 786691Stjones1@inf.ed.ac.uk for (int i = 0; i < number_of_threads; ++i) 7913617Sgabeblack@google.com comInstEventQueue[i] = new EventQueue("instruction-based event queue"); 806691Stjones1@inf.ed.ac.uk 816691Stjones1@inf.ed.ac.uk // 826691Stjones1@inf.ed.ac.uk // set up instruction-count-based termination events, if any 836691Stjones1@inf.ed.ac.uk // 846691Stjones1@inf.ed.ac.uk if (p->max_insts_any_thread != 0) 8513617Sgabeblack@google.com for (int i = 0; i < number_of_threads; ++i) 866691Stjones1@inf.ed.ac.uk new SimExitEvent(comInstEventQueue[i], p->max_insts_any_thread, 876691Stjones1@inf.ed.ac.uk "a thread reached the max instruction count"); 886691Stjones1@inf.ed.ac.uk 896691Stjones1@inf.ed.ac.uk if (p->max_insts_all_threads != 0) { 9012106SRekai.GonzalezAlberquilla@arm.com // allocate & initialize shared downcounter: each event will 9112106SRekai.GonzalezAlberquilla@arm.com // decrement this when triggered; simulation will terminate 926691Stjones1@inf.ed.ac.uk // when counter reaches 0 9310035Sandreas.hansson@arm.com int *counter = new int; 946691Stjones1@inf.ed.ac.uk *counter = number_of_threads; 956691Stjones1@inf.ed.ac.uk for (int i = 0; i < number_of_threads; ++i) 966691Stjones1@inf.ed.ac.uk new CountedExitEvent(comInstEventQueue[i], 976691Stjones1@inf.ed.ac.uk "all threads reached the max instruction count", 986691Stjones1@inf.ed.ac.uk p->max_insts_all_threads, *counter); 9910035Sandreas.hansson@arm.com } 1006691Stjones1@inf.ed.ac.uk 1016691Stjones1@inf.ed.ac.uk // allocate per-thread load-based event queues 1026691Stjones1@inf.ed.ac.uk comLoadEventQueue = new EventQueue *[number_of_threads]; 1036691Stjones1@inf.ed.ac.uk for (int i = 0; i < number_of_threads; ++i) 10412109SRekai.GonzalezAlberquilla@arm.com comLoadEventQueue[i] = new EventQueue("load-based event queue"); 10512109SRekai.GonzalezAlberquilla@arm.com 10612109SRekai.GonzalezAlberquilla@arm.com // 10712109SRekai.GonzalezAlberquilla@arm.com // set up instruction-count-based termination events, if any 10812109SRekai.GonzalezAlberquilla@arm.com // 10912109SRekai.GonzalezAlberquilla@arm.com if (p->max_loads_any_thread != 0) 11012109SRekai.GonzalezAlberquilla@arm.com for (int i = 0; i < number_of_threads; ++i) 11112109SRekai.GonzalezAlberquilla@arm.com new SimExitEvent(comLoadEventQueue[i], p->max_loads_any_thread, 11212109SRekai.GonzalezAlberquilla@arm.com "a thread reached the max load count"); 11312109SRekai.GonzalezAlberquilla@arm.com 11412109SRekai.GonzalezAlberquilla@arm.com if (p->max_loads_all_threads != 0) { 11512109SRekai.GonzalezAlberquilla@arm.com // allocate & initialize shared downcounter: each event will 11613610Sgiacomo.gabrielli@arm.com // decrement this when triggered; simulation will terminate 11713610Sgiacomo.gabrielli@arm.com // when counter reaches 0 11813610Sgiacomo.gabrielli@arm.com int *counter = new int; 11913610Sgiacomo.gabrielli@arm.com *counter = number_of_threads; 12013610Sgiacomo.gabrielli@arm.com for (int i = 0; i < number_of_threads; ++i) 12113610Sgiacomo.gabrielli@arm.com new CountedExitEvent(comLoadEventQueue[i], 1229920Syasuko.eckert@amd.com "all threads reached the max load count", 1239920Syasuko.eckert@amd.com p->max_loads_all_threads, *counter); 12410035Sandreas.hansson@arm.com } 1259920Syasuko.eckert@amd.com 1269920Syasuko.eckert@amd.com#if FULL_SYSTEM 1279920Syasuko.eckert@amd.com memset(interrupts, 0, sizeof(interrupts)); 1289920Syasuko.eckert@amd.com intstatus = 0; 12910033SAli.Saidi@ARM.com#endif 13010035Sandreas.hansson@arm.com 13110033SAli.Saidi@ARM.com functionTracingEnabled = false; 13210033SAli.Saidi@ARM.com if (p->functionTrace) { 13310033SAli.Saidi@ARM.com functionTraceStream = simout.find(csprintf("ftrace.%s", name())); 13410033SAli.Saidi@ARM.com currentFunctionStart = currentFunctionEnd = 0; 1359461Snilay@cs.wisc.edu functionEntryTick = p->functionTraceStart; 1369461Snilay@cs.wisc.edu 1379553Sandreas.hansson@arm.com if (p->functionTraceStart == 0) { 1389553Sandreas.hansson@arm.com functionTracingEnabled = true; 1399553Sandreas.hansson@arm.com } else { 1409384SAndreas.Sandberg@arm.com Event *e = 1419384SAndreas.Sandberg@arm.com new EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace>(this, 1429384SAndreas.Sandberg@arm.com true); 1436691Stjones1@inf.ed.ac.uk e->schedule(p->functionTraceStart); 1446691Stjones1@inf.ed.ac.uk } 1457811Ssteve.reinhardt@amd.com } 1466691Stjones1@inf.ed.ac.uk#if FULL_SYSTEM 1476691Stjones1@inf.ed.ac.uk profileEvent = NULL; 148 if (params->profile) 149 profileEvent = new ProfileEvent(this, params->profile); 150#endif 151} 152 153BaseCPU::Params::Params() 154{ 155#if FULL_SYSTEM 156 profile = false; 157#endif 158} 159 160void 161BaseCPU::enableFunctionTrace() 162{ 163 functionTracingEnabled = true; 164} 165 166BaseCPU::~BaseCPU() 167{ 168} 169 170void 171BaseCPU::init() 172{ 173 if (!params->deferRegistration) 174 registerExecContexts(); 175} 176 177void 178BaseCPU::startup() 179{ 180#if FULL_SYSTEM 181 if (!params->deferRegistration && profileEvent) 182 profileEvent->schedule(curTick); 183#endif 184} 185 186 187void 188BaseCPU::regStats() 189{ 190 using namespace Stats; 191 192 numCycles 193 .name(name() + ".numCycles") 194 .desc("number of cpu cycles simulated") 195 ; 196 197 int size = execContexts.size(); 198 if (size > 1) { 199 for (int i = 0; i < size; ++i) { 200 stringstream namestr; 201 ccprintf(namestr, "%s.ctx%d", name(), i); 202 execContexts[i]->regStats(namestr.str()); 203 } 204 } else if (size == 1) 205 execContexts[0]->regStats(name()); 206} 207 208 209void 210BaseCPU::registerExecContexts() 211{ 212 for (int i = 0; i < execContexts.size(); ++i) { 213 ExecContext *xc = execContexts[i]; 214#if FULL_SYSTEM 215 int id = params->cpu_id; 216 if (id != -1) 217 id += i; 218 219 xc->cpu_id = system->registerExecContext(xc, id); 220#else 221 xc->cpu_id = xc->process->registerExecContext(xc); 222#endif 223 } 224} 225 226 227void 228BaseCPU::switchOut(Sampler *sampler) 229{ 230 panic("This CPU doesn't support sampling!"); 231} 232 233void 234BaseCPU::takeOverFrom(BaseCPU *oldCPU) 235{ 236 assert(execContexts.size() == oldCPU->execContexts.size()); 237 238 for (int i = 0; i < execContexts.size(); ++i) { 239 ExecContext *newXC = execContexts[i]; 240 ExecContext *oldXC = oldCPU->execContexts[i]; 241 242 newXC->takeOverFrom(oldXC); 243 assert(newXC->cpu_id == oldXC->cpu_id); 244#if FULL_SYSTEM 245 system->replaceExecContext(newXC, newXC->cpu_id); 246#else 247 assert(newXC->process == oldXC->process); 248 newXC->process->replaceExecContext(newXC, newXC->cpu_id); 249#endif 250 } 251 252#if FULL_SYSTEM 253 for (int i = 0; i < TheISA::NumInterruptLevels; ++i) 254 interrupts[i] = oldCPU->interrupts[i]; 255 intstatus = oldCPU->intstatus; 256 257 for (int i = 0; i < execContexts.size(); ++i) 258 if (execContexts[i]->profile) 259 execContexts[i]->profile->clear(); 260 261 if (profileEvent) 262 profileEvent->schedule(curTick); 263#endif 264} 265 266 267#if FULL_SYSTEM 268BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, int _interval) 269 : Event(&mainEventQueue), cpu(_cpu), interval(_interval) 270{ } 271 272void 273BaseCPU::ProfileEvent::process() 274{ 275 for (int i = 0, size = cpu->execContexts.size(); i < size; ++i) { 276 ExecContext *xc = cpu->execContexts[i]; 277 xc->profile->sample(xc->profileNode, xc->profilePC); 278 } 279 280 schedule(curTick + interval); 281} 282 283void 284BaseCPU::post_interrupt(int int_num, int index) 285{ 286 DPRINTF(Interrupt, "Interrupt %d:%d posted\n", int_num, index); 287 288 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels) 289 panic("int_num out of bounds\n"); 290 291 if (index < 0 || index >= sizeof(uint64_t) * 8) 292 panic("int_num out of bounds\n"); 293 294 checkInterrupts = true; 295 interrupts[int_num] |= 1 << index; 296 intstatus |= (ULL(1) << int_num); 297} 298 299void 300BaseCPU::clear_interrupt(int int_num, int index) 301{ 302 DPRINTF(Interrupt, "Interrupt %d:%d cleared\n", int_num, index); 303 304 if (int_num < 0 || int_num >= TheISA::NumInterruptLevels) 305 panic("int_num out of bounds\n"); 306 307 if (index < 0 || index >= sizeof(uint64_t) * 8) 308 panic("int_num out of bounds\n"); 309 310 interrupts[int_num] &= ~(1 << index); 311 if (interrupts[int_num] == 0) 312 intstatus &= ~(ULL(1) << int_num); 313} 314 315void 316BaseCPU::clear_interrupts() 317{ 318 DPRINTF(Interrupt, "Interrupts all cleared\n"); 319 320 memset(interrupts, 0, sizeof(interrupts)); 321 intstatus = 0; 322} 323 324 325void 326BaseCPU::serialize(std::ostream &os) 327{ 328 SERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels); 329 SERIALIZE_SCALAR(intstatus); 330} 331 332void 333BaseCPU::unserialize(Checkpoint *cp, const std::string §ion) 334{ 335 UNSERIALIZE_ARRAY(interrupts, TheISA::NumInterruptLevels); 336 UNSERIALIZE_SCALAR(intstatus); 337} 338 339#endif // FULL_SYSTEM 340 341void 342BaseCPU::traceFunctionsInternal(Addr pc) 343{ 344 if (!debugSymbolTable) 345 return; 346 347 // if pc enters different function, print new function symbol and 348 // update saved range. Otherwise do nothing. 349 if (pc < currentFunctionStart || pc >= currentFunctionEnd) { 350 string sym_str; 351 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str, 352 currentFunctionStart, 353 currentFunctionEnd); 354 355 if (!found) { 356 // no symbol found: use addr as label 357 sym_str = csprintf("0x%x", pc); 358 currentFunctionStart = pc; 359 currentFunctionEnd = pc + 1; 360 } 361 362 ccprintf(*functionTraceStream, " (%d)\n%d: %s", 363 curTick - functionEntryTick, curTick, sym_str); 364 functionEntryTick = curTick; 365 } 366} 367 368 369DEFINE_SIM_OBJECT_CLASS_NAME("BaseCPU", BaseCPU) 370