test_encl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016-20 Intel Corporation. */
  3. #include <stddef.h>
  4. #include "defines.h"
  5. /*
  6. * Data buffer spanning two pages that will be placed first in the .data
  7. * segment via the linker script. Even if not used internally the second page
  8. * is needed by external test manipulating page permissions, so mark
  9. * encl_buffer as "used" to make sure it is entirely preserved by the compiler.
  10. */
  11. static uint8_t __used __section(".data.encl_buffer") encl_buffer[8192] = { 1 };
  12. enum sgx_enclu_function {
  13. EACCEPT = 0x5,
  14. EMODPE = 0x6,
  15. };
  16. static void do_encl_emodpe(void *_op)
  17. {
  18. struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
  19. struct encl_op_emodpe *op = _op;
  20. secinfo.flags = op->flags;
  21. asm volatile(".byte 0x0f, 0x01, 0xd7"
  22. : /* no outputs */
  23. : "a" (EMODPE),
  24. "b" (&secinfo),
  25. "c" (op->epc_addr)
  26. : "memory" /* read from secinfo pointer */);
  27. }
  28. static void do_encl_eaccept(void *_op)
  29. {
  30. struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
  31. struct encl_op_eaccept *op = _op;
  32. int rax;
  33. secinfo.flags = op->flags;
  34. asm volatile(".byte 0x0f, 0x01, 0xd7"
  35. : "=a" (rax)
  36. : "a" (EACCEPT),
  37. "b" (&secinfo),
  38. "c" (op->epc_addr)
  39. : "memory" /* read from secinfo pointer */);
  40. op->ret = rax;
  41. }
  42. static void *memcpy(void *dest, const void *src, size_t n)
  43. {
  44. size_t i;
  45. for (i = 0; i < n; i++)
  46. ((char *)dest)[i] = ((char *)src)[i];
  47. return dest;
  48. }
  49. static void *memset(void *dest, int c, size_t n)
  50. {
  51. size_t i;
  52. for (i = 0; i < n; i++)
  53. ((char *)dest)[i] = c;
  54. return dest;
  55. }
  56. static void do_encl_init_tcs_page(void *_op)
  57. {
  58. struct encl_op_init_tcs_page *op = _op;
  59. void *tcs = (void *)op->tcs_page;
  60. uint32_t val_32;
  61. memset(tcs, 0, 16); /* STATE and FLAGS */
  62. memcpy(tcs + 16, &op->ssa, 8); /* OSSA */
  63. memset(tcs + 24, 0, 4); /* CSSA */
  64. val_32 = 1;
  65. memcpy(tcs + 28, &val_32, 4); /* NSSA */
  66. memcpy(tcs + 32, &op->entry, 8); /* OENTRY */
  67. memset(tcs + 40, 0, 24); /* AEP, OFSBASE, OGSBASE */
  68. val_32 = 0xFFFFFFFF;
  69. memcpy(tcs + 64, &val_32, 4); /* FSLIMIT */
  70. memcpy(tcs + 68, &val_32, 4); /* GSLIMIT */
  71. memset(tcs + 72, 0, 4024); /* Reserved */
  72. }
  73. static void do_encl_op_put_to_buf(void *op)
  74. {
  75. struct encl_op_put_to_buf *op2 = op;
  76. memcpy(&encl_buffer[0], &op2->value, 8);
  77. }
  78. static void do_encl_op_get_from_buf(void *op)
  79. {
  80. struct encl_op_get_from_buf *op2 = op;
  81. memcpy(&op2->value, &encl_buffer[0], 8);
  82. }
  83. static void do_encl_op_put_to_addr(void *_op)
  84. {
  85. struct encl_op_put_to_addr *op = _op;
  86. memcpy((void *)op->addr, &op->value, 8);
  87. }
  88. static void do_encl_op_get_from_addr(void *_op)
  89. {
  90. struct encl_op_get_from_addr *op = _op;
  91. memcpy(&op->value, (void *)op->addr, 8);
  92. }
  93. static void do_encl_op_nop(void *_op)
  94. {
  95. }
  96. /*
  97. * Symbol placed at the start of the enclave image by the linker script.
  98. * Declare this extern symbol with visibility "hidden" to ensure the compiler
  99. * does not access it through the GOT and generates position-independent
  100. * addressing as __encl_base(%rip), so we can get the actual enclave base
  101. * during runtime.
  102. */
  103. extern const uint8_t __attribute__((visibility("hidden"))) __encl_base;
  104. typedef void (*encl_op_t)(void *);
  105. static const encl_op_t encl_op_array[ENCL_OP_MAX] = {
  106. do_encl_op_put_to_buf,
  107. do_encl_op_get_from_buf,
  108. do_encl_op_put_to_addr,
  109. do_encl_op_get_from_addr,
  110. do_encl_op_nop,
  111. do_encl_eaccept,
  112. do_encl_emodpe,
  113. do_encl_init_tcs_page,
  114. };
  115. void encl_body(void *rdi, void *rsi)
  116. {
  117. struct encl_op_header *header = (struct encl_op_header *)rdi;
  118. encl_op_t op;
  119. if (header->type >= ENCL_OP_MAX)
  120. return;
  121. /*
  122. * The enclave base address needs to be added, as this call site
  123. * *cannot be* made rip-relative by the compiler, or fixed up by
  124. * any other possible means.
  125. */
  126. op = ((uint64_t)&__encl_base) + encl_op_array[header->type];
  127. (*op)(header);
  128. }