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