<machine/mcontext.h>

Change-Id: I2ad64018f3f402e7ccc5c4dc037dd0a3fe56a929
This commit is contained in:
Ben Gras
2013-12-10 14:55:46 +01:00
committed by Lionel Sambuc
parent 17587738d3
commit 5cecdfcb3e
8 changed files with 377 additions and 85 deletions

View File

@@ -8,22 +8,22 @@ struct __ucontext
member UC_FLAGS uc_flags
member UC_LINK uc_link
member MAGIC uc_mcontext.mc_magic
member REG0 uc_mcontext.mc_p_reg.retreg
member REG1 uc_mcontext.mc_p_reg.r1
member REG2 uc_mcontext.mc_p_reg.r2
member REG3 uc_mcontext.mc_p_reg.r3
member REG4 uc_mcontext.mc_p_reg.r4
member REG5 uc_mcontext.mc_p_reg.r5
member REG6 uc_mcontext.mc_p_reg.r6
member REG7 uc_mcontext.mc_p_reg.r7
member REG8 uc_mcontext.mc_p_reg.r8
member REG9 uc_mcontext.mc_p_reg.r9
member REG10 uc_mcontext.mc_p_reg.r10
member FPREG uc_mcontext.mc_p_reg.fp
member REG12 uc_mcontext.mc_p_reg.r12
member SPREG uc_mcontext.mc_p_reg.sp
member LRREG uc_mcontext.mc_p_reg.lr
member PCREG uc_mcontext.mc_p_reg.pc
member REG0 uc_mcontext.__gregs[_REG_R0]
member REG1 uc_mcontext.__gregs[_REG_R1]
member REG2 uc_mcontext.__gregs[_REG_R2]
member REG3 uc_mcontext.__gregs[_REG_R3]
member REG4 uc_mcontext.__gregs[_REG_R4]
member REG5 uc_mcontext.__gregs[_REG_R5]
member REG6 uc_mcontext.__gregs[_REG_R6]
member REG7 uc_mcontext.__gregs[_REG_R7]
member REG8 uc_mcontext.__gregs[_REG_R8]
member REG9 uc_mcontext.__gregs[_REG_R9]
member REG10 uc_mcontext.__gregs[_REG_R10]
member FPREG uc_mcontext.__gregs[_REG_FP]
member REG12 uc_mcontext.__gregs[_REG_R12]
member SPREG uc_mcontext.__gregs[_REG_SP]
member LRREG uc_mcontext.__gregs[_REG_LR]
member PCREG uc_mcontext.__gregs[_REG_PC]
define EFAULT EFAULT
define EINVAL EINVAL
define MCF_MAGIC MCF_MAGIC

View File

@@ -8,15 +8,15 @@ struct __ucontext
member UC_FLAGS uc_flags
member UC_LINK uc_link
member MAGIC uc_mcontext.mc_magic
member DI uc_mcontext.mc_p_reg.di
member SI uc_mcontext.mc_p_reg.si
member BP uc_mcontext.mc_p_reg.fp
member AX uc_mcontext.mc_p_reg.retreg
member BX uc_mcontext.mc_p_reg.bx
member CX uc_mcontext.mc_p_reg.cx
member DX uc_mcontext.mc_p_reg.dx
member PC uc_mcontext.mc_p_reg.pc
member SP uc_mcontext.mc_p_reg.sp
member DI uc_mcontext.__gregs[_REG_EDI]
member SI uc_mcontext.__gregs[_REG_ESI]
member BP uc_mcontext.__gregs[_REG_EBP]
member AX uc_mcontext.__gregs[_REG_EAX]
member BX uc_mcontext.__gregs[_REG_EBX]
member CX uc_mcontext.__gregs[_REG_ECX]
member DX uc_mcontext.__gregs[_REG_EDX]
member PC uc_mcontext.__gregs[_REG_EIP]
member SP uc_mcontext.__gregs[_REG_ESP]
define EFAULT EFAULT
define EINVAL EINVAL
define MCF_MAGIC MCF_MAGIC

View File

@@ -84,7 +84,7 @@ void makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
} else if ((ucp->uc_stack.ss_sp == NULL) ||
(ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
ucp->uc_mcontext.mc_magic = 0;
ucp->uc_mcontext.mc_p_reg.sp = 0;
_UC_MACHINE_SET_STACK(ucp, 0);
return;
}
@@ -121,9 +121,9 @@ void makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
/* Adjust the machine context to point to the top of this stack and the
program counter to the context start wrapper. */
ucp->uc_mcontext.mc_p_reg.fp = 0; /* Clear frame pointer */
ucp->uc_mcontext.mc_p_reg.sp = (reg_t) stack_top;
ucp->uc_mcontext.mc_p_reg.pc = (reg_t) ctx_start;
_UC_MACHINE_SET_EBP(ucp, 0); /* Clear frame pointer */
_UC_MACHINE_SET_STACK(ucp, (reg_t) stack_top);
_UC_MACHINE_SET_PC(ucp, (reg_t) ctx_start);
*stack_top++ = (uintptr_t) func;
@@ -140,13 +140,14 @@ void makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
/* Set ESI to point to the base of the stack where ucp is stored, so
that the wrapper function knows how to clean up the stack after
calling `func' (i.e., how to adjust ESP). */
ucp->uc_mcontext.mc_p_reg.si = (reg_t) stack_top;
_UC_MACHINE_SET_ESI(ucp, (reg_t) stack_top);
/* If we ran out of stack space, invalidate stack pointer. Eventually,
swapcontext will choke on this and return ENOMEM. */
if (stack_top == ucp->uc_stack.ss_sp)
ucp->uc_mcontext.mc_p_reg.sp = 0;
if (stack_top == ucp->uc_stack.ss_sp) {
_UC_MACHINE_SET_STACK(ucp, 0);
}
#elif defined(__arm__)
/* The caller provides a pointer to a stack that we can use to run our
context on. When the context starts, control is given to the
@@ -180,23 +181,23 @@ void makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
/* Adjust the machine context to point to the top of this stack and the
program counter to the 'func' entry point. Set lr to ctx_start, so
ctx_start runs after 'func'. Save ucp in r4 */
ucp->uc_mcontext.mc_p_reg.fp = 0; /* Clear frame pointer */
ucp->uc_mcontext.mc_p_reg.sp = (reg_t) stack_top;
ucp->uc_mcontext.mc_p_reg.pc = (reg_t) func;
ucp->uc_mcontext.mc_p_reg.lr = (reg_t) ctx_start;
ucp->uc_mcontext.mc_p_reg.r4 = (reg_t) ucp;
_UC_MACHINE_SET_FP(ucp, 0); /* Clear frame pointer */
_UC_MACHINE_SET_STACK(ucp, (reg_t) stack_top);
_UC_MACHINE_SET_PC(ucp, (reg_t) func);
_UC_MACHINE_SET_LR(ucp, (reg_t) ctx_start);
_UC_MACHINE_SET_R4(ucp, (reg_t) ucp);
/* Copy arguments to r0-r3 and stack. */
va_start(ap, argc);
/* Pass up to four arguments in registers. */
if (argc-- > 0)
ucp->uc_mcontext.mc_p_reg.retreg = va_arg(ap, uintptr_t);
_UC_MACHINE_SET_R0(ucp, va_arg(ap, uintptr_t));
if (argc-- > 0)
ucp->uc_mcontext.mc_p_reg.r1 = va_arg(ap, uintptr_t);
_UC_MACHINE_SET_R1(ucp, va_arg(ap, uintptr_t));
if (argc-- > 0)
ucp->uc_mcontext.mc_p_reg.r2 = va_arg(ap, uintptr_t);
_UC_MACHINE_SET_R2(ucp, va_arg(ap, uintptr_t));
if (argc-- > 0)
ucp->uc_mcontext.mc_p_reg.r3 = va_arg(ap, uintptr_t);
_UC_MACHINE_SET_R3(ucp, va_arg(ap, uintptr_t));
/* Pass the rest on the stack. */
while (argc-- > 0) {
*stack_top++ = va_arg(ap, uintptr_t);
@@ -205,8 +206,9 @@ void makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
/* If we ran out of stack space, invalidate stack pointer. Eventually,
swapcontext will choke on this and return ENOMEM. */
if (stack_top == ucp->uc_stack.ss_sp)
ucp->uc_mcontext.mc_p_reg.sp = 0;
if (stack_top == ucp->uc_stack.ss_sp) {
_UC_MACHINE_SET_STACK(ucp, 0);
}
#else
# error "Unsupported platform"
#endif
@@ -226,7 +228,7 @@ int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
return(-1);
}
if (ucp->uc_mcontext.mc_p_reg.sp == 0) {
if (_UC_MACHINE_STACK(ucp) == 0) {
/* No stack space. Bail out. */
errno = ENOMEM;
return(-1);

View File

@@ -116,11 +116,10 @@ void mthread_stats(void)
*===========================================================================*/
void mthread_stacktrace(mthread_thread_t t)
{
#ifdef __i386__ /* stacktrace only implemented on x86 */
unsigned long bp, hbp, pc;
mthread_tcb_t *tcb;
ucontext_t *ctx;
mcontext_t *mtx;
struct stackframe_s *frame;
tcb = mthread_find_tcb(t);
ctx = &tcb->m_context;
@@ -130,9 +129,7 @@ void mthread_stacktrace(mthread_thread_t t)
printf("thread %d: ", t);
mtx = &ctx->uc_mcontext;
frame = &mtx->mc_p_reg;
bp = frame->fp;
bp = _UC_MACHINE_EBP(ctx);
while (bp) {
pc = ((unsigned long *) bp)[1];
@@ -149,6 +146,7 @@ void mthread_stacktrace(mthread_thread_t t)
}
printf("\n");
#endif
}
/*===========================================================================*