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