xref: /minix3/lib/libc/arch/sparc/gen/_lwp.c (revision e415d488727a332a2c69df018aa35e2cecf4148a)
1*e415d488SLionel Sambuc /*	$NetBSD: _lwp.c,v 1.7 2012/03/21 00:34:04 christos 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/21 00:34:04 christos 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 *), void *arg,
422fe8fb19SBen Gras 		 void *private, caddr_t stack_base, size_t stack_size)
432fe8fb19SBen Gras {
442fe8fb19SBen Gras 	__greg_t *gr;
452fe8fb19SBen Gras 	unsigned long *sp;
462fe8fb19SBen Gras 
472fe8fb19SBen Gras 	getcontext(u);
482fe8fb19SBen Gras 	gr = u->uc_mcontext.__gregs;
492fe8fb19SBen Gras 
502fe8fb19SBen Gras 	u->uc_link = NULL;
512fe8fb19SBen Gras 
522fe8fb19SBen Gras 	u->uc_stack.ss_sp = stack_base;
532fe8fb19SBen Gras 	u->uc_stack.ss_size = stack_size;
542fe8fb19SBen Gras 
552fe8fb19SBen Gras 
56*e415d488SLionel Sambuc 	sp = (void *)(stack_base + stack_size);
572fe8fb19SBen Gras 	sp = (ulong *)((ulong)sp & ~0x07);
582fe8fb19SBen Gras 
592fe8fb19SBen Gras 	/* Make room for the fake caller stack frame (CCFSZ, only in words) */
602fe8fb19SBen Gras 	sp -= 8 + 8 + 1 + 6 + 1;
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	/* Entering (*start)(arg), return is to _lwp_exit */
632fe8fb19SBen Gras 	gr[_REG_PC] = (ulong) start;
642fe8fb19SBen Gras 	gr[_REG_nPC] = (ulong) start + 4;
652fe8fb19SBen Gras 	gr[_REG_O0] = (ulong)arg;
662fe8fb19SBen Gras 	gr[_REG_O6] = (ulong)sp;
672fe8fb19SBen Gras 	gr[_REG_O7] = (ulong)_lwp_exit - 8;
68*e415d488SLionel Sambuc 	gr[_REG_G7] = (ulong)private;
692fe8fb19SBen Gras 
702fe8fb19SBen Gras 	/* XXX: uwe: why do we need this? */
712fe8fb19SBen Gras 	/* create loopback in the window save area on the stack? */
722fe8fb19SBen Gras 	sp[8+6] = (ulong)sp;		/* %i6 */
732fe8fb19SBen Gras 	sp[8+7] = (ulong)_lwp_exit - 8;	/* %i7 */
742fe8fb19SBen Gras }
75