xref: /netbsd/sys/compat/netbsd32/netbsd32_event.c (revision 4717fc23)
1 /*	$NetBSD: netbsd32_event.c,v 1.14 2023/07/29 12:48:15 rin Exp $	*/
2 
3 /*
4  *  Copyright (c) 2005 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_event.c,v 1.14 2023/07/29 12:48:15 rin Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/select.h>
38 #include <sys/event.h>
39 #include <sys/eventvar.h>
40 #include <sys/proc.h>
41 #include <sys/dirent.h>
42 
43 #include <compat/netbsd32/netbsd32.h>
44 #include <compat/netbsd32/netbsd32_syscall.h>
45 #include <compat/netbsd32/netbsd32_syscallargs.h>
46 #include <compat/netbsd32/netbsd32_conv.h>
47 #include <compat/netbsd32/netbsd32_event.h>
48 
49 int
netbsd32_kevent_fetch_timeout(const void * src,void * dest,size_t length)50 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
51 {
52 	struct netbsd32_timespec ts32;
53 	int error;
54 
55 	KASSERT(length == sizeof(struct timespec));
56 
57 	error = copyin(src, &ts32, sizeof(ts32));
58 	if (error)
59 		return error;
60 	netbsd32_to_timespec(&ts32, (struct timespec *)dest);
61 	return 0;
62 }
63 
64 static int
netbsd32_kevent_fetch_changes(void * ctx,const struct kevent * changelist,struct kevent * changes,size_t index,int n)65 netbsd32_kevent_fetch_changes(void *ctx, const struct kevent *changelist,
66     struct kevent *changes, size_t index, int n)
67 {
68 	const struct netbsd32_kevent *src =
69 	    (const struct netbsd32_kevent *)changelist;
70 	struct netbsd32_kevent *kev32, *changes32 = ctx;
71 	int error, i;
72 
73 	error = copyin(src + index, changes32, n * sizeof(*changes32));
74 	if (error)
75 		return error;
76 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
77 		netbsd32_to_kevent(kev32, changes);
78 	return 0;
79 }
80 
81 static int
netbsd32_kevent_put_events(void * ctx,struct kevent * events,struct kevent * eventlist,size_t index,int n)82 netbsd32_kevent_put_events(void *ctx, struct kevent *events,
83     struct kevent *eventlist, size_t index, int n)
84 {
85 	struct netbsd32_kevent *kev32, *events32 = ctx;
86 	int i;
87 
88 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
89 		netbsd32_from_kevent(events, kev32);
90 	kev32 = ((struct netbsd32_kevent *)eventlist) + index;
91 	return  copyout(events32, kev32, n * sizeof(*events32));
92 }
93 
94 int
netbsd32_kevent1(register_t * retval,int fd,const netbsd32_keventp_t changelist,netbsd32_size_t nchanges,netbsd32_keventp_t eventlist,netbsd32_size_t nevents,netbsd32_timespecp_t timeout,struct kevent_ops * kops)95 netbsd32_kevent1(register_t *retval, int fd,
96     const netbsd32_keventp_t changelist, netbsd32_size_t nchanges,
97     netbsd32_keventp_t eventlist, netbsd32_size_t nevents,
98     netbsd32_timespecp_t timeout, struct kevent_ops *kops)
99 {
100 	const size_t maxalloc = KQ_NEVENTS;
101 	int error;
102 
103 	kops->keo_private =
104 	    kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);
105 
106 	error = kevent1(retval, fd, NETBSD32PTR64(changelist), nchanges,
107 	    NETBSD32PTR64(eventlist), nevents, NETBSD32PTR64(timeout), kops);
108 
109 	kmem_free(kops->keo_private, maxalloc * sizeof(struct netbsd32_kevent));
110 	return error;
111 }
112 
113 int
netbsd32___kevent100(struct lwp * l,const struct netbsd32___kevent100_args * uap,register_t * retval)114 netbsd32___kevent100(struct lwp *l,
115     const struct netbsd32___kevent100_args *uap, register_t *retval)
116 {
117 	/* {
118 		syscallarg(int) fd;
119 		syscallarg(const netbsd32_keventp_t) changelist;
120 		syscallarg(netbsd32_size_t) nchanges;
121 		syscallarg(netbsd32_keventp_t) eventlist;
122 		syscallarg(netbsd32_size_t) nevents;
123 		syscallarg(netbsd32_timespecp_t) timeout;
124 	} */
125 	struct kevent_ops kops = {
126 		.keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
127 		.keo_fetch_changes = netbsd32_kevent_fetch_changes,
128 		.keo_put_events = netbsd32_kevent_put_events,
129 	};
130 
131 	return netbsd32_kevent1(retval, SCARG(uap, fd),
132 	    SCARG(uap, changelist), SCARG(uap, nchanges),
133 	    SCARG(uap, eventlist), SCARG(uap, nevents),
134 	    SCARG(uap, timeout), &kops);
135 }
136