root/sys/arch/amd64/amd64/sys_machdep.c
/*      $OpenBSD: sys_machdep.c,v 1.21 2025/10/30 18:23:30 jca Exp $    */
/*      $NetBSD: sys_machdep.c,v 1.1 2003/04/26 18:39:32 fvdl Exp $     */

/*-
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Charles M. Hannum.
 *
 * 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/param.h>
#include <sys/systm.h>
#include <sys/proc.h>

#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <sys/pledge.h>

#include <machine/psl.h>
#include <machine/sysarch.h>

extern struct vm_map *kernel_map;

int amd64_iopl(struct proc *, void *, register_t *);

#ifdef APERTURE
extern int allowaperture;
#endif

int
amd64_iopl(struct proc *p, void *args, register_t *retval)
{
        int error;
        struct trapframe *tf = p->p_md.md_regs;
        struct amd64_iopl_args ua;

        if ((error = suser(p)) != 0)
                return error;

#ifdef APERTURE
        if (!allowaperture && securelevel > 0)
                return EPERM;
#else
        if (securelevel > 0)
                return EPERM;
#endif

        if ((error = copyin(args, &ua, sizeof(ua))) != 0)
                return error;

        if (ua.iopl)
                tf->tf_rflags |= PSL_IOPL;
        else
                tf->tf_rflags &= ~PSL_IOPL;

        return 0;
}

int
sys_sysarch(struct proc *p, void *v, register_t *retval)
{
        struct sys_sysarch_args /* {
                syscallarg(int) op;
                syscallarg(void *) parms;
        } */ *uap = v;
        int error = 0;

        if ((p->p_p->ps_flags & PS_PLEDGE))
                return pledge_fail(p, EINVAL, 0);

        switch(SCARG(uap, op)) {
        case AMD64_IOPL: 
                error = amd64_iopl(p, SCARG(uap, parms), retval);
                break;

        default:
                error = EINVAL;
                break;
        }
        return (error);
}