system.cc revision 8229:78bf55f23338
1/*
2 * Copyright (c) 2003-2006 The Regents of The University of Michigan
3 * Copyright (c) 2011 Regents of the University of California
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Steve Reinhardt
30 *          Lisa Hsu
31 *          Nathan Binkert
32 *          Ali Saidi
33 *          Rick Strong
34 */
35
36#include "arch/isa_traits.hh"
37#include "arch/remote_gdb.hh"
38#include "arch/utility.hh"
39#include "base/loader/object_file.hh"
40#include "base/loader/symtab.hh"
41#include "base/trace.hh"
42#include "config/full_system.hh"
43#include "config/the_isa.hh"
44#include "cpu/thread_context.hh"
45#include "mem/mem_object.hh"
46#include "mem/physical.hh"
47#include "sim/byteswap.hh"
48#include "sim/debug.hh"
49#include "sim/system.hh"
50
51#if FULL_SYSTEM
52#include "arch/vtophys.hh"
53#include "kern/kernel_stats.hh"
54#include "mem/vport.hh"
55#else
56#include "params/System.hh"
57#endif
58
59using namespace std;
60using namespace TheISA;
61
62vector<System *> System::systemList;
63
64int System::numSystemsRunning = 0;
65
66System::System(Params *p)
67    : SimObject(p), physmem(p->physmem), _numContexts(0),
68#if FULL_SYSTEM
69      init_param(p->init_param),
70      loadAddrMask(p->load_addr_mask),
71#else
72      pagePtr(0),
73      nextPID(0),
74#endif
75      memoryMode(p->mem_mode),
76      workItemsBegin(0),
77      workItemsEnd(0),
78      _params(p),
79      totalNumInsts(0),
80      instEventQueue("system instruction-based event queue")
81{
82    // add self to global system list
83    systemList.push_back(this);
84
85#if FULL_SYSTEM
86    kernelSymtab = new SymbolTable;
87    if (!debugSymbolTable)
88        debugSymbolTable = new SymbolTable;
89
90
91    /**
92     * Get a functional port to memory
93     */
94    Port *mem_port;
95    functionalPort = new FunctionalPort(name() + "-fport");
96    mem_port = physmem->getPort("functional");
97    functionalPort->setPeer(mem_port);
98    mem_port->setPeer(functionalPort);
99
100    virtPort = new VirtualPort(name() + "-fport");
101    mem_port = physmem->getPort("functional");
102    virtPort->setPeer(mem_port);
103    mem_port->setPeer(virtPort);
104
105
106    /**
107     * Load the kernel code into memory
108     */
109    if (params()->kernel == "") {
110        inform("No kernel set for full system simulation. Assuming you know what"
111                " you're doing...\n");
112    } else {
113        // Load kernel code
114        kernel = createObjectFile(params()->kernel);
115        inform("kernel located at: %s", params()->kernel);
116
117        if (kernel == NULL)
118            fatal("Could not load kernel file %s", params()->kernel);
119
120        // Load program sections into memory
121        kernel->loadSections(functionalPort, loadAddrMask);
122
123        // setup entry points
124        kernelStart = kernel->textBase();
125        kernelEnd = kernel->bssBase() + kernel->bssSize();
126        kernelEntry = kernel->entryPoint();
127
128        // load symbols
129        if (!kernel->loadGlobalSymbols(kernelSymtab))
130            fatal("could not load kernel symbols\n");
131
132        if (!kernel->loadLocalSymbols(kernelSymtab))
133            fatal("could not load kernel local symbols\n");
134
135        if (!kernel->loadGlobalSymbols(debugSymbolTable))
136            fatal("could not load kernel symbols\n");
137
138        if (!kernel->loadLocalSymbols(debugSymbolTable))
139            fatal("could not load kernel local symbols\n");
140
141        DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
142        DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
143        DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
144        DPRINTF(Loader, "Kernel loaded...\n");
145    }
146#endif // FULL_SYSTEM
147
148    // increment the number of running systms
149    numSystemsRunning++;
150
151    activeCpus.clear();
152}
153
154System::~System()
155{
156#if FULL_SYSTEM
157    delete kernelSymtab;
158    delete kernel;
159#else
160    panic("System::fixFuncEventAddr needs to be rewritten "
161          "to work with syscall emulation");
162#endif // FULL_SYSTEM}
163}
164
165void
166System::setMemoryMode(Enums::MemoryMode mode)
167{
168    assert(getState() == Drained);
169    memoryMode = mode;
170}
171
172bool System::breakpoint()
173{
174    if (remoteGDB.size())
175        return remoteGDB[0]->breakpoint();
176    return false;
177}
178
179/**
180 * Setting rgdb_wait to a positive integer waits for a remote debugger to
181 * connect to that context ID before continuing.  This should really
182   be a parameter on the CPU object or something...
183 */
184int rgdb_wait = -1;
185
186int
187System::registerThreadContext(ThreadContext *tc, int assigned)
188{
189    int id;
190    if (assigned == -1) {
191        for (id = 0; id < threadContexts.size(); id++) {
192            if (!threadContexts[id])
193                break;
194        }
195
196        if (threadContexts.size() <= id)
197            threadContexts.resize(id + 1);
198    } else {
199        if (threadContexts.size() <= assigned)
200            threadContexts.resize(assigned + 1);
201        id = assigned;
202    }
203
204    if (threadContexts[id])
205        fatal("Cannot have two CPUs with the same id (%d)\n", id);
206
207    threadContexts[id] = tc;
208    _numContexts++;
209
210    int port = getRemoteGDBPort();
211    if (port) {
212        RemoteGDB *rgdb = new RemoteGDB(this, tc);
213        GDBListener *gdbl = new GDBListener(rgdb, port + id);
214        gdbl->listen();
215
216        if (rgdb_wait != -1 && rgdb_wait == id)
217            gdbl->accept();
218
219        if (remoteGDB.size() <= id) {
220            remoteGDB.resize(id + 1);
221        }
222
223        remoteGDB[id] = rgdb;
224    }
225
226    activeCpus.push_back(false);
227
228    return id;
229}
230
231int
232System::numRunningContexts()
233{
234    int running = 0;
235    for (int i = 0; i < _numContexts; ++i) {
236        if (threadContexts[i]->status() != ThreadContext::Halted)
237            ++running;
238    }
239    return running;
240}
241
242void
243System::initState()
244{
245#if FULL_SYSTEM
246    int i;
247    for (i = 0; i < threadContexts.size(); i++)
248        TheISA::startupCPU(threadContexts[i], i);
249#endif
250}
251
252void
253System::replaceThreadContext(ThreadContext *tc, int context_id)
254{
255    if (context_id >= threadContexts.size()) {
256        panic("replaceThreadContext: bad id, %d >= %d\n",
257              context_id, threadContexts.size());
258    }
259
260    threadContexts[context_id] = tc;
261    if (context_id < remoteGDB.size())
262        remoteGDB[context_id]->replaceThreadContext(tc);
263}
264
265#if !FULL_SYSTEM
266Addr
267System::new_page()
268{
269    Addr return_addr = pagePtr << LogVMPageSize;
270    ++pagePtr;
271    if (return_addr >= physmem->size())
272        fatal("Out of memory, please increase size of physical memory.");
273    return return_addr;
274}
275
276Addr
277System::memSize()
278{
279    return physmem->size();
280}
281
282Addr
283System::freeMemSize()
284{
285   return physmem->size() - (pagePtr << LogVMPageSize);
286}
287
288#endif
289
290void
291System::resume()
292{
293    SimObject::resume();
294    totalNumInsts = 0;
295}
296
297void
298System::serialize(ostream &os)
299{
300#if FULL_SYSTEM
301    kernelSymtab->serialize("kernel_symtab", os);
302#else // !FULL_SYSTEM
303    SERIALIZE_SCALAR(pagePtr);
304    SERIALIZE_SCALAR(nextPID);
305#endif
306}
307
308
309void
310System::unserialize(Checkpoint *cp, const string &section)
311{
312#if FULL_SYSTEM
313    kernelSymtab->unserialize("kernel_symtab", cp, section);
314#else // !FULL_SYSTEM
315    UNSERIALIZE_SCALAR(pagePtr);
316    UNSERIALIZE_SCALAR(nextPID);
317#endif
318}
319
320void
321System::printSystems()
322{
323    vector<System *>::iterator i = systemList.begin();
324    vector<System *>::iterator end = systemList.end();
325    for (; i != end; ++i) {
326        System *sys = *i;
327        cerr << "System " << sys->name() << ": " << hex << sys << endl;
328    }
329}
330
331void
332printSystems()
333{
334    System::printSystems();
335}
336
337const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
338    "timing"};
339
340#if !FULL_SYSTEM
341
342System *
343SystemParams::create()
344{
345    return new System(this);
346}
347
348#endif
349