stubs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <common.h>
  2. #include <exports.h>
  3. #include <linux/compiler.h>
  4. #define FO(x) offsetof(struct jt_funcs, x)
  5. #if defined(CONFIG_X86)
  6. /*
  7. * x86 does not have a dedicated register to store the pointer to
  8. * the global_data. Thus the jump table address is stored in a
  9. * global variable, but such approach does not allow for execution
  10. * from flash memory. The global_data address is passed as argv[-1]
  11. * to the application program.
  12. */
  13. static struct jt_funcs *jt;
  14. gd_t *global_data;
  15. #define EXPORT_FUNC(f, a, x, ...) \
  16. asm volatile ( \
  17. " .globl " #x "\n" \
  18. #x ":\n" \
  19. " movl %0, %%eax\n" \
  20. " movl jt, %%ecx\n" \
  21. " jmp *(%%ecx, %%eax)\n" \
  22. : : "i"(FO(x)) : "eax", "ecx");
  23. #elif defined(CONFIG_PPC)
  24. /*
  25. * r2 holds the pointer to the global_data, r11 is a call-clobbered
  26. * register
  27. */
  28. #define EXPORT_FUNC(f, a, x, ...) \
  29. asm volatile ( \
  30. " .globl " #x "\n" \
  31. #x ":\n" \
  32. " lwz %%r11, %0(%%r2)\n" \
  33. " lwz %%r11, %1(%%r11)\n" \
  34. " mtctr %%r11\n" \
  35. " bctr\n" \
  36. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "r11");
  37. #elif defined(CONFIG_ARM)
  38. #ifdef CONFIG_ARM64
  39. /*
  40. * x18 holds the pointer to the global_data, x9 is a call-clobbered
  41. * register
  42. */
  43. #define EXPORT_FUNC(f, a, x, ...) \
  44. asm volatile ( \
  45. " .globl " #x "\n" \
  46. #x ":\n" \
  47. " ldr x9, [x18, %0]\n" \
  48. " ldr x9, [x9, %1]\n" \
  49. " br x9\n" \
  50. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "x9");
  51. #else
  52. /*
  53. * r9 holds the pointer to the global_data, ip is a call-clobbered
  54. * register
  55. */
  56. #define EXPORT_FUNC(f, a, x, ...) \
  57. asm volatile ( \
  58. " .globl " #x "\n" \
  59. #x ":\n" \
  60. " ldr ip, [r9, %0]\n" \
  61. " ldr pc, [ip, %1]\n" \
  62. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "ip");
  63. #endif
  64. #elif defined(CONFIG_MIPS)
  65. #ifdef CONFIG_CPU_MIPS64
  66. /*
  67. * k0 ($26) holds the pointer to the global_data; t9 ($25) is a call-
  68. * clobbered register that is also used to set gp ($26). Note that the
  69. * jr instruction also executes the instruction immediately following
  70. * it; however, GCC/mips generates an additional `nop' after each asm
  71. * statement
  72. */
  73. #define EXPORT_FUNC(f, a, x, ...) \
  74. asm volatile ( \
  75. " .globl " #x "\n" \
  76. #x ":\n" \
  77. " ld $25, %0($26)\n" \
  78. " ld $25, %1($25)\n" \
  79. " jr $25\n" \
  80. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "t9");
  81. #else
  82. /*
  83. * k0 ($26) holds the pointer to the global_data; t9 ($25) is a call-
  84. * clobbered register that is also used to set gp ($26). Note that the
  85. * jr instruction also executes the instruction immediately following
  86. * it; however, GCC/mips generates an additional `nop' after each asm
  87. * statement
  88. */
  89. #define EXPORT_FUNC(f, a, x, ...) \
  90. asm volatile ( \
  91. " .globl " #x "\n" \
  92. #x ":\n" \
  93. " lw $25, %0($26)\n" \
  94. " lw $25, %1($25)\n" \
  95. " jr $25\n" \
  96. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "t9");
  97. #endif
  98. #elif defined(CONFIG_NIOS2)
  99. /*
  100. * gp holds the pointer to the global_data, r8 is call-clobbered
  101. */
  102. #define EXPORT_FUNC(f, a, x, ...) \
  103. asm volatile ( \
  104. " .globl " #x "\n" \
  105. #x ":\n" \
  106. " movhi r8, %%hi(%0)\n" \
  107. " ori r8, r0, %%lo(%0)\n" \
  108. " add r8, r8, gp\n" \
  109. " ldw r8, 0(r8)\n" \
  110. " ldw r8, %1(r8)\n" \
  111. " jmp r8\n" \
  112. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "gp");
  113. #elif defined(CONFIG_M68K)
  114. /*
  115. * d7 holds the pointer to the global_data, a0 is a call-clobbered
  116. * register
  117. */
  118. #define EXPORT_FUNC(f, a, x, ...) \
  119. asm volatile ( \
  120. " .globl " #x "\n" \
  121. #x ":\n" \
  122. " move.l %%d7, %%a0\n" \
  123. " adda.l %0, %%a0\n" \
  124. " move.l (%%a0), %%a0\n" \
  125. " adda.l %1, %%a0\n" \
  126. " move.l (%%a0), %%a0\n" \
  127. " jmp (%%a0)\n" \
  128. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "a0");
  129. #elif defined(CONFIG_MICROBLAZE)
  130. /*
  131. * r31 holds the pointer to the global_data. r5 is a call-clobbered.
  132. */
  133. #define EXPORT_FUNC(f, a, x, ...) \
  134. asm volatile ( \
  135. " .globl " #x "\n" \
  136. #x ":\n" \
  137. " lwi r5, r31, %0\n" \
  138. " lwi r5, r5, %1\n" \
  139. " bra r5\n" \
  140. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "r5");
  141. #elif defined(CONFIG_SH)
  142. /*
  143. * r13 holds the pointer to the global_data. r1 is a call clobbered.
  144. */
  145. #define EXPORT_FUNC(f, a, x, ...) \
  146. asm volatile ( \
  147. " .align 2\n" \
  148. " .globl " #x "\n" \
  149. #x ":\n" \
  150. " mov r13, r1\n" \
  151. " add %0, r1\n" \
  152. " mov.l @r1, r2\n" \
  153. " add %1, r2\n" \
  154. " mov.l @r2, r1\n" \
  155. " jmp @r1\n" \
  156. " nop\n" \
  157. " nop\n" \
  158. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "r1", "r2");
  159. #elif defined(CONFIG_NDS32)
  160. /*
  161. * r16 holds the pointer to the global_data. gp is call clobbered.
  162. * not support reduced register (16 GPR).
  163. */
  164. #define EXPORT_FUNC(f, a, x, ...) \
  165. asm volatile ( \
  166. " .globl " #x "\n" \
  167. #x ":\n" \
  168. " lwi $r16, [$gp + (%0)]\n" \
  169. " lwi $r16, [$r16 + (%1)]\n" \
  170. " jr $r16\n" \
  171. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "$r16");
  172. #elif defined(CONFIG_ARC)
  173. /*
  174. * r25 holds the pointer to the global_data. r10 is call clobbered.
  175. */
  176. #define EXPORT_FUNC(f, a, x, ...) \
  177. asm volatile( \
  178. " .align 4\n" \
  179. " .globl " #x "\n" \
  180. #x ":\n" \
  181. " ld r10, [r25, %0]\n" \
  182. " ld r10, [r10, %1]\n" \
  183. " j [r10]\n" \
  184. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "r10");
  185. #elif defined(CONFIG_XTENSA)
  186. /*
  187. * Global data ptr is in global_data, jump table ptr is in jt.
  188. * Windowed ABI: Jump just past 'entry' in target and adjust stack frame
  189. * (extract stack frame size from target 'entry' instruction).
  190. */
  191. static void **jt;
  192. #if defined(__XTENSA_CALL0_ABI__)
  193. #define EXPORT_FUNC(f, a, x, ...) \
  194. asm volatile ( \
  195. " .extern jt\n" \
  196. " .globl " #x "\n" \
  197. " .align 4\n" \
  198. #x ":\n" \
  199. " l32i a8, %0, 0\n" \
  200. " l32i a8, a8, %1\n" \
  201. " jx a8\n" \
  202. : : "r"(jt), "i" (FO(x)) : "a8");
  203. #elif defined(__XTENSA_WINDOWED_ABI__)
  204. #if XCHAL_HAVE_BE
  205. # define SFT "8"
  206. #else
  207. # define SFT "12"
  208. #endif
  209. #define EXPORT_FUNC(f, a, x, ...) \
  210. asm volatile ( \
  211. " .extern jt\n" \
  212. " .globl " #x "\n" \
  213. " .align 4\n" \
  214. #x ":\n" \
  215. " entry sp, 16\n" \
  216. " l32i a8, %0, 0\n" \
  217. " l32i a8, a8, %1\n" \
  218. " l32i a9, a8, 0\n" \
  219. " extui a9, a9, " SFT ", 12\n" \
  220. " subx8 a9, a9, sp\n" \
  221. " movi a10, 16\n" \
  222. " sub a9, a10, a9\n" \
  223. " movsp sp, a9\n" \
  224. " addi a8, a8, 3\n" \
  225. " jx a8\n" \
  226. : : "r"(jt), "i" (FO(x)) : "a8", "a9", "a10");
  227. #else
  228. #error Unsupported Xtensa ABI
  229. #endif
  230. #else
  231. /*" addi $sp, $sp, -24\n" \
  232. " br $r16\n" \*/
  233. #error stubs definition missing for this architecture
  234. #endif
  235. /* This function is necessary to prevent the compiler from
  236. * generating prologue/epilogue, preparing stack frame etc.
  237. * The stub functions are special, they do not use the stack
  238. * frame passed to them, but pass it intact to the actual
  239. * implementation. On the other hand, asm() statements with
  240. * arguments can be used only inside the functions (gcc limitation)
  241. */
  242. #if GCC_VERSION < 30400
  243. static
  244. #endif /* GCC_VERSION */
  245. void __attribute__((unused)) dummy(void)
  246. {
  247. #include <_exports.h>
  248. }
  249. #include <asm/sections.h>
  250. void app_startup(char * const *argv)
  251. {
  252. char *cp = __bss_start;
  253. /* Zero out BSS */
  254. while (cp < _end)
  255. *cp++ = 0;
  256. #if defined(CONFIG_X86)
  257. /* x86 does not have a dedicated register for passing global_data */
  258. global_data = (gd_t *)argv[-1];
  259. jt = global_data->jt;
  260. #endif
  261. }
  262. #undef EXPORT_FUNC