trts_pic.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * Description:
  33. * The file provides `enclave_entry' function to switch code between
  34. * trusted and untrusted envronment.
  35. */
  36. .file "trts_pic.S"
  37. #include "trts_pic.h"
  38. /* .text */
  39. .section .nipx,"ax",@progbits
  40. /*
  41. * ------------------------------------------------------------------------
  42. * extern "C" int do_egetkey(key_request_t *key_request, key_128bit_t *key)
  43. * return value:
  44. * 0 - success
  45. * none-zeor - EGETKEY error code
  46. * EGETKEY: rbx - the address of KEYREQUEST structure
  47. * rcx - the address where the key is outputted
  48. * ------------------------------------------------------------------------
  49. */
  50. DECLARE_LOCAL_FUNC do_egetkey
  51. SE_PROLOG
  52. mov $SE_EGETKEY, %xax /* EGETKEY leaf */
  53. ENCLU
  54. #ifdef SE_SIM
  55. cmp $SGX_SUCCESS, %xax /* In simulation mode, ZF flag will not be set */
  56. jnz .Legetkey_done /* because the stack clean operation will always clean ZF flag */
  57. #else
  58. jz .Legetkey_done /* if EGETKEY error, ZF flag is set and error code is set to xax */
  59. #endif
  60. xor %xax, %xax
  61. .Legetkey_done:
  62. SE_EPILOG
  63. /*
  64. * -------------------------------------------------------------------------
  65. * extern "C" void do_ereport(sgx_target_info_t *target_info, sgx_report_data_t *report_data, sgx_report_t *report);
  66. * EREPORT: rbx - the address of TARGETINFO;
  67. * rcx - the address of REPORTDATA;
  68. * rdx - the address where REPORT is outputted
  69. * -------------------------------------------------------------------------
  70. */
  71. DECLARE_LOCAL_FUNC do_ereport
  72. SE_PROLOG
  73. mov $SE_EREPORT, %xax /* EREPORT leaf */
  74. ENCLU
  75. SE_EPILOG
  76. DECLARE_GLOBAL_FUNC do_eaccept
  77. SE_PROLOG
  78. mov $SE_EACCEPT, %eax
  79. ENCLU
  80. cmp $SGX_SUCCESS, %eax
  81. jnz abort
  82. SE_EPILOG
  83. DECLARE_GLOBAL_FUNC do_emodpe
  84. SE_PROLOG
  85. mov $SE_EMODPE, %eax
  86. ENCLU
  87. SE_EPILOG
  88. #define _RDRAND_RETRY_TIMES 10
  89. /*
  90. * -------------------------------------
  91. * extern "C" uint32_t do_rdrand(uint32_t *rand);
  92. * return value:
  93. * non-zero: rdrand succeeded
  94. * zero: rdrand failed
  95. * -------------------------------------
  96. */
  97. DECLARE_LOCAL_FUNC do_rdrand
  98. mov $_RDRAND_RETRY_TIMES, %ecx
  99. .Lrdrand_retry:
  100. .byte 0x0F, 0xC7, 0xF0 /* rdrand %eax */
  101. jc .Lrdrand_return
  102. dec %ecx
  103. jnz .Lrdrand_retry
  104. xor %xax, %xax
  105. ret
  106. .Lrdrand_return:
  107. #ifdef LINUX32
  108. mov SE_WORDSIZE(%esp), %ecx
  109. #else
  110. mov %rdi, %rcx
  111. #endif
  112. movl %eax, (%xcx)
  113. mov $1, %xax
  114. ret
  115. /*
  116. * ---------------------------------------------------------------------
  117. * Function: thread_data_t* get_thread_data(void);
  118. *
  119. * Get the address of thread_data
  120. * ---------------------------------------------------------------------
  121. */
  122. DECLARE_LOCAL_FUNC get_thread_data
  123. READ_TD_DATA self_addr
  124. ret