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