stubs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. static 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_NDS32)
  161. /*
  162. * r16 holds the pointer to the global_data. gp is call clobbered.
  163. * not support reduced register (16 GPR).
  164. */
  165. #define EXPORT_FUNC(f, a, x, ...) \
  166. asm volatile ( \
  167. " .globl " #x "\n" \
  168. #x ":\n" \
  169. " lwi $r16, [$gp + (%0)]\n" \
  170. " lwi $r16, [$r16 + (%1)]\n" \
  171. " jr $r16\n" \
  172. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "$r16");
  173. #elif defined(CONFIG_RISCV)
  174. /*
  175. * gp holds the pointer to the global_data. t0 is call clobbered.
  176. */
  177. #ifdef CONFIG_ARCH_RV64I
  178. #define EXPORT_FUNC(f, a, x, ...) \
  179. asm volatile ( \
  180. " .globl " #x "\n" \
  181. #x ":\n" \
  182. " ld t0, %0(gp)\n" \
  183. " ld t0, %1(t0)\n" \
  184. " jr t0\n" \
  185. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "t0");
  186. #else
  187. #define EXPORT_FUNC(f, a, x, ...) \
  188. asm volatile ( \
  189. " .globl " #x "\n" \
  190. #x ":\n" \
  191. " lw t0, %0(gp)\n" \
  192. " lw t0, %1(t0)\n" \
  193. " jr t0\n" \
  194. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "t0");
  195. #endif
  196. #elif defined(CONFIG_ARC)
  197. /*
  198. * r25 holds the pointer to the global_data. r10 is call clobbered.
  199. */
  200. #define EXPORT_FUNC(f, a, x, ...) \
  201. asm volatile( \
  202. " .align 4\n" \
  203. " .globl " #x "\n" \
  204. #x ":\n" \
  205. " ld r10, [r25, %0]\n" \
  206. " ld r10, [r10, %1]\n" \
  207. " j [r10]\n" \
  208. : : "i"(offsetof(gd_t, jt)), "i"(FO(x)) : "r10");
  209. #elif defined(CONFIG_XTENSA)
  210. /*
  211. * Global data ptr is in global_data, jump table ptr is in jt.
  212. * Windowed ABI: Jump just past 'entry' in target and adjust stack frame
  213. * (extract stack frame size from target 'entry' instruction).
  214. */
  215. static void **jt;
  216. #if defined(__XTENSA_CALL0_ABI__)
  217. #define EXPORT_FUNC(f, a, x, ...) \
  218. asm volatile ( \
  219. " .extern jt\n" \
  220. " .globl " #x "\n" \
  221. " .align 4\n" \
  222. #x ":\n" \
  223. " l32i a8, %0, 0\n" \
  224. " l32i a8, a8, %1\n" \
  225. " jx a8\n" \
  226. : : "r"(jt), "i" (FO(x)) : "a8");
  227. #elif defined(__XTENSA_WINDOWED_ABI__)
  228. #if XCHAL_HAVE_BE
  229. # define SFT "8"
  230. #else
  231. # define SFT "12"
  232. #endif
  233. #define EXPORT_FUNC(f, a, x, ...) \
  234. asm volatile ( \
  235. " .extern jt\n" \
  236. " .globl " #x "\n" \
  237. " .align 4\n" \
  238. #x ":\n" \
  239. " entry sp, 16\n" \
  240. " l32i a8, %0, 0\n" \
  241. " l32i a8, a8, %1\n" \
  242. " l32i a9, a8, 0\n" \
  243. " extui a9, a9, " SFT ", 12\n" \
  244. " subx8 a9, a9, sp\n" \
  245. " movi a10, 16\n" \
  246. " sub a9, a10, a9\n" \
  247. " movsp sp, a9\n" \
  248. " addi a8, a8, 3\n" \
  249. " jx a8\n" \
  250. : : "r"(jt), "i" (FO(x)) : "a8", "a9", "a10");
  251. #else
  252. #error Unsupported Xtensa ABI
  253. #endif
  254. #else
  255. /*" addi $sp, $sp, -24\n" \
  256. " br $r16\n" \*/
  257. #error stubs definition missing for this architecture
  258. #endif
  259. /* This function is necessary to prevent the compiler from
  260. * generating prologue/epilogue, preparing stack frame etc.
  261. * The stub functions are special, they do not use the stack
  262. * frame passed to them, but pass it intact to the actual
  263. * implementation. On the other hand, asm() statements with
  264. * arguments can be used only inside the functions (gcc limitation)
  265. */
  266. #if GCC_VERSION < 30400
  267. static
  268. #endif /* GCC_VERSION */
  269. void __attribute__((unused)) dummy(void)
  270. {
  271. #include <_exports.h>
  272. }
  273. #include <asm/sections.h>
  274. void app_startup(char * const *argv)
  275. {
  276. char *cp = __bss_start;
  277. /* Zero out BSS */
  278. while (cp < _end)
  279. *cp++ = 0;
  280. #if defined(CONFIG_X86)
  281. /* x86 does not have a dedicated register for passing global_data */
  282. global_data = (gd_t *)argv[-1];
  283. jt = global_data->jt;
  284. #endif
  285. }
  286. #undef EXPORT_FUNC