system.cc revision 5254:c555f8b07345
1/*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
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 * Authors: Ali Saidi
29 *          Nathan Binkert
30 *          Jaidev Patwardhan
31 */
32
33#include "arch/mips/system.hh"
34#include "arch/vtophys.hh"
35#include "base/remote_gdb.hh"
36#include "base/loader/object_file.hh"
37#include "base/loader/hex_file.hh"
38#include "base/loader/symtab.hh"
39#include "base/trace.hh"
40#include "mem/physical.hh"
41#include "params/MipsSystem.hh"
42#include "sim/byteswap.hh"
43
44
45using namespace LittleEndianGuest;
46
47MipsSystem::MipsSystem(Params *p)
48    : System(p)
49{
50
51#if FULL_SYSTEM
52    if (p->bare_iron == true) {
53        hexFile = new HexFile(params()->hex_file_name);
54        if(!hexFile->loadSections(&functionalPort,MipsISA::LoadAddrMask))
55            panic("Could not load hex file\n");
56    }
57
58    Addr addr = 0;
59    /* Comment out old Alpha Based Code
60
61     Don't need the console before we start looking at booting linux */
62
63
64    consoleSymtab = new SymbolTable;
65
66
67    /**
68     * Load the console code into memory
69     */
70    //    Load Console Code
71    console = createObjectFile(params()->console);
72
73    warn("console code is located at: %s\n", params()->console);
74
75    if (console == NULL)
76        fatal("Could not load console file %s", params()->console);
77    //Load program sections into memory
78     console->loadSections(&functionalPort, MipsISA::LoadAddrMask);
79
80    //load symbols
81    if (!console->loadGlobalSymbols(consoleSymtab))
82        panic("could not load console symbols\n");
83
84    if (!console->loadGlobalSymbols(debugSymbolTable))
85        panic("could not load console symbols\n");
86
87
88#ifndef NDEBUG
89    consolePanicEvent = addConsoleFuncEvent<BreakPCEvent>("panic");
90#endif
91
92    /**
93     * Copy the osflags (kernel arguments) into the consoles
94     * memory. (Presently Linux does not use the console service
95     * routine to get these command line arguments, but Tru64 and
96     * others do.)
97     */
98    if (consoleSymtab->findAddress("env_booted_osflags", addr)) {
99        warn("writing addr starting from %#x", addr);
100        cout << "-" << endl;
101        virtPort.writeBlob(addr, (uint8_t*)params()->boot_osflags.c_str(),
102                strlen(params()->boot_osflags.c_str()));
103    }
104
105    /**
106     * Set the hardware reset parameter block system type and revision
107     * information to Tsunami.
108     */
109    if (consoleSymtab->findAddress("m5_rpb", addr)) {
110        uint64_t data;
111        data = htog(params()->system_type);
112        virtPort.write(addr+0x50, data);
113        data = htog(params()->system_rev);
114        virtPort.write(addr+0x58, data);
115    } else
116        panic("could not find hwrpb\n");
117#endif
118}
119
120MipsSystem::~MipsSystem()
121{
122}
123#if FULL_SYSTEM
124/**
125 * This function fixes up addresses that are used to match PCs for
126 * hooking simulator events on to target function executions.
127 *
128 * Mips binaries may have multiple global offset table (GOT)
129 * sections.  A function that uses the GOT starts with a
130 * two-instruction prolog which sets the global pointer (gp == r29) to
131 * the appropriate GOT section.  The proper gp value is calculated
132 * based on the function address, which must be passed by the caller
133 * in the procedure value register (pv aka t12 == r27).  This sequence
134 * looks like the following:
135 *
136 *			opcode Ra Rb offset
137 *	ldah gp,X(pv)     09   29 27   X
138 *	lda  gp,Y(gp)     08   29 29   Y
139 *
140 * for some constant offsets X and Y.  The catch is that the linker
141 * (or maybe even the compiler, I'm not sure) may recognize that the
142 * caller and callee are using the same GOT section, making this
143 * prolog redundant, and modify the call target to skip these
144 * instructions.  If we check for execution of the first instruction
145 * of a function (the one the symbol points to) to detect when to skip
146 * it, we'll miss all these modified calls.  It might work to
147 * unconditionally check for the third instruction, but not all
148 * functions have this prolog, and there's some chance that those
149 * first two instructions could have undesired consequences.  So we do
150 * the Right Thing and pattern-match the first two instructions of the
151 * function to decide where to patch.
152 *
153 * Eventually this code should be moved into an ISA-specific file.
154 */
155
156Addr
157MipsSystem::fixFuncEventAddr(Addr addr)
158{
159  /*
160    // mask for just the opcode, Ra, and Rb fields (not the offset)
161    const uint32_t inst_mask = 0xffff0000;
162    // ldah gp,X(pv): opcode 9, Ra = 29, Rb = 27
163    const uint32_t gp_ldah_pattern = (9 << 26) | (29 << 21) | (27 << 16);
164    // lda  gp,Y(gp): opcode 8, Ra = 29, rb = 29
165    const uint32_t gp_lda_pattern  = (8 << 26) | (29 << 21) | (29 << 16);
166
167    uint32_t i1 = virtPort.read<uint32_t>(addr);
168    uint32_t i2 = virtPort.read<uint32_t>(addr + sizeof(MipsISA::MachInst));
169
170    if ((i1 & inst_mask) == gp_ldah_pattern &&
171        (i2 & inst_mask) == gp_lda_pattern) {
172        Addr new_addr = addr + 2* sizeof(MipsISA::MachInst);
173        DPRINTF(Loader, "fixFuncEventAddr: %p -> %p", addr, new_addr);
174        return new_addr;
175    } else {
176        return addr;
177        }*/
178  return addr;
179}
180
181
182void
183MipsSystem::setMipsAccess(Addr access)
184{
185    Addr addr = 0;
186    if (consoleSymtab->findAddress("m5MipsAccess", addr)) {
187      //        virtPort.write(addr, htog(EV5::Phys2K0Seg(access)));
188    } else
189    panic("could not find m5MipsAccess\n");
190    }
191
192#endif
193
194bool
195MipsSystem::breakpoint()
196{
197  return 0;
198  //    return remoteGDB[0]->trap(MIPS_KENTRY_INT);
199}
200
201void
202MipsSystem::serialize(std::ostream &os)
203{
204    System::serialize(os);
205    //    consoleSymtab->serialize("console_symtab", os);
206}
207
208
209void
210MipsSystem::unserialize(Checkpoint *cp, const std::string &section)
211{
212    System::unserialize(cp,section);
213    //    consoleSymtab->unserialize("console_symtab", cp, section);
214}
215
216MipsSystem *
217MipsSystemParams::create()
218{
219    return new MipsSystem(this);
220}
221
222