setjmp.S 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2018 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/symbol.h>
  19. #include <grub/dl.h>
  20. .file "setjmp.S"
  21. GRUB_MOD_LICENSE "GPLv3+"
  22. .text
  23. #if __riscv_xlen == 64
  24. #define STORE_IDX(reg, idx) sd reg, (idx*8)(a0)
  25. #define LOAD_IDX(reg, idx) ld reg, (idx*8)(a0)
  26. #else
  27. #define STORE_IDX(reg, idx) sw reg, (idx*4)(a0)
  28. #define LOAD_IDX(reg, idx) lw reg, (idx*4)(a0)
  29. #endif
  30. /*
  31. * int grub_setjmp (grub_jmp_buf env)
  32. */
  33. FUNCTION(grub_setjmp)
  34. /* Preserve all callee-saved registers and the SP */
  35. STORE_IDX(s0, 0)
  36. STORE_IDX(s1, 1)
  37. STORE_IDX(s2, 2)
  38. STORE_IDX(s3, 3)
  39. STORE_IDX(s4, 4)
  40. STORE_IDX(s5, 5)
  41. STORE_IDX(s6, 6)
  42. STORE_IDX(s7, 7)
  43. STORE_IDX(s8, 8)
  44. STORE_IDX(s9, 9)
  45. STORE_IDX(s10, 10)
  46. STORE_IDX(s11, 11)
  47. STORE_IDX(ra, 12)
  48. STORE_IDX(sp, 13)
  49. li a0, 0
  50. ret
  51. /*
  52. * int grub_longjmp (grub_jmp_buf env, int val)
  53. */
  54. FUNCTION(grub_longjmp)
  55. LOAD_IDX(s0, 0)
  56. LOAD_IDX(s1, 1)
  57. LOAD_IDX(s2, 2)
  58. LOAD_IDX(s3, 3)
  59. LOAD_IDX(s4, 4)
  60. LOAD_IDX(s5, 5)
  61. LOAD_IDX(s6, 6)
  62. LOAD_IDX(s7, 7)
  63. LOAD_IDX(s8, 8)
  64. LOAD_IDX(s9, 9)
  65. LOAD_IDX(s10, 10)
  66. LOAD_IDX(s11, 11)
  67. LOAD_IDX(ra, 12)
  68. LOAD_IDX(sp, 13)
  69. /* Move the return value in place, but return 1 if passed 0. */
  70. beq a1, zero, longjmp_1
  71. mv a0, a1
  72. ret
  73. longjmp_1:
  74. li a0, 1
  75. ret