base.hh revision 4000
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 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.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Nathan Binkert
302SN/A */
312SN/A
321717SN/A#ifndef __CPU_BASE_HH__
331717SN/A#define __CPU_BASE_HH__
342SN/A
352SN/A#include <vector>
362SN/A
37707SN/A#include "base/statistics.hh"
381858SN/A#include "config/full_system.hh"
3956SN/A#include "sim/eventq.hh"
402856Srdreslin@umich.edu#include "mem/mem_object.hh"
412109SN/A#include "arch/isa_traits.hh"
422SN/A
433520Sgblack@eecs.umich.edu#if FULL_SYSTEM
443520Sgblack@eecs.umich.edu#include "arch/interrupts.hh"
453520Sgblack@eecs.umich.edu#endif
463520Sgblack@eecs.umich.edu
472190SN/Aclass BranchPred;
482315SN/Aclass CheckerCPU;
492680Sktlim@umich.educlass ThreadContext;
502SN/Aclass System;
512856Srdreslin@umich.educlass Port;
522SN/A
532356SN/Aclass CPUProgressEvent : public Event
542356SN/A{
552356SN/A  protected:
562356SN/A    Tick interval;
572356SN/A    Counter lastNumInst;
582356SN/A    BaseCPU *cpu;
592356SN/A
602356SN/A  public:
613126Sktlim@umich.edu    CPUProgressEvent(EventQueue *q, Tick ival, BaseCPU *_cpu);
622356SN/A
632356SN/A    void process();
642356SN/A
652356SN/A    virtual const char *description();
662356SN/A};
672356SN/A
682856Srdreslin@umich.educlass BaseCPU : public MemObject
692SN/A{
701634SN/A  protected:
711634SN/A    // CPU's clock period in terms of the number of ticks of curTime.
721695SN/A    Tick clock;
733814Ssaidi@eecs.umich.edu    // @todo remove me after debugging with legion done
743814Ssaidi@eecs.umich.edu    Tick instCnt;
751634SN/A
761634SN/A  public:
772359SN/A//    Tick currentTick;
781695SN/A    inline Tick frequency() const { return Clock::Frequency / clock; }
791695SN/A    inline Tick cycles(int numCycles) const { return clock * numCycles; }
801695SN/A    inline Tick curCycle() const { return curTick / clock; }
813814Ssaidi@eecs.umich.edu    // @todo remove me after debugging with legion done
823814Ssaidi@eecs.umich.edu    Tick instCount() { return instCnt; }
831634SN/A
843495Sktlim@umich.edu    /** The next cycle the CPU should be scheduled, given a cache
853495Sktlim@umich.edu     * access or quiesce event returning on this cycle.  This function
863495Sktlim@umich.edu     * may return curTick if the CPU should run on the current cycle.
873495Sktlim@umich.edu     */
883495Sktlim@umich.edu    Tick nextCycle();
893495Sktlim@umich.edu
903495Sktlim@umich.edu    /** The next cycle the CPU should be scheduled, given a cache
913495Sktlim@umich.edu     * access or quiesce event returning on the given Tick.  This
923495Sktlim@umich.edu     * function may return curTick if the CPU should run on the
933495Sktlim@umich.edu     * current cycle.
943495Sktlim@umich.edu     * @param begin_tick The tick that the event is completing on.
953495Sktlim@umich.edu     */
963495Sktlim@umich.edu    Tick nextCycle(Tick begin_tick);
973495Sktlim@umich.edu
981858SN/A#if FULL_SYSTEM
992SN/A  protected:
1003520Sgblack@eecs.umich.edu//    uint64_t interrupts[TheISA::NumInterruptLevels];
1013520Sgblack@eecs.umich.edu//    uint64_t intstatus;
1023520Sgblack@eecs.umich.edu    TheISA::Interrupts interrupts;
1032SN/A
1042SN/A  public:
1053894Shsul@eecs.umich.edu    virtual void post_interrupt(int int_type);
1062SN/A    virtual void post_interrupt(int int_num, int index);
1072SN/A    virtual void clear_interrupt(int int_num, int index);
1082SN/A    virtual void clear_interrupts();
1092SN/A
1103521Sgblack@eecs.umich.edu    bool check_interrupts(ThreadContext * tc) const
1113521Sgblack@eecs.umich.edu    { return interrupts.check_interrupts(tc); }
1121917SN/A
1131917SN/A    class ProfileEvent : public Event
1141917SN/A    {
1151917SN/A      private:
1161917SN/A        BaseCPU *cpu;
1171917SN/A        int interval;
1181917SN/A
1191917SN/A      public:
1201917SN/A        ProfileEvent(BaseCPU *cpu, int interval);
1211917SN/A        void process();
1221917SN/A    };
1231917SN/A    ProfileEvent *profileEvent;
1242SN/A#endif
1252SN/A
1262SN/A  protected:
1272680Sktlim@umich.edu    std::vector<ThreadContext *> threadContexts;
1282SN/A
1292SN/A  public:
130393SN/A
131393SN/A    /// Notify the CPU that the indicated context is now active.  The
132393SN/A    /// delay parameter indicates the number of ticks to wait before
133393SN/A    /// executing (typically 0 or 1).
134393SN/A    virtual void activateContext(int thread_num, int delay) {}
135393SN/A
136393SN/A    /// Notify the CPU that the indicated context is now suspended.
137393SN/A    virtual void suspendContext(int thread_num) {}
138393SN/A
139393SN/A    /// Notify the CPU that the indicated context is now deallocated.
140393SN/A    virtual void deallocateContext(int thread_num) {}
141393SN/A
142393SN/A    /// Notify the CPU that the indicated context is now halted.
143393SN/A    virtual void haltContext(int thread_num) {}
1442SN/A
1454000Ssaidi@eecs.umich.edu   /// Given a Thread Context pointer return the thread num
1464000Ssaidi@eecs.umich.edu   int findContext(ThreadContext *tc);
1474000Ssaidi@eecs.umich.edu
1484000Ssaidi@eecs.umich.edu   /// Given a thread num get tho thread context for it
1494000Ssaidi@eecs.umich.edu   ThreadContext *getContext(int tn) { return threadContexts[tn]; }
1504000Ssaidi@eecs.umich.edu
1512SN/A  public:
1521400SN/A    struct Params
1531400SN/A    {
1541400SN/A        std::string name;
1551400SN/A        int numberOfThreads;
1561400SN/A        bool deferRegistration;
1571400SN/A        Counter max_insts_any_thread;
1581400SN/A        Counter max_insts_all_threads;
1591400SN/A        Counter max_loads_any_thread;
1601400SN/A        Counter max_loads_all_threads;
1611695SN/A        Tick clock;
1621400SN/A        bool functionTrace;
1631400SN/A        Tick functionTraceStart;
1642378SN/A        System *system;
1653170Sstever@eecs.umich.edu        int cpu_id;
1663661Srdreslin@umich.edu        Tick phase;
1671858SN/A#if FULL_SYSTEM
1681917SN/A        Tick profile;
1693617Sbinkertn@umich.edu
1703617Sbinkertn@umich.edu        bool do_statistics_insts;
1713617Sbinkertn@umich.edu        bool do_checkpoint_insts;
1723617Sbinkertn@umich.edu        bool do_quiesce;
1731400SN/A#endif
1742356SN/A        Tick progress_interval;
1752315SN/A        BaseCPU *checker;
1761917SN/A
1771917SN/A        Params();
1781400SN/A    };
1792SN/A
1801400SN/A    const Params *params;
1812SN/A
1821400SN/A    BaseCPU(Params *params);
1831191SN/A    virtual ~BaseCPU();
1842SN/A
1851129SN/A    virtual void init();
1861917SN/A    virtual void startup();
1872SN/A    virtual void regStats();
1882SN/A
1892103SN/A    virtual void activateWhenReady(int tid) {};
1902103SN/A
1912680Sktlim@umich.edu    void registerThreadContexts();
192180SN/A
1931492SN/A    /// Prepare for another CPU to take over execution.  When it is
1941492SN/A    /// is ready (drained pipe) it signals the sampler.
1952798Sktlim@umich.edu    virtual void switchOut();
196180SN/A
197180SN/A    /// Take over execution from the given CPU.  Used for warm-up and
198180SN/A    /// sampling.
199180SN/A    virtual void takeOverFrom(BaseCPU *);
200180SN/A
201124SN/A    /**
202124SN/A     *  Number of threads we're actually simulating (<= SMT_MAX_THREADS).
203124SN/A     * This is a constant for the duration of the simulation.
204124SN/A     */
2052SN/A    int number_of_threads;
2062SN/A
207124SN/A    /**
208124SN/A     * Vector of per-thread instruction-based event queues.  Used for
209124SN/A     * scheduling events based on number of instructions committed by
210124SN/A     * a particular thread.
211124SN/A     */
212503SN/A    EventQueue **comInstEventQueue;
2132SN/A
214124SN/A    /**
215124SN/A     * Vector of per-thread load-based event queues.  Used for
216124SN/A     * scheduling events based on number of loads committed by
217124SN/A     *a particular thread.
218124SN/A     */
219124SN/A    EventQueue **comLoadEventQueue;
220124SN/A
2212SN/A    System *system;
222921SN/A
2233661Srdreslin@umich.edu    Tick phase;
2243661Srdreslin@umich.edu
2252378SN/A#if FULL_SYSTEM
226921SN/A    /**
227921SN/A     * Serialize this object to the given output stream.
228921SN/A     * @param os The stream to serialize to.
229921SN/A     */
230921SN/A    virtual void serialize(std::ostream &os);
231921SN/A
232921SN/A    /**
233921SN/A     * Reconstruct the state of this object from a checkpoint.
234921SN/A     * @param cp The checkpoint use.
235921SN/A     * @param section The section name of this object
236921SN/A     */
237921SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
238921SN/A
2392SN/A#endif
2402SN/A
241124SN/A    /**
242124SN/A     * Return pointer to CPU's branch predictor (NULL if none).
243124SN/A     * @return Branch predictor pointer.
244124SN/A     */
2452SN/A    virtual BranchPred *getBranchPred() { return NULL; };
2462SN/A
247707SN/A    virtual Counter totalInstructions() const { return 0; }
248707SN/A
2491191SN/A    // Function tracing
2501191SN/A  private:
2511191SN/A    bool functionTracingEnabled;
2521191SN/A    std::ostream *functionTraceStream;
2531191SN/A    Addr currentFunctionStart;
2541191SN/A    Addr currentFunctionEnd;
2551191SN/A    Tick functionEntryTick;
2561191SN/A    void enableFunctionTrace();
2571191SN/A    void traceFunctionsInternal(Addr pc);
2581191SN/A
2591191SN/A  protected:
2601191SN/A    void traceFunctions(Addr pc)
2611191SN/A    {
2621191SN/A        if (functionTracingEnabled)
2631191SN/A            traceFunctionsInternal(pc);
2641191SN/A    }
2651191SN/A
2662SN/A  private:
2672SN/A    static std::vector<BaseCPU *> cpuList;   //!< Static global cpu list
2682SN/A
2692SN/A  public:
2702SN/A    static int numSimulatedCPUs() { return cpuList.size(); }
271707SN/A    static Counter numSimulatedInstructions()
272707SN/A    {
273707SN/A        Counter total = 0;
274707SN/A
275707SN/A        int size = cpuList.size();
276707SN/A        for (int i = 0; i < size; ++i)
277707SN/A            total += cpuList[i]->totalInstructions();
278707SN/A
279707SN/A        return total;
280707SN/A    }
281707SN/A
282707SN/A  public:
283707SN/A    // Number of CPU cycles simulated
284729SN/A    Stats::Scalar<> numCycles;
2852SN/A};
2862SN/A
2871717SN/A#endif // __CPU_BASE_HH__
288