db_main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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_untrusted_slab_mgr (int pagesize);
  67. int init_enclave (void);
  68. int init_enclave_key (void);
  69. int init_child_process (PAL_HANDLE * parent_handle);
  70. static PAL_HANDLE setup_file_handle (const char * name, int fd)
  71. {
  72. if (!strpartcmp_static(name, "file:"))
  73. return NULL;
  74. name += static_strlen("file:");
  75. int len = strlen(name);
  76. PAL_HANDLE handle = malloc(HANDLE_SIZE(file) + len + 1);
  77. SET_HANDLE_TYPE(handle, file);
  78. HANDLE_HDR(handle)->flags |= RFD(0);
  79. handle->file.fd = fd;
  80. handle->file.append = 0;
  81. handle->file.pass = 0;
  82. char * path = (void *) handle + HANDLE_SIZE(file);
  83. get_norm_path(name, path, 0, len + 1);
  84. handle->file.realpath = path;
  85. handle->file.total = 0;
  86. handle->file.stubs = NULL;
  87. return handle;
  88. }
  89. static int loader_filter (const char * key, int len)
  90. {
  91. if (key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  92. key[4] == 'e' && key[5] == 'r' && key[6] == '.')
  93. return 0;
  94. if (key[0] == 's' && key[1] == 'g' && key[2] == 'x' && key[3] == '.')
  95. return 0;
  96. return 1;
  97. }
  98. extern void * enclave_base;
  99. void pal_linux_main(const char ** arguments, const char ** environments,
  100. struct pal_sec * sec_info)
  101. {
  102. PAL_HANDLE parent = NULL;
  103. unsigned long start_time = _DkSystemTimeQuery();
  104. /* relocate PAL itself */
  105. pal_map.l_addr = (ElfW(Addr)) sec_info->enclave_addr;
  106. pal_map.l_name = sec_info->enclave_image;
  107. elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
  108. pal_map.l_info, pal_map.l_addr);
  109. ELF_DYNAMIC_RELOCATE(&pal_map);
  110. memcpy(&pal_sec, sec_info, sizeof(struct pal_sec));
  111. /* set up page allocator and slab manager */
  112. init_slab_mgr(pagesz);
  113. init_untrusted_slab_mgr(pagesz);
  114. init_pages();
  115. init_enclave_key();
  116. /* now we can add a link map for PAL itself */
  117. setup_pal_map(&pal_map);
  118. /* initialize enclave properties */
  119. init_enclave();
  120. pal_state.start_time = start_time;
  121. /* if there is a parent, create parent handle */
  122. if (pal_sec.ppid) {
  123. if (init_child_process(&parent) < 0)
  124. ocall_exit();
  125. }
  126. linux_state.uid = pal_sec.uid;
  127. linux_state.gid = pal_sec.gid;
  128. linux_state.process_id = (start_time & (~0xffff)) | pal_sec.pid;
  129. /* now let's mark our enclave as initialized */
  130. pal_enclave_state.enclave_flags |= PAL_ENCLAVE_INITIALIZED;
  131. /* create executable handle */
  132. PAL_HANDLE manifest, exec = NULL;
  133. /* create manifest handle */
  134. manifest =
  135. setup_file_handle(pal_sec.manifest_name, pal_sec.manifest_fd);
  136. if (pal_sec.exec_fd != PAL_IDX_POISON) {
  137. exec = setup_file_handle(pal_sec.exec_name, pal_sec.exec_fd);
  138. } else {
  139. SGX_DBG(DBG_I, "Run without executable\n");
  140. }
  141. /* parse manifest data into config storage */
  142. struct config_store * root_config =
  143. malloc(sizeof(struct config_store));
  144. root_config->raw_data = pal_sec.manifest_addr;
  145. root_config->raw_size = pal_sec.manifest_size;
  146. root_config->malloc = malloc;
  147. root_config->free = free;
  148. const char * errstring = NULL;
  149. if (read_config(root_config, loader_filter, &errstring) < 0) {
  150. SGX_DBG(DBG_E, "Can't read manifest: %s\n", errstring);
  151. ocall_exit();
  152. }
  153. pal_state.root_config = root_config;
  154. __pal_control.manifest_preload.start = (PAL_PTR) pal_sec.manifest_addr;
  155. __pal_control.manifest_preload.end = (PAL_PTR) pal_sec.manifest_addr +
  156. pal_sec.manifest_size;
  157. init_trusted_files();
  158. init_trusted_children();
  159. #if PRINT_ENCLAVE_STAT == 1
  160. printf(" >>>>>>>> "
  161. "Enclave loading time = %10ld milliseconds\n",
  162. _DkSystemTimeQuery() - sec_info->start_time);
  163. #endif
  164. /* set up thread handle */
  165. PAL_HANDLE first_thread = malloc(HANDLE_SIZE(thread));
  166. SET_HANDLE_TYPE(first_thread, thread);
  167. first_thread->thread.tcs =
  168. enclave_base + GET_ENCLAVE_TLS(tcs_offset);
  169. SET_ENCLAVE_TLS(thread, (__pal_control.first_thread = first_thread));
  170. /* call main function */
  171. pal_main(pal_sec.instance_id, manifest, exec,
  172. pal_sec.exec_addr, parent, first_thread,
  173. arguments, environments);
  174. }
  175. /* the following code is borrowed from CPUID */
  176. #define WORD_EAX 0
  177. #define WORD_EBX 1
  178. #define WORD_ECX 2
  179. #define WORD_EDX 3
  180. #define WORD_NUM 4
  181. static void cpuid (unsigned int leaf, unsigned int subleaf,
  182. unsigned int words[])
  183. {
  184. _DkCpuIdRetrieve(leaf, subleaf, words);
  185. }
  186. #define FOUR_CHARS_VALUE(s, w) \
  187. (s)[0] = (w) & 0xff; \
  188. (s)[1] = ((w) >> 8) & 0xff; \
  189. (s)[2] = ((w) >> 16) & 0xff; \
  190. (s)[3] = ((w) >> 24) & 0xff;
  191. #define BPI 32
  192. #define POWER2(power) \
  193. (1ULL << (power))
  194. #define RIGHTMASK(width) \
  195. (((unsigned long) (width) >= BPI) ? ~0ULL : POWER2(width)-1ULL)
  196. #define BIT_EXTRACT_LE(value, start, after) \
  197. (((unsigned long) (value) & RIGHTMASK(after)) >> start)
  198. static char * cpu_flags[]
  199. = { "fpu", // "x87 FPU on chip"
  200. "vme", // "virtual-8086 mode enhancement"
  201. "de", // "debugging extensions"
  202. "pse", // "page size extensions"
  203. "tsc", // "time stamp counter"
  204. "msr", // "RDMSR and WRMSR support"
  205. "pae", // "physical address extensions"
  206. "mce", // "machine check exception"
  207. "cx8", // "CMPXCHG8B inst."
  208. "apic", // "APIC on chip"
  209. NULL,
  210. "sep", // "SYSENTER and SYSEXIT"
  211. "mtrr", // "memory type range registers"
  212. "pge", // "PTE global bit"
  213. "mca", // "machine check architecture"
  214. "cmov", // "conditional move/compare instruction"
  215. "pat", // "page attribute table"
  216. "pse36", // "page size extension"
  217. "pn", // "processor serial number"
  218. "clflush", // "CLFLUSH instruction"
  219. NULL,
  220. "dts" // "debug store"
  221. "tm", // "thermal monitor and clock ctrl"
  222. "mmx", // "MMX Technology"
  223. "fxsr", // "FXSAVE/FXRSTOR"
  224. "sse", // "SSE extensions"
  225. "sse2", // "SSE2 extensions"
  226. "ss", // "self snoop"
  227. "ht", // "hyper-threading / multi-core supported"
  228. "tm", // "therm. monitor"
  229. "ia64", // "IA64"
  230. "pbe", // "pending break event"
  231. };
  232. void _DkGetCPUInfo (PAL_CPU_INFO * ci)
  233. {
  234. unsigned int words[WORD_NUM];
  235. char * vendor_id = malloc(12);
  236. cpuid(0, 0, words);
  237. FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
  238. FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
  239. FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
  240. ci->cpu_vendor = vendor_id;
  241. char * brand = malloc(48);
  242. cpuid(0x80000002, 0, words);
  243. memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
  244. cpuid(0x80000003, 0, words);
  245. memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
  246. cpuid(0x80000004, 0, words);
  247. memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
  248. ci->cpu_brand = brand;
  249. cpuid(1, 0, words);
  250. ci->cpu_num = BIT_EXTRACT_LE(words[WORD_EBX], 16, 24);
  251. ci->cpu_family = BIT_EXTRACT_LE(words[WORD_EAX], 8, 12);
  252. ci->cpu_model = BIT_EXTRACT_LE(words[WORD_EAX], 4, 8);
  253. ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX], 0, 4);
  254. int flen = 0, fmax = 80;
  255. char * flags = malloc(fmax);
  256. for (int i = 0 ; i < 32 ; i++) {
  257. if (!cpu_flags[i])
  258. break;
  259. if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
  260. int len = strlen(cpu_flags[i]);
  261. if (flen + len + 1 > fmax) {
  262. char * new_flags = malloc(fmax * 2);
  263. memcpy(new_flags, flags, flen);
  264. free(flags);
  265. fmax *= 2;
  266. flags = new_flags;
  267. }
  268. memcpy(flags + flen, cpu_flags[i], len);
  269. flen += len;
  270. flags[flen++] = ' ';
  271. }
  272. }
  273. flags[flen ? flen - 1 : 0] = 0;
  274. ci->cpu_flags = flags;
  275. }