db_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. struct env {
  75. const char * str;
  76. int len, idx;
  77. } * envs = NULL;
  78. int nenvs = 0;
  79. if (pal_state.root_config)
  80. nenvs = get_config_entries(pal_state.root_config, "loader.env",
  81. cfgbuf, CONFIG_MAX);
  82. if (nenvs > 0) {
  83. envs = __alloca(sizeof(struct env) * nenvs);
  84. char * cfg = cfgbuf;
  85. for (int i = 0 ; i < nenvs ; i++) {
  86. int len = strlen(cfg);
  87. char * str = __alloca(len + 1);
  88. envs[i].str = str;
  89. envs[i].len = len;
  90. envs[i].idx = -1;
  91. memcpy(str, cfg, len + 1);
  92. cfg += len + 1;
  93. }
  94. }
  95. int envc = 0, add = nenvs;
  96. for (const char ** e = envp ; *e ; e++) {
  97. const char * s = *e, * p = s;
  98. envc++;
  99. while (*p && *p != '=')
  100. p++;
  101. int l = p - s;
  102. const char * v = p + 1;
  103. if (l > 8 &&
  104. s[0] == 'P' && s[1] == 'A' && s[2] == 'L' && s[3] == '_' &&
  105. s[4] == 'L' && s[5] == 'O' && s[6] == 'G' && s[7] == '_') {
  106. /* PAL_LOG_STREAM = */
  107. if (l == 14 && s[8] == 'S' && s[9] == 'T' &&
  108. s[10] == 'R' && s[11] == 'E' && s[12] == 'A' && s[13] == 'M') {
  109. PAL_HANDLE log;
  110. if (!_DkStreamOpen(&log, v, PAL_ACCESS_APPEND,
  111. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  112. PAL_CREAT_TRY, 0)) {
  113. if (pal_state.log_stream)
  114. _DkObjectClose(pal_state.log_stream);
  115. pal_state.log_stream = log;
  116. }
  117. }
  118. /* PAL_LOG_TYPES = */
  119. if (l == 13 && s[8] == 'T' && s[9] == 'Y' &&
  120. s[10] == 'P' && s[11] == 'E' && s[12] == 'S') {
  121. const char * t = v, * n;
  122. while (*t) {
  123. for (n = t ; *n && *n != ',' ; n++);
  124. switch (n - t) {
  125. case 4:
  126. if (t[0] == 'f' && t[1] == 'i' && t[2] == 'l' &&
  127. t[3] == 'e')
  128. pal_state.log_types |= LOG_FILE;
  129. if (t[0] == 'p' && t[1] == 'i' && t[2] == 'p' &&
  130. t[3] == 'e')
  131. pal_state.log_types |= LOG_PIPE;
  132. break;
  133. case 6:
  134. if (t[0] == 's' && t[1] == 'o' && t[2] == 'c' &&
  135. t[3] == 'k' && t[4] == 'e' && t[5] == 't')
  136. pal_state.log_types |= LOG_SOCKET;
  137. break;
  138. }
  139. t = *n ? n + 1 : n;
  140. }
  141. }
  142. continue;
  143. }
  144. for (int i = 0 ; i < nenvs ; i++)
  145. if (envs[i].len == l && !memcmp(envs[i].str, s, l)) {
  146. envs[i].idx = envc - 1;
  147. add--;
  148. break;
  149. }
  150. }
  151. if (add) {
  152. const char ** new_envp =
  153. malloc(sizeof(const char *) * (envc + add + 1));
  154. memcpy(new_envp, envp, sizeof(const char *) * envc);
  155. envp = new_envp;
  156. envp[envc + add] = NULL;
  157. }
  158. char key[CONFIG_MAX] = "loader.env.";
  159. const char ** ptr;
  160. for (int i = 0 ; i < nenvs ; i++) {
  161. const char * str = envs[i].str;
  162. int len = envs[i].len;
  163. int idx = envs[i].idx;
  164. int bytes;
  165. ptr = &envp[(idx == -1) ? envc++ : idx];
  166. memcpy(key + 11, str, len + 1);
  167. if ((bytes = get_config(pal_state.root_config, key, cfgbuf,
  168. CONFIG_MAX)) > 0) {
  169. char * e = malloc(len + bytes + 2);
  170. memcpy(e, str, len);
  171. e[len] = '=';
  172. memcpy(e + len + 1, cfgbuf, bytes + 1);
  173. *ptr = e;
  174. } else {
  175. char * e = malloc(len + 2);
  176. memcpy(e, str, len);
  177. e[len] = '=';
  178. e[len + 1] = 0;
  179. *ptr = e;
  180. }
  181. }
  182. *envpp = envp;
  183. }
  184. static void set_debug_type (void)
  185. {
  186. char cfgbuf[CONFIG_MAX];
  187. int ret;
  188. if (!pal_state.root_config)
  189. return;
  190. ret = get_config(pal_state.root_config, "loader.debug_type",
  191. cfgbuf, CONFIG_MAX);
  192. if (ret <= 0)
  193. return;
  194. PAL_HANDLE handle = NULL;
  195. if (!memcmp(cfgbuf, "inline", 7)) {
  196. ret = _DkStreamOpen(&handle, "dev:tty", PAL_ACCESS_RDWR, 0, 0, 0);
  197. if (ret < 0)
  198. init_fail(-ret, "cannot open debug stream");
  199. } else if (!memcmp(cfgbuf, "file", 5)) {
  200. ret = get_config(pal_state.root_config, "loader.debug_file",
  201. cfgbuf, CONFIG_MAX);
  202. if (ret <= 0)
  203. init_fail(PAL_ERROR_INVAL, "debug file not specified");
  204. ret = _DkStreamOpen(&handle, cfgbuf,
  205. PAL_ACCESS_RDWR,
  206. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  207. PAL_CREAT_TRY, 0);
  208. if (ret < 0)
  209. init_fail(-ret, "cannot open debug file");
  210. } else if (memcmp(cfgbuf, "none", 5)) {
  211. init_fail(PAL_ERROR_INVAL, "unknown debug type");
  212. }
  213. __pal_control.debug_stream = handle;
  214. }
  215. static void set_syscall_symbol (void)
  216. {
  217. char cfgbuf[CONFIG_MAX];
  218. int ret;
  219. if (!pal_state.root_config)
  220. return;
  221. ret = get_config(pal_state.root_config, "loader.syscall_symbol",
  222. cfgbuf, CONFIG_MAX);
  223. if (ret <= 0)
  224. return;
  225. pal_state.syscall_sym_name = remalloc(cfgbuf, ret + 1);
  226. }
  227. static int loader_filter (const char * key, int len)
  228. {
  229. return (key[0] == 'l' && key[1] == 'o' && key[2] == 'a' && key[3] == 'd' &&
  230. key[4] == 'e' && key[5] == 'r' && key[6] == '.') ? 0 : 1;
  231. }
  232. void pal_main (PAL_NUM pal_token, void * pal_addr,
  233. const char * pal_name,
  234. int argc, const char ** argv, const char ** envp,
  235. PAL_HANDLE parent_handle,
  236. PAL_HANDLE thread_handle,
  237. PAL_HANDLE exec_handle,
  238. PAL_HANDLE manifest_handle)
  239. {
  240. int ret;
  241. bool is_parent = !parent_handle;
  242. #if PROFILING == 1
  243. __pal_control.host_specific_startup_time =
  244. _DkSystemTimeQuery() - pal_state.start_time;
  245. #endif
  246. pal_state.pal_token = pal_token;
  247. pal_state.pal_addr = pal_addr;
  248. pal_state.parent_handle = parent_handle;
  249. pal_state.pagesize = _DkGetPagesize();
  250. pal_state.alloc_align = _DkGetAllocationAlignment();
  251. pal_state.alloc_shift = pal_state.alloc_align - 1;
  252. pal_state.alloc_mask = ~pal_state.alloc_shift;
  253. init_slab_mgr(pal_state.alloc_align);
  254. if (is_parent && !exec_handle && !manifest_handle) {
  255. printf("USAGE: %s [executable|manifest] args ...\n", pal_name);
  256. _DkProcessExit(0);
  257. return;
  258. }
  259. char * exec = NULL, * manifest = NULL;
  260. if (exec_handle) {
  261. exec = __alloca(URI_MAX);
  262. ret = _DkStreamGetName(exec_handle, exec, URI_MAX);
  263. if (ret < 0)
  264. init_fail(-ret, "cannot get executable name");
  265. }
  266. if (manifest_handle) {
  267. manifest = __alloca(URI_MAX);
  268. ret = _DkStreamGetName(manifest_handle, manifest, URI_MAX);
  269. if (ret < 0)
  270. init_fail(-ret, "cannot get manifest name");
  271. } else {
  272. if (is_parent) {
  273. #if PROFILING == 1
  274. unsigned long before_find_manifest = _DkSystemTimeQuery();
  275. #endif
  276. do {
  277. assert(!!exec);
  278. /* try open "<exec>.manifest" */
  279. manifest = __alloca(URI_MAX);
  280. snprintf(manifest, URI_MAX, "%s.manifest", exec);
  281. ret = _DkStreamOpen(&manifest_handle,
  282. manifest,
  283. PAL_ACCESS_RDONLY, 0, 0, 0);
  284. if (!ret)
  285. break;
  286. /* try open "file:manifest" */
  287. manifest = "file:manifest";
  288. ret = _DkStreamOpen(&manifest_handle,
  289. manifest,
  290. PAL_ACCESS_RDONLY, 0, 0, 0);
  291. if (!ret)
  292. break;
  293. /* well, there is no manifest file, leave it alone */
  294. if (!manifest_handle)
  295. printf("Can't fine any manifest, will run without one\n");
  296. } while (0);
  297. #if PROFILING == 1
  298. pal_state.manifest_loading_time +=
  299. _DkSystemTimeQuery() - before_find_manifest;
  300. #endif
  301. }
  302. }
  303. /* load manifest if there is one */
  304. if (manifest_handle && !pal_state.root_config) {
  305. #if PROFILING == 1
  306. unsigned long before_load_manifest = _DkSystemTimeQuery();
  307. #endif
  308. PAL_STREAM_ATTR attr;
  309. ret = _DkStreamAttributesQuerybyHandle(manifest_handle, &attr);
  310. if (ret < 0)
  311. init_fail(-ret, "cannot open manifest file");
  312. void * cfg_addr = NULL;
  313. int cfg_size = attr.pending_size;
  314. ret = _DkStreamMap(manifest_handle, &cfg_addr,
  315. PAL_PROT_READ, 0,
  316. ALLOC_ALIGNUP(cfg_size));
  317. if (ret < 0)
  318. init_fail(-ret, "cannot open manifest file");
  319. struct config_store * root_config = malloc(sizeof(struct config_store));
  320. root_config->raw_data = cfg_addr;
  321. root_config->raw_size = cfg_size;
  322. root_config->malloc = malloc;
  323. root_config->free = free;
  324. const char * errstring = NULL;
  325. if ((ret = read_config(root_config, loader_filter, &errstring)) < 0)
  326. init_fail(-ret, errstring);
  327. pal_state.root_config = root_config;
  328. #if PROFILING == 1
  329. pal_state.manifest_loading_time +=
  330. _DkSystemTimeQuery() - before_load_manifest;
  331. #endif
  332. }
  333. /* if there is no executable, try to find one in the manifest */
  334. if (is_parent && !exec_handle) {
  335. exec = __alloca(URI_MAX);
  336. assert(!!pal_state.root_config);
  337. ret = get_config(pal_state.root_config, "loader.exec", exec, URI_MAX);
  338. if (ret > 0) {
  339. ret = _DkStreamOpen(&exec_handle, exec, PAL_ACCESS_RDONLY,
  340. 0, 0, 0);
  341. if (ret < 0)
  342. init_fail(-ret, "cannot open executable");
  343. /* must be a ELF */
  344. if (check_elf_object(exec_handle) < 0)
  345. init_fail(PAL_ERROR_INVAL, "executable is not a ELF binary");
  346. } else {
  347. exec = NULL;
  348. }
  349. }
  350. pal_state.manifest = manifest;
  351. pal_state.manifest_handle = manifest_handle;
  352. pal_state.exec = exec;
  353. pal_state.exec_handle = exec_handle;
  354. const char * first_argv = *argv;
  355. argc--;
  356. argv++;
  357. if (is_parent && exec) {
  358. first_argv = exec;
  359. if (pal_state.root_config) {
  360. char cfgbuf[CONFIG_MAX];
  361. ret = get_config(pal_state.root_config, "loader.execname", cfgbuf,
  362. CONFIG_MAX);
  363. if (ret > 0)
  364. first_argv = remalloc(cfgbuf, ret + 1);
  365. }
  366. }
  367. read_envs(&envp);
  368. if (pal_state.log_stream && (pal_state.log_types & LOG_FILE)) {
  369. if (manifest)
  370. log_stream(manifest);
  371. if (exec)
  372. log_stream(exec);
  373. }
  374. if (pal_state.root_config)
  375. load_libraries();
  376. if (exec_handle) {
  377. #if PROFILING == 1
  378. unsigned long before_load_exec = _DkSystemTimeQuery();
  379. #endif
  380. ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC);
  381. if (ret < 0)
  382. init_fail(ret, PAL_STRERROR(ret));
  383. #if PROFILING == 1
  384. pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec;
  385. #endif
  386. }
  387. #if PROFILING == 1
  388. unsigned long before_tail = _DkSystemTimeQuery();
  389. #endif
  390. set_debug_type();
  391. set_syscall_symbol();
  392. __pal_control.process_id = _DkGetProcessId();
  393. __pal_control.host_id = _DkGetHostId();
  394. __pal_control.manifest_handle = manifest_handle;
  395. __pal_control.executable = exec;
  396. __pal_control.parent_process = parent_handle;
  397. __pal_control.first_thread = thread_handle;
  398. _DkGetAvailableUserAddressRange(&__pal_control.user_address.start,
  399. &__pal_control.user_address.end);
  400. __pal_control.pagesize = pal_state.pagesize;
  401. __pal_control.alloc_align = pal_state.alloc_align;
  402. __pal_control.broadcast_stream = _DkBroadcastStreamOpen();
  403. _DkGetCPUInfo(&__pal_control.cpu_info);
  404. __pal_control.mem_info.mem_total = _DkMemoryQuota();
  405. #if PROFILING == 1
  406. pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail;
  407. __pal_control.relocation_time = pal_state.relocation_time;
  408. __pal_control.linking_time = pal_state.linking_time;
  409. __pal_control.manifest_loading_time
  410. = pal_state.manifest_loading_time;
  411. __pal_control.allocation_time = pal_state.slab_time;
  412. __pal_control.child_creation_time = is_parent ? 0 : pal_state.start_time -
  413. pal_state.process_create_time;
  414. #endif
  415. /* Now we will start the execution */
  416. start_execution(first_argv, argc, argv, envp);
  417. /* We wish we will never reached here */
  418. init_fail(PAL_ERROR_DENIED, "unexpected termination");
  419. }
  420. void write_log (int nstrs, ...)
  421. {
  422. const char ** strs = __alloca(sizeof(const char *) * nstrs);
  423. int len = 0;
  424. va_list ap;
  425. va_start(ap, nstrs);
  426. for (int i = 0 ; i < nstrs ; i++) {
  427. strs[i] = va_arg(ap, const char *);
  428. len += strlen(strs[i]);
  429. }
  430. va_end(ap);
  431. char * buf = __alloca(len);
  432. int cnt = 0;
  433. for (int i = 0 ; i < nstrs ; i++) {
  434. int l = strlen(strs[i]);
  435. memcpy(buf + cnt, strs[i], l);
  436. cnt += l;
  437. }
  438. _DkStreamWrite(pal_state.log_stream, 0, cnt, buf, NULL, 0);
  439. }