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