xref: /netbsd-src/sys/compat/netbsd32/netbsd32_futex.c (revision 276ef2237848f9276aa522fb1629389cf68b910f)
1 /*	$NetBSD: netbsd32_futex.c,v 1.1 2020/04/26 18:53:33 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2019 The NetBSD Foundation.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell and Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_futex.c,v 1.1 2020/04/26 18:53:33 thorpej Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/lwp.h>
40 #include <sys/syscallargs.h>
41 #include <sys/futex.h>
42 
43 #include <compat/netbsd32/netbsd32.h>
44 #include <compat/netbsd32/netbsd32_syscallargs.h>
45 #include <compat/netbsd32/netbsd32_conv.h>
46 
47 /* Sycalls conversion */
48 
49 int
netbsd32___futex(struct lwp * l,const struct netbsd32___futex_args * uap,register_t * retval)50 netbsd32___futex(struct lwp *l, const struct netbsd32___futex_args *uap,
51     register_t *retval)
52 {
53 	/* {
54 		syscallarg(netbsd32_intp) uaddr;
55 		syscallarg(int) op;
56 		syscallarg(int) val;
57 		syscallarg(netbsd32_timespecp_t) timeout;
58 		syscallarg(netbsd32_intp) uaddr2;
59 		syscallarg(int) val2;
60 		syscallarg(int) val3;
61 	} */
62 	struct netbsd32_timespec ts32;
63 	struct timespec ts, *tsp;
64 	int error;
65 
66 	/*
67 	 * Copy in the timeout argument, if specified.
68 	 */
69 	if (SCARG_P32(uap, timeout)) {
70 		error = copyin(SCARG_P32(uap, timeout), &ts32, sizeof(ts32));
71 		if (error)
72 			return error;
73 		netbsd32_to_timespec(&ts32, &ts);
74 		tsp = &ts;
75 	} else {
76 		tsp = NULL;
77 	}
78 
79 	return do_futex(SCARG_P32(uap, uaddr), SCARG(uap, op),
80 	    SCARG(uap, val), tsp, SCARG_P32(uap, uaddr2), SCARG(uap, val2),
81 	    SCARG(uap, val3), retval);
82 }
83 
84 int
netbsd32___futex_set_robust_list(struct lwp * l,const struct netbsd32___futex_set_robust_list_args * uap,register_t * retval)85 netbsd32___futex_set_robust_list(struct lwp *l,
86     const struct netbsd32___futex_set_robust_list_args *uap, register_t *retval)
87 {
88 	/* {
89 		syscallarg(netbsd32_voidp) head;
90 		syscallarg(netbsd32_size_t) len;
91 	} */
92 	void *head = SCARG_P32(uap, head);
93 
94 	if (SCARG(uap, len) != _FUTEX_ROBUST_HEAD_SIZE32)
95 		return EINVAL;
96 	if ((uintptr_t)head % sizeof(uint32_t))
97 		return EINVAL;
98 
99 	l->l_robust_head = (uintptr_t)head;
100 
101 	return 0;
102 }
103 
104 int
netbsd32___futex_get_robust_list(struct lwp * l,const struct netbsd32___futex_get_robust_list_args * uap,register_t * retval)105 netbsd32___futex_get_robust_list(struct lwp *l,
106     const struct netbsd32___futex_get_robust_list_args *uap, register_t *retval)
107 {
108 	/* {
109 		syscallarg(lwpid_t) lwpid;
110 		syscallarg(netbsd32_voidp) headp;
111 		syscallarg(netbsd32_size_tp) lenp;
112 	} */
113 	void *head;
114 	const netbsd32_size_t len = _FUTEX_ROBUST_HEAD_SIZE32;
115 	netbsd32_voidp head32;
116 	int error;
117 
118 	error = futex_robust_head_lookup(l, SCARG(uap, lwpid), &head);
119 	if (error)
120 		return error;
121 
122 	head32.i32 = (uintptr_t)head;
123 	if (NETBSD32PTR64(head32) != head)
124 		return EFAULT;
125 
126 	/* Copy out the head pointer and the head structure length. */
127 	/* (N.B.: "headp" is actually a "void **". */
128 	error = copyout(&head32, SCARG_P32(uap, headp), sizeof(head32));
129 	if (__predict_true(error == 0)) {
130 		error = copyout(&len, SCARG_P32(uap, lenp), sizeof(len));
131 	}
132 
133 	return error;
134 }
135