Home | History | Annotate | Line # | Download | only in sys
t_ptrace_x86_wait.h revision 1.27
      1  1.27    mgorny /*	$NetBSD: t_ptrace_x86_wait.h,v 1.27 2020/10/09 17:43:30 mgorny Exp $	*/
      2   1.1     kamil 
      3   1.1     kamil /*-
      4   1.9     kamil  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
      5   1.1     kamil  * All rights reserved.
      6   1.1     kamil  *
      7   1.1     kamil  * Redistribution and use in source and binary forms, with or without
      8   1.1     kamil  * modification, are permitted provided that the following conditions
      9   1.1     kamil  * are met:
     10   1.1     kamil  * 1. Redistributions of source code must retain the above copyright
     11   1.1     kamil  *    notice, this list of conditions and the following disclaimer.
     12   1.1     kamil  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     kamil  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     kamil  *    documentation and/or other materials provided with the distribution.
     15   1.1     kamil  *
     16   1.1     kamil  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1     kamil  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1     kamil  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1     kamil  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1     kamil  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1     kamil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1     kamil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1     kamil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1     kamil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1     kamil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1     kamil  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1     kamil  */
     28   1.1     kamil 
     29   1.1     kamil #if defined(__i386__) || defined(__x86_64__)
     30   1.1     kamil union u {
     31   1.1     kamil 	unsigned long raw;
     32   1.1     kamil 	struct {
     33   1.1     kamil 		unsigned long local_dr0_breakpoint : 1;		/* 0 */
     34   1.1     kamil 		unsigned long global_dr0_breakpoint : 1;	/* 1 */
     35   1.1     kamil 		unsigned long local_dr1_breakpoint : 1;		/* 2 */
     36   1.1     kamil 		unsigned long global_dr1_breakpoint : 1;	/* 3 */
     37   1.1     kamil 		unsigned long local_dr2_breakpoint : 1;		/* 4 */
     38   1.1     kamil 		unsigned long global_dr2_breakpoint : 1;	/* 5 */
     39   1.1     kamil 		unsigned long local_dr3_breakpoint : 1;		/* 6 */
     40   1.1     kamil 		unsigned long global_dr3_breakpoint : 1;	/* 7 */
     41   1.1     kamil 		unsigned long local_exact_breakpt : 1;		/* 8 */
     42   1.1     kamil 		unsigned long global_exact_breakpt : 1;		/* 9 */
     43   1.1     kamil 		unsigned long reserved_10 : 1;			/* 10 */
     44   1.1     kamil 		unsigned long rest_trans_memory : 1;		/* 11 */
     45   1.1     kamil 		unsigned long reserved_12 : 1;			/* 12 */
     46   1.1     kamil 		unsigned long general_detect_enable : 1;	/* 13 */
     47   1.1     kamil 		unsigned long reserved_14 : 1;			/* 14 */
     48   1.1     kamil 		unsigned long reserved_15 : 1;			/* 15 */
     49   1.1     kamil 		unsigned long condition_dr0 : 2;		/* 16-17 */
     50   1.1     kamil 		unsigned long len_dr0 : 2;			/* 18-19 */
     51   1.1     kamil 		unsigned long condition_dr1 : 2;		/* 20-21 */
     52   1.1     kamil 		unsigned long len_dr1 : 2;			/* 22-23 */
     53   1.1     kamil 		unsigned long condition_dr2 : 2;		/* 24-25 */
     54   1.1     kamil 		unsigned long len_dr2 : 2;			/* 26-27 */
     55   1.1     kamil 		unsigned long condition_dr3 : 2;		/* 28-29 */
     56   1.1     kamil 		unsigned long len_dr3 : 2;			/* 30-31 */
     57   1.1     kamil 	} bits;
     58   1.1     kamil };
     59   1.1     kamil 
     60   1.1     kamil ATF_TC(dbregs_print);
     61   1.1     kamil ATF_TC_HEAD(dbregs_print, tc)
     62   1.1     kamil {
     63   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
     64   1.1     kamil 	    "Verify plain PT_GETDBREGS with printing Debug Registers");
     65   1.1     kamil }
     66   1.1     kamil 
     67   1.1     kamil ATF_TC_BODY(dbregs_print, tc)
     68   1.1     kamil {
     69   1.1     kamil 	const int exitval = 5;
     70   1.1     kamil 	const int sigval = SIGSTOP;
     71   1.1     kamil 	pid_t child, wpid;
     72   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
     73   1.1     kamil 	int status;
     74   1.1     kamil #endif
     75   1.1     kamil 	struct dbreg r;
     76   1.1     kamil 	size_t i;
     77   1.1     kamil 
     78   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
     79   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
     80   1.1     kamil 	if (child == 0) {
     81   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
     82   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
     83   1.1     kamil 
     84   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
     85   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
     86   1.1     kamil 
     87   1.2  christos 		DPRINTF("Before exiting of the child process\n");
     88   1.1     kamil 		_exit(exitval);
     89   1.1     kamil 	}
     90   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
     91   1.1     kamil 
     92   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
     93   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
     94   1.1     kamil 
     95   1.1     kamil 	validate_status_stopped(status, sigval);
     96   1.1     kamil 
     97   1.2  christos 	DPRINTF("Call GETDBREGS for the child process\n");
     98   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r, 0) != -1);
     99   1.1     kamil 
    100   1.2  christos 	DPRINTF("State of the debug registers:\n");
    101   1.1     kamil 	for (i = 0; i < __arraycount(r.dr); i++)
    102   1.2  christos 		DPRINTF("r[%zu]=%" PRIxREGISTER "\n", i, r.dr[i]);
    103   1.1     kamil 
    104   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
    105   1.1     kamil 	    "without signal to be sent\n");
    106   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    107   1.1     kamil 
    108   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    109   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    110   1.1     kamil 
    111   1.1     kamil 	validate_status_exited(status, exitval);
    112   1.1     kamil 
    113   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    114   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    115   1.1     kamil }
    116   1.1     kamil 
    117   1.1     kamil 
    118   1.1     kamil enum dbreg_preserve_mode {
    119   1.1     kamil 	dbreg_preserve_mode_none,
    120   1.1     kamil 	dbreg_preserve_mode_yield,
    121   1.1     kamil 	dbreg_preserve_mode_continued
    122   1.1     kamil };
    123   1.1     kamil 
    124   1.1     kamil static void
    125   1.1     kamil dbreg_preserve(int reg, enum dbreg_preserve_mode mode)
    126   1.1     kamil {
    127   1.1     kamil 	const int exitval = 5;
    128   1.1     kamil 	const int sigval = SIGSTOP;
    129   1.1     kamil 	pid_t child, wpid;
    130   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
    131   1.1     kamil 	int status;
    132   1.1     kamil #endif
    133   1.1     kamil 	struct dbreg r1;
    134   1.1     kamil 	struct dbreg r2;
    135   1.1     kamil 	size_t i;
    136   1.1     kamil 	int watchme;
    137   1.1     kamil 
    138   1.5     kamil 	if (!can_we_set_dbregs()) {
    139   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
    140   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
    141   1.5     kamil 	}
    142   1.5     kamil 
    143   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
    144   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
    145   1.1     kamil 	if (child == 0) {
    146   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
    147   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
    148   1.1     kamil 
    149   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
    150   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
    151   1.1     kamil 
    152   1.1     kamil 		if (mode == dbreg_preserve_mode_continued) {
    153   1.2  christos 			DPRINTF("Before raising %s from child\n",
    154   1.1     kamil 			       strsignal(sigval));
    155   1.1     kamil 			FORKEE_ASSERT(raise(sigval) == 0);
    156   1.1     kamil 		}
    157   1.1     kamil 
    158   1.2  christos 		DPRINTF("Before exiting of the child process\n");
    159   1.1     kamil 		_exit(exitval);
    160   1.1     kamil 	}
    161   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
    162   1.1     kamil 
    163   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    164   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    165   1.1     kamil 
    166   1.1     kamil 	validate_status_stopped(status, sigval);
    167   1.1     kamil 
    168   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
    169   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
    170   1.1     kamil 
    171   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
    172   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
    173   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
    174   1.1     kamil 
    175   1.1     kamil 	r1.dr[reg] = (long)(intptr_t)&watchme;
    176   1.2  christos 	DPRINTF("Set DR0 (r1.dr[%d]) to new value %" PRIxREGISTER "\n",
    177   1.1     kamil 	    reg, r1.dr[reg]);
    178   1.1     kamil 
    179   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
    180   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
    181   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
    182   1.1     kamil 
    183   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
    184   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
    185   1.1     kamil 
    186   1.1     kamil 	switch (mode) {
    187   1.1     kamil 	case dbreg_preserve_mode_none:
    188   1.1     kamil 		break;
    189   1.1     kamil 	case dbreg_preserve_mode_yield:
    190   1.2  christos 		DPRINTF("Yields a processor voluntarily and gives other "
    191   1.1     kamil 		       "threads a chance to run without waiting for an "
    192   1.1     kamil 		       "involuntary preemptive switch\n");
    193   1.1     kamil 		sched_yield();
    194   1.1     kamil 		break;
    195   1.1     kamil 	case dbreg_preserve_mode_continued:
    196   1.2  christos 		DPRINTF("Call CONTINUE for the child process\n");
    197   1.2  christos 	        SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    198   1.1     kamil 
    199   1.2  christos 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    200   1.1     kamil 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    201   1.1     kamil 
    202   1.1     kamil 		validate_status_stopped(status, sigval);
    203   1.1     kamil 		break;
    204   1.1     kamil 	}
    205   1.1     kamil 
    206   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r2)\n");
    207   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r2, 0) != -1);
    208   1.1     kamil 
    209   1.2  christos 	DPRINTF("Assert that (r1) and (r2) are the same\n");
    210   1.2  christos 	SYSCALL_REQUIRE(memcmp(&r1, &r2, sizeof(r1)) == 0);
    211   1.1     kamil 
    212   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
    213   1.1     kamil 	    "without signal to be sent\n");
    214   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    215   1.1     kamil 
    216   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    217   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    218   1.1     kamil 
    219   1.1     kamil 	validate_status_exited(status, exitval);
    220   1.1     kamil 
    221   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    222   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    223   1.1     kamil }
    224   1.1     kamil 
    225   1.1     kamil 
    226   1.1     kamil ATF_TC(dbregs_preserve_dr0);
    227   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr0, tc)
    228   1.1     kamil {
    229   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    230   1.1     kamil 	    "Verify that setting DR0 is preserved across ptrace(2) calls");
    231   1.1     kamil }
    232   1.1     kamil 
    233   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr0, tc)
    234   1.1     kamil {
    235   1.1     kamil 	dbreg_preserve(0, dbreg_preserve_mode_none);
    236   1.1     kamil }
    237   1.1     kamil 
    238   1.1     kamil ATF_TC(dbregs_preserve_dr1);
    239   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr1, tc)
    240   1.1     kamil {
    241   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    242   1.1     kamil 	    "Verify that setting DR1 is preserved across ptrace(2) calls");
    243   1.1     kamil }
    244   1.1     kamil 
    245   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr1, tc)
    246   1.1     kamil {
    247   1.1     kamil 	dbreg_preserve(1, dbreg_preserve_mode_none);
    248   1.1     kamil }
    249   1.1     kamil 
    250   1.1     kamil ATF_TC(dbregs_preserve_dr2);
    251   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr2, tc)
    252   1.1     kamil {
    253   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    254   1.1     kamil 	    "Verify that setting DR2 is preserved across ptrace(2) calls");
    255   1.1     kamil }
    256   1.1     kamil 
    257   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr2, tc)
    258   1.1     kamil {
    259   1.1     kamil 	dbreg_preserve(2, dbreg_preserve_mode_none);
    260   1.1     kamil }
    261   1.1     kamil 
    262   1.1     kamil ATF_TC(dbregs_preserve_dr3);
    263   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr3, tc)
    264   1.1     kamil {
    265   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    266   1.1     kamil 	    "Verify that setting DR3 is preserved across ptrace(2) calls");
    267   1.1     kamil }
    268   1.1     kamil 
    269   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr3, tc)
    270   1.1     kamil {
    271   1.1     kamil 	dbreg_preserve(3, dbreg_preserve_mode_none);
    272   1.1     kamil }
    273   1.1     kamil 
    274   1.1     kamil ATF_TC(dbregs_preserve_dr0_yield);
    275   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr0_yield, tc)
    276   1.1     kamil {
    277   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    278   1.1     kamil 	    "Verify that setting DR0 is preserved across ptrace(2) calls with "
    279   1.1     kamil 	    "scheduler yield");
    280   1.1     kamil }
    281   1.1     kamil 
    282   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr0_yield, tc)
    283   1.1     kamil {
    284   1.1     kamil 	dbreg_preserve(0, dbreg_preserve_mode_yield);
    285   1.1     kamil }
    286   1.1     kamil 
    287   1.1     kamil ATF_TC(dbregs_preserve_dr1_yield);
    288   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr1_yield, tc)
    289   1.1     kamil {
    290   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    291   1.1     kamil 	    "Verify that setting DR1 is preserved across ptrace(2) calls with "
    292   1.1     kamil 	    "scheduler yield");
    293   1.1     kamil }
    294   1.1     kamil 
    295   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr1_yield, tc)
    296   1.1     kamil {
    297   1.1     kamil 	dbreg_preserve(0, dbreg_preserve_mode_yield);
    298   1.1     kamil }
    299   1.1     kamil 
    300   1.1     kamil ATF_TC(dbregs_preserve_dr2_yield);
    301   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr2_yield, tc)
    302   1.1     kamil {
    303   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    304   1.1     kamil 	    "Verify that setting DR2 is preserved across ptrace(2) calls with "
    305   1.1     kamil 	    "scheduler yield");
    306   1.1     kamil }
    307   1.1     kamil 
    308   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr2_yield, tc)
    309   1.1     kamil {
    310   1.1     kamil 	dbreg_preserve(0, dbreg_preserve_mode_yield);
    311   1.1     kamil }
    312   1.1     kamil 
    313   1.1     kamil 
    314   1.1     kamil ATF_TC(dbregs_preserve_dr3_yield);
    315   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr3_yield, tc)
    316   1.1     kamil {
    317   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    318   1.1     kamil 	    "Verify that setting DR3 is preserved across ptrace(2) calls with "
    319   1.1     kamil 	    "scheduler yield");
    320   1.1     kamil }
    321   1.1     kamil 
    322   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr3_yield, tc)
    323   1.1     kamil {
    324   1.1     kamil 	dbreg_preserve(3, dbreg_preserve_mode_yield);
    325   1.1     kamil }
    326   1.1     kamil 
    327   1.1     kamil ATF_TC(dbregs_preserve_dr0_continued);
    328   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr0_continued, tc)
    329   1.1     kamil {
    330   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    331   1.1     kamil 	    "Verify that setting DR0 is preserved across ptrace(2) calls and "
    332   1.1     kamil 	    "with continued child");
    333   1.1     kamil }
    334   1.1     kamil 
    335   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr0_continued, tc)
    336   1.1     kamil {
    337   1.1     kamil 	dbreg_preserve(0, dbreg_preserve_mode_continued);
    338   1.1     kamil }
    339   1.1     kamil 
    340   1.1     kamil ATF_TC(dbregs_preserve_dr1_continued);
    341   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr1_continued, tc)
    342   1.1     kamil {
    343   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    344   1.1     kamil 	    "Verify that setting DR1 is preserved across ptrace(2) calls and "
    345   1.1     kamil 	    "with continued child");
    346   1.1     kamil }
    347   1.1     kamil 
    348   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr1_continued, tc)
    349   1.1     kamil {
    350   1.1     kamil 	dbreg_preserve(1, dbreg_preserve_mode_continued);
    351   1.1     kamil }
    352   1.1     kamil 
    353   1.1     kamil ATF_TC(dbregs_preserve_dr2_continued);
    354   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr2_continued, tc)
    355   1.1     kamil {
    356   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    357   1.1     kamil 	    "Verify that setting DR2 is preserved across ptrace(2) calls and "
    358   1.1     kamil 	    "with continued child");
    359   1.1     kamil }
    360   1.1     kamil 
    361   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr2_continued, tc)
    362   1.1     kamil {
    363   1.1     kamil 	dbreg_preserve(2, dbreg_preserve_mode_continued);
    364   1.1     kamil }
    365   1.1     kamil 
    366   1.1     kamil ATF_TC(dbregs_preserve_dr3_continued);
    367   1.1     kamil ATF_TC_HEAD(dbregs_preserve_dr3_continued, tc)
    368   1.1     kamil {
    369   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    370   1.1     kamil 	    "Verify that setting DR3 is preserved across ptrace(2) calls and "
    371   1.1     kamil 	    "with continued child");
    372   1.1     kamil }
    373   1.1     kamil 
    374   1.1     kamil ATF_TC_BODY(dbregs_preserve_dr3_continued, tc)
    375   1.1     kamil {
    376   1.1     kamil 	dbreg_preserve(3, dbreg_preserve_mode_continued);
    377   1.1     kamil }
    378   1.1     kamil 
    379   1.1     kamil 
    380   1.1     kamil static void
    381   1.1     kamil dbregs_trap_variable(int reg, int cond, int len, bool write)
    382   1.1     kamil {
    383   1.1     kamil 	const int exitval = 5;
    384   1.1     kamil 	const int sigval = SIGSTOP;
    385   1.1     kamil 	pid_t child, wpid;
    386   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
    387   1.1     kamil 	int status;
    388   1.1     kamil #endif
    389   1.1     kamil 	struct dbreg r1;
    390   1.1     kamil 	size_t i;
    391   1.1     kamil 	volatile int watchme = 0;
    392   1.1     kamil 	union u dr7;
    393   1.1     kamil 
    394   1.1     kamil 	struct ptrace_siginfo info;
    395   1.1     kamil 	memset(&info, 0, sizeof(info));
    396   1.1     kamil 
    397   1.5     kamil 	if (!can_we_set_dbregs()) {
    398   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
    399   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
    400   1.5     kamil 	}
    401   1.5     kamil 
    402   1.1     kamil 	dr7.raw = 0;
    403   1.1     kamil 	switch (reg) {
    404   1.1     kamil 	case 0:
    405   1.1     kamil 		dr7.bits.global_dr0_breakpoint = 1;
    406   1.1     kamil 		dr7.bits.condition_dr0 = cond;
    407   1.1     kamil 		dr7.bits.len_dr0 = len;
    408   1.8     kamil 		break;
    409   1.1     kamil 	case 1:
    410   1.1     kamil 		dr7.bits.global_dr1_breakpoint = 1;
    411   1.1     kamil 		dr7.bits.condition_dr1 = cond;
    412   1.1     kamil 		dr7.bits.len_dr1 = len;
    413   1.8     kamil 		break;
    414   1.1     kamil 	case 2:
    415   1.1     kamil 		dr7.bits.global_dr2_breakpoint = 1;
    416   1.1     kamil 		dr7.bits.condition_dr2 = cond;
    417   1.1     kamil 		dr7.bits.len_dr2 = len;
    418   1.8     kamil 		break;
    419   1.1     kamil 	case 3:
    420   1.1     kamil 		dr7.bits.global_dr3_breakpoint = 1;
    421   1.1     kamil 		dr7.bits.condition_dr3 = cond;
    422   1.1     kamil 		dr7.bits.len_dr3 = len;
    423   1.1     kamil 		break;
    424   1.1     kamil 	}
    425   1.1     kamil 
    426   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
    427   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
    428   1.1     kamil 	if (child == 0) {
    429   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
    430   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
    431   1.1     kamil 
    432   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
    433   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
    434   1.1     kamil 
    435   1.1     kamil 		if (write)
    436   1.1     kamil 			watchme = 1;
    437   1.1     kamil 		else
    438   1.4     kamil 			printf("watchme=%d\n", watchme);
    439   1.1     kamil 
    440   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
    441   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
    442   1.1     kamil 
    443   1.2  christos 		DPRINTF("Before exiting of the child process\n");
    444   1.1     kamil 		_exit(exitval);
    445   1.1     kamil 	}
    446   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
    447   1.1     kamil 
    448   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    449   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    450   1.1     kamil 
    451   1.1     kamil 	validate_status_stopped(status, sigval);
    452   1.1     kamil 
    453   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
    454   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
    455   1.1     kamil 
    456   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
    457   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
    458   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
    459   1.1     kamil 
    460   1.1     kamil 	r1.dr[reg] = (long)(intptr_t)&watchme;
    461   1.2  christos 	DPRINTF("Set DR%d (r1.dr[%d]) to new value %" PRIxREGISTER "\n",
    462   1.1     kamil 	    reg, reg, r1.dr[reg]);
    463   1.1     kamil 
    464   1.1     kamil 	r1.dr[7] = dr7.raw;
    465   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
    466   1.1     kamil 	    r1.dr[7]);
    467   1.1     kamil 
    468   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
    469   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
    470   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
    471   1.1     kamil 
    472   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
    473   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
    474   1.1     kamil 
    475   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
    476   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    477   1.1     kamil 
    478   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    479   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    480   1.1     kamil 
    481   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
    482   1.1     kamil 
    483   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
    484   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
    485   1.1     kamil 
    486   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
    487   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
    488   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
    489   1.1     kamil 	    info.psi_siginfo.si_errno);
    490   1.1     kamil 
    491   1.2  christos 	DPRINTF("Before checking siginfo_t\n");
    492   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
    493   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_DBREG);
    494   1.1     kamil 
    495   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
    496   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    497   1.1     kamil 
    498   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    499   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    500   1.1     kamil 
    501   1.1     kamil 	validate_status_stopped(status, sigval);
    502   1.1     kamil 
    503   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
    504   1.1     kamil 	    "without signal to be sent\n");
    505   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    506   1.1     kamil 
    507   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    508   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    509   1.1     kamil 
    510   1.1     kamil 	validate_status_exited(status, exitval);
    511   1.1     kamil 
    512   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    513   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    514   1.1     kamil }
    515   1.1     kamil 
    516   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_writeonly_byte);
    517   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_writeonly_byte, tc)
    518   1.1     kamil {
    519   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    520   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    521   1.1     kamil 	    "(break on data writes only and 1 byte mode)");
    522   1.1     kamil }
    523   1.1     kamil 
    524   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_writeonly_byte, tc)
    525   1.1     kamil {
    526   1.1     kamil 	/* 0b01 -- break on data write only */
    527   1.1     kamil 	/* 0b00 -- 1 byte */
    528   1.1     kamil 
    529   1.1     kamil 	dbregs_trap_variable(0, 1, 0, true);
    530   1.1     kamil }
    531   1.1     kamil 
    532   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_writeonly_byte);
    533   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_writeonly_byte, tc)
    534   1.1     kamil {
    535   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    536   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    537   1.1     kamil 	    "(break on data writes only and 1 byte mode)");
    538   1.1     kamil }
    539   1.1     kamil 
    540   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_writeonly_byte, tc)
    541   1.1     kamil {
    542   1.1     kamil 	/* 0b01 -- break on data write only */
    543   1.1     kamil 	/* 0b00 -- 1 byte */
    544   1.1     kamil 
    545   1.1     kamil 	dbregs_trap_variable(1, 1, 0, true);
    546   1.1     kamil }
    547   1.1     kamil 
    548   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_writeonly_byte);
    549   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_writeonly_byte, tc)
    550   1.1     kamil {
    551   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    552   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    553   1.1     kamil 	    "(break on data writes only and 1 byte mode)");
    554   1.1     kamil }
    555   1.1     kamil 
    556   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_writeonly_byte, tc)
    557   1.1     kamil {
    558   1.1     kamil 	/* 0b01 -- break on data write only */
    559   1.1     kamil 	/* 0b00 -- 1 byte */
    560   1.1     kamil 
    561   1.1     kamil 	dbregs_trap_variable(2, 1, 0, true);
    562   1.1     kamil }
    563   1.1     kamil 
    564   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_writeonly_byte);
    565   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_writeonly_byte, tc)
    566   1.1     kamil {
    567   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    568   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    569   1.1     kamil 	    "(break on data writes only and 1 byte mode)");
    570   1.1     kamil }
    571   1.1     kamil 
    572   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_writeonly_byte, tc)
    573   1.1     kamil {
    574   1.1     kamil 	/* 0b01 -- break on data write only */
    575   1.1     kamil 	/* 0b00 -- 1 byte */
    576   1.1     kamil 
    577   1.1     kamil 	dbregs_trap_variable(3, 1, 0, true);
    578   1.1     kamil }
    579   1.1     kamil 
    580   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_writeonly_2bytes);
    581   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_writeonly_2bytes, tc)
    582   1.1     kamil {
    583   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    584   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    585   1.1     kamil 	    "(break on data writes only and 2 bytes mode)");
    586   1.1     kamil }
    587   1.1     kamil 
    588   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_writeonly_2bytes, tc)
    589   1.1     kamil {
    590   1.1     kamil 	/* 0b01 -- break on data write only */
    591   1.1     kamil 	/* 0b01 -- 2 bytes */
    592   1.1     kamil 
    593   1.1     kamil 	dbregs_trap_variable(0, 1, 1, true);
    594   1.1     kamil }
    595   1.1     kamil 
    596   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_writeonly_2bytes);
    597   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_writeonly_2bytes, tc)
    598   1.1     kamil {
    599   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    600   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    601   1.1     kamil 	    "(break on data writes only and 2 bytes mode)");
    602   1.1     kamil }
    603   1.1     kamil 
    604   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_writeonly_2bytes, tc)
    605   1.1     kamil {
    606   1.1     kamil 	/* 0b01 -- break on data write only */
    607   1.1     kamil 	/* 0b01 -- 2 bytes */
    608   1.1     kamil 
    609   1.1     kamil 	dbregs_trap_variable(1, 1, 1, true);
    610   1.1     kamil }
    611   1.1     kamil 
    612   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_writeonly_2bytes);
    613   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_writeonly_2bytes, tc)
    614   1.1     kamil {
    615   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    616   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    617   1.1     kamil 	    "(break on data writes only and 2 bytes mode)");
    618   1.1     kamil }
    619   1.1     kamil 
    620   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_writeonly_2bytes, tc)
    621   1.1     kamil {
    622   1.1     kamil 	/* 0b01 -- break on data write only */
    623   1.1     kamil 	/* 0b01 -- 2 bytes */
    624   1.1     kamil 
    625   1.1     kamil 	dbregs_trap_variable(2, 1, 1, true);
    626   1.1     kamil }
    627   1.1     kamil 
    628   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_writeonly_2bytes);
    629   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_writeonly_2bytes, tc)
    630   1.1     kamil {
    631   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    632   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    633   1.1     kamil 	    "(break on data writes only and 2 bytes mode)");
    634   1.1     kamil }
    635   1.1     kamil 
    636   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_writeonly_2bytes, tc)
    637   1.1     kamil {
    638   1.1     kamil 	/* 0b01 -- break on data write only */
    639   1.1     kamil 	/* 0b01 -- 2 bytes */
    640   1.1     kamil 
    641   1.1     kamil 	dbregs_trap_variable(3, 1, 1, true);
    642   1.1     kamil }
    643   1.1     kamil 
    644   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_writeonly_4bytes);
    645   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_writeonly_4bytes, tc)
    646   1.1     kamil {
    647   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    648   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    649   1.1     kamil 	    "(break on data writes only and 4 bytes mode)");
    650   1.1     kamil }
    651   1.1     kamil 
    652   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_writeonly_4bytes, tc)
    653   1.1     kamil {
    654   1.1     kamil 	/* 0b01 -- break on data write only */
    655   1.1     kamil 	/* 0b11 -- 4 bytes */
    656   1.1     kamil 
    657   1.1     kamil 	dbregs_trap_variable(0, 1, 3, true);
    658   1.1     kamil }
    659   1.1     kamil 
    660   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_writeonly_4bytes);
    661   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_writeonly_4bytes, tc)
    662   1.1     kamil {
    663   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    664   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    665   1.1     kamil 	    "(break on data writes only and 4 bytes mode)");
    666   1.1     kamil }
    667   1.1     kamil 
    668   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_writeonly_4bytes, tc)
    669   1.1     kamil {
    670   1.1     kamil 	/* 0b01 -- break on data write only */
    671   1.1     kamil 	/* 0b11 -- 4 bytes */
    672   1.1     kamil 
    673   1.1     kamil 	dbregs_trap_variable(1, 1, 3, true);
    674   1.1     kamil }
    675   1.1     kamil 
    676   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_writeonly_4bytes);
    677   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_writeonly_4bytes, tc)
    678   1.1     kamil {
    679   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    680   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    681   1.1     kamil 	    "(break on data writes only and 4 bytes mode)");
    682   1.1     kamil }
    683   1.1     kamil 
    684   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_writeonly_4bytes, tc)
    685   1.1     kamil {
    686   1.1     kamil 	/* 0b01 -- break on data write only */
    687   1.1     kamil 	/* 0b11 -- 4 bytes */
    688   1.1     kamil 
    689   1.1     kamil 	dbregs_trap_variable(2, 1, 3, true);
    690   1.1     kamil }
    691   1.1     kamil 
    692   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_writeonly_4bytes);
    693   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_writeonly_4bytes, tc)
    694   1.1     kamil {
    695   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    696   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    697   1.1     kamil 	    "(break on data writes only and 4 bytes mode)");
    698   1.1     kamil }
    699   1.1     kamil 
    700   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_writeonly_4bytes, tc)
    701   1.1     kamil {
    702   1.1     kamil 	/* 0b01 -- break on data write only */
    703   1.1     kamil 	/* 0b11 -- 4 bytes */
    704   1.1     kamil 
    705   1.1     kamil 	dbregs_trap_variable(3, 1, 3, true);
    706   1.1     kamil }
    707   1.1     kamil 
    708   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_write_byte);
    709   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_write_byte, tc)
    710   1.1     kamil {
    711   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    712   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    713   1.1     kamil 	    "(break on data read/write trap in read 1 byte mode)");
    714   1.1     kamil }
    715   1.1     kamil 
    716   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_write_byte, tc)
    717   1.1     kamil {
    718   1.1     kamil 	/* 0b11 -- break on data write&read */
    719   1.1     kamil 	/* 0b00 -- 1 byte */
    720   1.1     kamil 
    721   1.1     kamil 	dbregs_trap_variable(0, 3, 0, true);
    722   1.1     kamil }
    723   1.1     kamil 
    724   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_write_byte);
    725   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_write_byte, tc)
    726   1.1     kamil {
    727   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    728   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    729   1.1     kamil 	    "(break on data read/write trap in read 1 byte mode)");
    730   1.1     kamil }
    731   1.1     kamil 
    732   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_write_byte, tc)
    733   1.1     kamil {
    734   1.1     kamil 	/* 0b11 -- break on data write&read */
    735   1.1     kamil 	/* 0b00 -- 1 byte */
    736   1.1     kamil 
    737   1.1     kamil 	dbregs_trap_variable(1, 3, 0, true);
    738   1.1     kamil }
    739   1.1     kamil 
    740   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_write_byte);
    741   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_write_byte, tc)
    742   1.1     kamil {
    743   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    744   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    745   1.1     kamil 	    "(break on data read/write trap in read 1 byte mode)");
    746   1.1     kamil }
    747   1.1     kamil 
    748   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_write_byte, tc)
    749   1.1     kamil {
    750   1.1     kamil 	/* 0b11 -- break on data write&read */
    751   1.1     kamil 	/* 0b00 -- 1 byte */
    752   1.1     kamil 
    753   1.1     kamil 	dbregs_trap_variable(2, 3, 0, true);
    754   1.1     kamil }
    755   1.1     kamil 
    756   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_write_byte);
    757   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_write_byte, tc)
    758   1.1     kamil {
    759   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    760   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    761   1.1     kamil 	    "(break on data read/write trap in read 1 byte mode)");
    762   1.1     kamil }
    763   1.1     kamil 
    764   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_write_byte, tc)
    765   1.1     kamil {
    766   1.1     kamil 	/* 0b11 -- break on data write&read */
    767   1.1     kamil 	/* 0b00 -- 1 byte */
    768   1.1     kamil 
    769   1.1     kamil 	dbregs_trap_variable(3, 3, 0, true);
    770   1.1     kamil }
    771   1.1     kamil 
    772   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_write_2bytes);
    773   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_write_2bytes, tc)
    774   1.1     kamil {
    775   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    776   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    777   1.1     kamil 	    "(break on data read/write trap in read 2 bytes mode)");
    778   1.1     kamil }
    779   1.1     kamil 
    780   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_write_2bytes, tc)
    781   1.1     kamil {
    782   1.1     kamil 	/* 0b11 -- break on data write&read */
    783   1.1     kamil 	/* 0b01 -- 2 bytes */
    784   1.1     kamil 
    785   1.1     kamil 	dbregs_trap_variable(0, 3, 1, true);
    786   1.1     kamil }
    787   1.1     kamil 
    788   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_write_2bytes);
    789   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_write_2bytes, tc)
    790   1.1     kamil {
    791   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    792   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    793   1.1     kamil 	    "(break on data read/write trap in read 2 bytes mode)");
    794   1.1     kamil }
    795   1.1     kamil 
    796   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_write_2bytes, tc)
    797   1.1     kamil {
    798   1.1     kamil 	/* 0b11 -- break on data write&read */
    799   1.1     kamil 	/* 0b01 -- 2 bytes */
    800   1.1     kamil 
    801   1.1     kamil 	dbregs_trap_variable(1, 3, 1, true);
    802   1.1     kamil }
    803   1.1     kamil 
    804   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_write_2bytes);
    805   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_write_2bytes, tc)
    806   1.1     kamil {
    807   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    808   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    809   1.1     kamil 	    "(break on data read/write trap in read 2 bytes mode)");
    810   1.1     kamil }
    811   1.1     kamil 
    812   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_write_2bytes, tc)
    813   1.1     kamil {
    814   1.1     kamil 	/* 0b11 -- break on data write&read */
    815   1.1     kamil 	/* 0b01 -- 2 bytes */
    816   1.1     kamil 
    817   1.1     kamil 	dbregs_trap_variable(2, 3, 1, true);
    818   1.1     kamil }
    819   1.1     kamil 
    820   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_write_2bytes);
    821   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_write_2bytes, tc)
    822   1.1     kamil {
    823   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    824   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    825   1.1     kamil 	    "(break on data read/write trap in read 2 bytes mode)");
    826   1.1     kamil }
    827   1.1     kamil 
    828   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_write_2bytes, tc)
    829   1.1     kamil {
    830   1.1     kamil 	/* 0b11 -- break on data write&read */
    831   1.1     kamil 	/* 0b01 -- 2 bytes */
    832   1.1     kamil 
    833   1.1     kamil 	dbregs_trap_variable(3, 3, 1, true);
    834   1.1     kamil }
    835   1.1     kamil 
    836   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_write_4bytes);
    837   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_write_4bytes, tc)
    838   1.1     kamil {
    839   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    840   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    841   1.1     kamil 	    "(break on data read/write trap in read 4 bytes mode)");
    842   1.1     kamil }
    843   1.1     kamil 
    844   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_write_4bytes, tc)
    845   1.1     kamil {
    846   1.1     kamil 	/* 0b11 -- break on data write&read */
    847   1.1     kamil 	/* 0b11 -- 4 bytes */
    848   1.1     kamil 
    849   1.1     kamil 	dbregs_trap_variable(0, 3, 3, true);
    850   1.1     kamil }
    851   1.1     kamil 
    852   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_write_4bytes);
    853   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_write_4bytes, tc)
    854   1.1     kamil {
    855   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    856   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    857   1.1     kamil 	    "(break on data read/write trap in read 4 bytes mode)");
    858   1.1     kamil }
    859   1.1     kamil 
    860   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_write_4bytes, tc)
    861   1.1     kamil {
    862   1.1     kamil 	/* 0b11 -- break on data write&read */
    863   1.1     kamil 	/* 0b11 -- 4 bytes */
    864   1.1     kamil 
    865   1.1     kamil 	dbregs_trap_variable(1, 3, 3, true);
    866   1.1     kamil }
    867   1.1     kamil 
    868   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_write_4bytes);
    869   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_write_4bytes, tc)
    870   1.1     kamil {
    871   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    872   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    873   1.1     kamil 	    "(break on data read/write trap in read 4 bytes mode)");
    874   1.1     kamil }
    875   1.1     kamil 
    876   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_write_4bytes, tc)
    877   1.1     kamil {
    878   1.1     kamil 	/* 0b11 -- break on data write&read */
    879   1.1     kamil 	/* 0b11 -- 4 bytes */
    880   1.1     kamil 
    881   1.1     kamil 	dbregs_trap_variable(2, 3, 3, true);
    882   1.1     kamil }
    883   1.1     kamil 
    884   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_write_4bytes);
    885   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_write_4bytes, tc)
    886   1.1     kamil {
    887   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    888   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    889   1.1     kamil 	    "(break on data read/write trap in read 4 bytes mode)");
    890   1.1     kamil }
    891   1.1     kamil 
    892   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_write_4bytes, tc)
    893   1.1     kamil {
    894   1.1     kamil 	/* 0b11 -- break on data write&read */
    895   1.1     kamil 	/* 0b11 -- 4 bytes */
    896   1.1     kamil 
    897   1.1     kamil 	dbregs_trap_variable(3, 3, 3, true);
    898   1.1     kamil }
    899   1.1     kamil 
    900   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_read_byte);
    901   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_read_byte, tc)
    902   1.1     kamil {
    903   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    904   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    905   1.1     kamil 	    "(break on data read/write trap in write 1 byte mode)");
    906   1.1     kamil }
    907   1.1     kamil 
    908   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_read_byte, tc)
    909   1.1     kamil {
    910   1.1     kamil 	/* 0b11 -- break on data write&read */
    911   1.1     kamil 	/* 0b00 -- 1 byte */
    912   1.1     kamil 
    913   1.1     kamil 	dbregs_trap_variable(0, 3, 0, false);
    914   1.1     kamil }
    915   1.1     kamil 
    916   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_read_byte);
    917   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_read_byte, tc)
    918   1.1     kamil {
    919   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    920   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    921   1.1     kamil 	    "(break on data read/write trap in write 1 byte mode)");
    922   1.1     kamil }
    923   1.1     kamil 
    924   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_read_byte, tc)
    925   1.1     kamil {
    926   1.1     kamil 	/* 0b11 -- break on data write&read */
    927   1.1     kamil 	/* 0b00 -- 1 byte */
    928   1.1     kamil 
    929   1.1     kamil 	dbregs_trap_variable(1, 3, 0, false);
    930   1.1     kamil }
    931   1.1     kamil 
    932   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_read_byte);
    933   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_read_byte, tc)
    934   1.1     kamil {
    935   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    936   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
    937   1.1     kamil 	    "(break on data read/write trap in write 1 byte mode)");
    938   1.1     kamil }
    939   1.1     kamil 
    940   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_read_byte, tc)
    941   1.1     kamil {
    942   1.1     kamil 	/* 0b11 -- break on data write&read */
    943   1.1     kamil 	/* 0b00 -- 1 byte */
    944   1.1     kamil 
    945   1.1     kamil 	dbregs_trap_variable(2, 3, 0, false);
    946   1.1     kamil }
    947   1.1     kamil 
    948   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_read_byte);
    949   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_read_byte, tc)
    950   1.1     kamil {
    951   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    952   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
    953   1.1     kamil 	    "(break on data read/write trap in write 1 byte mode)");
    954   1.1     kamil }
    955   1.1     kamil 
    956   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_read_byte, tc)
    957   1.1     kamil {
    958   1.1     kamil 	/* 0b11 -- break on data write&read */
    959   1.1     kamil 	/* 0b00 -- 1 byte */
    960   1.1     kamil 
    961   1.1     kamil 	dbregs_trap_variable(3, 3, 0, false);
    962   1.1     kamil }
    963   1.1     kamil 
    964   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_read_2bytes);
    965   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_read_2bytes, tc)
    966   1.1     kamil {
    967   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    968   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
    969   1.1     kamil 	    "(break on data read/write trap in write 2 bytes mode)");
    970   1.1     kamil }
    971   1.1     kamil 
    972   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_read_2bytes, tc)
    973   1.1     kamil {
    974   1.1     kamil 	/* 0b11 -- break on data write&read */
    975   1.1     kamil 	/* 0b01 -- 2 bytes */
    976   1.1     kamil 
    977   1.1     kamil 	dbregs_trap_variable(0, 3, 1, false);
    978   1.1     kamil }
    979   1.1     kamil 
    980   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_read_2bytes);
    981   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_read_2bytes, tc)
    982   1.1     kamil {
    983   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
    984   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
    985   1.1     kamil 	    "(break on data read/write trap in write 2 bytes mode)");
    986   1.1     kamil }
    987   1.1     kamil 
    988   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_read_2bytes, tc)
    989   1.1     kamil {
    990   1.1     kamil 	/* 0b11 -- break on data write&read */
    991   1.1     kamil 	/* 0b01 -- 2 bytes */
    992   1.1     kamil 
    993   1.1     kamil 	dbregs_trap_variable(1, 3, 1, false);
    994   1.1     kamil }
    995   1.1     kamil 
    996   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_read_2bytes);
    997   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_read_2bytes, tc)
    998   1.1     kamil {
    999   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1000   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
   1001   1.1     kamil 	    "(break on data read/write trap in write 2 bytes mode)");
   1002   1.1     kamil }
   1003   1.1     kamil 
   1004   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_read_2bytes, tc)
   1005   1.1     kamil {
   1006   1.1     kamil 	/* 0b11 -- break on data write&read */
   1007   1.1     kamil 	/* 0b01 -- 2 bytes */
   1008   1.1     kamil 
   1009   1.1     kamil 	dbregs_trap_variable(2, 3, 1, false);
   1010   1.1     kamil }
   1011   1.1     kamil 
   1012   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_read_2bytes);
   1013   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_read_2bytes, tc)
   1014   1.1     kamil {
   1015   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1016   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
   1017   1.1     kamil 	    "(break on data read/write trap in write 2 bytes mode)");
   1018   1.1     kamil }
   1019   1.1     kamil 
   1020   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_read_2bytes, tc)
   1021   1.1     kamil {
   1022   1.1     kamil 	/* 0b11 -- break on data write&read */
   1023   1.1     kamil 	/* 0b01 -- 2 bytes */
   1024   1.1     kamil 
   1025   1.1     kamil 	dbregs_trap_variable(3, 3, 1, false);
   1026   1.1     kamil }
   1027   1.1     kamil 
   1028   1.1     kamil ATF_TC(dbregs_dr0_trap_variable_readwrite_read_4bytes);
   1029   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_variable_readwrite_read_4bytes, tc)
   1030   1.1     kamil {
   1031   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1032   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
   1033   1.1     kamil 	    "(break on data read/write trap in write 4 bytes mode)");
   1034   1.1     kamil }
   1035   1.1     kamil 
   1036   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_variable_readwrite_read_4bytes, tc)
   1037   1.1     kamil {
   1038   1.1     kamil 	/* 0b11 -- break on data write&read */
   1039   1.1     kamil 	/* 0b11 -- 4 bytes */
   1040   1.1     kamil 
   1041   1.1     kamil 	dbregs_trap_variable(0, 3, 3, false);
   1042   1.1     kamil }
   1043   1.1     kamil 
   1044   1.1     kamil ATF_TC(dbregs_dr1_trap_variable_readwrite_read_4bytes);
   1045   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_variable_readwrite_read_4bytes, tc)
   1046   1.1     kamil {
   1047   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1048   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
   1049   1.1     kamil 	    "(break on data read/write trap in write 4 bytes mode)");
   1050   1.1     kamil }
   1051   1.1     kamil 
   1052   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_variable_readwrite_read_4bytes, tc)
   1053   1.1     kamil {
   1054   1.1     kamil 	/* 0b11 -- break on data write&read */
   1055   1.1     kamil 	/* 0b11 -- 4 bytes */
   1056   1.1     kamil 
   1057   1.1     kamil 	dbregs_trap_variable(1, 3, 3, false);
   1058   1.1     kamil }
   1059   1.1     kamil 
   1060   1.1     kamil ATF_TC(dbregs_dr2_trap_variable_readwrite_read_4bytes);
   1061   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_variable_readwrite_read_4bytes, tc)
   1062   1.1     kamil {
   1063   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1064   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
   1065   1.1     kamil 	    "(break on data read/write trap in write 4 bytes mode)");
   1066   1.1     kamil }
   1067   1.1     kamil 
   1068   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_variable_readwrite_read_4bytes, tc)
   1069   1.1     kamil {
   1070   1.1     kamil 	/* 0b11 -- break on data write&read */
   1071   1.1     kamil 	/* 0b11 -- 4 bytes */
   1072   1.1     kamil 
   1073   1.1     kamil 	dbregs_trap_variable(2, 3, 3, false);
   1074   1.1     kamil }
   1075   1.1     kamil 
   1076   1.1     kamil ATF_TC(dbregs_dr3_trap_variable_readwrite_read_4bytes);
   1077   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_variable_readwrite_read_4bytes, tc)
   1078   1.1     kamil {
   1079   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1080   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
   1081   1.1     kamil 	    "(break on data read/write trap in write 4 bytes mode)");
   1082   1.1     kamil }
   1083   1.1     kamil 
   1084   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_variable_readwrite_read_4bytes, tc)
   1085   1.1     kamil {
   1086   1.1     kamil 	/* 0b11 -- break on data write&read */
   1087   1.1     kamil 	/* 0b11 -- 4 bytes */
   1088   1.1     kamil 
   1089   1.1     kamil 	dbregs_trap_variable(3, 3, 3, false);
   1090   1.1     kamil }
   1091   1.1     kamil 
   1092   1.1     kamil #if defined(HAVE_DBREGS)
   1093   1.1     kamil ATF_TC(dbregs_dr0_trap_code);
   1094   1.1     kamil ATF_TC_HEAD(dbregs_dr0_trap_code, tc)
   1095   1.1     kamil {
   1096   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1097   1.1     kamil 	    "Verify that setting trap with DR0 triggers SIGTRAP "
   1098   1.1     kamil 	    "(break on code execution trap)");
   1099   1.1     kamil }
   1100   1.1     kamil 
   1101   1.1     kamil ATF_TC_BODY(dbregs_dr0_trap_code, tc)
   1102   1.1     kamil {
   1103   1.1     kamil 	const int exitval = 5;
   1104   1.1     kamil 	const int sigval = SIGSTOP;
   1105   1.1     kamil 	pid_t child, wpid;
   1106   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1107   1.1     kamil 	int status;
   1108   1.1     kamil #endif
   1109   1.1     kamil 	struct dbreg r1;
   1110   1.1     kamil 	size_t i;
   1111   1.1     kamil 	volatile int watchme = 1;
   1112   1.1     kamil 	union u dr7;
   1113   1.1     kamil 
   1114   1.1     kamil 	struct ptrace_siginfo info;
   1115   1.1     kamil 	memset(&info, 0, sizeof(info));
   1116   1.1     kamil 
   1117   1.5     kamil 	if (!can_we_set_dbregs()) {
   1118   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1119   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1120   1.5     kamil 	}
   1121   1.5     kamil 
   1122   1.1     kamil 	dr7.raw = 0;
   1123   1.1     kamil 	dr7.bits.global_dr0_breakpoint = 1;
   1124   1.1     kamil 	dr7.bits.condition_dr0 = 0;	/* 0b00 -- break on code execution */
   1125   1.1     kamil 	dr7.bits.len_dr0 = 0;		/* 0b00 -- 1 byte */
   1126   1.1     kamil 
   1127   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1128   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1129   1.1     kamil 	if (child == 0) {
   1130   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1131   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1132   1.1     kamil 
   1133   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1134   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1135   1.1     kamil 
   1136   1.4     kamil 		printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
   1137   1.1     kamil 
   1138   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1139   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1140   1.1     kamil 
   1141   1.2  christos 		DPRINTF("Before exiting of the child process\n");
   1142   1.1     kamil 		_exit(exitval);
   1143   1.1     kamil 	}
   1144   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1145   1.1     kamil 
   1146   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1147   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1148   1.1     kamil 
   1149   1.1     kamil 	validate_status_stopped(status, sigval);
   1150   1.1     kamil 
   1151   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1152   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1153   1.1     kamil 
   1154   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1155   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1156   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1157   1.1     kamil 
   1158   1.1     kamil 	r1.dr[0] = (long)(intptr_t)check_happy;
   1159   1.2  christos 	DPRINTF("Set DR0 (r1.dr[0]) to new value %" PRIxREGISTER "\n",
   1160   1.1     kamil 	    r1.dr[0]);
   1161   1.1     kamil 
   1162   1.1     kamil 	r1.dr[7] = dr7.raw;
   1163   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1164   1.1     kamil 	    r1.dr[7]);
   1165   1.1     kamil 
   1166   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1167   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1168   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1169   1.1     kamil 
   1170   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1171   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1172   1.1     kamil 
   1173   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1174   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1175   1.1     kamil 
   1176   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1177   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1178   1.1     kamil 
   1179   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
   1180   1.1     kamil 
   1181   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
   1182   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
   1183   1.1     kamil 
   1184   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
   1185   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
   1186   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
   1187   1.1     kamil 	    info.psi_siginfo.si_errno);
   1188   1.1     kamil 
   1189   1.2  christos 	DPRINTF("Before checking siginfo_t\n");
   1190   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
   1191   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_DBREG);
   1192   1.1     kamil 
   1193   1.2  christos 	DPRINTF("Remove code trap from check_happy=%p\n", check_happy);
   1194   1.1     kamil 	dr7.bits.global_dr0_breakpoint = 0;
   1195   1.1     kamil 	r1.dr[7] = dr7.raw;
   1196   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1197   1.1     kamil 	    r1.dr[7]);
   1198   1.1     kamil 
   1199   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1200   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1201   1.1     kamil 
   1202   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1203   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1204   1.1     kamil 
   1205   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1206   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1207   1.1     kamil 
   1208   1.1     kamil 	validate_status_stopped(status, sigval);
   1209   1.1     kamil 
   1210   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1211   1.1     kamil 	    "without signal to be sent\n");
   1212   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1213   1.1     kamil 
   1214   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1215   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1216   1.1     kamil 
   1217   1.1     kamil 	validate_status_exited(status, exitval);
   1218   1.1     kamil 
   1219   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1220   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1221   1.1     kamil }
   1222   1.1     kamil #endif
   1223   1.1     kamil 
   1224   1.1     kamil #if defined(HAVE_DBREGS)
   1225   1.1     kamil ATF_TC(dbregs_dr1_trap_code);
   1226   1.1     kamil ATF_TC_HEAD(dbregs_dr1_trap_code, tc)
   1227   1.1     kamil {
   1228   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1229   1.1     kamil 	    "Verify that setting trap with DR1 triggers SIGTRAP "
   1230   1.1     kamil 	    "(break on code execution trap)");
   1231   1.1     kamil }
   1232   1.1     kamil 
   1233   1.1     kamil ATF_TC_BODY(dbregs_dr1_trap_code, tc)
   1234   1.1     kamil {
   1235   1.1     kamil 	const int exitval = 5;
   1236   1.1     kamil 	const int sigval = SIGSTOP;
   1237   1.1     kamil 	pid_t child, wpid;
   1238   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1239   1.1     kamil 	int status;
   1240   1.1     kamil #endif
   1241   1.1     kamil 	struct dbreg r1;
   1242   1.1     kamil 	size_t i;
   1243   1.1     kamil 	volatile int watchme = 1;
   1244   1.1     kamil 	union u dr7;
   1245   1.1     kamil 
   1246   1.1     kamil 	struct ptrace_siginfo info;
   1247   1.1     kamil 	memset(&info, 0, sizeof(info));
   1248   1.1     kamil 
   1249   1.5     kamil 	if (!can_we_set_dbregs()) {
   1250   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1251   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1252   1.5     kamil 	}
   1253   1.5     kamil 
   1254   1.1     kamil 	dr7.raw = 0;
   1255   1.1     kamil 	dr7.bits.global_dr1_breakpoint = 1;
   1256   1.1     kamil 	dr7.bits.condition_dr1 = 0;	/* 0b00 -- break on code execution */
   1257   1.1     kamil 	dr7.bits.len_dr1 = 0;		/* 0b00 -- 1 byte */
   1258   1.1     kamil 
   1259   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1260   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1261   1.1     kamil 	if (child == 0) {
   1262   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1263   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1264   1.1     kamil 
   1265   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1266   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1267   1.1     kamil 
   1268   1.4     kamil 		printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
   1269   1.1     kamil 
   1270   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1271   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1272   1.1     kamil 
   1273   1.2  christos 		DPRINTF("Before exiting of the child process\n");
   1274   1.1     kamil 		_exit(exitval);
   1275   1.1     kamil 	}
   1276   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1277   1.1     kamil 
   1278   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1279   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1280   1.1     kamil 
   1281   1.1     kamil 	validate_status_stopped(status, sigval);
   1282   1.1     kamil 
   1283   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1284   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1285   1.1     kamil 
   1286   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1287   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1288   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1289   1.1     kamil 
   1290   1.1     kamil 	r1.dr[1] = (long)(intptr_t)check_happy;
   1291   1.2  christos 	DPRINTF("Set DR1 (r1.dr[1]) to new value %" PRIxREGISTER "\n",
   1292   1.1     kamil 	    r1.dr[1]);
   1293   1.1     kamil 
   1294   1.1     kamil 	r1.dr[7] = dr7.raw;
   1295   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1296   1.1     kamil 	    r1.dr[7]);
   1297   1.1     kamil 
   1298   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1299   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1300   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1301   1.1     kamil 
   1302   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1303   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1304   1.1     kamil 
   1305   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1306   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1307   1.1     kamil 
   1308   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1309   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1310   1.1     kamil 
   1311   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
   1312   1.1     kamil 
   1313   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
   1314   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
   1315   1.1     kamil 
   1316   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
   1317   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
   1318   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
   1319   1.1     kamil 	    info.psi_siginfo.si_errno);
   1320   1.1     kamil 
   1321   1.2  christos 	DPRINTF("Before checking siginfo_t\n");
   1322   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
   1323   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_DBREG);
   1324   1.1     kamil 
   1325   1.2  christos 	DPRINTF("Remove code trap from check_happy=%p\n", check_happy);
   1326   1.1     kamil 	dr7.bits.global_dr1_breakpoint = 0;
   1327   1.1     kamil 	r1.dr[7] = dr7.raw;
   1328   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1329   1.1     kamil 	    r1.dr[7]);
   1330   1.1     kamil 
   1331   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1332   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1333   1.1     kamil 
   1334   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1335   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1336   1.1     kamil 
   1337   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1338   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1339   1.1     kamil 
   1340   1.1     kamil 	validate_status_stopped(status, sigval);
   1341   1.1     kamil 
   1342   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1343   1.1     kamil 	    "without signal to be sent\n");
   1344   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1345   1.1     kamil 
   1346   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1347   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1348   1.1     kamil 
   1349   1.1     kamil 	validate_status_exited(status, exitval);
   1350   1.1     kamil 
   1351   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1352   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1353   1.1     kamil }
   1354   1.1     kamil #endif
   1355   1.1     kamil 
   1356   1.1     kamil #if defined(HAVE_DBREGS)
   1357   1.1     kamil ATF_TC(dbregs_dr2_trap_code);
   1358   1.1     kamil ATF_TC_HEAD(dbregs_dr2_trap_code, tc)
   1359   1.1     kamil {
   1360   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1361   1.1     kamil 	    "Verify that setting trap with DR2 triggers SIGTRAP "
   1362   1.1     kamil 	    "(break on code execution trap)");
   1363   1.1     kamil }
   1364   1.1     kamil 
   1365   1.1     kamil ATF_TC_BODY(dbregs_dr2_trap_code, tc)
   1366   1.1     kamil {
   1367   1.1     kamil 	const int exitval = 5;
   1368   1.1     kamil 	const int sigval = SIGSTOP;
   1369   1.1     kamil 	pid_t child, wpid;
   1370   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1371   1.1     kamil 	int status;
   1372   1.1     kamil #endif
   1373   1.1     kamil 	struct dbreg r1;
   1374   1.1     kamil 	size_t i;
   1375   1.1     kamil 	volatile int watchme = 1;
   1376   1.1     kamil 	union u dr7;
   1377   1.1     kamil 
   1378   1.1     kamil 	struct ptrace_siginfo info;
   1379   1.1     kamil 	memset(&info, 0, sizeof(info));
   1380   1.1     kamil 
   1381   1.5     kamil 	if (!can_we_set_dbregs()) {
   1382   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1383   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1384   1.5     kamil 	}
   1385   1.5     kamil 
   1386   1.1     kamil 	dr7.raw = 0;
   1387   1.1     kamil 	dr7.bits.global_dr2_breakpoint = 1;
   1388   1.1     kamil 	dr7.bits.condition_dr2 = 0;	/* 0b00 -- break on code execution */
   1389   1.1     kamil 	dr7.bits.len_dr2 = 0;		/* 0b00 -- 1 byte */
   1390   1.1     kamil 
   1391   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1392   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1393   1.1     kamil 	if (child == 0) {
   1394   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1395   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1396   1.1     kamil 
   1397   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1398   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1399   1.1     kamil 
   1400   1.4     kamil 		printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
   1401   1.1     kamil 
   1402   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1403   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1404   1.1     kamil 
   1405   1.2  christos 		DPRINTF("Before exiting of the child process\n");
   1406   1.1     kamil 		_exit(exitval);
   1407   1.1     kamil 	}
   1408   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1409   1.1     kamil 
   1410   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1411   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1412   1.1     kamil 
   1413   1.1     kamil 	validate_status_stopped(status, sigval);
   1414   1.1     kamil 
   1415   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1416   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1417   1.1     kamil 
   1418   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1419   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1420   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1421   1.1     kamil 
   1422   1.1     kamil 	r1.dr[2] = (long)(intptr_t)check_happy;
   1423   1.2  christos 	DPRINTF("Set DR2 (r1.dr[2]) to new value %" PRIxREGISTER "\n",
   1424   1.1     kamil 	    r1.dr[2]);
   1425   1.1     kamil 
   1426   1.1     kamil 	r1.dr[7] = dr7.raw;
   1427   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1428   1.1     kamil 	    r1.dr[7]);
   1429   1.1     kamil 
   1430   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1431   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1432   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1433   1.1     kamil 
   1434   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1435   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1436   1.1     kamil 
   1437   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1438   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1439   1.1     kamil 
   1440   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1441   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1442   1.1     kamil 
   1443   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
   1444   1.1     kamil 
   1445   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
   1446   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
   1447   1.1     kamil 
   1448   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
   1449   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
   1450   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
   1451   1.1     kamil 	    info.psi_siginfo.si_errno);
   1452   1.1     kamil 
   1453   1.2  christos 	DPRINTF("Before checking siginfo_t\n");
   1454   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
   1455   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_DBREG);
   1456   1.1     kamil 
   1457   1.2  christos 	DPRINTF("Remove code trap from check_happy=%p\n", check_happy);
   1458   1.1     kamil 	dr7.bits.global_dr2_breakpoint = 0;
   1459   1.1     kamil 	r1.dr[7] = dr7.raw;
   1460   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1461   1.1     kamil 	    r1.dr[7]);
   1462   1.1     kamil 
   1463   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1464   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1465   1.1     kamil 
   1466   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1467   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1468   1.1     kamil 
   1469   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1470   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1471   1.1     kamil 
   1472   1.1     kamil 	validate_status_stopped(status, sigval);
   1473   1.1     kamil 
   1474   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1475   1.1     kamil 	    "without signal to be sent\n");
   1476   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1477   1.1     kamil 
   1478   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1479   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1480   1.1     kamil 
   1481   1.1     kamil 	validate_status_exited(status, exitval);
   1482   1.1     kamil 
   1483   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1484   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1485   1.1     kamil }
   1486   1.1     kamil #endif
   1487   1.1     kamil 
   1488   1.1     kamil #if defined(HAVE_DBREGS)
   1489   1.1     kamil ATF_TC(dbregs_dr3_trap_code);
   1490   1.1     kamil ATF_TC_HEAD(dbregs_dr3_trap_code, tc)
   1491   1.1     kamil {
   1492   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1493   1.1     kamil 	    "Verify that setting trap with DR3 triggers SIGTRAP "
   1494   1.1     kamil 	    "(break on code execution trap)");
   1495   1.1     kamil }
   1496   1.1     kamil 
   1497   1.1     kamil ATF_TC_BODY(dbregs_dr3_trap_code, tc)
   1498   1.1     kamil {
   1499   1.1     kamil 	const int exitval = 5;
   1500   1.1     kamil 	const int sigval = SIGSTOP;
   1501   1.1     kamil 	pid_t child, wpid;
   1502   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1503   1.1     kamil 	int status;
   1504   1.1     kamil #endif
   1505   1.1     kamil 	struct dbreg r1;
   1506   1.1     kamil 	size_t i;
   1507   1.1     kamil 	volatile int watchme = 1;
   1508   1.1     kamil 	union u dr7;
   1509   1.1     kamil 
   1510   1.1     kamil 	struct ptrace_siginfo info;
   1511   1.1     kamil 	memset(&info, 0, sizeof(info));
   1512   1.1     kamil 
   1513   1.5     kamil 	if (!can_we_set_dbregs()) {
   1514   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1515   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1516   1.5     kamil 	}
   1517   1.5     kamil 
   1518   1.1     kamil 	dr7.raw = 0;
   1519   1.1     kamil 	dr7.bits.global_dr3_breakpoint = 1;
   1520   1.1     kamil 	dr7.bits.condition_dr3 = 0;	/* 0b00 -- break on code execution */
   1521   1.1     kamil 	dr7.bits.len_dr3 = 0;		/* 0b00 -- 1 byte */
   1522   1.1     kamil 
   1523   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1524   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1525   1.1     kamil 	if (child == 0) {
   1526   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1527   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1528   1.1     kamil 
   1529   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1530   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1531   1.1     kamil 
   1532   1.4     kamil 		printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
   1533   1.1     kamil 
   1534   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1535   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1536   1.1     kamil 
   1537   1.2  christos 		DPRINTF("Before exiting of the child process\n");
   1538   1.1     kamil 		_exit(exitval);
   1539   1.1     kamil 	}
   1540   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1541   1.1     kamil 
   1542   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1543   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1544   1.1     kamil 
   1545   1.1     kamil 	validate_status_stopped(status, sigval);
   1546   1.1     kamil 
   1547   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1548   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1549   1.1     kamil 
   1550   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1551   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1552   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1553   1.1     kamil 
   1554   1.1     kamil 	r1.dr[3] = (long)(intptr_t)check_happy;
   1555   1.2  christos 	DPRINTF("Set DR3 (r1.dr[3]) to new value %" PRIxREGISTER "\n",
   1556   1.1     kamil 	    r1.dr[3]);
   1557   1.1     kamil 
   1558   1.1     kamil 	r1.dr[7] = dr7.raw;
   1559   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1560   1.1     kamil 	    r1.dr[7]);
   1561   1.1     kamil 
   1562   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1563   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1564   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1565   1.1     kamil 
   1566   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1567   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1568   1.1     kamil 
   1569   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1570   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1571   1.1     kamil 
   1572   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1573   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1574   1.1     kamil 
   1575   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
   1576   1.1     kamil 
   1577   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
   1578   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
   1579   1.1     kamil 
   1580   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
   1581   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
   1582   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
   1583   1.1     kamil 	    info.psi_siginfo.si_errno);
   1584   1.1     kamil 
   1585   1.2  christos 	DPRINTF("Before checking siginfo_t\n");
   1586   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
   1587   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_DBREG);
   1588   1.1     kamil 
   1589   1.2  christos 	DPRINTF("Remove code trap from check_happy=%p\n", check_happy);
   1590   1.1     kamil 	dr7.bits.global_dr3_breakpoint = 0;
   1591   1.1     kamil 	r1.dr[7] = dr7.raw;
   1592   1.2  christos 	DPRINTF("Set DR7 (r1.dr[7]) to new value %" PRIxREGISTER "\n",
   1593   1.1     kamil 	    r1.dr[7]);
   1594   1.1     kamil 
   1595   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1596   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1597   1.1     kamil 
   1598   1.2  christos 	DPRINTF("Call CONTINUE for the child process\n");
   1599   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1600   1.1     kamil 
   1601   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1602   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1603   1.1     kamil 
   1604   1.1     kamil 	validate_status_stopped(status, sigval);
   1605   1.1     kamil 
   1606   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1607   1.1     kamil 	    "without signal to be sent\n");
   1608   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1609   1.1     kamil 
   1610   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1611   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1612   1.1     kamil 
   1613   1.1     kamil 	validate_status_exited(status, exitval);
   1614   1.1     kamil 
   1615   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1616   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1617   1.1     kamil }
   1618   1.1     kamil #endif
   1619   1.1     kamil 
   1620  1.24     kamil static void * __used
   1621  1.24     kamil x86_main_func(void *arg)
   1622  1.24     kamil {
   1623   1.1     kamil 
   1624  1.24     kamil 	return arg;
   1625   1.1     kamil }
   1626   1.1     kamil 
   1627   1.1     kamil static void
   1628   1.1     kamil dbregs_dont_inherit_lwp(int reg)
   1629   1.1     kamil {
   1630   1.1     kamil 	const int exitval = 5;
   1631   1.1     kamil 	const int sigval = SIGSTOP;
   1632   1.1     kamil 	pid_t child, wpid;
   1633   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1634   1.1     kamil 	int status;
   1635   1.1     kamil #endif
   1636   1.1     kamil 	ptrace_state_t state;
   1637   1.1     kamil 	const int slen = sizeof(state);
   1638   1.1     kamil 	ptrace_event_t event;
   1639   1.1     kamil 	const int elen = sizeof(event);
   1640  1.24     kamil 	pthread_t t;
   1641   1.1     kamil 	lwpid_t lid;
   1642   1.1     kamil 	size_t i;
   1643   1.1     kamil 	struct dbreg r1;
   1644   1.1     kamil 	struct dbreg r2;
   1645   1.1     kamil 
   1646   1.5     kamil 	if (!can_we_set_dbregs()) {
   1647   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1648   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1649   1.5     kamil 	}
   1650   1.5     kamil 
   1651   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1652   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1653   1.1     kamil 	if (child == 0) {
   1654   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1655   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1656   1.1     kamil 
   1657   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1658   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1659   1.1     kamil 
   1660  1.24     kamil 		FORKEE_ASSERT(!pthread_create(&t, NULL, x86_main_func, NULL));
   1661   1.1     kamil 
   1662  1.24     kamil 		DPRINTF("Before waiting for thread to exit\n");
   1663  1.24     kamil 		FORKEE_ASSERT(!pthread_join(t, NULL));
   1664   1.1     kamil 
   1665   1.2  christos 		DPRINTF("Before exiting of the child process\n");
   1666   1.1     kamil 		_exit(exitval);
   1667   1.1     kamil 	}
   1668   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1669   1.1     kamil 
   1670   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1671   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1672   1.1     kamil 
   1673   1.1     kamil 	validate_status_stopped(status, sigval);
   1674   1.1     kamil 
   1675   1.2  christos 	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
   1676   1.1     kamil 	event.pe_set_event = PTRACE_LWP_CREATE;
   1677   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
   1678   1.1     kamil 
   1679   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1680   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1681   1.1     kamil 
   1682   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1683   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1684   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1685   1.1     kamil 
   1686   1.1     kamil 	r1.dr[reg] = (long)(intptr_t)check_happy;
   1687   1.2  christos 	DPRINTF("Set DR%d (r1.dr[%d]) to new value %" PRIxREGISTER "\n",
   1688   1.1     kamil 	    reg, reg, r1.dr[0]);
   1689   1.1     kamil 
   1690   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1691   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1692   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1693   1.1     kamil 
   1694   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1695   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1696   1.1     kamil 
   1697   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1698   1.1     kamil 	    "without signal to be sent\n");
   1699   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1700   1.1     kamil 
   1701   1.2  christos 	DPRINTF("Before calling %s() for the child - expected stopped "
   1702   1.1     kamil 	    "SIGTRAP\n", TWAIT_FNAME);
   1703   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1704   1.1     kamil 
   1705   1.1     kamil 	validate_status_stopped(status, SIGTRAP);
   1706   1.1     kamil 
   1707   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
   1708   1.1     kamil 
   1709   1.1     kamil 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
   1710   1.1     kamil 
   1711   1.1     kamil 	lid = state.pe_lwp;
   1712   1.2  christos 	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
   1713   1.1     kamil 
   1714   1.2  christos 	DPRINTF("Call GETDBREGS for the child process new lwp (r2)\n");
   1715   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r2, lid) != -1);
   1716   1.1     kamil 
   1717   1.2  christos 	DPRINTF("State of the debug registers (r2):\n");
   1718   1.1     kamil 	for (i = 0; i < __arraycount(r2.dr); i++)
   1719   1.2  christos 		DPRINTF("r2[%zu]=%" PRIxREGISTER "\n", i, r2.dr[i]);
   1720   1.1     kamil 
   1721   1.2  christos 	DPRINTF("Assert that (r1) and (r2) are not the same\n");
   1722   1.1     kamil 	ATF_REQUIRE(memcmp(&r1, &r2, sizeof(r1)) != 0);
   1723   1.1     kamil 
   1724   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1725   1.1     kamil 	    "without signal to be sent\n");
   1726   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1727   1.1     kamil 
   1728   1.2  christos 	DPRINTF("Before calling %s() for the child - expected exited\n",
   1729   1.1     kamil 	    TWAIT_FNAME);
   1730   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1731   1.1     kamil 
   1732   1.1     kamil 	validate_status_exited(status, exitval);
   1733   1.1     kamil 
   1734   1.2  christos 	DPRINTF("Before calling %s() for the child - expected no process\n",
   1735   1.1     kamil 	    TWAIT_FNAME);
   1736   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1737   1.1     kamil }
   1738   1.1     kamil 
   1739   1.1     kamil ATF_TC(dbregs_dr0_dont_inherit_lwp);
   1740   1.1     kamil ATF_TC_HEAD(dbregs_dr0_dont_inherit_lwp, tc)
   1741   1.1     kamil {
   1742   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1743   1.1     kamil 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
   1744   1.1     kamil 	    "EVENT_MASK set to PTRACE_LWP_CREATE and Debug Register 0 from "
   1745   1.1     kamil 	    "the forker thread is not inherited");
   1746   1.1     kamil }
   1747   1.1     kamil 
   1748   1.1     kamil ATF_TC_BODY(dbregs_dr0_dont_inherit_lwp, tc)
   1749   1.1     kamil {
   1750   1.1     kamil 	dbregs_dont_inherit_lwp(0);
   1751   1.1     kamil }
   1752   1.1     kamil 
   1753   1.1     kamil ATF_TC(dbregs_dr1_dont_inherit_lwp);
   1754   1.1     kamil ATF_TC_HEAD(dbregs_dr1_dont_inherit_lwp, tc)
   1755   1.1     kamil {
   1756   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1757   1.1     kamil 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
   1758   1.1     kamil 	    "EVENT_MASK set to PTRACE_LWP_CREATE and Debug Register 1 from "
   1759   1.1     kamil 	    "the forker thread is not inherited");
   1760   1.1     kamil }
   1761   1.1     kamil 
   1762   1.1     kamil ATF_TC_BODY(dbregs_dr1_dont_inherit_lwp, tc)
   1763   1.1     kamil {
   1764   1.1     kamil 	dbregs_dont_inherit_lwp(1);
   1765   1.1     kamil }
   1766   1.1     kamil 
   1767   1.1     kamil ATF_TC(dbregs_dr2_dont_inherit_lwp);
   1768   1.1     kamil ATF_TC_HEAD(dbregs_dr2_dont_inherit_lwp, tc)
   1769   1.1     kamil {
   1770   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1771   1.1     kamil 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
   1772   1.1     kamil 	    "EVENT_MASK set to PTRACE_LWP_CREATE and Debug Register 2 from "
   1773   1.1     kamil 	    "the forker thread is not inherited");
   1774   1.1     kamil }
   1775   1.1     kamil 
   1776   1.1     kamil ATF_TC_BODY(dbregs_dr2_dont_inherit_lwp, tc)
   1777   1.1     kamil {
   1778   1.1     kamil 	dbregs_dont_inherit_lwp(2);
   1779   1.1     kamil }
   1780   1.1     kamil 
   1781   1.1     kamil ATF_TC(dbregs_dr3_dont_inherit_lwp);
   1782   1.1     kamil ATF_TC_HEAD(dbregs_dr3_dont_inherit_lwp, tc)
   1783   1.1     kamil {
   1784   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1785   1.1     kamil 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
   1786   1.1     kamil 	    "EVENT_MASK set to PTRACE_LWP_CREATE and Debug Register 3 from "
   1787   1.1     kamil 	    "the forker thread is not inherited");
   1788   1.1     kamil }
   1789   1.1     kamil 
   1790   1.1     kamil ATF_TC_BODY(dbregs_dr3_dont_inherit_lwp, tc)
   1791   1.1     kamil {
   1792   1.1     kamil 	dbregs_dont_inherit_lwp(3);
   1793   1.1     kamil }
   1794   1.1     kamil 
   1795   1.1     kamil static void
   1796   1.1     kamil dbregs_dont_inherit_execve(int reg)
   1797   1.1     kamil {
   1798   1.1     kamil 	const int sigval = SIGTRAP;
   1799   1.1     kamil 	pid_t child, wpid;
   1800   1.1     kamil #if defined(TWAIT_HAVE_STATUS)
   1801   1.1     kamil 	int status;
   1802   1.1     kamil #endif
   1803   1.1     kamil 	size_t i;
   1804   1.1     kamil 	struct dbreg r1;
   1805   1.1     kamil 	struct dbreg r2;
   1806   1.1     kamil 
   1807   1.1     kamil 	struct ptrace_siginfo info;
   1808   1.1     kamil 	memset(&info, 0, sizeof(info));
   1809   1.1     kamil 
   1810   1.5     kamil 	if (!can_we_set_dbregs()) {
   1811   1.5     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   1812   1.5     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   1813   1.5     kamil 	}
   1814   1.5     kamil 
   1815   1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
   1816   1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
   1817   1.1     kamil 	if (child == 0) {
   1818   1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   1819   1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   1820   1.1     kamil 
   1821   1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   1822   1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   1823   1.1     kamil 
   1824   1.2  christos 		DPRINTF("Before calling execve(2) from child\n");
   1825   1.1     kamil 		execlp("/bin/echo", "/bin/echo", NULL);
   1826   1.1     kamil 
   1827   1.1     kamil 		FORKEE_ASSERT(0 && "Not reached");
   1828   1.1     kamil 	}
   1829   1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   1830   1.1     kamil 
   1831   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1832   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1833   1.1     kamil 
   1834   1.1     kamil 	validate_status_stopped(status, sigval);
   1835   1.1     kamil 
   1836   1.2  christos 	DPRINTF("Call GETDBREGS for the child process (r1)\n");
   1837   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r1, 0) != -1);
   1838   1.1     kamil 
   1839   1.2  christos 	DPRINTF("State of the debug registers (r1):\n");
   1840   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1841   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1842   1.1     kamil 
   1843   1.1     kamil 	r1.dr[reg] = (long)(intptr_t)check_happy;
   1844   1.2  christos 	DPRINTF("Set DR%d (r1.dr[%d]) to new value %" PRIxREGISTER "\n",
   1845   1.1     kamil 	    reg, reg, r1.dr[reg]);
   1846   1.1     kamil 
   1847   1.2  christos 	DPRINTF("New state of the debug registers (r1):\n");
   1848   1.1     kamil 	for (i = 0; i < __arraycount(r1.dr); i++)
   1849   1.2  christos 		DPRINTF("r1[%zu]=%" PRIxREGISTER "\n", i, r1.dr[i]);
   1850   1.1     kamil 
   1851   1.2  christos 	DPRINTF("Call SETDBREGS for the child process (r1)\n");
   1852   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r1, 0) != -1);
   1853   1.1     kamil 
   1854   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1855   1.1     kamil 	    "without signal to be sent\n");
   1856   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1857   1.1     kamil 
   1858   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1859   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1860   1.1     kamil 
   1861   1.1     kamil 	validate_status_stopped(status, sigval);
   1862   1.1     kamil 
   1863   1.2  christos 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
   1864   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
   1865   1.1     kamil 
   1866   1.2  christos 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
   1867   1.2  christos 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
   1868   1.1     kamil 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
   1869   1.1     kamil 	    info.psi_siginfo.si_errno);
   1870   1.1     kamil 
   1871   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
   1872   1.1     kamil 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
   1873   1.1     kamil 
   1874   1.2  christos 	DPRINTF("Call GETDBREGS for the child process after execve(2)\n");
   1875   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r2, 0) != -1);
   1876   1.1     kamil 
   1877   1.2  christos 	DPRINTF("State of the debug registers (r2):\n");
   1878   1.1     kamil 	for (i = 0; i < __arraycount(r2.dr); i++)
   1879   1.2  christos 		DPRINTF("r2[%zu]=%" PRIxREGISTER "\n", i, r2.dr[i]);
   1880   1.1     kamil 
   1881   1.2  christos 	DPRINTF("Assert that (r1) and (r2) are not the same\n");
   1882   1.1     kamil 	ATF_REQUIRE(memcmp(&r1, &r2, sizeof(r1)) != 0);
   1883   1.1     kamil 
   1884   1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
   1885   1.1     kamil 	    "without signal to be sent\n");
   1886   1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   1887   1.1     kamil 
   1888   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1889   1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   1890   1.1     kamil 
   1891   1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   1892   1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   1893   1.1     kamil }
   1894   1.1     kamil 
   1895   1.1     kamil ATF_TC(dbregs_dr0_dont_inherit_execve);
   1896   1.1     kamil ATF_TC_HEAD(dbregs_dr0_dont_inherit_execve, tc)
   1897   1.1     kamil {
   1898   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1899   1.1     kamil 	    "Verify that execve(2) is intercepted by tracer and Debug "
   1900   1.1     kamil 	    "Register 0 is reset");
   1901   1.1     kamil }
   1902   1.1     kamil 
   1903   1.1     kamil ATF_TC_BODY(dbregs_dr0_dont_inherit_execve, tc)
   1904   1.1     kamil {
   1905   1.1     kamil 	dbregs_dont_inherit_execve(0);
   1906   1.1     kamil }
   1907   1.1     kamil 
   1908   1.1     kamil ATF_TC(dbregs_dr1_dont_inherit_execve);
   1909   1.1     kamil ATF_TC_HEAD(dbregs_dr1_dont_inherit_execve, tc)
   1910   1.1     kamil {
   1911   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1912   1.1     kamil 	    "Verify that execve(2) is intercepted by tracer and Debug "
   1913   1.1     kamil 	    "Register 1 is reset");
   1914   1.1     kamil }
   1915   1.1     kamil 
   1916   1.1     kamil ATF_TC_BODY(dbregs_dr1_dont_inherit_execve, tc)
   1917   1.1     kamil {
   1918   1.1     kamil 	dbregs_dont_inherit_execve(1);
   1919   1.1     kamil }
   1920   1.1     kamil 
   1921   1.1     kamil ATF_TC(dbregs_dr2_dont_inherit_execve);
   1922   1.1     kamil ATF_TC_HEAD(dbregs_dr2_dont_inherit_execve, tc)
   1923   1.1     kamil {
   1924   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1925   1.1     kamil 	    "Verify that execve(2) is intercepted by tracer and Debug "
   1926   1.1     kamil 	    "Register 2 is reset");
   1927   1.1     kamil }
   1928   1.1     kamil 
   1929   1.1     kamil ATF_TC_BODY(dbregs_dr2_dont_inherit_execve, tc)
   1930   1.1     kamil {
   1931   1.1     kamil 	dbregs_dont_inherit_execve(2);
   1932   1.1     kamil }
   1933   1.1     kamil 
   1934   1.1     kamil ATF_TC(dbregs_dr3_dont_inherit_execve);
   1935   1.1     kamil ATF_TC_HEAD(dbregs_dr3_dont_inherit_execve, tc)
   1936   1.1     kamil {
   1937   1.1     kamil 	atf_tc_set_md_var(tc, "descr",
   1938   1.1     kamil 	    "Verify that execve(2) is intercepted by tracer and Debug "
   1939   1.1     kamil 	    "Register 3 is reset");
   1940   1.1     kamil }
   1941   1.1     kamil 
   1942   1.1     kamil ATF_TC_BODY(dbregs_dr3_dont_inherit_execve, tc)
   1943   1.1     kamil {
   1944   1.1     kamil 	dbregs_dont_inherit_execve(3);
   1945   1.1     kamil }
   1946   1.7     kamil 
   1947   1.7     kamil /// ----------------------------------------------------------------------------
   1948   1.7     kamil 
   1949   1.7     kamil ATF_TC(x86_cve_2018_8897);
   1950   1.7     kamil ATF_TC_HEAD(x86_cve_2018_8897, tc)
   1951   1.7     kamil {
   1952   1.7     kamil 	atf_tc_set_md_var(tc, "descr",
   1953   1.7     kamil 	    "Verify mitigation for CVE-2018-8897 (POP SS debug exception)");
   1954   1.7     kamil }
   1955   1.7     kamil 
   1956   1.7     kamil #define X86_CVE_2018_8897_PAGE 0x5000 /* page addressable by 32-bit registers */
   1957   1.7     kamil 
   1958  1.22       tnn static void
   1959   1.7     kamil x86_cve_2018_8897_trigger(void)
   1960   1.7     kamil {
   1961   1.7     kamil 	/*
   1962   1.7     kamil 	 * A function to trigger the POP SS (CVE-2018-8897) vulnerability
   1963   1.7     kamil 	 *
   1964   1.7     kamil 	 * ifdef __x86_64__
   1965   1.7     kamil 	 *
   1966   1.7     kamil 	 * We need to switch to 32-bit mode execution on 64-bit kernel.
   1967   1.7     kamil 	 * This is achieved with far jump instruction and GDT descriptor
   1968   1.7     kamil 	 * set to 32-bit CS selector. The 32-bit CS selector is kernel
   1969   1.7     kamil 	 * specific, in the NetBSD case registered as GUCODE32_SEL
   1970   1.7     kamil 	 * that is equal to (14 (decimal) << 3) with GDT and user
   1971   1.7     kamil 	 * privilege level (this makes it 0x73).
   1972   1.7     kamil 	 *
   1973   1.7     kamil 	 * In UNIX as(1) assembly x86_64 far jump is coded as ljmp.
   1974   1.7     kamil 	 * amd64 ljmp requires an indirect address with cs:RIP.
   1975   1.7     kamil 	 *
   1976   1.7     kamil 	 * When we are running in 32-bit mode, it's similar to the
   1977   1.7     kamil 	 * mode as if the binary had been launched in netbsd32.
   1978   1.7     kamil 	 *
   1979   1.7     kamil 	 * There are two versions of this exploit, one with RIP
   1980   1.7     kamil 	 * relative code and the other with static addresses.
   1981   1.7     kamil 	 * The first one is PIE code aware, the other no-PIE one.
   1982   1.7     kamil 	 *
   1983   1.7     kamil 	 *
   1984   1.7     kamil 	 * After switching to the 32-bit mode we can move on to the remaining
   1985   1.7     kamil 	 * part of the exploit.
   1986   1.7     kamil 	 *
   1987   1.7     kamil 	 * endif //  __x86_64__
   1988   1.7     kamil 	 *
   1989   1.7     kamil 	 * Set the stack pointer to the page we allocated earlier. Remember
   1990   1.7     kamil 	 * that we put an SS selector exactly at this address, so we can pop.
   1991   1.7     kamil 	 *
   1992   1.7     kamil 	 * movl    $0x5000,%esp
   1993   1.7     kamil 	 *
   1994   1.7     kamil 	 * Pop the SS selector off the stack. This reloads the SS selector,
   1995   1.7     kamil 	 * which is fine. Remember that we set DR0 at address 0x5000, which
   1996   1.7     kamil 	 * we are now reading. Therefore, on this instruction, the CPU will
   1997   1.7     kamil 	 * raise a #DB exception.
   1998   1.7     kamil 	 *
   1999   1.7     kamil 	 * But the "pop %ss" instruction is special: it blocks exceptions
   2000   1.7     kamil 	 * until the next instruction is executed. So the #DB that we just
   2001   1.7     kamil 	 * raised is actually blocked.
   2002   1.7     kamil 	 *
   2003   1.7     kamil 	 * pop %ss
   2004   1.7     kamil 	 *
   2005   1.7     kamil 	 * We are still here, and didn't receive the #DB. After we execute
   2006   1.7     kamil 	 * this instruction, the effect of "pop %ss" will disappear, and
   2007   1.7     kamil 	 * we will receive the #DB for real.
   2008   1.7     kamil 	 *
   2009   1.7     kamil 	 * int $4
   2010   1.7     kamil 	 *
   2011   1.7     kamil 	 * Here the bug happens. We executed "int $4", so we entered the
   2012   1.7     kamil 	 * kernel, with interrupts disabled. The #DB that was pending is
   2013   1.7     kamil 	 * received. But, it is received immediately in kernel mode, and is
   2014   1.7     kamil 	 * _NOT_ received when interrupts are enabled again.
   2015   1.7     kamil 	 *
   2016   1.7     kamil 	 * It means that, in the first instruction of the $4 handler, we
   2017   1.7     kamil 	 * think we are safe with interrupts disabled. But we aren't, and
   2018   1.7     kamil 	 * just got interrupted.
   2019   1.7     kamil 	 *
   2020   1.7     kamil 	 * The new interrupt handler doesn't handle this particular context:
   2021   1.7     kamil 	 * we are entered in kernel mode, the previous context was kernel
   2022   1.7     kamil 	 * mode too but it still had the user context loaded.
   2023   1.7     kamil 	 *
   2024   1.7     kamil 	 * We find ourselves not doing a 'swapgs'. At the end of the day, it
   2025   1.7     kamil 	 * means that we call trap() with a curcpu() that is fully
   2026   1.7     kamil 	 * controllable by userland. From then on, it is easy to escalate
   2027   1.7     kamil 	 * privileges.
   2028   1.7     kamil 	 *
   2029   1.7     kamil 	 * With SVS it also means we don't switch CR3, so this results in a
   2030   1.7     kamil 	 * triple fault, which this time cannot be turned to a privilege
   2031   1.7     kamil 	 * escalation.
   2032   1.7     kamil 	 */
   2033   1.7     kamil 
   2034   1.7     kamil #if __x86_64__
   2035   1.7     kamil #if __PIE__
   2036   1.7     kamil 	void *csRIP;
   2037   1.7     kamil 
   2038   1.7     kamil 	csRIP = malloc(sizeof(int) + sizeof(short));
   2039   1.7     kamil 	FORKEE_ASSERT(csRIP != NULL);
   2040   1.7     kamil 
   2041   1.7     kamil 	__asm__ __volatile__(
   2042   1.7     kamil 		"	leal 24(%%eip), %%eax\n\t"
   2043   1.7     kamil 		"	movq %0, %%rdx\n\t"
   2044   1.7     kamil 		"	movl %%eax, (%%rdx)\n\t"
   2045   1.7     kamil 		"	movw $0x73, 4(%%rdx)\n\t"
   2046   1.7     kamil 		"	movq %1, %%rax\n\t"
   2047   1.7     kamil 		"	ljmp *(%%rax)\n\t"
   2048   1.7     kamil 		"	.code32\n\t"
   2049   1.7     kamil 		"	movl $0x5000, %%esp\n\t"
   2050   1.7     kamil 		"	pop %%ss\n\t"
   2051   1.7     kamil 		"	int $4\n\t"
   2052   1.7     kamil 		"	.code64\n\t"
   2053   1.7     kamil 		: "=m"(csRIP)
   2054   1.7     kamil 		: "m"(csRIP)
   2055   1.7     kamil 		: "%rax", "%rdx", "%rsp"
   2056   1.7     kamil 		);
   2057   1.7     kamil #else /* !__PIE__ */
   2058   1.7     kamil 	__asm__ __volatile__(
   2059  1.23  christos 		"       movq $farjmp32%=, %%rax\n\t"
   2060   1.7     kamil 		"       ljmp *(%%rax)\n\t"
   2061  1.23  christos 		"farjmp32%=:\n\t"
   2062  1.23  christos 		"       .long trigger32%=\n\t"
   2063   1.7     kamil 		"       .word 0x73\n\t"
   2064   1.7     kamil 		"       .code32\n\t"
   2065  1.23  christos 		"trigger32%=:\n\t"
   2066   1.7     kamil 		"       movl $0x5000, %%esp\n\t"
   2067   1.7     kamil 		"       pop %%ss\n\t"
   2068   1.7     kamil 		"       int $4\n\t"
   2069   1.7     kamil 		"       .code64\n\t"
   2070   1.7     kamil 		:
   2071   1.7     kamil 		:
   2072   1.7     kamil 		: "%rax", "%rsp"
   2073   1.7     kamil 		);
   2074   1.7     kamil #endif
   2075   1.7     kamil #elif __i386__
   2076   1.7     kamil 	__asm__ __volatile__(
   2077   1.7     kamil 		"movl $0x5000, %%esp\n\t"
   2078   1.7     kamil 		"pop %%ss\n\t"
   2079   1.7     kamil 		"int $4\n\t"
   2080   1.7     kamil 		:
   2081   1.7     kamil 		:
   2082   1.7     kamil 		: "%esp"
   2083   1.7     kamil 		);
   2084   1.7     kamil #endif
   2085   1.7     kamil }
   2086   1.7     kamil 
   2087   1.7     kamil ATF_TC_BODY(x86_cve_2018_8897, tc)
   2088   1.7     kamil {
   2089   1.7     kamil 	const int sigval = SIGSTOP;
   2090   1.7     kamil 	pid_t child, wpid;
   2091   1.7     kamil #if defined(TWAIT_HAVE_STATUS)
   2092   1.7     kamil 	int status;
   2093   1.7     kamil #endif
   2094   1.7     kamil 	char *trap_page;
   2095   1.7     kamil 	struct dbreg db;
   2096   1.7     kamil 
   2097   1.7     kamil 
   2098   1.7     kamil 	if (!can_we_set_dbregs()) {
   2099   1.7     kamil 		atf_tc_skip("Either run this test as root or set sysctl(3) "
   2100   1.7     kamil 		            "security.models.extensions.user_set_dbregs to 1");
   2101   1.7     kamil 	}
   2102   1.7     kamil 
   2103   1.7     kamil 	DPRINTF("Before forking process PID=%d\n", getpid());
   2104   1.7     kamil 	SYSCALL_REQUIRE((child = fork()) != -1);
   2105   1.7     kamil 	if (child == 0) {
   2106   1.7     kamil 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   2107   1.7     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   2108   1.7     kamil 
   2109   1.7     kamil 		trap_page = mmap((void *)X86_CVE_2018_8897_PAGE,
   2110   1.7     kamil 		                 sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE,
   2111   1.7     kamil 		                 MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
   2112   1.7     kamil 
   2113   1.7     kamil 		/* trigger page fault */
   2114   1.7     kamil 		memset(trap_page, 0, sysconf(_SC_PAGESIZE));
   2115   1.7     kamil 
   2116   1.7     kamil 		// kernel GDT
   2117   1.7     kamil #if __x86_64__
   2118   1.7     kamil 		/* SS selector (descriptor 9 (0x4f >> 3)) */
   2119   1.7     kamil 		*trap_page = 0x4f;
   2120   1.7     kamil #elif __i386__
   2121   1.7     kamil 		/* SS selector (descriptor 4 (0x23 >> 3)) */
   2122   1.7     kamil 		*trap_page = 0x23;
   2123   1.7     kamil #endif
   2124   1.7     kamil 
   2125   1.7     kamil 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
   2126   1.7     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
   2127   1.7     kamil 
   2128   1.7     kamil 		x86_cve_2018_8897_trigger();
   2129   1.7     kamil 
   2130   1.7     kamil 		/* NOTREACHED */
   2131   1.7     kamil 		FORKEE_ASSERTX(0 && "This shall not be reached");
   2132   1.7     kamil 	}
   2133   1.7     kamil 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   2134   1.7     kamil 
   2135   1.7     kamil 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   2136   1.7     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   2137   1.7     kamil 
   2138   1.7     kamil 	validate_status_stopped(status, sigval);
   2139   1.7     kamil 
   2140   1.7     kamil 	DPRINTF("Call GETDBREGS for the child process\n");
   2141   1.7     kamil 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &db, 0) != -1);
   2142   1.7     kamil 
   2143   1.7     kamil 	/*
   2144   1.7     kamil 	 * Set up the dbregs. We put the 0x5000 address in DR0.
   2145   1.7     kamil 	 * It means that, the first time we touch this, the CPU will trigger a
   2146   1.7     kamil 	 * #DB exception.
   2147   1.7     kamil 	 */
   2148   1.7     kamil 	db.dr[0] = X86_CVE_2018_8897_PAGE;
   2149   1.7     kamil 	db.dr[7] = 0x30003;
   2150   1.7     kamil 
   2151   1.7     kamil 	DPRINTF("Call SETDBREGS for the child process\n");
   2152   1.7     kamil 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &db, 0) != -1);
   2153   1.7     kamil 
   2154   1.7     kamil 	DPRINTF("Before resuming the child process where it left off and "
   2155   1.7     kamil 	    "without signal to be sent\n");
   2156   1.7     kamil 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   2157   1.7     kamil 
   2158   1.7     kamil 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   2159   1.7     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   2160   1.7     kamil 
   2161   1.7     kamil 	// In this test we receive SIGFPE, is this appropriate?
   2162   1.7     kamil //	validate_status_stopped(status, SIGFPE);
   2163   1.7     kamil 
   2164   1.7     kamil 	DPRINTF("Kill the child process\n");
   2165   1.7     kamil 	SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
   2166   1.7     kamil 
   2167   1.7     kamil 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   2168   1.7     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   2169   1.7     kamil 
   2170   1.7     kamil 	validate_status_signaled(status, SIGKILL, 0);
   2171   1.7     kamil 
   2172   1.7     kamil 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   2173   1.7     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   2174   1.7     kamil }
   2175   1.7     kamil 
   2176  1.17    mgorny /// ----------------------------------------------------------------------------
   2177  1.17    mgorny 
   2178  1.17    mgorny union x86_test_register {
   2179  1.17    mgorny 	struct {
   2180  1.17    mgorny 		uint64_t a, b, c, d;
   2181  1.17    mgorny 	} ymm;
   2182  1.17    mgorny 	struct {
   2183  1.17    mgorny 		uint64_t a, b;
   2184  1.17    mgorny 	} xmm;
   2185  1.17    mgorny 	uint64_t u64;
   2186  1.17    mgorny 	uint32_t u32;
   2187  1.17    mgorny };
   2188  1.17    mgorny 
   2189  1.27    mgorny struct x86_test_fpu_registers {
   2190  1.27    mgorny 	struct {
   2191  1.27    mgorny 		uint64_t mantissa;
   2192  1.27    mgorny 		uint16_t sign_exp;
   2193  1.27    mgorny 	} __aligned(16) st[8];
   2194  1.27    mgorny 
   2195  1.27    mgorny 	uint16_t cw;
   2196  1.27    mgorny 	uint16_t sw;
   2197  1.27    mgorny 	uint16_t tw;
   2198  1.27    mgorny 	uint8_t tw_abridged;
   2199  1.27    mgorny 	uint16_t opcode;
   2200  1.27    mgorny 	union fp_addr ip;
   2201  1.27    mgorny 	union fp_addr dp;
   2202  1.27    mgorny };
   2203  1.27    mgorny 
   2204  1.17    mgorny enum x86_test_regset {
   2205  1.17    mgorny 	TEST_GPREGS,
   2206  1.17    mgorny 	TEST_FPREGS,
   2207  1.17    mgorny 	TEST_XMMREGS,
   2208  1.17    mgorny 	TEST_XSTATE
   2209  1.17    mgorny };
   2210  1.17    mgorny 
   2211  1.17    mgorny /* Please keep them grouped by acceptable x86_test_regset. */
   2212  1.17    mgorny enum x86_test_registers {
   2213  1.17    mgorny 	/* TEST_GPREGS */
   2214  1.17    mgorny 	GPREGS_32,
   2215  1.17    mgorny 	GPREGS_32_EBP_ESP,
   2216  1.17    mgorny 	GPREGS_64,
   2217  1.17    mgorny 	GPREGS_64_R8,
   2218  1.17    mgorny 	/* TEST_FPREGS/TEST_XMMREGS */
   2219  1.27    mgorny 	FPREGS_FPU,
   2220  1.17    mgorny 	FPREGS_MM,
   2221  1.17    mgorny 	FPREGS_XMM,
   2222  1.17    mgorny 	/* TEST_XSTATE */
   2223  1.17    mgorny 	FPREGS_YMM
   2224  1.17    mgorny };
   2225  1.17    mgorny 
   2226  1.17    mgorny enum x86_test_regmode {
   2227  1.17    mgorny 	TEST_GETREGS,
   2228  1.18    mgorny 	TEST_SETREGS,
   2229  1.18    mgorny 	TEST_COREDUMP
   2230  1.17    mgorny };
   2231  1.17    mgorny 
   2232  1.17    mgorny static __inline void get_gp32_regs(union x86_test_register out[])
   2233  1.10    mgorny {
   2234  1.17    mgorny #if defined(__i386__)
   2235  1.17    mgorny 	const uint32_t fill = 0x0F0F0F0F;
   2236  1.17    mgorny 
   2237  1.17    mgorny 	__asm__ __volatile__(
   2238  1.17    mgorny 		/* fill registers with clobber pattern */
   2239  1.17    mgorny 		"movl    %6, %%eax\n\t"
   2240  1.17    mgorny 		"movl    %6, %%ebx\n\t"
   2241  1.17    mgorny 		"movl    %6, %%ecx\n\t"
   2242  1.17    mgorny 		"movl    %6, %%edx\n\t"
   2243  1.17    mgorny 		"movl    %6, %%esi\n\t"
   2244  1.17    mgorny 		"movl    %6, %%edi\n\t"
   2245  1.17    mgorny 		"\n\t"
   2246  1.17    mgorny 		"int3\n\t"
   2247  1.17    mgorny 		: "=a"(out[0].u32), "=b"(out[1].u32), "=c"(out[2].u32),
   2248  1.17    mgorny 		  "=d"(out[3].u32), "=S"(out[4].u32), "=D"(out[5].u32)
   2249  1.17    mgorny 		: "g"(fill)
   2250  1.17    mgorny 	);
   2251  1.17    mgorny #else
   2252  1.17    mgorny 	__unreachable();
   2253  1.17    mgorny #endif
   2254  1.10    mgorny }
   2255  1.10    mgorny 
   2256  1.17    mgorny static __inline void set_gp32_regs(const union x86_test_register data[])
   2257  1.10    mgorny {
   2258  1.17    mgorny #if defined(__i386__)
   2259  1.10    mgorny 	__asm__ __volatile__(
   2260  1.10    mgorny 		"int3\n\t"
   2261  1.10    mgorny 		:
   2262  1.17    mgorny 		: "a"(data[0].u32), "b"(data[1].u32), "c"(data[2].u32),
   2263  1.17    mgorny 		  "d"(data[3].u32), "S"(data[4].u32), "D"(data[5].u32)
   2264  1.17    mgorny 		:
   2265  1.10    mgorny 	);
   2266  1.10    mgorny #else
   2267  1.17    mgorny 	__unreachable();
   2268  1.10    mgorny #endif
   2269  1.10    mgorny }
   2270  1.10    mgorny 
   2271  1.17    mgorny static __inline void get_gp32_ebp_esp_regs(union x86_test_register out[])
   2272  1.13    mgorny {
   2273  1.17    mgorny #if defined(__i386__)
   2274  1.17    mgorny 	const uint32_t fill = 0x0F0F0F0F;
   2275  1.13    mgorny 
   2276  1.13    mgorny 	__asm__ __volatile__(
   2277  1.17    mgorny 		/* save original ebp & esp using our output registers */
   2278  1.17    mgorny 		"movl    %%esp, %0\n\t"
   2279  1.17    mgorny 		"movl    %%ebp, %1\n\t"
   2280  1.17    mgorny 		/* fill them with clobber pattern */
   2281  1.17    mgorny 		"movl    %2, %%esp\n\t"
   2282  1.17    mgorny 		"movl    %2, %%ebp\n\t"
   2283  1.13    mgorny 		"\n\t"
   2284  1.13    mgorny 		"int3\n\t"
   2285  1.13    mgorny 		"\n\t"
   2286  1.17    mgorny 		/* restore ebp & esp, and save the result */
   2287  1.17    mgorny 		"xchgl   %%esp, %0\n\t"
   2288  1.17    mgorny 		"xchgl   %%ebp, %1\n\t"
   2289  1.17    mgorny 		: "=r"(out[0].u32), "=r"(out[1].u32)
   2290  1.17    mgorny 		: "g"(fill)
   2291  1.13    mgorny 		:
   2292  1.13    mgorny 	);
   2293  1.13    mgorny #else
   2294  1.17    mgorny 	__unreachable();
   2295  1.13    mgorny #endif
   2296  1.13    mgorny }
   2297  1.13    mgorny 
   2298  1.17    mgorny static __inline void set_gp32_ebp_esp_regs(const union x86_test_register data[])
   2299  1.10    mgorny {
   2300  1.17    mgorny #if defined(__i386__)
   2301  1.10    mgorny 	__asm__ __volatile__(
   2302  1.17    mgorny 		/* ebp & ebp are a bit tricky, we must not clobber them */
   2303  1.17    mgorny 		"movl    %%esp, %%eax\n\t"
   2304  1.17    mgorny 		"movl    %%ebp, %%ebx\n\t"
   2305  1.17    mgorny 		"movl    %0, %%esp\n\t"
   2306  1.17    mgorny 		"movl    %1, %%ebp\n\t"
   2307  1.17    mgorny 		"\n\t"
   2308  1.10    mgorny 		"int3\n\t"
   2309  1.17    mgorny 		"\n\t"
   2310  1.17    mgorny 		"movl    %%eax, %%esp\n\t"
   2311  1.17    mgorny 		"movl    %%ebx, %%ebp\n\t"
   2312  1.10    mgorny 		:
   2313  1.17    mgorny 		: "ri"(data[0].u32), "ri"(data[1].u32)
   2314  1.17    mgorny 		: "%eax", "%ebx"
   2315  1.17    mgorny 	);
   2316  1.17    mgorny #else
   2317  1.17    mgorny 	__unreachable();
   2318  1.10    mgorny #endif
   2319  1.10    mgorny }
   2320  1.10    mgorny 
   2321  1.17    mgorny static __inline void get_gp64_regs(union x86_test_register out[])
   2322  1.10    mgorny {
   2323  1.17    mgorny #if defined(__x86_64__)
   2324  1.17    mgorny 	const uint64_t fill = 0x0F0F0F0F0F0F0F0F;
   2325  1.10    mgorny 
   2326  1.17    mgorny 	__asm__ __volatile__(
   2327  1.17    mgorny 		/* save rsp & rbp */
   2328  1.17    mgorny 		"movq    %%rsp, %6\n\t"
   2329  1.17    mgorny 		"movq    %%rbp, %7\n\t"
   2330  1.17    mgorny 		"\n\t"
   2331  1.17    mgorny 		/* fill registers with clobber pattern */
   2332  1.17    mgorny 		"movq    %8, %%rax\n\t"
   2333  1.17    mgorny 		"movq    %8, %%rbx\n\t"
   2334  1.17    mgorny 		"movq    %8, %%rcx\n\t"
   2335  1.17    mgorny 		"movq    %8, %%rdx\n\t"
   2336  1.17    mgorny 		"movq    %8, %%rsp\n\t"
   2337  1.17    mgorny 		"movq    %8, %%rbp\n\t"
   2338  1.17    mgorny 		"movq    %8, %%rsi\n\t"
   2339  1.17    mgorny 		"movq    %8, %%rdi\n\t"
   2340  1.17    mgorny 		"\n\t"
   2341  1.17    mgorny 		"int3\n\t"
   2342  1.17    mgorny 		"\n\t"
   2343  1.17    mgorny 		/* swap saved & current rsp & rbp */
   2344  1.17    mgorny 		"xchgq    %%rsp, %6\n\t"
   2345  1.17    mgorny 		"xchgq    %%rbp, %7\n\t"
   2346  1.17    mgorny 		: "=a"(out[0].u64), "=b"(out[1].u64), "=c"(out[2].u64),
   2347  1.17    mgorny 		  "=d"(out[3].u64), "=S"(out[4].u64), "=D"(out[5].u64),
   2348  1.17    mgorny 		  "=r"(out[6].u64), "=r"(out[7].u64)
   2349  1.17    mgorny 		: "g"(fill)
   2350  1.17    mgorny 	);
   2351  1.10    mgorny #else
   2352  1.17    mgorny 	__unreachable();
   2353  1.10    mgorny #endif
   2354  1.17    mgorny }
   2355  1.10    mgorny 
   2356  1.17    mgorny static __inline void set_gp64_regs(const union x86_test_register data[])
   2357  1.17    mgorny {
   2358  1.10    mgorny #if defined(__x86_64__)
   2359  1.17    mgorny 	__asm__ __volatile__(
   2360  1.17    mgorny 		/* rbp & rbp are a bit tricky, we must not clobber them */
   2361  1.17    mgorny 		"movq    %%rsp, %%r8\n\t"
   2362  1.17    mgorny 		"movq    %%rbp, %%r9\n\t"
   2363  1.17    mgorny 		"movq    %6, %%rsp\n\t"
   2364  1.17    mgorny 		"movq    %7, %%rbp\n\t"
   2365  1.17    mgorny 		"\n\t"
   2366  1.17    mgorny 		"int3\n\t"
   2367  1.17    mgorny 		"\n\t"
   2368  1.17    mgorny 		"movq    %%r8, %%rsp\n\t"
   2369  1.17    mgorny 		"movq    %%r9, %%rbp\n\t"
   2370  1.17    mgorny 		:
   2371  1.17    mgorny 		: "a"(data[0].u64), "b"(data[1].u64), "c"(data[2].u64),
   2372  1.17    mgorny 		  "d"(data[3].u64), "S"(data[4].u64), "D"(data[5].u64),
   2373  1.17    mgorny 		  "r"(data[6].u64), "r"(data[7].u64)
   2374  1.17    mgorny 		: "%r8", "%r9"
   2375  1.17    mgorny 	);
   2376  1.10    mgorny #else
   2377  1.17    mgorny 	__unreachable();
   2378  1.10    mgorny #endif
   2379  1.10    mgorny }
   2380  1.13    mgorny 
   2381  1.17    mgorny static __inline void get_gp64_r8_regs(union x86_test_register out[])
   2382  1.17    mgorny {
   2383  1.17    mgorny #if defined(__x86_64__)
   2384  1.17    mgorny 	const uint64_t fill = 0x0F0F0F0F0F0F0F0F;
   2385  1.16    mgorny 
   2386  1.17    mgorny 	__asm__ __volatile__(
   2387  1.17    mgorny 		/* fill registers with clobber pattern */
   2388  1.17    mgorny 		"movq    %1, %%r8\n\t"
   2389  1.17    mgorny 		"movq    %1, %%r9\n\t"
   2390  1.17    mgorny 		"movq    %1, %%r10\n\t"
   2391  1.17    mgorny 		"movq    %1, %%r11\n\t"
   2392  1.17    mgorny 		"movq    %1, %%r12\n\t"
   2393  1.17    mgorny 		"movq    %1, %%r13\n\t"
   2394  1.17    mgorny 		"movq    %1, %%r14\n\t"
   2395  1.17    mgorny 		"movq    %1, %%r15\n\t"
   2396  1.17    mgorny 		"\n\t"
   2397  1.17    mgorny 		"int3\n\t"
   2398  1.17    mgorny 		"\n\t"
   2399  1.17    mgorny 		"movq    %%r8, 0x00(%0)\n\t"
   2400  1.17    mgorny 		"movq    %%r9, 0x20(%0)\n\t"
   2401  1.17    mgorny 		"movq    %%r10, 0x40(%0)\n\t"
   2402  1.17    mgorny 		"movq    %%r11, 0x60(%0)\n\t"
   2403  1.17    mgorny 		"movq    %%r12, 0x80(%0)\n\t"
   2404  1.17    mgorny 		"movq    %%r13, 0xA0(%0)\n\t"
   2405  1.17    mgorny 		"movq    %%r14, 0xC0(%0)\n\t"
   2406  1.17    mgorny 		"movq    %%r15, 0xE0(%0)\n\t"
   2407  1.17    mgorny 		:
   2408  1.17    mgorny 		: "a"(out), "m"(fill)
   2409  1.17    mgorny 		: "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
   2410  1.17    mgorny 	);
   2411  1.17    mgorny #else
   2412  1.17    mgorny 	__unreachable();
   2413  1.17    mgorny #endif
   2414  1.17    mgorny }
   2415  1.16    mgorny 
   2416  1.17    mgorny static __inline void set_gp64_r8_regs(const union x86_test_register data[])
   2417  1.17    mgorny {
   2418  1.17    mgorny #if defined(__x86_64__)
   2419  1.17    mgorny 	__asm__ __volatile__(
   2420  1.17    mgorny 		"movq    0x00(%0), %%r8\n\t"
   2421  1.17    mgorny 		"movq    0x20(%0), %%r9\n\t"
   2422  1.17    mgorny 		"movq    0x40(%0), %%r10\n\t"
   2423  1.17    mgorny 		"movq    0x60(%0), %%r11\n\t"
   2424  1.17    mgorny 		"movq    0x80(%0), %%r12\n\t"
   2425  1.17    mgorny 		"movq    0xA0(%0), %%r13\n\t"
   2426  1.17    mgorny 		"movq    0xC0(%0), %%r14\n\t"
   2427  1.17    mgorny 		"movq    0xE0(%0), %%r15\n\t"
   2428  1.17    mgorny 		"int3\n\t"
   2429  1.17    mgorny 		:
   2430  1.17    mgorny 		: "b"(data)
   2431  1.17    mgorny 		: "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
   2432  1.17    mgorny 	);
   2433  1.17    mgorny #else
   2434  1.17    mgorny 	__unreachable();
   2435  1.16    mgorny #endif
   2436  1.17    mgorny }
   2437  1.16    mgorny 
   2438  1.27    mgorny static __inline void get_fpu_regs(struct x86_test_fpu_registers *out)
   2439  1.27    mgorny {
   2440  1.27    mgorny 	struct save87 fsave;
   2441  1.27    mgorny 	struct fxsave fxsave;
   2442  1.27    mgorny 
   2443  1.27    mgorny 	__CTASSERT(sizeof(out->st[0]) == 16);
   2444  1.27    mgorny 
   2445  1.27    mgorny 	__asm__ __volatile__(
   2446  1.27    mgorny 		"finit\n\t"
   2447  1.27    mgorny 		"int3\n\t"
   2448  1.27    mgorny #if defined(__x86_64__)
   2449  1.27    mgorny 		"fxsave64 %2\n\t"
   2450  1.27    mgorny #else
   2451  1.27    mgorny 		"fxsave %2\n\t"
   2452  1.27    mgorny #endif
   2453  1.27    mgorny 		"fnstenv %1\n\t"
   2454  1.27    mgorny 		"fnclex\n\t"
   2455  1.27    mgorny 		"fstpt 0x00(%0)\n\t"
   2456  1.27    mgorny 		"fstpt 0x10(%0)\n\t"
   2457  1.27    mgorny 		"fstpt 0x20(%0)\n\t"
   2458  1.27    mgorny 		"fstpt 0x30(%0)\n\t"
   2459  1.27    mgorny 		"fstpt 0x40(%0)\n\t"
   2460  1.27    mgorny 		"fstpt 0x50(%0)\n\t"
   2461  1.27    mgorny 		"fstpt 0x60(%0)\n\t"
   2462  1.27    mgorny 		"fstpt 0x70(%0)\n\t"
   2463  1.27    mgorny 		:
   2464  1.27    mgorny 		: "a"(out->st), "m"(fsave), "m"(fxsave)
   2465  1.27    mgorny 		: "st", "memory"
   2466  1.27    mgorny 	);
   2467  1.27    mgorny 
   2468  1.27    mgorny 	FORKEE_ASSERT(fsave.s87_cw == fxsave.fx_cw);
   2469  1.27    mgorny 	FORKEE_ASSERT(fsave.s87_sw == fxsave.fx_sw);
   2470  1.27    mgorny 
   2471  1.27    mgorny 	/* fsave contains full tw */
   2472  1.27    mgorny 	out->cw = fsave.s87_cw;
   2473  1.27    mgorny 	out->sw = fsave.s87_sw;
   2474  1.27    mgorny 	out->tw = fsave.s87_tw;
   2475  1.27    mgorny 	out->tw_abridged = fxsave.fx_tw;
   2476  1.27    mgorny 	out->opcode = fxsave.fx_opcode;
   2477  1.27    mgorny 	out->ip = fxsave.fx_ip;
   2478  1.27    mgorny 	out->dp = fxsave.fx_dp;
   2479  1.27    mgorny }
   2480  1.27    mgorny 
   2481  1.27    mgorny /* used as single-precision float */
   2482  1.27    mgorny uint32_t x86_test_zero = 0;
   2483  1.27    mgorny 
   2484  1.27    mgorny static __inline void set_fpu_regs(const struct x86_test_fpu_registers *data)
   2485  1.27    mgorny {
   2486  1.27    mgorny 	__CTASSERT(sizeof(data->st[0]) == 16);
   2487  1.27    mgorny 
   2488  1.27    mgorny 	__asm__ __volatile__(
   2489  1.27    mgorny 		"finit\n\t"
   2490  1.27    mgorny 		"fldcw %1\n\t"
   2491  1.27    mgorny 		/* load on stack in reverse order to make it easier to read */
   2492  1.27    mgorny 		"fldt 0x70(%0)\n\t"
   2493  1.27    mgorny 		"fldt 0x60(%0)\n\t"
   2494  1.27    mgorny 		"fldt 0x50(%0)\n\t"
   2495  1.27    mgorny 		"fldt 0x40(%0)\n\t"
   2496  1.27    mgorny 		"fldt 0x30(%0)\n\t"
   2497  1.27    mgorny 		"fldt 0x20(%0)\n\t"
   2498  1.27    mgorny 		"fldt 0x10(%0)\n\t"
   2499  1.27    mgorny 		"fldt 0x00(%0)\n\t"
   2500  1.27    mgorny 		/* free st7 */
   2501  1.27    mgorny 		"ffree %%st(7)\n\t"
   2502  1.27    mgorny 		/* this should trigger a divide-by-zero */
   2503  1.27    mgorny 		"fdivs (%2)\n\t"
   2504  1.27    mgorny 		"int3\n\t"
   2505  1.27    mgorny 		:
   2506  1.27    mgorny 		: "a"(&data->st), "m"(data->cw), "b"(&x86_test_zero)
   2507  1.27    mgorny 		: "st"
   2508  1.27    mgorny 	);
   2509  1.27    mgorny }
   2510  1.27    mgorny 
   2511  1.17    mgorny __attribute__((target("mmx")))
   2512  1.17    mgorny static __inline void get_mm_regs(union x86_test_register out[])
   2513  1.17    mgorny {
   2514  1.17    mgorny 	const uint64_t fill = 0x0F0F0F0F0F0F0F0F;
   2515  1.16    mgorny 
   2516  1.17    mgorny 	__asm__ __volatile__(
   2517  1.17    mgorny 		/* fill registers with clobber pattern */
   2518  1.17    mgorny 		"movq    %1, %%mm0\n\t"
   2519  1.17    mgorny 		"movq    %1, %%mm1\n\t"
   2520  1.17    mgorny 		"movq    %1, %%mm2\n\t"
   2521  1.17    mgorny 		"movq    %1, %%mm3\n\t"
   2522  1.17    mgorny 		"movq    %1, %%mm4\n\t"
   2523  1.17    mgorny 		"movq    %1, %%mm5\n\t"
   2524  1.17    mgorny 		"movq    %1, %%mm6\n\t"
   2525  1.17    mgorny 		"movq    %1, %%mm7\n\t"
   2526  1.17    mgorny 		"\n\t"
   2527  1.17    mgorny 		"int3\n\t"
   2528  1.17    mgorny 		"\n\t"
   2529  1.17    mgorny 		"movq    %%mm0, 0x00(%0)\n\t"
   2530  1.17    mgorny 		"movq    %%mm1, 0x20(%0)\n\t"
   2531  1.17    mgorny 		"movq    %%mm2, 0x40(%0)\n\t"
   2532  1.17    mgorny 		"movq    %%mm3, 0x60(%0)\n\t"
   2533  1.17    mgorny 		"movq    %%mm4, 0x80(%0)\n\t"
   2534  1.17    mgorny 		"movq    %%mm5, 0xA0(%0)\n\t"
   2535  1.17    mgorny 		"movq    %%mm6, 0xC0(%0)\n\t"
   2536  1.17    mgorny 		"movq    %%mm7, 0xE0(%0)\n\t"
   2537  1.17    mgorny 		:
   2538  1.17    mgorny 		: "a"(out), "m"(fill)
   2539  1.17    mgorny 		: "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7"
   2540  1.17    mgorny 	);
   2541  1.17    mgorny }
   2542  1.16    mgorny 
   2543  1.17    mgorny __attribute__((target("mmx")))
   2544  1.17    mgorny static __inline void set_mm_regs(const union x86_test_register data[])
   2545  1.17    mgorny {
   2546  1.17    mgorny 	__asm__ __volatile__(
   2547  1.17    mgorny 		"movq    0x00(%0), %%mm0\n\t"
   2548  1.17    mgorny 		"movq    0x20(%0), %%mm1\n\t"
   2549  1.17    mgorny 		"movq    0x40(%0), %%mm2\n\t"
   2550  1.17    mgorny 		"movq    0x60(%0), %%mm3\n\t"
   2551  1.17    mgorny 		"movq    0x80(%0), %%mm4\n\t"
   2552  1.17    mgorny 		"movq    0xA0(%0), %%mm5\n\t"
   2553  1.17    mgorny 		"movq    0xC0(%0), %%mm6\n\t"
   2554  1.17    mgorny 		"movq    0xE0(%0), %%mm7\n\t"
   2555  1.17    mgorny 		"int3\n\t"
   2556  1.17    mgorny 		:
   2557  1.17    mgorny 		: "b"(data)
   2558  1.17    mgorny 		: "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7"
   2559  1.17    mgorny 	);
   2560  1.16    mgorny }
   2561  1.16    mgorny 
   2562  1.17    mgorny __attribute__((target("sse")))
   2563  1.17    mgorny static __inline void get_xmm_regs(union x86_test_register out[])
   2564  1.16    mgorny {
   2565  1.17    mgorny 	union x86_test_register fill __aligned(32) = {
   2566  1.17    mgorny 		.xmm={ 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F }
   2567  1.17    mgorny 	};
   2568  1.17    mgorny 
   2569  1.16    mgorny 	__asm__ __volatile__(
   2570  1.17    mgorny 		/* fill registers with clobber pattern */
   2571  1.17    mgorny 		"movaps  %1, %%xmm0\n\t"
   2572  1.17    mgorny 		"movaps  %1, %%xmm1\n\t"
   2573  1.17    mgorny 		"movaps  %1, %%xmm2\n\t"
   2574  1.17    mgorny 		"movaps  %1, %%xmm3\n\t"
   2575  1.17    mgorny 		"movaps  %1, %%xmm4\n\t"
   2576  1.17    mgorny 		"movaps  %1, %%xmm5\n\t"
   2577  1.17    mgorny 		"movaps  %1, %%xmm6\n\t"
   2578  1.17    mgorny 		"movaps  %1, %%xmm7\n\t"
   2579  1.16    mgorny #if defined(__x86_64__)
   2580  1.17    mgorny 		"movaps  %1, %%xmm8\n\t"
   2581  1.17    mgorny 		"movaps  %1, %%xmm9\n\t"
   2582  1.17    mgorny 		"movaps  %1, %%xmm10\n\t"
   2583  1.17    mgorny 		"movaps  %1, %%xmm11\n\t"
   2584  1.17    mgorny 		"movaps  %1, %%xmm12\n\t"
   2585  1.17    mgorny 		"movaps  %1, %%xmm13\n\t"
   2586  1.17    mgorny 		"movaps  %1, %%xmm14\n\t"
   2587  1.17    mgorny 		"movaps  %1, %%xmm15\n\t"
   2588  1.16    mgorny #endif
   2589  1.17    mgorny 		"\n\t"
   2590  1.16    mgorny 		"int3\n\t"
   2591  1.17    mgorny 		"\n\t"
   2592  1.17    mgorny 		"movaps  %%xmm0, 0x000(%0)\n\t"
   2593  1.17    mgorny 		"movaps  %%xmm1, 0x020(%0)\n\t"
   2594  1.17    mgorny 		"movaps  %%xmm2, 0x040(%0)\n\t"
   2595  1.17    mgorny 		"movaps  %%xmm3, 0x060(%0)\n\t"
   2596  1.17    mgorny 		"movaps  %%xmm4, 0x080(%0)\n\t"
   2597  1.17    mgorny 		"movaps  %%xmm5, 0x0A0(%0)\n\t"
   2598  1.17    mgorny 		"movaps  %%xmm6, 0x0C0(%0)\n\t"
   2599  1.17    mgorny 		"movaps  %%xmm7, 0x0E0(%0)\n\t"
   2600  1.17    mgorny #if defined(__x86_64__)
   2601  1.17    mgorny 		"movaps  %%xmm8, 0x100(%0)\n\t"
   2602  1.17    mgorny 		"movaps  %%xmm9, 0x120(%0)\n\t"
   2603  1.17    mgorny 		"movaps  %%xmm10, 0x140(%0)\n\t"
   2604  1.17    mgorny 		"movaps  %%xmm11, 0x160(%0)\n\t"
   2605  1.17    mgorny 		"movaps  %%xmm12, 0x180(%0)\n\t"
   2606  1.17    mgorny 		"movaps  %%xmm13, 0x1A0(%0)\n\t"
   2607  1.17    mgorny 		"movaps  %%xmm14, 0x1C0(%0)\n\t"
   2608  1.17    mgorny 		"movaps  %%xmm15, 0x1E0(%0)\n\t"
   2609  1.17    mgorny #endif
   2610  1.16    mgorny 		:
   2611  1.17    mgorny 		: "a"(out), "m"(fill)
   2612  1.17    mgorny 		: "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
   2613  1.16    mgorny #if defined(__x86_64__)
   2614  1.17    mgorny 		, "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14",
   2615  1.17    mgorny 		"%xmm15"
   2616  1.16    mgorny #endif
   2617  1.16    mgorny 	);
   2618  1.16    mgorny }
   2619  1.16    mgorny 
   2620  1.17    mgorny __attribute__((target("sse")))
   2621  1.17    mgorny static __inline void set_xmm_regs(const union x86_test_register data[])
   2622  1.16    mgorny {
   2623  1.17    mgorny 	__asm__ __volatile__(
   2624  1.17    mgorny 		"movaps   0x000(%0), %%xmm0\n\t"
   2625  1.17    mgorny 		"movaps   0x020(%0), %%xmm1\n\t"
   2626  1.17    mgorny 		"movaps   0x040(%0), %%xmm2\n\t"
   2627  1.17    mgorny 		"movaps   0x060(%0), %%xmm3\n\t"
   2628  1.17    mgorny 		"movaps   0x080(%0), %%xmm4\n\t"
   2629  1.17    mgorny 		"movaps   0x0A0(%0), %%xmm5\n\t"
   2630  1.17    mgorny 		"movaps   0x0C0(%0), %%xmm6\n\t"
   2631  1.17    mgorny 		"movaps   0x0E0(%0), %%xmm7\n\t"
   2632  1.17    mgorny #if defined(__x86_64__)
   2633  1.17    mgorny 		"movaps   0x100(%0), %%xmm8\n\t"
   2634  1.17    mgorny 		"movaps   0x120(%0), %%xmm9\n\t"
   2635  1.17    mgorny 		"movaps   0x140(%0), %%xmm10\n\t"
   2636  1.17    mgorny 		"movaps   0x160(%0), %%xmm11\n\t"
   2637  1.17    mgorny 		"movaps   0x180(%0), %%xmm12\n\t"
   2638  1.17    mgorny 		"movaps   0x1A0(%0), %%xmm13\n\t"
   2639  1.17    mgorny 		"movaps   0x1C0(%0), %%xmm14\n\t"
   2640  1.17    mgorny 		"movaps   0x1E0(%0), %%xmm15\n\t"
   2641  1.16    mgorny #endif
   2642  1.17    mgorny 		"int3\n\t"
   2643  1.17    mgorny 		:
   2644  1.17    mgorny 		: "b"(data)
   2645  1.17    mgorny 		: "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6",
   2646  1.17    mgorny 		"%xmm7"
   2647  1.17    mgorny #if defined(__x86_64__)
   2648  1.17    mgorny 		, "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13",
   2649  1.17    mgorny 		"%xmm14", "%xmm15"
   2650  1.16    mgorny #endif
   2651  1.17    mgorny 	);
   2652  1.16    mgorny }
   2653  1.16    mgorny 
   2654  1.16    mgorny __attribute__((target("avx")))
   2655  1.17    mgorny static __inline void get_ymm_regs(union x86_test_register out[])
   2656  1.16    mgorny {
   2657  1.17    mgorny 	union x86_test_register fill __aligned(32) = {
   2658  1.17    mgorny 		{ 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F,
   2659  1.17    mgorny 		  0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F }
   2660  1.16    mgorny 	};
   2661  1.16    mgorny 
   2662  1.16    mgorny 	__asm__ __volatile__(
   2663  1.16    mgorny 		/* fill registers with clobber pattern */
   2664  1.16    mgorny 		"vmovaps  %1, %%ymm0\n\t"
   2665  1.16    mgorny 		"vmovaps  %1, %%ymm1\n\t"
   2666  1.16    mgorny 		"vmovaps  %1, %%ymm2\n\t"
   2667  1.16    mgorny 		"vmovaps  %1, %%ymm3\n\t"
   2668  1.16    mgorny 		"vmovaps  %1, %%ymm4\n\t"
   2669  1.16    mgorny 		"vmovaps  %1, %%ymm5\n\t"
   2670  1.16    mgorny 		"vmovaps  %1, %%ymm6\n\t"
   2671  1.16    mgorny 		"vmovaps  %1, %%ymm7\n\t"
   2672  1.16    mgorny #if defined(__x86_64__)
   2673  1.16    mgorny 		"vmovaps  %1, %%ymm8\n\t"
   2674  1.16    mgorny 		"vmovaps  %1, %%ymm9\n\t"
   2675  1.16    mgorny 		"vmovaps  %1, %%ymm10\n\t"
   2676  1.16    mgorny 		"vmovaps  %1, %%ymm11\n\t"
   2677  1.16    mgorny 		"vmovaps  %1, %%ymm12\n\t"
   2678  1.16    mgorny 		"vmovaps  %1, %%ymm13\n\t"
   2679  1.16    mgorny 		"vmovaps  %1, %%ymm14\n\t"
   2680  1.16    mgorny 		"vmovaps  %1, %%ymm15\n\t"
   2681  1.16    mgorny #endif
   2682  1.16    mgorny 		"\n\t"
   2683  1.16    mgorny 		"int3\n\t"
   2684  1.16    mgorny 		"\n\t"
   2685  1.16    mgorny 		"vmovaps %%ymm0,  0x000(%0)\n\t"
   2686  1.16    mgorny 		"vmovaps %%ymm1,  0x020(%0)\n\t"
   2687  1.16    mgorny 		"vmovaps %%ymm2,  0x040(%0)\n\t"
   2688  1.16    mgorny 		"vmovaps %%ymm3,  0x060(%0)\n\t"
   2689  1.16    mgorny 		"vmovaps %%ymm4,  0x080(%0)\n\t"
   2690  1.16    mgorny 		"vmovaps %%ymm5,  0x0A0(%0)\n\t"
   2691  1.16    mgorny 		"vmovaps %%ymm6,  0x0C0(%0)\n\t"
   2692  1.16    mgorny 		"vmovaps %%ymm7,  0x0E0(%0)\n\t"
   2693  1.16    mgorny #if defined(__x86_64__)
   2694  1.16    mgorny 		"vmovaps %%ymm8,  0x100(%0)\n\t"
   2695  1.16    mgorny 		"vmovaps %%ymm9,  0x120(%0)\n\t"
   2696  1.16    mgorny 		"vmovaps %%ymm10, 0x140(%0)\n\t"
   2697  1.16    mgorny 		"vmovaps %%ymm11, 0x160(%0)\n\t"
   2698  1.16    mgorny 		"vmovaps %%ymm12, 0x180(%0)\n\t"
   2699  1.16    mgorny 		"vmovaps %%ymm13, 0x1A0(%0)\n\t"
   2700  1.16    mgorny 		"vmovaps %%ymm14, 0x1C0(%0)\n\t"
   2701  1.16    mgorny 		"vmovaps %%ymm15, 0x1E0(%0)\n\t"
   2702  1.16    mgorny #endif
   2703  1.16    mgorny 		:
   2704  1.17    mgorny 		: "a"(out), "m"(fill)
   2705  1.16    mgorny 		: "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6", "%ymm7"
   2706  1.16    mgorny #if defined(__x86_64__)
   2707  1.16    mgorny 		, "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13", "%ymm14",
   2708  1.16    mgorny 		"%ymm15"
   2709  1.16    mgorny #endif
   2710  1.16    mgorny 	);
   2711  1.16    mgorny }
   2712  1.16    mgorny 
   2713  1.17    mgorny __attribute__((target("avx")))
   2714  1.17    mgorny static __inline void set_ymm_regs(const union x86_test_register data[])
   2715  1.16    mgorny {
   2716  1.17    mgorny 	__asm__ __volatile__(
   2717  1.17    mgorny 		"vmovaps  0x000(%0), %%ymm0\n\t"
   2718  1.17    mgorny 		"vmovaps  0x020(%0), %%ymm1\n\t"
   2719  1.17    mgorny 		"vmovaps  0x040(%0), %%ymm2\n\t"
   2720  1.17    mgorny 		"vmovaps  0x060(%0), %%ymm3\n\t"
   2721  1.17    mgorny 		"vmovaps  0x080(%0), %%ymm4\n\t"
   2722  1.17    mgorny 		"vmovaps  0x0A0(%0), %%ymm5\n\t"
   2723  1.17    mgorny 		"vmovaps  0x0C0(%0), %%ymm6\n\t"
   2724  1.17    mgorny 		"vmovaps  0x0E0(%0), %%ymm7\n\t"
   2725  1.17    mgorny #if defined(__x86_64__)
   2726  1.17    mgorny 		"vmovaps  0x100(%0), %%ymm8\n\t"
   2727  1.17    mgorny 		"vmovaps  0x120(%0), %%ymm9\n\t"
   2728  1.17    mgorny 		"vmovaps  0x140(%0), %%ymm10\n\t"
   2729  1.17    mgorny 		"vmovaps  0x160(%0), %%ymm11\n\t"
   2730  1.17    mgorny 		"vmovaps  0x180(%0), %%ymm12\n\t"
   2731  1.17    mgorny 		"vmovaps  0x1A0(%0), %%ymm13\n\t"
   2732  1.17    mgorny 		"vmovaps  0x1C0(%0), %%ymm14\n\t"
   2733  1.17    mgorny 		"vmovaps  0x1E0(%0), %%ymm15\n\t"
   2734  1.17    mgorny #endif
   2735  1.17    mgorny 		"int3\n\t"
   2736  1.17    mgorny 		:
   2737  1.17    mgorny 		: "b"(data)
   2738  1.17    mgorny 		: "%ymm0", "%ymm1", "%ymm2", "%ymm3", "%ymm4", "%ymm5", "%ymm6",
   2739  1.17    mgorny 		"%ymm7"
   2740  1.17    mgorny #if defined(__x86_64__)
   2741  1.17    mgorny 		, "%ymm8", "%ymm9", "%ymm10", "%ymm11", "%ymm12", "%ymm13",
   2742  1.17    mgorny 		"%ymm14", "%ymm15"
   2743  1.17    mgorny #endif
   2744  1.17    mgorny 	);
   2745  1.16    mgorny }
   2746  1.16    mgorny 
   2747  1.17    mgorny static void
   2748  1.17    mgorny x86_register_test(enum x86_test_regset regset, enum x86_test_registers regs,
   2749  1.17    mgorny     enum x86_test_regmode regmode)
   2750  1.16    mgorny {
   2751  1.16    mgorny 	const int exitval = 5;
   2752  1.16    mgorny 	pid_t child, wpid;
   2753  1.16    mgorny #if defined(TWAIT_HAVE_STATUS)
   2754  1.16    mgorny 	const int sigval = SIGTRAP;
   2755  1.16    mgorny 	int status;
   2756  1.16    mgorny #endif
   2757  1.17    mgorny 	struct reg gpr;
   2758  1.17    mgorny 	struct fpreg fpr;
   2759  1.17    mgorny #if defined(__i386__)
   2760  1.17    mgorny 	struct xmmregs xmm;
   2761  1.17    mgorny #endif
   2762  1.16    mgorny 	struct xstate xst;
   2763  1.16    mgorny 	struct iovec iov;
   2764  1.18    mgorny 	struct fxsave* fxs = NULL;
   2765  1.17    mgorny 	uint64_t xst_flags = 0;
   2766  1.18    mgorny 	char core_path[] = "/tmp/core.XXXXXX";
   2767  1.18    mgorny 	int core_fd;
   2768  1.17    mgorny 
   2769  1.17    mgorny 	const union x86_test_register expected[] __aligned(32) = {
   2770  1.17    mgorny 		{{ 0x0706050403020100, 0x0F0E0D0C0B0A0908,
   2771  1.17    mgorny 		   0x1716151413121110, 0x1F1E1D1C1B1A1918, }},
   2772  1.17    mgorny 		{{ 0x0807060504030201, 0x100F0E0D0C0B0A09,
   2773  1.17    mgorny 		   0x1817161514131211, 0x201F1E1D1C1B1A19, }},
   2774  1.17    mgorny 		{{ 0x0908070605040302, 0x11100F0E0D0C0B0A,
   2775  1.17    mgorny 		   0x1918171615141312, 0x21201F1E1D1C1B1A, }},
   2776  1.17    mgorny 		{{ 0x0A09080706050403, 0x1211100F0E0D0C0B,
   2777  1.17    mgorny 		   0x1A19181716151413, 0x2221201F1E1D1C1B, }},
   2778  1.17    mgorny 		{{ 0x0B0A090807060504, 0x131211100F0E0D0C,
   2779  1.17    mgorny 		   0x1B1A191817161514, 0x232221201F1E1D1C, }},
   2780  1.17    mgorny 		{{ 0x0C0B0A0908070605, 0x14131211100F0E0D,
   2781  1.17    mgorny 		   0x1C1B1A1918171615, 0x24232221201F1E1D, }},
   2782  1.17    mgorny 		{{ 0x0D0C0B0A09080706, 0x1514131211100F0E,
   2783  1.17    mgorny 		   0x1D1C1B1A19181716, 0x2524232221201F1E, }},
   2784  1.17    mgorny 		{{ 0x0E0D0C0B0A090807, 0x161514131211100F,
   2785  1.17    mgorny 		   0x1E1D1C1B1A191817, 0x262524232221201F, }},
   2786  1.17    mgorny 		{{ 0x0F0E0D0C0B0A0908, 0x1716151413121110,
   2787  1.17    mgorny 		   0x1F1E1D1C1B1A1918, 0x2726252423222120, }},
   2788  1.17    mgorny 		{{ 0x100F0E0D0C0B0A09, 0x1817161514131211,
   2789  1.17    mgorny 		   0x201F1E1D1C1B1A19, 0x2827262524232221, }},
   2790  1.17    mgorny 		{{ 0x11100F0E0D0C0B0A, 0x1918171615141312,
   2791  1.17    mgorny 		   0x21201F1E1D1C1B1A, 0x2928272625242322, }},
   2792  1.17    mgorny 		{{ 0x1211100F0E0D0C0B, 0x1A19181716151413,
   2793  1.17    mgorny 		   0x2221201F1E1D1C1B, 0x2A29282726252423, }},
   2794  1.17    mgorny 		{{ 0x131211100F0E0D0C, 0x1B1A191817161514,
   2795  1.17    mgorny 		   0x232221201F1E1D1C, 0x2B2A292827262524, }},
   2796  1.17    mgorny 		{{ 0x14131211100F0E0D, 0x1C1B1A1918171615,
   2797  1.17    mgorny 		   0x24232221201F1E1D, 0x2C2B2A2928272625, }},
   2798  1.17    mgorny 		{{ 0x1514131211100F0E, 0x1D1C1B1A19181716,
   2799  1.17    mgorny 		   0x2524232221201F1E, 0x2D2C2B2A29282726, }},
   2800  1.17    mgorny 		{{ 0x161514131211100F, 0x1E1D1C1B1A191817,
   2801  1.17    mgorny 		   0x262524232221201F, 0x2E2D2C2B2A292827, }},
   2802  1.17    mgorny 	};
   2803  1.17    mgorny 
   2804  1.27    mgorny 	const struct x86_test_fpu_registers expected_fpu = {
   2805  1.27    mgorny 		.st = {
   2806  1.27    mgorny 			{0x8000000000000000, 0x4000}, /* +2.0 */
   2807  1.27    mgorny 			{0x3f00000000000000, 0x0000}, /* 1.654785e-4932 */
   2808  1.27    mgorny 			{0x0000000000000000, 0x0000}, /* +0 */
   2809  1.27    mgorny 			{0x0000000000000000, 0x8000}, /* -0 */
   2810  1.27    mgorny 			{0x8000000000000000, 0x7fff}, /* +inf */
   2811  1.27    mgorny 			{0x8000000000000000, 0xffff}, /* -inf */
   2812  1.27    mgorny 			{0xc000000000000000, 0xffff}, /* nan */
   2813  1.27    mgorny 			/* st(7) will be freed to test tag word better */
   2814  1.27    mgorny 			{0x0000000000000000, 0x0000}, /* +0 */
   2815  1.27    mgorny 		},
   2816  1.27    mgorny 		/* 0000 0011 0111 1011
   2817  1.27    mgorny 		 *             PU OZDI -- unmask divide-by-zero exc.
   2818  1.27    mgorny 		 *           RR --------- reserved
   2819  1.27    mgorny 		 *        PC ------------ 64-bit precision
   2820  1.27    mgorny 		 *      RC -------------- round to nearest
   2821  1.27    mgorny 		 *    I ----------------- allow interrupts (unused)
   2822  1.27    mgorny 		 */
   2823  1.27    mgorny 		.cw = 0x037b,
   2824  1.27    mgorny 		/* 1000 0000 1000 0100
   2825  1.27    mgorny 		 *            SPU OZDI -- divide-by-zero exception
   2826  1.27    mgorny 		 *           I ---------- interrupt (exception handling)
   2827  1.27    mgorny 		 *  C    CCC ------------ condition codes
   2828  1.27    mgorny 		 *   TO P --------------- top register is 0
   2829  1.27    mgorny 		 * B -------------------- FPU is busy
   2830  1.27    mgorny 		 */
   2831  1.27    mgorny 		.sw = 0x8084,
   2832  1.27    mgorny 		/* 1110 1010 0101 1000
   2833  1.27    mgorny 		 * R7R6 R5R4 R3R2 R1R0
   2834  1.27    mgorny 		 *                  nz -- non-zero (+2.0)
   2835  1.27    mgorny 		 *                sp ---- special (denormal)
   2836  1.27    mgorny 		 *           zrzr ------- zeroes
   2837  1.27    mgorny 		 *   sp spsp ------------ specials (NaN + infinities)
   2838  1.27    mgorny 		 * em ------------------- empty register
   2839  1.27    mgorny 		 */
   2840  1.27    mgorny 		.tw = 0xea58,
   2841  1.27    mgorny 		/* 0111 1111 -- registers 0 to 6 are used */
   2842  1.27    mgorny 		.tw_abridged = 0x7f,
   2843  1.27    mgorny 		/* FDIV */
   2844  1.27    mgorny 		.opcode = 0x0033,
   2845  1.27    mgorny 		/* random bits for IP/DP write test
   2846  1.27    mgorny 		 * keep it below 48 bits since it can be truncated
   2847  1.27    mgorny 		 */
   2848  1.27    mgorny 		.ip = {.fa_64 = 0x00000a9876543210},
   2849  1.27    mgorny 		.dp = {.fa_64 = 0x0000056789abcdef},
   2850  1.27    mgorny 	};
   2851  1.27    mgorny 
   2852  1.17    mgorny 	bool need_32 = false, need_64 = false, need_cpuid = false;
   2853  1.17    mgorny 
   2854  1.17    mgorny 	switch (regs) {
   2855  1.17    mgorny 	case GPREGS_32:
   2856  1.17    mgorny 	case GPREGS_32_EBP_ESP:
   2857  1.17    mgorny 		need_32 = true;
   2858  1.17    mgorny 		break;
   2859  1.17    mgorny 	case GPREGS_64:
   2860  1.17    mgorny 	case GPREGS_64_R8:
   2861  1.17    mgorny 		need_64 = true;
   2862  1.17    mgorny 		break;
   2863  1.27    mgorny 	case FPREGS_FPU:
   2864  1.27    mgorny 		break;
   2865  1.17    mgorny 	case FPREGS_MM:
   2866  1.17    mgorny 	case FPREGS_XMM:
   2867  1.17    mgorny 	case FPREGS_YMM:
   2868  1.17    mgorny 		need_cpuid = true;
   2869  1.17    mgorny 		break;
   2870  1.17    mgorny 	}
   2871  1.16    mgorny 
   2872  1.17    mgorny 	if (need_32) {
   2873  1.17    mgorny #if defined(__x86_64__)
   2874  1.17    mgorny 		atf_tc_skip("Test requires 32-bit mode");
   2875  1.17    mgorny #endif
   2876  1.17    mgorny 	}
   2877  1.17    mgorny 	if (need_64) {
   2878  1.17    mgorny #if defined(__i386__)
   2879  1.17    mgorny 		atf_tc_skip("Test requires 64-bit mode");
   2880  1.16    mgorny #endif
   2881  1.17    mgorny 	}
   2882  1.16    mgorny 
   2883  1.17    mgorny 	if (need_cpuid) {
   2884  1.17    mgorny 		/* verify whether needed instruction sets are supported here */
   2885  1.16    mgorny 		unsigned int eax, ebx, ecx, edx;
   2886  1.17    mgorny 
   2887  1.17    mgorny 		DPRINTF("Before invoking cpuid\n");
   2888  1.16    mgorny 		if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
   2889  1.16    mgorny 			atf_tc_skip("CPUID is not supported by the CPU");
   2890  1.16    mgorny 
   2891  1.17    mgorny 		DPRINTF("cpuid: ECX = %08x, EDX = %08xd\n", ecx, edx);
   2892  1.16    mgorny 
   2893  1.17    mgorny 		switch (regs) {
   2894  1.17    mgorny 		case FPREGS_YMM:
   2895  1.17    mgorny 			if (!(ecx & bit_AVX))
   2896  1.17    mgorny 				atf_tc_skip("AVX is not supported by the CPU");
   2897  1.17    mgorny 			/*FALLTHROUGH*/
   2898  1.17    mgorny 		case FPREGS_XMM:
   2899  1.17    mgorny 			if (!(edx & bit_SSE))
   2900  1.17    mgorny 				atf_tc_skip("SSE is not supported by the CPU");
   2901  1.17    mgorny 			break;
   2902  1.17    mgorny 		case FPREGS_MM:
   2903  1.17    mgorny 			if (!(edx & bit_MMX))
   2904  1.17    mgorny 				atf_tc_skip("MMX is not supported by the CPU");
   2905  1.17    mgorny 			break;
   2906  1.17    mgorny 		case GPREGS_32:
   2907  1.17    mgorny 		case GPREGS_32_EBP_ESP:
   2908  1.17    mgorny 		case GPREGS_64:
   2909  1.17    mgorny 		case GPREGS_64_R8:
   2910  1.27    mgorny 		case FPREGS_FPU:
   2911  1.17    mgorny 			__unreachable();
   2912  1.17    mgorny 		}
   2913  1.16    mgorny 	}
   2914  1.16    mgorny 
   2915  1.16    mgorny 	DPRINTF("Before forking process PID=%d\n", getpid());
   2916  1.16    mgorny 	SYSCALL_REQUIRE((child = fork()) != -1);
   2917  1.16    mgorny 	if (child == 0) {
   2918  1.17    mgorny 		union x86_test_register vals[16] __aligned(32);
   2919  1.27    mgorny 		struct x86_test_fpu_registers vals_fpu;
   2920  1.16    mgorny 
   2921  1.16    mgorny 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
   2922  1.16    mgorny 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
   2923  1.16    mgorny 
   2924  1.16    mgorny 		DPRINTF("Before running assembly from child\n");
   2925  1.17    mgorny 		switch (regmode) {
   2926  1.17    mgorny 		case TEST_GETREGS:
   2927  1.18    mgorny 		case TEST_COREDUMP:
   2928  1.17    mgorny 			switch (regs) {
   2929  1.17    mgorny 			case GPREGS_32:
   2930  1.17    mgorny 				set_gp32_regs(expected);
   2931  1.17    mgorny 				break;
   2932  1.17    mgorny 			case GPREGS_32_EBP_ESP:
   2933  1.17    mgorny 				set_gp32_ebp_esp_regs(expected);
   2934  1.17    mgorny 				break;
   2935  1.17    mgorny 			case GPREGS_64:
   2936  1.17    mgorny 				set_gp64_regs(expected);
   2937  1.17    mgorny 				break;
   2938  1.17    mgorny 			case GPREGS_64_R8:
   2939  1.17    mgorny 				set_gp64_r8_regs(expected);
   2940  1.17    mgorny 				break;
   2941  1.27    mgorny 			case FPREGS_FPU:
   2942  1.27    mgorny 				set_fpu_regs(&expected_fpu);
   2943  1.27    mgorny 				break;
   2944  1.17    mgorny 			case FPREGS_MM:
   2945  1.17    mgorny 				set_mm_regs(expected);
   2946  1.17    mgorny 				break;
   2947  1.17    mgorny 			case FPREGS_XMM:
   2948  1.17    mgorny 				set_xmm_regs(expected);
   2949  1.17    mgorny 				break;
   2950  1.17    mgorny 			case FPREGS_YMM:
   2951  1.17    mgorny 				set_ymm_regs(expected);
   2952  1.17    mgorny 				break;
   2953  1.17    mgorny 			}
   2954  1.17    mgorny 			break;
   2955  1.17    mgorny 		case TEST_SETREGS:
   2956  1.17    mgorny 			switch (regs) {
   2957  1.17    mgorny 			case GPREGS_32:
   2958  1.17    mgorny 				get_gp32_regs(vals);
   2959  1.17    mgorny 				break;
   2960  1.17    mgorny 			case GPREGS_32_EBP_ESP:
   2961  1.17    mgorny 				get_gp32_ebp_esp_regs(vals);
   2962  1.17    mgorny 				break;
   2963  1.17    mgorny 			case GPREGS_64:
   2964  1.17    mgorny 				get_gp64_regs(vals);
   2965  1.17    mgorny 				break;
   2966  1.17    mgorny 			case GPREGS_64_R8:
   2967  1.17    mgorny 				get_gp64_r8_regs(vals);
   2968  1.17    mgorny 				break;
   2969  1.27    mgorny 			case FPREGS_FPU:
   2970  1.27    mgorny 				get_fpu_regs(&vals_fpu);
   2971  1.27    mgorny 				break;
   2972  1.17    mgorny 			case FPREGS_MM:
   2973  1.17    mgorny 				get_mm_regs(vals);
   2974  1.17    mgorny 				break;
   2975  1.17    mgorny 			case FPREGS_XMM:
   2976  1.17    mgorny 				get_xmm_regs(vals);
   2977  1.17    mgorny 				break;
   2978  1.17    mgorny 			case FPREGS_YMM:
   2979  1.17    mgorny 				get_ymm_regs(vals);
   2980  1.17    mgorny 				break;
   2981  1.17    mgorny 			}
   2982  1.17    mgorny 
   2983  1.17    mgorny 			DPRINTF("Before comparing results\n");
   2984  1.17    mgorny 			switch (regs) {
   2985  1.17    mgorny 			case GPREGS_32:
   2986  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[5].u32,
   2987  1.17    mgorny 				    &expected[5].u32, sizeof(vals->u32)));
   2988  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[4].u32,
   2989  1.17    mgorny 				    &expected[4].u32, sizeof(vals->u32)));
   2990  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[3].u32,
   2991  1.17    mgorny 				    &expected[3].u32, sizeof(vals->u32)));
   2992  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[2].u32,
   2993  1.17    mgorny 				    &expected[2].u32, sizeof(vals->u32)));
   2994  1.17    mgorny 				/*FALLTHROUGH*/
   2995  1.17    mgorny 			case GPREGS_32_EBP_ESP:
   2996  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[1].u32,
   2997  1.17    mgorny 				    &expected[1].u32, sizeof(vals->u32)));
   2998  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[0].u32,
   2999  1.17    mgorny 				    &expected[0].u32, sizeof(vals->u32)));
   3000  1.17    mgorny 				break;
   3001  1.17    mgorny 			case GPREGS_64:
   3002  1.17    mgorny 			case GPREGS_64_R8:
   3003  1.17    mgorny 			case FPREGS_MM:
   3004  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[0].u64,
   3005  1.17    mgorny 				    &expected[0].u64, sizeof(vals->u64)));
   3006  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[1].u64,
   3007  1.17    mgorny 				    &expected[1].u64, sizeof(vals->u64)));
   3008  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[2].u64,
   3009  1.17    mgorny 				    &expected[2].u64, sizeof(vals->u64)));
   3010  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[3].u64,
   3011  1.17    mgorny 				    &expected[3].u64, sizeof(vals->u64)));
   3012  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[4].u64,
   3013  1.17    mgorny 				    &expected[4].u64, sizeof(vals->u64)));
   3014  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[5].u64,
   3015  1.17    mgorny 				    &expected[5].u64, sizeof(vals->u64)));
   3016  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[6].u64,
   3017  1.17    mgorny 				    &expected[6].u64, sizeof(vals->u64)));
   3018  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[7].u64,
   3019  1.17    mgorny 				    &expected[7].u64, sizeof(vals->u64)));
   3020  1.17    mgorny 				break;
   3021  1.27    mgorny 			case FPREGS_FPU:
   3022  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.cw == expected_fpu.cw);
   3023  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.sw == expected_fpu.sw);
   3024  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.tw == expected_fpu.tw);
   3025  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.tw_abridged
   3026  1.27    mgorny 				    == expected_fpu.tw_abridged);
   3027  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.ip.fa_64
   3028  1.27    mgorny 				    == expected_fpu.ip.fa_64);
   3029  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.dp.fa_64
   3030  1.27    mgorny 				    == expected_fpu.dp.fa_64);
   3031  1.27    mgorny 
   3032  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[0].sign_exp
   3033  1.27    mgorny 				    == expected_fpu.st[0].sign_exp);
   3034  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[0].mantissa
   3035  1.27    mgorny 				    == expected_fpu.st[0].mantissa);
   3036  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[1].sign_exp
   3037  1.27    mgorny 				    == expected_fpu.st[1].sign_exp);
   3038  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[1].mantissa
   3039  1.27    mgorny 				    == expected_fpu.st[1].mantissa);
   3040  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[2].sign_exp
   3041  1.27    mgorny 				    == expected_fpu.st[2].sign_exp);
   3042  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[2].mantissa
   3043  1.27    mgorny 				    == expected_fpu.st[2].mantissa);
   3044  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[3].sign_exp
   3045  1.27    mgorny 				    == expected_fpu.st[3].sign_exp);
   3046  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[3].mantissa
   3047  1.27    mgorny 				    == expected_fpu.st[3].mantissa);
   3048  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[4].sign_exp
   3049  1.27    mgorny 				    == expected_fpu.st[4].sign_exp);
   3050  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[4].mantissa
   3051  1.27    mgorny 				    == expected_fpu.st[4].mantissa);
   3052  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[5].sign_exp
   3053  1.27    mgorny 				    == expected_fpu.st[5].sign_exp);
   3054  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[5].mantissa
   3055  1.27    mgorny 				    == expected_fpu.st[5].mantissa);
   3056  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[6].sign_exp
   3057  1.27    mgorny 				    == expected_fpu.st[6].sign_exp);
   3058  1.27    mgorny 				FORKEE_ASSERT(vals_fpu.st[6].mantissa
   3059  1.27    mgorny 				    == expected_fpu.st[6].mantissa);
   3060  1.27    mgorny 				/* st(7) is left empty == undefined */
   3061  1.27    mgorny 				break;
   3062  1.17    mgorny 			case FPREGS_XMM:
   3063  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[0].xmm,
   3064  1.17    mgorny 				    &expected[0].xmm, sizeof(vals->xmm)));
   3065  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[1].xmm,
   3066  1.17    mgorny 				    &expected[1].xmm, sizeof(vals->xmm)));
   3067  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[2].xmm,
   3068  1.17    mgorny 				    &expected[2].xmm, sizeof(vals->xmm)));
   3069  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[3].xmm,
   3070  1.17    mgorny 				    &expected[3].xmm, sizeof(vals->xmm)));
   3071  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[4].xmm,
   3072  1.17    mgorny 				    &expected[4].xmm, sizeof(vals->xmm)));
   3073  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[5].xmm,
   3074  1.17    mgorny 				    &expected[5].xmm, sizeof(vals->xmm)));
   3075  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[6].xmm,
   3076  1.17    mgorny 				    &expected[6].xmm, sizeof(vals->xmm)));
   3077  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[7].xmm,
   3078  1.17    mgorny 				    &expected[7].xmm, sizeof(vals->xmm)));
   3079  1.17    mgorny #if defined(__x86_64__)
   3080  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[8].xmm,
   3081  1.17    mgorny 				    &expected[8].xmm, sizeof(vals->xmm)));
   3082  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[9].xmm,
   3083  1.17    mgorny 				    &expected[9].xmm, sizeof(vals->xmm)));
   3084  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[10].xmm,
   3085  1.17    mgorny 				    &expected[10].xmm, sizeof(vals->xmm)));
   3086  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[11].xmm,
   3087  1.17    mgorny 				    &expected[11].xmm, sizeof(vals->xmm)));
   3088  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[12].xmm,
   3089  1.17    mgorny 				    &expected[12].xmm, sizeof(vals->xmm)));
   3090  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[13].xmm,
   3091  1.17    mgorny 				    &expected[13].xmm, sizeof(vals->xmm)));
   3092  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[14].xmm,
   3093  1.17    mgorny 				    &expected[14].xmm, sizeof(vals->xmm)));
   3094  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[15].xmm,
   3095  1.17    mgorny 				    &expected[15].xmm, sizeof(vals->xmm)));
   3096  1.17    mgorny #endif
   3097  1.17    mgorny 				break;
   3098  1.17    mgorny 			case FPREGS_YMM:
   3099  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[0].ymm,
   3100  1.17    mgorny 				    &expected[0].ymm, sizeof(vals->ymm)));
   3101  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[1].ymm,
   3102  1.17    mgorny 				    &expected[1].ymm, sizeof(vals->ymm)));
   3103  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[2].ymm,
   3104  1.17    mgorny 				    &expected[2].ymm, sizeof(vals->ymm)));
   3105  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[3].ymm,
   3106  1.17    mgorny 				    &expected[3].ymm, sizeof(vals->ymm)));
   3107  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[4].ymm,
   3108  1.17    mgorny 				    &expected[4].ymm, sizeof(vals->ymm)));
   3109  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[5].ymm,
   3110  1.17    mgorny 				    &expected[5].ymm, sizeof(vals->ymm)));
   3111  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[6].ymm,
   3112  1.17    mgorny 				    &expected[6].ymm, sizeof(vals->ymm)));
   3113  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[7].ymm,
   3114  1.17    mgorny 				    &expected[7].ymm, sizeof(vals->ymm)));
   3115  1.17    mgorny #if defined(__x86_64__)
   3116  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[8].ymm,
   3117  1.17    mgorny 				    &expected[8].ymm, sizeof(vals->ymm)));
   3118  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[9].ymm,
   3119  1.17    mgorny 				    &expected[9].ymm, sizeof(vals->ymm)));
   3120  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[10].ymm,
   3121  1.17    mgorny 				    &expected[10].ymm, sizeof(vals->ymm)));
   3122  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[11].ymm,
   3123  1.17    mgorny 				    &expected[11].ymm, sizeof(vals->ymm)));
   3124  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[12].ymm,
   3125  1.17    mgorny 				    &expected[12].ymm, sizeof(vals->ymm)));
   3126  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[13].ymm,
   3127  1.17    mgorny 				    &expected[13].ymm, sizeof(vals->ymm)));
   3128  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[14].ymm,
   3129  1.17    mgorny 				    &expected[14].ymm, sizeof(vals->ymm)));
   3130  1.17    mgorny 				FORKEE_ASSERT(!memcmp(&vals[15].ymm,
   3131  1.17    mgorny 				    &expected[15].ymm, sizeof(vals->ymm)));
   3132  1.17    mgorny #endif
   3133  1.17    mgorny 				break;
   3134  1.17    mgorny 			}
   3135  1.17    mgorny 			break;
   3136  1.17    mgorny 		}
   3137  1.16    mgorny 
   3138  1.16    mgorny 		DPRINTF("Before exiting of the child process\n");
   3139  1.16    mgorny 		_exit(exitval);
   3140  1.16    mgorny 	}
   3141  1.16    mgorny 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
   3142  1.16    mgorny 
   3143  1.16    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   3144  1.16    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   3145  1.16    mgorny 
   3146  1.16    mgorny 	validate_status_stopped(status, sigval);
   3147  1.16    mgorny 
   3148  1.18    mgorny 	if (regset == TEST_XSTATE) {
   3149  1.17    mgorny 		switch (regs) {
   3150  1.27    mgorny 		case FPREGS_FPU:
   3151  1.17    mgorny 		case FPREGS_MM:
   3152  1.17    mgorny 			xst_flags |= XCR0_X87;
   3153  1.17    mgorny 			break;
   3154  1.17    mgorny 		case FPREGS_YMM:
   3155  1.17    mgorny 			xst_flags |= XCR0_YMM_Hi128;
   3156  1.17    mgorny 			/*FALLTHROUGH*/
   3157  1.17    mgorny 		case FPREGS_XMM:
   3158  1.17    mgorny 			xst_flags |= XCR0_SSE;
   3159  1.17    mgorny 			break;
   3160  1.17    mgorny 		case GPREGS_32:
   3161  1.17    mgorny 		case GPREGS_32_EBP_ESP:
   3162  1.17    mgorny 		case GPREGS_64:
   3163  1.17    mgorny 		case GPREGS_64_R8:
   3164  1.17    mgorny 			__unreachable();
   3165  1.17    mgorny 			break;
   3166  1.17    mgorny 		}
   3167  1.18    mgorny 	}
   3168  1.18    mgorny 
   3169  1.18    mgorny 	switch (regmode) {
   3170  1.18    mgorny 	case TEST_GETREGS:
   3171  1.18    mgorny 	case TEST_SETREGS:
   3172  1.27    mgorny 		if (regset == TEST_GPREGS || regs == FPREGS_FPU) {
   3173  1.18    mgorny 			DPRINTF("Call GETREGS for the child process\n");
   3174  1.18    mgorny 			SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0)
   3175  1.18    mgorny 			    != -1);
   3176  1.27    mgorny 		}
   3177  1.27    mgorny 
   3178  1.27    mgorny 		switch (regset) {
   3179  1.27    mgorny 		case TEST_GPREGS:
   3180  1.27    mgorny 			/* already handled above */
   3181  1.18    mgorny 			break;
   3182  1.18    mgorny 		case TEST_XMMREGS:
   3183  1.18    mgorny #if defined(__i386__)
   3184  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_YMM);
   3185  1.18    mgorny 			DPRINTF("Call GETXMMREGS for the child process\n");
   3186  1.18    mgorny 			SYSCALL_REQUIRE(ptrace(PT_GETXMMREGS, child, &xmm, 0)
   3187  1.18    mgorny 			    != -1);
   3188  1.18    mgorny 			fxs = &xmm.fxstate;
   3189  1.18    mgorny 			break;
   3190  1.18    mgorny #else
   3191  1.18    mgorny 			/*FALLTHROUGH*/
   3192  1.18    mgorny #endif
   3193  1.18    mgorny 		case TEST_FPREGS:
   3194  1.18    mgorny #if defined(__x86_64__)
   3195  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_YMM);
   3196  1.18    mgorny 			fxs = &fpr.fxstate;
   3197  1.18    mgorny #else
   3198  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_XMM);
   3199  1.18    mgorny #endif
   3200  1.18    mgorny 			DPRINTF("Call GETFPREGS for the child process\n");
   3201  1.18    mgorny 			SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &fpr, 0)
   3202  1.18    mgorny 			    != -1);
   3203  1.18    mgorny 			break;
   3204  1.18    mgorny 		case TEST_XSTATE:
   3205  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU);
   3206  1.18    mgorny 			iov.iov_base = &xst;
   3207  1.18    mgorny 			iov.iov_len = sizeof(xst);
   3208  1.18    mgorny 
   3209  1.18    mgorny 			DPRINTF("Call GETXSTATE for the child process\n");
   3210  1.18    mgorny 			SYSCALL_REQUIRE(ptrace(PT_GETXSTATE, child, &iov, 0)
   3211  1.18    mgorny 			    != -1);
   3212  1.18    mgorny 
   3213  1.18    mgorny 			ATF_REQUIRE((xst.xs_rfbm & xst_flags) == xst_flags);
   3214  1.18    mgorny 			switch (regmode) {
   3215  1.18    mgorny 			case TEST_SETREGS:
   3216  1.18    mgorny 				xst.xs_rfbm = xst_flags;
   3217  1.18    mgorny 				xst.xs_xstate_bv = xst_flags;
   3218  1.18    mgorny 				break;
   3219  1.18    mgorny 			case TEST_GETREGS:
   3220  1.18    mgorny 				ATF_REQUIRE((xst.xs_xstate_bv & xst_flags)
   3221  1.18    mgorny 				    == xst_flags);
   3222  1.18    mgorny 				break;
   3223  1.18    mgorny 			case TEST_COREDUMP:
   3224  1.18    mgorny 				__unreachable();
   3225  1.18    mgorny 				break;
   3226  1.18    mgorny 			}
   3227  1.18    mgorny 
   3228  1.18    mgorny 			fxs = &xst.xs_fxsave;
   3229  1.18    mgorny 			break;
   3230  1.18    mgorny 		}
   3231  1.18    mgorny 		break;
   3232  1.18    mgorny 	case TEST_COREDUMP:
   3233  1.18    mgorny 		SYSCALL_REQUIRE((core_fd = mkstemp(core_path)) != -1);
   3234  1.18    mgorny 		close(core_fd);
   3235  1.18    mgorny 
   3236  1.18    mgorny 		DPRINTF("Call DUMPCORE for the child process\n");
   3237  1.18    mgorny 		SYSCALL_REQUIRE(ptrace(PT_DUMPCORE, child, core_path,
   3238  1.18    mgorny 		    strlen(core_path)) != -1);
   3239  1.17    mgorny 
   3240  1.27    mgorny 		if (regset == TEST_GPREGS || regs == FPREGS_FPU) {
   3241  1.18    mgorny 			DPRINTF("Parse core file for PT_GETREGS\n");
   3242  1.18    mgorny 			ATF_REQUIRE_EQ(core_find_note(core_path,
   3243  1.25   thorpej 			    "NetBSD-CORE@*", PT_GETREGS, &gpr, sizeof(gpr)),
   3244  1.18    mgorny 			    sizeof(gpr));
   3245  1.27    mgorny 		}
   3246  1.27    mgorny 
   3247  1.27    mgorny 		switch (regset) {
   3248  1.27    mgorny 		case TEST_GPREGS:
   3249  1.27    mgorny 			/* handled above */
   3250  1.18    mgorny 			break;
   3251  1.18    mgorny 		case TEST_XMMREGS:
   3252  1.18    mgorny #if defined(__i386__)
   3253  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_YMM);
   3254  1.18    mgorny 			unlink(core_path);
   3255  1.18    mgorny 			atf_tc_skip("XMMREGS not supported in core dumps");
   3256  1.18    mgorny 			break;
   3257  1.18    mgorny #else
   3258  1.18    mgorny 			/*FALLTHROUGH*/
   3259  1.18    mgorny #endif
   3260  1.18    mgorny 		case TEST_FPREGS:
   3261  1.18    mgorny #if defined(__x86_64__)
   3262  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_YMM);
   3263  1.18    mgorny 			fxs = &fpr.fxstate;
   3264  1.18    mgorny #else
   3265  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU && regs < FPREGS_XMM);
   3266  1.18    mgorny #endif
   3267  1.18    mgorny 			DPRINTF("Parse core file for PT_GETFPREGS\n");
   3268  1.18    mgorny 			ATF_REQUIRE_EQ(core_find_note(core_path,
   3269  1.25   thorpej 			    "NetBSD-CORE@*", PT_GETFPREGS, &fpr, sizeof(fpr)),
   3270  1.18    mgorny 			    sizeof(fpr));
   3271  1.17    mgorny 			break;
   3272  1.18    mgorny 		case TEST_XSTATE:
   3273  1.27    mgorny 			ATF_REQUIRE(regs >= FPREGS_FPU);
   3274  1.18    mgorny 			DPRINTF("Parse core file for PT_GETXSTATE\n");
   3275  1.18    mgorny 			ATF_REQUIRE_EQ(core_find_note(core_path,
   3276  1.25   thorpej 			    "NetBSD-CORE@*", PT_GETXSTATE, &xst, sizeof(xst)),
   3277  1.18    mgorny 			    sizeof(xst));
   3278  1.17    mgorny 			ATF_REQUIRE((xst.xs_xstate_bv & xst_flags)
   3279  1.17    mgorny 			    == xst_flags);
   3280  1.18    mgorny 			fxs = &xst.xs_fxsave;
   3281  1.17    mgorny 			break;
   3282  1.17    mgorny 		}
   3283  1.18    mgorny 		unlink(core_path);
   3284  1.17    mgorny 	}
   3285  1.16    mgorny 
   3286  1.17    mgorny #if defined(__x86_64__)
   3287  1.27    mgorny #define ST_EXP(n) fxs->fx_87_ac[n].r.f87_exp_sign
   3288  1.26    mgorny #define ST_MAN(n) fxs->fx_87_ac[n].r.f87_mantissa
   3289  1.17    mgorny #else
   3290  1.27    mgorny #define ST_EXP(n) *(							\
   3291  1.27    mgorny     regset == TEST_FPREGS						\
   3292  1.27    mgorny     ? &fpr.fstate.s87_ac[n].f87_exp_sign				\
   3293  1.27    mgorny     : &fxs->fx_87_ac[n].r.f87_exp_sign					\
   3294  1.27    mgorny     )
   3295  1.26    mgorny #define ST_MAN(n) *(							\
   3296  1.26    mgorny     regset == TEST_FPREGS						\
   3297  1.26    mgorny     ? &fpr.fstate.s87_ac[n].f87_mantissa				\
   3298  1.26    mgorny     : &fxs->fx_87_ac[n].r.f87_mantissa					\
   3299  1.26    mgorny     )
   3300  1.17    mgorny #endif
   3301  1.16    mgorny 
   3302  1.17    mgorny 	switch (regmode) {
   3303  1.17    mgorny 	case TEST_GETREGS:
   3304  1.18    mgorny 	case TEST_COREDUMP:
   3305  1.17    mgorny 		switch (regs) {
   3306  1.17    mgorny 		case GPREGS_32:
   3307  1.17    mgorny #if defined(__i386__)
   3308  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_eax, expected[0].u32);
   3309  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_ebx, expected[1].u32);
   3310  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_ecx, expected[2].u32);
   3311  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_edx, expected[3].u32);
   3312  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_esi, expected[4].u32);
   3313  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_edi, expected[5].u32);
   3314  1.17    mgorny #endif
   3315  1.17    mgorny 			break;
   3316  1.17    mgorny 		case GPREGS_32_EBP_ESP:
   3317  1.17    mgorny #if defined(__i386__)
   3318  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_esp, expected[0].u32);
   3319  1.17    mgorny 			ATF_CHECK_EQ((uint32_t)gpr.r_ebp, expected[1].u32);
   3320  1.17    mgorny #endif
   3321  1.17    mgorny 			break;
   3322  1.17    mgorny 		case GPREGS_64:
   3323  1.17    mgorny #if defined(__x86_64__)
   3324  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RAX],
   3325  1.17    mgorny 			    expected[0].u64);
   3326  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RBX],
   3327  1.17    mgorny 			    expected[1].u64);
   3328  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RCX],
   3329  1.17    mgorny 			    expected[2].u64);
   3330  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RDX],
   3331  1.17    mgorny 			    expected[3].u64);
   3332  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RSI],
   3333  1.17    mgorny 			    expected[4].u64);
   3334  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RDI],
   3335  1.17    mgorny 			    expected[5].u64);
   3336  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RSP],
   3337  1.17    mgorny 			    expected[6].u64);
   3338  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_RBP],
   3339  1.17    mgorny 			    expected[7].u64);
   3340  1.17    mgorny #endif
   3341  1.17    mgorny 			break;
   3342  1.17    mgorny 		case GPREGS_64_R8:
   3343  1.17    mgorny #if defined(__x86_64__)
   3344  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R8],
   3345  1.17    mgorny 			    expected[0].u64);
   3346  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R9],
   3347  1.17    mgorny 			    expected[1].u64);
   3348  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R10],
   3349  1.17    mgorny 			    expected[2].u64);
   3350  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R11],
   3351  1.17    mgorny 			    expected[3].u64);
   3352  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R12],
   3353  1.17    mgorny 			    expected[4].u64);
   3354  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R13],
   3355  1.17    mgorny 			    expected[5].u64);
   3356  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R14],
   3357  1.17    mgorny 			    expected[6].u64);
   3358  1.17    mgorny 			ATF_CHECK_EQ((uint64_t)gpr.regs[_REG_R15],
   3359  1.17    mgorny 			    expected[7].u64);
   3360  1.17    mgorny #endif
   3361  1.17    mgorny 			break;
   3362  1.27    mgorny 		case FPREGS_FPU:
   3363  1.27    mgorny #if defined(__i386__)
   3364  1.27    mgorny 			if (regset == TEST_FPREGS) {
   3365  1.27    mgorny 				/* GETFPREGS on i386 */
   3366  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_cw,
   3367  1.27    mgorny 				    expected_fpu.cw);
   3368  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_sw,
   3369  1.27    mgorny 				    expected_fpu.sw);
   3370  1.27    mgorny #if 0 /* TODO: translation from FXSAVE is broken */
   3371  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_tw,
   3372  1.27    mgorny 				    expected_fpu.tw);
   3373  1.27    mgorny #endif
   3374  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_opcode,
   3375  1.27    mgorny 				    expected_fpu.opcode);
   3376  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_ip.fa_32.fa_off,
   3377  1.27    mgorny 				    (uint32_t)gpr.r_eip - 3);
   3378  1.27    mgorny 				ATF_CHECK_EQ(fpr.fstate.s87_dp.fa_32.fa_off,
   3379  1.27    mgorny 				    (uint32_t)&x86_test_zero);
   3380  1.27    mgorny 				/* note: fa_seg is missing on newer CPUs */
   3381  1.27    mgorny 			} else
   3382  1.27    mgorny #endif
   3383  1.27    mgorny 			{
   3384  1.27    mgorny 				/* amd64 or GETXSTATE on i386 */
   3385  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_cw, expected_fpu.cw);
   3386  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_sw, expected_fpu.sw);
   3387  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_tw,
   3388  1.27    mgorny 				    expected_fpu.tw_abridged);
   3389  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_opcode,
   3390  1.27    mgorny 				    expected_fpu.opcode);
   3391  1.27    mgorny #if defined(__x86_64__)
   3392  1.27    mgorny #if 0 /* TODO: kernel needs patching to call *XSAVE64 */
   3393  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_ip.fa_64,
   3394  1.27    mgorny 				    ((uint64_t)gpr.regs[_REG_RIP]) - 3);
   3395  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_dp.fa_64,
   3396  1.27    mgorny 				    (uint64_t)&x86_test_zero);
   3397  1.27    mgorny #endif
   3398  1.27    mgorny #else
   3399  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_ip.fa_32.fa_off,
   3400  1.27    mgorny 				    (uint32_t)gpr.r_eip - 3);
   3401  1.27    mgorny 				ATF_CHECK_EQ(fxs->fx_dp.fa_32.fa_off,
   3402  1.27    mgorny 				    (uint32_t)&x86_test_zero);
   3403  1.27    mgorny 				/* note: fa_seg is missing on newer CPUs */
   3404  1.27    mgorny #endif
   3405  1.27    mgorny 			}
   3406  1.27    mgorny 
   3407  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(0), expected_fpu.st[0].sign_exp);
   3408  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(0), expected_fpu.st[0].mantissa);
   3409  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(1), expected_fpu.st[1].sign_exp);
   3410  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(1), expected_fpu.st[1].mantissa);
   3411  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(2), expected_fpu.st[2].sign_exp);
   3412  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(2), expected_fpu.st[2].mantissa);
   3413  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(3), expected_fpu.st[3].sign_exp);
   3414  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(3), expected_fpu.st[3].mantissa);
   3415  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(4), expected_fpu.st[4].sign_exp);
   3416  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(4), expected_fpu.st[4].mantissa);
   3417  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(5), expected_fpu.st[5].sign_exp);
   3418  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(5), expected_fpu.st[5].mantissa);
   3419  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(6), expected_fpu.st[6].sign_exp);
   3420  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(6), expected_fpu.st[6].mantissa);
   3421  1.27    mgorny 			ATF_CHECK_EQ(ST_EXP(7), expected_fpu.st[7].sign_exp);
   3422  1.27    mgorny 			ATF_CHECK_EQ(ST_MAN(7), expected_fpu.st[7].mantissa);
   3423  1.27    mgorny 			break;
   3424  1.17    mgorny 		case FPREGS_MM:
   3425  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(0), expected[0].u64);
   3426  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(1), expected[1].u64);
   3427  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(2), expected[2].u64);
   3428  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(3), expected[3].u64);
   3429  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(4), expected[4].u64);
   3430  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(5), expected[5].u64);
   3431  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(6), expected[6].u64);
   3432  1.26    mgorny 			ATF_CHECK_EQ(ST_MAN(7), expected[7].u64);
   3433  1.17    mgorny 			break;
   3434  1.17    mgorny 		case FPREGS_YMM:
   3435  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[0],
   3436  1.17    mgorny 			    &expected[0].ymm.c, sizeof(expected->ymm)/2));
   3437  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[1],
   3438  1.17    mgorny 			    &expected[1].ymm.c, sizeof(expected->ymm)/2));
   3439  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[2],
   3440  1.17    mgorny 			    &expected[2].ymm.c, sizeof(expected->ymm)/2));
   3441  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[3],
   3442  1.17    mgorny 			    &expected[3].ymm.c, sizeof(expected->ymm)/2));
   3443  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[4],
   3444  1.17    mgorny 			    &expected[4].ymm.c, sizeof(expected->ymm)/2));
   3445  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[5],
   3446  1.17    mgorny 			    &expected[5].ymm.c, sizeof(expected->ymm)/2));
   3447  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[6],
   3448  1.17    mgorny 			    &expected[6].ymm.c, sizeof(expected->ymm)/2));
   3449  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[7],
   3450  1.17    mgorny 			    &expected[7].ymm.c, sizeof(expected->ymm)/2));
   3451  1.17    mgorny #if defined(__x86_64__)
   3452  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[8],
   3453  1.17    mgorny 			    &expected[8].ymm.c, sizeof(expected->ymm)/2));
   3454  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[9],
   3455  1.17    mgorny 			    &expected[9].ymm.c, sizeof(expected->ymm)/2));
   3456  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[10],
   3457  1.17    mgorny 			    &expected[10].ymm.c, sizeof(expected->ymm)/2));
   3458  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[11],
   3459  1.17    mgorny 			    &expected[11].ymm.c, sizeof(expected->ymm)/2));
   3460  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[12],
   3461  1.17    mgorny 			    &expected[12].ymm.c, sizeof(expected->ymm)/2));
   3462  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[13],
   3463  1.17    mgorny 			    &expected[13].ymm.c, sizeof(expected->ymm)/2));
   3464  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[14],
   3465  1.17    mgorny 			    &expected[14].ymm.c, sizeof(expected->ymm)/2));
   3466  1.17    mgorny 			ATF_CHECK(!memcmp(&xst.xs_ymm_hi128.xs_ymm[15],
   3467  1.17    mgorny 			    &expected[15].ymm.c, sizeof(expected->ymm)/2));
   3468  1.17    mgorny #endif
   3469  1.17    mgorny 			/*FALLTHROUGH*/
   3470  1.17    mgorny 		case FPREGS_XMM:
   3471  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[0], &expected[0].ymm.a,
   3472  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3473  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[1], &expected[1].ymm.a,
   3474  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3475  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[2], &expected[2].ymm.a,
   3476  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3477  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[3], &expected[3].ymm.a,
   3478  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3479  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[4], &expected[4].ymm.a,
   3480  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3481  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[5], &expected[5].ymm.a,
   3482  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3483  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[6], &expected[6].ymm.a,
   3484  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3485  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[7], &expected[7].ymm.a,
   3486  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3487  1.17    mgorny #if defined(__x86_64__)
   3488  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[8], &expected[8].ymm.a,
   3489  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3490  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[9], &expected[9].ymm.a,
   3491  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3492  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[10], &expected[10].ymm.a,
   3493  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3494  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[11], &expected[11].ymm.a,
   3495  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3496  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[12], &expected[12].ymm.a,
   3497  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3498  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[13], &expected[13].ymm.a,
   3499  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3500  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[14], &expected[14].ymm.a,
   3501  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3502  1.17    mgorny 			ATF_CHECK(!memcmp(&fxs->fx_xmm[15], &expected[15].ymm.a,
   3503  1.17    mgorny 			    sizeof(expected->ymm)/2));
   3504  1.17    mgorny #endif
   3505  1.17    mgorny 			break;
   3506  1.17    mgorny 		}
   3507  1.17    mgorny 		break;
   3508  1.17    mgorny 	case TEST_SETREGS:
   3509  1.17    mgorny 		switch (regs) {
   3510  1.17    mgorny 		case GPREGS_32:
   3511  1.17    mgorny #if defined(__i386__)
   3512  1.17    mgorny 			gpr.r_eax = expected[0].u32;
   3513  1.17    mgorny 			gpr.r_ebx = expected[1].u32;
   3514  1.17    mgorny 			gpr.r_ecx = expected[2].u32;
   3515  1.17    mgorny 			gpr.r_edx = expected[3].u32;
   3516  1.17    mgorny 			gpr.r_esi = expected[4].u32;
   3517  1.17    mgorny 			gpr.r_edi = expected[5].u32;
   3518  1.17    mgorny #endif
   3519  1.17    mgorny 			break;
   3520  1.17    mgorny 		case GPREGS_32_EBP_ESP:
   3521  1.17    mgorny #if defined(__i386__)
   3522  1.17    mgorny 			gpr.r_esp = expected[0].u32;
   3523  1.17    mgorny 			gpr.r_ebp = expected[1].u32;
   3524  1.17    mgorny #endif
   3525  1.17    mgorny 			break;
   3526  1.17    mgorny 		case GPREGS_64:
   3527  1.17    mgorny #if defined(__x86_64__)
   3528  1.17    mgorny 			gpr.regs[_REG_RAX] = expected[0].u64;
   3529  1.17    mgorny 			gpr.regs[_REG_RBX] = expected[1].u64;
   3530  1.17    mgorny 			gpr.regs[_REG_RCX] = expected[2].u64;
   3531  1.17    mgorny 			gpr.regs[_REG_RDX] = expected[3].u64;
   3532  1.17    mgorny 			gpr.regs[_REG_RSI] = expected[4].u64;
   3533  1.17    mgorny 			gpr.regs[_REG_RDI] = expected[5].u64;
   3534  1.17    mgorny 			gpr.regs[_REG_RSP] = expected[6].u64;
   3535  1.17    mgorny 			gpr.regs[_REG_RBP] = expected[7].u64;
   3536  1.17    mgorny #endif
   3537  1.17    mgorny 			break;
   3538  1.17    mgorny 		case GPREGS_64_R8:
   3539  1.17    mgorny #if defined(__x86_64__)
   3540  1.17    mgorny 			gpr.regs[_REG_R8] = expected[0].u64;
   3541  1.17    mgorny 			gpr.regs[_REG_R9] = expected[1].u64;
   3542  1.17    mgorny 			gpr.regs[_REG_R10] = expected[2].u64;
   3543  1.17    mgorny 			gpr.regs[_REG_R11] = expected[3].u64;
   3544  1.17    mgorny 			gpr.regs[_REG_R12] = expected[4].u64;
   3545  1.17    mgorny 			gpr.regs[_REG_R13] = expected[5].u64;
   3546  1.17    mgorny 			gpr.regs[_REG_R14] = expected[6].u64;
   3547  1.17    mgorny 			gpr.regs[_REG_R15] = expected[7].u64;
   3548  1.17    mgorny #endif
   3549  1.17    mgorny 			break;
   3550  1.27    mgorny 		case FPREGS_FPU:
   3551  1.27    mgorny #if defined(__i386__)
   3552  1.27    mgorny 			if (regset == TEST_FPREGS) {
   3553  1.27    mgorny 				/* SETFPREGS on i386 */
   3554  1.27    mgorny 				fpr.fstate.s87_cw = expected_fpu.cw;
   3555  1.27    mgorny 				fpr.fstate.s87_sw = expected_fpu.sw;
   3556  1.27    mgorny //#if 0 /* TODO: translation from FXSAVE is broken */
   3557  1.27    mgorny 				fpr.fstate.s87_tw = expected_fpu.tw;
   3558  1.27    mgorny //#endif
   3559  1.27    mgorny 				fpr.fstate.s87_opcode = expected_fpu.opcode;
   3560  1.27    mgorny 				fpr.fstate.s87_ip = expected_fpu.ip;
   3561  1.27    mgorny 				fpr.fstate.s87_dp = expected_fpu.dp;
   3562  1.27    mgorny 			} else
   3563  1.27    mgorny #endif /*defined(__i386__)*/
   3564  1.27    mgorny 			{
   3565  1.27    mgorny 				/* amd64 or SETXSTATE on i386 */
   3566  1.27    mgorny 				fxs->fx_cw = expected_fpu.cw;
   3567  1.27    mgorny 				fxs->fx_sw = expected_fpu.sw;
   3568  1.27    mgorny 				fxs->fx_tw = expected_fpu.tw_abridged;
   3569  1.27    mgorny 				fxs->fx_opcode = expected_fpu.opcode;
   3570  1.27    mgorny 				fxs->fx_ip = expected_fpu.ip;
   3571  1.27    mgorny 				fxs->fx_dp = expected_fpu.dp;
   3572  1.27    mgorny 			}
   3573  1.27    mgorny 
   3574  1.27    mgorny 			ST_EXP(0) = expected_fpu.st[0].sign_exp;
   3575  1.27    mgorny 			ST_MAN(0) = expected_fpu.st[0].mantissa;
   3576  1.27    mgorny 			ST_EXP(1) = expected_fpu.st[1].sign_exp;
   3577  1.27    mgorny 			ST_MAN(1) = expected_fpu.st[1].mantissa;
   3578  1.27    mgorny 			ST_EXP(2) = expected_fpu.st[2].sign_exp;
   3579  1.27    mgorny 			ST_MAN(2) = expected_fpu.st[2].mantissa;
   3580  1.27    mgorny 			ST_EXP(3) = expected_fpu.st[3].sign_exp;
   3581  1.27    mgorny 			ST_MAN(3) = expected_fpu.st[3].mantissa;
   3582  1.27    mgorny 			ST_EXP(4) = expected_fpu.st[4].sign_exp;
   3583  1.27    mgorny 			ST_MAN(4) = expected_fpu.st[4].mantissa;
   3584  1.27    mgorny 			ST_EXP(5) = expected_fpu.st[5].sign_exp;
   3585  1.27    mgorny 			ST_MAN(5) = expected_fpu.st[5].mantissa;
   3586  1.27    mgorny 			ST_EXP(6) = expected_fpu.st[6].sign_exp;
   3587  1.27    mgorny 			ST_MAN(6) = expected_fpu.st[6].mantissa;
   3588  1.27    mgorny 			ST_EXP(7) = expected_fpu.st[7].sign_exp;
   3589  1.27    mgorny 			ST_MAN(7) = expected_fpu.st[7].mantissa;
   3590  1.27    mgorny 			break;
   3591  1.17    mgorny 		case FPREGS_MM:
   3592  1.26    mgorny 			ST_MAN(0) = expected[0].u64;
   3593  1.26    mgorny 			ST_MAN(1) = expected[1].u64;
   3594  1.26    mgorny 			ST_MAN(2) = expected[2].u64;
   3595  1.26    mgorny 			ST_MAN(3) = expected[3].u64;
   3596  1.26    mgorny 			ST_MAN(4) = expected[4].u64;
   3597  1.26    mgorny 			ST_MAN(5) = expected[5].u64;
   3598  1.26    mgorny 			ST_MAN(6) = expected[6].u64;
   3599  1.26    mgorny 			ST_MAN(7) = expected[7].u64;
   3600  1.17    mgorny 			break;
   3601  1.17    mgorny 		case FPREGS_YMM:
   3602  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[0],
   3603  1.17    mgorny 			    &expected[0].ymm.c, sizeof(expected->ymm)/2);
   3604  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[1],
   3605  1.17    mgorny 			    &expected[1].ymm.c, sizeof(expected->ymm)/2);
   3606  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[2],
   3607  1.17    mgorny 			    &expected[2].ymm.c, sizeof(expected->ymm)/2);
   3608  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[3],
   3609  1.17    mgorny 			    &expected[3].ymm.c, sizeof(expected->ymm)/2);
   3610  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[4],
   3611  1.17    mgorny 			    &expected[4].ymm.c, sizeof(expected->ymm)/2);
   3612  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[5],
   3613  1.17    mgorny 			    &expected[5].ymm.c, sizeof(expected->ymm)/2);
   3614  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[6],
   3615  1.17    mgorny 			    &expected[6].ymm.c, sizeof(expected->ymm)/2);
   3616  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[7],
   3617  1.17    mgorny 			    &expected[7].ymm.c, sizeof(expected->ymm)/2);
   3618  1.17    mgorny #if defined(__x86_64__)
   3619  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[8],
   3620  1.17    mgorny 			    &expected[8].ymm.c, sizeof(expected->ymm)/2);
   3621  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[9],
   3622  1.17    mgorny 			    &expected[9].ymm.c, sizeof(expected->ymm)/2);
   3623  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[10],
   3624  1.17    mgorny 			    &expected[10].ymm.c, sizeof(expected->ymm)/2);
   3625  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[11],
   3626  1.17    mgorny 			    &expected[11].ymm.c, sizeof(expected->ymm)/2);
   3627  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[12],
   3628  1.17    mgorny 			    &expected[12].ymm.c, sizeof(expected->ymm)/2);
   3629  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[13],
   3630  1.17    mgorny 			    &expected[13].ymm.c, sizeof(expected->ymm)/2);
   3631  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[14],
   3632  1.17    mgorny 			    &expected[14].ymm.c, sizeof(expected->ymm)/2);
   3633  1.17    mgorny 			memcpy(&xst.xs_ymm_hi128.xs_ymm[15],
   3634  1.17    mgorny 			    &expected[15].ymm.c, sizeof(expected->ymm)/2);
   3635  1.17    mgorny #endif
   3636  1.17    mgorny 			/*FALLTHROUGH*/
   3637  1.17    mgorny 		case FPREGS_XMM:
   3638  1.17    mgorny 			memcpy(&fxs->fx_xmm[0], &expected[0].ymm.a,
   3639  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3640  1.17    mgorny 			memcpy(&fxs->fx_xmm[1], &expected[1].ymm.a,
   3641  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3642  1.17    mgorny 			memcpy(&fxs->fx_xmm[2], &expected[2].ymm.a,
   3643  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3644  1.17    mgorny 			memcpy(&fxs->fx_xmm[3], &expected[3].ymm.a,
   3645  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3646  1.17    mgorny 			memcpy(&fxs->fx_xmm[4], &expected[4].ymm.a,
   3647  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3648  1.17    mgorny 			memcpy(&fxs->fx_xmm[5], &expected[5].ymm.a,
   3649  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3650  1.17    mgorny 			memcpy(&fxs->fx_xmm[6], &expected[6].ymm.a,
   3651  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3652  1.17    mgorny 			memcpy(&fxs->fx_xmm[7], &expected[7].ymm.a,
   3653  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3654  1.17    mgorny #if defined(__x86_64__)
   3655  1.17    mgorny 			memcpy(&fxs->fx_xmm[8], &expected[8].ymm.a,
   3656  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3657  1.17    mgorny 			memcpy(&fxs->fx_xmm[9], &expected[9].ymm.a,
   3658  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3659  1.17    mgorny 			memcpy(&fxs->fx_xmm[10], &expected[10].ymm.a,
   3660  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3661  1.17    mgorny 			memcpy(&fxs->fx_xmm[11], &expected[11].ymm.a,
   3662  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3663  1.17    mgorny 			memcpy(&fxs->fx_xmm[12], &expected[12].ymm.a,
   3664  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3665  1.17    mgorny 			memcpy(&fxs->fx_xmm[13], &expected[13].ymm.a,
   3666  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3667  1.17    mgorny 			memcpy(&fxs->fx_xmm[14], &expected[14].ymm.a,
   3668  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3669  1.17    mgorny 			memcpy(&fxs->fx_xmm[15], &expected[15].ymm.a,
   3670  1.17    mgorny 			    sizeof(expected->ymm)/2);
   3671  1.17    mgorny #endif
   3672  1.17    mgorny 			break;
   3673  1.17    mgorny 		}
   3674  1.16    mgorny 
   3675  1.17    mgorny 		switch (regset) {
   3676  1.17    mgorny 		case TEST_GPREGS:
   3677  1.17    mgorny 			DPRINTF("Call SETREGS for the child process\n");
   3678  1.17    mgorny 			SYSCALL_REQUIRE(ptrace(PT_SETREGS, child, &gpr, 0)
   3679  1.17    mgorny 			    != -1);
   3680  1.17    mgorny 			break;
   3681  1.17    mgorny 		case TEST_XMMREGS:
   3682  1.17    mgorny #if defined(__i386__)
   3683  1.17    mgorny 			DPRINTF("Call SETXMMREGS for the child process\n");
   3684  1.17    mgorny 			SYSCALL_REQUIRE(ptrace(PT_SETXMMREGS, child, &xmm, 0)
   3685  1.17    mgorny 			    != -1);
   3686  1.17    mgorny 			break;
   3687  1.17    mgorny #else
   3688  1.17    mgorny 			/*FALLTHROUGH*/
   3689  1.16    mgorny #endif
   3690  1.17    mgorny 		case TEST_FPREGS:
   3691  1.17    mgorny 			DPRINTF("Call SETFPREGS for the child process\n");
   3692  1.17    mgorny 			SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &fpr, 0)
   3693  1.17    mgorny 			    != -1);
   3694  1.17    mgorny 			break;
   3695  1.17    mgorny 		case TEST_XSTATE:
   3696  1.17    mgorny 			DPRINTF("Call SETXSTATE for the child process\n");
   3697  1.17    mgorny 			SYSCALL_REQUIRE(ptrace(PT_SETXSTATE, child, &iov, 0)
   3698  1.17    mgorny 			    != -1);
   3699  1.17    mgorny 			break;
   3700  1.17    mgorny 		}
   3701  1.17    mgorny 		break;
   3702  1.17    mgorny 	}
   3703  1.16    mgorny 
   3704  1.27    mgorny #undef ST_EXP
   3705  1.26    mgorny #undef ST_MAN
   3706  1.16    mgorny 
   3707  1.16    mgorny 	DPRINTF("Before resuming the child process where it left off and "
   3708  1.16    mgorny 	    "without signal to be sent\n");
   3709  1.16    mgorny 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
   3710  1.16    mgorny 
   3711  1.16    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   3712  1.16    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
   3713  1.16    mgorny 
   3714  1.16    mgorny 	validate_status_exited(status, exitval);
   3715  1.16    mgorny 
   3716  1.16    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
   3717  1.16    mgorny 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
   3718  1.16    mgorny }
   3719  1.16    mgorny 
   3720  1.17    mgorny #define X86_REGISTER_TEST(test, regset, regs, regmode, descr)		\
   3721  1.17    mgorny ATF_TC(test);								\
   3722  1.17    mgorny ATF_TC_HEAD(test, tc)							\
   3723  1.17    mgorny {									\
   3724  1.17    mgorny 	atf_tc_set_md_var(tc, "descr", descr);				\
   3725  1.17    mgorny }									\
   3726  1.17    mgorny 									\
   3727  1.17    mgorny ATF_TC_BODY(test, tc)							\
   3728  1.17    mgorny {									\
   3729  1.17    mgorny 	x86_register_test(regset, regs, regmode);			\
   3730  1.17    mgorny }
   3731  1.17    mgorny 
   3732  1.17    mgorny X86_REGISTER_TEST(x86_gpregs32_read, TEST_GPREGS, GPREGS_32, TEST_GETREGS,
   3733  1.17    mgorny     "Test reading basic 32-bit gp registers from debugged program "
   3734  1.17    mgorny     "via PT_GETREGS.");
   3735  1.17    mgorny X86_REGISTER_TEST(x86_gpregs32_write, TEST_GPREGS, GPREGS_32, TEST_SETREGS,
   3736  1.17    mgorny     "Test writing basic 32-bit gp registers into debugged program "
   3737  1.17    mgorny     "via PT_SETREGS.");
   3738  1.18    mgorny X86_REGISTER_TEST(x86_gpregs32_core, TEST_GPREGS, GPREGS_32, TEST_COREDUMP,
   3739  1.18    mgorny     "Test reading basic 32-bit gp registers from core dump.");
   3740  1.17    mgorny X86_REGISTER_TEST(x86_gpregs32_ebp_esp_read, TEST_GPREGS, GPREGS_32_EBP_ESP,
   3741  1.17    mgorny     TEST_GETREGS, "Test reading ebp & esp registers from debugged program "
   3742  1.17    mgorny     "via PT_GETREGS.");
   3743  1.17    mgorny X86_REGISTER_TEST(x86_gpregs32_ebp_esp_write, TEST_GPREGS, GPREGS_32_EBP_ESP,
   3744  1.17    mgorny     TEST_SETREGS, "Test writing ebp & esp registers into debugged program "
   3745  1.17    mgorny     "via PT_SETREGS.");
   3746  1.18    mgorny X86_REGISTER_TEST(x86_gpregs32_ebp_esp_core, TEST_GPREGS, GPREGS_32_EBP_ESP,
   3747  1.18    mgorny     TEST_COREDUMP, "Test reading ebp & esp registers from core dump.");
   3748  1.17    mgorny 
   3749  1.17    mgorny X86_REGISTER_TEST(x86_gpregs64_read, TEST_GPREGS, GPREGS_64, TEST_GETREGS,
   3750  1.17    mgorny     "Test reading basic 64-bit gp registers from debugged program "
   3751  1.17    mgorny     "via PT_GETREGS.");
   3752  1.17    mgorny X86_REGISTER_TEST(x86_gpregs64_write, TEST_GPREGS, GPREGS_64, TEST_SETREGS,
   3753  1.17    mgorny     "Test writing basic 64-bit gp registers into debugged program "
   3754  1.17    mgorny     "via PT_SETREGS.");
   3755  1.18    mgorny X86_REGISTER_TEST(x86_gpregs64_core, TEST_GPREGS, GPREGS_64, TEST_COREDUMP,
   3756  1.18    mgorny     "Test reading basic 64-bit gp registers from core dump.");
   3757  1.17    mgorny X86_REGISTER_TEST(x86_gpregs64_r8_read, TEST_GPREGS, GPREGS_64_R8, TEST_GETREGS,
   3758  1.17    mgorny     "Test reading r8..r15 registers from debugged program via PT_GETREGS.");
   3759  1.17    mgorny X86_REGISTER_TEST(x86_gpregs64_r8_write, TEST_GPREGS, GPREGS_64_R8,
   3760  1.17    mgorny     TEST_SETREGS, "Test writing r8..r15 registers into debugged program "
   3761  1.17    mgorny     "via PT_SETREGS.");
   3762  1.18    mgorny X86_REGISTER_TEST(x86_gpregs64_r8_core, TEST_GPREGS, GPREGS_64_R8,
   3763  1.18    mgorny     TEST_COREDUMP, "Test reading r8..r15 registers from core dump.");
   3764  1.17    mgorny 
   3765  1.27    mgorny X86_REGISTER_TEST(x86_fpregs_fpu_read, TEST_FPREGS, FPREGS_FPU, TEST_GETREGS,
   3766  1.27    mgorny     "Test reading base FPU registers from debugged program via PT_GETFPREGS.");
   3767  1.27    mgorny X86_REGISTER_TEST(x86_fpregs_fpu_write, TEST_FPREGS, FPREGS_FPU, TEST_SETREGS,
   3768  1.27    mgorny     "Test writing base FPU registers into debugged program via PT_SETFPREGS.");
   3769  1.27    mgorny X86_REGISTER_TEST(x86_fpregs_fpu_core, TEST_FPREGS, FPREGS_FPU, TEST_COREDUMP,
   3770  1.27    mgorny     "Test reading base FPU registers from coredump.");
   3771  1.17    mgorny X86_REGISTER_TEST(x86_fpregs_mm_read, TEST_FPREGS, FPREGS_MM, TEST_GETREGS,
   3772  1.17    mgorny     "Test reading mm0..mm7 registers from debugged program "
   3773  1.17    mgorny     "via PT_GETFPREGS.");
   3774  1.17    mgorny X86_REGISTER_TEST(x86_fpregs_mm_write, TEST_FPREGS, FPREGS_MM, TEST_SETREGS,
   3775  1.17    mgorny     "Test writing mm0..mm7 registers into debugged program "
   3776  1.17    mgorny     "via PT_SETFPREGS.");
   3777  1.18    mgorny X86_REGISTER_TEST(x86_fpregs_mm_core, TEST_FPREGS, FPREGS_MM, TEST_COREDUMP,
   3778  1.18    mgorny     "Test reading mm0..mm7 registers from coredump.");
   3779  1.17    mgorny X86_REGISTER_TEST(x86_fpregs_xmm_read, TEST_XMMREGS, FPREGS_XMM, TEST_GETREGS,
   3780  1.17    mgorny     "Test reading xmm0..xmm15 (..xmm7 on i386) from debugged program "
   3781  1.17    mgorny     "via PT_GETFPREGS (PT_GETXMMREGS on i386).");
   3782  1.17    mgorny X86_REGISTER_TEST(x86_fpregs_xmm_write, TEST_XMMREGS, FPREGS_XMM, TEST_SETREGS,
   3783  1.17    mgorny     "Test writing xmm0..xmm15 (..xmm7 on i386) into debugged program "
   3784  1.17    mgorny     "via PT_SETFPREGS (PT_SETXMMREGS on i386).");
   3785  1.18    mgorny X86_REGISTER_TEST(x86_fpregs_xmm_core, TEST_XMMREGS, FPREGS_XMM, TEST_COREDUMP,
   3786  1.18    mgorny     "Test reading xmm0..xmm15 (..xmm7 on i386) from coredump.");
   3787  1.17    mgorny 
   3788  1.27    mgorny X86_REGISTER_TEST(x86_xstate_fpu_read, TEST_XSTATE, FPREGS_FPU, TEST_GETREGS,
   3789  1.27    mgorny     "Test reading base FPU registers from debugged program via PT_GETXSTATE.");
   3790  1.27    mgorny X86_REGISTER_TEST(x86_xstate_fpu_write, TEST_XSTATE, FPREGS_FPU, TEST_SETREGS,
   3791  1.27    mgorny     "Test writing base FPU registers into debugged program via PT_SETXSTATE.");
   3792  1.27    mgorny X86_REGISTER_TEST(x86_xstate_fpu_core, TEST_XSTATE, FPREGS_FPU, TEST_COREDUMP,
   3793  1.27    mgorny     "Test reading base FPU registers from core dump via XSTATE note.");
   3794  1.17    mgorny X86_REGISTER_TEST(x86_xstate_mm_read, TEST_XSTATE, FPREGS_MM, TEST_GETREGS,
   3795  1.17    mgorny     "Test reading mm0..mm7 registers from debugged program "
   3796  1.17    mgorny     "via PT_GETXSTATE.");
   3797  1.17    mgorny X86_REGISTER_TEST(x86_xstate_mm_write, TEST_XSTATE, FPREGS_MM, TEST_SETREGS,
   3798  1.17    mgorny     "Test writing mm0..mm7 registers into debugged program "
   3799  1.17    mgorny     "via PT_SETXSTATE.");
   3800  1.18    mgorny X86_REGISTER_TEST(x86_xstate_mm_core, TEST_XSTATE, FPREGS_MM, TEST_COREDUMP,
   3801  1.18    mgorny     "Test reading mm0..mm7 registers from core dump via XSTATE note.");
   3802  1.17    mgorny X86_REGISTER_TEST(x86_xstate_xmm_read, TEST_XSTATE, FPREGS_XMM, TEST_GETREGS,
   3803  1.17    mgorny     "Test reading xmm0..xmm15 (..xmm7 on i386) from debugged program "
   3804  1.17    mgorny     "via PT_GETXSTATE.");
   3805  1.17    mgorny X86_REGISTER_TEST(x86_xstate_xmm_write, TEST_XSTATE, FPREGS_XMM, TEST_SETREGS,
   3806  1.17    mgorny     "Test writing xmm0..xmm15 (..xmm7 on i386) into debugged program "
   3807  1.17    mgorny     "via PT_SETXSTATE.");
   3808  1.18    mgorny X86_REGISTER_TEST(x86_xstate_xmm_core, TEST_XSTATE, FPREGS_XMM, TEST_COREDUMP,
   3809  1.18    mgorny     "Test reading xmm0..xmm15 (..xmm7 on i386) from coredump via XSTATE note.");
   3810  1.17    mgorny X86_REGISTER_TEST(x86_xstate_ymm_read, TEST_XSTATE, FPREGS_YMM, TEST_GETREGS,
   3811  1.17    mgorny     "Test reading ymm0..ymm15 (..ymm7 on i386) from debugged program "
   3812  1.17    mgorny     "via PT_GETXSTATE.");
   3813  1.17    mgorny X86_REGISTER_TEST(x86_xstate_ymm_write, TEST_XSTATE, FPREGS_YMM, TEST_SETREGS,
   3814  1.17    mgorny     "Test writing ymm0..ymm15 (..ymm7 on i386) into debugged program "
   3815  1.17    mgorny     "via PT_SETXSTATE.");
   3816  1.18    mgorny X86_REGISTER_TEST(x86_xstate_ymm_core, TEST_XSTATE, FPREGS_YMM, TEST_COREDUMP,
   3817  1.18    mgorny     "Test reading ymm0..ymm15 (..ymm7 on i386) from coredump via XSTATE note.");
   3818  1.17    mgorny 
   3819   1.7     kamil /// ----------------------------------------------------------------------------
   3820   1.7     kamil 
   3821  1.20    mgorny #if defined(TWAIT_HAVE_STATUS)
   3822  1.20    mgorny 
   3823  1.20    mgorny static void
   3824  1.20    mgorny thread_concurrent_lwp_setup(pid_t child, lwpid_t lwpid)
   3825  1.20    mgorny {
   3826  1.20    mgorny 	struct dbreg r;
   3827  1.20    mgorny 	union u dr7;
   3828  1.20    mgorny 
   3829  1.20    mgorny 	/* We need to set debug registers for every child */
   3830  1.20    mgorny 	DPRINTF("Call GETDBREGS for LWP %d\n", lwpid);
   3831  1.20    mgorny 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r, lwpid) != -1);
   3832  1.20    mgorny 
   3833  1.20    mgorny 	dr7.raw = 0;
   3834  1.20    mgorny 	/* should be set to 1 according to Intel manual, 17.2 */
   3835  1.20    mgorny 	dr7.bits.reserved_10 = 1;
   3836  1.20    mgorny 	dr7.bits.local_exact_breakpt = 1;
   3837  1.20    mgorny 	dr7.bits.global_exact_breakpt = 1;
   3838  1.20    mgorny 	/* use DR0 for breakpoints */
   3839  1.20    mgorny 	dr7.bits.global_dr0_breakpoint = 1;
   3840  1.20    mgorny 	dr7.bits.condition_dr0 = 0; /* exec */
   3841  1.20    mgorny 	dr7.bits.len_dr0 = 0;
   3842  1.21    mgorny 	/* use DR1 for watchpoints */
   3843  1.21    mgorny 	dr7.bits.global_dr1_breakpoint = 1;
   3844  1.21    mgorny 	dr7.bits.condition_dr1 = 1; /* write */
   3845  1.21    mgorny 	dr7.bits.len_dr1 = 3; /* 4 bytes */
   3846  1.20    mgorny 	r.dr[7] = dr7.raw;
   3847  1.20    mgorny 	r.dr[0] = (long)(intptr_t)check_happy;
   3848  1.21    mgorny 	r.dr[1] = (long)(intptr_t)&thread_concurrent_watchpoint_var;
   3849  1.20    mgorny 	DPRINTF("dr0=%" PRIxREGISTER "\n", r.dr[0]);
   3850  1.21    mgorny 	DPRINTF("dr1=%" PRIxREGISTER "\n", r.dr[1]);
   3851  1.20    mgorny 	DPRINTF("dr7=%" PRIxREGISTER "\n", r.dr[7]);
   3852  1.20    mgorny 
   3853  1.20    mgorny 	DPRINTF("Call SETDBREGS for LWP %d\n", lwpid);
   3854  1.20    mgorny 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r, lwpid) != -1);
   3855  1.20    mgorny }
   3856  1.20    mgorny 
   3857  1.20    mgorny static enum thread_concurrent_sigtrap_event
   3858  1.20    mgorny thread_concurrent_handle_sigtrap(pid_t child, ptrace_siginfo_t *info)
   3859  1.20    mgorny {
   3860  1.20    mgorny 	enum thread_concurrent_sigtrap_event ret = TCSE_UNKNOWN;
   3861  1.20    mgorny 	struct dbreg r;
   3862  1.20    mgorny 	union u dr7;
   3863  1.20    mgorny 
   3864  1.20    mgorny 	ATF_CHECK_EQ_MSG(info->psi_siginfo.si_code, TRAP_DBREG,
   3865  1.20    mgorny 	    "lwp=%d, expected TRAP_DBREG (%d), got %d", info->psi_lwpid,
   3866  1.20    mgorny 	    TRAP_DBREG, info->psi_siginfo.si_code);
   3867  1.20    mgorny 
   3868  1.20    mgorny 	DPRINTF("Call GETDBREGS for LWP %d\n", info->psi_lwpid);
   3869  1.20    mgorny 	SYSCALL_REQUIRE(ptrace(PT_GETDBREGS, child, &r, info->psi_lwpid) != -1);
   3870  1.20    mgorny 	DPRINTF("dr6=%" PRIxREGISTER ", dr7=%" PRIxREGISTER "\n",
   3871  1.20    mgorny 	    r.dr[6], r.dr[7]);
   3872  1.20    mgorny 
   3873  1.21    mgorny 	ATF_CHECK_MSG(r.dr[6] & 3, "lwp=%d, got DR6=%" PRIxREGISTER,
   3874  1.20    mgorny 	    info->psi_lwpid, r.dr[6]);
   3875  1.20    mgorny 
   3876  1.21    mgorny 	/* Handle only one event at a time, we should get
   3877  1.21    mgorny 	 * a separate SIGTRAP for the other one.
   3878  1.21    mgorny 	 */
   3879  1.20    mgorny 	if (r.dr[6] & 1) {
   3880  1.20    mgorny 		r.dr[6] &= ~1;
   3881  1.20    mgorny 
   3882  1.20    mgorny 		/* We need to disable the breakpoint to move
   3883  1.20    mgorny 		 * past it.
   3884  1.20    mgorny 		 *
   3885  1.20    mgorny 		 * TODO: single-step and reenable it?
   3886  1.20    mgorny 		 */
   3887  1.20    mgorny 		dr7.raw = r.dr[7];
   3888  1.20    mgorny 		dr7.bits.global_dr0_breakpoint = 0;
   3889  1.20    mgorny 		r.dr[7] = dr7.raw;
   3890  1.20    mgorny 
   3891  1.20    mgorny 		ret = TCSE_BREAKPOINT;
   3892  1.21    mgorny 	} else if (r.dr[6] & 2) {
   3893  1.21    mgorny 		r.dr[6] &= ~2;
   3894  1.21    mgorny 		ret = TCSE_WATCHPOINT;
   3895  1.20    mgorny 	}
   3896  1.20    mgorny 
   3897  1.20    mgorny 	DPRINTF("Call SETDBREGS for LWP %d\n", info->psi_lwpid);
   3898  1.20    mgorny 	DPRINTF("dr6=%" PRIxREGISTER ", dr7=%" PRIxREGISTER "\n",
   3899  1.20    mgorny 		r.dr[6], r.dr[7]);
   3900  1.20    mgorny 	SYSCALL_REQUIRE(ptrace(PT_SETDBREGS, child, &r, info->psi_lwpid) != -1);
   3901  1.20    mgorny 
   3902  1.20    mgorny 	return ret;
   3903  1.20    mgorny }
   3904  1.20    mgorny 
   3905  1.20    mgorny #endif /*defined(TWAIT_HAVE_STATUS)*/
   3906  1.20    mgorny 
   3907  1.20    mgorny /// ----------------------------------------------------------------------------
   3908  1.20    mgorny 
   3909   1.1     kamil #define ATF_TP_ADD_TCS_PTRACE_WAIT_X86() \
   3910   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_print); \
   3911   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr0); \
   3912   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr1); \
   3913   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr2); \
   3914   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr3); \
   3915   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr0_yield); \
   3916   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr1_yield); \
   3917   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr2_yield); \
   3918   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr3_yield); \
   3919   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr0_continued); \
   3920   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr1_continued); \
   3921   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr2_continued); \
   3922   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_preserve_dr3_continued); \
   3923   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_writeonly_byte); \
   3924   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_writeonly_byte); \
   3925   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_writeonly_byte); \
   3926   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_writeonly_byte); \
   3927   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_writeonly_2bytes); \
   3928   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_writeonly_2bytes); \
   3929   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_writeonly_2bytes); \
   3930   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_writeonly_2bytes); \
   3931   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_writeonly_4bytes); \
   3932   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_writeonly_4bytes); \
   3933   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_writeonly_4bytes); \
   3934   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_writeonly_4bytes); \
   3935   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_write_byte); \
   3936   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_write_byte); \
   3937   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_write_byte); \
   3938   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_write_byte); \
   3939   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_write_2bytes); \
   3940   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_write_2bytes); \
   3941   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_write_2bytes); \
   3942   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_write_2bytes); \
   3943   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_write_4bytes); \
   3944   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_write_4bytes); \
   3945   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_write_4bytes); \
   3946   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_write_4bytes); \
   3947   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_read_byte); \
   3948   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_read_byte); \
   3949   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_read_byte); \
   3950   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_read_byte); \
   3951   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_read_2bytes); \
   3952   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_read_2bytes); \
   3953   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_read_2bytes); \
   3954   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_read_2bytes); \
   3955   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_variable_readwrite_read_4bytes); \
   3956   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_variable_readwrite_read_4bytes); \
   3957   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_variable_readwrite_read_4bytes); \
   3958   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_variable_readwrite_read_4bytes); \
   3959   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_trap_code); \
   3960   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_trap_code); \
   3961   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_trap_code); \
   3962   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_trap_code); \
   3963   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_dont_inherit_lwp); \
   3964   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_dont_inherit_lwp); \
   3965   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_dont_inherit_lwp); \
   3966   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_dont_inherit_lwp); \
   3967   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr0_dont_inherit_execve); \
   3968   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr1_dont_inherit_execve); \
   3969   1.1     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr2_dont_inherit_execve); \
   3970   1.7     kamil 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, dbregs_dr3_dont_inherit_execve); \
   3971  1.10    mgorny 	ATF_TP_ADD_TC_HAVE_DBREGS(tp, x86_cve_2018_8897); \
   3972  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_read); \
   3973  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_write); \
   3974  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_core); \
   3975  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_ebp_esp_read); \
   3976  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_ebp_esp_write); \
   3977  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs32_ebp_esp_core); \
   3978  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_read); \
   3979  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_write); \
   3980  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_core); \
   3981  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_r8_read); \
   3982  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_r8_write); \
   3983  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_gpregs64_r8_core); \
   3984  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_fpu_read); \
   3985  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_fpu_write); \
   3986  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_fpu_core); \
   3987  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_mm_read); \
   3988  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_mm_write); \
   3989  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_mm_core); \
   3990  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_xmm_read); \
   3991  1.17    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_xmm_write); \
   3992  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_fpregs_xmm_core); \
   3993  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_fpu_read); \
   3994  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_fpu_write); \
   3995  1.27    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_fpu_core); \
   3996  1.16    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_mm_read); \
   3997  1.16    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_mm_write); \
   3998  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_mm_core); \
   3999  1.16    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_xmm_read); \
   4000  1.16    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_xmm_write); \
   4001  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_xmm_core); \
   4002  1.16    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_ymm_read); \
   4003  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_ymm_write); \
   4004  1.18    mgorny 	ATF_TP_ADD_TC(tp, x86_xstate_ymm_core);
   4005   1.1     kamil #else
   4006   1.1     kamil #define ATF_TP_ADD_TCS_PTRACE_WAIT_X86()
   4007   1.1     kamil #endif
   4008