update_global_data.hxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace {
  32. layout_entry_t *get_entry_by_id(const metadata_t *const metadata, uint16_t id)
  33. {
  34. layout_entry_t *layout_start = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  35. layout_entry_t *layout_end = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset + metadata->dirs[DIR_LAYOUT].size);
  36. for (layout_entry_t *layout = layout_start; layout < layout_end; layout++)
  37. {
  38. if(layout->id == id)
  39. return layout;
  40. }
  41. assert(false);
  42. return NULL;
  43. }
  44. bool do_update_global_data(const metadata_t *const metadata,
  45. const create_param_t* const create_param,
  46. global_data_t* global_data)
  47. {
  48. layout_entry_t *layout_heap = get_entry_by_id(metadata, LAYOUT_ID_HEAP_MIN);
  49. global_data->enclave_size = (sys_word_t)metadata->enclave_size;
  50. global_data->heap_offset = (sys_word_t)layout_heap->rva;
  51. global_data->heap_size = (sys_word_t)(create_param->heap_init_size);
  52. global_data->thread_policy = (sys_word_t)metadata->tcs_policy;
  53. thread_data_t *thread_data = &global_data->td_template;
  54. thread_data->stack_limit_addr = (sys_word_t)create_param->stack_limit_addr;
  55. thread_data->stack_base_addr = (sys_word_t)create_param->stack_base_addr;
  56. thread_data->last_sp = thread_data->stack_base_addr;
  57. thread_data->xsave_size = create_param->xsave_size;
  58. thread_data->first_ssa_gpr = (sys_word_t)create_param->ssa_base_addr + metadata->ssa_frame_size * SE_PAGE_SIZE - (uint32_t)sizeof(ssa_gpr_t);
  59. thread_data->flags = 0;
  60. // TD address relative to TCS
  61. thread_data->tls_addr = (sys_word_t)create_param->tls_addr;
  62. thread_data->self_addr = (sys_word_t)create_param->td_addr;
  63. thread_data->tls_array = thread_data->self_addr + (sys_word_t)offsetof(thread_data_t, tls_addr);
  64. // TCS template
  65. if(0 != memcpy_s(&global_data->tcs_template, sizeof(global_data->tcs_template),
  66. GET_PTR(void, metadata, get_entry_by_id(metadata, LAYOUT_ID_TCS)->content_offset),
  67. get_entry_by_id(metadata, LAYOUT_ID_TCS)->content_size))
  68. {
  69. return false;
  70. }
  71. // layout table: dynamic heap + dynamic thread group
  72. layout_entry_t *layout_start = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  73. layout_entry_t *layout_end = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset + metadata->dirs[DIR_LAYOUT].size);
  74. global_data->layout_entry_num = 0;
  75. for (layout_entry_t *layout = layout_start; layout < layout_end; layout++)
  76. {
  77. if(0 != memcpy_s(&global_data->layout_table[global_data->layout_entry_num],
  78. sizeof(global_data->layout_table) - global_data->layout_entry_num * sizeof(layout_t),
  79. layout,
  80. sizeof(layout_t)))
  81. {
  82. return false;
  83. }
  84. global_data->layout_entry_num++;
  85. }
  86. return true;
  87. }
  88. }