blob: 75bef61892d25742bc33b76a86f8e9654da1b53f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundta23ba432007-11-28 20:19:38 +09002 * arch/sh/kernel/traps_64.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003, 2004 Paul Mundt
6 * Copyright (C) 2003, 2004 Richard Curnow
7 *
Paul Mundta23ba432007-11-28 20:19:38 +09008 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/timer.h>
18#include <linux/mm.h>
19#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/spinlock.h>
23#include <linux/kallsyms.h>
24#include <linux/interrupt.h>
25#include <linux/sysctl.h>
26#include <linux/module.h>
Paul Mundtace2dc72010-10-13 06:55:26 +090027#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
29#include <asm/io.h>
Paul Mundtdb218b32012-06-14 14:05:24 +090030#include <asm/alignment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/processor.h>
32#include <asm/pgtable.h>
Adrian Bunk50387b32008-04-13 21:15:38 +030033#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static void do_unhandled_exception(int trapnr, int signr, char *str, char *fn_name,
36 unsigned long error_code, struct pt_regs *regs, struct task_struct *tsk);
37
38#define DO_ERROR(trapnr, signr, str, name, tsk) \
39asmlinkage void do_##name(unsigned long error_code, struct pt_regs *regs) \
40{ \
41 do_unhandled_exception(trapnr, signr, str, __stringify(name), error_code, regs, current); \
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044DO_ERROR(13, SIGILL, "illegal slot instruction", illegal_slot_inst, current)
45DO_ERROR(87, SIGSEGV, "address error (exec)", address_error_exec, current)
46
47
48/* Implement misaligned load/store handling for kernel (and optionally for user
49 mode too). Limitation : only SHmedia mode code is handled - there is no
50 handling at all for misaligned accesses occurring in SHcompact code yet. */
51
52static int misaligned_fixup(struct pt_regs *regs);
53
54asmlinkage void do_address_error_load(unsigned long error_code, struct pt_regs *regs)
55{
56 if (misaligned_fixup(regs) < 0) {
57 do_unhandled_exception(7, SIGSEGV, "address error(load)",
58 "do_address_error_load",
59 error_code, regs, current);
60 }
61 return;
62}
63
64asmlinkage void do_address_error_store(unsigned long error_code, struct pt_regs *regs)
65{
66 if (misaligned_fixup(regs) < 0) {
67 do_unhandled_exception(8, SIGSEGV, "address error(store)",
68 "do_address_error_store",
69 error_code, regs, current);
70 }
71 return;
72}
73
74#if defined(CONFIG_SH64_ID2815_WORKAROUND)
75
76#define OPCODE_INVALID 0
77#define OPCODE_USER_VALID 1
78#define OPCODE_PRIV_VALID 2
79
80/* getcon/putcon - requires checking which control register is referenced. */
81#define OPCODE_CTRL_REG 3
82
83/* Table of valid opcodes for SHmedia mode.
84 Form a 10-bit value by concatenating the major/minor opcodes i.e.
85 opcode[31:26,20:16]. The 6 MSBs of this value index into the following
86 array. The 4 LSBs select the bit-pair in the entry (bits 1:0 correspond to
87 LSBs==4'b0000 etc). */
88static unsigned long shmedia_opcode_table[64] = {
89 0x55554044,0x54445055,0x15141514,0x14541414,0x00000000,0x10001000,0x01110055,0x04050015,
90 0x00000444,0xc0000000,0x44545515,0x40405555,0x55550015,0x10005555,0x55555505,0x04050000,
91 0x00000555,0x00000404,0x00040445,0x15151414,0x00000000,0x00000000,0x00000000,0x00000000,
92 0x00000055,0x40404444,0x00000404,0xc0009495,0x00000000,0x00000000,0x00000000,0x00000000,
93 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
94 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
95 0x80005050,0x04005055,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
96 0x81055554,0x00000404,0x55555555,0x55555555,0x00000000,0x00000000,0x00000000,0x00000000
97};
98
99void do_reserved_inst(unsigned long error_code, struct pt_regs *regs)
100{
101 /* Workaround SH5-101 cut2 silicon defect #2815 :
102 in some situations, inter-mode branches from SHcompact -> SHmedia
103 which should take ITLBMISS or EXECPROT exceptions at the target
104 falsely take RESINST at the target instead. */
105
106 unsigned long opcode = 0x6ff4fff0; /* guaranteed reserved opcode */
107 unsigned long pc, aligned_pc;
108 int get_user_error;
109 int trapnr = 12;
110 int signr = SIGILL;
111 char *exception_name = "reserved_instruction";
112
113 pc = regs->pc;
114 if ((pc & 3) == 1) {
115 /* SHmedia : check for defect. This requires executable vmas
116 to be readable too. */
117 aligned_pc = pc & ~3;
118 if (!access_ok(VERIFY_READ, aligned_pc, sizeof(unsigned long))) {
119 get_user_error = -EFAULT;
120 } else {
121 get_user_error = __get_user(opcode, (unsigned long *)aligned_pc);
122 }
123 if (get_user_error >= 0) {
124 unsigned long index, shift;
125 unsigned long major, minor, combined;
126 unsigned long reserved_field;
127 reserved_field = opcode & 0xf; /* These bits are currently reserved as zero in all valid opcodes */
128 major = (opcode >> 26) & 0x3f;
129 minor = (opcode >> 16) & 0xf;
130 combined = (major << 4) | minor;
131 index = major;
132 shift = minor << 1;
133 if (reserved_field == 0) {
134 int opcode_state = (shmedia_opcode_table[index] >> shift) & 0x3;
135 switch (opcode_state) {
136 case OPCODE_INVALID:
137 /* Trap. */
138 break;
139 case OPCODE_USER_VALID:
140 /* Restart the instruction : the branch to the instruction will now be from an RTE
141 not from SHcompact so the silicon defect won't be triggered. */
142 return;
143 case OPCODE_PRIV_VALID:
144 if (!user_mode(regs)) {
145 /* Should only ever get here if a module has
146 SHcompact code inside it. If so, the same fix up is needed. */
147 return; /* same reason */
148 }
149 /* Otherwise, user mode trying to execute a privileged instruction -
150 fall through to trap. */
151 break;
152 case OPCODE_CTRL_REG:
153 /* If in privileged mode, return as above. */
154 if (!user_mode(regs)) return;
155 /* In user mode ... */
156 if (combined == 0x9f) { /* GETCON */
157 unsigned long regno = (opcode >> 20) & 0x3f;
158 if (regno >= 62) {
159 return;
160 }
161 /* Otherwise, reserved or privileged control register, => trap */
162 } else if (combined == 0x1bf) { /* PUTCON */
163 unsigned long regno = (opcode >> 4) & 0x3f;
164 if (regno >= 62) {
165 return;
166 }
167 /* Otherwise, reserved or privileged control register, => trap */
168 } else {
169 /* Trap */
170 }
171 break;
172 default:
173 /* Fall through to trap. */
174 break;
175 }
176 }
177 /* fall through to normal resinst processing */
178 } else {
179 /* Error trying to read opcode. This typically means a
180 real fault, not a RESINST any more. So change the
181 codes. */
182 trapnr = 87;
183 exception_name = "address error (exec)";
184 signr = SIGSEGV;
185 }
186 }
187
188 do_unhandled_exception(trapnr, signr, exception_name, "do_reserved_inst", error_code, regs, current);
189}
190
191#else /* CONFIG_SH64_ID2815_WORKAROUND */
192
193/* If the workaround isn't needed, this is just a straightforward reserved
194 instruction */
195DO_ERROR(12, SIGILL, "reserved instruction", reserved_inst, current)
196
197#endif /* CONFIG_SH64_ID2815_WORKAROUND */
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* Called with interrupts disabled */
200asmlinkage void do_exception_error(unsigned long ex, struct pt_regs *regs)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 die_if_kernel("exception", regs, ex);
203}
204
205int do_unknown_trapa(unsigned long scId, struct pt_regs *regs)
206{
207 /* Syscall debug */
208 printk("System call ID error: [0x1#args:8 #syscall:16 0x%lx]\n", scId);
209
210 die_if_kernel("unknown trapa", regs, scId);
211
212 return -ENOSYS;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static void do_unhandled_exception(int trapnr, int signr, char *str, char *fn_name,
216 unsigned long error_code, struct pt_regs *regs, struct task_struct *tsk)
217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (user_mode(regs))
219 force_sig(signr, tsk);
220
221 die_if_no_fixup(str, regs, error_code);
222}
223
Paul Mundtdb218b32012-06-14 14:05:24 +0900224static int read_opcode(reg_size_t pc, insn_size_t *result_opcode, int from_user_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int get_user_error;
227 unsigned long aligned_pc;
Paul Mundtdb218b32012-06-14 14:05:24 +0900228 insn_size_t opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if ((pc & 3) == 1) {
231 /* SHmedia */
232 aligned_pc = pc & ~3;
233 if (from_user_mode) {
Paul Mundtdb218b32012-06-14 14:05:24 +0900234 if (!access_ok(VERIFY_READ, aligned_pc, sizeof(insn_size_t))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 get_user_error = -EFAULT;
236 } else {
Paul Mundtdb218b32012-06-14 14:05:24 +0900237 get_user_error = __get_user(opcode, (insn_size_t *)aligned_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *result_opcode = opcode;
239 }
240 return get_user_error;
241 } else {
242 /* If the fault was in the kernel, we can either read
243 * this directly, or if not, we fault.
244 */
Paul Mundtdb218b32012-06-14 14:05:24 +0900245 *result_opcode = *(insn_size_t *)aligned_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247 }
248 } else if ((pc & 1) == 0) {
249 /* SHcompact */
250 /* TODO : provide handling for this. We don't really support
251 user-mode SHcompact yet, and for a kernel fault, this would
252 have to come from a module built for SHcompact. */
253 return -EFAULT;
254 } else {
255 /* misaligned */
256 return -EFAULT;
257 }
258}
259
260static int address_is_sign_extended(__u64 a)
261{
262 __u64 b;
263#if (NEFF == 32)
264 b = (__u64)(__s64)(__s32)(a & 0xffffffffUL);
265 return (b == a) ? 1 : 0;
266#else
267#error "Sign extend check only works for NEFF==32"
268#endif
269}
270
Paul Mundtdb218b32012-06-14 14:05:24 +0900271/* return -1 for fault, 0 for OK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int generate_and_check_address(struct pt_regs *regs,
Paul Mundtdb218b32012-06-14 14:05:24 +0900273 insn_size_t opcode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int displacement_not_indexed,
275 int width_shift,
276 __u64 *address)
277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 __u64 base_address, addr;
279 int basereg;
280
Paul Mundtdb218b32012-06-14 14:05:24 +0900281 switch (1 << width_shift) {
282 case 1: inc_unaligned_byte_access(); break;
283 case 2: inc_unaligned_word_access(); break;
284 case 4: inc_unaligned_dword_access(); break;
285 case 8: inc_unaligned_multi_access(); break;
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 basereg = (opcode >> 20) & 0x3f;
289 base_address = regs->regs[basereg];
290 if (displacement_not_indexed) {
291 __s64 displacement;
292 displacement = (opcode >> 10) & 0x3ff;
293 displacement = ((displacement << 54) >> 54); /* sign extend */
294 addr = (__u64)((__s64)base_address + (displacement << width_shift));
295 } else {
296 __u64 offset;
297 int offsetreg;
298 offsetreg = (opcode >> 10) & 0x3f;
299 offset = regs->regs[offsetreg];
300 addr = base_address + offset;
301 }
302
303 /* Check sign extended */
Paul Mundtdb218b32012-06-14 14:05:24 +0900304 if (!address_is_sign_extended(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Check accessible. For misaligned access in the kernel, assume the
308 address is always accessible (and if not, just fault when the
309 load/store gets done.) */
310 if (user_mode(regs)) {
Paul Mundtdb218b32012-06-14 14:05:24 +0900311 inc_unaligned_user_access();
312
313 if (addr >= TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -1;
Paul Mundtdb218b32012-06-14 14:05:24 +0900315 } else
316 inc_unaligned_kernel_access();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 *address = addr;
Paul Mundtdb218b32012-06-14 14:05:24 +0900319
320 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, addr);
321 unaligned_fixups_notify(current, opcode, regs);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static void misaligned_kernel_word_load(__u64 address, int do_sign_extend, __u64 *result)
327{
328 unsigned short x;
329 unsigned char *p, *q;
330 p = (unsigned char *) (int) address;
331 q = (unsigned char *) &x;
332 q[0] = p[0];
333 q[1] = p[1];
334
335 if (do_sign_extend) {
336 *result = (__u64)(__s64) *(short *) &x;
337 } else {
338 *result = (__u64) x;
339 }
340}
341
342static void misaligned_kernel_word_store(__u64 address, __u64 value)
343{
344 unsigned short x;
345 unsigned char *p, *q;
346 p = (unsigned char *) (int) address;
347 q = (unsigned char *) &x;
348
349 x = (__u16) value;
350 p[0] = q[0];
351 p[1] = q[1];
352}
353
354static int misaligned_load(struct pt_regs *regs,
Paul Mundtdb218b32012-06-14 14:05:24 +0900355 insn_size_t opcode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int displacement_not_indexed,
357 int width_shift,
358 int do_sign_extend)
359{
360 /* Return -1 for a fault, 0 for OK */
361 int error;
362 int destreg;
363 __u64 address;
364
365 error = generate_and_check_address(regs, opcode,
366 displacement_not_indexed, width_shift, &address);
Paul Mundtdb218b32012-06-14 14:05:24 +0900367 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return error;
Paul Mundtace2dc72010-10-13 06:55:26 +0900369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 destreg = (opcode >> 4) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (user_mode(regs)) {
372 __u64 buffer;
373
374 if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
375 return -1;
376 }
377
378 if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
379 return -1; /* fault */
380 }
381 switch (width_shift) {
382 case 1:
383 if (do_sign_extend) {
384 regs->regs[destreg] = (__u64)(__s64) *(__s16 *) &buffer;
385 } else {
386 regs->regs[destreg] = (__u64) *(__u16 *) &buffer;
387 }
388 break;
389 case 2:
390 regs->regs[destreg] = (__u64)(__s64) *(__s32 *) &buffer;
391 break;
392 case 3:
393 regs->regs[destreg] = buffer;
394 break;
395 default:
396 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
397 width_shift, (unsigned long) regs->pc);
398 break;
399 }
Paul Mundtc29418c2009-05-08 20:32:56 +0900400 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
402 __u64 lo, hi;
403
404 switch (width_shift) {
405 case 1:
406 misaligned_kernel_word_load(address, do_sign_extend, &regs->regs[destreg]);
407 break;
408 case 2:
409 asm ("ldlo.l %1, 0, %0" : "=r" (lo) : "r" (address));
410 asm ("ldhi.l %1, 3, %0" : "=r" (hi) : "r" (address));
411 regs->regs[destreg] = lo | hi;
412 break;
413 case 3:
414 asm ("ldlo.q %1, 0, %0" : "=r" (lo) : "r" (address));
415 asm ("ldhi.q %1, 7, %0" : "=r" (hi) : "r" (address));
416 regs->regs[destreg] = lo | hi;
417 break;
418
419 default:
420 printk("Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
421 width_shift, (unsigned long) regs->pc);
422 break;
423 }
424 }
425
426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429static int misaligned_store(struct pt_regs *regs,
Paul Mundtdb218b32012-06-14 14:05:24 +0900430 insn_size_t opcode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int displacement_not_indexed,
432 int width_shift)
433{
434 /* Return -1 for a fault, 0 for OK */
435 int error;
436 int srcreg;
437 __u64 address;
438
439 error = generate_and_check_address(regs, opcode,
440 displacement_not_indexed, width_shift, &address);
Paul Mundtdb218b32012-06-14 14:05:24 +0900441 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return error;
Paul Mundtace2dc72010-10-13 06:55:26 +0900443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 srcreg = (opcode >> 4) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (user_mode(regs)) {
446 __u64 buffer;
447
448 if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
449 return -1;
450 }
451
452 switch (width_shift) {
453 case 1:
454 *(__u16 *) &buffer = (__u16) regs->regs[srcreg];
455 break;
456 case 2:
457 *(__u32 *) &buffer = (__u32) regs->regs[srcreg];
458 break;
459 case 3:
460 buffer = regs->regs[srcreg];
461 break;
462 default:
463 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
464 width_shift, (unsigned long) regs->pc);
465 break;
466 }
467
468 if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
469 return -1; /* fault */
470 }
Paul Mundtc29418c2009-05-08 20:32:56 +0900471 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* kernel mode - we can take short cuts since if we fault, it's a genuine bug */
473 __u64 val = regs->regs[srcreg];
474
475 switch (width_shift) {
476 case 1:
477 misaligned_kernel_word_store(address, val);
478 break;
479 case 2:
480 asm ("stlo.l %1, 0, %0" : : "r" (val), "r" (address));
481 asm ("sthi.l %1, 3, %0" : : "r" (val), "r" (address));
482 break;
483 case 3:
484 asm ("stlo.q %1, 0, %0" : : "r" (val), "r" (address));
485 asm ("sthi.q %1, 7, %0" : : "r" (val), "r" (address));
486 break;
487
488 default:
489 printk("Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
490 width_shift, (unsigned long) regs->pc);
491 break;
492 }
493 }
494
495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/* Never need to fix up misaligned FPU accesses within the kernel since that's a real
499 error. */
500static int misaligned_fpu_load(struct pt_regs *regs,
Paul Mundtdb218b32012-06-14 14:05:24 +0900501 insn_size_t opcode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int displacement_not_indexed,
503 int width_shift,
504 int do_paired_load)
505{
506 /* Return -1 for a fault, 0 for OK */
507 int error;
508 int destreg;
509 __u64 address;
510
511 error = generate_and_check_address(regs, opcode,
512 displacement_not_indexed, width_shift, &address);
Paul Mundtdb218b32012-06-14 14:05:24 +0900513 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return error;
Paul Mundtace2dc72010-10-13 06:55:26 +0900515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 destreg = (opcode >> 4) & 0x3f;
517 if (user_mode(regs)) {
518 __u64 buffer;
519 __u32 buflo, bufhi;
520
521 if (!access_ok(VERIFY_READ, (unsigned long) address, 1UL<<width_shift)) {
522 return -1;
523 }
524
525 if (__copy_user(&buffer, (const void *)(int)address, (1 << width_shift)) > 0) {
526 return -1; /* fault */
527 }
528 /* 'current' may be the current owner of the FPU state, so
529 context switch the registers into memory so they can be
530 indexed by register number. */
531 if (last_task_used_math == current) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900532 enable_fpu();
Matt Fleming61cc7b02009-12-14 20:12:04 +0000533 save_fpu(current);
Paul Mundt256b22c2007-11-10 20:27:03 +0900534 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 last_task_used_math = NULL;
536 regs->sr |= SR_FD;
537 }
538
539 buflo = *(__u32*) &buffer;
540 bufhi = *(1 + (__u32*) &buffer);
541
542 switch (width_shift) {
543 case 2:
Paul Mundt3ef29322010-01-19 15:40:03 +0900544 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546 case 3:
547 if (do_paired_load) {
Paul Mundt3ef29322010-01-19 15:40:03 +0900548 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
549 current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 } else {
Paul Mundtf99cb7a42008-02-13 20:28:12 +0900551#if defined(CONFIG_CPU_LITTLE_ENDIAN)
Paul Mundt3ef29322010-01-19 15:40:03 +0900552 current->thread.xstate->hardfpu.fp_regs[destreg] = bufhi;
553 current->thread.xstate->hardfpu.fp_regs[destreg+1] = buflo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#else
Paul Mundt3ef29322010-01-19 15:40:03 +0900555 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
556 current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558 }
559 break;
560 default:
561 printk("Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
562 width_shift, (unsigned long) regs->pc);
563 break;
564 }
565 return 0;
566 } else {
567 die ("Misaligned FPU load inside kernel", regs, 0);
568 return -1;
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static int misaligned_fpu_store(struct pt_regs *regs,
Paul Mundtdb218b32012-06-14 14:05:24 +0900573 insn_size_t opcode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int displacement_not_indexed,
575 int width_shift,
576 int do_paired_load)
577{
578 /* Return -1 for a fault, 0 for OK */
579 int error;
580 int srcreg;
581 __u64 address;
582
583 error = generate_and_check_address(regs, opcode,
584 displacement_not_indexed, width_shift, &address);
Paul Mundtdb218b32012-06-14 14:05:24 +0900585 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return error;
Paul Mundtace2dc72010-10-13 06:55:26 +0900587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 srcreg = (opcode >> 4) & 0x3f;
589 if (user_mode(regs)) {
590 __u64 buffer;
591 /* Initialise these to NaNs. */
592 __u32 buflo=0xffffffffUL, bufhi=0xffffffffUL;
593
594 if (!access_ok(VERIFY_WRITE, (unsigned long) address, 1UL<<width_shift)) {
595 return -1;
596 }
597
598 /* 'current' may be the current owner of the FPU state, so
599 context switch the registers into memory so they can be
600 indexed by register number. */
601 if (last_task_used_math == current) {
Paul Mundt256b22c2007-11-10 20:27:03 +0900602 enable_fpu();
Matt Fleming61cc7b02009-12-14 20:12:04 +0000603 save_fpu(current);
Paul Mundt256b22c2007-11-10 20:27:03 +0900604 disable_fpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 last_task_used_math = NULL;
606 regs->sr |= SR_FD;
607 }
608
609 switch (width_shift) {
610 case 2:
Paul Mundt3ef29322010-01-19 15:40:03 +0900611 buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
613 case 3:
614 if (do_paired_load) {
Paul Mundt3ef29322010-01-19 15:40:03 +0900615 buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
616 bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
Paul Mundtf99cb7a42008-02-13 20:28:12 +0900618#if defined(CONFIG_CPU_LITTLE_ENDIAN)
Paul Mundt3ef29322010-01-19 15:40:03 +0900619 bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg];
620 buflo = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#else
Paul Mundt3ef29322010-01-19 15:40:03 +0900622 buflo = current->thread.xstate->hardfpu.fp_regs[srcreg];
623 bufhi = current->thread.xstate->hardfpu.fp_regs[srcreg+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#endif
625 }
626 break;
627 default:
628 printk("Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
629 width_shift, (unsigned long) regs->pc);
630 break;
631 }
632
633 *(__u32*) &buffer = buflo;
634 *(1 + (__u32*) &buffer) = bufhi;
635 if (__copy_user((void *)(int)address, &buffer, (1 << width_shift)) > 0) {
636 return -1; /* fault */
637 }
638 return 0;
639 } else {
640 die ("Misaligned FPU load inside kernel", regs, 0);
641 return -1;
642 }
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645static int misaligned_fixup(struct pt_regs *regs)
646{
Paul Mundtdb218b32012-06-14 14:05:24 +0900647 insn_size_t opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int error;
649 int major, minor;
Paul Mundtdb218b32012-06-14 14:05:24 +0900650 unsigned int user_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Paul Mundtdb218b32012-06-14 14:05:24 +0900652 user_action = unaligned_user_action();
653 if (!(user_action & UM_FIXUP))
Paul Mundtc29418c2009-05-08 20:32:56 +0900654 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 error = read_opcode(regs->pc, &opcode, user_mode(regs));
657 if (error < 0) {
658 return error;
659 }
660 major = (opcode >> 26) & 0x3f;
661 minor = (opcode >> 16) & 0xf;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 switch (major) {
664 case (0x84>>2): /* LD.W */
665 error = misaligned_load(regs, opcode, 1, 1, 1);
666 break;
667 case (0xb0>>2): /* LD.UW */
668 error = misaligned_load(regs, opcode, 1, 1, 0);
669 break;
670 case (0x88>>2): /* LD.L */
671 error = misaligned_load(regs, opcode, 1, 2, 1);
672 break;
673 case (0x8c>>2): /* LD.Q */
674 error = misaligned_load(regs, opcode, 1, 3, 0);
675 break;
676
677 case (0xa4>>2): /* ST.W */
678 error = misaligned_store(regs, opcode, 1, 1);
679 break;
680 case (0xa8>>2): /* ST.L */
681 error = misaligned_store(regs, opcode, 1, 2);
682 break;
683 case (0xac>>2): /* ST.Q */
684 error = misaligned_store(regs, opcode, 1, 3);
685 break;
686
687 case (0x40>>2): /* indexed loads */
688 switch (minor) {
689 case 0x1: /* LDX.W */
690 error = misaligned_load(regs, opcode, 0, 1, 1);
691 break;
692 case 0x5: /* LDX.UW */
693 error = misaligned_load(regs, opcode, 0, 1, 0);
694 break;
695 case 0x2: /* LDX.L */
696 error = misaligned_load(regs, opcode, 0, 2, 1);
697 break;
698 case 0x3: /* LDX.Q */
699 error = misaligned_load(regs, opcode, 0, 3, 0);
700 break;
701 default:
702 error = -1;
703 break;
704 }
705 break;
706
707 case (0x60>>2): /* indexed stores */
708 switch (minor) {
709 case 0x1: /* STX.W */
710 error = misaligned_store(regs, opcode, 0, 1);
711 break;
712 case 0x2: /* STX.L */
713 error = misaligned_store(regs, opcode, 0, 2);
714 break;
715 case 0x3: /* STX.Q */
716 error = misaligned_store(regs, opcode, 0, 3);
717 break;
718 default:
719 error = -1;
720 break;
721 }
722 break;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 case (0x94>>2): /* FLD.S */
725 error = misaligned_fpu_load(regs, opcode, 1, 2, 0);
726 break;
727 case (0x98>>2): /* FLD.P */
728 error = misaligned_fpu_load(regs, opcode, 1, 3, 1);
729 break;
730 case (0x9c>>2): /* FLD.D */
731 error = misaligned_fpu_load(regs, opcode, 1, 3, 0);
732 break;
733 case (0x1c>>2): /* floating indexed loads */
734 switch (minor) {
735 case 0x8: /* FLDX.S */
736 error = misaligned_fpu_load(regs, opcode, 0, 2, 0);
737 break;
738 case 0xd: /* FLDX.P */
739 error = misaligned_fpu_load(regs, opcode, 0, 3, 1);
740 break;
741 case 0x9: /* FLDX.D */
742 error = misaligned_fpu_load(regs, opcode, 0, 3, 0);
743 break;
744 default:
745 error = -1;
746 break;
747 }
748 break;
749 case (0xb4>>2): /* FLD.S */
750 error = misaligned_fpu_store(regs, opcode, 1, 2, 0);
751 break;
752 case (0xb8>>2): /* FLD.P */
753 error = misaligned_fpu_store(regs, opcode, 1, 3, 1);
754 break;
755 case (0xbc>>2): /* FLD.D */
756 error = misaligned_fpu_store(regs, opcode, 1, 3, 0);
757 break;
758 case (0x3c>>2): /* floating indexed stores */
759 switch (minor) {
760 case 0x8: /* FSTX.S */
761 error = misaligned_fpu_store(regs, opcode, 0, 2, 0);
762 break;
763 case 0xd: /* FSTX.P */
764 error = misaligned_fpu_store(regs, opcode, 0, 3, 1);
765 break;
766 case 0x9: /* FSTX.D */
767 error = misaligned_fpu_store(regs, opcode, 0, 3, 0);
768 break;
769 default:
770 error = -1;
771 break;
772 }
773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 default:
776 /* Fault */
777 error = -1;
778 break;
779 }
780
781 if (error < 0) {
782 return error;
783 } else {
784 regs->pc += 4; /* Skip the instruction that's just been emulated */
785 return 0;
786 }
787
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790asmlinkage void do_debug_interrupt(unsigned long code, struct pt_regs *regs)
791{
792 u64 peek_real_address_q(u64 addr);
793 u64 poke_real_address_q(u64 addr, u64 val);
794 unsigned long long DM_EXP_CAUSE_PHY = 0x0c100010;
795 unsigned long long exp_cause;
796 /* It's not worth ioremapping the debug module registers for the amount
797 of access we make to them - just go direct to their physical
798 addresses. */
799 exp_cause = peek_real_address_q(DM_EXP_CAUSE_PHY);
800 if (exp_cause & ~4) {
801 printk("DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
802 (unsigned long)(exp_cause & 0xffffffff));
803 }
804 show_state();
805 /* Clear all DEBUGINT causes */
806 poke_real_address_q(DM_EXP_CAUSE_PHY, 0x0);
807}
Paul Mundtdd2fdd22010-05-18 15:23:48 +0900808
809void __cpuinit per_cpu_trap_init(void)
810{
811 /* Nothing to do for now, VBR initialization later. */
812}