base.hh revision 13784
15470Snate@binkert.org/*
25470Snate@binkert.org * Copyright (c) 2011-2013, 2017 ARM Limited
35470Snate@binkert.org * All rights reserved
45470Snate@binkert.org *
55470Snate@binkert.org * The license below extends only to copyright in the software and shall
65470Snate@binkert.org * not be construed as granting a license to any other intellectual
75470Snate@binkert.org * property including but not limited to intellectual property relating
85470Snate@binkert.org * to a hardware implementation of the functionality of the software
95470Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
105470Snate@binkert.org * terms below provided that you ensure that this notice is replicated
115470Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
125470Snate@binkert.org * modified or unmodified, in source code or in binary form.
135470Snate@binkert.org *
145470Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan
155470Snate@binkert.org * Copyright (c) 2011 Regents of the University of California
165470Snate@binkert.org * All rights reserved.
175470Snate@binkert.org *
185470Snate@binkert.org * Redistribution and use in source and binary forms, with or without
195470Snate@binkert.org * modification, are permitted provided that the following conditions are
205470Snate@binkert.org * met: redistributions of source code must retain the above copyright
215470Snate@binkert.org * notice, this list of conditions and the following disclaimer;
225470Snate@binkert.org * redistributions in binary form must reproduce the above copyright
235470Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
245470Snate@binkert.org * documentation and/or other materials provided with the distribution;
255470Snate@binkert.org * neither the name of the copyright holders nor the names of its
265470Snate@binkert.org * contributors may be used to endorse or promote products derived from
275470Snate@binkert.org * this software without specific prior written permission.
285470Snate@binkert.org *
2913714Sandreas.sandberg@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3013714Sandreas.sandberg@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3113714Sandreas.sandberg@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
325470Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
335470Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
345470Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
355470Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
365470Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
375470Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
385470Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
395470Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
405470Snate@binkert.org *
415470Snate@binkert.org * Authors: Steve Reinhardt
425470Snate@binkert.org *          Nathan Binkert
435470Snate@binkert.org *          Rick Strong
445470Snate@binkert.org */
455470Snate@binkert.org
465470Snate@binkert.org#ifndef __CPU_BASE_HH__
475470Snate@binkert.org#define __CPU_BASE_HH__
485470Snate@binkert.org
495470Snate@binkert.org#include <vector>
505586Snate@binkert.org
515470Snate@binkert.org// Before we do anything else, check if this build is the NULL ISA,
525470Snate@binkert.org// and if so stop here
535470Snate@binkert.org#include "config/the_isa.hh"
545470Snate@binkert.org#if THE_ISA == NULL_ISA
555470Snate@binkert.org#include "arch/null/cpu_dummy.hh"
565470Snate@binkert.org#else
575470Snate@binkert.org#include "arch/interrupts.hh"
585470Snate@binkert.org#include "arch/isa_traits.hh"
595470Snate@binkert.org#include "arch/microcode_rom.hh"
605470Snate@binkert.org#include "base/statistics.hh"
615586Snate@binkert.org#include "mem/mem_object.hh"
625586Snate@binkert.org#include "sim/eventq.hh"
635586Snate@binkert.org#include "sim/full_system.hh"
645470Snate@binkert.org#include "sim/insttracer.hh"
655470Snate@binkert.org#include "sim/probe/pmu.hh"
665470Snate@binkert.org#include "sim/probe/probe.hh"
675470Snate@binkert.org#include "sim/system.hh"
685470Snate@binkert.org#include "debug/Mwait.hh"
695470Snate@binkert.org
705470Snate@binkert.orgclass BaseCPU;
715470Snate@binkert.orgstruct BaseCPUParams;
725470Snate@binkert.orgclass CheckerCPU;
735470Snate@binkert.orgclass ThreadContext;
745470Snate@binkert.org
755470Snate@binkert.orgstruct AddressMonitor
765470Snate@binkert.org{
775470Snate@binkert.org    AddressMonitor();
785470Snate@binkert.org    bool doMonitor(PacketPtr pkt);
795470Snate@binkert.org
805470Snate@binkert.org    bool armed;
815470Snate@binkert.org    Addr vAddr;
825470Snate@binkert.org    Addr pAddr;
835470Snate@binkert.org    uint64_t val;
845470Snate@binkert.org    bool waiting;   // 0=normal, 1=mwaiting
855470Snate@binkert.org    bool gotWakeup;
865470Snate@binkert.org};
875470Snate@binkert.org
885470Snate@binkert.orgclass CPUProgressEvent : public Event
895470Snate@binkert.org{
905470Snate@binkert.org  protected:
915470Snate@binkert.org    Tick _interval;
925470Snate@binkert.org    Counter lastNumInst;
935470Snate@binkert.org    BaseCPU *cpu;
945470Snate@binkert.org    bool _repeatEvent;
955470Snate@binkert.org
965470Snate@binkert.org  public:
975470Snate@binkert.org    CPUProgressEvent(BaseCPU *_cpu, Tick ival = 0);
985470Snate@binkert.org
995470Snate@binkert.org    void process();
1005470Snate@binkert.org
1015470Snate@binkert.org    void interval(Tick ival) { _interval = ival; }
1025470Snate@binkert.org    Tick interval() { return _interval; }
1035470Snate@binkert.org
1045470Snate@binkert.org    void repeatEvent(bool repeat) { _repeatEvent = repeat; }
1055470Snate@binkert.org
1065470Snate@binkert.org    virtual const char *description() const;
1075470Snate@binkert.org};
1085470Snate@binkert.org
1095470Snate@binkert.orgclass BaseCPU : public MemObject
1105470Snate@binkert.org{
1115470Snate@binkert.org  protected:
1125470Snate@binkert.org
1135470Snate@binkert.org    /// Instruction count used for SPARC misc register
1145586Snate@binkert.org    /// @todo unify this with the counters that cpus individually keep
1155470Snate@binkert.org    Tick instCnt;
1165586Snate@binkert.org
1175586Snate@binkert.org    // every cpu has an id, put it in the base cpu
1185470Snate@binkert.org    // Set at initialization, only time a cpuId might change is during a
1195586Snate@binkert.org    // takeover (which should be done from within the BaseCPU anyway,
1205470Snate@binkert.org    // therefore no setCpuId() method is provided
1215470Snate@binkert.org    int _cpuId;
1225470Snate@binkert.org
1235586Snate@binkert.org    /** Each cpu will have a socket ID that corresponds to its physical location
1245586Snate@binkert.org     * in the system. This is usually used to bucket cpu cores under single DVFS
1255586Snate@binkert.org     * domain. This information may also be required by the OS to identify the
1265586Snate@binkert.org     * cpu core grouping (as in the case of ARM via MPIDR register)
1275586Snate@binkert.org     */
1285586Snate@binkert.org    const uint32_t _socketId;
1295586Snate@binkert.org
1305586Snate@binkert.org    /** instruction side request id that must be placed in all requests */
1315470Snate@binkert.org    MasterID _instMasterId;
1325470Snate@binkert.org
1335470Snate@binkert.org    /** data side request id that must be placed in all requests */
1345470Snate@binkert.org    MasterID _dataMasterId;
13513709Sandreas.sandberg@arm.com
1365586Snate@binkert.org    /** An intrenal representation of a task identifier within gem5. This is
1375586Snate@binkert.org     * used so the CPU can add which taskId (which is an internal representation
1385470Snate@binkert.org     * of the OS process ID) to each request so components in the memory system
1395470Snate@binkert.org     * can track which process IDs are ultimately interacting with them
1405470Snate@binkert.org     */
1415470Snate@binkert.org    uint32_t _taskId;
1425470Snate@binkert.org
1435470Snate@binkert.org    /** The current OS process ID that is executing on this processor. This is
1445470Snate@binkert.org     * used to generate a taskId */
1455470Snate@binkert.org    uint32_t _pid;
146
147    /** Is the CPU switched out or active? */
148    bool _switchedOut;
149
150    /** Cache the cache line size that we get from the system */
151    const unsigned int _cacheLineSize;
152
153  public:
154
155    /**
156     * Purely virtual method that returns a reference to the data
157     * port. All subclasses must implement this method.
158     *
159     * @return a reference to the data port
160     */
161    virtual MasterPort &getDataPort() = 0;
162
163    /**
164     * Purely virtual method that returns a reference to the instruction
165     * port. All subclasses must implement this method.
166     *
167     * @return a reference to the instruction port
168     */
169    virtual MasterPort &getInstPort() = 0;
170
171    /** Reads this CPU's ID. */
172    int cpuId() const { return _cpuId; }
173
174    /** Reads this CPU's Socket ID. */
175    uint32_t socketId() const { return _socketId; }
176
177    /** Reads this CPU's unique data requestor ID */
178    MasterID dataMasterId() { return _dataMasterId; }
179    /** Reads this CPU's unique instruction requestor ID */
180    MasterID instMasterId() { return _instMasterId; }
181
182    /**
183     * Get a port on this CPU. All CPUs have a data and
184     * instruction port, and this method uses getDataPort and
185     * getInstPort of the subclasses to resolve the two ports.
186     *
187     * @param if_name the port name
188     * @param idx ignored index
189     *
190     * @return a reference to the port with the given name
191     */
192    Port &getPort(const std::string &if_name,
193                  PortID idx=InvalidPortID) override;
194
195    /** Get cpu task id */
196    uint32_t taskId() const { return _taskId; }
197    /** Set cpu task id */
198    void taskId(uint32_t id) { _taskId = id; }
199
200    uint32_t getPid() const { return _pid; }
201    void setPid(uint32_t pid) { _pid = pid; }
202
203    inline void workItemBegin() { numWorkItemsStarted++; }
204    inline void workItemEnd() { numWorkItemsCompleted++; }
205    // @todo remove me after debugging with legion done
206    Tick instCount() { return instCnt; }
207
208    TheISA::MicrocodeRom microcodeRom;
209
210  protected:
211    std::vector<TheISA::Interrupts*> interrupts;
212
213  public:
214    TheISA::Interrupts *
215    getInterruptController(ThreadID tid)
216    {
217        if (interrupts.empty())
218            return NULL;
219
220        assert(interrupts.size() > tid);
221        return interrupts[tid];
222    }
223
224    virtual void wakeup(ThreadID tid) = 0;
225
226    void
227    postInterrupt(ThreadID tid, int int_num, int index)
228    {
229        interrupts[tid]->post(int_num, index);
230        if (FullSystem)
231            wakeup(tid);
232    }
233
234    void
235    clearInterrupt(ThreadID tid, int int_num, int index)
236    {
237        interrupts[tid]->clear(int_num, index);
238    }
239
240    void
241    clearInterrupts(ThreadID tid)
242    {
243        interrupts[tid]->clearAll();
244    }
245
246    bool
247    checkInterrupts(ThreadContext *tc) const
248    {
249        return FullSystem && interrupts[tc->threadId()]->checkInterrupts(tc);
250    }
251
252    void processProfileEvent();
253    EventFunctionWrapper * profileEvent;
254
255  protected:
256    std::vector<ThreadContext *> threadContexts;
257
258    Trace::InstTracer * tracer;
259
260  public:
261
262
263    /** Invalid or unknown Pid. Possible when operating system is not present
264     *  or has not assigned a pid yet */
265    static const uint32_t invldPid = std::numeric_limits<uint32_t>::max();
266
267    // Mask to align PCs to MachInst sized boundaries
268    static const Addr PCMask = ~((Addr)sizeof(TheISA::MachInst) - 1);
269
270    /// Provide access to the tracer pointer
271    Trace::InstTracer * getTracer() { return tracer; }
272
273    /// Notify the CPU that the indicated context is now active.
274    virtual void activateContext(ThreadID thread_num);
275
276    /// Notify the CPU that the indicated context is now suspended.
277    /// Check if possible to enter a lower power state
278    virtual void suspendContext(ThreadID thread_num);
279
280    /// Notify the CPU that the indicated context is now halted.
281    virtual void haltContext(ThreadID thread_num);
282
283   /// Given a Thread Context pointer return the thread num
284   int findContext(ThreadContext *tc);
285
286   /// Given a thread num get tho thread context for it
287   virtual ThreadContext *getContext(int tn) { return threadContexts[tn]; }
288
289   /// Get the number of thread contexts available
290   unsigned numContexts() { return threadContexts.size(); }
291
292    /// Convert ContextID to threadID
293    ThreadID contextToThread(ContextID cid)
294    { return static_cast<ThreadID>(cid - threadContexts[0]->contextId()); }
295
296  public:
297    typedef BaseCPUParams Params;
298    const Params *params() const
299    { return reinterpret_cast<const Params *>(_params); }
300    BaseCPU(Params *params, bool is_checker = false);
301    virtual ~BaseCPU();
302
303    void init() override;
304    void startup() override;
305    void regStats() override;
306
307    void regProbePoints() override;
308
309    void registerThreadContexts();
310
311    // Functions to deschedule and reschedule the events to enter the
312    // power gating sleep before and after checkpoiting respectively.
313    void deschedulePowerGatingEvent();
314    void schedulePowerGatingEvent();
315
316    /**
317     * Prepare for another CPU to take over execution.
318     *
319     * When this method exits, all internal state should have been
320     * flushed. After the method returns, the simulator calls
321     * takeOverFrom() on the new CPU with this CPU as its parameter.
322     */
323    virtual void switchOut();
324
325    /**
326     * Load the state of a CPU from the previous CPU object, invoked
327     * on all new CPUs that are about to be switched in.
328     *
329     * A CPU model implementing this method is expected to initialize
330     * its state from the old CPU and connect its memory (unless they
331     * are already connected) to the memories connected to the old
332     * CPU.
333     *
334     * @param cpu CPU to initialize read state from.
335     */
336    virtual void takeOverFrom(BaseCPU *cpu);
337
338    /**
339     * Flush all TLBs in the CPU.
340     *
341     * This method is mainly used to flush stale translations when
342     * switching CPUs. It is also exported to the Python world to
343     * allow it to request a TLB flush after draining the CPU to make
344     * it easier to compare traces when debugging
345     * handover/checkpointing.
346     */
347    void flushTLBs();
348
349    /**
350     * Determine if the CPU is switched out.
351     *
352     * @return True if the CPU is switched out, false otherwise.
353     */
354    bool switchedOut() const { return _switchedOut; }
355
356    /**
357     * Verify that the system is in a memory mode supported by the
358     * CPU.
359     *
360     * Implementations are expected to query the system for the
361     * current memory mode and ensure that it is what the CPU model
362     * expects. If the check fails, the implementation should
363     * terminate the simulation using fatal().
364     */
365    virtual void verifyMemoryMode() const { };
366
367    /**
368     *  Number of threads we're actually simulating (<= SMT_MAX_THREADS).
369     * This is a constant for the duration of the simulation.
370     */
371    ThreadID numThreads;
372
373    /**
374     * Vector of per-thread instruction-based event queues.  Used for
375     * scheduling events based on number of instructions committed by
376     * a particular thread.
377     */
378    EventQueue **comInstEventQueue;
379
380    /**
381     * Vector of per-thread load-based event queues.  Used for
382     * scheduling events based on number of loads committed by
383     *a particular thread.
384     */
385    EventQueue **comLoadEventQueue;
386
387    System *system;
388
389    /**
390     * Get the cache line size of the system.
391     */
392    inline unsigned int cacheLineSize() const { return _cacheLineSize; }
393
394    /**
395     * Serialize this object to the given output stream.
396     *
397     * @note CPU models should normally overload the serializeThread()
398     * method instead of the serialize() method as this provides a
399     * uniform data format for all CPU models and promotes better code
400     * reuse.
401     *
402     * @param os The stream to serialize to.
403     */
404    void serialize(CheckpointOut &cp) const override;
405
406    /**
407     * Reconstruct the state of this object from a checkpoint.
408     *
409     * @note CPU models should normally overload the
410     * unserializeThread() method instead of the unserialize() method
411     * as this provides a uniform data format for all CPU models and
412     * promotes better code reuse.
413
414     * @param cp The checkpoint use.
415     * @param section The section name of this object.
416     */
417    void unserialize(CheckpointIn &cp) override;
418
419    /**
420     * Serialize a single thread.
421     *
422     * @param os The stream to serialize to.
423     * @param tid ID of the current thread.
424     */
425    virtual void serializeThread(CheckpointOut &cp, ThreadID tid) const {};
426
427    /**
428     * Unserialize one thread.
429     *
430     * @param cp The checkpoint use.
431     * @param section The section name of this thread.
432     * @param tid ID of the current thread.
433     */
434    virtual void unserializeThread(CheckpointIn &cp, ThreadID tid) {};
435
436    virtual Counter totalInsts() const = 0;
437
438    virtual Counter totalOps() const = 0;
439
440    /**
441     * Schedule an event that exits the simulation loops after a
442     * predefined number of instructions.
443     *
444     * This method is usually called from the configuration script to
445     * get an exit event some time in the future. It is typically used
446     * when the script wants to simulate for a specific number of
447     * instructions rather than ticks.
448     *
449     * @param tid Thread monitor.
450     * @param insts Number of instructions into the future.
451     * @param cause Cause to signal in the exit event.
452     */
453    void scheduleInstStop(ThreadID tid, Counter insts, const char *cause);
454
455    /**
456     * Schedule an event that exits the simulation loops after a
457     * predefined number of load operations.
458     *
459     * This method is usually called from the configuration script to
460     * get an exit event some time in the future. It is typically used
461     * when the script wants to simulate for a specific number of
462     * loads rather than ticks.
463     *
464     * @param tid Thread monitor.
465     * @param loads Number of load instructions into the future.
466     * @param cause Cause to signal in the exit event.
467     */
468    void scheduleLoadStop(ThreadID tid, Counter loads, const char *cause);
469
470    /**
471     * Get the number of instructions executed by the specified thread
472     * on this CPU. Used by Python to control simulation.
473     *
474     * @param tid Thread monitor
475     * @return Number of instructions executed
476     */
477    uint64_t getCurrentInstCount(ThreadID tid);
478
479  public:
480    /**
481     * @{
482     * @name PMU Probe points.
483     */
484
485    /**
486     * Helper method to trigger PMU probes for a committed
487     * instruction.
488     *
489     * @param inst Instruction that just committed
490     */
491    virtual void probeInstCommit(const StaticInstPtr &inst);
492
493   protected:
494    /**
495     * Helper method to instantiate probe points belonging to this
496     * object.
497     *
498     * @param name Name of the probe point.
499     * @return A unique_ptr to the new probe point.
500     */
501    ProbePoints::PMUUPtr pmuProbePoint(const char *name);
502
503    /**
504     * Instruction commit probe point.
505     *
506     * This probe point is triggered whenever one or more instructions
507     * are committed. It is normally triggered once for every
508     * instruction. However, CPU models committing bundles of
509     * instructions may call notify once for the entire bundle.
510     */
511    ProbePoints::PMUUPtr ppRetiredInsts;
512
513    /** Retired load instructions */
514    ProbePoints::PMUUPtr ppRetiredLoads;
515    /** Retired store instructions */
516    ProbePoints::PMUUPtr ppRetiredStores;
517
518    /** Retired branches (any type) */
519    ProbePoints::PMUUPtr ppRetiredBranches;
520
521    /** CPU cycle counter even if any thread Context is suspended*/
522    ProbePoints::PMUUPtr ppAllCycles;
523
524    /** CPU cycle counter, only counts if any thread contexts is active **/
525    ProbePoints::PMUUPtr ppActiveCycles;
526
527    /**
528     * ProbePoint that signals transitions of threadContexts sets.
529     * The ProbePoint reports information through it bool parameter.
530     * - If the parameter is true then the last enabled threadContext of the
531     * CPU object was disabled.
532     * - If the parameter is false then a threadContext was enabled, all the
533     * remaining threadContexts are disabled.
534     */
535    ProbePointArg<bool> *ppSleeping;
536    /** @} */
537
538    enum CPUState {
539        CPU_STATE_ON,
540        CPU_STATE_SLEEP,
541        CPU_STATE_WAKEUP
542    };
543
544    Cycles previousCycle;
545    CPUState previousState;
546
547    /** base method keeping track of cycle progression **/
548    inline void updateCycleCounters(CPUState state)
549    {
550        uint32_t delta = curCycle() - previousCycle;
551
552        if (previousState == CPU_STATE_ON) {
553            ppActiveCycles->notify(delta);
554        }
555
556        switch (state)
557        {
558          case CPU_STATE_WAKEUP:
559            ppSleeping->notify(false);
560            break;
561          case CPU_STATE_SLEEP:
562            ppSleeping->notify(true);
563            break;
564          default:
565            break;
566        }
567
568        ppAllCycles->notify(delta);
569
570        previousCycle = curCycle();
571        previousState = state;
572    }
573
574    // Function tracing
575  private:
576    bool functionTracingEnabled;
577    std::ostream *functionTraceStream;
578    Addr currentFunctionStart;
579    Addr currentFunctionEnd;
580    Tick functionEntryTick;
581    void enableFunctionTrace();
582    void traceFunctionsInternal(Addr pc);
583
584  private:
585    static std::vector<BaseCPU *> cpuList;   //!< Static global cpu list
586
587  public:
588    void traceFunctions(Addr pc)
589    {
590        if (functionTracingEnabled)
591            traceFunctionsInternal(pc);
592    }
593
594    static int numSimulatedCPUs() { return cpuList.size(); }
595    static Counter numSimulatedInsts()
596    {
597        Counter total = 0;
598
599        int size = cpuList.size();
600        for (int i = 0; i < size; ++i)
601            total += cpuList[i]->totalInsts();
602
603        return total;
604    }
605
606    static Counter numSimulatedOps()
607    {
608        Counter total = 0;
609
610        int size = cpuList.size();
611        for (int i = 0; i < size; ++i)
612            total += cpuList[i]->totalOps();
613
614        return total;
615    }
616
617  public:
618    // Number of CPU cycles simulated
619    Stats::Scalar numCycles;
620    Stats::Scalar numWorkItemsStarted;
621    Stats::Scalar numWorkItemsCompleted;
622
623  private:
624    std::vector<AddressMonitor> addressMonitor;
625
626  public:
627    void armMonitor(ThreadID tid, Addr address);
628    bool mwait(ThreadID tid, PacketPtr pkt);
629    void mwaitAtomic(ThreadID tid, ThreadContext *tc, BaseTLB *dtb);
630    AddressMonitor *getCpuAddrMonitor(ThreadID tid)
631    {
632        assert(tid < numThreads);
633        return &addressMonitor[tid];
634    }
635
636    bool waitForRemoteGDB() const;
637
638    Cycles syscallRetryLatency;
639
640  // Enables CPU to enter power gating on a configurable cycle count
641  protected:
642    void enterPwrGating();
643
644    const Cycles pwrGatingLatency;
645    const bool powerGatingOnIdle;
646    EventFunctionWrapper enterPwrGatingEvent;
647};
648
649#endif // THE_ISA == NULL_ISA
650
651#endif // __CPU_BASE_HH__
652