system.cc revision 13784:1941dc118243
1/* 2 * Copyright (c) 2011-2014,2017-2018 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Copyright (c) 2003-2006 The Regents of The University of Michigan 15 * Copyright (c) 2011 Regents of the University of California 16 * All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions are 20 * met: redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer; 22 * redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution; 25 * neither the name of the copyright holders nor the names of its 26 * contributors may be used to endorse or promote products derived from 27 * this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Authors: Steve Reinhardt 42 * Lisa Hsu 43 * Nathan Binkert 44 * Ali Saidi 45 * Rick Strong 46 */ 47 48#include "sim/system.hh" 49 50#include <algorithm> 51 52#include "arch/remote_gdb.hh" 53#include "arch/utility.hh" 54#include "base/loader/object_file.hh" 55#include "base/loader/symtab.hh" 56#include "base/str.hh" 57#include "base/trace.hh" 58#include "config/use_kvm.hh" 59#if USE_KVM 60#include "cpu/kvm/base.hh" 61#include "cpu/kvm/vm.hh" 62#endif 63#include "cpu/base.hh" 64#include "cpu/thread_context.hh" 65#include "debug/Loader.hh" 66#include "debug/WorkItems.hh" 67#include "mem/abstract_mem.hh" 68#include "mem/physical.hh" 69#include "params/System.hh" 70#include "sim/byteswap.hh" 71#include "sim/debug.hh" 72#include "sim/full_system.hh" 73 74/** 75 * To avoid linking errors with LTO, only include the header if we 76 * actually have a definition. 77 */ 78#if THE_ISA != NULL_ISA 79#include "kern/kernel_stats.hh" 80 81#endif 82 83using namespace std; 84using namespace TheISA; 85 86vector<System *> System::systemList; 87 88int System::numSystemsRunning = 0; 89 90System::System(Params *p) 91 : MemObject(p), _systemPort("system_port", this), 92 multiThread(p->multi_thread), 93 pagePtr(0), 94 init_param(p->init_param), 95 physProxy(_systemPort, p->cache_line_size), 96 kernelSymtab(nullptr), 97 kernel(nullptr), 98 loadAddrMask(p->load_addr_mask), 99 loadAddrOffset(p->load_offset), 100#if USE_KVM 101 kvmVM(p->kvm_vm), 102#else 103 kvmVM(nullptr), 104#endif 105 physmem(name() + ".physmem", p->memories, p->mmap_using_noreserve), 106 memoryMode(p->mem_mode), 107 _cacheLineSize(p->cache_line_size), 108 workItemsBegin(0), 109 workItemsEnd(0), 110 numWorkIds(p->num_work_ids), 111 thermalModel(p->thermal_model), 112 _params(p), 113 totalNumInsts(0), 114 instEventQueue("system instruction-based event queue") 115{ 116 // add self to global system list 117 systemList.push_back(this); 118 119#if USE_KVM 120 if (kvmVM) { 121 kvmVM->setSystem(this); 122 } 123#endif 124 125 if (FullSystem) { 126 kernelSymtab = new SymbolTable; 127 if (!debugSymbolTable) 128 debugSymbolTable = new SymbolTable; 129 } 130 131 // check if the cache line size is a value known to work 132 if (!(_cacheLineSize == 16 || _cacheLineSize == 32 || 133 _cacheLineSize == 64 || _cacheLineSize == 128)) 134 warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n"); 135 136 // Get the generic system master IDs 137 MasterID tmp_id M5_VAR_USED; 138 tmp_id = getMasterId(this, "writebacks"); 139 assert(tmp_id == Request::wbMasterId); 140 tmp_id = getMasterId(this, "functional"); 141 assert(tmp_id == Request::funcMasterId); 142 tmp_id = getMasterId(this, "interrupt"); 143 assert(tmp_id == Request::intMasterId); 144 145 if (FullSystem) { 146 if (params()->kernel == "") { 147 inform("No kernel set for full system simulation. " 148 "Assuming you know what you're doing\n"); 149 } else { 150 // Get the kernel code 151 kernel = createObjectFile(params()->kernel); 152 inform("kernel located at: %s", params()->kernel); 153 154 if (kernel == NULL) 155 fatal("Could not load kernel file %s", params()->kernel); 156 157 // setup entry points 158 kernelStart = kernel->textBase(); 159 kernelEnd = kernel->bssBase() + kernel->bssSize(); 160 kernelEntry = kernel->entryPoint(); 161 162 // If load_addr_mask is set to 0x0, then auto-calculate 163 // the smallest mask to cover all kernel addresses so gem5 164 // can relocate the kernel to a new offset. 165 if (loadAddrMask == 0) { 166 Addr shift_amt = findMsbSet(kernelEnd - kernelStart) + 1; 167 loadAddrMask = ((Addr)1 << shift_amt) - 1; 168 } 169 170 // load symbols 171 if (!kernel->loadGlobalSymbols(kernelSymtab)) 172 fatal("could not load kernel symbols\n"); 173 174 if (!kernel->loadLocalSymbols(kernelSymtab)) 175 fatal("could not load kernel local symbols\n"); 176 177 if (!kernel->loadGlobalSymbols(debugSymbolTable)) 178 fatal("could not load kernel symbols\n"); 179 180 if (!kernel->loadLocalSymbols(debugSymbolTable)) 181 fatal("could not load kernel local symbols\n"); 182 183 // Loading only needs to happen once and after memory system is 184 // connected so it will happen in initState() 185 } 186 187 for (const auto &obj_name : p->kernel_extras) { 188 inform("Loading additional kernel object: %s", obj_name); 189 ObjectFile *obj = createObjectFile(obj_name); 190 fatal_if(!obj, "Failed to additional kernel object '%s'.\n", 191 obj_name); 192 kernelExtras.push_back(obj); 193 } 194 } 195 196 // increment the number of running systems 197 numSystemsRunning++; 198 199 // Set back pointers to the system in all memories 200 for (int x = 0; x < params()->memories.size(); x++) 201 params()->memories[x]->system(this); 202} 203 204System::~System() 205{ 206 delete kernelSymtab; 207 delete kernel; 208 209 for (uint32_t j = 0; j < numWorkIds; j++) 210 delete workItemStats[j]; 211} 212 213void 214System::init() 215{ 216 // check that the system port is connected 217 if (!_systemPort.isConnected()) 218 panic("System port on %s is not connected.\n", name()); 219} 220 221Port & 222System::getPort(const std::string &if_name, PortID idx) 223{ 224 // no need to distinguish at the moment (besides checking) 225 return _systemPort; 226} 227 228void 229System::setMemoryMode(Enums::MemoryMode mode) 230{ 231 assert(drainState() == DrainState::Drained); 232 memoryMode = mode; 233} 234 235bool System::breakpoint() 236{ 237 if (remoteGDB.size()) 238 return remoteGDB[0]->breakpoint(); 239 return false; 240} 241 242ContextID 243System::registerThreadContext(ThreadContext *tc, ContextID assigned) 244{ 245 int id = assigned; 246 if (id == InvalidContextID) { 247 // Find an unused context ID for this thread. 248 id = 0; 249 while (id < threadContexts.size() && threadContexts[id]) 250 id++; 251 } 252 253 if (threadContexts.size() <= id) 254 threadContexts.resize(id + 1); 255 256 fatal_if(threadContexts[id], 257 "Cannot have two CPUs with the same id (%d)\n", id); 258 259 threadContexts[id] = tc; 260 261#if THE_ISA != NULL_ISA 262 int port = getRemoteGDBPort(); 263 if (port) { 264 RemoteGDB *rgdb = new RemoteGDB(this, tc, port + id); 265 rgdb->listen(); 266 267 BaseCPU *cpu = tc->getCpuPtr(); 268 if (cpu->waitForRemoteGDB()) { 269 inform("%s: Waiting for a remote GDB connection on port %d.\n", 270 cpu->name(), rgdb->port()); 271 272 rgdb->connect(); 273 } 274 if (remoteGDB.size() <= id) { 275 remoteGDB.resize(id + 1); 276 } 277 278 remoteGDB[id] = rgdb; 279 } 280#endif 281 282 activeCpus.push_back(false); 283 284 return id; 285} 286 287int 288System::numRunningContexts() 289{ 290 return std::count_if( 291 threadContexts.cbegin(), 292 threadContexts.cend(), 293 [] (ThreadContext* tc) { 294 return ((tc->status() != ThreadContext::Halted) && 295 (tc->status() != ThreadContext::Halting)); 296 } 297 ); 298} 299 300void 301System::initState() 302{ 303 if (FullSystem) { 304 for (int i = 0; i < threadContexts.size(); i++) 305 TheISA::startupCPU(threadContexts[i], i); 306 // Moved from the constructor to here since it relies on the 307 // address map being resolved in the interconnect 308 /** 309 * Load the kernel code into memory 310 */ 311 if (params()->kernel != "") { 312 if (params()->kernel_addr_check) { 313 // Validate kernel mapping before loading binary 314 if (!(isMemAddr((kernelStart & loadAddrMask) + 315 loadAddrOffset) && 316 isMemAddr((kernelEnd & loadAddrMask) + 317 loadAddrOffset))) { 318 fatal("Kernel is mapped to invalid location (not memory). " 319 "kernelStart 0x(%x) - kernelEnd 0x(%x) %#x:%#x\n", 320 kernelStart, 321 kernelEnd, (kernelStart & loadAddrMask) + 322 loadAddrOffset, 323 (kernelEnd & loadAddrMask) + loadAddrOffset); 324 } 325 } 326 // Load program sections into memory 327 kernel->loadSections(physProxy, loadAddrMask, loadAddrOffset); 328 for (const auto &extra_kernel : kernelExtras) { 329 extra_kernel->loadSections(physProxy, loadAddrMask, 330 loadAddrOffset); 331 } 332 333 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart); 334 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd); 335 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry); 336 DPRINTF(Loader, "Kernel loaded...\n"); 337 } 338 } 339} 340 341void 342System::replaceThreadContext(ThreadContext *tc, ContextID context_id) 343{ 344 if (context_id >= threadContexts.size()) { 345 panic("replaceThreadContext: bad id, %d >= %d\n", 346 context_id, threadContexts.size()); 347 } 348 349 threadContexts[context_id] = tc; 350 if (context_id < remoteGDB.size()) 351 remoteGDB[context_id]->replaceThreadContext(tc); 352} 353 354bool 355System::validKvmEnvironment() const 356{ 357#if USE_KVM 358 if (threadContexts.empty()) 359 return false; 360 361 for (auto tc : threadContexts) { 362 if (dynamic_cast<BaseKvmCPU*>(tc->getCpuPtr()) == nullptr) { 363 return false; 364 } 365 } 366 return true; 367#else 368 return false; 369#endif 370} 371 372Addr 373System::allocPhysPages(int npages) 374{ 375 Addr return_addr = pagePtr << PageShift; 376 pagePtr += npages; 377 378 Addr next_return_addr = pagePtr << PageShift; 379 380 AddrRange m5opRange(0xffff0000, 0xffffffff); 381 if (m5opRange.contains(next_return_addr)) { 382 warn("Reached m5ops MMIO region\n"); 383 return_addr = 0xffffffff; 384 pagePtr = 0xffffffff >> PageShift; 385 } 386 387 if ((pagePtr << PageShift) > physmem.totalSize()) 388 fatal("Out of memory, please increase size of physical memory."); 389 return return_addr; 390} 391 392Addr 393System::memSize() const 394{ 395 return physmem.totalSize(); 396} 397 398Addr 399System::freeMemSize() const 400{ 401 return physmem.totalSize() - (pagePtr << PageShift); 402} 403 404bool 405System::isMemAddr(Addr addr) const 406{ 407 return physmem.isMemAddr(addr); 408} 409 410void 411System::drainResume() 412{ 413 totalNumInsts = 0; 414} 415 416void 417System::serialize(CheckpointOut &cp) const 418{ 419 if (FullSystem) 420 kernelSymtab->serialize("kernel_symtab", cp); 421 SERIALIZE_SCALAR(pagePtr); 422 serializeSymtab(cp); 423 424 // also serialize the memories in the system 425 physmem.serializeSection(cp, "physmem"); 426} 427 428 429void 430System::unserialize(CheckpointIn &cp) 431{ 432 if (FullSystem) 433 kernelSymtab->unserialize("kernel_symtab", cp); 434 UNSERIALIZE_SCALAR(pagePtr); 435 unserializeSymtab(cp); 436 437 // also unserialize the memories in the system 438 physmem.unserializeSection(cp, "physmem"); 439} 440 441void 442System::regStats() 443{ 444 MemObject::regStats(); 445 446 for (uint32_t j = 0; j < numWorkIds ; j++) { 447 workItemStats[j] = new Stats::Histogram(); 448 stringstream namestr; 449 ccprintf(namestr, "work_item_type%d", j); 450 workItemStats[j]->init(20) 451 .name(name() + "." + namestr.str()) 452 .desc("Run time stat for" + namestr.str()) 453 .prereq(*workItemStats[j]); 454 } 455} 456 457void 458System::workItemEnd(uint32_t tid, uint32_t workid) 459{ 460 std::pair<uint32_t,uint32_t> p(tid, workid); 461 if (!lastWorkItemStarted.count(p)) 462 return; 463 464 Tick samp = curTick() - lastWorkItemStarted[p]; 465 DPRINTF(WorkItems, "Work item end: %d\t%d\t%lld\n", tid, workid, samp); 466 467 if (workid >= numWorkIds) 468 fatal("Got workid greater than specified in system configuration\n"); 469 470 workItemStats[workid]->sample(samp); 471 lastWorkItemStarted.erase(p); 472} 473 474void 475System::printSystems() 476{ 477 ios::fmtflags flags(cerr.flags()); 478 479 vector<System *>::iterator i = systemList.begin(); 480 vector<System *>::iterator end = systemList.end(); 481 for (; i != end; ++i) { 482 System *sys = *i; 483 cerr << "System " << sys->name() << ": " << hex << sys << endl; 484 } 485 486 cerr.flags(flags); 487} 488 489void 490printSystems() 491{ 492 System::printSystems(); 493} 494 495std::string 496System::stripSystemName(const std::string& master_name) const 497{ 498 if (startswith(master_name, name())) { 499 return master_name.substr(name().size()); 500 } else { 501 return master_name; 502 } 503} 504 505MasterID 506System::lookupMasterId(const SimObject* obj) const 507{ 508 MasterID id = Request::invldMasterId; 509 510 // number of occurrences of the SimObject pointer 511 // in the master list. 512 auto obj_number = 0; 513 514 for (int i = 0; i < masters.size(); i++) { 515 if (masters[i].obj == obj) { 516 id = i; 517 obj_number++; 518 } 519 } 520 521 fatal_if(obj_number > 1, 522 "Cannot lookup MasterID by SimObject pointer: " 523 "More than one master is sharing the same SimObject\n"); 524 525 return id; 526} 527 528MasterID 529System::lookupMasterId(const std::string& master_name) const 530{ 531 std::string name = stripSystemName(master_name); 532 533 for (int i = 0; i < masters.size(); i++) { 534 if (masters[i].masterName == name) { 535 return i; 536 } 537 } 538 539 return Request::invldMasterId; 540} 541 542MasterID 543System::getGlobalMasterId(const std::string& master_name) 544{ 545 return _getMasterId(nullptr, master_name); 546} 547 548MasterID 549System::getMasterId(const SimObject* master, std::string submaster) 550{ 551 auto master_name = leafMasterName(master, submaster); 552 return _getMasterId(master, master_name); 553} 554 555MasterID 556System::_getMasterId(const SimObject* master, const std::string& master_name) 557{ 558 std::string name = stripSystemName(master_name); 559 560 // CPUs in switch_cpus ask for ids again after switching 561 for (int i = 0; i < masters.size(); i++) { 562 if (masters[i].masterName == name) { 563 return i; 564 } 565 } 566 567 // Verify that the statistics haven't been enabled yet 568 // Otherwise objects will have sized their stat buckets and 569 // they will be too small 570 571 if (Stats::enabled()) { 572 fatal("Can't request a masterId after regStats(). " 573 "You must do so in init().\n"); 574 } 575 576 // Generate a new MasterID incrementally 577 MasterID master_id = masters.size(); 578 579 // Append the new Master metadata to the group of system Masters. 580 masters.emplace_back(master, name, master_id); 581 582 return masters.back().masterId; 583} 584 585std::string 586System::leafMasterName(const SimObject* master, const std::string& submaster) 587{ 588 if (submaster.empty()) { 589 return master->name(); 590 } else { 591 // Get the full master name by appending the submaster name to 592 // the root SimObject master name 593 return master->name() + "." + submaster; 594 } 595} 596 597std::string 598System::getMasterName(MasterID master_id) 599{ 600 if (master_id >= masters.size()) 601 fatal("Invalid master_id passed to getMasterName()\n"); 602 603 const auto& master_info = masters[master_id]; 604 return master_info.masterName; 605} 606 607System * 608SystemParams::create() 609{ 610 return new System(this); 611} 612