loader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 _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<tcs_t *>& get_tcs_list() const;
  60. void* get_symbol_address(const char* const sym);
  61. int set_memory_protection();
  62. private:
  63. int build_mem_region(const section_info_t &sec_info);
  64. 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);
  65. int build_secs(sgx_attributes_t * const secs_attr, sgx_misc_attribute_t * const misc_attr);
  66. int build_context(const uint64_t start_rva, layout_entry_t *layout);
  67. int build_contexts(layout_t *layout_start, layout_t *layout_end, uint64_t delta);
  68. int build_partial_page(const uint64_t rva, const uint64_t size, const void *source, const sec_info_t &sinfo, const uint32_t attr);
  69. int build_pages(const uint64_t start_rva, const uint64_t size, const void *source, const sec_info_t &sinfo, const uint32_t attr);
  70. bool is_relocation_page(const uint64_t rva, vector<uint8_t> *bitmap);
  71. bool is_ae(const enclave_css_t *enclave_css);
  72. bool is_metadata_buffer(uint32_t offset, uint32_t size);
  73. bool is_enclave_buffer(uint64_t offset, uint64_t size);
  74. int validate_layout_table();
  75. int validate_patch_table();
  76. int validate_metadata();
  77. int get_debug_flag(const token_t * const launch);
  78. virtual int build_sections(vector<uint8_t> *bitmap);
  79. int set_context_protection(layout_t *layout_start, layout_t *layout_end, uint64_t delta);
  80. uint8_t *m_mapped_file_base;
  81. sgx_enclave_id_t m_enclave_id;
  82. void *m_start_addr;
  83. // the TCS list
  84. std::vector<tcs_t *> m_tcs_list;
  85. // the enclave creation parameters
  86. const metadata_t *m_metadata;
  87. secs_t m_secs;
  88. BinParser &m_parser;
  89. };
  90. #endif