cpu.hh revision 3221:669a04468c0d
1/* 2 * Copyright (c) 2004-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Kevin Lim 29 * Korey Sewell 30 */ 31 32#ifndef __CPU_O3_CPU_HH__ 33#define __CPU_O3_CPU_HH__ 34 35#include <iostream> 36#include <list> 37#include <queue> 38#include <set> 39#include <vector> 40 41#include "arch/types.hh" 42#include "base/statistics.hh" 43#include "base/timebuf.hh" 44#include "config/full_system.hh" 45#include "cpu/activity.hh" 46#include "cpu/base.hh" 47#include "cpu/simple_thread.hh" 48#include "cpu/o3/comm.hh" 49#include "cpu/o3/cpu_policy.hh" 50#include "cpu/o3/scoreboard.hh" 51#include "cpu/o3/thread_state.hh" 52//#include "cpu/o3/thread_context.hh" 53#include "sim/process.hh" 54 55template <class> 56class Checker; 57class ThreadContext; 58template <class> 59class O3ThreadContext; 60 61class Checkpoint; 62class MemObject; 63class Process; 64 65class BaseO3CPU : public BaseCPU 66{ 67 //Stuff that's pretty ISA independent will go here. 68 public: 69 typedef BaseCPU::Params Params; 70 71 BaseO3CPU(Params *params); 72 73 void regStats(); 74 75 /** Sets this CPU's ID. */ 76 void setCpuId(int id) { cpu_id = id; } 77 78 /** Reads this CPU's ID. */ 79 int readCpuId() { return cpu_id; } 80 81 protected: 82 int cpu_id; 83}; 84 85/** 86 * FullO3CPU class, has each of the stages (fetch through commit) 87 * within it, as well as all of the time buffers between stages. The 88 * tick() function for the CPU is defined here. 89 */ 90template <class Impl> 91class FullO3CPU : public BaseO3CPU 92{ 93 public: 94 typedef TheISA::FloatReg FloatReg; 95 typedef TheISA::FloatRegBits FloatRegBits; 96 97 // Typedefs from the Impl here. 98 typedef typename Impl::CPUPol CPUPolicy; 99 typedef typename Impl::Params Params; 100 typedef typename Impl::DynInstPtr DynInstPtr; 101 102 typedef O3ThreadState<Impl> Thread; 103 104 typedef typename std::list<DynInstPtr>::iterator ListIt; 105 106 friend class O3ThreadContext<Impl>; 107 108 public: 109 enum Status { 110 Running, 111 Idle, 112 Halted, 113 Blocked, 114 SwitchedOut 115 }; 116 117 /** Overall CPU status. */ 118 Status _status; 119 120 /** Per-thread status in CPU, used for SMT. */ 121 Status _threadStatus[Impl::MaxThreads]; 122 123 private: 124 class TickEvent : public Event 125 { 126 private: 127 /** Pointer to the CPU. */ 128 FullO3CPU<Impl> *cpu; 129 130 public: 131 /** Constructs a tick event. */ 132 TickEvent(FullO3CPU<Impl> *c); 133 134 /** Processes a tick event, calling tick() on the CPU. */ 135 void process(); 136 /** Returns the description of the tick event. */ 137 const char *description(); 138 }; 139 140 /** The tick event used for scheduling CPU ticks. */ 141 TickEvent tickEvent; 142 143 /** Schedule tick event, regardless of its current state. */ 144 void scheduleTickEvent(int delay) 145 { 146 if (tickEvent.squashed()) 147 tickEvent.reschedule(curTick + cycles(delay)); 148 else if (!tickEvent.scheduled()) 149 tickEvent.schedule(curTick + cycles(delay)); 150 } 151 152 /** Unschedule tick event, regardless of its current state. */ 153 void unscheduleTickEvent() 154 { 155 if (tickEvent.scheduled()) 156 tickEvent.squash(); 157 } 158 159 class ActivateThreadEvent : public Event 160 { 161 private: 162 /** Number of Thread to Activate */ 163 int tid; 164 165 /** Pointer to the CPU. */ 166 FullO3CPU<Impl> *cpu; 167 168 public: 169 /** Constructs the event. */ 170 ActivateThreadEvent(); 171 172 /** Initialize Event */ 173 void init(int thread_num, FullO3CPU<Impl> *thread_cpu); 174 175 /** Processes the event, calling activateThread() on the CPU. */ 176 void process(); 177 178 /** Returns the description of the event. */ 179 const char *description(); 180 }; 181 182 /** Schedule thread to activate , regardless of its current state. */ 183 void scheduleActivateThreadEvent(int tid, int delay) 184 { 185 // Schedule thread to activate, regardless of its current state. 186 if (activateThreadEvent[tid].squashed()) 187 activateThreadEvent[tid].reschedule(curTick + cycles(delay)); 188 else if (!activateThreadEvent[tid].scheduled()) 189 activateThreadEvent[tid].schedule(curTick + cycles(delay)); 190 } 191 192 /** Unschedule actiavte thread event, regardless of its current state. */ 193 void unscheduleActivateThreadEvent(int tid) 194 { 195 if (activateThreadEvent[tid].scheduled()) 196 activateThreadEvent[tid].squash(); 197 } 198 199 /** The tick event used for scheduling CPU ticks. */ 200 ActivateThreadEvent activateThreadEvent[Impl::MaxThreads]; 201 202 class DeallocateContextEvent : public Event 203 { 204 private: 205 /** Number of Thread to deactivate */ 206 int tid; 207 208 /** Should the thread be removed from the CPU? */ 209 bool remove; 210 211 /** Pointer to the CPU. */ 212 FullO3CPU<Impl> *cpu; 213 214 public: 215 /** Constructs the event. */ 216 DeallocateContextEvent(); 217 218 /** Initialize Event */ 219 void init(int thread_num, FullO3CPU<Impl> *thread_cpu); 220 221 /** Processes the event, calling activateThread() on the CPU. */ 222 void process(); 223 224 /** Sets whether the thread should also be removed from the CPU. */ 225 void setRemove(bool _remove) { remove = _remove; } 226 227 /** Returns the description of the event. */ 228 const char *description(); 229 }; 230 231 /** Schedule cpu to deallocate thread context.*/ 232 void scheduleDeallocateContextEvent(int tid, bool remove, int delay) 233 { 234 // Schedule thread to activate, regardless of its current state. 235 if (deallocateContextEvent[tid].squashed()) 236 deallocateContextEvent[tid].reschedule(curTick + cycles(delay)); 237 else if (!deallocateContextEvent[tid].scheduled()) 238 deallocateContextEvent[tid].schedule(curTick + cycles(delay)); 239 } 240 241 /** Unschedule thread deallocation in CPU */ 242 void unscheduleDeallocateContextEvent(int tid) 243 { 244 if (deallocateContextEvent[tid].scheduled()) 245 deallocateContextEvent[tid].squash(); 246 } 247 248 /** The tick event used for scheduling CPU ticks. */ 249 DeallocateContextEvent deallocateContextEvent[Impl::MaxThreads]; 250 251 public: 252 /** Constructs a CPU with the given parameters. */ 253 FullO3CPU(Params *params); 254 /** Destructor. */ 255 ~FullO3CPU(); 256 257 /** Registers statistics. */ 258 void fullCPURegStats(); 259 260 /** Returns a specific port. */ 261 Port *getPort(const std::string &if_name, int idx); 262 263 /** Ticks CPU, calling tick() on each stage, and checking the overall 264 * activity to see if the CPU should deschedule itself. 265 */ 266 void tick(); 267 268 /** Initialize the CPU */ 269 void init(); 270 271 /** Returns the Number of Active Threads in the CPU */ 272 int numActiveThreads() 273 { return activeThreads.size(); } 274 275 /** Add Thread to Active Threads List */ 276 void activateThread(unsigned tid); 277 278 /** Remove Thread from Active Threads List */ 279 void deactivateThread(unsigned tid); 280 281 /** Setup CPU to insert a thread's context */ 282 void insertThread(unsigned tid); 283 284 /** Remove all of a thread's context from CPU */ 285 void removeThread(unsigned tid); 286 287 /** Count the Total Instructions Committed in the CPU. */ 288 virtual Counter totalInstructions() const 289 { 290 Counter total(0); 291 292 for (int i=0; i < thread.size(); i++) 293 total += thread[i]->numInst; 294 295 return total; 296 } 297 298 /** Add Thread to Active Threads List. */ 299 void activateContext(int tid, int delay); 300 301 /** Remove Thread from Active Threads List */ 302 void suspendContext(int tid); 303 304 /** Remove Thread from Active Threads List && 305 * Possibly Remove Thread Context from CPU. 306 */ 307 bool deallocateContext(int tid, bool remove, int delay = 1); 308 309 /** Remove Thread from Active Threads List && 310 * Remove Thread Context from CPU. 311 */ 312 void haltContext(int tid); 313 314 /** Activate a Thread When CPU Resources are Available. */ 315 void activateWhenReady(int tid); 316 317 /** Add or Remove a Thread Context in the CPU. */ 318 void doContextSwitch(); 319 320 /** Update The Order In Which We Process Threads. */ 321 void updateThreadPriority(); 322 323 /** Serialize state. */ 324 virtual void serialize(std::ostream &os); 325 326 /** Unserialize from a checkpoint. */ 327 virtual void unserialize(Checkpoint *cp, const std::string §ion); 328 329 public: 330 /** Executes a syscall on this cycle. 331 * --------------------------------------- 332 * Note: this is a virtual function. CPU-Specific 333 * functionality defined in derived classes 334 */ 335 virtual void syscall(int tid) { panic("Unimplemented!"); } 336 337 /** Starts draining the CPU's pipeline of all instructions in 338 * order to stop all memory accesses. */ 339 virtual unsigned int drain(Event *drain_event); 340 341 /** Resumes execution after a drain. */ 342 virtual void resume(); 343 344 /** Signals to this CPU that a stage has completed switching out. */ 345 void signalDrained(); 346 347 /** Switches out this CPU. */ 348 virtual void switchOut(); 349 350 /** Takes over from another CPU. */ 351 virtual void takeOverFrom(BaseCPU *oldCPU); 352 353 /** Get the current instruction sequence number, and increment it. */ 354 InstSeqNum getAndIncrementInstSeq() 355 { return globalSeqNum++; } 356 357#if FULL_SYSTEM 358 /** Check if this address is a valid instruction address. */ 359 bool validInstAddr(Addr addr) { return true; } 360 361 /** Check if this address is a valid data address. */ 362 bool validDataAddr(Addr addr) { return true; } 363 364 /** Get instruction asid. */ 365 int getInstAsid(unsigned tid) 366 { return regFile.miscRegs[tid].getInstAsid(); } 367 368 /** Get data asid. */ 369 int getDataAsid(unsigned tid) 370 { return regFile.miscRegs[tid].getDataAsid(); } 371#else 372 /** Get instruction asid. */ 373 int getInstAsid(unsigned tid) 374 { return thread[tid]->getInstAsid(); } 375 376 /** Get data asid. */ 377 int getDataAsid(unsigned tid) 378 { return thread[tid]->getDataAsid(); } 379 380#endif 381 382 /** Register accessors. Index refers to the physical register index. */ 383 uint64_t readIntReg(int reg_idx); 384 385 FloatReg readFloatReg(int reg_idx); 386 387 FloatReg readFloatReg(int reg_idx, int width); 388 389 FloatRegBits readFloatRegBits(int reg_idx); 390 391 FloatRegBits readFloatRegBits(int reg_idx, int width); 392 393 void setIntReg(int reg_idx, uint64_t val); 394 395 void setFloatReg(int reg_idx, FloatReg val); 396 397 void setFloatReg(int reg_idx, FloatReg val, int width); 398 399 void setFloatRegBits(int reg_idx, FloatRegBits val); 400 401 void setFloatRegBits(int reg_idx, FloatRegBits val, int width); 402 403 uint64_t readArchIntReg(int reg_idx, unsigned tid); 404 405 float readArchFloatRegSingle(int reg_idx, unsigned tid); 406 407 double readArchFloatRegDouble(int reg_idx, unsigned tid); 408 409 uint64_t readArchFloatRegInt(int reg_idx, unsigned tid); 410 411 /** Architectural register accessors. Looks up in the commit 412 * rename table to obtain the true physical index of the 413 * architected register first, then accesses that physical 414 * register. 415 */ 416 void setArchIntReg(int reg_idx, uint64_t val, unsigned tid); 417 418 void setArchFloatRegSingle(int reg_idx, float val, unsigned tid); 419 420 void setArchFloatRegDouble(int reg_idx, double val, unsigned tid); 421 422 void setArchFloatRegInt(int reg_idx, uint64_t val, unsigned tid); 423 424 /** Reads the commit PC of a specific thread. */ 425 uint64_t readPC(unsigned tid); 426 427 /** Sets the commit PC of a specific thread. */ 428 void setPC(Addr new_PC, unsigned tid); 429 430 /** Reads the next PC of a specific thread. */ 431 uint64_t readNextPC(unsigned tid); 432 433 /** Sets the next PC of a specific thread. */ 434 void setNextPC(uint64_t val, unsigned tid); 435 436 /** Reads the next NPC of a specific thread. */ 437 uint64_t readNextNPC(unsigned tid); 438 439 /** Sets the next NPC of a specific thread. */ 440 void setNextNPC(uint64_t val, unsigned tid); 441 442 /** Function to add instruction onto the head of the list of the 443 * instructions. Used when new instructions are fetched. 444 */ 445 ListIt addInst(DynInstPtr &inst); 446 447 /** Function to tell the CPU that an instruction has completed. */ 448 void instDone(unsigned tid); 449 450 /** Add Instructions to the CPU Remove List*/ 451 void addToRemoveList(DynInstPtr &inst); 452 453 /** Remove an instruction from the front end of the list. There's 454 * no restriction on location of the instruction. 455 */ 456 void removeFrontInst(DynInstPtr &inst); 457 458 /** Remove all instructions that are not currently in the ROB. 459 * There's also an option to not squash delay slot instructions.*/ 460 void removeInstsNotInROB(unsigned tid, bool squash_delay_slot, 461 const InstSeqNum &delay_slot_seq_num); 462 463 /** Remove all instructions younger than the given sequence number. */ 464 void removeInstsUntil(const InstSeqNum &seq_num,unsigned tid); 465 466 /** Removes the instruction pointed to by the iterator. */ 467 inline void squashInstIt(const ListIt &instIt, const unsigned &tid); 468 469 /** Cleans up all instructions on the remove list. */ 470 void cleanUpRemovedInsts(); 471 472 /** Debug function to print all instructions on the list. */ 473 void dumpInsts(); 474 475 public: 476 /** List of all the instructions in flight. */ 477 std::list<DynInstPtr> instList; 478 479 /** List of all the instructions that will be removed at the end of this 480 * cycle. 481 */ 482 std::queue<ListIt> removeList; 483 484#ifdef DEBUG 485 /** Debug structure to keep track of the sequence numbers still in 486 * flight. 487 */ 488 std::set<InstSeqNum> snList; 489#endif 490 491 /** Records if instructions need to be removed this cycle due to 492 * being retired or squashed. 493 */ 494 bool removeInstsThisCycle; 495 496 protected: 497 /** The fetch stage. */ 498 typename CPUPolicy::Fetch fetch; 499 500 /** The decode stage. */ 501 typename CPUPolicy::Decode decode; 502 503 /** The dispatch stage. */ 504 typename CPUPolicy::Rename rename; 505 506 /** The issue/execute/writeback stages. */ 507 typename CPUPolicy::IEW iew; 508 509 /** The commit stage. */ 510 typename CPUPolicy::Commit commit; 511 512 /** The register file. */ 513 typename CPUPolicy::RegFile regFile; 514 515 /** The free list. */ 516 typename CPUPolicy::FreeList freeList; 517 518 /** The rename map. */ 519 typename CPUPolicy::RenameMap renameMap[Impl::MaxThreads]; 520 521 /** The commit rename map. */ 522 typename CPUPolicy::RenameMap commitRenameMap[Impl::MaxThreads]; 523 524 /** The re-order buffer. */ 525 typename CPUPolicy::ROB rob; 526 527 /** Active Threads List */ 528 std::list<unsigned> activeThreads; 529 530 /** Integer Register Scoreboard */ 531 Scoreboard scoreboard; 532 533 public: 534 /** Enum to give each stage a specific index, so when calling 535 * activateStage() or deactivateStage(), they can specify which stage 536 * is being activated/deactivated. 537 */ 538 enum StageIdx { 539 FetchIdx, 540 DecodeIdx, 541 RenameIdx, 542 IEWIdx, 543 CommitIdx, 544 NumStages }; 545 546 /** Typedefs from the Impl to get the structs that each of the 547 * time buffers should use. 548 */ 549 typedef typename CPUPolicy::TimeStruct TimeStruct; 550 551 typedef typename CPUPolicy::FetchStruct FetchStruct; 552 553 typedef typename CPUPolicy::DecodeStruct DecodeStruct; 554 555 typedef typename CPUPolicy::RenameStruct RenameStruct; 556 557 typedef typename CPUPolicy::IEWStruct IEWStruct; 558 559 /** The main time buffer to do backwards communication. */ 560 TimeBuffer<TimeStruct> timeBuffer; 561 562 /** The fetch stage's instruction queue. */ 563 TimeBuffer<FetchStruct> fetchQueue; 564 565 /** The decode stage's instruction queue. */ 566 TimeBuffer<DecodeStruct> decodeQueue; 567 568 /** The rename stage's instruction queue. */ 569 TimeBuffer<RenameStruct> renameQueue; 570 571 /** The IEW stage's instruction queue. */ 572 TimeBuffer<IEWStruct> iewQueue; 573 574 private: 575 /** The activity recorder; used to tell if the CPU has any 576 * activity remaining or if it can go to idle and deschedule 577 * itself. 578 */ 579 ActivityRecorder activityRec; 580 581 public: 582 /** Records that there was time buffer activity this cycle. */ 583 void activityThisCycle() { activityRec.activity(); } 584 585 /** Changes a stage's status to active within the activity recorder. */ 586 void activateStage(const StageIdx idx) 587 { activityRec.activateStage(idx); } 588 589 /** Changes a stage's status to inactive within the activity recorder. */ 590 void deactivateStage(const StageIdx idx) 591 { activityRec.deactivateStage(idx); } 592 593 /** Wakes the CPU, rescheduling the CPU if it's not already active. */ 594 void wakeCPU(); 595 596 /** Gets a free thread id. Use if thread ids change across system. */ 597 int getFreeTid(); 598 599 public: 600 /** Returns a pointer to a thread context. */ 601 ThreadContext *tcBase(unsigned tid) 602 { 603 return thread[tid]->getTC(); 604 } 605 606 /** The global sequence number counter. */ 607 InstSeqNum globalSeqNum;//[Impl::MaxThreads]; 608 609 /** Pointer to the checker, which can dynamically verify 610 * instruction results at run time. This can be set to NULL if it 611 * is not being used. 612 */ 613 Checker<DynInstPtr> *checker; 614 615#if FULL_SYSTEM 616 /** Pointer to the system. */ 617 System *system; 618 619 /** Pointer to physical memory. */ 620 PhysicalMemory *physmem; 621#endif 622 623 /** Pointer to memory. */ 624 MemObject *mem; 625 626 /** Event to call process() on once draining has completed. */ 627 Event *drainEvent; 628 629 /** Counter of how many stages have completed draining. */ 630 int drainCount; 631 632 /** Pointers to all of the threads in the CPU. */ 633 std::vector<Thread *> thread; 634 635 /** Whether or not the CPU should defer its registration. */ 636 bool deferRegistration; 637 638 /** Is there a context switch pending? */ 639 bool contextSwitch; 640 641 /** Threads Scheduled to Enter CPU */ 642 std::list<int> cpuWaitList; 643 644 /** The cycle that the CPU was last running, used for statistics. */ 645 Tick lastRunningCycle; 646 647 /** The cycle that the CPU was last activated by a new thread*/ 648 Tick lastActivatedCycle; 649 650 /** Number of Threads CPU can process */ 651 unsigned numThreads; 652 653 /** Mapping for system thread id to cpu id */ 654 std::map<unsigned,unsigned> threadMap; 655 656 /** Available thread ids in the cpu*/ 657 std::vector<unsigned> tids; 658 659 /** Stat for total number of times the CPU is descheduled. */ 660 Stats::Scalar<> timesIdled; 661 /** Stat for total number of cycles the CPU spends descheduled. */ 662 Stats::Scalar<> idleCycles; 663 /** Stat for the number of committed instructions per thread. */ 664 Stats::Vector<> committedInsts; 665 /** Stat for the total number of committed instructions. */ 666 Stats::Scalar<> totalCommittedInsts; 667 /** Stat for the CPI per thread. */ 668 Stats::Formula cpi; 669 /** Stat for the total CPI. */ 670 Stats::Formula totalCpi; 671 /** Stat for the IPC per thread. */ 672 Stats::Formula ipc; 673 /** Stat for the total IPC. */ 674 Stats::Formula totalIpc; 675}; 676 677#endif // __CPU_O3_CPU_HH__ 678