metadata.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef _METADATA_H_
  32. #define _METADATA_H_
  33. #include "arch.h"
  34. #include "se_macro.h"
  35. #pragma pack(1)
  36. /* version of metadata */
  37. #define MAJOR_VERSION 2 //MAJOR_VERSION should not larger than 0ffffffff
  38. #define MINOR_VERSION 3 //MINOR_VERSION should not larger than 0ffffffff
  39. #define SGX_2_1_MAJOR_VERSION 2 //MAJOR_VERSION should not larger than 0ffffffff
  40. #define SGX_2_1_MINOR_VERSION 2 //MINOR_VERSION should not larger than 0ffffffff
  41. #define SGX_2_0_MAJOR_VERSION 2 //MAJOR_VERSION should not larger than 0ffffffff
  42. #define SGX_2_0_MINOR_VERSION 1 //MINOR_VERSION should not larger than 0ffffffff
  43. #define SGX_1_9_MAJOR_VERSION 1 //MAJOR_VERSION should not larger than 0ffffffff
  44. #define SGX_1_9_MINOR_VERSION 4 //MINOR_VERSION should not larger than 0ffffffff
  45. #define SGX_1_5_MAJOR_VERSION 1 //MAJOR_VERSION should not larger than 0ffffffff
  46. #define SGX_1_5_MINOR_VERSION 3 //MINOR_VERSION should not larger than 0ffffffff
  47. #define META_DATA_MAKE_VERSION(major, minor) (((uint64_t)major)<<32 | minor)
  48. #define MAJOR_VERSION_OF_METADATA(version) (((uint64_t)version) >> 32)
  49. #define MINOR_VERSION_OF_METADATA(version) (((uint64_t)version) & 0xFFFFFFFF)
  50. #define METADATA_MAGIC 0x86A80294635D0E4CULL
  51. #define METADATA_SIZE 0x5000
  52. #define TCS_TEMPLATE_SIZE 72
  53. /* TCS Policy bit masks */
  54. #define TCS_POLICY_BIND 0x00000000 /* If set, the TCS is bound to the application thread */
  55. #define TCS_POLICY_UNBIND 0x00000001
  56. #define MAX_SAVE_BUF_SIZE 2632
  57. #define TCS_NUM_MIN 1
  58. #define SSA_NUM_MIN 2
  59. #define SSA_FRAME_SIZE_MIN 1
  60. #define SSA_FRAME_SIZE_MAX 2
  61. #define STACK_SIZE_MIN 0x1000
  62. #define STACK_SIZE_MAX 0x40000
  63. #define HEAP_SIZE_MIN 0x1000
  64. #define HEAP_SIZE_MAX 0x1000000
  65. #define DEFAULT_MISC_SELECT 0
  66. #define DEFAULT_MISC_MASK 0xFFFFFFFF
  67. typedef struct _data_directory_t
  68. {
  69. uint32_t offset;
  70. uint32_t size;
  71. } data_directory_t;
  72. typedef enum
  73. {
  74. DIR_PATCH,
  75. DIR_LAYOUT,
  76. DIR_NUM,
  77. } dir_index_t;
  78. #define GROUP_FLAG (1<<12)
  79. #define GROUP_ID(x) (GROUP_FLAG | x)
  80. #define IS_GROUP_ID(x) !!((x) & GROUP_FLAG)
  81. #define LAYOUT_ID_HEAP_MIN 1
  82. #define LAYOUT_ID_HEAP_INIT 2
  83. #define LAYOUT_ID_HEAP_MAX 3
  84. #define LAYOUT_ID_TCS 4
  85. #define LAYOUT_ID_TD 5
  86. #define LAYOUT_ID_SSA 6
  87. #define LAYOUT_ID_STACK_MAX 7
  88. #define LAYOUT_ID_STACK_MIN 8
  89. #define LAYOUT_ID_THREAD_GROUP GROUP_ID(9)
  90. #define LAYOUT_ID_GUARD 10
  91. #define LAYOUT_ID_HEAP_DYN_MIN 11
  92. #define LAYOUT_ID_HEAP_DYN_INIT 12
  93. #define LAYOUT_ID_HEAP_DYN_MAX 13
  94. #define LAYOUT_ID_TCS_DYN 14
  95. #define LAYOUT_ID_TD_DYN 15
  96. #define LAYOUT_ID_SSA_DYN 16
  97. #define LAYOUT_ID_STACK_DYN_MAX 17
  98. #define LAYOUT_ID_STACK_DYN_MIN 18
  99. #define LAYOUT_ID_THREAD_GROUP_DYN GROUP_ID(19)
  100. /*
  101. ** layout table example
  102. ** entry0 - entry1 - entry2 - group3 (entry_count=2, load_times=3) ...
  103. ** the load sequence should be:
  104. ** entry0 - entry1 - entry2 - entry1 - entry2 - entry1 - entry2 - entry1 - entry2 ...
  105. ** -------------- -------------- --------------
  106. ** group3 1st time group3 2nd time group3 3rd time
  107. */
  108. typedef struct _layout_entry_t
  109. {
  110. uint16_t id; /* unique ID to identify the purpose for this entry */
  111. uint16_t attributes; /* EADD/EEXTEND/EREMOVE... */
  112. uint32_t page_count; /* map size in page. Biggest chunk = 2^32 pages = 2^44 bytes. */
  113. uint64_t rva; /* map offset, relative to encalve base */
  114. uint32_t content_size; /* if content_offset = 0, content_size is the initial data to fill the whole page. */
  115. uint32_t content_offset; /* offset to the initial content, relative to metadata */
  116. si_flags_t si_flags; /* security info, R/W/X, SECS/TCS/REG/VA */
  117. } layout_entry_t;
  118. typedef struct _layout_group_t
  119. {
  120. uint16_t id; /* unique ID to identify the purpose for this entry */
  121. uint16_t entry_count; /* reversely count entry_count entries for the group loading. */
  122. uint32_t load_times; /* the repeated times of loading */
  123. uint64_t load_step; /* the group size. the entry load rva should be adjusted with the load_step */
  124. /* rva = entry.rva + group.load_step * load_times */
  125. uint32_t reserved[4];
  126. } layout_group_t;
  127. typedef union _layout_t
  128. {
  129. layout_entry_t entry;
  130. layout_group_t group;
  131. } layout_t;
  132. typedef struct _patch_entry_t
  133. {
  134. uint64_t dst; /* relative to enclave file base */
  135. uint32_t src; /* relative to metadata base */
  136. uint32_t size; /* patched size */
  137. uint32_t reserved[4];
  138. } patch_entry_t;
  139. typedef struct _metadata_t
  140. {
  141. uint64_t magic_num; /* The magic number identifying the file as a signed enclave image */
  142. uint64_t version; /* The metadata version */
  143. uint32_t size; /* The size of this structure */
  144. uint32_t tcs_policy; /* TCS management policy */
  145. uint32_t ssa_frame_size; /* The size of SSA frame in page */
  146. uint32_t max_save_buffer_size; /* Max buffer size is 2632 */
  147. uint32_t desired_misc_select;
  148. uint32_t tcs_min_pool; /* TCS min pool*/
  149. uint64_t enclave_size; /* enclave virtual size */
  150. sgx_attributes_t attributes; /* XFeatureMask to be set in SECS. */
  151. enclave_css_t enclave_css; /* The enclave signature */
  152. data_directory_t dirs[DIR_NUM];
  153. uint8_t data[18592];
  154. }metadata_t;
  155. se_static_assert(sizeof(metadata_t) == METADATA_SIZE);
  156. #pragma pack()
  157. #endif