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