system.cc revision 8852:c744483edfcf
1/* 2 * Copyright (c) 2011-2012 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 "arch/isa_traits.hh" 49#include "arch/remote_gdb.hh" 50#include "arch/utility.hh" 51#include "arch/vtophys.hh" 52#include "base/loader/object_file.hh" 53#include "base/loader/symtab.hh" 54#include "base/trace.hh" 55#include "config/the_isa.hh" 56#include "cpu/thread_context.hh" 57#include "debug/Loader.hh" 58#include "debug/WorkItems.hh" 59#include "kern/kernel_stats.hh" 60#include "mem/physical.hh" 61#include "params/System.hh" 62#include "sim/byteswap.hh" 63#include "sim/debug.hh" 64#include "sim/full_system.hh" 65#include "sim/system.hh" 66 67using namespace std; 68using namespace TheISA; 69 70vector<System *> System::systemList; 71 72int System::numSystemsRunning = 0; 73 74System::System(Params *p) 75 : MemObject(p), _systemPort("system_port", this), 76 physmem(p->physmem), 77 _numContexts(0), 78 pagePtr(0), 79 init_param(p->init_param), 80 physProxy(_systemPort), 81 virtProxy(_systemPort), 82 loadAddrMask(p->load_addr_mask), 83 nextPID(0), 84 memoryMode(p->mem_mode), 85 workItemsBegin(0), 86 workItemsEnd(0), 87 numWorkIds(p->num_work_ids), 88 _params(p), 89 totalNumInsts(0), 90 instEventQueue("system instruction-based event queue") 91{ 92 // add self to global system list 93 systemList.push_back(this); 94 95 /** Keep track of all memories we can execute code out of 96 * in our system 97 */ 98 for (int x = 0; x < p->memories.size(); x++) { 99 if (!p->memories[x]) 100 continue; 101 memRanges.push_back(RangeSize(p->memories[x]->start(), 102 p->memories[x]->size())); 103 } 104 105 if (FullSystem) { 106 kernelSymtab = new SymbolTable; 107 if (!debugSymbolTable) 108 debugSymbolTable = new SymbolTable; 109 } 110 111 // Get the generic system master IDs 112 MasterID tmp_id M5_VAR_USED; 113 tmp_id = getMasterId("writebacks"); 114 assert(tmp_id == Request::wbMasterId); 115 tmp_id = getMasterId("functional"); 116 assert(tmp_id == Request::funcMasterId); 117 tmp_id = getMasterId("interrupt"); 118 assert(tmp_id == Request::intMasterId); 119 120} 121 122System::~System() 123{ 124 delete kernelSymtab; 125 delete kernel; 126 127 for (uint32_t j = 0; j < numWorkIds; j++) 128 delete workItemStats[j]; 129} 130 131void 132System::init() 133{ 134 // check that the system port is connected 135 if (!_systemPort.isConnected()) 136 panic("System port on %s is not connected.\n", name()); 137} 138 139Port* 140System::getPort(const std::string &if_name, int idx) 141{ 142 // no need to distinguish at the moment (besides checking) 143 return &_systemPort; 144} 145 146void 147System::setMemoryMode(Enums::MemoryMode mode) 148{ 149 assert(getState() == Drained); 150 memoryMode = mode; 151} 152 153bool System::breakpoint() 154{ 155 if (remoteGDB.size()) 156 return remoteGDB[0]->breakpoint(); 157 return false; 158} 159 160/** 161 * Setting rgdb_wait to a positive integer waits for a remote debugger to 162 * connect to that context ID before continuing. This should really 163 be a parameter on the CPU object or something... 164 */ 165int rgdb_wait = -1; 166 167int 168System::registerThreadContext(ThreadContext *tc, int assigned) 169{ 170 int id; 171 if (assigned == -1) { 172 for (id = 0; id < threadContexts.size(); id++) { 173 if (!threadContexts[id]) 174 break; 175 } 176 177 if (threadContexts.size() <= id) 178 threadContexts.resize(id + 1); 179 } else { 180 if (threadContexts.size() <= assigned) 181 threadContexts.resize(assigned + 1); 182 id = assigned; 183 } 184 185 if (threadContexts[id]) 186 fatal("Cannot have two CPUs with the same id (%d)\n", id); 187 188 threadContexts[id] = tc; 189 _numContexts++; 190 191 int port = getRemoteGDBPort(); 192 if (port) { 193 RemoteGDB *rgdb = new RemoteGDB(this, tc); 194 GDBListener *gdbl = new GDBListener(rgdb, port + id); 195 gdbl->listen(); 196 197 if (rgdb_wait != -1 && rgdb_wait == id) 198 gdbl->accept(); 199 200 if (remoteGDB.size() <= id) { 201 remoteGDB.resize(id + 1); 202 } 203 204 remoteGDB[id] = rgdb; 205 } 206 207 activeCpus.push_back(false); 208 209 return id; 210} 211 212int 213System::numRunningContexts() 214{ 215 int running = 0; 216 for (int i = 0; i < _numContexts; ++i) { 217 if (threadContexts[i]->status() != ThreadContext::Halted) 218 ++running; 219 } 220 return running; 221} 222 223void 224System::initState() 225{ 226 int i; 227 if (FullSystem) { 228 for (i = 0; i < threadContexts.size(); i++) 229 TheISA::startupCPU(threadContexts[i], i); 230 // Moved from the constructor to here since it relies on the 231 // address map being resolved in the interconnect 232 /** 233 * Load the kernel code into memory 234 */ 235 if (params()->kernel == "") { 236 inform("No kernel set for full system simulation. " 237 "Assuming you know what you're doing...\n"); 238 } else { 239 // Load kernel code 240 kernel = createObjectFile(params()->kernel); 241 inform("kernel located at: %s", params()->kernel); 242 243 if (kernel == NULL) 244 fatal("Could not load kernel file %s", params()->kernel); 245 246 // Load program sections into memory 247 kernel->loadSections(physProxy, loadAddrMask); 248 249 // setup entry points 250 kernelStart = kernel->textBase(); 251 kernelEnd = kernel->bssBase() + kernel->bssSize(); 252 kernelEntry = kernel->entryPoint(); 253 254 // load symbols 255 if (!kernel->loadGlobalSymbols(kernelSymtab)) 256 fatal("could not load kernel symbols\n"); 257 258 if (!kernel->loadLocalSymbols(kernelSymtab)) 259 fatal("could not load kernel local symbols\n"); 260 261 if (!kernel->loadGlobalSymbols(debugSymbolTable)) 262 fatal("could not load kernel symbols\n"); 263 264 if (!kernel->loadLocalSymbols(debugSymbolTable)) 265 fatal("could not load kernel local symbols\n"); 266 267 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart); 268 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd); 269 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry); 270 DPRINTF(Loader, "Kernel loaded...\n"); 271 } 272 } 273 274 // increment the number of running systms 275 numSystemsRunning++; 276 277 activeCpus.clear(); 278 279 if (!FullSystem) 280 return; 281 282 for (i = 0; i < threadContexts.size(); i++) 283 TheISA::startupCPU(threadContexts[i], i); 284} 285 286void 287System::replaceThreadContext(ThreadContext *tc, int context_id) 288{ 289 if (context_id >= threadContexts.size()) { 290 panic("replaceThreadContext: bad id, %d >= %d\n", 291 context_id, threadContexts.size()); 292 } 293 294 threadContexts[context_id] = tc; 295 if (context_id < remoteGDB.size()) 296 remoteGDB[context_id]->replaceThreadContext(tc); 297} 298 299Addr 300System::allocPhysPages(int npages) 301{ 302 Addr return_addr = pagePtr << LogVMPageSize; 303 pagePtr += npages; 304 if (pagePtr > physmem->size()) 305 fatal("Out of memory, please increase size of physical memory."); 306 return return_addr; 307} 308 309Addr 310System::memSize() 311{ 312 return physmem->size(); 313} 314 315Addr 316System::freeMemSize() 317{ 318 return physmem->size() - (pagePtr << LogVMPageSize); 319} 320 321bool 322System::isMemory(const Addr addr) const 323{ 324 std::list<Range<Addr> >::const_iterator i; 325 for (i = memRanges.begin(); i != memRanges.end(); i++) { 326 if (*i == addr) 327 return true; 328 } 329 return false; 330} 331 332void 333System::resume() 334{ 335 SimObject::resume(); 336 totalNumInsts = 0; 337} 338 339void 340System::serialize(ostream &os) 341{ 342 if (FullSystem) 343 kernelSymtab->serialize("kernel_symtab", os); 344 SERIALIZE_SCALAR(pagePtr); 345 SERIALIZE_SCALAR(nextPID); 346} 347 348 349void 350System::unserialize(Checkpoint *cp, const string §ion) 351{ 352 if (FullSystem) 353 kernelSymtab->unserialize("kernel_symtab", cp, section); 354 UNSERIALIZE_SCALAR(pagePtr); 355 UNSERIALIZE_SCALAR(nextPID); 356} 357 358void 359System::regStats() 360{ 361 for (uint32_t j = 0; j < numWorkIds ; j++) { 362 workItemStats[j] = new Stats::Histogram(); 363 stringstream namestr; 364 ccprintf(namestr, "work_item_type%d", j); 365 workItemStats[j]->init(20) 366 .name(name() + "." + namestr.str()) 367 .desc("Run time stat for" + namestr.str()) 368 .prereq(*workItemStats[j]); 369 } 370} 371 372void 373System::workItemEnd(uint32_t tid, uint32_t workid) 374{ 375 std::pair<uint32_t,uint32_t> p(tid, workid); 376 if (!lastWorkItemStarted.count(p)) 377 return; 378 379 Tick samp = curTick() - lastWorkItemStarted[p]; 380 DPRINTF(WorkItems, "Work item end: %d\t%d\t%lld\n", tid, workid, samp); 381 382 if (workid >= numWorkIds) 383 fatal("Got workid greater than specified in system configuration\n"); 384 385 workItemStats[workid]->sample(samp); 386 lastWorkItemStarted.erase(p); 387} 388 389void 390System::printSystems() 391{ 392 vector<System *>::iterator i = systemList.begin(); 393 vector<System *>::iterator end = systemList.end(); 394 for (; i != end; ++i) { 395 System *sys = *i; 396 cerr << "System " << sys->name() << ": " << hex << sys << endl; 397 } 398} 399 400void 401printSystems() 402{ 403 System::printSystems(); 404} 405 406MasterID 407System::getMasterId(std::string master_name) 408{ 409 // strip off system name if the string starts with it 410 if (master_name.size() > name().size() && 411 master_name.compare(0, name().size(), name()) == 0) 412 master_name = master_name.erase(0, name().size() + 1); 413 414 // CPUs in switch_cpus ask for ids again after switching 415 for (int i = 0; i < masterIds.size(); i++) { 416 if (masterIds[i] == master_name) { 417 return i; 418 } 419 } 420 421 // todo: Check if stats are enabled yet 422 // I just don't know a good way to do it 423 424 if (false) 425 fatal("Can't request a masterId after regStats(). \ 426 You must do so in init().\n"); 427 428 masterIds.push_back(master_name); 429 430 return masterIds.size() - 1; 431} 432 433std::string 434System::getMasterName(MasterID master_id) 435{ 436 if (master_id >= masterIds.size()) 437 fatal("Invalid master_id passed to getMasterName()\n"); 438 439 return masterIds[master_id]; 440} 441 442const char *System::MemoryModeStrings[3] = {"invalid", "atomic", 443 "timing"}; 444 445System * 446SystemParams::create() 447{ 448 return new System(this); 449} 450