system.cc revision 2689
12689Sktlim@umich.edu/*
22689Sktlim@umich.edu * Copyright (c) 2003-2006 The Regents of The University of Michigan
32689Sktlim@umich.edu * All rights reserved.
42689Sktlim@umich.edu *
52689Sktlim@umich.edu * Redistribution and use in source and binary forms, with or without
62689Sktlim@umich.edu * modification, are permitted provided that the following conditions are
72689Sktlim@umich.edu * met: redistributions of source code must retain the above copyright
82689Sktlim@umich.edu * notice, this list of conditions and the following disclaimer;
92689Sktlim@umich.edu * redistributions in binary form must reproduce the above copyright
102689Sktlim@umich.edu * notice, this list of conditions and the following disclaimer in the
112689Sktlim@umich.edu * documentation and/or other materials provided with the distribution;
122689Sktlim@umich.edu * neither the name of the copyright holders nor the names of its
132689Sktlim@umich.edu * contributors may be used to endorse or promote products derived from
142689Sktlim@umich.edu * this software without specific prior written permission.
152689Sktlim@umich.edu *
162689Sktlim@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172689Sktlim@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182689Sktlim@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192689Sktlim@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202689Sktlim@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212689Sktlim@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222689Sktlim@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232689Sktlim@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242689Sktlim@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252689Sktlim@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262689Sktlim@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272689Sktlim@umich.edu *
282689Sktlim@umich.edu * Authors: Steve Reinhardt
292689Sktlim@umich.edu *          Lisa Hsu
302689Sktlim@umich.edu *          Nathan Binkert
312689Sktlim@umich.edu *          Ali Saidi
322689Sktlim@umich.edu */
332689Sktlim@umich.edu
342521SN/A#include "arch/isa_traits.hh"
351070SN/A#include "base/loader/object_file.hh"
361070SN/A#include "base/loader/symtab.hh"
372521SN/A#include "base/trace.hh"
382680Sktlim@umich.edu#include "cpu/thread_context.hh"
392521SN/A#include "mem/mem_object.hh"
402522SN/A#include "mem/physical.hh"
411711SN/A#include "sim/builder.hh"
422037SN/A#include "sim/byteswap.hh"
4356SN/A#include "sim/system.hh"
442378SN/A#if FULL_SYSTEM
452521SN/A#include "arch/vtophys.hh"
462378SN/A#include "base/remote_gdb.hh"
472378SN/A#include "kern/kernel_stats.hh"
482378SN/A#endif
492SN/A
502SN/Ausing namespace std;
512107SN/Ausing namespace TheISA;
522SN/A
532SN/Avector<System *> System::systemList;
542SN/A
552SN/Aint System::numSystemsRunning = 0;
562SN/A
571070SN/ASystem::System(Params *p)
582378SN/A    : SimObject(p->name), physmem(p->physmem), numcpus(0),
592378SN/A#if FULL_SYSTEM
602521SN/A      init_param(p->init_param),
612640Sstever@eecs.umich.edu      functionalPort(p->name + "-fport"),
622640Sstever@eecs.umich.edu      virtPort(p->name + "-vport"),
632378SN/A#else
642378SN/A      page_ptr(0),
652378SN/A#endif
662422SN/A      _params(p)
672SN/A{
681070SN/A    // add self to global system list
691070SN/A    systemList.push_back(this);
701070SN/A
712378SN/A#if FULL_SYSTEM
721070SN/A    kernelSymtab = new SymbolTable;
731074SN/A    debugSymbolTable = new SymbolTable;
741070SN/A
752520SN/A
762520SN/A    /**
772520SN/A     * Get a functional port to memory
782520SN/A     */
792520SN/A    Port *mem_port;
802520SN/A    mem_port = physmem->getPort("functional");
812520SN/A    functionalPort.setPeer(mem_port);
822520SN/A    mem_port->setPeer(&functionalPort);
832520SN/A
842521SN/A    mem_port = physmem->getPort("functional");
852521SN/A    virtPort.setPeer(mem_port);
862521SN/A    mem_port->setPeer(&virtPort);
872521SN/A
882520SN/A
891070SN/A    /**
902158SN/A     * Load the kernel code into memory
911070SN/A     */
921070SN/A    // Load kernel code
932158SN/A    kernel = createObjectFile(params()->kernel_path);
941070SN/A    if (kernel == NULL)
952158SN/A        fatal("Could not load kernel file %s", params()->kernel_path);
961070SN/A
971070SN/A    // Load program sections into memory
982520SN/A    kernel->loadSections(&functionalPort, LoadAddrMask);
991070SN/A
1001070SN/A    // setup entry points
1011070SN/A    kernelStart = kernel->textBase();
1021070SN/A    kernelEnd = kernel->bssBase() + kernel->bssSize();
1031070SN/A    kernelEntry = kernel->entryPoint();
1041070SN/A
1051070SN/A    // load symbols
1061070SN/A    if (!kernel->loadGlobalSymbols(kernelSymtab))
1071070SN/A        panic("could not load kernel symbols\n");
1081070SN/A
1091070SN/A    if (!kernel->loadLocalSymbols(kernelSymtab))
1101070SN/A        panic("could not load kernel local symbols\n");
1111070SN/A
1121082SN/A    if (!kernel->loadGlobalSymbols(debugSymbolTable))
1131074SN/A        panic("could not load kernel symbols\n");
1141074SN/A
1151074SN/A    if (!kernel->loadLocalSymbols(debugSymbolTable))
1161074SN/A        panic("could not load kernel local symbols\n");
1171074SN/A
1181070SN/A    DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
1191070SN/A    DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
1201070SN/A    DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
1211070SN/A    DPRINTF(Loader, "Kernel loaded...\n");
1221070SN/A
1232378SN/A    kernelBinning = new Kernel::Binning(this);
1242378SN/A#endif // FULL_SYSTEM
1252378SN/A
1261070SN/A    // increment the number of running systms
127878SN/A    numSystemsRunning++;
1282SN/A}
1292SN/A
1302SN/ASystem::~System()
1312SN/A{
1322378SN/A#if FULL_SYSTEM
1331070SN/A    delete kernelSymtab;
1341070SN/A    delete kernel;
1351070SN/A
1361070SN/A    delete kernelBinning;
1372378SN/A#else
1382378SN/A    panic("System::fixFuncEventAddr needs to be rewritten "
1392378SN/A          "to work with syscall emulation");
1402378SN/A#endif // FULL_SYSTEM}
1412SN/A}
1422SN/A
1432378SN/A#if FULL_SYSTEM
1441885SN/A
1452SN/A
1461808SN/Aint rgdb_wait = -1;
1471808SN/A
1482378SN/A#endif // FULL_SYSTEM
1492378SN/A
150180SN/Aint
1512680Sktlim@umich.eduSystem::registerThreadContext(ThreadContext *tc, int id)
1522SN/A{
1531806SN/A    if (id == -1) {
1542680Sktlim@umich.edu        for (id = 0; id < threadContexts.size(); id++) {
1552680Sktlim@umich.edu            if (!threadContexts[id])
1561806SN/A                break;
1571806SN/A        }
1581806SN/A    }
1591806SN/A
1602680Sktlim@umich.edu    if (threadContexts.size() <= id)
1612680Sktlim@umich.edu        threadContexts.resize(id + 1);
1621806SN/A
1632680Sktlim@umich.edu    if (threadContexts[id])
1641806SN/A        panic("Cannot have two CPUs with the same id (%d)\n", id);
1651806SN/A
1662680Sktlim@umich.edu    threadContexts[id] = tc;
1671806SN/A    numcpus++;
1681070SN/A
1692378SN/A#if FULL_SYSTEM
1702680Sktlim@umich.edu    RemoteGDB *rgdb = new RemoteGDB(this, tc);
1711806SN/A    GDBListener *gdbl = new GDBListener(rgdb, 7000 + id);
1721070SN/A    gdbl->listen();
1731070SN/A    /**
1741070SN/A     * Uncommenting this line waits for a remote debugger to connect
1751070SN/A     * to the simulator before continuing.
1761070SN/A     */
1771808SN/A    if (rgdb_wait != -1 && rgdb_wait == id)
1781808SN/A        gdbl->accept();
1791070SN/A
1801806SN/A    if (remoteGDB.size() <= id) {
1811806SN/A        remoteGDB.resize(id + 1);
1821070SN/A    }
1831070SN/A
1841806SN/A    remoteGDB[id] = rgdb;
1852378SN/A#endif // FULL_SYSTEM
1861070SN/A
1871806SN/A    return id;
188180SN/A}
18975SN/A
190180SN/Avoid
1911129SN/ASystem::startup()
1921129SN/A{
1932114SN/A    int i;
1942680Sktlim@umich.edu    for (i = 0; i < threadContexts.size(); i++)
1952680Sktlim@umich.edu        threadContexts[i]->activate(0);
1961129SN/A}
1971129SN/A
1981129SN/Avoid
1992680Sktlim@umich.eduSystem::replaceThreadContext(ThreadContext *tc, int id)
200180SN/A{
2012680Sktlim@umich.edu    if (id >= threadContexts.size()) {
2022680Sktlim@umich.edu        panic("replaceThreadContext: bad id, %d >= %d\n",
2032680Sktlim@umich.edu              id, threadContexts.size());
204180SN/A    }
205180SN/A
2062680Sktlim@umich.edu    threadContexts[id] = tc;
2072378SN/A#if FULL_SYSTEM
2082680Sktlim@umich.edu    remoteGDB[id]->replaceThreadContext(tc);
2092378SN/A#endif // FULL_SYSTEM
2102SN/A}
2112SN/A
2122378SN/A#if !FULL_SYSTEM
2132378SN/AAddr
2142378SN/ASystem::new_page()
2152378SN/A{
2162378SN/A    Addr return_addr = page_ptr << LogVMPageSize;
2172378SN/A    ++page_ptr;
2182378SN/A    return return_addr;
2192378SN/A}
2202378SN/A#endif
2212378SN/A
2221070SN/Avoid
2231070SN/ASystem::regStats()
2241070SN/A{
2252378SN/A#if FULL_SYSTEM
2261070SN/A    kernelBinning->regStats(name() + ".kern");
2272378SN/A#endif // FULL_SYSTEM
2281070SN/A}
2291070SN/A
2301070SN/Avoid
2311070SN/ASystem::serialize(ostream &os)
2321070SN/A{
2332378SN/A#if FULL_SYSTEM
2341070SN/A    kernelBinning->serialize(os);
2351984SN/A
2361984SN/A    kernelSymtab->serialize("kernel_symtab", os);
2372378SN/A#endif // FULL_SYSTEM
2381070SN/A}
2391070SN/A
2401070SN/A
2411070SN/Avoid
2421070SN/ASystem::unserialize(Checkpoint *cp, const string &section)
2431070SN/A{
2442378SN/A#if FULL_SYSTEM
2451070SN/A    kernelBinning->unserialize(cp, section);
2461984SN/A
2471984SN/A    kernelSymtab->unserialize("kernel_symtab", cp, section);
2482378SN/A#endif // FULL_SYSTEM
2491070SN/A}
2502SN/A
2512SN/Avoid
2522SN/ASystem::printSystems()
2532SN/A{
2542SN/A    vector<System *>::iterator i = systemList.begin();
2552SN/A    vector<System *>::iterator end = systemList.end();
2562SN/A    for (; i != end; ++i) {
2572SN/A        System *sys = *i;
2582SN/A        cerr << "System " << sys->name() << ": " << hex << sys << endl;
2592SN/A    }
2602SN/A}
2612SN/A
2622SN/Aextern "C"
2632SN/Avoid
2642SN/AprintSystems()
2652SN/A{
2662SN/A    System::printSystems();
2672SN/A}
2682SN/A
2692424SN/A#if FULL_SYSTEM
2702424SN/A
2712424SN/A// In full system mode, only derived classes (e.g. AlphaLinuxSystem)
2722424SN/A// can be created directly.
2732424SN/A
2742158SN/ADEFINE_SIM_OBJECT_CLASS_NAME("System", System)
2752SN/A
2762424SN/A#else
2772424SN/A
2782424SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(System)
2792424SN/A
2802522SN/A    SimObjectParam<PhysicalMemory *> physmem;
2812424SN/A
2822424SN/AEND_DECLARE_SIM_OBJECT_PARAMS(System)
2832424SN/A
2842424SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(System)
2852424SN/A
2862424SN/A    INIT_PARAM(physmem, "physical memory")
2872424SN/A
2882424SN/AEND_INIT_SIM_OBJECT_PARAMS(System)
2892424SN/A
2902424SN/ACREATE_SIM_OBJECT(System)
2912424SN/A{
2922424SN/A    System::Params *p = new System::Params;
2932424SN/A    p->name = getInstanceName();
2942424SN/A    p->physmem = physmem;
2952424SN/A    return new System(p);
2962424SN/A}
2972424SN/A
2982424SN/AREGISTER_SIM_OBJECT("System", System)
2992424SN/A
3002424SN/A#endif
301