db_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.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. #include <bits/dlfcn.h>
  30. PAL_CONTROL __pal_control;
  31. PAL_CONTROL * pal_control_addr (void)
  32. {
  33. return &__pal_control;
  34. }
  35. struct pal_internal_state pal_state;
  36. static void load_libraries (void)
  37. {
  38. /* we will not make any assumption for where the libraries are loaded */
  39. char cfgbuf[CONFIG_MAX];
  40. int len, ret = 0;
  41. /* loader.preload:
  42. any other libraries to preload. The can be multiple URIs,
  43. seperated by commas */
  44. len = get_config(pal_state.root_config, "loader.preload", cfgbuf,
  45. CONFIG_MAX);
  46. if (len <= 0)
  47. return;
  48. char * c = cfgbuf, * library_name = c;
  49. for (;; c++)
  50. if (*c == ',' || !(*c)) {
  51. if (c > library_name) {
  52. #if PROFILING == 1
  53. unsigned long before_load_library = _DkSystemTimeQuery();
  54. #endif
  55. *c = 0;
  56. if ((ret = load_elf_object(library_name, OBJECT_PRELOAD)) < 0)
  57. init_fail(-ret, "Unable to load preload library");
  58. #if PROFILING == 1
  59. pal_state.linking_time +=
  60. _DkSystemTimeQuery() - before_load_library;
  61. #endif
  62. }
  63. if (c == cfgbuf + len)
  64. break;
  65. library_name = c + 1;
  66. }
  67. }
  68. static void read_envs (const char *** envpp)
  69. {
  70. const char ** envp = *envpp;
  71. char cfgbuf[CONFIG_MAX];
  72. /* loader.env.* and loader.exclude.env: filtering host environment
  73. * variables */
  74. int nenvs = get_config_entries(pal_state.root_config, "loader.env",
  75. cfgbuf, CONFIG_MAX);
  76. if (nenvs > 0) {
  77. struct env { const char * str; int len, idx; } * envs
  78. = __alloca(sizeof(struct env) * nenvs);
  79. char * cfg = cfgbuf;
  80. for (int i = 0 ; i < nenvs ; i++) {
  81. int len = strlen(cfg);
  82. char * str = __alloca(len + 1);
  83. envs[i].str = str;
  84. envs[i].len = len;
  85. envs[i].idx = -1;
  86. memcpy(str, cfg, len + 1);
  87. cfg += len + 1;
  88. }
  89. int envc = 0, add = nenvs;
  90. for (const char ** e = envp ; *e ; e++) {
  91. envc++;
  92. const char * p = *e;
  93. while (*p && *p != '=')
  94. p++;
  95. for (int i = 0 ; i < nenvs ; i++)
  96. if (envs[i].len == p - *e && !memcmp(envs[i].str, *e, p - *e)) {
  97. envs[i].idx = envc - 1;
  98. add--;
  99. break;
  100. }
  101. }
  102. if (add) {
  103. const char ** new_envp =
  104. malloc(sizeof(const char *) * (envc + add + 1));
  105. memcpy(new_envp, envp, sizeof(const char *) * envc);
  106. envp = new_envp;
  107. envp[envc + add] = NULL;
  108. }
  109. char key[CONFIG_MAX] = "loader.env.";
  110. const char ** ptr;
  111. for (int i = 0 ; i < nenvs ; i++) {
  112. const char * str = envs[i].str;
  113. int len = envs[i].len;
  114. int idx = envs[i].idx;
  115. int bytes;
  116. ptr = &envp[(idx == -1) ? envc++ : idx];
  117. memcpy(key + 11, str, len + 1);
  118. if ((bytes = get_config(pal_state.root_config, key, cfgbuf,
  119. CONFIG_MAX)) > 0) {
  120. char * e = malloc(len + bytes + 2);
  121. memcpy(e, str, len);
  122. e[len] = '=';
  123. memcpy(e + len + 1, cfgbuf, bytes + 1);
  124. *ptr = e;
  125. } else {
  126. char * e = malloc(len + 2);
  127. memcpy(e, str, len);
  128. e[len] = '=';
  129. e[len + 1] = 0;
  130. *ptr = e;
  131. }
  132. }
  133. }
  134. *envpp = envp;
  135. }
  136. static void set_debug_type (void)
  137. {
  138. char cfgbuf[CONFIG_MAX];
  139. int ret;
  140. ret = get_config(pal_state.root_config, "loader.debug_type",
  141. cfgbuf, CONFIG_MAX);
  142. if (ret <= 0)
  143. return;
  144. PAL_HANDLE handle = NULL;
  145. if (!memcmp(cfgbuf, "inline", 7)) {
  146. ret = _DkStreamOpen(&handle, "dev:tty", PAL_ACCESS_RDWR, 0, 0, 0);
  147. if (ret < 0)
  148. init_fail(-ret, "cannot open debug stream");
  149. } else if (!memcmp(cfgbuf, "file", 5)) {
  150. ret = get_config(pal_state.root_config, "loader.debug_file",
  151. cfgbuf, CONFIG_MAX);
  152. if (ret <= 0)
  153. init_fail(PAL_ERROR_INVAL, "debug file not specified");
  154. ret = _DkStreamOpen(&handle, cfgbuf,
  155. PAL_ACCESS_RDWR,
  156. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  157. PAL_CREAT_TRY, 0);
  158. if (ret < 0)
  159. init_fail(-ret, "cannot open debug file");
  160. } else if (memcmp(cfgbuf, "none", 5)) {
  161. init_fail(PAL_ERROR_INVAL, "unknown debug type");
  162. }
  163. __pal_control.debug_stream = handle;
  164. }
  165. static void set_syscall_symbol (void)
  166. {
  167. char cfgbuf[CONFIG_MAX];
  168. int ret;
  169. ret = get_config(pal_state.root_config, "loader.syscall_symbol",
  170. cfgbuf, CONFIG_MAX);
  171. if (ret <= 0)
  172. return;
  173. pal_state.syscall_sym_name = remalloc(cfgbuf, ret + 1);
  174. }
  175. static int loader_filter (const char * key, int len)
  176. {
  177. return (key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  178. key[4] == 'e' && key[5] == 'r' && key[6] == '.') ? 0 : 1;
  179. }
  180. void pal_main (PAL_NUM pal_token, void * pal_addr,
  181. const char * pal_name,
  182. int argc, const char ** argv, const char ** envp,
  183. PAL_HANDLE parent_handle,
  184. PAL_HANDLE thread_handle,
  185. PAL_HANDLE exec_handle,
  186. PAL_HANDLE manifest_handle)
  187. {
  188. int ret;
  189. bool is_parent = !parent_handle;
  190. #if PROFILING == 1
  191. __pal_control.host_specific_startup_time =
  192. _DkSystemTimeQuery() - pal_state.start_time;
  193. #endif
  194. pal_state.pal_token = pal_token;
  195. pal_state.pal_addr = pal_addr;
  196. pal_state.parent_handle = parent_handle;
  197. pal_state.pagesize = _DkGetPagesize();
  198. pal_state.alloc_align = _DkGetAllocationAlignment();
  199. pal_state.alloc_shift = pal_state.alloc_align - 1;
  200. pal_state.alloc_mask = ~pal_state.alloc_shift;
  201. init_slab_mgr(pal_state.alloc_align);
  202. char * exec = NULL, * manifest = NULL;
  203. if (exec_handle) {
  204. exec = __alloca(URI_MAX);
  205. ret = _DkStreamGetName(exec_handle, exec, URI_MAX);
  206. if (ret < 0)
  207. init_fail(-ret, "cannot get executable name");
  208. }
  209. if (manifest_handle) {
  210. manifest = __alloca(URI_MAX);
  211. ret = _DkStreamGetName(manifest_handle, manifest, URI_MAX);
  212. if (ret < 0)
  213. init_fail(-ret, "cannot get manifest name");
  214. } else {
  215. if (is_parent) {
  216. #if PROFILING == 1
  217. unsigned long before_find_manifest = _DkSystemTimeQuery();
  218. #endif
  219. do {
  220. if (exec_handle) {
  221. assert(!!exec);
  222. /* try open "<exec>.manifest" */
  223. manifest = __alloca(URI_MAX);
  224. snprintf(manifest, URI_MAX, "%s.manifest", exec);
  225. ret = _DkStreamOpen(&manifest_handle,
  226. manifest,
  227. PAL_ACCESS_RDONLY, 0, 0, 0);
  228. if (!ret)
  229. break;
  230. }
  231. /* try open "file:manifest" */
  232. manifest = "file:manifest";
  233. ret = _DkStreamOpen(&manifest_handle,
  234. manifest,
  235. PAL_ACCESS_RDONLY, 0, 0, 0);
  236. if (!ret)
  237. break;
  238. /* well, there is no manifest file, leave it alone */
  239. if (!manifest_handle)
  240. printf("Can't fine any manifest, will run without one\n");
  241. } while (0);
  242. #if PROFILING == 1
  243. pal_state.manifest_loading_time +=
  244. _DkSystemTimeQuery() - before_find_manifest;
  245. #endif
  246. }
  247. }
  248. /* load manifest if there is one */
  249. if (manifest_handle) {
  250. #if PROFILING == 1
  251. unsigned long before_load_manifest = _DkSystemTimeQuery();
  252. #endif
  253. PAL_STREAM_ATTR attr;
  254. ret = _DkStreamAttributesQuerybyHandle(manifest_handle, &attr);
  255. if (ret < 0)
  256. init_fail(-ret, "cannot open manifest file");
  257. void * cfg_addr = NULL;
  258. int cfg_size = attr.pending_size;
  259. ret = _DkStreamMap(manifest_handle, &cfg_addr,
  260. PAL_PROT_READ, 0,
  261. ALLOC_ALIGNUP(cfg_size));
  262. if (ret < 0)
  263. init_fail(-ret, "cannot open manifest file");
  264. struct config_store * root_config = malloc(sizeof(struct config_store));
  265. root_config->raw_data = cfg_addr;
  266. root_config->raw_size = cfg_size;
  267. root_config->malloc = malloc;
  268. root_config->free = free;
  269. const char * errstring = NULL;
  270. if ((ret = read_config(root_config, loader_filter, &errstring)) < 0)
  271. init_fail(-ret, errstring);
  272. pal_state.root_config = root_config;
  273. #if PROFILING == 1
  274. pal_state.manifest_loading_time +=
  275. _DkSystemTimeQuery() - before_load_manifest;
  276. #endif
  277. }
  278. /* if there is no executable, try to find one in the manifest */
  279. if (is_parent && !exec_handle) {
  280. exec = __alloca(URI_MAX);
  281. assert(!!pal_state.root_config);
  282. ret = get_config(pal_state.root_config, "loader.exec", exec, URI_MAX);
  283. if (ret > 0) {
  284. ret = _DkStreamOpen(&exec_handle, exec, PAL_ACCESS_RDONLY,
  285. 0, 0, 0);
  286. if (ret < 0)
  287. init_fail(-ret, "cannot open executable");
  288. /* must be a ELF */
  289. if (check_elf_object(exec_handle) < 0)
  290. init_fail(PAL_ERROR_INVAL, "executable is not a ELF binary");
  291. } else {
  292. exec = NULL;
  293. }
  294. }
  295. if (is_parent && !exec_handle && !manifest_handle) {
  296. printf("USAGE: %s [executable|manifest] args ...\n", pal_name);
  297. _DkProcessExit(0);
  298. return;
  299. }
  300. pal_state.manifest = manifest;
  301. pal_state.manifest_handle = manifest_handle;
  302. pal_state.exec = exec;
  303. pal_state.exec_handle = exec_handle;
  304. const char * first_argv = *argv;
  305. argc--;
  306. argv++;
  307. if (is_parent && exec_handle) {
  308. first_argv = exec;
  309. if (pal_state.root_config) {
  310. char cfgbuf[CONFIG_MAX];
  311. ret = get_config(pal_state.root_config, "loader.execname", cfgbuf,
  312. CONFIG_MAX);
  313. if (ret > 0)
  314. first_argv = remalloc(cfgbuf, ret + 1);
  315. }
  316. }
  317. if (pal_state.root_config)
  318. load_libraries();
  319. if (exec_handle) {
  320. #if PROFILING == 1
  321. unsigned long before_load_exec = _DkSystemTimeQuery();
  322. #endif
  323. ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC);
  324. if (ret < 0)
  325. init_fail(ret, PAL_STRERROR(ret));
  326. #if PROFILING == 1
  327. pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec;
  328. #endif
  329. }
  330. #if PROFILING == 1
  331. unsigned long before_tail = _DkSystemTimeQuery();
  332. #endif
  333. if (pal_state.root_config) {
  334. read_envs(&envp);
  335. set_debug_type();
  336. set_syscall_symbol();
  337. }
  338. __pal_control.process_id = _DkGetProcessId();
  339. __pal_control.host_id = _DkGetHostId();
  340. __pal_control.manifest_handle = manifest_handle;
  341. __pal_control.executable = exec;
  342. __pal_control.parent_process = parent_handle;
  343. __pal_control.first_thread = thread_handle;
  344. _DkGetAvailableUserAddressRange(&__pal_control.user_address.start,
  345. &__pal_control.user_address.end);
  346. __pal_control.pagesize = pal_state.pagesize;
  347. __pal_control.alloc_align = pal_state.alloc_align;
  348. __pal_control.broadcast_stream = _DkBroadcastStreamOpen();
  349. _DkGetCPUInfo(&__pal_control.cpu_info);
  350. __pal_control.mem_info.mem_total = _DkMemoryQuota();
  351. #if PROFILING == 1
  352. pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail;
  353. __pal_control.relocation_time = pal_state.relocation_time;
  354. __pal_control.linking_time = pal_state.linking_time;
  355. __pal_control.manifest_loading_time
  356. = pal_state.manifest_loading_time;
  357. __pal_control.allocation_time = pal_state.slab_time;
  358. __pal_control.child_creation_time = is_parent ? 0 : pal_state.start_time -
  359. pal_state.process_create_time;
  360. #endif
  361. /* Now we will start the execution */
  362. start_execution(first_argv, argc, argv, envp);
  363. /* We wish we will never reached here */
  364. init_fail(PAL_ERROR_DENIED, "unexpected termination");
  365. }