system.cc revision 5718:323cfbfec1a4
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2003-2006 The Regents of The University of Michigan
312855Sgabeblack@google.com * 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: Steve Reinhardt
29 *          Lisa Hsu
30 *          Nathan Binkert
31 *          Ali Saidi
32 */
33
34#include "arch/isa_traits.hh"
35#include "arch/remote_gdb.hh"
36#include "arch/utility.hh"
37#include "base/loader/object_file.hh"
38#include "base/loader/symtab.hh"
39#include "base/trace.hh"
40#include "cpu/thread_context.hh"
41#include "mem/mem_object.hh"
42#include "mem/physical.hh"
43#include "sim/byteswap.hh"
44#include "sim/system.hh"
45#include "sim/debug.hh"
46#if FULL_SYSTEM
47#include "arch/vtophys.hh"
48#include "kern/kernel_stats.hh"
49#else
50#include "params/System.hh"
51#endif
52
53using namespace std;
54using namespace TheISA;
55
56vector<System *> System::systemList;
57
58int System::numSystemsRunning = 0;
59
60System::System(Params *p)
61    : SimObject(p), physmem(p->physmem), _numContexts(0),
62#if FULL_SYSTEM
63      init_param(p->init_param),
64      functionalPort(p->name + "-fport"),
65      virtPort(p->name + "-vport"),
66#else
67      page_ptr(0),
68      next_PID(0),
69#endif
70      memoryMode(p->mem_mode), _params(p)
71{
72    // add self to global system list
73    systemList.push_back(this);
74
75#if FULL_SYSTEM
76    kernelSymtab = new SymbolTable;
77    if (!debugSymbolTable)
78        debugSymbolTable = new SymbolTable;
79
80
81    /**
82     * Get a functional port to memory
83     */
84    Port *mem_port;
85    mem_port = physmem->getPort("functional");
86    functionalPort.setPeer(mem_port);
87    mem_port->setPeer(&functionalPort);
88
89    mem_port = physmem->getPort("functional");
90    virtPort.setPeer(mem_port);
91    mem_port->setPeer(&virtPort);
92
93
94    /**
95     * Load the kernel code into memory
96     */
97    if (params()->kernel == "") {
98        warn("No kernel set for full system simulation. Assuming you know what"
99                " you're doing...\n");
100    } else {
101        // Load kernel code
102        kernel = createObjectFile(params()->kernel);
103        warn("kernel located at: %s", params()->kernel);
104
105        if (kernel == NULL)
106            fatal("Could not load kernel file %s", params()->kernel);
107
108        // Load program sections into memory
109        kernel->loadSections(&functionalPort, LoadAddrMask);
110
111        // setup entry points
112        kernelStart = kernel->textBase();
113        kernelEnd = kernel->bssBase() + kernel->bssSize();
114        kernelEntry = kernel->entryPoint();
115
116        // load symbols
117        if (!kernel->loadGlobalSymbols(kernelSymtab))
118            panic("could not load kernel symbols\n");
119
120        if (!kernel->loadLocalSymbols(kernelSymtab))
121            panic("could not load kernel local symbols\n");
122
123        if (!kernel->loadGlobalSymbols(debugSymbolTable))
124            panic("could not load kernel symbols\n");
125
126        if (!kernel->loadLocalSymbols(debugSymbolTable))
127            panic("could not load kernel local symbols\n");
128
129        DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
130        DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
131        DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
132        DPRINTF(Loader, "Kernel loaded...\n");
133    }
134#endif // FULL_SYSTEM
135
136    // increment the number of running systms
137    numSystemsRunning++;
138}
139
140System::~System()
141{
142#if FULL_SYSTEM
143    delete kernelSymtab;
144    delete kernel;
145#else
146    panic("System::fixFuncEventAddr needs to be rewritten "
147          "to work with syscall emulation");
148#endif // FULL_SYSTEM}
149}
150
151int rgdb_wait = -1;
152int rgdb_enable = true;
153
154void
155System::setMemoryMode(Enums::MemoryMode mode)
156{
157    assert(getState() == Drained);
158    memoryMode = mode;
159}
160
161bool System::breakpoint()
162{
163    if (remoteGDB.size())
164        return remoteGDB[0]->breakpoint();
165    return false;
166}
167
168int
169System::registerThreadContext(ThreadContext *tc, int assigned)
170{
171    int id;
172    if (assigned == -1) {
173        for (id = 0; id < threadContexts.size(); id++) {
174            if (!threadContexts[id])
175                break;
176        }
177
178        if (threadContexts.size() <= id)
179            threadContexts.resize(id + 1);
180    } else {
181        if (threadContexts.size() <= assigned)
182            threadContexts.resize(assigned + 1);
183        id = assigned;
184    }
185
186    if (threadContexts[id])
187        panic("Cannot have two CPUs with the same id (%d)\n", id);
188
189    threadContexts[id] = tc;
190    _numContexts++;
191
192    int port = getRemoteGDBPort();
193    if (rgdb_enable && port) {
194        RemoteGDB *rgdb = new RemoteGDB(this, tc);
195        GDBListener *gdbl = new GDBListener(rgdb, port + id);
196        gdbl->listen();
197        /**
198         * Uncommenting this line waits for a remote debugger to
199         * connect to the simulator before continuing.
200         */
201        if (rgdb_wait != -1 && rgdb_wait == id)
202            gdbl->accept();
203
204        if (remoteGDB.size() <= id) {
205            remoteGDB.resize(id + 1);
206        }
207
208        remoteGDB[id] = rgdb;
209    }
210
211    return id;
212}
213
214void
215System::startup()
216{
217#if FULL_SYSTEM
218    int i;
219    for (i = 0; i < threadContexts.size(); i++)
220        TheISA::startupCPU(threadContexts[i], i);
221#endif
222}
223
224void
225System::replaceThreadContext(ThreadContext *tc, int context_id)
226{
227    if (context_id >= threadContexts.size()) {
228        panic("replaceThreadContext: bad id, %d >= %d\n",
229              context_id, threadContexts.size());
230    }
231
232    threadContexts[context_id] = tc;
233    if (context_id < remoteGDB.size())
234        remoteGDB[context_id]->replaceThreadContext(tc);
235}
236
237#if !FULL_SYSTEM
238Addr
239System::new_page()
240{
241    Addr return_addr = page_ptr << LogVMPageSize;
242    ++page_ptr;
243    if (return_addr >= physmem->size())
244        fatal("Out of memory, please increase size of physical memory.");
245    return return_addr;
246}
247#endif
248
249void
250System::serialize(ostream &os)
251{
252#if FULL_SYSTEM
253    kernelSymtab->serialize("kernel_symtab", os);
254#else // !FULL_SYSTEM
255    SERIALIZE_SCALAR(page_ptr);
256#endif
257}
258
259
260void
261System::unserialize(Checkpoint *cp, const string &section)
262{
263#if FULL_SYSTEM
264    kernelSymtab->unserialize("kernel_symtab", cp, section);
265#else // !FULL_SYSTEM
266    UNSERIALIZE_SCALAR(page_ptr);
267#endif
268}
269
270void
271System::printSystems()
272{
273    vector<System *>::iterator i = systemList.begin();
274    vector<System *>::iterator end = systemList.end();
275    for (; i != end; ++i) {
276        System *sys = *i;
277        cerr << "System " << sys->name() << ": " << hex << sys << endl;
278    }
279}
280
281void
282printSystems()
283{
284    System::printSystems();
285}
286
287const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
288    "timing"};
289
290#if !FULL_SYSTEM
291
292System *
293SystemParams::create()
294{
295    return new System(this);
296}
297
298#endif
299