/*      $NetBSD: netbsd32_event.c,v 1.14 2023/07/29 12:48:15 rin Exp $  */

/*
*  Copyright (c) 2005 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_event.c,v 1.14 2023/07/29 12:48:15 rin Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/select.h>
#include <sys/event.h>
#include <sys/eventvar.h>
#include <sys/proc.h>
#include <sys/dirent.h>

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

int
netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
{
       struct netbsd32_timespec ts32;
       int error;

       KASSERT(length == sizeof(struct timespec));

       error = copyin(src, &ts32, sizeof(ts32));
       if (error)
               return error;
       netbsd32_to_timespec(&ts32, (struct timespec *)dest);
       return 0;
}

static int
netbsd32_kevent_fetch_changes(void *ctx, const struct kevent *changelist,
   struct kevent *changes, size_t index, int n)
{
       const struct netbsd32_kevent *src =
           (const struct netbsd32_kevent *)changelist;
       struct netbsd32_kevent *kev32, *changes32 = ctx;
       int error, i;

       error = copyin(src + index, changes32, n * sizeof(*changes32));
       if (error)
               return error;
       for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
               netbsd32_to_kevent(kev32, changes);
       return 0;
}

static int
netbsd32_kevent_put_events(void *ctx, struct kevent *events,
   struct kevent *eventlist, size_t index, int n)
{
       struct netbsd32_kevent *kev32, *events32 = ctx;
       int i;

       for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
               netbsd32_from_kevent(events, kev32);
       kev32 = ((struct netbsd32_kevent *)eventlist) + index;
       return  copyout(events32, kev32, n * sizeof(*events32));
}

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)
{
       const size_t maxalloc = KQ_NEVENTS;
       int error;

       kops->keo_private =
           kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);

       error = kevent1(retval, fd, NETBSD32PTR64(changelist), nchanges,
           NETBSD32PTR64(eventlist), nevents, NETBSD32PTR64(timeout), kops);

       kmem_free(kops->keo_private, maxalloc * sizeof(struct netbsd32_kevent));
       return error;
}

int
netbsd32___kevent100(struct lwp *l,
   const struct netbsd32___kevent100_args *uap, register_t *retval)
{
       /* {
               syscallarg(int) fd;
               syscallarg(const netbsd32_keventp_t) changelist;
               syscallarg(netbsd32_size_t) nchanges;
               syscallarg(netbsd32_keventp_t) eventlist;
               syscallarg(netbsd32_size_t) nevents;
               syscallarg(netbsd32_timespecp_t) timeout;
       } */
       struct kevent_ops kops = {
               .keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
               .keo_fetch_changes = netbsd32_kevent_fetch_changes,
               .keo_put_events = netbsd32_kevent_put_events,
       };

       return netbsd32_kevent1(retval, SCARG(uap, fd),
           SCARG(uap, changelist), SCARG(uap, nchanges),
           SCARG(uap, eventlist), SCARG(uap, nevents),
           SCARG(uap, timeout), &kops);
}