system.cc revision 5222
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"
353960Sgblack@eecs.umich.edu#include "arch/remote_gdb.hh"
364194Ssaidi@eecs.umich.edu#include "arch/utility.hh"
371070SN/A#include "base/loader/object_file.hh"
381070SN/A#include "base/loader/symtab.hh"
392521SN/A#include "base/trace.hh"
402680Sktlim@umich.edu#include "cpu/thread_context.hh"
412521SN/A#include "mem/mem_object.hh"
422522SN/A#include "mem/physical.hh"
432037SN/A#include "sim/byteswap.hh"
4456SN/A#include "sim/system.hh"
452378SN/A#if FULL_SYSTEM
462521SN/A#include "arch/vtophys.hh"
472378SN/A#include "kern/kernel_stats.hh"
484762Snate@binkert.org#else
494762Snate@binkert.org#include "params/System.hh"
502378SN/A#endif
512SN/A
522SN/Ausing namespace std;
532107SN/Ausing namespace TheISA;
542SN/A
552SN/Avector<System *> System::systemList;
562SN/A
572SN/Aint System::numSystemsRunning = 0;
582SN/A
591070SN/ASystem::System(Params *p)
605034Smilesck@eecs.umich.edu    : SimObject(p), physmem(p->physmem), numcpus(0),
612378SN/A#if FULL_SYSTEM
622521SN/A      init_param(p->init_param),
632640Sstever@eecs.umich.edu      functionalPort(p->name + "-fport"),
642640Sstever@eecs.umich.edu      virtPort(p->name + "-vport"),
652378SN/A#else
662378SN/A      page_ptr(0),
674997Sgblack@eecs.umich.edu      next_PID(0),
682378SN/A#endif
692902Ssaidi@eecs.umich.edu      memoryMode(p->mem_mode), _params(p)
702SN/A{
711070SN/A    // add self to global system list
721070SN/A    systemList.push_back(this);
731070SN/A
742378SN/A#if FULL_SYSTEM
751070SN/A    kernelSymtab = new SymbolTable;
764838Ssaidi@eecs.umich.edu    if (!debugSymbolTable)
774838Ssaidi@eecs.umich.edu        debugSymbolTable = new SymbolTable;
781070SN/A
792520SN/A
802520SN/A    /**
812520SN/A     * Get a functional port to memory
822520SN/A     */
832520SN/A    Port *mem_port;
842520SN/A    mem_port = physmem->getPort("functional");
852520SN/A    functionalPort.setPeer(mem_port);
862520SN/A    mem_port->setPeer(&functionalPort);
872520SN/A
882521SN/A    mem_port = physmem->getPort("functional");
892521SN/A    virtPort.setPeer(mem_port);
902521SN/A    mem_port->setPeer(&virtPort);
912521SN/A
922520SN/A
931070SN/A    /**
942158SN/A     * Load the kernel code into memory
951070SN/A     */
964762Snate@binkert.org    if (params()->kernel == "") {
973812Ssaidi@eecs.umich.edu        warn("No kernel set for full system simulation. Assuming you know what"
983812Ssaidi@eecs.umich.edu                " you're doing...\n");
993812Ssaidi@eecs.umich.edu    } else {
1003812Ssaidi@eecs.umich.edu        // Load kernel code
1014762Snate@binkert.org        kernel = createObjectFile(params()->kernel);
1025222Sksewell@umich.edu        warn("kernel located at: %s", params()->kernel);
1035222Sksewell@umich.edu
1043812Ssaidi@eecs.umich.edu        if (kernel == NULL)
1054762Snate@binkert.org            fatal("Could not load kernel file %s", params()->kernel);
1061070SN/A
1073812Ssaidi@eecs.umich.edu        // Load program sections into memory
1083812Ssaidi@eecs.umich.edu        kernel->loadSections(&functionalPort, LoadAddrMask);
1091070SN/A
1103812Ssaidi@eecs.umich.edu        // setup entry points
1113812Ssaidi@eecs.umich.edu        kernelStart = kernel->textBase();
1123812Ssaidi@eecs.umich.edu        kernelEnd = kernel->bssBase() + kernel->bssSize();
1133812Ssaidi@eecs.umich.edu        kernelEntry = kernel->entryPoint();
1141070SN/A
1153812Ssaidi@eecs.umich.edu        // load symbols
1163812Ssaidi@eecs.umich.edu        if (!kernel->loadGlobalSymbols(kernelSymtab))
1173812Ssaidi@eecs.umich.edu            panic("could not load kernel symbols\n");
1181070SN/A
1193812Ssaidi@eecs.umich.edu        if (!kernel->loadLocalSymbols(kernelSymtab))
1203812Ssaidi@eecs.umich.edu            panic("could not load kernel local symbols\n");
1211070SN/A
1223812Ssaidi@eecs.umich.edu        if (!kernel->loadGlobalSymbols(debugSymbolTable))
1233812Ssaidi@eecs.umich.edu            panic("could not load kernel symbols\n");
1241074SN/A
1253812Ssaidi@eecs.umich.edu        if (!kernel->loadLocalSymbols(debugSymbolTable))
1263812Ssaidi@eecs.umich.edu            panic("could not load kernel local symbols\n");
1271074SN/A
1283812Ssaidi@eecs.umich.edu        DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
1293812Ssaidi@eecs.umich.edu        DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
1303812Ssaidi@eecs.umich.edu        DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
1313812Ssaidi@eecs.umich.edu        DPRINTF(Loader, "Kernel loaded...\n");
1323812Ssaidi@eecs.umich.edu    }
1332378SN/A#endif // FULL_SYSTEM
1342378SN/A
1351070SN/A    // increment the number of running systms
136878SN/A    numSystemsRunning++;
1372SN/A}
1382SN/A
1392SN/ASystem::~System()
1402SN/A{
1412378SN/A#if FULL_SYSTEM
1421070SN/A    delete kernelSymtab;
1431070SN/A    delete kernel;
1442378SN/A#else
1452378SN/A    panic("System::fixFuncEventAddr needs to be rewritten "
1462378SN/A          "to work with syscall emulation");
1472378SN/A#endif // FULL_SYSTEM}
1482SN/A}
1492SN/A
1501808SN/Aint rgdb_wait = -1;
1514095Sbinkertn@umich.eduint rgdb_enable = true;
1521808SN/A
1532901Ssaidi@eecs.umich.eduvoid
1544762Snate@binkert.orgSystem::setMemoryMode(Enums::MemoryMode mode)
1552901Ssaidi@eecs.umich.edu{
1562901Ssaidi@eecs.umich.edu    assert(getState() == Drained);
1572901Ssaidi@eecs.umich.edu    memoryMode = mode;
1582901Ssaidi@eecs.umich.edu}
1592901Ssaidi@eecs.umich.edu
1603960Sgblack@eecs.umich.edubool System::breakpoint()
1613960Sgblack@eecs.umich.edu{
1624095Sbinkertn@umich.edu    if (remoteGDB.size())
1634095Sbinkertn@umich.edu        return remoteGDB[0]->breakpoint();
1644095Sbinkertn@umich.edu    return false;
1653960Sgblack@eecs.umich.edu}
1663960Sgblack@eecs.umich.edu
167180SN/Aint
1682680Sktlim@umich.eduSystem::registerThreadContext(ThreadContext *tc, int id)
1692SN/A{
1701806SN/A    if (id == -1) {
1712680Sktlim@umich.edu        for (id = 0; id < threadContexts.size(); id++) {
1722680Sktlim@umich.edu            if (!threadContexts[id])
1731806SN/A                break;
1741806SN/A        }
1751806SN/A    }
1761806SN/A
1772680Sktlim@umich.edu    if (threadContexts.size() <= id)
1782680Sktlim@umich.edu        threadContexts.resize(id + 1);
1791806SN/A
1802680Sktlim@umich.edu    if (threadContexts[id])
1811806SN/A        panic("Cannot have two CPUs with the same id (%d)\n", id);
1821806SN/A
1832680Sktlim@umich.edu    threadContexts[id] = tc;
1841806SN/A    numcpus++;
1851070SN/A
1864095Sbinkertn@umich.edu    if (rgdb_enable) {
1874095Sbinkertn@umich.edu        RemoteGDB *rgdb = new RemoteGDB(this, tc);
1884095Sbinkertn@umich.edu        GDBListener *gdbl = new GDBListener(rgdb, 7000 + id);
1894095Sbinkertn@umich.edu        gdbl->listen();
1904095Sbinkertn@umich.edu        /**
1914095Sbinkertn@umich.edu         * Uncommenting this line waits for a remote debugger to
1924095Sbinkertn@umich.edu         * connect to the simulator before continuing.
1934095Sbinkertn@umich.edu         */
1944095Sbinkertn@umich.edu        if (rgdb_wait != -1 && rgdb_wait == id)
1954095Sbinkertn@umich.edu            gdbl->accept();
1961070SN/A
1974095Sbinkertn@umich.edu        if (remoteGDB.size() <= id) {
1984095Sbinkertn@umich.edu            remoteGDB.resize(id + 1);
1994095Sbinkertn@umich.edu        }
2004095Sbinkertn@umich.edu
2014095Sbinkertn@umich.edu        remoteGDB[id] = rgdb;
2021070SN/A    }
2031070SN/A
2041806SN/A    return id;
205180SN/A}
20675SN/A
207180SN/Avoid
2081129SN/ASystem::startup()
2091129SN/A{
2102114SN/A    int i;
2112680Sktlim@umich.edu    for (i = 0; i < threadContexts.size(); i++)
2124194Ssaidi@eecs.umich.edu        TheISA::startupCPU(threadContexts[i], i);
2131129SN/A}
2141129SN/A
2151129SN/Avoid
2162680Sktlim@umich.eduSystem::replaceThreadContext(ThreadContext *tc, int id)
217180SN/A{
2182680Sktlim@umich.edu    if (id >= threadContexts.size()) {
2192680Sktlim@umich.edu        panic("replaceThreadContext: bad id, %d >= %d\n",
2202680Sktlim@umich.edu              id, threadContexts.size());
221180SN/A    }
222180SN/A
2232680Sktlim@umich.edu    threadContexts[id] = tc;
2244095Sbinkertn@umich.edu    if (id < remoteGDB.size())
2254095Sbinkertn@umich.edu        remoteGDB[id]->replaceThreadContext(tc);
2262SN/A}
2272SN/A
2282378SN/A#if !FULL_SYSTEM
2292378SN/AAddr
2302378SN/ASystem::new_page()
2312378SN/A{
2322378SN/A    Addr return_addr = page_ptr << LogVMPageSize;
2332378SN/A    ++page_ptr;
2343162Ssaidi@eecs.umich.edu    if (return_addr >= physmem->size())
2353162Ssaidi@eecs.umich.edu        fatal("Out of memory, please increase size of physical memory.");
2362378SN/A    return return_addr;
2372378SN/A}
2382378SN/A#endif
2392378SN/A
2401070SN/Avoid
2411070SN/ASystem::serialize(ostream &os)
2421070SN/A{
2432378SN/A#if FULL_SYSTEM
2441984SN/A    kernelSymtab->serialize("kernel_symtab", os);
2455183Ssaidi@eecs.umich.edu#else // !FULL_SYSTEM
2465183Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(page_ptr);
2475183Ssaidi@eecs.umich.edu#endif
2481070SN/A}
2491070SN/A
2501070SN/A
2511070SN/Avoid
2521070SN/ASystem::unserialize(Checkpoint *cp, const string &section)
2531070SN/A{
2542378SN/A#if FULL_SYSTEM
2551984SN/A    kernelSymtab->unserialize("kernel_symtab", cp, section);
2565183Ssaidi@eecs.umich.edu#else // !FULL_SYSTEM
2575183Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(page_ptr);
2585183Ssaidi@eecs.umich.edu#endif
2591070SN/A}
2602SN/A
2612SN/Avoid
2622SN/ASystem::printSystems()
2632SN/A{
2642SN/A    vector<System *>::iterator i = systemList.begin();
2652SN/A    vector<System *>::iterator end = systemList.end();
2662SN/A    for (; i != end; ++i) {
2672SN/A        System *sys = *i;
2682SN/A        cerr << "System " << sys->name() << ": " << hex << sys << endl;
2692SN/A    }
2702SN/A}
2712SN/A
2722SN/Avoid
2732SN/AprintSystems()
2742SN/A{
2752SN/A    System::printSystems();
2762SN/A}
2772SN/A
2782902Ssaidi@eecs.umich.educonst char *System::MemoryModeStrings[3] = {"invalid", "atomic",
2792902Ssaidi@eecs.umich.edu    "timing"};
2802902Ssaidi@eecs.umich.edu
2814762Snate@binkert.org#if !FULL_SYSTEM
2822424SN/A
2834762Snate@binkert.orgSystem *
2844762Snate@binkert.orgSystemParams::create()
2852424SN/A{
2862424SN/A    System::Params *p = new System::Params;
2874762Snate@binkert.org    p->name = name;
2882424SN/A    p->physmem = physmem;
2892902Ssaidi@eecs.umich.edu    p->mem_mode = mem_mode;
2902424SN/A    return new System(p);
2912424SN/A}
2922424SN/A
2932424SN/A#endif
294