metadata.h 5.9 KB

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