| 1 | #if defined(__ARM_ARCH_4__) || defined (__ARM_ARCH_4T__) |
| 2 | # define call_reg(x) "mov lr, pc ; bx " #x "\n" |
| 3 | #else |
| 4 | /* ARM >= 5 */ |
| 5 | # define call_reg(x) "blx " #x "\n" |
| 6 | #endif |
| 7 | |
| 8 | static void *slp_switch(void *(*save_state)(void*, void*), |
| 9 | void *(*restore_state)(void*, void*), |
| 10 | void *extra) __attribute__((noinline)); |
| 11 | |
| 12 | static void *slp_switch(void *(*save_state)(void*, void*), |
| 13 | void *(*restore_state)(void*, void*), |
| 14 | void *extra) |
| 15 | { |
| 16 | void *result; |
| 17 | /* |
| 18 | seven registers to preserve: r2, r3, r7, r8, r9, r10, r11 |
| 19 | registers marked as clobbered: r0, r1, r4, r5, r6, r12, lr |
| 20 | others: r13 is sp; r14 is lr; r15 is pc |
| 21 | */ |
| 22 | |
| 23 | __asm__ volatile ( |
| 24 | |
| 25 | /* align the stack and save 7 more registers explicitly */ |
| 26 | "mov r0, sp\n" |
| 27 | "and r1, r0, #-16\n" |
| 28 | "mov sp, r1\n" |
| 29 | "push {r0, r2, r3, r7, r8, r9, r10, r11}\n" /* total 8, still aligned */ |
| 30 | #ifndef __SOFTFP__ |
| 31 | /* We also push d8-d15 to preserve them explicitly. This assumes |
| 32 | * that this code is in a function that doesn't use floating-point |
| 33 | * at all, and so don't touch the "d" registers (that's why we mark |
| 34 | * it as non-inlinable). So here by pushing/poping d8-d15 we are |
| 35 | * saving precisely the callee-saved registers in all cases. We |
| 36 | * could also try to list all "d" registers as clobbered, but it |
| 37 | * doesn't work: there is no way I could find to know if we have 16 |
| 38 | * or 32 "d" registers (depends on the exact -mcpu=... and we don't |
| 39 | * know it from the C code). If we have 32, then gcc would "save" |
| 40 | * d8-d15 by copying them into d16-d23 for example, and it doesn't |
| 41 | * work. */ |
| 42 | "vpush {d8, d9, d10, d11, d12, d13, d14, d15}\n" /* 16 words, still aligned */ |
| 43 | #endif |
| 44 | |
| 45 | /* save values in callee saved registers for later */ |
| 46 | "mov r4, %[restore_state]\n" /* can't be r0 or r1: marked clobbered */ |
| 47 | "mov r5, %[extra]\n" /* can't be r0 or r1 or r4: marked clob. */ |
| 48 | "mov r3, %[save_state]\n" /* can't be r0, r1, r4, r5: marked clob. */ |
| 49 | "mov r0, sp\n" /* arg 1: current (old) stack pointer */ |
| 50 | "mov r1, r5\n" /* arg 2: extra */ |
| 51 | call_reg(r3) /* call save_state() */ |
| 52 | |
| 53 | /* skip the rest if the return value is null */ |
| 54 | "cmp r0, #0\n" |
| 55 | "beq zero\n" |
| 56 | |
| 57 | "mov sp, r0\n" /* change the stack pointer */ |
| 58 | |
| 59 | /* From now on, the stack pointer is modified, but the content of the |
| 60 | stack is not restored yet. It contains only garbage here. */ |
| 61 | "mov r1, r5\n" /* arg 2: extra */ |
| 62 | /* arg 1: current (new) stack pointer is already in r0*/ |
| 63 | call_reg(r4) /* call restore_state() */ |
| 64 | |
| 65 | /* The stack's content is now restored. */ |
| 66 | "zero:\n" |
| 67 | |
| 68 | #ifndef __SOFTFP__ |
| 69 | "vpop {d8, d9, d10, d11, d12, d13, d14, d15}\n" |
| 70 | #endif |
| 71 | "pop {r1, r2, r3, r7, r8, r9, r10, r11}\n" |
| 72 | "mov sp, r1\n" |
| 73 | "mov %[result], r0\n" |
| 74 | |
| 75 | : [result]"=r"(result) /* output variables */ |
| 76 | /* input variables */ |
| 77 | : [restore_state]"r"(restore_state), |
| 78 | [save_state]"r"(save_state), |
| 79 | [extra]"r"(extra) |
| 80 | : "r0", "r1", "r4", "r5", "r6", "r12", "lr", |
| 81 | "memory", "cc" |
| 82 | ); |
| 83 | return result; |
| 84 | } |