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