Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ARM: vfp: Reimplement VFP exception entry in C code

En/disabling softirqs from asm code turned out to be trickier than
expected, so vfp_support_entry now returns by tail calling
__local_enable_bh_ip() and passing the same arguments that a C call to
local_bh_enable() would pass. However, this is slightly hacky, as we
don't want to carry our own implementation of local_bh_enable().

So let's bite the bullet, and get rid of the asm logic in
vfp_support_entry that reasons about whether or not to save and/or
reload the VFP state, and about whether or not an FP exception is
pending, and only keep the VFP loading logic as a function that is
callable from C.

Replicate the removed logic in vfp_entry(), and use the exact same
reasoning as in the asm code. To emphasize the correspondence, retain
some of the asm comments in the C version as well.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Linus Walleij <linus.walleij@linaro.org>

+124 -216
+6 -6
arch/arm/vfp/entry.S
··· 22 22 @ IRQs enabled. 23 23 @ 24 24 ENTRY(do_vfp) 25 - mov r1, r10 26 - str lr, [sp, #-8]! 27 - add r3, sp, #4 28 - str r9, [r3] 29 - bl vfp_entry 30 - ldr pc, [sp], #8 25 + mov r1, r0 @ pass trigger opcode via R1 26 + mov r0, sp @ pass struct pt_regs via R0 27 + bl vfp_support_entry @ dispatch the VFP exception 28 + cmp r0, #0 @ handled successfully? 29 + reteq r9 @ then use R9 as return address 30 + ret lr @ pass to undef handler 31 31 ENDPROC(do_vfp)
+1
arch/arm/vfp/vfp.h
··· 375 375 }; 376 376 377 377 asmlinkage void vfp_save_state(void *location, u32 fpexc); 378 + asmlinkage u32 vfp_load_state(const void *location);
+14 -190
arch/arm/vfp/vfphw.S
··· 4 4 * 5 5 * Copyright (C) 2004 ARM Limited. 6 6 * Written by Deep Blue Solutions Limited. 7 - * 8 - * This code is called from the kernel's undefined instruction trap. 9 - * r1 holds the thread_info pointer 10 - * r3 holds the return address for successful handling. 11 - * lr holds the return address for unrecognised instructions. 12 - * sp points to a struct pt_regs (as defined in include/asm/proc/ptrace.h) 13 7 */ 14 8 #include <linux/init.h> 15 9 #include <linux/linkage.h> ··· 12 18 #include <linux/kern_levels.h> 13 19 #include <asm/assembler.h> 14 20 #include <asm/asm-offsets.h> 15 - 16 - .macro DBGSTR, str 17 - #ifdef DEBUG 18 - stmfd sp!, {r0-r3, ip, lr} 19 - ldr r0, =1f 20 - bl _printk 21 - ldmfd sp!, {r0-r3, ip, lr} 22 - 23 - .pushsection .rodata, "a" 24 - 1: .ascii KERN_DEBUG "VFP: \str\n" 25 - .byte 0 26 - .previous 27 - #endif 28 - .endm 29 21 30 22 .macro DBGSTR1, str, arg 31 23 #ifdef DEBUG ··· 28 48 #endif 29 49 .endm 30 50 31 - .macro DBGSTR3, str, arg1, arg2, arg3 32 - #ifdef DEBUG 33 - stmfd sp!, {r0-r3, ip, lr} 34 - mov r3, \arg3 35 - mov r2, \arg2 36 - mov r1, \arg1 37 - ldr r0, =1f 38 - bl _printk 39 - ldmfd sp!, {r0-r3, ip, lr} 40 - 41 - .pushsection .rodata, "a" 42 - 1: .ascii KERN_DEBUG "VFP: \str\n" 43 - .byte 0 44 - .previous 45 - #endif 46 - .endm 47 - 48 - 49 - @ VFP hardware support entry point. 50 - @ 51 - @ r0 = instruction opcode (32-bit ARM or two 16-bit Thumb) 52 - @ r1 = thread_info pointer 53 - @ r2 = PC value to resume execution after successful emulation 54 - @ r3 = normal "successful" return address 55 - @ lr = unrecognised instruction return address 56 - @ IRQs enabled. 57 - ENTRY(vfp_support_entry) 58 - ldr r11, [r1, #TI_CPU] @ CPU number 59 - add r10, r1, #TI_VFPSTATE @ r10 = workspace 60 - 61 - DBGSTR3 "instr %08x pc %08x state %p", r0, r2, r10 62 - 63 - .fpu vfpv2 64 - VFPFMRX r1, FPEXC @ Is the VFP enabled? 65 - DBGSTR1 "fpexc %08x", r1 66 - tst r1, #FPEXC_EN 67 - bne look_for_VFP_exceptions @ VFP is already enabled 68 - 69 - DBGSTR1 "enable %x", r10 70 - ldr r9, vfp_current_hw_state_address 71 - orr r1, r1, #FPEXC_EN @ user FPEXC has the enable bit set 72 - ldr r4, [r9, r11, lsl #2] @ vfp_current_hw_state pointer 73 - bic r5, r1, #FPEXC_EX @ make sure exceptions are disabled 74 - cmp r4, r10 @ this thread owns the hw context? 75 - #ifndef CONFIG_SMP 76 - @ For UP, checking that this thread owns the hw context is 77 - @ sufficient to determine that the hardware state is valid. 78 - beq vfp_hw_state_valid 79 - 80 - @ On UP, we lazily save the VFP context. As a different 81 - @ thread wants ownership of the VFP hardware, save the old 82 - @ state if there was a previous (valid) owner. 83 - 84 - VFPFMXR FPEXC, r5 @ enable VFP, disable any pending 85 - @ exceptions, so we can get at the 86 - @ rest of it 87 - 88 - DBGSTR1 "save old state %p", r4 89 - cmp r4, #0 @ if the vfp_current_hw_state is NULL 90 - beq vfp_reload_hw @ then the hw state needs reloading 91 - VFPFSTMIA r4, r5 @ save the working registers 92 - VFPFMRX r5, FPSCR @ current status 93 - tst r1, #FPEXC_EX @ is there additional state to save? 94 - beq 1f 95 - VFPFMRX r6, FPINST @ FPINST (only if FPEXC.EX is set) 96 - tst r1, #FPEXC_FP2V @ is there an FPINST2 to read? 97 - beq 1f 98 - VFPFMRX r8, FPINST2 @ FPINST2 if needed (and present) 99 - 1: 100 - stmia r4, {r1, r5, r6, r8} @ save FPEXC, FPSCR, FPINST, FPINST2 101 - vfp_reload_hw: 102 - 103 - #else 104 - @ For SMP, if this thread does not own the hw context, then we 105 - @ need to reload it. No need to save the old state as on SMP, 106 - @ we always save the state when we switch away from a thread. 107 - bne vfp_reload_hw 108 - 109 - @ This thread has ownership of the current hardware context. 110 - @ However, it may have been migrated to another CPU, in which 111 - @ case the saved state is newer than the hardware context. 112 - @ Check this by looking at the CPU number which the state was 113 - @ last loaded onto. 114 - ldr ip, [r10, #VFP_CPU] 115 - teq ip, r11 116 - beq vfp_hw_state_valid 117 - 118 - vfp_reload_hw: 119 - @ We're loading this threads state into the VFP hardware. Update 120 - @ the CPU number which contains the most up to date VFP context. 121 - str r11, [r10, #VFP_CPU] 122 - 123 - VFPFMXR FPEXC, r5 @ enable VFP, disable any pending 124 - @ exceptions, so we can get at the 125 - @ rest of it 126 - #endif 127 - 128 - DBGSTR1 "load state %p", r10 129 - str r10, [r9, r11, lsl #2] @ update the vfp_current_hw_state pointer 51 + ENTRY(vfp_load_state) 52 + @ Load the current VFP state 53 + @ r0 - load location 54 + @ returns FPEXC 55 + DBGSTR1 "load VFP state %p", r0 130 56 @ Load the saved state back into the VFP 131 - VFPFLDMIA r10, r5 @ reload the working registers while 57 + VFPFLDMIA r0, r1 @ reload the working registers while 132 58 @ FPEXC is in a safe state 133 - ldmia r10, {r1, r5, r6, r8} @ load FPEXC, FPSCR, FPINST, FPINST2 134 - tst r1, #FPEXC_EX @ is there additional state to restore? 59 + ldmia r0, {r0-r3} @ load FPEXC, FPSCR, FPINST, FPINST2 60 + tst r0, #FPEXC_EX @ is there additional state to restore? 135 61 beq 1f 136 - VFPFMXR FPINST, r6 @ restore FPINST (only if FPEXC.EX is set) 137 - tst r1, #FPEXC_FP2V @ is there an FPINST2 to write? 62 + VFPFMXR FPINST, r2 @ restore FPINST (only if FPEXC.EX is set) 63 + tst r0, #FPEXC_FP2V @ is there an FPINST2 to write? 138 64 beq 1f 139 - VFPFMXR FPINST2, r8 @ FPINST2 if needed (and present) 65 + VFPFMXR FPINST2, r3 @ FPINST2 if needed (and present) 140 66 1: 141 - VFPFMXR FPSCR, r5 @ restore status 142 - 143 - @ The context stored in the VFP hardware is up to date with this thread 144 - vfp_hw_state_valid: 145 - tst r1, #FPEXC_EX 146 - bne process_exception @ might as well handle the pending 147 - @ exception before retrying branch 148 - @ out before setting an FPEXC that 149 - @ stops us reading stuff 150 - VFPFMXR FPEXC, r1 @ Restore FPEXC last 151 - mov sp, r3 @ we think we have handled things 152 - pop {lr} 153 - sub r2, r2, #4 @ Retry current instruction - if Thumb 154 - str r2, [sp, #S_PC] @ mode it's two 16-bit instructions, 155 - @ else it's one 32-bit instruction, so 156 - @ always subtract 4 from the following 157 - @ instruction address. 158 - 159 - local_bh_enable_and_ret: 160 - adr r0, . 161 - mov r1, #SOFTIRQ_DISABLE_OFFSET 162 - b __local_bh_enable_ip @ tail call 163 - 164 - look_for_VFP_exceptions: 165 - @ Check for synchronous or asynchronous exception 166 - tst r1, #FPEXC_EX | FPEXC_DEX 167 - bne process_exception 168 - @ On some implementations of the VFP subarch 1, setting FPSCR.IXE 169 - @ causes all the CDP instructions to be bounced synchronously without 170 - @ setting the FPEXC.EX bit 171 - VFPFMRX r5, FPSCR 172 - tst r5, #FPSCR_IXE 173 - bne process_exception 174 - 175 - tst r5, #FPSCR_LENGTH_MASK 176 - beq skip 177 - orr r1, r1, #FPEXC_DEX 178 - b process_exception 179 - skip: 180 - 181 - @ Fall into hand on to next handler - appropriate coproc instr 182 - @ not recognised by VFP 183 - 184 - DBGSTR "not VFP" 185 - b local_bh_enable_and_ret 186 - 187 - process_exception: 188 - DBGSTR "bounce" 189 - mov sp, r3 @ setup for a return to the user code. 190 - pop {lr} 191 - mov r2, sp @ nothing stacked - regdump is at TOS 192 - 193 - @ Now call the C code to package up the bounce to the support code 194 - @ r0 holds the trigger instruction 195 - @ r1 holds the FPEXC value 196 - @ r2 pointer to register dump 197 - b VFP_bounce @ we have handled this - the support 198 - @ code will raise an exception if 199 - @ required. If not, the user code will 200 - @ retry the faulted instruction 201 - ENDPROC(vfp_support_entry) 67 + VFPFMXR FPSCR, r1 @ restore status 68 + ret lr 69 + ENDPROC(vfp_load_state) 202 70 203 71 ENTRY(vfp_save_state) 204 72 @ Save the current VFP state ··· 65 237 stmia r0, {r1, r2, r3, r12} @ save FPEXC, FPSCR, FPINST, FPINST2 66 238 ret lr 67 239 ENDPROC(vfp_save_state) 68 - 69 - .align 70 - vfp_current_hw_state_address: 71 - .word vfp_current_hw_state 72 240 73 241 .macro tbl_branch, base, tmp, shift 74 242 #ifdef CONFIG_THUMB2_KERNEL
+103 -20
arch/arm/vfp/vfpmodule.c
··· 30 30 #include "vfpinstr.h" 31 31 #include "vfp.h" 32 32 33 - /* 34 - * Our undef handlers (in entry.S) 35 - */ 36 - asmlinkage void vfp_support_entry(u32, void *, u32, u32); 37 - 38 33 static bool have_vfp __ro_after_init; 39 34 40 35 /* ··· 320 325 /* 321 326 * Package up a bounce condition. 322 327 */ 323 - void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs) 328 + static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs) 324 329 { 325 330 u32 fpscr, orig_fpscr, fpsid, exceptions; 326 331 ··· 369 374 * on VFP subarch 1. 370 375 */ 371 376 vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs); 372 - goto exit; 377 + return; 373 378 } 374 379 375 380 /* ··· 400 405 * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1. 401 406 */ 402 407 if ((fpexc & (FPEXC_EX | FPEXC_FP2V)) != (FPEXC_EX | FPEXC_FP2V)) 403 - goto exit; 408 + return; 404 409 405 410 /* 406 411 * The barrier() here prevents fpinst2 being read ··· 413 418 exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs); 414 419 if (exceptions) 415 420 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs); 416 - exit: 417 - local_bh_enable(); 418 421 } 419 422 420 423 static void vfp_enable(void *unused) ··· 642 649 } 643 650 644 651 /* 645 - * Entered with: 652 + * vfp_support_entry - Handle VFP exception from user mode 646 653 * 647 - * r0 = instruction opcode (32-bit ARM or two 16-bit Thumb) 648 - * r1 = thread_info pointer 649 - * r2 = PC value to resume execution after successful emulation 650 - * r3 = normal "successful" return address 651 - * lr = unrecognised instruction return address 654 + * @regs: pt_regs structure holding the register state at exception entry 655 + * @trigger: The opcode of the instruction that triggered the exception 656 + * 657 + * Returns 0 if the exception was handled, or an error code otherwise. 652 658 */ 653 - asmlinkage void vfp_entry(u32 trigger, struct thread_info *ti, u32 resume_pc, 654 - u32 resume_return_address) 659 + asmlinkage int vfp_support_entry(struct pt_regs *regs, u32 trigger) 655 660 { 661 + struct thread_info *ti = current_thread_info(); 662 + u32 fpexc; 663 + 656 664 if (unlikely(!have_vfp)) 657 - return; 665 + return -ENODEV; 658 666 659 667 local_bh_disable(); 660 - vfp_support_entry(trigger, ti, resume_pc, resume_return_address); 668 + fpexc = fmrx(FPEXC); 669 + 670 + /* 671 + * If the VFP unit was not enabled yet, we have to check whether the 672 + * VFP state in the CPU's registers is the most recent VFP state 673 + * associated with the process. On UP systems, we don't save the VFP 674 + * state eagerly on a context switch, so we may need to save the 675 + * VFP state to memory first, as it may belong to another process. 676 + */ 677 + if (!(fpexc & FPEXC_EN)) { 678 + /* 679 + * Enable the VFP unit but mask the FP exception flag for the 680 + * time being, so we can access all the registers. 681 + */ 682 + fpexc |= FPEXC_EN; 683 + fmxr(FPEXC, fpexc & ~FPEXC_EX); 684 + 685 + /* 686 + * Check whether or not the VFP state in the CPU's registers is 687 + * the most recent VFP state associated with this task. On SMP, 688 + * migration may result in multiple CPUs holding VFP states 689 + * that belong to the same task, but only the most recent one 690 + * is valid. 691 + */ 692 + if (!vfp_state_in_hw(ti->cpu, ti)) { 693 + if (!IS_ENABLED(CONFIG_SMP) && 694 + vfp_current_hw_state[ti->cpu] != NULL) { 695 + /* 696 + * This CPU is currently holding the most 697 + * recent VFP state associated with another 698 + * task, and we must save that to memory first. 699 + */ 700 + vfp_save_state(vfp_current_hw_state[ti->cpu], 701 + fpexc); 702 + } 703 + 704 + /* 705 + * We can now proceed with loading the task's VFP state 706 + * from memory into the CPU registers. 707 + */ 708 + fpexc = vfp_load_state(&ti->vfpstate); 709 + vfp_current_hw_state[ti->cpu] = &ti->vfpstate; 710 + #ifdef CONFIG_SMP 711 + /* 712 + * Record that this CPU is now the one holding the most 713 + * recent VFP state of the task. 714 + */ 715 + ti->vfpstate.hard.cpu = ti->cpu; 716 + #endif 717 + } 718 + 719 + if (fpexc & FPEXC_EX) 720 + /* 721 + * Might as well handle the pending exception before 722 + * retrying branch out before setting an FPEXC that 723 + * stops us reading stuff. 724 + */ 725 + goto bounce; 726 + 727 + /* 728 + * No FP exception is pending: just enable the VFP and 729 + * replay the instruction that trapped. 730 + */ 731 + fmxr(FPEXC, fpexc); 732 + regs->ARM_pc -= 4; 733 + } else { 734 + /* Check for synchronous or asynchronous exceptions */ 735 + if (!(fpexc & (FPEXC_EX | FPEXC_DEX))) { 736 + u32 fpscr = fmrx(FPSCR); 737 + 738 + /* 739 + * On some implementations of the VFP subarch 1, 740 + * setting FPSCR.IXE causes all the CDP instructions to 741 + * be bounced synchronously without setting the 742 + * FPEXC.EX bit 743 + */ 744 + if (!(fpscr & FPSCR_IXE)) { 745 + if (!(fpscr & FPSCR_LENGTH_MASK)) { 746 + pr_debug("not VFP\n"); 747 + local_bh_enable(); 748 + return -ENOEXEC; 749 + } 750 + fpexc |= FPEXC_DEX; 751 + } 752 + } 753 + bounce: VFP_bounce(trigger, fpexc, regs); 754 + } 755 + 756 + local_bh_enable(); 757 + return 0; 661 758 } 662 759 663 760 #ifdef CONFIG_KERNEL_MODE_NEON