metadata.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2011-2017 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 1 /* MAJOR_VERSION should not larger than 0ffffffff */
  38. #define MINOR_VERSION 4 /* MINOR_VERSION should not larger than 0ffffffff */
  39. #define SGX_1_5_MAJOR_VERSION 1 //MAJOR_VERSION should not larger than 0ffffffff
  40. #define SGX_1_5_MINOR_VERSION 3 //MINOR_VERSION should not larger than 0ffffffff
  41. #define META_DATA_MAKE_VERSION(major, minor) (((uint64_t)major)<<32 | minor)
  42. #define METADATA_MAGIC 0x86A80294635D0E4CULL
  43. #define METADATA_SIZE 0x1000
  44. /* TCS Policy bit masks */
  45. #define TCS_POLICY_BIND 0x00000000 /* If set, the TCS is bound to the application thread */
  46. #define TCS_POLICY_UNBIND 0x00000001
  47. #define MAX_SAVE_BUF_SIZE 2632
  48. #define TCS_NUM_MIN 1
  49. #define SSA_NUM_MIN 2
  50. #define SSA_FRAME_SIZE_MIN 1
  51. #define SSA_FRAME_SIZE_MAX 2
  52. #define STACK_SIZE_MIN 0x1000
  53. #define HEAP_SIZE_MIN 0
  54. #define DEFAULT_MISC_SELECT 0
  55. #define DEFAULT_MISC_MASK 0xFFFFFFFF
  56. typedef struct _data_directory_t
  57. {
  58. uint32_t offset;
  59. uint32_t size;
  60. } data_directory_t;
  61. typedef enum
  62. {
  63. DIR_PATCH,
  64. DIR_LAYOUT,
  65. DIR_NUM,
  66. } dir_index_t;
  67. #define GROUP_FLAG (1<<12)
  68. #define GROUP_ID(x) (GROUP_FLAG | x)
  69. #define IS_GROUP_ID(x) !!((x) & GROUP_FLAG)
  70. #define LAYOUT_ID_HEAP 1
  71. #define LAYOUT_ID_TCS 2
  72. #define LAYOUT_ID_TD 3
  73. #define LAYOUT_ID_SSA 4
  74. #define LAYOUT_ID_STACK 5
  75. #define LAYOUT_ID_THREAD_GROUP GROUP_ID(6)
  76. #define LAYOUT_ID_GUARD 7
  77. /*
  78. ** layout table example
  79. ** entry0 - entry1 - entry2 - group3 (entry_count=2, load_times=3) ...
  80. ** the load sequence should be:
  81. ** entry0 - entry1 - entry2 - entry1 - entry2 - entry1 - entry2 - entry1 - entry2 ...
  82. ** -------------- -------------- --------------
  83. ** group3 1st time group3 2nd time group3 3rd time
  84. */
  85. typedef struct _layout_entry_t
  86. {
  87. uint16_t id; /* unique ID to identify the purpose for this entry */
  88. uint16_t attributes; /* EADD/EEXTEND/EREMOVE... */
  89. uint32_t page_count; /* map size in page. Biggest chunk = 2^32 pages = 2^44 bytes. */
  90. uint64_t rva; /* map offset, relative to encalve base */
  91. uint32_t content_size; /* if content_offset = 0, content_size is the initial data to fill the whole page. */
  92. uint32_t content_offset; /* offset to the initial content, relative to metadata */
  93. si_flags_t si_flags; /* security info, R/W/X, SECS/TCS/REG/VA */
  94. } layout_entry_t;
  95. typedef struct _layout_group_t
  96. {
  97. uint16_t id; /* unique ID to identify the purpose for this entry */
  98. uint16_t entry_count; /* reversely count entry_count entries for the group loading. */
  99. uint32_t load_times; /* the repeated times of loading */
  100. uint64_t load_step; /* the group size. the entry load rva should be adjusted with the load_step */
  101. /* rva = entry.rva + group.load_step * load_times */
  102. uint32_t reserved[4];
  103. } layout_group_t;
  104. typedef union _layout_t
  105. {
  106. layout_entry_t entry;
  107. layout_group_t group;
  108. } layout_t;
  109. typedef struct _patch_entry_t
  110. {
  111. uint64_t dst; /* relative to enclave file base */
  112. uint32_t src; /* relative to metadata base */
  113. uint32_t size; /* patched size */
  114. uint32_t reserved[4];
  115. } patch_entry_t;
  116. typedef struct _metadata_t
  117. {
  118. uint64_t magic_num; /* The magic number identifying the file as a signed enclave image */
  119. uint64_t version; /* The metadata version */
  120. uint32_t size; /* The size of this structure */
  121. uint32_t tcs_policy; /* TCS management policy */
  122. uint32_t ssa_frame_size; /* The size of SSA frame in page */
  123. uint32_t max_save_buffer_size; /* Max buffer size is 2632 */
  124. uint32_t desired_misc_select;
  125. uint32_t reserved;
  126. uint64_t enclave_size; /* enclave virtual size */
  127. sgx_attributes_t attributes; /* XFeatureMask to be set in SECS. */
  128. enclave_css_t enclave_css; /* The enclave signature */
  129. data_directory_t dirs[DIR_NUM];
  130. uint8_t data[2208];
  131. }metadata_t;
  132. se_static_assert(sizeof(metadata_t) == METADATA_SIZE);
  133. #pragma pack()
  134. #endif