xref: /minix3/tests/lib/libc/sys/t_lwp_create.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_lwp_create.c,v 1.2 2012/05/22 09:23:39 martin Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc /*
30*11be35a1SLionel Sambuc  * This code is partly based on code by Joel Sing <joel at sing.id.au>
31*11be35a1SLionel Sambuc  */
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <atf-c.h>
34*11be35a1SLionel Sambuc #include <lwp.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc #include <stdlib.h>
37*11be35a1SLionel Sambuc #include <ucontext.h>
38*11be35a1SLionel Sambuc #include <inttypes.h>
39*11be35a1SLionel Sambuc #include <errno.h>
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #ifdef __alpha__
42*11be35a1SLionel Sambuc #include <machine/alpha_cpu.h>
43*11be35a1SLionel Sambuc #endif
44*11be35a1SLionel Sambuc #ifdef __amd64__
45*11be35a1SLionel Sambuc #include <machine/vmparam.h>
46*11be35a1SLionel Sambuc #include <machine/psl.h>
47*11be35a1SLionel Sambuc #endif
48*11be35a1SLionel Sambuc #ifdef __hppa__
49*11be35a1SLionel Sambuc #include <machine/psl.h>
50*11be35a1SLionel Sambuc #endif
51*11be35a1SLionel Sambuc #ifdef __i386__
52*11be35a1SLionel Sambuc #include <machine/segments.h>
53*11be35a1SLionel Sambuc #include <machine/psl.h>
54*11be35a1SLionel Sambuc #endif
55*11be35a1SLionel Sambuc #if defined(__m68k__) || defined(__sh3__) || defined __vax__
56*11be35a1SLionel Sambuc #include <machine/psl.h>
57*11be35a1SLionel Sambuc #endif
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc volatile lwpid_t the_lwp_id = 0;
60*11be35a1SLionel Sambuc 
lwp_main_func(void * arg)61*11be35a1SLionel Sambuc static void lwp_main_func(void* arg)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc 	the_lwp_id = _lwp_self();
64*11be35a1SLionel Sambuc 	_lwp_exit();
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc /*
68*11be35a1SLionel Sambuc  * Hard to document - see usage examples below.
69*11be35a1SLionel Sambuc  */
70*11be35a1SLionel Sambuc #define INVALID_UCONTEXT(ARCH,NAME,DESC)	\
71*11be35a1SLionel Sambuc static void ARCH##_##NAME(ucontext_t *);	\
72*11be35a1SLionel Sambuc ATF_TC(lwp_create_##ARCH##_fail_##NAME);	\
73*11be35a1SLionel Sambuc ATF_TC_HEAD(lwp_create_##ARCH##_fail_##NAME, tc)	\
74*11be35a1SLionel Sambuc {	\
75*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "verify rejection of invalid ucontext " \
76*11be35a1SLionel Sambuc 		"on " #ARCH " due to " DESC);	\
77*11be35a1SLionel Sambuc }	\
78*11be35a1SLionel Sambuc 	\
79*11be35a1SLionel Sambuc ATF_TC_BODY(lwp_create_##ARCH##_fail_##NAME, tc)	\
80*11be35a1SLionel Sambuc {	\
81*11be35a1SLionel Sambuc 	ucontext_t uc;		\
82*11be35a1SLionel Sambuc 	lwpid_t lid;		\
83*11be35a1SLionel Sambuc 	int error;		\
84*11be35a1SLionel Sambuc 				\
85*11be35a1SLionel Sambuc 	getcontext(&uc);	\
86*11be35a1SLionel Sambuc 	uc.uc_flags = _UC_CPU;	\
87*11be35a1SLionel Sambuc 	ARCH##_##NAME(&uc);	\
88*11be35a1SLionel Sambuc 				\
89*11be35a1SLionel Sambuc 	error = _lwp_create(&uc, 0, &lid);	\
90*11be35a1SLionel Sambuc 	ATF_REQUIRE(error != 0 && errno == EINVAL);	\
91*11be35a1SLionel Sambuc }	\
92*11be35a1SLionel Sambuc static void ARCH##_##NAME(ucontext_t *uc)	\
93*11be35a1SLionel Sambuc {
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc ATF_TC(lwp_create_works);
ATF_TC_HEAD(lwp_create_works,tc)97*11be35a1SLionel Sambuc ATF_TC_HEAD(lwp_create_works, tc)
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Verify creation of a lwp and waiting"
100*11be35a1SLionel Sambuc 	    " for it to finish");
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc 
ATF_TC_BODY(lwp_create_works,tc)103*11be35a1SLionel Sambuc ATF_TC_BODY(lwp_create_works, tc)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc 	ucontext_t uc;
106*11be35a1SLionel Sambuc 	lwpid_t lid;
107*11be35a1SLionel Sambuc 	int error;
108*11be35a1SLionel Sambuc 	void *stack;
109*11be35a1SLionel Sambuc 	static const size_t ssize = 16*1024;
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 	stack = malloc(ssize);
112*11be35a1SLionel Sambuc 	_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	error = _lwp_create(&uc, 0, &lid);
115*11be35a1SLionel Sambuc 	ATF_REQUIRE(error == 0);
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc 	error = _lwp_wait(lid, NULL);
118*11be35a1SLionel Sambuc 	ATF_REQUIRE(error == 0);
119*11be35a1SLionel Sambuc 	ATF_REQUIRE(lid == the_lwp_id);
120*11be35a1SLionel Sambuc }
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc INVALID_UCONTEXT(generic, no_uc_cpu, "not setting cpu registers")
123*11be35a1SLionel Sambuc 	uc->uc_flags &= ~_UC_CPU;
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc #ifdef __alpha__
127*11be35a1SLionel Sambuc INVALID_UCONTEXT(alpha, pslset, "trying to clear the USERMODE flag")
128*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PS] &= ~ALPHA_PSL_USERMODE;
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc INVALID_UCONTEXT(alpha, pslclr, "trying to set a 'must be zero' flag")
131*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PS] |= ALPHA_PSL_IPL_HIGH;
132*11be35a1SLionel Sambuc }
133*11be35a1SLionel Sambuc #endif
134*11be35a1SLionel Sambuc #ifdef __amd64__
135*11be35a1SLionel Sambuc INVALID_UCONTEXT(amd64, untouchable_rflags, "forbidden rflags changed")
136*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_RFLAGS] |= PSL_MBZ;
137*11be35a1SLionel Sambuc }
138*11be35a1SLionel Sambuc /*
139*11be35a1SLionel Sambuc  * XXX: add invalid GS/DS selector tests
140*11be35a1SLionel Sambuc  */
141*11be35a1SLionel Sambuc INVALID_UCONTEXT(amd64, pc_too_high,
142*11be35a1SLionel Sambuc      "instruction pointer outside userland address space")
143*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_RIP] = VM_MAXUSER_ADDRESS;
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc #endif
146*11be35a1SLionel Sambuc #ifdef __arm__
147*11be35a1SLionel Sambuc INVALID_UCONTEXT(arm, invalid_mode, "psr or r15 set to non-user-mode")
148*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PC] |= 0x1f /*PSR_SYS32_MODE*/;
149*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_CPSR] |= 0x03 /*R15_MODE_SVC*/;
150*11be35a1SLionel Sambuc }
151*11be35a1SLionel Sambuc #endif
152*11be35a1SLionel Sambuc #ifdef __hppa__
153*11be35a1SLionel Sambuc INVALID_UCONTEXT(hppa, invalid_1, "set illegal bits in psw")
154*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PSW] |= PSW_MBZ;
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc INVALID_UCONTEXT(hppa, invalid_0, "clear illegal bits in psw")
157*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PSW] &= ~PSW_MBS;
158*11be35a1SLionel Sambuc }
159*11be35a1SLionel Sambuc #endif
160*11be35a1SLionel Sambuc #ifdef __i386__
161*11be35a1SLionel Sambuc INVALID_UCONTEXT(i386, untouchable_eflags, "changing forbidden eflags")
162*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_EFL] |= PSL_IOPL;
163*11be35a1SLionel Sambuc }
164*11be35a1SLionel Sambuc INVALID_UCONTEXT(i386, priv_escalation, "modifying priviledge level")
165*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_CS] &= ~SEL_RPL;
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc #endif
168*11be35a1SLionel Sambuc #ifdef __m68k__
169*11be35a1SLionel Sambuc INVALID_UCONTEXT(m68k, invalid_ps_bits,
170*11be35a1SLionel Sambuc     "setting forbidden bits in the ps register")
171*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PS] |= (PSL_MBZ|PSL_IPL|PSL_S);
172*11be35a1SLionel Sambuc }
173*11be35a1SLionel Sambuc #endif
174*11be35a1SLionel Sambuc #ifdef __sh3__
175*11be35a1SLionel Sambuc INVALID_UCONTEXT(sh3, modify_userstatic,
176*11be35a1SLionel Sambuc     "modifying illegal bits in the status register")
177*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_SR] |= PSL_MD;
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc #endif
180*11be35a1SLionel Sambuc #ifdef __sparc__
181*11be35a1SLionel Sambuc INVALID_UCONTEXT(sparc, pc_odd, "mis-aligned instruction pointer")
182*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PC] = 0x100002;
183*11be35a1SLionel Sambuc }
184*11be35a1SLionel Sambuc INVALID_UCONTEXT(sparc, npc_odd, "mis-aligned next instruction pointer")
185*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_nPC] = 0x100002;
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc INVALID_UCONTEXT(sparc, pc_null, "NULL instruction pointer")
188*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PC] = 0;
189*11be35a1SLionel Sambuc }
190*11be35a1SLionel Sambuc INVALID_UCONTEXT(sparc, npc_null, "NULL next instruction pointer")
191*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_nPC] = 0;
192*11be35a1SLionel Sambuc }
193*11be35a1SLionel Sambuc #endif
194*11be35a1SLionel Sambuc #ifdef __vax__
195*11be35a1SLionel Sambuc INVALID_UCONTEXT(vax, psl_0, "clearing forbidden bits in psl")
196*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PSL] &= ~(PSL_U | PSL_PREVU);
197*11be35a1SLionel Sambuc }
198*11be35a1SLionel Sambuc INVALID_UCONTEXT(vax, psl_1, "setting forbidden bits in psl")
199*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_IPL | PSL_IS;
200*11be35a1SLionel Sambuc }
201*11be35a1SLionel Sambuc INVALID_UCONTEXT(vax, psl_cm, "setting CM bit in psl")
202*11be35a1SLionel Sambuc 	uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_CM;
203*11be35a1SLionel Sambuc }
204*11be35a1SLionel Sambuc #endif
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
207*11be35a1SLionel Sambuc {
208*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_works);
209*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_generic_fail_no_uc_cpu);
210*11be35a1SLionel Sambuc #ifdef __alpha__
211*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslset);
212*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslclr);
213*11be35a1SLionel Sambuc #endif
214*11be35a1SLionel Sambuc #ifdef __amd64__
215*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_untouchable_rflags);
216*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_pc_too_high);
217*11be35a1SLionel Sambuc #endif
218*11be35a1SLionel Sambuc #ifdef __arm__
219*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_arm_fail_invalid_mode);
220*11be35a1SLionel Sambuc #endif
221*11be35a1SLionel Sambuc #ifdef __hppa__
222*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_1);
223*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_0);
224*11be35a1SLionel Sambuc #endif
225*11be35a1SLionel Sambuc #ifdef __i386__
226*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_i386_fail_untouchable_eflags);
227*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_i386_fail_priv_escalation);
228*11be35a1SLionel Sambuc #endif
229*11be35a1SLionel Sambuc #ifdef __m68k__
230*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_m68k_fail_invalid_ps_bits);
231*11be35a1SLionel Sambuc #endif
232*11be35a1SLionel Sambuc #ifdef __sh3__
233*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_sh3_fail_modify_userstatic);
234*11be35a1SLionel Sambuc #endif
235*11be35a1SLionel Sambuc #ifdef __sparc__
236*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_odd);
237*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_odd);
238*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_null);
239*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_null);
240*11be35a1SLionel Sambuc #endif
241*11be35a1SLionel Sambuc #ifdef __vax__
242*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_0);
243*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_1);
244*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_cm);
245*11be35a1SLionel Sambuc #endif
246*11be35a1SLionel Sambuc 	return atf_no_error();
247*11be35a1SLionel Sambuc }
248