From 894bd3ad9d0df7fe47d309efdf7eb673e226676c Mon Sep 17 00:00:00 2001 From: fvdl Date: Thu, 30 Jan 2003 02:07:30 +0000 Subject: [PATCH] Add ucontext glue for x86_64. --- lib/libc/arch/x86_64/gen/Makefile.inc | 6 +- lib/libc/arch/x86_64/gen/_lwp.c | 70 ++++++++++++ lib/libc/arch/x86_64/gen/makecontext.c | 104 ++++++++++++++++++ lib/libc/arch/x86_64/gen/resumecontext.S | 68 ++++++++++++ lib/libc/arch/x86_64/gen/swapcontext.S | 75 +++++++++++++ .../arch/x86_64/sys/__sigaction14_sigtramp.c | 6 +- lib/libc/arch/x86_64/sys/getcontext.S | 63 +++++++++++ 7 files changed, 388 insertions(+), 4 deletions(-) create mode 100644 lib/libc/arch/x86_64/gen/_lwp.c create mode 100644 lib/libc/arch/x86_64/gen/makecontext.c create mode 100644 lib/libc/arch/x86_64/gen/resumecontext.S create mode 100644 lib/libc/arch/x86_64/gen/swapcontext.S create mode 100644 lib/libc/arch/x86_64/sys/getcontext.S diff --git a/lib/libc/arch/x86_64/gen/Makefile.inc b/lib/libc/arch/x86_64/gen/Makefile.inc index 6507441e72ec..f3d34a943bd6 100644 --- a/lib/libc/arch/x86_64/gen/Makefile.inc +++ b/lib/libc/arch/x86_64/gen/Makefile.inc @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.2 2002/02/19 13:08:35 simonb Exp $ +# $NetBSD: Makefile.inc,v 1.3 2003/01/30 02:07:30 fvdl Exp $ # objects built from assembler sources (need lint stubs) SRCS+= alloca.S bswap64.S byte_swap_2.S byte_swap_4.S fabs.S modf.S \ @@ -9,8 +9,10 @@ SRCS+= __setjmp14.S SRCS+= _setjmp.S SRCS+= __sigsetjmp14.S +SRCS+= resumecontext.S swapcontext.S + # objects built from C sources -SRCS+= ldexp.c +SRCS+= ldexp.c _lwp.c makecontext.c # Common ieee754 constants and functions SRCS+= ieee754_infinity.c ieee754_nanf.c diff --git a/lib/libc/arch/x86_64/gen/_lwp.c b/lib/libc/arch/x86_64/gen/_lwp.c new file mode 100644 index 000000000000..ac195f4cf952 --- /dev/null +++ b/lib/libc/arch/x86_64/gen/_lwp.c @@ -0,0 +1,70 @@ +/* $NetBSD: _lwp.c,v 1.1 2003/01/30 02:07:30 fvdl Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Nathan J. Williams. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +#include +#include +#include +#include +#include + +void +_lwp_makecontext(ucontext_t *u, void (*start)(void *), + void *arg, void *private, caddr_t stack_base, size_t stack_size) +{ + void **sp; + + getcontext(u); + u->uc_link = NULL; + + u->uc_stack.ss_sp = stack_base; + u->uc_stack.ss_size = stack_size; + + /* LINTED uintptr_t is safe */ + u->uc_mcontext.__gregs[_REG_RIP] = (uintptr_t)start; + + sp = (void **) (((uintptr_t)(stack_base + stack_size) & ~15) - 8); + + /* LINTED __greg_t is safe */ + u->uc_mcontext.__gregs[_REG_RDI] = (__greg_t)arg; + *--sp = (void *) _lwp_exit; + + /* LINTED uintptr_t is safe */ + u->uc_mcontext.__gregs[_REG_URSP] = (uintptr_t) sp; + + /* LINTED private is currently unused */ +} diff --git a/lib/libc/arch/x86_64/gen/makecontext.c b/lib/libc/arch/x86_64/gen/makecontext.c new file mode 100644 index 000000000000..2ac2c812d169 --- /dev/null +++ b/lib/libc/arch/x86_64/gen/makecontext.c @@ -0,0 +1,104 @@ +/* $NetBSD: makecontext.c,v 1.1 2003/01/30 02:07:31 fvdl Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + * + * Modified from the i386 version for x86_64 by fvdl@wasabisystems.com. + * + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: makecontext.c,v 1.1 2003/01/30 02:07:31 fvdl Exp $"); +#endif + +#include +#include +#include +#include "extern.h" + +#include + +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + __greg_t *gr = ucp->uc_mcontext.__gregs; + uintptr_t *sp; + va_list ap; + int stackargs, i; + + stackargs = argc - 6; + + /* LINTED __greg_t is safe */ + gr[_REG_RIP] = (__greg_t)func; + + /* LINTED uintptr_t is safe */ + sp = (uintptr_t *) + ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + + /* LINTED uintptr_t is safe */ + sp = (uintptr_t *)(((uintptr_t)sp & ~15) - 8); + sp--; + if (stackargs > 0) + sp -= stackargs; + /* LINTED __greg_t is safe */ + gr[_REG_URSP] = (__greg_t)sp; + gr[_REG_RBP] = (__greg_t)0; /* Wipe out frame pointer. */ + + /* Put return address on top of stack. */ + /* LINTED uintptr_t is safe */ + *sp++ = (uintptr_t)_resumecontext; + + /* + * Construct argument list. + * The registers used to pass the first 6 arguments + * (rdi, rsi, rdx, rcx, r8, r9) are the first 6 in gregs, + * in that order, so those arguments can just be copied to + * the gregs array. + */ + va_start(ap, argc); + for (i = 0; i < 6 && argc > 0; i++) { + argc--; + /* LINTED __greg_t is safe */ + gr[i] = va_arg(ap, __greg_t); + } + + while (stackargs-- > 0) { + /* LINTED uintptr_t is safe */ + *sp++ = va_arg(ap, uintptr_t); + } + + va_end(ap); +} diff --git a/lib/libc/arch/x86_64/gen/resumecontext.S b/lib/libc/arch/x86_64/gen/resumecontext.S new file mode 100644 index 000000000000..642bc7a3f664 --- /dev/null +++ b/lib/libc/arch/x86_64/gen/resumecontext.S @@ -0,0 +1,68 @@ +/* $NetBSD: resumecontext.S,v 1.1 2003/01/30 02:07:31 fvdl Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + * + * Modified for x86-64 by fvdl@wasabisystems.com + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: resumecontext.S,v 1.1 2003/01/30 02:07:31 fvdl Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * This assembly-language implementation differs from the (obvious) + * C-language implementation only in not clobbering the previous + * function's return address (us), which is the point of the exercise. + */ + +NENTRY(_resumecontext) /* profiling prologue would clobber TOS */ + leaq -(8 + 784)(%rsp),%rdi /* retaddr + sizeof (ucontext_t) */ + andq $~15,%rdi /* align on _UC_UCONTEXT_ALIGN */ + movq %rdi,%rsp +#ifdef PIC + call PIC_PLT(_C_LABEL(_getcontext)) +#else + call _C_LABEL(_getcontext) +#endif + movq 4(%rsp),%rdi +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif + /* NOTREACHED */ diff --git a/lib/libc/arch/x86_64/gen/swapcontext.S b/lib/libc/arch/x86_64/gen/swapcontext.S new file mode 100644 index 000000000000..56e12572f793 --- /dev/null +++ b/lib/libc/arch/x86_64/gen/swapcontext.S @@ -0,0 +1,75 @@ +/* $NetBSD: swapcontext.S,v 1.1 2003/01/30 02:07:31 fvdl Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ + +#include + +#if defined(LIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: swapcontext.S,v 1.1 2003/01/30 02:07:31 fvdl Exp $") +#endif /* LIBC_SCCS && !lint */ + +/* + * 56 == offsetof(ucontext_t, uc_mcontext) + * 21 == _REG_RIP + * 24 == _REG_URSP + */ + +ENTRY(swapcontext) + pushq %r12 + pushq %r13 + movq %rdi,%r12 /* preserve oucp */ + movq %rsi,%r13 /* preserve ucp */ +#ifdef PIC + call PIC_PLT(_C_LABEL(getcontext)) +#else + call _C_LABEL(getcontext) +#endif + testl %eax,%eax + jnz 2f + movq (%rsp),%r11 + movq %r11,(56 + 21 * 8)(%r12) + leaq 8(%rsp),%r11 + movq %r11,(56 + 24 * 8)(%r12) + movq %r13,%rdi +#ifdef PIC + call PIC_PLT(_C_LABEL(setcontext)) +#else + call _C_LABEL(setcontext) +#endif +2: + popq %r13 + popq %r12 + ret diff --git a/lib/libc/arch/x86_64/sys/__sigaction14_sigtramp.c b/lib/libc/arch/x86_64/sys/__sigaction14_sigtramp.c index 368ad63aafd5..d8160e7a3b4e 100644 --- a/lib/libc/arch/x86_64/sys/__sigaction14_sigtramp.c +++ b/lib/libc/arch/x86_64/sys/__sigaction14_sigtramp.c @@ -1,4 +1,4 @@ -/* $NetBSD: __sigaction14_sigtramp.c,v 1.1 2002/07/09 23:32:39 thorpej Exp $ */ +/* $NetBSD: __sigaction14_sigtramp.c,v 1.2 2003/01/30 02:07:32 fvdl Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -43,8 +43,10 @@ #include "extern.h" + __weak_alias(__sigaction14, __libc_sigaction14) + int -__sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) +__libc_sigaction14(int sig, const struct sigaction *act, struct sigaction *oact) { extern int __sigtramp_sigcontext_1[]; diff --git a/lib/libc/arch/x86_64/sys/getcontext.S b/lib/libc/arch/x86_64/sys/getcontext.S new file mode 100644 index 000000000000..87e31cf73580 --- /dev/null +++ b/lib/libc/arch/x86_64/sys/getcontext.S @@ -0,0 +1,63 @@ +/* $NetBSD: getcontext.S,v 1.1 2003/01/30 02:07:32 fvdl Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Frank van der Linden for Wasabi Systems, Inc. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC + * 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. + */ + + +#include "SYS.h" + +#if defined(SYSLIBC_SCCS) && !defined(lint) + RCSID("$NetBSD: getcontext.S,v 1.1 2003/01/30 02:07:32 fvdl Exp $") +#endif + +#ifdef WEAK_ALIAS +WEAK_ALIAS(getcontext, _getcontext) +#endif + +/* + * 56 == offsetof(ucontext_t, uc_mcontext) + * 14 == _REG_RAX + * 21 == _REG_RIP + * 24 == _REG_URSP + */ + +_SYSCALL(_getcontext,getcontext) + movq (%rsp),%r11 + movq %r11,(56 + 21 * 8)(%rdi) + leaq 8(%rsp),%r11 + movq %r11,(56 + 24 * 8)(%rdi) + movq $0,(56 + 14 * 8)(%rdi) + xorl %eax,%eax + ret