_lwp.c revision 1.2 1 1.2 christos /* $NetBSD: _lwp.c,v 1.2 2024/11/30 01:04:03 christos Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * Written by Allen Briggs for Wasabi Systems, Inc.
8 1.1 matt *
9 1.1 matt * Redistribution and use in source and binary forms, with or without
10 1.1 matt * modification, are permitted provided that the following conditions
11 1.1 matt * are met:
12 1.1 matt * 1. Redistributions of source code must retain the above copyright
13 1.1 matt * notice, this list of conditions and the following disclaimer.
14 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 matt * notice, this list of conditions and the following disclaimer in the
16 1.1 matt * documentation and/or other materials provided with the distribution.
17 1.1 matt * 3. All advertising materials mentioning features or use of this software
18 1.1 matt * must display the following acknowledgement:
19 1.1 matt * This product includes software developed for the NetBSD Project by
20 1.1 matt * Wasabi Systems, Inc.
21 1.1 matt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 matt * or promote products derived from this software without specific prior
23 1.1 matt * written permission.
24 1.1 matt *
25 1.1 matt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 matt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
36 1.1 matt */
37 1.1 matt
38 1.1 matt #include <sys/cdefs.h>
39 1.1 matt #if defined(LIBC_SCCS) && !defined(lint)
40 1.2 christos __RCSID("$NetBSD: _lwp.c,v 1.2 2024/11/30 01:04:03 christos Exp $");
41 1.1 matt #endif /* LIBC_SCCS and not lint */
42 1.1 matt
43 1.1 matt #include "namespace.h"
44 1.1 matt #include <sys/param.h>
45 1.1 matt #include <sys/types.h>
46 1.1 matt #include <ucontext.h>
47 1.1 matt #include <lwp.h>
48 1.1 matt #include <stdlib.h>
49 1.1 matt
50 1.2 christos #include <machine/lwp_private.h>
51 1.2 christos
52 1.1 matt void
53 1.1 matt _lwp_makecontext(ucontext_t *u, void (*start)(void *), void *arg,
54 1.1 matt void *tcb, caddr_t stack_base, size_t stack_size)
55 1.1 matt {
56 1.1 matt uintptr_t sp;
57 1.1 matt
58 1.1 matt getcontext(u);
59 1.1 matt u->uc_link = NULL;
60 1.1 matt
61 1.1 matt u->uc_stack.ss_sp = stack_base;
62 1.1 matt u->uc_stack.ss_size = stack_size;
63 1.1 matt
64 1.1 matt sp = (uintptr_t)stack_base + stack_size;
65 1.1 matt sp -= STACK_ALIGNBYTES + 1;
66 1.1 matt sp &= ~STACK_ALIGNBYTES;
67 1.1 matt
68 1.1 matt u->uc_mcontext.__gregs[_REG_RV] = (uintptr_t)arg; /* arg1 */
69 1.1 matt u->uc_mcontext.__gregs[_REG_SP] = (uintptr_t)sp; /* stack */
70 1.1 matt u->uc_mcontext.__gregs[_REG_RA] = (uintptr_t)_lwp_exit; /* RA */
71 1.1 matt u->uc_mcontext.__gregs[_REG_PC] = (uintptr_t)start; /* PC */
72 1.1 matt u->uc_mcontext.__gregs[_REG_TP] =
73 1.1 matt (uintptr_t)tcb + TLS_TP_OFFSET + sizeof(struct tls_tcb);
74 1.1 matt }
75