Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / kdb / kdba_support.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  * Copyright (C) David Mosberger-Tang <davidm@hpl.hp.com>
10  */
11
12 #include <linux/string.h>
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/ptrace.h>
16 #include <linux/interrupt.h>
17 #include <linux/mm.h>
18 #include <linux/sched.h>
19 #include <linux/kdb.h>
20 #include <linux/kdbprivate.h>
21 #include <linux/module.h>
22 #ifdef CONFIG_KDB_KDUMP
23 #include <linux/kexec.h>
24 #endif
25
26 #include <asm/processor.h>
27 #include <asm/uaccess.h>
28 #include <asm/rse.h>
29 #include <asm/delay.h>
30 #ifdef CONFIG_SMP
31 #include <asm/hw_irq.h>
32 #endif
33 #include <asm/mca.h>
34
35 struct kdb_running_process *kdb_running_process_save; /* [NR_CPUS] */
36 static int kdba_show_handlers = 0;
37
38 static int
39 kdba_itm (int argc, const char **argv)
40 {
41         int diag;
42         unsigned long val;
43
44         diag = kdbgetularg(argv[1], &val);
45         if (diag)
46                 return diag;
47         kdb_printf("new itm=" kdb_machreg_fmt "\n", val);
48
49         ia64_set_itm(val);
50         return 0;
51 }
52
53 static void
54 kdba_show_intregs(void)
55 {
56         u64 lid, tpr, lrr0, lrr1, itv, pmv, cmcv;
57
58         asm ("mov %0=cr.lid" : "=r"(lid));
59         asm ("mov %0=cr.tpr" : "=r"(tpr));
60         asm ("mov %0=cr.lrr0" : "=r"(lrr0));
61         asm ("mov %0=cr.lrr1" : "=r"(lrr1));
62         kdb_printf("lid=" kdb_machreg_fmt ", tpr=" kdb_machreg_fmt ", lrr0=" kdb_machreg_fmt ", llr1=" kdb_machreg_fmt "\n", lid, tpr, lrr0, lrr1);
63
64         asm ("mov %0=cr.itv" : "=r"(itv));
65         asm ("mov %0=cr.pmv" : "=r"(pmv));
66         asm ("mov %0=cr.cmcv" : "=r"(cmcv));
67         kdb_printf("itv=" kdb_machreg_fmt ", pmv=" kdb_machreg_fmt ", cmcv=" kdb_machreg_fmt "\n", itv, pmv, cmcv);
68
69         kdb_printf("irr=0x%016lx,0x%016lx,0x%016lx,0x%016lx\n",
70                 ia64_getreg(_IA64_REG_CR_IRR0), ia64_getreg(_IA64_REG_CR_IRR1), ia64_getreg(_IA64_REG_CR_IRR2), ia64_getreg(_IA64_REG_CR_IRR3));
71
72         kdb_printf("itc=0x%016lx, itm=0x%016lx\n", ia64_get_itc(), ia64_get_itm());
73 }
74
75 static int
76 kdba_sir (int argc, const char **argv)
77 {
78         kdba_show_intregs();
79
80         return 0;
81 }
82
83 /*
84  * kdba_pt_regs
85  *
86  *      Format a struct pt_regs
87  *
88  * Inputs:
89  *      argc    argument count
90  *      argv    argument vector
91  * Outputs:
92  *      None.
93  * Returns:
94  *      zero for success, a kdb diagnostic if error
95  * Locking:
96  *      none.
97  * Remarks:
98  *      If no address is supplied, it uses the current irq pt_regs.
99  */
100
101 static int
102 kdba_pt_regs(int argc, const char **argv)
103 {
104         int diag;
105         kdb_machreg_t addr;
106         long offset = 0;
107         int nextarg;
108         struct pt_regs *p;
109
110         if (argc == 0) {
111                 addr = (kdb_machreg_t) get_irq_regs();
112         } else if (argc == 1) {
113                 nextarg = 1;
114                 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
115                 if (diag)
116                         return diag;
117         } else {
118                 return KDB_ARGCOUNT;
119         }
120
121         p = (struct pt_regs *) addr;
122         kdb_printf("struct pt_regs %p-%p\n", p, (unsigned char *)p + sizeof(*p) - 1);
123         kdb_print_nameval("b6", p->b6);
124         kdb_print_nameval("b7", p->b7);
125         kdb_printf("  ar_csd      0x%lx\n", p->ar_csd);
126         kdb_printf("  ar_ssd      0x%lx\n", p->ar_ssd);
127         kdb_print_nameval("r8", p->r8);
128         kdb_print_nameval("r9", p->r9);
129         kdb_print_nameval("r10", p->r10);
130         kdb_print_nameval("r11", p->r11);
131         kdb_printf("  cr_ipsr     0x%lx\n", p->cr_ipsr);
132         kdb_print_nameval("cr_iip", p->cr_iip);
133         kdb_printf("  cr_ifs      0x%lx\n", p->cr_ifs);
134         kdb_printf("  ar_unat     0x%lx\n", p->ar_unat);
135         kdb_printf("  ar_pfs      0x%lx\n", p->ar_pfs);
136         kdb_printf("  ar_rsc      0x%lx\n", p->ar_rsc);
137         kdb_printf("  ar_rnat     0x%lx\n", p->ar_rnat);
138         kdb_printf("  ar_bspstore 0x%lx\n", p->ar_bspstore);
139         kdb_printf("  pr          0x%lx\n", p->pr);
140         kdb_print_nameval("b0", p->b0);
141         kdb_printf("  loadrs      0x%lx\n", p->loadrs);
142         kdb_print_nameval("r1", p->r1);
143         kdb_print_nameval("r12", p->r12);
144         kdb_print_nameval("r13", p->r13);
145         kdb_printf("  ar_fpsr     0x%lx\n", p->ar_fpsr);
146         kdb_print_nameval("r15", p->r15);
147         kdb_print_nameval("r14", p->r14);
148         kdb_print_nameval("r2", p->r2);
149         kdb_print_nameval("r3", p->r3);
150         kdb_print_nameval("r16", p->r16);
151         kdb_print_nameval("r17", p->r17);
152         kdb_print_nameval("r18", p->r18);
153         kdb_print_nameval("r19", p->r19);
154         kdb_print_nameval("r20", p->r20);
155         kdb_print_nameval("r21", p->r21);
156         kdb_print_nameval("r22", p->r22);
157         kdb_print_nameval("r23", p->r23);
158         kdb_print_nameval("r24", p->r24);
159         kdb_print_nameval("r25", p->r25);
160         kdb_print_nameval("r26", p->r26);
161         kdb_print_nameval("r27", p->r27);
162         kdb_print_nameval("r28", p->r28);
163         kdb_print_nameval("r29", p->r29);
164         kdb_print_nameval("r30", p->r30);
165         kdb_print_nameval("r31", p->r31);
166         kdb_printf("  ar_ccv      0x%lx\n", p->ar_ccv);
167         kdb_printf("  f6          0x%lx 0x%lx\n", p->f6.u.bits[0], p->f6.u.bits[1]);
168         kdb_printf("  f7          0x%lx 0x%lx\n", p->f7.u.bits[0], p->f7.u.bits[1]);
169         kdb_printf("  f8          0x%lx 0x%lx\n", p->f8.u.bits[0], p->f8.u.bits[1]);
170         kdb_printf("  f9          0x%lx 0x%lx\n", p->f9.u.bits[0], p->f9.u.bits[1]);
171         kdb_printf("  f10         0x%lx 0x%lx\n", p->f10.u.bits[0], p->f10.u.bits[1]);
172         kdb_printf("  f11         0x%lx 0x%lx\n", p->f11.u.bits[0], p->f11.u.bits[1]);
173
174         return 0;
175 }
176
177 /*
178  * kdba_stackdepth
179  *
180  *      Print processes that are using more than a specific percentage of their
181  *      stack.
182  *
183  * Inputs:
184  *      argc    argument count
185  *      argv    argument vector
186  * Outputs:
187  *      None.
188  * Returns:
189  *      zero for success, a kdb diagnostic if error
190  * Locking:
191  *      none.
192  * Remarks:
193  *      If no percentage is supplied, it uses 60.
194  */
195
196 static int
197 kdba_stackdepth(int argc, const char **argv)
198 {
199         int diag, threshold, used;
200         unsigned long percentage;
201         unsigned long esp;
202         long offset = 0;
203         int nextarg;
204         struct task_struct *p, *g;
205         struct switch_stack *sw;
206
207         if (argc == 0) {
208                 percentage = 60;
209         } else if (argc == 1) {
210                 nextarg = 1;
211                 diag = kdbgetaddrarg(argc, argv, &nextarg, &percentage, &offset, NULL);
212                 if (diag)
213                         return diag;
214         } else {
215                 return KDB_ARGCOUNT;
216         }
217         percentage = max_t(int, percentage, 1);
218         percentage = min_t(int, percentage, 100);
219         threshold = ((2 * THREAD_SIZE * percentage) / 100 + 1) >> 1;
220         kdb_printf("stackdepth: processes using more than %ld%% (%d bytes) of stack\n",
221                 percentage, threshold);
222         kdb_do_each_thread(g, p) {
223                 if (kdb_task_has_cpu(p)) {
224                         struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
225                         if (krp->seqno)
226                                 sw = krp->arch.sw;
227                         else
228                                 sw = NULL;
229                 } else
230                         sw = (struct switch_stack *) (p->thread.ksp + 16);
231                 if (!sw)
232                         continue;
233                 esp = (unsigned long) sw;
234                 used = THREAD_SIZE - (esp - sw->ar_bspstore);
235                 if (used >= threshold) {
236                         kdb_ps1(p);
237                         kdb_printf("  esp %lx bsp %lx used %d\n", esp, sw->ar_bspstore, used);
238                 }
239         } kdb_while_each_thread(g, p);
240
241         return 0;
242 }
243
244 /*
245  * kdb_switch_stack
246  *
247  *      Format a struct switch_stack
248  *
249  * Inputs:
250  *      argc    argument count
251  *      argv    argument vector
252  * Outputs:
253  *      None.
254  * Returns:
255  *      zero for success, a kdb diagnostic if error
256  * Locking:
257  *      none.
258  * Remarks:
259  *      If no address is supplied, it uses kdb_running_process[smp_processor_id()].arch.sw.
260  */
261
262 static int
263 kdba_switch_stack(int argc, const char **argv)
264 {
265         int diag;
266         kdb_machreg_t addr;
267         long offset = 0;
268         int nextarg;
269         struct switch_stack *p;
270
271         if (argc == 0) {
272                 addr = (kdb_machreg_t) kdb_running_process[smp_processor_id()].arch.sw;
273         } else if (argc == 1) {
274                 nextarg = 1;
275                 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
276                 if (diag)
277                         return diag;
278         } else {
279                 return KDB_ARGCOUNT;
280         }
281
282         p = (struct switch_stack *) addr;
283         kdb_printf("struct switch_stack %p-%p\n", p, (unsigned char *)p + sizeof(*p) - 1);
284         kdb_printf("  caller_unat 0x%lx\n", p->caller_unat);
285         kdb_printf("  ar_fpsr     0x%lx\n", p->ar_fpsr);
286         kdb_printf("  f2          0x%lx 0x%lx\n", p->f2.u.bits[0], p->f2.u.bits[1]);
287         kdb_printf("  f3          0x%lx 0x%lx\n", p->f3.u.bits[0], p->f3.u.bits[1]);
288         kdb_printf("  f4          0x%lx 0x%lx\n", p->f4.u.bits[0], p->f4.u.bits[1]);
289         kdb_printf("  f5          0x%lx 0x%lx\n", p->f5.u.bits[0], p->f5.u.bits[1]);
290         kdb_printf("  f12         0x%lx 0x%lx\n", p->f12.u.bits[0], p->f12.u.bits[1]);
291         kdb_printf("  f13         0x%lx 0x%lx\n", p->f13.u.bits[0], p->f13.u.bits[1]);
292         kdb_printf("  f14         0x%lx 0x%lx\n", p->f14.u.bits[0], p->f14.u.bits[1]);
293         kdb_printf("  f15         0x%lx 0x%lx\n", p->f15.u.bits[0], p->f15.u.bits[1]);
294         kdb_printf("  f16         0x%lx 0x%lx\n", p->f16.u.bits[0], p->f16.u.bits[1]);
295         kdb_printf("  f17         0x%lx 0x%lx\n", p->f17.u.bits[0], p->f17.u.bits[1]);
296         kdb_printf("  f18         0x%lx 0x%lx\n", p->f18.u.bits[0], p->f18.u.bits[1]);
297         kdb_printf("  f19         0x%lx 0x%lx\n", p->f19.u.bits[0], p->f19.u.bits[1]);
298         kdb_printf("  f20         0x%lx 0x%lx\n", p->f20.u.bits[0], p->f20.u.bits[1]);
299         kdb_printf("  f21         0x%lx 0x%lx\n", p->f21.u.bits[0], p->f21.u.bits[1]);
300         kdb_printf("  f22         0x%lx 0x%lx\n", p->f22.u.bits[0], p->f22.u.bits[1]);
301         kdb_printf("  f23         0x%lx 0x%lx\n", p->f23.u.bits[0], p->f23.u.bits[1]);
302         kdb_printf("  f24         0x%lx 0x%lx\n", p->f24.u.bits[0], p->f24.u.bits[1]);
303         kdb_printf("  f25         0x%lx 0x%lx\n", p->f25.u.bits[0], p->f25.u.bits[1]);
304         kdb_printf("  f26         0x%lx 0x%lx\n", p->f26.u.bits[0], p->f26.u.bits[1]);
305         kdb_printf("  f27         0x%lx 0x%lx\n", p->f27.u.bits[0], p->f27.u.bits[1]);
306         kdb_printf("  f28         0x%lx 0x%lx\n", p->f28.u.bits[0], p->f28.u.bits[1]);
307         kdb_printf("  f29         0x%lx 0x%lx\n", p->f29.u.bits[0], p->f29.u.bits[1]);
308         kdb_printf("  f30         0x%lx 0x%lx\n", p->f30.u.bits[0], p->f30.u.bits[1]);
309         kdb_printf("  f31         0x%lx 0x%lx\n", p->f31.u.bits[0], p->f31.u.bits[1]);
310         kdb_print_nameval("r4", p->r4);
311         kdb_print_nameval("r5", p->r5);
312         kdb_print_nameval("r6", p->r6);
313         kdb_print_nameval("r7", p->r7);
314         kdb_print_nameval("b0", p->b0);
315         kdb_print_nameval("b1", p->b1);
316         kdb_print_nameval("b2", p->b2);
317         kdb_print_nameval("b3", p->b3);
318         kdb_print_nameval("b4", p->b4);
319         kdb_print_nameval("b5", p->b5);
320         kdb_printf("  ar_pfs      0x%lx\n", p->ar_pfs);
321         kdb_printf("  ar_lc       0x%lx\n", p->ar_lc);
322         kdb_printf("  ar_unat     0x%lx\n", p->ar_unat);
323         kdb_printf("  ar_rnat     0x%lx\n", p->ar_rnat);
324         kdb_printf("  ar_bspstore 0x%lx\n", p->ar_bspstore);
325         kdb_printf("  pr          0x%lx\n", p->pr);
326
327         return 0;
328 }
329
330 /*
331  * kdb_minstate
332  *
333  *      Format the PAL minstate area.
334  *
335  * Inputs:
336  *      argc    argument count
337  *      argv    argument vector
338  * Outputs:
339  *      None.
340  * Returns:
341  *      zero for success, a kdb diagnostic if error
342  * Locking:
343  *      none.
344  * Remarks:
345  *      None.
346  */
347
348 static int
349 kdba_minstate(int argc, const char **argv)
350 {
351         int diag;
352         kdb_machreg_t addr;
353         long offset = 0;
354         int nextarg;
355         pal_min_state_area_t *p;
356
357         if (argc == 1) {
358                 nextarg = 1;
359                 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
360                 if (diag)
361                         return diag;
362         } else {
363                 return KDB_ARGCOUNT;
364         }
365
366         p = (pal_min_state_area_t *) addr;
367         kdb_printf("PAL minstate %p-%p\n", p, (unsigned char *)p + sizeof(*p) - 1);
368         kdb_printf("  pmsa_nat_bits 0x%lx\n", p->pmsa_nat_bits);
369         kdb_print_nameval("r1", p->pmsa_gr[1-1]);
370         kdb_print_nameval("r2", p->pmsa_gr[2-1]);
371         kdb_print_nameval("r3", p->pmsa_gr[3-1]);
372         kdb_print_nameval("r4", p->pmsa_gr[4-1]);
373         kdb_print_nameval("r5", p->pmsa_gr[5-1]);
374         kdb_print_nameval("r6", p->pmsa_gr[6-1]);
375         kdb_print_nameval("r7", p->pmsa_gr[7-1]);
376         kdb_print_nameval("r8", p->pmsa_gr[8-1]);
377         kdb_print_nameval("r9", p->pmsa_gr[9-1]);
378         kdb_print_nameval("r10", p->pmsa_gr[10-1]);
379         kdb_print_nameval("r11", p->pmsa_gr[11-1]);
380         kdb_print_nameval("r12", p->pmsa_gr[12-1]);
381         kdb_print_nameval("r13", p->pmsa_gr[13-1]);
382         kdb_print_nameval("r14", p->pmsa_gr[14-1]);
383         kdb_print_nameval("r15", p->pmsa_gr[15-1]);
384         kdb_printf("  Bank 0\n");
385         kdb_print_nameval("r16", p->pmsa_bank0_gr[16-16]);
386         kdb_print_nameval("r17", p->pmsa_bank0_gr[17-16]);
387         kdb_print_nameval("r18", p->pmsa_bank0_gr[18-16]);
388         kdb_print_nameval("r19", p->pmsa_bank0_gr[19-16]);
389         kdb_print_nameval("r20", p->pmsa_bank0_gr[20-16]);
390         kdb_print_nameval("r21", p->pmsa_bank0_gr[21-16]);
391         kdb_print_nameval("r22", p->pmsa_bank0_gr[22-16]);
392         kdb_print_nameval("r23", p->pmsa_bank0_gr[23-16]);
393         kdb_print_nameval("r24", p->pmsa_bank0_gr[24-16]);
394         kdb_print_nameval("r25", p->pmsa_bank0_gr[25-16]);
395         kdb_print_nameval("r26", p->pmsa_bank0_gr[26-16]);
396         kdb_print_nameval("r27", p->pmsa_bank0_gr[27-16]);
397         kdb_print_nameval("r28", p->pmsa_bank0_gr[28-16]);
398         kdb_print_nameval("r29", p->pmsa_bank0_gr[29-16]);
399         kdb_print_nameval("r30", p->pmsa_bank0_gr[30-16]);
400         kdb_print_nameval("r31", p->pmsa_bank0_gr[31-16]);
401         kdb_printf("  Bank 1\n");
402         kdb_print_nameval("r16", p->pmsa_bank1_gr[16-16]);
403         kdb_print_nameval("r17", p->pmsa_bank1_gr[17-16]);
404         kdb_print_nameval("r18", p->pmsa_bank1_gr[18-16]);
405         kdb_print_nameval("r19", p->pmsa_bank1_gr[19-16]);
406         kdb_print_nameval("r20", p->pmsa_bank1_gr[20-16]);
407         kdb_print_nameval("r21", p->pmsa_bank1_gr[21-16]);
408         kdb_print_nameval("r22", p->pmsa_bank1_gr[22-16]);
409         kdb_print_nameval("r23", p->pmsa_bank1_gr[23-16]);
410         kdb_print_nameval("r24", p->pmsa_bank1_gr[24-16]);
411         kdb_print_nameval("r25", p->pmsa_bank1_gr[25-16]);
412         kdb_print_nameval("r26", p->pmsa_bank1_gr[26-16]);
413         kdb_print_nameval("r27", p->pmsa_bank1_gr[27-16]);
414         kdb_print_nameval("r28", p->pmsa_bank1_gr[28-16]);
415         kdb_print_nameval("r29", p->pmsa_bank1_gr[29-16]);
416         kdb_print_nameval("r30", p->pmsa_bank1_gr[30-16]);
417         kdb_print_nameval("r31", p->pmsa_bank1_gr[31-16]);
418         kdb_printf("  pr          0x%lx\n", p->pmsa_pr);
419         kdb_print_nameval("b0", p->pmsa_br0);
420         kdb_printf("  ar.rsc      0x%lx\n", p->pmsa_rsc);
421         kdb_print_nameval("cr.iip", p->pmsa_iip);
422         kdb_printf("  cr.ipsr     0x%lx\n", p->pmsa_ipsr);
423         kdb_printf("  cr.ifs      0x%lx\n", p->pmsa_ifs);
424         kdb_print_nameval("cr.xip", p->pmsa_xip);
425         kdb_printf("  cr.xpsr     0x%lx\n", p->pmsa_xpsr);
426         kdb_printf("  cr.xfs      0x%lx\n", p->pmsa_xfs);
427         kdb_print_nameval("b1", p->pmsa_br1);
428
429         return 0;
430 }
431
432 /*
433  * kdba_cpuinfo
434  *
435  *      Format struct cpuinfo_ia64.
436  *
437  * Inputs:
438  *      argc    argument count
439  *      argv    argument vector
440  * Outputs:
441  *      None.
442  * Returns:
443  *      zero for success, a kdb diagnostic if error
444  * Locking:
445  *      none.
446  * Remarks:
447  *      If no cpu is supplied, it prints cpuinfo for all online cpus.
448  */
449
450 static int
451 kdba_cpuinfo(int argc, const char **argv)
452 {
453         int diag;
454         unsigned long cpunum = -1;
455         long offset = 0;
456         int nextarg, c, i;
457         struct cpuinfo_ia64 *cpuinfo;
458
459         if (argc == 1) {
460                 nextarg = 1;
461                 diag = kdbgetaddrarg(argc, argv, &nextarg, &cpunum, &offset, NULL);
462                 if (diag)
463                         return diag;
464                 if (cpunum >= NR_CPUS || !cpu_online(cpunum))
465                         return KDB_BADCPUNUM;
466         } else if (argc > 1) {
467                 return KDB_ARGCOUNT;
468         }
469
470         for (c = (cpunum == -1 ? 0 : cpunum);
471              c < (cpunum == -1 ? NR_CPUS : cpunum+1);
472              ++c) {
473                 if (!cpu_online(c))
474                         continue;
475                 cpuinfo = cpu_data(c);
476                 kdb_printf("struct cpuinfo_ia64 for cpu %d is at 0x%p\n", c, cpuinfo);
477                 kdb_printf("  softirq_pending  0x%x\n",   cpuinfo->softirq_pending);
478                 kdb_printf("  itm_delta        %ld\n",    cpuinfo->itm_delta);
479                 kdb_printf("  itm_next         %ld\n",    cpuinfo->itm_next);
480                 kdb_printf("  nsec_per_cyc     %ld\n",    cpuinfo->nsec_per_cyc);
481                 kdb_printf("  unimpl_va_mask   0x%lx\n",  cpuinfo->unimpl_va_mask);
482                 kdb_printf("  unimpl_pa_mask   0x%lx\n",  cpuinfo->unimpl_pa_mask);
483                 kdb_printf("  itc_freq         %ld\n",    cpuinfo->itc_freq);
484                 kdb_printf("  proc_freq        %ld\n",    cpuinfo->proc_freq);
485                 kdb_printf("  cyc_per_usec     %ld\n",    cpuinfo->cyc_per_usec);
486                 kdb_printf("  cyc_per_usec     %ld\n",    cpuinfo->cyc_per_usec);
487 #if 0   /* RJA per-cpu MCA */
488                 kdb_printf("  percpu_paddr     0x%lx\n",  cpuinfo->percpu_paddr);
489 #endif
490                 kdb_printf("  ptce_base        0x%lx\n",  cpuinfo->ptce_base);
491                 kdb_printf("  ptce_count       %d %d\n",  cpuinfo->ptce_count[0], cpuinfo->ptce_count[1]);
492                 kdb_printf("  ptce_stride      %d %d\n",  cpuinfo->ptce_stride[0], cpuinfo->ptce_stride[1]);
493 #if 0   /* RJA per-cpu MCA */
494                 kdb_printf("  pal_paddr        0x%lx\n",  cpuinfo->pal_paddr);
495                 kdb_printf("  pal_base         0x%lx\n",  cpuinfo->pal_base);
496 #endif
497                 kdb_printf("  ksoftirqd        0x%p\n",   cpuinfo->ksoftirqd);
498 #ifdef CONFIG_SMP
499                 kdb_printf("  loops_per_jiffy  %ld\n",    cpuinfo->loops_per_jiffy);
500                 kdb_printf("  cpu              %d\n",     cpuinfo->cpu);
501                 kdb_printf("  socket_id        %d\n",     cpuinfo->socket_id);
502                 kdb_printf("  core_id          %d\n",     cpuinfo->core_id);
503                 kdb_printf("  thread_id        %d\n",     cpuinfo->thread_id);
504                 kdb_printf("  num_log          %d\n",     cpuinfo->num_log);
505                 kdb_printf("  cores_per_socket %d\n",     cpuinfo->cores_per_socket);
506                 kdb_printf("  threads_per_core %d\n",     cpuinfo->threads_per_core);
507 #endif
508                 kdb_printf("  ppn              0x%lx\n",  cpuinfo->ppn);
509                 kdb_printf("  features         0x%lx\n",  cpuinfo->features);
510                 kdb_printf("  number           %d\n",     cpuinfo->number);
511                 kdb_printf("  revision         %d\n",     cpuinfo->revision);
512                 kdb_printf("  model            %d\n",     cpuinfo->model);
513                 kdb_printf("  family           %d\n",     cpuinfo->family);
514                 kdb_printf("  archrev          %d\n",     cpuinfo->archrev);
515                 kdb_printf("  vendor          ");
516                 for (i = 0; i < sizeof(cpuinfo->vendor); ++i)
517                         kdb_printf(" 0x%02x", cpuinfo->vendor[i]);
518                 kdb_printf("\n");
519 #ifdef CONFIG_NUMA
520                 kdb_printf("  node_data        0x%p\n",   cpuinfo->node_data);
521 #endif
522 #if 0   /* RJA per-cpu MCA */
523                 kdb_printf("  ia64_pa_mca_data 0x%p\n",   cpuinfo->ia64_pa_mca_data);
524 #endif
525         }
526         return 0;
527 }
528
529 #ifdef CONFIG_KDB_HARDWARE_BREAKPOINTS
530 void
531 kdba_installdbreg(kdb_bp_t *bp)
532 {
533         unsigned long mask;
534         unsigned int regbase;
535         static unsigned long masks[] = {
536             0x00FFFFFFFFFFFFFFUL,   // 1 byte long
537             0x00FFFFFFFFFFFFFEUL,   // 2 bytes long
538             0x0000000000000000UL,   // invalid
539             0x00FFFFFFFFFFFFFCUL    // 4 bytes long
540         };
541         static unsigned char modes[] = {
542             0x81,                   // instruction => x, plm=priv level 0 only
543             0x41,                   // write => w, plm=priv level 0 only
544             0x00,                   // io
545             0x81                    // read => r, plm=priv level 0 only
546         };
547
548         /* Note that bp->bp_hard[NR_CPU] is for x86.
549          * The ia64 uses bp->bp_hard[0] only.
550          */
551         if (KDB_DEBUG(BP))
552                 kdb_printf("kdba_installdbreg:\n");
553         mask = masks[bp->bp_hard[0]->bph_length] |
554                (((unsigned long)(modes[bp->bp_hard[0]->bph_mode])) << 56);
555         regbase = 2*bp->bp_hard[0]->bph_reg;
556
557         switch (bp->bp_hard[0]->bph_mode)
558         {
559         case 1:
560         case 3:
561                 if (KDB_DEBUG(BP)) {
562                         kdb_printf("kdba_installdbreg: dbr[%u]=%016lx\n",
563                                         regbase, bp->bp_addr);
564                         kdb_printf("kdba_installdbreg: dbr[%u]=%016lx\n",
565                                         regbase+1, mask);
566                 }
567
568                 ia64_set_dbr(regbase, bp->bp_addr);
569                 ia64_set_dbr(regbase+1, mask);
570                 ia64_srlz_d();
571                 break;
572
573         case 0: /* instruction */
574 #if 0
575                 ia64_set_ibr(regbase, bp->bp_addr);
576                 ia64_set_ibr(regbase+1, mask);
577                 ia64_srlz_d();
578 #else
579                 kdb_printf("\"instr\" mode not implemented\n");
580 #endif
581                 break;
582
583         case 2: /* io */
584                 kdb_printf("\"io\" mode not implemented\n");
585                 break;
586         }
587 }
588
589 void
590 kdba_removedbreg(kdb_bp_t *bp)
591 {
592         unsigned int regbase = 2*bp->bp_hard[0]->bph_reg;
593
594         /* Note that bp->bp_hard[NR_CPU] is for x86.
595          * The ia64 uses bp->bp_hard[0] only.
596          */
597         switch (bp->bp_hard[0]->bph_mode)
598         {
599         case 1:
600         case 3:
601                 ia64_set_dbr(regbase, 0);
602                 ia64_set_dbr(regbase+1, 0);
603                 ia64_srlz_d();
604                 break;
605
606         case 0: /* instruction */
607 #if 0
608                 ia64_set_ibr(regbase, 0);
609                 ia64_set_ibr(regbase+1, 0);
610                 ia64_srlz_d();
611 #else
612                 kdb_printf("\"instr\" mode not implemented\n");
613 #endif
614                 break;
615
616         case 2: /* io */
617                 kdb_printf("\"io\" mode not implemented\n");
618                 break;
619         }
620 }
621 #endif /* CONFIG_KDB_HARDWARE_BREAKPOINTS */
622
623
624 static kdb_machreg_t
625 kdba_getdr(int regnum)
626 {
627         kdb_machreg_t contents = 0;
628         unsigned long reg = (unsigned long)regnum;
629
630         __asm__ ("mov %0=ibr[%1]"::"r"(contents),"r"(reg));
631 //        __asm__ ("mov ibr[%0]=%1"::"r"(dbreg_cond),"r"(value));
632
633         return contents;
634 }
635
636
637 static void
638 get_fault_regs(fault_regs_t *fr)
639 {
640         fr->ifa = 0 ;
641         fr->isr = 0 ;
642
643         __asm__ ("rsm psr.ic;;") ;
644         ia64_srlz_d();
645         __asm__ ("mov %0=cr.ifa" : "=r"(fr->ifa));
646         __asm__ ("mov %0=cr.isr" : "=r"(fr->isr));
647         __asm__ ("ssm psr.ic;;") ;
648         ia64_srlz_d();
649 }
650
651 static void
652 show_kernel_regs (void)
653 {
654         unsigned long kr[8];
655         int i;
656
657         asm ("mov %0=ar.k0" : "=r"(kr[0])); asm ("mov %0=ar.k1" : "=r"(kr[1]));
658         asm ("mov %0=ar.k2" : "=r"(kr[2])); asm ("mov %0=ar.k3" : "=r"(kr[3]));
659         asm ("mov %0=ar.k4" : "=r"(kr[4])); asm ("mov %0=ar.k5" : "=r"(kr[5]));
660         asm ("mov %0=ar.k6" : "=r"(kr[6])); asm ("mov %0=ar.k7" : "=r"(kr[7]));
661
662         for (i = 0; i < 4; ++i)
663                 kdb_printf(" kr%d: %016lx  kr%d: %016lx\n", 2*i, kr[2*i], 2*i+1, kr[2*i+1]);
664         kdb_printf("\n");
665 }
666
667 static int
668 change_cur_stack_frame(int regno, unsigned long *contents)
669 {
670         unsigned long sof, i, cfm, sp, *bsp, __user *ubsp;
671         struct unw_frame_info info;
672         mm_segment_t old_fs;
673         int cpu = kdb_process_cpu(kdb_current_task);
674         struct kdb_running_process *krp = kdb_running_process + cpu;
675
676         if (kdb_current_task != krp->p) {
677                 kdb_printf("Stacked registers are not available for tasks that are not running.\n");
678                 kdb_printf("Use bt with a large BTARGS value instead\n");
679                 return 0;
680         }
681         unw_init_frame_info(&info, krp->p, krp->arch.sw);
682         do {
683                 if (unw_unwind(&info) < 0) {
684                         kdb_printf("Failed to unwind\n");
685                         return 0;
686                 }
687                 unw_get_sp(&info, &sp);
688         } while (sp <= (unsigned long) kdb_current_regs);
689         unw_get_bsp(&info, (unsigned long *) &bsp);
690         unw_get_cfm(&info, &cfm);
691
692         if (!bsp) {
693                 kdb_printf("Unable to get Current Stack Frame\n");
694                 return 0;
695         }
696
697         sof = (cfm & 0x7f);
698
699         if(((unsigned long)regno - 32) >= (sof - 2)) return 1;
700
701         old_fs = set_fs(KERNEL_DS);
702         for (i = 0; i < (regno - 32); ++i)
703                 bsp = ia64_rse_skip_regs(bsp, 1);
704         ubsp = (unsigned long __user *) bsp;
705         put_user(*contents, ubsp);
706         set_fs(old_fs);
707
708         return 0 ;
709 }
710
711 static int
712 show_cur_stack_frame(int regno, unsigned long *contents)
713 {
714         unsigned long sof, i, cfm, val, sp, *bsp, __user *ubsp;
715         struct unw_frame_info info;
716         mm_segment_t old_fs;
717         int cpu = kdb_process_cpu(kdb_current_task);
718         struct kdb_running_process *krp = kdb_running_process + cpu;
719
720         if (kdb_current_task != krp->p) {
721                 kdb_printf("Stacked registers are not available for tasks that are not running.\n");
722                 kdb_printf("Use bt with a large BTARGS value instead\n");
723                 return 0;
724         }
725         unw_init_frame_info(&info, krp->p, krp->arch.sw);
726         do {
727                 if (unw_unwind(&info) < 0) {
728                         kdb_printf("Failed to unwind\n");
729                         return 0;
730                 }
731                 unw_get_sp(&info, &sp);
732         } while (sp <= (unsigned long) kdb_current_regs);
733         unw_get_bsp(&info, (unsigned long *) &bsp);
734         unw_get_cfm(&info, &cfm);
735
736         if (!bsp) {
737                 kdb_printf("Unable to display Current Stack Frame\n");
738                 return 0;
739         }
740
741         sof = (cfm & 0x7f);
742
743         if (regno) {
744                 if ((unsigned) regno - 32 >= sof)
745                         return 0;
746                 bsp = ia64_rse_skip_regs(bsp, regno - 32);
747                 old_fs = set_fs(KERNEL_DS);
748                 ubsp = (unsigned long __user *) bsp;
749                 get_user(val, ubsp);
750                 set_fs(old_fs);
751                 *contents = val;
752                 return 1;
753         }
754
755         old_fs = set_fs(KERNEL_DS);
756         for (i = 0; i < sof; ++i) {
757                 ubsp = (unsigned long __user *) bsp;
758                 get_user(val, ubsp);
759                 kdb_printf(" r%lu: %016lx ", 32 + i, val);
760                 if (!((i + 1) % 3))
761                         kdb_printf("\n");
762                 bsp = ia64_rse_skip_regs(bsp, 1);
763         }
764         kdb_printf("\n");
765         set_fs(old_fs);
766
767         return 0 ;
768 }
769
770 /*
771  * kdba_getregcontents
772  *
773  *      Return 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  *      The following pseudo register names are supported:
778  *         &regs         - Prints address of exception frame
779  *         kesp          - Prints kernel stack pointer at time of fault
780  *         sstk          - Prints switch stack for ia64
781  *         %<regname>    - Uses the value of the registers at the
782  *                         last time the user process entered kernel
783  *                         mode, instead of the registers at the time
784  *                         kdb was entered.
785  *
786  * Parameters:
787  *      regname         Pointer to string naming register
788  *      regs            Pointer to structure containing registers.
789  * Outputs:
790  *      *contents       Pointer to unsigned long to recieve register contents
791  * Returns:
792  *      0               Success
793  *      KDB_BADREG      Invalid register name
794  * Locking:
795  *      None.
796  * Remarks:
797  *
798  *      Note that this function is really machine independent.   The kdb
799  *      register list is not, however.
800  */
801
802 static struct kdbregs {
803         char   *reg_name;
804         size_t  reg_offset;
805 } kdbreglist[] = {
806         { "psr",        offsetof(struct pt_regs, cr_ipsr) },
807         { "ifs",        offsetof(struct pt_regs, cr_ifs) },
808         { "ip", offsetof(struct pt_regs, cr_iip) },
809
810         { "unat",       offsetof(struct pt_regs, ar_unat) },
811         { "pfs",        offsetof(struct pt_regs, ar_pfs) },
812         { "rsc",        offsetof(struct pt_regs, ar_rsc) },
813
814         { "rnat",       offsetof(struct pt_regs, ar_rnat) },
815         { "bsps",       offsetof(struct pt_regs, ar_bspstore) },
816         { "pr", offsetof(struct pt_regs, pr) },
817
818         { "ldrs",       offsetof(struct pt_regs, loadrs) },
819         { "ccv",        offsetof(struct pt_regs, ar_ccv) },
820         { "fpsr",       offsetof(struct pt_regs, ar_fpsr) },
821
822         { "b0", offsetof(struct pt_regs, b0) },
823         { "b6", offsetof(struct pt_regs, b6) },
824         { "b7", offsetof(struct pt_regs, b7) },
825
826         { "r1",offsetof(struct pt_regs, r1) },
827         { "r2",offsetof(struct pt_regs, r2) },
828         { "r3",offsetof(struct pt_regs, r3) },
829
830         { "r8",offsetof(struct pt_regs, r8) },
831         { "r9",offsetof(struct pt_regs, r9) },
832         { "r10",offsetof(struct pt_regs, r10) },
833
834         { "r11",offsetof(struct pt_regs, r11) },
835         { "r12",offsetof(struct pt_regs, r12) },
836         { "r13",offsetof(struct pt_regs, r13) },
837
838         { "r14",offsetof(struct pt_regs, r14) },
839         { "r15",offsetof(struct pt_regs, r15) },
840         { "r16",offsetof(struct pt_regs, r16) },
841
842         { "r17",offsetof(struct pt_regs, r17) },
843         { "r18",offsetof(struct pt_regs, r18) },
844         { "r19",offsetof(struct pt_regs, r19) },
845
846         { "r20",offsetof(struct pt_regs, r20) },
847         { "r21",offsetof(struct pt_regs, r21) },
848         { "r22",offsetof(struct pt_regs, r22) },
849
850         { "r23",offsetof(struct pt_regs, r23) },
851         { "r24",offsetof(struct pt_regs, r24) },
852         { "r25",offsetof(struct pt_regs, r25) },
853
854         { "r26",offsetof(struct pt_regs, r26) },
855         { "r27",offsetof(struct pt_regs, r27) },
856         { "r28",offsetof(struct pt_regs, r28) },
857
858         { "r29",offsetof(struct pt_regs, r29) },
859         { "r30",offsetof(struct pt_regs, r30) },
860         { "r31",offsetof(struct pt_regs, r31) },
861
862 };
863
864 static const int nkdbreglist = sizeof(kdbreglist) / sizeof(struct kdbregs);
865
866 int
867 kdba_getregcontents(const char *regname, struct pt_regs *regs, unsigned long *contents)
868 {
869         int i;
870
871         if (strcmp(regname, "isr") == 0) {
872                 fault_regs_t fr ;
873                 get_fault_regs(&fr) ;
874                 *contents = fr.isr ;
875                 return 0 ;
876         }
877
878         if (!regs) {
879                 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
880                 return KDB_BADREG;
881         }
882
883         if (strcmp(regname, "&regs") == 0) {
884                 *contents = (unsigned long)regs;
885                 return 0;
886         }
887
888         if (strcmp(regname, "sstk") == 0) {
889                 *contents = (unsigned long)getprsregs(regs) ;
890                 return 0;
891         }
892
893         if (strcmp(regname, "ksp") == 0) {
894                 *contents = (unsigned long) (regs + 1);
895                 return 0;
896         }
897
898         for (i=0; i<nkdbreglist; i++) {
899                 if (strstr(kdbreglist[i].reg_name, regname))
900                         break;
901         }
902
903         if (i == nkdbreglist) {
904                 /* Lets check the rse maybe */
905                 if (regname[0] == 'r')
906                         if (show_cur_stack_frame(simple_strtoul(regname+1, NULL, 0), contents))
907                                 return 0 ;
908                 return KDB_BADREG;
909         }
910
911         *contents = *(unsigned long *)((unsigned long)regs +
912                         kdbreglist[i].reg_offset);
913
914         return 0;
915 }
916
917 /*
918  * kdba_setregcontents
919  *
920  *      Set the contents of the register specified by the
921  *      input string argument.   Return an error if the string
922  *      does not match a machine register.
923  *
924  *      Supports modification of user-mode registers via
925  *      %<register-name>
926  *
927  * Parameters:
928  *      regname         Pointer to string naming register
929  *      regs            Pointer to structure containing registers.
930  *      contents        Unsigned long containing new register contents
931  * Outputs:
932  * Returns:
933  *      0               Success
934  *      KDB_BADREG      Invalid register name
935  * Locking:
936  *      None.
937  * Remarks:
938  */
939
940 int
941 kdba_setregcontents(const char *regname,
942                   struct pt_regs *regs,
943                   unsigned long contents)
944 {
945         int i, ret = 0, fixed = 0;
946         char *endp;
947         unsigned long regno;
948
949         if (regname[0] == '%') {
950                 regname++;
951                 regs = (struct pt_regs *)
952                         (kdb_current_task->thread.ksp - sizeof(struct pt_regs));
953         }
954
955         if (!regs) {
956                 kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
957                 return KDB_BADREG;
958         }
959
960         /* fixed registers */
961         for (i=0; i<nkdbreglist; i++) {
962                 if (strnicmp(kdbreglist[i].reg_name,
963                              regname,
964                              strlen(regname)) == 0) {
965                         fixed = 1;
966                         break;
967                 }
968         }
969
970         /* stacked registers */
971         if (!fixed) {
972                 regno = (simple_strtoul(&regname[1], &endp, 10));
973                 if ((regname[0] == 'r') && regno > (unsigned long)31) {
974                         ret = change_cur_stack_frame(regno, &contents);
975                         if(!ret) return 0;
976                 }
977         }
978
979         if ((i == nkdbreglist)
980             || (strlen(kdbreglist[i].reg_name) != strlen(regname))
981             || ret) {
982                 return KDB_BADREG;
983         }
984
985         /* just in case of "standard" register */
986         *(unsigned long *)((unsigned long)regs + kdbreglist[i].reg_offset) =
987                 contents;
988
989         return 0;
990 }
991
992 /*
993  * kdba_dumpregs
994  *
995  *      Dump the specified register set to the display.
996  *
997  * Parameters:
998  *      regs            Pointer to structure containing registers.
999  *      type            Character string identifying register set to dump
1000  *      extra           string further identifying register (optional)
1001  * Outputs:
1002  * Returns:
1003  *      0               Success
1004  * Locking:
1005  *      None.
1006  * Remarks:
1007  *      This function will dump the general register set if the type
1008  *      argument is NULL (struct pt_regs).   The alternate register
1009  *      set types supported by this function:
1010  *
1011  *      d               Debug registers
1012  *      c               Control registers
1013  *      u               User registers at most recent entry to kernel
1014  *      i               Interrupt registers -- same as "irr" command
1015  * Following not yet implemented:
1016  *      m               Model Specific Registers (extra defines register #)
1017  *      r               Memory Type Range Registers (extra defines register)
1018  *
1019  *      For now, all registers are covered as follows:
1020  *
1021  *      rd              - dumps all regs
1022  *      rd      %isr    - current interrupt status reg, read freshly
1023  *      rd      s       - valid stacked regs
1024  *      rd      %sstk   - gets switch stack addr. dump memory and search
1025  *      rd      d       - debug regs, may not be too useful
1026  *      rd      k       - dump kernel regs
1027  *
1028  *      ARs             TB Done
1029  *      OTHERS          TB Decided ??
1030  *
1031  *      Intel wish list
1032  *      These will be implemented later - Srinivasa
1033  *
1034  *      type        action
1035  *      ----        ------
1036  *      g           dump all General static registers
1037  *      s           dump all general Stacked registers
1038  *      f           dump all Floating Point registers
1039  *      p           dump all Predicate registers
1040  *      b           dump all Branch registers
1041  *      a           dump all Application registers
1042  *      c           dump all Control registers
1043  *
1044  */
1045
1046 int
1047 kdba_dumpregs(struct pt_regs *regs,
1048             const char *type,
1049             const char *extra)
1050
1051 {
1052         int i;
1053         int count = 0;
1054
1055         if (type
1056          && (type[0] == 'u')) {
1057                 type = NULL;
1058                 regs = (struct pt_regs *)
1059                         (kdb_current_task->thread.ksp - sizeof(struct pt_regs));
1060         }
1061
1062         if (type == NULL) {
1063                 if (!regs) {
1064                         kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
1065                         return KDB_BADREG;
1066                 }
1067                 for (i=0; i<nkdbreglist; i++) {
1068                         kdb_printf("%4s: 0x%16.16lx  ",
1069                                    kdbreglist[i].reg_name,
1070                                    *(unsigned long *)((unsigned long)regs +
1071                                                   kdbreglist[i].reg_offset));
1072
1073                         if ((++count % 3) == 0)
1074                                 kdb_printf("\n");
1075                 }
1076
1077                 kdb_printf("&regs = %p\n", (void *)regs);
1078
1079                 return 0;
1080         }
1081
1082         switch (type[0]) {
1083         case 'd':
1084         {
1085                 for(i=0; i<8; i+=2) {
1086                         kdb_printf("idr%d: 0x%16.16lx  idr%d: 0x%16.16lx\n", i,
1087                                         kdba_getdr(i), i+1, kdba_getdr(i+1));
1088
1089                 }
1090                 return 0;
1091         }
1092         case 'i':
1093                 kdba_show_intregs();
1094                 break;
1095         case 'k':
1096                 show_kernel_regs();
1097                 break;
1098         case 'm':
1099                 break;
1100         case 'r':
1101                 break;
1102
1103         case 's':
1104         {
1105                 if (!regs) {
1106                         kdb_printf("%s: pt_regs not available, use bt* or pid to select a different task\n", __FUNCTION__);
1107                         return KDB_BADREG;
1108                 }
1109                 show_cur_stack_frame(0, NULL) ;
1110
1111                 return 0 ;
1112         }
1113
1114         case '%':
1115         {
1116                 unsigned long contents ;
1117
1118                 if (!kdba_getregcontents(type+1, regs, &contents))
1119                         kdb_printf("%s = 0x%16.16lx\n", type+1, contents) ;
1120                 else
1121                         kdb_printf("diag: Invalid register %s\n", type+1) ;
1122
1123                 return 0 ;
1124         }
1125
1126         default:
1127                 return KDB_BADREG;
1128         }
1129
1130         /* NOTREACHED */
1131         return 0;
1132 }
1133 EXPORT_SYMBOL(kdba_dumpregs);
1134
1135 kdb_machreg_t
1136 kdba_getpc(struct pt_regs *regs)
1137 {
1138         return regs ? regs->cr_iip + ia64_psr(regs)->ri : 0;
1139 }
1140
1141 int
1142 kdba_setpc(struct pt_regs *regs, kdb_machreg_t newpc)
1143 {
1144         if (KDB_NULL_REGS(regs))
1145                 return KDB_BADREG;
1146         regs->cr_iip = newpc & ~0xf;
1147         ia64_psr(regs)->ri = newpc & 0x3;
1148         KDB_STATE_SET(IP_ADJUSTED);
1149         return 0;
1150 }
1151
1152 struct kdba_main_loop_data {
1153         kdb_reason_t reason;
1154         kdb_reason_t reason2;
1155         int error;
1156         kdb_dbtrap_t db_result;
1157         struct pt_regs *regs;
1158         int ret;
1159 };
1160
1161 /*
1162  * do_kdba_main_loop
1163  *
1164  *      Invoked from kdba_main_loop via unw_init_running() after that routine
1165  *      has pushed a struct switch_stack.
1166  *
1167  * Inputs:
1168  *      info    Unwind information.
1169  *      data    kdb data passed as void * to unw_init_running.
1170  * Returns:
1171  *      none (unw_init_running requires void).  vdata->ret is set to
1172  *      0       KDB was invoked for an event which it wasn't responsible
1173  *      1       KDB handled the event for which it was invoked.
1174  * Outputs:
1175  *      none
1176  * Locking:
1177  *      None.
1178  * Remarks:
1179  *      unw_init_running() creates struct switch_stack then struct
1180  *      unw_frame_info.  We get the address of the info so step over
1181  *      that to get switch_stack.  Just hope that unw_init_running
1182  *      does not change its stack usage.  unw_init_running adds padding
1183  *      to put switch_stack on a 16 byte boundary.
1184  */
1185
1186 static void
1187 do_kdba_main_loop(struct unw_frame_info *info, void *vdata)
1188 {
1189         struct kdba_main_loop_data *data = vdata;
1190         struct switch_stack *sw, *prev_sw;
1191         struct pt_regs *prev_regs;
1192         struct kdb_running_process *krp =
1193                 kdb_running_process + smp_processor_id();
1194         KDB_DEBUG_STATE(__FUNCTION__, data->reason);
1195         prev_sw = krp->arch.sw;
1196         sw = (struct switch_stack *)(info+1);
1197         /* padding from unw_init_running */
1198         sw = (struct switch_stack *)(((unsigned long)sw + 15) & ~15);
1199         krp->arch.sw = sw;
1200         prev_regs = krp->regs;
1201         data->ret = kdb_save_running(data->regs, data->reason, data->reason2,
1202                         data->error, data->db_result);
1203         kdb_unsave_running(data->regs);
1204         krp->regs = prev_regs;
1205         krp->arch.sw = prev_sw;
1206 }
1207
1208 /*
1209  * kdba_main_loop
1210  *
1211  *      Do any architecture specific set up before entering the main kdb loop.
1212  *      The primary function of this routine is to make all processes look the
1213  *      same to kdb, kdb must be able to list a process without worrying if the
1214  *      process is running or blocked, so make all processes look as though they
1215  *      are blocked.
1216  *
1217  * Inputs:
1218  *      reason          The reason KDB was invoked
1219  *      error           The hardware-defined error code
1220  *      error2          kdb's current reason code.  Initially error but can change
1221  *                      acording to kdb state.
1222  *      db_result       Result from break or debug point.
1223  *      regs            The exception frame at time of fault/breakpoint.  If reason
1224  *                      is SILENT or CPU_UP then regs is NULL, otherwise it should
1225  *                      always be valid.
1226  * Returns:
1227  *      0       KDB was invoked for an event which it wasn't responsible
1228  *      1       KDB handled the event for which it was invoked.
1229  * Outputs:
1230  *      Builds a switch_stack structure before calling the main loop.
1231  * Locking:
1232  *      None.
1233  * Remarks:
1234  *      none.
1235  */
1236
1237 int
1238 kdba_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
1239                kdb_dbtrap_t db_result, struct pt_regs *regs)
1240 {
1241         struct kdba_main_loop_data data;
1242         KDB_DEBUG_STATE("kdba_main_loop", reason);
1243         data.reason = reason;
1244         data.reason2 = reason2;
1245         data.error = error;
1246         data.db_result = db_result;
1247         data.regs = regs;
1248         unw_init_running(do_kdba_main_loop, &data);
1249         return(data.ret);
1250 }
1251
1252 void
1253 kdba_disableint(kdb_intstate_t *state)
1254 {
1255         unsigned long *fp = (unsigned long *)state;
1256         unsigned long flags;
1257
1258         local_irq_save(flags);
1259         *fp = flags;
1260 }
1261
1262 void
1263 kdba_restoreint(kdb_intstate_t *state)
1264 {
1265         unsigned long flags = *(unsigned long *)state;
1266         local_irq_restore(flags);
1267 }
1268
1269 void
1270 kdba_setsinglestep(struct pt_regs *regs)
1271 {
1272         if (KDB_NULL_REGS(regs))
1273                 return;
1274         ia64_psr(regs)->ss = 1;
1275 }
1276
1277 void
1278 kdba_clearsinglestep(struct pt_regs *regs)
1279 {
1280         if (KDB_NULL_REGS(regs))
1281                 return;
1282         ia64_psr(regs)->ss = 0;
1283 }
1284
1285 /*
1286  * kdb_tpa
1287  *
1288  *      Virtual to Physical address translation command.
1289  *
1290  *      tpa  <addr>
1291  *
1292  * Parameters:
1293  *      argc    Count of arguments in argv
1294  *      argv    Space delimited command line arguments
1295  * Outputs:
1296  *      None.
1297  * Returns:
1298  *      Zero for success, a kdb diagnostic if failure.
1299  * Locking:
1300  *      None.
1301  * Remarks:
1302  */
1303 #define __xtpa(x)               ({ia64_va _v; asm("tpa %0=%1" : "=r"(_v.l) : "r"(x)); _v.l;})
1304 static int
1305 kdba_tpa(int argc, const char **argv)
1306 {
1307         kdb_machreg_t addr;
1308         int diag;
1309         long offset = 0;
1310         int nextarg;
1311         char c;
1312
1313         nextarg = 1;
1314         if (argc != 1)
1315                 return KDB_ARGCOUNT;
1316         diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1317         if (diag)
1318                 return diag;
1319         if (kdb_getarea(c, addr))
1320                 return(0);
1321         kdb_printf("vaddr: 0x%lx , paddr: 0x%lx\n", addr, __xtpa(addr));
1322         return(0);
1323 }
1324 #if defined(CONFIG_NUMA)
1325 static int
1326 kdba_tpav(int argc, const char **argv)
1327 {
1328         kdb_machreg_t addr, end, paddr;
1329         int diag;
1330         long offset = 0;
1331         int nextarg, nid, nid_old;
1332         char c;
1333
1334         nextarg = 1;
1335         if (argc != 2)
1336                 return KDB_ARGCOUNT;
1337         diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
1338         if (diag)
1339                 return diag;
1340         diag = kdbgetaddrarg(argc, argv, &nextarg, &end, &offset, NULL);
1341         if (diag)
1342                 return diag;
1343         if (kdb_getarea(c, addr))
1344                 return(0);
1345         if (kdb_getarea(c, end))
1346                 return(0);
1347         paddr=__xtpa(addr);
1348         nid = paddr_to_nid(paddr);
1349         kdb_printf("begin: 0x%lx , paddr: 0x%lx , nid: %d\n", addr, __xtpa(addr), nid);
1350         for(;addr<end; addr += PAGE_SIZE) {
1351                 nid_old=nid;
1352                 paddr =__xtpa(addr);
1353                 nid = paddr_to_nid(paddr);
1354                 if (nid != nid_old)
1355                         kdb_printf("NOT on same NODE: addr: 0x%lx , paddr: 0x%lx , nid: %d\n", addr, paddr, nid);
1356         }
1357         paddr=__xtpa(end);
1358         nid=paddr_to_nid(end);
1359         kdb_printf("end: 0x%lx , paddr: 0x%lx , nid: %d\n", end, paddr, nid);
1360         return(0);
1361 }
1362 #endif
1363
1364 #if defined(CONFIG_SMP)
1365 /*
1366  * kdba_sendinit
1367  *
1368  *      This function implements the 'init' command.
1369  *
1370  *      init    [<cpunum>]
1371  *
1372  * Inputs:
1373  *      argc    argument count
1374  *      argv    argument vector
1375  * Outputs:
1376  *      None.
1377  * Returns:
1378  *      zero for success, a kdb diagnostic if error
1379  * Locking:
1380  *      none.
1381  * Remarks:
1382  */
1383
1384 static int
1385 kdba_sendinit(int argc, const char **argv)
1386 {
1387         unsigned long cpunum;
1388         int diag;
1389
1390         if (argc != 1)
1391                 return KDB_ARGCOUNT;
1392
1393         diag = kdbgetularg(argv[1], &cpunum);
1394         if (diag)
1395                 return diag;
1396
1397         if (cpunum >= NR_CPUS || !cpu_online(cpunum))
1398                 return KDB_BADCPUNUM;
1399
1400         platform_send_ipi(cpunum, 0, IA64_IPI_DM_INIT, 0);
1401         return 0;
1402 }
1403
1404 /* Invoked once from kdb_wait_for_cpus when waiting for cpus.  For those cpus
1405  * that have not responded to the normal KDB interrupt yet, hit them with an
1406  * INIT event.
1407  */
1408 void
1409 kdba_wait_for_cpus(void)
1410 {
1411         int c;
1412         if (KDB_FLAG(CATASTROPHIC))
1413                 return;
1414         kdb_printf("  Sending INIT to cpus that have not responded yet\n");
1415         for_each_online_cpu(c)
1416                 if (kdb_running_process[c].seqno < kdb_seqno - 1)
1417                         platform_send_ipi(c, 0, IA64_IPI_DM_INIT, 0);
1418 }
1419
1420 #endif /* CONFIG_SMP */
1421
1422 /* This code is sensitive to the layout of the MCA/INIT stack (see mca_asm.h)
1423  * and to the stack layout that ia64_mca_modify_original_stack() creates when
1424  * it makes the original task look blocked.
1425  */
1426 static void
1427 kdba_handlers_modify(struct task_struct *task, int cpu)
1428 {
1429         struct kdb_running_process *work, *save;
1430         work = kdb_running_process + cpu;
1431         save = kdb_running_process_save + cpu;
1432         *work = *save;
1433         if (!kdba_show_handlers && REGION_NUMBER(task) >= RGN_GATE &&
1434             (task_thread_info(task)->flags & _TIF_MCA_INIT)) {
1435                 struct ia64_sal_os_state *sos = (struct ia64_sal_os_state *)
1436                         ((unsigned long)save->p + MCA_SOS_OFFSET);
1437                 char *p;
1438                 if (!sos->prev_task)
1439                         return;
1440                 work->p = sos->prev_task;
1441                 p = (char *)sos->prev_task->thread.ksp;
1442                 p += 16;
1443                 work->arch.sw = (struct switch_stack *)p;
1444                 p += sizeof(struct switch_stack);
1445                 work->regs = (struct pt_regs *)p;
1446                 work->irq_depth = 2;    /* any value >1 will do */
1447         }
1448 }
1449
1450 /* Turn the display of the MCA/INIT handlers on or off, or display the status
1451  * of the MCA/INIT handlers.
1452  */
1453 static int
1454 kdba_handlers(int argc, const char **argv)
1455 {
1456         int cpu;
1457         struct kdb_running_process *krp;
1458         if (argc != 1)
1459                 return KDB_ARGCOUNT;
1460         if (strcmp(argv[1], "show") == 0)
1461                 kdba_show_handlers = 1;
1462         else if (strcmp(argv[1], "hide") == 0)
1463                 kdba_show_handlers = 0;
1464         else if (strcmp(argv[1], "status") != 0) {
1465                 kdb_printf("handlers <show|hide|status>\n");
1466                 return 0;
1467         }
1468         for (cpu = 0, krp = kdb_running_process_save; cpu < NR_CPUS; ++cpu, ++krp) {
1469                 if (krp->p)
1470                         kdba_handlers_modify(krp->p, cpu);
1471         }
1472         if (strcmp(argv[1], "status") != 0)
1473                 return 0;
1474         kdb_printf("handlers status is %s\n", kdba_show_handlers ? "'show'" : "'hide'");
1475         kdb_printf(" cpu handler task       command            original task      command\n");
1476         for (cpu = 0, krp = kdb_running_process_save; cpu < NR_CPUS; ++cpu, ++krp) {
1477                 struct task_struct *p = krp->p;
1478                 if (!p)
1479                         continue;
1480                 kdb_printf("%4d", cpu);
1481                 if (task_thread_info(p)->flags & _TIF_MCA_INIT) {
1482                         struct ia64_sal_os_state *sos;
1483                         kdb_printf(" " kdb_machreg_fmt0 " %-*s  ",
1484                                    (unsigned long)p, (int)sizeof(p->comm), p->comm);
1485                         sos = (struct ia64_sal_os_state *)((unsigned long)p + MCA_SOS_OFFSET);
1486                         p = sos->prev_task;
1487                 } else
1488                         kdb_printf("%*s", (int)(1+2+16+1+sizeof(p->comm)+2), " ");
1489                 if (p)
1490                         kdb_printf(" " kdb_machreg_fmt0 " %-*s",
1491                                    (unsigned long)p, (int)sizeof(p->comm), p->comm);
1492                 kdb_printf("\n");
1493         }
1494         return 0;
1495 }
1496
1497 /* Executed once on each cpu at startup. */
1498 void
1499 kdba_cpu_up(void)
1500 {
1501 }
1502
1503 /*
1504  * kdba_init
1505  *
1506  *      Architecture specific initialization.
1507  *
1508  * Parameters:
1509  *      None.
1510  * Returns:
1511  *      None.
1512  * Locking:
1513  *      None.
1514  * Remarks:
1515  *      None.
1516  */
1517
1518 void
1519 kdba_init(void)
1520 {
1521         kdb_running_process_save = kzalloc(
1522                 sizeof(*kdb_running_process_save) * NR_CPUS, GFP_KERNEL);
1523         BUG_ON(!kdb_running_process_save);
1524         kdb_register("irr", kdba_sir, "", "Show interrupt registers", 0);
1525         kdb_register("itm", kdba_itm, "", "Set new ITM value", 0);
1526 #if defined(CONFIG_SMP)
1527         kdb_register("init", kdba_sendinit, "", "Send INIT to cpu", 0);
1528 #endif
1529         kdb_register("pt_regs", kdba_pt_regs, "address", "Format struct pt_regs", 0);
1530         kdb_register("switch_stack", kdba_switch_stack, "address", "Format struct switch_stack", 0);
1531         kdb_register("minstate", kdba_minstate, "address", "Format PAL minstate", 0);
1532         kdb_register("tpa", kdba_tpa, "<vaddr>", "Translate virtual to physical address", 0);
1533 #if defined(CONFIG_NUMA)
1534         kdb_register("tpav", kdba_tpav, "<begin addr> <end addr>", "Verify that physical addresses corresponding to virtual addresses from <begin addr> to <end addr> are in same node", 0);
1535 #endif
1536         kdb_register("stackdepth", kdba_stackdepth, "[percentage]", "Print processes using >= stack percentage", 0);
1537         kdb_register("cpuinfo", kdba_cpuinfo, "[cpu]", "Print struct cpuinfo_ia64", 0);
1538         kdb_register("handlers", kdba_handlers, "<show|hide|status>", "Control the display of MCA/INIT handlers", 0);
1539
1540 #ifdef CONFIG_SERIAL_8250_CONSOLE
1541         kdba_serial_console = KDBA_SC_STANDARD;
1542 #endif
1543 #ifdef CONFIG_SERIAL_SGI_L1_CONSOLE
1544         if (ia64_platform_is("sn2"))
1545                 kdba_serial_console = KDBA_SC_SGI_L1;
1546 #endif
1547         return;
1548 }
1549
1550 /*
1551  * kdba_adjust_ip
1552  *
1553  *      Architecture specific adjustment of instruction pointer before leaving
1554  *      kdb.
1555  *
1556  * Parameters:
1557  *      reason          The reason KDB was invoked
1558  *      error           The hardware-defined error code
1559  *      regs            The exception frame at time of fault/breakpoint.  If reason
1560  *                      is SILENT or CPU_UP then regs is NULL, otherwise it should
1561  *                      always be valid.
1562  * Returns:
1563  *      None.
1564  * Locking:
1565  *      None.
1566  * Remarks:
1567  *      On IA64, KDB_ENTER() and KDB_ENTER_SLAVE() use break which is a fault,
1568  *      not a trap.  The instruction pointer must be stepped before leaving
1569  *      kdb, otherwise we get a loop.
1570  */
1571
1572 void
1573 kdba_adjust_ip(kdb_reason_t reason, int error, struct pt_regs *regs)
1574 {
1575         if ((reason == KDB_REASON_ENTER || reason == KDB_REASON_ENTER_SLAVE) &&
1576             !KDB_STATE(IP_ADJUSTED)) {
1577                 if (KDB_NULL_REGS(regs))
1578                         return;
1579                 if (ia64_psr(regs)->ri < 2)
1580                         kdba_setpc(regs, regs->cr_iip + ia64_psr(regs)->ri + 1);
1581                 else
1582                         kdba_setpc(regs, regs->cr_iip + 16);
1583         }
1584 }
1585
1586 void
1587 kdba_save_running(struct kdba_running_process *k, struct pt_regs *regs)
1588 {
1589         struct kdb_running_process *work, *save;
1590         int cpu = smp_processor_id();
1591         work = kdb_running_process + cpu;
1592         save = kdb_running_process_save + cpu;
1593         *save = *work;
1594         if (!regs)
1595                 return;
1596         kdba_handlers_modify((struct task_struct *)regs->r13, cpu);
1597 }
1598
1599 void
1600 kdba_unsave_running(struct kdba_running_process *k, struct pt_regs *regs)
1601 {
1602         memset(kdb_running_process_save + smp_processor_id(), 0,
1603                         sizeof(*kdb_running_process_save));
1604 }
1605
1606 void
1607 kdba_set_current_task(const struct task_struct *p)
1608 {
1609         int cpu = kdb_process_cpu(p);
1610         struct kdb_running_process *work, *save;
1611         work = kdb_running_process + cpu;
1612         save = kdb_running_process_save + cpu;
1613         kdb_current_task = p;
1614         if (kdb_task_has_cpu(p)) {
1615                 kdb_current_regs = work->regs;
1616                 return;
1617         }
1618         kdb_current_regs = NULL;
1619         /* For most blocked tasks we cannot get the pt_regs without doing an
1620          * unwind, which is not worth doing.  For tasks interrupted by
1621          * MCA/INIT, when the user is not working on the handlers, we must use
1622          * the registers at the time of interrupt.
1623          */
1624         if (work->p == save->p || work->p != p)
1625                 return;
1626         kdb_current_regs = (struct pt_regs *)(work->p->thread.ksp + 16 +
1627                 sizeof(struct switch_stack));
1628 }
1629
1630 /*
1631  * asm-ia64 uaccess.h supplies __copy_to_user which relies on MMU to
1632  * trap invalid addresses in the _xxx fields.  Verify the other address
1633  * of the pair is valid by accessing the first and last byte ourselves,
1634  * then any access violations should only be caused by the _xxx
1635  * addresses,
1636  */
1637
1638 int
1639 kdba_putarea_size(unsigned long to_xxx, void *from, size_t size)
1640 {
1641         mm_segment_t oldfs = get_fs();
1642         int r;
1643         char c;
1644         c = *((volatile char *)from);
1645         c = *((volatile char *)from + size - 1);
1646
1647         if (to_xxx >> 61 <= 4) {
1648                 return kdb_putuserarea_size(to_xxx, from, size);
1649         }
1650
1651         set_fs(KERNEL_DS);
1652         r = __copy_to_user_inatomic((void __user *)to_xxx, from, size);
1653         set_fs(oldfs);
1654         return r;
1655 }
1656
1657 int
1658 kdba_getarea_size(void *to, unsigned long from_xxx, size_t size)
1659 {
1660         mm_segment_t oldfs = get_fs();
1661         int r;
1662         *((volatile char *)to) = '\0';
1663         *((volatile char *)to + size - 1) = '\0';
1664
1665         if (from_xxx >> 61 <= 4)
1666                 return kdb_getuserarea_size(to, from_xxx, size);
1667
1668         set_fs(KERNEL_DS);
1669         switch (size) {
1670         case 1:
1671                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 1);
1672                 break;
1673         case 2:
1674                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 2);
1675                 break;
1676         case 4:
1677                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 4);
1678                 break;
1679         case 8:
1680                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, 8);
1681                 break;
1682         default:
1683                 r = __copy_to_user_inatomic((void __user *)to, (void *)from_xxx, size);
1684                 break;
1685         }
1686         set_fs(oldfs);
1687         return r;
1688 }
1689
1690 int
1691 kdba_verify_rw(unsigned long addr, size_t size)
1692 {
1693         unsigned char data[(__force size_t) size];
1694         return(kdba_getarea_size(data, addr, size) || kdba_putarea_size(addr, data, size));
1695 }
1696
1697 #ifdef CONFIG_KDB_KDUMP
1698 void
1699 kdba_kdump_prepare(struct pt_regs *fixed_regs)
1700 {
1701         int i;
1702
1703         /* Set on KEXEC bit on all onlinr cpus */
1704         for (i = 1; i < NR_CPUS; ++i) {
1705                 if (!cpu_online(i))
1706                         continue;
1707
1708                 KDB_STATE_SET_CPU(KEXEC, i);
1709         }
1710
1711         machine_crash_shutdown(fixed_regs);
1712 }
1713
1714 void kdba_kdump_shutdown_slave(struct pt_regs *regs)
1715 {
1716         if (kdb_kdump_state != KDB_KDUMP_RESET) {
1717                 unw_init_running(kdump_cpu_freeze, NULL);
1718         }
1719 }
1720 #endif