cpu.hh (13652:45d94ac03a27) cpu.hh (13892:0182a0601f66)
1/*
2 * Copyright (c) 2011-2013, 2016-2019 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2004-2005 The Regents of The University of Michigan
16 * Copyright (c) 2011 Regents of the University of California
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Authors: Kevin Lim
43 * Korey Sewell
44 * Rick Strong
45 */
46
47#ifndef __CPU_O3_CPU_HH__
48#define __CPU_O3_CPU_HH__
49
50#include <iostream>
51#include <list>
52#include <queue>
53#include <set>
54#include <vector>
55
56#include "arch/generic/types.hh"
57#include "arch/types.hh"
58#include "base/statistics.hh"
59#include "config/the_isa.hh"
60#include "cpu/o3/comm.hh"
61#include "cpu/o3/cpu_policy.hh"
62#include "cpu/o3/scoreboard.hh"
63#include "cpu/o3/thread_state.hh"
64#include "cpu/activity.hh"
65#include "cpu/base.hh"
66#include "cpu/simple_thread.hh"
67#include "cpu/timebuf.hh"
68//#include "cpu/o3/thread_context.hh"
69#include "params/DerivO3CPU.hh"
70#include "sim/process.hh"
71
72template <class>
73class Checker;
74class ThreadContext;
75template <class>
76class O3ThreadContext;
77
78class Checkpoint;
1/*
2 * Copyright (c) 2011-2013, 2016-2019 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2004-2005 The Regents of The University of Michigan
16 * Copyright (c) 2011 Regents of the University of California
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Authors: Kevin Lim
43 * Korey Sewell
44 * Rick Strong
45 */
46
47#ifndef __CPU_O3_CPU_HH__
48#define __CPU_O3_CPU_HH__
49
50#include <iostream>
51#include <list>
52#include <queue>
53#include <set>
54#include <vector>
55
56#include "arch/generic/types.hh"
57#include "arch/types.hh"
58#include "base/statistics.hh"
59#include "config/the_isa.hh"
60#include "cpu/o3/comm.hh"
61#include "cpu/o3/cpu_policy.hh"
62#include "cpu/o3/scoreboard.hh"
63#include "cpu/o3/thread_state.hh"
64#include "cpu/activity.hh"
65#include "cpu/base.hh"
66#include "cpu/simple_thread.hh"
67#include "cpu/timebuf.hh"
68//#include "cpu/o3/thread_context.hh"
69#include "params/DerivO3CPU.hh"
70#include "sim/process.hh"
71
72template <class>
73class Checker;
74class ThreadContext;
75template <class>
76class O3ThreadContext;
77
78class Checkpoint;
79class MemObject;
80class Process;
81
82struct BaseCPUParams;
83
84class BaseO3CPU : public BaseCPU
85{
86 //Stuff that's pretty ISA independent will go here.
87 public:
88 BaseO3CPU(BaseCPUParams *params);
89
90 void regStats();
91};
92
93/**
94 * FullO3CPU class, has each of the stages (fetch through commit)
95 * within it, as well as all of the time buffers between stages. The
96 * tick() function for the CPU is defined here.
97 */
98template <class Impl>
99class FullO3CPU : public BaseO3CPU
100{
101 public:
102 // Typedefs from the Impl here.
103 typedef typename Impl::CPUPol CPUPolicy;
104 typedef typename Impl::DynInstPtr DynInstPtr;
105 typedef typename Impl::O3CPU O3CPU;
106
107 using VecElem = TheISA::VecElem;
108 using VecRegContainer = TheISA::VecRegContainer;
109
110 using VecPredRegContainer = TheISA::VecPredRegContainer;
111
112 typedef O3ThreadState<Impl> ImplState;
113 typedef O3ThreadState<Impl> Thread;
114
115 typedef typename std::list<DynInstPtr>::iterator ListIt;
116
117 friend class O3ThreadContext<Impl>;
118
119 public:
120 enum Status {
121 Running,
122 Idle,
123 Halted,
124 Blocked,
125 SwitchedOut
126 };
127
128 BaseTLB *itb;
129 BaseTLB *dtb;
130 using LSQRequest = typename LSQ<Impl>::LSQRequest;
131
132 /** Overall CPU status. */
133 Status _status;
134
135 private:
136
137 /**
138 * IcachePort class for instruction fetch.
139 */
140 class IcachePort : public MasterPort
141 {
142 protected:
143 /** Pointer to fetch. */
144 DefaultFetch<Impl> *fetch;
145
146 public:
147 /** Default constructor. */
148 IcachePort(DefaultFetch<Impl> *_fetch, FullO3CPU<Impl>* _cpu)
149 : MasterPort(_cpu->name() + ".icache_port", _cpu), fetch(_fetch)
150 { }
151
152 protected:
153
154 /** Timing version of receive. Handles setting fetch to the
155 * proper status to start fetching. */
156 virtual bool recvTimingResp(PacketPtr pkt);
157
158 /** Handles doing a retry of a failed fetch. */
159 virtual void recvReqRetry();
160 };
161
162 /**
163 * DcachePort class for the load/store queue.
164 */
165 class DcachePort : public MasterPort
166 {
167 protected:
168
169 /** Pointer to LSQ. */
170 LSQ<Impl> *lsq;
171 FullO3CPU<Impl> *cpu;
172
173 public:
174 /** Default constructor. */
175 DcachePort(LSQ<Impl> *_lsq, FullO3CPU<Impl>* _cpu)
176 : MasterPort(_cpu->name() + ".dcache_port", _cpu), lsq(_lsq),
177 cpu(_cpu)
178 { }
179
180 protected:
181
182 /** Timing version of receive. Handles writing back and
183 * completing the load or store that has returned from
184 * memory. */
185 virtual bool recvTimingResp(PacketPtr pkt);
186 virtual void recvTimingSnoopReq(PacketPtr pkt);
187
188 virtual void recvFunctionalSnoop(PacketPtr pkt)
189 {
190 // @todo: Is there a need for potential invalidation here?
191 }
192
193 /** Handles doing a retry of the previous send. */
194 virtual void recvReqRetry();
195
196 /**
197 * As this CPU requires snooping to maintain the load store queue
198 * change the behaviour from the base CPU port.
199 *
200 * @return true since we have to snoop
201 */
202 virtual bool isSnooping() const { return true; }
203 };
204
205 /** The tick event used for scheduling CPU ticks. */
206 EventFunctionWrapper tickEvent;
207
208 /** The exit event used for terminating all ready-to-exit threads */
209 EventFunctionWrapper threadExitEvent;
210
211 /** Schedule tick event, regardless of its current state. */
212 void scheduleTickEvent(Cycles delay)
213 {
214 if (tickEvent.squashed())
215 reschedule(tickEvent, clockEdge(delay));
216 else if (!tickEvent.scheduled())
217 schedule(tickEvent, clockEdge(delay));
218 }
219
220 /** Unschedule tick event, regardless of its current state. */
221 void unscheduleTickEvent()
222 {
223 if (tickEvent.scheduled())
224 tickEvent.squash();
225 }
226
227 /**
228 * Check if the pipeline has drained and signal drain done.
229 *
230 * This method checks if a drain has been requested and if the CPU
231 * has drained successfully (i.e., there are no instructions in
232 * the pipeline). If the CPU has drained, it deschedules the tick
233 * event and signals the drain manager.
234 *
235 * @return False if a drain hasn't been requested or the CPU
236 * hasn't drained, true otherwise.
237 */
238 bool tryDrain();
239
240 /**
241 * Perform sanity checks after a drain.
242 *
243 * This method is called from drain() when it has determined that
244 * the CPU is fully drained when gem5 is compiled with the NDEBUG
245 * macro undefined. The intention of this method is to do more
246 * extensive tests than the isDrained() method to weed out any
247 * draining bugs.
248 */
249 void drainSanityCheck() const;
250
251 /** Check if a system is in a drained state. */
252 bool isDrained() const;
253
254 public:
255 /** Constructs a CPU with the given parameters. */
256 FullO3CPU(DerivO3CPUParams *params);
257 /** Destructor. */
258 ~FullO3CPU();
259
260 /** Registers statistics. */
261 void regStats() override;
262
263 ProbePointArg<PacketPtr> *ppInstAccessComplete;
264 ProbePointArg<std::pair<DynInstPtr, PacketPtr> > *ppDataAccessComplete;
265
266 /** Register probe points. */
267 void regProbePoints() override;
268
269 void demapPage(Addr vaddr, uint64_t asn)
270 {
271 this->itb->demapPage(vaddr, asn);
272 this->dtb->demapPage(vaddr, asn);
273 }
274
275 void demapInstPage(Addr vaddr, uint64_t asn)
276 {
277 this->itb->demapPage(vaddr, asn);
278 }
279
280 void demapDataPage(Addr vaddr, uint64_t asn)
281 {
282 this->dtb->demapPage(vaddr, asn);
283 }
284
285 /** Ticks CPU, calling tick() on each stage, and checking the overall
286 * activity to see if the CPU should deschedule itself.
287 */
288 void tick();
289
290 /** Initialize the CPU */
291 void init() override;
292
293 void startup() override;
294
295 /** Returns the Number of Active Threads in the CPU */
296 int numActiveThreads()
297 { return activeThreads.size(); }
298
299 /** Add Thread to Active Threads List */
300 void activateThread(ThreadID tid);
301
302 /** Remove Thread from Active Threads List */
303 void deactivateThread(ThreadID tid);
304
305 /** Setup CPU to insert a thread's context */
306 void insertThread(ThreadID tid);
307
308 /** Remove all of a thread's context from CPU */
309 void removeThread(ThreadID tid);
310
311 /** Count the Total Instructions Committed in the CPU. */
312 Counter totalInsts() const override;
313
314 /** Count the Total Ops (including micro ops) committed in the CPU. */
315 Counter totalOps() const override;
316
317 /** Add Thread to Active Threads List. */
318 void activateContext(ThreadID tid) override;
319
320 /** Remove Thread from Active Threads List */
321 void suspendContext(ThreadID tid) override;
322
323 /** Remove Thread from Active Threads List &&
324 * Remove Thread Context from CPU.
325 */
326 void haltContext(ThreadID tid) override;
327
328 /** Update The Order In Which We Process Threads. */
329 void updateThreadPriority();
330
331 /** Is the CPU draining? */
332 bool isDraining() const { return drainState() == DrainState::Draining; }
333
334 void serializeThread(CheckpointOut &cp, ThreadID tid) const override;
335 void unserializeThread(CheckpointIn &cp, ThreadID tid) override;
336
337 /** Insert tid to the list of threads trying to exit */
338 void addThreadToExitingList(ThreadID tid);
339
340 /** Is the thread trying to exit? */
341 bool isThreadExiting(ThreadID tid) const;
342
343 /**
344 * If a thread is trying to exit and its corresponding trap event
345 * has been completed, schedule an event to terminate the thread.
346 */
347 void scheduleThreadExitEvent(ThreadID tid);
348
349 /** Terminate all threads that are ready to exit */
350 void exitThreads();
351
352 public:
353 /** Executes a syscall.
354 * @todo: Determine if this needs to be virtual.
355 */
356 void syscall(int64_t callnum, ThreadID tid, Fault *fault);
357
358 /** Starts draining the CPU's pipeline of all instructions in
359 * order to stop all memory accesses. */
360 DrainState drain() override;
361
362 /** Resumes execution after a drain. */
363 void drainResume() override;
364
365 /**
366 * Commit has reached a safe point to drain a thread.
367 *
368 * Commit calls this method to inform the pipeline that it has
369 * reached a point where it is not executed microcode and is about
370 * to squash uncommitted instructions to fully drain the pipeline.
371 */
372 void commitDrained(ThreadID tid);
373
374 /** Switches out this CPU. */
375 void switchOut() override;
376
377 /** Takes over from another CPU. */
378 void takeOverFrom(BaseCPU *oldCPU) override;
379
380 void verifyMemoryMode() const override;
381
382 /** Get the current instruction sequence number, and increment it. */
383 InstSeqNum getAndIncrementInstSeq()
384 { return globalSeqNum++; }
385
386 /** Traps to handle given fault. */
387 void trap(const Fault &fault, ThreadID tid, const StaticInstPtr &inst);
388
389 /** HW return from error interrupt. */
390 Fault hwrei(ThreadID tid);
391
392 bool simPalCheck(int palFunc, ThreadID tid);
393
394 /** Check if a change in renaming is needed for vector registers.
395 * The vecMode variable is updated and propagated to rename maps.
396 *
397 * @param tid ThreadID
398 * @param freelist list of free registers
399 */
400 void switchRenameMode(ThreadID tid, UnifiedFreeList* freelist);
401
402 /** Returns the Fault for any valid interrupt. */
403 Fault getInterrupts();
404
405 /** Processes any an interrupt fault. */
406 void processInterrupts(const Fault &interrupt);
407
408 /** Halts the CPU. */
409 void halt() { panic("Halt not implemented!\n"); }
410
411 /** Register accessors. Index refers to the physical register index. */
412
413 /** Reads a miscellaneous register. */
414 RegVal readMiscRegNoEffect(int misc_reg, ThreadID tid) const;
415
416 /** Reads a misc. register, including any side effects the read
417 * might have as defined by the architecture.
418 */
419 RegVal readMiscReg(int misc_reg, ThreadID tid);
420
421 /** Sets a miscellaneous register. */
422 void setMiscRegNoEffect(int misc_reg, RegVal val, ThreadID tid);
423
424 /** Sets a misc. register, including any side effects the write
425 * might have as defined by the architecture.
426 */
427 void setMiscReg(int misc_reg, RegVal val, ThreadID tid);
428
429 RegVal readIntReg(PhysRegIdPtr phys_reg);
430
431 RegVal readFloatReg(PhysRegIdPtr phys_reg);
432
433 const VecRegContainer& readVecReg(PhysRegIdPtr reg_idx) const;
434
435 /**
436 * Read physical vector register for modification.
437 */
438 VecRegContainer& getWritableVecReg(PhysRegIdPtr reg_idx);
439
440 /** Returns current vector renaming mode */
441 Enums::VecRegRenameMode vecRenameMode() const { return vecMode; }
442
443 /** Sets the current vector renaming mode */
444 void vecRenameMode(Enums::VecRegRenameMode vec_mode)
445 { vecMode = vec_mode; }
446
447 /**
448 * Read physical vector register lane
449 */
450 template<typename VecElem, int LaneIdx>
451 VecLaneT<VecElem, true>
452 readVecLane(PhysRegIdPtr phys_reg) const
453 {
454 vecRegfileReads++;
455 return regFile.readVecLane<VecElem, LaneIdx>(phys_reg);
456 }
457
458 /**
459 * Read physical vector register lane
460 */
461 template<typename VecElem>
462 VecLaneT<VecElem, true>
463 readVecLane(PhysRegIdPtr phys_reg) const
464 {
465 vecRegfileReads++;
466 return regFile.readVecLane<VecElem>(phys_reg);
467 }
468
469 /** Write a lane of the destination vector register. */
470 template<typename LD>
471 void
472 setVecLane(PhysRegIdPtr phys_reg, const LD& val)
473 {
474 vecRegfileWrites++;
475 return regFile.setVecLane(phys_reg, val);
476 }
477
478 const VecElem& readVecElem(PhysRegIdPtr reg_idx) const;
479
480 const VecPredRegContainer& readVecPredReg(PhysRegIdPtr reg_idx) const;
481
482 VecPredRegContainer& getWritableVecPredReg(PhysRegIdPtr reg_idx);
483
484 RegVal readCCReg(PhysRegIdPtr phys_reg);
485
486 void setIntReg(PhysRegIdPtr phys_reg, RegVal val);
487
488 void setFloatReg(PhysRegIdPtr phys_reg, RegVal val);
489
490 void setVecReg(PhysRegIdPtr reg_idx, const VecRegContainer& val);
491
492 void setVecElem(PhysRegIdPtr reg_idx, const VecElem& val);
493
494 void setVecPredReg(PhysRegIdPtr reg_idx, const VecPredRegContainer& val);
495
496 void setCCReg(PhysRegIdPtr phys_reg, RegVal val);
497
498 RegVal readArchIntReg(int reg_idx, ThreadID tid);
499
500 RegVal readArchFloatReg(int reg_idx, ThreadID tid);
501
502 const VecRegContainer& readArchVecReg(int reg_idx, ThreadID tid) const;
503 /** Read architectural vector register for modification. */
504 VecRegContainer& getWritableArchVecReg(int reg_idx, ThreadID tid);
505
506 /** Read architectural vector register lane. */
507 template<typename VecElem>
508 VecLaneT<VecElem, true>
509 readArchVecLane(int reg_idx, int lId, ThreadID tid) const
510 {
511 PhysRegIdPtr phys_reg = commitRenameMap[tid].lookup(
512 RegId(VecRegClass, reg_idx));
513 return readVecLane<VecElem>(phys_reg);
514 }
515
516
517 /** Write a lane of the destination vector register. */
518 template<typename LD>
519 void
520 setArchVecLane(int reg_idx, int lId, ThreadID tid, const LD& val)
521 {
522 PhysRegIdPtr phys_reg = commitRenameMap[tid].lookup(
523 RegId(VecRegClass, reg_idx));
524 setVecLane(phys_reg, val);
525 }
526
527 const VecElem& readArchVecElem(const RegIndex& reg_idx,
528 const ElemIndex& ldx, ThreadID tid) const;
529
530 const VecPredRegContainer& readArchVecPredReg(int reg_idx,
531 ThreadID tid) const;
532
533 VecPredRegContainer& getWritableArchVecPredReg(int reg_idx, ThreadID tid);
534
535 RegVal readArchCCReg(int reg_idx, ThreadID tid);
536
537 /** Architectural register accessors. Looks up in the commit
538 * rename table to obtain the true physical index of the
539 * architected register first, then accesses that physical
540 * register.
541 */
542 void setArchIntReg(int reg_idx, RegVal val, ThreadID tid);
543
544 void setArchFloatReg(int reg_idx, RegVal val, ThreadID tid);
545
546 void setArchVecPredReg(int reg_idx, const VecPredRegContainer& val,
547 ThreadID tid);
548
549 void setArchVecReg(int reg_idx, const VecRegContainer& val, ThreadID tid);
550
551 void setArchVecElem(const RegIndex& reg_idx, const ElemIndex& ldx,
552 const VecElem& val, ThreadID tid);
553
554 void setArchCCReg(int reg_idx, RegVal val, ThreadID tid);
555
556 /** Sets the commit PC state of a specific thread. */
557 void pcState(const TheISA::PCState &newPCState, ThreadID tid);
558
559 /** Reads the commit PC state of a specific thread. */
560 TheISA::PCState pcState(ThreadID tid);
561
562 /** Reads the commit PC of a specific thread. */
563 Addr instAddr(ThreadID tid);
564
565 /** Reads the commit micro PC of a specific thread. */
566 MicroPC microPC(ThreadID tid);
567
568 /** Reads the next PC of a specific thread. */
569 Addr nextInstAddr(ThreadID tid);
570
571 /** Initiates a squash of all in-flight instructions for a given
572 * thread. The source of the squash is an external update of
573 * state through the TC.
574 */
575 void squashFromTC(ThreadID tid);
576
577 /** Function to add instruction onto the head of the list of the
578 * instructions. Used when new instructions are fetched.
579 */
580 ListIt addInst(const DynInstPtr &inst);
581
582 /** Function to tell the CPU that an instruction has completed. */
583 void instDone(ThreadID tid, const DynInstPtr &inst);
584
585 /** Remove an instruction from the front end of the list. There's
586 * no restriction on location of the instruction.
587 */
588 void removeFrontInst(const DynInstPtr &inst);
589
590 /** Remove all instructions that are not currently in the ROB.
591 * There's also an option to not squash delay slot instructions.*/
592 void removeInstsNotInROB(ThreadID tid);
593
594 /** Remove all instructions younger than the given sequence number. */
595 void removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid);
596
597 /** Removes the instruction pointed to by the iterator. */
598 inline void squashInstIt(const ListIt &instIt, ThreadID tid);
599
600 /** Cleans up all instructions on the remove list. */
601 void cleanUpRemovedInsts();
602
603 /** Debug function to print all instructions on the list. */
604 void dumpInsts();
605
606 public:
607#ifndef NDEBUG
608 /** Count of total number of dynamic instructions in flight. */
609 int instcount;
610#endif
611
612 /** List of all the instructions in flight. */
613 std::list<DynInstPtr> instList;
614
615 /** List of all the instructions that will be removed at the end of this
616 * cycle.
617 */
618 std::queue<ListIt> removeList;
619
620#ifdef DEBUG
621 /** Debug structure to keep track of the sequence numbers still in
622 * flight.
623 */
624 std::set<InstSeqNum> snList;
625#endif
626
627 /** Records if instructions need to be removed this cycle due to
628 * being retired or squashed.
629 */
630 bool removeInstsThisCycle;
631
632 protected:
633 /** The fetch stage. */
634 typename CPUPolicy::Fetch fetch;
635
636 /** The decode stage. */
637 typename CPUPolicy::Decode decode;
638
639 /** The dispatch stage. */
640 typename CPUPolicy::Rename rename;
641
642 /** The issue/execute/writeback stages. */
643 typename CPUPolicy::IEW iew;
644
645 /** The commit stage. */
646 typename CPUPolicy::Commit commit;
647
648 /** The rename mode of the vector registers */
649 Enums::VecRegRenameMode vecMode;
650
651 /** The register file. */
652 PhysRegFile regFile;
653
654 /** The free list. */
655 typename CPUPolicy::FreeList freeList;
656
657 /** The rename map. */
658 typename CPUPolicy::RenameMap renameMap[Impl::MaxThreads];
659
660 /** The commit rename map. */
661 typename CPUPolicy::RenameMap commitRenameMap[Impl::MaxThreads];
662
663 /** The re-order buffer. */
664 typename CPUPolicy::ROB rob;
665
666 /** Active Threads List */
667 std::list<ThreadID> activeThreads;
668
669 /**
670 * This is a list of threads that are trying to exit. Each thread id
671 * is mapped to a boolean value denoting whether the thread is ready
672 * to exit.
673 */
674 std::unordered_map<ThreadID, bool> exitingThreads;
675
676 /** Integer Register Scoreboard */
677 Scoreboard scoreboard;
678
679 std::vector<TheISA::ISA *> isa;
680
681 /** Instruction port. Note that it has to appear after the fetch stage. */
682 IcachePort icachePort;
683
684 /** Data port. Note that it has to appear after the iew stages */
685 DcachePort dcachePort;
686
687 public:
688 /** Enum to give each stage a specific index, so when calling
689 * activateStage() or deactivateStage(), they can specify which stage
690 * is being activated/deactivated.
691 */
692 enum StageIdx {
693 FetchIdx,
694 DecodeIdx,
695 RenameIdx,
696 IEWIdx,
697 CommitIdx,
698 NumStages };
699
700 /** Typedefs from the Impl to get the structs that each of the
701 * time buffers should use.
702 */
703 typedef typename CPUPolicy::TimeStruct TimeStruct;
704
705 typedef typename CPUPolicy::FetchStruct FetchStruct;
706
707 typedef typename CPUPolicy::DecodeStruct DecodeStruct;
708
709 typedef typename CPUPolicy::RenameStruct RenameStruct;
710
711 typedef typename CPUPolicy::IEWStruct IEWStruct;
712
713 /** The main time buffer to do backwards communication. */
714 TimeBuffer<TimeStruct> timeBuffer;
715
716 /** The fetch stage's instruction queue. */
717 TimeBuffer<FetchStruct> fetchQueue;
718
719 /** The decode stage's instruction queue. */
720 TimeBuffer<DecodeStruct> decodeQueue;
721
722 /** The rename stage's instruction queue. */
723 TimeBuffer<RenameStruct> renameQueue;
724
725 /** The IEW stage's instruction queue. */
726 TimeBuffer<IEWStruct> iewQueue;
727
728 private:
729 /** The activity recorder; used to tell if the CPU has any
730 * activity remaining or if it can go to idle and deschedule
731 * itself.
732 */
733 ActivityRecorder activityRec;
734
735 public:
736 /** Records that there was time buffer activity this cycle. */
737 void activityThisCycle() { activityRec.activity(); }
738
739 /** Changes a stage's status to active within the activity recorder. */
740 void activateStage(const StageIdx idx)
741 { activityRec.activateStage(idx); }
742
743 /** Changes a stage's status to inactive within the activity recorder. */
744 void deactivateStage(const StageIdx idx)
745 { activityRec.deactivateStage(idx); }
746
747 /** Wakes the CPU, rescheduling the CPU if it's not already active. */
748 void wakeCPU();
749
750 virtual void wakeup(ThreadID tid) override;
751
752 /** Gets a free thread id. Use if thread ids change across system. */
753 ThreadID getFreeTid();
754
755 public:
756 /** Returns a pointer to a thread context. */
757 ThreadContext *
758 tcBase(ThreadID tid)
759 {
760 return thread[tid]->getTC();
761 }
762
763 /** The global sequence number counter. */
764 InstSeqNum globalSeqNum;//[Impl::MaxThreads];
765
766 /** Pointer to the checker, which can dynamically verify
767 * instruction results at run time. This can be set to NULL if it
768 * is not being used.
769 */
770 Checker<Impl> *checker;
771
772 /** Pointer to the system. */
773 System *system;
774
775 /** Pointers to all of the threads in the CPU. */
776 std::vector<Thread *> thread;
777
778 /** Threads Scheduled to Enter CPU */
779 std::list<int> cpuWaitList;
780
781 /** The cycle that the CPU was last running, used for statistics. */
782 Cycles lastRunningCycle;
783
784 /** The cycle that the CPU was last activated by a new thread*/
785 Tick lastActivatedCycle;
786
787 /** Mapping for system thread id to cpu id */
788 std::map<ThreadID, unsigned> threadMap;
789
790 /** Available thread ids in the cpu*/
791 std::vector<ThreadID> tids;
792
793 /** CPU pushRequest function, forwards request to LSQ. */
794 Fault pushRequest(const DynInstPtr& inst, bool isLoad, uint8_t *data,
795 unsigned int size, Addr addr, Request::Flags flags,
796 uint64_t *res, AtomicOpFunctor *amo_op = nullptr)
797 {
798 return iew.ldstQueue.pushRequest(inst, isLoad, data, size, addr,
799 flags, res, amo_op);
800 }
801
802 /** CPU read function, forwards read to LSQ. */
803 Fault read(LSQRequest* req, int load_idx)
804 {
805 return this->iew.ldstQueue.read(req, load_idx);
806 }
807
808 /** CPU write function, forwards write to LSQ. */
809 Fault write(LSQRequest* req, uint8_t *data, int store_idx)
810 {
811 return this->iew.ldstQueue.write(req, data, store_idx);
812 }
813
814 /** Used by the fetch unit to get a hold of the instruction port. */
815 MasterPort &getInstPort() override { return icachePort; }
816
817 /** Get the dcache port (used to find block size for translations). */
818 MasterPort &getDataPort() override { return dcachePort; }
819
820 /** Stat for total number of times the CPU is descheduled. */
821 Stats::Scalar timesIdled;
822 /** Stat for total number of cycles the CPU spends descheduled. */
823 Stats::Scalar idleCycles;
824 /** Stat for total number of cycles the CPU spends descheduled due to a
825 * quiesce operation or waiting for an interrupt. */
826 Stats::Scalar quiesceCycles;
827 /** Stat for the number of committed instructions per thread. */
828 Stats::Vector committedInsts;
829 /** Stat for the number of committed ops (including micro ops) per thread. */
830 Stats::Vector committedOps;
831 /** Stat for the CPI per thread. */
832 Stats::Formula cpi;
833 /** Stat for the total CPI. */
834 Stats::Formula totalCpi;
835 /** Stat for the IPC per thread. */
836 Stats::Formula ipc;
837 /** Stat for the total IPC. */
838 Stats::Formula totalIpc;
839
840 //number of integer register file accesses
841 Stats::Scalar intRegfileReads;
842 Stats::Scalar intRegfileWrites;
843 //number of float register file accesses
844 Stats::Scalar fpRegfileReads;
845 Stats::Scalar fpRegfileWrites;
846 //number of vector register file accesses
847 mutable Stats::Scalar vecRegfileReads;
848 Stats::Scalar vecRegfileWrites;
849 //number of predicate register file accesses
850 mutable Stats::Scalar vecPredRegfileReads;
851 Stats::Scalar vecPredRegfileWrites;
852 //number of CC register file accesses
853 Stats::Scalar ccRegfileReads;
854 Stats::Scalar ccRegfileWrites;
855 //number of misc
856 Stats::Scalar miscRegfileReads;
857 Stats::Scalar miscRegfileWrites;
858};
859
860#endif // __CPU_O3_CPU_HH__
79class Process;
80
81struct BaseCPUParams;
82
83class BaseO3CPU : public BaseCPU
84{
85 //Stuff that's pretty ISA independent will go here.
86 public:
87 BaseO3CPU(BaseCPUParams *params);
88
89 void regStats();
90};
91
92/**
93 * FullO3CPU class, has each of the stages (fetch through commit)
94 * within it, as well as all of the time buffers between stages. The
95 * tick() function for the CPU is defined here.
96 */
97template <class Impl>
98class FullO3CPU : public BaseO3CPU
99{
100 public:
101 // Typedefs from the Impl here.
102 typedef typename Impl::CPUPol CPUPolicy;
103 typedef typename Impl::DynInstPtr DynInstPtr;
104 typedef typename Impl::O3CPU O3CPU;
105
106 using VecElem = TheISA::VecElem;
107 using VecRegContainer = TheISA::VecRegContainer;
108
109 using VecPredRegContainer = TheISA::VecPredRegContainer;
110
111 typedef O3ThreadState<Impl> ImplState;
112 typedef O3ThreadState<Impl> Thread;
113
114 typedef typename std::list<DynInstPtr>::iterator ListIt;
115
116 friend class O3ThreadContext<Impl>;
117
118 public:
119 enum Status {
120 Running,
121 Idle,
122 Halted,
123 Blocked,
124 SwitchedOut
125 };
126
127 BaseTLB *itb;
128 BaseTLB *dtb;
129 using LSQRequest = typename LSQ<Impl>::LSQRequest;
130
131 /** Overall CPU status. */
132 Status _status;
133
134 private:
135
136 /**
137 * IcachePort class for instruction fetch.
138 */
139 class IcachePort : public MasterPort
140 {
141 protected:
142 /** Pointer to fetch. */
143 DefaultFetch<Impl> *fetch;
144
145 public:
146 /** Default constructor. */
147 IcachePort(DefaultFetch<Impl> *_fetch, FullO3CPU<Impl>* _cpu)
148 : MasterPort(_cpu->name() + ".icache_port", _cpu), fetch(_fetch)
149 { }
150
151 protected:
152
153 /** Timing version of receive. Handles setting fetch to the
154 * proper status to start fetching. */
155 virtual bool recvTimingResp(PacketPtr pkt);
156
157 /** Handles doing a retry of a failed fetch. */
158 virtual void recvReqRetry();
159 };
160
161 /**
162 * DcachePort class for the load/store queue.
163 */
164 class DcachePort : public MasterPort
165 {
166 protected:
167
168 /** Pointer to LSQ. */
169 LSQ<Impl> *lsq;
170 FullO3CPU<Impl> *cpu;
171
172 public:
173 /** Default constructor. */
174 DcachePort(LSQ<Impl> *_lsq, FullO3CPU<Impl>* _cpu)
175 : MasterPort(_cpu->name() + ".dcache_port", _cpu), lsq(_lsq),
176 cpu(_cpu)
177 { }
178
179 protected:
180
181 /** Timing version of receive. Handles writing back and
182 * completing the load or store that has returned from
183 * memory. */
184 virtual bool recvTimingResp(PacketPtr pkt);
185 virtual void recvTimingSnoopReq(PacketPtr pkt);
186
187 virtual void recvFunctionalSnoop(PacketPtr pkt)
188 {
189 // @todo: Is there a need for potential invalidation here?
190 }
191
192 /** Handles doing a retry of the previous send. */
193 virtual void recvReqRetry();
194
195 /**
196 * As this CPU requires snooping to maintain the load store queue
197 * change the behaviour from the base CPU port.
198 *
199 * @return true since we have to snoop
200 */
201 virtual bool isSnooping() const { return true; }
202 };
203
204 /** The tick event used for scheduling CPU ticks. */
205 EventFunctionWrapper tickEvent;
206
207 /** The exit event used for terminating all ready-to-exit threads */
208 EventFunctionWrapper threadExitEvent;
209
210 /** Schedule tick event, regardless of its current state. */
211 void scheduleTickEvent(Cycles delay)
212 {
213 if (tickEvent.squashed())
214 reschedule(tickEvent, clockEdge(delay));
215 else if (!tickEvent.scheduled())
216 schedule(tickEvent, clockEdge(delay));
217 }
218
219 /** Unschedule tick event, regardless of its current state. */
220 void unscheduleTickEvent()
221 {
222 if (tickEvent.scheduled())
223 tickEvent.squash();
224 }
225
226 /**
227 * Check if the pipeline has drained and signal drain done.
228 *
229 * This method checks if a drain has been requested and if the CPU
230 * has drained successfully (i.e., there are no instructions in
231 * the pipeline). If the CPU has drained, it deschedules the tick
232 * event and signals the drain manager.
233 *
234 * @return False if a drain hasn't been requested or the CPU
235 * hasn't drained, true otherwise.
236 */
237 bool tryDrain();
238
239 /**
240 * Perform sanity checks after a drain.
241 *
242 * This method is called from drain() when it has determined that
243 * the CPU is fully drained when gem5 is compiled with the NDEBUG
244 * macro undefined. The intention of this method is to do more
245 * extensive tests than the isDrained() method to weed out any
246 * draining bugs.
247 */
248 void drainSanityCheck() const;
249
250 /** Check if a system is in a drained state. */
251 bool isDrained() const;
252
253 public:
254 /** Constructs a CPU with the given parameters. */
255 FullO3CPU(DerivO3CPUParams *params);
256 /** Destructor. */
257 ~FullO3CPU();
258
259 /** Registers statistics. */
260 void regStats() override;
261
262 ProbePointArg<PacketPtr> *ppInstAccessComplete;
263 ProbePointArg<std::pair<DynInstPtr, PacketPtr> > *ppDataAccessComplete;
264
265 /** Register probe points. */
266 void regProbePoints() override;
267
268 void demapPage(Addr vaddr, uint64_t asn)
269 {
270 this->itb->demapPage(vaddr, asn);
271 this->dtb->demapPage(vaddr, asn);
272 }
273
274 void demapInstPage(Addr vaddr, uint64_t asn)
275 {
276 this->itb->demapPage(vaddr, asn);
277 }
278
279 void demapDataPage(Addr vaddr, uint64_t asn)
280 {
281 this->dtb->demapPage(vaddr, asn);
282 }
283
284 /** Ticks CPU, calling tick() on each stage, and checking the overall
285 * activity to see if the CPU should deschedule itself.
286 */
287 void tick();
288
289 /** Initialize the CPU */
290 void init() override;
291
292 void startup() override;
293
294 /** Returns the Number of Active Threads in the CPU */
295 int numActiveThreads()
296 { return activeThreads.size(); }
297
298 /** Add Thread to Active Threads List */
299 void activateThread(ThreadID tid);
300
301 /** Remove Thread from Active Threads List */
302 void deactivateThread(ThreadID tid);
303
304 /** Setup CPU to insert a thread's context */
305 void insertThread(ThreadID tid);
306
307 /** Remove all of a thread's context from CPU */
308 void removeThread(ThreadID tid);
309
310 /** Count the Total Instructions Committed in the CPU. */
311 Counter totalInsts() const override;
312
313 /** Count the Total Ops (including micro ops) committed in the CPU. */
314 Counter totalOps() const override;
315
316 /** Add Thread to Active Threads List. */
317 void activateContext(ThreadID tid) override;
318
319 /** Remove Thread from Active Threads List */
320 void suspendContext(ThreadID tid) override;
321
322 /** Remove Thread from Active Threads List &&
323 * Remove Thread Context from CPU.
324 */
325 void haltContext(ThreadID tid) override;
326
327 /** Update The Order In Which We Process Threads. */
328 void updateThreadPriority();
329
330 /** Is the CPU draining? */
331 bool isDraining() const { return drainState() == DrainState::Draining; }
332
333 void serializeThread(CheckpointOut &cp, ThreadID tid) const override;
334 void unserializeThread(CheckpointIn &cp, ThreadID tid) override;
335
336 /** Insert tid to the list of threads trying to exit */
337 void addThreadToExitingList(ThreadID tid);
338
339 /** Is the thread trying to exit? */
340 bool isThreadExiting(ThreadID tid) const;
341
342 /**
343 * If a thread is trying to exit and its corresponding trap event
344 * has been completed, schedule an event to terminate the thread.
345 */
346 void scheduleThreadExitEvent(ThreadID tid);
347
348 /** Terminate all threads that are ready to exit */
349 void exitThreads();
350
351 public:
352 /** Executes a syscall.
353 * @todo: Determine if this needs to be virtual.
354 */
355 void syscall(int64_t callnum, ThreadID tid, Fault *fault);
356
357 /** Starts draining the CPU's pipeline of all instructions in
358 * order to stop all memory accesses. */
359 DrainState drain() override;
360
361 /** Resumes execution after a drain. */
362 void drainResume() override;
363
364 /**
365 * Commit has reached a safe point to drain a thread.
366 *
367 * Commit calls this method to inform the pipeline that it has
368 * reached a point where it is not executed microcode and is about
369 * to squash uncommitted instructions to fully drain the pipeline.
370 */
371 void commitDrained(ThreadID tid);
372
373 /** Switches out this CPU. */
374 void switchOut() override;
375
376 /** Takes over from another CPU. */
377 void takeOverFrom(BaseCPU *oldCPU) override;
378
379 void verifyMemoryMode() const override;
380
381 /** Get the current instruction sequence number, and increment it. */
382 InstSeqNum getAndIncrementInstSeq()
383 { return globalSeqNum++; }
384
385 /** Traps to handle given fault. */
386 void trap(const Fault &fault, ThreadID tid, const StaticInstPtr &inst);
387
388 /** HW return from error interrupt. */
389 Fault hwrei(ThreadID tid);
390
391 bool simPalCheck(int palFunc, ThreadID tid);
392
393 /** Check if a change in renaming is needed for vector registers.
394 * The vecMode variable is updated and propagated to rename maps.
395 *
396 * @param tid ThreadID
397 * @param freelist list of free registers
398 */
399 void switchRenameMode(ThreadID tid, UnifiedFreeList* freelist);
400
401 /** Returns the Fault for any valid interrupt. */
402 Fault getInterrupts();
403
404 /** Processes any an interrupt fault. */
405 void processInterrupts(const Fault &interrupt);
406
407 /** Halts the CPU. */
408 void halt() { panic("Halt not implemented!\n"); }
409
410 /** Register accessors. Index refers to the physical register index. */
411
412 /** Reads a miscellaneous register. */
413 RegVal readMiscRegNoEffect(int misc_reg, ThreadID tid) const;
414
415 /** Reads a misc. register, including any side effects the read
416 * might have as defined by the architecture.
417 */
418 RegVal readMiscReg(int misc_reg, ThreadID tid);
419
420 /** Sets a miscellaneous register. */
421 void setMiscRegNoEffect(int misc_reg, RegVal val, ThreadID tid);
422
423 /** Sets a misc. register, including any side effects the write
424 * might have as defined by the architecture.
425 */
426 void setMiscReg(int misc_reg, RegVal val, ThreadID tid);
427
428 RegVal readIntReg(PhysRegIdPtr phys_reg);
429
430 RegVal readFloatReg(PhysRegIdPtr phys_reg);
431
432 const VecRegContainer& readVecReg(PhysRegIdPtr reg_idx) const;
433
434 /**
435 * Read physical vector register for modification.
436 */
437 VecRegContainer& getWritableVecReg(PhysRegIdPtr reg_idx);
438
439 /** Returns current vector renaming mode */
440 Enums::VecRegRenameMode vecRenameMode() const { return vecMode; }
441
442 /** Sets the current vector renaming mode */
443 void vecRenameMode(Enums::VecRegRenameMode vec_mode)
444 { vecMode = vec_mode; }
445
446 /**
447 * Read physical vector register lane
448 */
449 template<typename VecElem, int LaneIdx>
450 VecLaneT<VecElem, true>
451 readVecLane(PhysRegIdPtr phys_reg) const
452 {
453 vecRegfileReads++;
454 return regFile.readVecLane<VecElem, LaneIdx>(phys_reg);
455 }
456
457 /**
458 * Read physical vector register lane
459 */
460 template<typename VecElem>
461 VecLaneT<VecElem, true>
462 readVecLane(PhysRegIdPtr phys_reg) const
463 {
464 vecRegfileReads++;
465 return regFile.readVecLane<VecElem>(phys_reg);
466 }
467
468 /** Write a lane of the destination vector register. */
469 template<typename LD>
470 void
471 setVecLane(PhysRegIdPtr phys_reg, const LD& val)
472 {
473 vecRegfileWrites++;
474 return regFile.setVecLane(phys_reg, val);
475 }
476
477 const VecElem& readVecElem(PhysRegIdPtr reg_idx) const;
478
479 const VecPredRegContainer& readVecPredReg(PhysRegIdPtr reg_idx) const;
480
481 VecPredRegContainer& getWritableVecPredReg(PhysRegIdPtr reg_idx);
482
483 RegVal readCCReg(PhysRegIdPtr phys_reg);
484
485 void setIntReg(PhysRegIdPtr phys_reg, RegVal val);
486
487 void setFloatReg(PhysRegIdPtr phys_reg, RegVal val);
488
489 void setVecReg(PhysRegIdPtr reg_idx, const VecRegContainer& val);
490
491 void setVecElem(PhysRegIdPtr reg_idx, const VecElem& val);
492
493 void setVecPredReg(PhysRegIdPtr reg_idx, const VecPredRegContainer& val);
494
495 void setCCReg(PhysRegIdPtr phys_reg, RegVal val);
496
497 RegVal readArchIntReg(int reg_idx, ThreadID tid);
498
499 RegVal readArchFloatReg(int reg_idx, ThreadID tid);
500
501 const VecRegContainer& readArchVecReg(int reg_idx, ThreadID tid) const;
502 /** Read architectural vector register for modification. */
503 VecRegContainer& getWritableArchVecReg(int reg_idx, ThreadID tid);
504
505 /** Read architectural vector register lane. */
506 template<typename VecElem>
507 VecLaneT<VecElem, true>
508 readArchVecLane(int reg_idx, int lId, ThreadID tid) const
509 {
510 PhysRegIdPtr phys_reg = commitRenameMap[tid].lookup(
511 RegId(VecRegClass, reg_idx));
512 return readVecLane<VecElem>(phys_reg);
513 }
514
515
516 /** Write a lane of the destination vector register. */
517 template<typename LD>
518 void
519 setArchVecLane(int reg_idx, int lId, ThreadID tid, const LD& val)
520 {
521 PhysRegIdPtr phys_reg = commitRenameMap[tid].lookup(
522 RegId(VecRegClass, reg_idx));
523 setVecLane(phys_reg, val);
524 }
525
526 const VecElem& readArchVecElem(const RegIndex& reg_idx,
527 const ElemIndex& ldx, ThreadID tid) const;
528
529 const VecPredRegContainer& readArchVecPredReg(int reg_idx,
530 ThreadID tid) const;
531
532 VecPredRegContainer& getWritableArchVecPredReg(int reg_idx, ThreadID tid);
533
534 RegVal readArchCCReg(int reg_idx, ThreadID tid);
535
536 /** Architectural register accessors. Looks up in the commit
537 * rename table to obtain the true physical index of the
538 * architected register first, then accesses that physical
539 * register.
540 */
541 void setArchIntReg(int reg_idx, RegVal val, ThreadID tid);
542
543 void setArchFloatReg(int reg_idx, RegVal val, ThreadID tid);
544
545 void setArchVecPredReg(int reg_idx, const VecPredRegContainer& val,
546 ThreadID tid);
547
548 void setArchVecReg(int reg_idx, const VecRegContainer& val, ThreadID tid);
549
550 void setArchVecElem(const RegIndex& reg_idx, const ElemIndex& ldx,
551 const VecElem& val, ThreadID tid);
552
553 void setArchCCReg(int reg_idx, RegVal val, ThreadID tid);
554
555 /** Sets the commit PC state of a specific thread. */
556 void pcState(const TheISA::PCState &newPCState, ThreadID tid);
557
558 /** Reads the commit PC state of a specific thread. */
559 TheISA::PCState pcState(ThreadID tid);
560
561 /** Reads the commit PC of a specific thread. */
562 Addr instAddr(ThreadID tid);
563
564 /** Reads the commit micro PC of a specific thread. */
565 MicroPC microPC(ThreadID tid);
566
567 /** Reads the next PC of a specific thread. */
568 Addr nextInstAddr(ThreadID tid);
569
570 /** Initiates a squash of all in-flight instructions for a given
571 * thread. The source of the squash is an external update of
572 * state through the TC.
573 */
574 void squashFromTC(ThreadID tid);
575
576 /** Function to add instruction onto the head of the list of the
577 * instructions. Used when new instructions are fetched.
578 */
579 ListIt addInst(const DynInstPtr &inst);
580
581 /** Function to tell the CPU that an instruction has completed. */
582 void instDone(ThreadID tid, const DynInstPtr &inst);
583
584 /** Remove an instruction from the front end of the list. There's
585 * no restriction on location of the instruction.
586 */
587 void removeFrontInst(const DynInstPtr &inst);
588
589 /** Remove all instructions that are not currently in the ROB.
590 * There's also an option to not squash delay slot instructions.*/
591 void removeInstsNotInROB(ThreadID tid);
592
593 /** Remove all instructions younger than the given sequence number. */
594 void removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid);
595
596 /** Removes the instruction pointed to by the iterator. */
597 inline void squashInstIt(const ListIt &instIt, ThreadID tid);
598
599 /** Cleans up all instructions on the remove list. */
600 void cleanUpRemovedInsts();
601
602 /** Debug function to print all instructions on the list. */
603 void dumpInsts();
604
605 public:
606#ifndef NDEBUG
607 /** Count of total number of dynamic instructions in flight. */
608 int instcount;
609#endif
610
611 /** List of all the instructions in flight. */
612 std::list<DynInstPtr> instList;
613
614 /** List of all the instructions that will be removed at the end of this
615 * cycle.
616 */
617 std::queue<ListIt> removeList;
618
619#ifdef DEBUG
620 /** Debug structure to keep track of the sequence numbers still in
621 * flight.
622 */
623 std::set<InstSeqNum> snList;
624#endif
625
626 /** Records if instructions need to be removed this cycle due to
627 * being retired or squashed.
628 */
629 bool removeInstsThisCycle;
630
631 protected:
632 /** The fetch stage. */
633 typename CPUPolicy::Fetch fetch;
634
635 /** The decode stage. */
636 typename CPUPolicy::Decode decode;
637
638 /** The dispatch stage. */
639 typename CPUPolicy::Rename rename;
640
641 /** The issue/execute/writeback stages. */
642 typename CPUPolicy::IEW iew;
643
644 /** The commit stage. */
645 typename CPUPolicy::Commit commit;
646
647 /** The rename mode of the vector registers */
648 Enums::VecRegRenameMode vecMode;
649
650 /** The register file. */
651 PhysRegFile regFile;
652
653 /** The free list. */
654 typename CPUPolicy::FreeList freeList;
655
656 /** The rename map. */
657 typename CPUPolicy::RenameMap renameMap[Impl::MaxThreads];
658
659 /** The commit rename map. */
660 typename CPUPolicy::RenameMap commitRenameMap[Impl::MaxThreads];
661
662 /** The re-order buffer. */
663 typename CPUPolicy::ROB rob;
664
665 /** Active Threads List */
666 std::list<ThreadID> activeThreads;
667
668 /**
669 * This is a list of threads that are trying to exit. Each thread id
670 * is mapped to a boolean value denoting whether the thread is ready
671 * to exit.
672 */
673 std::unordered_map<ThreadID, bool> exitingThreads;
674
675 /** Integer Register Scoreboard */
676 Scoreboard scoreboard;
677
678 std::vector<TheISA::ISA *> isa;
679
680 /** Instruction port. Note that it has to appear after the fetch stage. */
681 IcachePort icachePort;
682
683 /** Data port. Note that it has to appear after the iew stages */
684 DcachePort dcachePort;
685
686 public:
687 /** Enum to give each stage a specific index, so when calling
688 * activateStage() or deactivateStage(), they can specify which stage
689 * is being activated/deactivated.
690 */
691 enum StageIdx {
692 FetchIdx,
693 DecodeIdx,
694 RenameIdx,
695 IEWIdx,
696 CommitIdx,
697 NumStages };
698
699 /** Typedefs from the Impl to get the structs that each of the
700 * time buffers should use.
701 */
702 typedef typename CPUPolicy::TimeStruct TimeStruct;
703
704 typedef typename CPUPolicy::FetchStruct FetchStruct;
705
706 typedef typename CPUPolicy::DecodeStruct DecodeStruct;
707
708 typedef typename CPUPolicy::RenameStruct RenameStruct;
709
710 typedef typename CPUPolicy::IEWStruct IEWStruct;
711
712 /** The main time buffer to do backwards communication. */
713 TimeBuffer<TimeStruct> timeBuffer;
714
715 /** The fetch stage's instruction queue. */
716 TimeBuffer<FetchStruct> fetchQueue;
717
718 /** The decode stage's instruction queue. */
719 TimeBuffer<DecodeStruct> decodeQueue;
720
721 /** The rename stage's instruction queue. */
722 TimeBuffer<RenameStruct> renameQueue;
723
724 /** The IEW stage's instruction queue. */
725 TimeBuffer<IEWStruct> iewQueue;
726
727 private:
728 /** The activity recorder; used to tell if the CPU has any
729 * activity remaining or if it can go to idle and deschedule
730 * itself.
731 */
732 ActivityRecorder activityRec;
733
734 public:
735 /** Records that there was time buffer activity this cycle. */
736 void activityThisCycle() { activityRec.activity(); }
737
738 /** Changes a stage's status to active within the activity recorder. */
739 void activateStage(const StageIdx idx)
740 { activityRec.activateStage(idx); }
741
742 /** Changes a stage's status to inactive within the activity recorder. */
743 void deactivateStage(const StageIdx idx)
744 { activityRec.deactivateStage(idx); }
745
746 /** Wakes the CPU, rescheduling the CPU if it's not already active. */
747 void wakeCPU();
748
749 virtual void wakeup(ThreadID tid) override;
750
751 /** Gets a free thread id. Use if thread ids change across system. */
752 ThreadID getFreeTid();
753
754 public:
755 /** Returns a pointer to a thread context. */
756 ThreadContext *
757 tcBase(ThreadID tid)
758 {
759 return thread[tid]->getTC();
760 }
761
762 /** The global sequence number counter. */
763 InstSeqNum globalSeqNum;//[Impl::MaxThreads];
764
765 /** Pointer to the checker, which can dynamically verify
766 * instruction results at run time. This can be set to NULL if it
767 * is not being used.
768 */
769 Checker<Impl> *checker;
770
771 /** Pointer to the system. */
772 System *system;
773
774 /** Pointers to all of the threads in the CPU. */
775 std::vector<Thread *> thread;
776
777 /** Threads Scheduled to Enter CPU */
778 std::list<int> cpuWaitList;
779
780 /** The cycle that the CPU was last running, used for statistics. */
781 Cycles lastRunningCycle;
782
783 /** The cycle that the CPU was last activated by a new thread*/
784 Tick lastActivatedCycle;
785
786 /** Mapping for system thread id to cpu id */
787 std::map<ThreadID, unsigned> threadMap;
788
789 /** Available thread ids in the cpu*/
790 std::vector<ThreadID> tids;
791
792 /** CPU pushRequest function, forwards request to LSQ. */
793 Fault pushRequest(const DynInstPtr& inst, bool isLoad, uint8_t *data,
794 unsigned int size, Addr addr, Request::Flags flags,
795 uint64_t *res, AtomicOpFunctor *amo_op = nullptr)
796 {
797 return iew.ldstQueue.pushRequest(inst, isLoad, data, size, addr,
798 flags, res, amo_op);
799 }
800
801 /** CPU read function, forwards read to LSQ. */
802 Fault read(LSQRequest* req, int load_idx)
803 {
804 return this->iew.ldstQueue.read(req, load_idx);
805 }
806
807 /** CPU write function, forwards write to LSQ. */
808 Fault write(LSQRequest* req, uint8_t *data, int store_idx)
809 {
810 return this->iew.ldstQueue.write(req, data, store_idx);
811 }
812
813 /** Used by the fetch unit to get a hold of the instruction port. */
814 MasterPort &getInstPort() override { return icachePort; }
815
816 /** Get the dcache port (used to find block size for translations). */
817 MasterPort &getDataPort() override { return dcachePort; }
818
819 /** Stat for total number of times the CPU is descheduled. */
820 Stats::Scalar timesIdled;
821 /** Stat for total number of cycles the CPU spends descheduled. */
822 Stats::Scalar idleCycles;
823 /** Stat for total number of cycles the CPU spends descheduled due to a
824 * quiesce operation or waiting for an interrupt. */
825 Stats::Scalar quiesceCycles;
826 /** Stat for the number of committed instructions per thread. */
827 Stats::Vector committedInsts;
828 /** Stat for the number of committed ops (including micro ops) per thread. */
829 Stats::Vector committedOps;
830 /** Stat for the CPI per thread. */
831 Stats::Formula cpi;
832 /** Stat for the total CPI. */
833 Stats::Formula totalCpi;
834 /** Stat for the IPC per thread. */
835 Stats::Formula ipc;
836 /** Stat for the total IPC. */
837 Stats::Formula totalIpc;
838
839 //number of integer register file accesses
840 Stats::Scalar intRegfileReads;
841 Stats::Scalar intRegfileWrites;
842 //number of float register file accesses
843 Stats::Scalar fpRegfileReads;
844 Stats::Scalar fpRegfileWrites;
845 //number of vector register file accesses
846 mutable Stats::Scalar vecRegfileReads;
847 Stats::Scalar vecRegfileWrites;
848 //number of predicate register file accesses
849 mutable Stats::Scalar vecPredRegfileReads;
850 Stats::Scalar vecPredRegfileWrites;
851 //number of CC register file accesses
852 Stats::Scalar ccRegfileReads;
853 Stats::Scalar ccRegfileWrites;
854 //number of misc
855 Stats::Scalar miscRegfileReads;
856 Stats::Scalar miscRegfileWrites;
857};
858
859#endif // __CPU_O3_CPU_HH__