nlrpowerpc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019, Michael Neuling, IBM Corporation.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpstate.h"
  27. #if MICROPY_NLR_POWERPC
  28. #undef nlr_push
  29. // Saving all ABI non-vol registers here
  30. #ifdef __LP64__
  31. unsigned int nlr_push(nlr_buf_t *nlr) {
  32. __asm__ volatile (
  33. "li 4, 0x4eed ; " // Store canary
  34. "std 4, 0x00(%0) ;"
  35. "std 0, 0x08(%0) ;"
  36. "std 1, 0x10(%0) ;"
  37. "std 2, 0x18(%0) ;"
  38. "std 14, 0x20(%0) ;"
  39. "std 15, 0x28(%0) ;"
  40. "std 16, 0x30(%0) ;"
  41. "std 17, 0x38(%0) ;"
  42. "std 18, 0x40(%0) ;"
  43. "std 19, 0x48(%0) ;"
  44. "std 20, 0x50(%0) ;"
  45. "std 21, 0x58(%0) ;"
  46. "std 22, 0x60(%0) ;"
  47. "std 23, 0x68(%0) ;"
  48. "std 24, 0x70(%0) ;"
  49. "std 25, 0x78(%0) ;"
  50. "std 26, 0x80(%0) ;"
  51. "std 27, 0x88(%0) ;"
  52. "std 28, 0x90(%0) ;"
  53. "std 29, 0x98(%0) ;"
  54. "std 30, 0xA0(%0) ;"
  55. "std 31, 0xA8(%0) ;"
  56. "mfcr 4 ; "
  57. "std 4, 0xB0(%0) ;"
  58. "mflr 4 ;"
  59. "std 4, 0xB8(%0) ;"
  60. "li 4, nlr_push_tail@l ;"
  61. "oris 4, 4, nlr_push_tail@h ;"
  62. "mtctr 4 ;"
  63. "mr 3, %1 ; "
  64. "bctr ;"
  65. :
  66. : "r" (&nlr->regs), "r" (nlr)
  67. :
  68. );
  69. return 0;
  70. }
  71. NORETURN void nlr_jump(void *val) {
  72. MP_NLR_JUMP_HEAD(val, top)
  73. __asm__ volatile (
  74. "ld 3, 0x0(%0) ;"
  75. "cmpdi 3, 0x4eed ; " // Check canary
  76. "bne . ; "
  77. "ld 0, 0x08(%0) ;"
  78. "ld 1, 0x10(%0) ;"
  79. "ld 2, 0x18(%0) ;"
  80. "ld 14, 0x20(%0) ;"
  81. "ld 15, 0x28(%0) ;"
  82. "ld 16, 0x30(%0) ;"
  83. "ld 17, 0x38(%0) ;"
  84. "ld 18, 0x40(%0) ;"
  85. "ld 19, 0x48(%0) ;"
  86. "ld 20, 0x50(%0) ;"
  87. "ld 21, 0x58(%0) ;"
  88. "ld 22, 0x60(%0) ;"
  89. "ld 23, 0x68(%0) ;"
  90. "ld 24, 0x70(%0) ;"
  91. "ld 25, 0x78(%0) ;"
  92. "ld 26, 0x80(%0) ;"
  93. "ld 27, 0x88(%0) ;"
  94. "ld 28, 0x90(%0) ;"
  95. "ld 29, 0x98(%0) ;"
  96. "ld 30, 0xA0(%0) ;"
  97. "ld 31, 0xA8(%0) ;"
  98. "ld 3, 0xB0(%0) ;"
  99. "mtcr 3 ;"
  100. "ld 3, 0xB8(%0) ;"
  101. "mtlr 3 ; "
  102. "li 3, 1;"
  103. "blr ;"
  104. :
  105. : "r" (&top->regs)
  106. : "memory"
  107. );
  108. MP_UNREACHABLE;
  109. }
  110. #else
  111. // Saving all ABI non-vol registers here
  112. unsigned int nlr_push(nlr_buf_t *nlr) {
  113. __asm__ volatile (
  114. "li 4, 0x4eed ; " // Store canary
  115. "stw 4, 0x00(%0) ;"
  116. "stw 0, 0x04(%0) ;"
  117. "stw 1, 0x08(%0) ;"
  118. "stw 2, 0x0c(%0) ;"
  119. "stw 14, 0x10(%0) ;"
  120. "stw 15, 0x14(%0) ;"
  121. "stw 16, 0x18(%0) ;"
  122. "stw 17, 0x1c(%0) ;"
  123. "stw 18, 0x20(%0) ;"
  124. "stw 19, 0x24(%0) ;"
  125. "stw 20, 0x28(%0) ;"
  126. "stw 21, 0x2c(%0) ;"
  127. "stw 22, 0x30(%0) ;"
  128. "stw 23, 0x34(%0) ;"
  129. "stw 24, 0x38(%0) ;"
  130. "stw 25, 0x3c(%0) ;"
  131. "stw 26, 0x40(%0) ;"
  132. "stw 27, 0x44(%0) ;"
  133. "stw 28, 0x48(%0) ;"
  134. "stw 29, 0x4c(%0) ;"
  135. "stw 30, 0x50(%0) ;"
  136. "stw 31, 0x54(%0) ;"
  137. "mfcr 4 ; "
  138. "stw 4, 0x58(%0) ;"
  139. "mflr 4 ;"
  140. "stw 4, 0x5c(%0) ;"
  141. "li 4, nlr_push_tail@l ;"
  142. "oris 4, 4, nlr_push_tail@h ;"
  143. "mtctr 4 ;"
  144. "mr 3, %1 ; "
  145. "bctr ;"
  146. :
  147. : "r" (&nlr->regs), "r" (nlr)
  148. :
  149. );
  150. return 0;
  151. }
  152. NORETURN void nlr_jump(void *val) {
  153. MP_NLR_JUMP_HEAD(val, top)
  154. __asm__ volatile (
  155. "l 3, 0x0(%0) ;"
  156. "cmpdi 3, 0x4eed ; " // Check canary
  157. "bne . ; "
  158. "l 0, 0x04(%0) ;"
  159. "l 1, 0x08(%0) ;"
  160. "l 2, 0x0c(%0) ;"
  161. "l 14, 0x10(%0) ;"
  162. "l 15, 0x14(%0) ;"
  163. "l 16, 0x18(%0) ;"
  164. "l 17, 0x1c(%0) ;"
  165. "l 18, 0x20(%0) ;"
  166. "l 19, 0x24(%0) ;"
  167. "l 20, 0x28(%0) ;"
  168. "l 21, 0x2c(%0) ;"
  169. "l 22, 0x30(%0) ;"
  170. "l 23, 0x34(%0) ;"
  171. "l 24, 0x38(%0) ;"
  172. "l 25, 0x3c(%0) ;"
  173. "l 26, 0x40(%0) ;"
  174. "l 27, 0x44(%0) ;"
  175. "l 28, 0x48(%0) ;"
  176. "l 29, 0x4c(%0) ;"
  177. "l 30, 0x50(%0) ;"
  178. "l 31, 0x54(%0) ;"
  179. "l 3, 0x58(%0) ;"
  180. "mtcr 3 ;"
  181. "l 3, 0x5c(%0) ;"
  182. "mtlr 3 ; "
  183. "li 3, 1;"
  184. "blr ;"
  185. :
  186. : "r" (&top->regs)
  187. : "memory"
  188. );
  189. MP_UNREACHABLE;
  190. }
  191. #endif // __LP64__
  192. #endif // MICROPY_NLR_POWERPC