xref: /freebsd/sys/amd64/sgx/sgx_linux.c (revision 6bb132ba1e4e28cf1e713c8dcd77f2e8af7bde88)
12164af29SRuslan Bukin /*-
22164af29SRuslan Bukin  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
32164af29SRuslan Bukin  * All rights reserved.
42164af29SRuslan Bukin  *
52164af29SRuslan Bukin  * This software was developed by BAE Systems, the University of Cambridge
62164af29SRuslan Bukin  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
72164af29SRuslan Bukin  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
82164af29SRuslan Bukin  * (TC) research program.
92164af29SRuslan Bukin  *
102164af29SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
112164af29SRuslan Bukin  * modification, are permitted provided that the following conditions
122164af29SRuslan Bukin  * are met:
132164af29SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
142164af29SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
152164af29SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
162164af29SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
172164af29SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
182164af29SRuslan Bukin  *
192164af29SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
202164af29SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212164af29SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222164af29SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
232164af29SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242164af29SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252164af29SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262164af29SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272164af29SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282164af29SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292164af29SRuslan Bukin  * SUCH DAMAGE.
302164af29SRuslan Bukin  */
312164af29SRuslan Bukin 
322164af29SRuslan Bukin #include <sys/param.h>
332164af29SRuslan Bukin #include <sys/systm.h>
342164af29SRuslan Bukin #include <sys/capsicum.h>
352164af29SRuslan Bukin #include <sys/conf.h>
362164af29SRuslan Bukin #include <sys/kernel.h>
37*6bb132baSBrooks Davis #include <sys/malloc.h>
382164af29SRuslan Bukin #include <sys/module.h>
392164af29SRuslan Bukin #include <sys/file.h>
402164af29SRuslan Bukin #include <sys/proc.h>
412164af29SRuslan Bukin 
422164af29SRuslan Bukin #include <machine/sgx.h>
432164af29SRuslan Bukin #include <machine/../linux/linux.h>
442164af29SRuslan Bukin #include <machine/../linux/linux_proto.h>
452164af29SRuslan Bukin #include <compat/linux/linux_ioctl.h>
462164af29SRuslan Bukin 
472164af29SRuslan Bukin #include <amd64/sgx/sgxvar.h>
482164af29SRuslan Bukin 
492164af29SRuslan Bukin #include <sys/ioccom.h>
502164af29SRuslan Bukin 
512164af29SRuslan Bukin #define	SGX_LINUX_IOCTL_MIN	(SGX_IOC_ENCLAVE_CREATE & 0xffff)
522164af29SRuslan Bukin #define	SGX_LINUX_IOCTL_MAX	(SGX_IOC_ENCLAVE_INIT & 0xffff)
532164af29SRuslan Bukin 
542164af29SRuslan Bukin static int
552164af29SRuslan Bukin sgx_linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
562164af29SRuslan Bukin {
572164af29SRuslan Bukin 	uint8_t data[SGX_IOCTL_MAX_DATA_LEN];
582164af29SRuslan Bukin 	cap_rights_t rights;
592164af29SRuslan Bukin 	struct file *fp;
602164af29SRuslan Bukin 	u_long cmd;
612164af29SRuslan Bukin 	int error;
622164af29SRuslan Bukin 	int len;
632164af29SRuslan Bukin 
646b3a9a0fSMateusz Guzik 	error = fget(td, args->fd, cap_rights_init_one(&rights, CAP_IOCTL),
656b3a9a0fSMateusz Guzik 	    &fp);
662164af29SRuslan Bukin 	if (error != 0)
672164af29SRuslan Bukin 		return (error);
682164af29SRuslan Bukin 
692164af29SRuslan Bukin 	cmd = args->cmd;
702164af29SRuslan Bukin 
712164af29SRuslan Bukin 	args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
728cbc89c7SMark Johnston 	if ((cmd & LINUX_IOC_IN) != 0)
732164af29SRuslan Bukin 		args->cmd |= IOC_IN;
748cbc89c7SMark Johnston 	if ((cmd & LINUX_IOC_OUT) != 0)
752164af29SRuslan Bukin 		args->cmd |= IOC_OUT;
762164af29SRuslan Bukin 
772164af29SRuslan Bukin 	len = IOCPARM_LEN(cmd);
782164af29SRuslan Bukin 	if (len > SGX_IOCTL_MAX_DATA_LEN) {
798cbc89c7SMark Johnston 		error = EINVAL;
808cbc89c7SMark Johnston 		goto out;
812164af29SRuslan Bukin 	}
822164af29SRuslan Bukin 
838cbc89c7SMark Johnston 	if ((cmd & LINUX_IOC_IN) != 0) {
842164af29SRuslan Bukin 		error = copyin((void *)args->arg, data, len);
858cbc89c7SMark Johnston 		if (error != 0)
868cbc89c7SMark Johnston 			goto out;
872164af29SRuslan Bukin 	}
882164af29SRuslan Bukin 
898cbc89c7SMark Johnston 	error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td);
908cbc89c7SMark Johnston out:
912164af29SRuslan Bukin 	fdrop(fp, td);
922164af29SRuslan Bukin 	return (error);
932164af29SRuslan Bukin }
942164af29SRuslan Bukin 
952164af29SRuslan Bukin static struct linux_ioctl_handler sgx_linux_handler = {
962164af29SRuslan Bukin 	sgx_linux_ioctl,
972164af29SRuslan Bukin 	SGX_LINUX_IOCTL_MIN,
982164af29SRuslan Bukin 	SGX_LINUX_IOCTL_MAX,
992164af29SRuslan Bukin };
1002164af29SRuslan Bukin 
1012164af29SRuslan Bukin SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
1022164af29SRuslan Bukin     linux_ioctl_register_handler, &sgx_linux_handler);
1032164af29SRuslan Bukin SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
1042164af29SRuslan Bukin     linux_ioctl_unregister_handler, &sgx_linux_handler);
1052164af29SRuslan Bukin 
1062164af29SRuslan Bukin static int
1072164af29SRuslan Bukin sgx_linux_modevent(module_t mod, int type, void *data)
1082164af29SRuslan Bukin {
1092164af29SRuslan Bukin 
1102164af29SRuslan Bukin 	return (0);
1112164af29SRuslan Bukin }
1122164af29SRuslan Bukin 
1132164af29SRuslan Bukin DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);
1142164af29SRuslan Bukin MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);
115