xref: /freebsd/sys/amd64/sgx/sgx_linux.c (revision 6bb132ba)
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/param.h>
33 #include <sys/systm.h>
34 #include <sys/capsicum.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/file.h>
40 #include <sys/proc.h>
41 
42 #include <machine/sgx.h>
43 #include <machine/../linux/linux.h>
44 #include <machine/../linux/linux_proto.h>
45 #include <compat/linux/linux_ioctl.h>
46 
47 #include <amd64/sgx/sgxvar.h>
48 
49 #include <sys/ioccom.h>
50 
51 #define	SGX_LINUX_IOCTL_MIN	(SGX_IOC_ENCLAVE_CREATE & 0xffff)
52 #define	SGX_LINUX_IOCTL_MAX	(SGX_IOC_ENCLAVE_INIT & 0xffff)
53 
54 static int
sgx_linux_ioctl(struct thread * td,struct linux_ioctl_args * args)55 sgx_linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
56 {
57 	uint8_t data[SGX_IOCTL_MAX_DATA_LEN];
58 	cap_rights_t rights;
59 	struct file *fp;
60 	u_long cmd;
61 	int error;
62 	int len;
63 
64 	error = fget(td, args->fd, cap_rights_init_one(&rights, CAP_IOCTL),
65 	    &fp);
66 	if (error != 0)
67 		return (error);
68 
69 	cmd = args->cmd;
70 
71 	args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
72 	if ((cmd & LINUX_IOC_IN) != 0)
73 		args->cmd |= IOC_IN;
74 	if ((cmd & LINUX_IOC_OUT) != 0)
75 		args->cmd |= IOC_OUT;
76 
77 	len = IOCPARM_LEN(cmd);
78 	if (len > SGX_IOCTL_MAX_DATA_LEN) {
79 		error = EINVAL;
80 		goto out;
81 	}
82 
83 	if ((cmd & LINUX_IOC_IN) != 0) {
84 		error = copyin((void *)args->arg, data, len);
85 		if (error != 0)
86 			goto out;
87 	}
88 
89 	error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td);
90 out:
91 	fdrop(fp, td);
92 	return (error);
93 }
94 
95 static struct linux_ioctl_handler sgx_linux_handler = {
96 	sgx_linux_ioctl,
97 	SGX_LINUX_IOCTL_MIN,
98 	SGX_LINUX_IOCTL_MAX,
99 };
100 
101 SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
102     linux_ioctl_register_handler, &sgx_linux_handler);
103 SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
104     linux_ioctl_unregister_handler, &sgx_linux_handler);
105 
106 static int
sgx_linux_modevent(module_t mod,int type,void * data)107 sgx_linux_modevent(module_t mod, int type, void *data)
108 {
109 
110 	return (0);
111 }
112 
113 DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);
114 MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);
115