db_main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_main.c
  17. *
  18. * This file contains the main function of the PAL loader, which loads and
  19. * processes environment, arguments and manifest.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "pal_security.h"
  29. #include "api.h"
  30. #include <asm/mman.h>
  31. #include <asm/ioctls.h>
  32. #include <elf/elf.h>
  33. #include <sysdeps/generic/ldsodefs.h>
  34. #include "ecall_types.h"
  35. #include "enclave_pages.h"
  36. #define RTLD_BOOTSTRAP
  37. #define _ENTRY enclave_entry
  38. struct pal_linux_state linux_state;
  39. struct pal_sec pal_sec;
  40. unsigned int pagesz = PRESET_PAGESIZE;
  41. unsigned long _DkGetPagesize (void)
  42. {
  43. return pagesz;
  44. }
  45. unsigned long _DkGetAllocationAlignment (void)
  46. {
  47. return pagesz;
  48. }
  49. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end)
  50. {
  51. *start = (PAL_PTR) pal_sec.heap_min;
  52. *end = (PAL_PTR) get_reserved_pages(NULL, pagesz);
  53. }
  54. PAL_NUM _DkGetProcessId (void)
  55. {
  56. return linux_state.process_id;
  57. }
  58. PAL_NUM _DkGetHostId (void)
  59. {
  60. return 0;
  61. }
  62. #include "elf-x86_64.h"
  63. #include "dynamic_link.h"
  64. void setup_pal_map (struct link_map * map);
  65. static struct link_map pal_map;
  66. int init_enclave (void);
  67. int init_child_process (PAL_HANDLE * parent_handle);
  68. static PAL_HANDLE setup_file_handle (const char * name, int fd)
  69. {
  70. if (!strpartcmp_static(name, "file:"))
  71. return NULL;
  72. name += static_strlen("file:");
  73. int len = strlen(name);
  74. PAL_HANDLE handle = malloc(HANDLE_SIZE(file) + len + 1);
  75. SET_HANDLE_TYPE(handle, file);
  76. HANDLE_HDR(handle)->flags |= RFD(0);
  77. handle->file.fd = fd;
  78. handle->file.append = 0;
  79. handle->file.pass = 0;
  80. char * path = (void *) handle + HANDLE_SIZE(file);
  81. get_norm_path(name, path, 0, len + 1);
  82. handle->file.realpath = path;
  83. return handle;
  84. }
  85. static int loader_filter (const char * key, int len)
  86. {
  87. if (key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  88. key[4] == 'e' && key[5] == 'r' && key[6] == '.')
  89. return 0;
  90. if (key[0] == 's' && key[1] == 'g' && key[2] == 'x' && key[3] == '.')
  91. return 0;
  92. return 1;
  93. }
  94. void pal_linux_main(int argc, const char ** argv, const char ** envp,
  95. struct pal_sec * sec_info)
  96. {
  97. PAL_HANDLE parent = NULL;
  98. unsigned long start_time = _DkSystemTimeQuery();
  99. /* relocate PAL itself */
  100. pal_map.l_addr = elf_machine_load_address();
  101. pal_map.l_name = pal_sec.enclave_image;
  102. elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
  103. pal_map.l_info, pal_map.l_addr);
  104. ELF_DYNAMIC_RELOCATE(&pal_map);
  105. memcpy(&pal_sec, sec_info, sizeof(struct pal_sec));
  106. /* set up page allocator and slab manager */
  107. init_slab_mgr(pagesz);
  108. init_pages();
  109. /* now we can add a link map for PAL itself */
  110. setup_pal_map(&pal_map);
  111. /* initialize enclave properties */
  112. init_enclave();
  113. pal_state.start_time = start_time;
  114. /* if there is a parent, create parent handle */
  115. if (pal_sec.ppid) {
  116. if (init_child_process(&parent) < 0)
  117. ocall_exit_process(0);
  118. }
  119. linux_state.uid = pal_sec.uid;
  120. linux_state.gid = pal_sec.gid;
  121. linux_state.process_id = (start_time & (~0xffff)) | pal_sec.pid;
  122. /* now let's mark our enclave as initialized */
  123. pal_enclave_state.enclave_flags |= PAL_ENCLAVE_INITIALIZED;
  124. /* create executable handle */
  125. PAL_HANDLE manifest, exec = NULL;
  126. /* create manifest handle */
  127. manifest =
  128. setup_file_handle(pal_sec.manifest_name, pal_sec.manifest_fd);
  129. if (pal_sec.exec_fd != PAL_IDX_POISON) {
  130. exec = setup_file_handle(pal_sec.exec_name, pal_sec.exec_fd);
  131. } else {
  132. SGX_DBG(DBG_I, "Run without executable\n");
  133. }
  134. /* parse manifest data into config storage */
  135. struct config_store * root_config =
  136. malloc(sizeof(struct config_store));
  137. root_config->raw_data = pal_sec.manifest_addr;
  138. root_config->raw_size = pal_sec.manifest_size;
  139. root_config->malloc = malloc;
  140. root_config->free = free;
  141. const char * errstring = NULL;
  142. if (read_config(root_config, loader_filter, &errstring) < 0) {
  143. SGX_DBG(DBG_E, "Can't read manifest: %s\n", errstring);
  144. ocall_exit_process(0);
  145. }
  146. pal_state.root_config = root_config;
  147. __pal_control.manifest_preload.start = (PAL_PTR) pal_sec.manifest_addr;
  148. __pal_control.manifest_preload.end = (PAL_PTR) pal_sec.manifest_addr +
  149. pal_sec.manifest_size;
  150. init_trusted_files();
  151. init_trusted_children();
  152. /* set up thread handle */
  153. PAL_HANDLE first_thread = malloc(HANDLE_SIZE(thread));
  154. SET_HANDLE_TYPE(first_thread, thread);
  155. first_thread->thread.tid = pal_sec.pid;
  156. ENCLAVE_TLS(thread) = __pal_control.first_thread = first_thread;
  157. /* call main function */
  158. pal_main(pal_sec.instance_id, manifest, exec, pal_sec.exec_addr,
  159. parent, first_thread, argv, envp);
  160. }
  161. /* the following code is borrowed from CPUID */
  162. #define WORD_EAX 0
  163. #define WORD_EBX 1
  164. #define WORD_ECX 2
  165. #define WORD_EDX 3
  166. #define WORD_NUM 4
  167. static void cpuid (unsigned int leaf, unsigned int subleaf,
  168. unsigned int words[])
  169. {
  170. _DkCpuIdRetrieve(leaf, subleaf, words);
  171. }
  172. #define FOUR_CHARS_VALUE(s, w) \
  173. (s)[0] = (w) & 0xff; \
  174. (s)[1] = ((w) >> 8) & 0xff; \
  175. (s)[2] = ((w) >> 16) & 0xff; \
  176. (s)[3] = ((w) >> 24) & 0xff;
  177. #define BPI 32
  178. #define POWER2(power) \
  179. (1ULL << (power))
  180. #define RIGHTMASK(width) \
  181. (((unsigned long) (width) >= BPI) ? ~0ULL : POWER2(width)-1ULL)
  182. #define BIT_EXTRACT_LE(value, start, after) \
  183. (((unsigned long) (value) & RIGHTMASK(after)) >> start)
  184. static char * cpu_flags[]
  185. = { "fpu", // "x87 FPU on chip"
  186. "vme", // "virtual-8086 mode enhancement"
  187. "de", // "debugging extensions"
  188. "pse", // "page size extensions"
  189. "tsc", // "time stamp counter"
  190. "msr", // "RDMSR and WRMSR support"
  191. "pae", // "physical address extensions"
  192. "mce", // "machine check exception"
  193. "cx8", // "CMPXCHG8B inst."
  194. "apic", // "APIC on chip"
  195. NULL,
  196. "sep", // "SYSENTER and SYSEXIT"
  197. "mtrr", // "memory type range registers"
  198. "pge", // "PTE global bit"
  199. "mca", // "machine check architecture"
  200. "cmov", // "conditional move/compare instruction"
  201. "pat", // "page attribute table"
  202. "pse36", // "page size extension"
  203. "pn", // "processor serial number"
  204. "clflush", // "CLFLUSH instruction"
  205. NULL,
  206. "dts" // "debug store"
  207. "tm", // "thermal monitor and clock ctrl"
  208. "mmx", // "MMX Technology"
  209. "fxsr", // "FXSAVE/FXRSTOR"
  210. "sse", // "SSE extensions"
  211. "sse2", // "SSE2 extensions"
  212. "ss", // "self snoop"
  213. "ht", // "hyper-threading / multi-core supported"
  214. "tm", // "therm. monitor"
  215. "ia64", // "IA64"
  216. "pbe", // "pending break event"
  217. };
  218. void _DkGetCPUInfo (PAL_CPU_INFO * ci)
  219. {
  220. unsigned int words[WORD_NUM];
  221. char * vendor_id = malloc(12);
  222. cpuid(0, 0, words);
  223. FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
  224. FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
  225. FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
  226. ci->cpu_vendor = vendor_id;
  227. char * brand = malloc(48);
  228. cpuid(0x80000002, 0, words);
  229. memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
  230. cpuid(0x80000003, 0, words);
  231. memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
  232. cpuid(0x80000004, 0, words);
  233. memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
  234. ci->cpu_brand = brand;
  235. cpuid(1, 0, words);
  236. ci->cpu_num = BIT_EXTRACT_LE(words[WORD_EBX], 16, 24);
  237. ci->cpu_family = BIT_EXTRACT_LE(words[WORD_EAX], 8, 12);
  238. ci->cpu_model = BIT_EXTRACT_LE(words[WORD_EAX], 4, 8);
  239. ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX], 0, 4);
  240. int flen = 0, fmax = 80;
  241. char * flags = malloc(fmax);
  242. for (int i = 0 ; i < 32 ; i++) {
  243. if (!cpu_flags[i])
  244. break;
  245. if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
  246. int len = strlen(cpu_flags[i]);
  247. if (flen + len + 1 > fmax) {
  248. char * new_flags = malloc(fmax * 2);
  249. memcpy(new_flags, flags, flen);
  250. free(flags);
  251. fmax *= 2;
  252. flags = new_flags;
  253. }
  254. memcpy(flags + flen, cpu_flags[i], len);
  255. flen += len;
  256. flags[flen++] = ' ';
  257. }
  258. }
  259. flags[flen ? flen - 1 : 0] = 0;
  260. ci->cpu_flags = flags;
  261. }