simple_thread.cc revision 3673:34386ba8cb41
1/* 2 * Copyright (c) 2001-2006 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Steve Reinhardt 29 * Nathan Binkert 30 * Lisa Hsu 31 * Kevin Lim 32 */ 33 34#include <string> 35 36#include "arch/isa_traits.hh" 37#include "cpu/base.hh" 38#include "cpu/simple_thread.hh" 39#include "cpu/thread_context.hh" 40 41#if FULL_SYSTEM 42#include "arch/kernel_stats.hh" 43#include "base/callback.hh" 44#include "base/cprintf.hh" 45#include "base/output.hh" 46#include "base/trace.hh" 47#include "cpu/profile.hh" 48#include "cpu/quiesce_event.hh" 49#include "sim/serialize.hh" 50#include "sim/sim_exit.hh" 51#include "arch/stacktrace.hh" 52#else 53#include "sim/process.hh" 54#include "sim/system.hh" 55#include "mem/translating_port.hh" 56#endif 57 58using namespace std; 59 60// constructor 61#if FULL_SYSTEM 62SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys, 63 TheISA::ITB *_itb, TheISA::DTB *_dtb, 64 bool use_kernel_stats) 65 : ThreadState(_cpu, -1, _thread_num), cpu(_cpu), system(_sys), itb(_itb), 66 dtb(_dtb) 67 68{ 69 tc = new ProxyThreadContext<SimpleThread>(this); 70 71 quiesceEvent = new EndQuiesceEvent(tc); 72 73 regs.clear(); 74 75 if (cpu->params->profile) { 76 profile = new FunctionProfile(system->kernelSymtab); 77 Callback *cb = 78 new MakeCallback<SimpleThread, 79 &SimpleThread::dumpFuncProfile>(this); 80 registerExitCallback(cb); 81 } 82 83 // let's fill with a dummy node for now so we don't get a segfault 84 // on the first cycle when there's no node available. 85 static ProfileNode dummyNode; 86 profileNode = &dummyNode; 87 profilePC = 3; 88 89 if (use_kernel_stats) { 90 kernelStats = new TheISA::Kernel::Statistics(system); 91 } else { 92 kernelStats = NULL; 93 } 94} 95#else 96SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, 97 Process *_process, int _asid) 98 : ThreadState(_cpu, -1, _thread_num, _process, _asid), 99 cpu(_cpu) 100{ 101 regs.clear(); 102 tc = new ProxyThreadContext<SimpleThread>(this); 103} 104 105#endif 106 107#if FULL_SYSTEM 108void 109SimpleThread::init() 110{ 111 Port *mem_port; 112 physPort = new FunctionalPort(csprintf("%s-%d-funcport", 113 cpu->name(), tid)); 114 mem_port = getMemFuncPort(); 115 mem_port->setPeer(physPort); 116 physPort->setPeer(mem_port); 117 118 virtPort = new VirtualPort(csprintf("%s-%d-vport", 119 cpu->name(), tid)); 120 mem_port = getMemFuncPort(); 121 mem_port->setPeer(virtPort); 122 virtPort->setPeer(mem_port); 123} 124#endif 125 126SimpleThread::SimpleThread() 127#if FULL_SYSTEM 128 : ThreadState(NULL, -1, -1) 129#else 130 : ThreadState(NULL, -1, -1, NULL, -1) 131#endif 132{ 133 tc = new ProxyThreadContext<SimpleThread>(this); 134 regs.clear(); 135} 136 137SimpleThread::~SimpleThread() 138{ 139#if FULL_SYSTEM 140 delete physPort; 141 delete virtPort; 142#endif 143 delete tc; 144} 145 146void 147SimpleThread::takeOverFrom(ThreadContext *oldContext) 148{ 149 // some things should already be set up 150#if FULL_SYSTEM 151 assert(system == oldContext->getSystemPtr()); 152#else 153 assert(process == oldContext->getProcessPtr()); 154#endif 155 156 copyState(oldContext); 157#if FULL_SYSTEM 158 EndQuiesceEvent *quiesce = oldContext->getQuiesceEvent(); 159 if (quiesce) { 160 // Point the quiesce event's TC at this TC so that it wakes up 161 // the proper CPU. 162 quiesce->tc = tc; 163 } 164 if (quiesceEvent) { 165 quiesceEvent->tc = tc; 166 } 167 168 TheISA::Kernel::Statistics *stats = oldContext->getKernelStats(); 169 if (stats) { 170 kernelStats = stats; 171 } 172#endif 173 174 storeCondFailures = 0; 175 176 oldContext->setStatus(ThreadContext::Unallocated); 177} 178 179void 180SimpleThread::copyTC(ThreadContext *context) 181{ 182 copyState(context); 183 184#if FULL_SYSTEM 185 EndQuiesceEvent *quiesce = context->getQuiesceEvent(); 186 if (quiesce) { 187 quiesceEvent = quiesce; 188 } 189 TheISA::Kernel::Statistics *stats = context->getKernelStats(); 190 if (stats) { 191 kernelStats = stats; 192 } 193#endif 194} 195 196void 197SimpleThread::copyState(ThreadContext *oldContext) 198{ 199 // copy over functional state 200 _status = oldContext->status(); 201 copyArchRegs(oldContext); 202 cpuId = oldContext->readCpuId(); 203#if !FULL_SYSTEM 204 funcExeInst = oldContext->readFuncExeInst(); 205#endif 206 inst = oldContext->getInst(); 207} 208 209void 210SimpleThread::serialize(ostream &os) 211{ 212 ThreadState::serialize(os); 213 regs.serialize(os); 214 // thread_num and cpu_id are deterministic from the config 215} 216 217 218void 219SimpleThread::unserialize(Checkpoint *cp, const std::string §ion) 220{ 221 ThreadState::unserialize(cp, section); 222 regs.unserialize(cp, section); 223 // thread_num and cpu_id are deterministic from the config 224} 225 226#if FULL_SYSTEM 227void 228SimpleThread::dumpFuncProfile() 229{ 230 std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name())); 231 profile->dump(tc, *os); 232} 233#endif 234 235void 236SimpleThread::activate(int delay) 237{ 238 if (status() == ThreadContext::Active) 239 return; 240 241 lastActivate = curTick; 242 243 if (status() == ThreadContext::Unallocated) { 244 cpu->activateWhenReady(tid); 245 return; 246 } 247 248 _status = ThreadContext::Active; 249 250 // status() == Suspended 251 cpu->activateContext(tid, delay); 252} 253 254void 255SimpleThread::suspend() 256{ 257 if (status() == ThreadContext::Suspended) 258 return; 259 260 lastActivate = curTick; 261 lastSuspend = curTick; 262/* 263#if FULL_SYSTEM 264 // Don't change the status from active if there are pending interrupts 265 if (cpu->check_interrupts()) { 266 assert(status() == ThreadContext::Active); 267 return; 268 } 269#endif 270*/ 271 _status = ThreadContext::Suspended; 272 cpu->suspendContext(tid); 273} 274 275void 276SimpleThread::deallocate() 277{ 278 if (status() == ThreadContext::Unallocated) 279 return; 280 281 _status = ThreadContext::Unallocated; 282 cpu->deallocateContext(tid); 283} 284 285void 286SimpleThread::halt() 287{ 288 if (status() == ThreadContext::Halted) 289 return; 290 291 _status = ThreadContext::Halted; 292 cpu->haltContext(tid); 293} 294 295 296void 297SimpleThread::regStats(const string &name) 298{ 299#if FULL_SYSTEM 300 if (kernelStats) 301 kernelStats->regStats(name + ".kern"); 302#endif 303} 304 305void 306SimpleThread::copyArchRegs(ThreadContext *src_tc) 307{ 308 TheISA::copyRegs(src_tc, tc); 309} 310 311#if FULL_SYSTEM 312VirtualPort* 313SimpleThread::getVirtPort(ThreadContext *src_tc) 314{ 315 if (!src_tc) 316 return virtPort; 317 318 VirtualPort *vp = new VirtualPort("tc-vport", src_tc); 319 Port *mem_port = getMemFuncPort(); 320 321 mem_port->setPeer(vp); 322 vp->setPeer(mem_port); 323 return vp; 324} 325 326void 327SimpleThread::delVirtPort(VirtualPort *vp) 328{ 329 if (vp != virtPort) { 330 delete vp->getPeer(); 331 delete vp; 332 } 333} 334 335#endif 336 337