loader.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 _LOADER_H_
  32. #define _LOADER_H_
  33. #include "se_wrapper.h"
  34. #include "arch.h"
  35. #include "enclave.h"
  36. #include "enclave_creator.h"
  37. #include "section_info.h"
  38. #include "launch_checker.h"
  39. #include "file.h"
  40. #define GET_RELOC_FAILED ((uint8_t *)-1)
  41. #if defined(SE_SIM)
  42. #define ENCLAVE_ID_IOCTL m_enclave_id
  43. #else
  44. //only translate enclave id to start address for linux HW mode.
  45. #define ENCLAVE_ID_IOCTL (sgx_enclave_id_t)((uintptr_t)m_start_addr)
  46. #endif
  47. class BinParser;
  48. class CLoader: private Uncopyable
  49. {
  50. public:
  51. CLoader(uint8_t *mapped_file_base, BinParser &parser);
  52. virtual ~CLoader();
  53. int load_enclave(SGXLaunchToken *lc, int flag, const metadata_t *metadata, le_prd_css_file_t *prd_css_file = NULL, sgx_misc_attribute_t *misc_attr = NULL);
  54. int load_enclave_ex(SGXLaunchToken *lc, bool is_debug, const metadata_t *metadata, le_prd_css_file_t *prd_css_file = NULL, sgx_misc_attribute_t *misc_attr = NULL);
  55. int destroy_enclave();
  56. sgx_enclave_id_t get_enclave_id() const;
  57. const void* get_start_addr() const;
  58. const secs_t& get_secs() const;
  59. const std::vector<std::pair<tcs_t *, bool>>& get_tcs_list() const;
  60. void* get_symbol_address(const char* const sym);
  61. int set_memory_protection(bool is_after_initialization);
  62. int post_init_action(layout_t *start, layout_t *end, uint64_t delta);
  63. int post_init_action_commit(layout_t *start, layout_t *end, uint64_t delta);
  64. private:
  65. int build_mem_region(const section_info_t &sec_info);
  66. int build_image(SGXLaunchToken * const lc, sgx_attributes_t * const secs_attr, le_prd_css_file_t *prd_css_file, sgx_misc_attribute_t * const misc_attr);
  67. int build_secs(sgx_attributes_t * const secs_attr, sgx_misc_attribute_t * const misc_attr);
  68. int build_context(const uint64_t start_rva, layout_entry_t *layout);
  69. int build_contexts(layout_t *layout_start, layout_t *layout_end, uint64_t delta);
  70. int build_partial_page(const uint64_t rva, const uint64_t size, const void *source, const sec_info_t &sinfo, const uint32_t attr);
  71. int build_pages(const uint64_t start_rva, const uint64_t size, const void *source, const sec_info_t &sinfo, const uint32_t attr);
  72. bool is_relocation_page(const uint64_t rva, vector<uint8_t> *bitmap);
  73. bool is_ae(const enclave_css_t *enclave_css);
  74. bool is_metadata_buffer(uint32_t offset, uint32_t size);
  75. bool is_enclave_buffer(uint64_t offset, uint64_t size);
  76. int validate_layout_table();
  77. int validate_patch_table();
  78. int validate_metadata();
  79. int get_debug_flag(const token_t * const launch);
  80. virtual int build_sections(vector<uint8_t> *bitmap);
  81. int set_context_protection(layout_t *layout_start, layout_t *layout_end, uint64_t delta);
  82. uint8_t *m_mapped_file_base;
  83. sgx_enclave_id_t m_enclave_id;
  84. void *m_start_addr;
  85. // the TCS list
  86. std::vector<std::pair<tcs_t *, bool>> m_tcs_list;
  87. // the enclave creation parameters
  88. const metadata_t *m_metadata;
  89. secs_t m_secs;
  90. BinParser &m_parser;
  91. };
  92. #endif