system.cc revision 2114
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;
44
45vector<System *> System::systemList;
46
47int System::numSystemsRunning = 0;
48
49System::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
168System::~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 */
215Addr
216System::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
242void
243System::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
260bool
261System::breakpoint()
262{
263    return remoteGDB[0]->trap(ALPHA_KENTRY_INT);
264}
265
266int rgdb_wait = -1;
267
268int
269System::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
306void
307System::startup()
308{
309    int i;
310    for (i = 0; i < execContexts.size(); i++)
311        execContexts[i]->activate(0);
312}
313
314void
315System::replaceExecContext(ExecContext *xc, int id)
316{
317    if (id >= execContexts.size()) {
318        panic("replaceExecContext: bad id, %d >= %d\n",
319              id, execContexts.size());
320    }
321
322    execContexts[id] = xc;
323    remoteGDB[id]->replaceExecContext(xc);
324}
325
326void
327System::regStats()
328{
329    kernelBinning->regStats(name() + ".kern");
330}
331
332void
333System::serialize(ostream &os)
334{
335    kernelBinning->serialize(os);
336
337    kernelSymtab->serialize("kernel_symtab", os);
338    consoleSymtab->serialize("console_symtab", os);
339    palSymtab->serialize("pal_symtab", os);
340}
341
342
343void
344System::unserialize(Checkpoint *cp, const string &section)
345{
346    kernelBinning->unserialize(cp, section);
347
348    kernelSymtab->unserialize("kernel_symtab", cp, section);
349    consoleSymtab->unserialize("console_symtab", cp, section);
350    palSymtab->unserialize("pal_symtab", cp, section);
351}
352
353void
354System::printSystems()
355{
356    vector<System *>::iterator i = systemList.begin();
357    vector<System *>::iterator end = systemList.end();
358    for (; i != end; ++i) {
359        System *sys = *i;
360        cerr << "System " << sys->name() << ": " << hex << sys << endl;
361    }
362}
363
364extern "C"
365void
366printSystems()
367{
368    System::printSystems();
369}
370
371BEGIN_DECLARE_SIM_OBJECT_PARAMS(System)
372
373    Param<Tick> boot_cpu_frequency;
374    SimObjectParam<MemoryController *> memctrl;
375    SimObjectParam<PhysicalMemory *> physmem;
376
377    Param<string> kernel;
378    Param<string> console;
379    Param<string> pal;
380
381    Param<string> boot_osflags;
382    Param<string> readfile;
383    Param<unsigned int> init_param;
384
385    Param<uint64_t> system_type;
386    Param<uint64_t> system_rev;
387
388    Param<bool> bin;
389    VectorParam<string> binned_fns;
390    Param<bool> bin_int;
391
392END_DECLARE_SIM_OBJECT_PARAMS(System)
393
394BEGIN_INIT_SIM_OBJECT_PARAMS(System)
395
396    INIT_PARAM(boot_cpu_frequency, "Frequency of the boot CPU"),
397    INIT_PARAM(memctrl, "memory controller"),
398    INIT_PARAM(physmem, "phsyical memory"),
399    INIT_PARAM(kernel, "file that contains the kernel code"),
400    INIT_PARAM(console, "file that contains the console code"),
401    INIT_PARAM(pal, "file that contains palcode"),
402    INIT_PARAM_DFLT(boot_osflags, "flags to pass to the kernel during boot",
403                    "a"),
404    INIT_PARAM_DFLT(readfile, "file to read startup script from", ""),
405    INIT_PARAM_DFLT(init_param, "numerical value to pass into simulator", 0),
406    INIT_PARAM_DFLT(system_type, "Type of system we are emulating", 34),
407    INIT_PARAM_DFLT(system_rev, "Revision of system we are emulating", 1<<10),
408    INIT_PARAM_DFLT(bin, "is this system to be binned", false),
409    INIT_PARAM(binned_fns, "functions to be broken down and binned"),
410    INIT_PARAM_DFLT(bin_int, "is interrupt code binned seperately?", true)
411
412END_INIT_SIM_OBJECT_PARAMS(System)
413
414CREATE_SIM_OBJECT(System)
415{
416    System::Params *p = new System::Params;
417    p->name = getInstanceName();
418    p->boot_cpu_frequency = boot_cpu_frequency;
419    p->memctrl = memctrl;
420    p->physmem = physmem;
421    p->kernel_path = kernel;
422    p->console_path = console;
423    p->palcode = pal;
424    p->boot_osflags = boot_osflags;
425    p->init_param = init_param;
426    p->readfile = readfile;
427    p->system_type = system_type;
428    p->system_rev = system_rev;
429    p->bin = bin;
430    p->binned_fns = binned_fns;
431    p->bin_int = bin_int;
432    return new System(p);
433}
434
435REGISTER_SIM_OBJECT("System", System)
436
437