1*e415d488SLionel Sambuc /* $NetBSD: _lwp.c,v 1.7 2012/03/22 05:36:50 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 2001 Wasabi Systems, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Written by Allen Briggs for Wasabi Systems, Inc.
82fe8fb19SBen Gras *
92fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
102fe8fb19SBen Gras * modification, are permitted provided that the following conditions
112fe8fb19SBen Gras * are met:
122fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
142fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
152fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
162fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
172fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
182fe8fb19SBen Gras * must display the following acknowledgement:
192fe8fb19SBen Gras * This product includes software developed for the NetBSD Project by
202fe8fb19SBen Gras * Wasabi Systems, Inc.
212fe8fb19SBen Gras * 4. The name of Wasabi Systems, Inc. may not be used to endorse
222fe8fb19SBen Gras * or promote products derived from this software without specific prior
232fe8fb19SBen Gras * written permission.
242fe8fb19SBen Gras *
252fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
262fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
272fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
282fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
292fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
312fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
322fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
332fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
342fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
352fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
362fe8fb19SBen Gras */
372fe8fb19SBen Gras
382fe8fb19SBen Gras #include <sys/cdefs.h>
392fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
40*e415d488SLionel Sambuc __RCSID("$NetBSD: _lwp.c,v 1.7 2012/03/22 05:36:50 matt Exp $");
412fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
422fe8fb19SBen Gras
432fe8fb19SBen Gras #include "namespace.h"
44*e415d488SLionel Sambuc #include <sys/param.h>
452fe8fb19SBen Gras #include <sys/types.h>
462fe8fb19SBen Gras #include <ucontext.h>
472fe8fb19SBen Gras #include <lwp.h>
482fe8fb19SBen Gras #include <stdlib.h>
492fe8fb19SBen Gras
502fe8fb19SBen Gras void
_lwp_makecontext(ucontext_t * u,void (* start)(void *),void * arg,void * tcb,caddr_t stack_base,size_t stack_size)512fe8fb19SBen Gras _lwp_makecontext(ucontext_t *u, void (*start)(void *), void *arg,
52*e415d488SLionel Sambuc void *tcb, caddr_t stack_base, size_t stack_size)
532fe8fb19SBen Gras {
54*e415d488SLionel Sambuc uintptr_t sp;
552fe8fb19SBen Gras
562fe8fb19SBen Gras getcontext(u);
572fe8fb19SBen Gras u->uc_link = NULL;
582fe8fb19SBen Gras
592fe8fb19SBen Gras u->uc_stack.ss_sp = stack_base;
602fe8fb19SBen Gras u->uc_stack.ss_size = stack_size;
612fe8fb19SBen Gras
62*e415d488SLionel Sambuc sp = (uintptr_t)stack_base + stack_size;
63*e415d488SLionel Sambuc sp -= STACK_ALIGNBYTES + 1;
64*e415d488SLionel Sambuc sp &= ~STACK_ALIGNBYTES;
652fe8fb19SBen Gras
66*e415d488SLionel Sambuc u->uc_mcontext.__gregs[3] = (uintptr_t) arg; /* arg1 */
67*e415d488SLionel Sambuc u->uc_mcontext.__gregs[1] = sp; /* stack */
68*e415d488SLionel Sambuc u->uc_mcontext.__gregs[33] = (uintptr_t) _lwp_exit; /* LR */
69*e415d488SLionel Sambuc u->uc_mcontext.__gregs[34] = (uintptr_t) start; /* PC */
70*e415d488SLionel Sambuc u->uc_mcontext.__gregs[_REG_R2] =
71*e415d488SLionel Sambuc (uintptr_t)tcb + TLS_TP_OFFSET + sizeof(struct tls_tcb);
722fe8fb19SBen Gras }
73