system.cc revision 2158
12158SN/A/*
22158SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32158SN/A * All rights reserved.
42158SN/A *
52158SN/A * Redistribution and use in source and binary forms, with or without
62158SN/A * modification, are permitted provided that the following conditions are
72158SN/A * met: redistributions of source code must retain the above copyright
82158SN/A * notice, this list of conditions and the following disclaimer;
92158SN/A * redistributions in binary form must reproduce the above copyright
102158SN/A * notice, this list of conditions and the following disclaimer in the
112158SN/A * documentation and/or other materials provided with the distribution;
122158SN/A * neither the name of the copyright holders nor the names of its
132158SN/A * contributors may be used to endorse or promote products derived from
142158SN/A * this software without specific prior written permission.
152158SN/A *
162158SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172158SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182158SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192158SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202158SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212158SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222158SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232158SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242158SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252158SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262158SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu */
282665Ssaidi@eecs.umich.edu
292760Sbinkertn@umich.edu#include "arch/alpha/system.hh"
302158SN/A#include "base/remote_gdb.hh"
312158SN/A#include "base/loader/object_file.hh"
323567Sgblack@eecs.umich.edu#include "base/loader/symtab.hh"
333567Sgblack@eecs.umich.edu#include "base/trace.hh"
342432SN/A#include "mem/functional/memory_control.hh"
352158SN/A#include "mem/functional/physical.hh"
363536Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
372215SN/A#include "sim/builder.hh"
382158SN/A#include "targetarch/vtophys.hh"
392158SN/A
402158SN/Ausing namespace LittleEndianGuest;
412521SN/A
424762Snate@binkert.orgAlphaSystem::AlphaSystem(Params *p)
432158SN/A    : System(p)
442158SN/A{
452158SN/A    consoleSymtab = new SymbolTable;
462158SN/A    palSymtab = new SymbolTable;
472158SN/A
482158SN/A
492158SN/A    /**
502158SN/A     * Load the pal, and console code into memory
512158SN/A     */
522158SN/A    // Load Console Code
532158SN/A    console = createObjectFile(params()->console_path);
542158SN/A    if (console == NULL)
552158SN/A        fatal("Could not load console file %s", params()->console_path);
562158SN/A
572158SN/A    // Load pal file
582158SN/A    pal = createObjectFile(params()->palcode);
594762Snate@binkert.org    if (pal == NULL)
602158SN/A        fatal("Could not load PALcode file %s", params()->palcode);
614762Snate@binkert.org
622158SN/A
632158SN/A    // Load program sections into memory
644762Snate@binkert.org    pal->loadSections(physmem, true);
652158SN/A    console->loadSections(physmem, true);
664762Snate@binkert.org
672158SN/A    // load symbols
682158SN/A    if (!console->loadGlobalSymbols(consoleSymtab))
692158SN/A        panic("could not load console symbols\n");
702521SN/A
712521SN/A    if (!pal->loadGlobalSymbols(palSymtab))
722158SN/A        panic("could not load pal symbols\n");
732158SN/A
742158SN/A    if (!pal->loadLocalSymbols(palSymtab))
752158SN/A        panic("could not load pal symbols\n");
762158SN/A
772158SN/A    if (!console->loadGlobalSymbols(debugSymbolTable))
782158SN/A        panic("could not load console symbols\n");
792158SN/A
802158SN/A    if (!pal->loadGlobalSymbols(debugSymbolTable))
812158SN/A        panic("could not load pal symbols\n");
822158SN/A
832158SN/A    if (!pal->loadLocalSymbols(debugSymbolTable))
842158SN/A        panic("could not load pal symbols\n");
852158SN/A
862158SN/A     Addr addr = 0;
872158SN/A#ifndef NDEBUG
882158SN/A    consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic");
892158SN/A#endif
902158SN/A
912158SN/A    /**
922158SN/A     * Copy the osflags (kernel arguments) into the consoles
932158SN/A     * memory. (Presently Linux does not use the console service
942158SN/A     * routine to get these command line arguments, but Tru64 and
952158SN/A     * others do.)
962158SN/A     */
972158SN/A    if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
982158SN/A        Addr paddr = vtophys(physmem, addr);
992158SN/A        char *osflags = (char *)physmem->dma_addr(paddr, sizeof(uint32_t));
1002158SN/A
1012158SN/A        if (osflags)
1022158SN/A              strcpy(osflags, params()->boot_osflags.c_str());
1032158SN/A    }
1042521SN/A
1052521SN/A    /**
1062158SN/A     * Set the hardware reset parameter block system type and revision
1072158SN/A     * information to Tsunami.
1082158SN/A     */
1092158SN/A    if (consoleSymtab->findAddress("m5_rpb", addr)) {
1102158SN/A        Addr paddr = vtophys(physmem, addr);
1112158SN/A        char *hwrpb = (char *)physmem->dma_addr(paddr, sizeof(uint64_t));
1122158SN/A
1132521SN/A        if (!hwrpb)
1142521SN/A            panic("could not translate hwrpb addr\n");
1152549SN/A
1162521SN/A        *(uint64_t*)(hwrpb+0x50) = htog(params()->system_type);
1172549SN/A        *(uint64_t*)(hwrpb+0x58) = htog(params()->system_rev);
1182158SN/A    } else
1192158SN/A        panic("could not find hwrpb\n");
1202158SN/A
1212158SN/A}
1222158SN/A
1232158SN/AAlphaSystem::~AlphaSystem()
1242158SN/A{
1252158SN/A    delete consoleSymtab;
1262158SN/A    delete console;
1272158SN/A    delete pal;
1282158SN/A#ifdef DEBUG
1292158SN/A    delete consolePanicEvent;
1302158SN/A#endif
1312158SN/A}
1322158SN/A
1332158SN/A/**
1342158SN/A * This function fixes up addresses that are used to match PCs for
1352158SN/A * hooking simulator events on to target function executions.
1362158SN/A *
1372158SN/A * Alpha binaries may have multiple global offset table (GOT)
1382158SN/A * sections.  A function that uses the GOT starts with a
1392158SN/A * two-instruction prolog which sets the global pointer (gp == r29) to
1402158SN/A * the appropriate GOT section.  The proper gp value is calculated
1412158SN/A * based on the function address, which must be passed by the caller
1422158SN/A * in the procedure value register (pv aka t12 == r27).  This sequence
1432158SN/A * looks like the following:
1442158SN/A *
1452158SN/A *			opcode Ra Rb offset
1462158SN/A *	ldah gp,X(pv)     09   29 27   X
1472158SN/A *	lda  gp,Y(gp)     08   29 29   Y
1482158SN/A *
1492158SN/A * for some constant offsets X and Y.  The catch is that the linker
1502158SN/A * (or maybe even the compiler, I'm not sure) may recognize that the
1512158SN/A * caller and callee are using the same GOT section, making this
1522158SN/A * prolog redundant, and modify the call target to skip these
1532158SN/A * instructions.  If we check for execution of the first instruction
1542158SN/A * of a function (the one the symbol points to) to detect when to skip
1552158SN/A * it, we'll miss all these modified calls.  It might work to
1562158SN/A * unconditionally check for the third instruction, but not all
1572158SN/A * functions have this prolog, and there's some chance that those
1582158SN/A * first two instructions could have undesired consequences.  So we do
1592158SN/A * the Right Thing and pattern-match the first two instructions of the
1602158SN/A * function to decide where to patch.
1612158SN/A *
1622158SN/A * Eventually this code should be moved into an ISA-specific file.
1632158SN/A */
1642158SN/AAddr
1652158SN/AAlphaSystem::fixFuncEventAddr(Addr addr)
1662158SN/A{
1672158SN/A    // mask for just the opcode, Ra, and Rb fields (not the offset)
1682158SN/A    const uint32_t inst_mask = 0xffff0000;
1692158SN/A    // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27
1702158SN/A    const uint32_t gp_ldah_pattern = (9 << 26) | (29 << 21) | (27 << 16);
1712158SN/A    // lda  gp,Y(gp): opcode 8, Ra = 29, rb = 29
1722158SN/A    const uint32_t gp_lda_pattern  = (8 << 26) | (29 << 21) | (29 << 16);
1732158SN/A    // instruction size
1742521SN/A    const int sz = sizeof(uint32_t);
1752521SN/A
1762158SN/A    Addr paddr = vtophys(physmem, addr);
1772158SN/A    uint32_t i1 = *(uint32_t *)physmem->dma_addr(paddr, sz);
1782158SN/A    uint32_t i2 = *(uint32_t *)physmem->dma_addr(paddr+sz, sz);
1792521SN/A
1802158SN/A    if ((i1 & inst_mask) == gp_ldah_pattern &&
1812158SN/A        (i2 & inst_mask) == gp_lda_pattern) {
1822158SN/A        Addr new_addr = addr + 2*sz;
1832158SN/A        DPRINTF(Loader, "fixFuncEventAddr: %p -> %p", addr, new_addr);
1842158SN/A        return new_addr;
1852158SN/A    } else {
1862158SN/A        return addr;
1872158SN/A    }
1882158SN/A}
1892158SN/A
1902158SN/A
1912158SN/Avoid
1922158SN/AAlphaSystem::setAlphaAccess(Addr access)
1932521SN/A{
1942158SN/A    Addr addr = 0;
1952158SN/A    if (consoleSymtab->findAddress("m5AlphaAccess", addr)) {
1962158SN/A        Addr paddr = vtophys(physmem, addr);
1972158SN/A        uint64_t *m5AlphaAccess =
1982158SN/A            (uint64_t *)physmem->dma_addr(paddr, sizeof(uint64_t));
1992158SN/A
2002158SN/A        if (!m5AlphaAccess)
2012158SN/A            panic("could not translate m5AlphaAccess addr\n");
2022158SN/A
2032158SN/A        *m5AlphaAccess = htog(EV5::Phys2K0Seg(access));
2042158SN/A    } else
2052158SN/A        panic("could not find m5AlphaAccess\n");
2062158SN/A}
2072158SN/A
2082158SN/Abool
2092158SN/AAlphaSystem::breakpoint()
2102158SN/A{
2112158SN/A    return remoteGDB[0]->trap(ALPHA_KENTRY_INT);
2122158SN/A}
2132158SN/A
2142158SN/Avoid
2154762Snate@binkert.orgAlphaSystem::serialize(std::ostream &os)
2164762Snate@binkert.org{
2172158SN/A    System::serialize(os);
2184762Snate@binkert.org    consoleSymtab->serialize("console_symtab", os);
2192158SN/A    palSymtab->serialize("pal_symtab", os);
220}
221
222
223void
224AlphaSystem::unserialize(Checkpoint *cp, const std::string &section)
225{
226    System::unserialize(cp,section);
227    consoleSymtab->unserialize("console_symtab", cp, section);
228    palSymtab->unserialize("pal_symtab", cp, section);
229}
230
231
232BEGIN_DECLARE_SIM_OBJECT_PARAMS(AlphaSystem)
233
234    Param<Tick> boot_cpu_frequency;
235    SimObjectParam<MemoryController *> memctrl;
236    SimObjectParam<PhysicalMemory *> physmem;
237
238    Param<std::string> kernel;
239    Param<std::string> console;
240    Param<std::string> pal;
241
242    Param<std::string> boot_osflags;
243    Param<std::string> readfile;
244    Param<unsigned int> init_param;
245
246    Param<uint64_t> system_type;
247    Param<uint64_t> system_rev;
248
249    Param<bool> bin;
250    VectorParam<std::string> binned_fns;
251    Param<bool> bin_int;
252
253END_DECLARE_SIM_OBJECT_PARAMS(AlphaSystem)
254
255BEGIN_INIT_SIM_OBJECT_PARAMS(AlphaSystem)
256
257    INIT_PARAM(boot_cpu_frequency, "Frequency of the boot CPU"),
258    INIT_PARAM(memctrl, "memory controller"),
259    INIT_PARAM(physmem, "phsyical memory"),
260    INIT_PARAM(kernel, "file that contains the kernel code"),
261    INIT_PARAM(console, "file that contains the console code"),
262    INIT_PARAM(pal, "file that contains palcode"),
263    INIT_PARAM_DFLT(boot_osflags, "flags to pass to the kernel during boot",
264                    "a"),
265    INIT_PARAM_DFLT(readfile, "file to read startup script from", ""),
266    INIT_PARAM_DFLT(init_param, "numerical value to pass into simulator", 0),
267    INIT_PARAM_DFLT(system_type, "Type of system we are emulating", 34),
268    INIT_PARAM_DFLT(system_rev, "Revision of system we are emulating", 1<<10),
269    INIT_PARAM_DFLT(bin, "is this system to be binned", false),
270    INIT_PARAM(binned_fns, "functions to be broken down and binned"),
271    INIT_PARAM_DFLT(bin_int, "is interrupt code binned seperately?", true)
272
273END_INIT_SIM_OBJECT_PARAMS(AlphaSystem)
274
275CREATE_SIM_OBJECT(AlphaSystem)
276{
277    AlphaSystem::Params *p = new AlphaSystem::Params;
278    p->name = getInstanceName();
279    p->boot_cpu_frequency = boot_cpu_frequency;
280    p->memctrl = memctrl;
281    p->physmem = physmem;
282    p->kernel_path = kernel;
283    p->console_path = console;
284    p->palcode = pal;
285    p->boot_osflags = boot_osflags;
286    p->init_param = init_param;
287    p->readfile = readfile;
288    p->system_type = system_type;
289    p->system_rev = system_rev;
290    p->bin = bin;
291    p->binned_fns = binned_fns;
292    p->bin_int = bin_int;
293    return new AlphaSystem(p);
294}
295
296REGISTER_SIM_OBJECT("AlphaSystem", AlphaSystem)
297
298
299