xref: /minix3/lib/libc/arch/arm/gen/_lwp.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: _lwp.c,v 1.8 2012/03/22 09:32:04 he Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 2001 Wasabi Systems, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Written by Jason R. Thorpe 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*f14fb602SLionel Sambuc __RCSID("$NetBSD: _lwp.c,v 1.8 2012/03/22 09:32:04 he Exp $");
412fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
422fe8fb19SBen Gras 
432fe8fb19SBen Gras #include "namespace.h"
442fe8fb19SBen Gras #include <sys/types.h>
452fe8fb19SBen Gras #include <ucontext.h>
462fe8fb19SBen Gras #include <lwp.h>
472fe8fb19SBen Gras #include <stdlib.h>
482fe8fb19SBen Gras 
492fe8fb19SBen Gras void
_lwp_makecontext(ucontext_t * u,void (* start)(void *),void * arg,void * private,caddr_t stack_base,size_t stack_size)502fe8fb19SBen Gras _lwp_makecontext(ucontext_t *u, void (*start)(void *),
512fe8fb19SBen Gras     void *arg, void *private, caddr_t stack_base, size_t stack_size)
522fe8fb19SBen Gras {
53*f14fb602SLionel Sambuc 	uintptr_t sp;
542fe8fb19SBen Gras 
552fe8fb19SBen Gras 	getcontext(u);
562fe8fb19SBen Gras 	u->uc_link = NULL;
572fe8fb19SBen Gras 
582fe8fb19SBen Gras 	u->uc_stack.ss_sp = stack_base;
592fe8fb19SBen Gras 	u->uc_stack.ss_size = stack_size;
602fe8fb19SBen Gras 
61*f14fb602SLionel Sambuc 	sp = (uintptr_t)stack_base + stack_size;
622fe8fb19SBen Gras 	/*
632fe8fb19SBen Gras 	 * Note: We make sure the stack is 8-byte aligned, here.
642fe8fb19SBen Gras 	 */
652fe8fb19SBen Gras 
66*f14fb602SLionel Sambuc 	u->uc_mcontext.__gregs[_REG_R0] = (__greg_t)(uintptr_t)arg;
672fe8fb19SBen Gras 	u->uc_mcontext.__gregs[_REG_SP] = ((__greg_t)sp) & ~7;
68*f14fb602SLionel Sambuc 	u->uc_mcontext.__gregs[_REG_LR] = (__greg_t)(uintptr_t)_lwp_exit;
69*f14fb602SLionel Sambuc 	u->uc_mcontext.__gregs[_REG_PC] = (__greg_t)(uintptr_t)start;
70*f14fb602SLionel Sambuc 	u->uc_mcontext._mc_tlsbase = (__greg_t)(uintptr_t)private;
71*f14fb602SLionel Sambuc 	u->uc_flags |= _UC_TLSBASE;
722fe8fb19SBen Gras }
73