update_global_data.hxx 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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. // TD address relative to TCS
  60. thread_data->tls_addr = (sys_word_t)create_param->tls_addr;
  61. thread_data->self_addr = (sys_word_t)create_param->td_addr;
  62. thread_data->tls_array = thread_data->self_addr + (sys_word_t)offsetof(thread_data_t, tls_addr);
  63. // TCS template
  64. if(0 != memcpy_s(&global_data->tcs_template, sizeof(global_data->tcs_template),
  65. GET_PTR(void, metadata, get_entry_by_id(metadata, LAYOUT_ID_TCS)->content_offset),
  66. get_entry_by_id(metadata, LAYOUT_ID_TCS)->content_size))
  67. {
  68. return false;
  69. }
  70. // layout table: dynamic heap + dynamic thread group
  71. layout_entry_t *layout_start = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  72. layout_entry_t *layout_end = GET_PTR(layout_entry_t, metadata, metadata->dirs[DIR_LAYOUT].offset + metadata->dirs[DIR_LAYOUT].size);
  73. global_data->layout_entry_num = 0;
  74. for (layout_entry_t *layout = layout_start; layout < layout_end; layout++)
  75. {
  76. if(0 != memcpy_s(&global_data->layout_table[global_data->layout_entry_num],
  77. sizeof(global_data->layout_table) - global_data->layout_entry_num * sizeof(layout_t),
  78. layout,
  79. sizeof(layout_t)))
  80. {
  81. return false;
  82. }
  83. global_data->layout_entry_num++;
  84. }
  85. return true;
  86. }
  87. }