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>
25 #include <linux/sysctl.h>
26 #include <linux/module.h>
27 #include <linux/perf_event.h>
28 #include <asm/uaccess.h>
31 #include <asm/processor.h>
32 #include <asm/pgtable.h>
38 unsigned long aligned_pc;
51 return get_user_error;
59 }
else if ((pc & 1) == 0) {
71 static int address_is_sign_extended(
__u64 a)
76 return (b == a) ? 1 : 0;
78 #error "Sign extend check only works for NEFF==32"
83 static int generate_and_check_address(
struct pt_regs *
regs,
85 int displacement_not_indexed,
92 switch (1 << width_shift) {
99 basereg = (opcode >> 20) & 0x3f;
100 base_address = regs->
regs[basereg];
101 if (displacement_not_indexed) {
103 displacement = (opcode >> 10) & 0x3ff;
104 displacement = ((displacement << 54) >> 54);
105 addr = (
__u64)((
__s64)base_address + (displacement << width_shift));
109 offsetreg = (opcode >> 10) & 0x3f;
110 offset = regs->
regs[offsetreg];
111 addr = base_address +
offset;
115 if (!address_is_sign_extended(addr))
137 static void misaligned_kernel_word_load(
__u64 address,
int do_sign_extend,
__u64 *
result)
140 unsigned char *
p, *
q;
141 p = (
unsigned char *) (
int)
address;
142 q = (
unsigned char *) &x;
146 if (do_sign_extend) {
156 unsigned char *
p, *
q;
157 p = (
unsigned char *) (
int)
address;
158 q = (
unsigned char *) &x;
165 static int misaligned_load(
struct pt_regs *regs,
167 int displacement_not_indexed,
176 error = generate_and_check_address(regs, opcode,
177 displacement_not_indexed, width_shift, &address);
181 destreg = (opcode >> 4) & 0x3f;
189 if (
__copy_user(&buffer, (
const void *)(
int)address, (1 << width_shift)) > 0) {
192 switch (width_shift) {
194 if (do_sign_extend) {
207 printk(
"Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
208 width_shift, (
unsigned long) regs->
pc);
215 switch (width_shift) {
217 misaligned_kernel_word_load(address, do_sign_extend, ®s->
regs[destreg]);
220 asm (
"ldlo.l %1, 0, %0" :
"=r" (lo) :
"r" (address));
221 asm (
"ldhi.l %1, 3, %0" :
"=r" (hi) :
"r" (address));
222 regs->
regs[destreg] = lo | hi;
225 asm (
"ldlo.q %1, 0, %0" :
"=r" (lo) :
"r" (address));
226 asm (
"ldhi.q %1, 7, %0" :
"=r" (hi) :
"r" (address));
227 regs->
regs[destreg] = lo | hi;
231 printk(
"Unexpected width_shift %d in misaligned_load, PC=%08lx\n",
232 width_shift, (
unsigned long) regs->
pc);
240 static int misaligned_store(
struct pt_regs *regs,
242 int displacement_not_indexed,
250 error = generate_and_check_address(regs, opcode,
251 displacement_not_indexed, width_shift, &address);
255 srcreg = (opcode >> 4) & 0x3f;
263 switch (width_shift) {
271 buffer = regs->
regs[srcreg];
274 printk(
"Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
275 width_shift, (
unsigned long) regs->
pc);
279 if (
__copy_user((
void *)(
int)address, &buffer, (1 << width_shift)) > 0) {
286 switch (width_shift) {
288 misaligned_kernel_word_store(address, val);
291 asm (
"stlo.l %1, 0, %0" : :
"r" (
val),
"r" (address));
292 asm (
"sthi.l %1, 3, %0" : :
"r" (
val),
"r" (address));
295 asm (
"stlo.q %1, 0, %0" : :
"r" (
val),
"r" (address));
296 asm (
"sthi.q %1, 7, %0" : :
"r" (
val),
"r" (address));
300 printk(
"Unexpected width_shift %d in misaligned_store, PC=%08lx\n",
301 width_shift, (
unsigned long) regs->
pc);
311 static int misaligned_fpu_load(
struct pt_regs *regs,
313 int displacement_not_indexed,
322 error = generate_and_check_address(regs, opcode,
323 displacement_not_indexed, width_shift, &address);
327 destreg = (opcode >> 4) & 0x3f;
336 if (
__copy_user(&buffer, (
const void *)(
int)address, (1 << width_shift)) > 0) {
350 buflo = *(
__u32*) &buffer;
351 bufhi = *(1 + (
__u32*) &buffer);
353 switch (width_shift) {
355 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
358 if (do_paired_load) {
359 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
360 current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
362 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
363 current->thread.xstate->hardfpu.fp_regs[destreg] = bufhi;
364 current->thread.xstate->hardfpu.fp_regs[destreg+1] = buflo;
366 current->thread.xstate->hardfpu.fp_regs[destreg] = buflo;
367 current->thread.xstate->hardfpu.fp_regs[destreg+1] = bufhi;
372 printk(
"Unexpected width_shift %d in misaligned_fpu_load, PC=%08lx\n",
373 width_shift, (
unsigned long) regs->
pc);
378 die (
"Misaligned FPU load inside kernel", regs, 0);
383 static int misaligned_fpu_store(
struct pt_regs *regs,
385 int displacement_not_indexed,
394 error = generate_and_check_address(regs, opcode,
395 displacement_not_indexed, width_shift, &address);
399 srcreg = (opcode >> 4) & 0x3f;
403 __u32 buflo=0xffffffff
UL, bufhi=0xffffffff
UL;
420 switch (width_shift) {
422 buflo =
current->thread.xstate->hardfpu.fp_regs[srcreg];
425 if (do_paired_load) {
426 buflo =
current->thread.xstate->hardfpu.fp_regs[srcreg];
427 bufhi =
current->thread.xstate->hardfpu.fp_regs[srcreg+1];
429 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
430 bufhi =
current->thread.xstate->hardfpu.fp_regs[srcreg];
431 buflo =
current->thread.xstate->hardfpu.fp_regs[srcreg+1];
433 buflo =
current->thread.xstate->hardfpu.fp_regs[srcreg];
434 bufhi =
current->thread.xstate->hardfpu.fp_regs[srcreg+1];
439 printk(
"Unexpected width_shift %d in misaligned_fpu_store, PC=%08lx\n",
440 width_shift, (
unsigned long) regs->
pc);
444 *(
__u32*) &buffer = buflo;
445 *(1 + (
__u32*) &buffer) = bufhi;
446 if (
__copy_user((
void *)(
int)address, &buffer, (1 << width_shift)) > 0) {
451 die (
"Misaligned FPU load inside kernel", regs, 0);
456 static int misaligned_fixup(
struct pt_regs *regs)
461 unsigned int user_action;
467 error = read_opcode(regs->
pc, &opcode,
user_mode(regs));
471 major = (opcode >> 26) & 0x3f;
472 minor = (opcode >> 16) & 0xf;
476 error = misaligned_load(regs, opcode, 1, 1, 1);
479 error = misaligned_load(regs, opcode, 1, 1, 0);
482 error = misaligned_load(regs, opcode, 1, 2, 1);
485 error = misaligned_load(regs, opcode, 1, 3, 0);
489 error = misaligned_store(regs, opcode, 1, 1);
492 error = misaligned_store(regs, opcode, 1, 2);
495 error = misaligned_store(regs, opcode, 1, 3);
501 error = misaligned_load(regs, opcode, 0, 1, 1);
504 error = misaligned_load(regs, opcode, 0, 1, 0);
507 error = misaligned_load(regs, opcode, 0, 2, 1);
510 error = misaligned_load(regs, opcode, 0, 3, 0);
521 error = misaligned_store(regs, opcode, 0, 1);
524 error = misaligned_store(regs, opcode, 0, 2);
527 error = misaligned_store(regs, opcode, 0, 3);
536 error = misaligned_fpu_load(regs, opcode, 1, 2, 0);
539 error = misaligned_fpu_load(regs, opcode, 1, 3, 1);
542 error = misaligned_fpu_load(regs, opcode, 1, 3, 0);
547 error = misaligned_fpu_load(regs, opcode, 0, 2, 0);
550 error = misaligned_fpu_load(regs, opcode, 0, 3, 1);
553 error = misaligned_fpu_load(regs, opcode, 0, 3, 0);
561 error = misaligned_fpu_store(regs, opcode, 1, 2, 0);
564 error = misaligned_fpu_store(regs, opcode, 1, 3, 1);
567 error = misaligned_fpu_store(regs, opcode, 1, 3, 0);
572 error = misaligned_fpu_store(regs, opcode, 0, 2, 0);
575 error = misaligned_fpu_store(regs, opcode, 0, 3, 1);
578 error = misaligned_fpu_store(regs, opcode, 0, 3, 0);
600 static void do_unhandled_exception(
int signr,
char *
str,
unsigned long error,
609 #define DO_ERROR(signr, str, name) \
610 asmlinkage void do_##name(unsigned long error_code, struct pt_regs *regs) \
612 do_unhandled_exception(signr, str, error_code, regs); \
618 #if defined(CONFIG_SH64_ID2815_WORKAROUND)
620 #define OPCODE_INVALID 0
621 #define OPCODE_USER_VALID 1
622 #define OPCODE_PRIV_VALID 2
625 #define OPCODE_CTRL_REG 3
632 static unsigned long shmedia_opcode_table[64] = {
633 0x55554044,0x54445055,0x15141514,0x14541414,0x00000000,0x10001000,0x01110055,0x04050015,
634 0x00000444,0xc0000000,0x44545515,0x40405555,0x55550015,0x10005555,0x55555505,0x04050000,
635 0x00000555,0x00000404,0x00040445,0x15151414,0x00000000,0x00000000,0x00000000,0x00000000,
636 0x00000055,0x40404444,0x00000404,0xc0009495,0x00000000,0x00000000,0x00000000,0x00000000,
637 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
638 0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
639 0x80005050,0x04005055,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,0x55555555,
640 0x81055554,0x00000404,0x55555555,0x55555555,0x00000000,0x00000000,0x00000000,0x00000000
650 unsigned long pc, aligned_pc;
651 unsigned long index, shift;
652 unsigned long major, minor, combined;
653 unsigned long reserved_field;
657 char *exception_name =
"reserved_instruction";
667 aligned_pc = pc & ~3;
673 if (get_user_error < 0) {
679 exception_name =
"address error (exec)";
685 reserved_field = opcode & 0xf;
689 major = (opcode >> 26) & 0x3f;
690 minor = (opcode >> 16) & 0xf;
691 combined = (major << 4) | minor;
694 opcode_state = (shmedia_opcode_table[
index] >> shift) & 0x3;
695 switch (opcode_state) {
699 case OPCODE_USER_VALID:
706 case OPCODE_PRIV_VALID:
721 case OPCODE_CTRL_REG:
727 if (combined == 0x9f) {
728 unsigned long regno = (opcode >> 20) & 0x3f;
734 }
else if (combined == 0x1bf) {
735 unsigned long regno = (opcode >> 4) & 0x3f;
750 do_unhandled_exception(signr, exception_name, error_code, regs);
770 printk(
"System call ID error: [0x1#args:8 #syscall:16 0x%lx]\n", scId);
783 if (misaligned_fixup(regs) < 0)
784 do_unhandled_exception(SIGSEGV,
"address error(load)",
790 if (misaligned_fixup(regs) < 0)
791 do_unhandled_exception(SIGSEGV,
"address error(store)",
797 u64 peek_real_address_q(
u64 addr);
798 u64 poke_real_address_q(
u64 addr,
u64 val);
799 unsigned long long DM_EXP_CAUSE_PHY = 0x0c100010;
800 unsigned long long exp_cause;
804 exp_cause = peek_real_address_q(DM_EXP_CAUSE_PHY);
806 printk(
"DM.EXP_CAUSE had unexpected bits set (=%08lx)\n",
807 (
unsigned long)(exp_cause & 0xffffffff));
810 poke_real_address_q(DM_EXP_CAUSE_PHY, 0x0);