2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
34 #include "config/the_isa.hh"
35 #if THE_ISA != SPARC_ISA
36 #error Legion tracing only works with SPARC simulations!
39 #include "config/full_system.hh"
41 #error Legion tracing only works in full system!
50 #include "arch/sparc/predecoder.hh"
51 #include "arch/sparc/registers.hh"
52 #include "arch/sparc/utility.hh"
53 #include "base/socket.hh"
54 #include "cpu/base.hh"
55 #include "cpu/decode.hh"
56 #include "cpu/legiontrace.hh"
57 #include "cpu/static_inst.hh"
58 #include "cpu/thread_context.hh"
59 #include "sim/system.hh"
62 #include "arch/tlb.hh"
65 //XXX This is temporary
66 #include "cpu/m5legion_interface.h"
69 using namespace TheISA
;
72 static int diffcount
= 0;
73 static bool wasMicro
= false;
77 SharedData
*shared_data
= NULL
;
82 int shmfd
= shmget('M' << 24 | getuid(), sizeof(SharedData
), 0777);
84 fatal("Couldn't get shared memory fd. Is Legion running?");
86 shared_data
= (SharedData
*)shmat(shmfd
, NULL
, SHM_RND
);
87 if (shared_data
== (SharedData
*)-1)
88 fatal("Couldn't allocate shared memory");
90 if (shared_data
->flags
!= OWN_M5
)
91 fatal("Shared memory has invalid owner");
93 if (shared_data
->version
!= VERSION
)
94 fatal("Shared Data is wrong version! M5: %d Legion: %d", VERSION
,
95 shared_data
->version
);
97 // step legion forward one cycle so we can get register values
98 shared_data
->flags
= OWN_LEGION
;
101 ////////////////////////////////////////////////////////////////////////
103 // Utility methods for pretty printing a report about a difference
106 inline char * genCenteredLabel(int length
, char * buffer
, const char * label
)
108 int labelLength
= strlen(label
);
109 assert(labelLength
<= length
);
110 int leftPad
= (length
- labelLength
) / 2;
111 int rightPad
= length
- leftPad
- labelLength
;
113 sprintf(format
, "%%%ds%%s%%%ds", leftPad
, rightPad
);
114 sprintf(buffer
, format
, "", label
, "");
118 inline void printRegPair(ostream
& os
, char const * title
, uint64_t a
, uint64_t b
)
120 ccprintf(os
, " %16s | %#018x %s %#-018x \n",
121 title
, a
, (a
== b
) ? "|" : "X", b
);
124 inline void printColumnLabels(ostream
& os
)
126 static char * regLabel
= genCenteredLabel(16, new char[17], "Register");
127 static char * m5Label
= genCenteredLabel(18, new char[18], "M5");
128 static char * legionLabel
= genCenteredLabel(18, new char[18], "Legion");
129 ccprintf(os
, " %s | %s | %s \n", regLabel
, m5Label
, legionLabel
);
130 ccprintf(os
, "--------------------+-----------------------+-----------------------\n");
133 inline void printSectionHeader(ostream
& os
, const char * name
)
135 char sectionString
[70];
136 genCenteredLabel(69, sectionString
, name
);
137 ccprintf(os
, "====================================================================\n");
138 ccprintf(os
, "%69s\n", sectionString
);
139 ccprintf(os
, "====================================================================\n");
142 inline void printLevelHeader(ostream
& os
, int level
)
144 char sectionString
[70];
146 sprintf(levelName
, "Trap stack level %d", level
);
147 genCenteredLabel(69, sectionString
, levelName
);
148 ccprintf(os
, "====================================================================\n");
149 ccprintf(os
, "%69s\n", sectionString
);
150 ccprintf(os
, "====================================================================\n");
154 Trace::LegionTraceRecord::dump()
156 ostream
&outs
= Trace::output();
158 static TheISA::Predecoder
predecoder(NULL
);
160 bool compared
= false;
163 bool diffInst
= false;
164 bool diffIntRegs
= false;
165 bool diffFpRegs
= false;
166 bool diffTpc
= false;
167 bool diffTnpc
= false;
168 bool diffTstate
= false;
170 bool diffTba
= false;
171 bool diffHpstate
= false;
172 bool diffHtstate
= false;
173 bool diffHtba
= false;
174 bool diffPstate
= false;
176 bool diffFsr
= false;
177 bool diffCcr
= false;
180 bool diffAsi
= false;
181 bool diffPil
= false;
182 bool diffCwp
= false;
183 bool diffCansave
= false;
184 bool diffCanrestore
= false;
185 bool diffOtherwin
= false;
186 bool diffCleanwin
= false;
187 bool diffTlb
= false;
193 // We took a trap on a micro-op...
194 if (wasMicro
&& !staticInst
->isMicroop())
196 // let's skip comparing this tick
198 if (shared_data
->flags
== OWN_M5
) {
199 shared_data
->flags
= OWN_LEGION
;
206 if (staticInst
->isLastMicroop())
208 else if (staticInst
->isMicroop())
212 if(!staticInst
->isMicroop() || staticInst
->isLastMicroop()) {
214 if (shared_data
->flags
== OWN_M5
) {
215 m5Pc
= pc
.instAddr() & SparcISA::PAddrImplMask
;
216 if (bits(shared_data
->pstate
,3,3)) {
219 lgnPc
= shared_data
->pc
& SparcISA::PAddrImplMask
;
223 if (shared_data
->cycle_count
!=
224 thread
->getCpuPtr()->instCount())
227 if (shared_data
->instruction
!=
228 (SparcISA::MachInst
)staticInst
->machInst
) {
231 // assume we have %g0 working correctly
232 for (int i
= 1; i
< TheISA::NumIntArchRegs
; i
++) {
233 if (thread
->readIntReg(i
) != shared_data
->intregs
[i
]) {
237 for (int i
= 0; i
< TheISA::NumFloatRegs
/2; i
++) {
238 if (thread
->readFloatRegBits(i
*2) !=
239 shared_data
->fpregs
[i
]) {
244 thread
->readMiscRegNoEffect(MISCREG_TL
);
245 if (oldTl
!= shared_data
->tl
)
247 for (int i
= 1; i
<= MaxTL
; i
++) {
248 thread
->setMiscRegNoEffect(MISCREG_TL
, i
);
249 if (thread
->readMiscRegNoEffect(MISCREG_TPC
) !=
250 shared_data
->tpc
[i
-1])
252 if (thread
->readMiscRegNoEffect(MISCREG_TNPC
) !=
253 shared_data
->tnpc
[i
-1])
255 if (thread
->readMiscRegNoEffect(MISCREG_TSTATE
) !=
256 shared_data
->tstate
[i
-1])
258 if (thread
->readMiscRegNoEffect(MISCREG_TT
) !=
259 shared_data
->tt
[i
-1])
261 if (thread
->readMiscRegNoEffect(MISCREG_HTSTATE
) !=
262 shared_data
->htstate
[i
-1])
265 thread
->setMiscRegNoEffect(MISCREG_TL
, oldTl
);
267 if(shared_data
->tba
!= thread
->readMiscRegNoEffect(MISCREG_TBA
))
269 //When the hpstate register is read by an instruction,
270 //legion has bit 11 set. When it's in storage, it doesn't.
271 //Since we don't directly support seperate interpretations
272 //of the registers like that, the bit is always set to 1 and
273 //we just don't compare it. It's not supposed to matter
275 if((shared_data
->hpstate
| (1 << 11)) !=
276 thread
->readMiscRegNoEffect(MISCREG_HPSTATE
))
278 if(shared_data
->htba
!=
279 thread
->readMiscRegNoEffect(MISCREG_HTBA
))
281 if(shared_data
->pstate
!=
282 thread
->readMiscRegNoEffect(MISCREG_PSTATE
))
284 //if(shared_data->y !=
285 // thread->readMiscRegNoEffect(MISCREG_Y))
287 thread
->readIntReg(NumIntArchRegs
+ 1))
289 if(shared_data
->fsr
!=
290 thread
->readMiscRegNoEffect(MISCREG_FSR
)) {
292 if (mbits(shared_data
->fsr
, 63,10) ==
293 mbits(thread
->readMiscRegNoEffect(MISCREG_FSR
),
295 thread
->setMiscRegNoEffect(MISCREG_FSR
,
300 //if(shared_data->ccr !=
301 // thread->readMiscRegNoEffect(MISCREG_CCR))
302 if(shared_data
->ccr
!=
303 thread
->readIntReg(NumIntArchRegs
+ 2))
305 if(shared_data
->gl
!=
306 thread
->readMiscRegNoEffect(MISCREG_GL
))
308 if(shared_data
->asi
!=
309 thread
->readMiscRegNoEffect(MISCREG_ASI
))
311 if(shared_data
->pil
!=
312 thread
->readMiscRegNoEffect(MISCREG_PIL
))
314 if(shared_data
->cwp
!=
315 thread
->readMiscRegNoEffect(MISCREG_CWP
))
317 //if(shared_data->cansave !=
318 // thread->readMiscRegNoEffect(MISCREG_CANSAVE))
319 if(shared_data
->cansave
!=
320 thread
->readIntReg(NumIntArchRegs
+ 3))
322 //if(shared_data->canrestore !=
323 // thread->readMiscRegNoEffect(MISCREG_CANRESTORE))
324 if(shared_data
->canrestore
!=
325 thread
->readIntReg(NumIntArchRegs
+ 4))
326 diffCanrestore
= true;
327 //if(shared_data->otherwin !=
328 // thread->readMiscRegNoEffect(MISCREG_OTHERWIN))
329 if(shared_data
->otherwin
!=
330 thread
->readIntReg(NumIntArchRegs
+ 6))
332 //if(shared_data->cleanwin !=
333 // thread->readMiscRegNoEffect(MISCREG_CLEANWIN))
334 if(shared_data
->cleanwin
!=
335 thread
->readIntReg(NumIntArchRegs
+ 5))
338 for (int i
= 0; i
< 64; i
++) {
339 if (shared_data
->itb
[i
] !=
340 thread
->getITBPtr()->TteRead(i
))
342 if (shared_data
->dtb
[i
] !=
343 thread
->getDTBPtr()->TteRead(i
))
347 if (diffPC
|| diffCC
|| diffInst
||
348 diffIntRegs
|| diffFpRegs
||
349 diffTpc
|| diffTnpc
|| diffTstate
|| diffTt
||
350 diffHpstate
|| diffHtstate
|| diffHtba
||
351 diffPstate
|| diffY
|| diffCcr
|| diffTl
|| diffFsr
||
352 diffGl
|| diffAsi
|| diffPil
|| diffCwp
||
353 diffCansave
|| diffCanrestore
||
354 diffOtherwin
|| diffCleanwin
|| diffTlb
) {
356 outs
<< "Differences found between M5 and Legion:";
362 outs
<< " [Instruction]";
364 outs
<< " [IntRegs]";
376 outs
<< " [Hpstate]";
378 outs
<< " [Htstate]";
400 outs
<< " [Cansave]";
402 outs
<< " [Canrestore]";
404 outs
<< " [Otherwin]";
406 outs
<< " [Cleanwin]";
409 outs
<< endl
<< endl
;
411 outs
<< right
<< setfill(' ') << setw(15)
412 << "M5 PC: " << "0x"<< setw(16) << setfill('0')
413 << hex
<< m5Pc
<< endl
;
414 outs
<< setfill(' ') << setw(15)
415 << "Legion PC: " << "0x"
416 << setw(16) << setfill('0') << hex
417 << lgnPc
<< endl
<< endl
;
419 outs
<< right
<< setfill(' ') << setw(15)
421 << setw(16) << setfill('0') << hex
422 << thread
->getCpuPtr()->instCount() << endl
;
423 outs
<< setfill(' ') << setw(15)
424 << "Legion CC: " << "0x"
425 << setw(16) << setfill('0') << hex
426 << shared_data
->cycle_count
<< endl
<< endl
;
428 outs
<< setfill(' ') << setw(15)
429 << "M5 Inst: " << "0x"
430 << setw(8) << setfill('0') << hex
431 << staticInst
->machInst
432 << staticInst
->disassemble(m5Pc
, debugSymbolTable
)
435 predecoder
.setTC(thread
);
436 predecoder
.moreBytes(m5Pc
, m5Pc
, shared_data
->instruction
);
438 assert(predecoder
.extMachInstReady());
441 StaticInstPtr legionInst
=
442 thread
->getDecoderPtr()->decode(
443 predecoder
.getExtMachInst(tempPC
), lgnPc
);
444 outs
<< setfill(' ') << setw(15)
446 << "0x" << setw(8) << setfill('0') << hex
447 << shared_data
->instruction
448 << legionInst
->disassemble(lgnPc
, debugSymbolTable
)
451 printSectionHeader(outs
, "General State");
452 printColumnLabels(outs
);
453 printRegPair(outs
, "HPstate",
454 thread
->readMiscRegNoEffect(MISCREG_HPSTATE
),
455 shared_data
->hpstate
| (1 << 11));
456 printRegPair(outs
, "Htba",
457 thread
->readMiscRegNoEffect(MISCREG_HTBA
),
459 printRegPair(outs
, "Pstate",
460 thread
->readMiscRegNoEffect(MISCREG_PSTATE
),
461 shared_data
->pstate
);
462 printRegPair(outs
, "Y",
463 //thread->readMiscRegNoEffect(MISCREG_Y),
464 thread
->readIntReg(NumIntArchRegs
+ 1),
466 printRegPair(outs
, "FSR",
467 thread
->readMiscRegNoEffect(MISCREG_FSR
),
469 printRegPair(outs
, "Ccr",
470 //thread->readMiscRegNoEffect(MISCREG_CCR),
471 thread
->readIntReg(NumIntArchRegs
+ 2),
473 printRegPair(outs
, "Tl",
474 thread
->readMiscRegNoEffect(MISCREG_TL
),
476 printRegPair(outs
, "Gl",
477 thread
->readMiscRegNoEffect(MISCREG_GL
),
479 printRegPair(outs
, "Asi",
480 thread
->readMiscRegNoEffect(MISCREG_ASI
),
482 printRegPair(outs
, "Pil",
483 thread
->readMiscRegNoEffect(MISCREG_PIL
),
485 printRegPair(outs
, "Cwp",
486 thread
->readMiscRegNoEffect(MISCREG_CWP
),
488 printRegPair(outs
, "Cansave",
489 //thread->readMiscRegNoEffect(MISCREG_CANSAVE),
490 thread
->readIntReg(NumIntArchRegs
+ 3),
491 shared_data
->cansave
);
492 printRegPair(outs
, "Canrestore",
493 //thread->readMiscRegNoEffect(MISCREG_CANRESTORE),
494 thread
->readIntReg(NumIntArchRegs
+ 4),
495 shared_data
->canrestore
);
496 printRegPair(outs
, "Otherwin",
497 //thread->readMiscRegNoEffect(MISCREG_OTHERWIN),
498 thread
->readIntReg(NumIntArchRegs
+ 6),
499 shared_data
->otherwin
);
500 printRegPair(outs
, "Cleanwin",
501 //thread->readMiscRegNoEffect(MISCREG_CLEANWIN),
502 thread
->readIntReg(NumIntArchRegs
+ 5),
503 shared_data
->cleanwin
);
505 for (int i
= 1; i
<= MaxTL
; i
++) {
506 printLevelHeader(outs
, i
);
507 printColumnLabels(outs
);
508 thread
->setMiscRegNoEffect(MISCREG_TL
, i
);
509 printRegPair(outs
, "Tpc",
510 thread
->readMiscRegNoEffect(MISCREG_TPC
),
511 shared_data
->tpc
[i
-1]);
512 printRegPair(outs
, "Tnpc",
513 thread
->readMiscRegNoEffect(MISCREG_TNPC
),
514 shared_data
->tnpc
[i
-1]);
515 printRegPair(outs
, "Tstate",
516 thread
->readMiscRegNoEffect(MISCREG_TSTATE
),
517 shared_data
->tstate
[i
-1]);
518 printRegPair(outs
, "Tt",
519 thread
->readMiscRegNoEffect(MISCREG_TT
),
520 shared_data
->tt
[i
-1]);
521 printRegPair(outs
, "Htstate",
522 thread
->readMiscRegNoEffect(MISCREG_HTSTATE
),
523 shared_data
->htstate
[i
-1]);
525 thread
->setMiscRegNoEffect(MISCREG_TL
, oldTl
);
528 printSectionHeader(outs
, "General Purpose Registers");
529 static const char * regtypes
[4] =
530 {"%g", "%o", "%l", "%i"};
531 for(int y
= 0; y
< 4; y
++) {
532 for(int x
= 0; x
< 8; x
++) {
534 sprintf(label
, "%s%d", regtypes
[y
], x
);
535 printRegPair(outs
, label
,
536 thread
->readIntReg(y
*8+x
),
537 shared_data
->intregs
[y
*8+x
]);
541 for (int x
= 0; x
< 32; x
++) {
543 sprintf(label
, "%%f%d", x
);
544 printRegPair(outs
, label
,
545 thread
->readFloatRegBits(x
*2),
546 shared_data
->fpregs
[x
]);
550 printColumnLabels(outs
);
552 for (int x
= 0; x
< 64; x
++) {
553 if (shared_data
->itb
[x
] !=
554 ULL(0xFFFFFFFFFFFFFFFF) ||
555 thread
->getITBPtr()->TteRead(x
) !=
556 ULL(0xFFFFFFFFFFFFFFFF)) {
557 sprintf(label
, "I-TLB:%02d", x
);
558 printRegPair(outs
, label
,
559 thread
->getITBPtr()->TteRead(x
),
560 shared_data
->itb
[x
]);
563 for (int x
= 0; x
< 64; x
++) {
564 if (shared_data
->dtb
[x
] !=
565 ULL(0xFFFFFFFFFFFFFFFF) ||
566 thread
->getDTBPtr()->TteRead(x
) !=
567 ULL(0xFFFFFFFFFFFFFFFF)) {
568 sprintf(label
, "D-TLB:%02d", x
);
569 printRegPair(outs
, label
,
570 thread
->getDTBPtr()->TteRead(x
),
571 shared_data
->dtb
[x
]);
574 thread
->getITBPtr()->dumpAll();
575 thread
->getDTBPtr()->dumpAll();
580 fatal("Differences found between Legion and M5\n");
585 shared_data
->flags
= OWN_LEGION
;
593 ////////////////////////////////////////////////////////////////////////
595 // ExeTracer Simulation Object
598 LegionTraceParams::create()
600 return new Trace::LegionTrace(this);