1*626fac18Sandvar /* $NetBSD: t_lwp_create.c,v 1.4 2021/08/22 20:18:39 andvar Exp $ */
26c3cc552Smartin
36c3cc552Smartin /*-
46c3cc552Smartin * Copyright (c) 2012 The NetBSD Foundation, Inc.
56c3cc552Smartin * All rights reserved.
66c3cc552Smartin *
76c3cc552Smartin * Redistribution and use in source and binary forms, with or without
86c3cc552Smartin * modification, are permitted provided that the following conditions
96c3cc552Smartin * are met:
106c3cc552Smartin * 1. Redistributions of source code must retain the above copyright
116c3cc552Smartin * notice, this list of conditions and the following disclaimer.
126c3cc552Smartin * 2. Redistributions in binary form must reproduce the above copyright
136c3cc552Smartin * notice, this list of conditions and the following disclaimer in the
146c3cc552Smartin * documentation and/or other materials provided with the distribution.
156c3cc552Smartin *
166c3cc552Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
176c3cc552Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
186c3cc552Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
196c3cc552Smartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
206c3cc552Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
216c3cc552Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
226c3cc552Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
236c3cc552Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
246c3cc552Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
256c3cc552Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
266c3cc552Smartin * POSSIBILITY OF SUCH DAMAGE.
276c3cc552Smartin */
286c3cc552Smartin
296c3cc552Smartin /*
306c3cc552Smartin * This code is partly based on code by Joel Sing <joel at sing.id.au>
316c3cc552Smartin */
326c3cc552Smartin
336c3cc552Smartin #include <atf-c.h>
346c3cc552Smartin #include <lwp.h>
356c3cc552Smartin #include <stdio.h>
366c3cc552Smartin #include <stdlib.h>
376c3cc552Smartin #include <ucontext.h>
386c3cc552Smartin #include <inttypes.h>
396c3cc552Smartin #include <errno.h>
406c3cc552Smartin
416c3cc552Smartin #ifdef __alpha__
426c3cc552Smartin #include <machine/alpha_cpu.h>
436c3cc552Smartin #endif
446c3cc552Smartin #ifdef __amd64__
456c3cc552Smartin #include <machine/vmparam.h>
466c3cc552Smartin #include <machine/psl.h>
476c3cc552Smartin #endif
486c3cc552Smartin #ifdef __hppa__
496c3cc552Smartin #include <machine/psl.h>
506c3cc552Smartin #endif
516c3cc552Smartin #ifdef __i386__
526c3cc552Smartin #include <machine/segments.h>
536c3cc552Smartin #include <machine/psl.h>
546c3cc552Smartin #endif
556c3cc552Smartin #if defined(__m68k__) || defined(__sh3__) || defined __vax__
566c3cc552Smartin #include <machine/psl.h>
576c3cc552Smartin #endif
586c3cc552Smartin
596c3cc552Smartin volatile lwpid_t the_lwp_id = 0;
606c3cc552Smartin
lwp_main_func(void * arg)616c3cc552Smartin static void lwp_main_func(void* arg)
626c3cc552Smartin {
636c3cc552Smartin the_lwp_id = _lwp_self();
646c3cc552Smartin _lwp_exit();
656c3cc552Smartin }
666c3cc552Smartin
676c3cc552Smartin /*
686a653c66Smartin * Hard to document - see usage examples below.
696c3cc552Smartin */
706c3cc552Smartin #define INVALID_UCONTEXT(ARCH,NAME,DESC) \
716c3cc552Smartin static void ARCH##_##NAME(ucontext_t *); \
726c3cc552Smartin ATF_TC(lwp_create_##ARCH##_fail_##NAME); \
736c3cc552Smartin ATF_TC_HEAD(lwp_create_##ARCH##_fail_##NAME, tc) \
746c3cc552Smartin { \
756c3cc552Smartin atf_tc_set_md_var(tc, "descr", "verify rejection of invalid ucontext " \
766c3cc552Smartin "on " #ARCH " due to " DESC); \
776c3cc552Smartin } \
786c3cc552Smartin \
796c3cc552Smartin ATF_TC_BODY(lwp_create_##ARCH##_fail_##NAME, tc) \
806c3cc552Smartin { \
816c3cc552Smartin ucontext_t uc; \
826c3cc552Smartin lwpid_t lid; \
836c3cc552Smartin int error; \
846c3cc552Smartin \
856c3cc552Smartin getcontext(&uc); \
866c3cc552Smartin uc.uc_flags = _UC_CPU; \
876c3cc552Smartin ARCH##_##NAME(&uc); \
886c3cc552Smartin \
896c3cc552Smartin error = _lwp_create(&uc, 0, &lid); \
906c3cc552Smartin ATF_REQUIRE(error != 0 && errno == EINVAL); \
916c3cc552Smartin } \
926c3cc552Smartin static void ARCH##_##NAME(ucontext_t *uc) \
936c3cc552Smartin {
946c3cc552Smartin
956c3cc552Smartin
966c3cc552Smartin ATF_TC(lwp_create_works);
ATF_TC_HEAD(lwp_create_works,tc)976c3cc552Smartin ATF_TC_HEAD(lwp_create_works, tc)
986c3cc552Smartin {
996c3cc552Smartin atf_tc_set_md_var(tc, "descr", "Verify creation of a lwp and waiting"
1006c3cc552Smartin " for it to finish");
1016c3cc552Smartin }
1026c3cc552Smartin
ATF_TC_BODY(lwp_create_works,tc)1036c3cc552Smartin ATF_TC_BODY(lwp_create_works, tc)
1046c3cc552Smartin {
1056c3cc552Smartin ucontext_t uc;
1066c3cc552Smartin lwpid_t lid;
1076c3cc552Smartin int error;
1086c3cc552Smartin void *stack;
1096c3cc552Smartin static const size_t ssize = 16*1024;
1106c3cc552Smartin
1116c3cc552Smartin stack = malloc(ssize);
1126c3cc552Smartin _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
1136c3cc552Smartin
1146c3cc552Smartin error = _lwp_create(&uc, 0, &lid);
1156c3cc552Smartin ATF_REQUIRE(error == 0);
1166c3cc552Smartin
1176c3cc552Smartin error = _lwp_wait(lid, NULL);
1186c3cc552Smartin ATF_REQUIRE(error == 0);
1196c3cc552Smartin ATF_REQUIRE(lid == the_lwp_id);
1206c3cc552Smartin }
1216c3cc552Smartin
1229dd0cf53Sthorpej ATF_TC(lwp_create_bad_lid_ptr);
ATF_TC_HEAD(lwp_create_bad_lid_ptr,tc)1239dd0cf53Sthorpej ATF_TC_HEAD(lwp_create_bad_lid_ptr, tc)
1249dd0cf53Sthorpej {
1259dd0cf53Sthorpej atf_tc_set_md_var(tc, "descr",
1269dd0cf53Sthorpej "Verify _lwp_create() fails as expected with bad lid pointer");
1279dd0cf53Sthorpej }
1289dd0cf53Sthorpej
ATF_TC_BODY(lwp_create_bad_lid_ptr,tc)1299dd0cf53Sthorpej ATF_TC_BODY(lwp_create_bad_lid_ptr, tc)
1309dd0cf53Sthorpej {
1319dd0cf53Sthorpej ucontext_t uc;
1329dd0cf53Sthorpej int error;
1339dd0cf53Sthorpej int serrno;
1349dd0cf53Sthorpej void *stack;
1359dd0cf53Sthorpej static const size_t ssize = 16*1024;
1369dd0cf53Sthorpej
1379dd0cf53Sthorpej stack = malloc(ssize);
1389dd0cf53Sthorpej _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
1399dd0cf53Sthorpej
1409dd0cf53Sthorpej error = _lwp_create(&uc, 0, NULL);
1419dd0cf53Sthorpej serrno = errno;
1429dd0cf53Sthorpej ATF_REQUIRE(error == -1);
1439dd0cf53Sthorpej ATF_REQUIRE(serrno == EFAULT);
1449dd0cf53Sthorpej }
1459dd0cf53Sthorpej
1466c3cc552Smartin INVALID_UCONTEXT(generic, no_uc_cpu, "not setting cpu registers")
1476c3cc552Smartin uc->uc_flags &= ~_UC_CPU;
1486c3cc552Smartin }
1496c3cc552Smartin
1506c3cc552Smartin #ifdef __alpha__
1516c3cc552Smartin INVALID_UCONTEXT(alpha, pslset, "trying to clear the USERMODE flag")
1526c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PS] &= ~ALPHA_PSL_USERMODE;
1536c3cc552Smartin }
1546c3cc552Smartin INVALID_UCONTEXT(alpha, pslclr, "trying to set a 'must be zero' flag")
1556c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PS] |= ALPHA_PSL_IPL_HIGH;
1566c3cc552Smartin }
1576c3cc552Smartin #endif
1586c3cc552Smartin #ifdef __amd64__
1596c3cc552Smartin INVALID_UCONTEXT(amd64, untouchable_rflags, "forbidden rflags changed")
1606c3cc552Smartin uc->uc_mcontext.__gregs[_REG_RFLAGS] |= PSL_MBZ;
1616c3cc552Smartin }
1626c3cc552Smartin /*
1636c3cc552Smartin * XXX: add invalid GS/DS selector tests
1646c3cc552Smartin */
1656c3cc552Smartin INVALID_UCONTEXT(amd64, pc_too_high,
1666c3cc552Smartin "instruction pointer outside userland address space")
1676c3cc552Smartin uc->uc_mcontext.__gregs[_REG_RIP] = VM_MAXUSER_ADDRESS;
1686c3cc552Smartin }
1696c3cc552Smartin #endif
1706c3cc552Smartin #ifdef __arm__
1716c3cc552Smartin INVALID_UCONTEXT(arm, invalid_mode, "psr or r15 set to non-user-mode")
1726c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PC] |= 0x1f /*PSR_SYS32_MODE*/;
1736c3cc552Smartin uc->uc_mcontext.__gregs[_REG_CPSR] |= 0x03 /*R15_MODE_SVC*/;
1746c3cc552Smartin }
1756c3cc552Smartin #endif
1766c3cc552Smartin #ifdef __hppa__
1776c3cc552Smartin INVALID_UCONTEXT(hppa, invalid_1, "set illegal bits in psw")
1786c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PSW] |= PSW_MBZ;
1796c3cc552Smartin }
1806c3cc552Smartin INVALID_UCONTEXT(hppa, invalid_0, "clear illegal bits in psw")
1816c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PSW] &= ~PSW_MBS;
1826c3cc552Smartin }
1836c3cc552Smartin #endif
1846c3cc552Smartin #ifdef __i386__
1856c3cc552Smartin INVALID_UCONTEXT(i386, untouchable_eflags, "changing forbidden eflags")
1866c3cc552Smartin uc->uc_mcontext.__gregs[_REG_EFL] |= PSL_IOPL;
1876c3cc552Smartin }
188*626fac18Sandvar INVALID_UCONTEXT(i386, priv_escalation, "modifying privilege level")
1896c3cc552Smartin uc->uc_mcontext.__gregs[_REG_CS] &= ~SEL_RPL;
1906c3cc552Smartin }
1916c3cc552Smartin #endif
1926c3cc552Smartin #ifdef __m68k__
1936c3cc552Smartin INVALID_UCONTEXT(m68k, invalid_ps_bits,
1946c3cc552Smartin "setting forbidden bits in the ps register")
1956c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PS] |= (PSL_MBZ|PSL_IPL|PSL_S);
1966c3cc552Smartin }
1976c3cc552Smartin #endif
1986c3cc552Smartin #ifdef __sh3__
1996c3cc552Smartin INVALID_UCONTEXT(sh3, modify_userstatic,
2006c3cc552Smartin "modifying illegal bits in the status register")
2016c3cc552Smartin uc->uc_mcontext.__gregs[_REG_SR] |= PSL_MD;
2026c3cc552Smartin }
2036c3cc552Smartin #endif
2046c3cc552Smartin #ifdef __sparc__
2056c3cc552Smartin INVALID_UCONTEXT(sparc, pc_odd, "mis-aligned instruction pointer")
2066c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PC] = 0x100002;
2076c3cc552Smartin }
2086c3cc552Smartin INVALID_UCONTEXT(sparc, npc_odd, "mis-aligned next instruction pointer")
2096c3cc552Smartin uc->uc_mcontext.__gregs[_REG_nPC] = 0x100002;
2106c3cc552Smartin }
2116c3cc552Smartin INVALID_UCONTEXT(sparc, pc_null, "NULL instruction pointer")
2126c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PC] = 0;
2136c3cc552Smartin }
2146c3cc552Smartin INVALID_UCONTEXT(sparc, npc_null, "NULL next instruction pointer")
2156c3cc552Smartin uc->uc_mcontext.__gregs[_REG_nPC] = 0;
2166c3cc552Smartin }
2176c3cc552Smartin #endif
2186c3cc552Smartin #ifdef __vax__
2196c3cc552Smartin INVALID_UCONTEXT(vax, psl_0, "clearing forbidden bits in psl")
2206c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PSL] &= ~(PSL_U | PSL_PREVU);
2216c3cc552Smartin }
2226c3cc552Smartin INVALID_UCONTEXT(vax, psl_1, "setting forbidden bits in psl")
2236c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_IPL | PSL_IS;
2246c3cc552Smartin }
2256c3cc552Smartin INVALID_UCONTEXT(vax, psl_cm, "setting CM bit in psl")
2266c3cc552Smartin uc->uc_mcontext.__gregs[_REG_PSL] |= PSL_CM;
2276c3cc552Smartin }
2286c3cc552Smartin #endif
2296c3cc552Smartin
2306c3cc552Smartin ATF_TP_ADD_TCS(tp)
2316c3cc552Smartin {
2326c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_works);
2339dd0cf53Sthorpej ATF_TP_ADD_TC(tp, lwp_create_bad_lid_ptr);
2346c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_generic_fail_no_uc_cpu);
2356c3cc552Smartin #ifdef __alpha__
2366c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslset);
2376c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_alpha_fail_pslclr);
2386c3cc552Smartin #endif
2396c3cc552Smartin #ifdef __amd64__
2406c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_untouchable_rflags);
2416c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_amd64_fail_pc_too_high);
2426c3cc552Smartin #endif
2436c3cc552Smartin #ifdef __arm__
2446c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_arm_fail_invalid_mode);
2456c3cc552Smartin #endif
2466c3cc552Smartin #ifdef __hppa__
2476c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_1);
2486c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_hppa_fail_invalid_0);
2496c3cc552Smartin #endif
2506c3cc552Smartin #ifdef __i386__
2516c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_i386_fail_untouchable_eflags);
2526c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_i386_fail_priv_escalation);
2536c3cc552Smartin #endif
2546c3cc552Smartin #ifdef __m68k__
2556c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_m68k_fail_invalid_ps_bits);
2566c3cc552Smartin #endif
2576c3cc552Smartin #ifdef __sh3__
2586c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_sh3_fail_modify_userstatic);
2596c3cc552Smartin #endif
2606c3cc552Smartin #ifdef __sparc__
2616c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_odd);
2626c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_odd);
2636c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_pc_null);
2646c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_sparc_fail_npc_null);
2656c3cc552Smartin #endif
2666c3cc552Smartin #ifdef __vax__
2676c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_0);
2686c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_1);
2696c3cc552Smartin ATF_TP_ADD_TC(tp, lwp_create_vax_fail_psl_cm);
2706c3cc552Smartin #endif
2716c3cc552Smartin return atf_no_error();
2726c3cc552Smartin }
273