system.cc revision 7897:d9e8b1fd1a9f
12139SN/A/*
22139SN/A * Copyright (c) 2003-2006 The Regents of The University of Michigan
32139SN/A * Copyright (c) 2011 Regents of the University of California
42139SN/A * All rights reserved.
52139SN/A *
62139SN/A * Redistribution and use in source and binary forms, with or without
72139SN/A * modification, are permitted provided that the following conditions are
82139SN/A * met: redistributions of source code must retain the above copyright
92139SN/A * notice, this list of conditions and the following disclaimer;
102139SN/A * redistributions in binary form must reproduce the above copyright
112139SN/A * notice, this list of conditions and the following disclaimer in the
122139SN/A * documentation and/or other materials provided with the distribution;
132139SN/A * neither the name of the copyright holders nor the names of its
142139SN/A * contributors may be used to endorse or promote products derived from
152139SN/A * this software without specific prior written permission.
162139SN/A *
172139SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182139SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192139SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202139SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212139SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222139SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232139SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242139SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252139SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262139SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272139SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
302139SN/A *          Lisa Hsu
314202Sbinkertn@umich.edu *          Nathan Binkert
322139SN/A *          Ali Saidi
334202Sbinkertn@umich.edu *          Rick Strong
342152SN/A */
352152SN/A
362139SN/A#include "arch/isa_traits.hh"
372139SN/A#include "arch/remote_gdb.hh"
382139SN/A#include "arch/utility.hh"
392139SN/A#include "base/loader/object_file.hh"
402139SN/A#include "base/loader/symtab.hh"
412152SN/A#include "base/trace.hh"
422152SN/A#include "cpu/thread_context.hh"
432139SN/A#include "config/full_system.hh"
442139SN/A#include "config/the_isa.hh"
452139SN/A#include "mem/mem_object.hh"
464781Snate@binkert.org#include "mem/physical.hh"
474781Snate@binkert.org#include "sim/byteswap.hh"
484781Snate@binkert.org#include "sim/system.hh"
496313Sgblack@eecs.umich.edu#include "sim/debug.hh"
504781Snate@binkert.org
514781Snate@binkert.org#if FULL_SYSTEM
523170Sstever@eecs.umich.edu#include "arch/vtophys.hh"
535664Sgblack@eecs.umich.edu#include "kern/kernel_stats.hh"
543806Ssaidi@eecs.umich.edu#include "mem/vport.hh"
556179Sksewell@umich.edu#else
564781Snate@binkert.org#include "params/System.hh"
574781Snate@binkert.org#endif
586329Sgblack@eecs.umich.edu
594781Snate@binkert.orgusing namespace std;
604781Snate@binkert.orgusing namespace TheISA;
614781Snate@binkert.org
624781Snate@binkert.orgvector<System *> System::systemList;
634781Snate@binkert.org
644781Snate@binkert.orgint System::numSystemsRunning = 0;
652139SN/A
662139SN/ASystem::System(Params *p)
673546Sgblack@eecs.umich.edu    : SimObject(p), physmem(p->physmem), _numContexts(0),
684202Sbinkertn@umich.edu#if FULL_SYSTEM
692152SN/A      init_param(p->init_param),
702152SN/A      loadAddrMask(p->load_addr_mask),
712152SN/A#else
722152SN/A      pagePtr(0),
732152SN/A      nextPID(0),
742152SN/A#endif
752152SN/A      memoryMode(p->mem_mode), _params(p),
762152SN/A      totalNumInsts(0),
772152SN/A      instEventQueue("system instruction-based event queue")
782152SN/A{
792152SN/A    // add self to global system list
802152SN/A    systemList.push_back(this);
812504SN/A
822504SN/A#if FULL_SYSTEM
832504SN/A    kernelSymtab = new SymbolTable;
842504SN/A    if (!debugSymbolTable)
852152SN/A        debugSymbolTable = new SymbolTable;
862504SN/A
872152SN/A
882152SN/A    /**
892152SN/A     * Get a functional port to memory
902152SN/A     */
912152SN/A    Port *mem_port;
922152SN/A    functionalPort = new FunctionalPort(name() + "-fport");
936993Snate@binkert.org    mem_port = physmem->getPort("functional");
946993Snate@binkert.org    functionalPort->setPeer(mem_port);
956993Snate@binkert.org    mem_port->setPeer(functionalPort);
966993Snate@binkert.org
976993Snate@binkert.org    virtPort = new VirtualPort(name() + "-fport");
986993Snate@binkert.org    mem_port = physmem->getPort("functional");
996993Snate@binkert.org    virtPort->setPeer(mem_port);
1006993Snate@binkert.org    mem_port->setPeer(virtPort);
1016993Snate@binkert.org
1026993Snate@binkert.org
1036993Snate@binkert.org    /**
1046993Snate@binkert.org     * Load the kernel code into memory
1056993Snate@binkert.org     */
1066993Snate@binkert.org    if (params()->kernel == "") {
1076993Snate@binkert.org        inform("No kernel set for full system simulation. Assuming you know what"
1086993Snate@binkert.org                " you're doing...\n");
1096993Snate@binkert.org    } else {
1106998Snate@binkert.org        // Load kernel code
1116998Snate@binkert.org        kernel = createObjectFile(params()->kernel);
1126998Snate@binkert.org        inform("kernel located at: %s", params()->kernel);
1136993Snate@binkert.org
1146993Snate@binkert.org        if (kernel == NULL)
1156993Snate@binkert.org            fatal("Could not load kernel file %s", params()->kernel);
1166993Snate@binkert.org
1176993Snate@binkert.org        // Load program sections into memory
1186993Snate@binkert.org        kernel->loadSections(functionalPort, loadAddrMask);
1196993Snate@binkert.org
1206993Snate@binkert.org        // setup entry points
1216993Snate@binkert.org        kernelStart = kernel->textBase();
1222152SN/A        kernelEnd = kernel->bssBase() + kernel->bssSize();
1232766Sktlim@umich.edu        kernelEntry = kernel->entryPoint();
1242766Sktlim@umich.edu
1256993Snate@binkert.org        // load symbols
1262152SN/A        if (!kernel->loadGlobalSymbols(kernelSymtab))
1272152SN/A            fatal("could not load kernel symbols\n");
1285944Sgblack@eecs.umich.edu
1295944Sgblack@eecs.umich.edu        if (!kernel->loadLocalSymbols(kernelSymtab))
1305944Sgblack@eecs.umich.edu            fatal("could not load kernel local symbols\n");
1315944Sgblack@eecs.umich.edu
1325944Sgblack@eecs.umich.edu        if (!kernel->loadGlobalSymbols(debugSymbolTable))
133            fatal("could not load kernel symbols\n");
134
135        if (!kernel->loadLocalSymbols(debugSymbolTable))
136            fatal("could not load kernel local symbols\n");
137
138        DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
139        DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
140        DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
141        DPRINTF(Loader, "Kernel loaded...\n");
142    }
143#endif // FULL_SYSTEM
144
145    // increment the number of running systms
146    numSystemsRunning++;
147}
148
149System::~System()
150{
151#if FULL_SYSTEM
152    delete kernelSymtab;
153    delete kernel;
154#else
155    panic("System::fixFuncEventAddr needs to be rewritten "
156          "to work with syscall emulation");
157#endif // FULL_SYSTEM}
158}
159
160void
161System::setMemoryMode(Enums::MemoryMode mode)
162{
163    assert(getState() == Drained);
164    memoryMode = mode;
165}
166
167bool System::breakpoint()
168{
169    if (remoteGDB.size())
170        return remoteGDB[0]->breakpoint();
171    return false;
172}
173
174/**
175 * Setting rgdb_wait to a positive integer waits for a remote debugger to
176 * connect to that context ID before continuing.  This should really
177   be a parameter on the CPU object or something...
178 */
179int rgdb_wait = -1;
180
181int
182System::registerThreadContext(ThreadContext *tc, int assigned)
183{
184    int id;
185    if (assigned == -1) {
186        for (id = 0; id < threadContexts.size(); id++) {
187            if (!threadContexts[id])
188                break;
189        }
190
191        if (threadContexts.size() <= id)
192            threadContexts.resize(id + 1);
193    } else {
194        if (threadContexts.size() <= assigned)
195            threadContexts.resize(assigned + 1);
196        id = assigned;
197    }
198
199    if (threadContexts[id])
200        fatal("Cannot have two CPUs with the same id (%d)\n", id);
201
202    threadContexts[id] = tc;
203    _numContexts++;
204
205    int port = getRemoteGDBPort();
206    if (port) {
207        RemoteGDB *rgdb = new RemoteGDB(this, tc);
208        GDBListener *gdbl = new GDBListener(rgdb, port + id);
209        gdbl->listen();
210
211        if (rgdb_wait != -1 && rgdb_wait == id)
212            gdbl->accept();
213
214        if (remoteGDB.size() <= id) {
215            remoteGDB.resize(id + 1);
216        }
217
218        remoteGDB[id] = rgdb;
219    }
220
221    return id;
222}
223
224int
225System::numRunningContexts()
226{
227    int running = 0;
228    for (int i = 0; i < _numContexts; ++i) {
229        if (threadContexts[i]->status() != ThreadContext::Halted)
230            ++running;
231    }
232    return running;
233}
234
235void
236System::initState()
237{
238#if FULL_SYSTEM
239    int i;
240    for (i = 0; i < threadContexts.size(); i++)
241        TheISA::startupCPU(threadContexts[i], i);
242#endif
243}
244
245void
246System::replaceThreadContext(ThreadContext *tc, int context_id)
247{
248    if (context_id >= threadContexts.size()) {
249        panic("replaceThreadContext: bad id, %d >= %d\n",
250              context_id, threadContexts.size());
251    }
252
253    threadContexts[context_id] = tc;
254    if (context_id < remoteGDB.size())
255        remoteGDB[context_id]->replaceThreadContext(tc);
256}
257
258#if !FULL_SYSTEM
259Addr
260System::new_page()
261{
262    Addr return_addr = pagePtr << LogVMPageSize;
263    ++pagePtr;
264    if (return_addr >= physmem->size())
265        fatal("Out of memory, please increase size of physical memory.");
266    return return_addr;
267}
268
269Addr
270System::memSize()
271{
272    return physmem->size();
273}
274
275Addr
276System::freeMemSize()
277{
278   return physmem->size() - (pagePtr << LogVMPageSize);
279}
280
281#endif
282
283void
284System::resume()
285{
286    SimObject::resume();
287    totalNumInsts = 0;
288}
289
290void
291System::serialize(ostream &os)
292{
293#if FULL_SYSTEM
294    kernelSymtab->serialize("kernel_symtab", os);
295#else // !FULL_SYSTEM
296    SERIALIZE_SCALAR(pagePtr);
297    SERIALIZE_SCALAR(nextPID);
298#endif
299}
300
301
302void
303System::unserialize(Checkpoint *cp, const string &section)
304{
305#if FULL_SYSTEM
306    kernelSymtab->unserialize("kernel_symtab", cp, section);
307#else // !FULL_SYSTEM
308    UNSERIALIZE_SCALAR(pagePtr);
309    UNSERIALIZE_SCALAR(nextPID);
310#endif
311}
312
313void
314System::printSystems()
315{
316    vector<System *>::iterator i = systemList.begin();
317    vector<System *>::iterator end = systemList.end();
318    for (; i != end; ++i) {
319        System *sys = *i;
320        cerr << "System " << sys->name() << ": " << hex << sys << endl;
321    }
322}
323
324void
325printSystems()
326{
327    System::printSystems();
328}
329
330const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
331    "timing"};
332
333#if !FULL_SYSTEM
334
335System *
336SystemParams::create()
337{
338    return new System(this);
339}
340
341#endif
342