10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <asm/ptrace.h>
14 #include <asm/processor.h>
15 #include <asm/uaccess.h>
17 #include <linux/perf_event.h>
28 static inline enum direction decode_direction(
unsigned int insn)
30 unsigned long tmp = (insn >> 21) & 1;
35 if(((insn>>19)&0x3f) == 15)
43 static inline int decode_access_size(
unsigned int insn)
45 insn = (insn >> 19) & 3;
54 printk(
"Impossible unaligned trap. insn=%08x\n", insn);
61 static inline int decode_signedness(
unsigned int insn)
63 return (insn & 0x400000);
66 static inline void maybe_flush_windows(
unsigned int rs1,
unsigned int rs2,
69 if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
71 __asm__ __volatile__(
"save %sp, -0x40, %sp\n\t"
72 "save %sp, -0x40, %sp\n\t"
73 "save %sp, -0x40, %sp\n\t"
74 "save %sp, -0x40, %sp\n\t"
75 "save %sp, -0x40, %sp\n\t"
76 "save %sp, -0x40, %sp\n\t"
77 "save %sp, -0x40, %sp\n\t"
78 "restore; restore; restore; restore;\n\t"
79 "restore; restore; restore;\n\t");
83 static inline int sign_extend_imm13(
int imm)
85 return imm << 19 >> 19;
88 static inline unsigned long fetch_reg(
unsigned int reg,
struct pt_regs *
regs)
93 return (!reg ? 0 : regs->
u_regs[reg]);
97 return win->
locals[reg - 16];
100 static inline unsigned long safe_fetch_reg(
unsigned int reg,
struct pt_regs *regs)
106 return (!reg ? 0 : regs->
u_regs[reg]);
111 if ((
unsigned long)win & 3)
120 static inline unsigned long *fetch_reg_addr(
unsigned int reg,
struct pt_regs *regs)
127 return &win->
locals[reg - 16];
133 unsigned int rs1 = (insn >> 14) & 0x1f;
134 unsigned int rs2 = insn & 0x1f;
135 unsigned int rd = (insn >> 25) & 0x1f;
138 maybe_flush_windows(rs1, 0, rd);
139 return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
141 maybe_flush_windows(rs1, rs2, rd);
142 return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
149 unsigned int rs1 = (insn >> 14) & 0x1f;
150 unsigned int rs2 = insn & 0x1f;
151 unsigned int rd = (insn >> 25) & 0x1f;
154 maybe_flush_windows(rs1, 0, rd);
155 return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
157 maybe_flush_windows(rs1, rs2, rd);
158 return (safe_fetch_reg(rs1, regs) + safe_fetch_reg(rs2, regs));
163 static void unaligned_panic(
char *
str)
172 unsigned long *src_val);
174 static int do_int_store(
int reg_num,
int size,
unsigned long *
dst_addr,
177 unsigned long zero[2] = { 0, 0 };
178 unsigned long *src_val;
181 src_val = fetch_reg_addr(reg_num, regs);
185 zero[1] = fetch_reg(1, regs);
193 static inline void advance(
struct pt_regs *regs)
195 regs->
pc = regs->
npc;
199 static inline int floating_point_load_or_store_p(
unsigned int insn)
201 return (insn >> 24) & 1;
204 static inline int ok_for_kernel(
unsigned int insn)
206 return !floating_point_load_or_store_p(insn);
209 static void kernel_mna_trap_fault(
struct pt_regs *regs,
unsigned int insn)
217 printk(
KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
226 (
unsigned long)
current->active_mm->pgd));
231 regs->
npc = regs->
pc + 4;
238 int size = decode_access_size(insn);
240 if(!ok_for_kernel(insn) || dir ==
both) {
241 printk(
"Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
243 unaligned_panic(
"Wheee. Kernel does fpu/atomic unaligned load/store.");
251 err =
do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
253 size, (
unsigned long *) addr,
254 decode_signedness(insn));
258 err = do_int_store(((insn>>25)&0x1f), size,
259 (
unsigned long *) addr, regs);
262 panic(
"Impossible kernel unaligned trap.");
266 kernel_mna_trap_fault(regs, insn);
272 static inline int ok_for_user(
struct pt_regs *regs,
unsigned int insn,
277 int size = ((insn >> 19) & 3) == 3 ? 8 : 4;
279 if ((regs->
pc | regs->
npc) & 3)
283 #define WINREG_ADDR(regnum) \
284 ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
286 reg = (insn >> 25) & 0x1f;
291 reg = (insn >> 14) & 0x1f;
296 if (!(insn & 0x2000)) {
307 static void user_mna_trap_fault(
struct pt_regs *regs,
unsigned int insn)
324 (((insn >> 30) & 3) != 3))
326 dir = decode_direction(insn);
327 if(!ok_for_user(regs, insn, dir)) {
330 int err, size = decode_access_size(insn);
333 if(floating_point_load_or_store_p(insn)) {
334 printk(
"User FPU load/store unaligned unsupported.\n");
342 err =
do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
344 size, (
unsigned long *) addr,
345 decode_signedness(insn));
349 err = do_int_store(((insn>>25)&0x1f), size,
350 (
unsigned long *) addr, regs);
358 printk(
"Unaligned SWAP unsupported.\n");
363 unaligned_panic(
"Impossible user unaligned trap.");
374 user_mna_trap_fault(regs, insn);