system.cc revision 2107
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 "base/loader/object_file.hh" 30#include "base/loader/symtab.hh" 31#include "base/remote_gdb.hh" 32#include "cpu/exec_context.hh" 33#include "kern/kernel_stats.hh" 34#include "mem/functional/memory_control.hh" 35#include "mem/functional/physical.hh" 36#include "targetarch/vtophys.hh" 37#include "sim/builder.hh" 38#include "arch/isa_traits.hh" 39#include "sim/byteswap.hh" 40#include "sim/system.hh" 41#include "base/trace.hh" 42 43using namespace std; 44using namespace TheISA; 45 46vector<System *> System::systemList; 47 48int System::numSystemsRunning = 0; 49 50System::System(Params *p) 51 : SimObject(p->name), memctrl(p->memctrl), physmem(p->physmem), 52 init_param(p->init_param), numcpus(0), params(p) 53{ 54 // add self to global system list 55 systemList.push_back(this); 56 57 kernelSymtab = new SymbolTable; 58 consoleSymtab = new SymbolTable; 59 palSymtab = new SymbolTable; 60 debugSymbolTable = new SymbolTable; 61 62 /** 63 * Load the kernel, pal, and console code into memory 64 */ 65 // Load kernel code 66 kernel = createObjectFile(params->kernel_path); 67 if (kernel == NULL) 68 fatal("Could not load kernel file %s", params->kernel_path); 69 70 // Load Console Code 71 console = createObjectFile(params->console_path); 72 if (console == NULL) 73 fatal("Could not load console file %s", params->console_path); 74 75 // Load pal file 76 pal = createObjectFile(params->palcode); 77 if (pal == NULL) 78 fatal("Could not load PALcode file %s", params->palcode); 79 80 81 // Load program sections into memory 82 pal->loadSections(physmem, true); 83 console->loadSections(physmem, true); 84 kernel->loadSections(physmem, true); 85 86 // setup entry points 87 kernelStart = kernel->textBase(); 88 kernelEnd = kernel->bssBase() + kernel->bssSize(); 89 kernelEntry = kernel->entryPoint(); 90 91 // load symbols 92 if (!kernel->loadGlobalSymbols(kernelSymtab)) 93 panic("could not load kernel symbols\n"); 94 95 if (!kernel->loadLocalSymbols(kernelSymtab)) 96 panic("could not load kernel local symbols\n"); 97 98 if (!console->loadGlobalSymbols(consoleSymtab)) 99 panic("could not load console symbols\n"); 100 101 if (!pal->loadGlobalSymbols(palSymtab)) 102 panic("could not load pal symbols\n"); 103 104 if (!pal->loadLocalSymbols(palSymtab)) 105 panic("could not load pal symbols\n"); 106 107 if (!kernel->loadGlobalSymbols(debugSymbolTable)) 108 panic("could not load kernel symbols\n"); 109 110 if (!kernel->loadLocalSymbols(debugSymbolTable)) 111 panic("could not load kernel local symbols\n"); 112 113 if (!console->loadGlobalSymbols(debugSymbolTable)) 114 panic("could not load console symbols\n"); 115 116 if (!pal->loadGlobalSymbols(debugSymbolTable)) 117 panic("could not load pal symbols\n"); 118 119 if (!pal->loadLocalSymbols(debugSymbolTable)) 120 panic("could not load pal symbols\n"); 121 122 123 DPRINTF(Loader, "Kernel start = %#x\n", kernelStart); 124 DPRINTF(Loader, "Kernel end = %#x\n", kernelEnd); 125 DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry); 126 DPRINTF(Loader, "Kernel loaded...\n"); 127 128 Addr addr = 0; 129#ifndef NDEBUG 130 consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic"); 131#endif 132 133 /** 134 * Copy the osflags (kernel arguments) into the consoles 135 * memory. (Presently Linux does not use the console service 136 * routine to get these command line arguments, but Tru64 and 137 * others do.) 138 */ 139 if (consoleSymtab->findAddress("env_booted_osflags", addr)) { 140 Addr paddr = vtophys(physmem, addr); 141 char *osflags = (char *)physmem->dma_addr(paddr, sizeof(uint32_t)); 142 143 if (osflags) 144 strcpy(osflags, params->boot_osflags.c_str()); 145 } 146 147 /** 148 * Set the hardware reset parameter block system type and revision 149 * information to Tsunami. 150 */ 151 if (consoleSymtab->findAddress("m5_rpb", addr)) { 152 Addr paddr = vtophys(physmem, addr); 153 char *hwrpb = (char *)physmem->dma_addr(paddr, sizeof(uint64_t)); 154 155 if (!hwrpb) 156 panic("could not translate hwrpb addr\n"); 157 158 *(uint64_t*)(hwrpb+0x50) = htog(params->system_type); 159 *(uint64_t*)(hwrpb+0x58) = htog(params->system_rev); 160 } else 161 panic("could not find hwrpb\n"); 162 163 // increment the number of running systms 164 numSystemsRunning++; 165 166 kernelBinning = new Kernel::Binning(this); 167} 168 169System::~System() 170{ 171 delete kernelSymtab; 172 delete consoleSymtab; 173 delete kernel; 174 delete console; 175 delete pal; 176 177 delete kernelBinning; 178 179#ifdef DEBUG 180 delete consolePanicEvent; 181#endif 182} 183 184 185/** 186 * This function fixes up addresses that are used to match PCs for 187 * hooking simulator events on to target function executions. 188 * 189 * Alpha binaries may have multiple global offset table (GOT) 190 * sections. A function that uses the GOT starts with a 191 * two-instruction prolog which sets the global pointer (gp == r29) to 192 * the appropriate GOT section. The proper gp value is calculated 193 * based on the function address, which must be passed by the caller 194 * in the procedure value register (pv aka t12 == r27). This sequence 195 * looks like the following: 196 * 197 * opcode Ra Rb offset 198 * ldah gp,X(pv) 09 29 27 X 199 * lda gp,Y(gp) 08 29 29 Y 200 * 201 * for some constant offsets X and Y. The catch is that the linker 202 * (or maybe even the compiler, I'm not sure) may recognize that the 203 * caller and callee are using the same GOT section, making this 204 * prolog redundant, and modify the call target to skip these 205 * instructions. If we check for execution of the first instruction 206 * of a function (the one the symbol points to) to detect when to skip 207 * it, we'll miss all these modified calls. It might work to 208 * unconditionally check for the third instruction, but not all 209 * functions have this prolog, and there's some chance that those 210 * first two instructions could have undesired consequences. So we do 211 * the Right Thing and pattern-match the first two instructions of the 212 * function to decide where to patch. 213 * 214 * Eventually this code should be moved into an ISA-specific file. 215 */ 216Addr 217System::fixFuncEventAddr(Addr addr) 218{ 219 // mask for just the opcode, Ra, and Rb fields (not the offset) 220 const uint32_t inst_mask = 0xffff0000; 221 // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27 222 const uint32_t gp_ldah_pattern = (9 << 26) | (29 << 21) | (27 << 16); 223 // lda gp,Y(gp): opcode 8, Ra = 29, rb = 29 224 const uint32_t gp_lda_pattern = (8 << 26) | (29 << 21) | (29 << 16); 225 // instruction size 226 const int sz = sizeof(uint32_t); 227 228 Addr paddr = vtophys(physmem, addr); 229 uint32_t i1 = *(uint32_t *)physmem->dma_addr(paddr, sz); 230 uint32_t i2 = *(uint32_t *)physmem->dma_addr(paddr+sz, sz); 231 232 if ((i1 & inst_mask) == gp_ldah_pattern && 233 (i2 & inst_mask) == gp_lda_pattern) { 234 Addr new_addr = addr + 2*sz; 235 DPRINTF(Loader, "fixFuncEventAddr: %p -> %p", addr, new_addr); 236 return new_addr; 237 } else { 238 return addr; 239 } 240} 241 242 243void 244System::setAlphaAccess(Addr access) 245{ 246 Addr addr = 0; 247 if (consoleSymtab->findAddress("m5AlphaAccess", addr)) { 248 Addr paddr = vtophys(physmem, addr); 249 uint64_t *m5AlphaAccess = 250 (uint64_t *)physmem->dma_addr(paddr, sizeof(uint64_t)); 251 252 if (!m5AlphaAccess) 253 panic("could not translate m5AlphaAccess addr\n"); 254 255 *m5AlphaAccess = htog(EV5::Phys2K0Seg(access)); 256 } else 257 panic("could not find m5AlphaAccess\n"); 258} 259 260 261bool 262System::breakpoint() 263{ 264 return remoteGDB[0]->trap(ALPHA_KENTRY_INT); 265} 266 267int rgdb_wait = -1; 268 269int 270System::registerExecContext(ExecContext *xc, int id) 271{ 272 if (id == -1) { 273 for (id = 0; id < execContexts.size(); id++) { 274 if (!execContexts[id]) 275 break; 276 } 277 } 278 279 if (execContexts.size() <= id) 280 execContexts.resize(id + 1); 281 282 if (execContexts[id]) 283 panic("Cannot have two CPUs with the same id (%d)\n", id); 284 285 execContexts[id] = xc; 286 numcpus++; 287 288 RemoteGDB *rgdb = new RemoteGDB(this, xc); 289 GDBListener *gdbl = new GDBListener(rgdb, 7000 + id); 290 gdbl->listen(); 291 /** 292 * Uncommenting this line waits for a remote debugger to connect 293 * to the simulator before continuing. 294 */ 295 if (rgdb_wait != -1 && rgdb_wait == id) 296 gdbl->accept(); 297 298 if (remoteGDB.size() <= id) { 299 remoteGDB.resize(id + 1); 300 } 301 302 remoteGDB[id] = rgdb; 303 304 return id; 305} 306 307void 308System::startup() 309{ 310 if (!execContexts.empty()) { 311 // activate with zero delay so that we start ticking right 312 // away on cycle 0 313 execContexts[0]->activate(0); 314 } 315} 316 317void 318System::replaceExecContext(ExecContext *xc, int id) 319{ 320 if (id >= execContexts.size()) { 321 panic("replaceExecContext: bad id, %d >= %d\n", 322 id, execContexts.size()); 323 } 324 325 execContexts[id] = xc; 326 remoteGDB[id]->replaceExecContext(xc); 327} 328 329void 330System::regStats() 331{ 332 kernelBinning->regStats(name() + ".kern"); 333} 334 335void 336System::serialize(ostream &os) 337{ 338 kernelBinning->serialize(os); 339 340 kernelSymtab->serialize("kernel_symtab", os); 341 consoleSymtab->serialize("console_symtab", os); 342 palSymtab->serialize("pal_symtab", os); 343} 344 345 346void 347System::unserialize(Checkpoint *cp, const string §ion) 348{ 349 kernelBinning->unserialize(cp, section); 350 351 kernelSymtab->unserialize("kernel_symtab", cp, section); 352 consoleSymtab->unserialize("console_symtab", cp, section); 353 palSymtab->unserialize("pal_symtab", cp, section); 354} 355 356void 357System::printSystems() 358{ 359 vector<System *>::iterator i = systemList.begin(); 360 vector<System *>::iterator end = systemList.end(); 361 for (; i != end; ++i) { 362 System *sys = *i; 363 cerr << "System " << sys->name() << ": " << hex << sys << endl; 364 } 365} 366 367extern "C" 368void 369printSystems() 370{ 371 System::printSystems(); 372} 373 374BEGIN_DECLARE_SIM_OBJECT_PARAMS(System) 375 376 Param<Tick> boot_cpu_frequency; 377 SimObjectParam<MemoryController *> memctrl; 378 SimObjectParam<PhysicalMemory *> physmem; 379 380 Param<string> kernel; 381 Param<string> console; 382 Param<string> pal; 383 384 Param<string> boot_osflags; 385 Param<string> readfile; 386 Param<unsigned int> init_param; 387 388 Param<uint64_t> system_type; 389 Param<uint64_t> system_rev; 390 391 Param<bool> bin; 392 VectorParam<string> binned_fns; 393 Param<bool> bin_int; 394 395END_DECLARE_SIM_OBJECT_PARAMS(System) 396 397BEGIN_INIT_SIM_OBJECT_PARAMS(System) 398 399 INIT_PARAM(boot_cpu_frequency, "Frequency of the boot CPU"), 400 INIT_PARAM(memctrl, "memory controller"), 401 INIT_PARAM(physmem, "phsyical memory"), 402 INIT_PARAM(kernel, "file that contains the kernel code"), 403 INIT_PARAM(console, "file that contains the console code"), 404 INIT_PARAM(pal, "file that contains palcode"), 405 INIT_PARAM_DFLT(boot_osflags, "flags to pass to the kernel during boot", 406 "a"), 407 INIT_PARAM_DFLT(readfile, "file to read startup script from", ""), 408 INIT_PARAM_DFLT(init_param, "numerical value to pass into simulator", 0), 409 INIT_PARAM_DFLT(system_type, "Type of system we are emulating", 34), 410 INIT_PARAM_DFLT(system_rev, "Revision of system we are emulating", 1<<10), 411 INIT_PARAM_DFLT(bin, "is this system to be binned", false), 412 INIT_PARAM(binned_fns, "functions to be broken down and binned"), 413 INIT_PARAM_DFLT(bin_int, "is interrupt code binned seperately?", true) 414 415END_INIT_SIM_OBJECT_PARAMS(System) 416 417CREATE_SIM_OBJECT(System) 418{ 419 System::Params *p = new System::Params; 420 p->name = getInstanceName(); 421 p->boot_cpu_frequency = boot_cpu_frequency; 422 p->memctrl = memctrl; 423 p->physmem = physmem; 424 p->kernel_path = kernel; 425 p->console_path = console; 426 p->palcode = pal; 427 p->boot_osflags = boot_osflags; 428 p->init_param = init_param; 429 p->readfile = readfile; 430 p->system_type = system_type; 431 p->system_rev = system_rev; 432 p->bin = bin; 433 p->binned_fns = binned_fns; 434 p->bin_int = bin_int; 435 return new System(p); 436} 437 438REGISTER_SIM_OBJECT("System", System) 439 440