- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kdb / kdbasupport_32.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-2006 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
11 #include <linux/string.h>
12 #include <linux/stddef.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/ptrace.h>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <linux/hardirq.h>
21 #include <linux/kdb.h>
22 #include <linux/kdbprivate.h>
23
24 #include <asm/processor.h>
25 #include <asm/msr.h>
26 #include <asm/uaccess.h>
27 #include <asm/desc.h>
28
29 static kdb_machreg_t
30 kdba_getcr(int regnum)
31 {
32         kdb_machreg_t contents = 0;
33         switch(regnum) {
34         case 0:
35                 __asm__ ("movl %%cr0,%0\n\t":"=r"(contents));
36                 break;
37         case 1:
38                 break;
39         case 2:
40                 __asm__ ("movl %%cr2,%0\n\t":"=r"(contents));
41                 break;
42         case 3:
43                 __asm__ ("movl %%cr3,%0\n\t":"=r"(contents));
44                 break;
45         case 4:
46                 __asm__ ("movl %%cr4,%0\n\t":"=r"(contents));
47                 break;
48         default:
49                 break;
50         }
51
52         return contents;
53 }
54
55 static void
56 kdba_putdr(int regnum, kdb_machreg_t contents)
57 {
58         switch(regnum) {
59         case 0:
60                 __asm__ ("movl %0,%%db0\n\t"::"r"(contents));
61                 break;
62         case 1:
63                 __asm__ ("movl %0,%%db1\n\t"::"r"(contents));
64                 break;
65         case 2:
66                 __asm__ ("movl %0,%%db2\n\t"::"r"(contents));
67                 break;
68         case 3:
69                 __asm__ ("movl %0,%%db3\n\t"::"r"(contents));
70                 break;
71         case 4:
72         case 5:
73                 break;
74         case 6:
75                 __asm__ ("movl %0,%%db6\n\t"::"r"(contents));
76                 break;
77         case 7:
78                 __asm__ ("movl %0,%%db7\n\t"::"r"(contents));
79                 break;
80         default:
81                 break;
82         }
83 }
84
85 static kdb_machreg_t
86 kdba_getdr(int regnum)
87 {
88         kdb_machreg_t contents = 0;
89         switch(regnum) {
90         case 0:
91                 __asm__ ("movl %%db0,%0\n\t":"=r"(contents));
92                 break;
93         case 1:
94                 __asm__ ("movl %%db1,%0\n\t":"=r"(contents));
95                 break;
96         case 2:
97                 __asm__ ("movl %%db2,%0\n\t":"=r"(contents));
98                 break;
99         case 3:
100                 __asm__ ("movl %%db3,%0\n\t":"=r"(contents));
101                 break;
102         case 4:
103         case 5:
104                 break;
105         case 6:
106                 __asm__ ("movl %%db6,%0\n\t":"=r"(contents));
107                 break;
108         case 7:
109                 __asm__ ("movl %%db7,%0\n\t":"=r"(contents));
110                 break;
111         default:
112                 break;
113         }
114
115         return contents;
116 }
117
118 kdb_machreg_t
119 kdba_getdr6(void)
120 {
121         return kdba_getdr(6);
122 }
123
124 kdb_machreg_t
125 kdba_getdr7(void)
126 {
127         return kdba_getdr(7);
128 }
129
130 void
131 kdba_putdr6(kdb_machreg_t contents)
132 {
133         kdba_putdr(6, contents);
134 }
135
136 static void
137 kdba_putdr7(kdb_machreg_t contents)
138 {
139         kdba_putdr(7, contents);
140 }
141
142 void
143 kdba_installdbreg(kdb_bp_t *bp)
144 {
145         kdb_machreg_t dr7;
146
147         dr7 = kdba_getdr7();
148
149         kdba_putdr(bp->bp_hard->bph_reg, bp->bp_addr);
150
151         dr7 |= DR7_GE;
152         if (cpu_has_de)
153                 set_in_cr4(X86_CR4_DE);
154
155         switch (bp->bp_hard->bph_reg){
156         case 0:
157                 DR7_RW0SET(dr7,bp->bp_hard->bph_mode);
158                 DR7_LEN0SET(dr7,bp->bp_hard->bph_length);
159                 DR7_G0SET(dr7);
160                 break;
161         case 1:
162                 DR7_RW1SET(dr7,bp->bp_hard->bph_mode);
163                 DR7_LEN1SET(dr7,bp->bp_hard->bph_length);
164                 DR7_G1SET(dr7);
165                 break;
166         case 2:
167                 DR7_RW2SET(dr7,bp->bp_hard->bph_mode);
168                 DR7_LEN2SET(dr7,bp->bp_hard->bph_length);
169                 DR7_G2SET(dr7);
170                 break;
171         case 3:
172                 DR7_RW3SET(dr7,bp->bp_hard->bph_mode);
173                 DR7_LEN3SET(dr7,bp->bp_hard->bph_length);
174                 DR7_G3SET(dr7);
175                 break;
176         default:
177                 kdb_printf("kdb: Bad debug register!! %ld\n",
178                            bp->bp_hard->bph_reg);
179                 break;
180         }
181
182         kdba_putdr7(dr7);
183         return;
184 }
185
186 void
187 kdba_removedbreg(kdb_bp_t *bp)
188 {
189         int regnum;
190         kdb_machreg_t dr7;
191
192         if (!bp->bp_hard)
193                 return;
194
195         regnum = bp->bp_hard->bph_reg;
196
197         dr7 = kdba_getdr7();
198
199         kdba_putdr(regnum, 0);
200
201         switch (regnum) {
202         case 0:
203                 DR7_G0CLR(dr7);
204                 DR7_L0CLR(dr7);
205                 break;
206         case 1:
207                 DR7_G1CLR(dr7);
208                 DR7_L1CLR(dr7);
209                 break;
210         case 2:
211                 DR7_G2CLR(dr7);
212                 DR7_L2CLR(dr7);
213                 break;
214         case 3:
215                 DR7_G3CLR(dr7);
216                 DR7_L3CLR(dr7);
217                 break;
218         default:
219                 kdb_printf("kdb: Bad debug register!! %d\n", regnum);
220                 break;
221         }
222
223         kdba_putdr7(dr7);
224 }
225
226
227 /*
228  * kdba_getregcontents
229  *
230  *      Return the contents of the register specified by the
231  *      input string argument.   Return an error if the string
232  *      does not match a machine register.
233  *
234  *      The following pseudo register names are supported:
235  *         &regs         - Prints address of exception frame
236  *         kesp          - Prints kernel stack pointer at time of fault
237  *         cesp          - Prints current kernel stack pointer, inside kdb
238  *         ceflags       - Prints current flags, inside kdb
239  *         %<regname>    - Uses the value of the registers at the
240  *                         last time the user process entered kernel
241  *                         mode, instead of the registers at the time
242  *                         kdb was entered.
243  *
244  * Parameters:
245  *      regname         Pointer to string naming register
246  *      regs            Pointer to structure containing registers.
247  * Outputs:
248  *      *contents       Pointer to unsigned long to recieve register contents
249  * Returns:
250  *      0               Success
251  *      KDB_BADREG      Invalid register name
252  * Locking:
253  *      None.
254  * Remarks:
255  *      If kdb was entered via an interrupt from the kernel itself then
256  *      ss and esp are *not* on the stack.
257  */
258
259 static struct kdbregs {
260         char   *reg_name;
261         size_t  reg_offset;
262 } kdbreglist[] = {
263         { "eax",        offsetof(struct pt_regs, ax) },
264         { "ebx",        offsetof(struct pt_regs, bx) },
265         { "ecx",        offsetof(struct pt_regs, cx) },
266         { "edx",        offsetof(struct pt_regs, dx) },
267
268         { "esi",        offsetof(struct pt_regs, si) },
269         { "edi",        offsetof(struct pt_regs, di) },
270         { "esp",        offsetof(struct pt_regs, sp) },
271         { "eip",        offsetof(struct pt_regs, ip) },
272
273         { "ebp",        offsetof(struct pt_regs, bp) },
274         { "xss",        offsetof(struct pt_regs, ss) },
275         { "xcs",        offsetof(struct pt_regs, cs) },
276         { "eflags",     offsetof(struct pt_regs, flags) },
277
278         { "xds",        offsetof(struct pt_regs, ds) },
279         { "xes",        offsetof(struct pt_regs, es) },
280         { "origeax",    offsetof(struct pt_regs, orig_ax) },
281
282 };
283
284 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
285
286 static struct kdbregs dbreglist[] = {
287         { "dr0",        0 },
288         { "dr1",        1 },
289         { "dr2",        2 },
290         { "dr3",        3 },
291         { "dr6",        6 },
292         { "dr7",        7 },
293 };
294
295 static const int ndbreglist = sizeof(dbreglist) / sizeof(struct kdbregs);
296
297 int
298 kdba_getregcontents(const char *regname,
299                     struct pt_regs *regs,
300                     kdb_machreg_t *contents)
301 {
302         int i;
303
304         if (strcmp(regname, "cesp") == 0) {
305                 asm volatile("movl %%esp,%0":"=m" (*contents));
306                 return 0;
307         }
308
309         if (strcmp(regname, "ceflags") == 0) {
310                 unsigned long flags;
311                 local_save_flags(flags);
312                 *contents = flags;
313                 return 0;
314         }
315
316         if (regname[0] == '%') {
317                 /* User registers:  %%e[a-c]x, etc */
318                 regname++;
319                 regs = (struct pt_regs *)
320                         (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
321         }
322
323         for (i=0; i<ndbreglist; i++) {
324                 if (strnicmp(dbreglist[i].reg_name,
325                              regname,
326                              strlen(regname)) == 0)
327                         break;
328         }
329
330         if ((i < ndbreglist)
331          && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
332                 *contents = kdba_getdr(dbreglist[i].reg_offset);
333                 return 0;
334         }
335
336         if (!regs) {
337                 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
338                 return KDB_BADREG;
339         }
340
341         if (strcmp(regname, "&regs") == 0) {
342                 *contents = (unsigned long)regs;
343                 return 0;
344         }
345
346         if (strcmp(regname, "kesp") == 0) {
347                 *contents = (unsigned long)regs + sizeof(struct pt_regs);
348                 if ((regs->cs & 0xffff) == __KERNEL_CS) {
349                         /* esp and ss are not on stack */
350                         *contents -= 2*4;
351                 }
352                 return 0;
353         }
354
355         for (i=0; i<nkdbreglist; i++) {
356                 if (strnicmp(kdbreglist[i].reg_name,
357                              regname,
358                              strlen(regname)) == 0)
359                         break;
360         }
361
362         if ((i < nkdbreglist)
363          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
364                 if ((regs->cs & 0xffff) == __KERNEL_CS) {
365                         /* No cpl switch, esp and ss are not on stack */
366                         if (strcmp(kdbreglist[i].reg_name, "esp") == 0) {
367                                 *contents = (kdb_machreg_t)regs +
368                                         sizeof(struct pt_regs) - 2*4;
369                                 return(0);
370                         }
371                         if (strcmp(kdbreglist[i].reg_name, "xss") == 0) {
372                                 asm volatile(
373                                         "pushl %%ss\n"
374                                         "popl %0\n"
375                                         :"=m" (*contents));
376                                 return(0);
377                         }
378                 }
379                 *contents = *(unsigned long *)((unsigned long)regs +
380                                 kdbreglist[i].reg_offset);
381                 return(0);
382         }
383
384         return KDB_BADREG;
385 }
386
387 /*
388  * kdba_setregcontents
389  *
390  *      Set the contents of the register specified by the
391  *      input string argument.   Return an error if the string
392  *      does not match a machine register.
393  *
394  *      Supports modification of user-mode registers via
395  *      %<register-name>
396  *
397  * Parameters:
398  *      regname         Pointer to string naming register
399  *      regs            Pointer to structure containing registers.
400  *      contents        Unsigned long containing new register contents
401  * Outputs:
402  * Returns:
403  *      0               Success
404  *      KDB_BADREG      Invalid register name
405  * Locking:
406  *      None.
407  * Remarks:
408  */
409
410 int
411 kdba_setregcontents(const char *regname,
412                   struct pt_regs *regs,
413                   unsigned long contents)
414 {
415         int i;
416
417         if (regname[0] == '%') {
418                 regname++;
419                 regs = (struct pt_regs *)
420                         (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
421         }
422
423         for (i=0; i<ndbreglist; i++) {
424                 if (strnicmp(dbreglist[i].reg_name,
425                              regname,
426                              strlen(regname)) == 0)
427                         break;
428         }
429
430         if ((i < ndbreglist)
431          && (strlen(dbreglist[i].reg_name) == strlen(regname))) {
432                 kdba_putdr(dbreglist[i].reg_offset, contents);
433                 return 0;
434         }
435
436         if (!regs) {
437                 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
438                 return KDB_BADREG;
439         }
440
441         for (i=0; i<nkdbreglist; i++) {
442                 if (strnicmp(kdbreglist[i].reg_name,
443                              regname,
444                              strlen(regname)) == 0)
445                         break;
446         }
447
448         if ((i < nkdbreglist)
449          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
450                 *(unsigned long *)((unsigned long)regs
451                                    + kdbreglist[i].reg_offset) = contents;
452                 return 0;
453         }
454
455         return KDB_BADREG;
456 }
457
458 /*
459  * kdba_dumpregs
460  *
461  *      Dump the specified register set to the display.
462  *
463  * Parameters:
464  *      regs            Pointer to structure containing registers.
465  *      type            Character string identifying register set to dump
466  *      extra           string further identifying register (optional)
467  * Outputs:
468  * Returns:
469  *      0               Success
470  * Locking:
471  *      None.
472  * Remarks:
473  *      This function will dump the general register set if the type
474  *      argument is NULL (struct pt_regs).   The alternate register
475  *      set types supported by this function:
476  *
477  *      d               Debug registers
478  *      c               Control registers
479  *      u               User registers at most recent entry to kernel
480  *                      for the process currently selected with "pid" command.
481  * Following not yet implemented:
482  *      r               Memory Type Range Registers (extra defines register)
483  *
484  * MSR on i386/x86_64 are handled by rdmsr/wrmsr commands.
485  */
486
487 int
488 kdba_dumpregs(struct pt_regs *regs,
489             const char *type,
490             const char *extra)
491 {
492         int i;
493         int count = 0;
494
495         if (type
496          && (type[0] == 'u')) {
497                 type = NULL;
498                 regs = (struct pt_regs *)
499                         (kdb_current_task->thread.sp0 - sizeof(struct pt_regs));
500         }
501
502         if (type == NULL) {
503                 struct kdbregs *rlp;
504                 kdb_machreg_t contents;
505
506                 if (!regs) {
507                         kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
508                         return KDB_BADREG;
509                 }
510
511                 for (i=0, rlp=kdbreglist; i<nkdbreglist; i++,rlp++) {
512                         kdb_printf("%s = ", rlp->reg_name);
513                         kdba_getregcontents(rlp->reg_name, regs, &contents);
514                         kdb_printf("0x%08lx ", contents);
515                         if ((++count % 4) == 0)
516                                 kdb_printf("\n");
517                 }
518
519                 kdb_printf("&regs = 0x%p\n", regs);
520
521                 return 0;
522         }
523
524         switch (type[0]) {
525         case 'd':
526         {
527                 unsigned long dr[8];
528
529                 for(i=0; i<8; i++) {
530                         if ((i == 4) || (i == 5)) continue;
531                         dr[i] = kdba_getdr(i);
532                 }
533                 kdb_printf("dr0 = 0x%08lx  dr1 = 0x%08lx  dr2 = 0x%08lx  dr3 = 0x%08lx\n",
534                            dr[0], dr[1], dr[2], dr[3]);
535                 kdb_printf("dr6 = 0x%08lx  dr7 = 0x%08lx\n",
536                            dr[6], dr[7]);
537                 return 0;
538         }
539         case 'c':
540         {
541                 unsigned long cr[5];
542
543                 for (i=0; i<5; i++) {
544                         cr[i] = kdba_getcr(i);
545                 }
546                 kdb_printf("cr0 = 0x%08lx  cr1 = 0x%08lx  cr2 = 0x%08lx  cr3 = 0x%08lx\ncr4 = 0x%08lx\n",
547                            cr[0], cr[1], cr[2], cr[3], cr[4]);
548                 return 0;
549         }
550         case 'r':
551                 break;
552         default:
553                 return KDB_BADREG;
554         }
555
556         /* NOTREACHED */
557         return 0;
558 }
559 EXPORT_SYMBOL(kdba_dumpregs);
560
561 kdb_machreg_t
562 kdba_getpc(struct pt_regs *regs)
563 {
564         return regs ? regs->ip : 0;
565 }
566
567 int
568 kdba_setpc(struct pt_regs *regs, kdb_machreg_t newpc)
569 {
570         if (KDB_NULL_REGS(regs))
571                 return KDB_BADREG;
572         regs->ip = newpc;
573         KDB_STATE_SET(IP_ADJUSTED);
574         return 0;
575 }
576
577 /*
578  * kdba_main_loop
579  *
580  *      Do any architecture specific set up before entering the main kdb loop.
581  *      The primary function of this routine is to make all processes look the
582  *      same to kdb, kdb must be able to list a process without worrying if the
583  *      process is running or blocked, so make all process look as though they
584  *      are blocked.
585  *
586  * Inputs:
587  *      reason          The reason KDB was invoked
588  *      error           The hardware-defined error code
589  *      error2          kdb's current reason code.  Initially error but can change
590  *                      acording to kdb state.
591  *      db_result       Result from break or debug point.
592  *      regs            The exception frame at time of fault/breakpoint.  If reason
593  *                      is SILENT or CPU_UP then regs is NULL, otherwise it should
594  *                      always be valid.
595  * Returns:
596  *      0       KDB was invoked for an event which it wasn't responsible
597  *      1       KDB handled the event for which it was invoked.
598  * Outputs:
599  *      Sets eip and esp in current->thread.
600  * Locking:
601  *      None.
602  * Remarks:
603  *      none.
604  */
605
606 int
607 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
608                kdb_dbtrap_t db_result, struct pt_regs *regs)
609 {
610         int ret;
611         ret = kdb_save_running(regs, reason, reason2, error, db_result);
612         kdb_unsave_running(regs);
613         return ret;
614 }
615
616 void
617 kdba_disableint(kdb_intstate_t *state)
618 {
619         unsigned long *fp = (unsigned long *)state;
620         unsigned long flags;
621
622         local_irq_save(flags);
623
624         *fp = flags;
625 }
626
627 void
628 kdba_restoreint(kdb_intstate_t *state)
629 {
630         unsigned long flags = *(int *)state;
631         local_irq_restore(flags);
632 }
633
634 void
635 kdba_setsinglestep(struct pt_regs *regs)
636 {
637         if (KDB_NULL_REGS(regs))
638                 return;
639         if (regs->flags & EF_IE)
640                 KDB_STATE_SET(A_IF);
641         else
642                 KDB_STATE_CLEAR(A_IF);
643         regs->flags = (regs->flags | EF_TF) & ~EF_IE;
644 }
645
646 void
647 kdba_clearsinglestep(struct pt_regs *regs)
648 {
649         if (KDB_NULL_REGS(regs))
650                 return;
651         if (KDB_STATE(A_IF))
652                 regs->flags |= EF_IE;
653         else
654                 regs->flags &= ~EF_IE;
655 }
656
657 int asmlinkage
658 kdba_setjmp(kdb_jmp_buf *jb)
659 {
660 #if defined(CONFIG_FRAME_POINTER)
661         __asm__ ("movl 8(%esp), %eax\n\t"
662                  "movl %ebx, 0(%eax)\n\t"
663                  "movl %esi, 4(%eax)\n\t"
664                  "movl %edi, 8(%eax)\n\t"
665                  "movl (%esp), %ecx\n\t"
666                  "movl %ecx, 12(%eax)\n\t"
667                  "leal 8(%esp), %ecx\n\t"
668                  "movl %ecx, 16(%eax)\n\t"
669                  "movl 4(%esp), %ecx\n\t"
670                  "movl %ecx, 20(%eax)\n\t");
671 #else    /* CONFIG_FRAME_POINTER */
672         __asm__ ("movl 4(%esp), %eax\n\t"
673                  "movl %ebx, 0(%eax)\n\t"
674                  "movl %esi, 4(%eax)\n\t"
675                  "movl %edi, 8(%eax)\n\t"
676                  "movl %ebp, 12(%eax)\n\t"
677                  "leal 4(%esp), %ecx\n\t"
678                  "movl %ecx, 16(%eax)\n\t"
679                  "movl 0(%esp), %ecx\n\t"
680                  "movl %ecx, 20(%eax)\n\t");
681 #endif   /* CONFIG_FRAME_POINTER */
682         return 0;
683 }
684
685 void asmlinkage
686 kdba_longjmp(kdb_jmp_buf *jb, int reason)
687 {
688 #if defined(CONFIG_FRAME_POINTER)
689         __asm__("movl 8(%esp), %ecx\n\t"
690                 "movl 12(%esp), %eax\n\t"
691                 "movl 20(%ecx), %edx\n\t"
692                 "movl 0(%ecx), %ebx\n\t"
693                 "movl 4(%ecx), %esi\n\t"
694                 "movl 8(%ecx), %edi\n\t"
695                 "movl 12(%ecx), %ebp\n\t"
696                 "movl 16(%ecx), %esp\n\t"
697                 "jmp *%edx\n");
698 #else    /* CONFIG_FRAME_POINTER */
699         __asm__("movl 4(%esp), %ecx\n\t"
700                 "movl 8(%esp), %eax\n\t"
701                 "movl 20(%ecx), %edx\n\t"
702                 "movl 0(%ecx), %ebx\n\t"
703                 "movl 4(%ecx), %esi\n\t"
704                 "movl 8(%ecx), %edi\n\t"
705                 "movl 12(%ecx), %ebp\n\t"
706                 "movl 16(%ecx), %esp\n\t"
707                 "jmp *%edx\n");
708 #endif   /* CONFIG_FRAME_POINTER */
709 }
710
711 /*
712  * kdba_pt_regs
713  *
714  *      Format a struct pt_regs
715  *
716  * Inputs:
717  *      argc    argument count
718  *      argv    argument vector
719  * Outputs:
720  *      None.
721  * Returns:
722  *      zero for success, a kdb diagnostic if error
723  * Locking:
724  *      none.
725  * Remarks:
726  *      If no address is supplied, it uses the last irq pt_regs.
727  */
728
729 static int
730 kdba_pt_regs(int argc, const char **argv)
731 {
732         int diag;
733         kdb_machreg_t addr;
734         long offset = 0;
735         int nextarg;
736         struct pt_regs *p;
737         static const char *fmt = "  %-11.11s 0x%lx\n";
738
739         if (argc == 0) {
740                 addr = (kdb_machreg_t) get_irq_regs();
741         } else if (argc == 1) {
742                 nextarg = 1;
743                 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
744                 if (diag)
745                         return diag;
746         } else {
747                 return KDB_ARGCOUNT;
748         }
749
750         p = (struct pt_regs *) addr;
751         kdb_printf("struct pt_regs 0x%p-0x%p\n", p, (unsigned char *)p + sizeof(*p) - 1);
752         kdb_print_nameval("ebx", p->bx);
753         kdb_print_nameval("ecx", p->cx);
754         kdb_print_nameval("edx", p->dx);
755         kdb_print_nameval("esi", p->si);
756         kdb_print_nameval("edi", p->di);
757         kdb_print_nameval("ebp", p->bp);
758         kdb_print_nameval("eax", p->ax);
759         kdb_printf(fmt, "xds", p->ds);
760         kdb_printf(fmt, "xes", p->es);
761         kdb_print_nameval("orig_eax", p->orig_ax);
762         kdb_print_nameval("eip", p->ip);
763         kdb_printf(fmt, "xcs", p->cs);
764         kdb_printf(fmt, "eflags", p->flags);
765         kdb_printf(fmt, "esp", p->sp);
766         kdb_printf(fmt, "xss", p->ss);
767         return 0;
768 }
769
770 /*
771  * kdba_stackdepth
772  *
773  *      Print processes that are using more than a specific percentage of their
774  *      stack.
775  *
776  * Inputs:
777  *      argc    argument count
778  *      argv    argument vector
779  * Outputs:
780  *      None.
781  * Returns:
782  *      zero for success, a kdb diagnostic if error
783  * Locking:
784  *      none.
785  * Remarks:
786  *      If no percentage is supplied, it uses 60.
787  */
788
789 static void
790 kdba_stackdepth1(struct task_struct *p, unsigned long esp)
791 {
792         struct thread_info *tinfo;
793         int used;
794         const char *type;
795         kdb_ps1(p);
796         do {
797                 tinfo = (struct thread_info *)(esp & -THREAD_SIZE);
798                 used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
799                 type = NULL;
800                 if (kdb_task_has_cpu(p)) {
801                         struct kdb_activation_record ar;
802                         memset(&ar, 0, sizeof(ar));
803                         kdba_get_stack_info_alternate(esp, -1, &ar);
804                         type = ar.stack.id;
805                 }
806                 if (!type)
807                         type = "process";
808                 kdb_printf("  %s stack %p esp %lx used %d\n", type, tinfo, esp, used);
809                 esp = tinfo->previous_esp;
810         } while (esp);
811 }
812
813 static int
814 kdba_stackdepth(int argc, const char **argv)
815 {
816         int diag, cpu, threshold, used, over;
817         unsigned long percentage;
818         unsigned long esp;
819         long offset = 0;
820         int nextarg;
821         struct task_struct *p, *g;
822         struct kdb_running_process *krp;
823         struct thread_info *tinfo;
824
825         if (argc == 0) {
826                 percentage = 60;
827         } else if (argc == 1) {
828                 nextarg = 1;
829                 diag = kdbgetaddrarg(argc, argv, &nextarg, &percentage, &offset, NULL);
830                 if (diag)
831                         return diag;
832         } else {
833                 return KDB_ARGCOUNT;
834         }
835         percentage = max_t(int, percentage, 1);
836         percentage = min_t(int, percentage, 100);
837         threshold = ((2 * THREAD_SIZE * percentage) / 100 + 1) >> 1;
838         kdb_printf("stackdepth: processes using more than %ld%% (%d bytes) of stack\n",
839                 percentage, threshold);
840
841         /* Run the active tasks first, they can have multiple stacks */
842         for (cpu = 0, krp = kdb_running_process; cpu < NR_CPUS; ++cpu, ++krp) {
843                 if (!cpu_online(cpu))
844                         continue;
845                 p = krp->p;
846                 esp = krp->arch.esp;
847                 over = 0;
848                 do {
849                         tinfo = (struct thread_info *)(esp & -THREAD_SIZE);
850                         used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
851                         if (used >= threshold)
852                                 over = 1;
853                         esp = tinfo->previous_esp;
854                 } while (esp);
855                 if (over)
856                         kdba_stackdepth1(p, krp->arch.esp);
857         }
858         /* Now the tasks that are not on cpus */
859         kdb_do_each_thread(g, p) {
860                 if (kdb_task_has_cpu(p))
861                         continue;
862                 esp = p->thread.sp;
863                 used = sizeof(*tinfo) + THREAD_SIZE - (esp & (THREAD_SIZE-1));
864                 over = used >= threshold;
865                 if (over)
866                         kdba_stackdepth1(p, esp);
867         } kdb_while_each_thread(g, p);
868
869         return 0;
870 }
871
872 asmlinkage int kdb_call(void);
873
874 /* Executed once on each cpu at startup. */
875 void
876 kdba_cpu_up(void)
877 {
878 }
879
880 static int __init
881 kdba_arch_init(void)
882 {
883 #ifdef  CONFIG_SMP
884         set_intr_gate(KDB_VECTOR, kdb_interrupt);
885 #endif
886         set_intr_gate(KDBENTER_VECTOR, kdb_call);
887         return 0;
888 }
889
890 arch_initcall(kdba_arch_init);
891
892 /*
893  * kdba_init
894  *
895  *      Architecture specific initialization.
896  *
897  * Parameters:
898  *      None.
899  * Returns:
900  *      None.
901  * Locking:
902  *      None.
903  * Remarks:
904  *      None.
905  */
906
907 void __init
908 kdba_init(void)
909 {
910         kdba_arch_init();       /* Need to register KDBENTER_VECTOR early */
911         kdb_register("pt_regs", kdba_pt_regs, "address", "Format struct pt_regs", 0);
912         kdb_register("stackdepth", kdba_stackdepth, "[percentage]", "Print processes using >= stack percentage", 0);
913
914         return;
915 }
916
917 /*
918  * kdba_adjust_ip
919  *
920  *      Architecture specific adjustment of instruction pointer before leaving
921  *      kdb.
922  *
923  * Parameters:
924  *      reason          The reason KDB was invoked
925  *      error           The hardware-defined error code
926  *      regs            The exception frame at time of fault/breakpoint.  If reason
927  *                      is SILENT or CPU_UP then regs is NULL, otherwise it should
928  *                      always be valid.
929  * Returns:
930  *      None.
931  * Locking:
932  *      None.
933  * Remarks:
934  *      noop on ix86.
935  */
936
937 void
938 kdba_adjust_ip(kdb_reason_t reason, int error, struct pt_regs *regs)
939 {
940         return;
941 }
942
943 void
944 kdba_set_current_task(const struct task_struct *p)
945 {
946         kdb_current_task = p;
947         if (kdb_task_has_cpu(p)) {
948                 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
949                 kdb_current_regs = krp->regs;
950                 return;
951         }
952         kdb_current_regs = NULL;
953 }
954
955 /*
956  * asm-i386 uaccess.h supplies __copy_to_user which relies on MMU to
957  * trap invalid addresses in the _xxx fields.  Verify the other address
958  * of the pair is valid by accessing the first and last byte ourselves,
959  * then any access violations should only be caused by the _xxx
960  * addresses,
961  */
962
963 int
964 kdba_putarea_size(unsigned long to_xxx, void *from, size_t size)
965 {
966         mm_segment_t oldfs = get_fs();
967         int r;
968         char c;
969         c = *((volatile char *)from);
970         c = *((volatile char *)from + size - 1);
971
972         if (to_xxx < PAGE_OFFSET) {
973                 return kdb_putuserarea_size(to_xxx, from, size);
974         }
975
976         set_fs(KERNEL_DS);
977         r = __copy_to_user_inatomic((void __user *)to_xxx, from, size);
978         set_fs(oldfs);
979         return r;
980 }
981
982 int
983 kdba_getarea_size(void *to, unsigned long from_xxx, size_t size)
984 {
985         mm_segment_t oldfs = get_fs();
986         int r;
987         *((volatile char *)to) = '\0';
988         *((volatile char *)to + size - 1) = '\0';
989
990         if (from_xxx < PAGE_OFFSET) {
991                 return kdb_getuserarea_size(to, from_xxx, size);
992         }
993
994         set_fs(KERNEL_DS);
995         switch (size) {
996         case 1:
997                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 1);
998                 break;
999         case 2:
1000                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 2);
1001                 break;
1002         case 4:
1003                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 4);
1004                 break;
1005         case 8:
1006                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 8);
1007                 break;
1008         default:
1009                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, size);
1010                 break;
1011         }
1012         set_fs(oldfs);
1013         return r;
1014 }
1015
1016 int
1017 kdba_verify_rw(unsigned long addr, size_t size)
1018 {
1019         unsigned char data[size];
1020         return(kdba_getarea_size(data, addr, size) || kdba_putarea_size(addr, data, size));
1021 }
1022
1023 #ifdef  CONFIG_SMP
1024
1025 #include <mach_ipi.h>
1026
1027 /* When first entering KDB, try a normal IPI.  That reduces backtrace problems
1028  * on the other cpus.
1029  */
1030 void
1031 smp_kdb_stop(void)
1032 {
1033         if (!KDB_FLAG(NOIPI))
1034                 send_IPI_allbutself(KDB_VECTOR);
1035 }
1036
1037 /* The normal KDB IPI handler */
1038 void
1039 smp_kdb_interrupt(struct pt_regs *regs)
1040 {
1041         struct pt_regs *old_regs = set_irq_regs(regs);
1042         ack_APIC_irq();
1043         irq_enter();
1044         kdb_ipi(regs, NULL);
1045         irq_exit();
1046         set_irq_regs(old_regs);
1047 }
1048
1049 /* Invoked once from kdb_wait_for_cpus when waiting for cpus.  For those cpus
1050  * that have not responded to the normal KDB interrupt yet, hit them with an
1051  * NMI event.
1052  */
1053 void
1054 kdba_wait_for_cpus(void)
1055 {
1056         int c;
1057         if (KDB_FLAG(CATASTROPHIC))
1058                 return;
1059         kdb_printf("  Sending NMI to cpus that have not responded yet\n");
1060         for_each_online_cpu(c)
1061                 if (kdb_running_process[c].seqno < kdb_seqno - 1)
1062                         send_IPI_mask(cpumask_of_cpu(c), NMI_VECTOR);
1063 }
1064
1065 #endif  /* CONFIG_SMP */