| 1 | |
| 2 | static void *slp_switch(void *(*save_state)(void*, void*), |
| 3 | void *(*restore_state)(void*, void*), |
| 4 | void *extra) |
| 5 | { |
| 6 | void *result, *garbage1, *garbage2; |
| 7 | __asm__ volatile ( |
| 8 | "pushl %%ebp\n" |
| 9 | "pushl %%ebx\n" /* push some registers that may contain */ |
| 10 | "pushl %%esi\n" /* some value that is meant to be saved */ |
| 11 | "movl %%esp, %%ebp\n" |
| 12 | "andl $-16, %%esp\n" /* <= align the stack here, for the calls */ |
| 13 | "pushl %%edi\n" |
| 14 | "pushl %%ebp\n" |
| 15 | |
| 16 | "movl %%eax, %%esi\n" /* save 'restore_state' for later */ |
| 17 | "movl %%edx, %%edi\n" /* save 'extra' for later */ |
| 18 | |
| 19 | "movl %%esp, %%eax\n" |
| 20 | |
| 21 | "pushl %%edx\n" /* arg 2: extra */ |
| 22 | "pushl %%eax\n" /* arg 1: current (old) stack pointer */ |
| 23 | "call *%%ecx\n" /* call save_state() */ |
| 24 | |
| 25 | "testl %%eax, %%eax\n"/* skip the rest if the return value is null */ |
| 26 | "jz 0f\n" |
| 27 | |
| 28 | "movl %%eax, %%esp\n" /* change the stack pointer */ |
| 29 | |
| 30 | /* From now on, the stack pointer is modified, but the content of the |
| 31 | stack is not restored yet. It contains only garbage here. */ |
| 32 | |
| 33 | "pushl %%edi\n" /* arg 2: extra */ |
| 34 | "pushl %%eax\n" /* arg 1: current (new) stack pointer */ |
| 35 | "call *%%esi\n" /* call restore_state() */ |
| 36 | |
| 37 | /* The stack's content is now restored. */ |
| 38 | |
| 39 | "0:\n" |
| 40 | "addl $8, %%esp\n" |
| 41 | "popl %%ebp\n" |
| 42 | "popl %%edi\n" |
| 43 | "movl %%ebp, %%esp\n" |
| 44 | "popl %%esi\n" |
| 45 | "popl %%ebx\n" |
| 46 | "popl %%ebp\n" |
| 47 | |
| 48 | : "=a"(result), /* output variables */ |
| 49 | "=c"(garbage1), |
| 50 | "=d"(garbage2) |
| 51 | : "a"(restore_state), /* input variables */ |
| 52 | "c"(save_state), |
| 53 | "d"(extra) |
| 54 | : "memory" |
| 55 | ); |
| 56 | /* Note: we should also list all fp/xmm registers, but is there a way |
| 57 | to list only the ones used by the current compilation target? |
| 58 | For now we will just ignore the issue and hope (reasonably) that |
| 59 | this function is never inlined all the way into 3rd-party user code. */ |
| 60 | return result; |
| 61 | } |