stubs.c 7.0 KB

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