system.cc revision 2901:f9a45473ab55
15450Sgblack@eecs.umich.edu/*
25132Sgblack@eecs.umich.edu * Copyright (c) 2003-2006 The Regents of The University of Michigan
35132Sgblack@eecs.umich.edu * All rights reserved.
47087Snate@binkert.org *
57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67087Snate@binkert.org * modification, are permitted provided that the following conditions are
77087Snate@binkert.org * met: redistributions of source code must retain the above copyright
87087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117087Snate@binkert.org * documentation and/or other materials provided with the distribution;
125132Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137087Snate@binkert.org * contributors may be used to endorse or promote products derived from
147087Snate@binkert.org * this software without specific prior written permission.
157087Snate@binkert.org *
167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215132Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227087Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235132Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245132Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255132Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265132Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275132Sgblack@eecs.umich.edu *
285132Sgblack@eecs.umich.edu * Authors: Steve Reinhardt
295132Sgblack@eecs.umich.edu *          Lisa Hsu
305132Sgblack@eecs.umich.edu *          Nathan Binkert
315132Sgblack@eecs.umich.edu *          Ali Saidi
325132Sgblack@eecs.umich.edu */
335132Sgblack@eecs.umich.edu
345132Sgblack@eecs.umich.edu#include "arch/isa_traits.hh"
355132Sgblack@eecs.umich.edu#include "base/loader/object_file.hh"
365132Sgblack@eecs.umich.edu#include "base/loader/symtab.hh"
375132Sgblack@eecs.umich.edu#include "base/trace.hh"
385132Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
395450Sgblack@eecs.umich.edu#include "mem/mem_object.hh"
405615Sgblack@eecs.umich.edu#include "mem/physical.hh"
415625Sgblack@eecs.umich.edu#include "sim/builder.hh"
425627Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
435132Sgblack@eecs.umich.edu#include "sim/system.hh"
445132Sgblack@eecs.umich.edu#if FULL_SYSTEM
455132Sgblack@eecs.umich.edu#include "arch/vtophys.hh"
465132Sgblack@eecs.umich.edu#include "base/remote_gdb.hh"
475615Sgblack@eecs.umich.edu#include "kern/kernel_stats.hh"
485615Sgblack@eecs.umich.edu#endif
495625Sgblack@eecs.umich.edu
505625Sgblack@eecs.umich.eduusing namespace std;
515625Sgblack@eecs.umich.eduusing namespace TheISA;
525625Sgblack@eecs.umich.edu
535625Sgblack@eecs.umich.eduvector<System *> System::systemList;
545625Sgblack@eecs.umich.edu
555627Sgblack@eecs.umich.eduint System::numSystemsRunning = 0;
565627Sgblack@eecs.umich.edu
575299Sgblack@eecs.umich.eduSystem::System(Params *p)
585299Sgblack@eecs.umich.edu    : SimObject(p->name), physmem(p->physmem), numcpus(0),
595299Sgblack@eecs.umich.edu#if FULL_SYSTEM
605450Sgblack@eecs.umich.edu      init_param(p->init_param),
615450Sgblack@eecs.umich.edu      functionalPort(p->name + "-fport"),
625450Sgblack@eecs.umich.edu      virtPort(p->name + "-vport"),
63#else
64      page_ptr(0),
65#endif
66      _params(p)
67{
68    // add self to global system list
69    systemList.push_back(this);
70
71#if FULL_SYSTEM
72    kernelSymtab = new SymbolTable;
73    debugSymbolTable = new SymbolTable;
74
75
76    /**
77     * Get a functional port to memory
78     */
79    Port *mem_port;
80    mem_port = physmem->getPort("functional");
81    functionalPort.setPeer(mem_port);
82    mem_port->setPeer(&functionalPort);
83
84    mem_port = physmem->getPort("functional");
85    virtPort.setPeer(mem_port);
86    mem_port->setPeer(&virtPort);
87
88
89    /**
90     * Load the kernel code into memory
91     */
92    // Load kernel code
93    kernel = createObjectFile(params()->kernel_path);
94    if (kernel == NULL)
95        fatal("Could not load kernel file %s", params()->kernel_path);
96
97    // Load program sections into memory
98    kernel->loadSections(&functionalPort, LoadAddrMask);
99
100    // setup entry points
101    kernelStart = kernel->textBase();
102    kernelEnd = kernel->bssBase() + kernel->bssSize();
103    kernelEntry = kernel->entryPoint();
104
105    // load symbols
106    if (!kernel->loadGlobalSymbols(kernelSymtab))
107        panic("could not load kernel symbols\n");
108
109    if (!kernel->loadLocalSymbols(kernelSymtab))
110        panic("could not load kernel local symbols\n");
111
112    if (!kernel->loadGlobalSymbols(debugSymbolTable))
113        panic("could not load kernel symbols\n");
114
115    if (!kernel->loadLocalSymbols(debugSymbolTable))
116        panic("could not load kernel local symbols\n");
117
118    DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
119    DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
120    DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
121    DPRINTF(Loader, "Kernel loaded...\n");
122#endif // FULL_SYSTEM
123
124    // increment the number of running systms
125    numSystemsRunning++;
126}
127
128System::~System()
129{
130#if FULL_SYSTEM
131    delete kernelSymtab;
132    delete kernel;
133#else
134    panic("System::fixFuncEventAddr needs to be rewritten "
135          "to work with syscall emulation");
136#endif // FULL_SYSTEM}
137}
138
139#if FULL_SYSTEM
140
141
142int rgdb_wait = -1;
143
144#endif // FULL_SYSTEM
145
146
147void
148System::setMemoryMode(MemoryMode mode)
149{
150    assert(getState() == Drained);
151    memoryMode = mode;
152}
153
154int
155System::registerThreadContext(ThreadContext *tc, int id)
156{
157    if (id == -1) {
158        for (id = 0; id < threadContexts.size(); id++) {
159            if (!threadContexts[id])
160                break;
161        }
162    }
163
164    if (threadContexts.size() <= id)
165        threadContexts.resize(id + 1);
166
167    if (threadContexts[id])
168        panic("Cannot have two CPUs with the same id (%d)\n", id);
169
170    threadContexts[id] = tc;
171    numcpus++;
172
173#if FULL_SYSTEM
174    RemoteGDB *rgdb = new RemoteGDB(this, tc);
175    GDBListener *gdbl = new GDBListener(rgdb, 7000 + id);
176    gdbl->listen();
177    /**
178     * Uncommenting this line waits for a remote debugger to connect
179     * to the simulator before continuing.
180     */
181    if (rgdb_wait != -1 && rgdb_wait == id)
182        gdbl->accept();
183
184    if (remoteGDB.size() <= id) {
185        remoteGDB.resize(id + 1);
186    }
187
188    remoteGDB[id] = rgdb;
189#endif // FULL_SYSTEM
190
191    return id;
192}
193
194void
195System::startup()
196{
197    int i;
198    for (i = 0; i < threadContexts.size(); i++)
199        threadContexts[i]->activate(0);
200}
201
202void
203System::replaceThreadContext(ThreadContext *tc, int id)
204{
205    if (id >= threadContexts.size()) {
206        panic("replaceThreadContext: bad id, %d >= %d\n",
207              id, threadContexts.size());
208    }
209
210    threadContexts[id] = tc;
211#if FULL_SYSTEM
212    remoteGDB[id]->replaceThreadContext(tc);
213#endif // FULL_SYSTEM
214}
215
216#if !FULL_SYSTEM
217Addr
218System::new_page()
219{
220    Addr return_addr = page_ptr << LogVMPageSize;
221    ++page_ptr;
222    return return_addr;
223}
224#endif
225
226void
227System::serialize(ostream &os)
228{
229#if FULL_SYSTEM
230    kernelSymtab->serialize("kernel_symtab", os);
231#endif // FULL_SYSTEM
232}
233
234
235void
236System::unserialize(Checkpoint *cp, const string &section)
237{
238#if FULL_SYSTEM
239    kernelSymtab->unserialize("kernel_symtab", cp, section);
240#endif // FULL_SYSTEM
241}
242
243void
244System::printSystems()
245{
246    vector<System *>::iterator i = systemList.begin();
247    vector<System *>::iterator end = systemList.end();
248    for (; i != end; ++i) {
249        System *sys = *i;
250        cerr << "System " << sys->name() << ": " << hex << sys << endl;
251    }
252}
253
254void
255printSystems()
256{
257    System::printSystems();
258}
259
260#if FULL_SYSTEM
261
262// In full system mode, only derived classes (e.g. AlphaLinuxSystem)
263// can be created directly.
264
265DEFINE_SIM_OBJECT_CLASS_NAME("System", System)
266
267#else
268
269BEGIN_DECLARE_SIM_OBJECT_PARAMS(System)
270
271    SimObjectParam<PhysicalMemory *> physmem;
272
273END_DECLARE_SIM_OBJECT_PARAMS(System)
274
275BEGIN_INIT_SIM_OBJECT_PARAMS(System)
276
277    INIT_PARAM(physmem, "physical memory")
278
279END_INIT_SIM_OBJECT_PARAMS(System)
280
281CREATE_SIM_OBJECT(System)
282{
283    System::Params *p = new System::Params;
284    p->name = getInstanceName();
285    p->physmem = physmem;
286    return new System(p);
287}
288
289REGISTER_SIM_OBJECT("System", System)
290
291#endif
292