system.cc revision 10282:3ea92bc6393b
19020Sgblack@eecs.umich.edu/*
29020Sgblack@eecs.umich.edu * Copyright (c) 2011-2014 ARM Limited
39020Sgblack@eecs.umich.edu * All rights reserved
49020Sgblack@eecs.umich.edu *
59020Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
69020Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
79020Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
89020Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
99020Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
109020Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
119020Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
129020Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
139020Sgblack@eecs.umich.edu *
149020Sgblack@eecs.umich.edu * Copyright (c) 2003-2006 The Regents of The University of Michigan
159020Sgblack@eecs.umich.edu * Copyright (c) 2011 Regents of the University of California
169020Sgblack@eecs.umich.edu * All rights reserved.
179020Sgblack@eecs.umich.edu *
189020Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
199020Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
209020Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
219020Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
229020Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
239020Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
249020Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
259020Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
269020Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
279020Sgblack@eecs.umich.edu * this software without specific prior written permission.
289020Sgblack@eecs.umich.edu *
299020Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
309020Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
319020Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
329020Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
339020Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
349024Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
359023Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
369022Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
379024Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
389020Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
399020Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
409020Sgblack@eecs.umich.edu *
419020Sgblack@eecs.umich.edu * Authors: Steve Reinhardt
4211165SRekai.GonzalezAlberquilla@arm.com *          Lisa Hsu
439022Sgblack@eecs.umich.edu *          Nathan Binkert
449022Sgblack@eecs.umich.edu *          Ali Saidi
459022Sgblack@eecs.umich.edu *          Rick Strong
469023Sgblack@eecs.umich.edu */
479023Sgblack@eecs.umich.edu
489023Sgblack@eecs.umich.edu#include "arch/isa_traits.hh"
499375Sgblack@eecs.umich.edu#include "arch/remote_gdb.hh"
509023Sgblack@eecs.umich.edu#include "arch/utility.hh"
519023Sgblack@eecs.umich.edu#include "base/loader/object_file.hh"
5211165SRekai.GonzalezAlberquilla@arm.com#include "base/loader/symtab.hh"
539023Sgblack@eecs.umich.edu#include "base/str.hh"
549023Sgblack@eecs.umich.edu#include "base/trace.hh"
559023Sgblack@eecs.umich.edu#include "config/the_isa.hh"
569023Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
579023Sgblack@eecs.umich.edu#include "debug/Loader.hh"
589023Sgblack@eecs.umich.edu#include "debug/WorkItems.hh"
599023Sgblack@eecs.umich.edu#include "kern/kernel_stats.hh"
609023Sgblack@eecs.umich.edu#include "mem/abstract_mem.hh"
619023Sgblack@eecs.umich.edu#include "mem/physical.hh"
629023Sgblack@eecs.umich.edu#include "params/System.hh"
639023Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
649023Sgblack@eecs.umich.edu#include "sim/debug.hh"
659023Sgblack@eecs.umich.edu#include "sim/full_system.hh"
669023Sgblack@eecs.umich.edu#include "sim/system.hh"
679023Sgblack@eecs.umich.edu
689023Sgblack@eecs.umich.eduusing namespace std;
699023Sgblack@eecs.umich.eduusing namespace TheISA;
709023Sgblack@eecs.umich.edu
719023Sgblack@eecs.umich.eduvector<System *> System::systemList;
729023Sgblack@eecs.umich.edu
739023Sgblack@eecs.umich.eduint System::numSystemsRunning = 0;
749023Sgblack@eecs.umich.edu
759375Sgblack@eecs.umich.eduSystem::System(Params *p)
769023Sgblack@eecs.umich.edu    : MemObject(p), _systemPort("system_port", this),
779023Sgblack@eecs.umich.edu      _numContexts(0),
789023Sgblack@eecs.umich.edu      pagePtr(0),
799023Sgblack@eecs.umich.edu      init_param(p->init_param),
809023Sgblack@eecs.umich.edu      physProxy(_systemPort, p->cache_line_size),
819023Sgblack@eecs.umich.edu      loadAddrMask(p->load_addr_mask),
829023Sgblack@eecs.umich.edu      loadAddrOffset(p->load_offset),
839023Sgblack@eecs.umich.edu      nextPID(0),
849023Sgblack@eecs.umich.edu      physmem(name() + ".physmem", p->memories),
859023Sgblack@eecs.umich.edu      memoryMode(p->mem_mode),
869023Sgblack@eecs.umich.edu      _cacheLineSize(p->cache_line_size),
879023Sgblack@eecs.umich.edu      workItemsBegin(0),
889023Sgblack@eecs.umich.edu      workItemsEnd(0),
899023Sgblack@eecs.umich.edu      numWorkIds(p->num_work_ids),
909023Sgblack@eecs.umich.edu      _params(p),
919023Sgblack@eecs.umich.edu      totalNumInsts(0),
929023Sgblack@eecs.umich.edu      instEventQueue("system instruction-based event queue")
939023Sgblack@eecs.umich.edu{
949023Sgblack@eecs.umich.edu    // add self to global system list
959375Sgblack@eecs.umich.edu    systemList.push_back(this);
969375Sgblack@eecs.umich.edu
979375Sgblack@eecs.umich.edu    if (FullSystem) {
989375Sgblack@eecs.umich.edu        kernelSymtab = new SymbolTable;
999375Sgblack@eecs.umich.edu        if (!debugSymbolTable)
1009375Sgblack@eecs.umich.edu            debugSymbolTable = new SymbolTable;
1019478Snilay@cs.wisc.edu    }
1029478Snilay@cs.wisc.edu
1039023Sgblack@eecs.umich.edu    // check if the cache line size is a value known to work
1049022Sgblack@eecs.umich.edu    if (!(_cacheLineSize == 16 || _cacheLineSize == 32 ||
1059024Sgblack@eecs.umich.edu          _cacheLineSize == 64 || _cacheLineSize == 128))
1069022Sgblack@eecs.umich.edu        warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n");
1079022Sgblack@eecs.umich.edu
1089022Sgblack@eecs.umich.edu    // Get the generic system master IDs
1099022Sgblack@eecs.umich.edu    MasterID tmp_id M5_VAR_USED;
1109022Sgblack@eecs.umich.edu    tmp_id = getMasterId("writebacks");
1119022Sgblack@eecs.umich.edu    assert(tmp_id == Request::wbMasterId);
1129022Sgblack@eecs.umich.edu    tmp_id = getMasterId("functional");
1139022Sgblack@eecs.umich.edu    assert(tmp_id == Request::funcMasterId);
1149022Sgblack@eecs.umich.edu    tmp_id = getMasterId("interrupt");
1159022Sgblack@eecs.umich.edu    assert(tmp_id == Request::intMasterId);
1169022Sgblack@eecs.umich.edu
1179022Sgblack@eecs.umich.edu    if (FullSystem) {
1189023Sgblack@eecs.umich.edu        if (params()->kernel == "") {
1199023Sgblack@eecs.umich.edu            inform("No kernel set for full system simulation. "
1209023Sgblack@eecs.umich.edu                   "Assuming you know what you're doing\n");
1219023Sgblack@eecs.umich.edu
1229023Sgblack@eecs.umich.edu            kernel = NULL;
1239023Sgblack@eecs.umich.edu        } else {
1249023Sgblack@eecs.umich.edu            // Get the kernel code
1259023Sgblack@eecs.umich.edu            kernel = createObjectFile(params()->kernel);
1269023Sgblack@eecs.umich.edu            inform("kernel located at: %s", params()->kernel);
1279022Sgblack@eecs.umich.edu
1289020Sgblack@eecs.umich.edu            if (kernel == NULL)
1299020Sgblack@eecs.umich.edu                fatal("Could not load kernel file %s", params()->kernel);
1309020Sgblack@eecs.umich.edu
1319020Sgblack@eecs.umich.edu            // setup entry points
132            kernelStart = kernel->textBase();
133            kernelEnd = kernel->bssBase() + kernel->bssSize();
134            kernelEntry = kernel->entryPoint();
135
136            // load symbols
137            if (!kernel->loadGlobalSymbols(kernelSymtab))
138                fatal("could not load kernel symbols\n");
139
140            if (!kernel->loadLocalSymbols(kernelSymtab))
141                fatal("could not load kernel local symbols\n");
142
143            if (!kernel->loadGlobalSymbols(debugSymbolTable))
144                fatal("could not load kernel symbols\n");
145
146            if (!kernel->loadLocalSymbols(debugSymbolTable))
147                fatal("could not load kernel local symbols\n");
148
149            // Loading only needs to happen once and after memory system is
150            // connected so it will happen in initState()
151        }
152    }
153
154    // increment the number of running systms
155    numSystemsRunning++;
156
157    // Set back pointers to the system in all memories
158    for (int x = 0; x < params()->memories.size(); x++)
159        params()->memories[x]->system(this);
160}
161
162System::~System()
163{
164    delete kernelSymtab;
165    delete kernel;
166
167    for (uint32_t j = 0; j < numWorkIds; j++)
168        delete workItemStats[j];
169}
170
171void
172System::init()
173{
174    // check that the system port is connected
175    if (!_systemPort.isConnected())
176        panic("System port on %s is not connected.\n", name());
177}
178
179BaseMasterPort&
180System::getMasterPort(const std::string &if_name, PortID idx)
181{
182    // no need to distinguish at the moment (besides checking)
183    return _systemPort;
184}
185
186void
187System::setMemoryMode(Enums::MemoryMode mode)
188{
189    assert(getDrainState() == Drainable::Drained);
190    memoryMode = mode;
191}
192
193bool System::breakpoint()
194{
195    if (remoteGDB.size())
196        return remoteGDB[0]->breakpoint();
197    return false;
198}
199
200/**
201 * Setting rgdb_wait to a positive integer waits for a remote debugger to
202 * connect to that context ID before continuing.  This should really
203   be a parameter on the CPU object or something...
204 */
205int rgdb_wait = -1;
206
207int
208System::registerThreadContext(ThreadContext *tc, int assigned)
209{
210    int id;
211    if (assigned == -1) {
212        for (id = 0; id < threadContexts.size(); id++) {
213            if (!threadContexts[id])
214                break;
215        }
216
217        if (threadContexts.size() <= id)
218            threadContexts.resize(id + 1);
219    } else {
220        if (threadContexts.size() <= assigned)
221            threadContexts.resize(assigned + 1);
222        id = assigned;
223    }
224
225    if (threadContexts[id])
226        fatal("Cannot have two CPUs with the same id (%d)\n", id);
227
228    threadContexts[id] = tc;
229    _numContexts++;
230
231#if THE_ISA != NULL_ISA
232    int port = getRemoteGDBPort();
233    if (port) {
234        RemoteGDB *rgdb = new RemoteGDB(this, tc);
235        GDBListener *gdbl = new GDBListener(rgdb, port + id);
236        gdbl->listen();
237
238        if (rgdb_wait != -1 && rgdb_wait == id)
239            gdbl->accept();
240
241        if (remoteGDB.size() <= id) {
242            remoteGDB.resize(id + 1);
243        }
244
245        remoteGDB[id] = rgdb;
246    }
247#endif
248
249    activeCpus.push_back(false);
250
251    return id;
252}
253
254int
255System::numRunningContexts()
256{
257    int running = 0;
258    for (int i = 0; i < _numContexts; ++i) {
259        if (threadContexts[i]->status() != ThreadContext::Halted)
260            ++running;
261    }
262    return running;
263}
264
265void
266System::initState()
267{
268    if (FullSystem) {
269        for (int i = 0; i < threadContexts.size(); i++)
270            TheISA::startupCPU(threadContexts[i], i);
271        // Moved from the constructor to here since it relies on the
272        // address map being resolved in the interconnect
273        /**
274         * Load the kernel code into memory
275         */
276        if (params()->kernel != "")  {
277            if (params()->kernel_addr_check) {
278                // Validate kernel mapping before loading binary
279                if (!(isMemAddr((kernelStart & loadAddrMask) +
280                                loadAddrOffset) &&
281                      isMemAddr((kernelEnd & loadAddrMask) +
282                                loadAddrOffset))) {
283                    fatal("Kernel is mapped to invalid location (not memory). "
284                          "kernelStart 0x(%x) - kernelEnd 0x(%x) %#x:%#x\n",
285                          kernelStart,
286                          kernelEnd, (kernelStart & loadAddrMask) +
287                          loadAddrOffset,
288                          (kernelEnd & loadAddrMask) + loadAddrOffset);
289                }
290            }
291            // Load program sections into memory
292            kernel->loadSections(physProxy, loadAddrMask, loadAddrOffset);
293
294            DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
295            DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
296            DPRINTF(Loader, "Kernel entry = %#x\n", kernelEntry);
297            DPRINTF(Loader, "Kernel loaded...\n");
298        }
299    }
300
301    activeCpus.clear();
302}
303
304void
305System::replaceThreadContext(ThreadContext *tc, int context_id)
306{
307    if (context_id >= threadContexts.size()) {
308        panic("replaceThreadContext: bad id, %d >= %d\n",
309              context_id, threadContexts.size());
310    }
311
312    threadContexts[context_id] = tc;
313    if (context_id < remoteGDB.size())
314        remoteGDB[context_id]->replaceThreadContext(tc);
315}
316
317Addr
318System::allocPhysPages(int npages)
319{
320    Addr return_addr = pagePtr << LogVMPageSize;
321    pagePtr += npages;
322    if ((pagePtr << LogVMPageSize) > physmem.totalSize())
323        fatal("Out of memory, please increase size of physical memory.");
324    return return_addr;
325}
326
327Addr
328System::memSize() const
329{
330    return physmem.totalSize();
331}
332
333Addr
334System::freeMemSize() const
335{
336   return physmem.totalSize() - (pagePtr << LogVMPageSize);
337}
338
339bool
340System::isMemAddr(Addr addr) const
341{
342    return physmem.isMemAddr(addr);
343}
344
345unsigned int
346System::drain(DrainManager *dm)
347{
348    setDrainState(Drainable::Drained);
349    return 0;
350}
351
352void
353System::drainResume()
354{
355    Drainable::drainResume();
356    totalNumInsts = 0;
357}
358
359void
360System::serialize(ostream &os)
361{
362    if (FullSystem)
363        kernelSymtab->serialize("kernel_symtab", os);
364    SERIALIZE_SCALAR(pagePtr);
365    SERIALIZE_SCALAR(nextPID);
366    serializeSymtab(os);
367
368    // also serialize the memories in the system
369    nameOut(os, csprintf("%s.physmem", name()));
370    physmem.serialize(os);
371}
372
373
374void
375System::unserialize(Checkpoint *cp, const string &section)
376{
377    if (FullSystem)
378        kernelSymtab->unserialize("kernel_symtab", cp, section);
379    UNSERIALIZE_SCALAR(pagePtr);
380    UNSERIALIZE_SCALAR(nextPID);
381    unserializeSymtab(cp, section);
382
383    // also unserialize the memories in the system
384    physmem.unserialize(cp, csprintf("%s.physmem", name()));
385}
386
387void
388System::regStats()
389{
390    for (uint32_t j = 0; j < numWorkIds ; j++) {
391        workItemStats[j] = new Stats::Histogram();
392        stringstream namestr;
393        ccprintf(namestr, "work_item_type%d", j);
394        workItemStats[j]->init(20)
395                         .name(name() + "." + namestr.str())
396                         .desc("Run time stat for" + namestr.str())
397                         .prereq(*workItemStats[j]);
398    }
399}
400
401void
402System::workItemEnd(uint32_t tid, uint32_t workid)
403{
404    std::pair<uint32_t,uint32_t> p(tid, workid);
405    if (!lastWorkItemStarted.count(p))
406        return;
407
408    Tick samp = curTick() - lastWorkItemStarted[p];
409    DPRINTF(WorkItems, "Work item end: %d\t%d\t%lld\n", tid, workid, samp);
410
411    if (workid >= numWorkIds)
412        fatal("Got workid greater than specified in system configuration\n");
413
414    workItemStats[workid]->sample(samp);
415    lastWorkItemStarted.erase(p);
416}
417
418void
419System::printSystems()
420{
421    vector<System *>::iterator i = systemList.begin();
422    vector<System *>::iterator end = systemList.end();
423    for (; i != end; ++i) {
424        System *sys = *i;
425        cerr << "System " << sys->name() << ": " << hex << sys << endl;
426    }
427}
428
429void
430printSystems()
431{
432    System::printSystems();
433}
434
435MasterID
436System::getMasterId(std::string master_name)
437{
438    // strip off system name if the string starts with it
439    if (startswith(master_name, name()))
440        master_name = master_name.erase(0, name().size() + 1);
441
442    // CPUs in switch_cpus ask for ids again after switching
443    for (int i = 0; i < masterIds.size(); i++) {
444        if (masterIds[i] == master_name) {
445            return i;
446        }
447    }
448
449    // Verify that the statistics haven't been enabled yet
450    // Otherwise objects will have sized their stat buckets and
451    // they will be too small
452
453    if (Stats::enabled())
454        fatal("Can't request a masterId after regStats(). \
455                You must do so in init().\n");
456
457    masterIds.push_back(master_name);
458
459    return masterIds.size() - 1;
460}
461
462std::string
463System::getMasterName(MasterID master_id)
464{
465    if (master_id >= masterIds.size())
466        fatal("Invalid master_id passed to getMasterName()\n");
467
468    return masterIds[master_id];
469}
470
471System *
472SystemParams::create()
473{
474    return new System(this);
475}
476