]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/hexagon/kernel/traps.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-artful-kernel.git] / arch / hexagon / kernel / traps.c
CommitLineData
cf9750ba
RK
1/*
2 * Kernel traps/events for Hexagon processor
3 *
8914d7e8 4 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
cf9750ba
RK
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 */
20
21#include <linux/init.h>
3f07c014 22#include <linux/sched/signal.h>
b17b0153 23#include <linux/sched/debug.h>
cf9750ba
RK
24#include <linux/module.h>
25#include <linux/kallsyms.h>
26#include <linux/kdebug.h>
27#include <linux/syscalls.h>
28#include <linux/signal.h>
29#include <linux/tracehook.h>
30#include <asm/traps.h>
31#include <asm/vm_fault.h>
32#include <asm/syscall.h>
33#include <asm/registers.h>
34#include <asm/unistd.h>
35#include <asm/sections.h>
36#ifdef CONFIG_KGDB
37# include <linux/kgdb.h>
38#endif
39
40#define TRAP_SYSCALL 1
41#define TRAP_DEBUG 0xdb
42
43void __init trap_init(void)
44{
45}
46
47#ifdef CONFIG_GENERIC_BUG
48/* Maybe should resemble arch/sh/kernel/traps.c ?? */
49int is_valid_bugaddr(unsigned long addr)
50{
51 return 1;
52}
53#endif /* CONFIG_GENERIC_BUG */
54
55static const char *ex_name(int ex)
56{
57 switch (ex) {
58 case HVM_GE_C_XPROT:
59 case HVM_GE_C_XUSER:
60 return "Execute protection fault";
61 case HVM_GE_C_RPROT:
62 case HVM_GE_C_RUSER:
63 return "Read protection fault";
64 case HVM_GE_C_WPROT:
65 case HVM_GE_C_WUSER:
66 return "Write protection fault";
67 case HVM_GE_C_XMAL:
68 return "Misaligned instruction";
db0fe532
RK
69 case HVM_GE_C_WREG:
70 return "Multiple writes to same register in packet";
71 case HVM_GE_C_PCAL:
72 return "Program counter values that are not properly aligned";
cf9750ba
RK
73 case HVM_GE_C_RMAL:
74 return "Misaligned data load";
75 case HVM_GE_C_WMAL:
76 return "Misaligned data store";
77 case HVM_GE_C_INVI:
78 case HVM_GE_C_PRIVI:
79 return "Illegal instruction";
80 case HVM_GE_C_BUS:
81 return "Precise bus error";
82 case HVM_GE_C_CACHE:
83 return "Cache error";
84
85 case 0xdb:
86 return "Debugger trap";
87
88 default:
89 return "Unrecognized exception";
90 }
91}
92
93static void do_show_stack(struct task_struct *task, unsigned long *fp,
94 unsigned long ip)
95{
96 int kstack_depth_to_print = 24;
97 unsigned long offset, size;
98 const char *name = NULL;
99 unsigned long *newfp;
100 unsigned long low, high;
101 char tmpstr[128];
102 char *modname;
103 int i;
104
105 if (task == NULL)
106 task = current;
107
108 printk(KERN_INFO "CPU#%d, %s/%d, Call Trace:\n",
109 raw_smp_processor_id(), task->comm,
110 task_pid_nr(task));
111
112 if (fp == NULL) {
113 if (task == current) {
114 asm("%0 = r30" : "=r" (fp));
115 } else {
116 fp = (unsigned long *)
117 ((struct hexagon_switch_stack *)
118 task->thread.switch_sp)->fp;
119 }
120 }
121
122 if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
123 printk(KERN_INFO "-- Corrupt frame pointer %p\n", fp);
124 return;
125 }
126
127 /* Saved link reg is one word above FP */
128 if (!ip)
129 ip = *(fp+1);
130
131 /* Expect kernel stack to be in-bounds */
132 low = (unsigned long)task_stack_page(task);
133 high = low + THREAD_SIZE - 8;
134 low += sizeof(struct thread_info);
135
136 for (i = 0; i < kstack_depth_to_print; i++) {
137
138 name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
139
140 printk(KERN_INFO "[%p] 0x%lx: %s + 0x%lx", fp, ip, name,
141 offset);
142 if (((unsigned long) fp < low) || (high < (unsigned long) fp))
143 printk(KERN_CONT " (FP out of bounds!)");
144 if (modname)
145 printk(KERN_CONT " [%s] ", modname);
146 printk(KERN_CONT "\n");
147
148 newfp = (unsigned long *) *fp;
149
150 if (((unsigned long) newfp) & 0x3) {
151 printk(KERN_INFO "-- Corrupt frame pointer %p\n",
152 newfp);
153 break;
154 }
155
156 /* Attempt to continue past exception. */
157 if (0 == newfp) {
158 struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
159 + 8);
160
161 if (regs->syscall_nr != -1) {
162 printk(KERN_INFO "-- trap0 -- syscall_nr: %ld",
163 regs->syscall_nr);
164 printk(KERN_CONT " psp: %lx elr: %lx\n",
165 pt_psp(regs), pt_elr(regs));
166 break;
167 } else {
168 /* really want to see more ... */
169 kstack_depth_to_print += 6;
170 printk(KERN_INFO "-- %s (0x%lx) badva: %lx\n",
171 ex_name(pt_cause(regs)), pt_cause(regs),
172 pt_badva(regs));
173 }
174
175 newfp = (unsigned long *) regs->r30;
176 ip = pt_elr(regs);
177 } else {
178 ip = *(newfp + 1);
179 }
180
181 /* If link reg is null, we are done. */
182 if (ip == 0x0)
183 break;
184
185 /* If newfp isn't larger, we're tracing garbage. */
186 if (newfp > fp)
187 fp = newfp;
188 else
189 break;
190 }
191}
192
193void show_stack(struct task_struct *task, unsigned long *fp)
194{
195 /* Saved link reg is one word above FP */
196 do_show_stack(task, fp, 0);
197}
198
cf9750ba
RK
199int die(const char *str, struct pt_regs *regs, long err)
200{
201 static struct {
202 spinlock_t lock;
203 int counter;
204 } die = {
205 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
206 .counter = 0
207 };
208
209 console_verbose();
210 oops_enter();
211
212 spin_lock_irq(&die.lock);
213 bust_spinlocks(1);
214 printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
215
216 if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
217 NOTIFY_STOP)
218 return 1;
219
220 print_modules();
221 show_regs(regs);
222 do_show_stack(current, &regs->r30, pt_elr(regs));
223
224 bust_spinlocks(0);
373d4d09 225 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
cf9750ba
RK
226
227 spin_unlock_irq(&die.lock);
228
229 if (in_interrupt())
230 panic("Fatal exception in interrupt");
231
232 if (panic_on_oops)
233 panic("Fatal exception");
234
235 oops_exit();
236 do_exit(err);
237 return 0;
238}
239
240int die_if_kernel(char *str, struct pt_regs *regs, long err)
241{
242 if (!user_mode(regs))
243 return die(str, regs, err);
244 else
245 return 0;
246}
247
248/*
249 * It's not clear that misaligned fetches are ever recoverable.
250 */
251static void misaligned_instruction(struct pt_regs *regs)
252{
253 die_if_kernel("Misaligned Instruction", regs, 0);
254 force_sig(SIGBUS, current);
255}
256
257/*
258 * Misaligned loads and stores, on the other hand, can be
259 * emulated, and probably should be, some day. But for now
260 * they will be considered fatal.
261 */
262static void misaligned_data_load(struct pt_regs *regs)
263{
264 die_if_kernel("Misaligned Data Load", regs, 0);
265 force_sig(SIGBUS, current);
266}
267
268static void misaligned_data_store(struct pt_regs *regs)
269{
270 die_if_kernel("Misaligned Data Store", regs, 0);
271 force_sig(SIGBUS, current);
272}
273
274static void illegal_instruction(struct pt_regs *regs)
275{
276 die_if_kernel("Illegal Instruction", regs, 0);
277 force_sig(SIGILL, current);
278}
279
280/*
281 * Precise bus errors may be recoverable with a a retry,
282 * but for now, treat them as irrecoverable.
283 */
284static void precise_bus_error(struct pt_regs *regs)
285{
286 die_if_kernel("Precise Bus Error", regs, 0);
287 force_sig(SIGBUS, current);
288}
289
290/*
291 * If anything is to be done here other than panic,
292 * it will probably be complex and migrate to another
293 * source module. For now, just die.
294 */
295static void cache_error(struct pt_regs *regs)
296{
297 die("Cache Error", regs, 0);
298}
299
300/*
301 * General exception handler
302 */
303void do_genex(struct pt_regs *regs)
304{
305 /*
306 * Decode Cause and Dispatch
307 */
308 switch (pt_cause(regs)) {
309 case HVM_GE_C_XPROT:
310 case HVM_GE_C_XUSER:
311 execute_protection_fault(regs);
312 break;
313 case HVM_GE_C_RPROT:
314 case HVM_GE_C_RUSER:
315 read_protection_fault(regs);
316 break;
317 case HVM_GE_C_WPROT:
318 case HVM_GE_C_WUSER:
319 write_protection_fault(regs);
320 break;
321 case HVM_GE_C_XMAL:
322 misaligned_instruction(regs);
323 break;
db0fe532
RK
324 case HVM_GE_C_WREG:
325 illegal_instruction(regs);
326 break;
327 case HVM_GE_C_PCAL:
328 misaligned_instruction(regs);
329 break;
cf9750ba
RK
330 case HVM_GE_C_RMAL:
331 misaligned_data_load(regs);
332 break;
333 case HVM_GE_C_WMAL:
334 misaligned_data_store(regs);
335 break;
336 case HVM_GE_C_INVI:
337 case HVM_GE_C_PRIVI:
338 illegal_instruction(regs);
339 break;
340 case HVM_GE_C_BUS:
341 precise_bus_error(regs);
342 break;
343 case HVM_GE_C_CACHE:
344 cache_error(regs);
345 break;
346 default:
347 /* Halt and catch fire */
348 panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
349 break;
350 }
351}
352
353/* Indirect system call dispatch */
354long sys_syscall(void)
355{
356 printk(KERN_ERR "sys_syscall invoked!\n");
357 return -ENOSYS;
358}
359
360void do_trap0(struct pt_regs *regs)
361{
cf9750ba
RK
362 syscall_fn syscall;
363
364 switch (pt_cause(regs)) {
365 case TRAP_SYSCALL:
366 /* System call is trap0 #1 */
367
368 /* allow strace to catch syscall args */
369 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
370 tracehook_report_syscall_entry(regs)))
371 return; /* return -ENOSYS somewhere? */
372
373 /* Interrupts should be re-enabled for syscall processing */
374 __vmsetie(VM_INT_ENABLE);
375
376 /*
377 * System call number is in r6, arguments in r0..r5.
378 * Fortunately, no Linux syscall has more than 6 arguments,
379 * and Hexagon ABI passes first 6 arguments in registers.
380 * 64-bit arguments are passed in odd/even register pairs.
381 * Fortunately, we have no system calls that take more
382 * than three arguments with more than one 64-bit value.
383 * Should that change, we'd need to redesign to copy
384 * between user and kernel stacks.
385 */
386 regs->syscall_nr = regs->r06;
387
388 /*
389 * GPR R0 carries the first parameter, and is also used
390 * to report the return value. We need a backup of
391 * the user's value in case we need to do a late restart
392 * of the system call.
393 */
394 regs->restart_r0 = regs->r00;
395
396 if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
397 regs->r00 = -1;
398 } else {
399 syscall = (syscall_fn)
400 (sys_call_table[regs->syscall_nr]);
a11e67c2 401 regs->r00 = syscall(regs->r00, regs->r01,
cf9750ba
RK
402 regs->r02, regs->r03,
403 regs->r04, regs->r05);
404 }
405
cf9750ba
RK
406 /* allow strace to get the syscall return state */
407 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
408 tracehook_report_syscall_exit(regs, 0);
409
410 break;
411 case TRAP_DEBUG:
412 /* Trap0 0xdb is debug breakpoint */
413 if (user_mode(regs)) {
414 struct siginfo info;
415
416 info.si_signo = SIGTRAP;
417 info.si_errno = 0;
418 /*
419 * Some architecures add some per-thread state
420 * to distinguish between breakpoint traps and
421 * trace traps. We may want to do that, and
422 * set the si_code value appropriately, or we
423 * may want to use a different trap0 flavor.
424 */
425 info.si_code = TRAP_BRKPT;
426 info.si_addr = (void __user *) pt_elr(regs);
8914d7e8 427 force_sig_info(SIGTRAP, &info, current);
cf9750ba
RK
428 } else {
429#ifdef CONFIG_KGDB
430 kgdb_handle_exception(pt_cause(regs), SIGTRAP,
431 TRAP_BRKPT, regs);
432#endif
433 }
434 break;
435 }
436 /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
437}
438
439/*
440 * Machine check exception handler
441 */
442void do_machcheck(struct pt_regs *regs)
443{
444 /* Halt and catch fire */
445 __vmstop();
446}
7777746c
RK
447
448/*
449 * Treat this like the old 0xdb trap.
450 */
451
452void do_debug_exception(struct pt_regs *regs)
453{
454 regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
455 regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);
456 do_trap0(regs);
457}