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