1 /* $Id: unaligned.c,v 1.23 2001/12/21 00:54:31 davem Exp $
2 * unaligned.c: Unaligned load/store trap handling with special
3 * cases for the kernel to do them more quickly.
5 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/module.h>
14 #include <asm/ptrace.h>
15 #include <asm/processor.h>
16 #include <asm/system.h>
17 #include <asm/uaccess.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
21 /* #define DEBUG_MNA */
24 load
, /* ld, ldd, ldh, ldsh */
25 store
, /* st, std, sth, stsh */
26 both
, /* Swap, ldstub, etc. */
33 static char *dirstrings
[] = {
34 "load", "store", "both", "fpload", "fpstore", "invalid"
38 static inline enum direction
decode_direction(unsigned int insn
)
40 unsigned long tmp
= (insn
>> 21) & 1;
45 if(((insn
>>19)&0x3f) == 15)
52 /* 8 = double-word, 4 = word, 2 = half-word */
53 static inline int decode_access_size(unsigned int insn
)
55 insn
= (insn
>> 19) & 3;
64 printk("Impossible unaligned trap. insn=%08x\n", insn
);
65 die_if_kernel("Byte sized unaligned access?!?!", current
->thread
.kregs
);
66 return 4; /* just to keep gcc happy. */
70 /* 0x400000 = signed, 0 = unsigned */
71 static inline int decode_signedness(unsigned int insn
)
73 return (insn
& 0x400000);
76 static inline void maybe_flush_windows(unsigned int rs1
, unsigned int rs2
,
79 if(rs2
>= 16 || rs1
>= 16 || rd
>= 16) {
81 __asm__
__volatile__("save %sp, -0x40, %sp\n\t"
82 "save %sp, -0x40, %sp\n\t"
83 "save %sp, -0x40, %sp\n\t"
84 "save %sp, -0x40, %sp\n\t"
85 "save %sp, -0x40, %sp\n\t"
86 "save %sp, -0x40, %sp\n\t"
87 "save %sp, -0x40, %sp\n\t"
88 "restore; restore; restore; restore;\n\t"
89 "restore; restore; restore;\n\t");
93 static inline int sign_extend_imm13(int imm
)
95 return imm
<< 19 >> 19;
98 static inline unsigned long fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
100 struct reg_window
*win
;
103 return (!reg
? 0 : regs
->u_regs
[reg
]);
105 /* Ho hum, the slightly complicated case. */
106 win
= (struct reg_window
*) regs
->u_regs
[UREG_FP
];
107 return win
->locals
[reg
- 16]; /* yes, I know what this does... */
110 static inline unsigned long safe_fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
112 struct reg_window __user
*win
;
116 return (!reg
? 0 : regs
->u_regs
[reg
]);
118 /* Ho hum, the slightly complicated case. */
119 win
= (struct reg_window __user
*) regs
->u_regs
[UREG_FP
];
121 if ((unsigned long)win
& 3)
124 if (get_user(ret
, &win
->locals
[reg
- 16]))
130 static inline unsigned long *fetch_reg_addr(unsigned int reg
, struct pt_regs
*regs
)
132 struct reg_window
*win
;
135 return ®s
->u_regs
[reg
];
136 win
= (struct reg_window
*) regs
->u_regs
[UREG_FP
];
137 return &win
->locals
[reg
- 16];
140 static unsigned long compute_effective_address(struct pt_regs
*regs
,
143 unsigned int rs1
= (insn
>> 14) & 0x1f;
144 unsigned int rs2
= insn
& 0x1f;
145 unsigned int rd
= (insn
>> 25) & 0x1f;
148 maybe_flush_windows(rs1
, 0, rd
);
149 return (fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
151 maybe_flush_windows(rs1
, rs2
, rd
);
152 return (fetch_reg(rs1
, regs
) + fetch_reg(rs2
, regs
));
156 unsigned long safe_compute_effective_address(struct pt_regs
*regs
,
159 unsigned int rs1
= (insn
>> 14) & 0x1f;
160 unsigned int rs2
= insn
& 0x1f;
161 unsigned int rd
= (insn
>> 25) & 0x1f;
164 maybe_flush_windows(rs1
, 0, rd
);
165 return (safe_fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
167 maybe_flush_windows(rs1
, rs2
, rd
);
168 return (safe_fetch_reg(rs1
, regs
) + safe_fetch_reg(rs2
, regs
));
172 /* This is just to make gcc think panic does return... */
173 static void unaligned_panic(char *str
)
178 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
179 #define do_integer_load(dest_reg, size, saddr, is_signed, errh) ({ \
180 __asm__ __volatile__ ( \
185 "4:\t" " ldub [%2], %%l1\n" \
186 "5:\t" "ldub [%2 + 1], %%l2\n\t" \
187 "sll %%l1, 8, %%l1\n\t" \
190 " add %%l1, %%l2, %%l1\n\t" \
191 "sll %%l1, 16, %%l1\n\t" \
192 "sra %%l1, 16, %%l1\n" \
195 "6:\t" "ldub [%2 + 1], %%l2\n\t" \
196 "sll %%l1, 24, %%l1\n" \
197 "7:\t" "ldub [%2 + 2], %%g7\n\t" \
198 "sll %%l2, 16, %%l2\n" \
199 "8:\t" "ldub [%2 + 3], %%g1\n\t" \
200 "sll %%g7, 8, %%g7\n\t" \
201 "or %%l1, %%l2, %%l1\n\t" \
202 "or %%g7, %%g1, %%g7\n\t" \
203 "or %%l1, %%g7, %%l1\n\t" \
206 "9:\t" "ldub [%2], %%l1\n" \
207 "10:\t" "ldub [%2 + 1], %%l2\n\t" \
208 "sll %%l1, 24, %%l1\n" \
209 "11:\t" "ldub [%2 + 2], %%g7\n\t" \
210 "sll %%l2, 16, %%l2\n" \
211 "12:\t" "ldub [%2 + 3], %%g1\n\t" \
212 "sll %%g7, 8, %%g7\n\t" \
213 "or %%l1, %%l2, %%l1\n\t" \
214 "or %%g7, %%g1, %%g7\n\t" \
215 "or %%l1, %%g7, %%g7\n" \
216 "13:\t" "ldub [%2 + 4], %%l1\n\t" \
218 "14:\t" "ldub [%2 + 5], %%l2\n\t" \
219 "sll %%l1, 24, %%l1\n" \
220 "15:\t" "ldub [%2 + 6], %%g7\n\t" \
221 "sll %%l2, 16, %%l2\n" \
222 "16:\t" "ldub [%2 + 7], %%g1\n\t" \
223 "sll %%g7, 8, %%g7\n\t" \
224 "or %%l1, %%l2, %%l1\n\t" \
225 "or %%g7, %%g1, %%g7\n\t" \
226 "or %%l1, %%g7, %%g7\n\t" \
227 "st %%g7, [%0 + 4]\n" \
229 ".section __ex_table,#alloc\n\t" \
230 ".word 4b, " #errh "\n\t" \
231 ".word 5b, " #errh "\n\t" \
232 ".word 6b, " #errh "\n\t" \
233 ".word 7b, " #errh "\n\t" \
234 ".word 8b, " #errh "\n\t" \
235 ".word 9b, " #errh "\n\t" \
236 ".word 10b, " #errh "\n\t" \
237 ".word 11b, " #errh "\n\t" \
238 ".word 12b, " #errh "\n\t" \
239 ".word 13b, " #errh "\n\t" \
240 ".word 14b, " #errh "\n\t" \
241 ".word 15b, " #errh "\n\t" \
242 ".word 16b, " #errh "\n\n\t" \
244 : : "r" (dest_reg), "r" (size), "r" (saddr), "r" (is_signed) \
245 : "l1", "l2", "g7", "g1", "cc"); \
248 #define store_common(dst_addr, size, src_val, errh) ({ \
249 __asm__ __volatile__ ( \
255 " srl %%l1, 24, %%l2\n\t" \
256 "srl %%l1, 16, %%g7\n" \
257 "4:\t" "stb %%l2, [%0]\n\t" \
258 "srl %%l1, 8, %%l2\n" \
259 "5:\t" "stb %%g7, [%0 + 1]\n\t" \
260 "ld [%2 + 4], %%g7\n" \
261 "6:\t" "stb %%l2, [%0 + 2]\n\t" \
262 "srl %%g7, 24, %%l2\n" \
263 "7:\t" "stb %%l1, [%0 + 3]\n\t" \
264 "srl %%g7, 16, %%l1\n" \
265 "8:\t" "stb %%l2, [%0 + 4]\n\t" \
266 "srl %%g7, 8, %%l2\n" \
267 "9:\t" "stb %%l1, [%0 + 5]\n" \
268 "10:\t" "stb %%l2, [%0 + 6]\n\t" \
270 "11:\t" " stb %%g7, [%0 + 7]\n" \
271 "1:\t" "srl %%l1, 16, %%g7\n" \
272 "12:\t" "stb %%l2, [%0]\n\t" \
273 "srl %%l1, 8, %%l2\n" \
274 "13:\t" "stb %%g7, [%0 + 1]\n" \
275 "14:\t" "stb %%l2, [%0 + 2]\n\t" \
277 "15:\t" " stb %%l1, [%0 + 3]\n" \
278 "2:\t" "srl %%l1, 8, %%l2\n" \
279 "16:\t" "stb %%l2, [%0]\n" \
280 "17:\t" "stb %%l1, [%0 + 1]\n" \
282 ".section __ex_table,#alloc\n\t" \
283 ".word 4b, " #errh "\n\t" \
284 ".word 5b, " #errh "\n\t" \
285 ".word 6b, " #errh "\n\t" \
286 ".word 7b, " #errh "\n\t" \
287 ".word 8b, " #errh "\n\t" \
288 ".word 9b, " #errh "\n\t" \
289 ".word 10b, " #errh "\n\t" \
290 ".word 11b, " #errh "\n\t" \
291 ".word 12b, " #errh "\n\t" \
292 ".word 13b, " #errh "\n\t" \
293 ".word 14b, " #errh "\n\t" \
294 ".word 15b, " #errh "\n\t" \
295 ".word 16b, " #errh "\n\t" \
296 ".word 17b, " #errh "\n\n\t" \
298 : : "r" (dst_addr), "r" (size), "r" (src_val) \
299 : "l1", "l2", "g7", "g1", "cc"); \
302 #define do_integer_store(reg_num, size, dst_addr, regs, errh) ({ \
303 unsigned long *src_val; \
304 static unsigned long zero[2] = { 0, }; \
306 if (reg_num) src_val = fetch_reg_addr(reg_num, regs); \
308 src_val = &zero[0]; \
310 zero[1] = fetch_reg(1, regs); \
312 store_common(dst_addr, size, src_val, errh); \
316 extern int do_int_load(unsigned long *dest_reg
, int size
,
317 unsigned long *saddr
, int is_signed
);
318 extern int __do_int_store(unsigned long *dst_addr
, int size
,
319 unsigned long *src_val
);
321 static int do_int_store(int reg_num
, int size
, unsigned long *dst_addr
,
322 struct pt_regs
*regs
)
324 unsigned long zero
[2] = { 0, 0 };
325 unsigned long *src_val
;
328 src_val
= fetch_reg_addr(reg_num
, regs
);
332 zero
[1] = fetch_reg(1, regs
);
334 return __do_int_store(dst_addr
, size
, src_val
);
336 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
338 extern void smp_capture(void);
339 extern void smp_release(void);
341 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
342 #define do_atomic(srcdest_reg, mem, errh) ({ \
343 unsigned long flags, tmp; \
346 local_irq_save(flags); \
347 tmp = *srcdest_reg; \
348 do_integer_load(srcdest_reg, 4, mem, 0, errh); \
349 store_common(mem, 4, &tmp, errh); \
350 local_irq_restore(flags); \
355 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
356 static inline void advance(struct pt_regs
*regs
)
358 regs
->pc
= regs
->npc
;
362 static inline int floating_point_load_or_store_p(unsigned int insn
)
364 return (insn
>> 24) & 1;
367 static inline int ok_for_kernel(unsigned int insn
)
369 return !floating_point_load_or_store_p(insn
);
372 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
373 void kernel_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
) __asm__ ("kernel_mna_trap_fault");
375 void kernel_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
377 static void kernel_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
378 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
380 unsigned long g2
= regs
->u_regs
[UREG_G2
];
381 unsigned long fixup
= search_extables_range(regs
->pc
, &g2
);
384 unsigned long address
= compute_effective_address(regs
, insn
);
385 if(address
< PAGE_SIZE
) {
386 printk(KERN_ALERT
"Unable to handle kernel NULL pointer dereference in mna handler");
388 printk(KERN_ALERT
"Unable to handle kernel paging request in mna handler");
389 printk(KERN_ALERT
" at virtual address %08lx\n",address
);
390 printk(KERN_ALERT
"current->{mm,active_mm}->context = %08lx\n",
391 (current
->mm
? current
->mm
->context
:
392 current
->active_mm
->context
));
393 printk(KERN_ALERT
"current->{mm,active_mm}->pgd = %08lx\n",
394 (current
->mm
? (unsigned long) current
->mm
->pgd
:
395 (unsigned long) current
->active_mm
->pgd
));
396 die_if_kernel("Oops", regs
);
400 regs
->npc
= regs
->pc
+ 4;
401 regs
->u_regs
[UREG_G2
] = g2
;
404 asmlinkage
void kernel_unaligned_trap(struct pt_regs
*regs
, unsigned int insn
)
406 enum direction dir
= decode_direction(insn
);
407 int size
= decode_access_size(insn
);
409 if(!ok_for_kernel(insn
) || dir
== both
) {
410 printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
412 unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
413 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
415 __asm__
__volatile__ ("\n"
416 "kernel_unaligned_trap_fault:\n\t"
418 "call kernel_mna_trap_fault\n\t"
421 : "r" (regs
), "r" (insn
)
422 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
423 "g1", "g2", "g3", "g4", "g5", "g7", "cc");
425 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
427 unsigned long addr
= compute_effective_address(regs
, insn
);
428 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
431 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
434 printk("KMNA: pc=%08lx [dir=%s addr=%08lx size=%d] retpc[%08lx]\n",
435 regs
->pc
, dirstrings
[dir
], addr
, size
, regs
->u_regs
[UREG_RETPC
]);
437 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
441 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
443 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
444 do_integer_load(fetch_reg_addr(((insn
>>25)&0x1f), regs
),
445 size
, (unsigned long *) addr
,
446 decode_signedness(insn
),
447 kernel_unaligned_trap_fault
);
449 err
= do_int_load(fetch_reg_addr(((insn
>>25)&0x1f),
451 size
, (unsigned long *) addr
,
452 decode_signedness(insn
));
453 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
457 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
458 do_integer_store(((insn
>>25)&0x1f), size
,
459 (unsigned long *) addr
, regs
,
460 kernel_unaligned_trap_fault
);
462 err
= do_int_store(((insn
>>25)&0x1f), size
,
463 (unsigned long *) addr
, regs
);
464 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
466 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
467 #if 0 /* unsupported */
469 do_atomic(fetch_reg_addr(((insn
>>25)&0x1f), regs
),
470 (unsigned long *) addr
,
471 kernel_unaligned_trap_fault
);
475 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
477 panic("Impossible kernel unaligned trap.");
480 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
484 kernel_mna_trap_fault(regs
, insn
);
487 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
491 static inline int ok_for_user(struct pt_regs
*regs
, unsigned int insn
,
495 int check
= (dir
== load
) ? VERIFY_READ
: VERIFY_WRITE
;
496 int size
= ((insn
>> 19) & 3) == 3 ? 8 : 4;
498 if ((regs
->pc
| regs
->npc
) & 3)
501 /* Must access_ok() in all the necessary places. */
502 #define WINREG_ADDR(regnum) \
503 ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
505 reg
= (insn
>> 25) & 0x1f;
507 if (!access_ok(check
, WINREG_ADDR(reg
- 16), size
))
510 reg
= (insn
>> 14) & 0x1f;
512 if (!access_ok(check
, WINREG_ADDR(reg
- 16), size
))
515 if (!(insn
& 0x2000)) {
518 if (!access_ok(check
, WINREG_ADDR(reg
- 16), size
))
526 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
527 void user_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
) __asm__ ("user_mna_trap_fault");
529 void user_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
531 static void user_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
532 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
536 info
.si_signo
= SIGBUS
;
538 info
.si_code
= BUS_ADRALN
;
539 info
.si_addr
= (void __user
*)safe_compute_effective_address(regs
, insn
);
541 send_sig_info(SIGBUS
, &info
, current
);
544 asmlinkage
void user_unaligned_trap(struct pt_regs
*regs
, unsigned int insn
)
549 if(!(current
->thread
.flags
& SPARC_FLAG_UNALIGNED
) ||
550 (((insn
>> 30) & 3) != 3))
552 dir
= decode_direction(insn
);
553 if(!ok_for_user(regs
, insn
, dir
)) {
556 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
557 int size
= decode_access_size(insn
);
559 int err
, size
= decode_access_size(insn
);
560 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
563 if(floating_point_load_or_store_p(insn
)) {
564 printk("User FPU load/store unaligned unsupported.\n");
568 addr
= compute_effective_address(regs
, insn
);
571 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
572 do_integer_load(fetch_reg_addr(((insn
>>25)&0x1f), regs
),
573 size
, (unsigned long *) addr
,
574 decode_signedness(insn
),
575 user_unaligned_trap_fault
);
577 err
= do_int_load(fetch_reg_addr(((insn
>>25)&0x1f),
579 size
, (unsigned long *) addr
,
580 decode_signedness(insn
));
581 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
585 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
586 do_integer_store(((insn
>>25)&0x1f), size
,
587 (unsigned long *) addr
, regs
,
588 user_unaligned_trap_fault
);
590 err
= do_int_store(((insn
>>25)&0x1f), size
,
591 (unsigned long *) addr
, regs
);
592 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
596 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
597 #if 0 /* unsupported */
598 do_atomic(fetch_reg_addr(((insn
>>25)&0x1f), regs
),
599 (unsigned long *) addr
,
600 user_unaligned_trap_fault
);
603 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
605 * This was supported in 2.4. However, we question
606 * the value of SWAP instruction across word boundaries.
608 printk("Unaligned SWAP unsupported.\n");
609 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
614 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
618 unaligned_panic("Impossible user unaligned trap.");
619 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
621 __asm__
__volatile__ ("\n"
622 "user_unaligned_trap_fault:\n\t"
624 "call user_mna_trap_fault\n\t"
627 : "r" (regs
), "r" (insn
)
628 : "o0", "o1", "o2", "o3", "o4", "o5", "o7",
629 "g1", "g2", "g3", "g4", "g5", "g7", "cc");
631 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
634 <<<<<<< HEAD
:arch
/sparc
/kernel
/unaligned
.c
641 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/sparc
/kernel
/unaligned
.c
646 user_mna_trap_fault(regs
, insn
);