1*0d34d14bSdsl /* $NetBSD: makecontext.c,v 1.4 2009/07/30 20:57:17 dsl Exp $ */
2894bd3adSfvdl
3894bd3adSfvdl /*-
4894bd3adSfvdl * Copyright (c) 1999 The NetBSD Foundation, Inc.
5894bd3adSfvdl * All rights reserved.
6894bd3adSfvdl *
7894bd3adSfvdl * This code is derived from software contributed to The NetBSD Foundation
8894bd3adSfvdl * by Klaus Klein.
9894bd3adSfvdl *
10894bd3adSfvdl * Redistribution and use in source and binary forms, with or without
11894bd3adSfvdl * modification, are permitted provided that the following conditions
12894bd3adSfvdl * are met:
13894bd3adSfvdl * 1. Redistributions of source code must retain the above copyright
14894bd3adSfvdl * notice, this list of conditions and the following disclaimer.
15894bd3adSfvdl * 2. Redistributions in binary form must reproduce the above copyright
16894bd3adSfvdl * notice, this list of conditions and the following disclaimer in the
17894bd3adSfvdl * documentation and/or other materials provided with the distribution.
18894bd3adSfvdl *
19894bd3adSfvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20894bd3adSfvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21894bd3adSfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22894bd3adSfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23894bd3adSfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24894bd3adSfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25894bd3adSfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26894bd3adSfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27894bd3adSfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28894bd3adSfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29894bd3adSfvdl * POSSIBILITY OF SUCH DAMAGE.
30894bd3adSfvdl *
31894bd3adSfvdl * Modified from the i386 version for x86_64 by fvdl@wasabisystems.com.
32894bd3adSfvdl *
33894bd3adSfvdl */
34894bd3adSfvdl
35894bd3adSfvdl #include <sys/cdefs.h>
36894bd3adSfvdl #if defined(LIBC_SCCS) && !defined(lint)
37*0d34d14bSdsl __RCSID("$NetBSD: makecontext.c,v 1.4 2009/07/30 20:57:17 dsl Exp $");
38894bd3adSfvdl #endif
39894bd3adSfvdl
40894bd3adSfvdl #include <inttypes.h>
41894bd3adSfvdl #include <stddef.h>
42894bd3adSfvdl #include <ucontext.h>
43894bd3adSfvdl #include "extern.h"
44894bd3adSfvdl
45894bd3adSfvdl #include <stdarg.h>
46894bd3adSfvdl
47894bd3adSfvdl void
makecontext(ucontext_t * ucp,void (* func)(void),int argc,...)48894bd3adSfvdl makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
49894bd3adSfvdl {
50894bd3adSfvdl __greg_t *gr = ucp->uc_mcontext.__gregs;
51894bd3adSfvdl uintptr_t *sp;
52894bd3adSfvdl va_list ap;
53894bd3adSfvdl int stackargs, i;
54894bd3adSfvdl
55894bd3adSfvdl stackargs = argc - 6;
56894bd3adSfvdl
57894bd3adSfvdl /* LINTED __greg_t is safe */
58894bd3adSfvdl gr[_REG_RIP] = (__greg_t)func;
59894bd3adSfvdl
60894bd3adSfvdl /* LINTED uintptr_t is safe */
61894bd3adSfvdl sp = (uintptr_t *)
62894bd3adSfvdl ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
63894bd3adSfvdl
64894bd3adSfvdl /* LINTED uintptr_t is safe */
65e9ee1552Sdrochner sp = (uintptr_t *)(((uintptr_t)sp & ~15));
66894bd3adSfvdl sp--;
67894bd3adSfvdl if (stackargs > 0)
68894bd3adSfvdl sp -= stackargs;
69894bd3adSfvdl /* LINTED __greg_t is safe */
70*0d34d14bSdsl gr[_REG_RSP] = (__greg_t)sp;
71894bd3adSfvdl gr[_REG_RBP] = (__greg_t)0; /* Wipe out frame pointer. */
72894bd3adSfvdl
73894bd3adSfvdl /* Put return address on top of stack. */
74894bd3adSfvdl /* LINTED uintptr_t is safe */
75894bd3adSfvdl *sp++ = (uintptr_t)_resumecontext;
76894bd3adSfvdl
77894bd3adSfvdl /*
78894bd3adSfvdl * Construct argument list.
79894bd3adSfvdl * The registers used to pass the first 6 arguments
80894bd3adSfvdl * (rdi, rsi, rdx, rcx, r8, r9) are the first 6 in gregs,
81894bd3adSfvdl * in that order, so those arguments can just be copied to
82894bd3adSfvdl * the gregs array.
83894bd3adSfvdl */
84894bd3adSfvdl va_start(ap, argc);
85894bd3adSfvdl for (i = 0; i < 6 && argc > 0; i++) {
86894bd3adSfvdl argc--;
87894bd3adSfvdl /* LINTED __greg_t is safe */
88894bd3adSfvdl gr[i] = va_arg(ap, __greg_t);
89894bd3adSfvdl }
90894bd3adSfvdl
91894bd3adSfvdl while (stackargs-- > 0) {
92894bd3adSfvdl /* LINTED uintptr_t is safe */
93894bd3adSfvdl *sp++ = va_arg(ap, uintptr_t);
94894bd3adSfvdl }
95894bd3adSfvdl
96894bd3adSfvdl va_end(ap);
97894bd3adSfvdl }
98