system.cc revision 8706
16757SAli.Saidi@ARM.com/*
27650SAli.Saidi@ARM.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
36757SAli.Saidi@ARM.com * All rights reserved.
46757SAli.Saidi@ARM.com *
57111Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67111Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77111Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87111Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97111Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107111Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117111Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127111Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137111Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
146757SAli.Saidi@ARM.com * this software without specific prior written permission.
156757SAli.Saidi@ARM.com *
166757SAli.Saidi@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176757SAli.Saidi@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186757SAli.Saidi@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196757SAli.Saidi@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206757SAli.Saidi@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216757SAli.Saidi@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226757SAli.Saidi@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236757SAli.Saidi@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246757SAli.Saidi@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256757SAli.Saidi@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266757SAli.Saidi@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276757SAli.Saidi@ARM.com *
286757SAli.Saidi@ARM.com * Authors: Ali Saidi
296757SAli.Saidi@ARM.com *          Nathan Binkert
306757SAli.Saidi@ARM.com */
316757SAli.Saidi@ARM.com
326757SAli.Saidi@ARM.com#include <sys/signal.h>
336757SAli.Saidi@ARM.com
346757SAli.Saidi@ARM.com#include "arch/alpha/ev5.hh"
356757SAli.Saidi@ARM.com#include "arch/alpha/system.hh"
366757SAli.Saidi@ARM.com#include "arch/vtophys.hh"
376757SAli.Saidi@ARM.com#include "base/loader/object_file.hh"
386757SAli.Saidi@ARM.com#include "base/loader/symtab.hh"
396735Sgblack@eecs.umich.edu#include "base/trace.hh"
406757SAli.Saidi@ARM.com#include "debug/Loader.hh"
416757SAli.Saidi@ARM.com#include "mem/fs_translating_port_proxy.hh"
427707Sgblack@eecs.umich.edu#include "params/AlphaSystem.hh"
436757SAli.Saidi@ARM.com#include "sim/byteswap.hh"
446757SAli.Saidi@ARM.com
456735Sgblack@eecs.umich.eduusing namespace AlphaISA;
467693SAli.Saidi@ARM.com
477693SAli.Saidi@ARM.comAlphaSystem::AlphaSystem(Params *p)
487693SAli.Saidi@ARM.com    : System(p)
497693SAli.Saidi@ARM.com{
506735Sgblack@eecs.umich.edu    consoleSymtab = new SymbolTable;
517749SAli.Saidi@ARM.com    palSymtab = new SymbolTable;
527749SAli.Saidi@ARM.com
536735Sgblack@eecs.umich.edu
546735Sgblack@eecs.umich.edu    /**
556735Sgblack@eecs.umich.edu     * Load the pal, and console code into memory
566735Sgblack@eecs.umich.edu     */
576735Sgblack@eecs.umich.edu    // Load Console Code
586735Sgblack@eecs.umich.edu    console = createObjectFile(params()->console);
596735Sgblack@eecs.umich.edu    if (console == NULL)
606735Sgblack@eecs.umich.edu        fatal("Could not load console file %s", params()->console);
616735Sgblack@eecs.umich.edu
626757SAli.Saidi@ARM.com    // Load pal file
638286SAli.Saidi@ARM.com    pal = createObjectFile(params()->pal);
646735Sgblack@eecs.umich.edu    if (pal == NULL)
656735Sgblack@eecs.umich.edu        fatal("Could not load PALcode file %s", params()->pal);
667707Sgblack@eecs.umich.edu}
677707Sgblack@eecs.umich.edu
687707Sgblack@eecs.umich.eduAlphaSystem::~AlphaSystem()
696757SAli.Saidi@ARM.com{
707707Sgblack@eecs.umich.edu    delete consoleSymtab;
717707Sgblack@eecs.umich.edu    delete console;
727693SAli.Saidi@ARM.com    delete pal;
737693SAli.Saidi@ARM.com#ifdef DEBUG
747693SAli.Saidi@ARM.com    delete consolePanicEvent;
757650SAli.Saidi@ARM.com#endif
767693SAli.Saidi@ARM.com}
777693SAli.Saidi@ARM.com
787693SAli.Saidi@ARM.comvoid
797693SAli.Saidi@ARM.comAlphaSystem::initState()
807693SAli.Saidi@ARM.com{
817693SAli.Saidi@ARM.com    // Moved from the constructor to here since it relies on the
827693SAli.Saidi@ARM.com    // address map being resolved in the interconnect
837693SAli.Saidi@ARM.com
847693SAli.Saidi@ARM.com    // Call the initialisation of the super class
857693SAli.Saidi@ARM.com    System::initState();
867693SAli.Saidi@ARM.com
877693SAli.Saidi@ARM.com    // Load program sections into memory
887693SAli.Saidi@ARM.com    pal->loadSections(physProxy, loadAddrMask);
897693SAli.Saidi@ARM.com    console->loadSections(physProxy, loadAddrMask);
907693SAli.Saidi@ARM.com
917693SAli.Saidi@ARM.com    // load symbols
927693SAli.Saidi@ARM.com    if (!console->loadGlobalSymbols(consoleSymtab))
937693SAli.Saidi@ARM.com        panic("could not load console symbols\n");
947693SAli.Saidi@ARM.com
957693SAli.Saidi@ARM.com    if (!pal->loadGlobalSymbols(palSymtab))
967693SAli.Saidi@ARM.com        panic("could not load pal symbols\n");
977693SAli.Saidi@ARM.com
987693SAli.Saidi@ARM.com    if (!pal->loadLocalSymbols(palSymtab))
997693SAli.Saidi@ARM.com        panic("could not load pal symbols\n");
1007693SAli.Saidi@ARM.com
1017693SAli.Saidi@ARM.com    if (!console->loadGlobalSymbols(debugSymbolTable))
1027693SAli.Saidi@ARM.com        panic("could not load console symbols\n");
1037693SAli.Saidi@ARM.com
1047693SAli.Saidi@ARM.com    if (!pal->loadGlobalSymbols(debugSymbolTable))
1057693SAli.Saidi@ARM.com        panic("could not load pal symbols\n");
1067693SAli.Saidi@ARM.com
1077650SAli.Saidi@ARM.com    if (!pal->loadLocalSymbols(debugSymbolTable))
1086757SAli.Saidi@ARM.com        panic("could not load pal symbols\n");
1096757SAli.Saidi@ARM.com
1106757SAli.Saidi@ARM.com     Addr addr = 0;
1116757SAli.Saidi@ARM.com#ifndef NDEBUG
1126757SAli.Saidi@ARM.com    consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic");
1136757SAli.Saidi@ARM.com#endif
1147693SAli.Saidi@ARM.com
1157693SAli.Saidi@ARM.com    /**
1167693SAli.Saidi@ARM.com     * Copy the osflags (kernel arguments) into the consoles
1177720Sgblack@eecs.umich.edu     * memory. (Presently Linux does not use the console service
1187720Sgblack@eecs.umich.edu     * routine to get these command line arguments, but Tru64 and
1197720Sgblack@eecs.umich.edu     * others do.)
1207693SAli.Saidi@ARM.com     */
1217693SAli.Saidi@ARM.com    if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
1227748SAli.Saidi@ARM.com        virtProxy->writeBlob(addr, (uint8_t*)params()->boot_osflags.c_str(),
1237748SAli.Saidi@ARM.com                             strlen(params()->boot_osflags.c_str()));
1247748SAli.Saidi@ARM.com    }
1257748SAli.Saidi@ARM.com
1268208SAli.Saidi@ARM.com    /**
1278208SAli.Saidi@ARM.com     * Set the hardware reset parameter block system type and revision
1288208SAli.Saidi@ARM.com     * information to Tsunami.
1298208SAli.Saidi@ARM.com     */
1308208SAli.Saidi@ARM.com    if (consoleSymtab->findAddress("m5_rpb", addr)) {
1318208SAli.Saidi@ARM.com        uint64_t data;
1328208SAli.Saidi@ARM.com        data = htog(params()->system_type);
1338208SAli.Saidi@ARM.com        virtProxy->write(addr+0x50, data);
1347748SAli.Saidi@ARM.com        data = htog(params()->system_rev);
1357748SAli.Saidi@ARM.com        virtProxy->write(addr+0x58, data);
1368208SAli.Saidi@ARM.com    } else
1378208SAli.Saidi@ARM.com        panic("could not find hwrpb\n");
1388208SAli.Saidi@ARM.com}
1398208SAli.Saidi@ARM.com
1408208SAli.Saidi@ARM.com/**
1417748SAli.Saidi@ARM.com * This function fixes up addresses that are used to match PCs for
1427748SAli.Saidi@ARM.com * hooking simulator events on to target function executions.
1437748SAli.Saidi@ARM.com *
1447748SAli.Saidi@ARM.com * Alpha binaries may have multiple global offset table (GOT)
1456759SAli.Saidi@ARM.com * sections.  A function that uses the GOT starts with a
1467748SAli.Saidi@ARM.com * two-instruction prolog which sets the global pointer (gp == r29) to
1477748SAli.Saidi@ARM.com * the appropriate GOT section.  The proper gp value is calculated
1487748SAli.Saidi@ARM.com * based on the function address, which must be passed by the caller
1497748SAli.Saidi@ARM.com * in the procedure value register (pv aka t12 == r27).  This sequence
1507749SAli.Saidi@ARM.com * looks like the following:
1517748SAli.Saidi@ARM.com *
1527749SAli.Saidi@ARM.com *                      opcode Ra Rb offset
1537749SAli.Saidi@ARM.com *      ldah gp,X(pv)     09   29 27   X
1547749SAli.Saidi@ARM.com *      lda  gp,Y(gp)     08   29 29   Y
1557749SAli.Saidi@ARM.com *
1566759SAli.Saidi@ARM.com * for some constant offsets X and Y.  The catch is that the linker
1577752SWilliam.Wang@arm.com * (or maybe even the compiler, I'm not sure) may recognize that the
1587752SWilliam.Wang@arm.com * caller and callee are using the same GOT section, making this
1597752SWilliam.Wang@arm.com * prolog redundant, and modify the call target to skip these
1607752SWilliam.Wang@arm.com * instructions.  If we check for execution of the first instruction
1617752SWilliam.Wang@arm.com * of a function (the one the symbol points to) to detect when to skip
1627748SAli.Saidi@ARM.com * it, we'll miss all these modified calls.  It might work to
1637752SWilliam.Wang@arm.com * unconditionally check for the third instruction, but not all
1647752SWilliam.Wang@arm.com * functions have this prolog, and there's some chance that those
1657752SWilliam.Wang@arm.com * first two instructions could have undesired consequences.  So we do
1667752SWilliam.Wang@arm.com * the Right Thing and pattern-match the first two instructions of the
1677752SWilliam.Wang@arm.com * function to decide where to patch.
1687752SWilliam.Wang@arm.com *
1697752SWilliam.Wang@arm.com * Eventually this code should be moved into an ISA-specific file.
1707752SWilliam.Wang@arm.com */
171Addr
172AlphaSystem::fixFuncEventAddr(Addr addr)
173{
174    // mask for just the opcode, Ra, and Rb fields (not the offset)
175    const uint32_t inst_mask = 0xffff0000;
176    // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27
177    const uint32_t gp_ldah_pattern = (9 << 26) | (29 << 21) | (27 << 16);
178    // lda  gp,Y(gp): opcode 8, Ra = 29, rb = 29
179    const uint32_t gp_lda_pattern  = (8 << 26) | (29 << 21) | (29 << 16);
180
181    uint32_t i1 = virtProxy->read<uint32_t>(addr);
182    uint32_t i2 = virtProxy->read<uint32_t>(addr + sizeof(MachInst));
183
184    if ((i1 & inst_mask) == gp_ldah_pattern &&
185        (i2 & inst_mask) == gp_lda_pattern) {
186        Addr new_addr = addr + 2 * sizeof(MachInst);
187        DPRINTF(Loader, "fixFuncEventAddr: %p -> %p", addr, new_addr);
188        return new_addr;
189    } else {
190        return addr;
191    }
192}
193
194void
195AlphaSystem::setAlphaAccess(Addr access)
196{
197    Addr addr = 0;
198    if (consoleSymtab->findAddress("m5AlphaAccess", addr)) {
199        virtProxy->write(addr, htog(Phys2K0Seg(access)));
200    } else {
201        panic("could not find m5AlphaAccess\n");
202    }
203}
204
205void
206AlphaSystem::serialize(std::ostream &os)
207{
208    System::serialize(os);
209    consoleSymtab->serialize("console_symtab", os);
210    palSymtab->serialize("pal_symtab", os);
211}
212
213void
214AlphaSystem::unserialize(Checkpoint *cp, const std::string &section)
215{
216    System::unserialize(cp,section);
217    consoleSymtab->unserialize("console_symtab", cp, section);
218    palSymtab->unserialize("pal_symtab", cp, section);
219}
220
221AlphaSystem *
222AlphaSystemParams::create()
223{
224    return new AlphaSystem(this);
225}
226