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