system.cc revision 2720:695250d6fa42
112244Sgabeblack@google.com/*
212244Sgabeblack@google.com * Copyright (c) 2003-2006 The Regents of The University of Michigan
312244Sgabeblack@google.com * All rights reserved.
412244Sgabeblack@google.com *
512244Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612244Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712244Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812244Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912244Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012244Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112244Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212244Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312244Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412244Sgabeblack@google.com * this software without specific prior written permission.
1512244Sgabeblack@google.com *
1612244Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712244Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812244Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912244Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012244Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112244Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212244Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312244Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412244Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512244Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612244Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712244Sgabeblack@google.com *
2812244Sgabeblack@google.com * Authors: Steve Reinhardt
2912244Sgabeblack@google.com *          Lisa Hsu
3012244Sgabeblack@google.com *          Nathan Binkert
3112244Sgabeblack@google.com *          Ali Saidi
3212244Sgabeblack@google.com */
3312244Sgabeblack@google.com
3412244Sgabeblack@google.com#include "arch/isa_traits.hh"
3512244Sgabeblack@google.com#include "base/loader/object_file.hh"
3612244Sgabeblack@google.com#include "base/loader/symtab.hh"
3712244Sgabeblack@google.com#include "base/trace.hh"
3812244Sgabeblack@google.com#include "cpu/thread_context.hh"
3912244Sgabeblack@google.com#include "mem/mem_object.hh"
4012244Sgabeblack@google.com#include "mem/physical.hh"
4112244Sgabeblack@google.com#include "sim/builder.hh"
4212244Sgabeblack@google.com#include "sim/byteswap.hh"
4312244Sgabeblack@google.com#include "sim/system.hh"
4412244Sgabeblack@google.com#if FULL_SYSTEM
4512244Sgabeblack@google.com#include "arch/vtophys.hh"
4612244Sgabeblack@google.com#include "base/remote_gdb.hh"
4712244Sgabeblack@google.com#include "kern/kernel_stats.hh"
4812244Sgabeblack@google.com#endif
4912244Sgabeblack@google.com
5012244Sgabeblack@google.comusing namespace std;
5112244Sgabeblack@google.comusing namespace TheISA;
5212244Sgabeblack@google.com
5312244Sgabeblack@google.comvector<System *> System::systemList;
5412244Sgabeblack@google.com
5512244Sgabeblack@google.comint System::numSystemsRunning = 0;
5612244Sgabeblack@google.com
5712244Sgabeblack@google.comSystem::System(Params *p)
5812244Sgabeblack@google.com    : SimObject(p->name), physmem(p->physmem), numcpus(0),
5912244Sgabeblack@google.com#if FULL_SYSTEM
6012244Sgabeblack@google.com      init_param(p->init_param),
6112244Sgabeblack@google.com      functionalPort(p->name + "-fport"),
6212244Sgabeblack@google.com      virtPort(p->name + "-vport"),
6312244Sgabeblack@google.com#else
6412244Sgabeblack@google.com      page_ptr(0),
6512244Sgabeblack@google.com#endif
6612244Sgabeblack@google.com      _params(p)
6712244Sgabeblack@google.com{
6812244Sgabeblack@google.com    // add self to global system list
6912244Sgabeblack@google.com    systemList.push_back(this);
7012244Sgabeblack@google.com
7112244Sgabeblack@google.com#if FULL_SYSTEM
7212244Sgabeblack@google.com    kernelSymtab = new SymbolTable;
7312244Sgabeblack@google.com    debugSymbolTable = new SymbolTable;
7412244Sgabeblack@google.com
7512244Sgabeblack@google.com
7612244Sgabeblack@google.com    /**
7712244Sgabeblack@google.com     * Get a functional port to memory
7812244Sgabeblack@google.com     */
7912244Sgabeblack@google.com    Port *mem_port;
8012244Sgabeblack@google.com    mem_port = physmem->getPort("functional");
8112244Sgabeblack@google.com    functionalPort.setPeer(mem_port);
8212244Sgabeblack@google.com    mem_port->setPeer(&functionalPort);
8312244Sgabeblack@google.com
8412244Sgabeblack@google.com    mem_port = physmem->getPort("functional");
8512244Sgabeblack@google.com    virtPort.setPeer(mem_port);
8612244Sgabeblack@google.com    mem_port->setPeer(&virtPort);
8712244Sgabeblack@google.com
8812244Sgabeblack@google.com
8912244Sgabeblack@google.com    /**
9012244Sgabeblack@google.com     * Load the kernel code into memory
9112244Sgabeblack@google.com     */
9212244Sgabeblack@google.com    // Load kernel code
9312244Sgabeblack@google.com    kernel = createObjectFile(params()->kernel_path);
9412244Sgabeblack@google.com    if (kernel == NULL)
9512244Sgabeblack@google.com        fatal("Could not load kernel file %s", params()->kernel_path);
9612244Sgabeblack@google.com
9712244Sgabeblack@google.com    // Load program sections into memory
9812244Sgabeblack@google.com    kernel->loadSections(&functionalPort, LoadAddrMask);
9912244Sgabeblack@google.com
10012244Sgabeblack@google.com    // setup entry points
10112244Sgabeblack@google.com    kernelStart = kernel->textBase();
10212244Sgabeblack@google.com    kernelEnd = kernel->bssBase() + kernel->bssSize();
10312244Sgabeblack@google.com    kernelEntry = kernel->entryPoint();
10412244Sgabeblack@google.com
10512244Sgabeblack@google.com    // load symbols
10612244Sgabeblack@google.com    if (!kernel->loadGlobalSymbols(kernelSymtab))
10712244Sgabeblack@google.com        panic("could not load kernel symbols\n");
10812244Sgabeblack@google.com
10912244Sgabeblack@google.com    if (!kernel->loadLocalSymbols(kernelSymtab))
11012244Sgabeblack@google.com        panic("could not load kernel local symbols\n");
11112244Sgabeblack@google.com
11212244Sgabeblack@google.com    if (!kernel->loadGlobalSymbols(debugSymbolTable))
11312244Sgabeblack@google.com        panic("could not load kernel symbols\n");
11412244Sgabeblack@google.com
11512244Sgabeblack@google.com    if (!kernel->loadLocalSymbols(debugSymbolTable))
11612244Sgabeblack@google.com        panic("could not load kernel local symbols\n");
11712244Sgabeblack@google.com
11812244Sgabeblack@google.com    DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
11912244Sgabeblack@google.com    DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
12012244Sgabeblack@google.com    DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
12112244Sgabeblack@google.com    DPRINTF(Loader, "Kernel loaded...\n");
12212244Sgabeblack@google.com#endif // FULL_SYSTEM
12312244Sgabeblack@google.com
12412244Sgabeblack@google.com    // increment the number of running systms
12512244Sgabeblack@google.com    numSystemsRunning++;
12612244Sgabeblack@google.com}
12712244Sgabeblack@google.com
12812244Sgabeblack@google.comSystem::~System()
12912244Sgabeblack@google.com{
13012244Sgabeblack@google.com#if FULL_SYSTEM
13112244Sgabeblack@google.com    delete kernelSymtab;
13212244Sgabeblack@google.com    delete kernel;
13312244Sgabeblack@google.com#else
13412244Sgabeblack@google.com    panic("System::fixFuncEventAddr needs to be rewritten "
13512244Sgabeblack@google.com          "to work with syscall emulation");
13612244Sgabeblack@google.com#endif // FULL_SYSTEM}
13712244Sgabeblack@google.com}
13812244Sgabeblack@google.com
13912244Sgabeblack@google.com#if FULL_SYSTEM
14012244Sgabeblack@google.com
14112244Sgabeblack@google.com
14212244Sgabeblack@google.comint rgdb_wait = -1;
14312244Sgabeblack@google.com
14412244Sgabeblack@google.com#endif // FULL_SYSTEM
14512244Sgabeblack@google.com
14612244Sgabeblack@google.comint
147System::registerThreadContext(ThreadContext *tc, int id)
148{
149    if (id == -1) {
150        for (id = 0; id < threadContexts.size(); id++) {
151            if (!threadContexts[id])
152                break;
153        }
154    }
155
156    if (threadContexts.size() <= id)
157        threadContexts.resize(id + 1);
158
159    if (threadContexts[id])
160        panic("Cannot have two CPUs with the same id (%d)\n", id);
161
162    threadContexts[id] = tc;
163    numcpus++;
164
165#if FULL_SYSTEM
166    RemoteGDB *rgdb = new RemoteGDB(this, tc);
167    GDBListener *gdbl = new GDBListener(rgdb, 7000 + id);
168    gdbl->listen();
169    /**
170     * Uncommenting this line waits for a remote debugger to connect
171     * to the simulator before continuing.
172     */
173    if (rgdb_wait != -1 && rgdb_wait == id)
174        gdbl->accept();
175
176    if (remoteGDB.size() <= id) {
177        remoteGDB.resize(id + 1);
178    }
179
180    remoteGDB[id] = rgdb;
181#endif // FULL_SYSTEM
182
183    return id;
184}
185
186void
187System::startup()
188{
189    int i;
190    for (i = 0; i < threadContexts.size(); i++)
191        threadContexts[i]->activate(0);
192}
193
194void
195System::replaceThreadContext(ThreadContext *tc, int id)
196{
197    if (id >= threadContexts.size()) {
198        panic("replaceThreadContext: bad id, %d >= %d\n",
199              id, threadContexts.size());
200    }
201
202    threadContexts[id] = tc;
203#if FULL_SYSTEM
204    remoteGDB[id]->replaceThreadContext(tc);
205#endif // FULL_SYSTEM
206}
207
208#if !FULL_SYSTEM
209Addr
210System::new_page()
211{
212    Addr return_addr = page_ptr << LogVMPageSize;
213    ++page_ptr;
214    return return_addr;
215}
216#endif
217
218void
219System::serialize(ostream &os)
220{
221#if FULL_SYSTEM
222    kernelSymtab->serialize("kernel_symtab", os);
223#endif // FULL_SYSTEM
224}
225
226
227void
228System::unserialize(Checkpoint *cp, const string &section)
229{
230#if FULL_SYSTEM
231    kernelSymtab->unserialize("kernel_symtab", cp, section);
232#endif // FULL_SYSTEM
233}
234
235void
236System::printSystems()
237{
238    vector<System *>::iterator i = systemList.begin();
239    vector<System *>::iterator end = systemList.end();
240    for (; i != end; ++i) {
241        System *sys = *i;
242        cerr << "System " << sys->name() << ": " << hex << sys << endl;
243    }
244}
245
246extern "C"
247void
248printSystems()
249{
250    System::printSystems();
251}
252
253#if FULL_SYSTEM
254
255// In full system mode, only derived classes (e.g. AlphaLinuxSystem)
256// can be created directly.
257
258DEFINE_SIM_OBJECT_CLASS_NAME("System", System)
259
260#else
261
262BEGIN_DECLARE_SIM_OBJECT_PARAMS(System)
263
264    SimObjectParam<PhysicalMemory *> physmem;
265
266END_DECLARE_SIM_OBJECT_PARAMS(System)
267
268BEGIN_INIT_SIM_OBJECT_PARAMS(System)
269
270    INIT_PARAM(physmem, "physical memory")
271
272END_INIT_SIM_OBJECT_PARAMS(System)
273
274CREATE_SIM_OBJECT(System)
275{
276    System::Params *p = new System::Params;
277    p->name = getInstanceName();
278    p->physmem = physmem;
279    return new System(p);
280}
281
282REGISTER_SIM_OBJECT("System", System)
283
284#endif
285