summaryrefslogtreecommitdiff
path: root/sys/compat/netbsd32/netbsd32_sem.c
blob: cbf197948cd4d06576f380b9f65c9bb3cd35bce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*	$NetBSD: netbsd32_sem.c,v 1.12 2019/02/03 03:20:23 thorpej Exp $	*/

/*
 *  Copyright (c) 2006 The NetBSD Foundation.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_sem.c,v 1.12 2019/02/03 03:20:23 thorpej Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/dirent.h>
#include <sys/condvar.h>
#include <sys/ksem.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/syscallargs.h>

#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_syscallargs.h>
#include <compat/netbsd32/netbsd32_conv.h>

static int
netbsd32_ksem_copyin(const void *src, void *dst, size_t size)
{
	intptr_t *argp = dst;
	netbsd32_intptr_t arg32;
	int error;

	KASSERT(size == sizeof(intptr_t));

	if ((error = copyin(src, &arg32, sizeof(arg32))) != 0)
		return error;
	*argp = arg32;
	return 0;
}

static int
netbsd32_ksem_copyout(const void *src, void *dst, size_t size)
{
	const intptr_t *idp = src;
	netbsd32_intptr_t id32, *outidp = dst;

	KASSERT(size == sizeof(intptr_t));

	/* Returning a kernel pointer to userspace sucks badly :-( */
	/* (Luckily, it's not actually a pointer. --thorpej */
	id32 = (netbsd32_intptr_t)*idp;
	return copyout(&id32, outidp, sizeof(id32));
}

int
netbsd32__ksem_init(struct lwp *l, const struct netbsd32__ksem_init_args *uap, register_t *retval)
{
	/* {
		syscallarg(unsigned int) value;
		syscallarg(netbsd32_semidp_t) idp;
	} */

	return do_ksem_init(l, SCARG(uap, value),
	    SCARG_P32(uap, idp), netbsd32_ksem_copyin, netbsd32_ksem_copyout);
}

int
netbsd32__ksem_open(struct lwp *l, const struct netbsd32__ksem_open_args *uap, register_t *retval)
{
	/* {
		syscallarg(const netbsd32_charp) name;
		syscallarg(int) oflag;
		syscallarg(mode_t) mode;
		syscallarg(unsigned int) value;
		syscallarg(netbsd32_semidp_t) idp;
	} */

	return do_ksem_open(l, SCARG_P32(uap, name),
	    SCARG(uap, oflag), SCARG(uap, mode), SCARG(uap, value),
	    SCARG_P32(uap, idp), netbsd32_ksem_copyout);
}

int
netbsd32__ksem_unlink(struct lwp *l, const struct netbsd32__ksem_unlink_args *uap, register_t *retval)
{
	/* {
		syscallarg(const netbsd32_charp) name;
	} */
	struct sys__ksem_unlink_args ua;

	NETBSD32TOP_UAP(name, const char);
	return sys__ksem_unlink(l, &ua, retval);
}

int
netbsd32__ksem_close(struct lwp *l, const struct netbsd32__ksem_close_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
	} */
	struct sys__ksem_close_args ua;

	NETBSD32TOX_UAP(id, intptr_t);
	return sys__ksem_close(l, &ua, retval);
}

int
netbsd32__ksem_post(struct lwp *l, const struct netbsd32__ksem_post_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
	} */
	struct sys__ksem_post_args ua;

	NETBSD32TOX_UAP(id, intptr_t);
	return sys__ksem_post(l, &ua, retval);
}

int
netbsd32__ksem_wait(struct lwp *l, const struct netbsd32__ksem_wait_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
	} */

	return do_ksem_wait(l, SCARG(uap, id), false, NULL);
}

int
netbsd32__ksem_trywait(struct lwp *l, const struct netbsd32__ksem_trywait_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
	} */

	return do_ksem_wait(l, SCARG(uap, id), true, NULL);
}

int
netbsd32__ksem_timedwait(struct lwp *l, const struct netbsd32__ksem_timedwait_args *uap,
    register_t *retval)
{
	/* {
		intptr_t id;
		const netbsd32_timespecp_t abstime;
	} */
	struct netbsd32_timespec ts32;
	struct timespec ts;
	intptr_t id;
	int error;

	id = SCARG(uap, id);

	error = copyin(SCARG_P32(uap, abstime), &ts32, sizeof(ts32));
	if (error != 0)
		return error;
	netbsd32_to_timespec(&ts32, &ts);

	if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
		return EINVAL;

	error = do_ksem_wait(l, id, false, &ts);
	if (error == EWOULDBLOCK)
		error = ETIMEDOUT;
	return error;
}

int
netbsd32__ksem_destroy(struct lwp *l, const struct netbsd32__ksem_destroy_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
	} */
	struct sys__ksem_destroy_args ua;

	NETBSD32TOX_UAP(id, intptr_t);
	return sys__ksem_destroy(l, &ua, retval);
}

int
netbsd32__ksem_getvalue(struct lwp *l, const struct netbsd32__ksem_getvalue_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_intptr_t) id;
		syscallarg(netbsd32_intp) value;
	} */
	struct sys__ksem_getvalue_args ua;

	NETBSD32TOX_UAP(id, intptr_t);
	NETBSD32TOP_UAP(value, unsigned int);
	return sys__ksem_getvalue(l, &ua, retval);
}