blob: 52c00d90d4b46556a981db8b727d5255ebfe850e [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>
Paul Gortmakercdd4f4c2016-09-19 17:36:29 -040014#include <linux/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Sam Ravnborg8df52622014-05-16 23:25:54 +020027#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Kirill Tkhai812cb832013-09-14 16:02:11 +040029#include "entry.h"
Sam Ravnborg8df52622014-05-16 23:25:54 +020030#include "kernel.h"
Kirill Tkhai812cb832013-09-14 16:02:11 +040031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032enum direction {
33 load, /* ld, ldd, ldh, ldsh */
34 store, /* st, std, sth, stsh */
35 both, /* Swap, ldstub, cas, ... */
36 fpld,
37 fpst,
38 invalid,
39};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static inline enum direction decode_direction(unsigned int insn)
42{
43 unsigned long tmp = (insn >> 21) & 1;
44
45 if (!tmp)
46 return load;
47 else {
48 switch ((insn>>19)&0xf) {
49 case 15: /* swap* */
50 return both;
51 default:
52 return store;
53 }
54 }
55}
56
57/* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
David S. Millerbaa06772010-04-19 13:46:48 -070058static inline int decode_access_size(struct pt_regs *regs, unsigned int insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 unsigned int tmp;
61
62 tmp = ((insn >> 19) & 0xf);
63 if (tmp == 11 || tmp == 14) /* ldx/stx */
64 return 8;
65 tmp &= 3;
66 if (!tmp)
67 return 4;
68 else if (tmp == 3)
69 return 16; /* ldd/std - Although it is actually 8 */
70 else if (tmp == 2)
71 return 2;
72 else {
73 printk("Impossible unaligned trap. insn=%08x\n", insn);
David S. Millerbaa06772010-04-19 13:46:48 -070074 die_if_kernel("Byte sized unaligned access?!?!", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* GCC should never warn that control reaches the end
77 * of this function without returning a value because
78 * die_if_kernel() is marked with attribute 'noreturn'.
79 * Alas, some versions do...
80 */
81
82 return 0;
83 }
84}
85
86static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
87{
88 if (insn & 0x800000) {
89 if (insn & 0x2000)
90 return (unsigned char)(regs->tstate >> 24); /* %asi */
91 else
92 return (unsigned char)(insn >> 5); /* imm_asi */
93 } else
94 return ASI_P;
95}
96
97/* 0x400000 = signed, 0 = unsigned */
98static inline int decode_signedness(unsigned int insn)
99{
100 return (insn & 0x400000);
101}
102
103static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
104 unsigned int rd, int from_kernel)
105{
106 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
107 if (from_kernel != 0)
108 __asm__ __volatile__("flushw");
109 else
110 flushw_user();
111 }
112}
113
114static inline long sign_extend_imm13(long imm)
115{
116 return imm << 51 >> 51;
117}
118
119static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
120{
David S. Miller517ffce2012-10-26 15:18:37 -0700121 unsigned long value, fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (reg < 16)
124 return (!reg ? 0 : regs->u_regs[reg]);
David S. Miller517ffce2012-10-26 15:18:37 -0700125
126 fp = regs->u_regs[UREG_FP];
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (regs->tstate & TSTATE_PRIV) {
129 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700130 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 value = win->locals[reg - 16];
David S. Miller517ffce2012-10-26 15:18:37 -0700132 } else if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct reg_window32 __user *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700134 win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 get_user(value, &win32->locals[reg - 16]);
136 } else {
137 struct reg_window __user *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700138 win = (struct reg_window __user *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 get_user(value, &win->locals[reg - 16]);
140 }
141 return value;
142}
143
144static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
145{
David S. Miller517ffce2012-10-26 15:18:37 -0700146 unsigned long fp;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (reg < 16)
149 return &regs->u_regs[reg];
David S. Miller517ffce2012-10-26 15:18:37 -0700150
151 fp = regs->u_regs[UREG_FP];
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (regs->tstate & TSTATE_PRIV) {
154 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700155 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return &win->locals[reg - 16];
David S. Miller517ffce2012-10-26 15:18:37 -0700157 } else if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct reg_window32 *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700159 win32 = (struct reg_window32 *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return (unsigned long *)&win32->locals[reg - 16];
161 } else {
162 struct reg_window *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700163 win = (struct reg_window *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return &win->locals[reg - 16];
165 }
166}
167
168unsigned long compute_effective_address(struct pt_regs *regs,
169 unsigned int insn, unsigned int rd)
170{
David S. Millerd037d1632014-04-28 23:50:08 -0700171 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int rs1 = (insn >> 14) & 0x1f;
173 unsigned int rs2 = insn & 0x1f;
David S. Millerd037d1632014-04-28 23:50:08 -0700174 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (insn & 0x2000) {
177 maybe_flush_windows(rs1, 0, rd, from_kernel);
David S. Millerd037d1632014-04-28 23:50:08 -0700178 addr = (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 } else {
180 maybe_flush_windows(rs1, rs2, rd, from_kernel);
David S. Millerd037d1632014-04-28 23:50:08 -0700181 addr = (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
David S. Millerd037d1632014-04-28 23:50:08 -0700183
184 if (!from_kernel && test_thread_flag(TIF_32BIT))
185 addr &= 0xffffffff;
186
187 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/* This is just to make gcc think die_if_kernel does return... */
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100191static void __used unaligned_panic(char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 die_if_kernel(str, regs);
194}
195
David S. Miller5fd297522005-09-28 20:41:45 -0700196extern int do_int_load(unsigned long *dest_reg, int size,
197 unsigned long *saddr, int is_signed, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
David S. Miller5fd297522005-09-28 20:41:45 -0700199extern int __do_int_store(unsigned long *dst_addr, int size,
200 unsigned long src_val, int asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
David S. Miller5fd297522005-09-28 20:41:45 -0700202static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
203 struct pt_regs *regs, int asi, int orig_asi)
David S. Millera3f99852005-08-19 15:55:33 -0700204{
205 unsigned long zero = 0;
David S. Millerff171d82005-09-19 19:56:06 -0700206 unsigned long *src_val_p = &zero;
207 unsigned long src_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David S. Millera3f99852005-08-19 15:55:33 -0700209 if (size == 16) {
210 size = 8;
211 zero = (((long)(reg_num ?
Joe Perches9ef595d2016-03-10 15:21:43 -0800212 (unsigned int)fetch_reg(reg_num, regs) : 0)) << 32) |
213 (unsigned int)fetch_reg(reg_num + 1, regs);
David S. Millera3f99852005-08-19 15:55:33 -0700214 } else if (reg_num) {
David S. Millerff171d82005-09-19 19:56:06 -0700215 src_val_p = fetch_reg_addr(reg_num, regs);
216 }
217 src_val = *src_val_p;
218 if (unlikely(asi != orig_asi)) {
219 switch (size) {
220 case 2:
221 src_val = swab16(src_val);
222 break;
223 case 4:
224 src_val = swab32(src_val);
225 break;
226 case 8:
227 src_val = swab64(src_val);
228 break;
229 case 16:
230 default:
231 BUG();
232 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000233 }
David S. Millera3f99852005-08-19 15:55:33 -0700234 }
David S. Miller5fd297522005-09-28 20:41:45 -0700235 return __do_int_store(dst_addr, size, src_val, asi);
David S. Millera3f99852005-08-19 15:55:33 -0700236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238static inline void advance(struct pt_regs *regs)
239{
240 regs->tpc = regs->tnpc;
241 regs->tnpc += 4;
242 if (test_thread_flag(TIF_32BIT)) {
243 regs->tpc &= 0xffffffff;
244 regs->tnpc &= 0xffffffff;
245 }
246}
247
248static inline int floating_point_load_or_store_p(unsigned int insn)
249{
250 return (insn >> 24) & 1;
251}
252
253static inline int ok_for_kernel(unsigned int insn)
254{
255 return !floating_point_load_or_store_p(insn);
256}
257
David S. Millerc449c382006-11-28 20:18:05 -0800258static void kernel_mna_trap_fault(int fixup_tstate_asi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
David S. Millera3f99852005-08-19 15:55:33 -0700260 struct pt_regs *regs = current_thread_info()->kern_una_regs;
261 unsigned int insn = current_thread_info()->kern_una_insn;
David S. Miller8cf14af2005-09-28 20:21:11 -0700262 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David S. Miller8cf14af2005-09-28 20:21:11 -0700264 entry = search_exception_tables(regs->tpc);
265 if (!entry) {
David S. Millera3f99852005-08-19 15:55:33 -0700266 unsigned long address;
267
268 address = compute_effective_address(regs, insn,
269 ((insn >> 25) & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (address < PAGE_SIZE) {
David S. Millera3f99852005-08-19 15:55:33 -0700271 printk(KERN_ALERT "Unable to handle kernel NULL "
272 "pointer dereference in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else
David S. Millera3f99852005-08-19 15:55:33 -0700274 printk(KERN_ALERT "Unable to handle kernel paging "
275 "request in mna handler");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 printk(KERN_ALERT " at virtual address %016lx\n",address);
David S. Millera3f99852005-08-19 15:55:33 -0700277 printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 (current->mm ? CTX_HWBITS(current->mm->context) :
279 CTX_HWBITS(current->active_mm->context)));
David S. Millera3f99852005-08-19 15:55:33 -0700280 printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 (current->mm ? (unsigned long) current->mm->pgd :
282 (unsigned long) current->active_mm->pgd));
283 die_if_kernel("Oops", regs);
284 /* Not reached */
285 }
David S. Miller8cf14af2005-09-28 20:21:11 -0700286 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
David S. Millerc449c382006-11-28 20:18:05 -0800289 if (fixup_tstate_asi) {
290 regs->tstate &= ~TSTATE_ASI;
291 regs->tstate |= (ASI_AIUS << 24UL);
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
David S. Millerc449c382006-11-28 20:18:05 -0800295static void log_unaligned(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Akinobu Mitac7ec2b52010-02-28 03:31:29 -0800297 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
David S. Millera3f99852005-08-19 15:55:33 -0700298
Akinobu Mitac7ec2b52010-02-28 03:31:29 -0800299 if (__ratelimit(&ratelimit)) {
David S. Miller4fe3ebe2008-07-17 22:11:32 -0700300 printk("Kernel unaligned access at TPC[%lx] %pS\n",
301 regs->tpc, (void *) regs->tpc);
David S. Miller27cc64c2006-06-21 22:31:08 -0700302 }
David S. Millerc449c382006-11-28 20:18:05 -0800303}
304
305asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
306{
307 enum direction dir = decode_direction(insn);
David S. Millerbaa06772010-04-19 13:46:48 -0700308 int size = decode_access_size(regs, insn);
David S. Millerc449c382006-11-28 20:18:05 -0800309 int orig_asi, asi;
310
311 current_thread_info()->kern_una_regs = regs;
312 current_thread_info()->kern_una_insn = insn;
313
314 orig_asi = asi = decode_asi(insn, regs);
315
316 /* If this is a {get,put}_user() on an unaligned userspace pointer,
317 * just signal a fault and do not log the event.
318 */
319 if (asi == ASI_AIUS) {
320 kernel_mna_trap_fault(0);
321 return;
322 }
323
324 log_unaligned(regs);
David S. Miller27cc64c2006-06-21 22:31:08 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!ok_for_kernel(insn) || dir == both) {
David S. Millera3f99852005-08-19 15:55:33 -0700327 printk("Unsupported unaligned load/store trap for kernel "
328 "at <%016lx>.\n", regs->tpc);
329 unaligned_panic("Kernel does fpu/atomic "
330 "unaligned load/store.", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David S. Millerc449c382006-11-28 20:18:05 -0800332 kernel_mna_trap_fault(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 } else {
David S. Miller705747a2005-09-28 16:48:40 -0700334 unsigned long addr, *reg_addr;
David S. Millerc449c382006-11-28 20:18:05 -0800335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David S. Millera3f99852005-08-19 15:55:33 -0700337 addr = compute_effective_address(regs, insn,
338 ((insn >> 25) & 0x1f));
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200339 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
David S. Millerff171d82005-09-19 19:56:06 -0700340 switch (asi) {
341 case ASI_NL:
342 case ASI_AIUPL:
343 case ASI_AIUSL:
344 case ASI_PL:
345 case ASI_SL:
346 case ASI_PNFL:
347 case ASI_SNFL:
348 asi &= ~0x08;
349 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 switch (dir) {
352 case load:
David S. Miller705747a2005-09-28 16:48:40 -0700353 reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
David S. Miller5fd297522005-09-28 20:41:45 -0700354 err = do_int_load(reg_addr, size,
355 (unsigned long *) addr,
356 decode_signedness(insn), asi);
357 if (likely(!err) && unlikely(asi != orig_asi)) {
David S. Miller705747a2005-09-28 16:48:40 -0700358 unsigned long val_in = *reg_addr;
David S. Millerff171d82005-09-19 19:56:06 -0700359 switch (size) {
360 case 2:
361 val_in = swab16(val_in);
362 break;
363 case 4:
364 val_in = swab32(val_in);
365 break;
366 case 8:
367 val_in = swab64(val_in);
368 break;
369 case 16:
370 default:
371 BUG();
372 break;
Joe Perches6cb79b32011-06-03 14:45:23 +0000373 }
David S. Miller705747a2005-09-28 16:48:40 -0700374 *reg_addr = val_in;
David S. Millerff171d82005-09-19 19:56:06 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377
378 case store:
David S. Miller5fd297522005-09-28 20:41:45 -0700379 err = do_int_store(((insn>>25)&0x1f), size,
380 (unsigned long *) addr, regs,
381 asi, orig_asi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
David S. Millera3f99852005-08-19 15:55:33 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 default:
385 panic("Impossible kernel unaligned trap.");
386 /* Not reached... */
387 }
David S. Miller5fd297522005-09-28 20:41:45 -0700388 if (unlikely(err))
David S. Millerc449c382006-11-28 20:18:05 -0800389 kernel_mna_trap_fault(1);
David S. Miller5fd297522005-09-28 20:41:45 -0700390 else
391 advance(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395int handle_popc(u32 insn, struct pt_regs *regs)
396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
David S. Millerd600cbe2011-08-01 19:41:12 -0700398 int ret, rd = ((insn >> 25) & 0x1f);
399 u64 value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200401 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (insn & 0x2000) {
403 maybe_flush_windows(0, 0, rd, from_kernel);
404 value = sign_extend_imm13(insn);
405 } else {
406 maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
407 value = fetch_reg(insn & 0x1f, regs);
408 }
David S. Millerd600cbe2011-08-01 19:41:12 -0700409 ret = hweight64(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (rd < 16) {
411 if (rd)
412 regs->u_regs[rd] = ret;
413 } else {
David S. Miller517ffce2012-10-26 15:18:37 -0700414 unsigned long fp = regs->u_regs[UREG_FP];
415
416 if (!test_thread_64bit_stack(fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct reg_window32 __user *win32;
David S. Miller517ffce2012-10-26 15:18:37 -0700418 win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 put_user(ret, &win32->locals[rd - 16]);
420 } else {
421 struct reg_window __user *win;
David S. Miller517ffce2012-10-26 15:18:37 -0700422 win = (struct reg_window __user *)(fp + STACK_BIAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 put_user(ret, &win->locals[rd - 16]);
424 }
425 }
426 advance(regs);
427 return 1;
428}
429
430extern void do_fpother(struct pt_regs *regs);
431extern void do_privact(struct pt_regs *regs);
David S. Millered6b0b42006-02-09 20:20:34 -0800432extern void sun4v_data_access_exception(struct pt_regs *regs,
433 unsigned long addr,
434 unsigned long type_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436int handle_ldf_stq(u32 insn, struct pt_regs *regs)
437{
438 unsigned long addr = compute_effective_address(regs, insn, 0);
Rob Gardnercae9af62015-10-30 22:36:23 -0600439 int freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct fpustate *f = FPUSTATE;
441 int asi = decode_asi(insn, regs);
Rob Gardnercae9af62015-10-30 22:36:23 -0600442 int flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200444 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
David S. Miller121dd5f2009-12-11 01:07:53 -0800445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 save_and_clear_fpu();
447 current_thread_info()->xfsr[0] &= ~0x1c000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (insn & 0x200000) {
449 /* STQ */
450 u64 first = 0, second = 0;
451
Rob Gardnercae9af62015-10-30 22:36:23 -0600452 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
453 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
454 if (freg & 3) {
455 current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
456 do_fpother(regs);
457 return 0;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (current_thread_info()->fpsaved[0] & flag) {
460 first = *(u64 *)&f->regs[freg];
461 second = *(u64 *)&f->regs[freg+2];
462 }
463 if (asi < 0x80) {
464 do_privact(regs);
465 return 1;
466 }
467 switch (asi) {
468 case ASI_P:
469 case ASI_S: break;
470 case ASI_PL:
471 case ASI_SL:
472 {
473 /* Need to convert endians */
474 u64 tmp = __swab64p(&first);
475
476 first = __swab64p(&second);
477 second = tmp;
478 break;
479 }
480 default:
David S. Millered6b0b42006-02-09 20:20:34 -0800481 if (tlb_type == hypervisor)
482 sun4v_data_access_exception(regs, addr, 0);
483 else
484 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 1;
486 }
487 if (put_user (first >> 32, (u32 __user *)addr) ||
488 __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
489 __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
490 __put_user ((u32)second, (u32 __user *)(addr + 12))) {
David S. Millered6b0b42006-02-09 20:20:34 -0800491 if (tlb_type == hypervisor)
492 sun4v_data_access_exception(regs, addr, 0);
493 else
494 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 1;
496 }
497 } else {
498 /* LDF, LDDF, LDQF */
499 u32 data[4] __attribute__ ((aligned(8)));
500 int size, i;
501 int err;
502
503 if (asi < 0x80) {
504 do_privact(regs);
505 return 1;
506 } else if (asi > ASI_SNFL) {
David S. Millered6b0b42006-02-09 20:20:34 -0800507 if (tlb_type == hypervisor)
508 sun4v_data_access_exception(regs, addr, 0);
509 else
510 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 1;
512 }
513 switch (insn & 0x180000) {
514 case 0x000000: size = 1; break;
515 case 0x100000: size = 4; break;
516 default: size = 2; break;
517 }
Rob Gardnercae9af62015-10-30 22:36:23 -0600518 if (size == 1)
519 freg = (insn >> 25) & 0x1f;
520 else
521 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
522 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 for (i = 0; i < size; i++)
525 data[i] = 0;
526
527 err = get_user (data[0], (u32 __user *) addr);
528 if (!err) {
529 for (i = 1; i < size; i++)
530 err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
531 }
532 if (err && !(asi & 0x2 /* NF */)) {
David S. Millered6b0b42006-02-09 20:20:34 -0800533 if (tlb_type == hypervisor)
534 sun4v_data_access_exception(regs, addr, 0);
535 else
536 spitfire_data_access_exception(regs, 0, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 1;
538 }
539 if (asi & 0x8) /* Little */ {
540 u64 tmp;
541
542 switch (size) {
543 case 1: data[0] = le32_to_cpup(data + 0); break;
544 default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
545 break;
546 case 4: tmp = le64_to_cpup((u64 *)(data + 0));
547 *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
548 *(u64 *)(data + 2) = tmp;
549 break;
550 }
551 }
552 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
553 current_thread_info()->fpsaved[0] = FPRS_FEF;
554 current_thread_info()->gsr[0] = 0;
555 }
556 if (!(current_thread_info()->fpsaved[0] & flag)) {
557 if (freg < 32)
558 memset(f->regs, 0, 32*sizeof(u32));
559 else
560 memset(f->regs+32, 0, 32*sizeof(u32));
561 }
562 memcpy(f->regs + freg, data, size * 4);
563 current_thread_info()->fpsaved[0] |= flag;
564 }
565 advance(regs);
566 return 1;
567}
568
569void handle_ld_nf(u32 insn, struct pt_regs *regs)
570{
571 int rd = ((insn >> 25) & 0x1f);
572 int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
573 unsigned long *reg;
574
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200575 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
David S. Miller121dd5f2009-12-11 01:07:53 -0800576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 maybe_flush_windows(0, 0, rd, from_kernel);
578 reg = fetch_reg_addr(rd, regs);
579 if (from_kernel || rd < 16) {
580 reg[0] = 0;
581 if ((insn & 0x780000) == 0x180000)
582 reg[1] = 0;
David S. Miller517ffce2012-10-26 15:18:37 -0700583 } else if (!test_thread_64bit_stack(regs->u_regs[UREG_FP])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 put_user(0, (int __user *) reg);
585 if ((insn & 0x780000) == 0x180000)
586 put_user(0, ((int __user *) reg) + 1);
587 } else {
588 put_user(0, (unsigned long __user *) reg);
589 if ((insn & 0x780000) == 0x180000)
590 put_user(0, (unsigned long __user *) reg + 1);
591 }
592 advance(regs);
593}
594
595void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
596{
Kirill Tkhai812cb832013-09-14 16:02:11 +0400597 enum ctx_state prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unsigned long pc = regs->tpc;
599 unsigned long tstate = regs->tstate;
600 u32 insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800602 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int flag;
604 struct fpustate *f = FPUSTATE;
605
606 if (tstate & TSTATE_PRIV)
607 die_if_kernel("lddfmna from kernel", regs);
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200608 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (test_thread_flag(TIF_32BIT))
610 pc = (u32)pc;
611 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800612 int asi = decode_asi(insn, regs);
David S. Millerb41418f2009-01-08 16:52:36 -0800613 u32 first, second;
David S. Miller18b8e082009-01-07 17:15:57 -0800614 int err;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if ((asi > ASI_SNFL) ||
617 (asi < ASI_P))
618 goto daex;
David S. Millerb41418f2009-01-08 16:52:36 -0800619 first = second = 0;
David S. Miller18b8e082009-01-07 17:15:57 -0800620 err = get_user(first, (u32 __user *)sfar);
621 if (!err)
622 err = get_user(second, (u32 __user *)(sfar + 4));
623 if (err) {
David S. Millerb41418f2009-01-08 16:52:36 -0800624 if (!(asi & 0x2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto daex;
David S. Millerb41418f2009-01-08 16:52:36 -0800626 first = second = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 save_and_clear_fpu();
629 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
630 value = (((u64)first) << 32) | second;
631 if (asi & 0x8) /* Little */
632 value = __swab64p(&value);
633 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
634 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
635 current_thread_info()->fpsaved[0] = FPRS_FEF;
636 current_thread_info()->gsr[0] = 0;
637 }
638 if (!(current_thread_info()->fpsaved[0] & flag)) {
639 if (freg < 32)
640 memset(f->regs, 0, 32*sizeof(u32));
641 else
642 memset(f->regs+32, 0, 32*sizeof(u32));
643 }
644 *(u64 *)(f->regs + freg) = value;
645 current_thread_info()->fpsaved[0] |= flag;
646 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800647daex:
648 if (tlb_type == hypervisor)
649 sun4v_data_access_exception(regs, sfar, sfsr);
650 else
651 spitfire_data_access_exception(regs, sfsr, sfar);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400652 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 advance(regs);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400655out:
656 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
660{
Kirill Tkhai812cb832013-09-14 16:02:11 +0400661 enum ctx_state prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 unsigned long pc = regs->tpc;
663 unsigned long tstate = regs->tstate;
664 u32 insn;
665 u64 value;
David S. Millered6b0b42006-02-09 20:20:34 -0800666 u8 freg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int flag;
668 struct fpustate *f = FPUSTATE;
669
670 if (tstate & TSTATE_PRIV)
671 die_if_kernel("stdfmna from kernel", regs);
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200672 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (test_thread_flag(TIF_32BIT))
674 pc = (u32)pc;
675 if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
David S. Millered6b0b42006-02-09 20:20:34 -0800676 int asi = decode_asi(insn, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 value = 0;
679 flag = (freg < 32) ? FPRS_DL : FPRS_DU;
680 if ((asi > ASI_SNFL) ||
681 (asi < ASI_P))
682 goto daex;
683 save_and_clear_fpu();
684 if (current_thread_info()->fpsaved[0] & flag)
685 value = *(u64 *)&f->regs[freg];
686 switch (asi) {
687 case ASI_P:
688 case ASI_S: break;
689 case ASI_PL:
690 case ASI_SL:
691 value = __swab64p(&value); break;
692 default: goto daex;
693 }
694 if (put_user (value >> 32, (u32 __user *) sfar) ||
695 __put_user ((u32)value, (u32 __user *)(sfar + 4)))
696 goto daex;
697 } else {
David S. Millered6b0b42006-02-09 20:20:34 -0800698daex:
699 if (tlb_type == hypervisor)
700 sun4v_data_access_exception(regs, sfar, sfsr);
701 else
702 spitfire_data_access_exception(regs, sfsr, sfar);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400703 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 advance(regs);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400706out:
707 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}