xref: /minix3/lib/libc/arch/sparc64/gen/_lwp.c (revision e415d488727a332a2c69df018aa35e2cecf4148a)
1*e415d488SLionel Sambuc /*	$NetBSD: _lwp.c,v 1.7 2012/03/17 20:48:58 martin Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2002 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
272fe8fb19SBen Gras  */
282fe8fb19SBen Gras 
292fe8fb19SBen Gras #include <sys/cdefs.h>
302fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*e415d488SLionel Sambuc __RCSID("$NetBSD: _lwp.c,v 1.7 2012/03/17 20:48:58 martin Exp $");
322fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include "namespace.h"
352fe8fb19SBen Gras #include <sys/types.h>
362fe8fb19SBen Gras #include <ucontext.h>
372fe8fb19SBen Gras #include <lwp.h>
382fe8fb19SBen Gras #include <stdlib.h>
392fe8fb19SBen Gras 
402fe8fb19SBen Gras void
_lwp_makecontext(ucontext_t * u,void (* start)(void *),void * arg,void * private,caddr_t stack_base,size_t stack_size)412fe8fb19SBen Gras _lwp_makecontext(ucontext_t *u, void (*start)(void *),
422fe8fb19SBen Gras 		      void *arg, void *private,
432fe8fb19SBen Gras 		      caddr_t stack_base, size_t stack_size)
442fe8fb19SBen Gras {
452fe8fb19SBen Gras 	__greg_t *gr;
462fe8fb19SBen Gras 	unsigned long *sp;
472fe8fb19SBen Gras 
482fe8fb19SBen Gras 	getcontext(u);
492fe8fb19SBen Gras 	gr = u->uc_mcontext.__gregs;
502fe8fb19SBen Gras 
512fe8fb19SBen Gras 	u->uc_link = NULL;
522fe8fb19SBen Gras 
532fe8fb19SBen Gras 	u->uc_stack.ss_sp = stack_base;
542fe8fb19SBen Gras 	u->uc_stack.ss_size = stack_size;
552fe8fb19SBen Gras 
56*e415d488SLionel Sambuc 	/*LINTED*/
572fe8fb19SBen Gras 	sp = (ulong *)(stack_base + stack_size);
582fe8fb19SBen Gras 	sp = (ulong *)((ulong)sp & ~0x0f);
592fe8fb19SBen Gras 
602fe8fb19SBen Gras 	sp -= 8 + 8 + 6;
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	sp[8]  = (ulong)arg;
632fe8fb19SBen Gras 	sp[14] = (ulong)sp - 2047;
642fe8fb19SBen Gras 	sp[15] = (ulong)_lwp_exit - 8;
652fe8fb19SBen Gras 
66*e415d488SLionel Sambuc 	/*LINTED*/
672fe8fb19SBen Gras 	sp = (ulong *)((caddr_t)sp - 2047);
682fe8fb19SBen Gras 
692fe8fb19SBen Gras 	gr[_REG_PC] = (ulong) start;
702fe8fb19SBen Gras 	gr[_REG_nPC] = (ulong) start + 4;
712fe8fb19SBen Gras 
722fe8fb19SBen Gras 	gr[_REG_O0] = (ulong) arg;
732fe8fb19SBen Gras 	gr[_REG_O6] = (ulong) sp;
742fe8fb19SBen Gras 	gr[_REG_O7] = (ulong)_lwp_exit - 8;
75*e415d488SLionel Sambuc 	gr[_REG_G7] = (ulong)private;
762fe8fb19SBen Gras }
77