cleanup
[linux-2.4.21-pre4.git] / arch / ppc64 / kdb / kdbasupport.c
1 /*
2  * Kernel Debugger Architecture Independent Support Functions
3  *
4  * Copyright (C) 1999 Silicon Graphics, Inc.
5  * Copyright (C) Scott Lurndal (slurn@engr.sgi.com)
6  * Copyright (C) Scott Foehner (sfoehner@engr.sgi.com)
7  * Copyright (C) Srinivasa Thirumalachar (sprasad@engr.sgi.com)
8  *
9  * See the file LIA-COPYRIGHT for additional information.
10  *
11  * Written March 1999 by Scott Lurndal at Silicon Graphics, Inc.
12  *
13  * Modifications from:
14  *      Richard Bass                    1999/07/20
15  *              Many bug fixes and enhancements.
16  *      Scott Foehner
17  *              Port to ia64
18  *      Scott Lurndal                   1999/12/12
19  *              v1.0 restructuring.
20  *      Keith Owens                     2000/05/23
21  *              KDB v1.2
22  */
23
24 #include <linux/string.h>
25 #include <linux/stddef.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/ptrace.h>
29 #include <linux/mm.h>
30 #include <linux/sched.h>
31 #include <linux/kdb.h>
32 #include <linux/kdbprivate.h>
33
34 #include <asm/processor.h>
35 #include "privinst.h"
36 #include <asm/uaccess.h>
37 #include <asm/machdep.h>
38
39 extern const char *kdb_diemsg;
40
41
42 /* prototypes */
43 int valid_ppc64_kernel_address(unsigned long addr, unsigned long size);
44 extern int kdba_excprint(int argc, const char **argv, const char **envp, struct pt_regs *regs);
45 extern int kdba_super_regs(int argc, const char **argv, const char **envp, struct pt_regs *regs);
46 extern int kdba_dissect_msr(int argc, const char **argv, const char **envp, struct pt_regs *regs);
47 extern int kdba_halt(int argc, const char **argv, const char **envp, struct pt_regs *regs);
48 extern int kdba_dump_tce_table(int argc, const char **argv, const char **envp, struct pt_regs *regs);
49 extern int kdba_kernelversion(int argc, const char **argv, const char **envp, struct pt_regs *regs);
50 extern int kdba_dmesg(int argc, const char **argv, const char **envp, struct pt_regs *regs);
51 extern int kdba_dump_pci_info(int argc, const char **argv, const char **envp, struct pt_regs *regs);
52 unsigned long kdba_getword(unsigned long addr, size_t width);
53
54 /*
55  * kdba_init
56  *      Architecture specific initialization.
57  */
58 /*
59 kdb_register("commandname",              # name of command user will use to invoke function  
60              function_name,              # name of function within the code 
61              "function example usage",   # sample usage 
62              "function description",     # brief description. 
63              0                           # if i hit enter again, will command repeat itself ?
64 Note: functions must take parameters as such:
65 functionname(int argc, const char **argv, const char **envp, struct pt_regs *regs)
66 */
67
68 void __init
69 kdba_init(void)
70 {
71         kdba_enable_lbr();
72         kdb_register("excp", kdba_excprint, "excp", "print exception info", 0);
73         kdb_register("superreg", kdba_super_regs, "superreg", "display super_regs", 0);
74         kdb_register("msr", kdba_dissect_msr, "msr", "dissect msr", 0);
75         kdb_register("halt", kdba_halt, "halt", "halt machine", 0);
76         kdb_register("dump_tce_table", kdba_dump_tce_table, "dump_tce_table <addr> [full]", "dump the tce table located at <addr>", 0);
77         kdb_register("kernel", kdba_kernelversion, "version", "display running kernel version", 0);
78         kdb_register("version", kdba_kernelversion, "version", "display running kernel version", 0);
79         kdb_register("_dmesg", kdba_dmesg, "dmesg <lines>", "display lines from dmesg (log_buf) buffer", 0);
80         kdb_register("pci_info", kdba_dump_pci_info, "dump_pci_info", "dump pci device info", 0);
81
82
83         if (!ppc_md.udbg_getc_poll)
84                 kdb_on = 0;
85 }
86
87
88
89
90 /*
91  * kdba_prologue
92  *
93  *      This function analyzes a gcc-generated function prototype
94  *      with or without frame pointers to determine the amount of
95  *      automatic storage and register save storage is used on the
96  *      stack of the target function.  It only counts instructions
97  *      that have been executed up to but excluding the current eip.
98  * Inputs:
99  *      code    Start address of function code to analyze
100  *      pc      Current program counter within function
101  *      sp      Current stack pointer for function
102  *      fp      Current frame pointer for function, may not be valid
103  *      ss      Start of stack for current process.
104  *      caller  1 if looking for data on the caller frame, 0 for callee.
105  * Outputs:
106  *      ar      Activation record, all fields may be set.  fp and oldfp
107  *              are 0 if they cannot be extracted.  return is 0 if the
108  *              code cannot find a valid return address.  args and arg0
109  *              are 0 if the number of arguments cannot be safely
110  *              calculated.
111  * Returns:
112  *      1 if prologue is valid, 0 otherwise.  If pc is 0 treat it as a
113  *      valid prologue to allow bt on wild branches.
114  * Locking:
115  *      None.
116  * Remarks:
117  *
118  */
119 int
120 kdba_prologue(const kdb_symtab_t *symtab, kdb_machreg_t pc, kdb_machreg_t sp,
121               kdb_machreg_t fp, kdb_machreg_t ss, int caller, kdb_ar_t *ar)
122 {
123         /* We don't currently use kdb's generic activation record scanning
124          * code to handle backtrace.
125          */
126         return 0;
127 }
128
129
130
131 /*
132  * kdba_getregcontents
133  *
134  *      Return the contents of the register specified by the
135  *      input string argument.   Return an error if the string
136  *      does not match a machine register.
137  *
138  *      The following pseudo register names are supported:
139  *         &regs         - Prints address of exception frame
140  *         kesp          - Prints kernel stack pointer at time of fault
141  *         cesp          - Prints current kernel stack pointer, inside kdb
142  *         ceflags       - Prints current flags, inside kdb
143  *         %<regname>    - Uses the value of the registers at the
144  *                         last time the user process entered kernel
145  *                         mode, instead of the registers at the time
146  *                         kdb was entered.
147  *
148  * Parameters:
149  *      regname         Pointer to string naming register
150  *      regs            Pointer to structure containing registers.
151  * Outputs:
152  *      *contents       Pointer to unsigned long to recieve register contents
153  * Returns:
154  *      0               Success
155  *      KDB_BADREG      Invalid register name
156  * Locking:
157  *      None.
158  * Remarks:
159  *      If kdb was entered via an interrupt from the kernel itself then
160  *      ss and esp are *not* on the stack.
161  */
162
163 static struct kdbregs {
164         char   *reg_name;
165         size_t  reg_offset;
166 } kdbreglist[] = {
167         { "gpr0",       offsetof(struct pt_regs, gpr[0]) },
168         { "gpr1",       offsetof(struct pt_regs, gpr[1]) },
169         { "gpr2",       offsetof(struct pt_regs, gpr[2]) },
170         { "gpr3",       offsetof(struct pt_regs, gpr[3]) },
171         { "gpr4",       offsetof(struct pt_regs, gpr[4]) },
172         { "gpr5",       offsetof(struct pt_regs, gpr[5]) },
173         { "gpr6",       offsetof(struct pt_regs, gpr[6]) },
174         { "gpr7",       offsetof(struct pt_regs, gpr[7]) },
175         { "gpr8",       offsetof(struct pt_regs, gpr[8]) },
176         { "gpr9",       offsetof(struct pt_regs, gpr[9]) },
177         { "gpr10",      offsetof(struct pt_regs, gpr[10]) },
178         { "gpr11",      offsetof(struct pt_regs, gpr[11]) },
179         { "gpr12",      offsetof(struct pt_regs, gpr[12]) },
180         { "gpr13",      offsetof(struct pt_regs, gpr[13]) },
181         { "gpr14",      offsetof(struct pt_regs, gpr[14]) },
182         { "gpr15",      offsetof(struct pt_regs, gpr[15]) },
183         { "gpr16",      offsetof(struct pt_regs, gpr[16]) },
184         { "gpr17",      offsetof(struct pt_regs, gpr[17]) },
185         { "gpr18",      offsetof(struct pt_regs, gpr[18]) },
186         { "gpr19",      offsetof(struct pt_regs, gpr[19]) },
187         { "gpr20",      offsetof(struct pt_regs, gpr[20]) },
188         { "gpr21",      offsetof(struct pt_regs, gpr[21]) },
189         { "gpr22",      offsetof(struct pt_regs, gpr[22]) },
190         { "gpr23",      offsetof(struct pt_regs, gpr[23]) },
191         { "gpr24",      offsetof(struct pt_regs, gpr[24]) },
192         { "gpr25",      offsetof(struct pt_regs, gpr[25]) },
193         { "gpr26",      offsetof(struct pt_regs, gpr[26]) },
194         { "gpr27",      offsetof(struct pt_regs, gpr[27]) },
195         { "gpr28",      offsetof(struct pt_regs, gpr[28]) },
196         { "gpr29",      offsetof(struct pt_regs, gpr[29]) },
197         { "gpr30",      offsetof(struct pt_regs, gpr[30]) },
198         { "gpr31",      offsetof(struct pt_regs, gpr[31]) },
199         { "eip",        offsetof(struct pt_regs, nip) },
200         { "msr",        offsetof(struct pt_regs, msr) },
201         { "esp",        offsetof(struct pt_regs, gpr[1]) },
202         { "orig_gpr3",  offsetof(struct pt_regs, orig_gpr3) },
203         { "ctr",        offsetof(struct pt_regs, ctr) },
204         { "link",       offsetof(struct pt_regs, link) },
205         { "xer",        offsetof(struct pt_regs, xer) },
206         { "ccr",        offsetof(struct pt_regs, ccr) },
207         { "mq",         offsetof(struct pt_regs, softe) /* mq */ },
208         { "trap",       offsetof(struct pt_regs, trap) },
209         { "dar",        offsetof(struct pt_regs, dar)  },
210         { "dsisr",      offsetof(struct pt_regs, dsisr) },
211         { "result",     offsetof(struct pt_regs, result) },
212 };
213
214 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
215
216 unsigned long
217 getsp(void)
218 {
219         unsigned long x;
220         asm("mr %0,1" : "=r" (x):);
221         return x;
222 }
223
224 int
225 kdba_getregcontents(const char *regname,
226                     struct pt_regs *regs,
227                     kdb_machreg_t *contents)
228 {
229         int i;
230
231         if (strcmp(regname, "&regs") == 0) {
232                 *contents = (unsigned long)regs;
233                 return 0;
234         }
235
236         if (strcmp(regname, "kesp") == 0) {
237                 *contents = (unsigned long) current->thread.ksp;
238                 return 0;
239         }
240
241         if (strcmp(regname, "cesp") == 0) {
242                 *contents = getsp();
243                 return 0;
244         }
245
246         if (strcmp(regname, "ceflags") == 0) {
247                 int flags;
248                 save_flags(flags);
249                 *contents = flags;
250                 return 0;
251         }
252
253         if (regname[0] == '%') {
254                 /* User registers:  %%e[a-c]x, etc */
255                 regname++;
256                 regs = (struct pt_regs *)
257                         (current->thread.ksp - sizeof(struct pt_regs));
258         }
259
260         for (i=0; i<nkdbreglist; i++) {
261                 if (strnicmp(kdbreglist[i].reg_name,
262                              regname,
263                              strlen(regname)) == 0)
264                         break;
265         }
266
267         if ((i < nkdbreglist)
268          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
269                 *contents = *(unsigned long *)((unsigned long)regs +
270                                 kdbreglist[i].reg_offset);
271                 return(0);
272         }
273
274         return KDB_BADREG;
275 }
276
277 /*
278  * kdba_setregcontents
279  *
280  *      Set the contents of the register specified by the
281  *      input string argument.   Return an error if the string
282  *      does not match a machine register.
283  *
284  *      Supports modification of user-mode registers via
285  *      %<register-name>
286  *
287  * Parameters:
288  *      regname         Pointer to string naming register
289  *      regs            Pointer to structure containing registers.
290  *      contents        Unsigned long containing new register contents
291  * Outputs:
292  * Returns:
293  *      0               Success
294  *      KDB_BADREG      Invalid register name
295  * Locking:
296  *      None.
297  * Remarks:
298  */
299
300 int
301 kdba_setregcontents(const char *regname,
302                   struct pt_regs *regs,
303                   unsigned long contents)
304 {
305         int i;
306
307         if (regname[0] == '%') {
308                 regname++;
309                 regs = (struct pt_regs *)
310                         (current->thread.ksp - sizeof(struct pt_regs));
311         }
312
313         for (i=0; i<nkdbreglist; i++) {
314                 if (strnicmp(kdbreglist[i].reg_name,
315                              regname,
316                              strlen(regname)) == 0)
317                         break;
318         }
319
320         if ((i < nkdbreglist)
321          && (strlen(kdbreglist[i].reg_name) == strlen(regname))) {
322                 *(unsigned long *)((unsigned long)regs
323                                    + kdbreglist[i].reg_offset) = contents;
324                 return 0;
325         }
326
327         return KDB_BADREG;
328 }
329
330 /*
331  * kdba_dumpregs
332  *
333  *      Dump the specified register set to the display.
334  *
335  * Parameters:
336  *      regs            Pointer to structure containing registers.
337  *      type            Character string identifying register set to dump
338  *      extra           string further identifying register (optional)
339  * Outputs:
340  * Returns:
341  *      0               Success
342  * Locking:
343  *      None.
344  * Remarks:
345  *      This function will dump the general register set if the type
346  *      argument is NULL (struct pt_regs).   The alternate register
347  *      set types supported by this function:
348  *
349  *      d               Debug registers
350  *      c               Control registers
351  *      u               User registers at most recent entry to kernel
352  * Following not yet implemented:
353  *      m               Model Specific Registers (extra defines register #)
354  *      r               Memory Type Range Registers (extra defines register)
355  */
356
357 int
358 kdba_dumpregs(struct pt_regs *regs,
359             const char *type,
360             const char *extra)
361 {
362         int i;
363         int count = 0;
364
365         if (type
366          && (type[0] == 'u')) {
367                 type = NULL;
368                 regs = (struct pt_regs *)
369                         (current->thread.ksp - sizeof(struct pt_regs));
370         }
371
372         if (type == NULL) {
373                 struct kdbregs *rlp;
374                 kdb_machreg_t contents;
375
376                 for (i=0, rlp=kdbreglist; i<nkdbreglist; i++,rlp++) {
377                         kdba_getregcontents(rlp->reg_name, regs, &contents);
378                         kdb_printf("%-5s = 0x%p%c", rlp->reg_name, (void *)contents, (++count % 2) ? ' ' : '\n');
379                 }
380
381                 kdb_printf("&regs = 0x%p\n", regs);
382
383                 return 0;
384         }
385
386         switch (type[0]) {
387         case 'm':
388                 break;
389         case 'r':
390                 break;
391         default:
392                 return KDB_BADREG;
393         }
394
395         /* NOTREACHED */
396         return 0;
397 }
398
399 kdb_machreg_t
400 kdba_getpc(kdb_eframe_t ef)
401 {
402         return ef->nip;
403 }
404
405 int
406 kdba_setpc(kdb_eframe_t ef, kdb_machreg_t newpc)
407 {
408 /* for ppc64, newpc passed in is actually a function descriptor for kdb. */
409     ef->nip =     kdba_getword(newpc+8, 8);
410     KDB_STATE_SET(IP_ADJUSTED);
411     return 0;
412 }
413
414 /*
415  * kdba_main_loop
416  *
417  *      Do any architecture specific set up before entering the main kdb loop.
418  *      The primary function of this routine is to make all processes look the
419  *      same to kdb, kdb must be able to list a process without worrying if the
420  *      process is running or blocked, so make all process look as though they
421  *      are blocked.
422  *
423  * Inputs:
424  *      reason          The reason KDB was invoked
425  *      error           The hardware-defined error code
426  *      error2          kdb's current reason code.  Initially error but can change
427  *                      acording to kdb state.
428  *      db_result       Result from break or debug point.
429  *      ef              The exception frame at time of fault/breakpoint.  If reason
430  *                      is KDB_REASON_SILENT then ef is NULL, otherwise it should
431  *                      always be valid.
432  * Returns:
433  *      0       KDB was invoked for an event which it wasn't responsible
434  *      1       KDB handled the event for which it was invoked.
435  * Outputs:
436  *      Sets eip and esp in current->thread.
437  * Locking:
438  *      None.
439  * Remarks:
440  *      none.
441  */
442
443 int
444 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
445                kdb_dbtrap_t db_result, kdb_eframe_t ef)
446 {
447         int rv;
448         unsigned int msr;
449         if (current->thread.regs == NULL)
450         {
451                 struct pt_regs regs;
452                 asm volatile ("std      0,0(%0)\n\
453                                std      1,8(%0)\n\
454                                std      2,16(%0)\n\
455                                std      3,24(%0)\n\
456                                std      4,32(%0)\n\
457                                std      5,40(%0)\n\
458                                std      6,48(%0)\n\
459                                std      7,56(%0)\n\
460                                std      8,64(%0)\n\
461                                std      9,72(%0)\n\
462                                std      10,80(%0)\n\
463                                std      11,88(%0)\n\
464                                std      12,96(%0)\n\
465                                std      13,104(%0)\n\
466                                std      14,112(%0)\n\
467                                std      15,120(%0)\n\
468                                std      16,128(%0)\n\
469                                std      17,136(%0)\n\
470                                std      18,144(%0)\n\
471                                std      19,152(%0)\n\
472                                std      20,160(%0)\n\
473                                std      21,168(%0)\n\
474                                std      22,176(%0)\n\
475                                std      23,184(%0)\n\
476                                std      24,192(%0)\n\
477                                std      25,200(%0)\n\
478                                std      26,208(%0)\n\
479                                std      27,216(%0)\n\
480                                std      28,224(%0)\n\
481                                std      29,232(%0)\n\
482                                std      30,240(%0)\n\
483                                std      31,248(%0)" : : "b" (&regs));
484                 /* Fetch the link reg for this stack frame.
485                  NOTE: the prev kdb_printf fills in the lr. */
486                 regs.nip = regs.link = ((unsigned long *)regs.gpr[1])[2];
487                 regs.msr = get_msr();
488                 regs.ctr = get_ctr();
489                 regs.xer = get_xer();
490                 regs.ccr = get_cr();
491                 regs.trap = 0;
492                 current->thread.regs = &regs;
493         }
494         if (ef) {
495                 kdba_getregcontents("eip", ef, &(current->thread.regs->nip));
496                 kdba_getregcontents("esp", ef, &(current->thread.regs->gpr[1]));
497                 
498         }
499         msr = get_msr();
500         set_msr( msr & ~0x8000);
501         rv = kdb_main_loop(reason, reason2, error, db_result, ef);
502         set_msr(msr);
503         return rv;
504 }
505
506 void
507 kdba_disableint(kdb_intstate_t *state)
508 {
509         int *fp = (int *)state;
510         int   flags;
511
512         save_flags(flags);
513         cli();
514
515         *fp = flags;
516 }
517
518 void
519 kdba_restoreint(kdb_intstate_t *state)
520 {
521         int flags = *(int *)state;
522         restore_flags(flags);
523 }
524
525 void
526 kdba_setsinglestep(struct pt_regs *regs)
527 {
528         regs->msr |= MSR_SE;
529 }
530
531 void
532 kdba_clearsinglestep(struct pt_regs *regs)
533 {
534         
535         regs->msr &= ~MSR_SE;
536 }
537
538 int
539 kdba_getcurrentframe(struct pt_regs *regs)
540 {
541         regs->gpr[1] = getsp();
542         /* this stack pointer becomes invalid after we return, so take another step back.  */
543         regs->gpr[1] = kdba_getword(regs->gpr[1], 8);
544         return 0;
545 }
546
547 #ifdef KDB_HAVE_LONGJMP
548 int kdba_setjmp(kdb_jmp_buf *buf)
549 {
550     asm volatile (
551         "mflr 0; std 0,0(%0)\n\
552          std    1,8(%0)\n\
553          std    2,16(%0)\n\
554          mfcr 0; std 0,24(%0)\n\
555          std    13,32(%0)\n\
556          std    14,40(%0)\n\
557          std    15,48(%0)\n\
558          std    16,56(%0)\n\
559          std    17,64(%0)\n\
560          std    18,72(%0)\n\
561          std    19,80(%0)\n\
562          std    20,88(%0)\n\
563          std    21,96(%0)\n\
564          std    22,104(%0)\n\
565          std    23,112(%0)\n\
566          std    24,120(%0)\n\
567          std    25,128(%0)\n\
568          std    26,136(%0)\n\
569          std    27,144(%0)\n\
570          std    28,152(%0)\n\
571          std    29,160(%0)\n\
572          std    30,168(%0)\n\
573          std    31,176(%0)\n\
574          " : : "r" (buf));
575     KDB_STATE_SET(LONGJMP);
576     return 0;
577 }
578 void kdba_longjmp(kdb_jmp_buf *buf, int val)
579 {
580     if (val == 0)
581         val = 1;
582     asm volatile (
583         "ld     13,32(%0)\n\
584          ld     14,40(%0)\n\
585          ld     15,48(%0)\n\
586          ld     16,56(%0)\n\
587          ld     17,64(%0)\n\
588          ld     18,72(%0)\n\
589          ld     19,80(%0)\n\
590          ld     20,88(%0)\n\
591          ld     21,96(%0)\n\
592          ld     22,104(%0)\n\
593          ld     23,112(%0)\n\
594          ld     24,120(%0)\n\
595          ld     25,128(%0)\n\
596          ld     26,136(%0)\n\
597          ld     27,144(%0)\n\
598          ld     28,152(%0)\n\
599          ld     29,160(%0)\n\
600          ld     30,168(%0)\n\
601          ld     31,176(%0)\n\
602          ld     0,24(%0)\n\
603          mtcrf  0x38,0\n\
604          ld     0,0(%0)\n\
605          ld     1,8(%0)\n\
606          ld     2,16(%0)\n\
607          mtlr   0\n\
608          mr     3,%1\n\
609          " : : "r" (buf), "r" (val));
610 }
611 #endif
612
613 /*
614  * kdba_enable_mce
615  *
616  *      This function is called once on each CPU to enable machine
617  *      check exception handling.
618  *
619  * Inputs:
620  *      None.
621  * Outputs:
622  *      None.
623  * Returns:
624  *      None.
625  * Locking:
626  *      None.
627  * Remarks:
628  *
629  */
630
631 void
632 kdba_enable_mce(void)
633 {
634 }
635
636 /*
637  * kdba_enable_lbr
638  *
639  *      Enable last branch recording.
640  *
641  * Parameters:
642  *      None.
643  * Returns:
644  *      None
645  * Locking:
646  *      None
647  * Remarks:
648  *      None.
649  */
650
651 void
652 kdba_enable_lbr(void)
653 {
654 }
655
656 /*
657  * kdba_disable_lbr
658  *
659  *      disable last branch recording.
660  *
661  * Parameters:
662  *      None.
663  * Returns:
664  *      None
665  * Locking:
666  *      None
667  * Remarks:
668  *      None.
669  */
670
671 void
672 kdba_disable_lbr(void)
673 {
674 }
675
676 /*
677  * kdba_print_lbr
678  *
679  *      Print last branch and last exception addresses
680  *
681  * Parameters:
682  *      None.
683  * Returns:
684  *      None
685  * Locking:
686  *      None
687  * Remarks:
688  *      None.
689  */
690
691 void
692 kdba_print_lbr(void)
693 {
694 }
695
696 /*
697  * kdba_getword
698  *
699  *      Architecture specific function to access kernel virtual
700  *      address space.
701  *
702  * Parameters:
703  *      None.
704  * Returns:
705  *      None.
706  * Locking:
707  *      None.
708  * Remarks:
709  *      None.
710  */
711
712 /*      if (access_ok(VERIFY_READ,__gu_addr,size))                      \ */
713  
714 extern inline void sync(void)
715 {
716         asm volatile("sync; isync");
717 }
718
719 extern void (*debugger_fault_handler)(struct pt_regs *);
720 extern void longjmp(u_int *, int);
721 #if 0
722 static void handle_fault(struct pt_regs *);
723
724 static int fault_type;
725 #endif
726
727 unsigned long
728 kdba_getword(unsigned long addr, size_t width)
729 {
730         /*
731          * This function checks the address for validity.  Any address
732          * in the range PAGE_OFFSET to high_memory is legal, any address
733          * which maps to a vmalloc region is legal, and any address which
734          * is a user address, we use get_user() to verify validity.
735          */
736
737     if (!valid_ppc64_kernel_address(addr, width)) {
738                         /*
739                          * Would appear to be an illegal kernel address;
740                          * Print a message once, and don't print again until
741                          * a legal address is used.
742                          */
743                         if (!KDB_STATE(SUPPRESS)) {
744 #if 1
745                                 kdb_printf("    kdb: Possibly Bad kernel address 0x%lx \n",addr);
746 #else
747                                 kdb_printf("kdb: ! \n");
748 #endif
749                                 KDB_STATE_SET(SUPPRESS);
750                         }
751                         return 0L;
752         }
753
754
755         /*
756          * A good address.  Reset error flag.
757          */
758         KDB_STATE_CLEAR(SUPPRESS);
759
760         switch (width) {
761         case 8:
762         {       unsigned long *lp;
763
764                 lp = (unsigned long *)(addr);
765                 return *lp;
766         }
767         case 4:
768         {       unsigned int *ip;
769
770                 ip = (unsigned int *)(addr);
771                 return *ip;
772         }
773         case 2:
774         {       unsigned short *sp;
775
776                 sp = (unsigned short *)(addr);
777                 return *sp;
778         }
779         case 1:
780         {       unsigned char *cp;
781
782                 cp = (unsigned char *)(addr);
783                 return *cp;
784         }
785         }
786
787         kdb_printf("kdbgetword: Bad width\n");
788         return 0L;
789 }
790
791
792
793 /*
794  * kdba_putword
795  *
796  *      Architecture specific function to access kernel virtual
797  *      address space.
798  *
799  * Parameters:
800  *      None.
801  * Returns:
802  *      None.
803  * Locking:
804  *      None.
805  * Remarks:
806  *      None.
807  */
808
809 unsigned long
810 kdba_putword(unsigned long addr, size_t size, unsigned long contents)
811 {
812         /*
813          * This function checks the address for validity.  Any address
814          * in the range PAGE_OFFSET to high_memory is legal, any address
815          * which maps to a vmalloc region is legal, and any address which
816          * is a user address, we use get_user() to verify validity.
817          */
818
819         if (addr < PAGE_OFFSET) {
820                 /*
821                  * Usermode address.
822                  */
823                 unsigned long diag;
824
825                 switch (size) {
826                 case 4:
827                 {       unsigned long *lp;
828
829                         lp = (unsigned long *) addr;
830                         diag = put_user(contents, lp);
831                         break;
832                 }
833                 case 2:
834                 {       unsigned short *sp;
835
836                         sp = (unsigned short *) addr;
837                         diag = put_user(contents, sp);
838                         break;
839                 }
840                 case 1:
841                 {       unsigned char *cp;
842
843                         cp = (unsigned char *) addr;
844                         diag = put_user(contents, cp);
845                         break;
846                 }
847                 default:
848                         kdb_printf("kdba_putword: Bad width\n");
849                         return 0;
850                 }
851
852                 if (diag) {
853                         if (!KDB_STATE(SUPPRESS)) {
854                                 kdb_printf("kdb: Bad user address 0x%lx\n", addr);
855                                 KDB_STATE_SET(SUPPRESS);
856                         }
857                         return 0;
858                 }
859                 KDB_STATE_CLEAR(SUPPRESS);
860                 return 0;
861         }
862
863 #if 0
864         if (addr > (unsigned long)high_memory) {
865                 if (!kdb_vmlist_check(addr, addr+size)) {
866                         /*
867                          * Would appear to be an illegal kernel address;
868                          * Print a message once, and don't print again until
869                          * a legal address is used.
870                          */
871                         if (!KDB_STATE(SUPPRESS)) {
872                                 kdb_printf("kdb: xx Bad kernel address 0x%lx\n", addr);
873                                 KDB_STATE_SET(SUPPRESS);
874                         }
875                         return 0L;
876                 }
877         }
878 #endif
879
880         /*
881          * A good address.  Reset error flag.
882          */
883         KDB_STATE_CLEAR(SUPPRESS);
884
885         switch (size) {
886         case 4:
887         {       unsigned long *lp;
888
889                 lp = (unsigned long *)(addr);
890                 *lp = contents;
891                 return 0;
892         }
893         case 2:
894         {       unsigned short *sp;
895
896                 sp = (unsigned short *)(addr);
897                 *sp = (unsigned short) contents;
898                 return 0;
899         }
900         case 1:
901         {       unsigned char *cp;
902
903                 cp = (unsigned char *)(addr);
904                 *cp = (unsigned char) contents;
905                 return 0;
906         }
907         }
908
909         kdb_printf("kdba_putword: Bad width\n");
910         return 0;
911 }
912
913 /*
914  * kdba_callback_die
915  *
916  *      Callback function for kernel 'die' function.
917  *
918  * Parameters:
919  *      regs    Register contents at time of trap
920  *      error_code  Trap-specific error code value
921  *      trapno  Trap number
922  *      vp      Pointer to die message
923  * Returns:
924  *      Returns 1 if fault handled by kdb.
925  * Locking:
926  *      None.
927  * Remarks:
928  *
929  */
930 int
931 kdba_callback_die(struct pt_regs *regs, int error_code, long trapno, void *vp)
932 {
933         /*
934          * Save a pointer to the message provided to 'die()'.
935          */
936         kdb_diemsg = (char *)vp;
937
938         return kdb(KDB_REASON_OOPS, error_code, (kdb_eframe_t) regs);
939 }
940
941 /*
942  * kdba_callback_bp
943  *
944  *      Callback function for kernel breakpoint trap.
945  *
946  * Parameters:
947  *      regs    Register contents at time of trap
948  *      error_code  Trap-specific error code value
949  *      trapno  Trap number
950  *      vp      Not Used.
951  * Returns:
952  *      Returns 1 if fault handled by kdb.
953  * Locking:
954  *      None.
955  * Remarks:
956  *
957  */
958
959 int
960 kdba_callback_bp(struct pt_regs *regs, int error_code, long trapno, void *vp)
961 {
962         int diag;
963
964         if (KDB_DEBUG(BP))
965                 kdb_printf("cb_bp: e_c = %d  tn = %ld regs = 0x%p\n", error_code,
966                            trapno, regs);
967
968         diag = kdb(KDB_REASON_BREAK, error_code, (kdb_eframe_t) regs);
969
970         if (KDB_DEBUG(BP))
971                 kdb_printf("cb_bp: e_c = %d  tn = %ld regs = 0x%p diag = %d\n", error_code,
972                            trapno, regs, diag);
973         return diag;
974 }
975
976 /*
977  * kdba_callback_debug
978  *
979  *      Callback function for kernel debug register trap.
980  *
981  * Parameters:
982  *      regs    Register contents at time of trap
983  *      error_code  Trap-specific error code value
984  *      trapno  Trap number
985  *      vp      Not used.
986  * Returns:
987  *      Returns 1 if fault handled by kdb.
988  * Locking:
989  *      None.
990  * Remarks:
991  *
992  */
993
994 int
995 kdba_callback_debug(struct pt_regs *regs, int error_code, long trapno, void *vp)
996 {
997         return kdb(KDB_REASON_DEBUG, error_code, (kdb_eframe_t) regs);
998 }
999
1000
1001
1002
1003 /*
1004  * kdba_adjust_ip
1005  *
1006  *      Architecture specific adjustment of instruction pointer before leaving
1007  *      kdb.
1008  *
1009  * Parameters:
1010  *      reason          The reason KDB was invoked
1011  *      error           The hardware-defined error code
1012  *      ef              The exception frame at time of fault/breakpoint.  If reason
1013  *                      is KDB_REASON_SILENT then ef is NULL, otherwise it should
1014  *                      always be valid.
1015  * Returns:
1016  *      None.
1017  * Locking:
1018  *      None.
1019  * Remarks:
1020  *      noop on ix86.
1021  */
1022
1023 void
1024 kdba_adjust_ip(kdb_reason_t reason, int error, kdb_eframe_t ef)
1025 {
1026         return;
1027 }
1028
1029
1030
1031 /*
1032  * kdba_find_tb_table
1033  *
1034  *      Find the traceback table (defined by the ELF64 ABI) located at
1035  *      the end of the function containing pc.
1036  *
1037  * Parameters:
1038  *      eip     starting instruction addr.  does not need to be at the start of the func.
1039  *      tab     table to populate if successful
1040  * Returns:
1041  *      non-zero if successful.  unsuccessful means that a valid tb table was not found
1042  * Locking:
1043  *      None.
1044  * Remarks:
1045  *      None.
1046  */
1047 int kdba_find_tb_table(kdb_machreg_t eip, kdbtbtable_t *tab)
1048 {
1049         kdb_machreg_t codeaddr = eip;
1050         kdb_machreg_t codeaddr_max;
1051         kdb_machreg_t tbtab_start;
1052         int instr;
1053         int num_parms;
1054
1055         if (tab == NULL)
1056                 return 0;
1057         memset(tab, 0, sizeof(tab));
1058
1059         if (eip < PAGE_OFFSET) {  /* this is gonna fail for userspace, at least for now.. */
1060             return 0;
1061         }
1062
1063         /* Scan instructions starting at codeaddr for 128k max */
1064         for (codeaddr_max = codeaddr + 128*1024*4;
1065              codeaddr < codeaddr_max;
1066              codeaddr += 4) {
1067                 instr = kdba_getword(codeaddr, 4);
1068                 if (instr == 0) {
1069                         /* table should follow. */
1070                         int version;
1071                         unsigned long flags;
1072                         tbtab_start = codeaddr; /* save it to compute func start addr */
1073                         codeaddr += 4;
1074                         flags = kdba_getword(codeaddr, 8);
1075                         tab->flags = flags;
1076                         version = (flags >> 56) & 0xff;
1077                         if (version != 0)
1078                                 continue;       /* No tb table here. */
1079                         /* Now, like the version, some of the flags are values
1080                          that are more conveniently extracted... */
1081                         tab->fp_saved = (flags >> 24) & 0x3f;
1082                         tab->gpr_saved = (flags >> 16) & 0x3f;
1083                         tab->fixedparms = (flags >> 8) & 0xff;
1084                         tab->floatparms = (flags >> 1) & 0x7f;
1085                         codeaddr += 8;
1086                         num_parms = tab->fixedparms + tab->floatparms;
1087                         if (num_parms) {
1088                                 unsigned int parminfo;
1089                                 int parm;
1090                                 if (num_parms > 32)
1091                                         return 1;       /* incomplete */
1092                                 parminfo = kdba_getword(codeaddr, 4);
1093                                 /* decode parminfo...32 bits.
1094                                  A zero means fixed.  A one means float and the
1095                                  following bit determines single (0) or double (1).
1096                                  */
1097                                 for (parm = 0; parm < num_parms; parm++) {
1098                                         if (parminfo & 0x80000000) {
1099                                                 parminfo <<= 1;
1100                                                 if (parminfo & 0x80000000)
1101                                                         tab->parminfo[parm] = KDBTBTAB_PARMDFLOAT;
1102                                                 else
1103                                                         tab->parminfo[parm] = KDBTBTAB_PARMSFLOAT;
1104                                         } else {
1105                                                 tab->parminfo[parm] = KDBTBTAB_PARMFIXED;
1106                                         }
1107                                         parminfo <<= 1;
1108                                 }
1109                                 codeaddr += 4;
1110                         }
1111                         if (flags & KDBTBTAB_FLAGSHASTBOFF) {
1112                                 tab->tb_offset = kdba_getword(codeaddr, 4);
1113                                 if (tab->tb_offset > 0) {
1114                                         tab->funcstart = tbtab_start - tab->tb_offset;
1115                                 }
1116                                 codeaddr += 4;
1117                         }
1118                         /* hand_mask appears to be always be omitted. */
1119                         if (flags & KDBTBTAB_FLAGSHASCTL) {
1120                                 /* Assume this will never happen for C or asm */
1121                                 return 1;       /* incomplete */
1122                         }
1123                         if (flags & KDBTBTAB_FLAGSNAMEPRESENT) {
1124                                 int i;
1125                                 short namlen = kdba_getword(codeaddr, 2);
1126                                 if (namlen >= sizeof(tab->name))
1127                                         namlen = sizeof(tab->name)-1;
1128                                 codeaddr += 2;
1129                                 for (i = 0; i < namlen; i++) {
1130                                         tab->name[i] = kdba_getword(codeaddr++, 1);
1131                                 }
1132                                 tab->name[namlen] = '\0';
1133                         }
1134                         /* Fake up a symtab entry in case the caller finds it useful */
1135                         tab->symtab.value = tab->symtab.sym_start = tab->funcstart;
1136                         tab->symtab.sym_name = tab->name;
1137                         tab->symtab.sym_end = tbtab_start;
1138                         return 1;
1139                 }
1140         }
1141         return 0;       /* hit max...sorry. */
1142 }
1143
1144
1145 int
1146 kdba_putarea_size(unsigned long to_xxx, void *from, size_t size)
1147 {
1148     char c;
1149     kdb_printf("   ** this function calls copy_to_user...  \n");
1150     kdb_printf("   kdba_putarea_size [0x%ul]\n",(unsigned int) to_xxx);
1151     c = *((volatile char *)from);
1152     c=*((volatile char *)from+size-1);
1153     return __copy_to_user((void *)to_xxx,from,size);
1154 }
1155
1156
1157
1158
1159
1160 /*
1161  * valid_ppc64_kernel_address() returns '1' if the address passed in is
1162  * within a valid range.  Function returns 0 if address is outside valid ranges.
1163  */
1164
1165 /*
1166
1167     KERNELBASE    c000000000000000
1168         (good range)
1169     high_memory   c0000000 20000000
1170
1171     VMALLOC_START d000000000000000
1172         (good range)
1173     VMALLOC_END   VMALLOC_START + VALID_EA_BITS  
1174
1175     IMALLOC_START e000000000000000
1176         (good range)
1177     IMALLOC_END   IMALLOC_START + VALID_EA_BITS
1178
1179 */
1180
1181 int valid_ppc64_kernel_address(unsigned long addr, unsigned long size)
1182 {
1183         unsigned long i;
1184         unsigned long end = (addr + size - 1);
1185
1186
1187         for (i = addr; i <= end; i = i ++ ) {
1188             if (((unsigned long)i < (unsigned long long)KERNELBASE     )  || 
1189                 (((unsigned long)i > (unsigned long long)high_memory) &&
1190                  ((unsigned long)i < (unsigned long long)VMALLOC_START) )  ||
1191                 (((unsigned long)i > (unsigned long long)VMALLOC_END) &&
1192                  ((unsigned long)i < (unsigned long long)IMALLOC_START) )  ||
1193                 ( (unsigned long)i > (unsigned long long)IMALLOC_END    )       ) {
1194                 return 0;
1195             }
1196         }
1197         return 1;
1198 }
1199
1200
1201 int
1202 kdba_getarea_size(void *to, unsigned long from_xxx, size_t size)
1203 {
1204         int is_valid_kern_addr = valid_ppc64_kernel_address(from_xxx, size);
1205         int diag = 0;
1206
1207         *((volatile char *)to) = '\0';
1208         *((volatile char *)to + size - 1) = '\0';
1209
1210
1211         if (is_valid_kern_addr) {
1212                 memcpy(to, (void *)from_xxx, size);
1213         } else {
1214             /*  user space address, just return.  */
1215             diag = -1;
1216         }
1217
1218         return diag;
1219 }
1220
1221
1222
1223 /*
1224  *  kdba_readarea_size, reads size-lump of memory into to* passed in, returns size.
1225  * Making it feel a bit more like mread.. when i'm clearer on kdba end, probally will
1226  * remove one of these.
1227  */
1228 int
1229 kdba_readarea_size(unsigned long from_xxx,void *to, size_t size)
1230 {
1231     int is_valid_kern_addr = valid_ppc64_kernel_address(from_xxx, size);
1232
1233     *((volatile char *)to) = '\0';
1234     *((volatile char *)to + size - 1) = '\0';
1235
1236     if (is_valid_kern_addr) {
1237         memcpy(to, (void *)from_xxx, size);
1238         return size;
1239     } else {
1240         /*  user-space, just return...    */
1241         return 0;
1242     }
1243     /* wont get here */
1244     return 0;
1245 }
1246