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