trts_util.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "trts_util.h"
  32. #include "global_data.h"
  33. #include "util.h"
  34. #include "thread_data.h"
  35. // No need to check the state of enclave or thread.
  36. // The functions should be called within an ECALL, so the enclave and thread must be initialized at that time.
  37. void * get_heap_base(void)
  38. {
  39. return GET_PTR(void, &__ImageBase, g_global_data.heap_offset);
  40. }
  41. size_t get_heap_size(void)
  42. {
  43. size_t heap_size = g_global_data.heap_size;
  44. if (EDMM_supported)
  45. {
  46. for(uint32_t i = 0; i < g_global_data.layout_entry_num; i++)
  47. {
  48. if(g_global_data.layout_table[i].entry.id == LAYOUT_ID_HEAP_MAX)
  49. {
  50. heap_size += ((size_t)g_global_data.layout_table[i].entry.page_count << SE_PAGE_SHIFT);
  51. }
  52. }
  53. }
  54. return heap_size;
  55. }
  56. size_t get_heap_min_size(void)
  57. {
  58. size_t heap_size = 0;
  59. for(uint32_t i = 0; i < g_global_data.layout_entry_num; i++)
  60. {
  61. if(g_global_data.layout_table[i].entry.id == LAYOUT_ID_HEAP_MIN)
  62. {
  63. heap_size = ((size_t)g_global_data.layout_table[i].entry.page_count << SE_PAGE_SHIFT);
  64. break;
  65. }
  66. }
  67. return heap_size;
  68. }
  69. int * get_errno_addr(void)
  70. {
  71. thread_data_t *thread_data = get_thread_data();
  72. return reinterpret_cast<int *>(&thread_data->last_error);
  73. }
  74. //tRTS will receive a pointer to an array of uint64_t which indicates the
  75. //features of the running system. This function can be used to query whether
  76. //a certain feature (such as EDMM) is supported.
  77. //It takes as input the pointer to the array and the feature bit location.
  78. //The feature array coming from uRTS should be dealt with in the following way:
  79. //Every bit except the MSb in each uint64 represents a certain feature.
  80. //The MSb of each uint64_t, if set, indicates this is the last uint64_t to
  81. //search for the feature's existance.
  82. //For example, if we have two uint64_t elements in the array:
  83. //array[0]: xxxxxxxxxxxxxxxx array[1] Xxxxxxxxxxxxxxxx
  84. //MSb of array[1] should already be set to one by uRTS. Shown by capital 'X' here.
  85. //Features listed in array[0], counting from right-most bit to left-most bit,
  86. //have feature shift values 0 ~ 62, while features listed in array[1], have feature
  87. //shift values 64 ~ 126.
  88. int feature_supported(const uint64_t *feature_set, uint32_t feature_shift)
  89. {
  90. const uint64_t *f_set = feature_set;
  91. uint32_t bit_position = 0, i = 0;
  92. if (!f_set)
  93. return 0;
  94. while (((i+1) << 6) <= feature_shift)
  95. {
  96. if (f_set[i] & (0x1ULL << 63))
  97. return 0;
  98. i++;
  99. }
  100. bit_position = feature_shift - (i << 6);
  101. if (f_set[i] & (0x1ULL << bit_position))
  102. return 1;
  103. else
  104. return 0;
  105. }
  106. bool is_stack_addr(void *address, size_t size)
  107. {
  108. thread_data_t *thread_data = get_thread_data();
  109. size_t stack_base = thread_data->stack_base_addr;
  110. size_t stack_limit = thread_data->stack_limit_addr;
  111. size_t addr = (size_t) address;
  112. return (addr <= (addr + size)) && (stack_base >= (addr + size)) && (stack_limit <= addr);
  113. }
  114. bool is_valid_sp(uintptr_t sp)
  115. {
  116. return ( !(sp & (sizeof(uintptr_t) - 1)) // sp is expected to be 4/8 bytes aligned
  117. && is_stack_addr((void*)sp, 0) ); // sp points to the top/bottom of stack are accepted
  118. }