xref: /freebsd/sys/amd64/sgx/sgx_linux.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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_one(&rights, CAP_IOCTL),
67 	    &fp);
68 	if (error != 0)
69 		return (error);
70 
71 	cmd = args->cmd;
72 
73 	args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
74 	if ((cmd & LINUX_IOC_IN) != 0)
75 		args->cmd |= IOC_IN;
76 	if ((cmd & LINUX_IOC_OUT) != 0)
77 		args->cmd |= IOC_OUT;
78 
79 	len = IOCPARM_LEN(cmd);
80 	if (len > SGX_IOCTL_MAX_DATA_LEN) {
81 		error = EINVAL;
82 		goto out;
83 	}
84 
85 	if ((cmd & LINUX_IOC_IN) != 0) {
86 		error = copyin((void *)args->arg, data, len);
87 		if (error != 0)
88 			goto out;
89 	}
90 
91 	error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td);
92 out:
93 	fdrop(fp, td);
94 	return (error);
95 }
96 
97 static struct linux_ioctl_handler sgx_linux_handler = {
98 	sgx_linux_ioctl,
99 	SGX_LINUX_IOCTL_MIN,
100 	SGX_LINUX_IOCTL_MAX,
101 };
102 
103 SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
104     linux_ioctl_register_handler, &sgx_linux_handler);
105 SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
106     linux_ioctl_unregister_handler, &sgx_linux_handler);
107 
108 static int
109 sgx_linux_modevent(module_t mod, int type, void *data)
110 {
111 
112 	return (0);
113 }
114 
115 DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);
116 MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);
117