base.hh revision 5712
12SN/A/* 29448SAndreas.Sandberg@ARM.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 37338SAli.Saidi@ARM.com * All rights reserved. 47338SAli.Saidi@ARM.com * 57338SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without 67338SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are 77338SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright 87338SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer; 97338SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright 107338SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the 117338SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution; 127338SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its 137338SAli.Saidi@ARM.com * contributors may be used to endorse or promote products derived from 141762SN/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. 272SN/A * 282SN/A * Authors: Steve Reinhardt 292SN/A * Nathan Binkert 302SN/A */ 312SN/A 322SN/A#ifndef __CPU_BASE_HH__ 332SN/A#define __CPU_BASE_HH__ 342SN/A 352SN/A#include <vector> 362SN/A 372SN/A#include "arch/isa_traits.hh" 382SN/A#include "arch/microcode_rom.hh" 392665Ssaidi@eecs.umich.edu#include "base/statistics.hh" 402665Ssaidi@eecs.umich.edu#include "config/full_system.hh" 412SN/A#include "sim/eventq.hh" 422SN/A#include "sim/insttracer.hh" 438779Sgblack@eecs.umich.edu#include "mem/mem_object.hh" 448779Sgblack@eecs.umich.edu 458779Sgblack@eecs.umich.edu#if FULL_SYSTEM 462439SN/A#include "arch/interrupts.hh" 478779Sgblack@eecs.umich.edu#endif 488229Snate@binkert.org 496216Snate@binkert.orgclass BaseCPUParams; 50146SN/Aclass BranchPred; 51146SN/Aclass CheckerCPU; 52146SN/Aclass ThreadContext; 53146SN/Aclass System; 54146SN/Aclass Port; 556216Snate@binkert.org 566658Snate@binkert.orgnamespace TheISA 578229Snate@binkert.org{ 581717SN/A class Predecoder; 598887Sgeoffrey.blake@arm.com} 608887Sgeoffrey.blake@arm.com 61146SN/Aclass CPUProgressEvent : public Event 621977SN/A{ 632683Sktlim@umich.edu protected: 641717SN/A Tick interval; 65146SN/A Counter lastNumInst; 662683Sktlim@umich.edu BaseCPU *cpu; 678232Snate@binkert.org 688232Snate@binkert.org public: 698232Snate@binkert.org CPUProgressEvent(BaseCPU *_cpu, Tick ival); 708779Sgblack@eecs.umich.edu 713348Sbinkertn@umich.edu void process(); 726105Ssteve.reinhardt@amd.com 736216Snate@binkert.org virtual const char *description() const; 742036SN/A}; 75146SN/A 768817Sgblack@eecs.umich.educlass BaseCPU : public MemObject 778793Sgblack@eecs.umich.edu{ 7856SN/A protected: 7956SN/A // CPU's clock period in terms of the number of ticks of curTime. 80695SN/A Tick clock; 812901Ssaidi@eecs.umich.edu // @todo remove me after debugging with legion done 822SN/A Tick instCnt; 832SN/A // every cpu has an id, put it in the base cpu 842449SN/A // Set at initialization, only time a cpuId might change is during a 851355SN/A // takeover (which should be done from within the BaseCPU anyway, 865529Snate@binkert.org // therefore no setCpuId() method is provided 879023Sgblack@eecs.umich.edu int _cpuId; 88224SN/A 898793Sgblack@eecs.umich.edu public: 909384SAndreas.Sandberg@arm.com /** Reads this CPU's ID. */ 919384SAndreas.Sandberg@arm.com int cpuId() { return _cpuId; } 928793Sgblack@eecs.umich.edu 938820Sgblack@eecs.umich.edu// Tick currentTick; 949384SAndreas.Sandberg@arm.com inline Tick frequency() const { return Clock::Frequency / clock; } 952SN/A inline Tick ticks(int numCycles) const { return clock * numCycles; } 966029Ssteve.reinhardt@amd.com inline Tick curCycle() const { return curTick / clock; } 972672Sktlim@umich.edu inline Tick tickToCycles(Tick val) const { return val / clock; } 982683Sktlim@umich.edu // @todo remove me after debugging with legion done 992SN/A Tick instCount() { return instCnt; } 1008733Sgeoffrey.blake@arm.com 1018733Sgeoffrey.blake@arm.com /** The next cycle the CPU should be scheduled, given a cache 1028733Sgeoffrey.blake@arm.com * access or quiesce event returning on this cycle. This function 1038733Sgeoffrey.blake@arm.com * may return curTick if the CPU should run on the current cycle. 1048733Sgeoffrey.blake@arm.com */ 1058733Sgeoffrey.blake@arm.com Tick nextCycle(); 1068733Sgeoffrey.blake@arm.com 1078733Sgeoffrey.blake@arm.com /** The next cycle the CPU should be scheduled, given a cache 1088733Sgeoffrey.blake@arm.com * access or quiesce event returning on the given Tick. This 1098733Sgeoffrey.blake@arm.com * function may return curTick if the CPU should run on the 1108733Sgeoffrey.blake@arm.com * current cycle. 1112SN/A * @param begin_tick The tick that the event is completing on. 112334SN/A */ 1138834Satgutier@umich.edu Tick nextCycle(Tick begin_tick); 1148834Satgutier@umich.edu 115140SN/A TheISA::MicrocodeRom microcodeRom; 116334SN/A 1172SN/A#if FULL_SYSTEM 1182SN/A protected: 1192SN/A TheISA::Interrupts *interrupts; 1202680Sktlim@umich.edu 1214377Sgblack@eecs.umich.edu public: 1225169Ssaidi@eecs.umich.edu TheISA::Interrupts * 1234377Sgblack@eecs.umich.edu getInterruptController() 1244377Sgblack@eecs.umich.edu { 1252SN/A return interrupts; 1262SN/A } 1272623SN/A 1282SN/A virtual void postInterrupt(int int_num, int index); 1292SN/A virtual void clearInterrupt(int int_num, int index); 1302SN/A virtual void clearInterrupts(); 131180SN/A 1328737Skoansin.tan@gmail.com bool 133393SN/A checkInterrupts(ThreadContext *tc) const 134393SN/A { 135393SN/A return interrupts->checkInterrupts(tc); 136393SN/A } 137384SN/A 138384SN/A class ProfileEvent : public Event 139393SN/A { 1408737Skoansin.tan@gmail.com private: 141393SN/A BaseCPU *cpu; 142393SN/A Tick interval; 143393SN/A 144393SN/A public: 145384SN/A ProfileEvent(BaseCPU *cpu, Tick interval); 146189SN/A void process(); 147189SN/A }; 1482623SN/A ProfileEvent *profileEvent; 1492SN/A#endif 150729SN/A 151334SN/A protected: 1522SN/A std::vector<ThreadContext *> threadContexts; 1532SN/A std::vector<TheISA::Predecoder *> predecoders; 1542SN/A 1558834Satgutier@umich.edu Trace::InstTracer * tracer; 1568834Satgutier@umich.edu 1578834Satgutier@umich.edu public: 1588834Satgutier@umich.edu 1598834Satgutier@umich.edu /// Provide access to the tracer pointer 1608834Satgutier@umich.edu Trace::InstTracer * getTracer() { return tracer; } 1618834Satgutier@umich.edu 1622SN/A /// Notify the CPU that the indicated context is now active. The 1632SN/A /// delay parameter indicates the number of ticks to wait before 1647897Shestness@cs.utexas.edu /// executing (typically 0 or 1). 1657897Shestness@cs.utexas.edu virtual void activateContext(int thread_num, int delay) {} 1667897Shestness@cs.utexas.edu 1677897Shestness@cs.utexas.edu /// Notify the CPU that the indicated context is now suspended. 1687897Shestness@cs.utexas.edu virtual void suspendContext(int thread_num) {} 1697897Shestness@cs.utexas.edu 1707897Shestness@cs.utexas.edu /// Notify the CPU that the indicated context is now deallocated. 1717897Shestness@cs.utexas.edu virtual void deallocateContext(int thread_num) {} 1727897Shestness@cs.utexas.edu 1737897Shestness@cs.utexas.edu /// Notify the CPU that the indicated context is now halted. 1747897Shestness@cs.utexas.edu virtual void haltContext(int thread_num) {} 1757897Shestness@cs.utexas.edu 1767897Shestness@cs.utexas.edu /// Given a Thread Context pointer return the thread num 1777897Shestness@cs.utexas.edu int findContext(ThreadContext *tc); 1787897Shestness@cs.utexas.edu 1797897Shestness@cs.utexas.edu /// Given a thread num get tho thread context for it 1807897Shestness@cs.utexas.edu ThreadContext *getContext(int tn) { return threadContexts[tn]; } 1817897Shestness@cs.utexas.edu 1827897Shestness@cs.utexas.edu public: 1837897Shestness@cs.utexas.edu typedef BaseCPUParams Params; 1847897Shestness@cs.utexas.edu const Params *params() const 1857897Shestness@cs.utexas.edu { return reinterpret_cast<const Params *>(_params); } 1867897Shestness@cs.utexas.edu BaseCPU(Params *params); 1877897Shestness@cs.utexas.edu virtual ~BaseCPU(); 1887897Shestness@cs.utexas.edu 1897897Shestness@cs.utexas.edu virtual void init(); 1907897Shestness@cs.utexas.edu virtual void startup(); 1917897Shestness@cs.utexas.edu virtual void regStats(); 1927897Shestness@cs.utexas.edu 1937897Shestness@cs.utexas.edu virtual void activateWhenReady(int tid) {}; 1947897Shestness@cs.utexas.edu 1957897Shestness@cs.utexas.edu void registerThreadContexts(); 1967897Shestness@cs.utexas.edu 1977897Shestness@cs.utexas.edu /// Prepare for another CPU to take over execution. When it is 1987897Shestness@cs.utexas.edu /// is ready (drained pipe) it signals the sampler. 1997897Shestness@cs.utexas.edu virtual void switchOut(); 2007897Shestness@cs.utexas.edu 2017897Shestness@cs.utexas.edu /// Take over execution from the given CPU. Used for warm-up and 2027897Shestness@cs.utexas.edu /// sampling. 2037897Shestness@cs.utexas.edu virtual void takeOverFrom(BaseCPU *, Port *ic, Port *dc); 2047897Shestness@cs.utexas.edu 2057897Shestness@cs.utexas.edu /** 2067897Shestness@cs.utexas.edu * Number of threads we're actually simulating (<= SMT_MAX_THREADS). 2077897Shestness@cs.utexas.edu * This is a constant for the duration of the simulation. 2087897Shestness@cs.utexas.edu */ 2097897Shestness@cs.utexas.edu int number_of_threads; 2107897Shestness@cs.utexas.edu 2117897Shestness@cs.utexas.edu TheISA::CoreSpecific coreParams; //ISA-Specific Params That Set Up State in Core 2127897Shestness@cs.utexas.edu 2137897Shestness@cs.utexas.edu /** 2142SN/A * Vector of per-thread instruction-based event queues. Used for 2157897Shestness@cs.utexas.edu * scheduling events based on number of instructions committed by 2167897Shestness@cs.utexas.edu * a particular thread. 2177897Shestness@cs.utexas.edu */ 2187897Shestness@cs.utexas.edu EventQueue **comInstEventQueue; 2197897Shestness@cs.utexas.edu 2207897Shestness@cs.utexas.edu /** 2217897Shestness@cs.utexas.edu * Vector of per-thread load-based event queues. Used for 2227897Shestness@cs.utexas.edu * scheduling events based on number of loads committed by 2237897Shestness@cs.utexas.edu *a particular thread. 2247897Shestness@cs.utexas.edu */ 2257897Shestness@cs.utexas.edu EventQueue **comLoadEventQueue; 2267897Shestness@cs.utexas.edu 2272SN/A System *system; 2282SN/A 2291001SN/A Tick phase; 2301001SN/A 2311001SN/A#if FULL_SYSTEM 2321001SN/A /** 2331001SN/A * Serialize this object to the given output stream. 2342SN/A * @param os The stream to serialize to. 2352SN/A */ 2362SN/A virtual void serialize(std::ostream &os); 2372SN/A 2382SN/A /** 2397897Shestness@cs.utexas.edu * Reconstruct the state of this object from a checkpoint. 2407897Shestness@cs.utexas.edu * @param cp The checkpoint use. 2417897Shestness@cs.utexas.edu * @param section The section name of this object 2427897Shestness@cs.utexas.edu */ 2437897Shestness@cs.utexas.edu virtual void unserialize(Checkpoint *cp, const std::string §ion); 2447897Shestness@cs.utexas.edu 2457897Shestness@cs.utexas.edu#endif 2467897Shestness@cs.utexas.edu 2477897Shestness@cs.utexas.edu /** 2487897Shestness@cs.utexas.edu * Return pointer to CPU's branch predictor (NULL if none). 2492SN/A * @return Branch predictor pointer. 2502SN/A */ 2512SN/A virtual BranchPred *getBranchPred() { return NULL; }; 2522SN/A 2532SN/A virtual Counter totalInstructions() const { return 0; } 2542SN/A 2552SN/A // Function tracing 2562SN/A private: 2572SN/A bool functionTracingEnabled; 2582SN/A std::ostream *functionTraceStream; 2592SN/A Addr currentFunctionStart; 2602SN/A Addr currentFunctionEnd; 2612390SN/A Tick functionEntryTick; 2622390SN/A void enableFunctionTrace(); 2632390SN/A void traceFunctionsInternal(Addr pc); 2642390SN/A 2652390SN/A protected: 2662390SN/A void traceFunctions(Addr pc) 2672390SN/A { 2682390SN/A if (functionTracingEnabled) 2692390SN/A traceFunctionsInternal(pc); 2702390SN/A } 2712390SN/A 2722390SN/A private: 273385SN/A static std::vector<BaseCPU *> cpuList; //!< Static global cpu list 2747897Shestness@cs.utexas.edu 2757897Shestness@cs.utexas.edu public: 2762SN/A static int numSimulatedCPUs() { return cpuList.size(); } 2772SN/A static Counter numSimulatedInstructions() 2782SN/A { 2792623SN/A Counter total = 0; 280334SN/A 2812361SN/A int size = cpuList.size(); 2825496Ssaidi@eecs.umich.edu for (int i = 0; i < size; ++i) 283334SN/A total += cpuList[i]->totalInstructions(); 284334SN/A 285334SN/A return total; 2869448SAndreas.Sandberg@ARM.com } 2872SN/A 2889448SAndreas.Sandberg@ARM.com public: 2899448SAndreas.Sandberg@ARM.com // Number of CPU cycles simulated 2909448SAndreas.Sandberg@ARM.com Stats::Scalar<> numCycles; 2912683Sktlim@umich.edu}; 2922SN/A 2932SN/A#endif // __CPU_BASE_HH__ 2942SN/A