/*-1* Copyright (c) 2017 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* This software was developed by BAE Systems, the University of Cambridge5* Computer Laboratory, and Memorial University under DARPA/AFRL contract6* FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing7* (TC) research program.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/param.h>32#include <sys/systm.h>33#include <sys/capsicum.h>34#include <sys/conf.h>35#include <sys/kernel.h>36#include <sys/malloc.h>37#include <sys/module.h>38#include <sys/file.h>39#include <sys/proc.h>4041#include <machine/sgx.h>42#include <machine/../linux/linux.h>43#include <machine/../linux/linux_proto.h>44#include <compat/linux/linux_ioctl.h>4546#include <amd64/sgx/sgxvar.h>4748#include <sys/ioccom.h>4950#define SGX_LINUX_IOCTL_MIN (SGX_IOC_ENCLAVE_CREATE & 0xffff)51#define SGX_LINUX_IOCTL_MAX (SGX_IOC_ENCLAVE_INIT & 0xffff)5253static int54sgx_linux_ioctl(struct thread *td, struct linux_ioctl_args *args)55{56uint8_t data[SGX_IOCTL_MAX_DATA_LEN];57cap_rights_t rights;58struct file *fp;59u_long cmd;60int error;61int len;6263error = fget(td, args->fd, cap_rights_init_one(&rights, CAP_IOCTL),64&fp);65if (error != 0)66return (error);6768cmd = args->cmd;6970args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);71if ((cmd & LINUX_IOC_IN) != 0)72args->cmd |= IOC_IN;73if ((cmd & LINUX_IOC_OUT) != 0)74args->cmd |= IOC_OUT;7576len = IOCPARM_LEN(cmd);77if (len > SGX_IOCTL_MAX_DATA_LEN) {78error = EINVAL;79goto out;80}8182if ((cmd & LINUX_IOC_IN) != 0) {83error = copyin((void *)args->arg, data, len);84if (error != 0)85goto out;86}8788error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td);89out:90fdrop(fp, td);91return (error);92}9394static struct linux_ioctl_handler sgx_linux_handler = {95sgx_linux_ioctl,96SGX_LINUX_IOCTL_MIN,97SGX_LINUX_IOCTL_MAX,98};99100SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,101linux_ioctl_register_handler, &sgx_linux_handler);102SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,103linux_ioctl_unregister_handler, &sgx_linux_handler);104105static int106sgx_linux_modevent(module_t mod, int type, void *data)107{108109return (0);110}111112DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);113MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);114115116