netbsd32_sem.c revision 1.11
1/*	$NetBSD: netbsd32_sem.c,v 1.11 2014/09/19 17:25:33 matt Exp $	*/
2
3/*
4 *  Copyright (c) 2006 The NetBSD Foundation.
5 *  All rights reserved.
6 *
7 *  Redistribution and use in source and binary forms, with or without
8 *  modification, are permitted provided that the following conditions
9 *  are met:
10 *  1. Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *  2. Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 *
16 *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 *  POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: netbsd32_sem.c,v 1.11 2014/09/19 17:25:33 matt Exp $");
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/dirent.h>
37#include <sys/condvar.h>
38#include <sys/ksem.h>
39#include <sys/mount.h>
40#include <sys/proc.h>
41#include <sys/syscallargs.h>
42
43#include <compat/netbsd32/netbsd32.h>
44#include <compat/netbsd32/netbsd32_syscallargs.h>
45#include <compat/netbsd32/netbsd32_conv.h>
46
47static int
48netbsd32_ksem_copyout(const void *src, void *dst, size_t size)
49{
50	const intptr_t *idp = src;
51	netbsd32_intptr_t id32, *outidp = dst;
52
53	KASSERT(size == sizeof(intptr_t));
54
55	/* Returning a kernel pointer to userspace sucks badly :-( */
56	id32 = (netbsd32_intptr_t)*idp;
57	return copyout(&id32, outidp, sizeof(id32));
58}
59
60int
61netbsd32__ksem_init(struct lwp *l, const struct netbsd32__ksem_init_args *uap, register_t *retval)
62{
63	/* {
64		syscallarg(unsigned int) value;
65		syscallarg(netbsd32_semidp_t) idp;
66	} */
67
68	return do_ksem_init(l, SCARG(uap, value),
69	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
70}
71
72int
73netbsd32__ksem_open(struct lwp *l, const struct netbsd32__ksem_open_args *uap, register_t *retval)
74{
75	/* {
76		syscallarg(const netbsd32_charp) name;
77		syscallarg(int) oflag;
78		syscallarg(mode_t) mode;
79		syscallarg(unsigned int) value;
80		syscallarg(netbsd32_semidp_t) idp;
81	} */
82
83	return do_ksem_open(l, SCARG_P32(uap, name),
84	    SCARG(uap, oflag), SCARG(uap, mode), SCARG(uap, value),
85	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
86}
87
88int
89netbsd32__ksem_unlink(struct lwp *l, const struct netbsd32__ksem_unlink_args *uap, register_t *retval)
90{
91	/* {
92		syscallarg(const netbsd32_charp) name;
93	} */
94	struct sys__ksem_unlink_args ua;
95
96	NETBSD32TOP_UAP(name, const char);
97	return sys__ksem_unlink(l, &ua, retval);
98}
99
100int
101netbsd32__ksem_close(struct lwp *l, const struct netbsd32__ksem_close_args *uap, register_t *retval)
102{
103	/* {
104		syscallarg(netbsd32_intptr_t) id;
105	} */
106	struct sys__ksem_close_args ua;
107
108	NETBSD32TOX_UAP(id, intptr_t);
109	return sys__ksem_close(l, &ua, retval);
110}
111
112int
113netbsd32__ksem_post(struct lwp *l, const struct netbsd32__ksem_post_args *uap, register_t *retval)
114{
115	/* {
116		syscallarg(netbsd32_intptr_t) id;
117	} */
118	struct sys__ksem_post_args ua;
119
120	NETBSD32TOX_UAP(id, intptr_t);
121	return sys__ksem_post(l, &ua, retval);
122}
123
124int
125netbsd32__ksem_wait(struct lwp *l, const struct netbsd32__ksem_wait_args *uap, register_t *retval)
126{
127	/* {
128		syscallarg(netbsd32_intptr_t) id;
129	} */
130
131	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
132}
133
134int
135netbsd32__ksem_trywait(struct lwp *l, const struct netbsd32__ksem_trywait_args *uap, register_t *retval)
136{
137	/* {
138		syscallarg(netbsd32_intptr_t) id;
139	} */
140
141	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
142}
143
144int
145netbsd32__ksem_timedwait(struct lwp *l, const struct netbsd32__ksem_timedwait_args *uap,
146    register_t *retval)
147{
148	/* {
149		intptr_t id;
150		const netbsd32_timespecp_t abstime;
151	} */
152	struct netbsd32_timespec ts32;
153	struct timespec ts;
154	intptr_t id;
155	int error;
156
157	id = SCARG(uap, id);
158
159	error = copyin(SCARG_P32(uap, abstime), &ts32, sizeof(ts32));
160	if (error != 0)
161		return error;
162	netbsd32_to_timespec(&ts32, &ts);
163
164	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
165		return EINVAL;
166
167	error = do_ksem_wait(l, id, false, &ts);
168	if (error == EWOULDBLOCK)
169		error = ETIMEDOUT;
170	return error;
171}
172
173int
174netbsd32__ksem_destroy(struct lwp *l, const struct netbsd32__ksem_destroy_args *uap, register_t *retval)
175{
176	/* {
177		syscallarg(netbsd32_intptr_t) id;
178	} */
179	struct sys__ksem_destroy_args ua;
180
181	NETBSD32TOX_UAP(id, intptr_t);
182	return sys__ksem_destroy(l, &ua, retval);
183}
184
185int
186netbsd32__ksem_getvalue(struct lwp *l, const struct netbsd32__ksem_getvalue_args *uap, register_t *retval)
187{
188	/* {
189		syscallarg(netbsd32_intptr_t) id;
190		syscallarg(netbsd32_intp) value;
191	} */
192	struct sys__ksem_getvalue_args ua;
193
194	NETBSD32TOX_UAP(id, intptr_t);
195	NETBSD32TOP_UAP(value, unsigned int);
196	return sys__ksem_getvalue(l, &ua, retval);
197}
198