Home | History | Annotate | Line # | Download | only in sys
      1  1.14  riastrad /* $NetBSD: t_futex_ops.c,v 1.14 2025/03/05 12:02:00 riastradh Exp $ */
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2019, 2020 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  *
     16   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1   thorpej  */
     28   1.1   thorpej 
     29   1.1   thorpej #include <sys/cdefs.h>
     30   1.1   thorpej __COPYRIGHT("@(#) Copyright (c) 2019, 2020\
     31   1.1   thorpej  The NetBSD Foundation, inc. All rights reserved.");
     32  1.14  riastrad __RCSID("$NetBSD: t_futex_ops.c,v 1.14 2025/03/05 12:02:00 riastradh Exp $");
     33   1.1   thorpej 
     34   1.1   thorpej #include <sys/fcntl.h>
     35   1.1   thorpej #include <sys/mman.h>
     36   1.1   thorpej #include <sys/wait.h>
     37   1.1   thorpej #include <atomic.h>
     38   1.1   thorpej #include <errno.h>
     39   1.1   thorpej #include <lwp.h>
     40   1.1   thorpej #include <stdlib.h>
     41   1.1   thorpej #include <stdio.h>
     42   1.3   thorpej #include <signal.h>
     43   1.1   thorpej #include <time.h>
     44   1.1   thorpej #include <limits.h>
     45   1.4   thorpej #include <sched.h>
     46   1.1   thorpej #include <unistd.h>
     47   1.1   thorpej 
     48   1.1   thorpej #include <atf-c.h>
     49   1.1   thorpej 
     50   1.1   thorpej #include <libc/include/futex_private.h>
     51   1.1   thorpej 
     52  1.11  riastrad #include "h_macros.h"
     53  1.11  riastrad 
     54   1.1   thorpej #define	LOAD(x)		(*(volatile int *)(x))
     55   1.1   thorpej #define	STORE(x, y)	*(volatile int *)(x) = (y)
     56   1.1   thorpej 
     57   1.1   thorpej #if 0
     58   1.1   thorpej #define	DPRINTF(x)	printf x
     59   1.1   thorpej #else
     60   1.1   thorpej #define	DPRINTF(x)	__nothing
     61   1.1   thorpej #endif
     62   1.1   thorpej 
     63   1.1   thorpej #define	STACK_SIZE	65536
     64   1.1   thorpej 
     65   1.1   thorpej static volatile int futex_word;
     66   1.1   thorpej static volatile int futex_word1;
     67   1.1   thorpej 
     68   1.1   thorpej static volatile unsigned int nlwps_running;
     69   1.1   thorpej 
     70   1.1   thorpej struct lwp_data {
     71   1.1   thorpej 	ucontext_t	context;
     72   1.1   thorpej 	void		(*func)(void *);
     73   1.1   thorpej 	void		*stack_base;
     74   1.1   thorpej 	lwpid_t		lwpid;
     75   1.1   thorpej 	pid_t		child;
     76   1.1   thorpej 	lwpid_t		threadid;
     77   1.1   thorpej 	int		wait_op;
     78   1.1   thorpej 	int		op_flags;
     79   1.1   thorpej 	int		bitset;
     80   1.1   thorpej 	volatile int	*futex_ptr;
     81   1.1   thorpej 	volatile int	*error_ptr;
     82   1.1   thorpej 	int		block_val;
     83   1.1   thorpej 
     84   1.1   thorpej 	void		(*exit_func)(void);
     85   1.1   thorpej 
     86   1.1   thorpej 	int		futex_error;
     87   1.1   thorpej };
     88   1.1   thorpej 
     89   1.1   thorpej #define	WAITER_LWP0		0
     90   1.1   thorpej #define	WAITER_LWP1		1
     91   1.1   thorpej #define	WAITER_LWP2		2
     92   1.1   thorpej #define	WAITER_LWP3		3
     93   1.1   thorpej #define	WAITER_LWP4		4
     94   1.1   thorpej #define	WAITER_LWP5		5
     95   1.1   thorpej #define	NLWPS			6
     96   1.1   thorpej 
     97   1.1   thorpej struct lwp_data lwp_data[NLWPS];
     98   1.1   thorpej 
     99   1.1   thorpej static const char *bs_path = "t_futex_ops_backing_store";
    100   1.1   thorpej static int bs_fd = -1;
    101   1.1   thorpej static int *bs_addr = MAP_FAILED;
    102   1.1   thorpej static void *bs_source_buffer = NULL;
    103   1.1   thorpej static void *bs_verify_buffer = NULL;
    104   1.1   thorpej static long bs_pagesize;
    105   1.1   thorpej 
    106   1.1   thorpej static void
    107   1.1   thorpej create_lwp_waiter(struct lwp_data *d)
    108   1.1   thorpej {
    109  1.11  riastrad 	RL(_lwp_create(&d->context, 0, &d->lwpid));
    110   1.1   thorpej }
    111   1.1   thorpej 
    112   1.1   thorpej static void
    113   1.1   thorpej exit_lwp_waiter(void)
    114   1.1   thorpej {
    115   1.1   thorpej 	_lwp_exit();
    116   1.1   thorpej }
    117   1.1   thorpej 
    118   1.1   thorpej static void
    119   1.1   thorpej reap_lwp_waiter(struct lwp_data *d)
    120   1.1   thorpej {
    121  1.11  riastrad 	RL(_lwp_wait(d->lwpid, NULL));
    122   1.1   thorpej }
    123   1.1   thorpej 
    124   1.1   thorpej static void
    125   1.1   thorpej create_proc_waiter(struct lwp_data *d)
    126   1.1   thorpej {
    127   1.1   thorpej 	pid_t pid;
    128   1.1   thorpej 
    129  1.11  riastrad 	RL(pid = fork());
    130   1.1   thorpej 	if (pid == 0) {
    131   1.1   thorpej 		(*d->func)(d);
    132   1.1   thorpej 		_exit(666);		/* backstop */
    133   1.1   thorpej 	} else
    134   1.1   thorpej 		d->child = pid;
    135   1.1   thorpej }
    136   1.1   thorpej 
    137   1.1   thorpej static void
    138   1.1   thorpej exit_proc_waiter(void)
    139   1.1   thorpej {
    140   1.1   thorpej 	_exit(0);
    141   1.1   thorpej }
    142   1.1   thorpej 
    143   1.1   thorpej static void
    144   1.1   thorpej reap_proc_waiter(struct lwp_data *d)
    145   1.1   thorpej {
    146  1.11  riastrad 	pid_t pid;
    147   1.1   thorpej 	int status;
    148   1.1   thorpej 
    149  1.11  riastrad 	RL(pid = waitpid(d->child, &status, 0));
    150  1.12  riastrad 	ATF_CHECK_EQ_MSG(pid, d->child,
    151  1.11  riastrad 	    "pid=%lld d->child=%lld", (long long)pid, (long long)d->child);
    152  1.12  riastrad 	ATF_CHECK_MSG(WIFEXITED(status), "status=0x%x", status);
    153  1.12  riastrad 	ATF_CHECK_EQ_MSG(WEXITSTATUS(status), 0, "status=0x%x", status);
    154   1.1   thorpej }
    155   1.1   thorpej 
    156   1.1   thorpej static void
    157   1.1   thorpej setup_lwp_context(struct lwp_data *d, void (*func)(void *))
    158   1.1   thorpej {
    159   1.1   thorpej 
    160   1.1   thorpej 	memset(d, 0, sizeof(*d));
    161  1.11  riastrad 	REQUIRE_LIBC(d->stack_base = mmap(NULL, STACK_SIZE,
    162  1.11  riastrad 		PROT_READ | PROT_WRITE, MAP_ANON | MAP_STACK | MAP_PRIVATE,
    163  1.11  riastrad 		-1, 0),
    164  1.11  riastrad 	    MAP_FAILED);
    165  1.11  riastrad 	_lwp_makecontext(&d->context, func, d, NULL, d->stack_base,
    166  1.11  riastrad 	    STACK_SIZE);
    167   1.1   thorpej 	d->threadid = 0;
    168   1.1   thorpej 	d->func = func;
    169   1.1   thorpej }
    170   1.1   thorpej 
    171   1.1   thorpej static void
    172   1.1   thorpej simple_test_waiter_lwp(void *arg)
    173   1.1   thorpej {
    174   1.1   thorpej 	struct lwp_data *d = arg;
    175   1.1   thorpej 
    176   1.1   thorpej 	d->threadid = _lwp_self();
    177   1.1   thorpej 
    178   1.3   thorpej 	membar_producer();
    179   1.1   thorpej 	atomic_inc_uint(&nlwps_running);
    180   1.1   thorpej 	membar_sync();
    181   1.1   thorpej 
    182   1.1   thorpej 	if (__futex(d->futex_ptr, d->wait_op | d->op_flags,
    183  1.11  riastrad 		d->block_val, NULL, NULL, 0, d->bitset) == -1) {
    184   1.1   thorpej 		d->futex_error = errno;
    185   1.3   thorpej 		membar_sync();
    186   1.3   thorpej 		atomic_dec_uint(&nlwps_running);
    187   1.1   thorpej 		_lwp_exit();
    188   1.1   thorpej 	} else {
    189   1.1   thorpej 		d->futex_error = 0;
    190   1.1   thorpej 	}
    191   1.1   thorpej 
    192   1.1   thorpej 	membar_sync();
    193   1.1   thorpej 	atomic_dec_uint(&nlwps_running);
    194   1.1   thorpej 
    195   1.1   thorpej 	_lwp_exit();
    196   1.1   thorpej }
    197   1.1   thorpej 
    198   1.1   thorpej static bool
    199   1.1   thorpej verify_zero_bs(void)
    200   1.1   thorpej {
    201  1.11  riastrad 	ssize_t nread;
    202   1.1   thorpej 
    203   1.1   thorpej 	if (bs_verify_buffer == NULL) {
    204  1.11  riastrad 		REQUIRE_LIBC(bs_verify_buffer = malloc(bs_pagesize), NULL);
    205   1.1   thorpej 	}
    206   1.1   thorpej 
    207  1.11  riastrad 	RL(nread = pread(bs_fd, bs_verify_buffer, bs_pagesize, 0));
    208  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nread, bs_pagesize, "nread=%zu bs_pagesize=%lu",
    209  1.11  riastrad 	    nread, bs_pagesize);
    210   1.1   thorpej 
    211   1.1   thorpej 	return (memcmp(bs_verify_buffer, bs_source_buffer, bs_pagesize) == 0);
    212   1.1   thorpej }
    213   1.1   thorpej 
    214   1.1   thorpej static void
    215   1.1   thorpej create_bs(int map_flags)
    216   1.1   thorpej {
    217  1.11  riastrad 	ssize_t nwrit;
    218   1.1   thorpej 
    219   1.1   thorpej 	bs_pagesize = sysconf(_SC_PAGESIZE);
    220  1.11  riastrad 	ATF_REQUIRE_MSG(bs_pagesize > 0, "bs_pagesize=%ld", bs_pagesize);
    221   1.1   thorpej 
    222   1.1   thorpej 	if ((map_flags & (MAP_FILE | MAP_ANON)) == MAP_FILE) {
    223  1.11  riastrad 		REQUIRE_LIBC(bs_source_buffer = calloc(1, bs_pagesize), NULL);
    224   1.1   thorpej 
    225  1.11  riastrad 		RL(bs_fd = open(bs_path, O_RDWR | O_CREAT | O_EXCL, 0644));
    226  1.11  riastrad 		RL(nwrit = pwrite(bs_fd, bs_source_buffer, bs_pagesize, 0));
    227  1.11  riastrad 		ATF_REQUIRE_EQ_MSG(nwrit, bs_pagesize,
    228  1.11  riastrad 		    "nwrit=%zu bs_pagesize=%lu", nwrit, bs_pagesize);
    229   1.1   thorpej 		ATF_REQUIRE(verify_zero_bs());
    230   1.1   thorpej 	}
    231   1.1   thorpej 
    232  1.11  riastrad 	REQUIRE_LIBC(bs_addr = mmap(NULL, bs_pagesize, PROT_READ | PROT_WRITE,
    233  1.11  riastrad 		map_flags | MAP_HASSEMAPHORE, bs_fd, 0),
    234  1.11  riastrad 	    MAP_FAILED);
    235   1.1   thorpej }
    236   1.1   thorpej 
    237   1.1   thorpej static void
    238   1.1   thorpej cleanup_bs(void)
    239   1.1   thorpej {
    240   1.1   thorpej 
    241   1.1   thorpej 	if (bs_fd != -1) {
    242   1.1   thorpej 		(void) close(bs_fd);
    243   1.1   thorpej 		bs_fd = -1;
    244   1.1   thorpej 		(void) unlink(bs_path);
    245   1.1   thorpej 	}
    246   1.1   thorpej 	if (bs_source_buffer != NULL) {
    247   1.1   thorpej 		free(bs_source_buffer);
    248   1.1   thorpej 		bs_source_buffer = NULL;
    249   1.1   thorpej 	}
    250   1.1   thorpej 	if (bs_verify_buffer != NULL) {
    251   1.1   thorpej 		free(bs_verify_buffer);
    252   1.1   thorpej 		bs_verify_buffer = NULL;
    253   1.1   thorpej 	}
    254   1.1   thorpej 	if (bs_addr != MAP_FAILED) {
    255   1.1   thorpej 		munmap(bs_addr, bs_pagesize);
    256   1.1   thorpej 		bs_addr = MAP_FAILED;
    257   1.1   thorpej 	}
    258   1.1   thorpej }
    259   1.1   thorpej 
    260   1.1   thorpej static void
    261   1.1   thorpej do_cleanup(void)
    262   1.1   thorpej {
    263   1.1   thorpej 	int i;
    264   1.1   thorpej 
    265   1.1   thorpej 	for (i = 0; i < NLWPS; i++) {
    266   1.1   thorpej 		struct lwp_data *d = &lwp_data[i];
    267   1.1   thorpej 		if (d->stack_base != NULL && d->stack_base != MAP_FAILED) {
    268   1.1   thorpej 			(void) munmap(d->stack_base, STACK_SIZE);
    269   1.1   thorpej 		}
    270   1.1   thorpej 	}
    271   1.1   thorpej 	memset(lwp_data, 0, sizeof(lwp_data));
    272   1.1   thorpej 	STORE(&futex_word, 0);
    273   1.1   thorpej 	STORE(&futex_word1, 0);
    274   1.1   thorpej 	nlwps_running = 0;
    275   1.1   thorpej 
    276   1.1   thorpej 	cleanup_bs();
    277   1.1   thorpej }
    278   1.1   thorpej 
    279   1.1   thorpej /*****************************************************************************/
    280   1.1   thorpej 
    281   1.1   thorpej static void
    282   1.1   thorpej wait_wake_test_waiter_lwp(void *arg)
    283   1.1   thorpej {
    284   1.1   thorpej 	struct lwp_data *d = arg;
    285   1.1   thorpej 
    286   1.1   thorpej 	d->threadid = _lwp_self();
    287   1.1   thorpej 
    288   1.1   thorpej 	STORE(d->futex_ptr, 1);
    289   1.1   thorpej 	membar_sync();
    290   1.1   thorpej 
    291   1.1   thorpej 	/* This will block because *futex_ptr == 1. */
    292   1.1   thorpej 	if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
    293  1.11  riastrad 		1, NULL, NULL, 0, 0) == -1) {
    294   1.1   thorpej 		STORE(d->error_ptr, errno);
    295   1.1   thorpej 		(*d->exit_func)();
    296   1.1   thorpej 	} else {
    297   1.1   thorpej 		STORE(d->error_ptr, 0);
    298   1.1   thorpej 	}
    299   1.1   thorpej 
    300   1.1   thorpej 	do {
    301   1.1   thorpej 		membar_sync();
    302   1.1   thorpej 		sleep(1);
    303   1.1   thorpej 	} while (LOAD(d->futex_ptr) != 0);
    304   1.1   thorpej 
    305   1.1   thorpej 	STORE(d->futex_ptr, 2);
    306   1.1   thorpej 	membar_sync();
    307   1.1   thorpej 
    308   1.1   thorpej 	do {
    309   1.1   thorpej 		membar_sync();
    310   1.1   thorpej 		sleep(1);
    311   1.1   thorpej 	} while (LOAD(d->futex_ptr) != 3);
    312   1.1   thorpej 
    313   1.1   thorpej 	/* This will not block because futex_word != 666. */
    314   1.1   thorpej 	if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
    315  1.11  riastrad 		666, NULL, NULL, 0, 0) == -1) {
    316   1.1   thorpej 		/* This SHOULD be EAGAIN. */
    317   1.1   thorpej 		STORE(d->error_ptr, errno);
    318   1.1   thorpej 	}
    319   1.1   thorpej 
    320   1.1   thorpej 	STORE(d->futex_ptr, 4);
    321   1.1   thorpej 	membar_sync();
    322   1.1   thorpej 
    323   1.1   thorpej 	(*d->exit_func)();
    324   1.1   thorpej }
    325   1.1   thorpej 
    326   1.1   thorpej static void
    327   1.1   thorpej do_futex_wait_wake_test(volatile int *futex_ptr, volatile int *error_ptr,
    328  1.11  riastrad     void (*create_func)(struct lwp_data *),
    329  1.11  riastrad     void (*exit_func)(void),
    330  1.11  riastrad     void (*reap_func)(struct lwp_data *),
    331  1.11  riastrad     int flags)
    332   1.1   thorpej {
    333   1.1   thorpej 	struct lwp_data *wlwp = &lwp_data[WAITER_LWP0];
    334   1.1   thorpej 	int tries;
    335  1.11  riastrad 	int n;
    336   1.1   thorpej 
    337   1.1   thorpej 	if (error_ptr == NULL)
    338   1.1   thorpej 		error_ptr = &wlwp->futex_error;
    339   1.1   thorpej 
    340   1.1   thorpej 	if (create_func == NULL)
    341   1.1   thorpej 		create_func = create_lwp_waiter;
    342   1.1   thorpej 	if (exit_func == NULL)
    343   1.1   thorpej 		exit_func = exit_lwp_waiter;
    344   1.1   thorpej 	if (reap_func == NULL)
    345   1.1   thorpej 		reap_func = reap_lwp_waiter;
    346   1.1   thorpej 
    347   1.1   thorpej 	setup_lwp_context(wlwp, wait_wake_test_waiter_lwp);
    348   1.1   thorpej 
    349   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: testing with flags 0x%x\n", flags));
    350   1.1   thorpej 	wlwp->op_flags = flags;
    351   1.1   thorpej 	wlwp->error_ptr = error_ptr;
    352   1.1   thorpej 	STORE(error_ptr, -1);
    353   1.1   thorpej 	wlwp->futex_ptr = futex_ptr;
    354   1.1   thorpej 	STORE(futex_ptr, 0);
    355   1.1   thorpej 	wlwp->exit_func = exit_func;
    356   1.1   thorpej 	membar_sync();
    357   1.1   thorpej 
    358   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: creating watier LWP\n"));
    359   1.1   thorpej 	(*create_func)(wlwp);
    360   1.1   thorpej 
    361   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP %d to enter futex\n",
    362   1.1   thorpej 	    wlwp->lwpid));
    363   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    364   1.1   thorpej 		membar_sync();
    365   1.1   thorpej 		if (LOAD(futex_ptr) == 1)
    366   1.1   thorpej 			break;
    367   1.1   thorpej 		sleep(1);
    368   1.1   thorpej 	}
    369   1.1   thorpej 	membar_sync();
    370  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(futex_ptr)), 1, "LOAD(futex_ptr)=%d", n);
    371   1.1   thorpej 
    372   1.1   thorpej 	/*
    373   1.1   thorpej 	 * If the LWP is blocked in the futex, it will not have yet
    374   1.1   thorpej 	 * modified *error_ptr.
    375   1.1   thorpej 	 */
    376   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for successful wait (%d)\n",
    377   1.1   thorpej 	    LOAD(error_ptr)));
    378   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    379   1.1   thorpej 		membar_sync();
    380   1.1   thorpej 		if (LOAD(error_ptr) == -1)
    381   1.1   thorpej 			break;
    382   1.1   thorpej 		sleep(1);
    383   1.1   thorpej 	}
    384   1.1   thorpej 	membar_sync();
    385  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(error_ptr)), -1, "error=%d", n);
    386   1.1   thorpej 
    387   1.1   thorpej 	/* Make sure invalid #wakes in rejected. */
    388   1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    389   1.1   thorpej 	    __futex(futex_ptr, FUTEX_WAKE | flags,
    390  1.11  riastrad 		-1, NULL, NULL, 0, 0) == -1);
    391   1.1   thorpej 
    392   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waking 1 waiter\n"));
    393  1.11  riastrad 	RL(n = __futex(futex_ptr, FUTEX_WAKE | flags, 1, NULL, NULL, 0, 0));
    394  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 1, "n=%d wakeups", n);
    395   1.1   thorpej 
    396   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for successful wake (%d)\n",
    397   1.1   thorpej 	    LOAD(error_ptr)));
    398   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    399   1.1   thorpej 		membar_sync();
    400   1.1   thorpej 		if (LOAD(error_ptr) == 0)
    401   1.1   thorpej 			break;
    402   1.1   thorpej 		sleep(1);
    403   1.1   thorpej 	}
    404   1.1   thorpej 	membar_sync();
    405  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(error_ptr)), 0, "error=%d", n);
    406   1.1   thorpej 
    407   1.1   thorpej 	STORE(futex_ptr, 0);
    408   1.1   thorpej 	membar_sync();
    409   1.1   thorpej 
    410   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (2)\n"));
    411   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    412   1.1   thorpej 		membar_sync();
    413   1.1   thorpej 		if (LOAD(futex_ptr) == 2)
    414   1.1   thorpej 			break;
    415   1.1   thorpej 		sleep(1);
    416   1.1   thorpej 	}
    417   1.1   thorpej 	membar_sync();
    418  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(futex_ptr)), 2, "LOAD(futex_ptr)=%d", n);
    419   1.1   thorpej 
    420   1.1   thorpej 	STORE(futex_ptr, 3);
    421   1.1   thorpej 	membar_sync();
    422   1.1   thorpej 
    423   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (4)\n"));
    424   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    425   1.1   thorpej 		membar_sync();
    426   1.1   thorpej 		if (LOAD(futex_ptr) == 4)
    427   1.1   thorpej 			break;
    428   1.1   thorpej 		sleep(1);
    429   1.1   thorpej 	}
    430   1.1   thorpej 	membar_sync();
    431  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(futex_ptr)), 4, "error=%d", n);
    432   1.1   thorpej 
    433   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for expected EGAIN\n"));
    434  1.11  riastrad 	ATF_REQUIRE_EQ_MSG((n = LOAD(error_ptr)), EAGAIN, "error=%d", n);
    435   1.1   thorpej 
    436   1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: reaping LWP %d\n", wlwp->lwpid));
    437   1.1   thorpej 	(*reap_func)(wlwp);
    438   1.1   thorpej }
    439   1.1   thorpej 
    440   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_private);
    441   1.1   thorpej ATF_TC_HEAD(futex_basic_wait_wake_private, tc)
    442   1.1   thorpej {
    443   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    444   1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (PRIVATE)");
    445   1.1   thorpej }
    446   1.1   thorpej ATF_TC_BODY(futex_basic_wait_wake_private, tc)
    447   1.1   thorpej {
    448   1.1   thorpej 	do_futex_wait_wake_test(&futex_word, NULL,
    449  1.11  riastrad 	    NULL, NULL, NULL,
    450  1.11  riastrad 	    FUTEX_PRIVATE_FLAG);
    451   1.1   thorpej }
    452   1.1   thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_private, tc)
    453   1.1   thorpej {
    454   1.1   thorpej 	do_cleanup();
    455   1.1   thorpej }
    456   1.1   thorpej 
    457   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_shared);
    458   1.1   thorpej ATF_TC_HEAD(futex_basic_wait_wake_shared, tc)
    459   1.1   thorpej {
    460   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    461   1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (SHARED)");
    462   1.1   thorpej }
    463   1.1   thorpej ATF_TC_BODY(futex_basic_wait_wake_shared, tc)
    464   1.1   thorpej {
    465   1.1   thorpej 	do_futex_wait_wake_test(&futex_word, NULL,
    466  1.11  riastrad 	    NULL, NULL, NULL,
    467  1.11  riastrad 	    0);
    468   1.1   thorpej }
    469   1.1   thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_shared, tc)
    470   1.1   thorpej {
    471   1.1   thorpej 	do_cleanup();
    472   1.1   thorpej }
    473   1.1   thorpej 
    474   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_private);
    475   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_private, tc)
    476   1.1   thorpej {
    477   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    478   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_ANON + PRIVATE)");
    479   1.1   thorpej }
    480   1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_private, tc)
    481   1.1   thorpej {
    482   1.1   thorpej 	create_bs(MAP_ANON | MAP_PRIVATE);
    483   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    484  1.11  riastrad 	    NULL, NULL, NULL,
    485  1.11  riastrad 	    FUTEX_PRIVATE_FLAG);
    486   1.1   thorpej }
    487   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_private, tc)
    488   1.1   thorpej {
    489   1.1   thorpej 	do_cleanup();
    490   1.1   thorpej }
    491   1.1   thorpej 
    492   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared);
    493   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared, tc)
    494   1.1   thorpej {
    495   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    496   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    497   1.1   thorpej }
    498   1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared, tc)
    499   1.1   thorpej {
    500   1.1   thorpej 	create_bs(MAP_ANON | MAP_PRIVATE);
    501   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    502  1.11  riastrad 	    NULL, NULL, NULL,
    503  1.11  riastrad 	    0);
    504   1.1   thorpej }
    505   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared, tc)
    506   1.1   thorpej {
    507   1.1   thorpej 	do_cleanup();
    508   1.1   thorpej }
    509   1.1   thorpej 
    510   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_private);
    511   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_private, tc)
    512   1.1   thorpej {
    513   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    514   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE + PRIVATE)");
    515   1.1   thorpej }
    516   1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_private, tc)
    517   1.1   thorpej {
    518   1.1   thorpej 	/*
    519   1.1   thorpej 	 * This combination (non-COW mapped file + PRIVATE futex)
    520   1.1   thorpej 	 * doesn't really make sense, but we should make sure it
    521   1.1   thorpej 	 * works as expected.
    522   1.1   thorpej 	 */
    523   1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    524   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    525  1.11  riastrad 	    NULL, NULL, NULL,
    526  1.11  riastrad 	    FUTEX_PRIVATE_FLAG);
    527  1.11  riastrad 	ATF_REQUIRE(!verify_zero_bs());
    528   1.1   thorpej }
    529   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_private, tc)
    530   1.1   thorpej {
    531   1.1   thorpej 	do_cleanup();
    532   1.1   thorpej }
    533   1.1   thorpej 
    534   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_private);
    535   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_private, tc)
    536   1.1   thorpej {
    537   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    538   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE COW + PRIVATE)");
    539   1.1   thorpej }
    540   1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_private, tc)
    541   1.1   thorpej {
    542   1.1   thorpej 	create_bs(MAP_FILE | MAP_PRIVATE);
    543   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    544  1.11  riastrad 	    NULL, NULL, NULL,
    545  1.11  riastrad 	    FUTEX_PRIVATE_FLAG);
    546   1.1   thorpej 	ATF_REQUIRE(verify_zero_bs());
    547   1.1   thorpej }
    548   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_private, tc)
    549   1.1   thorpej {
    550   1.1   thorpej 	do_cleanup();
    551   1.1   thorpej }
    552   1.1   thorpej 
    553   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared);
    554   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared, tc)
    555   1.1   thorpej {
    556   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    557   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE + SHARED)");
    558   1.1   thorpej }
    559   1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared, tc)
    560   1.1   thorpej {
    561   1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    562   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    563  1.11  riastrad 	    NULL, NULL, NULL,
    564  1.11  riastrad 	    0);
    565  1.11  riastrad 	ATF_REQUIRE(!verify_zero_bs());
    566   1.1   thorpej }
    567   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared, tc)
    568   1.1   thorpej {
    569   1.1   thorpej 	do_cleanup();
    570   1.1   thorpej }
    571   1.1   thorpej 
    572   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_shared);
    573   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_shared, tc)
    574   1.1   thorpej {
    575   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    576   1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE COW + SHARED)");
    577   1.1   thorpej }
    578   1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_shared, tc)
    579   1.1   thorpej {
    580   1.1   thorpej 	/*
    581   1.1   thorpej 	 * This combination (COW mapped file + SHARED futex)
    582   1.1   thorpej 	 * doesn't really make sense, but we should make sure it
    583   1.1   thorpej 	 * works as expected.
    584   1.1   thorpej 	 */
    585   1.1   thorpej 	create_bs(MAP_FILE | MAP_PRIVATE);
    586   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    587  1.11  riastrad 	    NULL, NULL, NULL,
    588  1.11  riastrad 	    0);
    589   1.1   thorpej 	ATF_REQUIRE(verify_zero_bs());
    590   1.1   thorpej }
    591   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_shared, tc)
    592   1.1   thorpej {
    593   1.1   thorpej 	do_cleanup();
    594   1.1   thorpej }
    595   1.1   thorpej 
    596   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared_proc);
    597   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared_proc, tc)
    598   1.1   thorpej {
    599   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    600   1.1   thorpej 	    "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    601   1.1   thorpej }
    602   1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared_proc, tc)
    603   1.1   thorpej {
    604   1.1   thorpej 	create_bs(MAP_ANON | MAP_SHARED);
    605   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
    606  1.11  riastrad 	    create_proc_waiter,
    607  1.11  riastrad 	    exit_proc_waiter,
    608  1.11  riastrad 	    reap_proc_waiter,
    609  1.11  riastrad 	    0);
    610   1.1   thorpej }
    611   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared_proc, tc)
    612   1.1   thorpej {
    613   1.1   thorpej 	do_cleanup();
    614   1.1   thorpej }
    615   1.1   thorpej 
    616   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared_proc);
    617   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared_proc, tc)
    618   1.1   thorpej {
    619   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    620   1.1   thorpej 	    "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    621   1.1   thorpej }
    622   1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared_proc, tc)
    623   1.1   thorpej {
    624   1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    625   1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
    626  1.11  riastrad 	    create_proc_waiter,
    627  1.11  riastrad 	    exit_proc_waiter,
    628  1.11  riastrad 	    reap_proc_waiter,
    629  1.11  riastrad 	    0);
    630   1.1   thorpej }
    631   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared_proc, tc)
    632   1.1   thorpej {
    633   1.1   thorpej 	do_cleanup();
    634   1.1   thorpej }
    635   1.1   thorpej 
    636   1.1   thorpej /*****************************************************************************/
    637   1.1   thorpej 
    638   1.1   thorpej ATF_TC(futex_wait_pointless_bitset);
    639   1.1   thorpej ATF_TC_HEAD(futex_wait_pointless_bitset, tc)
    640   1.1   thorpej {
    641   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    642   1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (SHARED)");
    643   1.1   thorpej }
    644   1.1   thorpej ATF_TC_BODY(futex_wait_pointless_bitset, tc)
    645   1.1   thorpej {
    646   1.1   thorpej 
    647   1.1   thorpej 	futex_word = 1;
    648   1.2  riastrad 	ATF_REQUIRE_ERRNO(EINVAL,
    649   1.2  riastrad 	    __futex(&futex_word, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG,
    650   1.2  riastrad 		1, NULL, NULL, 0, 0) == -1);
    651   1.1   thorpej }
    652   1.1   thorpej 
    653   1.1   thorpej static void
    654   1.1   thorpej do_futex_wait_wake_bitset_test(int flags)
    655   1.1   thorpej {
    656   1.1   thorpej 	struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
    657   1.1   thorpej 	struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
    658  1.11  riastrad 	int i, tries, n;
    659   1.1   thorpej 
    660   1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP1; i++) {
    661   1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
    662   1.1   thorpej 		lwp_data[i].op_flags = flags;
    663   1.1   thorpej 		lwp_data[i].futex_error = -1;
    664   1.1   thorpej 		lwp_data[i].bitset = __BIT(i);
    665   1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT_BITSET;
    666   1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word;
    667   1.1   thorpej 		lwp_data[i].block_val = 1;
    668   1.1   thorpej 	}
    669   1.1   thorpej 
    670   1.1   thorpej 	STORE(&futex_word, 1);
    671   1.1   thorpej 	membar_sync();
    672   1.1   thorpej 
    673  1.11  riastrad 	RL(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid));
    674  1.11  riastrad 	RL(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid));
    675   1.1   thorpej 
    676   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    677   1.1   thorpej 		membar_sync();
    678   1.1   thorpej 		if (nlwps_running == 2)
    679   1.1   thorpej 			break;
    680   1.1   thorpej 		sleep(1);
    681   1.1   thorpej 	}
    682   1.1   thorpej 	membar_sync();
    683  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 2,
    684  1.11  riastrad 	    "waiters failed to start, nlwps_running=%u", nlwps_running);
    685   1.1   thorpej 
    686   1.1   thorpej 	/* Ensure they're blocked. */
    687  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp0->futex_error, -1, "wlwp0->futex_error=%d",
    688  1.11  riastrad 	    wlwp0->futex_error);
    689  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp1->futex_error, -1, "wlwp1->futex_error=%d",
    690  1.11  riastrad 	    wlwp1->futex_error);
    691   1.1   thorpej 
    692   1.1   thorpej 	/* Make sure invalid #wakes in rejected. */
    693   1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    694   1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    695  1.11  riastrad 		-1, NULL, NULL, 0, 0) == -1);
    696   1.1   thorpej 
    697   1.1   thorpej 	/* This should result in no wakeups because no bits are set. */
    698  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    699  1.11  riastrad 		INT_MAX, NULL, NULL, 0, 0));
    700  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 0, "n=%d wakeups", n);
    701   1.1   thorpej 
    702   1.1   thorpej 	/* This should result in no wakeups because the wrongs bits are set. */
    703  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    704  1.11  riastrad 		INT_MAX, NULL, NULL, 0,
    705  1.11  riastrad 		~(wlwp0->bitset | wlwp1->bitset)));
    706  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 0, "n=%d wakeups", n);
    707   1.1   thorpej 
    708   1.1   thorpej 	/* Trust, but verify. */
    709   1.1   thorpej 	sleep(1);
    710   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    711   1.1   thorpej 		membar_sync();
    712   1.1   thorpej 		if (nlwps_running == 2)
    713   1.1   thorpej 			break;
    714   1.1   thorpej 		sleep(1);
    715   1.1   thorpej 	}
    716   1.1   thorpej 	membar_sync();
    717  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 2,
    718  1.11  riastrad 	    "waiters exited unexpectedly, nlwps_running=%u", nlwps_running);
    719   1.1   thorpej 
    720   1.1   thorpej 	/* Wake up the first LWP. */
    721  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    722  1.11  riastrad 		INT_MAX, NULL, NULL, 0, wlwp0->bitset));
    723  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 1, "n=%d wakeups", n);
    724   1.1   thorpej 	sleep(1);
    725   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    726   1.1   thorpej 		membar_sync();
    727   1.1   thorpej 		if (nlwps_running == 1)
    728   1.1   thorpej 			break;
    729   1.1   thorpej 		sleep(1);
    730   1.1   thorpej 	}
    731   1.1   thorpej 	membar_sync();
    732  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 1, "nlwps_running=%u",
    733  1.11  riastrad 	    nlwps_running);
    734  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp0->futex_error, 0, "wlwp0->futex_error=%d",
    735  1.11  riastrad 	    wlwp0->futex_error);
    736  1.11  riastrad 	RL(_lwp_wait(wlwp0->lwpid, NULL));
    737   1.1   thorpej 
    738   1.1   thorpej 	/* Wake up the second LWP. */
    739  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    740  1.11  riastrad 		INT_MAX, NULL, NULL, 0, wlwp1->bitset));
    741  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 1, "n=%d wakeups", n);
    742   1.1   thorpej 	sleep(1);
    743   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    744   1.1   thorpej 		membar_sync();
    745   1.1   thorpej 		if (nlwps_running == 0)
    746   1.1   thorpej 			break;
    747   1.1   thorpej 		sleep(1);
    748   1.1   thorpej 	}
    749   1.1   thorpej 	membar_sync();
    750  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "nlwps_running=%u",
    751  1.11  riastrad 	    nlwps_running);
    752  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp1->futex_error, 0, "wlwp1->futex_error=%d",
    753  1.11  riastrad 	    wlwp1->futex_error);
    754  1.11  riastrad 	RL(_lwp_wait(wlwp1->lwpid, NULL));
    755   1.1   thorpej }
    756   1.1   thorpej 
    757   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_bitset);
    758   1.1   thorpej ATF_TC_HEAD(futex_wait_wake_bitset, tc)
    759   1.1   thorpej {
    760   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    761   1.1   thorpej 	    "tests futex WAIT_BITSET + WAKE_BITSET operations");
    762   1.1   thorpej }
    763   1.1   thorpej ATF_TC_BODY(futex_wait_wake_bitset, tc)
    764   1.1   thorpej {
    765   1.1   thorpej 	do_futex_wait_wake_bitset_test(FUTEX_PRIVATE_FLAG);
    766   1.1   thorpej }
    767   1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_bitset, tc)
    768   1.1   thorpej {
    769   1.1   thorpej 	do_cleanup();
    770   1.1   thorpej }
    771   1.1   thorpej 
    772   1.1   thorpej /*****************************************************************************/
    773   1.1   thorpej 
    774   1.1   thorpej static void
    775   1.1   thorpej do_futex_requeue_test(int flags, int op)
    776   1.1   thorpej {
    777   1.1   thorpej 	struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
    778   1.1   thorpej 	struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
    779   1.1   thorpej 	struct lwp_data *wlwp2 = &lwp_data[WAITER_LWP2];
    780   1.1   thorpej 	struct lwp_data *wlwp3 = &lwp_data[WAITER_LWP3];
    781   1.1   thorpej 	const int good_val3 = (op == FUTEX_CMP_REQUEUE) ?   1 : 0;
    782   1.1   thorpej 	const int bad_val3  = (op == FUTEX_CMP_REQUEUE) ? 666 : 0;
    783  1.11  riastrad 	int i, tries, n;
    784   1.1   thorpej 
    785   1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP3; i++) {
    786   1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
    787   1.1   thorpej 		lwp_data[i].op_flags = flags;
    788   1.1   thorpej 		lwp_data[i].futex_error = -1;
    789   1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word;
    790   1.1   thorpej 		lwp_data[i].block_val = 1;
    791   1.1   thorpej 		lwp_data[i].bitset = 0;
    792   1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT;
    793   1.1   thorpej 	}
    794   1.1   thorpej 
    795   1.1   thorpej 	STORE(&futex_word, 1);
    796   1.1   thorpej 	STORE(&futex_word1, 1);
    797   1.1   thorpej 	membar_sync();
    798   1.1   thorpej 
    799  1.11  riastrad 	RL(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid));
    800  1.11  riastrad 	RL(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid));
    801  1.11  riastrad 	RL(_lwp_create(&wlwp2->context, 0, &wlwp2->lwpid));
    802  1.11  riastrad 	RL(_lwp_create(&wlwp3->context, 0, &wlwp3->lwpid));
    803   1.1   thorpej 
    804   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    805   1.1   thorpej 		membar_sync();
    806   1.1   thorpej 		if (nlwps_running == 4)
    807   1.1   thorpej 			break;
    808   1.1   thorpej 		sleep(1);
    809   1.1   thorpej 	}
    810   1.1   thorpej 	membar_sync();
    811  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 4,
    812  1.11  riastrad 	    "waiters failed to start, nlwps_running=%u", nlwps_running);
    813   1.1   thorpej 
    814   1.1   thorpej 	/* Ensure they're blocked. */
    815  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp0->futex_error, -1, "wlwp0->futex_error=%d",
    816  1.11  riastrad 	    wlwp0->futex_error);
    817  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp1->futex_error, -1, "wlwp1->futex_error=%d",
    818  1.11  riastrad 	    wlwp1->futex_error);
    819  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp2->futex_error, -1, "wlwp2->futex_error=%d",
    820  1.11  riastrad 	    wlwp2->futex_error);
    821  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(wlwp3->futex_error, -1, "wlwp3->futex_error=%d",
    822  1.11  riastrad 	    wlwp3->futex_error);
    823   1.1   thorpej 
    824   1.1   thorpej 	/* Make sure invalid #wakes and #requeues are rejected. */
    825   1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    826   1.1   thorpej 	    __futex(&futex_word, op | flags,
    827  1.11  riastrad 		-1, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
    828   1.1   thorpej 
    829   1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    830   1.1   thorpej 	    __futex(&futex_word, op | flags,
    831  1.11  riastrad 		0, NULL, &futex_word1, -1, bad_val3) == -1);
    832   1.1   thorpej 
    833   1.1   thorpej 	/*
    834   1.1   thorpej 	 * FUTEX 0: 4 LWPs
    835   1.1   thorpej 	 * FUTEX 1: 0 LWPs
    836   1.1   thorpej 	 */
    837   1.1   thorpej 
    838   1.1   thorpej 	if (op == FUTEX_CMP_REQUEUE) {
    839   1.1   thorpej 		/* This should fail because the futex_word value is 1. */
    840   1.1   thorpej 		ATF_REQUIRE_ERRNO(EAGAIN,
    841   1.1   thorpej 		    __futex(&futex_word, op | flags,
    842  1.11  riastrad 			0, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
    843   1.1   thorpej 	}
    844   1.1   thorpej 
    845   1.1   thorpej 	/*
    846   1.1   thorpej 	 * FUTEX 0: 4 LWPs
    847   1.1   thorpej 	 * FUTEX 1: 0 LWPs
    848   1.1   thorpej 	 */
    849   1.1   thorpej 
    850   1.1   thorpej 	/* Move all waiters from 0 to 1. */
    851  1.11  riastrad 	RL(n = __futex(&futex_word, op | flags, 0, NULL, &futex_word1,
    852  1.11  riastrad 		INT_MAX, good_val3));
    853  1.11  riastrad 	ATF_CHECK_EQ_MSG(n, 4, "n=%d woken or requeued", n);
    854   1.1   thorpej 
    855   1.1   thorpej 	/*
    856   1.1   thorpej 	 * FUTEX 0: 0 LWPs
    857   1.1   thorpej 	 * FUTEX 1: 4 LWPs
    858   1.1   thorpej 	 */
    859   1.1   thorpej 
    860   1.1   thorpej 	if (op == FUTEX_CMP_REQUEUE) {
    861   1.1   thorpej 		/* This should fail because the futex_word1 value is 1. */
    862   1.1   thorpej 		ATF_REQUIRE_ERRNO(EAGAIN,
    863   1.1   thorpej 		    __futex(&futex_word1, op | flags,
    864  1.11  riastrad 			1, NULL, &futex_word, 1, bad_val3) == -1);
    865   1.1   thorpej 	}
    866   1.1   thorpej 
    867   1.1   thorpej 	/*
    868   1.1   thorpej 	 * FUTEX 0: 0 LWPs
    869   1.1   thorpej 	 * FUTEX 1: 4 LWPs
    870   1.1   thorpej 	 */
    871   1.1   thorpej 
    872   1.1   thorpej 	/* Wake one waiter on 1, move one waiter to 0. */
    873  1.11  riastrad 	RL(n = __futex(&futex_word1, op | flags, 1, NULL, &futex_word,
    874  1.11  riastrad 		1, good_val3));
    875  1.11  riastrad 	ATF_CHECK_EQ_MSG(n, 2, "n=%d woken or requeued", n);
    876   1.1   thorpej 
    877   1.1   thorpej 	/*
    878   1.1   thorpej 	 * FUTEX 0: 1 LWP
    879   1.1   thorpej 	 * FUTEX 1: 2 LWPs
    880   1.1   thorpej 	 */
    881   1.1   thorpej 
    882   1.1   thorpej 	/* Wake all waiters on 0 (should be 1). */
    883  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE | flags, INT_MAX, NULL, NULL,
    884  1.11  riastrad 		0, 0));
    885  1.11  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
    886   1.1   thorpej 
    887   1.1   thorpej 	/* Wake all waiters on 1 (should be 2). */
    888  1.11  riastrad 	RL(n = __futex(&futex_word1, FUTEX_WAKE | flags, INT_MAX, NULL, NULL,
    889  1.11  riastrad 		0, 0));
    890  1.11  riastrad 	ATF_CHECK_EQ_MSG(n, 2, "n=%d woken", n);
    891   1.1   thorpej 
    892   1.1   thorpej 	/* Trust, but verify. */
    893   1.1   thorpej 	sleep(1);
    894   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    895   1.1   thorpej 		membar_sync();
    896   1.1   thorpej 		if (nlwps_running == 0)
    897   1.1   thorpej 			break;
    898   1.1   thorpej 		sleep(1);
    899   1.1   thorpej 	}
    900   1.1   thorpej 	membar_sync();
    901  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0,
    902  1.11  riastrad 	    "waiters failed to exit, nlwps_running=%u", nlwps_running);
    903   1.1   thorpej 
    904  1.11  riastrad 	RL(_lwp_wait(wlwp0->lwpid, NULL));
    905  1.11  riastrad 	RL(_lwp_wait(wlwp1->lwpid, NULL));
    906  1.11  riastrad 	RL(_lwp_wait(wlwp2->lwpid, NULL));
    907  1.11  riastrad 	RL(_lwp_wait(wlwp3->lwpid, NULL));
    908   1.1   thorpej }
    909   1.1   thorpej 
    910   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_requeue);
    911   1.1   thorpej ATF_TC_HEAD(futex_requeue, tc)
    912   1.1   thorpej {
    913   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    914   1.1   thorpej 	    "tests futex REQUEUE operations");
    915   1.1   thorpej }
    916   1.1   thorpej ATF_TC_BODY(futex_requeue, tc)
    917   1.1   thorpej {
    918   1.1   thorpej 	do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_REQUEUE);
    919   1.1   thorpej }
    920   1.1   thorpej ATF_TC_CLEANUP(futex_requeue, tc)
    921   1.1   thorpej {
    922   1.1   thorpej 	do_cleanup();
    923   1.1   thorpej }
    924   1.1   thorpej 
    925   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_cmp_requeue);
    926   1.1   thorpej ATF_TC_HEAD(futex_cmp_requeue, tc)
    927   1.1   thorpej {
    928   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    929   1.1   thorpej 	    "tests futex CMP_REQUEUE operations");
    930   1.1   thorpej }
    931   1.1   thorpej ATF_TC_BODY(futex_cmp_requeue, tc)
    932   1.1   thorpej {
    933   1.1   thorpej 	do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_CMP_REQUEUE);
    934   1.1   thorpej }
    935   1.1   thorpej ATF_TC_CLEANUP(futex_cmp_requeue, tc)
    936   1.1   thorpej {
    937   1.1   thorpej 	do_cleanup();
    938   1.1   thorpej }
    939   1.1   thorpej 
    940   1.6  riastrad ATF_TC(futex_cmp_requeue_trivial);
    941   1.6  riastrad ATF_TC_HEAD(futex_cmp_requeue_trivial, tc)
    942   1.6  riastrad {
    943   1.6  riastrad 	atf_tc_set_md_var(tc, "descr",
    944   1.6  riastrad 	    "tests trivial cases of futex CMP_REQUEUE operations");
    945   1.6  riastrad }
    946   1.6  riastrad ATF_TC_BODY(futex_cmp_requeue_trivial, tc)
    947   1.6  riastrad {
    948   1.6  riastrad 	int nwoken;
    949   1.6  riastrad 
    950   1.6  riastrad 	futex_word = 123;
    951   1.6  riastrad 	futex_word1 = 456;	/* should be ignored */
    952   1.6  riastrad 	ATF_CHECK_ERRNO(EAGAIN, __futex(&futex_word, FUTEX_CMP_REQUEUE,
    953   1.6  riastrad 		/*nwake*/1, NULL, &futex_word1, /*nrequeue*/1, 0) == -1);
    954   1.6  riastrad 	ATF_CHECK_ERRNO(EAGAIN, __futex(&futex_word, FUTEX_CMP_REQUEUE,
    955   1.6  riastrad 		/*nwake*/1, NULL, &futex_word1, /*nrequeue*/1, 122) == -1);
    956   1.6  riastrad 	nwoken = __futex(&futex_word, FUTEX_CMP_REQUEUE,
    957   1.6  riastrad 	    /*nwake*/1, NULL, &futex_word1, /*nrequeue*/1, 123);
    958   1.6  riastrad 	ATF_CHECK_MSG(nwoken != -1, "errno=%d (%s)", errno, strerror(errno));
    959   1.6  riastrad 	ATF_CHECK_EQ_MSG(nwoken, 0, "nwoken=%d", nwoken);
    960   1.6  riastrad 	ATF_CHECK_EQ_MSG(futex_word, 123, "futex_word=%d", futex_word);
    961   1.6  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 456, "futex_word1=%d", futex_word1);
    962   1.6  riastrad }
    963   1.6  riastrad 
    964   1.1   thorpej /*****************************************************************************/
    965   1.1   thorpej 
    966   1.1   thorpej static void
    967   1.1   thorpej do_futex_wake_op_op_test(int flags)
    968   1.1   thorpej {
    969  1.11  riastrad 	int op, n;
    970   1.1   thorpej 
    971   1.1   thorpej 	futex_word = 0;
    972   1.1   thorpej 	futex_word1 = 0;
    973   1.1   thorpej 
    974   1.1   thorpej 	/*
    975   1.1   thorpej 	 * The op= operations should work even if there are no waiters.
    976   1.1   thorpej 	 */
    977   1.1   thorpej 
    978   1.1   thorpej 	/*
    979   1.1   thorpej 	 * Because these operations use both futex addresses, exercise
    980   1.1   thorpej 	 * rejecting unaligned futex addresses here.
    981   1.1   thorpej 	 */
    982   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
    983  1.12  riastrad 	ATF_CHECK_ERRNO(EINVAL,
    984   1.1   thorpej 	    __futex((int *)1, FUTEX_WAKE_OP | flags,
    985  1.11  riastrad 		0, NULL, &futex_word1, 0, op) == -1);
    986  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 0, "futex_word1=%d", futex_word1);
    987   1.1   thorpej 
    988  1.12  riastrad 	ATF_CHECK_ERRNO(EINVAL,
    989   1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
    990  1.11  riastrad 		0, NULL, (int *)1, 0, op) == -1);
    991  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word, 0, "futex_word=%d", futex_word);
    992   1.1   thorpej 
    993   1.1   thorpej 	/* Check unmapped uaddr2 handling, too. */
    994  1.12  riastrad 	ATF_CHECK_ERRNO(EFAULT,
    995   1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
    996  1.11  riastrad 		0, NULL, NULL, 0, op) == -1);
    997  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word, 0, "futex_word=%d", futex_word);
    998   1.1   thorpej 
    999   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
   1000  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1001  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1002  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1003  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 1, "futex_word1=%d", futex_word1);
   1004   1.1   thorpej 
   1005   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_ADD, 1, FUTEX_OP_CMP_EQ, 0);
   1006  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1007  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1008  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1009  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1010   1.1   thorpej 
   1011   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR, 2, FUTEX_OP_CMP_EQ, 0);
   1012  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1013  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1014  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1015  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1016   1.1   thorpej 
   1017   1.1   thorpej 	/* This should fail because of invalid shift value 32. */
   1018   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 32,
   1019  1.11  riastrad 	    FUTEX_OP_CMP_EQ, 0);
   1020  1.12  riastrad 	ATF_CHECK_ERRNO(EINVAL,
   1021   1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1022  1.11  riastrad 		0, NULL, &futex_word1, 0, op) == -1);
   1023  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1024   1.1   thorpej 
   1025   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 31,
   1026  1.11  riastrad 	    FUTEX_OP_CMP_EQ, 0);
   1027  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1028  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1029  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1030  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, (int)0x80000002,
   1031  1.11  riastrad 	    "futex_word1=0x%x", futex_word1);
   1032   1.1   thorpej 
   1033   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_ANDN | FUTEX_OP_OPARG_SHIFT, 31,
   1034  1.11  riastrad 	    FUTEX_OP_CMP_EQ, 0);
   1035  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1036  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1037  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1038  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1039   1.1   thorpej 
   1040   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_XOR, 2, FUTEX_OP_CMP_EQ, 0);
   1041  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1042  1.11  riastrad 		0, NULL, &futex_word1, 0, op));
   1043  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1044  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 0, "futex_word1=%d", futex_word1);
   1045  1.13  riastrad 
   1046  1.13  riastrad 	/*
   1047  1.13  riastrad 	 * Verify oparg is sign-extended.
   1048  1.13  riastrad 	 */
   1049  1.13  riastrad 	futex_word1 = 0;
   1050  1.13  riastrad 	op = FUTEX_OP(FUTEX_OP_SET, 0xfff, FUTEX_OP_CMP_EQ, 0);
   1051  1.13  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1052  1.13  riastrad 		0, NULL, &futex_word1, 0, op));
   1053  1.13  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1054  1.13  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, -1, "futex_word1=%d", futex_word1);
   1055  1.13  riastrad 
   1056  1.13  riastrad 	futex_word1 = 0;
   1057  1.14  riastrad 	op = FUTEX_OP(FUTEX_OP_SET, -1, FUTEX_OP_CMP_EQ, 0);
   1058  1.13  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1059  1.13  riastrad 		0, NULL, &futex_word1, 0, op));
   1060  1.13  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1061  1.13  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, -1, "futex_word1=%d", futex_word1);
   1062   1.1   thorpej }
   1063   1.1   thorpej 
   1064   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_op);
   1065   1.1   thorpej ATF_TC_HEAD(futex_wake_op_op, tc)
   1066   1.1   thorpej {
   1067   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1068   1.1   thorpej 	    "tests futex WAKE_OP OP operations");
   1069   1.1   thorpej }
   1070   1.1   thorpej ATF_TC_BODY(futex_wake_op_op, tc)
   1071   1.1   thorpej {
   1072   1.1   thorpej 	do_futex_wake_op_op_test(FUTEX_PRIVATE_FLAG);
   1073   1.1   thorpej }
   1074   1.1   thorpej ATF_TC_CLEANUP(futex_wake_op_op, tc)
   1075   1.1   thorpej {
   1076   1.1   thorpej 	do_cleanup();
   1077   1.1   thorpej }
   1078   1.1   thorpej 
   1079   1.1   thorpej static void
   1080   1.1   thorpej create_wake_op_test_lwps(int flags)
   1081   1.1   thorpej {
   1082   1.1   thorpej 	int i;
   1083   1.1   thorpej 
   1084   1.1   thorpej 	futex_word1 = 0;
   1085   1.1   thorpej 	membar_sync();
   1086   1.1   thorpej 
   1087   1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1088   1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
   1089   1.1   thorpej 		lwp_data[i].op_flags = flags;
   1090   1.1   thorpej 		lwp_data[i].futex_error = -1;
   1091   1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word1;
   1092   1.1   thorpej 		lwp_data[i].block_val = 0;
   1093   1.1   thorpej 		lwp_data[i].bitset = 0;
   1094   1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT;
   1095  1.11  riastrad 		RL(_lwp_create(&lwp_data[i].context, 0, &lwp_data[i].lwpid));
   1096   1.1   thorpej 	}
   1097   1.1   thorpej 
   1098   1.1   thorpej 	for (i = 0; i < 5; i++) {
   1099   1.1   thorpej 		membar_sync();
   1100   1.1   thorpej 		if (nlwps_running == 6)
   1101   1.1   thorpej 			break;
   1102   1.1   thorpej 		sleep(1);
   1103   1.1   thorpej 	}
   1104   1.1   thorpej 	membar_sync();
   1105  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 6,
   1106  1.11  riastrad 	    "waiters failed to start, nlwps_running=%u", nlwps_running);
   1107   1.1   thorpej 
   1108   1.1   thorpej 	/* Ensure they're blocked. */
   1109   1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1110  1.11  riastrad 		ATF_REQUIRE_EQ_MSG(lwp_data[i].futex_error, -1,
   1111  1.11  riastrad 		    "i=%d lwp_data[i].futex_error=%d",
   1112  1.11  riastrad 		    i, lwp_data[i].futex_error);
   1113   1.1   thorpej 	}
   1114   1.1   thorpej }
   1115   1.1   thorpej 
   1116   1.1   thorpej static void
   1117   1.1   thorpej reap_wake_op_test_lwps(void)
   1118   1.1   thorpej {
   1119   1.1   thorpej 	int i;
   1120   1.1   thorpej 
   1121   1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1122  1.11  riastrad 		RL(_lwp_wait(lwp_data[i].lwpid, NULL));
   1123   1.1   thorpej 	}
   1124   1.1   thorpej }
   1125   1.1   thorpej 
   1126   1.1   thorpej static void
   1127   1.1   thorpej do_futex_wake_op_cmp_test(int flags)
   1128   1.1   thorpej {
   1129  1.11  riastrad 	int tries, op, n;
   1130   1.1   thorpej 
   1131   1.1   thorpej 	futex_word = 0;
   1132   1.1   thorpej 	membar_sync();
   1133   1.1   thorpej 
   1134   1.1   thorpej 	/*
   1135   1.1   thorpej 	 * Verify and negative and positive for each individual
   1136   1.1   thorpej 	 * compare.
   1137   1.1   thorpej 	 */
   1138   1.1   thorpej 
   1139   1.1   thorpej 	create_wake_op_test_lwps(flags);
   1140   1.1   thorpej 
   1141   1.1   thorpej 	/* #LWPs = 6 */
   1142  1.13  riastrad 	futex_word1 = 0xfff;
   1143  1.13  riastrad 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 0xfff);
   1144  1.13  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1145  1.13  riastrad 		0, NULL, &futex_word1, 1, op));
   1146  1.13  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1147  1.13  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 0, "futex_word1=%d", futex_word1);
   1148  1.13  riastrad 
   1149  1.13  riastrad 	futex_word1 = 0xfff;
   1150  1.14  riastrad 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, -1);
   1151  1.13  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1152  1.13  riastrad 		0, NULL, &futex_word1, 1, op));
   1153  1.13  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1154  1.13  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 0, "futex_word1=%d", futex_word1);
   1155  1.13  riastrad 
   1156   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 1);
   1157  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1158  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1159  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1160  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 0, "futex_word1=%d", futex_word1);
   1161   1.1   thorpej 
   1162  1.13  riastrad 	futex_word1 = -1;
   1163  1.13  riastrad 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0xfff);
   1164  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1165  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1166  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1167  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 1, "futex_word1=%d", futex_word1);
   1168   1.1   thorpej 
   1169   1.1   thorpej 	/* #LWPs = 5 */
   1170   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_NE, 1);
   1171  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1172  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1173  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1174  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 1, "futex_word1=%d", futex_word1);
   1175   1.1   thorpej 
   1176   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_NE, 2);
   1177  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1178  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1179  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1180  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1181   1.1   thorpej 
   1182   1.1   thorpej 	/* #LWPs = 4 */
   1183   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 2);
   1184  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1185  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1186  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1187  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1188   1.1   thorpej 
   1189   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 3);
   1190  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1191  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1192  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1193  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1194   1.1   thorpej 
   1195   1.1   thorpej 	/* #LWPs = 3 */
   1196   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
   1197  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1198  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1199  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1200  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 1, "futex_word1=%d", futex_word1);
   1201   1.1   thorpej 
   1202   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
   1203  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1204  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1205  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1206  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 1, "futex_word1=%d", futex_word1);
   1207   1.1   thorpej 
   1208   1.1   thorpej 	/* #LWPs = 2 */
   1209   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GT, 3);
   1210  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1211  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1212  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1213  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 3, "futex_word1=%d", futex_word1);
   1214   1.1   thorpej 
   1215   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GT, 2);
   1216  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1217  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1218  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1219  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1220   1.1   thorpej 
   1221   1.1   thorpej 	/* #LWPs = 1 */
   1222   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GE, 4);
   1223  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1224  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1225  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 0, "n=%d woken", n);
   1226  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 3, "futex_word1=%d", futex_word1);
   1227  1.11  riastrad 
   1228   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GE, 3);
   1229  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE_OP | flags,
   1230  1.11  riastrad 		0, NULL, &futex_word1, 1, op));
   1231  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 1, "n=%d woken", n);
   1232  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word1, 2, "futex_word1=%d", futex_word1);
   1233   1.1   thorpej 
   1234   1.1   thorpej 	/* #LWPs = 0 */
   1235   1.1   thorpej 
   1236   1.1   thorpej 	/* Trust, but verify. */
   1237   1.1   thorpej 	sleep(1);
   1238   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
   1239   1.1   thorpej 		membar_sync();
   1240   1.1   thorpej 		if (nlwps_running == 0)
   1241   1.1   thorpej 			break;
   1242   1.1   thorpej 		sleep(1);
   1243   1.1   thorpej 	}
   1244   1.1   thorpej 	membar_sync();
   1245  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0,
   1246  1.11  riastrad 	    "waiters failed to exit, nlwps_running=%u", nlwps_running);
   1247   1.1   thorpej 
   1248   1.1   thorpej 	reap_wake_op_test_lwps();
   1249   1.1   thorpej 
   1250   1.1   thorpej 	/*
   1251   1.1   thorpej 	 * Verify wakes on uaddr work even if the uaddr2 comparison
   1252   1.1   thorpej 	 * fails.
   1253   1.1   thorpej 	 */
   1254   1.1   thorpej 
   1255   1.1   thorpej 	create_wake_op_test_lwps(flags);
   1256   1.1   thorpej 
   1257   1.1   thorpej 	/* #LWPs = 6 */
   1258  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word, 0, "futex_word=%d", futex_word);
   1259   1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 666);
   1260  1.11  riastrad 	RL(n = __futex(&futex_word1, FUTEX_WAKE_OP | flags,
   1261  1.11  riastrad 		INT_MAX, NULL, &futex_word, 0, op));
   1262  1.12  riastrad 	ATF_CHECK_EQ_MSG(n, 6, "n=%d woken", n);
   1263  1.12  riastrad 	ATF_CHECK_EQ_MSG(futex_word, 0, "futex_word=%d", futex_word);
   1264   1.1   thorpej 
   1265   1.1   thorpej 	/* #LWPs = 0 */
   1266   1.1   thorpej 
   1267   1.1   thorpej 	/* Trust, but verify. */
   1268   1.1   thorpej 	sleep(1);
   1269   1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
   1270   1.1   thorpej 		membar_sync();
   1271   1.1   thorpej 		if (nlwps_running == 0)
   1272   1.1   thorpej 			break;
   1273   1.1   thorpej 		sleep(1);
   1274   1.1   thorpej 	}
   1275   1.1   thorpej 	membar_sync();
   1276  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0,
   1277  1.11  riastrad 	    "waiters failed to exit, nlwps_running=%u", nlwps_running);
   1278   1.1   thorpej 
   1279   1.1   thorpej 	reap_wake_op_test_lwps();
   1280   1.1   thorpej }
   1281   1.1   thorpej 
   1282   1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_cmp);
   1283   1.1   thorpej ATF_TC_HEAD(futex_wake_op_cmp, tc)
   1284   1.1   thorpej {
   1285   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1286   1.1   thorpej 	    "tests futex WAKE_OP CMP operations");
   1287   1.1   thorpej }
   1288   1.1   thorpej ATF_TC_BODY(futex_wake_op_cmp, tc)
   1289   1.1   thorpej {
   1290   1.1   thorpej 	do_futex_wake_op_cmp_test(FUTEX_PRIVATE_FLAG);
   1291   1.1   thorpej }
   1292   1.1   thorpej ATF_TC_CLEANUP(futex_wake_op_cmp, tc)
   1293   1.1   thorpej {
   1294   1.1   thorpej 	do_cleanup();
   1295   1.1   thorpej }
   1296   1.1   thorpej 
   1297   1.1   thorpej /*****************************************************************************/
   1298   1.1   thorpej 
   1299  1.11  riastrad 
   1300  1.11  riastrad 
   1301  1.11  riastrad /*****************************************************************************/
   1302  1.11  riastrad 
   1303   1.1   thorpej static void
   1304   1.1   thorpej do_futex_wait_timeout(bool relative, clockid_t clock)
   1305   1.1   thorpej {
   1306   1.1   thorpej 	struct timespec ts;
   1307   1.1   thorpej 	struct timespec deadline;
   1308   1.1   thorpej 	int op = relative ? FUTEX_WAIT : FUTEX_WAIT_BITSET;
   1309   1.1   thorpej 
   1310   1.1   thorpej 	if (clock == CLOCK_REALTIME)
   1311   1.1   thorpej 		op |= FUTEX_CLOCK_REALTIME;
   1312   1.1   thorpej 
   1313  1.11  riastrad 	RL(clock_gettime(clock, &deadline));
   1314   1.1   thorpej 	deadline.tv_sec += 2;
   1315   1.1   thorpej 	if (relative) {
   1316   1.1   thorpej 		ts.tv_sec = 2;
   1317   1.1   thorpej 		ts.tv_nsec = 0;
   1318   1.1   thorpej 	} else {
   1319   1.1   thorpej 		ts = deadline;
   1320   1.1   thorpej 	}
   1321   1.1   thorpej 
   1322   1.1   thorpej 	futex_word = 1;
   1323   1.1   thorpej 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
   1324   1.1   thorpej 	    __futex(&futex_word, op | FUTEX_PRIVATE_FLAG,
   1325  1.11  riastrad 		1, &ts, NULL, 0, FUTEX_BITSET_MATCH_ANY) == -1);
   1326   1.1   thorpej 
   1327   1.1   thorpej 	/* Can't reliably check CLOCK_REALTIME in the presence of NTP. */
   1328   1.1   thorpej 	if (clock != CLOCK_REALTIME) {
   1329  1.11  riastrad 		RL(clock_gettime(clock, &ts));
   1330  1.12  riastrad 		ATF_CHECK_MSG(ts.tv_sec >= deadline.tv_sec,
   1331  1.11  riastrad 		    "ts=%lld.%09ldsec deadline=%lld.%09ldsec",
   1332  1.11  riastrad 		    (long long)ts.tv_sec, ts.tv_nsec,
   1333  1.11  riastrad 		    (long long)deadline.tv_sec, deadline.tv_nsec);
   1334  1.12  riastrad 		ATF_CHECK_MSG((ts.tv_sec > deadline.tv_sec ||
   1335  1.11  riastrad 			ts.tv_nsec >= deadline.tv_nsec),
   1336  1.11  riastrad 		    "ts=%lld.%09ldsec deadline=%lld.%09ldsec",
   1337  1.11  riastrad 		    (long long)ts.tv_sec, ts.tv_nsec,
   1338  1.11  riastrad 		    (long long)deadline.tv_sec, deadline.tv_nsec);
   1339   1.1   thorpej 	}
   1340   1.1   thorpej }
   1341   1.1   thorpej 
   1342   1.1   thorpej ATF_TC(futex_wait_timeout_relative);
   1343   1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_relative, tc)
   1344   1.1   thorpej {
   1345   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1346   1.1   thorpej 	    "tests futex WAIT with relative timeout");
   1347   1.1   thorpej }
   1348   1.1   thorpej ATF_TC_BODY(futex_wait_timeout_relative, tc)
   1349   1.1   thorpej {
   1350   1.1   thorpej 	do_futex_wait_timeout(true, CLOCK_MONOTONIC);
   1351   1.1   thorpej }
   1352   1.1   thorpej 
   1353   1.1   thorpej ATF_TC(futex_wait_timeout_relative_rt);
   1354   1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_relative_rt, tc)
   1355   1.1   thorpej {
   1356   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1357   1.1   thorpej 	    "tests futex WAIT with relative timeout (REALTIME)");
   1358   1.1   thorpej }
   1359   1.1   thorpej ATF_TC_BODY(futex_wait_timeout_relative_rt, tc)
   1360   1.1   thorpej {
   1361   1.1   thorpej 	do_futex_wait_timeout(true, CLOCK_REALTIME);
   1362   1.1   thorpej }
   1363   1.1   thorpej 
   1364   1.1   thorpej ATF_TC(futex_wait_timeout_deadline);
   1365   1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_deadline, tc)
   1366   1.1   thorpej {
   1367   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1368   1.1   thorpej 	    "tests futex WAIT with absolute deadline");
   1369   1.1   thorpej }
   1370   1.1   thorpej ATF_TC_BODY(futex_wait_timeout_deadline, tc)
   1371   1.1   thorpej {
   1372   1.1   thorpej 	do_futex_wait_timeout(false, CLOCK_MONOTONIC);
   1373   1.1   thorpej }
   1374   1.1   thorpej 
   1375   1.1   thorpej ATF_TC(futex_wait_timeout_deadline_rt);
   1376   1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_deadline_rt, tc)
   1377   1.1   thorpej {
   1378   1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1379   1.1   thorpej 	    "tests futex WAIT with absolute deadline (REALTIME)");
   1380   1.1   thorpej }
   1381   1.1   thorpej ATF_TC_BODY(futex_wait_timeout_deadline_rt, tc)
   1382   1.1   thorpej {
   1383   1.1   thorpej 	do_futex_wait_timeout(false, CLOCK_REALTIME);
   1384   1.1   thorpej }
   1385   1.1   thorpej 
   1386   1.1   thorpej /*****************************************************************************/
   1387   1.1   thorpej 
   1388   1.3   thorpej static void
   1389   1.3   thorpej sig_noop(int sig __unused)
   1390   1.3   thorpej {
   1391   1.3   thorpej }
   1392   1.3   thorpej 
   1393   1.3   thorpej static void (*old_act)(int) = SIG_DFL;
   1394   1.3   thorpej 
   1395   1.3   thorpej static void
   1396   1.3   thorpej do_futex_wait_evil_unmapped(int map_flags)
   1397   1.3   thorpej {
   1398   1.3   thorpej 	int i;
   1399   1.3   thorpej 
   1400   1.3   thorpej 	create_bs(map_flags);
   1401   1.3   thorpej 
   1402  1.11  riastrad 	REQUIRE_LIBC(signal(SIGUSR1, sig_noop), SIG_ERR);
   1403   1.3   thorpej 
   1404   1.3   thorpej 	setup_lwp_context(&lwp_data[0], simple_test_waiter_lwp);
   1405   1.3   thorpej 	lwp_data[0].op_flags = 0;
   1406   1.3   thorpej 	lwp_data[0].futex_error = -1;
   1407   1.3   thorpej 	lwp_data[0].futex_ptr = &bs_addr[0];
   1408   1.3   thorpej 	lwp_data[0].block_val = 0;
   1409   1.3   thorpej 	lwp_data[0].bitset = 0;
   1410   1.3   thorpej 	lwp_data[0].wait_op = FUTEX_WAIT;
   1411  1.11  riastrad 	RL(_lwp_create(&lwp_data[0].context, 0, &lwp_data[0].lwpid));
   1412   1.3   thorpej 
   1413   1.3   thorpej 	for (i = 0; i < 5; i++) {
   1414   1.3   thorpej 		membar_sync();
   1415   1.3   thorpej 		if (nlwps_running == 1)
   1416   1.3   thorpej 			break;
   1417   1.3   thorpej 		sleep(1);
   1418   1.3   thorpej 	}
   1419   1.3   thorpej 	membar_sync();
   1420  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 1,
   1421  1.11  riastrad 	    "waiters failed to start, nlwps_running=%u", nlwps_running);
   1422   1.3   thorpej 
   1423   1.3   thorpej 	/* Ensure it's blocked. */
   1424  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(lwp_data[0].futex_error, -1,
   1425  1.11  riastrad 	    "lwp_data[0].futex_error=%d", lwp_data[0].futex_error);
   1426   1.3   thorpej 
   1427   1.3   thorpej 	/* Rudely unmap the backing store. */
   1428   1.3   thorpej 	cleanup_bs();
   1429   1.3   thorpej 
   1430   1.3   thorpej 	/* Signal the waiter so that it leaves the futex. */
   1431  1.11  riastrad 	RL(_lwp_kill(lwp_data[0].threadid, SIGUSR1));
   1432   1.3   thorpej 
   1433   1.3   thorpej 	/* Yay! No panic! */
   1434   1.3   thorpej 
   1435   1.3   thorpej 	reap_lwp_waiter(&lwp_data[0]);
   1436   1.3   thorpej }
   1437   1.3   thorpej 
   1438   1.3   thorpej ATF_TC_WITH_CLEANUP(futex_wait_evil_unmapped_anon);
   1439   1.3   thorpej ATF_TC_HEAD(futex_wait_evil_unmapped_anon, tc)
   1440   1.3   thorpej {
   1441   1.3   thorpej 	atf_tc_set_md_var(tc, "descr",
   1442   1.3   thorpej 	    "tests futex WAIT while futex is unmapped - anon memory");
   1443   1.3   thorpej }
   1444   1.3   thorpej ATF_TC_BODY(futex_wait_evil_unmapped_anon, tc)
   1445   1.3   thorpej {
   1446   1.3   thorpej 	do_futex_wait_evil_unmapped(MAP_ANON);
   1447   1.3   thorpej }
   1448   1.3   thorpej ATF_TC_CLEANUP(futex_wait_evil_unmapped_anon, tc)
   1449   1.3   thorpej {
   1450   1.3   thorpej 	signal(SIGUSR1, old_act);
   1451   1.3   thorpej 	do_cleanup();
   1452   1.3   thorpej }
   1453   1.3   thorpej 
   1454   1.3   thorpej /*****************************************************************************/
   1455   1.3   thorpej 
   1456   1.4   thorpej static int pri_min;
   1457   1.4   thorpej static int pri_max;
   1458   1.4   thorpej 
   1459   1.4   thorpej static void
   1460   1.4   thorpej lowpri_simple_test_waiter_lwp(void *arg)
   1461   1.4   thorpej {
   1462   1.4   thorpej 	struct lwp_data *d = arg;
   1463   1.4   thorpej 	struct sched_param sp;
   1464   1.4   thorpej 	int policy;
   1465   1.4   thorpej 
   1466   1.4   thorpej 	d->threadid = _lwp_self();
   1467   1.4   thorpej 
   1468  1.11  riastrad 	RL(_sched_getparam(getpid(), d->threadid, &policy, &sp));
   1469   1.4   thorpej 	policy = SCHED_RR;
   1470   1.4   thorpej 	sp.sched_priority = pri_min;
   1471  1.11  riastrad 	RL(_sched_setparam(getpid(), d->threadid, policy, &sp));
   1472   1.4   thorpej 
   1473   1.4   thorpej 	simple_test_waiter_lwp(arg);
   1474   1.4   thorpej }
   1475   1.4   thorpej 
   1476   1.4   thorpej static void
   1477   1.4   thorpej highpri_simple_test_waiter_lwp(void *arg)
   1478   1.4   thorpej {
   1479   1.4   thorpej 	struct lwp_data *d = arg;
   1480   1.4   thorpej 	struct sched_param sp;
   1481   1.4   thorpej 	int policy;
   1482   1.4   thorpej 
   1483   1.4   thorpej 	d->threadid = _lwp_self();
   1484   1.4   thorpej 
   1485  1.11  riastrad 	RL(_sched_getparam(getpid(), d->threadid, &policy, &sp));
   1486   1.4   thorpej 	policy = SCHED_RR;
   1487   1.4   thorpej 	sp.sched_priority = pri_max;
   1488  1.11  riastrad 	RL(_sched_setparam(getpid(), d->threadid, policy, &sp));
   1489   1.4   thorpej 
   1490   1.4   thorpej 	simple_test_waiter_lwp(arg);
   1491   1.4   thorpej }
   1492   1.4   thorpej 
   1493   1.4   thorpej static void
   1494   1.4   thorpej do_test_wake_highest_pri(void)
   1495   1.4   thorpej {
   1496   1.4   thorpej 	lwpid_t waiter;
   1497   1.4   thorpej 	int tries;
   1498   1.4   thorpej 	long pri;
   1499  1.11  riastrad 	int n;
   1500   1.4   thorpej 
   1501  1.11  riastrad 	RL(pri = sysconf(_SC_SCHED_PRI_MIN));
   1502   1.4   thorpej 	pri_min = (int)pri;
   1503  1.11  riastrad 	RL(pri = sysconf(_SC_SCHED_PRI_MAX));
   1504   1.4   thorpej 	pri_max = (int)pri;
   1505   1.4   thorpej 
   1506   1.4   thorpej 	futex_word = 0;
   1507   1.4   thorpej 	membar_sync();
   1508   1.4   thorpej 
   1509   1.4   thorpej 	setup_lwp_context(&lwp_data[0], lowpri_simple_test_waiter_lwp);
   1510   1.4   thorpej 	lwp_data[0].op_flags = FUTEX_PRIVATE_FLAG;
   1511   1.4   thorpej 	lwp_data[0].futex_error = -1;
   1512   1.4   thorpej 	lwp_data[0].futex_ptr = &futex_word;
   1513   1.4   thorpej 	lwp_data[0].block_val = 0;
   1514   1.4   thorpej 	lwp_data[0].bitset = 0;
   1515   1.4   thorpej 	lwp_data[0].wait_op = FUTEX_WAIT;
   1516  1.11  riastrad 	RL(_lwp_create(&lwp_data[0].context, 0, &lwp_data[0].lwpid));
   1517   1.4   thorpej 
   1518   1.4   thorpej 	for (tries = 0; tries < 5; tries++) {
   1519   1.4   thorpej 		membar_sync();
   1520   1.4   thorpej 		if (nlwps_running == 1)
   1521   1.4   thorpej 			break;
   1522   1.4   thorpej 		sleep(1);
   1523   1.4   thorpej 	}
   1524   1.4   thorpej 	membar_sync();
   1525  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 1,
   1526  1.11  riastrad 	    "lowpri waiter failed to start, nlwps_running=%u", nlwps_running);
   1527   1.4   thorpej 
   1528   1.4   thorpej 	/* Ensure it's blocked. */
   1529  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(lwp_data[0].futex_error, -1,
   1530  1.11  riastrad 	    "lwp_data[0].futex_error=%d", lwp_data[0].futex_error);
   1531   1.4   thorpej 
   1532   1.4   thorpej 	setup_lwp_context(&lwp_data[1], highpri_simple_test_waiter_lwp);
   1533   1.4   thorpej 	lwp_data[1].op_flags = FUTEX_PRIVATE_FLAG;
   1534   1.4   thorpej 	lwp_data[1].futex_error = -1;
   1535   1.4   thorpej 	lwp_data[1].futex_ptr = &futex_word;
   1536   1.4   thorpej 	lwp_data[1].block_val = 0;
   1537   1.4   thorpej 	lwp_data[1].bitset = 0;
   1538   1.4   thorpej 	lwp_data[1].wait_op = FUTEX_WAIT;
   1539  1.11  riastrad 	RL(_lwp_create(&lwp_data[1].context, 0, &lwp_data[1].lwpid));
   1540   1.4   thorpej 
   1541   1.4   thorpej 	for (tries = 0; tries < 5; tries++) {
   1542   1.4   thorpej 		membar_sync();
   1543   1.4   thorpej 		if (nlwps_running == 2)
   1544   1.4   thorpej 			break;
   1545   1.4   thorpej 		sleep(1);
   1546   1.4   thorpej 	}
   1547   1.4   thorpej 	membar_sync();
   1548  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 2,
   1549  1.11  riastrad 	    "highpri waiter failed to start, nlwps_running=%u", nlwps_running);
   1550   1.4   thorpej 
   1551   1.4   thorpej 	/* Ensure it's blocked. */
   1552  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(lwp_data[1].futex_error, -1,
   1553  1.11  riastrad 	    "lwp_data[1].futex_error=%d", lwp_data[1].futex_error);
   1554   1.4   thorpej 
   1555   1.4   thorpej 	/* Wake the first LWP.  We should get the highpri thread. */
   1556  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE | FUTEX_PRIVATE_FLAG,
   1557  1.11  riastrad 		1, NULL, NULL, 0, 0));
   1558  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 1, "n=%d woken", n);
   1559   1.4   thorpej 	sleep(1);
   1560   1.4   thorpej 	for (tries = 0; tries < 5; tries++) {
   1561   1.4   thorpej 		membar_sync();
   1562   1.4   thorpej 		if (nlwps_running == 1)
   1563   1.4   thorpej 			break;
   1564   1.4   thorpej 		sleep(1);
   1565   1.4   thorpej 	}
   1566   1.4   thorpej 	membar_sync();
   1567  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 1, "nlwps_running=%u",
   1568  1.11  riastrad 	    nlwps_running);
   1569  1.11  riastrad 	RL(_lwp_wait(0, &waiter));
   1570  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(waiter, lwp_data[1].threadid,
   1571  1.11  riastrad 	    "waiter=%ld lwp_data[1].threadid=%ld",
   1572  1.11  riastrad 	    (long)waiter, (long)lwp_data[1].threadid);
   1573   1.4   thorpej 
   1574   1.4   thorpej 	/* Wake the second LWP.  We should get the lowpri thread. */
   1575  1.11  riastrad 	RL(n = __futex(&futex_word, FUTEX_WAKE | FUTEX_PRIVATE_FLAG,
   1576  1.11  riastrad 		1, NULL, NULL, 0, 0));
   1577  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(n, 1, "n=%d woken", n);
   1578   1.4   thorpej 	sleep(1);
   1579   1.4   thorpej 	for (tries = 0; tries < 5; tries++) {
   1580   1.4   thorpej 		membar_sync();
   1581   1.4   thorpej 		if (nlwps_running == 0)
   1582   1.4   thorpej 			break;
   1583   1.4   thorpej 		sleep(1);
   1584   1.4   thorpej 	}
   1585   1.4   thorpej 	membar_sync();
   1586  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "nlwps_running=%u",
   1587  1.11  riastrad 	    nlwps_running);
   1588  1.11  riastrad 	RL(_lwp_wait(0, &waiter));
   1589  1.11  riastrad 	ATF_REQUIRE_EQ_MSG(waiter, lwp_data[0].threadid,
   1590  1.11  riastrad 	    "waiter=%ld lwp_data[0].threadid=%ld",
   1591  1.11  riastrad 	    (long)waiter, (long)lwp_data[0].threadid);
   1592   1.4   thorpej }
   1593   1.4   thorpej 
   1594   1.4   thorpej ATF_TC_WITH_CLEANUP(futex_wake_highest_pri);
   1595   1.4   thorpej ATF_TC_HEAD(futex_wake_highest_pri, tc)
   1596   1.4   thorpej {
   1597   1.4   thorpej 	atf_tc_set_md_var(tc, "descr",
   1598   1.4   thorpej 	    "tests that futex WAKE wakes the highest priority waiter");
   1599   1.4   thorpej 	atf_tc_set_md_var(tc, "require.user", "root");
   1600   1.4   thorpej }
   1601   1.4   thorpej ATF_TC_BODY(futex_wake_highest_pri, tc)
   1602   1.4   thorpej {
   1603   1.4   thorpej 	atf_tc_expect_fail("PR kern/55230");
   1604   1.4   thorpej 	do_test_wake_highest_pri();
   1605   1.4   thorpej }
   1606   1.4   thorpej ATF_TC_CLEANUP(futex_wake_highest_pri, tc)
   1607   1.4   thorpej {
   1608   1.4   thorpej 	do_cleanup();
   1609   1.4   thorpej }
   1610   1.4   thorpej 
   1611   1.4   thorpej /*****************************************************************************/
   1612   1.4   thorpej 
   1613   1.1   thorpej ATF_TP_ADD_TCS(tp)
   1614   1.1   thorpej {
   1615   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_basic_wait_wake_private);
   1616   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_basic_wait_wake_shared);
   1617   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_private);
   1618   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared);
   1619   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_private);
   1620   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared);
   1621   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_private);
   1622   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_shared);
   1623   1.1   thorpej 
   1624   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared_proc);
   1625   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared_proc);
   1626   1.1   thorpej 
   1627   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_pointless_bitset);
   1628   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_bitset);
   1629   1.1   thorpej 
   1630   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_relative);
   1631   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_relative_rt);
   1632   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline);
   1633   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline_rt);
   1634   1.1   thorpej 
   1635   1.3   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_evil_unmapped_anon);
   1636   1.3   thorpej 
   1637   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_requeue);
   1638   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_cmp_requeue);
   1639   1.6  riastrad 	ATF_TP_ADD_TC(tp, futex_cmp_requeue_trivial);
   1640   1.1   thorpej 
   1641   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wake_op_op);
   1642   1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wake_op_cmp);
   1643   1.1   thorpej 
   1644   1.4   thorpej 	ATF_TP_ADD_TC(tp, futex_wake_highest_pri);
   1645   1.4   thorpej 
   1646   1.1   thorpej 	return atf_no_error();
   1647   1.1   thorpej }
   1648