simple_thread.cc revision 9428
12SN/A/*
22188SN/A * Copyright (c) 2001-2006 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Steve Reinhardt
292665SN/A *          Nathan Binkert
302665SN/A *          Lisa Hsu
312665SN/A *          Kevin Lim
322SN/A */
332SN/A
342SN/A#include <string>
352SN/A
362465SN/A#include "arch/isa_traits.hh"
373565Sgblack@eecs.umich.edu#include "arch/kernel_stats.hh"
385529Snate@binkert.org#include "arch/stacktrace.hh"
398777Sgblack@eecs.umich.edu#include "arch/utility.hh"
401917SN/A#include "base/callback.hh"
411070SN/A#include "base/cprintf.hh"
421917SN/A#include "base/output.hh"
432188SN/A#include "base/trace.hh"
448777Sgblack@eecs.umich.edu#include "config/the_isa.hh"
458777Sgblack@eecs.umich.edu#include "cpu/base.hh"
461917SN/A#include "cpu/profile.hh"
472290SN/A#include "cpu/quiesce_event.hh"
488777Sgblack@eecs.umich.edu#include "cpu/simple_thread.hh"
498777Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
508706Sandreas.hansson@arm.com#include "mem/fs_translating_port_proxy.hh"
518799Sgblack@eecs.umich.edu#include "mem/se_translating_port_proxy.hh"
528809Sgblack@eecs.umich.edu#include "params/BaseCPU.hh"
538793Sgblack@eecs.umich.edu#include "sim/full_system.hh"
548777Sgblack@eecs.umich.edu#include "sim/process.hh"
551070SN/A#include "sim/serialize.hh"
561917SN/A#include "sim/sim_exit.hh"
572519SN/A#include "sim/system.hh"
582SN/A
592SN/Ausing namespace std;
602SN/A
612SN/A// constructor
628820Sgblack@eecs.umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
638820Sgblack@eecs.umich.edu                           Process *_process, TheISA::TLB *_itb,
649384SAndreas.Sandberg@arm.com                           TheISA::TLB *_dtb, TheISA::ISA *_isa)
659384SAndreas.Sandberg@arm.com    : ThreadState(_cpu, _thread_num, _process), isa(_isa), system(_sys),
669384SAndreas.Sandberg@arm.com      itb(_itb), dtb(_dtb)
678766Sgblack@eecs.umich.edu{
688766Sgblack@eecs.umich.edu    clearArchRegs();
698766Sgblack@eecs.umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
708766Sgblack@eecs.umich.edu}
719377Sgblack@eecs.umich.edu
722683Sktlim@umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
736022Sgblack@eecs.umich.edu                           TheISA::TLB *_itb, TheISA::TLB *_dtb,
749384SAndreas.Sandberg@arm.com                           TheISA::ISA *_isa, bool use_kernel_stats)
759384SAndreas.Sandberg@arm.com    : ThreadState(_cpu, _thread_num, NULL), isa(_isa), system(_sys), itb(_itb),
769384SAndreas.Sandberg@arm.com      dtb(_dtb)
772SN/A{
782683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
792190SN/A
802680SN/A    quiesceEvent = new EndQuiesceEvent(tc);
812290SN/A
826316Sgblack@eecs.umich.edu    clearArchRegs();
831917SN/A
848735Sandreas.hanson@arm.com    if (baseCpu->params()->profile) {
851982SN/A        profile = new FunctionProfile(system->kernelSymtab);
861917SN/A        Callback *cb =
872683Sktlim@umich.edu            new MakeCallback<SimpleThread,
882683Sktlim@umich.edu            &SimpleThread::dumpFuncProfile>(this);
891917SN/A        registerExitCallback(cb);
901917SN/A    }
911917SN/A
921917SN/A    // let's fill with a dummy node for now so we don't get a segfault
931917SN/A    // on the first cycle when there's no node available.
941917SN/A    static ProfileNode dummyNode;
951917SN/A    profileNode = &dummyNode;
961917SN/A    profilePC = 3;
972521SN/A
985482Snate@binkert.org    if (use_kernel_stats)
993548Sgblack@eecs.umich.edu        kernelStats = new TheISA::Kernel::Statistics(system);
1002SN/A}
1012862Sktlim@umich.edu
1022683Sktlim@umich.eduSimpleThread::~SimpleThread()
1031070SN/A{
1042680SN/A    delete tc;
1051070SN/A}
1061070SN/A
1071917SN/Avoid
1082683Sktlim@umich.eduSimpleThread::takeOverFrom(ThreadContext *oldContext)
109180SN/A{
110180SN/A    // some things should already be set up
1118793Sgblack@eecs.umich.edu    if (FullSystem)
1128793Sgblack@eecs.umich.edu        assert(system == oldContext->getSystemPtr());
1132235SN/A    assert(process == oldContext->getProcessPtr());
114180SN/A
1152862Sktlim@umich.edu    copyState(oldContext);
1168793Sgblack@eecs.umich.edu    if (FullSystem) {
1178793Sgblack@eecs.umich.edu        EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
1188793Sgblack@eecs.umich.edu        if (quiesce) {
1198793Sgblack@eecs.umich.edu            // Point the quiesce event's TC at this TC so that it wakes up
1208793Sgblack@eecs.umich.edu            // the proper CPU.
1218793Sgblack@eecs.umich.edu            quiesce->tc = tc;
1228793Sgblack@eecs.umich.edu        }
1238793Sgblack@eecs.umich.edu        if (quiesceEvent) {
1248793Sgblack@eecs.umich.edu            quiesceEvent->tc = tc;
1258793Sgblack@eecs.umich.edu        }
1268793Sgblack@eecs.umich.edu
1278793Sgblack@eecs.umich.edu        TheISA::Kernel::Statistics *stats = oldContext->getKernelStats();
1288793Sgblack@eecs.umich.edu        if (stats) {
1298793Sgblack@eecs.umich.edu            kernelStats = stats;
1308793Sgblack@eecs.umich.edu        }
1312313SN/A    }
132180SN/A
133180SN/A    storeCondFailures = 0;
134180SN/A
1356029Ssteve.reinhardt@amd.com    oldContext->setStatus(ThreadContext::Halted);
136180SN/A}
137180SN/A
1382SN/Avoid
1392864Sktlim@umich.eduSimpleThread::copyTC(ThreadContext *context)
1402864Sktlim@umich.edu{
1412864Sktlim@umich.edu    copyState(context);
1422864Sktlim@umich.edu
1438793Sgblack@eecs.umich.edu    if (FullSystem) {
1448793Sgblack@eecs.umich.edu        EndQuiesceEvent *quiesce = context->getQuiesceEvent();
1458793Sgblack@eecs.umich.edu        if (quiesce) {
1468793Sgblack@eecs.umich.edu            quiesceEvent = quiesce;
1478793Sgblack@eecs.umich.edu        }
1488793Sgblack@eecs.umich.edu        TheISA::Kernel::Statistics *stats = context->getKernelStats();
1498793Sgblack@eecs.umich.edu        if (stats) {
1508793Sgblack@eecs.umich.edu            kernelStats = stats;
1518793Sgblack@eecs.umich.edu        }
1522864Sktlim@umich.edu    }
1532864Sktlim@umich.edu}
1542864Sktlim@umich.edu
1552864Sktlim@umich.eduvoid
1562862Sktlim@umich.eduSimpleThread::copyState(ThreadContext *oldContext)
1572862Sktlim@umich.edu{
1582862Sktlim@umich.edu    // copy over functional state
1592862Sktlim@umich.edu    _status = oldContext->status();
1602862Sktlim@umich.edu    copyArchRegs(oldContext);
1618793Sgblack@eecs.umich.edu    if (FullSystem)
1628793Sgblack@eecs.umich.edu        funcExeInst = oldContext->readFuncExeInst();
1635714Shsul@eecs.umich.edu
1645715Shsul@eecs.umich.edu    _threadId = oldContext->threadId();
1655714Shsul@eecs.umich.edu    _contextId = oldContext->contextId();
1662862Sktlim@umich.edu}
1672862Sktlim@umich.edu
1682862Sktlim@umich.eduvoid
1692683Sktlim@umich.eduSimpleThread::serialize(ostream &os)
170217SN/A{
1712862Sktlim@umich.edu    ThreadState::serialize(os);
1729428SAndreas.Sandberg@ARM.com    ::serialize(*tc, os);
173217SN/A}
174217SN/A
175217SN/A
176217SN/Avoid
1772683Sktlim@umich.eduSimpleThread::unserialize(Checkpoint *cp, const std::string &section)
178217SN/A{
1792862Sktlim@umich.edu    ThreadState::unserialize(cp, section);
1809428SAndreas.Sandberg@ARM.com    ::unserialize(*tc, cp, section);
181217SN/A}
182217SN/A
1832683Sktlim@umich.eduvoid
1842683Sktlim@umich.eduSimpleThread::dumpFuncProfile()
1852683Sktlim@umich.edu{
1868735Sandreas.hanson@arm.com    std::ostream *os = simout.create(csprintf("profile.%s.dat",
1878735Sandreas.hanson@arm.com                                              baseCpu->name()));
1882683Sktlim@umich.edu    profile->dump(tc, *os);
1892683Sktlim@umich.edu}
190217SN/A
191217SN/Avoid
1929180Sandreas.hansson@arm.comSimpleThread::activate(Cycles delay)
1932SN/A{
1942680SN/A    if (status() == ThreadContext::Active)
1952SN/A        return;
1962SN/A
1977823Ssteve.reinhardt@amd.com    lastActivate = curTick();
1982188SN/A
1994400Srdreslin@umich.edu//    if (status() == ThreadContext::Unallocated) {
2005715Shsul@eecs.umich.edu//      cpu->activateWhenReady(_threadId);
2015543Ssaidi@eecs.umich.edu//      return;
2024400Srdreslin@umich.edu//   }
2032290SN/A
2042680SN/A    _status = ThreadContext::Active;
2052290SN/A
2062290SN/A    // status() == Suspended
2078735Sandreas.hanson@arm.com    baseCpu->activateContext(_threadId, delay);
208393SN/A}
209393SN/A
210393SN/Avoid
2112683Sktlim@umich.eduSimpleThread::suspend()
212393SN/A{
2132680SN/A    if (status() == ThreadContext::Suspended)
214393SN/A        return;
215393SN/A
2167823Ssteve.reinhardt@amd.com    lastActivate = curTick();
2177823Ssteve.reinhardt@amd.com    lastSuspend = curTick();
2182680SN/A    _status = ThreadContext::Suspended;
2198735Sandreas.hanson@arm.com    baseCpu->suspendContext(_threadId);
2202SN/A}
2212SN/A
222393SN/A
223393SN/Avoid
2242683Sktlim@umich.eduSimpleThread::halt()
225393SN/A{
2262680SN/A    if (status() == ThreadContext::Halted)
227393SN/A        return;
228393SN/A
2292680SN/A    _status = ThreadContext::Halted;
2308735Sandreas.hanson@arm.com    baseCpu->haltContext(_threadId);
231393SN/A}
232393SN/A
233393SN/A
234393SN/Avoid
2352683Sktlim@umich.eduSimpleThread::regStats(const string &name)
2362SN/A{
2378793Sgblack@eecs.umich.edu    if (FullSystem && kernelStats)
2382341SN/A        kernelStats->regStats(name + ".kern");
2392SN/A}
240716SN/A
241716SN/Avoid
2422683Sktlim@umich.eduSimpleThread::copyArchRegs(ThreadContext *src_tc)
2432190SN/A{
2442680SN/A    TheISA::copyRegs(src_tc, tc);
2452190SN/A}
2462190SN/A
247