]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/sgx/sgx_linux.c
Import PCG-C into sys/contrib
[FreeBSD/FreeBSD.git] / sys / amd64 / sgx / sgx_linux.c
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) != 0)
74                 args->cmd |= IOC_IN;
75         if ((cmd & LINUX_IOC_OUT) != 0)
76                 args->cmd |= IOC_OUT;
77
78         len = IOCPARM_LEN(cmd);
79         if (len > SGX_IOCTL_MAX_DATA_LEN) {
80                 error = EINVAL;
81                 goto out;
82         }
83
84         if ((cmd & LINUX_IOC_IN) != 0) {
85                 error = copyin((void *)args->arg, data, len);
86                 if (error != 0)
87                         goto out;
88         }
89
90         error = fo_ioctl(fp, args->cmd, (caddr_t)data, td->td_ucred, td);
91 out:
92         fdrop(fp, td);
93         return (error);
94 }
95
96 static struct linux_ioctl_handler sgx_linux_handler = {
97         sgx_linux_ioctl,
98         SGX_LINUX_IOCTL_MIN,
99         SGX_LINUX_IOCTL_MAX,
100 };
101
102 SYSINIT(sgx_linux_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
103     linux_ioctl_register_handler, &sgx_linux_handler);
104 SYSUNINIT(sgx_linux_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
105     linux_ioctl_unregister_handler, &sgx_linux_handler);
106
107 static int
108 sgx_linux_modevent(module_t mod, int type, void *data)
109 {
110
111         return (0);
112 }
113
114 DEV_MODULE(sgx_linux, sgx_linux_modevent, NULL);
115 MODULE_DEPEND(sgx_linux, linux64, 1, 1, 1);