1*1a625c58Sad /* $NetBSD: kern_50.c,v 1.3 2020/01/29 15:47:51 ad Exp $ */
210a2075cSnjoly
310a2075cSnjoly /*-
4*1a625c58Sad * Copyright (c) 2008, 2009, 2020 The NetBSD Foundation, Inc.
510a2075cSnjoly * All rights reserved.
610a2075cSnjoly *
710a2075cSnjoly * This code is derived from software contributed to The NetBSD Foundation
810a2075cSnjoly * by Christos Zoulas.
910a2075cSnjoly *
1010a2075cSnjoly * Redistribution and use in source and binary forms, with or without
1110a2075cSnjoly * modification, are permitted provided that the following conditions
1210a2075cSnjoly * are met:
1310a2075cSnjoly * 1. Redistributions of source code must retain the above copyright
1410a2075cSnjoly * notice, this list of conditions and the following disclaimer.
1510a2075cSnjoly * 2. Redistributions in binary form must reproduce the above copyright
1610a2075cSnjoly * notice, this list of conditions and the following disclaimer in the
1710a2075cSnjoly * documentation and/or other materials provided with the distribution.
1810a2075cSnjoly *
1910a2075cSnjoly * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2010a2075cSnjoly * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2110a2075cSnjoly * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2210a2075cSnjoly * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2310a2075cSnjoly * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2410a2075cSnjoly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2510a2075cSnjoly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2610a2075cSnjoly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2710a2075cSnjoly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2810a2075cSnjoly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2910a2075cSnjoly * POSSIBILITY OF SUCH DAMAGE.
3010a2075cSnjoly */
3110a2075cSnjoly #include <sys/cdefs.h>
32*1a625c58Sad __KERNEL_RCSID(0, "$NetBSD: kern_50.c,v 1.3 2020/01/29 15:47:51 ad Exp $");
33cc17ee2eSpgoyette
34cc17ee2eSpgoyette #if defined(_KERNEL_OPT)
35cc17ee2eSpgoyette #include "opt_compat_netbsd.h"
36cc17ee2eSpgoyette #endif
3710a2075cSnjoly
3810a2075cSnjoly #include <sys/param.h>
3910a2075cSnjoly #include <sys/lwp.h>
4010a2075cSnjoly #include <sys/proc.h>
41cc17ee2eSpgoyette #include <sys/syscall.h>
42cc17ee2eSpgoyette #include <sys/syscallvar.h>
4310a2075cSnjoly #include <sys/syscallargs.h>
4410a2075cSnjoly
4510a2075cSnjoly #include <compat/sys/resource.h>
4610a2075cSnjoly #include <compat/sys/time.h>
4710a2075cSnjoly
48cc17ee2eSpgoyette #include <compat/common/compat_mod.h>
49cc17ee2eSpgoyette
50cc17ee2eSpgoyette static const struct syscall_package kern_50_syscalls[] = {
51cc17ee2eSpgoyette { SYS_compat_50__lwp_park, 0, (sy_call_t *)compat_50_sys__lwp_park },
52cc17ee2eSpgoyette { SYS_compat_50___sigtimedwait, 0,
53cc17ee2eSpgoyette (sy_call_t *)compat_50_sys___sigtimedwait },
54cc17ee2eSpgoyette { SYS_compat_50_wait4, 0, (sy_call_t *)compat_50_sys_wait4 },
55cc17ee2eSpgoyette { 0, 0, NULL }
56cc17ee2eSpgoyette };
57cc17ee2eSpgoyette
5810a2075cSnjoly int
compat_50_sys__lwp_park(struct lwp * l,const struct compat_50_sys__lwp_park_args * uap,register_t * retval)5910a2075cSnjoly compat_50_sys__lwp_park(struct lwp *l,
6010a2075cSnjoly const struct compat_50_sys__lwp_park_args *uap, register_t *retval)
6110a2075cSnjoly {
6210a2075cSnjoly /* {
6310a2075cSnjoly syscallarg(const struct timespec50 *) ts;
6410a2075cSnjoly syscallarg(lwpid_t) unpark;
6510a2075cSnjoly syscallarg(const void *) hint;
6610a2075cSnjoly syscallarg(const void *) unparkhint;
6710a2075cSnjoly } */
6810a2075cSnjoly struct timespec ts, *tsp;
6910a2075cSnjoly struct timespec50 ts50;
7010a2075cSnjoly int error;
7110a2075cSnjoly
7210a2075cSnjoly if (SCARG(uap, ts) == NULL)
7310a2075cSnjoly tsp = NULL;
7410a2075cSnjoly else {
7510a2075cSnjoly error = copyin(SCARG(uap, ts), &ts50, sizeof(ts50));
7610a2075cSnjoly if (error != 0)
7710a2075cSnjoly return error;
7810a2075cSnjoly timespec50_to_timespec(&ts50, &ts);
7910a2075cSnjoly tsp = &ts;
8010a2075cSnjoly }
8110a2075cSnjoly
8210a2075cSnjoly if (SCARG(uap, unpark) != 0) {
83*1a625c58Sad error = lwp_unpark(&SCARG(uap, unpark), 1);
8410a2075cSnjoly if (error != 0)
8510a2075cSnjoly return error;
8610a2075cSnjoly }
8710a2075cSnjoly
88*1a625c58Sad return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
8910a2075cSnjoly }
9010a2075cSnjoly
9110a2075cSnjoly static int
tscopyin(const void * u,void * s,size_t len)9210a2075cSnjoly tscopyin(const void *u, void *s, size_t len)
9310a2075cSnjoly {
9410a2075cSnjoly struct timespec50 ts50;
9510a2075cSnjoly int error;
9610a2075cSnjoly
9710a2075cSnjoly KASSERT(len == sizeof(struct timespec));
9810a2075cSnjoly error = copyin(u, &ts50, sizeof(ts50));
9910a2075cSnjoly if (error)
10010a2075cSnjoly return error;
10110a2075cSnjoly timespec50_to_timespec(&ts50, s);
10210a2075cSnjoly return 0;
10310a2075cSnjoly }
10410a2075cSnjoly
10510a2075cSnjoly static int
tscopyout(const void * s,void * u,size_t len)10610a2075cSnjoly tscopyout(const void *s, void *u, size_t len)
10710a2075cSnjoly {
10810a2075cSnjoly struct timespec50 ts50;
10910a2075cSnjoly
11010a2075cSnjoly KASSERT(len == sizeof(struct timespec));
11110a2075cSnjoly timespec_to_timespec50(s, &ts50);
11210a2075cSnjoly return copyout(&ts50, u, sizeof(ts50));
11310a2075cSnjoly }
11410a2075cSnjoly
11510a2075cSnjoly int
compat_50_sys___sigtimedwait(struct lwp * l,const struct compat_50_sys___sigtimedwait_args * uap,register_t * retval)11610a2075cSnjoly compat_50_sys___sigtimedwait(struct lwp *l,
11710a2075cSnjoly const struct compat_50_sys___sigtimedwait_args *uap, register_t *retval)
11810a2075cSnjoly {
11910a2075cSnjoly int res;
12010a2075cSnjoly
12110a2075cSnjoly res = sigtimedwait1(l,
12210a2075cSnjoly (const struct sys_____sigtimedwait50_args *)uap, retval, copyin,
12310a2075cSnjoly copyout, tscopyin, tscopyout);
12410a2075cSnjoly if (!res)
12510a2075cSnjoly *retval = 0; /* XXX NetBSD<=5 was not POSIX compliant */
12610a2075cSnjoly return res;
12710a2075cSnjoly }
12810a2075cSnjoly
12910a2075cSnjoly int
compat_50_sys_wait4(struct lwp * l,const struct compat_50_sys_wait4_args * uap,register_t * retval)13010a2075cSnjoly compat_50_sys_wait4(struct lwp *l, const struct compat_50_sys_wait4_args *uap,
13110a2075cSnjoly register_t *retval)
13210a2075cSnjoly {
13310a2075cSnjoly /* {
13410a2075cSnjoly syscallarg(int) pid;
13510a2075cSnjoly syscallarg(int *) status;
13610a2075cSnjoly syscallarg(int) options;
13710a2075cSnjoly syscallarg(struct rusage50 *) rusage;
13810a2075cSnjoly } */
13910a2075cSnjoly int status, error, pid = SCARG(uap, pid);
14010a2075cSnjoly struct rusage50 ru50;
14110a2075cSnjoly struct rusage ru;
14210a2075cSnjoly
14310a2075cSnjoly error = do_sys_wait(&pid, &status, SCARG(uap, options),
14410a2075cSnjoly SCARG(uap, rusage) != NULL ? &ru : NULL);
14510a2075cSnjoly
14610a2075cSnjoly retval[0] = pid;
14710a2075cSnjoly if (pid == 0)
14810a2075cSnjoly return error;
14910a2075cSnjoly
15010a2075cSnjoly if (SCARG(uap, rusage)) {
15110a2075cSnjoly rusage_to_rusage50(&ru, &ru50);
15210a2075cSnjoly error = copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
15310a2075cSnjoly }
15410a2075cSnjoly
15510a2075cSnjoly if (error == 0 && SCARG(uap, status))
15610a2075cSnjoly error = copyout(&status, SCARG(uap, status), sizeof(status));
15710a2075cSnjoly
15810a2075cSnjoly return error;
15910a2075cSnjoly }
160cc17ee2eSpgoyette
161cc17ee2eSpgoyette int
kern_50_init(void)162cc17ee2eSpgoyette kern_50_init(void)
163cc17ee2eSpgoyette {
164cc17ee2eSpgoyette
165cc17ee2eSpgoyette return syscall_establish(NULL, kern_50_syscalls);
166cc17ee2eSpgoyette }
167cc17ee2eSpgoyette
168cc17ee2eSpgoyette int
kern_50_fini(void)169cc17ee2eSpgoyette kern_50_fini(void)
170cc17ee2eSpgoyette {
171cc17ee2eSpgoyette
172cc17ee2eSpgoyette return syscall_disestablish(NULL, kern_50_syscalls);
173cc17ee2eSpgoyette }
174