netbsd32_sem.c revision 1.9
1/*	$NetBSD: netbsd32_sem.c,v 1.9 2012/03/08 21:59:29 joerg 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.9 2012/03/08 21:59:29 joerg Exp $");
31
32#ifdef _KERNEL_OPT
33#include "opt_posix.h"
34#endif
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/dirent.h>
41#include <sys/ksem.h>
42#include <sys/mount.h>
43#include <sys/proc.h>
44#include <sys/syscallargs.h>
45
46#include <compat/netbsd32/netbsd32.h>
47#include <compat/netbsd32/netbsd32_syscallargs.h>
48#include <compat/netbsd32/netbsd32_conv.h>
49
50static int
51netbsd32_ksem_copyout(const void *src, void *dst, size_t size)
52{
53	const intptr_t *idp = src;
54	netbsd32_intptr_t id32, *outidp = dst;
55
56	KASSERT(size == sizeof(intptr_t));
57
58	/* Returning a kernel pointer to userspace sucks badly :-( */
59	id32 = (netbsd32_intptr_t)*idp;
60	return copyout(&id32, outidp, sizeof(id32));
61}
62
63int
64netbsd32__ksem_init(struct lwp *l, const struct netbsd32__ksem_init_args *uap, register_t *retval)
65{
66	/* {
67		syscallarg(unsigned int) value;
68		syscallarg(netbsd32_semidp_t) idp;
69	} */
70
71	return do_ksem_init(l, SCARG(uap, value),
72	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
73}
74
75int
76netbsd32__ksem_open(struct lwp *l, const struct netbsd32__ksem_open_args *uap, register_t *retval)
77{
78	/* {
79		syscallarg(const netbsd32_charp) name;
80		syscallarg(int) oflag;
81		syscallarg(mode_t) mode;
82		syscallarg(unsigned int) value;
83		syscallarg(netbsd32_semidp_t) idp;
84	} */
85
86	return do_ksem_open(l, SCARG_P32(uap, name),
87	    SCARG(uap, oflag), SCARG(uap, mode), SCARG(uap, value),
88	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
89}
90
91int
92netbsd32__ksem_unlink(struct lwp *l, const struct netbsd32__ksem_unlink_args *uap, register_t *retval)
93{
94	/* {
95		syscallarg(const netbsd32_charp) name;
96	} */
97	struct sys__ksem_unlink_args ua;
98
99	NETBSD32TOP_UAP(name, const char);
100	return sys__ksem_unlink(l, &ua, retval);
101}
102
103int
104netbsd32__ksem_close(struct lwp *l, const struct netbsd32__ksem_close_args *uap, register_t *retval)
105{
106	/* {
107		syscallarg(netbsd32_intptr_t) id;
108	} */
109	struct sys__ksem_close_args ua;
110
111	NETBSD32TOX_UAP(id, intptr_t);
112	return sys__ksem_close(l, &ua, retval);
113}
114
115int
116netbsd32__ksem_post(struct lwp *l, const struct netbsd32__ksem_post_args *uap, register_t *retval)
117{
118	/* {
119		syscallarg(netbsd32_intptr_t) id;
120	} */
121	struct sys__ksem_post_args ua;
122
123	NETBSD32TOX_UAP(id, intptr_t);
124	return sys__ksem_post(l, &ua, retval);
125}
126
127int
128netbsd32__ksem_wait(struct lwp *l, const struct netbsd32__ksem_wait_args *uap, register_t *retval)
129{
130	/* {
131		syscallarg(netbsd32_intptr_t) id;
132	} */
133
134	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
135}
136
137int
138netbsd32__ksem_trywait(struct lwp *l, const struct netbsd32__ksem_trywait_args *uap, register_t *retval)
139{
140	/* {
141		syscallarg(netbsd32_intptr_t) id;
142	} */
143
144	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
145}
146
147int
148netbsd32__ksem_timedwait(struct lwp *l, const struct netbsd32__ksem_timedwait_args *uap,
149    register_t *retval)
150{
151	/* {
152		intptr_t id;
153		const netbsd32_timespecp_t abstime;
154	} */
155	struct netbsd32_timespec ts32;
156	struct timespec ts;
157	intptr_t id;
158	int error;
159
160	id = SCARG(uap, id);
161
162	error = copyin(SCARG_P32(uap, abstime), &ts32, sizeof(ts32));
163	if (error != 0)
164		return error;
165	netbsd32_to_timespec(&ts32, &ts);
166
167	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
168		return EINVAL;
169
170	error = do_ksem_wait(l, id, false, &ts);
171	if (error == EWOULDBLOCK)
172		error = ETIMEDOUT;
173	return error;
174}
175
176int
177netbsd32__ksem_destroy(struct lwp *l, const struct netbsd32__ksem_destroy_args *uap, register_t *retval)
178{
179	/* {
180		syscallarg(netbsd32_intptr_t) id;
181	} */
182	struct sys__ksem_destroy_args ua;
183
184	NETBSD32TOX_UAP(id, intptr_t);
185	return sys__ksem_destroy(l, &ua, retval);
186}
187
188int
189netbsd32__ksem_getvalue(struct lwp *l, const struct netbsd32__ksem_getvalue_args *uap, register_t *retval)
190{
191	/* {
192		syscallarg(netbsd32_intptr_t) id;
193		syscallarg(netbsd32_intp) value;
194	} */
195	struct sys__ksem_getvalue_args ua;
196
197	NETBSD32TOX_UAP(id, intptr_t);
198	NETBSD32TOP_UAP(value, unsigned int);
199	return sys__ksem_getvalue(l, &ua, retval);
200}
201