simple_thread.cc revision 5714
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"
371717SN/A#include "cpu/base.hh"
382683Sktlim@umich.edu#include "cpu/simple_thread.hh"
392680SN/A#include "cpu/thread_context.hh"
405529Snate@binkert.org#include "params/BaseCPU.hh"
412SN/A
421858SN/A#if FULL_SYSTEM
433565Sgblack@eecs.umich.edu#include "arch/kernel_stats.hh"
445529Snate@binkert.org#include "arch/stacktrace.hh"
451917SN/A#include "base/callback.hh"
461070SN/A#include "base/cprintf.hh"
471917SN/A#include "base/output.hh"
482188SN/A#include "base/trace.hh"
491917SN/A#include "cpu/profile.hh"
502290SN/A#include "cpu/quiesce_event.hh"
511070SN/A#include "sim/serialize.hh"
521917SN/A#include "sim/sim_exit.hh"
532SN/A#else
545529Snate@binkert.org#include "mem/translating_port.hh"
55360SN/A#include "sim/process.hh"
562519SN/A#include "sim/system.hh"
572SN/A#endif
582SN/A
592SN/Ausing namespace std;
602SN/A
612SN/A// constructor
621858SN/A#if FULL_SYSTEM
632683Sktlim@umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
643453Sgblack@eecs.umich.edu                           TheISA::ITB *_itb, TheISA::DTB *_dtb,
652683Sktlim@umich.edu                           bool use_kernel_stats)
665712Shsul@eecs.umich.edu    : ThreadState(_cpu, _thread_num), cpu(_cpu), system(_sys), itb(_itb),
672683Sktlim@umich.edu      dtb(_dtb)
682521SN/A
692SN/A{
702683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
712190SN/A
722680SN/A    quiesceEvent = new EndQuiesceEvent(tc);
732290SN/A
742526SN/A    regs.clear();
751917SN/A
765529Snate@binkert.org    if (cpu->params()->profile) {
771982SN/A        profile = new FunctionProfile(system->kernelSymtab);
781917SN/A        Callback *cb =
792683Sktlim@umich.edu            new MakeCallback<SimpleThread,
802683Sktlim@umich.edu            &SimpleThread::dumpFuncProfile>(this);
811917SN/A        registerExitCallback(cb);
821917SN/A    }
831917SN/A
841917SN/A    // let's fill with a dummy node for now so we don't get a segfault
851917SN/A    // on the first cycle when there's no node available.
861917SN/A    static ProfileNode dummyNode;
871917SN/A    profileNode = &dummyNode;
881917SN/A    profilePC = 3;
892521SN/A
905482Snate@binkert.org    if (use_kernel_stats)
913548Sgblack@eecs.umich.edu        kernelStats = new TheISA::Kernel::Statistics(system);
922SN/A}
932SN/A#else
944997Sgblack@eecs.umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process,
954997Sgblack@eecs.umich.edu                           TheISA::ITB *_itb, TheISA::DTB *_dtb, int _asid)
965712Shsul@eecs.umich.edu    : ThreadState(_cpu, _thread_num, _process, _asid),
974997Sgblack@eecs.umich.edu      cpu(_cpu), itb(_itb), dtb(_dtb)
982SN/A{
992526SN/A    regs.clear();
1002683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
1012SN/A}
1022190SN/A
1032862Sktlim@umich.edu#endif
1042862Sktlim@umich.edu
1052864Sktlim@umich.eduSimpleThread::SimpleThread()
1062862Sktlim@umich.edu#if FULL_SYSTEM
1075712Shsul@eecs.umich.edu    : ThreadState(NULL, -1)
1082862Sktlim@umich.edu#else
1095712Shsul@eecs.umich.edu    : ThreadState(NULL, -1, NULL, -1)
1102862Sktlim@umich.edu#endif
1112190SN/A{
1122683Sktlim@umich.edu    tc = new ProxyThreadContext<SimpleThread>(this);
1132862Sktlim@umich.edu    regs.clear();
1142190SN/A}
1152190SN/A
1162683Sktlim@umich.eduSimpleThread::~SimpleThread()
1171070SN/A{
1183486Sktlim@umich.edu#if FULL_SYSTEM
1193486Sktlim@umich.edu    delete physPort;
1203486Sktlim@umich.edu    delete virtPort;
1213486Sktlim@umich.edu#endif
1222680SN/A    delete tc;
1231070SN/A}
1241070SN/A
1251917SN/Avoid
1262683Sktlim@umich.eduSimpleThread::takeOverFrom(ThreadContext *oldContext)
127180SN/A{
128180SN/A    // some things should already be set up
1291858SN/A#if FULL_SYSTEM
1302235SN/A    assert(system == oldContext->getSystemPtr());
131180SN/A#else
1322235SN/A    assert(process == oldContext->getProcessPtr());
133180SN/A#endif
134180SN/A
1352862Sktlim@umich.edu    copyState(oldContext);
1362862Sktlim@umich.edu#if FULL_SYSTEM
1372313SN/A    EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent();
1382313SN/A    if (quiesce) {
1392680SN/A        // Point the quiesce event's TC at this TC so that it wakes up
1402313SN/A        // the proper CPU.
1412680SN/A        quiesce->tc = tc;
1422313SN/A    }
1432313SN/A    if (quiesceEvent) {
1442680SN/A        quiesceEvent->tc = tc;
1452313SN/A    }
1462361SN/A
1473548Sgblack@eecs.umich.edu    TheISA::Kernel::Statistics *stats = oldContext->getKernelStats();
1482361SN/A    if (stats) {
1492361SN/A        kernelStats = stats;
1502361SN/A    }
1512235SN/A#endif
152180SN/A
153180SN/A    storeCondFailures = 0;
154180SN/A
1552680SN/A    oldContext->setStatus(ThreadContext::Unallocated);
156180SN/A}
157180SN/A
1582SN/Avoid
1592864Sktlim@umich.eduSimpleThread::copyTC(ThreadContext *context)
1602864Sktlim@umich.edu{
1612864Sktlim@umich.edu    copyState(context);
1622864Sktlim@umich.edu
1632864Sktlim@umich.edu#if FULL_SYSTEM
1642864Sktlim@umich.edu    EndQuiesceEvent *quiesce = context->getQuiesceEvent();
1652864Sktlim@umich.edu    if (quiesce) {
1662864Sktlim@umich.edu        quiesceEvent = quiesce;
1672864Sktlim@umich.edu    }
1683548Sgblack@eecs.umich.edu    TheISA::Kernel::Statistics *stats = context->getKernelStats();
1692864Sktlim@umich.edu    if (stats) {
1702864Sktlim@umich.edu        kernelStats = stats;
1712864Sktlim@umich.edu    }
1722864Sktlim@umich.edu#endif
1732864Sktlim@umich.edu}
1742864Sktlim@umich.edu
1752864Sktlim@umich.eduvoid
1762862Sktlim@umich.eduSimpleThread::copyState(ThreadContext *oldContext)
1772862Sktlim@umich.edu{
1782862Sktlim@umich.edu    // copy over functional state
1792862Sktlim@umich.edu    _status = oldContext->status();
1802862Sktlim@umich.edu    copyArchRegs(oldContext);
1812862Sktlim@umich.edu#if !FULL_SYSTEM
1822862Sktlim@umich.edu    funcExeInst = oldContext->readFuncExeInst();
1832862Sktlim@umich.edu#endif
1842915Sktlim@umich.edu    inst = oldContext->getInst();
1855714Shsul@eecs.umich.edu
1865714Shsul@eecs.umich.edu    _contextId = oldContext->contextId();
1872862Sktlim@umich.edu}
1882862Sktlim@umich.edu
1892862Sktlim@umich.eduvoid
1902683Sktlim@umich.eduSimpleThread::serialize(ostream &os)
191217SN/A{
1922862Sktlim@umich.edu    ThreadState::serialize(os);
1935606Snate@binkert.org    regs.serialize(cpu, os);
194223SN/A    // thread_num and cpu_id are deterministic from the config
195217SN/A}
196217SN/A
197217SN/A
198217SN/Avoid
1992683Sktlim@umich.eduSimpleThread::unserialize(Checkpoint *cp, const std::string &section)
200217SN/A{
2012862Sktlim@umich.edu    ThreadState::unserialize(cp, section);
2025606Snate@binkert.org    regs.unserialize(cpu, cp, section);
203223SN/A    // thread_num and cpu_id are deterministic from the config
204217SN/A}
205217SN/A
2062683Sktlim@umich.edu#if FULL_SYSTEM
2072683Sktlim@umich.eduvoid
2082683Sktlim@umich.eduSimpleThread::dumpFuncProfile()
2092683Sktlim@umich.edu{
2102683Sktlim@umich.edu    std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name()));
2112683Sktlim@umich.edu    profile->dump(tc, *os);
2122683Sktlim@umich.edu}
2132683Sktlim@umich.edu#endif
214217SN/A
215217SN/Avoid
2162683Sktlim@umich.eduSimpleThread::activate(int delay)
2172SN/A{
2182680SN/A    if (status() == ThreadContext::Active)
2192SN/A        return;
2202SN/A
2212188SN/A    lastActivate = curTick;
2222188SN/A
2234400Srdreslin@umich.edu//    if (status() == ThreadContext::Unallocated) {
2245543Ssaidi@eecs.umich.edu//      cpu->activateWhenReady(tid);
2255543Ssaidi@eecs.umich.edu//      return;
2264400Srdreslin@umich.edu//   }
2272290SN/A
2282680SN/A    _status = ThreadContext::Active;
2292290SN/A
2302290SN/A    // status() == Suspended
2312683Sktlim@umich.edu    cpu->activateContext(tid, delay);
232393SN/A}
233393SN/A
234393SN/Avoid
2352683Sktlim@umich.eduSimpleThread::suspend()
236393SN/A{
2372680SN/A    if (status() == ThreadContext::Suspended)
238393SN/A        return;
239393SN/A
2402188SN/A    lastActivate = curTick;
2412188SN/A    lastSuspend = curTick;
2422188SN/A/*
2431858SN/A#if FULL_SYSTEM
2442SN/A    // Don't change the status from active if there are pending interrupts
2455704Snate@binkert.org    if (cpu->checkInterrupts()) {
2462680SN/A        assert(status() == ThreadContext::Active);
2472SN/A        return;
2482SN/A    }
2492SN/A#endif
2502188SN/A*/
2512680SN/A    _status = ThreadContext::Suspended;
2522683Sktlim@umich.edu    cpu->suspendContext(tid);
2532SN/A}
2542SN/A
2552SN/Avoid
2562683Sktlim@umich.eduSimpleThread::deallocate()
257393SN/A{
2582680SN/A    if (status() == ThreadContext::Unallocated)
259393SN/A        return;
260393SN/A
2612680SN/A    _status = ThreadContext::Unallocated;
2622683Sktlim@umich.edu    cpu->deallocateContext(tid);
263393SN/A}
264393SN/A
265393SN/Avoid
2662683Sktlim@umich.eduSimpleThread::halt()
267393SN/A{
2682680SN/A    if (status() == ThreadContext::Halted)
269393SN/A        return;
270393SN/A
2712680SN/A    _status = ThreadContext::Halted;
2722683Sktlim@umich.edu    cpu->haltContext(tid);
273393SN/A}
274393SN/A
275393SN/A
276393SN/Avoid
2772683Sktlim@umich.eduSimpleThread::regStats(const string &name)
2782SN/A{
2792330SN/A#if FULL_SYSTEM
2802341SN/A    if (kernelStats)
2812341SN/A        kernelStats->regStats(name + ".kern");
2822330SN/A#endif
2832SN/A}
284716SN/A
285716SN/Avoid
2862683Sktlim@umich.eduSimpleThread::copyArchRegs(ThreadContext *src_tc)
2872190SN/A{
2882680SN/A    TheISA::copyRegs(src_tc, tc);
2892190SN/A}
2902190SN/A
291