entry.S 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. NetWinder Floating Point Emulator
  4. (c) Rebel.COM, 1998
  5. (c) 1998, 1999 Philip Blundell
  6. Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
  7. */
  8. #include <linux/linkage.h>
  9. #include <asm/assembler.h>
  10. #include <asm/opcodes.h>
  11. /* This is the kernel's entry point into the floating point emulator.
  12. It is called from the kernel with code similar to this:
  13. sub r4, r5, #4
  14. ldrt r0, [r4] @ r0 = instruction
  15. adrsvc al, r9, ret_from_exception @ r9 = normal FP return
  16. adrsvc al, lr, fpundefinstr @ lr = undefined instr return
  17. get_current_task r10
  18. mov r8, #1
  19. strb r8, [r10, #TSK_USED_MATH] @ set current->used_math
  20. add r10, r10, #TSS_FPESAVE @ r10 = workspace
  21. ldr r4, .LC2
  22. ldr pc, [r4] @ Call FP emulator entry point
  23. The kernel expects the emulator to return via one of two possible
  24. points of return it passes to the emulator. The emulator, if
  25. successful in its emulation, jumps to ret_from_exception (passed in
  26. r9) and the kernel takes care of returning control from the trap to
  27. the user code. If the emulator is unable to emulate the instruction,
  28. it returns via _fpundefinstr (passed via lr) and the kernel halts the
  29. user program with a core dump.
  30. On entry to the emulator r10 points to an area of private FP workspace
  31. reserved in the thread structure for this process. This is where the
  32. emulator saves its registers across calls. The first word of this area
  33. is used as a flag to detect the first time a process uses floating point,
  34. so that the emulator startup cost can be avoided for tasks that don't
  35. want it.
  36. This routine does three things:
  37. 1) The kernel has created a struct pt_regs on the stack and saved the
  38. user registers into it. See /usr/include/asm/proc/ptrace.h for details.
  39. 2) It calls EmulateAll to emulate a floating point instruction.
  40. EmulateAll returns 1 if the emulation was successful, or 0 if not.
  41. 3) If an instruction has been emulated successfully, it looks ahead at
  42. the next instruction. If it is a floating point instruction, it
  43. executes the instruction, without returning to user space. In this
  44. way it repeatedly looks ahead and executes floating point instructions
  45. until it encounters a non floating point instruction, at which time it
  46. returns via _fpreturn.
  47. This is done to reduce the effect of the trap overhead on each
  48. floating point instructions. GCC attempts to group floating point
  49. instructions to allow the emulator to spread the cost of the trap over
  50. several floating point instructions. */
  51. #include <asm/asm-offsets.h>
  52. .globl nwfpe_enter
  53. nwfpe_enter:
  54. mov r4, lr @ save the failure-return addresses
  55. mov sl, sp @ we access the registers via 'sl'
  56. ldr r5, [sp, #S_PC] @ get contents of PC;
  57. mov r6, r0 @ save the opcode
  58. emulate:
  59. ldr r1, [sp, #S_PSR] @ fetch the PSR
  60. bl arm_check_condition @ check the condition
  61. cmp r0, #ARM_OPCODE_CONDTEST_PASS @ condition passed?
  62. @ if condition code failed to match, next insn
  63. bne next @ get the next instruction;
  64. mov r0, r6 @ prepare for EmulateAll()
  65. bl EmulateAll @ emulate the instruction
  66. cmp r0, #0 @ was emulation successful
  67. reteq r4 @ no, return failure
  68. next:
  69. uaccess_enable r3
  70. .Lx1: ldrt r6, [r5], #4 @ get the next instruction and
  71. @ increment PC
  72. uaccess_disable r3
  73. and r2, r6, #0x0F000000 @ test for FP insns
  74. teq r2, #0x0C000000
  75. teqne r2, #0x0D000000
  76. teqne r2, #0x0E000000
  77. retne r9 @ return ok if not a fp insn
  78. str r5, [sp, #S_PC] @ update PC copy in regs
  79. mov r0, r6 @ save a copy
  80. b emulate @ check condition and emulate
  81. @ We need to be prepared for the instructions at .Lx1 and .Lx2
  82. @ to fault. Emit the appropriate exception gunk to fix things up.
  83. @ ??? For some reason, faults can happen at .Lx2 even with a
  84. @ plain LDR instruction. Weird, but it seems harmless.
  85. .pushsection .text.fixup,"ax"
  86. .align 2
  87. .Lrep: str r4, [sp, #S_PC] @ retry current instruction
  88. .Lfix: ret r9 @ let the user eat segfaults
  89. .popsection
  90. .pushsection __ex_table,"a"
  91. .align 3
  92. .long .Lx1, .Lfix
  93. .popsection
  94. @
  95. @ Check whether the instruction is a co-processor instruction.
  96. @ If yes, we need to call the relevant co-processor handler.
  97. @ Only FPE instructions are dispatched here, everything else
  98. @ is handled by undef hooks.
  99. @
  100. @ Emulators may wish to make use of the following registers:
  101. @ r4 = PC value to resume execution after successful emulation
  102. @ r9 = normal "successful" return address
  103. @ lr = unrecognised instruction return address
  104. @ IRQs enabled, FIQs enabled.
  105. @
  106. ENTRY(call_fpe)
  107. mov r2, r4
  108. sub r4, r4, #4 @ ARM instruction at user PC - 4
  109. USERL( .Lrep, ldrt r0, [r4]) @ load opcode from user space
  110. ARM_BE8(rev r0, r0) @ little endian instruction
  111. uaccess_disable ip
  112. get_thread_info r10 @ get current thread
  113. tst r0, #0x08000000 @ only CDP/CPRT/LDC/STC have bit 27
  114. reteq lr
  115. and r8, r0, #0x00000f00 @ mask out CP number
  116. #ifdef CONFIG_IWMMXT
  117. @ Test if we need to give access to iWMMXt coprocessors
  118. ldr r5, [r10, #TI_FLAGS]
  119. rsbs r7, r8, #(1 << 8) @ CP 0 or 1 only
  120. movscs r7, r5, lsr #(TIF_USING_IWMMXT + 1)
  121. movcs r0, sp @ pass struct pt_regs
  122. bcs iwmmxt_task_enable
  123. #endif
  124. add pc, pc, r8, lsr #6
  125. nop
  126. ret lr @ CP#0
  127. b do_fpe @ CP#1 (FPE)
  128. b do_fpe @ CP#2 (FPE)
  129. ret lr @ CP#3
  130. ret lr @ CP#4
  131. ret lr @ CP#5
  132. ret lr @ CP#6
  133. ret lr @ CP#7
  134. ret lr @ CP#8
  135. ret lr @ CP#9
  136. ret lr @ CP#10 (VFP)
  137. ret lr @ CP#11 (VFP)
  138. ret lr @ CP#12
  139. ret lr @ CP#13
  140. ret lr @ CP#14 (Debug)
  141. ret lr @ CP#15 (Control)
  142. do_fpe:
  143. add r10, r10, #TI_FPSTATE @ r10 = workspace
  144. ldr_va pc, fp_enter, tmp=r4 @ Call FP module USR entry point
  145. @
  146. @ The FP module is called with these registers set:
  147. @ r0 = instruction
  148. @ r2 = PC+4
  149. @ r9 = normal "successful" return address
  150. @ r10 = FP workspace
  151. @ lr = unrecognised FP instruction return address
  152. @
  153. .pushsection .data
  154. .align 2
  155. ENTRY(fp_enter)
  156. .word no_fp
  157. .popsection
  158. no_fp:
  159. ret lr
  160. ENDPROC(no_fp)