base.hh revision 3520
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;
731634SN/A
741634SN/A  public:
752359SN/A//    Tick currentTick;
761695SN/A    inline Tick frequency() const { return Clock::Frequency / clock; }
771695SN/A    inline Tick cycles(int numCycles) const { return clock * numCycles; }
781695SN/A    inline Tick curCycle() const { return curTick / clock; }
791634SN/A
801858SN/A#if FULL_SYSTEM
812SN/A  protected:
823520Sgblack@eecs.umich.edu//    uint64_t interrupts[TheISA::NumInterruptLevels];
833520Sgblack@eecs.umich.edu//    uint64_t intstatus;
843520Sgblack@eecs.umich.edu    TheISA::Interrupts interrupts;
852SN/A
862SN/A  public:
872SN/A    virtual void post_interrupt(int int_num, int index);
882SN/A    virtual void clear_interrupt(int int_num, int index);
892SN/A    virtual void clear_interrupts();
901133SN/A    bool checkInterrupts;
912SN/A
922SN/A    bool check_interrupt(int int_num) const {
933520Sgblack@eecs.umich.edu        return interrupts.check_interrupt(int_num);
942SN/A    }
952SN/A
963520Sgblack@eecs.umich.edu    bool check_interrupts() const { return interrupts.check_interrupts(); }
973520Sgblack@eecs.umich.edu    //uint64_t intr_status() const { return interrupts.intr_status(); }
981917SN/A
991917SN/A    class ProfileEvent : public Event
1001917SN/A    {
1011917SN/A      private:
1021917SN/A        BaseCPU *cpu;
1031917SN/A        int interval;
1041917SN/A
1051917SN/A      public:
1061917SN/A        ProfileEvent(BaseCPU *cpu, int interval);
1071917SN/A        void process();
1081917SN/A    };
1091917SN/A    ProfileEvent *profileEvent;
1102SN/A#endif
1112SN/A
1122SN/A  protected:
1132680Sktlim@umich.edu    std::vector<ThreadContext *> threadContexts;
1142SN/A
1152SN/A  public:
116393SN/A
117393SN/A    /// Notify the CPU that the indicated context is now active.  The
118393SN/A    /// delay parameter indicates the number of ticks to wait before
119393SN/A    /// executing (typically 0 or 1).
120393SN/A    virtual void activateContext(int thread_num, int delay) {}
121393SN/A
122393SN/A    /// Notify the CPU that the indicated context is now suspended.
123393SN/A    virtual void suspendContext(int thread_num) {}
124393SN/A
125393SN/A    /// Notify the CPU that the indicated context is now deallocated.
126393SN/A    virtual void deallocateContext(int thread_num) {}
127393SN/A
128393SN/A    /// Notify the CPU that the indicated context is now halted.
129393SN/A    virtual void haltContext(int thread_num) {}
1302SN/A
1312SN/A  public:
1321400SN/A    struct Params
1331400SN/A    {
1341400SN/A        std::string name;
1351400SN/A        int numberOfThreads;
1361400SN/A        bool deferRegistration;
1371400SN/A        Counter max_insts_any_thread;
1381400SN/A        Counter max_insts_all_threads;
1391400SN/A        Counter max_loads_any_thread;
1401400SN/A        Counter max_loads_all_threads;
1411695SN/A        Tick clock;
1421400SN/A        bool functionTrace;
1431400SN/A        Tick functionTraceStart;
1442378SN/A        System *system;
1453170Sstever@eecs.umich.edu        int cpu_id;
1461858SN/A#if FULL_SYSTEM
1471917SN/A        Tick profile;
1481400SN/A#endif
1492356SN/A        Tick progress_interval;
1502315SN/A        BaseCPU *checker;
1511917SN/A
1521917SN/A        Params();
1531400SN/A    };
1542SN/A
1551400SN/A    const Params *params;
1562SN/A
1571400SN/A    BaseCPU(Params *params);
1581191SN/A    virtual ~BaseCPU();
1592SN/A
1601129SN/A    virtual void init();
1611917SN/A    virtual void startup();
1622SN/A    virtual void regStats();
1632SN/A
1642103SN/A    virtual void activateWhenReady(int tid) {};
1652103SN/A
1662680Sktlim@umich.edu    void registerThreadContexts();
167180SN/A
1681492SN/A    /// Prepare for another CPU to take over execution.  When it is
1691492SN/A    /// is ready (drained pipe) it signals the sampler.
1702798Sktlim@umich.edu    virtual void switchOut();
171180SN/A
172180SN/A    /// Take over execution from the given CPU.  Used for warm-up and
173180SN/A    /// sampling.
174180SN/A    virtual void takeOverFrom(BaseCPU *);
175180SN/A
176124SN/A    /**
177124SN/A     *  Number of threads we're actually simulating (<= SMT_MAX_THREADS).
178124SN/A     * This is a constant for the duration of the simulation.
179124SN/A     */
1802SN/A    int number_of_threads;
1812SN/A
182124SN/A    /**
183124SN/A     * Vector of per-thread instruction-based event queues.  Used for
184124SN/A     * scheduling events based on number of instructions committed by
185124SN/A     * a particular thread.
186124SN/A     */
187503SN/A    EventQueue **comInstEventQueue;
1882SN/A
189124SN/A    /**
190124SN/A     * Vector of per-thread load-based event queues.  Used for
191124SN/A     * scheduling events based on number of loads committed by
192124SN/A     *a particular thread.
193124SN/A     */
194124SN/A    EventQueue **comLoadEventQueue;
195124SN/A
1962SN/A    System *system;
197921SN/A
1982378SN/A#if FULL_SYSTEM
199921SN/A    /**
200921SN/A     * Serialize this object to the given output stream.
201921SN/A     * @param os The stream to serialize to.
202921SN/A     */
203921SN/A    virtual void serialize(std::ostream &os);
204921SN/A
205921SN/A    /**
206921SN/A     * Reconstruct the state of this object from a checkpoint.
207921SN/A     * @param cp The checkpoint use.
208921SN/A     * @param section The section name of this object
209921SN/A     */
210921SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section);
211921SN/A
2122SN/A#endif
2132SN/A
214124SN/A    /**
215124SN/A     * Return pointer to CPU's branch predictor (NULL if none).
216124SN/A     * @return Branch predictor pointer.
217124SN/A     */
2182SN/A    virtual BranchPred *getBranchPred() { return NULL; };
2192SN/A
220707SN/A    virtual Counter totalInstructions() const { return 0; }
221707SN/A
2221191SN/A    // Function tracing
2231191SN/A  private:
2241191SN/A    bool functionTracingEnabled;
2251191SN/A    std::ostream *functionTraceStream;
2261191SN/A    Addr currentFunctionStart;
2271191SN/A    Addr currentFunctionEnd;
2281191SN/A    Tick functionEntryTick;
2291191SN/A    void enableFunctionTrace();
2301191SN/A    void traceFunctionsInternal(Addr pc);
2311191SN/A
2321191SN/A  protected:
2331191SN/A    void traceFunctions(Addr pc)
2341191SN/A    {
2351191SN/A        if (functionTracingEnabled)
2361191SN/A            traceFunctionsInternal(pc);
2371191SN/A    }
2381191SN/A
2392SN/A  private:
2402SN/A    static std::vector<BaseCPU *> cpuList;   //!< Static global cpu list
2412SN/A
2422SN/A  public:
2432SN/A    static int numSimulatedCPUs() { return cpuList.size(); }
244707SN/A    static Counter numSimulatedInstructions()
245707SN/A    {
246707SN/A        Counter total = 0;
247707SN/A
248707SN/A        int size = cpuList.size();
249707SN/A        for (int i = 0; i < size; ++i)
250707SN/A            total += cpuList[i]->totalInstructions();
251707SN/A
252707SN/A        return total;
253707SN/A    }
254707SN/A
255707SN/A  public:
256707SN/A    // Number of CPU cycles simulated
257729SN/A    Stats::Scalar<> numCycles;
2582SN/A};
2592SN/A
2601717SN/A#endif // __CPU_BASE_HH__
261