Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_compat_100.c,v 1.4 2024/05/20 01:30:33 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software developed for The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_100.c,v 1.4 2024/05/20 01:30:33 christos Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/filedesc.h>
     37 #include <sys/event.h>
     38 #include <sys/eventvar.h>
     39 #include <sys/module.h>
     40 #include <sys/syscallvar.h>
     41 #include <sys/syscallargs.h>
     42 #include <sys/systm.h>
     43 
     44 #include <compat/netbsd32/netbsd32.h>
     45 #include <compat/netbsd32/netbsd32_syscall.h>
     46 #include <compat/netbsd32/netbsd32_syscallargs.h>
     47 #include <compat/netbsd32/netbsd32_conv.h>
     48 #include <compat/netbsd32/netbsd32_event.h>
     49 
     50 static void
     51 compat_100_netbsd32_to_kevent(const struct netbsd32_kevent100 *ke32,
     52     struct kevent *ke)
     53 {
     54 
     55 	memset(ke, 0, sizeof(*ke));
     56 	ke->ident = ke32->ident;
     57 	ke->filter = ke32->filter;
     58 	ke->flags = ke32->flags;
     59 	ke->fflags = ke32->fflags;
     60 	ke->data = ke32->data;
     61 	ke->udata = NETBSD32PTR64(ke32->udata);
     62 }
     63 
     64 static void
     65 compat_100_netbsd32_from_kevent(const struct kevent *ke,
     66     struct netbsd32_kevent100 *ke32)
     67 {
     68 
     69 	memset(ke32, 0, sizeof(*ke32));
     70 	ke32->ident = ke->ident;
     71 	ke32->filter = ke->filter;
     72 	ke32->flags = ke->flags;
     73 	ke32->fflags = ke->fflags;
     74 	ke32->data = ke->data;
     75 	NETBSD32PTR32(ke32->udata, ke->udata);
     76 }
     77 
     78 int
     79 compat_100_netbsd32_kevent_fetch_changes(void *ctx,
     80     const struct kevent *changelist, struct kevent *changes, size_t index,
     81     int n)
     82 {
     83 	const struct netbsd32_kevent100 *src =
     84 	    (const struct netbsd32_kevent100 *)changelist;
     85 	struct netbsd32_kevent100 *ke32, *changes32 = ctx;
     86 	int error, i;
     87 
     88 	error = copyin(src + index, changes32, n * sizeof(*changes32));
     89 	if (error)
     90 		return error;
     91 	for (i = 0, ke32 = changes32; i < n; i++, ke32++, changes++)
     92 		compat_100_netbsd32_to_kevent(ke32, changes);
     93 	return 0;
     94 }
     95 
     96 int
     97 compat_100_netbsd32_kevent_put_events(void *ctx, struct kevent *events,
     98     struct kevent *eventlist, size_t index, int n)
     99 {
    100 	struct netbsd32_kevent100 *ke32, *events32 = ctx;
    101 	int i;
    102 
    103 	for (i = 0, ke32 = events32; i < n; i++, ke32++, events++)
    104 		compat_100_netbsd32_from_kevent(events, ke32);
    105 	ke32 = ((struct netbsd32_kevent100 *)eventlist) + index;
    106 	return copyout(events32, ke32, n * sizeof(*events32));
    107 }
    108 
    109 int
    110 compat_100_netbsd32___kevent50(struct lwp *l,
    111     const struct compat_100_netbsd32___kevent50_args *uap, register_t *retval)
    112 {
    113 	/* {
    114 		syscallarg(int) fd;
    115 		syscallarg(const netbsd32_kevent100p_t) changelist;
    116 		syscallarg(netbsd32_size_t) nchanges;
    117 		syscallarg(netbsd32_kevent100p_t) eventlist;
    118 		syscallarg(netbsd32_size_t) nevents;
    119 		syscallarg(netbsd32_timespecp_t) timeout;
    120 	} */
    121 	struct kevent_ops netbsd32_kevent_ops = {
    122 		.keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
    123 		.keo_fetch_changes = compat_100_netbsd32_kevent_fetch_changes,
    124 		.keo_put_events = compat_100_netbsd32_kevent_put_events,
    125 	};
    126 
    127 	return netbsd32_kevent1(retval, SCARG(uap, fd),
    128 	    (netbsd32_keventp_t)SCARG(uap, changelist), SCARG(uap, nchanges),
    129 	    (netbsd32_keventp_t)SCARG(uap, eventlist), SCARG(uap, nevents),
    130 	    SCARG(uap, timeout), &netbsd32_kevent_ops);
    131 }
    132 
    133 int
    134 compat_100_netbsd32_dup3(struct lwp *l,
    135     const struct compat_100_netbsd32_dup3_args *uap, register_t *retval)
    136 {
    137 	/* {
    138 		syscallarg(int) from;
    139 		syscallarg(int) to;
    140 		syscallarg(int) flags;
    141 		syscallarg(const netbsd32_kevent100p_t) changelist;
    142 		syscallarg(netbsd32_size_t) nchanges;
    143 		syscallarg(netbsd32_kevent100p_t) eventlist;
    144 		syscallarg(netbsd32_size_t) nevents;
    145 		syscallarg(netbsd32_timespecp_t) timeout;
    146 	} */
    147 	return dodup(l, SCARG(uap, from), SCARG(uap, to), SCARG(uap, flags),
    148 	    retval);
    149 }
    150 
    151 static struct syscall_package compat_netbsd32_100_syscalls[] = {
    152 	{ NETBSD32_SYS_compat_100_netbsd32___kevent50, 0,
    153 	    (sy_call_t *)compat_100_netbsd32___kevent50 },
    154 	{ NETBSD32_SYS_compat_100_netbsd32_dup3, 0,
    155 	    (sy_call_t *)compat_100_netbsd32_dup3 },
    156 	{ 0, 0, NULL },
    157 };
    158 
    159 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_100, "compat_netbsd32,compat_100");
    160 
    161 static int
    162 compat_netbsd32_100_modcmd(modcmd_t cmd, void *arg)
    163 {
    164 
    165 	switch (cmd) {
    166 	case MODULE_CMD_INIT:
    167 		return syscall_establish(&emul_netbsd32,
    168 		    compat_netbsd32_100_syscalls);
    169 
    170 	case MODULE_CMD_FINI:
    171 		return syscall_disestablish(&emul_netbsd32,
    172 		    compat_netbsd32_100_syscalls);
    173 
    174 	default:
    175 		return ENOTTY;
    176 	}
    177 }
    178