simple_thread.cc revision 8767
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"
377680Sgblack@eecs.umich.edu#include "arch/utility.hh"
386658Snate@binkert.org#include "config/the_isa.hh"
391717SN/A#include "cpu/base.hh"
402683Sktlim@umich.edu#include "cpu/simple_thread.hh"
412680SN/A#include "cpu/thread_context.hh"
428761Sgblack@eecs.umich.edu#include "mem/vport.hh"
435529Snate@binkert.org#include "params/BaseCPU.hh"
448767Sgblack@eecs.umich.edu#include "sim/process.hh"
452SN/A
461858SN/A#if FULL_SYSTEM
473565Sgblack@eecs.umich.edu#include "arch/kernel_stats.hh"
485529Snate@binkert.org#include "arch/stacktrace.hh"
491917SN/A#include "base/callback.hh"
501070SN/A#include "base/cprintf.hh"
511917SN/A#include "base/output.hh"
522188SN/A#include "base/trace.hh"
531917SN/A#include "cpu/profile.hh"
542290SN/A#include "cpu/quiesce_event.hh"
551070SN/A#include "sim/serialize.hh"
561917SN/A#include "sim/sim_exit.hh"
572SN/A#else
585529Snate@binkert.org#include "mem/translating_port.hh"
592519SN/A#include "sim/system.hh"
602SN/A#endif
612SN/A
622SN/Ausing namespace std;
632SN/A
642SN/A// constructor
658766Sgblack@eecs.umich.edu#if !FULL_SYSTEM
668766Sgblack@eecs.umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process,
678766Sgblack@eecs.umich.edu                           TheISA::TLB *_itb, TheISA::TLB *_dtb)
688766Sgblack@eecs.umich.edu    : ThreadState(_cpu, _thread_num, _process),
698766Sgblack@eecs.umich.edu      cpu(_cpu), itb(_itb), dtb(_dtb)
708766Sgblack@eecs.umich.edu{
718766Sgblack@eecs.umich.edu    clearArchRegs();
728766Sgblack@eecs.umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
738766Sgblack@eecs.umich.edu}
748766Sgblack@eecs.umich.edu#else
752683Sktlim@umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
766022Sgblack@eecs.umich.edu                           TheISA::TLB *_itb, TheISA::TLB *_dtb,
772683Sktlim@umich.edu                           bool use_kernel_stats)
788766Sgblack@eecs.umich.edu    : ThreadState(_cpu, _thread_num, NULL),
796324Sgblack@eecs.umich.edu      cpu(_cpu), system(_sys), itb(_itb), dtb(_dtb)
802521SN/A
812SN/A{
822683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
832190SN/A
842680SN/A    quiesceEvent = new EndQuiesceEvent(tc);
852290SN/A
866316Sgblack@eecs.umich.edu    clearArchRegs();
871917SN/A
885529Snate@binkert.org    if (cpu->params()->profile) {
891982SN/A        profile = new FunctionProfile(system->kernelSymtab);
901917SN/A        Callback *cb =
912683Sktlim@umich.edu            new MakeCallback<SimpleThread,
922683Sktlim@umich.edu            &SimpleThread::dumpFuncProfile>(this);
931917SN/A        registerExitCallback(cb);
941917SN/A    }
951917SN/A
961917SN/A    // let's fill with a dummy node for now so we don't get a segfault
971917SN/A    // on the first cycle when there's no node available.
981917SN/A    static ProfileNode dummyNode;
991917SN/A    profileNode = &dummyNode;
1001917SN/A    profilePC = 3;
1012521SN/A
1025482Snate@binkert.org    if (use_kernel_stats)
1033548Sgblack@eecs.umich.edu        kernelStats = new TheISA::Kernel::Statistics(system);
1042SN/A}
1052862Sktlim@umich.edu#endif
1062862Sktlim@umich.edu
1072864Sktlim@umich.eduSimpleThread::SimpleThread()
1086331Sgblack@eecs.umich.edu    : ThreadState(NULL, -1, NULL)
1092190SN/A{
1102683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
1112190SN/A}
1122190SN/A
1132683Sktlim@umich.eduSimpleThread::~SimpleThread()
1141070SN/A{
1158754Sgblack@eecs.umich.edu    delete physPort;
1163486Sktlim@umich.edu    delete virtPort;
1172680SN/A    delete tc;
1181070SN/A}
1191070SN/A
1201917SN/Avoid
1212683Sktlim@umich.eduSimpleThread::takeOverFrom(ThreadContext *oldContext)
122180SN/A{
123180SN/A    // some things should already be set up
1241858SN/A#if FULL_SYSTEM
1252235SN/A    assert(system == oldContext->getSystemPtr());
1268767Sgblack@eecs.umich.edu#endif
1272235SN/A    assert(process == oldContext->getProcessPtr());
128180SN/A
1292862Sktlim@umich.edu    copyState(oldContext);
1302862Sktlim@umich.edu#if FULL_SYSTEM
1312313SN/A    EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
1322313SN/A    if (quiesce) {
1332680SN/A        // Point the quiesce event's TC at this TC so that it wakes up
1342313SN/A        // the proper CPU.
1352680SN/A        quiesce->tc = tc;
1362313SN/A    }
1372313SN/A    if (quiesceEvent) {
1382680SN/A        quiesceEvent->tc = tc;
1392313SN/A    }
1402361SN/A
1413548Sgblack@eecs.umich.edu    TheISA::Kernel::Statistics *stats = oldContext->getKernelStats();
1422361SN/A    if (stats) {
1432361SN/A        kernelStats = stats;
1442361SN/A    }
1452235SN/A#endif
146180SN/A
147180SN/A    storeCondFailures = 0;
148180SN/A
1496029Ssteve.reinhardt@amd.com    oldContext->setStatus(ThreadContext::Halted);
150180SN/A}
151180SN/A
1522SN/Avoid
1532864Sktlim@umich.eduSimpleThread::copyTC(ThreadContext *context)
1542864Sktlim@umich.edu{
1552864Sktlim@umich.edu    copyState(context);
1562864Sktlim@umich.edu
1572864Sktlim@umich.edu#if FULL_SYSTEM
1582864Sktlim@umich.edu    EndQuiesceEvent *quiesce = context->getQuiesceEvent();
1592864Sktlim@umich.edu    if (quiesce) {
1602864Sktlim@umich.edu        quiesceEvent = quiesce;
1612864Sktlim@umich.edu    }
1623548Sgblack@eecs.umich.edu    TheISA::Kernel::Statistics *stats = context->getKernelStats();
1632864Sktlim@umich.edu    if (stats) {
1642864Sktlim@umich.edu        kernelStats = stats;
1652864Sktlim@umich.edu    }
1662864Sktlim@umich.edu#endif
1672864Sktlim@umich.edu}
1682864Sktlim@umich.edu
1692864Sktlim@umich.eduvoid
1702862Sktlim@umich.eduSimpleThread::copyState(ThreadContext *oldContext)
1712862Sktlim@umich.edu{
1722862Sktlim@umich.edu    // copy over functional state
1732862Sktlim@umich.edu    _status = oldContext->status();
1742862Sktlim@umich.edu    copyArchRegs(oldContext);
1752862Sktlim@umich.edu#if !FULL_SYSTEM
1762862Sktlim@umich.edu    funcExeInst = oldContext->readFuncExeInst();
1772862Sktlim@umich.edu#endif
1785714Shsul@eecs.umich.edu
1795715Shsul@eecs.umich.edu    _threadId = oldContext->threadId();
1805714Shsul@eecs.umich.edu    _contextId = oldContext->contextId();
1812862Sktlim@umich.edu}
1822862Sktlim@umich.edu
1832862Sktlim@umich.eduvoid
1842683Sktlim@umich.eduSimpleThread::serialize(ostream &os)
185217SN/A{
1862862Sktlim@umich.edu    ThreadState::serialize(os);
1876315Sgblack@eecs.umich.edu    SERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs);
1886316Sgblack@eecs.umich.edu    SERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs);
1897720Sgblack@eecs.umich.edu    _pcState.serialize(os);
190223SN/A    // thread_num and cpu_id are deterministic from the config
1916677SBrad.Beckmann@amd.com
1926677SBrad.Beckmann@amd.com    //
1936677SBrad.Beckmann@amd.com    // Now must serialize all the ISA dependent state
1946677SBrad.Beckmann@amd.com    //
1956678Sgblack@eecs.umich.edu    isa.serialize(cpu, os);
196217SN/A}
197217SN/A
198217SN/A
199217SN/Avoid
2002683Sktlim@umich.eduSimpleThread::unserialize(Checkpoint *cp, const std::string &section)
201217SN/A{
2022862Sktlim@umich.edu    ThreadState::unserialize(cp, section);
2036315Sgblack@eecs.umich.edu    UNSERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs);
2046316Sgblack@eecs.umich.edu    UNSERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs);
2057720Sgblack@eecs.umich.edu    _pcState.unserialize(cp, section);
206223SN/A    // thread_num and cpu_id are deterministic from the config
2076677SBrad.Beckmann@amd.com
2086677SBrad.Beckmann@amd.com    //
2096677SBrad.Beckmann@amd.com    // Now must unserialize all the ISA dependent state
2106677SBrad.Beckmann@amd.com    //
2116678Sgblack@eecs.umich.edu    isa.unserialize(cpu, cp, section);
212217SN/A}
213217SN/A
2142683Sktlim@umich.edu#if FULL_SYSTEM
2152683Sktlim@umich.eduvoid
2162683Sktlim@umich.eduSimpleThread::dumpFuncProfile()
2172683Sktlim@umich.edu{
2182683Sktlim@umich.edu    std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name()));
2192683Sktlim@umich.edu    profile->dump(tc, *os);
2202683Sktlim@umich.edu}
2212683Sktlim@umich.edu#endif
222217SN/A
223217SN/Avoid
2242683Sktlim@umich.eduSimpleThread::activate(int delay)
2252SN/A{
2262680SN/A    if (status() == ThreadContext::Active)
2272SN/A        return;
2282SN/A
2297823Ssteve.reinhardt@amd.com    lastActivate = curTick();
2302188SN/A
2314400Srdreslin@umich.edu//    if (status() == ThreadContext::Unallocated) {
2325715Shsul@eecs.umich.edu//      cpu->activateWhenReady(_threadId);
2335543Ssaidi@eecs.umich.edu//      return;
2344400Srdreslin@umich.edu//   }
2352290SN/A
2362680SN/A    _status = ThreadContext::Active;
2372290SN/A
2382290SN/A    // status() == Suspended
2395715Shsul@eecs.umich.edu    cpu->activateContext(_threadId, delay);
240393SN/A}
241393SN/A
242393SN/Avoid
2432683Sktlim@umich.eduSimpleThread::suspend()
244393SN/A{
2452680SN/A    if (status() == ThreadContext::Suspended)
246393SN/A        return;
247393SN/A
2487823Ssteve.reinhardt@amd.com    lastActivate = curTick();
2497823Ssteve.reinhardt@amd.com    lastSuspend = curTick();
2502188SN/A/*
2511858SN/A#if FULL_SYSTEM
2522SN/A    // Don't change the status from active if there are pending interrupts
2535704Snate@binkert.org    if (cpu->checkInterrupts()) {
2542680SN/A        assert(status() == ThreadContext::Active);
2552SN/A        return;
2562SN/A    }
2572SN/A#endif
2582188SN/A*/
2592680SN/A    _status = ThreadContext::Suspended;
2605715Shsul@eecs.umich.edu    cpu->suspendContext(_threadId);
2612SN/A}
2622SN/A
263393SN/A
264393SN/Avoid
2652683Sktlim@umich.eduSimpleThread::halt()
266393SN/A{
2672680SN/A    if (status() == ThreadContext::Halted)
268393SN/A        return;
269393SN/A
2702680SN/A    _status = ThreadContext::Halted;
2715715Shsul@eecs.umich.edu    cpu->haltContext(_threadId);
272393SN/A}
273393SN/A
274393SN/A
275393SN/Avoid
2762683Sktlim@umich.eduSimpleThread::regStats(const string &name)
2772SN/A{
2782330SN/A#if FULL_SYSTEM
2792341SN/A    if (kernelStats)
2802341SN/A        kernelStats->regStats(name + ".kern");
2812330SN/A#endif
2822SN/A}
283716SN/A
284716SN/Avoid
2852683Sktlim@umich.eduSimpleThread::copyArchRegs(ThreadContext *src_tc)
2862190SN/A{
2872680SN/A    TheISA::copyRegs(src_tc, tc);
2882190SN/A}
2892190SN/A
290