xref: /netbsd/lib/libc/arch/x86_64/gen/_lwp.c (revision fbfa05fb)
1*fbfa05fbSskrll /*	$NetBSD: _lwp.c,v 1.8 2023/04/02 07:26:18 skrll Exp $	*/
2894bd3adSfvdl 
3894bd3adSfvdl /*-
4894bd3adSfvdl  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5894bd3adSfvdl  * All rights reserved.
6894bd3adSfvdl  *
7894bd3adSfvdl  * This code is derived from software contributed to The NetBSD Foundation
8894bd3adSfvdl  * by Nathan J. Williams.
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 
3288c3eadbSlukem #include <sys/cdefs.h>
3388c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
34*fbfa05fbSskrll __RCSID("$NetBSD: _lwp.c,v 1.8 2023/04/02 07:26:18 skrll Exp $");
3588c3eadbSlukem #endif /* LIBC_SCCS and not lint */
3688c3eadbSlukem 
373b364b9bSkleink #include "namespace.h"
38894bd3adSfvdl #include <sys/types.h>
39894bd3adSfvdl #include <inttypes.h>
40894bd3adSfvdl #include <ucontext.h>
41894bd3adSfvdl #include <lwp.h>
42894bd3adSfvdl #include <stdlib.h>
43894bd3adSfvdl 
44894bd3adSfvdl void
_lwp_makecontext(ucontext_t * u,void (* start)(void *),void * arg,void * private,caddr_t stack_base,size_t stack_size)45894bd3adSfvdl _lwp_makecontext(ucontext_t *u, void (*start)(void *),
46894bd3adSfvdl     void *arg, void *private, caddr_t stack_base, size_t stack_size)
47894bd3adSfvdl {
48c1faa994Sjoerg 	__greg_t *gr = u->uc_mcontext.__gregs;
49894bd3adSfvdl 	void **sp;
50894bd3adSfvdl 
51894bd3adSfvdl 	getcontext(u);
52894bd3adSfvdl 	u->uc_link = NULL;
53894bd3adSfvdl 
54894bd3adSfvdl 	u->uc_stack.ss_sp = stack_base;
55894bd3adSfvdl 	u->uc_stack.ss_size = stack_size;
56894bd3adSfvdl 
57894bd3adSfvdl 	/* LINTED uintptr_t is safe */
58c1faa994Sjoerg 	gr[_REG_RIP] = (uintptr_t)start;
59894bd3adSfvdl 
609d87375bSjoerg 	sp = (void **) (((uintptr_t)(stack_base + stack_size) & ~15));
61894bd3adSfvdl 
62894bd3adSfvdl 	/* LINTED __greg_t is safe */
63c1faa994Sjoerg 	gr[_REG_RDI] = (__greg_t)arg;
64894bd3adSfvdl 	*--sp = (void *) _lwp_exit;
65894bd3adSfvdl 
66894bd3adSfvdl 	/* LINTED uintptr_t is safe */
67c1faa994Sjoerg 	gr[_REG_URSP] = (uintptr_t) sp;
68894bd3adSfvdl 
694c92f569Sjoerg 	u->uc_mcontext._mc_tlsbase = (uintptr_t)private;
704c92f569Sjoerg 	u->uc_flags |= _UC_TLSBASE;
71894bd3adSfvdl }
72