db_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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.h"
  23. #include "pal_internal.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. #include <sysdeps/generic/ldsodefs.h>
  28. #include <elf/elf.h>
  29. PAL_CONTROL __pal_control;
  30. PAL_CONTROL * pal_control_addr (void)
  31. {
  32. return &__pal_control;
  33. }
  34. struct pal_internal_state pal_state;
  35. static void load_libraries (void)
  36. {
  37. /* we will not make any assumption for where the libraries are loaded */
  38. char cfgbuf[CONFIG_MAX];
  39. ssize_t len, ret = 0;
  40. /* loader.preload:
  41. any other libraries to preload. The can be multiple URIs,
  42. seperated by commas */
  43. len = get_config(pal_state.root_config, "loader.preload", cfgbuf,
  44. CONFIG_MAX);
  45. if (len <= 0)
  46. return;
  47. char * c = cfgbuf, * library_name = c;
  48. for (;; c++)
  49. if (*c == ',' || !(*c)) {
  50. if (c > library_name) {
  51. #if PROFILING == 1
  52. unsigned long before_load_library = _DkSystemTimeQuery();
  53. #endif
  54. *c = 0;
  55. if ((ret = load_elf_object(library_name, OBJECT_PRELOAD)) < 0)
  56. init_fail(-ret, "Unable to load preload library");
  57. #if PROFILING == 1
  58. pal_state.linking_time +=
  59. _DkSystemTimeQuery() - before_load_library;
  60. #endif
  61. }
  62. if (c == cfgbuf + len)
  63. break;
  64. library_name = c + 1;
  65. }
  66. }
  67. static void read_environments (const char *** envpp)
  68. {
  69. struct config_store * store = pal_state.root_config;
  70. const char ** envp = *envpp;
  71. char * cfgbuf;
  72. /* loader.env.*: rewriting host environment variables */
  73. struct setenv {
  74. const char * str;
  75. int len, idx;
  76. } * setenvs = NULL;
  77. int nsetenvs = 0;
  78. int size;
  79. if (!pal_state.root_config)
  80. return;
  81. ssize_t cfgsize = get_config_entries_size(store, "loader.env");
  82. /* XXX Propagate this error? */
  83. if (cfgsize < 0)
  84. return;
  85. cfgbuf = malloc(cfgsize);
  86. assert(cfgbuf);
  87. nsetenvs = get_config_entries(store, "loader.env", cfgbuf, cfgsize);
  88. if (nsetenvs <= 0) {
  89. free(cfgbuf);
  90. return;
  91. }
  92. setenvs = __alloca(sizeof(struct setenv) * nsetenvs);
  93. char * cfg = cfgbuf;
  94. for (int i = 0 ; i < nsetenvs ; i++) {
  95. size_t len = strlen(cfg);
  96. char * str = __alloca(len + 1);
  97. setenvs[i].str = str;
  98. setenvs[i].len = len;
  99. setenvs[i].idx = -1;
  100. memcpy(str, cfg, len + 1);
  101. cfg += len + 1;
  102. }
  103. int nenvs = 0, noverwrite = 0;
  104. for (const char ** e = envp ; *e ; e++, nenvs++)
  105. for (int i = 0 ; i < nsetenvs ; i++)
  106. if (!memcmp(setenvs[i].str, *e, setenvs[i].len) &&
  107. (*e)[setenvs[i].len] == '=') {
  108. setenvs[i].idx = nenvs;
  109. noverwrite++;
  110. break;
  111. }
  112. /* TODO: This code appears to rely on the memory buffer being zero-
  113. * initialized, so we use calloc here to get zeroed memory. We should
  114. * audit this code to verify that it's correct. */
  115. const char ** new_envp =
  116. calloc((nenvs + nsetenvs - noverwrite + 1), sizeof(const char *));
  117. memcpy(new_envp, envp, sizeof(const char *) * nenvs);
  118. envp = new_envp;
  119. char key[CONFIG_MAX] = "loader.env.";
  120. int prefix_len = static_strlen("loader.env.");
  121. const char ** ptr;
  122. free(cfgbuf);
  123. cfgbuf = __alloca(sizeof(char) * CONFIG_MAX);
  124. for (int i = 0 ; i < nsetenvs ; i++) {
  125. const char * str = setenvs[i].str;
  126. int len = setenvs[i].len;
  127. int idx = setenvs[i].idx;
  128. ssize_t bytes;
  129. ptr = &envp[(idx == -1) ? nenvs++ : idx];
  130. memcpy(key + prefix_len, str, len + 1);
  131. if ((bytes = get_config(store, key, cfgbuf, CONFIG_MAX)) > 0) {
  132. char * e = malloc(len + bytes + 2);
  133. memcpy(e, str, len);
  134. e[len] = '=';
  135. memcpy(e + len + 1, cfgbuf, bytes + 1);
  136. *ptr = e;
  137. } else {
  138. char * e = malloc(len + 2);
  139. memcpy(e, str, len);
  140. e[len] = '=';
  141. e[len + 1] = 0;
  142. *ptr = e;
  143. }
  144. }
  145. *envpp = envp;
  146. }
  147. static void set_debug_type (void)
  148. {
  149. char cfgbuf[CONFIG_MAX];
  150. ssize_t ret = 0;
  151. if (!pal_state.root_config)
  152. return;
  153. ret = get_config(pal_state.root_config, "loader.debug_type",
  154. cfgbuf, CONFIG_MAX);
  155. if (ret <= 0)
  156. return;
  157. PAL_HANDLE handle = NULL;
  158. if (strcmp_static(cfgbuf, "inline")) {
  159. ret = _DkStreamOpen(&handle, "dev:tty", PAL_ACCESS_RDWR, 0, 0, 0);
  160. goto out;
  161. }
  162. if (strcmp_static(cfgbuf, "file")) {
  163. ret = get_config(pal_state.root_config, "loader.debug_file",
  164. cfgbuf, CONFIG_MAX);
  165. if (ret <= 0)
  166. init_fail(PAL_ERROR_INVAL, "debug file not specified");
  167. ret = _DkStreamOpen(&handle, cfgbuf,
  168. PAL_ACCESS_RDWR,
  169. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  170. PAL_CREAT_TRY, 0);
  171. goto out;
  172. }
  173. if (strcmp_static(cfgbuf, "none"))
  174. goto out;
  175. init_fail(PAL_ERROR_INVAL, "unknown debug type");
  176. out:
  177. if (ret < 0)
  178. init_fail(-ret, "cannot open debug stream");
  179. __pal_control.debug_stream = handle;
  180. }
  181. static int loader_filter (const char * key, int len)
  182. {
  183. /* try to do this as fast as possible */
  184. return (key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  185. key[4] == 'e' && key[5] == 'r' && key[6] == '.') ? 0 : 1;
  186. }
  187. void start_execution (const char * first_argument, const char ** arguments,
  188. const char ** environments);
  189. /* 'pal_main' must be called by the host-specific bootloader */
  190. void pal_main (
  191. PAL_NUM instance_id, /* current instance id */
  192. PAL_HANDLE manifest_handle, /* manifest handle if opened */
  193. PAL_HANDLE exec_handle, /* executable handle if opened */
  194. PAL_PTR exec_loaded_addr, /* executable addr if loaded */
  195. PAL_HANDLE parent_process, /* parent process if it's a child */
  196. PAL_HANDLE first_thread, /* first thread handle */
  197. PAL_STR * arguments, /* application arguments */
  198. PAL_STR * environments /* environment variables */
  199. )
  200. {
  201. bool is_parent = (parent_process == NULL);
  202. #if PROFILING == 1
  203. __pal_control.host_specific_startup_time =
  204. _DkSystemTimeQuery() - pal_state.start_time;
  205. #endif
  206. pal_state.instance_id = instance_id;
  207. pal_state.pagesize = _DkGetPagesize();
  208. pal_state.alloc_align = _DkGetAllocationAlignment();
  209. pal_state.alloc_shift = pal_state.alloc_align - 1;
  210. pal_state.alloc_mask = ~pal_state.alloc_shift;
  211. init_slab_mgr(pal_state.alloc_align);
  212. pal_state.parent_process = parent_process;
  213. char uri_buf[URI_MAX];
  214. char * manifest_uri = NULL, * exec_uri = NULL;
  215. ssize_t ret;
  216. if (exec_handle) {
  217. ret = _DkStreamGetName(exec_handle, uri_buf, URI_MAX);
  218. if (ret < 0)
  219. init_fail(-ret, "cannot get executable name");
  220. exec_uri = malloc_copy(uri_buf, ret + 1);
  221. }
  222. if (manifest_handle) {
  223. ret = _DkStreamGetName(manifest_handle, uri_buf, URI_MAX);
  224. if (ret < 0)
  225. init_fail(-ret, "cannot get manifest name");
  226. manifest_uri = malloc_copy(uri_buf, ret + 1);
  227. goto has_manifest;
  228. }
  229. if (!exec_handle)
  230. init_fail(PAL_ERROR_INVAL, "Must have manifest or executable");
  231. #if PROFILING == 1
  232. unsigned long before_find_manifest = _DkSystemTimeQuery();
  233. #endif
  234. /* The rule is to only find the manifest in the current directory */
  235. /* try open "<execname>.manifest" */
  236. ret = get_base_name(exec_uri, uri_buf, URI_MAX);
  237. strcpy_static(uri_buf + ret, ".manifest", URI_MAX - ret);
  238. ret = _DkStreamOpen(&manifest_handle, uri_buf, PAL_ACCESS_RDONLY, 0, 0, 0);
  239. if (!ret)
  240. goto has_manifest;
  241. /* try open "file:manifest" */
  242. manifest_uri = "file:manifest";
  243. ret = _DkStreamOpen(&manifest_handle, manifest_uri, PAL_ACCESS_RDONLY,
  244. 0, 0, 0);
  245. if (!ret)
  246. goto has_manifest;
  247. #if PROFILING == 1
  248. pal_state.manifest_loading_time +=
  249. _DkSystemTimeQuery() - before_find_manifest;
  250. #endif
  251. /* well, there is no manifest file, leave it alone */
  252. printf("Can't find any manifest, will run without one.\n");
  253. has_manifest:
  254. /* load manifest if there is one */
  255. if (!pal_state.root_config && manifest_handle) {
  256. #if PROFILING == 1
  257. unsigned long before_load_manifest = _DkSystemTimeQuery();
  258. #endif
  259. PAL_STREAM_ATTR attr;
  260. ret = _DkStreamAttributesQuerybyHandle(manifest_handle, &attr);
  261. if (ret < 0)
  262. init_fail(-ret, "cannot open manifest file");
  263. void * cfg_addr = NULL;
  264. int cfg_size = attr.pending_size;
  265. ret = _DkStreamMap(manifest_handle, &cfg_addr,
  266. PAL_PROT_READ, 0,
  267. ALLOC_ALIGNUP(cfg_size));
  268. if (ret < 0)
  269. init_fail(-ret, "cannot open manifest file");
  270. struct config_store * root_config = malloc(sizeof(struct config_store));
  271. root_config->raw_data = cfg_addr;
  272. root_config->raw_size = cfg_size;
  273. root_config->malloc = malloc;
  274. root_config->free = free;
  275. const char * errstring = NULL;
  276. if ((ret = read_config(root_config, loader_filter, &errstring)) < 0)
  277. init_fail(-ret, errstring);
  278. pal_state.root_config = root_config;
  279. #if PROFILING == 1
  280. pal_state.manifest_loading_time +=
  281. _DkSystemTimeQuery() - before_load_manifest;
  282. #endif
  283. }
  284. /* if there is no executable, try to find one in the manifest */
  285. if (!exec_handle && pal_state.root_config) {
  286. ret = get_config(pal_state.root_config, "loader.exec",
  287. uri_buf, URI_MAX);
  288. if (ret > 0) {
  289. exec_uri = malloc_copy(uri_buf, ret + 1);
  290. ret = _DkStreamOpen(&exec_handle, exec_uri, PAL_ACCESS_RDONLY,
  291. 0, 0, 0);
  292. if (ret < 0)
  293. init_fail(-ret, "cannot open executable");
  294. }
  295. }
  296. /* If we still don't have an exec in the manifest, but we have a manifest
  297. * try implicitly from the manifest name */
  298. if ((!exec_handle) && manifest_uri) {
  299. size_t manifest_strlen = strlen(manifest_uri);
  300. size_t exec_strlen = manifest_strlen - 9;
  301. int success = 0;
  302. // Try .manifest
  303. if (strcmp_static(&manifest_uri[exec_strlen], ".manifest")) {
  304. success = 1;
  305. } else {
  306. exec_strlen -= 4;
  307. if (strcmp_static(&manifest_uri[exec_strlen], ".manifest.sgx")) {
  308. success = 1;
  309. }
  310. }
  311. if (success) {
  312. exec_uri = malloc(exec_strlen + 1);
  313. if (!exec_uri)
  314. init_fail(-PAL_ERROR_NOMEM, "Cannot allocate URI buf");
  315. memcpy (exec_uri, manifest_uri, exec_strlen);
  316. exec_uri[exec_strlen] = '\0';
  317. ret = _DkStreamOpen(&exec_handle, exec_uri, PAL_ACCESS_RDONLY,
  318. 0, 0, 0);
  319. // DEP 3/20/17: There are cases where we want to let
  320. // the PAL start up without a main executable. Don't
  321. // die here, just free the exec_uri buffer.
  322. if (ret < 0) {
  323. free(exec_uri);
  324. exec_uri = NULL;
  325. }
  326. }
  327. }
  328. /* must be a ELF */
  329. if (exec_handle && check_elf_object(exec_handle) < 0)
  330. init_fail(PAL_ERROR_INVAL, "executable is not a ELF binary");
  331. pal_state.manifest = manifest_uri;
  332. pal_state.manifest_handle = manifest_handle;
  333. pal_state.exec = exec_uri;
  334. pal_state.exec_handle = exec_handle;
  335. const char * first_argument =
  336. (is_parent && exec_uri) ? exec_uri : *arguments;
  337. arguments++;
  338. if (pal_state.root_config) {
  339. char cfgbuf[CONFIG_MAX];
  340. ret = get_config(pal_state.root_config, "loader.execname", cfgbuf,
  341. CONFIG_MAX);
  342. if (ret > 0)
  343. first_argument = malloc_copy(cfgbuf, ret + 1);
  344. }
  345. read_environments(&environments);
  346. if (pal_state.root_config)
  347. load_libraries();
  348. if (exec_handle) {
  349. #if PROFILING == 1
  350. unsigned long before_load_exec = _DkSystemTimeQuery();
  351. #endif
  352. if (exec_loaded_addr) {
  353. ret = add_elf_object(exec_loaded_addr, exec_handle, OBJECT_EXEC);
  354. } else {
  355. ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC);
  356. }
  357. if (ret < 0)
  358. init_fail(ret, PAL_STRERROR(ret));
  359. #if PROFILING == 1
  360. pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec;
  361. #endif
  362. }
  363. #if PROFILING == 1
  364. unsigned long before_tail = _DkSystemTimeQuery();
  365. #endif
  366. set_debug_type();
  367. __pal_control.host_type = XSTRINGIFY(HOST_TYPE);
  368. __pal_control.process_id = _DkGetProcessId();
  369. __pal_control.host_id = _DkGetHostId();
  370. __pal_control.manifest_handle = manifest_handle;
  371. __pal_control.executable = exec_uri;
  372. __pal_control.parent_process = parent_process;
  373. __pal_control.first_thread = first_thread;
  374. _DkGetAvailableUserAddressRange(&__pal_control.user_address.start,
  375. &__pal_control.user_address.end);
  376. __pal_control.pagesize = pal_state.pagesize;
  377. __pal_control.alloc_align = pal_state.alloc_align;
  378. __pal_control.broadcast_stream = _DkBroadcastStreamOpen();
  379. _DkGetCPUInfo(&__pal_control.cpu_info);
  380. __pal_control.mem_info.mem_total = _DkMemoryQuota();
  381. #if PROFILING == 1
  382. pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail;
  383. __pal_control.relocation_time = pal_state.relocation_time;
  384. __pal_control.linking_time = pal_state.linking_time;
  385. __pal_control.manifest_loading_time
  386. = pal_state.manifest_loading_time;
  387. __pal_control.allocation_time = pal_state.slab_time;
  388. __pal_control.child_creation_time = is_parent ? 0 : pal_state.start_time -
  389. pal_state.process_create_time;
  390. #endif
  391. /* Now we will start the execution */
  392. start_execution(first_argument, arguments, environments);
  393. /* We wish we will never reached here */
  394. init_fail(PAL_ERROR_DENIED, "unexpected termination");
  395. }
  396. void write_log (int nstrs, ...)
  397. {
  398. const char ** strs = __alloca(sizeof(const char *) * nstrs);
  399. int len = 0;
  400. va_list ap;
  401. va_start(ap, nstrs);
  402. for (int i = 0 ; i < nstrs ; i++) {
  403. strs[i] = va_arg(ap, char *);
  404. len += strlen(strs[i]);
  405. }
  406. va_end(ap);
  407. char * buf = __alloca(len);
  408. int cnt = 0;
  409. for (int i = 0 ; i < nstrs ; i++) {
  410. int l = strlen(strs[i]);
  411. memcpy(buf + cnt, strs[i], l);
  412. cnt += l;
  413. }
  414. _DkStreamWrite(pal_state.log_stream, 0, cnt, buf, NULL, 0);
  415. }