unaligned_64.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  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,2008 David S. Miller (davem@davemloft.net)
  6. * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. */
  8. #include <linux/jiffies.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <asm/asi.h>
  14. #include <asm/ptrace.h>
  15. #include <asm/pstate.h>
  16. #include <asm/processor.h>
  17. #include <asm/uaccess.h>
  18. #include <linux/smp.h>
  19. #include <linux/bitops.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/context_tracking.h>
  23. #include <asm/fpumacro.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/setup.h>
  26. #include "entry.h"
  27. #include "kernel.h"
  28. enum direction {
  29. load, /* ld, ldd, ldh, ldsh */
  30. store, /* st, std, sth, stsh */
  31. both, /* Swap, ldstub, cas, ... */
  32. fpld,
  33. fpst,
  34. invalid,
  35. };
  36. static inline enum direction decode_direction(unsigned int insn)
  37. {
  38. unsigned long tmp = (insn >> 21) & 1;
  39. if (!tmp)
  40. return load;
  41. else {
  42. switch ((insn>>19)&0xf) {
  43. case 15: /* swap* */
  44. return both;
  45. default:
  46. return store;
  47. }
  48. }
  49. }
  50. /* 16 = double-word, 8 = extra-word, 4 = word, 2 = half-word */
  51. static inline int decode_access_size(struct pt_regs *regs, unsigned int insn)
  52. {
  53. unsigned int tmp;
  54. tmp = ((insn >> 19) & 0xf);
  55. if (tmp == 11 || tmp == 14) /* ldx/stx */
  56. return 8;
  57. tmp &= 3;
  58. if (!tmp)
  59. return 4;
  60. else if (tmp == 3)
  61. return 16; /* ldd/std - Although it is actually 8 */
  62. else if (tmp == 2)
  63. return 2;
  64. else {
  65. printk("Impossible unaligned trap. insn=%08x\n", insn);
  66. die_if_kernel("Byte sized unaligned access?!?!", regs);
  67. /* GCC should never warn that control reaches the end
  68. * of this function without returning a value because
  69. * die_if_kernel() is marked with attribute 'noreturn'.
  70. * Alas, some versions do...
  71. */
  72. return 0;
  73. }
  74. }
  75. static inline int decode_asi(unsigned int insn, struct pt_regs *regs)
  76. {
  77. if (insn & 0x800000) {
  78. if (insn & 0x2000)
  79. return (unsigned char)(regs->tstate >> 24); /* %asi */
  80. else
  81. return (unsigned char)(insn >> 5); /* imm_asi */
  82. } else
  83. return ASI_P;
  84. }
  85. /* 0x400000 = signed, 0 = unsigned */
  86. static inline int decode_signedness(unsigned int insn)
  87. {
  88. return (insn & 0x400000);
  89. }
  90. static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
  91. unsigned int rd, int from_kernel)
  92. {
  93. if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
  94. if (from_kernel != 0)
  95. __asm__ __volatile__("flushw");
  96. else
  97. flushw_user();
  98. }
  99. }
  100. static inline long sign_extend_imm13(long imm)
  101. {
  102. return imm << 51 >> 51;
  103. }
  104. static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
  105. {
  106. unsigned long value, fp;
  107. if (reg < 16)
  108. return (!reg ? 0 : regs->u_regs[reg]);
  109. fp = regs->u_regs[UREG_FP];
  110. if (regs->tstate & TSTATE_PRIV) {
  111. struct reg_window *win;
  112. win = (struct reg_window *)(fp + STACK_BIAS);
  113. value = win->locals[reg - 16];
  114. } else if (!test_thread_64bit_stack(fp)) {
  115. struct reg_window32 __user *win32;
  116. win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
  117. get_user(value, &win32->locals[reg - 16]);
  118. } else {
  119. struct reg_window __user *win;
  120. win = (struct reg_window __user *)(fp + STACK_BIAS);
  121. get_user(value, &win->locals[reg - 16]);
  122. }
  123. return value;
  124. }
  125. static unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
  126. {
  127. unsigned long fp;
  128. if (reg < 16)
  129. return &regs->u_regs[reg];
  130. fp = regs->u_regs[UREG_FP];
  131. if (regs->tstate & TSTATE_PRIV) {
  132. struct reg_window *win;
  133. win = (struct reg_window *)(fp + STACK_BIAS);
  134. return &win->locals[reg - 16];
  135. } else if (!test_thread_64bit_stack(fp)) {
  136. struct reg_window32 *win32;
  137. win32 = (struct reg_window32 *)((unsigned long)((u32)fp));
  138. return (unsigned long *)&win32->locals[reg - 16];
  139. } else {
  140. struct reg_window *win;
  141. win = (struct reg_window *)(fp + STACK_BIAS);
  142. return &win->locals[reg - 16];
  143. }
  144. }
  145. unsigned long compute_effective_address(struct pt_regs *regs,
  146. unsigned int insn, unsigned int rd)
  147. {
  148. int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
  149. unsigned int rs1 = (insn >> 14) & 0x1f;
  150. unsigned int rs2 = insn & 0x1f;
  151. unsigned long addr;
  152. if (insn & 0x2000) {
  153. maybe_flush_windows(rs1, 0, rd, from_kernel);
  154. addr = (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
  155. } else {
  156. maybe_flush_windows(rs1, rs2, rd, from_kernel);
  157. addr = (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
  158. }
  159. if (!from_kernel && test_thread_flag(TIF_32BIT))
  160. addr &= 0xffffffff;
  161. return addr;
  162. }
  163. /* This is just to make gcc think die_if_kernel does return... */
  164. static void __used unaligned_panic(char *str, struct pt_regs *regs)
  165. {
  166. die_if_kernel(str, regs);
  167. }
  168. extern int do_int_load(unsigned long *dest_reg, int size,
  169. unsigned long *saddr, int is_signed, int asi);
  170. extern int __do_int_store(unsigned long *dst_addr, int size,
  171. unsigned long src_val, int asi);
  172. static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
  173. struct pt_regs *regs, int asi, int orig_asi)
  174. {
  175. unsigned long zero = 0;
  176. unsigned long *src_val_p = &zero;
  177. unsigned long src_val;
  178. if (size == 16) {
  179. size = 8;
  180. zero = (((long)(reg_num ?
  181. (unsigned)fetch_reg(reg_num, regs) : 0)) << 32) |
  182. (unsigned)fetch_reg(reg_num + 1, regs);
  183. } else if (reg_num) {
  184. src_val_p = fetch_reg_addr(reg_num, regs);
  185. }
  186. src_val = *src_val_p;
  187. if (unlikely(asi != orig_asi)) {
  188. switch (size) {
  189. case 2:
  190. src_val = swab16(src_val);
  191. break;
  192. case 4:
  193. src_val = swab32(src_val);
  194. break;
  195. case 8:
  196. src_val = swab64(src_val);
  197. break;
  198. case 16:
  199. default:
  200. BUG();
  201. break;
  202. }
  203. }
  204. return __do_int_store(dst_addr, size, src_val, asi);
  205. }
  206. static inline void advance(struct pt_regs *regs)
  207. {
  208. regs->tpc = regs->tnpc;
  209. regs->tnpc += 4;
  210. if (test_thread_flag(TIF_32BIT)) {
  211. regs->tpc &= 0xffffffff;
  212. regs->tnpc &= 0xffffffff;
  213. }
  214. }
  215. static inline int floating_point_load_or_store_p(unsigned int insn)
  216. {
  217. return (insn >> 24) & 1;
  218. }
  219. static inline int ok_for_kernel(unsigned int insn)
  220. {
  221. return !floating_point_load_or_store_p(insn);
  222. }
  223. static void kernel_mna_trap_fault(int fixup_tstate_asi)
  224. {
  225. struct pt_regs *regs = current_thread_info()->kern_una_regs;
  226. unsigned int insn = current_thread_info()->kern_una_insn;
  227. const struct exception_table_entry *entry;
  228. entry = search_exception_tables(regs->tpc);
  229. if (!entry) {
  230. unsigned long address;
  231. address = compute_effective_address(regs, insn,
  232. ((insn >> 25) & 0x1f));
  233. if (address < PAGE_SIZE) {
  234. printk(KERN_ALERT "Unable to handle kernel NULL "
  235. "pointer dereference in mna handler");
  236. } else
  237. printk(KERN_ALERT "Unable to handle kernel paging "
  238. "request in mna handler");
  239. printk(KERN_ALERT " at virtual address %016lx\n",address);
  240. printk(KERN_ALERT "current->{active_,}mm->context = %016lx\n",
  241. (current->mm ? CTX_HWBITS(current->mm->context) :
  242. CTX_HWBITS(current->active_mm->context)));
  243. printk(KERN_ALERT "current->{active_,}mm->pgd = %016lx\n",
  244. (current->mm ? (unsigned long) current->mm->pgd :
  245. (unsigned long) current->active_mm->pgd));
  246. die_if_kernel("Oops", regs);
  247. /* Not reached */
  248. }
  249. regs->tpc = entry->fixup;
  250. regs->tnpc = regs->tpc + 4;
  251. if (fixup_tstate_asi) {
  252. regs->tstate &= ~TSTATE_ASI;
  253. regs->tstate |= (ASI_AIUS << 24UL);
  254. }
  255. }
  256. static void log_unaligned(struct pt_regs *regs)
  257. {
  258. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
  259. if (__ratelimit(&ratelimit)) {
  260. printk("Kernel unaligned access at TPC[%lx] %pS\n",
  261. regs->tpc, (void *) regs->tpc);
  262. }
  263. }
  264. asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
  265. {
  266. enum direction dir = decode_direction(insn);
  267. int size = decode_access_size(regs, insn);
  268. int orig_asi, asi;
  269. current_thread_info()->kern_una_regs = regs;
  270. current_thread_info()->kern_una_insn = insn;
  271. orig_asi = asi = decode_asi(insn, regs);
  272. /* If this is a {get,put}_user() on an unaligned userspace pointer,
  273. * just signal a fault and do not log the event.
  274. */
  275. if (asi == ASI_AIUS) {
  276. kernel_mna_trap_fault(0);
  277. return;
  278. }
  279. log_unaligned(regs);
  280. if (!ok_for_kernel(insn) || dir == both) {
  281. printk("Unsupported unaligned load/store trap for kernel "
  282. "at <%016lx>.\n", regs->tpc);
  283. unaligned_panic("Kernel does fpu/atomic "
  284. "unaligned load/store.", regs);
  285. kernel_mna_trap_fault(0);
  286. } else {
  287. unsigned long addr, *reg_addr;
  288. int err;
  289. addr = compute_effective_address(regs, insn,
  290. ((insn >> 25) & 0x1f));
  291. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
  292. switch (asi) {
  293. case ASI_NL:
  294. case ASI_AIUPL:
  295. case ASI_AIUSL:
  296. case ASI_PL:
  297. case ASI_SL:
  298. case ASI_PNFL:
  299. case ASI_SNFL:
  300. asi &= ~0x08;
  301. break;
  302. }
  303. switch (dir) {
  304. case load:
  305. reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
  306. err = do_int_load(reg_addr, size,
  307. (unsigned long *) addr,
  308. decode_signedness(insn), asi);
  309. if (likely(!err) && unlikely(asi != orig_asi)) {
  310. unsigned long val_in = *reg_addr;
  311. switch (size) {
  312. case 2:
  313. val_in = swab16(val_in);
  314. break;
  315. case 4:
  316. val_in = swab32(val_in);
  317. break;
  318. case 8:
  319. val_in = swab64(val_in);
  320. break;
  321. case 16:
  322. default:
  323. BUG();
  324. break;
  325. }
  326. *reg_addr = val_in;
  327. }
  328. break;
  329. case store:
  330. err = do_int_store(((insn>>25)&0x1f), size,
  331. (unsigned long *) addr, regs,
  332. asi, orig_asi);
  333. break;
  334. default:
  335. panic("Impossible kernel unaligned trap.");
  336. /* Not reached... */
  337. }
  338. if (unlikely(err))
  339. kernel_mna_trap_fault(1);
  340. else
  341. advance(regs);
  342. }
  343. }
  344. int handle_popc(u32 insn, struct pt_regs *regs)
  345. {
  346. int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
  347. int ret, rd = ((insn >> 25) & 0x1f);
  348. u64 value;
  349. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  350. if (insn & 0x2000) {
  351. maybe_flush_windows(0, 0, rd, from_kernel);
  352. value = sign_extend_imm13(insn);
  353. } else {
  354. maybe_flush_windows(0, insn & 0x1f, rd, from_kernel);
  355. value = fetch_reg(insn & 0x1f, regs);
  356. }
  357. ret = hweight64(value);
  358. if (rd < 16) {
  359. if (rd)
  360. regs->u_regs[rd] = ret;
  361. } else {
  362. unsigned long fp = regs->u_regs[UREG_FP];
  363. if (!test_thread_64bit_stack(fp)) {
  364. struct reg_window32 __user *win32;
  365. win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
  366. put_user(ret, &win32->locals[rd - 16]);
  367. } else {
  368. struct reg_window __user *win;
  369. win = (struct reg_window __user *)(fp + STACK_BIAS);
  370. put_user(ret, &win->locals[rd - 16]);
  371. }
  372. }
  373. advance(regs);
  374. return 1;
  375. }
  376. extern void do_fpother(struct pt_regs *regs);
  377. extern void do_privact(struct pt_regs *regs);
  378. extern void sun4v_data_access_exception(struct pt_regs *regs,
  379. unsigned long addr,
  380. unsigned long type_ctx);
  381. int handle_ldf_stq(u32 insn, struct pt_regs *regs)
  382. {
  383. unsigned long addr = compute_effective_address(regs, insn, 0);
  384. int freg;
  385. struct fpustate *f = FPUSTATE;
  386. int asi = decode_asi(insn, regs);
  387. int flag;
  388. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  389. save_and_clear_fpu();
  390. current_thread_info()->xfsr[0] &= ~0x1c000;
  391. if (insn & 0x200000) {
  392. /* STQ */
  393. u64 first = 0, second = 0;
  394. freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
  395. flag = (freg < 32) ? FPRS_DL : FPRS_DU;
  396. if (freg & 3) {
  397. current_thread_info()->xfsr[0] |= (6 << 14) /* invalid_fp_register */;
  398. do_fpother(regs);
  399. return 0;
  400. }
  401. if (current_thread_info()->fpsaved[0] & flag) {
  402. first = *(u64 *)&f->regs[freg];
  403. second = *(u64 *)&f->regs[freg+2];
  404. }
  405. if (asi < 0x80) {
  406. do_privact(regs);
  407. return 1;
  408. }
  409. switch (asi) {
  410. case ASI_P:
  411. case ASI_S: break;
  412. case ASI_PL:
  413. case ASI_SL:
  414. {
  415. /* Need to convert endians */
  416. u64 tmp = __swab64p(&first);
  417. first = __swab64p(&second);
  418. second = tmp;
  419. break;
  420. }
  421. default:
  422. if (tlb_type == hypervisor)
  423. sun4v_data_access_exception(regs, addr, 0);
  424. else
  425. spitfire_data_access_exception(regs, 0, addr);
  426. return 1;
  427. }
  428. if (put_user (first >> 32, (u32 __user *)addr) ||
  429. __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
  430. __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
  431. __put_user ((u32)second, (u32 __user *)(addr + 12))) {
  432. if (tlb_type == hypervisor)
  433. sun4v_data_access_exception(regs, addr, 0);
  434. else
  435. spitfire_data_access_exception(regs, 0, addr);
  436. return 1;
  437. }
  438. } else {
  439. /* LDF, LDDF, LDQF */
  440. u32 data[4] __attribute__ ((aligned(8)));
  441. int size, i;
  442. int err;
  443. if (asi < 0x80) {
  444. do_privact(regs);
  445. return 1;
  446. } else if (asi > ASI_SNFL) {
  447. if (tlb_type == hypervisor)
  448. sun4v_data_access_exception(regs, addr, 0);
  449. else
  450. spitfire_data_access_exception(regs, 0, addr);
  451. return 1;
  452. }
  453. switch (insn & 0x180000) {
  454. case 0x000000: size = 1; break;
  455. case 0x100000: size = 4; break;
  456. default: size = 2; break;
  457. }
  458. if (size == 1)
  459. freg = (insn >> 25) & 0x1f;
  460. else
  461. freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
  462. flag = (freg < 32) ? FPRS_DL : FPRS_DU;
  463. for (i = 0; i < size; i++)
  464. data[i] = 0;
  465. err = get_user (data[0], (u32 __user *) addr);
  466. if (!err) {
  467. for (i = 1; i < size; i++)
  468. err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
  469. }
  470. if (err && !(asi & 0x2 /* NF */)) {
  471. if (tlb_type == hypervisor)
  472. sun4v_data_access_exception(regs, addr, 0);
  473. else
  474. spitfire_data_access_exception(regs, 0, addr);
  475. return 1;
  476. }
  477. if (asi & 0x8) /* Little */ {
  478. u64 tmp;
  479. switch (size) {
  480. case 1: data[0] = le32_to_cpup(data + 0); break;
  481. default:*(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 0));
  482. break;
  483. case 4: tmp = le64_to_cpup((u64 *)(data + 0));
  484. *(u64 *)(data + 0) = le64_to_cpup((u64 *)(data + 2));
  485. *(u64 *)(data + 2) = tmp;
  486. break;
  487. }
  488. }
  489. if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
  490. current_thread_info()->fpsaved[0] = FPRS_FEF;
  491. current_thread_info()->gsr[0] = 0;
  492. }
  493. if (!(current_thread_info()->fpsaved[0] & flag)) {
  494. if (freg < 32)
  495. memset(f->regs, 0, 32*sizeof(u32));
  496. else
  497. memset(f->regs+32, 0, 32*sizeof(u32));
  498. }
  499. memcpy(f->regs + freg, data, size * 4);
  500. current_thread_info()->fpsaved[0] |= flag;
  501. }
  502. advance(regs);
  503. return 1;
  504. }
  505. void handle_ld_nf(u32 insn, struct pt_regs *regs)
  506. {
  507. int rd = ((insn >> 25) & 0x1f);
  508. int from_kernel = (regs->tstate & TSTATE_PRIV) != 0;
  509. unsigned long *reg;
  510. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  511. maybe_flush_windows(0, 0, rd, from_kernel);
  512. reg = fetch_reg_addr(rd, regs);
  513. if (from_kernel || rd < 16) {
  514. reg[0] = 0;
  515. if ((insn & 0x780000) == 0x180000)
  516. reg[1] = 0;
  517. } else if (!test_thread_64bit_stack(regs->u_regs[UREG_FP])) {
  518. put_user(0, (int __user *) reg);
  519. if ((insn & 0x780000) == 0x180000)
  520. put_user(0, ((int __user *) reg) + 1);
  521. } else {
  522. put_user(0, (unsigned long __user *) reg);
  523. if ((insn & 0x780000) == 0x180000)
  524. put_user(0, (unsigned long __user *) reg + 1);
  525. }
  526. advance(regs);
  527. }
  528. void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
  529. {
  530. enum ctx_state prev_state = exception_enter();
  531. unsigned long pc = regs->tpc;
  532. unsigned long tstate = regs->tstate;
  533. u32 insn;
  534. u64 value;
  535. u8 freg;
  536. int flag;
  537. struct fpustate *f = FPUSTATE;
  538. if (tstate & TSTATE_PRIV)
  539. die_if_kernel("lddfmna from kernel", regs);
  540. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
  541. if (test_thread_flag(TIF_32BIT))
  542. pc = (u32)pc;
  543. if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
  544. int asi = decode_asi(insn, regs);
  545. u32 first, second;
  546. int err;
  547. if ((asi > ASI_SNFL) ||
  548. (asi < ASI_P))
  549. goto daex;
  550. first = second = 0;
  551. err = get_user(first, (u32 __user *)sfar);
  552. if (!err)
  553. err = get_user(second, (u32 __user *)(sfar + 4));
  554. if (err) {
  555. if (!(asi & 0x2))
  556. goto daex;
  557. first = second = 0;
  558. }
  559. save_and_clear_fpu();
  560. freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
  561. value = (((u64)first) << 32) | second;
  562. if (asi & 0x8) /* Little */
  563. value = __swab64p(&value);
  564. flag = (freg < 32) ? FPRS_DL : FPRS_DU;
  565. if (!(current_thread_info()->fpsaved[0] & FPRS_FEF)) {
  566. current_thread_info()->fpsaved[0] = FPRS_FEF;
  567. current_thread_info()->gsr[0] = 0;
  568. }
  569. if (!(current_thread_info()->fpsaved[0] & flag)) {
  570. if (freg < 32)
  571. memset(f->regs, 0, 32*sizeof(u32));
  572. else
  573. memset(f->regs+32, 0, 32*sizeof(u32));
  574. }
  575. *(u64 *)(f->regs + freg) = value;
  576. current_thread_info()->fpsaved[0] |= flag;
  577. } else {
  578. daex:
  579. if (tlb_type == hypervisor)
  580. sun4v_data_access_exception(regs, sfar, sfsr);
  581. else
  582. spitfire_data_access_exception(regs, sfsr, sfar);
  583. goto out;
  584. }
  585. advance(regs);
  586. out:
  587. exception_exit(prev_state);
  588. }
  589. void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr)
  590. {
  591. enum ctx_state prev_state = exception_enter();
  592. unsigned long pc = regs->tpc;
  593. unsigned long tstate = regs->tstate;
  594. u32 insn;
  595. u64 value;
  596. u8 freg;
  597. int flag;
  598. struct fpustate *f = FPUSTATE;
  599. if (tstate & TSTATE_PRIV)
  600. die_if_kernel("stdfmna from kernel", regs);
  601. perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, sfar);
  602. if (test_thread_flag(TIF_32BIT))
  603. pc = (u32)pc;
  604. if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
  605. int asi = decode_asi(insn, regs);
  606. freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
  607. value = 0;
  608. flag = (freg < 32) ? FPRS_DL : FPRS_DU;
  609. if ((asi > ASI_SNFL) ||
  610. (asi < ASI_P))
  611. goto daex;
  612. save_and_clear_fpu();
  613. if (current_thread_info()->fpsaved[0] & flag)
  614. value = *(u64 *)&f->regs[freg];
  615. switch (asi) {
  616. case ASI_P:
  617. case ASI_S: break;
  618. case ASI_PL:
  619. case ASI_SL:
  620. value = __swab64p(&value); break;
  621. default: goto daex;
  622. }
  623. if (put_user (value >> 32, (u32 __user *) sfar) ||
  624. __put_user ((u32)value, (u32 __user *)(sfar + 4)))
  625. goto daex;
  626. } else {
  627. daex:
  628. if (tlb_type == hypervisor)
  629. sun4v_data_access_exception(regs, sfar, sfsr);
  630. else
  631. spitfire_data_access_exception(regs, sfsr, sfar);
  632. goto out;
  633. }
  634. advance(regs);
  635. out:
  636. exception_exit(prev_state);
  637. }