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