gsgx_ioctl_1_7.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * (C) Copyright 2013 Intel Corporation
  3. * Author: Jarkko Sakkinen <jarkko.sakkinen@intel.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/version.h>
  13. #include <linux/highmem.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/security.h>
  17. #include <asm/tlbflush.h>
  18. #include "gsgx.h"
  19. #if SDK_DRIVER_VERSION == KERNEL_VERSION(1, 7, 0)
  20. struct file *isgx_dev;
  21. static long enclave_create(struct file *filep, void * arg)
  22. {
  23. struct gsgx_enclave_create *createp = arg;
  24. struct sgx_enclave_create isgx_create;
  25. isgx_create.src = createp->src;
  26. filep->private_data = (void *) createp->src;
  27. return KSYM(isgx_ioctl_enclave_create)(filep, SGX_IOC_ENCLAVE_CREATE,
  28. (unsigned long) &isgx_create);
  29. }
  30. static long enclave_add_pages(struct file *filep, void * arg)
  31. {
  32. struct gsgx_enclave_add_pages *addp = arg;
  33. struct sgx_enclave_add_page isgx_add;
  34. uint64_t off;
  35. int ret = 0;
  36. if (!addp->addr || (addp->addr & (PAGE_SIZE - 1)))
  37. return -EINVAL;
  38. if (!addp->size || (addp->size & (PAGE_SIZE - 1)))
  39. return -EINVAL;
  40. if (!addp->secinfo)
  41. return -EINVAL;
  42. isgx_add.secinfo = addp->secinfo;
  43. for (off = 0 ; off < addp->size ; off += PAGE_SIZE) {
  44. isgx_add.addr = addp->addr + off;
  45. isgx_add.src =
  46. addp->flags & GSGX_ENCLAVE_ADD_PAGES_REPEAT_SRC ?
  47. addp->user_addr : addp->user_addr + off;
  48. isgx_add.mrmask =
  49. addp->flags & GSGX_ENCLAVE_ADD_PAGES_SKIP_EEXTEND ?
  50. 0 : ~0;
  51. ret = KSYM(isgx_ioctl_enclave_add_page)(filep,
  52. SGX_IOC_ENCLAVE_ADD_PAGE, (unsigned long) &isgx_add);
  53. if (ret < 0)
  54. break;
  55. }
  56. return ret;
  57. }
  58. static long enclave_init(struct file *filep, void * arg)
  59. {
  60. struct gsgx_enclave_init *initp = arg;
  61. struct sgx_enclave_init isgx_init;
  62. isgx_init.addr = initp->addr;
  63. isgx_init.sigstruct = initp->sigstruct;
  64. isgx_init.einittoken = initp->einittoken;
  65. return KSYM(isgx_ioctl_enclave_init)(filep, SGX_IOC_ENCLAVE_INIT,
  66. (unsigned long) &isgx_init);
  67. }
  68. long gsgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  69. {
  70. char data[256];
  71. long (*handler) (struct file *filp, void *arg) = NULL;
  72. long ret;
  73. switch (cmd) {
  74. case GSGX_IOCTL_ENCLAVE_CREATE:
  75. handler = enclave_create;
  76. break;
  77. case GSGX_IOCTL_ENCLAVE_ADD_PAGES:
  78. handler = enclave_add_pages;
  79. break;
  80. case GSGX_IOCTL_ENCLAVE_INIT:
  81. handler = enclave_init;
  82. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. if (copy_from_user(data, (void __user *) arg, _IOC_SIZE(cmd)))
  87. return -EFAULT;
  88. ret = handler(filep, (void *) data);
  89. if (!ret && (cmd & IOC_OUT)) {
  90. if (copy_to_user((void __user *) arg, data, _IOC_SIZE(cmd)))
  91. return -EFAULT;
  92. }
  93. return ret;
  94. }
  95. int gsgx_mmap(struct file *file, struct vm_area_struct *vma)
  96. {
  97. return KSYM(isgx_mmap)(file, vma);
  98. }
  99. unsigned long gsgx_get_unmapped_area(struct file *file, unsigned long addr,
  100. unsigned long len, unsigned long pgoff,
  101. unsigned long flags)
  102. {
  103. if (file->private_data == (void *) GSGX_ENCLAVE_CREATE_NO_ADDR) {
  104. unsigned long unmapped_addr =
  105. KSYM(isgx_get_unmapped_area)(file, addr, len,
  106. pgoff, flags);
  107. file->private_data = (void *) unmapped_addr;
  108. return unmapped_addr;
  109. } else {
  110. unsigned long unmapped_addr = (unsigned long) file->private_data;
  111. struct mm_struct *mm = current->mm;
  112. struct vm_area_struct *vma = find_vma(mm, unmapped_addr);
  113. if (vma && vma->vm_start <= len)
  114. return -EINVAL;
  115. return unmapped_addr;
  116. }
  117. }
  118. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_create, long,
  119. struct file *filep, unsigned int cmd, unsigned long arg);
  120. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_init, long,
  121. struct file *filep, unsigned int cmd, unsigned long arg);
  122. IMPORT_KSYM_PROTO(isgx_ioctl_enclave_add_page, long,
  123. struct file *filep, unsigned int cmd, unsigned long arg);
  124. IMPORT_KSYM(isgx_enclave_release);
  125. IMPORT_KSYM_PROTO(isgx_mmap, int, struct file *, struct vm_area_struct *);
  126. IMPORT_KSYM_PROTO(isgx_get_unmapped_area, unsigned long,
  127. struct file *, unsigned long, unsigned long,
  128. unsigned long, unsigned long);
  129. int gsgx_lookup_ksyms(void)
  130. {
  131. int ret;
  132. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_create)))
  133. return ret;
  134. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_init)))
  135. return ret;
  136. if ((ret = LOOKUP_KSYM(isgx_ioctl_enclave_add_page)))
  137. return ret;
  138. if ((ret = LOOKUP_KSYM(isgx_enclave_release)))
  139. return ret;
  140. if ((ret = LOOKUP_KSYM(isgx_mmap)))
  141. return ret;
  142. if ((ret = LOOKUP_KSYM(isgx_get_unmapped_area)))
  143. return ret;
  144. return 0;
  145. }
  146. #endif /* SGX_DRIVER_VERSION == 1.7 */