blob: bb2d685778555e9fb9f65a0fcbd69d23b9d18112 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: unaligned.c,v 1.24 2002/02/09 19:49:31 davem Exp $
2 * unaligned.c: Unaligned load/store trap handling with special
3 * cases for the kernel to do them more quickly.
4 *
5 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <asm/asi.h>
15#include <asm/ptrace.h>
16#include <asm/pstate.h>
17#include <asm/processor.h>
18#include <asm/system.h>
19#include <asm/uaccess.h>
20#include <linux/smp.h>
21#include <linux/smp_lock.h>
22#include <linux/bitops.h>
23#include <asm/fpumacro.h>
24
25/* #define DEBUG_MNA */
26
27enum direction {
28 load, /* ld, ldd, ldh, ldsh */
29 store, /* st, std, sth, stsh */
30 both, /* Swap, ldstub, cas, ... */
31 fpld,
32 fpst,
33 invalid,
34};
35
36#ifdef DEBUG_MNA
37static char *dirstrings[] = {
38 "load", "store", "both", "fpload", "fpstore", "invalid"
39};
40#endif
41
42static inline enum direction decode_direction(unsigned int insn)
43{
44 unsigned long tmp = (insn >> 21) & 1;
45
46 if (!tmp)
47 return load;
48 else {
49 switch ((insn>>19)&0xf) {
50 case 15: /* swap* */
51 return both;
52 default:
53 return store;
54 }
55 }
56}
57
58/* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
59static inline int decode_access_size(unsigned int insn)
60{
61 unsigned int tmp;
62
63 tmp = ((insn >> 19) & 0xf);
64 if (tmp == 11 || tmp == 14) /* ldx/stx */
65 return 8;
66 tmp &= 3;
67 if (!tmp)
68 return 4;
69 else if (tmp == 3)
70 return 16; /* ldd/std - Although it is actually 8 */
71 else if (tmp == 2)
72 return 2;
73 else {
74 printk("Impossible unaligned trap. insn=%08x\n", insn);
75 die_if_kernel("Byte sized unaligned access?!?!", current_thread_info()->kregs);
76
77 /* GCC should never warn that control reaches the end
78 * of this function without returning a value because
79 * die_if_kernel() is marked with attribute 'noreturn'.
80 * Alas, some versions do...
81 */
82
83 return 0;
84 }
85}
86
87static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
88{
89 if (insn & 0x800000) {
90 if (insn & 0x2000)
91 return (unsigned char)(regs->tstate >> 24); /* %asi */
92 else
93 return (unsigned char)(insn >> 5); /* imm_asi */
94 } else
95 return ASI_P;
96}
97
98/* 0x400000 = signed, 0 = unsigned */
99static inline int decode_signedness(unsigned int insn)
100{
101 return (insn & 0x400000);
102}
103
104static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
105 unsigned int rd, int from_kernel)
106{
107 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
108 if (from_kernel != 0)
109 __asm__ __volatile__("flushw");
110 else
111 flushw_user();
112 }
113}
114
115static inline long sign_extend_imm13(long imm)
116{
117 return imm << 51 >> 51;
118}
119
120static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
121{
122 unsigned long value;
123
124 if (reg < 16)
125 return (!reg ? 0 : regs->u_regs[reg]);
126 if (regs->tstate & TSTATE_PRIV) {
127 struct reg_window *win;
128 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
129 value = win->locals[reg - 16];
130 } else if (test_thread_flag(TIF_32BIT)) {
131 struct reg_window32 __user *win32;
132 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
133 get_user(value, &win32->locals[reg - 16]);
134 } else {
135 struct reg_window __user *win;
136 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
137 get_user(value, &win->locals[reg - 16]);
138 }
139 return value;
140}
141
142static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
143{
144 if (reg < 16)
145 return &regs->u_regs[reg];
146 if (regs->tstate & TSTATE_PRIV) {
147 struct reg_window *win;
148 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
149 return &win->locals[reg - 16];
150 } else if (test_thread_flag(TIF_32BIT)) {
151 struct reg_window32 *win32;
152 win32 = (struct reg_window32 *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
153 return (unsigned long *)&win32->locals[reg - 16];
154 } else {
155 struct reg_window *win;
156 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
157 return &win->locals[reg - 16];
158 }
159}
160
161unsigned long compute_effective_address(struct pt_regs *regs,
162 unsigned int insn, unsigned int rd)
163{
164 unsigned int rs1 = (insn >> 14) & 0x1f;
165 unsigned int rs2 = insn & 0x1f;
166 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
167
168 if (insn & 0x2000) {
169 maybe_flush_windows(rs1, 0, rd, from_kernel);
170 return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
171 } else {
172 maybe_flush_windows(rs1, rs2, rd, from_kernel);
173 return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
174 }
175}
176
177/* This is just to make gcc think die_if_kernel does return... */
178static void __attribute_used__ unaligned_panic(char *str, struct pt_regs *regs)
179{
180 die_if_kernel(str, regs);
181}
182
David S. Miller5fd29752005-09-28 20:41:45 -0700183extern int do_int_load(unsigned long *dest_reg, int size,
184 unsigned long *saddr, int is_signed, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David S. Miller5fd29752005-09-28 20:41:45 -0700186extern int __do_int_store(unsigned long *dst_addr, int size,
187 unsigned long src_val, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
David S. Miller5fd29752005-09-28 20:41:45 -0700189static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
190 struct pt_regs *regs, int asi, int orig_asi)
David S. Millera3f99852005-08-19 15:55:33 -0700191{
192 unsigned long zero = 0;
David S. Millerff171d82005-09-19 19:56:06 -0700193 unsigned long *src_val_p = &zero;
194 unsigned long src_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
David S. Millera3f99852005-08-19 15:55:33 -0700196 if (size == 16) {
197 size = 8;
198 zero = (((long)(reg_num ?
199 (unsigned)fetch_reg(reg_num, regs) : 0)) << 32) |
200 (unsigned)fetch_reg(reg_num + 1, regs);
201 } else if (reg_num) {
David S. Millerff171d82005-09-19 19:56:06 -0700202 src_val_p = fetch_reg_addr(reg_num, regs);
203 }
204 src_val = *src_val_p;
205 if (unlikely(asi != orig_asi)) {
206 switch (size) {
207 case 2:
208 src_val = swab16(src_val);
209 break;
210 case 4:
211 src_val = swab32(src_val);
212 break;
213 case 8:
214 src_val = swab64(src_val);
215 break;
216 case 16:
217 default:
218 BUG();
219 break;
220 };
David S. Millera3f99852005-08-19 15:55:33 -0700221 }
David S. Miller5fd29752005-09-28 20:41:45 -0700222 return __do_int_store(dst_addr, size, src_val, asi);
David S. Millera3f99852005-08-19 15:55:33 -0700223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static inline void advance(struct pt_regs *regs)
226{
227 regs->tpc = regs->tnpc;
228 regs->tnpc += 4;
229 if (test_thread_flag(TIF_32BIT)) {
230 regs->tpc &= 0xffffffff;
231 regs->tnpc &= 0xffffffff;
232 }
233}
234
235static inline int floating_point_load_or_store_p(unsigned int insn)
236{
237 return (insn >> 24) & 1;
238}
239
240static inline int ok_for_kernel(unsigned int insn)
241{
242 return !floating_point_load_or_store_p(insn);
243}
244
David S. Miller5fd29752005-09-28 20:41:45 -0700245static void kernel_mna_trap_fault(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
David S. Millera3f99852005-08-19 15:55:33 -0700247 struct pt_regs *regs = current_thread_info()->kern_una_regs;
248 unsigned int insn = current_thread_info()->kern_una_insn;
David S. Miller8cf14af2005-09-28 20:21:11 -0700249 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David S. Miller8cf14af2005-09-28 20:21:11 -0700251 entry = search_exception_tables(regs->tpc);
252 if (!entry) {
David S. Millera3f99852005-08-19 15:55:33 -0700253 unsigned long address;
254
255 address = compute_effective_address(regs, insn,
256 ((insn >> 25) & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (address < PAGE_SIZE) {
David S. Millera3f99852005-08-19 15:55:33 -0700258 printk(KERN_ALERT "Unable to handle kernel NULL "
259 "pointer dereference in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else
David S. Millera3f99852005-08-19 15:55:33 -0700261 printk(KERN_ALERT "Unable to handle kernel paging "
262 "request in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk(KERN_ALERT " at virtual address %016lx\n",address);
David S. Millera3f99852005-08-19 15:55:33 -0700264 printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 (current->mm ? CTX_HWBITS(current->mm->context) :
266 CTX_HWBITS(current->active_mm->context)));
David S. Millera3f99852005-08-19 15:55:33 -0700267 printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 (current->mm ? (unsigned long) current->mm->pgd :
269 (unsigned long) current->active_mm->pgd));
270 die_if_kernel("Oops", regs);
271 /* Not reached */
272 }
David S. Miller8cf14af2005-09-28 20:21:11 -0700273 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 regs->tstate &= ~TSTATE_ASI;
277 regs->tstate |= (ASI_AIUS << 24UL);
278}
279
David S. Millered6b0b42006-02-09 20:20:34 -0800280asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
David S. Miller27cc64c2006-06-21 22:31:08 -0700282 static unsigned long count, last_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 enum direction dir = decode_direction(insn);
284 int size = decode_access_size(insn);
285
David S. Millera3f99852005-08-19 15:55:33 -0700286 current_thread_info()->kern_una_regs = regs;
287 current_thread_info()->kern_una_insn = insn;
288
David S. Miller27cc64c2006-06-21 22:31:08 -0700289 if (jiffies - last_time > 5 * HZ)
290 count = 0;
291 if (count < 5) {
292 last_time = jiffies;
293 count++;
294 printk("Kernel unaligned access at TPC[%lx]\n", regs->tpc);
295 }
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (!ok_for_kernel(insn) || dir == both) {
David S. Millera3f99852005-08-19 15:55:33 -0700298 printk("Unsupported unaligned load/store trap for kernel "
299 "at <%016lx>.\n", regs->tpc);
300 unaligned_panic("Kernel does fpu/atomic "
301 "unaligned load/store.", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David S. Millera3f99852005-08-19 15:55:33 -0700303 kernel_mna_trap_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else {
David S. Miller705747a2005-09-28 16:48:40 -0700305 unsigned long addr, *reg_addr;
David S. Miller5fd29752005-09-28 20:41:45 -0700306 int orig_asi, asi, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David S. Millera3f99852005-08-19 15:55:33 -0700308 addr = compute_effective_address(regs, insn,
309 ((insn >> 25) & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#ifdef DEBUG_MNA
David S. Millera3f99852005-08-19 15:55:33 -0700311 printk("KMNA: pc=%016lx [dir=%s addr=%016lx size=%d] "
312 "retpc[%016lx]\n",
313 regs->tpc, dirstrings[dir], addr, size,
314 regs->u_regs[UREG_RETPC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
David S. Millerff171d82005-09-19 19:56:06 -0700316 orig_asi = asi = decode_asi(insn, regs);
317 switch (asi) {
318 case ASI_NL:
319 case ASI_AIUPL:
320 case ASI_AIUSL:
321 case ASI_PL:
322 case ASI_SL:
323 case ASI_PNFL:
324 case ASI_SNFL:
325 asi &= ~0x08;
326 break;
327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 switch (dir) {
329 case load:
David S. Miller705747a2005-09-28 16:48:40 -0700330 reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
David S. Miller5fd29752005-09-28 20:41:45 -0700331 err = do_int_load(reg_addr, size,
332 (unsigned long *) addr,
333 decode_signedness(insn), asi);
334 if (likely(!err) && unlikely(asi != orig_asi)) {
David S. Miller705747a2005-09-28 16:48:40 -0700335 unsigned long val_in = *reg_addr;
David S. Millerff171d82005-09-19 19:56:06 -0700336 switch (size) {
337 case 2:
338 val_in = swab16(val_in);
339 break;
340 case 4:
341 val_in = swab32(val_in);
342 break;
343 case 8:
344 val_in = swab64(val_in);
345 break;
346 case 16:
347 default:
348 BUG();
349 break;
350 };
David S. Miller705747a2005-09-28 16:48:40 -0700351 *reg_addr = val_in;
David S. Millerff171d82005-09-19 19:56:06 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354
355 case store:
David S. Miller5fd29752005-09-28 20:41:45 -0700356 err = do_int_store(((insn>>25)&0x1f), size,
357 (unsigned long *) addr, regs,
358 asi, orig_asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
David S. Millera3f99852005-08-19 15:55:33 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 default:
362 panic("Impossible kernel unaligned trap.");
363 /* Not reached... */
364 }
David S. Miller5fd29752005-09-28 20:41:45 -0700365 if (unlikely(err))
366 kernel_mna_trap_fault();
367 else
368 advance(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370}
371
372static char popc_helper[] = {
3730, 1, 1, 2, 1, 2, 2, 3,
3741, 2, 2, 3, 2, 3, 3, 4,
375};
376
377int handle_popc(u32 insn, struct pt_regs *regs)
378{
379 u64 value;
380 int ret, i, rd = ((insn >> 25) & 0x1f);
381 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
382
383 if (insn & 0x2000) {
384 maybe_flush_windows(0, 0, rd, from_kernel);
385 value = sign_extend_imm13(insn);
386 } else {
387 maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
388 value = fetch_reg(insn & 0x1f, regs);
389 }
390 for (ret = 0, i = 0; i < 16; i++) {
391 ret += popc_helper[value & 0xf];
392 value >>= 4;
393 }
394 if (rd < 16) {
395 if (rd)
396 regs->u_regs[rd] = ret;
397 } else {
398 if (test_thread_flag(TIF_32BIT)) {
399 struct reg_window32 __user *win32;
400 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
401 put_user(ret, &win32->locals[rd - 16]);
402 } else {
403 struct reg_window __user *win;
404 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
405 put_user(ret, &win->locals[rd - 16]);
406 }
407 }
408 advance(regs);
409 return 1;
410}
411
412extern void do_fpother(struct pt_regs *regs);
413extern void do_privact(struct pt_regs *regs);
David S. Miller6c52a962005-08-29 12:45:11 -0700414extern void spitfire_data_access_exception(struct pt_regs *regs,
415 unsigned long sfsr,
416 unsigned long sfar);
David S. Millered6b0b42006-02-09 20:20:34 -0800417extern void sun4v_data_access_exception(struct pt_regs *regs,
418 unsigned long addr,
419 unsigned long type_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421int handle_ldf_stq(u32 insn, struct pt_regs *regs)
422{
423 unsigned long addr = compute_effective_address(regs, insn, 0);
424 int freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
425 struct fpustate *f = FPUSTATE;
426 int asi = decode_asi(insn, regs);
427 int flag = (freg < 32) ? FPRS_DL : FPRS_DU;
428
429 save_and_clear_fpu();
430 current_thread_info()->xfsr[0] &= ~0x1c000;
431 if (freg & 3) {
432 current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
433 do_fpother(regs);
434 return 0;
435 }
436 if (insn & 0x200000) {
437 /* STQ */
438 u64 first = 0, second = 0;
439
440 if (current_thread_info()->fpsaved[0] & flag) {
441 first = *(u64 *)&f->regs[freg];
442 second = *(u64 *)&f->regs[freg+2];
443 }
444 if (asi < 0x80) {
445 do_privact(regs);
446 return 1;
447 }
448 switch (asi) {
449 case ASI_P:
450 case ASI_S: break;
451 case ASI_PL:
452 case ASI_SL:
453 {
454 /* Need to convert endians */
455 u64 tmp = __swab64p(&first);
456
457 first = __swab64p(&second);
458 second = tmp;
459 break;
460 }
461 default:
David S. Millered6b0b42006-02-09 20:20:34 -0800462 if (tlb_type == hypervisor)
463 sun4v_data_access_exception(regs, addr, 0);
464 else
465 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 1;
467 }
468 if (put_user (first >> 32, (u32 __user *)addr) ||
469 __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
470 __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
471 __put_user ((u32)second, (u32 __user *)(addr + 12))) {
David S. Millered6b0b42006-02-09 20:20:34 -0800472 if (tlb_type == hypervisor)
473 sun4v_data_access_exception(regs, addr, 0);
474 else
475 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 1;
477 }
478 } else {
479 /* LDF, LDDF, LDQF */
480 u32 data[4] __attribute__ ((aligned(8)));
481 int size, i;
482 int err;
483
484 if (asi < 0x80) {
485 do_privact(regs);
486 return 1;
487 } else if (asi > ASI_SNFL) {
David S. Millered6b0b42006-02-09 20:20:34 -0800488 if (tlb_type == hypervisor)
489 sun4v_data_access_exception(regs, addr, 0);
490 else
491 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 1;
493 }
494 switch (insn & 0x180000) {
495 case 0x000000: size = 1; break;
496 case 0x100000: size = 4; break;
497 default: size = 2; break;
498 }
499 for (i = 0; i < size; i++)
500 data[i] = 0;
501
502 err = get_user (data[0], (u32 __user *) addr);
503 if (!err) {
504 for (i = 1; i < size; i++)
505 err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
506 }
507 if (err && !(asi & 0x2 /* NF */)) {
David S. Millered6b0b42006-02-09 20:20:34 -0800508 if (tlb_type == hypervisor)
509 sun4v_data_access_exception(regs, addr, 0);
510 else
511 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 1;
513 }
514 if (asi & 0x8) /* Little */ {
515 u64 tmp;
516
517 switch (size) {
518 case 1: data[0] = le32_to_cpup(data + 0); break;
519 default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
520 break;
521 case 4: tmp = le64_to_cpup((u64 *)(data + 0));
522 *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
523 *(u64 *)(data + 2) = tmp;
524 break;
525 }
526 }
527 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
528 current_thread_info()->fpsaved[0] = FPRS_FEF;
529 current_thread_info()->gsr[0] = 0;
530 }
531 if (!(current_thread_info()->fpsaved[0] & flag)) {
532 if (freg < 32)
533 memset(f->regs, 0, 32*sizeof(u32));
534 else
535 memset(f->regs+32, 0, 32*sizeof(u32));
536 }
537 memcpy(f->regs + freg, data, size * 4);
538 current_thread_info()->fpsaved[0] |= flag;
539 }
540 advance(regs);
541 return 1;
542}
543
544void handle_ld_nf(u32 insn, struct pt_regs *regs)
545{
546 int rd = ((insn >> 25) & 0x1f);
547 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
548 unsigned long *reg;
549
550 maybe_flush_windows(0, 0, rd, from_kernel);
551 reg = fetch_reg_addr(rd, regs);
552 if (from_kernel || rd < 16) {
553 reg[0] = 0;
554 if ((insn & 0x780000) == 0x180000)
555 reg[1] = 0;
556 } else if (test_thread_flag(TIF_32BIT)) {
557 put_user(0, (int __user *) reg);
558 if ((insn & 0x780000) == 0x180000)
559 put_user(0, ((int __user *) reg) + 1);
560 } else {
561 put_user(0, (unsigned long __user *) reg);
562 if ((insn & 0x780000) == 0x180000)
563 put_user(0, (unsigned long __user *) reg + 1);
564 }
565 advance(regs);
566}
567
568void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
569{
570 unsigned long pc = regs->tpc;
571 unsigned long tstate = regs->tstate;
572 u32 insn;
573 u32 first, second;
574 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800575 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int flag;
577 struct fpustate *f = FPUSTATE;
578
579 if (tstate & TSTATE_PRIV)
580 die_if_kernel("lddfmna from kernel", regs);
581 if (test_thread_flag(TIF_32BIT))
582 pc = (u32)pc;
583 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800584 int asi = decode_asi(insn, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if ((asi > ASI_SNFL) ||
586 (asi < ASI_P))
587 goto daex;
588 if (get_user(first, (u32 __user *)sfar) ||
589 get_user(second, (u32 __user *)(sfar + 4))) {
590 if (asi & 0x2) /* NF */ {
591 first = 0; second = 0;
592 } else
593 goto daex;
594 }
595 save_and_clear_fpu();
596 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
597 value = (((u64)first) << 32) | second;
598 if (asi & 0x8) /* Little */
599 value = __swab64p(&value);
600 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
601 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
602 current_thread_info()->fpsaved[0] = FPRS_FEF;
603 current_thread_info()->gsr[0] = 0;
604 }
605 if (!(current_thread_info()->fpsaved[0] & flag)) {
606 if (freg < 32)
607 memset(f->regs, 0, 32*sizeof(u32));
608 else
609 memset(f->regs+32, 0, 32*sizeof(u32));
610 }
611 *(u64 *)(f->regs + freg) = value;
612 current_thread_info()->fpsaved[0] |= flag;
613 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800614daex:
615 if (tlb_type == hypervisor)
616 sun4v_data_access_exception(regs, sfar, sfsr);
617 else
618 spitfire_data_access_exception(regs, sfsr, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return;
620 }
621 advance(regs);
622 return;
623}
624
625void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
626{
627 unsigned long pc = regs->tpc;
628 unsigned long tstate = regs->tstate;
629 u32 insn;
630 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800631 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int flag;
633 struct fpustate *f = FPUSTATE;
634
635 if (tstate & TSTATE_PRIV)
636 die_if_kernel("stdfmna from kernel", regs);
637 if (test_thread_flag(TIF_32BIT))
638 pc = (u32)pc;
639 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800640 int asi = decode_asi(insn, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 value = 0;
643 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
644 if ((asi > ASI_SNFL) ||
645 (asi < ASI_P))
646 goto daex;
647 save_and_clear_fpu();
648 if (current_thread_info()->fpsaved[0] & flag)
649 value = *(u64 *)&f->regs[freg];
650 switch (asi) {
651 case ASI_P:
652 case ASI_S: break;
653 case ASI_PL:
654 case ASI_SL:
655 value = __swab64p(&value); break;
656 default: goto daex;
657 }
658 if (put_user (value >> 32, (u32 __user *) sfar) ||
659 __put_user ((u32)value, (u32 __user *)(sfar + 4)))
660 goto daex;
661 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800662daex:
663 if (tlb_type == hypervisor)
664 sun4v_data_access_exception(regs, sfar, sfsr);
665 else
666 spitfire_data_access_exception(regs, sfsr, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668 }
669 advance(regs);
670 return;
671}