sgx_api.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef SGX_API_H
  14. #define SGX_API_H
  15. #include "sgx_arch.h"
  16. #include "pal_error.h"
  17. long sgx_ocall (unsigned long code, void * ms);
  18. bool sgx_is_completely_within_enclave (const void * addr, uint64_t size);
  19. bool sgx_is_completely_outside_enclave(const void * addr, uint64_t size);
  20. void* sgx_alloc_on_ustack(uint64_t size);
  21. void* sgx_copy_to_ustack(const void* ptr, uint64_t size);
  22. void sgx_reset_ustack(void);
  23. bool sgx_copy_ptr_to_enclave(void** ptr, void* uptr, uint64_t size);
  24. uint64_t sgx_copy_to_enclave(const void* ptr, uint64_t maxsize, const void* uptr, uint64_t usize);
  25. /*
  26. * sgx_report:
  27. * Generate SGX hardware signed report.
  28. */
  29. static inline int sgx_report (sgx_target_info_t * targetinfo,
  30. void * reportdata, sgx_report_t * report)
  31. {
  32. __asm__ volatile(
  33. ENCLU "\n"
  34. :: "a"(EREPORT), "b"(targetinfo), "c"(reportdata), "d"(report)
  35. : "memory");
  36. return 0;
  37. }
  38. /*
  39. * sgx_getkey:
  40. * Retrieve SGX hardware enclave cryptography key.
  41. */
  42. static inline int64_t sgx_getkey (sgx_key_request_t * keyrequest,
  43. sgx_key_128bit_t * key)
  44. {
  45. int64_t rax = EGETKEY;
  46. __asm__ volatile(
  47. ENCLU "\n"
  48. : "+a"(rax)
  49. : "b"(keyrequest), "c"(key)
  50. : "memory");
  51. return rax;
  52. }
  53. /*
  54. * rdrand:
  55. * Get hardware generated random value.
  56. */
  57. static inline uint32_t rdrand (void)
  58. {
  59. uint32_t ret;
  60. __asm__ volatile(
  61. "1: .byte 0x0f, 0xc7, 0xf0\n" /* RDRAND %EAX */
  62. "jnc 1b\n"
  63. :"=a"(ret)
  64. :: "cc");
  65. return ret;
  66. }
  67. /*
  68. * rdfsbase:
  69. * read FS register (allowed in enclaves).
  70. */
  71. static inline uint64_t rdfsbase (void)
  72. {
  73. uint64_t fsbase;
  74. __asm__ volatile(
  75. ".byte 0xf3, 0x48, 0x0f, 0xae, 0xc0\n" /* RDFSBASE %RAX */
  76. : "=a"(fsbase));
  77. return fsbase;
  78. }
  79. /*
  80. * wrfsbase:
  81. * modify FS register (allowed in enclaves).
  82. */
  83. static inline void wrfsbase (uint64_t addr)
  84. {
  85. __asm__ volatile(
  86. ".byte 0xf3, 0x48, 0x0f, 0xae, 0xd7\n" /* WRFSBASE %RDI */
  87. :: "D"(addr));
  88. }
  89. #endif /* SGX_API_H */