2010-08-17 18:44:07 +02:00
|
|
|
#include <machine/asm.h>
|
2012-06-19 04:16:41 +02:00
|
|
|
#include <ucontextoffsets.h>
|
2010-03-12 16:58:41 +01:00
|
|
|
|
2010-08-17 18:44:07 +02:00
|
|
|
IMPORT(getuctx)
|
|
|
|
IMPORT(setuctx)
|
|
|
|
IMPORT(resumecontext)
|
|
|
|
|
2013-02-01 10:39:47 +01:00
|
|
|
.globl _C_LABEL(__errno)
|
2010-03-12 16:58:41 +01:00
|
|
|
|
|
|
|
/* int getcontext(ucontext_t *ucp)
|
|
|
|
* Initialise the structure pointed to by ucp to the current user context
|
|
|
|
* of the calling thread. */
|
2010-08-17 18:44:07 +02:00
|
|
|
ENTRY(getcontext)
|
2010-03-12 16:58:41 +01:00
|
|
|
/* In case a process does not use the FPU and is neither interested in
|
|
|
|
* saving its signal mask, then we can skip the context switch to
|
|
|
|
* PM and kernel altogether and only save general-purpose registers. */
|
|
|
|
|
|
|
|
mov 4(%esp), %edx /* edx = ucp */
|
|
|
|
/* Check null pointer */
|
|
|
|
cmp $0, %edx /* edx == NULL? */
|
|
|
|
jne 3f /* Not null, continue */
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_PROLOGUE
|
|
|
|
call PIC_PLT(_C_LABEL(__errno))
|
|
|
|
PIC_EPILOGUE
|
|
|
|
movl $EFAULT, (%eax)
|
2010-03-12 16:58:41 +01:00
|
|
|
xor %eax, %eax
|
|
|
|
dec %eax /* return -1 */
|
|
|
|
ret
|
|
|
|
|
|
|
|
3: /* Check flags */
|
2013-02-01 10:39:47 +01:00
|
|
|
mov UC_FLAGS(%edx), %eax /* eax = ucp->uc_flags */
|
2013-12-10 09:57:38 +01:00
|
|
|
and $[_UC_IGNFPU|_UC_IGNSIGM], %eax
|
|
|
|
cmp $[_UC_IGNFPU|_UC_IGNSIGM], %eax
|
|
|
|
jz 5f /* Ignore both, skip getuctx */
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_PROLOGUE
|
2010-03-12 16:58:41 +01:00
|
|
|
push %edx
|
2013-02-01 10:39:47 +01:00
|
|
|
call PIC_PLT(_C_LABEL(getuctx)) /* getuctx(ucp) */
|
2010-03-12 16:58:41 +01:00
|
|
|
pop %edx /* clean up stack and restore edx */
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_EPILOGUE
|
2010-03-12 16:58:41 +01:00
|
|
|
|
2013-02-01 10:39:47 +01:00
|
|
|
5:
|
2010-03-12 16:58:41 +01:00
|
|
|
/* Save the context */
|
2013-02-01 10:39:47 +01:00
|
|
|
pop PC(%edx) /* Save real RTA in mcp struct */
|
2010-03-12 16:58:41 +01:00
|
|
|
mov %esp, SP(%edx) /* Save stack pointer (now pointing to ucp) */
|
2013-02-01 10:39:47 +01:00
|
|
|
/* Save GP registers (except EAX and EDX) */
|
2010-03-12 16:58:41 +01:00
|
|
|
mov %ebp, BP(%edx) /* Save EBP */
|
|
|
|
mov %esi, SI(%edx) /* Save ESI */
|
|
|
|
mov %edi, DI(%edx) /* Save EDI */
|
|
|
|
mov %ebx, BX(%edx) /* Save EBX */
|
|
|
|
mov %ecx, CX(%edx) /* Save ECX */
|
|
|
|
movl $MCF_MAGIC, MAGIC(%edx) /* Set magic value */
|
|
|
|
xor %eax, %eax /* Return 0 */
|
2013-02-01 10:39:47 +01:00
|
|
|
jmp *PC(%edx) /* Return return address */
|
2010-03-12 16:58:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* int setcontext(const ucontext_t *ucp)
|
|
|
|
* Restore the user context pointed to by ucp. A successful call to
|
|
|
|
* setcontext does not return; program execution resumes at the point
|
|
|
|
* specified by the ucp argument. If ucp was created with getcontext(),
|
|
|
|
* program execution continues as if the corresponding call of getcontext()
|
|
|
|
* had just returned. If ucp was created with makecontext(), program
|
|
|
|
* execution continues with the function passed to makecontext(). */
|
2010-08-17 18:44:07 +02:00
|
|
|
ENTRY(setcontext)
|
2010-03-12 16:58:41 +01:00
|
|
|
/* In case a process does not use the FPU and is neither interested in
|
|
|
|
* restoring its signal mask, then we can skip the context switch to
|
|
|
|
* PM and kernel altogether and restore state here. */
|
|
|
|
|
|
|
|
mov 4(%esp), %edx /* edx = ucp */
|
|
|
|
|
|
|
|
/* Check null pointer */
|
|
|
|
cmp $0, %edx /* edx == NULL? */
|
|
|
|
jnz 3f /* Not null, continue */
|
2013-02-01 10:39:47 +01:00
|
|
|
movl $EFAULT, %edx
|
|
|
|
0: push %edx /* preserve errno */
|
|
|
|
PIC_PROLOGUE
|
|
|
|
call PIC_PLT(_C_LABEL(__errno))
|
|
|
|
PIC_EPILOGUE
|
|
|
|
pop %edx
|
|
|
|
movl %edx, (%eax)
|
2010-03-12 16:58:41 +01:00
|
|
|
xor %eax, %eax
|
|
|
|
dec %eax /* return -1 */
|
|
|
|
ret
|
|
|
|
|
|
|
|
3: /* Check flags */
|
2013-02-01 10:39:47 +01:00
|
|
|
cmpl $MCF_MAGIC, MAGIC(%edx) /* is the magic value set (is context valid)?*/
|
2010-03-12 16:58:41 +01:00
|
|
|
jz 4f /* is set, proceed */
|
2013-02-01 10:39:47 +01:00
|
|
|
movl $EINVAL, %edx /* not set, return error code */
|
|
|
|
jmp 0b
|
2010-03-12 16:58:41 +01:00
|
|
|
|
|
|
|
|
2013-02-01 10:39:47 +01:00
|
|
|
4: mov UC_FLAGS(%edx), %eax /* eax = ucp->uc_flags */
|
2013-12-10 09:57:38 +01:00
|
|
|
and $[_UC_IGNFPU|_UC_IGNSIGM], %eax
|
|
|
|
cmp $[_UC_IGNFPU|_UC_IGNSIGM], %eax
|
|
|
|
jz 5f /* Ignore both, so don't bother restoring FPU
|
2010-03-12 16:58:41 +01:00
|
|
|
* state and signal mask */
|
|
|
|
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_PROLOGUE
|
2010-03-12 16:58:41 +01:00
|
|
|
push %edx
|
2013-02-01 10:39:47 +01:00
|
|
|
call PIC_PLT(_C_LABEL(setuctx)) /* setuctx(ucp) */
|
2010-03-12 16:58:41 +01:00
|
|
|
pop %edx /* Clean up stack and restore edx */
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_EPILOGUE
|
2010-03-12 16:58:41 +01:00
|
|
|
|
2013-02-01 10:39:47 +01:00
|
|
|
5: /* Restore the registers (except EAX and EDX) */
|
2010-03-12 16:58:41 +01:00
|
|
|
mov CX(%edx), %ecx /* Restore ECX */
|
|
|
|
mov BX(%edx), %ebx /* Restore EBX */
|
|
|
|
mov DI(%edx), %edi /* Restore EDI */
|
|
|
|
mov SI(%edx), %esi /* Restore ESI */
|
|
|
|
mov BP(%edx), %ebp /* Restore EBP */
|
|
|
|
mov SP(%edx), %esp /* Restore stack pointer */
|
2013-02-01 10:39:47 +01:00
|
|
|
jmp *PC(%edx) /* Return to RTA */
|
2010-03-12 16:58:41 +01:00
|
|
|
|
|
|
|
/* void ctx_start((void *func)(int arg1, ..., argn), arg1, ..., argn,
|
|
|
|
* ucontext_t *ucp)
|
|
|
|
* A wrapper to start function `func'. ESI register will contain a pointer
|
|
|
|
* to ucp on the stack. By setting ESP to ESI, we effectively 'remove' all
|
|
|
|
* arguments to `func' from the stack. Finally, a call to resumecontext
|
|
|
|
* will start the next context in the linked list (or exit the program if
|
2013-02-01 10:39:47 +01:00
|
|
|
* there is no context).
|
|
|
|
*
|
|
|
|
* Since PIC needs the EBX register, which is pushed on the stack by
|
|
|
|
* PIC_PROLOGUE, we need an extra of salsa here.
|
|
|
|
*/
|
2010-08-17 18:44:07 +02:00
|
|
|
ENTRY(ctx_start)
|
2010-03-12 16:58:41 +01:00
|
|
|
/* 0(esp) -> func
|
|
|
|
* 4(esp) -> arg1
|
|
|
|
* ...
|
|
|
|
* 4*n(esp) -> argn
|
|
|
|
* 4*(n+1)(esp) -> ucp */
|
|
|
|
|
|
|
|
pop %eax /* eax = func */
|
|
|
|
call *%eax /* func(arg1, ..., argn) */
|
2013-02-01 10:39:47 +01:00
|
|
|
PIC_PROLOGUE /* may push %ebx, but we do not care */
|
|
|
|
mov %esi, %esp /* Clean up stack, keep %ebx = &GOT */
|
2010-03-12 16:58:41 +01:00
|
|
|
/* ucp is now at the top of the stack again */
|
2013-02-01 10:39:47 +01:00
|
|
|
call PIC_PLT(_C_LABEL(resumecontext)) /* resumecontext(ucp) */
|
2010-03-12 16:58:41 +01:00
|
|
|
ret /* never reached */
|
|
|
|
|
|
|
|
|