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