simple_thread.cc revision 8809
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
628766Sgblack@eecs.umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process,
638766Sgblack@eecs.umich.edu                           TheISA::TLB *_itb, TheISA::TLB *_dtb)
648809Sgblack@eecs.umich.edu    : ThreadState(_cpu, _thread_num, _process), itb(_itb), dtb(_dtb)
658766Sgblack@eecs.umich.edu{
668766Sgblack@eecs.umich.edu    clearArchRegs();
678766Sgblack@eecs.umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
688766Sgblack@eecs.umich.edu}
692683Sktlim@umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
706022Sgblack@eecs.umich.edu                           TheISA::TLB *_itb, TheISA::TLB *_dtb,
712683Sktlim@umich.edu                           bool use_kernel_stats)
728809Sgblack@eecs.umich.edu    : ThreadState(_cpu, _thread_num, NULL), system(_sys), itb(_itb), dtb(_dtb)
732SN/A{
742683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
752190SN/A
762680SN/A    quiesceEvent = new EndQuiesceEvent(tc);
772290SN/A
786316Sgblack@eecs.umich.edu    clearArchRegs();
791917SN/A
808735Sandreas.hanson@arm.com    if (baseCpu->params()->profile) {
811982SN/A        profile = new FunctionProfile(system->kernelSymtab);
821917SN/A        Callback *cb =
832683Sktlim@umich.edu            new MakeCallback<SimpleThread,
842683Sktlim@umich.edu            &SimpleThread::dumpFuncProfile>(this);
851917SN/A        registerExitCallback(cb);
861917SN/A    }
871917SN/A
881917SN/A    // let's fill with a dummy node for now so we don't get a segfault
891917SN/A    // on the first cycle when there's no node available.
901917SN/A    static ProfileNode dummyNode;
911917SN/A    profileNode = &dummyNode;
921917SN/A    profilePC = 3;
932521SN/A
945482Snate@binkert.org    if (use_kernel_stats)
953548Sgblack@eecs.umich.edu        kernelStats = new TheISA::Kernel::Statistics(system);
962SN/A}
972862Sktlim@umich.edu
982864Sktlim@umich.eduSimpleThread::SimpleThread()
996331Sgblack@eecs.umich.edu    : ThreadState(NULL, -1, NULL)
1002190SN/A{
1012683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
1022190SN/A}
1032190SN/A
1042683Sktlim@umich.eduSimpleThread::~SimpleThread()
1051070SN/A{
1062680SN/A    delete tc;
1071070SN/A}
1081070SN/A
1091917SN/Avoid
1102683Sktlim@umich.eduSimpleThread::takeOverFrom(ThreadContext *oldContext)
111180SN/A{
112180SN/A    // some things should already be set up
1138793Sgblack@eecs.umich.edu    if (FullSystem)
1148793Sgblack@eecs.umich.edu        assert(system == oldContext->getSystemPtr());
1152235SN/A    assert(process == oldContext->getProcessPtr());
116180SN/A
1172862Sktlim@umich.edu    copyState(oldContext);
1188793Sgblack@eecs.umich.edu    if (FullSystem) {
1198793Sgblack@eecs.umich.edu        EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
1208793Sgblack@eecs.umich.edu        if (quiesce) {
1218793Sgblack@eecs.umich.edu            // Point the quiesce event's TC at this TC so that it wakes up
1228793Sgblack@eecs.umich.edu            // the proper CPU.
1238793Sgblack@eecs.umich.edu            quiesce->tc = tc;
1248793Sgblack@eecs.umich.edu        }
1258793Sgblack@eecs.umich.edu        if (quiesceEvent) {
1268793Sgblack@eecs.umich.edu            quiesceEvent->tc = tc;
1278793Sgblack@eecs.umich.edu        }
1288793Sgblack@eecs.umich.edu
1298793Sgblack@eecs.umich.edu        TheISA::Kernel::Statistics *stats = oldContext->getKernelStats();
1308793Sgblack@eecs.umich.edu        if (stats) {
1318793Sgblack@eecs.umich.edu            kernelStats = stats;
1328793Sgblack@eecs.umich.edu        }
1332313SN/A    }
134180SN/A
135180SN/A    storeCondFailures = 0;
136180SN/A
1376029Ssteve.reinhardt@amd.com    oldContext->setStatus(ThreadContext::Halted);
138180SN/A}
139180SN/A
1402SN/Avoid
1412864Sktlim@umich.eduSimpleThread::copyTC(ThreadContext *context)
1422864Sktlim@umich.edu{
1432864Sktlim@umich.edu    copyState(context);
1442864Sktlim@umich.edu
1458793Sgblack@eecs.umich.edu    if (FullSystem) {
1468793Sgblack@eecs.umich.edu        EndQuiesceEvent *quiesce = context->getQuiesceEvent();
1478793Sgblack@eecs.umich.edu        if (quiesce) {
1488793Sgblack@eecs.umich.edu            quiesceEvent = quiesce;
1498793Sgblack@eecs.umich.edu        }
1508793Sgblack@eecs.umich.edu        TheISA::Kernel::Statistics *stats = context->getKernelStats();
1518793Sgblack@eecs.umich.edu        if (stats) {
1528793Sgblack@eecs.umich.edu            kernelStats = stats;
1538793Sgblack@eecs.umich.edu        }
1542864Sktlim@umich.edu    }
1552864Sktlim@umich.edu}
1562864Sktlim@umich.edu
1572864Sktlim@umich.eduvoid
1582862Sktlim@umich.eduSimpleThread::copyState(ThreadContext *oldContext)
1592862Sktlim@umich.edu{
1602862Sktlim@umich.edu    // copy over functional state
1612862Sktlim@umich.edu    _status = oldContext->status();
1622862Sktlim@umich.edu    copyArchRegs(oldContext);
1638793Sgblack@eecs.umich.edu    if (FullSystem)
1648793Sgblack@eecs.umich.edu        funcExeInst = oldContext->readFuncExeInst();
1655714Shsul@eecs.umich.edu
1665715Shsul@eecs.umich.edu    _threadId = oldContext->threadId();
1675714Shsul@eecs.umich.edu    _contextId = oldContext->contextId();
1682862Sktlim@umich.edu}
1692862Sktlim@umich.edu
1702862Sktlim@umich.eduvoid
1712683Sktlim@umich.eduSimpleThread::serialize(ostream &os)
172217SN/A{
1732862Sktlim@umich.edu    ThreadState::serialize(os);
1746315Sgblack@eecs.umich.edu    SERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs);
1756316Sgblack@eecs.umich.edu    SERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs);
1767720Sgblack@eecs.umich.edu    _pcState.serialize(os);
177223SN/A    // thread_num and cpu_id are deterministic from the config
1786677SBrad.Beckmann@amd.com
1796677SBrad.Beckmann@amd.com    //
1806677SBrad.Beckmann@amd.com    // Now must serialize all the ISA dependent state
1816677SBrad.Beckmann@amd.com    //
1828735Sandreas.hanson@arm.com    isa.serialize(baseCpu, os);
183217SN/A}
184217SN/A
185217SN/A
186217SN/Avoid
1872683Sktlim@umich.eduSimpleThread::unserialize(Checkpoint *cp, const std::string &section)
188217SN/A{
1892862Sktlim@umich.edu    ThreadState::unserialize(cp, section);
1906315Sgblack@eecs.umich.edu    UNSERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs);
1916316Sgblack@eecs.umich.edu    UNSERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs);
1927720Sgblack@eecs.umich.edu    _pcState.unserialize(cp, section);
193223SN/A    // thread_num and cpu_id are deterministic from the config
1946677SBrad.Beckmann@amd.com
1956677SBrad.Beckmann@amd.com    //
1966677SBrad.Beckmann@amd.com    // Now must unserialize all the ISA dependent state
1976677SBrad.Beckmann@amd.com    //
1988735Sandreas.hanson@arm.com    isa.unserialize(baseCpu, cp, section);
199217SN/A}
200217SN/A
2012683Sktlim@umich.eduvoid
2022683Sktlim@umich.eduSimpleThread::dumpFuncProfile()
2032683Sktlim@umich.edu{
2048735Sandreas.hanson@arm.com    std::ostream *os = simout.create(csprintf("profile.%s.dat",
2058735Sandreas.hanson@arm.com                                              baseCpu->name()));
2062683Sktlim@umich.edu    profile->dump(tc, *os);
2072683Sktlim@umich.edu}
208217SN/A
209217SN/Avoid
2102683Sktlim@umich.eduSimpleThread::activate(int delay)
2112SN/A{
2122680SN/A    if (status() == ThreadContext::Active)
2132SN/A        return;
2142SN/A
2157823Ssteve.reinhardt@amd.com    lastActivate = curTick();
2162188SN/A
2174400Srdreslin@umich.edu//    if (status() == ThreadContext::Unallocated) {
2185715Shsul@eecs.umich.edu//      cpu->activateWhenReady(_threadId);
2195543Ssaidi@eecs.umich.edu//      return;
2204400Srdreslin@umich.edu//   }
2212290SN/A
2222680SN/A    _status = ThreadContext::Active;
2232290SN/A
2242290SN/A    // status() == Suspended
2258735Sandreas.hanson@arm.com    baseCpu->activateContext(_threadId, delay);
226393SN/A}
227393SN/A
228393SN/Avoid
2292683Sktlim@umich.eduSimpleThread::suspend()
230393SN/A{
2312680SN/A    if (status() == ThreadContext::Suspended)
232393SN/A        return;
233393SN/A
2347823Ssteve.reinhardt@amd.com    lastActivate = curTick();
2357823Ssteve.reinhardt@amd.com    lastSuspend = curTick();
2362680SN/A    _status = ThreadContext::Suspended;
2378735Sandreas.hanson@arm.com    baseCpu->suspendContext(_threadId);
2382SN/A}
2392SN/A
240393SN/A
241393SN/Avoid
2422683Sktlim@umich.eduSimpleThread::halt()
243393SN/A{
2442680SN/A    if (status() == ThreadContext::Halted)
245393SN/A        return;
246393SN/A
2472680SN/A    _status = ThreadContext::Halted;
2488735Sandreas.hanson@arm.com    baseCpu->haltContext(_threadId);
249393SN/A}
250393SN/A
251393SN/A
252393SN/Avoid
2532683Sktlim@umich.eduSimpleThread::regStats(const string &name)
2542SN/A{
2558793Sgblack@eecs.umich.edu    if (FullSystem && kernelStats)
2562341SN/A        kernelStats->regStats(name + ".kern");
2572SN/A}
258716SN/A
259716SN/Avoid
2602683Sktlim@umich.eduSimpleThread::copyArchRegs(ThreadContext *src_tc)
2612190SN/A{
2622680SN/A    TheISA::copyRegs(src_tc, tc);
2632190SN/A}
2642190SN/A
265