simple_thread.cc revision 2188
12SN/A/*
211526Sdavid.guillen@arm.com * Copyright (c) 2001-2006 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
141762SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
169983Sstever@gmail.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179983Sstever@gmail.com * "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.
272SN/A */
282SN/A
292SN/A#include <string>
302SN/A
312SN/A#include "cpu/base.hh"
322SN/A#include "cpu/exec_context.hh"
332SN/A
342SN/A#if FULL_SYSTEM
352SN/A#include "base/callback.hh"
362SN/A#include "base/cprintf.hh"
372SN/A#include "base/output.hh"
382SN/A#include "base/trace.hh"
392SN/A#include "cpu/profile.hh"
402SN/A#include "kern/kernel_stats.hh"
412SN/A#include "sim/serialize.hh"
422665Ssaidi@eecs.umich.edu#include "sim/sim_exit.hh"
432665Ssaidi@eecs.umich.edu#include "sim/system.hh"
442665Ssaidi@eecs.umich.edu#include "targetarch/stacktrace.hh"
457897Shestness@cs.utexas.edu#else
462SN/A#include "sim/process.hh"
472SN/A#endif
481388SN/A
498229Snate@binkert.orgusing namespace std;
502SN/A
512SN/A// constructor
527781SAli.Saidi@ARM.com#if FULL_SYSTEM
538229Snate@binkert.orgExecContext::ExecContext(BaseCPU *_cpu, int _thread_num, System *_sys,
541191SN/A                         AlphaITB *_itb, AlphaDTB *_dtb,
551191SN/A                         FunctionalMemory *_mem)
561388SN/A    : _status(ExecContext::Unallocated), cpu(_cpu), thread_num(_thread_num),
575529Snate@binkert.org      cpu_id(-1), lastActivate(0), lastSuspend(0), mem(_mem), itb(_itb),
5810529Smorr@cs.wisc.edu      dtb(_dtb), system(_sys), memctrl(_sys->memctrl), physmem(_sys->physmem),
591717SN/A      kernelBinning(system->kernelBinning), bin(kernelBinning->bin),
602651Ssaidi@eecs.umich.edu      fnbin(kernelBinning->fnbin), profile(NULL), quiesceEvent(this),
618229Snate@binkert.org      func_exe_inst(0), storeCondFailures(0)
622680Sktlim@umich.edu{
6310529Smorr@cs.wisc.edu    kernelStats = new Kernel::Statistics(this);
648232Snate@binkert.org    memset(&regs, 0, sizeof(RegFile));
6510529Smorr@cs.wisc.edu
665529Snate@binkert.org    if (cpu->params->profile) {
6711526Sdavid.guillen@arm.com        profile = new FunctionProfile(system->kernelSymtab);
688779Sgblack@eecs.umich.edu        Callback *cb =
692190SN/A            new MakeCallback<ExecContext, &ExecContext::dumpFuncProfile>(this);
7056SN/A        registerExitCallback(cb);
718229Snate@binkert.org    }
722190SN/A
732SN/A    // let's fill with a dummy node for now so we don't get a segfault
742359SN/A    // on the first cycle when there's no node available.
752359SN/A    static ProfileNode dummyNode;
762359SN/A    profileNode = &dummyNode;
772SN/A    profilePC = 3;
782SN/A}
792SN/A#else
802SN/AExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
812SN/A                         Process *_process, int _asid)
822SN/A    : _status(ExecContext::Unallocated),
832SN/A      cpu(_cpu), thread_num(_thread_num), cpu_id(-1), lastActivate(0),
842SN/A      lastSuspend(0), process(_process), mem(process->getMemory()), asid(_asid),
852SN/A      func_exe_inst(0), storeCondFailures(0)
865606Snate@binkert.org{
876144Sksewell@umich.edu    memset(&regs, 0, sizeof(RegFile));
886144Sksewell@umich.edu}
893126Sktlim@umich.edu
906144Sksewell@umich.eduExecContext::ExecContext(BaseCPU *_cpu, int _thread_num,
917823Ssteve.reinhardt@amd.com                         FunctionalMemory *_mem, int _asid)
923126Sktlim@umich.edu    : cpu(_cpu), thread_num(_thread_num), process(0), mem(_mem), asid(_asid),
933126Sktlim@umich.edu      func_exe_inst(0), storeCondFailures(0)
942356SN/A{
952356SN/A    memset(&regs, 0, sizeof(RegFile));
962356SN/A}
978834Satgutier@umich.edu#endif
9810786Smalek.musleh@gmail.com
9910786Smalek.musleh@gmail.comExecContext::~ExecContext()
10010786Smalek.musleh@gmail.com{
10110786Smalek.musleh@gmail.com#if FULL_SYSTEM
10211321Ssteve.reinhardt@amd.com    delete kernelStats;
10310786Smalek.musleh@gmail.com#endif
10410786Smalek.musleh@gmail.com}
10510786Smalek.musleh@gmail.com
1062356SN/A#if FULL_SYSTEM
1079179Sandreas.hansson@arm.comvoid
1082367SN/AExecContext::dumpFuncProfile()
1096144Sksewell@umich.edu{
1106144Sksewell@umich.edu    std::ostream *os = simout.create(csprintf("profile.%s.dat", cpu->name()));
1116144Sksewell@umich.edu    profile->dump(this, *os);
1122356SN/A}
1132367SN/A
1146144Sksewell@umich.eduExecContext::EndQuiesceEvent::EndQuiesceEvent(ExecContext *_xc)
1157823Ssteve.reinhardt@amd.com    : Event(&mainEventQueue), xc(_xc)
1166144Sksewell@umich.edu{
1172367SN/A}
1182356SN/A
1192356SN/Avoid
1202356SN/AExecContext::EndQuiesceEvent::process()
1212356SN/A{
1225336Shines@cs.fsu.edu    xc->activate();
1232356SN/A}
1244873Sstever@eecs.umich.edu
1252356SN/Aconst char*
1262356SN/AExecContext::EndQuiesceEvent::description()
1278876Sandreas.hansson@arm.com{
12810190Sakash.bagdia@arm.com    return "End Quiesce Event.";
1298832SAli.Saidi@ARM.com}
1308832SAli.Saidi@ARM.com#endif
13111050Sandreas.hansson@arm.com
1329814Sandreas.hansson@arm.comvoid
1339220Shestness@cs.wisc.eduExecContext::takeOverFrom(ExecContext *oldContext)
13410529Smorr@cs.wisc.edu{
13510537Sandreas.hansson@arm.com    // some things should already be set up
13610537Sandreas.hansson@arm.com    assert(mem == oldContext->mem);
13711148Smitch.hayenga@arm.com#if FULL_SYSTEM
1382SN/A    assert(system == oldContext->system);
1395712Shsul@eecs.umich.edu#else
1405712Shsul@eecs.umich.edu    assert(process == oldContext->process);
1415712Shsul@eecs.umich.edu#endif
1425712Shsul@eecs.umich.edu
1435712Shsul@eecs.umich.edu    // copy over functional state
1442SN/A    _status = oldContext->_status;
1452SN/A    regs = oldContext->regs;
1462SN/A    cpu_id = oldContext->cpu_id;
14710190Sakash.bagdia@arm.com    func_exe_inst = oldContext->func_exe_inst;
14810190Sakash.bagdia@arm.com
1495712Shsul@eecs.umich.edu    storeCondFailures = 0;
1506221Snate@binkert.org
1516221Snate@binkert.org    oldContext->_status = ExecContext::Unallocated;
1522SN/A}
1532SN/A
1546221Snate@binkert.orgvoid
1556221Snate@binkert.orgExecContext::serialize(ostream &os)
1566221Snate@binkert.org{
1576221Snate@binkert.org    SERIALIZE_ENUM(_status);
1582SN/A    regs.serialize(os);
1592SN/A    // thread_num and cpu_id are deterministic from the config
1602SN/A    SERIALIZE_SCALAR(func_exe_inst);
1612SN/A    SERIALIZE_SCALAR(inst);
1625606Snate@binkert.org
1635606Snate@binkert.org#if FULL_SYSTEM
1649749Sandreas@sandberg.pp.se    Tick quiesceEndTick = 0;
1659749Sandreas@sandberg.pp.se    if (quiesceEvent.scheduled())
1665606Snate@binkert.org        quiesceEndTick = quiesceEvent.when();
1672SN/A    SERIALIZE_SCALAR(quiesceEndTick);
1689647Sdam.sunwoo@arm.com    kernelStats->serialize(os);
1699647Sdam.sunwoo@arm.com
1709647Sdam.sunwoo@arm.com#endif
1719647Sdam.sunwoo@arm.com}
1729647Sdam.sunwoo@arm.com
1739647Sdam.sunwoo@arm.com
1749749Sandreas@sandberg.pp.sevoid
1759749Sandreas@sandberg.pp.seExecContext::unserialize(Checkpoint *cp, const std::string &section)
1769647Sdam.sunwoo@arm.com{
1779647Sdam.sunwoo@arm.com    UNSERIALIZE_ENUM(_status);
1781400SN/A    regs.unserialize(cp, section);
1795606Snate@binkert.org    // thread_num and cpu_id are deterministic from the config
1805606Snate@binkert.org    UNSERIALIZE_SCALAR(func_exe_inst);
1812SN/A    UNSERIALIZE_SCALAR(inst);
1822SN/A
1832SN/A#if FULL_SYSTEM
1842SN/A    Tick quiesceEndTick;
1856221Snate@binkert.org    UNSERIALIZE_SCALAR(quiesceEndTick);
1866221Snate@binkert.org    if (quiesceEndTick)
1875606Snate@binkert.org        quiesceEvent.schedule(quiesceEndTick);
1886670Shsul@eecs.umich.edu
1895606Snate@binkert.org    kernelStats->unserialize(cp, section);
1902SN/A#endif
1912SN/A}
192124SN/A
1936221Snate@binkert.org
1946221Snate@binkert.orgvoid
1956221Snate@binkert.orgExecContext::activate(int delay)
196124SN/A{
197124SN/A    if (status() == Active)
198124SN/A        return;
199124SN/A
2005606Snate@binkert.org    lastActivate = curTick;
2015606Snate@binkert.org
2029749Sandreas@sandberg.pp.se    _status = Active;
2039749Sandreas@sandberg.pp.se    cpu->activateContext(thread_num, delay);
2045606Snate@binkert.org}
205124SN/A
2061400SN/Avoid
2075606Snate@binkert.orgExecContext::suspend()
208124SN/A{
209124SN/A    if (status() == Suspended)
210124SN/A        return;
211124SN/A
2126221Snate@binkert.org    lastActivate = curTick;
2136221Snate@binkert.org    lastSuspend = curTick;
2145606Snate@binkert.org/*
2156221Snate@binkert.org#if FULL_SYSTEM
2165606Snate@binkert.org    // Don't change the status from active if there are pending interrupts
217124SN/A    if (cpu->check_interrupts()) {
218124SN/A        assert(status() == Active);
2191191SN/A        return;
2205529Snate@binkert.org    }
2218634Schris.emmons@arm.com#endif
22211359Sandreas@sandberg.pp.se*/
2238634Schris.emmons@arm.com    _status = Suspended;
2241191SN/A    cpu->suspendContext(thread_num);
2255529Snate@binkert.org}
2261191SN/A
2275529Snate@binkert.orgvoid
2281191SN/AExecContext::deallocate()
2291191SN/A{
2305606Snate@binkert.org    if (status() == Unallocated)
2315606Snate@binkert.org        return;
2325606Snate@binkert.org
2331191SN/A    _status = Unallocated;
2341191SN/A    cpu->deallocateContext(thread_num);
2358876Sandreas.hansson@arm.com}
2368876Sandreas.hansson@arm.com
2378876Sandreas.hansson@arm.comvoid
2389433SAndreas.Sandberg@ARM.comExecContext::halt()
23911221Sandreas.sandberg@arm.com{
24011221Sandreas.sandberg@arm.com    if (status() == Halted)
24111221Sandreas.sandberg@arm.com        return;
24211221Sandreas.sandberg@arm.com
24311221Sandreas.sandberg@arm.com    _status = Halted;
24411221Sandreas.sandberg@arm.com    cpu->haltContext(thread_num);
2458876Sandreas.hansson@arm.com}
2465810Sgblack@eecs.umich.edu
2478779Sgblack@eecs.umich.edu
2488779Sgblack@eecs.umich.eduvoid
2498779Sgblack@eecs.umich.eduExecContext::regStats(const string &name)
2508779Sgblack@eecs.umich.edu{
2515529Snate@binkert.org#if FULL_SYSTEM
2529384SAndreas.Sandberg@arm.com    kernelStats->regStats(name + ".kern");
2539384SAndreas.Sandberg@arm.com#endif
2549384SAndreas.Sandberg@arm.com}
2559384SAndreas.Sandberg@arm.com
2569384SAndreas.Sandberg@arm.comvoid
2571917SN/AExecContext::trap(Fault fault)
2581191SN/A{
2591191SN/A    //TheISA::trap(fault);    //One possible way to do it...
2601191SN/A
2611191SN/A    /** @todo: Going to hack it for now.  Do a true fixup later. */
2621191SN/A#if FULL_SYSTEM
2631191SN/A    ev5_trap(fault);
2641191SN/A#else
2651191SN/A    fatal("fault (%d) detected @ PC 0x%08p", fault, readPC());
2661191SN/A#endif
2679086Sandreas.hansson@arm.com}
2689086Sandreas.hansson@arm.com