blob: 35ab8b60d25609220c1b1d10c1495642ea3700b2 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * unaligned.c: Unaligned load/store trap handling with special
3 * cases for the kernel to do them more quickly.
4 *
David S. Miller4fe3ebe2008-07-17 22:11:32 -07005 * Copyright (C) 1996,2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9
S.Çağlar Onurcbc9fc52008-02-17 23:24:10 -080010#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/module.h>
15#include <asm/asi.h>
16#include <asm/ptrace.h>
17#include <asm/pstate.h>
18#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/bitops.h>
David S. Miller121dd5f2009-12-11 01:07:53 -080022#include <linux/perf_event.h>
Akinobu Mitac7ec2b52010-02-28 03:31:29 -080023#include <linux/ratelimit.h>
Kirill Tkhai812cb832013-09-14 16:02:11 +040024#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/fpumacro.h>
David Howellsd550bbd2012-03-28 18:30:03 +010026#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Kirill Tkhai812cb832013-09-14 16:02:11 +040028#include "entry.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030enum direction {
31 load, /* ld, ldd, ldh, ldsh */
32 store, /* st, std, sth, stsh */
33 both, /* Swap, ldstub, cas, ... */
34 fpld,
35 fpst,
36 invalid,
37};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline enum direction decode_direction(unsigned int insn)
40{
41 unsigned long tmp = (insn >> 21) & 1;
42
43 if (!tmp)
44 return load;
45 else {
46 switch ((insn>>19)&0xf) {
47 case 15: /* swap* */
48 return both;
49 default:
50 return store;
51 }
52 }
53}
54
55/* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
David S. Millerbaa06772010-04-19 13:46:48 -070056static inline int decode_access_size(struct pt_regs *regs, unsigned int insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 unsigned int tmp;
59
60 tmp = ((insn >> 19) & 0xf);
61 if (tmp == 11 || tmp == 14) /* ldx/stx */
62 return 8;
63 tmp &= 3;
64 if (!tmp)
65 return 4;
66 else if (tmp == 3)
67 return 16; /* ldd/std - Although it is actually 8 */
68 else if (tmp == 2)
69 return 2;
70 else {
71 printk("Impossible unaligned trap. insn=%08x\n", insn);
David S. Millerbaa06772010-04-19 13:46:48 -070072 die_if_kernel("Byte sized unaligned access?!?!", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* GCC should never warn that control reaches the end
75 * of this function without returning a value because
76 * die_if_kernel() is marked with attribute 'noreturn'.
77 * Alas, some versions do...
78 */
79
80 return 0;
81 }
82}
83
84static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
85{
86 if (insn & 0x800000) {
87 if (insn & 0x2000)
88 return (unsigned char)(regs->tstate >> 24); /* %asi */
89 else
90 return (unsigned char)(insn >> 5); /* imm_asi */
91 } else
92 return ASI_P;
93}
94
95/* 0x400000 = signed, 0 = unsigned */
96static inline int decode_signedness(unsigned int insn)
97{
98 return (insn & 0x400000);
99}
100
101static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
102 unsigned int rd, int from_kernel)
103{
104 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
105 if (from_kernel != 0)
106 __asm__ __volatile__("flushw");
107 else
108 flushw_user();
109 }
110}
111
112static inline long sign_extend_imm13(long imm)
113{
114 return imm << 51 >> 51;
115}
116
117static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
118{
David S. Miller517ffce2012-10-26 15:18:37 -0700119 unsigned long value, fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (reg < 16)
122 return (!reg ? 0 : regs->u_regs[reg]);
David S. Miller517ffce2012-10-26 15:18:37 -0700123
124 fp = regs->u_regs[UREG_FP];
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (regs->tstate & TSTATE_PRIV) {
127 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700128 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 value = win->locals[reg - 16];
David S. Miller517ffce2012-10-26 15:18:37 -0700130 } else if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct reg_window32 __user *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700132 win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 get_user(value, &win32->locals[reg - 16]);
134 } else {
135 struct reg_window __user *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700136 win = (struct reg_window __user *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 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{
David S. Miller517ffce2012-10-26 15:18:37 -0700144 unsigned long fp;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (reg < 16)
147 return &regs->u_regs[reg];
David S. Miller517ffce2012-10-26 15:18:37 -0700148
149 fp = regs->u_regs[UREG_FP];
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (regs->tstate & TSTATE_PRIV) {
152 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700153 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return &win->locals[reg - 16];
David S. Miller517ffce2012-10-26 15:18:37 -0700155 } else if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct reg_window32 *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700157 win32 = (struct reg_window32 *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return (unsigned long *)&win32->locals[reg - 16];
159 } else {
160 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700161 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return &win->locals[reg - 16];
163 }
164}
165
166unsigned long compute_effective_address(struct pt_regs *regs,
167 unsigned int insn, unsigned int rd)
168{
David S. Millerd037d162014-04-28 23:50:08 -0700169 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int rs1 = (insn >> 14) & 0x1f;
171 unsigned int rs2 = insn & 0x1f;
David S. Millerd037d162014-04-28 23:50:08 -0700172 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (insn & 0x2000) {
175 maybe_flush_windows(rs1, 0, rd, from_kernel);
David S. Millerd037d162014-04-28 23:50:08 -0700176 addr = (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 } else {
178 maybe_flush_windows(rs1, rs2, rd, from_kernel);
David S. Millerd037d162014-04-28 23:50:08 -0700179 addr = (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
David S. Millerd037d162014-04-28 23:50:08 -0700181
182 if (!from_kernel && test_thread_flag(TIF_32BIT))
183 addr &= 0xffffffff;
184
185 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/* This is just to make gcc think die_if_kernel does return... */
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100189static void __used unaligned_panic(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 die_if_kernel(str, regs);
192}
193
David S. Miller5fd29752005-09-28 20:41:45 -0700194extern int do_int_load(unsigned long *dest_reg, int size,
195 unsigned long *saddr, int is_signed, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
David S. Miller5fd29752005-09-28 20:41:45 -0700197extern int __do_int_store(unsigned long *dst_addr, int size,
198 unsigned long src_val, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
David S. Miller5fd29752005-09-28 20:41:45 -0700200static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
201 struct pt_regs *regs, int asi, int orig_asi)
David S. Millera3f99852005-08-19 15:55:33 -0700202{
203 unsigned long zero = 0;
David S. Millerff171d82005-09-19 19:56:06 -0700204 unsigned long *src_val_p = &zero;
205 unsigned long src_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David S. Millera3f99852005-08-19 15:55:33 -0700207 if (size == 16) {
208 size = 8;
209 zero = (((long)(reg_num ?
210 (unsigned)fetch_reg(reg_num, regs) : 0)) << 32) |
211 (unsigned)fetch_reg(reg_num + 1, regs);
212 } else if (reg_num) {
David S. Millerff171d82005-09-19 19:56:06 -0700213 src_val_p = fetch_reg_addr(reg_num, regs);
214 }
215 src_val = *src_val_p;
216 if (unlikely(asi != orig_asi)) {
217 switch (size) {
218 case 2:
219 src_val = swab16(src_val);
220 break;
221 case 4:
222 src_val = swab32(src_val);
223 break;
224 case 8:
225 src_val = swab64(src_val);
226 break;
227 case 16:
228 default:
229 BUG();
230 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000231 }
David S. Millera3f99852005-08-19 15:55:33 -0700232 }
David S. Miller5fd29752005-09-28 20:41:45 -0700233 return __do_int_store(dst_addr, size, src_val, asi);
David S. Millera3f99852005-08-19 15:55:33 -0700234}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236static inline void advance(struct pt_regs *regs)
237{
238 regs->tpc = regs->tnpc;
239 regs->tnpc += 4;
240 if (test_thread_flag(TIF_32BIT)) {
241 regs->tpc &= 0xffffffff;
242 regs->tnpc &= 0xffffffff;
243 }
244}
245
246static inline int floating_point_load_or_store_p(unsigned int insn)
247{
248 return (insn >> 24) & 1;
249}
250
251static inline int ok_for_kernel(unsigned int insn)
252{
253 return !floating_point_load_or_store_p(insn);
254}
255
David S. Millerc449c382006-11-28 20:18:05 -0800256static void kernel_mna_trap_fault(int fixup_tstate_asi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
David S. Millera3f99852005-08-19 15:55:33 -0700258 struct pt_regs *regs = current_thread_info()->kern_una_regs;
259 unsigned int insn = current_thread_info()->kern_una_insn;
David S. Miller8cf14af2005-09-28 20:21:11 -0700260 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David S. Miller8cf14af2005-09-28 20:21:11 -0700262 entry = search_exception_tables(regs->tpc);
263 if (!entry) {
David S. Millera3f99852005-08-19 15:55:33 -0700264 unsigned long address;
265
266 address = compute_effective_address(regs, insn,
267 ((insn >> 25) & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (address < PAGE_SIZE) {
David S. Millera3f99852005-08-19 15:55:33 -0700269 printk(KERN_ALERT "Unable to handle kernel NULL "
270 "pointer dereference in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else
David S. Millera3f99852005-08-19 15:55:33 -0700272 printk(KERN_ALERT "Unable to handle kernel paging "
273 "request in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 printk(KERN_ALERT " at virtual address %016lx\n",address);
David S. Millera3f99852005-08-19 15:55:33 -0700275 printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 (current->mm ? CTX_HWBITS(current->mm->context) :
277 CTX_HWBITS(current->active_mm->context)));
David S. Millera3f99852005-08-19 15:55:33 -0700278 printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 (current->mm ? (unsigned long) current->mm->pgd :
280 (unsigned long) current->active_mm->pgd));
281 die_if_kernel("Oops", regs);
282 /* Not reached */
283 }
David S. Miller8cf14af2005-09-28 20:21:11 -0700284 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
David S. Millerc449c382006-11-28 20:18:05 -0800287 if (fixup_tstate_asi) {
288 regs->tstate &= ~TSTATE_ASI;
289 regs->tstate |= (ASI_AIUS << 24UL);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
David S. Millerc449c382006-11-28 20:18:05 -0800293static void log_unaligned(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Akinobu Mitac7ec2b52010-02-28 03:31:29 -0800295 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
David S. Millera3f99852005-08-19 15:55:33 -0700296
Akinobu Mitac7ec2b52010-02-28 03:31:29 -0800297 if (__ratelimit(&ratelimit)) {
David S. Miller4fe3ebe2008-07-17 22:11:32 -0700298 printk("Kernel unaligned access at TPC[%lx] %pS\n",
299 regs->tpc, (void *) regs->tpc);
David S. Miller27cc64c2006-06-21 22:31:08 -0700300 }
David S. Millerc449c382006-11-28 20:18:05 -0800301}
302
303asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
304{
305 enum direction dir = decode_direction(insn);
David S. Millerbaa06772010-04-19 13:46:48 -0700306 int size = decode_access_size(regs, insn);
David S. Millerc449c382006-11-28 20:18:05 -0800307 int orig_asi, asi;
308
309 current_thread_info()->kern_una_regs = regs;
310 current_thread_info()->kern_una_insn = insn;
311
312 orig_asi = asi = decode_asi(insn, regs);
313
314 /* If this is a {get,put}_user() on an unaligned userspace pointer,
315 * just signal a fault and do not log the event.
316 */
317 if (asi == ASI_AIUS) {
318 kernel_mna_trap_fault(0);
319 return;
320 }
321
322 log_unaligned(regs);
David S. Miller27cc64c2006-06-21 22:31:08 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!ok_for_kernel(insn) || dir == both) {
David S. Millera3f99852005-08-19 15:55:33 -0700325 printk("Unsupported unaligned load/store trap for kernel "
326 "at <%016lx>.\n", regs->tpc);
327 unaligned_panic("Kernel does fpu/atomic "
328 "unaligned load/store.", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David S. Millerc449c382006-11-28 20:18:05 -0800330 kernel_mna_trap_fault(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else {
David S. Miller705747a2005-09-28 16:48:40 -0700332 unsigned long addr, *reg_addr;
David S. Millerc449c382006-11-28 20:18:05 -0800333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David S. Millera3f99852005-08-19 15:55:33 -0700335 addr = compute_effective_address(regs, insn,
336 ((insn >> 25) & 0x1f));
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200337 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
David S. Millerff171d82005-09-19 19:56:06 -0700338 switch (asi) {
339 case ASI_NL:
340 case ASI_AIUPL:
341 case ASI_AIUSL:
342 case ASI_PL:
343 case ASI_SL:
344 case ASI_PNFL:
345 case ASI_SNFL:
346 asi &= ~0x08;
347 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 switch (dir) {
350 case load:
David S. Miller705747a2005-09-28 16:48:40 -0700351 reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
David S. Miller5fd29752005-09-28 20:41:45 -0700352 err = do_int_load(reg_addr, size,
353 (unsigned long *) addr,
354 decode_signedness(insn), asi);
355 if (likely(!err) && unlikely(asi != orig_asi)) {
David S. Miller705747a2005-09-28 16:48:40 -0700356 unsigned long val_in = *reg_addr;
David S. Millerff171d82005-09-19 19:56:06 -0700357 switch (size) {
358 case 2:
359 val_in = swab16(val_in);
360 break;
361 case 4:
362 val_in = swab32(val_in);
363 break;
364 case 8:
365 val_in = swab64(val_in);
366 break;
367 case 16:
368 default:
369 BUG();
370 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000371 }
David S. Miller705747a2005-09-28 16:48:40 -0700372 *reg_addr = val_in;
David S. Millerff171d82005-09-19 19:56:06 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375
376 case store:
David S. Miller5fd29752005-09-28 20:41:45 -0700377 err = do_int_store(((insn>>25)&0x1f), size,
378 (unsigned long *) addr, regs,
379 asi, orig_asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
David S. Millera3f99852005-08-19 15:55:33 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 default:
383 panic("Impossible kernel unaligned trap.");
384 /* Not reached... */
385 }
David S. Miller5fd29752005-09-28 20:41:45 -0700386 if (unlikely(err))
David S. Millerc449c382006-11-28 20:18:05 -0800387 kernel_mna_trap_fault(1);
David S. Miller5fd29752005-09-28 20:41:45 -0700388 else
389 advance(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393int handle_popc(u32 insn, struct pt_regs *regs)
394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
David S. Millerd600cbe2011-08-01 19:41:12 -0700396 int ret, rd = ((insn >> 25) & 0x1f);
397 u64 value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200399 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (insn & 0x2000) {
401 maybe_flush_windows(0, 0, rd, from_kernel);
402 value = sign_extend_imm13(insn);
403 } else {
404 maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
405 value = fetch_reg(insn & 0x1f, regs);
406 }
David S. Millerd600cbe2011-08-01 19:41:12 -0700407 ret = hweight64(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (rd < 16) {
409 if (rd)
410 regs->u_regs[rd] = ret;
411 } else {
David S. Miller517ffce2012-10-26 15:18:37 -0700412 unsigned long fp = regs->u_regs[UREG_FP];
413
414 if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct reg_window32 __user *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700416 win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 put_user(ret, &win32->locals[rd - 16]);
418 } else {
419 struct reg_window __user *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700420 win = (struct reg_window __user *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 put_user(ret, &win->locals[rd - 16]);
422 }
423 }
424 advance(regs);
425 return 1;
426}
427
428extern void do_fpother(struct pt_regs *regs);
429extern void do_privact(struct pt_regs *regs);
David S. Millered6b0b42006-02-09 20:20:34 -0800430extern void sun4v_data_access_exception(struct pt_regs *regs,
431 unsigned long addr,
432 unsigned long type_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434int handle_ldf_stq(u32 insn, struct pt_regs *regs)
435{
436 unsigned long addr = compute_effective_address(regs, insn, 0);
437 int freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
438 struct fpustate *f = FPUSTATE;
439 int asi = decode_asi(insn, regs);
440 int flag = (freg < 32) ? FPRS_DL : FPRS_DU;
441
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200442 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
David S. Miller121dd5f2009-12-11 01:07:53 -0800443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 save_and_clear_fpu();
445 current_thread_info()->xfsr[0] &= ~0x1c000;
446 if (freg & 3) {
447 current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
448 do_fpother(regs);
449 return 0;
450 }
451 if (insn & 0x200000) {
452 /* STQ */
453 u64 first = 0, second = 0;
454
455 if (current_thread_info()->fpsaved[0] & flag) {
456 first = *(u64 *)&f->regs[freg];
457 second = *(u64 *)&f->regs[freg+2];
458 }
459 if (asi < 0x80) {
460 do_privact(regs);
461 return 1;
462 }
463 switch (asi) {
464 case ASI_P:
465 case ASI_S: break;
466 case ASI_PL:
467 case ASI_SL:
468 {
469 /* Need to convert endians */
470 u64 tmp = __swab64p(&first);
471
472 first = __swab64p(&second);
473 second = tmp;
474 break;
475 }
476 default:
David S. Millered6b0b42006-02-09 20:20:34 -0800477 if (tlb_type == hypervisor)
478 sun4v_data_access_exception(regs, addr, 0);
479 else
480 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 1;
482 }
483 if (put_user (first >> 32, (u32 __user *)addr) ||
484 __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
485 __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
486 __put_user ((u32)second, (u32 __user *)(addr + 12))) {
David S. Millered6b0b42006-02-09 20:20:34 -0800487 if (tlb_type == hypervisor)
488 sun4v_data_access_exception(regs, addr, 0);
489 else
490 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 1;
492 }
493 } else {
494 /* LDF, LDDF, LDQF */
495 u32 data[4] __attribute__ ((aligned(8)));
496 int size, i;
497 int err;
498
499 if (asi < 0x80) {
500 do_privact(regs);
501 return 1;
502 } else if (asi > ASI_SNFL) {
David S. Millered6b0b42006-02-09 20:20:34 -0800503 if (tlb_type == hypervisor)
504 sun4v_data_access_exception(regs, addr, 0);
505 else
506 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 1;
508 }
509 switch (insn & 0x180000) {
510 case 0x000000: size = 1; break;
511 case 0x100000: size = 4; break;
512 default: size = 2; break;
513 }
514 for (i = 0; i < size; i++)
515 data[i] = 0;
516
517 err = get_user (data[0], (u32 __user *) addr);
518 if (!err) {
519 for (i = 1; i < size; i++)
520 err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
521 }
522 if (err && !(asi & 0x2 /* NF */)) {
David S. Millered6b0b42006-02-09 20:20:34 -0800523 if (tlb_type == hypervisor)
524 sun4v_data_access_exception(regs, addr, 0);
525 else
526 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 1;
528 }
529 if (asi & 0x8) /* Little */ {
530 u64 tmp;
531
532 switch (size) {
533 case 1: data[0] = le32_to_cpup(data + 0); break;
534 default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
535 break;
536 case 4: tmp = le64_to_cpup((u64 *)(data + 0));
537 *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
538 *(u64 *)(data + 2) = tmp;
539 break;
540 }
541 }
542 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
543 current_thread_info()->fpsaved[0] = FPRS_FEF;
544 current_thread_info()->gsr[0] = 0;
545 }
546 if (!(current_thread_info()->fpsaved[0] & flag)) {
547 if (freg < 32)
548 memset(f->regs, 0, 32*sizeof(u32));
549 else
550 memset(f->regs+32, 0, 32*sizeof(u32));
551 }
552 memcpy(f->regs + freg, data, size * 4);
553 current_thread_info()->fpsaved[0] |= flag;
554 }
555 advance(regs);
556 return 1;
557}
558
559void handle_ld_nf(u32 insn, struct pt_regs *regs)
560{
561 int rd = ((insn >> 25) & 0x1f);
562 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
563 unsigned long *reg;
564
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200565 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
David S. Miller121dd5f2009-12-11 01:07:53 -0800566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 maybe_flush_windows(0, 0, rd, from_kernel);
568 reg = fetch_reg_addr(rd, regs);
569 if (from_kernel || rd < 16) {
570 reg[0] = 0;
571 if ((insn & 0x780000) == 0x180000)
572 reg[1] = 0;
David S. Miller517ffce2012-10-26 15:18:37 -0700573 } else if (!test_thread_64bit_stack(regs->u_regs[UREG_FP])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 put_user(0, (int __user *) reg);
575 if ((insn & 0x780000) == 0x180000)
576 put_user(0, ((int __user *) reg) + 1);
577 } else {
578 put_user(0, (unsigned long __user *) reg);
579 if ((insn & 0x780000) == 0x180000)
580 put_user(0, (unsigned long __user *) reg + 1);
581 }
582 advance(regs);
583}
584
585void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
586{
Kirill Tkhai812cb832013-09-14 16:02:11 +0400587 enum ctx_state prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long pc = regs->tpc;
589 unsigned long tstate = regs->tstate;
590 u32 insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800592 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int flag;
594 struct fpustate *f = FPUSTATE;
595
596 if (tstate & TSTATE_PRIV)
597 die_if_kernel("lddfmna from kernel", regs);
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200598 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (test_thread_flag(TIF_32BIT))
600 pc = (u32)pc;
601 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800602 int asi = decode_asi(insn, regs);
David S. Millerb41418f2009-01-08 16:52:36 -0800603 u32 first, second;
David S. Miller18b8e082009-01-07 17:15:57 -0800604 int err;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if ((asi > ASI_SNFL) ||
607 (asi < ASI_P))
608 goto daex;
David S. Millerb41418f2009-01-08 16:52:36 -0800609 first = second = 0;
David S. Miller18b8e082009-01-07 17:15:57 -0800610 err = get_user(first, (u32 __user *)sfar);
611 if (!err)
612 err = get_user(second, (u32 __user *)(sfar + 4));
613 if (err) {
David S. Millerb41418f2009-01-08 16:52:36 -0800614 if (!(asi & 0x2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto daex;
David S. Millerb41418f2009-01-08 16:52:36 -0800616 first = second = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 save_and_clear_fpu();
619 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
620 value = (((u64)first) << 32) | second;
621 if (asi & 0x8) /* Little */
622 value = __swab64p(&value);
623 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
624 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
625 current_thread_info()->fpsaved[0] = FPRS_FEF;
626 current_thread_info()->gsr[0] = 0;
627 }
628 if (!(current_thread_info()->fpsaved[0] & flag)) {
629 if (freg < 32)
630 memset(f->regs, 0, 32*sizeof(u32));
631 else
632 memset(f->regs+32, 0, 32*sizeof(u32));
633 }
634 *(u64 *)(f->regs + freg) = value;
635 current_thread_info()->fpsaved[0] |= flag;
636 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800637daex:
638 if (tlb_type == hypervisor)
639 sun4v_data_access_exception(regs, sfar, sfsr);
640 else
641 spitfire_data_access_exception(regs, sfsr, sfar);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 advance(regs);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400645out:
646 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
650{
Kirill Tkhai812cb832013-09-14 16:02:11 +0400651 enum ctx_state prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned long pc = regs->tpc;
653 unsigned long tstate = regs->tstate;
654 u32 insn;
655 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800656 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int flag;
658 struct fpustate *f = FPUSTATE;
659
660 if (tstate & TSTATE_PRIV)
661 die_if_kernel("stdfmna from kernel", regs);
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200662 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (test_thread_flag(TIF_32BIT))
664 pc = (u32)pc;
665 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800666 int asi = decode_asi(insn, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 value = 0;
669 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
670 if ((asi > ASI_SNFL) ||
671 (asi < ASI_P))
672 goto daex;
673 save_and_clear_fpu();
674 if (current_thread_info()->fpsaved[0] & flag)
675 value = *(u64 *)&f->regs[freg];
676 switch (asi) {
677 case ASI_P:
678 case ASI_S: break;
679 case ASI_PL:
680 case ASI_SL:
681 value = __swab64p(&value); break;
682 default: goto daex;
683 }
684 if (put_user (value >> 32, (u32 __user *) sfar) ||
685 __put_user ((u32)value, (u32 __user *)(sfar + 4)))
686 goto daex;
687 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800688daex:
689 if (tlb_type == hypervisor)
690 sun4v_data_access_exception(regs, sfar, sfsr);
691 else
692 spitfire_data_access_exception(regs, sfsr, sfar);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 advance(regs);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400696out:
697 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}