- more 2.6.17 port work (still does not build)
[linux-flexiantxendom0-3.2.10.git] / arch / x86_64 / kdb / kdbasupport.c
1 /*
2  * Kernel Debugger Architecture Independent Support Functions
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
11 #include <linux/config.h>
12 #include <linux/string.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/ptrace.h>
17 #include <linux/mm.h>
18 #include <linux/sched.h>
19 #include <linux/kdb.h>
20 #include <linux/kdbprivate.h>
21 #include <asm/kdebug.h>
22 #include <asm/processor.h>
23 #include <asm/msr.h>
24 #include <asm/uaccess.h>
25
26
27 #define SP_SANITY(ss, sp)       1
28 #define SS(sp)                  ((sp) & -THREAD_SIZE)   /* XXX */
29
30 /*
31  * kdba_find_return
32  *
33  *      Given a starting point on the stack and symtab data for the
34  *      current function, scan up the stack looking for a return
35  *      address for this function.
36  * Inputs:
37  *      sp      Starting stack pointer for scan
38  *      ss      Start of stack for current process
39  *      symtab  kallsyms symbol data for the function
40  * Outputs:
41  *      None.
42  * Returns:
43  *      Position on stack of return address, 0 if not found.
44  * Locking:
45  *      None.
46  * Remarks:
47  *      This is sensitive to the calling sequence generated by gcc.
48  */
49
50
51 static kdb_machreg_t
52 kdba_find_return(kdb_machreg_t sp, kdb_machreg_t ss, const kdb_symtab_t *symtab)
53 {
54         kdb_machreg_t   ret;
55         kdb_symtab_t    caller_symtab;
56         unsigned long   disp8;
57         unsigned long   disp32;
58         unsigned char   code[7];
59 #define retaddr(off) code[sizeof(code)+(off)]
60
61         if (KDB_DEBUG(ARA)) {
62                 kdb_printf("  kdba_find_return: start\n");
63         }
64
65         if (SP_SANITY(ss, sp) == 0) {
66                 kdb_printf("    sp is in wrong stack 0x%lx\n", sp);
67                 return(0);
68         }
69
70         if ((sp & (THREAD_SIZE - 1)) < sizeof(struct task_struct)) {
71                 kdb_printf("    sp is inside task_struct\n");
72                 return(0);
73         }
74
75         for (;ret = 0, sp & (THREAD_SIZE-1);sp += 8) {
76                 if (KDB_DEBUG(ARA)) {
77                         kdb_printf("    sp=0x%lx", sp);
78                 }
79                 if (kdb_getword(&ret, sp, 8))
80                         break;
81                 kdbnearsym(ret, &caller_symtab);
82                 if (KDB_DEBUG(ARA)) {
83                         kdb_printf(" ret=");
84                         kdb_symbol_print(ret, &caller_symtab, KDB_SP_DEFAULT|KDB_SP_SYMSIZE);
85                 }
86                 if (!caller_symtab.sym_name) {
87                         if (KDB_DEBUG(ARA)) {
88                                 kdb_printf("\n");
89                         }
90                         continue;       /* not a valid kernel address */
91                 }
92                 KDB_STATE_SET(SUPPRESS);
93                 if (kdb_getarea(code, ret-sizeof(code)) ||
94                     kdb_getword(&disp32, ret-4, 4) ||
95                     kdb_getword(&disp8, ret-1, 1))
96                         continue;       /* not a valid return address */
97                 if (retaddr(-5) == 0xe8) {
98                         /* call disp32 */
99                         if (KDB_DEBUG(ARA)) {
100                                 kdb_printf(" call disp32");
101                         }
102                         if (ret + (s32) disp32 == symtab->sym_start) {
103                                 if (KDB_DEBUG(ARA)) {
104                                         kdb_printf(" matched\n");
105                                 }
106                                 break;          /* call to this function */
107                         }
108                         if (KDB_DEBUG(ARA)) {
109                                 kdb_printf(" failed");
110                         }
111                 } else if (retaddr(-7) == 0xff && retaddr(-6) == 0x14 && retaddr(-5) == 0x85) {
112                         /* call *disp32(,%rax,4), used by syscall.
113                          * Cannot calculate address, assume it is valid
114                          * if the current function name starts with
115                          * 'sys_' or 'old_'.
116                          */
117                         if (KDB_DEBUG(ARA)) {
118                                 /* XXX FIXME */
119                                 kdb_printf(" call *0xnnnn(,%%rax,4)");
120                         }
121                         if (strncmp(symtab->sym_name, "sys_", 4) == 0 ||
122                             strncmp(symtab->sym_name, "old_", 4) == 0) {
123                                 if (KDB_DEBUG(ARA)) {
124                                         kdb_printf(" matched\n");
125                                 }
126                                 break;          /* probably call to this function */
127                         }
128                         if (KDB_DEBUG(ARA)) {
129                                 kdb_printf(" failed");
130                         }
131                 } else if (retaddr(-2) == 0xff && (retaddr(-1) & 0xf8) == 0xd0) {
132                         /* call *%reg.  Cannot validate, have to assume
133                          * it is valid.
134                          */
135                         if (KDB_DEBUG(ARA)) {
136                                 kdb_printf(" call *%%reg, assume valid\n");
137                         }
138                         break;          /* hope it is a call to this function */
139                 } else if (retaddr(-3) == 0xff && (retaddr(-2) & 0xf8) == 0x50) {
140                         /* call *disp8(%reg).  Cannot validate, have to assume
141                          * it is valid.
142                          */
143                         if (KDB_DEBUG(ARA)) {
144                                 kdb_printf(" call *disp8(%%reg), assume valid\n");
145                         }
146                         break;          /* hope it is a call to this function */
147                 } else if (retaddr(-5) == 0xe9) {
148                         /* jmp disp32.  I have been told that gcc may
149                          * do function tail optimization and replace
150                          * call with jmp.
151                          */
152                         if (KDB_DEBUG(ARA)) {
153                                 kdb_printf(" jmp disp32\n");
154                         }
155                         if (ret + (s32) disp32 == symtab->sym_start) {
156                                 if (KDB_DEBUG(ARA)) {
157                                         kdb_printf(" matched\n");
158                                 }
159                                 break;          /* jmp to this function */
160                         }
161                         if (KDB_DEBUG(ARA)) {
162                                 kdb_printf(" failed");
163                         }
164                 } else if (retaddr(-2) == 0xeb) {
165                         /* jmp disp8 */
166                         if (KDB_DEBUG(ARA)) {
167                                 kdb_printf(" jmp disp8\n");
168                         }
169                         if (ret + (s8) disp8 == symtab->sym_start) {
170                                 if (KDB_DEBUG(ARA)) {
171                                         kdb_printf(" matched\n");
172                                 }
173                                 break;          /* jmp to this function */
174                         }
175                         if (KDB_DEBUG(ARA)) {
176                                 kdb_printf(" failed");
177                         }
178                 } else if (strcmp(caller_symtab.sym_name, "ret_from_intr") == 0
179                            && ret == caller_symtab.sym_start) {
180                         /* ret_from_intr is pushed on stack for interrupts */
181                         if (KDB_DEBUG(ARA)) {
182                                 kdb_printf(" ret_from_intr matched\n");
183                         }
184                         break;          /* special case, hand crafted frame */
185                 }
186                 if (KDB_DEBUG(ARA)) {
187                         kdb_printf("\n");
188                 }
189         }
190         if (KDB_DEBUG(ARA)) {
191                 kdb_printf("  end ret=0x%lx sp=0x%lx\n", ret, sp);
192         }
193         if (ret)
194                 return(sp);
195         return(0);
196 }
197
198 /*
199  * kdba_prologue
200  *
201  *      This function analyzes a gcc-generated function prototype
202  *      with or without frame pointers to determine the amount of
203  *      automatic storage and register save storage is used on the
204  *      stack of the target function.  It only counts instructions
205  *      that have been executed up to but excluding the current rip.
206  * Inputs:
207  *      code    Start address of function code to analyze
208  *      pc      Current program counter within function
209  *      sp      Current stack pointer for function
210  *      ss      Start of stack for current process
211  *      fp      Current frame pointer for function, may not be valid
212  *      caller  1 if looking for data on the caller frame, 0 for callee.
213  * Outputs:
214  *      ar      Activation record, all fields may be set.  fp and oldfp
215  *              are 0 if they cannot be extracted.  return is 0 if the
216  *              code cannot find a valid return address.  args and arg0
217  *              are 0 if the number of arguments cannot be safely
218  *              calculated.
219  * Returns:
220  *      1 if prologue is valid, 0 otherwise.  If pc is 0 treat it as a
221  *      valid prologue to allow bt on wild branches.
222  * Locking:
223  *      None.
224  * Remarks:
225  *
226  *      x86_64 prologues
227  *
228  *      kdba_prologue():
229  *              push   %r15
230  *              mov    %rdi,%r15
231  *              push   %r14
232  *              mov    %rsi,%r14
233  *              push   %r13
234  *              push   %r12
235  *              push   %rbp
236  *              mov    %rdx,%rbp
237  *
238  *      leaf fn w/ 1 arg:
239  *              push   %rbp
240  *              mov    %rsp,%rbp
241  *              mov    %rdi,0xfffffffffffffff8(%rbp)
242  *
243  */
244
245 int
246 kdba_prologue(const kdb_symtab_t *symtab, kdb_machreg_t pc, kdb_machreg_t sp,
247               kdb_machreg_t fp, kdb_machreg_t ss, int caller, kdb_ar_t *ar)
248 {
249         kdb_machreg_t   ret_p, code = symtab->sym_start;
250         int             oldfp_present = 0, unwound = 0;
251         unsigned char   instruction[6];
252
253         if (KDB_DEBUG(ARA)) {
254                 kdb_printf("kdba_prologue: code=0x%lx %s pc=0x%lx sp=0x%lx fp=0x%lx\n",
255                 code, symtab->sym_name, pc, sp, fp);
256         }
257
258         /* Special case for wild branches.  Assumes top of stack is return address */
259         if (pc == 0) {
260                 memset(ar, 0, sizeof(*ar));
261                 ar->setup = 8;
262                 ar->end = sp;
263                 ar->start = ar->end + 8;
264                 kdb_getword(&(ar->ret), sp, 8);
265                 if (KDB_DEBUG(ARA)) {
266                         kdb_printf("  pc==0: ret=0x%lx\n", ar->ret);
267                 }
268                 return(1);
269         }
270
271         if (code == 0 || sp & 3 || (SP_SANITY(ss, sp) == 0))
272                 return(0);
273
274         ar->end = sp;                           /* End of activation record +1 */
275
276         /* Special cases galore when the caller pc is within entry.S.
277          * The return address for these routines is outside the kernel,
278          * so the normal algorithm to find the frame does not work.
279          * Hand craft the frame to no setup, regs, locals etc, assume 6
280          * parameters.
281          * This list was extracted from entry.S by looking for all call
282          * instructions that were eventually followed by RESTORE_ALL,
283          * take the label before each such instruction.
284          */
285         if (caller &&
286             (strcmp(symtab->sym_name, "lcall7") == 0 ||
287              strcmp(symtab->sym_name, "lcall27") == 0 ||
288              strcmp(symtab->sym_name, "kdb_call") == 0 ||
289              strcmp(symtab->sym_name, "system_call") == 0 ||
290              strcmp(symtab->sym_name, "tracesys") == 0 ||
291              strcmp(symtab->sym_name, "signal_return") == 0 ||
292              strcmp(symtab->sym_name, "v86_signal_return") == 0 ||
293              strcmp(symtab->sym_name, "tracesys") == 0 ||
294              strcmp(symtab->sym_name, "tracesys_exit") == 0 ||
295              strcmp(symtab->sym_name, "handle_softirq") == 0 ||
296              strcmp(symtab->sym_name, "reschedule") == 0 ||
297              strcmp(symtab->sym_name, "error_code") == 0 ||
298              strcmp(symtab->sym_name, "device_not_available") == 0 ||
299              strcmp(symtab->sym_name, "nmi") == 0)) {
300                 ar->start = ar->end + 6*8;      /* 6 parameters */
301                 if (SP_SANITY(ss, ar->start) == 0)
302                         ar->start = 0;
303                 return(1);
304         }
305
306         ar->setup = 8;  /* Return address is always on stack */
307
308         /* Kludge.  If we are sitting on 'ret' then the stack has been unwound,
309          * ignore all the startup code.
310          */
311         if (kdb_getarea(instruction[0], pc))
312                 return(0);
313         if (instruction[0] == 0xc3) {
314                 /* ret */
315                 unwound = 1;
316         }
317
318         if (kdb_getarea(instruction, code))
319                 return(0);
320         if (!unwound && code < pc && instruction[0] == 0x55) {
321                 /* pushl %rbp */
322                 ar->setup += 8; /* Frame pointer is on stack */
323                 oldfp_present = 1;
324                 ++code;
325                 if (KDB_DEBUG(ARA)) {
326                         kdb_printf("  pushq %%rbp\n");
327                 }
328                 if (code < pc && instruction[0] == 0x89 && instruction[1] == 0xe5) {
329                         /* movl %esp,%ebp */
330                         if (fp >= sp && SP_SANITY(ss, fp))
331                                 ar->fp = fp;    /* %rbp has been set */
332                         code += 2;
333                         if (KDB_DEBUG(ARA)) {
334                                 kdb_printf("  movq %%rsp,%%rbp, fp=0x%lx\n", ar->fp);
335                         }
336                 }
337         }
338
339         if (!unwound && code < pc) {
340                 if (instruction[0] == 0x83 && instruction[1] == 0xec) {
341                         /* subl $xx,%rsp */
342                         kdb_getword(&(ar->locals), (unsigned long)(instruction+2), 1);
343                         code += 3;
344                         if (KDB_DEBUG(ARA)) {
345                                 kdb_printf("  subl $xx,%%rsp, locals=%ld\n", ar->locals);
346                         }
347                 } else if (instruction[0] == 0x81 && instruction[1] == 0xec) {
348                         /* subl $xxxxxxxx,%rsp */
349                         kdb_getword(&(ar->locals), (unsigned long)(instruction+2), 8);
350                         code += 6;
351                         if (KDB_DEBUG(ARA)) {
352                                 kdb_printf("  subl $xxxxxxxx,%%rsp, locals=%ld\n", ar->locals);
353                         }
354                 }
355         }
356
357         while (!unwound && code < pc &&
358                kdb_getarea(instruction, code) == 0 &&
359                (instruction[0] & 0xf8) == 0x50) {
360                 /* pushl %reg */
361                 ar->regs += 8;
362                 ++code;
363                 if (KDB_DEBUG(ARA)) {
364                         kdb_printf("  pushq %%reg, regs=%ld\n", ar->regs);
365                 }
366         }
367
368         /* Check the return address.  It must point within the kernel
369          * and the code at that location must be a valid entry sequence.
370          */
371         if (ar->fp) {
372                 ret_p = ar->fp + ar->setup;
373         }
374         else {
375                 ret_p = ar->end + ar->regs + ar->locals + ar->setup;
376         }
377         ret_p -= 8;
378         if (KDB_DEBUG(ARA)) {
379                 kdb_printf("  ret_p(0)=0x%lx\n", ret_p);
380         }
381         ar->ret = 0;
382         if ((SP_SANITY(ss, ret_p) != 0) &&
383             (ret_p = kdba_find_return(ret_p, ss, symtab))) {
384                 kdb_getword(&(ar->ret), ret_p, 8);
385         }
386         if (KDB_DEBUG(ARA)) {
387                 kdb_printf("  ret_p(1)=0x%lx ret=0x%lx\n", ret_p, ar->ret);
388         }
389         if (ar->ret) {
390                 ar->fp = ret_p - ar->setup + 8; /* "accurate" fp */
391                 ar->start = ret_p + 8;
392                 if (KDB_DEBUG(ARA)) {
393                         kdb_printf("  fp=0x%lx start=0x%lx\n", ar->fp, ar->start);
394                 }
395         }
396         if (oldfp_present) {
397                 if (ar->fp)
398                         kdb_getword(&(ar->oldfp), ar->fp, 8);
399                 if (KDB_DEBUG(ARA)) {
400                         kdb_printf("  oldfp=0x%lx", ar->oldfp);
401                 }
402                 if (ar->oldfp <= ar->fp || (SP_SANITY(ss, ar->oldfp) == 0)) {
403                         ar->oldfp = 0;
404                         if (KDB_DEBUG(ARA)) {
405                                 kdb_printf(" (out of range)");
406                         }
407                 }
408                 if (KDB_DEBUG(ARA)) {
409                         kdb_printf("\n");
410                 }
411         }
412         return(1);
413 }
414
415 kdb_machreg_t
416 kdba_getdr6(void)
417 {
418         return kdba_getdr(6);
419 }
420
421 kdb_machreg_t
422 kdba_getdr7(void)
423 {
424         return kdba_getdr(7);
425 }
426
427 void
428 kdba_putdr6(kdb_machreg_t contents)
429 {
430         kdba_putdr(6, contents);
431 }
432
433 static void
434 kdba_putdr7(kdb_machreg_t contents)
435 {
436         kdba_putdr(7, contents);
437 }
438
439 void
440 kdba_installdbreg(kdb_bp_t *bp)
441 {
442         kdb_machreg_t   dr7;
443
444         dr7 = kdba_getdr7();
445
446         kdba_putdr(bp->bp_hard->bph_reg, bp->bp_addr);
447
448         dr7 |= DR7_GE;
449         if (cpu_has_de)
450                 set_in_cr4(X86_CR4_DE);
451
452         switch (bp->bp_hard->bph_reg){
453         case 0:
454                 DR7_RW0SET(dr7,bp->bp_hard->bph_mode);
455                 DR7_LEN0SET(dr7,bp->bp_hard->bph_length);
456                 DR7_G0SET(dr7);
457                 break;
458         case 1:
459                 DR7_RW1SET(dr7,bp->bp_hard->bph_mode);
460                 DR7_LEN1SET(dr7,bp->bp_hard->bph_length);
461                 DR7_G1SET(dr7);
462                 break;
463         case 2:
464                 DR7_RW2SET(dr7,bp->bp_hard->bph_mode);
465                 DR7_LEN2SET(dr7,bp->bp_hard->bph_length);
466                 DR7_G2SET(dr7);
467                 break;
468         case 3:
469                 DR7_RW3SET(dr7,bp->bp_hard->bph_mode);
470                 DR7_LEN3SET(dr7,bp->bp_hard->bph_length);
471                 DR7_G3SET(dr7);
472                 break;
473         default:
474                 kdb_printf("kdb: Bad debug register!! %ld\n",
475                            bp->bp_hard->bph_reg);
476                 break;
477         }
478
479         kdba_putdr7(dr7);
480         return;
481 }
482
483 void
484 kdba_removedbreg(kdb_bp_t *bp)
485 {
486         int             regnum;
487         kdb_machreg_t   dr7;
488
489         if (!bp->bp_hard)
490                 return;
491
492         regnum = bp->bp_hard->bph_reg;
493
494         dr7 = kdba_getdr7();
495
496         kdba_putdr(regnum, 0);
497
498         switch (regnum) {
499         case 0:
500                 DR7_G0CLR(dr7);
501                 DR7_L0CLR(dr7);
502                 break;
503         case 1:
504                 DR7_G1CLR(dr7);
505                 DR7_L1CLR(dr7);
506                 break;
507         case 2:
508                 DR7_G2CLR(dr7);
509                 DR7_L2CLR(dr7);
510                 break;
511         case 3:
512                 DR7_G3CLR(dr7);
513                 DR7_L3CLR(dr7);
514                 break;
515         default:
516                 kdb_printf("kdb: Bad debug register!! %d\n", regnum);
517                 break;
518         }
519
520         kdba_putdr7(dr7);
521 }
522
523 kdb_machreg_t
524 kdba_getdr(int regnum)
525 {
526         kdb_machreg_t contents = 0;
527         switch(regnum) {
528         case 0:
529                 __asm__ ("movq %%db0,%0\n\t":"=r"(contents));
530                 break;
531         case 1:
532                 __asm__ ("movq %%db1,%0\n\t":"=r"(contents));
533                 break;
534         case 2:
535                 __asm__ ("movq %%db2,%0\n\t":"=r"(contents));
536                 break;
537         case 3:
538                 __asm__ ("movq %%db3,%0\n\t":"=r"(contents));
539                 break;
540         case 4:
541         case 5:
542                 break;
543         case 6:
544                 __asm__ ("movq %%db6,%0\n\t":"=r"(contents));
545                 break;
546         case 7:
547                 __asm__ ("movq %%db7,%0\n\t":"=r"(contents));
548                 break;
549         default:
550                 break;
551         }
552
553         return contents;
554 }
555
556
557 kdb_machreg_t
558 kdb_getcr(int regnum)
559 {
560         kdb_machreg_t contents = 0;
561         switch(regnum) {
562         case 0:
563                 __asm__ ("movq %%cr0,%0\n\t":"=r"(contents));
564                 break;
565         case 1:
566                 break;
567         case 2:
568                 __asm__ ("movq %%cr2,%0\n\t":"=r"(contents));
569                 break;
570         case 3:
571                 __asm__ ("movq %%cr3,%0\n\t":"=r"(contents));
572                 break;
573         case 4:
574                 __asm__ ("movq %%cr4,%0\n\t":"=r"(contents));
575                 break;
576         default:
577                 break;
578         }
579
580         return contents;
581 }
582
583 void
584 kdba_putdr(int regnum, kdb_machreg_t contents)
585 {
586         switch(regnum) {
587         case 0:
588                 __asm__ ("movq %0,%%db0\n\t"::"r"(contents));
589                 break;
590         case 1:
591                 __asm__ ("movq %0,%%db1\n\t"::"r"(contents));
592                 break;
593         case 2:
594                 __asm__ ("movq %0,%%db2\n\t"::"r"(contents));
595                 break;
596         case 3:
597                 __asm__ ("movq %0,%%db3\n\t"::"r"(contents));
598                 break;
599         case 4:
600         case 5:
601                 break;
602         case 6:
603                 __asm__ ("movq %0,%%db6\n\t"::"r"(contents));
604                 break;
605         case 7:
606                 __asm__ ("movq %0,%%db7\n\t"::"r"(contents));
607                 break;
608         default:
609                 break;
610         }
611 }
612
613 /*
614  * kdba_getregcontents
615  *
616  *      Return the contents of the register specified by the
617  *      input string argument.   Return an error if the string
618  *      does not match a machine register.
619  *
620  *      The following pseudo register names are supported:
621  *         &regs         - Prints address of exception frame
622  *         krsp          - Prints kernel stack pointer at time of fault
623  *         crsp          - Prints current kernel stack pointer, inside kdb
624  *         ceflags       - Prints current flags, inside kdb
625  *         %<regname>    - Uses the value of the registers at the
626  *                         last time the user process entered kernel
627  *                         mode, instead of the registers at the time
628  *                         kdb was entered.
629  *
630  * Parameters:
631  *      regname         Pointer to string naming register
632  *      regs            Pointer to structure containing registers.
633  * Outputs:
634  *      *contents       Pointer to unsigned long to recieve register contents
635  * Returns:
636  *      0               Success
637  *      KDB_BADREG      Invalid register name
638  * Locking:
639  *      None.
640  * Remarks:
641  *      If kdb was entered via an interrupt from the kernel itself then
642  *      ss and rsp are *not* on the stack.
643  */
644
645 static struct kdbregs {
646         char   *reg_name;
647         size_t  reg_offset;
648 } kdbreglist[] = {
649         { "r15",        offsetof(struct pt_regs, r15) },
650         { "r14",        offsetof(struct pt_regs, r14) },
651         { "r13",        offsetof(struct pt_regs, r13) },
652         { "r12",        offsetof(struct pt_regs, r12) },
653         { "rbp",        offsetof(struct pt_regs, rbp) },
654         { "rbx",        offsetof(struct pt_regs, rbx) },
655         { "r11",        offsetof(struct pt_regs, r11) },
656         { "r10",        offsetof(struct pt_regs, r10) },
657         { "r9",         offsetof(struct pt_regs, r9) },
658         { "r8",         offsetof(struct pt_regs, r8) },
659         { "rax",        offsetof(struct pt_regs, rax) },
660         { "rcx",        offsetof(struct pt_regs, rcx) },
661         { "rdx",        offsetof(struct pt_regs, rdx) },
662         { "rsi",        offsetof(struct pt_regs, rsi) },
663         { "rdi",        offsetof(struct pt_regs, rdi) },
664         { "orig_rax",   offsetof(struct pt_regs, orig_rax) },
665         { "rip",        offsetof(struct pt_regs, rip) },
666         { "cs",         offsetof(struct pt_regs, cs) },
667         { "eflags",     offsetof(struct pt_regs, eflags) },
668         { "rsp",        offsetof(struct pt_regs, rsp) },
669         { "ss",         offsetof(struct pt_regs, ss) },
670 };
671
672 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
673
674 static struct kdbregs dbreglist[] = {
675         { "dr0",        0 },
676         { "dr1",        1 },
677         { "dr2",        2 },
678         { "dr3",        3 },
679         { "dr6",        6 },
680         { "dr7",        7 },
681 };
682
683 static const int ndbreglist = sizeof(dbreglist) / sizeof(struct kdbregs);
684
685 int
686 kdba_getregcontents(const char *regname,
687                     struct pt_regs *regs,
688                     kdb_machreg_t *contents)
689 {
690         int i;
691
692         if (strcmp(regname, "&regs") == 0) {
693                 *contents = (unsigned long)regs;
694                 return 0;
695         }
696
697         if (strcmp(regname, "krsp") == 0) {
698                 *contents = (unsigned long)regs + sizeof(struct pt_regs);
699                 if ((regs->cs & 0xffff) == __KERNEL_CS) {
700                         /* rsp and ss are not on stack */
701                         *contents -= 2*4;
702                 }
703                 return 0;
704         }
705
706         if (strcmp(regname, "crsp") == 0) {
707                 asm volatile("movq %%rsp,%0":"=m" (*contents));
708                 return 0;
709         }
710
711         if (strcmp(regname, "ceflags") == 0) {
712                 unsigned long flags;
713                 local_save_flags(flags);
714                 *contents = flags;
715                 return 0;
716         }
717
718         if (regname[0] == '%') {
719                 /* User registers:  %%r[a-c]x, etc */
720                 regname++;
721                 regs = (struct pt_regs *)
722                         (current->thread.rsp0 - sizeof(struct pt_regs));
723         }
724
725         for (i=0; i<nkdbreglist; i++) {
726                 if (strnicmp(kdbreglist[i].reg_name,
727                              regname,
728                              strlen(regname)) == 0)
729                         break;
730         }
731
732         if ((i < nkdbreglist)
733          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
734                 if ((regs->cs & 0xffff) == __KERNEL_CS) {
735                         /* No cpl switch, rsp is not on stack */
736                         if (strcmp(kdbreglist[i].reg_name, "rsp") == 0) {
737                                 *contents = (kdb_machreg_t)regs +
738                                         sizeof(struct pt_regs) - 2*8;
739                                 return(0);
740                         }
741                         if (strcmp(kdbreglist[i].reg_name, "ss") == 0) {
742                                 kdb_machreg_t r;
743
744                                 r = (kdb_machreg_t)regs +
745                                         sizeof(struct pt_regs) - 2*8;
746                                 *contents = (kdb_machreg_t)SS(r);       /* XXX */
747                                 return(0);
748                         }
749                 }
750                 *contents = *(unsigned long *)((unsigned long)regs +
751                                 kdbreglist[i].reg_offset);
752                 return(0);
753         }
754
755         for (i=0; i<ndbreglist; i++) {
756                 if (strnicmp(dbreglist[i].reg_name,
757                              regname,
758                              strlen(regname)) == 0)
759                         break;
760         }
761
762         if ((i < ndbreglist)
763          && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
764                 *contents = kdba_getdr(dbreglist[i].reg_offset);
765                 return 0;
766         }
767         return KDB_BADREG;
768 }
769
770 /*
771  * kdba_setregcontents
772  *
773  *      Set the contents of the register specified by the
774  *      input string argument.   Return an error if the string
775  *      does not match a machine register.
776  *
777  *      Supports modification of user-mode registers via
778  *      %<register-name>
779  *
780  * Parameters:
781  *      regname         Pointer to string naming register
782  *      regs            Pointer to structure containing registers.
783  *      contents        Unsigned long containing new register contents
784  * Outputs:
785  * Returns:
786  *      0               Success
787  *      KDB_BADREG      Invalid register name
788  * Locking:
789  *      None.
790  * Remarks:
791  */
792
793 int
794 kdba_setregcontents(const char *regname,
795                   struct pt_regs *regs,
796                   unsigned long contents)
797 {
798         int i;
799
800         if (regname[0] == '%') {
801                 regname++;
802                 regs = (struct pt_regs *)
803                         (current->thread.rsp0 - sizeof(struct pt_regs));
804         }
805
806         for (i=0; i<nkdbreglist; i++) {
807                 if (strnicmp(kdbreglist[i].reg_name,
808                              regname,
809                              strlen(regname)) == 0)
810                         break;
811         }
812
813         if ((i < nkdbreglist)
814          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
815                 *(unsigned long *)((unsigned long)regs
816                                    + kdbreglist[i].reg_offset) = contents;
817                 return 0;
818         }
819
820         for (i=0; i<ndbreglist; i++) {
821                 if (strnicmp(dbreglist[i].reg_name,
822                              regname,
823                              strlen(regname)) == 0)
824                         break;
825         }
826
827         if ((i < ndbreglist)
828          && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
829                 kdba_putdr(dbreglist[i].reg_offset, contents);
830                 return 0;
831         }
832
833         return KDB_BADREG;
834 }
835
836 /*
837  * kdba_dumpregs
838  *
839  *      Dump the specified register set to the display.
840  *
841  * Parameters:
842  *      regs            Pointer to structure containing registers.
843  *      type            Character string identifying register set to dump
844  *      extra           string further identifying register (optional)
845  * Outputs:
846  * Returns:
847  *      0               Success
848  * Locking:
849  *      None.
850  * Remarks:
851  *      This function will dump the general register set if the type
852  *      argument is NULL (struct pt_regs).   The alternate register
853  *      set types supported by this function:
854  *
855  *      d               Debug registers
856  *      c               Control registers
857  *      u               User registers at most recent entry to kernel
858  * Following not yet implemented:
859  *      m               Model Specific Registers (extra defines register #)
860  *      r               Memory Type Range Registers (extra defines register)
861  */
862
863 int
864 kdba_dumpregs(struct pt_regs *regs,
865             const char *type,
866             const char *extra)
867 {
868         int i;
869         int count = 0;
870
871         if (type
872          && (type[0] == 'u')) {
873                 type = NULL;
874                 regs = (struct pt_regs *)
875                         (current->thread.rsp0 - sizeof(struct pt_regs));
876         }
877
878         if (type == NULL) {
879                 struct kdbregs *rlp;
880                 kdb_machreg_t contents;
881
882                 for (i=0, rlp=kdbreglist; i<nkdbreglist; i++,rlp++) {
883                         kdb_printf("%8s = ", rlp->reg_name);
884                         kdba_getregcontents(rlp->reg_name, regs, &contents);
885                         kdb_printf("0x%016lx ", contents);
886                         if ((++count % 2) == 0)
887                                 kdb_printf("\n");
888                 }
889
890                 kdb_printf("&regs = 0x%p\n", regs);
891
892                 return 0;
893         }
894
895         switch (type[0]) {
896         case 'd':
897         {
898                 unsigned long dr[8];
899
900                 for(i=0; i<8; i++) {
901                         if ((i == 4) || (i == 5)) continue;
902                         dr[i] = kdba_getdr(i);
903                 }
904                 kdb_printf("dr0 = 0x%08lx  dr1 = 0x%08lx  dr2 = 0x%08lx  dr3 = 0x%08lx\n",
905                            dr[0], dr[1], dr[2], dr[3]);
906                 kdb_printf("dr6 = 0x%08lx  dr7 = 0x%08lx\n",
907                            dr[6], dr[7]);
908                 return 0;
909         }
910         case 'c':
911         {
912                 unsigned long cr[5];
913
914                 for (i=0; i<5; i++) {
915                         cr[i] = kdb_getcr(i);
916                 }
917                 kdb_printf("cr0 = 0x%08lx  cr1 = 0x%08lx  cr2 = 0x%08lx  cr3 = 0x%08lx\ncr4 = 0x%08lx\n",
918                            cr[0], cr[1], cr[2], cr[3], cr[4]);
919                 return 0;
920         }
921         case 'm':
922                 break;
923         case 'r':
924                 break;
925         default:
926                 return KDB_BADREG;
927         }
928
929         /* NOTREACHED */
930         return 0;
931 }
932
933 kdb_machreg_t
934 kdba_getpc(struct pt_regs *regs)
935 {
936         return regs->rip;
937 }
938
939 int
940 kdba_setpc(struct pt_regs *regs, kdb_machreg_t newpc)
941 {
942         if (KDB_NULL_REGS(regs))
943                 return KDB_BADREG;
944         regs->rip = newpc;
945         KDB_STATE_SET(IP_ADJUSTED);
946         return 0;
947 }
948
949 /*
950  * kdba_main_loop
951  *
952  *      Do any architecture specific set up before entering the main kdb loop.
953  *      The primary function of this routine is to make all processes look the
954  *      same to kdb, kdb must be able to list a process without worrying if the
955  *      process is running or blocked, so make all process look as though they
956  *      are blocked.
957  *
958  * Inputs:
959  *      reason          The reason KDB was invoked
960  *      error           The hardware-defined error code
961  *      error2          kdb's current reason code.  Initially error but can change
962  *                      acording to kdb state.
963  *      db_result       Result from break or debug point.
964  *      ef              The exception frame at time of fault/breakpoint.  If reason
965  *                      is KDB_REASON_SILENT then ef is NULL, otherwise it should
966  *                      always be valid.
967  * Returns:
968  *      0       KDB was invoked for an event which it wasn't responsible
969  *      1       KDB handled the event for which it was invoked.
970  * Outputs:
971  *      Sets rip and rsp in current->thread.
972  * Locking:
973  *      None.
974  * Remarks:
975  *      none.
976  */
977
978 int
979 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
980                kdb_dbtrap_t db_result, struct pt_regs *regs)
981 {
982         int ret;
983
984         if (regs)
985                 kdba_getregcontents("rsp", regs, &(current->thread.rsp));
986         kdb_save_running(regs);
987         ret = kdb_main_loop(reason, reason2, error, db_result, regs);
988         kdb_unsave_running(regs);
989         return ret;
990 }
991
992 void
993 kdba_disableint(kdb_intstate_t *state)
994 {
995         unsigned long *fp = (unsigned long *)state;
996         unsigned long flags;
997
998         local_irq_save(flags);
999         *fp = flags;
1000 }
1001
1002 void
1003 kdba_restoreint(kdb_intstate_t *state)
1004 {
1005         unsigned long flags = *(unsigned long *)state;
1006         local_irq_restore(flags);
1007 }
1008
1009 void
1010 kdba_setsinglestep(struct pt_regs *regs)
1011 {
1012         if (KDB_NULL_REGS(regs))
1013                 return;
1014         if (regs->eflags & EF_IE)
1015                 KDB_STATE_SET(A_IF);
1016         else
1017                 KDB_STATE_CLEAR(A_IF);
1018         regs->eflags = (regs->eflags | EF_TF) & ~EF_IE;
1019 }
1020
1021 void
1022 kdba_clearsinglestep(struct pt_regs *regs)
1023 {
1024         if (KDB_NULL_REGS(regs))
1025                 return;
1026         if (KDB_STATE(A_IF))
1027                 regs->eflags |= EF_IE;
1028         else
1029                 regs->eflags &= ~EF_IE;
1030 }
1031
1032 #if 0
1033 #ifdef KDB_HAVE_LONGJMP
1034 int asmlinkage
1035 kdba_setjmp(kdb_jmp_buf *jb)
1036 {
1037 #if defined(CONFIG_FRAME_POINTER)
1038         __asm__("movq %rbx, (0*8)(%rdi);"
1039                 "movq %rbp, (1*8)(%rdi);"
1040                 "movq %r12, (2*8)(%rdi);"
1041                 "movq %r13, (3*8)(%rdi);"
1042                 "movq %r14, (4*8)(%rdi);"
1043                 "movq %r15, (5*8)(%rdi);"
1044                 "leaq 16(%rsp), %rdx;"
1045                 "movq %rdx, (6*8)(%rdi);"
1046                 "movq (%rsp), %rax;"
1047                 "movq %rax, (7*8)(%rdi)");
1048 #else    /* CONFIG_FRAME_POINTER */
1049         __asm__("movq %rbx, (0*8)(%rdi);"
1050                 "movq %rbp, (1*8)(%rdi);"
1051                 "movq %r12, (2*8)(%rdi);"
1052                 "movq %r13, (3*8)(%rdi);"
1053                 "movq %r14, (4*8)(%rdi);"
1054                 "movq %r15, (5*8)(%rdi);"
1055                 "leaq 8(%rsp), %rdx;"
1056                 "movq %rdx, (6*8)(%rdi);"
1057                 "movq (%rsp), %rax;"
1058                 "movq %rax, (7*8)(%rdi)");
1059 #endif   /* CONFIG_FRAME_POINTER */
1060         KDB_STATE_SET(LONGJMP);
1061         return 0;
1062 }
1063
1064 void asmlinkage
1065 kdba_longjmp(kdb_jmp_buf *jb, int reason)
1066 {
1067 #if defined(CONFIG_FRAME_POINTER)
1068         __asm__("movq (0*8)(%rdi),%rbx;"
1069                 "movq (1*8)(%rdi),%rbp;"
1070                 "movq (2*8)(%rdi),%r12;"
1071                 "movq (3*8)(%rdi),%r13;"
1072                 "movq (4*8)(%rdi),%r14;"
1073                 "movq (5*8)(%rdi),%r15;"
1074                 "test %esi,%esi;"
1075                 "mov $01,%eax;"
1076                 "cmove %eax,%esi;"
1077                 "mov %esi, %eax;"
1078                 "movq (7*8)(%rdi),%rdx;"
1079                 "movq (6*8)(%rdi),%rsp;"
1080                 "jmpq *%rdx");
1081 #else    /* CONFIG_FRAME_POINTER */
1082         __asm__("movq (0*8)(%rdi),%rbx;"
1083                 "movq (1*8)(%rdi),%rbp;"
1084                 "movq (2*8)(%rdi),%r12;"
1085                 "movq (3*8)(%rdi),%r13;"
1086                 "movq (4*8)(%rdi),%r14;"
1087                 "movq (5*8)(%rdi),%r15;"
1088                 "test %esi,%esi;"
1089                 "mov $01,%eax;"
1090                 "cmove %eax,%esi;"
1091                 "mov %esi, %eax;"
1092                 "movq (7*8)(%rdi),%rdx;"
1093                 "movq (6*8)(%rdi),%rsp;"
1094                 "jmpq *%rdx");
1095 #endif   /* CONFIG_FRAME_POINTER */
1096 }
1097 #endif  /* KDB_HAVE_LONGJMP */
1098 #endif
1099
1100 /*
1101  * kdba_enable_lbr
1102  *
1103  *      Enable last branch recording.
1104  *
1105  * Parameters:
1106  *      None.
1107  * Returns:
1108  *      None
1109  * Locking:
1110  *      None
1111  * Remarks:
1112  *      None.
1113  */
1114
1115 static unsigned char lbr_warned;
1116
1117 void
1118 kdba_enable_lbr(void)
1119 {
1120         u32  lv, hv;
1121
1122         if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability)) {
1123                 if (lbr_warned) {
1124                         kdb_printf("kdb: machine does not support last branch recording\n");
1125                         lbr_warned = 1;
1126                 }
1127                 return;
1128         }
1129         rdmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1130         lv |= 0x1;      /* Set LBR enable */
1131         wrmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1132 }
1133
1134 /*
1135  * kdba_disable_lbr
1136  *
1137  *      disable last branch recording.
1138  *
1139  * Parameters:
1140  *      None.
1141  * Returns:
1142  *      None
1143  * Locking:
1144  *      None
1145  * Remarks:
1146  *      None.
1147  */
1148
1149 void
1150 kdba_disable_lbr(void)
1151 {
1152         u32  lv, hv;
1153
1154         if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability)) {
1155                 if (lbr_warned) {
1156                         kdb_printf("kdb: machine does not support last branch recording\n");
1157                         lbr_warned = 1;
1158                 }
1159                 return;
1160         }
1161         rdmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1162         lv &= ~0x1;     /* Set LBR disable */
1163         wrmsr(MSR_IA32_DEBUGCTLMSR, lv, hv);
1164 }
1165
1166 /*
1167  * kdba_print_lbr
1168  *
1169  *      Print last branch and last exception addresses
1170  *
1171  * Parameters:
1172  *      None.
1173  * Returns:
1174  *      None
1175  * Locking:
1176  *      None
1177  * Remarks:
1178  *      None.
1179  */
1180
1181 void
1182 kdba_print_lbr(void)
1183 {
1184         u32  from, to, dummy;
1185
1186         if (!test_bit(X86_FEATURE_MCA, &boot_cpu_data.x86_capability))
1187                 return;
1188
1189         rdmsr(MSR_IA32_LASTBRANCHFROMIP, from, dummy);
1190         rdmsr(MSR_IA32_LASTBRANCHTOIP, to, dummy);
1191         kdb_printf("Last Branch IP, from: ");
1192         kdb_symbol_print(from, NULL, KDB_SP_DEFAULT);
1193         kdb_printf(" to: ");
1194         kdb_symbol_print(to, NULL, KDB_SP_DEFAULT);
1195         kdb_printf("\n");
1196         rdmsr(MSR_IA32_LASTINTFROMIP, from, dummy);
1197         rdmsr(MSR_IA32_LASTINTTOIP, to, dummy);
1198         kdb_printf("Last Int IP, from: ");
1199         kdb_symbol_print(from, NULL, KDB_SP_DEFAULT);
1200         kdb_printf(" to: ");
1201         kdb_symbol_print(to, NULL, KDB_SP_DEFAULT);
1202         kdb_printf("\n");
1203 }
1204
1205 /*
1206  * kdba_entry
1207  *
1208  *      This is the interface routine between
1209  *      the notifier die_chain and kdb
1210  */
1211 static int kdba_entry( struct notifier_block *b, unsigned long val, void *v)
1212 {
1213         struct die_args *args = v;
1214         int err, trap, ret = 0;
1215         struct pt_regs *regs;
1216
1217         regs = args->regs;
1218         err  = args->err;
1219         trap  = args->trapnr;
1220         switch (val){
1221 #if defined(CONFIG_SMP)
1222                 case DIE_NMI_IPI:
1223                         ret = kdb_ipi(regs, NULL);
1224                         break;
1225 #endif
1226                 case DIE_OOPS:
1227                         ret = kdb(KDB_REASON_OOPS, err, regs);
1228                         break;
1229                 case DIE_CALL:
1230                         ret = kdb(KDB_REASON_ENTER, err, regs);
1231                         break;
1232                 case DIE_DEBUG:
1233                         ret = kdb(KDB_REASON_DEBUG, err, regs);
1234                         break;
1235                 case DIE_INT3:
1236                          ret = kdb(KDB_REASON_BREAK, err, regs);
1237                         // falls thru
1238                 default:
1239                         break;
1240         }
1241         return (ret ? NOTIFY_STOP : NOTIFY_DONE);
1242 }
1243
1244 /*
1245  * notifier block for kdb entry
1246  */
1247 static struct notifier_block kdba_notifier = {
1248         .notifier_call = kdba_entry
1249 };
1250
1251 /*
1252  * kdba_init
1253  *
1254  *      Architecture specific initialization.
1255  *
1256  * Parameters:
1257  *      None.
1258  * Returns:
1259  *      None.
1260  * Locking:
1261  *      None.
1262  * Remarks:
1263  *      None.
1264  */
1265
1266 void __init
1267 kdba_init(void)
1268 {
1269         kdba_enable_lbr();
1270         notifier_chain_register(&die_chain, &kdba_notifier);
1271         return;
1272 }
1273
1274 /*
1275  * kdba_adjust_ip
1276  *
1277  *      Architecture specific adjustment of instruction pointer before leaving
1278  *      kdb.
1279  *
1280  * Parameters:
1281  *      reason          The reason KDB was invoked
1282  *      error           The hardware-defined error code
1283  *      ef              The exception frame at time of fault/breakpoint.  If reason
1284  *                      is KDB_REASON_SILENT then ef is NULL, otherwise it should
1285  *                      always be valid.
1286  * Returns:
1287  *      None.
1288  * Locking:
1289  *      None.
1290  * Remarks:
1291  *      noop on ix86.
1292  */
1293
1294 void
1295 kdba_adjust_ip(kdb_reason_t reason, int error, struct pt_regs *ef)
1296 {
1297         return;
1298 }
1299
1300 /* dummy code to make common layer happy for now */
1301 void
1302 kdba_enable_mce(void)
1303 {
1304 }
1305  
1306 void
1307 kdba_set_current_task(const struct task_struct *p)
1308 {
1309         kdb_current_task = p;
1310         if (kdb_task_has_cpu(p)) {
1311                 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
1312                 kdb_current_regs = krp->regs;
1313                 return;
1314         }
1315         kdb_current_regs = NULL;
1316 }
1317
1318 #include <asm/mach_apic.h>
1319 #include <asm/hw_irq.h>
1320 void smp_kdb_stop(void)
1321 {
1322         send_IPI_allbutself(NMI_VECTOR);
1323 }