base.hh revision 3617
12847Sksewell@umich.edu/*
27783SGiacomo.Gabrielli@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
39913Ssteve.reinhardt@amd.com * All rights reserved.
47783SGiacomo.Gabrielli@arm.com *
57783SGiacomo.Gabrielli@arm.com * Redistribution and use in source and binary forms, with or without
67783SGiacomo.Gabrielli@arm.com * modification, are permitted provided that the following conditions are
77783SGiacomo.Gabrielli@arm.com * met: redistributions of source code must retain the above copyright
87783SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer;
97783SGiacomo.Gabrielli@arm.com * redistributions in binary form must reproduce the above copyright
107783SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer in the
117783SGiacomo.Gabrielli@arm.com * documentation and/or other materials provided with the distribution;
127783SGiacomo.Gabrielli@arm.com * neither the name of the copyright holders nor the names of its
137783SGiacomo.Gabrielli@arm.com * contributors may be used to endorse or promote products derived from
147783SGiacomo.Gabrielli@arm.com * this software without specific prior written permission.
155596Sgblack@eecs.umich.edu *
162847Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172847Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182847Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192847Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202847Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212847Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222847Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232847Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242847Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252847Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262847Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272847Sksewell@umich.edu *
282847Sksewell@umich.edu * Authors: Steve Reinhardt
292847Sksewell@umich.edu *          Nathan Binkert
302847Sksewell@umich.edu */
312847Sksewell@umich.edu
322847Sksewell@umich.edu#ifndef __CPU_BASE_HH__
332847Sksewell@umich.edu#define __CPU_BASE_HH__
342847Sksewell@umich.edu
352847Sksewell@umich.edu#include <vector>
362847Sksewell@umich.edu
372847Sksewell@umich.edu#include "base/statistics.hh"
382847Sksewell@umich.edu#include "config/full_system.hh"
392847Sksewell@umich.edu#include "sim/eventq.hh"
402847Sksewell@umich.edu#include "mem/mem_object.hh"
415596Sgblack@eecs.umich.edu#include "arch/isa_traits.hh"
422847Sksewell@umich.edu
432847Sksewell@umich.edu#if FULL_SYSTEM
442847Sksewell@umich.edu#include "arch/interrupts.hh"
452847Sksewell@umich.edu#endif
462847Sksewell@umich.edu
4710835Sandreas.hansson@arm.comclass BranchPred;
4810835Sandreas.hansson@arm.comclass CheckerCPU;
495596Sgblack@eecs.umich.educlass ThreadContext;
506658Snate@binkert.orgclass System;
518229Snate@binkert.orgclass Port;
528229Snate@binkert.org
535596Sgblack@eecs.umich.educlass CPUProgressEvent : public Event
545596Sgblack@eecs.umich.edu{
559913Ssteve.reinhardt@amd.com  protected:
562847Sksewell@umich.edu    Tick interval;
575596Sgblack@eecs.umich.edu    Counter lastNumInst;
585596Sgblack@eecs.umich.edu    BaseCPU *cpu;
595596Sgblack@eecs.umich.edu
605596Sgblack@eecs.umich.edu  public:
615596Sgblack@eecs.umich.edu    CPUProgressEvent(EventQueue *q, Tick ival, BaseCPU *_cpu);
625596Sgblack@eecs.umich.edu
635596Sgblack@eecs.umich.edu    void process();
645596Sgblack@eecs.umich.edu
655596Sgblack@eecs.umich.edu    virtual const char *description();
665596Sgblack@eecs.umich.edu};
675596Sgblack@eecs.umich.edu
685596Sgblack@eecs.umich.educlass BaseCPU : public MemObject
695596Sgblack@eecs.umich.edu{
7012104Snathanael.premillieu@arm.com  protected:
715596Sgblack@eecs.umich.edu    // CPU's clock period in terms of the number of ticks of curTime.
725596Sgblack@eecs.umich.edu    Tick clock;
735596Sgblack@eecs.umich.edu
749920Syasuko.eckert@amd.com  public:
7510319SAndreas.Sandberg@ARM.com//    Tick currentTick;
7612104Snathanael.premillieu@arm.com    inline Tick frequency() const { return Clock::Frequency / clock; }
775596Sgblack@eecs.umich.edu    inline Tick cycles(int numCycles) const { return clock * numCycles; }
785596Sgblack@eecs.umich.edu    inline Tick curCycle() const { return curTick / clock; }
795596Sgblack@eecs.umich.edu
805596Sgblack@eecs.umich.edu    /** The next cycle the CPU should be scheduled, given a cache
818902Sandreas.hansson@arm.com     * access or quiesce event returning on this cycle.  This function
825596Sgblack@eecs.umich.edu     * may return curTick if the CPU should run on the current cycle.
835596Sgblack@eecs.umich.edu     */
845596Sgblack@eecs.umich.edu    Tick nextCycle();
855596Sgblack@eecs.umich.edu
8610417Sandreas.hansson@arm.com    /** The next cycle the CPU should be scheduled, given a cache
877720Sgblack@eecs.umich.edu     * access or quiesce event returning on the given Tick.  This
887720Sgblack@eecs.umich.edu     * function may return curTick if the CPU should run on the
895596Sgblack@eecs.umich.edu     * current cycle.
905596Sgblack@eecs.umich.edu     * @param begin_tick The tick that the event is completing on.
9110417Sandreas.hansson@arm.com     */
9210417Sandreas.hansson@arm.com    Tick nextCycle(Tick begin_tick);
935596Sgblack@eecs.umich.edu
949252Sdjordje.kovacevic@arm.com#if FULL_SYSTEM
959252Sdjordje.kovacevic@arm.com  protected:
965596Sgblack@eecs.umich.edu//    uint64_t interrupts[TheISA::NumInterruptLevels];
975596Sgblack@eecs.umich.edu//    uint64_t intstatus;
985596Sgblack@eecs.umich.edu    TheISA::Interrupts interrupts;
995596Sgblack@eecs.umich.edu
1005596Sgblack@eecs.umich.edu  public:
1015596Sgblack@eecs.umich.edu    virtual void post_interrupt(int int_num, int index);
1025596Sgblack@eecs.umich.edu    virtual void clear_interrupt(int int_num, int index);
1035596Sgblack@eecs.umich.edu    virtual void clear_interrupts();
1045596Sgblack@eecs.umich.edu    bool checkInterrupts;
1055596Sgblack@eecs.umich.edu
1065596Sgblack@eecs.umich.edu    bool check_interrupts(ThreadContext * tc) const
1075596Sgblack@eecs.umich.edu    { return interrupts.check_interrupts(tc); }
1085596Sgblack@eecs.umich.edu
1097783SGiacomo.Gabrielli@arm.com    class ProfileEvent : public Event
1109046SAli.Saidi@ARM.com    {
11110835Sandreas.hansson@arm.com      private:
1129046SAli.Saidi@ARM.com        BaseCPU *cpu;
1137783SGiacomo.Gabrielli@arm.com        int interval;
1147783SGiacomo.Gabrielli@arm.com
1157783SGiacomo.Gabrielli@arm.com      public:
1167783SGiacomo.Gabrielli@arm.com        ProfileEvent(BaseCPU *cpu, int interval);
11710835Sandreas.hansson@arm.com        void process();
1189046SAli.Saidi@ARM.com    };
1197783SGiacomo.Gabrielli@arm.com    ProfileEvent *profileEvent;
1209046SAli.Saidi@ARM.com#endif
1219046SAli.Saidi@ARM.com
1227783SGiacomo.Gabrielli@arm.com  protected:
1235596Sgblack@eecs.umich.edu    std::vector<ThreadContext *> threadContexts;
1248471SGiacomo.Gabrielli@arm.com
1258471SGiacomo.Gabrielli@arm.com  public:
1269252Sdjordje.kovacevic@arm.com
1279252Sdjordje.kovacevic@arm.com    /// Notify the CPU that the indicated context is now active.  The
1289252Sdjordje.kovacevic@arm.com    /// delay parameter indicates the number of ticks to wait before
1299252Sdjordje.kovacevic@arm.com    /// executing (typically 0 or 1).
1309252Sdjordje.kovacevic@arm.com    virtual void activateContext(int thread_num, int delay) {}
1319252Sdjordje.kovacevic@arm.com
1329252Sdjordje.kovacevic@arm.com    /// Notify the CPU that the indicated context is now suspended.
1339527SMatt.Horsnell@arm.com    virtual void suspendContext(int thread_num) {}
1348471SGiacomo.Gabrielli@arm.com
1358471SGiacomo.Gabrielli@arm.com    /// Notify the CPU that the indicated context is now deallocated.
1365596Sgblack@eecs.umich.edu    virtual void deallocateContext(int thread_num) {}
1375596Sgblack@eecs.umich.edu
1385596Sgblack@eecs.umich.edu    /// Notify the CPU that the indicated context is now halted.
1395596Sgblack@eecs.umich.edu    virtual void haltContext(int thread_num) {}
1405596Sgblack@eecs.umich.edu
1415596Sgblack@eecs.umich.edu  public:
1425596Sgblack@eecs.umich.edu    struct Params
1435596Sgblack@eecs.umich.edu    {
1445596Sgblack@eecs.umich.edu        std::string name;
1455596Sgblack@eecs.umich.edu        int numberOfThreads;
1465596Sgblack@eecs.umich.edu        bool deferRegistration;
1475596Sgblack@eecs.umich.edu        Counter max_insts_any_thread;
1485596Sgblack@eecs.umich.edu        Counter max_insts_all_threads;
1497783SGiacomo.Gabrielli@arm.com        Counter max_loads_any_thread;
1509532Sgeoffrey.blake@arm.com        Counter max_loads_all_threads;
1519532Sgeoffrey.blake@arm.com        Tick clock;
1529532Sgeoffrey.blake@arm.com        bool functionTrace;
1539532Sgeoffrey.blake@arm.com        Tick functionTraceStart;
1547783SGiacomo.Gabrielli@arm.com        System *system;
1559532Sgeoffrey.blake@arm.com        int cpu_id;
1569532Sgeoffrey.blake@arm.com#if FULL_SYSTEM
1579532Sgeoffrey.blake@arm.com        Tick profile;
1589532Sgeoffrey.blake@arm.com
1599532Sgeoffrey.blake@arm.com        bool do_statistics_insts;
1609532Sgeoffrey.blake@arm.com        bool do_checkpoint_insts;
1619532Sgeoffrey.blake@arm.com        bool do_quiesce;
1629046SAli.Saidi@ARM.com#endif
1637783SGiacomo.Gabrielli@arm.com        Tick progress_interval;
1647783SGiacomo.Gabrielli@arm.com        BaseCPU *checker;
1657783SGiacomo.Gabrielli@arm.com
1665596Sgblack@eecs.umich.edu        Params();
1675596Sgblack@eecs.umich.edu    };
1685596Sgblack@eecs.umich.edu
1695596Sgblack@eecs.umich.edu    const Params *params;
1705596Sgblack@eecs.umich.edu
1715596Sgblack@eecs.umich.edu    BaseCPU(Params *params);
1725596Sgblack@eecs.umich.edu    virtual ~BaseCPU();
17312104Snathanael.premillieu@arm.com
17412104Snathanael.premillieu@arm.com    virtual void init();
17512104Snathanael.premillieu@arm.com    virtual void startup();
1765596Sgblack@eecs.umich.edu    virtual void regStats();
1775596Sgblack@eecs.umich.edu
1785596Sgblack@eecs.umich.edu    virtual void activateWhenReady(int tid) {};
1795596Sgblack@eecs.umich.edu
1805596Sgblack@eecs.umich.edu    void registerThreadContexts();
1815596Sgblack@eecs.umich.edu
1825596Sgblack@eecs.umich.edu    /// Prepare for another CPU to take over execution.  When it is
1835596Sgblack@eecs.umich.edu    /// is ready (drained pipe) it signals the sampler.
18412104Snathanael.premillieu@arm.com    virtual void switchOut();
18512104Snathanael.premillieu@arm.com
18612104Snathanael.premillieu@arm.com    /// Take over execution from the given CPU.  Used for warm-up and
1877783SGiacomo.Gabrielli@arm.com    /// sampling.
1887783SGiacomo.Gabrielli@arm.com    virtual void takeOverFrom(BaseCPU *);
1897783SGiacomo.Gabrielli@arm.com
1907783SGiacomo.Gabrielli@arm.com    /**
1917783SGiacomo.Gabrielli@arm.com     *  Number of threads we're actually simulating (<= SMT_MAX_THREADS).
1927783SGiacomo.Gabrielli@arm.com     * This is a constant for the duration of the simulation.
1937783SGiacomo.Gabrielli@arm.com     */
1947783SGiacomo.Gabrielli@arm.com    int number_of_threads;
1957783SGiacomo.Gabrielli@arm.com
1969382SAli.Saidi@ARM.com    /**
1979382SAli.Saidi@ARM.com     * Vector of per-thread instruction-based event queues.  Used for
1987783SGiacomo.Gabrielli@arm.com     * scheduling events based on number of instructions committed by
1997783SGiacomo.Gabrielli@arm.com     * a particular thread.
2007783SGiacomo.Gabrielli@arm.com     */
2017783SGiacomo.Gabrielli@arm.com    EventQueue **comInstEventQueue;
2027783SGiacomo.Gabrielli@arm.com
2039382SAli.Saidi@ARM.com    /**
2045596Sgblack@eecs.umich.edu     * Vector of per-thread load-based event queues.  Used for
2055596Sgblack@eecs.umich.edu     * scheduling events based on number of loads committed by
2067848SAli.Saidi@ARM.com     *a particular thread.
2077848SAli.Saidi@ARM.com     */
20810935Snilay@cs.wisc.edu    EventQueue **comLoadEventQueue;
2097848SAli.Saidi@ARM.com
21012105Snathanael.premillieu@arm.com    System *system;
21112104Snathanael.premillieu@arm.com
2129913Ssteve.reinhardt@amd.com#if FULL_SYSTEM
21312104Snathanael.premillieu@arm.com    /**
2149913Ssteve.reinhardt@amd.com     * Serialize this object to the given output stream.
2159913Ssteve.reinhardt@amd.com     * @param os The stream to serialize to.
2169913Ssteve.reinhardt@amd.com     */
2179913Ssteve.reinhardt@amd.com    virtual void serialize(std::ostream &os);
2189913Ssteve.reinhardt@amd.com
2199913Ssteve.reinhardt@amd.com    /**
2209913Ssteve.reinhardt@amd.com     * Reconstruct the state of this object from a checkpoint.
2219913Ssteve.reinhardt@amd.com     * @param cp The checkpoint use.
2229920Syasuko.eckert@amd.com     * @param section The section name of this object
2239920Syasuko.eckert@amd.com     */
2249920Syasuko.eckert@amd.com    virtual void unserialize(Checkpoint *cp, const std::string &section);
2259920Syasuko.eckert@amd.com
2269913Ssteve.reinhardt@amd.com#endif
2279913Ssteve.reinhardt@amd.com
2289913Ssteve.reinhardt@amd.com    /**
2299913Ssteve.reinhardt@amd.com     * Return pointer to CPU's branch predictor (NULL if none).
2307848SAli.Saidi@ARM.com     * @return Branch predictor pointer.
2317848SAli.Saidi@ARM.com     */
2325702Ssaidi@eecs.umich.edu    virtual BranchPred *getBranchPred() { return NULL; };
2335702Ssaidi@eecs.umich.edu
2345596Sgblack@eecs.umich.edu    virtual Counter totalInstructions() const { return 0; }
23510379Sandreas.hansson@arm.com
2365702Ssaidi@eecs.umich.edu    // Function tracing
2378557Sgblack@eecs.umich.edu  private:
2388557Sgblack@eecs.umich.edu    bool functionTracingEnabled;
23911877Sbrandon.potter@amd.com    std::ostream *functionTraceStream;
2402847Sksewell@umich.edu    Addr currentFunctionStart;
2415596Sgblack@eecs.umich.edu    Addr currentFunctionEnd;
2425596Sgblack@eecs.umich.edu    Tick functionEntryTick;
2435596Sgblack@eecs.umich.edu    void enableFunctionTrace();
2445596Sgblack@eecs.umich.edu    void traceFunctionsInternal(Addr pc);
2455596Sgblack@eecs.umich.edu
2465596Sgblack@eecs.umich.edu  protected:
2475596Sgblack@eecs.umich.edu    void traceFunctions(Addr pc)
2485596Sgblack@eecs.umich.edu    {
2495596Sgblack@eecs.umich.edu        if (functionTracingEnabled)
2505596Sgblack@eecs.umich.edu            traceFunctionsInternal(pc);
2515596Sgblack@eecs.umich.edu    }
2525596Sgblack@eecs.umich.edu
2535596Sgblack@eecs.umich.edu  private:
25410319SAndreas.Sandberg@ARM.com    static std::vector<BaseCPU *> cpuList;   //!< Static global cpu list
2555596Sgblack@eecs.umich.edu
2565596Sgblack@eecs.umich.edu  public:
2575596Sgblack@eecs.umich.edu    static int numSimulatedCPUs() { return cpuList.size(); }
2585596Sgblack@eecs.umich.edu    static Counter numSimulatedInstructions()
2595596Sgblack@eecs.umich.edu    {
2605596Sgblack@eecs.umich.edu        Counter total = 0;
2615596Sgblack@eecs.umich.edu
2625596Sgblack@eecs.umich.edu        int size = cpuList.size();
2635596Sgblack@eecs.umich.edu        for (int i = 0; i < size; ++i)
2645596Sgblack@eecs.umich.edu            total += cpuList[i]->totalInstructions();
2655596Sgblack@eecs.umich.edu
2665596Sgblack@eecs.umich.edu        return total;
2675596Sgblack@eecs.umich.edu    }
2685596Sgblack@eecs.umich.edu
26910319SAndreas.Sandberg@ARM.com  public:
2709920Syasuko.eckert@amd.com    // Number of CPU cycles simulated
2719920Syasuko.eckert@amd.com    Stats::Scalar<> numCycles;
2729920Syasuko.eckert@amd.com};
2739920Syasuko.eckert@amd.com
2745596Sgblack@eecs.umich.edu#endif // __CPU_BASE_HH__
2755596Sgblack@eecs.umich.edu