simple_thread.cc revision 8761
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" 442SN/A 451858SN/A#if FULL_SYSTEM 463565Sgblack@eecs.umich.edu#include "arch/kernel_stats.hh" 475529Snate@binkert.org#include "arch/stacktrace.hh" 481917SN/A#include "base/callback.hh" 491070SN/A#include "base/cprintf.hh" 501917SN/A#include "base/output.hh" 512188SN/A#include "base/trace.hh" 521917SN/A#include "cpu/profile.hh" 532290SN/A#include "cpu/quiesce_event.hh" 541070SN/A#include "sim/serialize.hh" 551917SN/A#include "sim/sim_exit.hh" 562SN/A#else 575529Snate@binkert.org#include "mem/translating_port.hh" 58360SN/A#include "sim/process.hh" 592519SN/A#include "sim/system.hh" 602SN/A#endif 612SN/A 622SN/Ausing namespace std; 632SN/A 642SN/A// constructor 651858SN/A#if FULL_SYSTEM 662683Sktlim@umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys, 676022Sgblack@eecs.umich.edu TheISA::TLB *_itb, TheISA::TLB *_dtb, 682683Sktlim@umich.edu bool use_kernel_stats) 696324Sgblack@eecs.umich.edu : ThreadState(_cpu, _thread_num), 706324Sgblack@eecs.umich.edu cpu(_cpu), system(_sys), itb(_itb), dtb(_dtb) 712521SN/A 722SN/A{ 732683Sktlim@umich.edu tc = new ProxyThreadContext<SimpleThread>(this); 742190SN/A 752680SN/A quiesceEvent = new EndQuiesceEvent(tc); 762290SN/A 776316Sgblack@eecs.umich.edu clearArchRegs(); 781917SN/A 795529Snate@binkert.org if (cpu->params()->profile) { 801982SN/A profile = new FunctionProfile(system->kernelSymtab); 811917SN/A Callback *cb = 822683Sktlim@umich.edu new MakeCallback<SimpleThread, 832683Sktlim@umich.edu &SimpleThread::dumpFuncProfile>(this); 841917SN/A registerExitCallback(cb); 851917SN/A } 861917SN/A 871917SN/A // let's fill with a dummy node for now so we don't get a segfault 881917SN/A // on the first cycle when there's no node available. 891917SN/A static ProfileNode dummyNode; 901917SN/A profileNode = &dummyNode; 911917SN/A profilePC = 3; 922521SN/A 935482Snate@binkert.org if (use_kernel_stats) 943548Sgblack@eecs.umich.edu kernelStats = new TheISA::Kernel::Statistics(system); 952SN/A} 962SN/A#else 974997Sgblack@eecs.umich.eduSimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, Process *_process, 986331Sgblack@eecs.umich.edu TheISA::TLB *_itb, TheISA::TLB *_dtb) 996331Sgblack@eecs.umich.edu : ThreadState(_cpu, _thread_num, _process), 1004997Sgblack@eecs.umich.edu cpu(_cpu), itb(_itb), dtb(_dtb) 1012SN/A{ 1026316Sgblack@eecs.umich.edu clearArchRegs(); 1032683Sktlim@umich.edu tc = new ProxyThreadContext<SimpleThread>(this); 1042SN/A} 1052190SN/A 1062862Sktlim@umich.edu#endif 1072862Sktlim@umich.edu 1082864Sktlim@umich.eduSimpleThread::SimpleThread() 1092862Sktlim@umich.edu#if FULL_SYSTEM 1105712Shsul@eecs.umich.edu : ThreadState(NULL, -1) 1112862Sktlim@umich.edu#else 1126331Sgblack@eecs.umich.edu : ThreadState(NULL, -1, NULL) 1132862Sktlim@umich.edu#endif 1142190SN/A{ 1152683Sktlim@umich.edu tc = new ProxyThreadContext<SimpleThread>(this); 1162190SN/A} 1172190SN/A 1182683Sktlim@umich.eduSimpleThread::~SimpleThread() 1191070SN/A{ 1208754Sgblack@eecs.umich.edu delete physPort; 1213486Sktlim@umich.edu delete virtPort; 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 1556029Ssteve.reinhardt@amd.com oldContext->setStatus(ThreadContext::Halted); 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 1845714Shsul@eecs.umich.edu 1855715Shsul@eecs.umich.edu _threadId = oldContext->threadId(); 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); 1936315Sgblack@eecs.umich.edu SERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs); 1946316Sgblack@eecs.umich.edu SERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs); 1957720Sgblack@eecs.umich.edu _pcState.serialize(os); 196223SN/A // thread_num and cpu_id are deterministic from the config 1976677SBrad.Beckmann@amd.com 1986677SBrad.Beckmann@amd.com // 1996677SBrad.Beckmann@amd.com // Now must serialize all the ISA dependent state 2006677SBrad.Beckmann@amd.com // 2016678Sgblack@eecs.umich.edu isa.serialize(cpu, os); 202217SN/A} 203217SN/A 204217SN/A 205217SN/Avoid 2062683Sktlim@umich.eduSimpleThread::unserialize(Checkpoint *cp, const std::string §ion) 207217SN/A{ 2082862Sktlim@umich.edu ThreadState::unserialize(cp, section); 2096315Sgblack@eecs.umich.edu UNSERIALIZE_ARRAY(floatRegs.i, TheISA::NumFloatRegs); 2106316Sgblack@eecs.umich.edu UNSERIALIZE_ARRAY(intRegs, TheISA::NumIntRegs); 2117720Sgblack@eecs.umich.edu _pcState.unserialize(cp, section); 212223SN/A // thread_num and cpu_id are deterministic from the config 2136677SBrad.Beckmann@amd.com 2146677SBrad.Beckmann@amd.com // 2156677SBrad.Beckmann@amd.com // Now must unserialize all the ISA dependent state 2166677SBrad.Beckmann@amd.com // 2176678Sgblack@eecs.umich.edu isa.unserialize(cpu, cp, section); 218217SN/A} 219217SN/A 2202683Sktlim@umich.edu#if FULL_SYSTEM 2212683Sktlim@umich.eduvoid 2222683Sktlim@umich.eduSimpleThread::dumpFuncProfile() 2232683Sktlim@umich.edu{ 2242683Sktlim@umich.edu std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name())); 2252683Sktlim@umich.edu profile->dump(tc, *os); 2262683Sktlim@umich.edu} 2272683Sktlim@umich.edu#endif 228217SN/A 229217SN/Avoid 2302683Sktlim@umich.eduSimpleThread::activate(int delay) 2312SN/A{ 2322680SN/A if (status() == ThreadContext::Active) 2332SN/A return; 2342SN/A 2357823Ssteve.reinhardt@amd.com lastActivate = curTick(); 2362188SN/A 2374400Srdreslin@umich.edu// if (status() == ThreadContext::Unallocated) { 2385715Shsul@eecs.umich.edu// cpu->activateWhenReady(_threadId); 2395543Ssaidi@eecs.umich.edu// return; 2404400Srdreslin@umich.edu// } 2412290SN/A 2422680SN/A _status = ThreadContext::Active; 2432290SN/A 2442290SN/A // status() == Suspended 2455715Shsul@eecs.umich.edu cpu->activateContext(_threadId, delay); 246393SN/A} 247393SN/A 248393SN/Avoid 2492683Sktlim@umich.eduSimpleThread::suspend() 250393SN/A{ 2512680SN/A if (status() == ThreadContext::Suspended) 252393SN/A return; 253393SN/A 2547823Ssteve.reinhardt@amd.com lastActivate = curTick(); 2557823Ssteve.reinhardt@amd.com lastSuspend = curTick(); 2562188SN/A/* 2571858SN/A#if FULL_SYSTEM 2582SN/A // Don't change the status from active if there are pending interrupts 2595704Snate@binkert.org if (cpu->checkInterrupts()) { 2602680SN/A assert(status() == ThreadContext::Active); 2612SN/A return; 2622SN/A } 2632SN/A#endif 2642188SN/A*/ 2652680SN/A _status = ThreadContext::Suspended; 2665715Shsul@eecs.umich.edu cpu->suspendContext(_threadId); 2672SN/A} 2682SN/A 269393SN/A 270393SN/Avoid 2712683Sktlim@umich.eduSimpleThread::halt() 272393SN/A{ 2732680SN/A if (status() == ThreadContext::Halted) 274393SN/A return; 275393SN/A 2762680SN/A _status = ThreadContext::Halted; 2775715Shsul@eecs.umich.edu cpu->haltContext(_threadId); 278393SN/A} 279393SN/A 280393SN/A 281393SN/Avoid 2822683Sktlim@umich.eduSimpleThread::regStats(const string &name) 2832SN/A{ 2842330SN/A#if FULL_SYSTEM 2852341SN/A if (kernelStats) 2862341SN/A kernelStats->regStats(name + ".kern"); 2872330SN/A#endif 2882SN/A} 289716SN/A 290716SN/Avoid 2912683Sktlim@umich.eduSimpleThread::copyArchRegs(ThreadContext *src_tc) 2922190SN/A{ 2932680SN/A TheISA::copyRegs(src_tc, tc); 2942190SN/A} 2952190SN/A 296