a6abb28b21ad1b875e561a09a10005081ec823da
[gem5.git] / src / cpu / inorder / inorder_dyn_inst.cc
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
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: Korey Sewell
29 *
30 */
31
32 #include <iostream>
33 #include <set>
34 #include <string>
35 #include <sstream>
36
37 #include "base/cprintf.hh"
38 #include "base/trace.hh"
39
40 #include "arch/faults.hh"
41 #include "cpu/exetrace.hh"
42 #include "mem/request.hh"
43
44 #include "cpu/inorder/inorder_dyn_inst.hh"
45 #include "cpu/inorder/cpu.hh"
46
47 using namespace std;
48 using namespace TheISA;
49 using namespace ThePipeline;
50
51 InOrderDynInst::InOrderDynInst(TheISA::ExtMachInst machInst, Addr inst_PC,
52 Addr pred_PC, InstSeqNum seq_num,
53 InOrderCPU *cpu)
54 : staticInst(machInst, inst_PC), traceData(NULL), cpu(cpu)
55 {
56 seqNum = seq_num;
57
58 PC = inst_PC;
59 nextPC = PC + sizeof(MachInst);
60 nextNPC = nextPC + sizeof(MachInst);
61 predPC = pred_PC;
62
63 initVars();
64 }
65
66 InOrderDynInst::InOrderDynInst(InOrderCPU *cpu,
67 InOrderThreadState *state,
68 InstSeqNum seq_num,
69 ThreadID tid,
70 unsigned _asid)
71 : traceData(NULL), cpu(cpu)
72 {
73 seqNum = seq_num;
74 thread = state;
75 threadNumber = tid;
76 asid = _asid;
77 initVars();
78 }
79
80 InOrderDynInst::InOrderDynInst(StaticInstPtr &_staticInst)
81 : seqNum(0), staticInst(_staticInst), traceData(NULL)
82 {
83 initVars();
84 }
85
86 InOrderDynInst::InOrderDynInst()
87 : seqNum(0), traceData(NULL), cpu(cpu)
88 {
89 initVars();
90 }
91
92 int InOrderDynInst::instcount = 0;
93
94
95 void
96 InOrderDynInst::setMachInst(ExtMachInst machInst)
97 {
98 staticInst = StaticInst::decode(machInst, PC);
99
100 for (int i = 0; i < this->staticInst->numDestRegs(); i++) {
101 _destRegIdx[i] = this->staticInst->destRegIdx(i);
102 }
103
104 for (int i = 0; i < this->staticInst->numSrcRegs(); i++) {
105 _srcRegIdx[i] = this->staticInst->srcRegIdx(i);
106 this->_readySrcRegIdx[i] = 0;
107 }
108 }
109
110 void
111 InOrderDynInst::initVars()
112 {
113 fetchMemReq = NULL;
114 dataMemReq = NULL;
115
116 effAddr = 0;
117 physEffAddr = 0;
118
119 readyRegs = 0;
120
121 nextStage = 0;
122 nextInstStageNum = 0;
123
124 for(int i = 0; i < MaxInstDestRegs; i++)
125 instResult[i].val.integer = 0;
126
127 status.reset();
128
129 memAddrReady = false;
130 eaCalcDone = false;
131 memOpDone = false;
132
133 predictTaken = false;
134 procDelaySlotOnMispred = false;
135
136 lqIdx = -1;
137 sqIdx = -1;
138
139 // Also make this a parameter, or perhaps get it from xc or cpu.
140 asid = 0;
141
142 virtProcNumber = 0;
143
144 // Initialize the fault to be NoFault.
145 fault = NoFault;
146
147 // Make sure to have the renamed register entries set to the same
148 // as the normal register entries. It will allow the IQ to work
149 // without any modifications.
150 if (this->staticInst) {
151 for (int i = 0; i < this->staticInst->numDestRegs(); i++) {
152 _destRegIdx[i] = this->staticInst->destRegIdx(i);
153 }
154
155 for (int i = 0; i < this->staticInst->numSrcRegs(); i++) {
156 _srcRegIdx[i] = this->staticInst->srcRegIdx(i);
157 this->_readySrcRegIdx[i] = 0;
158 }
159 }
160
161 // Update Instruction Count for this instruction
162 ++instcount;
163 if (instcount > 500) {
164 fatal("Number of Active Instructions in CPU is too high. "
165 "(Not Dereferencing Ptrs. Correctly?)\n");
166 }
167
168
169
170 DPRINTF(InOrderDynInst, "DynInst: [tid:%i] [sn:%lli] Instruction created. (active insts: %i)\n",
171 threadNumber, seqNum, instcount);
172 }
173
174
175 InOrderDynInst::~InOrderDynInst()
176 {
177 if (fetchMemReq != 0x0) {
178 delete fetchMemReq;
179 fetchMemReq = NULL;
180 }
181
182 if (dataMemReq != 0x0) {
183 delete dataMemReq;
184 dataMemReq = NULL;
185 }
186
187 if (traceData) {
188 delete traceData;
189 }
190
191 fault = NoFault;
192
193 --instcount;
194
195 deleteStages();
196
197 DPRINTF(InOrderDynInst, "DynInst: [tid:%i] [sn:%lli] Instruction destroyed. (active insts: %i)\n",
198 threadNumber, seqNum, instcount);
199 }
200
201 void
202 InOrderDynInst::setStaticInst(StaticInstPtr &static_inst)
203 {
204 this->staticInst = static_inst;
205
206 // Make sure to have the renamed register entries set to the same
207 // as the normal register entries. It will allow the IQ to work
208 // without any modifications.
209 if (this->staticInst) {
210 for (int i = 0; i < this->staticInst->numDestRegs(); i++) {
211 _destRegIdx[i] = this->staticInst->destRegIdx(i);
212 }
213
214 for (int i = 0; i < this->staticInst->numSrcRegs(); i++) {
215 _srcRegIdx[i] = this->staticInst->srcRegIdx(i);
216 this->_readySrcRegIdx[i] = 0;
217 }
218 }
219 }
220
221 Fault
222 InOrderDynInst::execute()
223 {
224 // @todo: Pretty convoluted way to avoid squashing from happening
225 // when using the TC during an instruction's execution
226 // (specifically for instructions that have side-effects that use
227 // the TC). Fix this.
228 bool in_syscall = this->thread->inSyscall;
229 this->thread->inSyscall = true;
230
231 this->fault = this->staticInst->execute(this, this->traceData);
232
233 this->thread->inSyscall = in_syscall;
234
235 return this->fault;
236 }
237
238 Fault
239 InOrderDynInst::calcEA()
240 {
241 this->fault = this->staticInst->eaComp(this, this->traceData);
242 return this->fault;
243 }
244
245 Fault
246 InOrderDynInst::initiateAcc()
247 {
248 // @todo: Pretty convoluted way to avoid squashing from happening
249 // when using the TC during an instruction's execution
250 // (specifically for instructions that have side-effects that use
251 // the TC). Fix this.
252 bool in_syscall = this->thread->inSyscall;
253 this->thread->inSyscall = true;
254
255 this->fault = this->staticInst->initiateAcc(this, this->traceData);
256
257 this->thread->inSyscall = in_syscall;
258
259 return this->fault;
260 }
261
262
263 Fault
264 InOrderDynInst::completeAcc(Packet *pkt)
265 {
266 this->fault = this->staticInst->completeAcc(pkt, this, this->traceData);
267
268 return this->fault;
269 }
270
271 InstStage *InOrderDynInst::addStage()
272 {
273 this->currentInstStage = new InstStage(this, nextInstStageNum++);
274 instStageList.push_back( this->currentInstStage );
275 return this->currentInstStage;
276 }
277
278 InstStage *InOrderDynInst::addStage(int stage_num)
279 {
280 nextInstStageNum = stage_num;
281 return InOrderDynInst::addStage();
282 }
283
284 void InOrderDynInst::deleteStages() {
285 std::list<InstStage*>::iterator list_it = instStageList.begin();
286 std::list<InstStage*>::iterator list_end = instStageList.end();
287
288 while(list_it != list_end) {
289 delete *list_it;
290 list_it++;
291 }
292 }
293
294 Fault
295 InOrderDynInst::memAccess()
296 {
297 return initiateAcc();
298 }
299
300 void
301 InOrderDynInst::syscall(int64_t callnum)
302 {
303 cpu->syscall(callnum, this->threadNumber);
304 }
305
306 void
307 InOrderDynInst::prefetch(Addr addr, unsigned flags)
308 {
309 cpu->prefetch(this);
310 }
311
312 void
313 InOrderDynInst::writeHint(Addr addr, int size, unsigned flags)
314 {
315 cpu->writeHint(this);
316 }
317
318 /**
319 * @todo Need to find a way to get the cache block size here.
320 */
321 Fault
322 InOrderDynInst::copySrcTranslate(Addr src)
323 {
324 // Not currently supported.
325 return NoFault;
326 }
327
328 /**
329 * @todo Need to find a way to get the cache block size here.
330 */
331 Fault
332 InOrderDynInst::copy(Addr dest)
333 {
334 // Not currently supported.
335 return NoFault;
336 }
337
338 void
339 InOrderDynInst::releaseReq(ResourceRequest* req)
340 {
341 std::list<ResourceRequest*>::iterator list_it = reqList.begin();
342 std::list<ResourceRequest*>::iterator list_end = reqList.end();
343
344 while(list_it != list_end) {
345 if((*list_it)->getResIdx() == req->getResIdx() &&
346 (*list_it)->getSlot() == req->getSlot()) {
347 DPRINTF(InOrderDynInst, "[tid:%u]: [sn:%i] Done with request to %s.\n",
348 threadNumber, seqNum, req->res->name());
349 reqList.erase(list_it);
350 return;
351 }
352 list_it++;
353 }
354
355 panic("Releasing Res. Request That Isnt There!\n");
356 }
357
358 /** Records an integer source register being set to a value. */
359 void
360 InOrderDynInst::setIntSrc(int idx, uint64_t val)
361 {
362 DPRINTF(InOrderDynInst, "[tid:%i]: [sn:%i] Source Value %i being set to %#x.\n",
363 threadNumber, seqNum, idx, val);
364 instSrc[idx].integer = val;
365 }
366
367 /** Records an fp register being set to a value. */
368 void
369 InOrderDynInst::setFloatSrc(int idx, FloatReg val)
370 {
371 instSrc[idx].dbl = val;
372 }
373
374 /** Records an fp register being set to an integer value. */
375 void
376 InOrderDynInst::setFloatRegBitsSrc(int idx, uint64_t val)
377 {
378 instSrc[idx].integer = val;
379 }
380
381 /** Reads a integer register. */
382 IntReg
383 InOrderDynInst::readIntRegOperand(const StaticInst *si, int idx, ThreadID tid)
384 {
385 DPRINTF(InOrderDynInst, "[tid:%i]: [sn:%i] Source Value %i read as %#x.\n",
386 threadNumber, seqNum, idx, instSrc[idx].integer);
387 return instSrc[idx].integer;
388 }
389
390 /** Reads a FP register. */
391 FloatReg
392 InOrderDynInst::readFloatRegOperand(const StaticInst *si, int idx)
393 {
394 return instSrc[idx].dbl;
395 }
396
397
398 /** Reads a FP register as a integer. */
399 FloatRegBits
400 InOrderDynInst::readFloatRegOperandBits(const StaticInst *si, int idx)
401 {
402 return instSrc[idx].integer;
403 }
404
405 /** Reads a miscellaneous register. */
406 MiscReg
407 InOrderDynInst::readMiscReg(int misc_reg)
408 {
409 return this->cpu->readMiscReg(misc_reg, threadNumber);
410 }
411
412 /** Reads a misc. register, including any side-effects the read
413 * might have as defined by the architecture.
414 */
415 MiscReg
416 InOrderDynInst::readMiscRegNoEffect(int misc_reg)
417 {
418 return this->cpu->readMiscRegNoEffect(misc_reg, threadNumber);
419 }
420
421 /** Reads a miscellaneous register. */
422 MiscReg
423 InOrderDynInst::readMiscRegOperandNoEffect(const StaticInst *si, int idx)
424 {
425 DPRINTF(InOrderDynInst, "[tid:%i]: [sn:%i] Misc. Reg Source Value %i"
426 " read as %#x.\n", threadNumber, seqNum, idx,
427 instSrc[idx].integer);
428 return instSrc[idx].integer;
429 }
430
431 /** Reads a misc. register, including any side-effects the read
432 * might have as defined by the architecture.
433 */
434 MiscReg
435 InOrderDynInst::readMiscRegOperand(const StaticInst *si, int idx)
436 {
437 // For In-Order, the side-effect of reading a register happens
438 // when explicitly executing a "ReadSrc" command. This simply returns
439 // a value.
440 return readMiscRegOperandNoEffect(si, idx);
441 }
442
443 /** Sets a misc. register. */
444 void
445 InOrderDynInst::setMiscRegOperandNoEffect(const StaticInst * si, int idx,
446 const MiscReg &val)
447 {
448 instResult[idx].type = Integer;
449 instResult[idx].val.integer = val;
450 instResult[idx].tick = curTick;
451
452 DPRINTF(InOrderDynInst, "[tid:%i]: [sn:%i] Setting Misc Reg. Operand %i "
453 "being set to %#x.\n", threadNumber, seqNum, idx, val);
454 }
455
456 /** Sets a misc. register, including any side-effects the write
457 * might have as defined by the architecture.
458 */
459 void
460 InOrderDynInst::setMiscRegOperand(const StaticInst *si, int idx,
461 const MiscReg &val)
462 {
463 // For In-Order, the side-effect of setting a register happens
464 // when explicitly writing back the register value. This
465 // simply maintains the operand value.
466 setMiscRegOperandNoEffect(si, idx, val);
467 }
468
469 MiscReg
470 InOrderDynInst::readRegOtherThread(unsigned reg_idx, ThreadID tid)
471 {
472 if (tid == -1) {
473 tid = TheISA::getTargetThread(this->cpu->tcBase(threadNumber));
474 }
475
476 if (reg_idx < FP_Base_DepTag) { // Integer Register File
477 return this->cpu->readIntReg(reg_idx, tid);
478 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
479 reg_idx -= FP_Base_DepTag;
480 return this->cpu->readFloatRegBits(reg_idx, tid);
481 } else {
482 reg_idx -= Ctrl_Base_DepTag;
483 return this->cpu->readMiscReg(reg_idx, tid); // Misc. Register File
484 }
485 }
486
487 /** Sets a Integer register. */
488 void
489 InOrderDynInst::setIntRegOperand(const StaticInst *si, int idx, IntReg val)
490 {
491 instResult[idx].type = Integer;
492 instResult[idx].val.integer = val;
493 instResult[idx].tick = curTick;
494 }
495
496 /** Sets a FP register. */
497 void
498 InOrderDynInst::setFloatRegOperand(const StaticInst *si, int idx, FloatReg val)
499 {
500 instResult[idx].val.dbl = val;
501 instResult[idx].type = Float;
502
503 instResult[idx].tick = curTick;
504 }
505
506 /** Sets a FP register as a integer. */
507 void
508 InOrderDynInst::setFloatRegOperandBits(const StaticInst *si, int idx,
509 FloatRegBits val)
510 {
511 instResult[idx].type = Integer;
512 instResult[idx].val.integer = val;
513 instResult[idx].tick = curTick;
514 }
515
516 /** Sets a misc. register. */
517 /* Alter this when wanting to *speculate* on Miscellaneous registers */
518 void
519 InOrderDynInst::setMiscRegNoEffect(int misc_reg, const MiscReg &val)
520 {
521 this->cpu->setMiscRegNoEffect(misc_reg, val, threadNumber);
522 }
523
524 /** Sets a misc. register, including any side-effects the write
525 * might have as defined by the architecture.
526 */
527 /* Alter this if/when wanting to *speculate* on Miscellaneous registers */
528 void
529 InOrderDynInst::setMiscReg(int misc_reg, const MiscReg &val)
530 {
531 this->cpu->setMiscReg(misc_reg, val, threadNumber);
532 }
533
534 void
535 InOrderDynInst::setRegOtherThread(unsigned reg_idx, const MiscReg &val,
536 ThreadID tid)
537 {
538 if (tid == InvalidThreadID) {
539 tid = TheISA::getTargetThread(this->cpu->tcBase(threadNumber));
540 }
541
542 if (reg_idx < FP_Base_DepTag) { // Integer Register File
543 this->cpu->setIntReg(reg_idx, val, tid);
544 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
545 reg_idx -= FP_Base_DepTag;
546 this->cpu->setFloatRegBits(reg_idx, val, tid);
547 } else {
548 reg_idx -= Ctrl_Base_DepTag;
549 this->cpu->setMiscReg(reg_idx, val, tid); // Misc. Register File
550 }
551 }
552
553 void
554 InOrderDynInst::deallocateContext(int thread_num)
555 {
556 this->cpu->deallocateContext(thread_num);
557 }
558
559 void
560 InOrderDynInst::enableVirtProcElement(unsigned vpe)
561 {
562 this->cpu->enableVirtProcElement(vpe);
563 }
564
565 void
566 InOrderDynInst::disableVirtProcElement(unsigned vpe)
567 {
568 this->cpu->disableVirtProcElement(threadNumber, vpe);
569 }
570
571 void
572 InOrderDynInst::enableMultiThreading(unsigned vpe)
573 {
574 this->cpu->enableMultiThreading(vpe);
575 }
576
577 void
578 InOrderDynInst::disableMultiThreading(unsigned vpe)
579 {
580 this->cpu->disableMultiThreading(threadNumber, vpe);
581 }
582
583 template<class T>
584 inline Fault
585 InOrderDynInst::read(Addr addr, T &data, unsigned flags)
586 {
587 return cpu->read(this, addr, data, flags);
588 }
589
590 #ifndef DOXYGEN_SHOULD_SKIP_THIS
591
592 template
593 Fault
594 InOrderDynInst::read(Addr addr, uint64_t &data, unsigned flags);
595
596 template
597 Fault
598 InOrderDynInst::read(Addr addr, uint32_t &data, unsigned flags);
599
600 template
601 Fault
602 InOrderDynInst::read(Addr addr, uint16_t &data, unsigned flags);
603
604 template
605 Fault
606 InOrderDynInst::read(Addr addr, uint8_t &data, unsigned flags);
607
608 #endif //DOXYGEN_SHOULD_SKIP_THIS
609
610 template<>
611 Fault
612 InOrderDynInst::read(Addr addr, double &data, unsigned flags)
613 {
614 return read(addr, *(uint64_t*)&data, flags);
615 }
616
617 template<>
618 Fault
619 InOrderDynInst::read(Addr addr, float &data, unsigned flags)
620 {
621 return read(addr, *(uint32_t*)&data, flags);
622 }
623
624 template<>
625 Fault
626 InOrderDynInst::read(Addr addr, int32_t &data, unsigned flags)
627 {
628 return read(addr, (uint32_t&)data, flags);
629 }
630
631 template<class T>
632 inline Fault
633 InOrderDynInst::write(T data, Addr addr, unsigned flags, uint64_t *res)
634 {
635 //memcpy(memData, gtoh(data), sizeof(T));
636 storeData = data;
637
638 DPRINTF(InOrderDynInst, "[tid:%i]: [sn:%i] Setting store data to %#x.\n",
639 threadNumber, seqNum, memData);
640 return cpu->write(this, data, addr, flags, res);
641 }
642
643 #ifndef DOXYGEN_SHOULD_SKIP_THIS
644 template
645 Fault
646 InOrderDynInst::write(uint64_t data, Addr addr,
647 unsigned flags, uint64_t *res);
648
649 template
650 Fault
651 InOrderDynInst::write(uint32_t data, Addr addr,
652 unsigned flags, uint64_t *res);
653
654 template
655 Fault
656 InOrderDynInst::write(uint16_t data, Addr addr,
657 unsigned flags, uint64_t *res);
658
659 template
660 Fault
661 InOrderDynInst::write(uint8_t data, Addr addr,
662 unsigned flags, uint64_t *res);
663
664 #endif //DOXYGEN_SHOULD_SKIP_THIS
665
666 template<>
667 Fault
668 InOrderDynInst::write(double data, Addr addr, unsigned flags, uint64_t *res)
669 {
670 return write(*(uint64_t*)&data, addr, flags, res);
671 }
672
673 template<>
674 Fault
675 InOrderDynInst::write(float data, Addr addr, unsigned flags, uint64_t *res)
676 {
677 return write(*(uint32_t*)&data, addr, flags, res);
678 }
679
680
681 template<>
682 Fault
683 InOrderDynInst::write(int32_t data, Addr addr, unsigned flags, uint64_t *res)
684 {
685 return write((uint32_t)data, addr, flags, res);
686 }
687
688
689 void
690 InOrderDynInst::dump()
691 {
692 cprintf("T%d : %#08d `", threadNumber, PC);
693 cout << staticInst->disassemble(PC);
694 cprintf("'\n");
695 }
696
697 void
698 InOrderDynInst::dump(std::string &outstring)
699 {
700 std::ostringstream s;
701 s << "T" << threadNumber << " : 0x" << PC << " "
702 << staticInst->disassemble(PC);
703
704 outstring = s.str();
705 }
706
707
708 #define NOHASH
709 #ifndef NOHASH
710
711 #include "base/hashmap.hh"
712
713 unsigned int MyHashFunc(const InOrderDynInst *addr)
714 {
715 unsigned a = (unsigned)addr;
716 unsigned hash = (((a >> 14) ^ ((a >> 2) & 0xffff))) & 0x7FFFFFFF;
717
718 return hash;
719 }
720
721 typedef m5::hash_map<const InOrderDynInst *, const InOrderDynInst *, MyHashFunc>
722 my_hash_t;
723
724 my_hash_t thishash;
725 #endif