Merge branch 'exec_rm_compat' of git://git.kernel.org/pub/scm/linux/kernel/git/oleg...
[pandora-kernel.git] / arch / powerpc / kernel / kgdb.c
1 /*
2  * PowerPC backend to the KGDB stub.
3  *
4  * 1998 (c) Michael AK Tesch (tesch@cs.wisc.edu)
5  * Copyright (C) 2003 Timesys Corporation.
6  * Copyright (C) 2004-2006 MontaVista Software, Inc.
7  * PPC64 Mods (C) 2005 Frank Rowand (frowand@mvista.com)
8  * PPC32 support restored by Vitaly Wool <vwool@ru.mvista.com> and
9  * Sergei Shtylyov <sshtylyov@ru.mvista.com>
10  * Copyright (C) 2007-2008 Wind River Systems, Inc.
11  *
12  * This file is licensed under the terms of the GNU General Public License
13  * version 2. This program as licensed "as is" without any warranty of any
14  * kind, whether express or implied.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/kgdb.h>
20 #include <linux/smp.h>
21 #include <linux/signal.h>
22 #include <linux/ptrace.h>
23 #include <linux/kdebug.h>
24 #include <asm/current.h>
25 #include <asm/processor.h>
26 #include <asm/machdep.h>
27
28 /*
29  * This table contains the mapping between PowerPC hardware trap types, and
30  * signals, which are primarily what GDB understands.  GDB and the kernel
31  * don't always agree on values, so we use constants taken from gdb-6.2.
32  */
33 static struct hard_trap_info
34 {
35         unsigned int tt;                /* Trap type code for powerpc */
36         unsigned char signo;            /* Signal that we map this trap into */
37 } hard_trap_info[] = {
38         { 0x0100, 0x02 /* SIGINT */  },         /* system reset */
39         { 0x0200, 0x0b /* SIGSEGV */ },         /* machine check */
40         { 0x0300, 0x0b /* SIGSEGV */ },         /* data access */
41         { 0x0400, 0x0b /* SIGSEGV */ },         /* instruction access */
42         { 0x0500, 0x02 /* SIGINT */  },         /* external interrupt */
43         { 0x0600, 0x0a /* SIGBUS */  },         /* alignment */
44         { 0x0700, 0x05 /* SIGTRAP */ },         /* program check */
45         { 0x0800, 0x08 /* SIGFPE */  },         /* fp unavailable */
46         { 0x0900, 0x0e /* SIGALRM */ },         /* decrementer */
47         { 0x0c00, 0x14 /* SIGCHLD */ },         /* system call */
48 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
49         { 0x2002, 0x05 /* SIGTRAP */ },         /* debug */
50 #if defined(CONFIG_FSL_BOOKE)
51         { 0x2010, 0x08 /* SIGFPE */  },         /* spe unavailable */
52         { 0x2020, 0x08 /* SIGFPE */  },         /* spe unavailable */
53         { 0x2030, 0x08 /* SIGFPE */  },         /* spe fp data */
54         { 0x2040, 0x08 /* SIGFPE */  },         /* spe fp data */
55         { 0x2050, 0x08 /* SIGFPE */  },         /* spe fp round */
56         { 0x2060, 0x0e /* SIGILL */  },         /* performance monitor */
57         { 0x2900, 0x08 /* SIGFPE */  },         /* apu unavailable */
58         { 0x3100, 0x0e /* SIGALRM */ },         /* fixed interval timer */
59         { 0x3200, 0x02 /* SIGINT */  },         /* watchdog */
60 #else /* ! CONFIG_FSL_BOOKE */
61         { 0x1000, 0x0e /* SIGALRM */ },         /* prog interval timer */
62         { 0x1010, 0x0e /* SIGALRM */ },         /* fixed interval timer */
63         { 0x1020, 0x02 /* SIGINT */  },         /* watchdog */
64         { 0x2010, 0x08 /* SIGFPE */  },         /* fp unavailable */
65         { 0x2020, 0x08 /* SIGFPE */  },         /* ap unavailable */
66 #endif
67 #else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */
68         { 0x0d00, 0x05 /* SIGTRAP */ },         /* single-step */
69 #if defined(CONFIG_8xx)
70         { 0x1000, 0x04 /* SIGILL */  },         /* software emulation */
71 #else /* ! CONFIG_8xx */
72         { 0x0f00, 0x04 /* SIGILL */  },         /* performance monitor */
73         { 0x0f20, 0x08 /* SIGFPE */  },         /* altivec unavailable */
74         { 0x1300, 0x05 /* SIGTRAP */ },         /* instruction address break */
75 #if defined(CONFIG_PPC64)
76         { 0x1200, 0x05 /* SIGILL */  },         /* system error */
77         { 0x1500, 0x04 /* SIGILL */  },         /* soft patch */
78         { 0x1600, 0x04 /* SIGILL */  },         /* maintenance */
79         { 0x1700, 0x08 /* SIGFPE */  },         /* altivec assist */
80         { 0x1800, 0x04 /* SIGILL */  },         /* thermal */
81 #else /* ! CONFIG_PPC64 */
82         { 0x1400, 0x02 /* SIGINT */  },         /* SMI */
83         { 0x1600, 0x08 /* SIGFPE */  },         /* altivec assist */
84         { 0x1700, 0x04 /* SIGILL */  },         /* TAU */
85         { 0x2000, 0x05 /* SIGTRAP */ },         /* run mode */
86 #endif
87 #endif
88 #endif
89         { 0x0000, 0x00 }                        /* Must be last */
90 };
91
92 static int computeSignal(unsigned int tt)
93 {
94         struct hard_trap_info *ht;
95
96         for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
97                 if (ht->tt == tt)
98                         return ht->signo;
99
100         return SIGHUP;          /* default for things we don't know about */
101 }
102
103 static int kgdb_call_nmi_hook(struct pt_regs *regs)
104 {
105         kgdb_nmicallback(raw_smp_processor_id(), regs);
106         return 0;
107 }
108
109 #ifdef CONFIG_SMP
110 void kgdb_roundup_cpus(unsigned long flags)
111 {
112         smp_send_debugger_break();
113 }
114 #endif
115
116 /* KGDB functions to use existing PowerPC64 hooks. */
117 static int kgdb_debugger(struct pt_regs *regs)
118 {
119         return !kgdb_handle_exception(1, computeSignal(TRAP(regs)),
120                                       DIE_OOPS, regs);
121 }
122
123 static int kgdb_handle_breakpoint(struct pt_regs *regs)
124 {
125         if (user_mode(regs))
126                 return 0;
127
128         if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0)
129                 return 0;
130
131         if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr))
132                 regs->nip += BREAK_INSTR_SIZE;
133
134         return 1;
135 }
136
137 static int kgdb_singlestep(struct pt_regs *regs)
138 {
139         struct thread_info *thread_info, *exception_thread_info;
140
141         if (user_mode(regs))
142                 return 0;
143
144         /*
145          * On Book E and perhaps other processsors, singlestep is handled on
146          * the critical exception stack.  This causes current_thread_info()
147          * to fail, since it it locates the thread_info by masking off
148          * the low bits of the current stack pointer.  We work around
149          * this issue by copying the thread_info from the kernel stack
150          * before calling kgdb_handle_exception, and copying it back
151          * afterwards.  On most processors the copy is avoided since
152          * exception_thread_info == thread_info.
153          */
154         thread_info = (struct thread_info *)(regs->gpr[1] & ~(THREAD_SIZE-1));
155         exception_thread_info = current_thread_info();
156
157         if (thread_info != exception_thread_info)
158                 memcpy(exception_thread_info, thread_info, sizeof *thread_info);
159
160         kgdb_handle_exception(0, SIGTRAP, 0, regs);
161
162         if (thread_info != exception_thread_info)
163                 memcpy(thread_info, exception_thread_info, sizeof *thread_info);
164
165         return 1;
166 }
167
168 static int kgdb_iabr_match(struct pt_regs *regs)
169 {
170         if (user_mode(regs))
171                 return 0;
172
173         if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
174                 return 0;
175         return 1;
176 }
177
178 static int kgdb_dabr_match(struct pt_regs *regs)
179 {
180         if (user_mode(regs))
181                 return 0;
182
183         if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
184                 return 0;
185         return 1;
186 }
187
188 #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
189
190 #define PACK32(ptr, src) do {          \
191         u32 *ptr32;                   \
192         ptr32 = (u32 *)ptr;           \
193         *(ptr32++) = (src);           \
194         ptr = (unsigned long *)ptr32; \
195         } while (0)
196
197 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
198 {
199         struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
200                                                   STACK_FRAME_OVERHEAD);
201         unsigned long *ptr = gdb_regs;
202         int reg;
203
204         memset(gdb_regs, 0, NUMREGBYTES);
205
206         /* Regs GPR0-2 */
207         for (reg = 0; reg < 3; reg++)
208                 PACK64(ptr, regs->gpr[reg]);
209
210         /* Regs GPR3-13 are caller saved, not in regs->gpr[] */
211         ptr += 11;
212
213         /* Regs GPR14-31 */
214         for (reg = 14; reg < 32; reg++)
215                 PACK64(ptr, regs->gpr[reg]);
216
217 #ifdef CONFIG_FSL_BOOKE
218 #ifdef CONFIG_SPE
219         for (reg = 0; reg < 32; reg++)
220                 PACK64(ptr, p->thread.evr[reg]);
221 #else
222         ptr += 32;
223 #endif
224 #else
225         /* fp registers not used by kernel, leave zero */
226         ptr += 32 * 8 / sizeof(long);
227 #endif
228
229         PACK64(ptr, regs->nip);
230         PACK64(ptr, regs->msr);
231         PACK32(ptr, regs->ccr);
232         PACK64(ptr, regs->link);
233         PACK64(ptr, regs->ctr);
234         PACK32(ptr, regs->xer);
235
236         BUG_ON((unsigned long)ptr >
237                (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
238 }
239
240 #define GDB_SIZEOF_REG sizeof(unsigned long)
241 #define GDB_SIZEOF_REG_U32 sizeof(u32)
242
243 #ifdef CONFIG_FSL_BOOKE
244 #define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long)
245 #else
246 #define GDB_SIZEOF_FLOAT_REG sizeof(u64)
247 #endif
248
249 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
250 {
251         { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) },
252         { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) },
253         { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) },
254         { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) },
255         { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) },
256         { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) },
257         { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) },
258         { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) },
259         { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) },
260         { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) },
261         { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) },
262         { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) },
263         { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) },
264         { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) },
265         { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) },
266         { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) },
267         { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) },
268         { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) },
269         { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) },
270         { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) },
271         { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) },
272         { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) },
273         { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) },
274         { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) },
275         { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) },
276         { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) },
277         { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) },
278         { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) },
279         { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) },
280         { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) },
281         { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) },
282         { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) },
283
284         { "f0", GDB_SIZEOF_FLOAT_REG, 0 },
285         { "f1", GDB_SIZEOF_FLOAT_REG, 1 },
286         { "f2", GDB_SIZEOF_FLOAT_REG, 2 },
287         { "f3", GDB_SIZEOF_FLOAT_REG, 3 },
288         { "f4", GDB_SIZEOF_FLOAT_REG, 4 },
289         { "f5", GDB_SIZEOF_FLOAT_REG, 5 },
290         { "f6", GDB_SIZEOF_FLOAT_REG, 6 },
291         { "f7", GDB_SIZEOF_FLOAT_REG, 7 },
292         { "f8", GDB_SIZEOF_FLOAT_REG, 8 },
293         { "f9", GDB_SIZEOF_FLOAT_REG, 9 },
294         { "f10", GDB_SIZEOF_FLOAT_REG, 10 },
295         { "f11", GDB_SIZEOF_FLOAT_REG, 11 },
296         { "f12", GDB_SIZEOF_FLOAT_REG, 12 },
297         { "f13", GDB_SIZEOF_FLOAT_REG, 13 },
298         { "f14", GDB_SIZEOF_FLOAT_REG, 14 },
299         { "f15", GDB_SIZEOF_FLOAT_REG, 15 },
300         { "f16", GDB_SIZEOF_FLOAT_REG, 16 },
301         { "f17", GDB_SIZEOF_FLOAT_REG, 17 },
302         { "f18", GDB_SIZEOF_FLOAT_REG, 18 },
303         { "f19", GDB_SIZEOF_FLOAT_REG, 19 },
304         { "f20", GDB_SIZEOF_FLOAT_REG, 20 },
305         { "f21", GDB_SIZEOF_FLOAT_REG, 21 },
306         { "f22", GDB_SIZEOF_FLOAT_REG, 22 },
307         { "f23", GDB_SIZEOF_FLOAT_REG, 23 },
308         { "f24", GDB_SIZEOF_FLOAT_REG, 24 },
309         { "f25", GDB_SIZEOF_FLOAT_REG, 25 },
310         { "f26", GDB_SIZEOF_FLOAT_REG, 26 },
311         { "f27", GDB_SIZEOF_FLOAT_REG, 27 },
312         { "f28", GDB_SIZEOF_FLOAT_REG, 28 },
313         { "f29", GDB_SIZEOF_FLOAT_REG, 29 },
314         { "f30", GDB_SIZEOF_FLOAT_REG, 30 },
315         { "f31", GDB_SIZEOF_FLOAT_REG, 31 },
316
317         { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) },
318         { "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) },
319         { "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) },
320         { "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) },
321         { "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) },
322         { "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) },
323 };
324
325 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
326 {
327         if (regno >= DBG_MAX_REG_NUM || regno < 0)
328                 return NULL;
329
330         if (regno < 32 || regno >= 64)
331                 /* First 0 -> 31 gpr registers*/
332                 /* pc, msr, ls... registers 64 -> 69 */
333                 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
334                                 dbg_reg_def[regno].size);
335
336         if (regno >= 32 && regno < 64) {
337                 /* FP registers 32 -> 63 */
338 #if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
339                 if (current)
340                         memcpy(mem, &current->thread.evr[regno-32],
341                                         dbg_reg_def[regno].size);
342 #else
343                 /* fp registers not used by kernel, leave zero */
344                 memset(mem, 0, dbg_reg_def[regno].size);
345 #endif
346         }
347
348         return dbg_reg_def[regno].name;
349 }
350
351 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
352 {
353         if (regno >= DBG_MAX_REG_NUM || regno < 0)
354                 return -EINVAL;
355
356         if (regno < 32 || regno >= 64)
357                 /* First 0 -> 31 gpr registers*/
358                 /* pc, msr, ls... registers 64 -> 69 */
359                 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
360                                 dbg_reg_def[regno].size);
361
362         if (regno >= 32 && regno < 64) {
363                 /* FP registers 32 -> 63 */
364 #if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
365                 memcpy(&current->thread.evr[regno-32], mem,
366                                 dbg_reg_def[regno].size);
367 #else
368                 /* fp registers not used by kernel, leave zero */
369                 return 0;
370 #endif
371         }
372
373         return 0;
374 }
375
376 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
377 {
378         regs->nip = pc;
379 }
380
381 /*
382  * This function does PowerPC specific procesing for interfacing to gdb.
383  */
384 int kgdb_arch_handle_exception(int vector, int signo, int err_code,
385                                char *remcom_in_buffer, char *remcom_out_buffer,
386                                struct pt_regs *linux_regs)
387 {
388         char *ptr = &remcom_in_buffer[1];
389         unsigned long addr;
390
391         switch (remcom_in_buffer[0]) {
392                 /*
393                  * sAA..AA   Step one instruction from AA..AA
394                  * This will return an error to gdb ..
395                  */
396         case 's':
397         case 'c':
398                 /* handle the optional parameter */
399                 if (kgdb_hex2long(&ptr, &addr))
400                         linux_regs->nip = addr;
401
402                 atomic_set(&kgdb_cpu_doing_single_step, -1);
403                 /* set the trace bit if we're stepping */
404                 if (remcom_in_buffer[0] == 's') {
405 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
406                         mtspr(SPRN_DBCR0,
407                               mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
408                         linux_regs->msr |= MSR_DE;
409 #else
410                         linux_regs->msr |= MSR_SE;
411 #endif
412                         kgdb_single_step = 1;
413                         atomic_set(&kgdb_cpu_doing_single_step,
414                                    raw_smp_processor_id());
415                 }
416                 return 0;
417         }
418
419         return -1;
420 }
421
422 /*
423  * Global data
424  */
425 struct kgdb_arch arch_kgdb_ops = {
426         .gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08},
427 };
428
429 static int kgdb_not_implemented(struct pt_regs *regs)
430 {
431         return 0;
432 }
433
434 static void *old__debugger_ipi;
435 static void *old__debugger;
436 static void *old__debugger_bpt;
437 static void *old__debugger_sstep;
438 static void *old__debugger_iabr_match;
439 static void *old__debugger_dabr_match;
440 static void *old__debugger_fault_handler;
441
442 int kgdb_arch_init(void)
443 {
444         old__debugger_ipi = __debugger_ipi;
445         old__debugger = __debugger;
446         old__debugger_bpt = __debugger_bpt;
447         old__debugger_sstep = __debugger_sstep;
448         old__debugger_iabr_match = __debugger_iabr_match;
449         old__debugger_dabr_match = __debugger_dabr_match;
450         old__debugger_fault_handler = __debugger_fault_handler;
451
452         __debugger_ipi = kgdb_call_nmi_hook;
453         __debugger = kgdb_debugger;
454         __debugger_bpt = kgdb_handle_breakpoint;
455         __debugger_sstep = kgdb_singlestep;
456         __debugger_iabr_match = kgdb_iabr_match;
457         __debugger_dabr_match = kgdb_dabr_match;
458         __debugger_fault_handler = kgdb_not_implemented;
459
460         return 0;
461 }
462
463 void kgdb_arch_exit(void)
464 {
465         __debugger_ipi = old__debugger_ipi;
466         __debugger = old__debugger;
467         __debugger_bpt = old__debugger_bpt;
468         __debugger_sstep = old__debugger_sstep;
469         __debugger_iabr_match = old__debugger_iabr_match;
470         __debugger_dabr_match = old__debugger_dabr_match;
471         __debugger_fault_handler = old__debugger_fault_handler;
472 }