system.cc revision 8286
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 "debug/Loader.hh"
46#include "mem/mem_object.hh"
47#include "mem/physical.hh"
48#include "sim/byteswap.hh"
49#include "sim/debug.hh"
50#include "sim/system.hh"
51
52#if FULL_SYSTEM
53#include "arch/vtophys.hh"
54#include "kern/kernel_stats.hh"
55#include "mem/vport.hh"
56#else
57#include "params/System.hh"
58#endif
59
60using namespace std;
61using namespace TheISA;
62
63vector<System *> System::systemList;
64
65int System::numSystemsRunning = 0;
66
67System::System(Params *p)
68    : SimObject(p), physmem(p->physmem), _numContexts(0),
69#if FULL_SYSTEM
70      init_param(p->init_param),
71      loadAddrMask(p->load_addr_mask),
72#else
73      pagePtr(0),
74      nextPID(0),
75#endif
76      memoryMode(p->mem_mode),
77      workItemsBegin(0),
78      workItemsEnd(0),
79      _params(p),
80      totalNumInsts(0),
81      instEventQueue("system instruction-based event queue")
82{
83    // add self to global system list
84    systemList.push_back(this);
85
86#if FULL_SYSTEM
87    kernelSymtab = new SymbolTable;
88    if (!debugSymbolTable)
89        debugSymbolTable = new SymbolTable;
90
91
92    /**
93     * Get a functional port to memory
94     */
95    Port *mem_port;
96    functionalPort = new FunctionalPort(name() + "-fport");
97    mem_port = physmem->getPort("functional");
98    functionalPort->setPeer(mem_port);
99    mem_port->setPeer(functionalPort);
100
101    virtPort = new VirtualPort(name() + "-fport");
102    mem_port = physmem->getPort("functional");
103    virtPort->setPeer(mem_port);
104    mem_port->setPeer(virtPort);
105
106
107    /**
108     * Load the kernel code into memory
109     */
110    if (params()->kernel == "") {
111        inform("No kernel set for full system simulation. Assuming you know what"
112                " you're doing...\n");
113    } else {
114        // Load kernel code
115        kernel = createObjectFile(params()->kernel);
116        inform("kernel located at: %s", params()->kernel);
117
118        if (kernel == NULL)
119            fatal("Could not load kernel file %s", params()->kernel);
120
121        // Load program sections into memory
122        kernel->loadSections(functionalPort, loadAddrMask);
123
124        // setup entry points
125        kernelStart = kernel->textBase();
126        kernelEnd = kernel->bssBase() + kernel->bssSize();
127        kernelEntry = kernel->entryPoint();
128
129        // load symbols
130        if (!kernel->loadGlobalSymbols(kernelSymtab))
131            fatal("could not load kernel symbols\n");
132
133        if (!kernel->loadLocalSymbols(kernelSymtab))
134            fatal("could not load kernel local symbols\n");
135
136        if (!kernel->loadGlobalSymbols(debugSymbolTable))
137            fatal("could not load kernel symbols\n");
138
139        if (!kernel->loadLocalSymbols(debugSymbolTable))
140            fatal("could not load kernel local symbols\n");
141
142        DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
143        DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
144        DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
145        DPRINTF(Loader, "Kernel loaded...\n");
146    }
147#endif // FULL_SYSTEM
148
149    // increment the number of running systms
150    numSystemsRunning++;
151
152    activeCpus.clear();
153}
154
155System::~System()
156{
157#if FULL_SYSTEM
158    delete kernelSymtab;
159    delete kernel;
160#else
161    panic("System::fixFuncEventAddr needs to be rewritten "
162          "to work with syscall emulation");
163#endif // FULL_SYSTEM}
164}
165
166void
167System::setMemoryMode(Enums::MemoryMode mode)
168{
169    assert(getState() == Drained);
170    memoryMode = mode;
171}
172
173bool System::breakpoint()
174{
175    if (remoteGDB.size())
176        return remoteGDB[0]->breakpoint();
177    return false;
178}
179
180/**
181 * Setting rgdb_wait to a positive integer waits for a remote debugger to
182 * connect to that context ID before continuing.  This should really
183   be a parameter on the CPU object or something...
184 */
185int rgdb_wait = -1;
186
187int
188System::registerThreadContext(ThreadContext *tc, int assigned)
189{
190    int id;
191    if (assigned == -1) {
192        for (id = 0; id < threadContexts.size(); id++) {
193            if (!threadContexts[id])
194                break;
195        }
196
197        if (threadContexts.size() <= id)
198            threadContexts.resize(id + 1);
199    } else {
200        if (threadContexts.size() <= assigned)
201            threadContexts.resize(assigned + 1);
202        id = assigned;
203    }
204
205    if (threadContexts[id])
206        fatal("Cannot have two CPUs with the same id (%d)\n", id);
207
208    threadContexts[id] = tc;
209    _numContexts++;
210
211    int port = getRemoteGDBPort();
212    if (port) {
213        RemoteGDB *rgdb = new RemoteGDB(this, tc);
214        GDBListener *gdbl = new GDBListener(rgdb, port + id);
215        gdbl->listen();
216
217        if (rgdb_wait != -1 && rgdb_wait == id)
218            gdbl->accept();
219
220        if (remoteGDB.size() <= id) {
221            remoteGDB.resize(id + 1);
222        }
223
224        remoteGDB[id] = rgdb;
225    }
226
227    activeCpus.push_back(false);
228
229    return id;
230}
231
232int
233System::numRunningContexts()
234{
235    int running = 0;
236    for (int i = 0; i < _numContexts; ++i) {
237        if (threadContexts[i]->status() != ThreadContext::Halted)
238            ++running;
239    }
240    return running;
241}
242
243void
244System::initState()
245{
246#if FULL_SYSTEM
247    int i;
248    for (i = 0; i < threadContexts.size(); i++)
249        TheISA::startupCPU(threadContexts[i], i);
250#endif
251}
252
253void
254System::replaceThreadContext(ThreadContext *tc, int context_id)
255{
256    if (context_id >= threadContexts.size()) {
257        panic("replaceThreadContext: bad id, %d >= %d\n",
258              context_id, threadContexts.size());
259    }
260
261    threadContexts[context_id] = tc;
262    if (context_id < remoteGDB.size())
263        remoteGDB[context_id]->replaceThreadContext(tc);
264}
265
266#if !FULL_SYSTEM
267Addr
268System::new_page()
269{
270    Addr return_addr = pagePtr << LogVMPageSize;
271    ++pagePtr;
272    if (return_addr >= physmem->size())
273        fatal("Out of memory, please increase size of physical memory.");
274    return return_addr;
275}
276
277Addr
278System::memSize()
279{
280    return physmem->size();
281}
282
283Addr
284System::freeMemSize()
285{
286   return physmem->size() - (pagePtr << LogVMPageSize);
287}
288
289#endif
290
291void
292System::resume()
293{
294    SimObject::resume();
295    totalNumInsts = 0;
296}
297
298void
299System::serialize(ostream &os)
300{
301#if FULL_SYSTEM
302    kernelSymtab->serialize("kernel_symtab", os);
303#else // !FULL_SYSTEM
304    SERIALIZE_SCALAR(pagePtr);
305    SERIALIZE_SCALAR(nextPID);
306#endif
307}
308
309
310void
311System::unserialize(Checkpoint *cp, const string &section)
312{
313#if FULL_SYSTEM
314    kernelSymtab->unserialize("kernel_symtab", cp, section);
315#else // !FULL_SYSTEM
316    UNSERIALIZE_SCALAR(pagePtr);
317    UNSERIALIZE_SCALAR(nextPID);
318#endif
319}
320
321void
322System::printSystems()
323{
324    vector<System *>::iterator i = systemList.begin();
325    vector<System *>::iterator end = systemList.end();
326    for (; i != end; ++i) {
327        System *sys = *i;
328        cerr << "System " << sys->name() << ": " << hex << sys << endl;
329    }
330}
331
332void
333printSystems()
334{
335    System::printSystems();
336}
337
338const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
339    "timing"};
340
341#if !FULL_SYSTEM
342
343System *
344SystemParams::create()
345{
346    return new System(this);
347}
348
349#endif
350