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!
45 #include "arch/sparc/decoder.hh"
46 #include "arch/sparc/registers.hh"
47 #include "arch/sparc/utility.hh"
48 #include "arch/tlb.hh"
49 #include "base/socket.hh"
50 #include "cpu/base.hh"
51 #include "cpu/legiontrace.hh"
52 #include "cpu/static_inst.hh"
53 #include "cpu/thread_context.hh"
54 #include "sim/full_system.hh"
55 #include "sim/system.hh"
57 //XXX This is temporary
58 #include "cpu/m5legion_interface.h"
61 using namespace TheISA
;
63 static int diffcount
= 0;
64 static bool wasMicro
= false;
67 SharedData
*shared_data
= NULL
;
72 int shmfd
= shmget('M' << 24 | getuid(), sizeof(SharedData
), 0777);
74 fatal("Couldn't get shared memory fd. Is Legion running?");
76 shared_data
= (SharedData
*)shmat(shmfd
, NULL
, SHM_RND
);
77 if (shared_data
== (SharedData
*)-1)
78 fatal("Couldn't allocate shared memory");
80 if (shared_data
->flags
!= OWN_M5
)
81 fatal("Shared memory has invalid owner");
83 if (shared_data
->version
!= VERSION
)
84 fatal("Shared Data is wrong version! M5: %d Legion: %d", VERSION
,
85 shared_data
->version
);
87 // step legion forward one cycle so we can get register values
88 shared_data
->flags
= OWN_LEGION
;
91 ////////////////////////////////////////////////////////////////////////
93 // Utility methods for pretty printing a report about a difference
96 inline char * genCenteredLabel(int length
, char * buffer
, const char * label
)
98 int labelLength
= strlen(label
);
99 assert(labelLength
<= length
);
100 int leftPad
= (length
- labelLength
) / 2;
101 int rightPad
= length
- leftPad
- labelLength
;
103 sprintf(format
, "%%%ds%%s%%%ds", leftPad
, rightPad
);
104 sprintf(buffer
, format
, "", label
, "");
108 inline void printRegPair(ostream
& os
, char const * title
, uint64_t a
, uint64_t b
)
110 ccprintf(os
, " %16s | %#018x %s %#-018x \n",
111 title
, a
, (a
== b
) ? "|" : "X", b
);
114 inline void printColumnLabels(ostream
& os
)
116 static char * regLabel
= genCenteredLabel(16, new char[17], "Register");
117 static char * m5Label
= genCenteredLabel(18, new char[18], "M5");
118 static char * legionLabel
= genCenteredLabel(18, new char[18], "Legion");
119 ccprintf(os
, " %s | %s | %s \n", regLabel
, m5Label
, legionLabel
);
120 ccprintf(os
, "--------------------+-----------------------+-----------------------\n");
123 inline void printSectionHeader(ostream
& os
, const char * name
)
125 char sectionString
[70];
126 genCenteredLabel(69, sectionString
, name
);
127 ccprintf(os
, "====================================================================\n");
128 ccprintf(os
, "%69s\n", sectionString
);
129 ccprintf(os
, "====================================================================\n");
132 inline void printLevelHeader(ostream
& os
, int level
)
134 char sectionString
[70];
136 sprintf(levelName
, "Trap stack level %d", level
);
137 genCenteredLabel(69, sectionString
, levelName
);
138 ccprintf(os
, "====================================================================\n");
139 ccprintf(os
, "%69s\n", sectionString
);
140 ccprintf(os
, "====================================================================\n");
144 Trace::LegionTraceRecord::dump()
146 ostream
&outs
= Trace::output();
149 bool compared
= false;
152 bool diffInst
= false;
153 bool diffIntRegs
= false;
154 bool diffFpRegs
= false;
155 bool diffTpc
= false;
156 bool diffTnpc
= false;
157 bool diffTstate
= false;
159 bool diffTba M5_VAR_USED
= false;
160 bool diffHpstate
= false;
161 bool diffHtstate
= false;
162 bool diffHtba
= false;
163 bool diffPstate
= false;
165 bool diffFsr
= false;
166 bool diffCcr
= false;
169 bool diffAsi
= false;
170 bool diffPil
= false;
171 bool diffCwp
= false;
172 bool diffCansave
= false;
173 bool diffCanrestore
= false;
174 bool diffOtherwin
= false;
175 bool diffCleanwin
= false;
176 bool diffTlb
= false;
182 // We took a trap on a micro-op...
183 if (wasMicro
&& !staticInst
->isMicroop())
185 // let's skip comparing this tick
187 if (shared_data
->flags
== OWN_M5
) {
188 shared_data
->flags
= OWN_LEGION
;
195 if (staticInst
->isLastMicroop())
197 else if (staticInst
->isMicroop())
201 if(!staticInst
->isMicroop() || staticInst
->isLastMicroop()) {
203 if (shared_data
->flags
== OWN_M5
) {
204 m5Pc
= pc
.instAddr() & SparcISA::PAddrImplMask
;
205 if (bits(shared_data
->pstate
,3,3)) {
208 lgnPc
= shared_data
->pc
& SparcISA::PAddrImplMask
;
212 if (shared_data
->cycle_count
!=
213 thread
->getCpuPtr()->instCount())
216 if (shared_data
->instruction
!=
217 (SparcISA::MachInst
)staticInst
->machInst
) {
220 // assume we have %g0 working correctly
221 for (int i
= 1; i
< TheISA::NumIntArchRegs
; i
++) {
222 if (thread
->readIntReg(i
) != shared_data
->intregs
[i
]) {
226 for (int i
= 0; i
< TheISA::NumFloatRegs
/2; i
++) {
227 if (thread
->readFloatRegBits(i
*2) !=
228 shared_data
->fpregs
[i
]) {
233 thread
->readMiscRegNoEffect(MISCREG_TL
);
234 if (oldTl
!= shared_data
->tl
)
236 for (int i
= 1; i
<= MaxTL
; i
++) {
237 thread
->setMiscRegNoEffect(MISCREG_TL
, i
);
238 if (thread
->readMiscRegNoEffect(MISCREG_TPC
) !=
239 shared_data
->tpc
[i
-1])
241 if (thread
->readMiscRegNoEffect(MISCREG_TNPC
) !=
242 shared_data
->tnpc
[i
-1])
244 if (thread
->readMiscRegNoEffect(MISCREG_TSTATE
) !=
245 shared_data
->tstate
[i
-1])
247 if (thread
->readMiscRegNoEffect(MISCREG_TT
) !=
248 shared_data
->tt
[i
-1])
250 if (thread
->readMiscRegNoEffect(MISCREG_HTSTATE
) !=
251 shared_data
->htstate
[i
-1])
254 thread
->setMiscRegNoEffect(MISCREG_TL
, oldTl
);
256 if(shared_data
->tba
!= thread
->readMiscRegNoEffect(MISCREG_TBA
))
258 //When the hpstate register is read by an instruction,
259 //legion has bit 11 set. When it's in storage, it doesn't.
260 //Since we don't directly support seperate interpretations
261 //of the registers like that, the bit is always set to 1 and
262 //we just don't compare it. It's not supposed to matter
264 if((shared_data
->hpstate
| (1 << 11)) !=
265 thread
->readMiscRegNoEffect(MISCREG_HPSTATE
))
267 if(shared_data
->htba
!=
268 thread
->readMiscRegNoEffect(MISCREG_HTBA
))
270 if(shared_data
->pstate
!=
271 thread
->readMiscRegNoEffect(MISCREG_PSTATE
))
273 //if(shared_data->y !=
274 // thread->readMiscRegNoEffect(MISCREG_Y))
276 thread
->readIntReg(NumIntArchRegs
+ 1))
278 if(shared_data
->fsr
!=
279 thread
->readMiscRegNoEffect(MISCREG_FSR
)) {
281 if (mbits(shared_data
->fsr
, 63,10) ==
282 mbits(thread
->readMiscRegNoEffect(MISCREG_FSR
),
284 thread
->setMiscRegNoEffect(MISCREG_FSR
,
289 //if(shared_data->ccr !=
290 // thread->readMiscRegNoEffect(MISCREG_CCR))
291 if(shared_data
->ccr
!=
292 thread
->readIntReg(NumIntArchRegs
+ 2))
294 if(shared_data
->gl
!=
295 thread
->readMiscRegNoEffect(MISCREG_GL
))
297 if(shared_data
->asi
!=
298 thread
->readMiscRegNoEffect(MISCREG_ASI
))
300 if(shared_data
->pil
!=
301 thread
->readMiscRegNoEffect(MISCREG_PIL
))
303 if(shared_data
->cwp
!=
304 thread
->readMiscRegNoEffect(MISCREG_CWP
))
306 //if(shared_data->cansave !=
307 // thread->readMiscRegNoEffect(MISCREG_CANSAVE))
308 if(shared_data
->cansave
!=
309 thread
->readIntReg(NumIntArchRegs
+ 3))
311 //if(shared_data->canrestore !=
312 // thread->readMiscRegNoEffect(MISCREG_CANRESTORE))
313 if(shared_data
->canrestore
!=
314 thread
->readIntReg(NumIntArchRegs
+ 4))
315 diffCanrestore
= true;
316 //if(shared_data->otherwin !=
317 // thread->readMiscRegNoEffect(MISCREG_OTHERWIN))
318 if(shared_data
->otherwin
!=
319 thread
->readIntReg(NumIntArchRegs
+ 6))
321 //if(shared_data->cleanwin !=
322 // thread->readMiscRegNoEffect(MISCREG_CLEANWIN))
323 if(shared_data
->cleanwin
!=
324 thread
->readIntReg(NumIntArchRegs
+ 5))
327 for (int i
= 0; i
< 64; i
++) {
328 if (shared_data
->itb
[i
] !=
329 thread
->getITBPtr()->TteRead(i
))
331 if (shared_data
->dtb
[i
] !=
332 thread
->getDTBPtr()->TteRead(i
))
336 if (diffPC
|| diffCC
|| diffInst
||
337 diffIntRegs
|| diffFpRegs
||
338 diffTpc
|| diffTnpc
|| diffTstate
|| diffTt
||
339 diffHpstate
|| diffHtstate
|| diffHtba
||
340 diffPstate
|| diffY
|| diffCcr
|| diffTl
|| diffFsr
||
341 diffGl
|| diffAsi
|| diffPil
|| diffCwp
||
342 diffCansave
|| diffCanrestore
||
343 diffOtherwin
|| diffCleanwin
|| diffTlb
) {
345 outs
<< "Differences found between M5 and Legion:";
351 outs
<< " [Instruction]";
353 outs
<< " [IntRegs]";
365 outs
<< " [Hpstate]";
367 outs
<< " [Htstate]";
389 outs
<< " [Cansave]";
391 outs
<< " [Canrestore]";
393 outs
<< " [Otherwin]";
395 outs
<< " [Cleanwin]";
398 outs
<< endl
<< endl
;
400 outs
<< right
<< setfill(' ') << setw(15)
401 << "M5 PC: " << "0x"<< setw(16) << setfill('0')
402 << hex
<< m5Pc
<< endl
;
403 outs
<< setfill(' ') << setw(15)
404 << "Legion PC: " << "0x"
405 << setw(16) << setfill('0') << hex
406 << lgnPc
<< endl
<< endl
;
408 outs
<< right
<< setfill(' ') << setw(15)
410 << setw(16) << setfill('0') << hex
411 << thread
->getCpuPtr()->instCount() << endl
;
412 outs
<< setfill(' ') << setw(15)
413 << "Legion CC: " << "0x"
414 << setw(16) << setfill('0') << hex
415 << shared_data
->cycle_count
<< endl
<< endl
;
417 outs
<< setfill(' ') << setw(15)
418 << "M5 Inst: " << "0x"
419 << setw(8) << setfill('0') << hex
420 << staticInst
->machInst
421 << staticInst
->disassemble(m5Pc
, debugSymbolTable
)
424 TheISA::Decoder
*decoder
= thread
->getDecoderPtr();
425 decoder
->moreBytes(m5Pc
, m5Pc
, shared_data
->instruction
);
427 assert(decoder
->instReady());
430 StaticInstPtr legionInst
= decoder
->decode(tempPC
);
431 outs
<< setfill(' ') << setw(15)
433 << "0x" << setw(8) << setfill('0') << hex
434 << shared_data
->instruction
435 << legionInst
->disassemble(lgnPc
, debugSymbolTable
)
438 printSectionHeader(outs
, "General State");
439 printColumnLabels(outs
);
440 printRegPair(outs
, "HPstate",
441 thread
->readMiscRegNoEffect(MISCREG_HPSTATE
),
442 shared_data
->hpstate
| (1 << 11));
443 printRegPair(outs
, "Htba",
444 thread
->readMiscRegNoEffect(MISCREG_HTBA
),
446 printRegPair(outs
, "Pstate",
447 thread
->readMiscRegNoEffect(MISCREG_PSTATE
),
448 shared_data
->pstate
);
449 printRegPair(outs
, "Y",
450 //thread->readMiscRegNoEffect(MISCREG_Y),
451 thread
->readIntReg(NumIntArchRegs
+ 1),
453 printRegPair(outs
, "FSR",
454 thread
->readMiscRegNoEffect(MISCREG_FSR
),
456 printRegPair(outs
, "Ccr",
457 //thread->readMiscRegNoEffect(MISCREG_CCR),
458 thread
->readIntReg(NumIntArchRegs
+ 2),
460 printRegPair(outs
, "Tl",
461 thread
->readMiscRegNoEffect(MISCREG_TL
),
463 printRegPair(outs
, "Gl",
464 thread
->readMiscRegNoEffect(MISCREG_GL
),
466 printRegPair(outs
, "Asi",
467 thread
->readMiscRegNoEffect(MISCREG_ASI
),
469 printRegPair(outs
, "Pil",
470 thread
->readMiscRegNoEffect(MISCREG_PIL
),
472 printRegPair(outs
, "Cwp",
473 thread
->readMiscRegNoEffect(MISCREG_CWP
),
475 printRegPair(outs
, "Cansave",
476 //thread->readMiscRegNoEffect(MISCREG_CANSAVE),
477 thread
->readIntReg(NumIntArchRegs
+ 3),
478 shared_data
->cansave
);
479 printRegPair(outs
, "Canrestore",
480 //thread->readMiscRegNoEffect(MISCREG_CANRESTORE),
481 thread
->readIntReg(NumIntArchRegs
+ 4),
482 shared_data
->canrestore
);
483 printRegPair(outs
, "Otherwin",
484 //thread->readMiscRegNoEffect(MISCREG_OTHERWIN),
485 thread
->readIntReg(NumIntArchRegs
+ 6),
486 shared_data
->otherwin
);
487 printRegPair(outs
, "Cleanwin",
488 //thread->readMiscRegNoEffect(MISCREG_CLEANWIN),
489 thread
->readIntReg(NumIntArchRegs
+ 5),
490 shared_data
->cleanwin
);
492 for (int i
= 1; i
<= MaxTL
; i
++) {
493 printLevelHeader(outs
, i
);
494 printColumnLabels(outs
);
495 thread
->setMiscRegNoEffect(MISCREG_TL
, i
);
496 printRegPair(outs
, "Tpc",
497 thread
->readMiscRegNoEffect(MISCREG_TPC
),
498 shared_data
->tpc
[i
-1]);
499 printRegPair(outs
, "Tnpc",
500 thread
->readMiscRegNoEffect(MISCREG_TNPC
),
501 shared_data
->tnpc
[i
-1]);
502 printRegPair(outs
, "Tstate",
503 thread
->readMiscRegNoEffect(MISCREG_TSTATE
),
504 shared_data
->tstate
[i
-1]);
505 printRegPair(outs
, "Tt",
506 thread
->readMiscRegNoEffect(MISCREG_TT
),
507 shared_data
->tt
[i
-1]);
508 printRegPair(outs
, "Htstate",
509 thread
->readMiscRegNoEffect(MISCREG_HTSTATE
),
510 shared_data
->htstate
[i
-1]);
512 thread
->setMiscRegNoEffect(MISCREG_TL
, oldTl
);
515 printSectionHeader(outs
, "General Purpose Registers");
516 static const char * regtypes
[4] =
517 {"%g", "%o", "%l", "%i"};
518 for(int y
= 0; y
< 4; y
++) {
519 for(int x
= 0; x
< 8; x
++) {
521 sprintf(label
, "%s%d", regtypes
[y
], x
);
522 printRegPair(outs
, label
,
523 thread
->readIntReg(y
*8+x
),
524 shared_data
->intregs
[y
*8+x
]);
528 for (int x
= 0; x
< 32; x
++) {
530 sprintf(label
, "%%f%d", x
);
531 printRegPair(outs
, label
,
532 thread
->readFloatRegBits(x
*2),
533 shared_data
->fpregs
[x
]);
537 printColumnLabels(outs
);
539 for (int x
= 0; x
< 64; x
++) {
540 if (shared_data
->itb
[x
] !=
541 ULL(0xFFFFFFFFFFFFFFFF) ||
542 thread
->getITBPtr()->TteRead(x
) !=
543 ULL(0xFFFFFFFFFFFFFFFF)) {
544 sprintf(label
, "I-TLB:%02d", x
);
545 printRegPair(outs
, label
,
546 thread
->getITBPtr()->TteRead(x
),
547 shared_data
->itb
[x
]);
550 for (int x
= 0; x
< 64; x
++) {
551 if (shared_data
->dtb
[x
] !=
552 ULL(0xFFFFFFFFFFFFFFFF) ||
553 thread
->getDTBPtr()->TteRead(x
) !=
554 ULL(0xFFFFFFFFFFFFFFFF)) {
555 sprintf(label
, "D-TLB:%02d", x
);
556 printRegPair(outs
, label
,
557 thread
->getDTBPtr()->TteRead(x
),
558 shared_data
->dtb
[x
]);
561 thread
->getITBPtr()->dumpAll();
562 thread
->getDTBPtr()->dumpAll();
567 fatal("Differences found between Legion and M5\n");
572 shared_data
->flags
= OWN_LEGION
;
580 ////////////////////////////////////////////////////////////////////////
582 // ExeTracer Simulation Object
585 LegionTraceParams::create()
588 panic("Legion tracing only works in full system!");
589 return new Trace::LegionTrace(this);