From 18b94797b5fa68d15b19435c0e85b82264ece0f3 Mon Sep 17 00:00:00 2001 From: Alex Hornung Date: Wed, 13 Apr 2011 13:47:51 +0100 Subject: [PATCH] getcontext - x86_64 implementation --- lib/libc/x86_64/sys/getcontext.S | 88 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 88 insertions(+), 0 deletions(-) create mode 100644 lib/libc/x86_64/sys/getcontext.S diff --git a/lib/libc/x86_64/sys/getcontext.S b/lib/libc/x86_64/sys/getcontext.S new file mode 100644 index 0000000..9a9b7e8 --- /dev/null +++ b/lib/libc/x86_64/sys/getcontext.S @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2006 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/lib/libc/i386/sys/getcontext.S,v 1.2 2007/02/04 20:28:21 corecode Exp $ + */ + +#include +#include + + /* + * This function is special-cased because the context it saves + * includes a stale stack context (because it returns before the + * caller presumably makes the call to setcontext()). + */ + .weak getcontext + .set getcontext,_getcontext +ENTRY(_getcontext) + /* + * Retrieve the current signal mask and save it in &ucp->uc_sigmask. + */ + movq %rcx,%r9 /* save ucontext_t in %r9 */ + movq %rcx,%r8 /* ucontext_t pointer */ + addq $UC_SIGMASK,%r8 /* pointer to signal mask */ + PIC_PROLOGUE + movl $SIG_BLOCK,%ecx + movq $0,$rdx + call PIC_PLT(_sigprocmask) /* retrieve & save signal mask */ + PIC_EPILOGUE + + /* + * Save what we need because our stack context is going stale. + */ + movq %r9,%rcx + movq (%rsp),%rdx /* save return PC in %rdx */ + addq $UC_MCONTEXT,%rcx + call get_mcontext /* returns non-zero on resume */ + cmpl $0,%eax + je 2f + /* + * On resume, resave the stale return pc and restore the signal + * mask (signals are blocked right now from the setcontext call). + */ + movq %rdx,(%rsp) /* re-save the return PC */ + movq %r9,%rdx + addq $UC_SIGMASK,%rdx + PIC_PROLOGUE + movl $SIG_SETMASK,%ecx + movq $0,$r8 + call PIC_PLT(_sigprocmask) /* retrieve & save signal mask */ + PIC_EPILOGUE +2: + addq $8,%rsp + movq $0,%rax /* return success */ + ret + +1: + PIC_PROLOGUE + jmp PIC_PLT(HIDENAME(cerror)) -- 1.7.4.3