Linux 2.6.25.3
[linux/fpc-iii.git] / arch / sparc / kernel / unaligned.c
blob33857be16661a53e7b2c1741eb51ab02a5fee020
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)
7 */
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.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 */
23 enum direction {
24 load, /* ld, ldd, ldh, ldsh */
25 store, /* st, std, sth, stsh */
26 both, /* Swap, ldstub, etc. */
27 fpload,
28 fpstore,
29 invalid,
32 #ifdef DEBUG_MNA
33 static char *dirstrings[] = {
34 "load", "store", "both", "fpload", "fpstore", "invalid"
36 #endif
38 static inline enum direction decode_direction(unsigned int insn)
40 unsigned long tmp = (insn >> 21) & 1;
42 if(!tmp)
43 return load;
44 else {
45 if(((insn>>19)&0x3f) == 15)
46 return both;
47 else
48 return store;
52 /* 8 = double-word, 4 = word, 2 = half-word */
53 static inline int decode_access_size(unsigned int insn)
55 insn = (insn >> 19) & 3;
57 if(!insn)
58 return 4;
59 else if(insn == 3)
60 return 8;
61 else if(insn == 2)
62 return 2;
63 else {
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,
77 unsigned int rd)
79 if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
80 /* Wheee... */
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;
102 if(reg < 16)
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;
113 unsigned long ret;
115 if (reg < 16)
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)
122 return -1;
124 if (get_user(ret, &win->locals[reg - 16]))
125 return -1;
127 return ret;
130 static inline unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
132 struct reg_window *win;
134 if(reg < 16)
135 return &regs->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,
141 unsigned int insn)
143 unsigned int rs1 = (insn >> 14) & 0x1f;
144 unsigned int rs2 = insn & 0x1f;
145 unsigned int rd = (insn >> 25) & 0x1f;
147 if(insn & 0x2000) {
148 maybe_flush_windows(rs1, 0, rd);
149 return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
150 } else {
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,
157 unsigned int insn)
159 unsigned int rs1 = (insn >> 14) & 0x1f;
160 unsigned int rs2 = insn & 0x1f;
161 unsigned int rd = (insn >> 25) & 0x1f;
163 if(insn & 0x2000) {
164 maybe_flush_windows(rs1, 0, rd);
165 return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
166 } else {
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)
175 panic(str);
178 /* una_asm.S */
179 extern int do_int_load(unsigned long *dest_reg, int size,
180 unsigned long *saddr, int is_signed);
181 extern int __do_int_store(unsigned long *dst_addr, int size,
182 unsigned long *src_val);
184 static int do_int_store(int reg_num, int size, unsigned long *dst_addr,
185 struct pt_regs *regs)
187 unsigned long zero[2] = { 0, 0 };
188 unsigned long *src_val;
190 if (reg_num)
191 src_val = fetch_reg_addr(reg_num, regs);
192 else {
193 src_val = &zero[0];
194 if (size == 8)
195 zero[1] = fetch_reg(1, regs);
197 return __do_int_store(dst_addr, size, src_val);
200 extern void smp_capture(void);
201 extern void smp_release(void);
203 static inline void advance(struct pt_regs *regs)
205 regs->pc = regs->npc;
206 regs->npc += 4;
209 static inline int floating_point_load_or_store_p(unsigned int insn)
211 return (insn >> 24) & 1;
214 static inline int ok_for_kernel(unsigned int insn)
216 return !floating_point_load_or_store_p(insn);
219 static void kernel_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
221 unsigned long g2 = regs->u_regs [UREG_G2];
222 unsigned long fixup = search_extables_range(regs->pc, &g2);
224 if (!fixup) {
225 unsigned long address = compute_effective_address(regs, insn);
226 if(address < PAGE_SIZE) {
227 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
228 } else
229 printk(KERN_ALERT "Unable to handle kernel paging request in mna handler");
230 printk(KERN_ALERT " at virtual address %08lx\n",address);
231 printk(KERN_ALERT "current->{mm,active_mm}->context = %08lx\n",
232 (current->mm ? current->mm->context :
233 current->active_mm->context));
234 printk(KERN_ALERT "current->{mm,active_mm}->pgd = %08lx\n",
235 (current->mm ? (unsigned long) current->mm->pgd :
236 (unsigned long) current->active_mm->pgd));
237 die_if_kernel("Oops", regs);
238 /* Not reached */
240 regs->pc = fixup;
241 regs->npc = regs->pc + 4;
242 regs->u_regs [UREG_G2] = g2;
245 asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
247 enum direction dir = decode_direction(insn);
248 int size = decode_access_size(insn);
250 if(!ok_for_kernel(insn) || dir == both) {
251 printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
252 regs->pc);
253 unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
254 } else {
255 unsigned long addr = compute_effective_address(regs, insn);
256 int err;
258 #ifdef DEBUG_MNA
259 printk("KMNA: pc=%08lx [dir=%s addr=%08lx size=%d] retpc[%08lx]\n",
260 regs->pc, dirstrings[dir], addr, size, regs->u_regs[UREG_RETPC]);
261 #endif
262 switch (dir) {
263 case load:
264 err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
265 regs),
266 size, (unsigned long *) addr,
267 decode_signedness(insn));
268 break;
270 case store:
271 err = do_int_store(((insn>>25)&0x1f), size,
272 (unsigned long *) addr, regs);
273 break;
274 default:
275 panic("Impossible kernel unaligned trap.");
276 /* Not reached... */
278 if (err)
279 kernel_mna_trap_fault(regs, insn);
280 else
281 advance(regs);
285 static inline int ok_for_user(struct pt_regs *regs, unsigned int insn,
286 enum direction dir)
288 unsigned int reg;
289 int check = (dir == load) ? VERIFY_READ : VERIFY_WRITE;
290 int size = ((insn >> 19) & 3) == 3 ? 8 : 4;
292 if ((regs->pc | regs->npc) & 3)
293 return 0;
295 /* Must access_ok() in all the necessary places. */
296 #define WINREG_ADDR(regnum) \
297 ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
299 reg = (insn >> 25) & 0x1f;
300 if (reg >= 16) {
301 if (!access_ok(check, WINREG_ADDR(reg - 16), size))
302 return -EFAULT;
304 reg = (insn >> 14) & 0x1f;
305 if (reg >= 16) {
306 if (!access_ok(check, WINREG_ADDR(reg - 16), size))
307 return -EFAULT;
309 if (!(insn & 0x2000)) {
310 reg = (insn & 0x1f);
311 if (reg >= 16) {
312 if (!access_ok(check, WINREG_ADDR(reg - 16), size))
313 return -EFAULT;
316 #undef WINREG_ADDR
317 return 0;
320 static void user_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
322 siginfo_t info;
324 info.si_signo = SIGBUS;
325 info.si_errno = 0;
326 info.si_code = BUS_ADRALN;
327 info.si_addr = (void __user *)safe_compute_effective_address(regs, insn);
328 info.si_trapno = 0;
329 send_sig_info(SIGBUS, &info, current);
332 asmlinkage void user_unaligned_trap(struct pt_regs *regs, unsigned int insn)
334 enum direction dir;
336 lock_kernel();
337 if(!(current->thread.flags & SPARC_FLAG_UNALIGNED) ||
338 (((insn >> 30) & 3) != 3))
339 goto kill_user;
340 dir = decode_direction(insn);
341 if(!ok_for_user(regs, insn, dir)) {
342 goto kill_user;
343 } else {
344 int err, size = decode_access_size(insn);
345 unsigned long addr;
347 if(floating_point_load_or_store_p(insn)) {
348 printk("User FPU load/store unaligned unsupported.\n");
349 goto kill_user;
352 addr = compute_effective_address(regs, insn);
353 switch(dir) {
354 case load:
355 err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
356 regs),
357 size, (unsigned long *) addr,
358 decode_signedness(insn));
359 break;
361 case store:
362 err = do_int_store(((insn>>25)&0x1f), size,
363 (unsigned long *) addr, regs);
364 break;
366 case both:
368 * This was supported in 2.4. However, we question
369 * the value of SWAP instruction across word boundaries.
371 printk("Unaligned SWAP unsupported.\n");
372 err = -EFAULT;
373 break;
375 default:
376 unaligned_panic("Impossible user unaligned trap.");
377 goto out;
379 if (err)
380 goto kill_user;
381 else
382 advance(regs);
383 goto out;
386 kill_user:
387 user_mna_trap_fault(regs, insn);
388 out:
389 unlock_kernel();