system.cc revision 1984
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com */
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com#include "base/loader/object_file.hh"
3012855Sgabeblack@google.com#include "base/loader/symtab.hh"
3112855Sgabeblack@google.com#include "base/remote_gdb.hh"
3212855Sgabeblack@google.com#include "cpu/exec_context.hh"
3312855Sgabeblack@google.com#include "kern/kernel_stats.hh"
3412855Sgabeblack@google.com#include "mem/functional/memory_control.hh"
3512855Sgabeblack@google.com#include "mem/functional/physical.hh"
3612855Sgabeblack@google.com#include "targetarch/vtophys.hh"
3712855Sgabeblack@google.com#include "sim/builder.hh"
3812855Sgabeblack@google.com#include "sim/system.hh"
3912855Sgabeblack@google.com#include "base/trace.hh"
4012855Sgabeblack@google.com
4112855Sgabeblack@google.comusing namespace std;
4212855Sgabeblack@google.com
4312855Sgabeblack@google.comvector<System *> System::systemList;
4412855Sgabeblack@google.com
4512855Sgabeblack@google.comint System::numSystemsRunning = 0;
4612855Sgabeblack@google.com
4712855Sgabeblack@google.comSystem::System(Params *p)
4812855Sgabeblack@google.com    : SimObject(p->name), memctrl(p->memctrl), physmem(p->physmem),
4912855Sgabeblack@google.com      init_param(p->init_param), numcpus(0), params(p)
5012855Sgabeblack@google.com{
5112855Sgabeblack@google.com    // add self to global system list
5212855Sgabeblack@google.com    systemList.push_back(this);
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com    kernelSymtab = new SymbolTable;
5512855Sgabeblack@google.com    consoleSymtab = new SymbolTable;
5612855Sgabeblack@google.com    palSymtab = new SymbolTable;
5712855Sgabeblack@google.com    debugSymbolTable = new SymbolTable;
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com    /**
6012855Sgabeblack@google.com     * Load the kernel, pal, and console code into memory
6112855Sgabeblack@google.com     */
6212855Sgabeblack@google.com    // Load kernel code
6312855Sgabeblack@google.com    kernel = createObjectFile(params->kernel_path);
6412855Sgabeblack@google.com    if (kernel == NULL)
6512855Sgabeblack@google.com        fatal("Could not load kernel file %s", params->kernel_path);
6612855Sgabeblack@google.com
6712855Sgabeblack@google.com    // Load Console Code
6812855Sgabeblack@google.com    console = createObjectFile(params->console_path);
6912855Sgabeblack@google.com    if (console == NULL)
7012855Sgabeblack@google.com        fatal("Could not load console file %s", params->console_path);
7112855Sgabeblack@google.com
7212855Sgabeblack@google.com    // Load pal file
7312855Sgabeblack@google.com    pal = createObjectFile(params->palcode);
7412855Sgabeblack@google.com    if (pal == NULL)
7512855Sgabeblack@google.com        fatal("Could not load PALcode file %s", params->palcode);
7612855Sgabeblack@google.com
7712855Sgabeblack@google.com
7812855Sgabeblack@google.com    // Load program sections into memory
7912855Sgabeblack@google.com    pal->loadSections(physmem, true);
8012855Sgabeblack@google.com    console->loadSections(physmem, true);
8112855Sgabeblack@google.com    kernel->loadSections(physmem, true);
8212855Sgabeblack@google.com
8312855Sgabeblack@google.com    // setup entry points
8412855Sgabeblack@google.com    kernelStart = kernel->textBase();
8512855Sgabeblack@google.com    kernelEnd = kernel->bssBase() + kernel->bssSize();
8612855Sgabeblack@google.com    kernelEntry = kernel->entryPoint();
8712855Sgabeblack@google.com
8812855Sgabeblack@google.com    // load symbols
8912855Sgabeblack@google.com    if (!kernel->loadGlobalSymbols(kernelSymtab))
9012855Sgabeblack@google.com        panic("could not load kernel symbols\n");
9112855Sgabeblack@google.com
9212855Sgabeblack@google.com    if (!kernel->loadLocalSymbols(kernelSymtab))
9312855Sgabeblack@google.com        panic("could not load kernel local symbols\n");
9412855Sgabeblack@google.com
9512855Sgabeblack@google.com    if (!console->loadGlobalSymbols(consoleSymtab))
9612855Sgabeblack@google.com        panic("could not load console symbols\n");
9712855Sgabeblack@google.com
9812855Sgabeblack@google.com    if (!pal->loadGlobalSymbols(palSymtab))
9912855Sgabeblack@google.com        panic("could not load pal symbols\n");
10012855Sgabeblack@google.com
10112855Sgabeblack@google.com    if (!pal->loadLocalSymbols(palSymtab))
10212855Sgabeblack@google.com        panic("could not load pal symbols\n");
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com    if (!kernel->loadGlobalSymbols(debugSymbolTable))
10512855Sgabeblack@google.com        panic("could not load kernel symbols\n");
10612855Sgabeblack@google.com
10712855Sgabeblack@google.com    if (!kernel->loadLocalSymbols(debugSymbolTable))
10812855Sgabeblack@google.com        panic("could not load kernel local symbols\n");
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    if (!console->loadGlobalSymbols(debugSymbolTable))
11112855Sgabeblack@google.com        panic("could not load console symbols\n");
11212855Sgabeblack@google.com
11312855Sgabeblack@google.com    if (!pal->loadGlobalSymbols(debugSymbolTable))
11412855Sgabeblack@google.com        panic("could not load pal symbols\n");
11512855Sgabeblack@google.com
11612855Sgabeblack@google.com    if (!pal->loadLocalSymbols(debugSymbolTable))
11712855Sgabeblack@google.com        panic("could not load pal symbols\n");
11812855Sgabeblack@google.com
11912855Sgabeblack@google.com
12012855Sgabeblack@google.com    DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
12112855Sgabeblack@google.com    DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
12212855Sgabeblack@google.com    DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
12312855Sgabeblack@google.com    DPRINTF(Loader, "Kernel loaded...\n");
12412855Sgabeblack@google.com
12512855Sgabeblack@google.com    Addr addr = 0;
12612855Sgabeblack@google.com#ifdef DEBUG
12712855Sgabeblack@google.com    consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic");
12812855Sgabeblack@google.com#endif
12912855Sgabeblack@google.com
13012855Sgabeblack@google.com    /**
13112855Sgabeblack@google.com     * Copy the osflags (kernel arguments) into the consoles
13212855Sgabeblack@google.com     * memory. (Presently Linux does not use the console service
13312855Sgabeblack@google.com     * routine to get these command line arguments, but Tru64 and
13412855Sgabeblack@google.com     * others do.)
13512855Sgabeblack@google.com     */
13612855Sgabeblack@google.com    if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
13712855Sgabeblack@google.com        Addr paddr = vtophys(physmem, addr);
13812855Sgabeblack@google.com        char *osflags = (char *)physmem->dma_addr(paddr, sizeof(uint32_t));
13912855Sgabeblack@google.com
14012855Sgabeblack@google.com        if (osflags)
14112855Sgabeblack@google.com              strcpy(osflags, params->boot_osflags.c_str());
14212855Sgabeblack@google.com    }
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com    /**
14512855Sgabeblack@google.com     * Set the hardware reset parameter block system type and revision
14612855Sgabeblack@google.com     * information to Tsunami.
14712855Sgabeblack@google.com     */
14812855Sgabeblack@google.com    if (consoleSymtab->findAddress("m5_rpb", addr)) {
14912855Sgabeblack@google.com        Addr paddr = vtophys(physmem, addr);
15012855Sgabeblack@google.com        char *hwrpb = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
15112855Sgabeblack@google.com
15212855Sgabeblack@google.com        if (!hwrpb)
15312855Sgabeblack@google.com            panic("could not translate hwrpb addr\n");
15412855Sgabeblack@google.com
15512855Sgabeblack@google.com        *(uint64_t*)(hwrpb+0x50) = htog(params->system_type);
15612855Sgabeblack@google.com        *(uint64_t*)(hwrpb+0x58) = htog(params->system_rev);
15712855Sgabeblack@google.com    } 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    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