base.cc revision 10061
12SN/A/*
28922Swilliam.wang@arm.com * Copyright (c) 2011-2012 ARM Limited
38707Sandreas.hansson@arm.com * All rights reserved
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68707Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78707Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88707Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98707Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108707Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118707Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128707Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138707Sandreas.hansson@arm.com *
141762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
157897Shestness@cs.utexas.edu * Copyright (c) 2011 Regents of the University of California
169983Sstever@gmail.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
179983Sstever@gmail.com * Copyright (c) 2013 Mark D. Hill and David A. Wood
182SN/A * All rights reserved.
192SN/A *
202SN/A * Redistribution and use in source and binary forms, with or without
212SN/A * modification, are permitted provided that the following conditions are
222SN/A * met: redistributions of source code must retain the above copyright
232SN/A * notice, this list of conditions and the following disclaimer;
242SN/A * redistributions in binary form must reproduce the above copyright
252SN/A * notice, this list of conditions and the following disclaimer in the
262SN/A * documentation and/or other materials provided with the distribution;
272SN/A * neither the name of the copyright holders nor the names of its
282SN/A * contributors may be used to endorse or promote products derived from
292SN/A * this software without specific prior written permission.
302SN/A *
312SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
322SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
332SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
342SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
352SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
362SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
372SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
382SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
392SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
402SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
412SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
422665Ssaidi@eecs.umich.edu *
432665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
442665Ssaidi@eecs.umich.edu *          Nathan Binkert
457897Shestness@cs.utexas.edu *          Rick Strong
462SN/A */
472SN/A
481388SN/A#include <iostream>
498229Snate@binkert.org#include <sstream>
502SN/A#include <string>
512SN/A
527781SAli.Saidi@ARM.com#include "arch/tlb.hh"
538229Snate@binkert.org#include "base/loader/symtab.hh"
541191SN/A#include "base/cprintf.hh"
551191SN/A#include "base/misc.hh"
561388SN/A#include "base/output.hh"
575529Snate@binkert.org#include "base/trace.hh"
581717SN/A#include "cpu/base.hh"
598887Sgeoffrey.blake@arm.com#include "cpu/checker/cpu.hh"
602651Ssaidi@eecs.umich.edu#include "cpu/cpuevent.hh"
618229Snate@binkert.org#include "cpu/profile.hh"
622680Sktlim@umich.edu#include "cpu/thread_context.hh"
638232Snate@binkert.org#include "debug/SyscallVerbose.hh"
645529Snate@binkert.org#include "params/BaseCPU.hh"
658779Sgblack@eecs.umich.edu#include "sim/full_system.hh"
662190SN/A#include "sim/process.hh"
6756SN/A#include "sim/sim_events.hh"
688229Snate@binkert.org#include "sim/sim_exit.hh"
692190SN/A#include "sim/system.hh"
702SN/A
712359SN/A// Hack
722359SN/A#include "sim/stat_control.hh"
732359SN/A
742SN/Ausing namespace std;
752SN/A
762SN/Avector<BaseCPU *> BaseCPU::cpuList;
772SN/A
782SN/A// This variable reflects the max number of threads in any CPU.  Be
792SN/A// careful to only use it once all the CPUs that you care about have
802SN/A// been initialized
812SN/Aint maxThreadsPerCPU = 1;
822SN/A
835606Snate@binkert.orgCPUProgressEvent::CPUProgressEvent(BaseCPU *_cpu, Tick ival)
846144Sksewell@umich.edu    : Event(Event::Progress_Event_Pri), _interval(ival), lastNumInst(0),
856144Sksewell@umich.edu      cpu(_cpu), _repeatEvent(true)
863126Sktlim@umich.edu{
876144Sksewell@umich.edu    if (_interval)
887823Ssteve.reinhardt@amd.com        cpu->schedule(this, curTick() + _interval);
893126Sktlim@umich.edu}
903126Sktlim@umich.edu
912356SN/Avoid
922356SN/ACPUProgressEvent::process()
932356SN/A{
948834Satgutier@umich.edu    Counter temp = cpu->totalOps();
952356SN/A#ifndef NDEBUG
969179Sandreas.hansson@arm.com    double ipc = double(temp - lastNumInst) / (_interval / cpu->clockPeriod());
972367SN/A
986144Sksewell@umich.edu    DPRINTFN("%s progress event, total committed:%i, progress insts committed: "
996144Sksewell@umich.edu             "%lli, IPC: %0.8d\n", cpu->name(), temp, temp - lastNumInst,
1006144Sksewell@umich.edu             ipc);
1012356SN/A    ipc = 0.0;
1022367SN/A#else
1036144Sksewell@umich.edu    cprintf("%lli: %s progress event, total committed:%i, progress insts "
1047823Ssteve.reinhardt@amd.com            "committed: %lli\n", curTick(), cpu->name(), temp,
1056144Sksewell@umich.edu            temp - lastNumInst);
1062367SN/A#endif
1072356SN/A    lastNumInst = temp;
1086144Sksewell@umich.edu
1096144Sksewell@umich.edu    if (_repeatEvent)
1107823Ssteve.reinhardt@amd.com        cpu->schedule(this, curTick() + _interval);
1112356SN/A}
1122356SN/A
1132356SN/Aconst char *
1145336Shines@cs.fsu.eduCPUProgressEvent::description() const
1152356SN/A{
1164873Sstever@eecs.umich.edu    return "CPU Progress";
1172356SN/A}
1182356SN/A
1198876Sandreas.hansson@arm.comBaseCPU::BaseCPU(Params *p, bool is_checker)
1209157Sandreas.hansson@arm.com    : MemObject(p), instCnt(0), _cpuId(p->cpu_id),
1218832SAli.Saidi@ARM.com      _instMasterId(p->system->getMasterId(name() + ".inst")),
1228832SAli.Saidi@ARM.com      _dataMasterId(p->system->getMasterId(name() + ".data")),
1239332Sdam.sunwoo@arm.com      _taskId(ContextSwitchTaskId::Unknown), _pid(Request::invldPid),
1249814Sandreas.hansson@arm.com      _switchedOut(p->switched_out), _cacheLineSize(p->system->cacheLineSize()),
1259220Shestness@cs.wisc.edu      interrupts(p->interrupts), profileEvent(NULL),
1269157Sandreas.hansson@arm.com      numThreads(p->numThreads), system(p->system)
1272SN/A{
1285712Shsul@eecs.umich.edu    // if Python did not provide a valid ID, do it here
1295712Shsul@eecs.umich.edu    if (_cpuId == -1 ) {
1305712Shsul@eecs.umich.edu        _cpuId = cpuList.size();
1315712Shsul@eecs.umich.edu    }
1325712Shsul@eecs.umich.edu
1332SN/A    // add self to global list of CPUs
1342SN/A    cpuList.push_back(this);
1352SN/A
1365712Shsul@eecs.umich.edu    DPRINTF(SyscallVerbose, "Constructing CPU with id %d\n", _cpuId);
1375712Shsul@eecs.umich.edu
1386221Snate@binkert.org    if (numThreads > maxThreadsPerCPU)
1396221Snate@binkert.org        maxThreadsPerCPU = numThreads;
1402SN/A
1412SN/A    // allocate per-thread instruction-based event queues
1426221Snate@binkert.org    comInstEventQueue = new EventQueue *[numThreads];
1436221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid)
1446221Snate@binkert.org        comInstEventQueue[tid] =
1456221Snate@binkert.org            new EventQueue("instruction-based event queue");
1462SN/A
1472SN/A    //
1482SN/A    // set up instruction-count-based termination events, if any
1492SN/A    //
1505606Snate@binkert.org    if (p->max_insts_any_thread != 0) {
1515606Snate@binkert.org        const char *cause = "a thread reached the max instruction count";
1529749Sandreas@sandberg.pp.se        for (ThreadID tid = 0; tid < numThreads; ++tid)
1539749Sandreas@sandberg.pp.se            scheduleInstStop(tid, p->max_insts_any_thread, cause);
1545606Snate@binkert.org    }
1552SN/A
1569647Sdam.sunwoo@arm.com    // Set up instruction-count-based termination events for SimPoints
1579647Sdam.sunwoo@arm.com    // Typically, there are more than one action points.
1589647Sdam.sunwoo@arm.com    // Simulation.py is responsible to take the necessary actions upon
1599647Sdam.sunwoo@arm.com    // exitting the simulation loop.
1609647Sdam.sunwoo@arm.com    if (!p->simpoint_start_insts.empty()) {
1619647Sdam.sunwoo@arm.com        const char *cause = "simpoint starting point found";
1629749Sandreas@sandberg.pp.se        for (size_t i = 0; i < p->simpoint_start_insts.size(); ++i)
1639749Sandreas@sandberg.pp.se            scheduleInstStop(0, p->simpoint_start_insts[i], cause);
1649647Sdam.sunwoo@arm.com    }
1659647Sdam.sunwoo@arm.com
1661400SN/A    if (p->max_insts_all_threads != 0) {
1675606Snate@binkert.org        const char *cause = "all threads reached the max instruction count";
1685606Snate@binkert.org
1692SN/A        // allocate & initialize shared downcounter: each event will
1702SN/A        // decrement this when triggered; simulation will terminate
1712SN/A        // when counter reaches 0
1722SN/A        int *counter = new int;
1736221Snate@binkert.org        *counter = numThreads;
1746221Snate@binkert.org        for (ThreadID tid = 0; tid < numThreads; ++tid) {
1755606Snate@binkert.org            Event *event = new CountedExitEvent(cause, *counter);
1766670Shsul@eecs.umich.edu            comInstEventQueue[tid]->schedule(event, p->max_insts_all_threads);
1775606Snate@binkert.org        }
1782SN/A    }
1792SN/A
180124SN/A    // allocate per-thread load-based event queues
1816221Snate@binkert.org    comLoadEventQueue = new EventQueue *[numThreads];
1826221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; ++tid)
1836221Snate@binkert.org        comLoadEventQueue[tid] = new EventQueue("load-based event queue");
184124SN/A
185124SN/A    //
186124SN/A    // set up instruction-count-based termination events, if any
187124SN/A    //
1885606Snate@binkert.org    if (p->max_loads_any_thread != 0) {
1895606Snate@binkert.org        const char *cause = "a thread reached the max load count";
1909749Sandreas@sandberg.pp.se        for (ThreadID tid = 0; tid < numThreads; ++tid)
1919749Sandreas@sandberg.pp.se            scheduleLoadStop(tid, p->max_loads_any_thread, cause);
1925606Snate@binkert.org    }
193124SN/A
1941400SN/A    if (p->max_loads_all_threads != 0) {
1955606Snate@binkert.org        const char *cause = "all threads reached the max load count";
196124SN/A        // allocate & initialize shared downcounter: each event will
197124SN/A        // decrement this when triggered; simulation will terminate
198124SN/A        // when counter reaches 0
199124SN/A        int *counter = new int;
2006221Snate@binkert.org        *counter = numThreads;
2016221Snate@binkert.org        for (ThreadID tid = 0; tid < numThreads; ++tid) {
2025606Snate@binkert.org            Event *event = new CountedExitEvent(cause, *counter);
2036221Snate@binkert.org            comLoadEventQueue[tid]->schedule(event, p->max_loads_all_threads);
2045606Snate@binkert.org        }
205124SN/A    }
206124SN/A
2071191SN/A    functionTracingEnabled = false;
2085529Snate@binkert.org    if (p->function_trace) {
2098634Schris.emmons@arm.com        const string fname = csprintf("ftrace.%s", name());
2108634Schris.emmons@arm.com        functionTraceStream = simout.find(fname);
2118634Schris.emmons@arm.com        if (!functionTraceStream)
2128634Schris.emmons@arm.com            functionTraceStream = simout.create(fname);
2138634Schris.emmons@arm.com
2141191SN/A        currentFunctionStart = currentFunctionEnd = 0;
2155529Snate@binkert.org        functionEntryTick = p->function_trace_start;
2161191SN/A
2175529Snate@binkert.org        if (p->function_trace_start == 0) {
2181191SN/A            functionTracingEnabled = true;
2191191SN/A        } else {
2205606Snate@binkert.org            typedef EventWrapper<BaseCPU, &BaseCPU::enableFunctionTrace> wrap;
2215606Snate@binkert.org            Event *event = new wrap(this, true);
2225606Snate@binkert.org            schedule(event, p->function_trace_start);
2231191SN/A        }
2241191SN/A    }
2258876Sandreas.hansson@arm.com
2268876Sandreas.hansson@arm.com    // The interrupts should always be present unless this CPU is
2278876Sandreas.hansson@arm.com    // switched in later or in case it is a checker CPU
2289433SAndreas.Sandberg@ARM.com    if (!params()->switched_out && !is_checker) {
2298876Sandreas.hansson@arm.com        if (interrupts) {
2308876Sandreas.hansson@arm.com            interrupts->setCPU(this);
2318876Sandreas.hansson@arm.com        } else {
2328876Sandreas.hansson@arm.com            fatal("CPU %s has no interrupt controller.\n"
2338876Sandreas.hansson@arm.com                  "Ensure createInterruptController() is called.\n", name());
2348876Sandreas.hansson@arm.com        }
2358876Sandreas.hansson@arm.com    }
2365810Sgblack@eecs.umich.edu
2378779Sgblack@eecs.umich.edu    if (FullSystem) {
2388779Sgblack@eecs.umich.edu        if (params()->profile)
2398779Sgblack@eecs.umich.edu            profileEvent = new ProfileEvent(this, params()->profile);
2408779Sgblack@eecs.umich.edu    }
2415529Snate@binkert.org    tracer = params()->tracer;
2429384SAndreas.Sandberg@arm.com
2439384SAndreas.Sandberg@arm.com    if (params()->isa.size() != numThreads) {
2449384SAndreas.Sandberg@arm.com        fatal("Number of ISAs (%i) assigned to the CPU does not equal number "
2459384SAndreas.Sandberg@arm.com              "of threads (%i).\n", params()->isa.size(), numThreads);
2469384SAndreas.Sandberg@arm.com    }
2471917SN/A}
2481191SN/A
2491191SN/Avoid
2501191SN/ABaseCPU::enableFunctionTrace()
2511191SN/A{
2521191SN/A    functionTracingEnabled = true;
2531191SN/A}
2541191SN/A
2551191SN/ABaseCPU::~BaseCPU()
2561191SN/A{
2579086Sandreas.hansson@arm.com    delete profileEvent;
2589086Sandreas.hansson@arm.com    delete[] comLoadEventQueue;
2599086Sandreas.hansson@arm.com    delete[] comInstEventQueue;
2601191SN/A}
2611191SN/A
2621129SN/Avoid
2631129SN/ABaseCPU::init()
2641129SN/A{
2659523SAndreas.Sandberg@ARM.com    if (!params()->switched_out) {
2662680Sktlim@umich.edu        registerThreadContexts();
2679523SAndreas.Sandberg@ARM.com
2689523SAndreas.Sandberg@ARM.com        verifyMemoryMode();
2699523SAndreas.Sandberg@ARM.com    }
2701129SN/A}
271180SN/A
2722SN/Avoid
2731917SN/ABaseCPU::startup()
2741917SN/A{
2758779Sgblack@eecs.umich.edu    if (FullSystem) {
2769433SAndreas.Sandberg@ARM.com        if (!params()->switched_out && profileEvent)
2778779Sgblack@eecs.umich.edu            schedule(profileEvent, curTick());
2788779Sgblack@eecs.umich.edu    }
2792356SN/A
2805529Snate@binkert.org    if (params()->progress_interval) {
2819179Sandreas.hansson@arm.com        new CPUProgressEvent(this, params()->progress_interval);
2822356SN/A    }
2831917SN/A}
2841917SN/A
2851917SN/A
2861917SN/Avoid
2872SN/ABaseCPU::regStats()
2882SN/A{
289729SN/A    using namespace Stats;
290707SN/A
291707SN/A    numCycles
292707SN/A        .name(name() + ".numCycles")
293707SN/A        .desc("number of cpu cycles simulated")
294707SN/A        ;
295707SN/A
2967914SBrad.Beckmann@amd.com    numWorkItemsStarted
2977914SBrad.Beckmann@amd.com        .name(name() + ".numWorkItemsStarted")
2987914SBrad.Beckmann@amd.com        .desc("number of work items this cpu started")
2997914SBrad.Beckmann@amd.com        ;
3007914SBrad.Beckmann@amd.com
3017914SBrad.Beckmann@amd.com    numWorkItemsCompleted
3027914SBrad.Beckmann@amd.com        .name(name() + ".numWorkItemsCompleted")
3037914SBrad.Beckmann@amd.com        .desc("number of work items this cpu completed")
3047914SBrad.Beckmann@amd.com        ;
3057914SBrad.Beckmann@amd.com
3062680Sktlim@umich.edu    int size = threadContexts.size();
3072SN/A    if (size > 1) {
3082SN/A        for (int i = 0; i < size; ++i) {
3092SN/A            stringstream namestr;
3102SN/A            ccprintf(namestr, "%s.ctx%d", name(), i);
3112680Sktlim@umich.edu            threadContexts[i]->regStats(namestr.str());
3122SN/A        }
3132SN/A    } else if (size == 1)
3142680Sktlim@umich.edu        threadContexts[0]->regStats(name());
3152SN/A}
3162SN/A
3179294Sandreas.hansson@arm.comBaseMasterPort &
3189294Sandreas.hansson@arm.comBaseCPU::getMasterPort(const string &if_name, PortID idx)
3198850Sandreas.hansson@arm.com{
3208850Sandreas.hansson@arm.com    // Get the right port based on name. This applies to all the
3218850Sandreas.hansson@arm.com    // subclasses of the base CPU and relies on their implementation
3228850Sandreas.hansson@arm.com    // of getDataPort and getInstPort. In all cases there methods
3239608Sandreas.hansson@arm.com    // return a MasterPort pointer.
3248850Sandreas.hansson@arm.com    if (if_name == "dcache_port")
3258922Swilliam.wang@arm.com        return getDataPort();
3268850Sandreas.hansson@arm.com    else if (if_name == "icache_port")
3278922Swilliam.wang@arm.com        return getInstPort();
3288850Sandreas.hansson@arm.com    else
3298922Swilliam.wang@arm.com        return MemObject::getMasterPort(if_name, idx);
3308850Sandreas.hansson@arm.com}
3318850Sandreas.hansson@arm.com
332180SN/Avoid
3332680Sktlim@umich.eduBaseCPU::registerThreadContexts()
334180SN/A{
3356221Snate@binkert.org    ThreadID size = threadContexts.size();
3366221Snate@binkert.org    for (ThreadID tid = 0; tid < size; ++tid) {
3376221Snate@binkert.org        ThreadContext *tc = threadContexts[tid];
3382378SN/A
3395718Shsul@eecs.umich.edu        /** This is so that contextId and cpuId match where there is a
3405718Shsul@eecs.umich.edu         * 1cpu:1context relationship.  Otherwise, the order of registration
3415718Shsul@eecs.umich.edu         * could affect the assignment and cpu 1 could have context id 3, for
3425718Shsul@eecs.umich.edu         * example.  We may even want to do something like this for SMT so that
3435718Shsul@eecs.umich.edu         * cpu 0 has the lowest thread contexts and cpu N has the highest, but
3445718Shsul@eecs.umich.edu         * I'll just do this for now
3455718Shsul@eecs.umich.edu         */
3466221Snate@binkert.org        if (numThreads == 1)
3475718Shsul@eecs.umich.edu            tc->setContextId(system->registerThreadContext(tc, _cpuId));
3485718Shsul@eecs.umich.edu        else
3495718Shsul@eecs.umich.edu            tc->setContextId(system->registerThreadContext(tc));
3508779Sgblack@eecs.umich.edu
3518779Sgblack@eecs.umich.edu        if (!FullSystem)
3528779Sgblack@eecs.umich.edu            tc->getProcessPtr()->assignThreadContext(tc->contextId());
353180SN/A    }
354180SN/A}
355180SN/A
356180SN/A
3574000Ssaidi@eecs.umich.eduint
3584000Ssaidi@eecs.umich.eduBaseCPU::findContext(ThreadContext *tc)
3594000Ssaidi@eecs.umich.edu{
3606221Snate@binkert.org    ThreadID size = threadContexts.size();
3616221Snate@binkert.org    for (ThreadID tid = 0; tid < size; ++tid) {
3626221Snate@binkert.org        if (tc == threadContexts[tid])
3636221Snate@binkert.org            return tid;
3644000Ssaidi@eecs.umich.edu    }
3654000Ssaidi@eecs.umich.edu    return 0;
3664000Ssaidi@eecs.umich.edu}
3674000Ssaidi@eecs.umich.edu
368180SN/Avoid
3692798Sktlim@umich.eduBaseCPU::switchOut()
370180SN/A{
3719430SAndreas.Sandberg@ARM.com    assert(!_switchedOut);
3729430SAndreas.Sandberg@ARM.com    _switchedOut = true;
3732359SN/A    if (profileEvent && profileEvent->scheduled())
3745606Snate@binkert.org        deschedule(profileEvent);
3759446SAndreas.Sandberg@ARM.com
3769446SAndreas.Sandberg@ARM.com    // Flush all TLBs in the CPU to avoid having stale translations if
3779446SAndreas.Sandberg@ARM.com    // it gets switched in later.
3789446SAndreas.Sandberg@ARM.com    flushTLBs();
379180SN/A}
380180SN/A
381180SN/Avoid
3828737Skoansin.tan@gmail.comBaseCPU::takeOverFrom(BaseCPU *oldCPU)
383180SN/A{
3842680Sktlim@umich.edu    assert(threadContexts.size() == oldCPU->threadContexts.size());
3859152Satgutier@umich.edu    assert(_cpuId == oldCPU->cpuId());
3869430SAndreas.Sandberg@ARM.com    assert(_switchedOut);
3879430SAndreas.Sandberg@ARM.com    assert(oldCPU != this);
3889332Sdam.sunwoo@arm.com    _pid = oldCPU->getPid();
3899332Sdam.sunwoo@arm.com    _taskId = oldCPU->taskId();
3909430SAndreas.Sandberg@ARM.com    _switchedOut = false;
3915712Shsul@eecs.umich.edu
3926221Snate@binkert.org    ThreadID size = threadContexts.size();
3936221Snate@binkert.org    for (ThreadID i = 0; i < size; ++i) {
3942680Sktlim@umich.edu        ThreadContext *newTC = threadContexts[i];
3952680Sktlim@umich.edu        ThreadContext *oldTC = oldCPU->threadContexts[i];
396180SN/A
3972680Sktlim@umich.edu        newTC->takeOverFrom(oldTC);
3982651Ssaidi@eecs.umich.edu
3992680Sktlim@umich.edu        CpuEvent::replaceThreadContext(oldTC, newTC);
4002651Ssaidi@eecs.umich.edu
4015714Shsul@eecs.umich.edu        assert(newTC->contextId() == oldTC->contextId());
4025715Shsul@eecs.umich.edu        assert(newTC->threadId() == oldTC->threadId());
4035714Shsul@eecs.umich.edu        system->replaceThreadContext(newTC, newTC->contextId());
4042359SN/A
4055875Ssteve.reinhardt@amd.com        /* This code no longer works since the zero register (e.g.,
4065875Ssteve.reinhardt@amd.com         * r31 on Alpha) doesn't necessarily contain zero at this
4075875Ssteve.reinhardt@amd.com         * point.
4085875Ssteve.reinhardt@amd.com           if (DTRACE(Context))
4095217Ssaidi@eecs.umich.edu            ThreadContext::compare(oldTC, newTC);
4105875Ssteve.reinhardt@amd.com        */
4117781SAli.Saidi@ARM.com
4129294Sandreas.hansson@arm.com        BaseMasterPort *old_itb_port = oldTC->getITBPtr()->getMasterPort();
4139294Sandreas.hansson@arm.com        BaseMasterPort *old_dtb_port = oldTC->getDTBPtr()->getMasterPort();
4149294Sandreas.hansson@arm.com        BaseMasterPort *new_itb_port = newTC->getITBPtr()->getMasterPort();
4159294Sandreas.hansson@arm.com        BaseMasterPort *new_dtb_port = newTC->getDTBPtr()->getMasterPort();
4167781SAli.Saidi@ARM.com
4177781SAli.Saidi@ARM.com        // Move over any table walker ports if they exist
4189178Sandreas.hansson@arm.com        if (new_itb_port) {
4199178Sandreas.hansson@arm.com            assert(!new_itb_port->isConnected());
4207781SAli.Saidi@ARM.com            assert(old_itb_port);
4219178Sandreas.hansson@arm.com            assert(old_itb_port->isConnected());
4229294Sandreas.hansson@arm.com            BaseSlavePort &slavePort = old_itb_port->getSlavePort();
4239178Sandreas.hansson@arm.com            old_itb_port->unbind();
4248922Swilliam.wang@arm.com            new_itb_port->bind(slavePort);
4257781SAli.Saidi@ARM.com        }
4269178Sandreas.hansson@arm.com        if (new_dtb_port) {
4279178Sandreas.hansson@arm.com            assert(!new_dtb_port->isConnected());
4287781SAli.Saidi@ARM.com            assert(old_dtb_port);
4299178Sandreas.hansson@arm.com            assert(old_dtb_port->isConnected());
4309294Sandreas.hansson@arm.com            BaseSlavePort &slavePort = old_dtb_port->getSlavePort();
4319178Sandreas.hansson@arm.com            old_dtb_port->unbind();
4328922Swilliam.wang@arm.com            new_dtb_port->bind(slavePort);
4337781SAli.Saidi@ARM.com        }
4348733Sgeoffrey.blake@arm.com
4358887Sgeoffrey.blake@arm.com        // Checker whether or not we have to transfer CheckerCPU
4368887Sgeoffrey.blake@arm.com        // objects over in the switch
4378887Sgeoffrey.blake@arm.com        CheckerCPU *oldChecker = oldTC->getCheckerCpuPtr();
4388887Sgeoffrey.blake@arm.com        CheckerCPU *newChecker = newTC->getCheckerCpuPtr();
4398887Sgeoffrey.blake@arm.com        if (oldChecker && newChecker) {
4409294Sandreas.hansson@arm.com            BaseMasterPort *old_checker_itb_port =
4418922Swilliam.wang@arm.com                oldChecker->getITBPtr()->getMasterPort();
4429294Sandreas.hansson@arm.com            BaseMasterPort *old_checker_dtb_port =
4438922Swilliam.wang@arm.com                oldChecker->getDTBPtr()->getMasterPort();
4449294Sandreas.hansson@arm.com            BaseMasterPort *new_checker_itb_port =
4458922Swilliam.wang@arm.com                newChecker->getITBPtr()->getMasterPort();
4469294Sandreas.hansson@arm.com            BaseMasterPort *new_checker_dtb_port =
4478922Swilliam.wang@arm.com                newChecker->getDTBPtr()->getMasterPort();
4488733Sgeoffrey.blake@arm.com
4498887Sgeoffrey.blake@arm.com            // Move over any table walker ports if they exist for checker
4509178Sandreas.hansson@arm.com            if (new_checker_itb_port) {
4519178Sandreas.hansson@arm.com                assert(!new_checker_itb_port->isConnected());
4528887Sgeoffrey.blake@arm.com                assert(old_checker_itb_port);
4539178Sandreas.hansson@arm.com                assert(old_checker_itb_port->isConnected());
4549294Sandreas.hansson@arm.com                BaseSlavePort &slavePort =
4559294Sandreas.hansson@arm.com                    old_checker_itb_port->getSlavePort();
4569178Sandreas.hansson@arm.com                old_checker_itb_port->unbind();
4578922Swilliam.wang@arm.com                new_checker_itb_port->bind(slavePort);
4588887Sgeoffrey.blake@arm.com            }
4599178Sandreas.hansson@arm.com            if (new_checker_dtb_port) {
4609178Sandreas.hansson@arm.com                assert(!new_checker_dtb_port->isConnected());
4618887Sgeoffrey.blake@arm.com                assert(old_checker_dtb_port);
4629178Sandreas.hansson@arm.com                assert(old_checker_dtb_port->isConnected());
4639294Sandreas.hansson@arm.com                BaseSlavePort &slavePort =
4649294Sandreas.hansson@arm.com                    old_checker_dtb_port->getSlavePort();
4659178Sandreas.hansson@arm.com                old_checker_dtb_port->unbind();
4668922Swilliam.wang@arm.com                new_checker_dtb_port->bind(slavePort);
4678887Sgeoffrey.blake@arm.com            }
4688733Sgeoffrey.blake@arm.com        }
469180SN/A    }
470605SN/A
4713520Sgblack@eecs.umich.edu    interrupts = oldCPU->interrupts;
4725810Sgblack@eecs.umich.edu    interrupts->setCPU(this);
4739152Satgutier@umich.edu    oldCPU->interrupts = NULL;
4742254SN/A
4758779Sgblack@eecs.umich.edu    if (FullSystem) {
4768779Sgblack@eecs.umich.edu        for (ThreadID i = 0; i < size; ++i)
4778779Sgblack@eecs.umich.edu            threadContexts[i]->profileClear();
4782254SN/A
4798779Sgblack@eecs.umich.edu        if (profileEvent)
4808779Sgblack@eecs.umich.edu            schedule(profileEvent, curTick());
4818779Sgblack@eecs.umich.edu    }
4824192Sktlim@umich.edu
4839178Sandreas.hansson@arm.com    // All CPUs have an instruction and a data port, and the new CPU's
4849178Sandreas.hansson@arm.com    // ports are dangling while the old CPU has its ports connected
4859178Sandreas.hansson@arm.com    // already. Unbind the old CPU and then bind the ports of the one
4869178Sandreas.hansson@arm.com    // we are switching to.
4879178Sandreas.hansson@arm.com    assert(!getInstPort().isConnected());
4889178Sandreas.hansson@arm.com    assert(oldCPU->getInstPort().isConnected());
4899294Sandreas.hansson@arm.com    BaseSlavePort &inst_peer_port = oldCPU->getInstPort().getSlavePort();
4909178Sandreas.hansson@arm.com    oldCPU->getInstPort().unbind();
4919178Sandreas.hansson@arm.com    getInstPort().bind(inst_peer_port);
4924192Sktlim@umich.edu
4939178Sandreas.hansson@arm.com    assert(!getDataPort().isConnected());
4949178Sandreas.hansson@arm.com    assert(oldCPU->getDataPort().isConnected());
4959294Sandreas.hansson@arm.com    BaseSlavePort &data_peer_port = oldCPU->getDataPort().getSlavePort();
4969178Sandreas.hansson@arm.com    oldCPU->getDataPort().unbind();
4979178Sandreas.hansson@arm.com    getDataPort().bind(data_peer_port);
498180SN/A}
499180SN/A
5009446SAndreas.Sandberg@ARM.comvoid
5019446SAndreas.Sandberg@ARM.comBaseCPU::flushTLBs()
5029446SAndreas.Sandberg@ARM.com{
5039446SAndreas.Sandberg@ARM.com    for (ThreadID i = 0; i < threadContexts.size(); ++i) {
5049446SAndreas.Sandberg@ARM.com        ThreadContext &tc(*threadContexts[i]);
5059446SAndreas.Sandberg@ARM.com        CheckerCPU *checker(tc.getCheckerCpuPtr());
5069446SAndreas.Sandberg@ARM.com
5079446SAndreas.Sandberg@ARM.com        tc.getITBPtr()->flushAll();
5089446SAndreas.Sandberg@ARM.com        tc.getDTBPtr()->flushAll();
5099446SAndreas.Sandberg@ARM.com        if (checker) {
5109446SAndreas.Sandberg@ARM.com            checker->getITBPtr()->flushAll();
5119446SAndreas.Sandberg@ARM.com            checker->getDTBPtr()->flushAll();
5129446SAndreas.Sandberg@ARM.com        }
5139446SAndreas.Sandberg@ARM.com    }
5149446SAndreas.Sandberg@ARM.com}
5159446SAndreas.Sandberg@ARM.com
516180SN/A
5175536Srstrong@hp.comBaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, Tick _interval)
5185606Snate@binkert.org    : cpu(_cpu), interval(_interval)
5191917SN/A{ }
5201917SN/A
5211917SN/Avoid
5221917SN/ABaseCPU::ProfileEvent::process()
5231917SN/A{
5246221Snate@binkert.org    ThreadID size = cpu->threadContexts.size();
5256221Snate@binkert.org    for (ThreadID i = 0; i < size; ++i) {
5262680Sktlim@umich.edu        ThreadContext *tc = cpu->threadContexts[i];
5272680Sktlim@umich.edu        tc->profileSample();
5281917SN/A    }
5292254SN/A
5307823Ssteve.reinhardt@amd.com    cpu->schedule(this, curTick() + interval);
5311917SN/A}
5321917SN/A
5332SN/Avoid
534921SN/ABaseCPU::serialize(std::ostream &os)
535921SN/A{
5364000Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(instCnt);
5379332Sdam.sunwoo@arm.com
5389448SAndreas.Sandberg@ARM.com    if (!_switchedOut) {
5399448SAndreas.Sandberg@ARM.com        /* Unlike _pid, _taskId is not serialized, as they are dynamically
5409448SAndreas.Sandberg@ARM.com         * assigned unique ids that are only meaningful for the duration of
5419448SAndreas.Sandberg@ARM.com         * a specific run. We will need to serialize the entire taskMap in
5429448SAndreas.Sandberg@ARM.com         * system. */
5439448SAndreas.Sandberg@ARM.com        SERIALIZE_SCALAR(_pid);
5449332Sdam.sunwoo@arm.com
5459448SAndreas.Sandberg@ARM.com        interrupts->serialize(os);
5469448SAndreas.Sandberg@ARM.com
5479448SAndreas.Sandberg@ARM.com        // Serialize the threads, this is done by the CPU implementation.
5489448SAndreas.Sandberg@ARM.com        for (ThreadID i = 0; i < numThreads; ++i) {
5499448SAndreas.Sandberg@ARM.com            nameOut(os, csprintf("%s.xc.%i", name(), i));
5509448SAndreas.Sandberg@ARM.com            serializeThread(os, i);
5519448SAndreas.Sandberg@ARM.com        }
5529448SAndreas.Sandberg@ARM.com    }
553921SN/A}
554921SN/A
555921SN/Avoid
556921SN/ABaseCPU::unserialize(Checkpoint *cp, const std::string &section)
557921SN/A{
5584000Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(instCnt);
5599448SAndreas.Sandberg@ARM.com
5609448SAndreas.Sandberg@ARM.com    if (!_switchedOut) {
5619448SAndreas.Sandberg@ARM.com        UNSERIALIZE_SCALAR(_pid);
5629448SAndreas.Sandberg@ARM.com        interrupts->unserialize(cp, section);
5639448SAndreas.Sandberg@ARM.com
5649448SAndreas.Sandberg@ARM.com        // Unserialize the threads, this is done by the CPU implementation.
5659448SAndreas.Sandberg@ARM.com        for (ThreadID i = 0; i < numThreads; ++i)
5669448SAndreas.Sandberg@ARM.com            unserializeThread(cp, csprintf("%s.xc.%i", section, i), i);
5679448SAndreas.Sandberg@ARM.com    }
568921SN/A}
569921SN/A
5701191SN/Avoid
5719749Sandreas@sandberg.pp.seBaseCPU::scheduleInstStop(ThreadID tid, Counter insts, const char *cause)
5729749Sandreas@sandberg.pp.se{
5739749Sandreas@sandberg.pp.se    const Tick now(comInstEventQueue[tid]->getCurTick());
5749983Sstever@gmail.com    Event *event(new LocalSimLoopExitEvent(cause, 0));
5759749Sandreas@sandberg.pp.se
5769749Sandreas@sandberg.pp.se    comInstEventQueue[tid]->schedule(event, now + insts);
5779749Sandreas@sandberg.pp.se}
5789749Sandreas@sandberg.pp.se
5799749Sandreas@sandberg.pp.sevoid
5809749Sandreas@sandberg.pp.seBaseCPU::scheduleLoadStop(ThreadID tid, Counter loads, const char *cause)
5819749Sandreas@sandberg.pp.se{
5829749Sandreas@sandberg.pp.se    const Tick now(comLoadEventQueue[tid]->getCurTick());
5839983Sstever@gmail.com    Event *event(new LocalSimLoopExitEvent(cause, 0));
5849749Sandreas@sandberg.pp.se
5859749Sandreas@sandberg.pp.se    comLoadEventQueue[tid]->schedule(event, now + loads);
5869749Sandreas@sandberg.pp.se}
5879749Sandreas@sandberg.pp.se
5889749Sandreas@sandberg.pp.se
5899749Sandreas@sandberg.pp.sevoid
5901191SN/ABaseCPU::traceFunctionsInternal(Addr pc)
5911191SN/A{
5921191SN/A    if (!debugSymbolTable)
5931191SN/A        return;
5941191SN/A
5951191SN/A    // if pc enters different function, print new function symbol and
5961191SN/A    // update saved range.  Otherwise do nothing.
5971191SN/A    if (pc < currentFunctionStart || pc >= currentFunctionEnd) {
5981191SN/A        string sym_str;
5991191SN/A        bool found = debugSymbolTable->findNearestSymbol(pc, sym_str,
6001191SN/A                                                         currentFunctionStart,
6011191SN/A                                                         currentFunctionEnd);
6021191SN/A
6031191SN/A        if (!found) {
6041191SN/A            // no symbol found: use addr as label
6051191SN/A            sym_str = csprintf("0x%x", pc);
6061191SN/A            currentFunctionStart = pc;
6071191SN/A            currentFunctionEnd = pc + 1;
6081191SN/A        }
6091191SN/A
6101191SN/A        ccprintf(*functionTraceStream, " (%d)\n%d: %s",
6117823Ssteve.reinhardt@amd.com                 curTick() - functionEntryTick, curTick(), sym_str);
6127823Ssteve.reinhardt@amd.com        functionEntryTick = curTick();
6131191SN/A    }
6141191SN/A}
615