0589-Update-vfpmodule.c.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. From 124370babf0428ec8db2d0ba314105cd9e6ea2c7 Mon Sep 17 00:00:00 2001
  2. From: Claggy3 <stephen.maclagan@hotmail.com>
  3. Date: Sat, 11 Feb 2017 14:00:30 +0000
  4. Subject: [PATCH] Update vfpmodule.c
  5. Christopher Alexander Tobias Schulze - May 2, 2015, 11:57 a.m.
  6. This patch fixes a problem with VFP state save and restore related
  7. to exception handling (panic with message "BUG: unsupported FP
  8. instruction in kernel mode") present on VFP11 floating point units
  9. (as used with ARM1176JZF-S CPUs, e.g. on first generation Raspberry
  10. Pi boards). This patch was developed and discussed on
  11. https://github.com/raspberrypi/linux/issues/859
  12. A precondition to see the crashes is that floating point exception
  13. traps are enabled. In this case, the VFP11 might determine that a FPU
  14. operation needs to trap at a point in time when it is not possible to
  15. signal this to the ARM11 core any more. The VFP11 will then set the
  16. FPEXC.EX bit and store the trapped opcode in FPINST. (In some cases,
  17. a second opcode might have been accepted by the VFP11 before the
  18. exception was detected and could be reported to the ARM11 - in this
  19. case, the VFP11 also sets FPEXC.FP2V and stores the second opcode in
  20. FPINST2.)
  21. If FPEXC.EX is set, the VFP11 will "bounce" the next FPU opcode issued
  22. by the ARM11 CPU, which will be seen by the ARM11 as an undefined opcode
  23. trap. The VFP support code examines the FPEXC.EX and FPEXC.FP2V bits
  24. to decide what actions to take, i.e., whether to emulate the opcodes
  25. found in FPINST and FPINST2, and whether to retry the bounced instruction.
  26. If a user space application has left the VFP11 in this "pending trap"
  27. state, the next FPU opcode issued to the VFP11 might actually be the
  28. VSTMIA operation vfp_save_state() uses to store the FPU registers
  29. to memory (in our test cases, when building the signal stack frame).
  30. In this case, the kernel crashes as described above.
  31. This patch fixes the problem by making sure that vfp_save_state() is
  32. always entered with FPEXC.EX cleared. (The current value of FPEXC has
  33. already been saved, so this does not corrupt the context. Clearing
  34. FPEXC.EX has no effects on FPINST or FPINST2. Also note that many
  35. callers already modify FPEXC by setting FPEXC.EN before invoking
  36. vfp_save_state().)
  37. This patch also addresses a second problem related to FPEXC.EX: After
  38. returning from signal handling, the kernel reloads the VFP context
  39. from the user mode stack. However, the current code explicitly clears
  40. both FPEXC.EX and FPEXC.FP2V during reload. As VFP11 requires these
  41. bits to be preserved, this patch disables clearing them for VFP
  42. implementations belonging to architecture 1. There should be no
  43. negative side effects: the user can set both bits by executing FPU
  44. opcodes anyway, and while user code may now place arbitrary values
  45. into FPINST and FPINST2 (e.g., non-VFP ARM opcodes) the VFP support
  46. code knows which instructions can be emulated, and rejects other
  47. opcodes with "unhandled bounce" messages, so there should be no
  48. security impact from allowing reloading FPEXC.EX and FPEXC.FP2V.
  49. Signed-off-by: Christopher Alexander Tobias Schulze <cat.schulze@alice-dsl.net>
  50. ---
  51. arch/arm/vfp/vfpmodule.c | 27 ++++++++++++++++++++-------
  52. 1 file changed, 20 insertions(+), 7 deletions(-)
  53. --- a/arch/arm/vfp/vfpmodule.c
  54. +++ b/arch/arm/vfp/vfpmodule.c
  55. @@ -183,8 +183,11 @@ static int vfp_notifier(struct notifier_
  56. * case the thread migrates to a different CPU. The
  57. * restoring is done lazily.
  58. */
  59. - if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
  60. - vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  61. + if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu]) {
  62. + /* vfp_save_state oopses on VFP11 if EX bit set */
  63. + fmxr(FPEXC, fpexc & ~FPEXC_EX);
  64. + vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  65. + }
  66. #endif
  67. /*
  68. @@ -467,13 +470,16 @@ static int vfp_pm_suspend(void)
  69. /* if vfp is on, then save state for resumption */
  70. if (fpexc & FPEXC_EN) {
  71. pr_debug("%s: saving vfp state\n", __func__);
  72. + /* vfp_save_state oopses on VFP11 if EX bit set */
  73. + fmxr(FPEXC, fpexc & ~FPEXC_EX);
  74. vfp_save_state(&ti->vfpstate, fpexc);
  75. /* disable, just in case */
  76. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  77. } else if (vfp_current_hw_state[ti->cpu]) {
  78. #ifndef CONFIG_SMP
  79. - fmxr(FPEXC, fpexc | FPEXC_EN);
  80. + /* vfp_save_state oopses on VFP11 if EX bit set */
  81. + fmxr(FPEXC, (fpexc & ~FPEXC_EX) | FPEXC_EN);
  82. vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc);
  83. fmxr(FPEXC, fpexc);
  84. #endif
  85. @@ -536,7 +542,8 @@ void vfp_sync_hwstate(struct thread_info
  86. /*
  87. * Save the last VFP state on this CPU.
  88. */
  89. - fmxr(FPEXC, fpexc | FPEXC_EN);
  90. + /* vfp_save_state oopses on VFP11 if EX bit set */
  91. + fmxr(FPEXC, (fpexc & ~FPEXC_EX) | FPEXC_EN);
  92. vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
  93. fmxr(FPEXC, fpexc);
  94. }
  95. @@ -608,6 +615,7 @@ int vfp_restore_user_hwstate(struct user
  96. struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
  97. unsigned long fpexc;
  98. int err = 0;
  99. + u32 fpsid = fmrx(FPSID);
  100. /* Disable VFP to avoid corrupting the new thread state. */
  101. vfp_flush_hwstate(thread);
  102. @@ -631,8 +639,12 @@ int vfp_restore_user_hwstate(struct user
  103. /* Ensure the VFP is enabled. */
  104. fpexc |= FPEXC_EN;
  105. - /* Ensure FPINST2 is invalid and the exception flag is cleared. */
  106. - fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
  107. + /* Mask FPXEC_EX and FPEXC_FP2V if not required by VFP arch */
  108. + if ((fpsid & FPSID_ARCH_MASK) != (1 << FPSID_ARCH_BIT)) {
  109. + /* Ensure FPINST2 is invalid and the exception flag is cleared. */
  110. + fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
  111. + }
  112. +
  113. hwstate->fpexc = fpexc;
  114. __get_user_error(hwstate->fpinst, &ufp_exc->fpinst, err);
  115. @@ -702,7 +714,8 @@ void kernel_neon_begin(void)
  116. cpu = get_cpu();
  117. fpexc = fmrx(FPEXC) | FPEXC_EN;
  118. - fmxr(FPEXC, fpexc);
  119. + /* vfp_save_state oopses on VFP11 if EX bit set */
  120. + fmxr(FPEXC, fpexc & ~FPEXC_EX);
  121. /*
  122. * Save the userland NEON/VFP state. Under UP,