X86: Implement MOVQ2DQ.
[gem5.git] / src / cpu / legiontrace.cc
1 /*
2 * Copyright (c) 2001-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: Steve Reinhardt
29 * Lisa Hsu
30 * Nathan Binkert
31 * Steve Raasch
32 */
33
34 #include "arch/isa_specific.hh"
35 #if THE_ISA != SPARC_ISA
36 #error Legion tracing only works with SPARC simulations!
37 #endif
38
39 #include "config/full_system.hh"
40 #if !FULL_SYSTEM
41 #error Legion tracing only works in full system!
42 #endif
43
44 #include <iomanip>
45 #include <sys/ipc.h>
46 #include <sys/shm.h>
47
48 #include "arch/sparc/predecoder.hh"
49 #include "arch/sparc/registers.hh"
50 #include "arch/sparc/utility.hh"
51 #include "base/socket.hh"
52 #include "cpu/base.hh"
53 #include "cpu/legiontrace.hh"
54 #include "cpu/static_inst.hh"
55 #include "cpu/thread_context.hh"
56 #include "sim/system.hh"
57
58 #if FULL_SYSTEM
59 #include "arch/tlb.hh"
60 #endif
61
62 //XXX This is temporary
63 #include "cpu/m5legion_interface.h"
64
65 using namespace std;
66 using namespace TheISA;
67
68 #if FULL_SYSTEM
69 static int diffcount = 0;
70 static bool wasMicro = false;
71 #endif
72
73 namespace Trace {
74 SharedData *shared_data = NULL;
75
76 void
77 setupSharedData()
78 {
79 int shmfd = shmget('M' << 24 | getuid(), sizeof(SharedData), 0777);
80 if (shmfd < 0)
81 fatal("Couldn't get shared memory fd. Is Legion running?");
82
83 shared_data = (SharedData*)shmat(shmfd, NULL, SHM_RND);
84 if (shared_data == (SharedData*)-1)
85 fatal("Couldn't allocate shared memory");
86
87 if (shared_data->flags != OWN_M5)
88 fatal("Shared memory has invalid owner");
89
90 if (shared_data->version != VERSION)
91 fatal("Shared Data is wrong version! M5: %d Legion: %d", VERSION,
92 shared_data->version);
93
94 // step legion forward one cycle so we can get register values
95 shared_data->flags = OWN_LEGION;
96 }
97
98 ////////////////////////////////////////////////////////////////////////
99 //
100 // Utility methods for pretty printing a report about a difference
101 //
102
103 inline char * genCenteredLabel(int length, char * buffer, const char * label)
104 {
105 int labelLength = strlen(label);
106 assert(labelLength <= length);
107 int leftPad = (length - labelLength) / 2;
108 int rightPad = length - leftPad - labelLength;
109 char format[64];
110 sprintf(format, "%%%ds%%s%%%ds", leftPad, rightPad);
111 sprintf(buffer, format, "", label, "");
112 return buffer;
113 }
114
115 inline void printRegPair(ostream & os, char const * title, uint64_t a, uint64_t b)
116 {
117 ccprintf(os, " %16s | %#018x %s %#-018x \n",
118 title, a, (a == b) ? "|" : "X", b);
119 }
120
121 inline void printColumnLabels(ostream & os)
122 {
123 static char * regLabel = genCenteredLabel(16, new char[17], "Register");
124 static char * m5Label = genCenteredLabel(18, new char[18], "M5");
125 static char * legionLabel = genCenteredLabel(18, new char[18], "Legion");
126 ccprintf(os, " %s | %s | %s \n", regLabel, m5Label, legionLabel);
127 ccprintf(os, "--------------------+-----------------------+-----------------------\n");
128 }
129
130 inline void printSectionHeader(ostream & os, const char * name)
131 {
132 char sectionString[70];
133 genCenteredLabel(69, sectionString, name);
134 ccprintf(os, "====================================================================\n");
135 ccprintf(os, "%69s\n", sectionString);
136 ccprintf(os, "====================================================================\n");
137 }
138
139 inline void printLevelHeader(ostream & os, int level)
140 {
141 char sectionString[70];
142 char levelName[70];
143 sprintf(levelName, "Trap stack level %d", level);
144 genCenteredLabel(69, sectionString, levelName);
145 ccprintf(os, "====================================================================\n");
146 ccprintf(os, "%69s\n", sectionString);
147 ccprintf(os, "====================================================================\n");
148 }
149
150 void
151 Trace::LegionTraceRecord::dump()
152 {
153 ostream &outs = Trace::output();
154
155 static TheISA::Predecoder predecoder(NULL);
156 // Compare
157 bool compared = false;
158 bool diffPC = false;
159 bool diffCC = false;
160 bool diffInst = false;
161 bool diffIntRegs = false;
162 bool diffFpRegs = false;
163 bool diffTpc = false;
164 bool diffTnpc = false;
165 bool diffTstate = false;
166 bool diffTt = false;
167 bool diffTba = false;
168 bool diffHpstate = false;
169 bool diffHtstate = false;
170 bool diffHtba = false;
171 bool diffPstate = false;
172 bool diffY = false;
173 bool diffFsr = false;
174 bool diffCcr = false;
175 bool diffTl = false;
176 bool diffGl = false;
177 bool diffAsi = false;
178 bool diffPil = false;
179 bool diffCwp = false;
180 bool diffCansave = false;
181 bool diffCanrestore = false;
182 bool diffOtherwin = false;
183 bool diffCleanwin = false;
184 bool diffTlb = false;
185 Addr m5Pc, lgnPc;
186
187 if (!shared_data)
188 setupSharedData();
189
190 // We took a trap on a micro-op...
191 if (wasMicro && !staticInst->isMicroop())
192 {
193 // let's skip comparing this tick
194 while (!compared)
195 if (shared_data->flags == OWN_M5) {
196 shared_data->flags = OWN_LEGION;
197 compared = true;
198 }
199 compared = false;
200 wasMicro = false;
201 }
202
203 if (staticInst->isLastMicroop())
204 wasMicro = false;
205 else if (staticInst->isMicroop())
206 wasMicro = true;
207
208
209 if(!staticInst->isMicroop() || staticInst->isLastMicroop()) {
210 while (!compared) {
211 if (shared_data->flags == OWN_M5) {
212 m5Pc = PC & SparcISA::PAddrImplMask;
213 if (bits(shared_data->pstate,3,3)) {
214 m5Pc &= mask(32);
215 }
216 lgnPc = shared_data->pc & SparcISA::PAddrImplMask;
217 if (lgnPc != m5Pc)
218 diffPC = true;
219
220 if (shared_data->cycle_count !=
221 thread->getCpuPtr()->instCount())
222 diffCC = true;
223
224 if (shared_data->instruction !=
225 (SparcISA::MachInst)staticInst->machInst) {
226 diffInst = true;
227 }
228 // assume we have %g0 working correctly
229 for (int i = 1; i < TheISA::NumIntArchRegs; i++) {
230 if (thread->readIntReg(i) != shared_data->intregs[i]) {
231 diffIntRegs = true;
232 }
233 }
234 for (int i = 0; i < TheISA::NumFloatRegs/2; i++) {
235 if (thread->readFloatRegBits(i*2) !=
236 shared_data->fpregs[i]) {
237 diffFpRegs = true;
238 }
239 }
240 uint64_t oldTl =
241 thread->readMiscRegNoEffect(MISCREG_TL);
242 if (oldTl != shared_data->tl)
243 diffTl = true;
244 for (int i = 1; i <= MaxTL; i++) {
245 thread->setMiscRegNoEffect(MISCREG_TL, i);
246 if (thread->readMiscRegNoEffect(MISCREG_TPC) !=
247 shared_data->tpc[i-1])
248 diffTpc = true;
249 if (thread->readMiscRegNoEffect(MISCREG_TNPC) !=
250 shared_data->tnpc[i-1])
251 diffTnpc = true;
252 if (thread->readMiscRegNoEffect(MISCREG_TSTATE) !=
253 shared_data->tstate[i-1])
254 diffTstate = true;
255 if (thread->readMiscRegNoEffect(MISCREG_TT) !=
256 shared_data->tt[i-1])
257 diffTt = true;
258 if (thread->readMiscRegNoEffect(MISCREG_HTSTATE) !=
259 shared_data->htstate[i-1])
260 diffHtstate = true;
261 }
262 thread->setMiscRegNoEffect(MISCREG_TL, oldTl);
263
264 if(shared_data->tba != thread->readMiscRegNoEffect(MISCREG_TBA))
265 diffTba = true;
266 //When the hpstate register is read by an instruction,
267 //legion has bit 11 set. When it's in storage, it doesn't.
268 //Since we don't directly support seperate interpretations
269 //of the registers like that, the bit is always set to 1 and
270 //we just don't compare it. It's not supposed to matter
271 //anyway.
272 if((shared_data->hpstate | (1 << 11)) !=
273 thread->readMiscRegNoEffect(MISCREG_HPSTATE))
274 diffHpstate = true;
275 if(shared_data->htba !=
276 thread->readMiscRegNoEffect(MISCREG_HTBA))
277 diffHtba = true;
278 if(shared_data->pstate !=
279 thread->readMiscRegNoEffect(MISCREG_PSTATE))
280 diffPstate = true;
281 //if(shared_data->y !=
282 // thread->readMiscRegNoEffect(MISCREG_Y))
283 if(shared_data->y !=
284 thread->readIntReg(NumIntArchRegs + 1))
285 diffY = true;
286 if(shared_data->fsr !=
287 thread->readMiscRegNoEffect(MISCREG_FSR)) {
288 diffFsr = true;
289 if (mbits(shared_data->fsr, 63,10) ==
290 mbits(thread->readMiscRegNoEffect(MISCREG_FSR),
291 63,10)) {
292 thread->setMiscRegNoEffect(MISCREG_FSR,
293 shared_data->fsr);
294 diffFsr = false;
295 }
296 }
297 //if(shared_data->ccr !=
298 // thread->readMiscRegNoEffect(MISCREG_CCR))
299 if(shared_data->ccr !=
300 thread->readIntReg(NumIntArchRegs + 2))
301 diffCcr = true;
302 if(shared_data->gl !=
303 thread->readMiscRegNoEffect(MISCREG_GL))
304 diffGl = true;
305 if(shared_data->asi !=
306 thread->readMiscRegNoEffect(MISCREG_ASI))
307 diffAsi = true;
308 if(shared_data->pil !=
309 thread->readMiscRegNoEffect(MISCREG_PIL))
310 diffPil = true;
311 if(shared_data->cwp !=
312 thread->readMiscRegNoEffect(MISCREG_CWP))
313 diffCwp = true;
314 //if(shared_data->cansave !=
315 // thread->readMiscRegNoEffect(MISCREG_CANSAVE))
316 if(shared_data->cansave !=
317 thread->readIntReg(NumIntArchRegs + 3))
318 diffCansave = true;
319 //if(shared_data->canrestore !=
320 // thread->readMiscRegNoEffect(MISCREG_CANRESTORE))
321 if(shared_data->canrestore !=
322 thread->readIntReg(NumIntArchRegs + 4))
323 diffCanrestore = true;
324 //if(shared_data->otherwin !=
325 // thread->readMiscRegNoEffect(MISCREG_OTHERWIN))
326 if(shared_data->otherwin !=
327 thread->readIntReg(NumIntArchRegs + 6))
328 diffOtherwin = true;
329 //if(shared_data->cleanwin !=
330 // thread->readMiscRegNoEffect(MISCREG_CLEANWIN))
331 if(shared_data->cleanwin !=
332 thread->readIntReg(NumIntArchRegs + 5))
333 diffCleanwin = true;
334
335 for (int i = 0; i < 64; i++) {
336 if (shared_data->itb[i] !=
337 thread->getITBPtr()->TteRead(i))
338 diffTlb = true;
339 if (shared_data->dtb[i] !=
340 thread->getDTBPtr()->TteRead(i))
341 diffTlb = true;
342 }
343
344 if (diffPC || diffCC || diffInst ||
345 diffIntRegs || diffFpRegs ||
346 diffTpc || diffTnpc || diffTstate || diffTt ||
347 diffHpstate || diffHtstate || diffHtba ||
348 diffPstate || diffY || diffCcr || diffTl || diffFsr ||
349 diffGl || diffAsi || diffPil || diffCwp ||
350 diffCansave || diffCanrestore ||
351 diffOtherwin || diffCleanwin || diffTlb) {
352
353 outs << "Differences found between M5 and Legion:";
354 if (diffPC)
355 outs << " [PC]";
356 if (diffCC)
357 outs << " [CC]";
358 if (diffInst)
359 outs << " [Instruction]";
360 if (diffIntRegs)
361 outs << " [IntRegs]";
362 if (diffFpRegs)
363 outs << " [FpRegs]";
364 if (diffTpc)
365 outs << " [Tpc]";
366 if (diffTnpc)
367 outs << " [Tnpc]";
368 if (diffTstate)
369 outs << " [Tstate]";
370 if (diffTt)
371 outs << " [Tt]";
372 if (diffHpstate)
373 outs << " [Hpstate]";
374 if (diffHtstate)
375 outs << " [Htstate]";
376 if (diffHtba)
377 outs << " [Htba]";
378 if (diffPstate)
379 outs << " [Pstate]";
380 if (diffY)
381 outs << " [Y]";
382 if (diffFsr)
383 outs << " [FSR]";
384 if (diffCcr)
385 outs << " [Ccr]";
386 if (diffTl)
387 outs << " [Tl]";
388 if (diffGl)
389 outs << " [Gl]";
390 if (diffAsi)
391 outs << " [Asi]";
392 if (diffPil)
393 outs << " [Pil]";
394 if (diffCwp)
395 outs << " [Cwp]";
396 if (diffCansave)
397 outs << " [Cansave]";
398 if (diffCanrestore)
399 outs << " [Canrestore]";
400 if (diffOtherwin)
401 outs << " [Otherwin]";
402 if (diffCleanwin)
403 outs << " [Cleanwin]";
404 if (diffTlb)
405 outs << " [Tlb]";
406 outs << endl << endl;
407
408 outs << right << setfill(' ') << setw(15)
409 << "M5 PC: " << "0x"<< setw(16) << setfill('0')
410 << hex << m5Pc << endl;
411 outs << setfill(' ') << setw(15)
412 << "Legion PC: " << "0x"
413 << setw(16) << setfill('0') << hex
414 << lgnPc << endl << endl;
415
416 outs << right << setfill(' ') << setw(15)
417 << "M5 CC: " << "0x"
418 << setw(16) << setfill('0') << hex
419 << thread->getCpuPtr()->instCount() << endl;
420 outs << setfill(' ') << setw(15)
421 << "Legion CC: " << "0x"
422 << setw(16) << setfill('0') << hex
423 << shared_data->cycle_count << endl << endl;
424
425 outs << setfill(' ') << setw(15)
426 << "M5 Inst: " << "0x"
427 << setw(8) << setfill('0') << hex
428 << staticInst->machInst
429 << staticInst->disassemble(m5Pc, debugSymbolTable)
430 << endl;
431
432 predecoder.setTC(thread);
433 predecoder.moreBytes(m5Pc, m5Pc,
434 shared_data->instruction);
435
436 assert(predecoder.extMachInstReady());
437
438 StaticInstPtr legionInst =
439 StaticInst::decode(predecoder.getExtMachInst(), lgnPc);
440 outs << setfill(' ') << setw(15)
441 << " Legion Inst: "
442 << "0x" << setw(8) << setfill('0') << hex
443 << shared_data->instruction
444 << legionInst->disassemble(lgnPc, debugSymbolTable)
445 << endl << endl;
446
447 printSectionHeader(outs, "General State");
448 printColumnLabels(outs);
449 printRegPair(outs, "HPstate",
450 thread->readMiscRegNoEffect(MISCREG_HPSTATE),
451 shared_data->hpstate | (1 << 11));
452 printRegPair(outs, "Htba",
453 thread->readMiscRegNoEffect(MISCREG_HTBA),
454 shared_data->htba);
455 printRegPair(outs, "Pstate",
456 thread->readMiscRegNoEffect(MISCREG_PSTATE),
457 shared_data->pstate);
458 printRegPair(outs, "Y",
459 //thread->readMiscRegNoEffect(MISCREG_Y),
460 thread->readIntReg(NumIntArchRegs + 1),
461 shared_data->y);
462 printRegPair(outs, "FSR",
463 thread->readMiscRegNoEffect(MISCREG_FSR),
464 shared_data->fsr);
465 printRegPair(outs, "Ccr",
466 //thread->readMiscRegNoEffect(MISCREG_CCR),
467 thread->readIntReg(NumIntArchRegs + 2),
468 shared_data->ccr);
469 printRegPair(outs, "Tl",
470 thread->readMiscRegNoEffect(MISCREG_TL),
471 shared_data->tl);
472 printRegPair(outs, "Gl",
473 thread->readMiscRegNoEffect(MISCREG_GL),
474 shared_data->gl);
475 printRegPair(outs, "Asi",
476 thread->readMiscRegNoEffect(MISCREG_ASI),
477 shared_data->asi);
478 printRegPair(outs, "Pil",
479 thread->readMiscRegNoEffect(MISCREG_PIL),
480 shared_data->pil);
481 printRegPair(outs, "Cwp",
482 thread->readMiscRegNoEffect(MISCREG_CWP),
483 shared_data->cwp);
484 printRegPair(outs, "Cansave",
485 //thread->readMiscRegNoEffect(MISCREG_CANSAVE),
486 thread->readIntReg(NumIntArchRegs + 3),
487 shared_data->cansave);
488 printRegPair(outs, "Canrestore",
489 //thread->readMiscRegNoEffect(MISCREG_CANRESTORE),
490 thread->readIntReg(NumIntArchRegs + 4),
491 shared_data->canrestore);
492 printRegPair(outs, "Otherwin",
493 //thread->readMiscRegNoEffect(MISCREG_OTHERWIN),
494 thread->readIntReg(NumIntArchRegs + 6),
495 shared_data->otherwin);
496 printRegPair(outs, "Cleanwin",
497 //thread->readMiscRegNoEffect(MISCREG_CLEANWIN),
498 thread->readIntReg(NumIntArchRegs + 5),
499 shared_data->cleanwin);
500 outs << endl;
501 for (int i = 1; i <= MaxTL; i++) {
502 printLevelHeader(outs, i);
503 printColumnLabels(outs);
504 thread->setMiscRegNoEffect(MISCREG_TL, i);
505 printRegPair(outs, "Tpc",
506 thread->readMiscRegNoEffect(MISCREG_TPC),
507 shared_data->tpc[i-1]);
508 printRegPair(outs, "Tnpc",
509 thread->readMiscRegNoEffect(MISCREG_TNPC),
510 shared_data->tnpc[i-1]);
511 printRegPair(outs, "Tstate",
512 thread->readMiscRegNoEffect(MISCREG_TSTATE),
513 shared_data->tstate[i-1]);
514 printRegPair(outs, "Tt",
515 thread->readMiscRegNoEffect(MISCREG_TT),
516 shared_data->tt[i-1]);
517 printRegPair(outs, "Htstate",
518 thread->readMiscRegNoEffect(MISCREG_HTSTATE),
519 shared_data->htstate[i-1]);
520 }
521 thread->setMiscRegNoEffect(MISCREG_TL, oldTl);
522 outs << endl;
523
524 printSectionHeader(outs, "General Purpose Registers");
525 static const char * regtypes[4] =
526 {"%g", "%o", "%l", "%i"};
527 for(int y = 0; y < 4; y++) {
528 for(int x = 0; x < 8; x++) {
529 char label[8];
530 sprintf(label, "%s%d", regtypes[y], x);
531 printRegPair(outs, label,
532 thread->readIntReg(y*8+x),
533 shared_data->intregs[y*8+x]);
534 }
535 }
536 if (diffFpRegs) {
537 for (int x = 0; x < 32; x++) {
538 char label[8];
539 sprintf(label, "%%f%d", x);
540 printRegPair(outs, label,
541 thread->readFloatRegBits(x*2),
542 shared_data->fpregs[x]);
543 }
544 }
545 if (diffTlb) {
546 printColumnLabels(outs);
547 char label[8];
548 for (int x = 0; x < 64; x++) {
549 if (shared_data->itb[x] !=
550 ULL(0xFFFFFFFFFFFFFFFF) ||
551 thread->getITBPtr()->TteRead(x) !=
552 ULL(0xFFFFFFFFFFFFFFFF)) {
553 sprintf(label, "I-TLB:%02d", x);
554 printRegPair(outs, label,
555 thread->getITBPtr()->TteRead(x),
556 shared_data->itb[x]);
557 }
558 }
559 for (int x = 0; x < 64; x++) {
560 if (shared_data->dtb[x] !=
561 ULL(0xFFFFFFFFFFFFFFFF) ||
562 thread->getDTBPtr()->TteRead(x) !=
563 ULL(0xFFFFFFFFFFFFFFFF)) {
564 sprintf(label, "D-TLB:%02d", x);
565 printRegPair(outs, label,
566 thread->getDTBPtr()->TteRead(x),
567 shared_data->dtb[x]);
568 }
569 }
570 thread->getITBPtr()->dumpAll();
571 thread->getDTBPtr()->dumpAll();
572 }
573
574 diffcount++;
575 if (diffcount > 3)
576 fatal("Differences found between Legion and M5\n");
577 } else
578 diffcount = 0;
579
580 compared = true;
581 shared_data->flags = OWN_LEGION;
582 }
583 } // while
584 } // if not microop
585 }
586
587 /* namespace Trace */ }
588
589 ////////////////////////////////////////////////////////////////////////
590 //
591 // ExeTracer Simulation Object
592 //
593 Trace::LegionTrace *
594 LegionTraceParams::create()
595 {
596 return new Trace::LegionTrace(this);
597 };