xref: /freebsd/sys/amd64/sgx/sgx_linux.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by BAE Systems, the University of Cambridge
6  * Computer Laboratory, and Memorial University under DARPA/AFRL contract
7  * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
8  * (TC) research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/capsicum.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/file.h>
42 #include <sys/proc.h>
43 
44 #include <machine/sgx.h>
45 #include <machine/../linux/linux.h>
46 #include <machine/../linux/linux_proto.h>
47 #include <compat/linux/linux_ioctl.h>
48 
49 #include <amd64/sgx/sgxvar.h>
50 
51 #include <sys/ioccom.h>
52 
53 #define	SGX_LINUX_IOCTL_MIN	(SGX_IOC_ENCLAVE_CREATE & 0xffff)
54 #define	SGX_LINUX_IOCTL_MAX	(SGX_IOC_ENCLAVE_INIT & 0xffff)
55 
56 static int
57 sgx_linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
58 {
59 	uint8_t data[SGX_IOCTL_MAX_DATA_LEN];
60 	cap_rights_t rights;
61 	struct file *fp;
62 	u_long cmd;
63 	int error;
64 	int len;
65 
66 	error = fget(td, args->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
67 	if (error != 0)
68 		return (error);
69 
70 	cmd = args->cmd;
71 
72 	args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
73 	if (cmd & LINUX_IOC_IN)
74 		args->cmd |= IOC_IN;
75 	if (cmd & LINUX_IOC_OUT)
76 		args->cmd |= IOC_OUT;
77 
78 	len = IOCPARM_LEN(cmd);
79 	if (len > SGX_IOCTL_MAX_DATA_LEN) {
80 		printf("%s: Can't copy data: cmd len is too big %d\n",
81 		    __func__, len);
82 		return (EINVAL);
83 	}
84 
85 	if (cmd & LINUX_IOC_IN) {
86 		error = copyin((void *)args->arg, data, len);
87 		if (error) {
88 			printf("%s: Can't copy data, error %d\n",
89 			    __func__, error);
90 			return (EINVAL);
91 		}
92 	}
93 
94 	error = (fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td));
95 	fdrop(fp, td);
96 
97 	return (error);
98 }
99 
100 static struct linux_ioctl_handler sgx_linux_handler = {
101 	sgx_linux_ioctl,
102 	SGX_LINUX_IOCTL_MIN,
103 	SGX_LINUX_IOCTL_MAX,
104 };
105 
106 SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
107     linux_ioctl_register_handler, &sgx_linux_handler);
108 SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
109     linux_ioctl_unregister_handler, &sgx_linux_handler);
110 
111 static int
112 sgx_linux_modevent(module_t mod, int type, void *data)
113 {
114 
115 	return (0);
116 }
117 
118 DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);
119 MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);
120