Merge from vendor branch NCURSES:
[dragonfly.git] / lib / libc / amd64 / gen / makecontext.c
1 /*
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libc/amd64/gen/makecontext.c,v 1.2 2003/12/05 01:36:44 peter Exp $
27  * $DragonFly: src/lib/libc/amd64/gen/Attic/makecontext.c,v 1.1 2004/02/02 05:43:14 dillon Exp $
28  */
29
30
31 #include <sys/types.h>
32 #include <sys/ucontext.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35
36 typedef void (*func_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
37     uint64_t);
38
39 /* Prototypes */
40 static void ctx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args);
41
42 __weak_reference(__makecontext, makecontext);
43
44 void
45 __makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
46 {
47         uint64_t *args;
48         uint64_t *sp;
49         va_list ap;
50         int i;
51
52         /* A valid context is required. */
53         if ((ucp == NULL) || (ucp->uc_mcontext.mc_len != sizeof(mcontext_t)))
54                 return;
55         else if ((argc < 0) || (argc > 6) || (ucp->uc_stack.ss_sp == NULL) ||
56             (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
57                 /*
58                  * This should really return -1 with errno set to ENOMEM
59                  * or something, but the spec says that makecontext is
60                  * a void function.   At least make sure that the context
61                  * isn't valid so it can't be used without an error.
62                  */
63                 ucp->uc_mcontext.mc_len = 0;
64                 return;
65         }
66
67         /* Align the stack to 16 bytes. */
68         sp = (uint64_t *)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
69         sp = (uint64_t *)((uint64_t)sp & -15UL);
70
71         /* Allocate space for a maximum of 6 arguments on the stack. */
72         args = sp - 6;
73
74         /*
75          * Account for arguments on stack and do the funky C entry alignment.
76          * This means that we need an 8-byte-odd alignment since the ABI expects
77          * the return address to be pushed, thus breaking the 16 byte alignment.
78          */
79         sp -= 7;
80
81         /* Add the arguments: */
82         va_start(ap, argc);
83         for (i = 0; i < argc; i++)
84                 args[i] = va_arg(ap, uint64_t);
85         va_end(ap);
86         for (i = argc; i < 6; i++)
87                 args[i] = 0;
88
89         ucp->uc_mcontext.mc_rdi = (register_t)ucp;
90         ucp->uc_mcontext.mc_rsi = (register_t)start;
91         ucp->uc_mcontext.mc_rdx = (register_t)args;
92         ucp->uc_mcontext.mc_rbp = (register_t)sp;
93         ucp->uc_mcontext.mc_rbx = (register_t)sp;
94         ucp->uc_mcontext.mc_rsp = (register_t)sp;
95         ucp->uc_mcontext.mc_rip = (register_t)ctx_wrapper;
96 }
97
98 static void
99 ctx_wrapper(ucontext_t *ucp, func_t func, uint64_t *args)
100 {
101         (*func)(args[0], args[1], args[2], args[3], args[4], args[5]);
102         if (ucp->uc_link == NULL)
103                 exit(0);
104         setcontext((const ucontext_t *)ucp->uc_link);
105         /* should never get here */
106         abort();
107         /* NOTREACHED */
108 }