shim_init.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. * shim_init.c
  17. *
  18. * This file contains entry and exit functions of library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_tls.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_vma.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_fs.h>
  27. #include <shim_ipc.h>
  28. #include <shim_profile.h>
  29. #include <pal.h>
  30. #include <pal_debug.h>
  31. #include <pal_error.h>
  32. #include <sys/mman.h>
  33. #include <asm/unistd.h>
  34. #include <asm/fcntl.h>
  35. unsigned long allocsize;
  36. unsigned long allocshift;
  37. unsigned long allocmask;
  38. /* The following constants will help matching glibc version with compatible
  39. SHIM libraries */
  40. #include "glibc-version.h"
  41. const unsigned int glibc_vers_2_17 = GLIBC_VERSION_2_17;
  42. static void handle_failure (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  43. {
  44. SHIM_GET_TLS()->pal_errno = (arg <= PAL_ERROR_BOUND) ? arg : 0;
  45. }
  46. void __assert_fail (const char * assertion, const char * file,
  47. unsigned int line, const char * function)
  48. {
  49. __sys_printf("assert failed %s:%d %s\n", file, line, assertion);
  50. pause();
  51. shim_terminate();
  52. }
  53. void __stack_chk_fail (void)
  54. {
  55. }
  56. static int pal_errno_to_unix_errno [PAL_ERROR_BOUND + 1] = {
  57. /* reserved */ 0,
  58. /* PAL_ERROR_NOTIMPLEMENTED */ ENOSYS,
  59. /* PAL_ERROR_NOTDEFINED */ ENOSYS,
  60. /* PAL_ERROR_NOTSUPPORT */ EACCES,
  61. /* PAL_ERROR_INVAL */ EINVAL,
  62. /* PAL_ERROR_TOOLONG */ ENAMETOOLONG,
  63. /* PAL_ERROR_DENIED */ EACCES,
  64. /* PAL_ERROR_BADHANDLE */ EFAULT,
  65. /* PAL_ERROR_STREAMEXIST */ EEXIST,
  66. /* PAL_ERROR_STREAMNOTEXIST */ ENOENT,
  67. /* PAL_ERROR_STREAMISFILE */ ENOTDIR,
  68. /* PAL_ERROR_STREAMISDIR */ EISDIR,
  69. /* PAL_ERROR_STREAMISDEVICE */ ESPIPE,
  70. /* PAL_ERROR_INTERRUPTED */ EINTR,
  71. /* PAL_ERROR_OVERFLOW */ EFAULT,
  72. /* PAL_ERROR_BADADDR */ EFAULT,
  73. /* PAL_ERROR_NOMEM */ ENOMEM,
  74. /* PAL_ERROR_NOTKILLABLE */ EACCES,
  75. /* PAL_ERROR_INCONSIST */ EFAULT,
  76. /* PAL_ERROR_TRYAGAIN */ EAGAIN,
  77. /* PAL_ERROR_ENDOFSTREAM */ 0,
  78. /* PAL_ERROR_NOTSERVER */ EINVAL,
  79. /* PAL_ERROR_NOTCONNECTION */ ENOTCONN,
  80. /* PAL_ERROR_ZEROSIZE */ 0,
  81. /* PAL_ERROR_CONNFAILED */ ECONNRESET,
  82. /* PAL_ERROR_ADDRNOTEXIST */ EADDRNOTAVAIL,
  83. };
  84. long convert_pal_errno (long err)
  85. {
  86. return (err >= 0 && err <= PAL_ERROR_BOUND) ?
  87. pal_errno_to_unix_errno[err] : 0;
  88. }
  89. void * migrated_memory_start;
  90. void * migrated_memory_end;
  91. void * migrated_shim_addr;
  92. void * initial_stack;
  93. const char ** initial_envp __attribute_migratable;
  94. const char ** library_paths;
  95. LOCKTYPE __master_lock;
  96. bool lock_enabled;
  97. bool in_gdb;
  98. void init_tcb (shim_tcb_t * tcb)
  99. {
  100. tcb->canary = SHIM_TLS_CANARY;
  101. tcb->self = tcb;
  102. }
  103. void copy_tcb (shim_tcb_t * new_tcb, const shim_tcb_t * old_tcb)
  104. {
  105. memset(new_tcb, 0, sizeof(shim_tcb_t));
  106. new_tcb->canary = SHIM_TLS_CANARY;
  107. new_tcb->self = new_tcb;
  108. new_tcb->tp = old_tcb->tp;
  109. memcpy(&new_tcb->context, &old_tcb->context, sizeof(struct shim_context));
  110. new_tcb->tid = old_tcb->tid;
  111. new_tcb->debug_buf = old_tcb->debug_buf;
  112. }
  113. /* This function is used to allocate tls before interpreter start running */
  114. void allocate_tls (void * tcb_location, bool user, struct shim_thread * thread)
  115. {
  116. __libc_tcb_t * tcb = tcb_location;
  117. assert(tcb);
  118. tcb->tcb = tcb;
  119. init_tcb(&tcb->shim_tcb);
  120. if (thread) {
  121. thread->tcb = tcb;
  122. thread->user_tcb = user;
  123. tcb->shim_tcb.tp = thread;
  124. tcb->shim_tcb.tid = thread->tid;
  125. } else {
  126. tcb->shim_tcb.tp = NULL;
  127. tcb->shim_tcb.tid = 0;
  128. }
  129. DkSegmentRegister(PAL_SEGMENT_FS, tcb);
  130. assert(SHIM_TLS_CHECK_CANARY());
  131. }
  132. void populate_tls (void * tcb_location, bool user)
  133. {
  134. __libc_tcb_t * tcb = (__libc_tcb_t *) tcb_location;
  135. assert(tcb);
  136. tcb->tcb = tcb;
  137. copy_tcb(&tcb->shim_tcb, SHIM_GET_TLS());
  138. struct shim_thread * thread = (struct shim_thread *) tcb->shim_tcb.tp;
  139. if (thread) {
  140. thread->tcb = tcb;
  141. thread->user_tcb = user;
  142. }
  143. DkSegmentRegister(PAL_SEGMENT_FS, tcb);
  144. assert(SHIM_TLS_CHECK_CANARY());
  145. }
  146. DEFINE_PROFILE_OCCURENCE(alloc_stack, memory);
  147. DEFINE_PROFILE_OCCURENCE(alloc_stack_count, memory);
  148. #define STACK_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS)
  149. void * allocate_stack (size_t size, size_t protect_size, bool user)
  150. {
  151. size = ALIGN_UP(size);
  152. protect_size = ALIGN_UP(protect_size);
  153. /* preserve a non-readable, non-writeable page below the user
  154. stack to stop user program to clobber other vmas */
  155. void * stack = user ?
  156. get_unmapped_vma(size + protect_size, STACK_FLAGS) :
  157. NULL;
  158. if (user)
  159. stack = (void *) DkVirtualMemoryAlloc(stack, size + protect_size,
  160. 0, PAL_PROT_READ|PAL_PROT_WRITE);
  161. else
  162. stack = system_malloc(size + protect_size);
  163. if (!stack)
  164. return NULL;
  165. ADD_PROFILE_OCCURENCE(alloc_stack, size + protect_size);
  166. INC_PROFILE_OCCURENCE(alloc_stack_count);
  167. if (protect_size &&
  168. !DkVirtualMemoryProtect(stack, protect_size, PAL_PROT_NONE))
  169. return NULL;
  170. stack += protect_size;
  171. if (user) {
  172. if (bkeep_mmap(stack, size, PROT_READ|PROT_WRITE,
  173. STACK_FLAGS, NULL, 0, "stack") < 0)
  174. return NULL;
  175. if (protect_size &&
  176. bkeep_mmap(stack - protect_size, protect_size, 0,
  177. STACK_FLAGS, NULL, 0, NULL) < 0)
  178. return NULL;
  179. }
  180. debug("allocated stack at %p (size = %d)\n", stack, size);
  181. return stack;
  182. }
  183. int populate_user_stack (void * stack, size_t stack_size,
  184. int nauxv, elf_auxv_t ** auxpp,
  185. const char *** argvp, const char *** envpp)
  186. {
  187. const char ** argv = *argvp, ** envp = *envpp;
  188. const char ** new_argv = NULL, ** new_envp = NULL;
  189. void * stack_bottom = stack;
  190. void * stack_top = stack + stack_size;
  191. #define ALLOCATE_TOP(size) \
  192. ({ if ((stack_top -= (size)) < stack_bottom) return -ENOMEM; \
  193. stack_top; })
  194. #define ALLOCATE_BOTTOM(size) \
  195. ({ if ((stack_bottom += (size)) > stack_top) return -ENOMEM; \
  196. stack_bottom - (size); })
  197. if (!argv) {
  198. *(const char **) ALLOCATE_BOTTOM(sizeof(const char *)) = NULL;
  199. goto copy_envp;
  200. }
  201. new_argv = stack_bottom;
  202. while (argv) {
  203. for (const char ** a = argv ; *a ; a++) {
  204. const char ** t = ALLOCATE_BOTTOM(sizeof(const char *));
  205. int len = strlen(*a) + 1;
  206. char * abuf = ALLOCATE_TOP(len);
  207. memcpy(abuf, *a, len);
  208. *t = abuf;
  209. }
  210. *((const char **) ALLOCATE_BOTTOM(sizeof(const char *))) = NULL;
  211. copy_envp:
  212. if (!envp)
  213. break;
  214. new_envp = stack_bottom;
  215. argv = envp;
  216. envp = NULL;
  217. }
  218. if (!new_envp)
  219. *(const char **) ALLOCATE_BOTTOM(sizeof(const char *)) = NULL;
  220. stack_bottom = (void *) ((unsigned long) stack_bottom & ~7UL);
  221. *((unsigned long *) ALLOCATE_TOP(sizeof(unsigned long))) = 0;
  222. if (nauxv) {
  223. elf_auxv_t * old_auxp = *auxpp;
  224. *auxpp = ALLOCATE_TOP(sizeof(elf_auxv_t) * nauxv);
  225. if (old_auxp)
  226. memcpy(*auxpp, old_auxp, nauxv * sizeof(elf_auxv_t));
  227. }
  228. memmove(stack_top - (stack_bottom - stack), stack, stack_bottom - stack);
  229. if (new_argv)
  230. *argvp = (void *) new_argv + (stack_top - stack_bottom);
  231. if (new_envp)
  232. *envpp = (void *) new_envp + (stack_top - stack_bottom);
  233. return 0;
  234. }
  235. unsigned long sys_stack_size = 0;
  236. int init_stack (const char ** argv, const char ** envp, const char *** argpp,
  237. int nauxv, elf_auxv_t ** auxpp)
  238. {
  239. if (!sys_stack_size) {
  240. sys_stack_size = DEFAULT_SYS_STACK_SIZE;
  241. if (root_config) {
  242. char stack_cfg[CONFIG_MAX];
  243. if (get_config(root_config, "sys.stack.size", stack_cfg,
  244. CONFIG_MAX) > 0)
  245. sys_stack_size = ALIGN_UP(atoi(stack_cfg));
  246. }
  247. }
  248. struct shim_thread * cur_thread = get_cur_thread();
  249. if (!cur_thread || cur_thread->stack)
  250. return 0;
  251. void * stack = allocate_stack(sys_stack_size, allocsize, true);
  252. if (!stack)
  253. return -ENOMEM;
  254. if (initial_envp)
  255. envp = initial_envp;
  256. int ret = populate_user_stack(stack, sys_stack_size,
  257. nauxv, auxpp, &argv, &envp);
  258. if (ret < 0)
  259. return ret;
  260. *argpp = argv;
  261. initial_envp = envp;
  262. cur_thread->stack_top = stack + sys_stack_size;
  263. cur_thread->stack = stack;
  264. cur_thread->stack_red = stack - allocsize;
  265. return 0;
  266. }
  267. int read_environs (const char ** envp)
  268. {
  269. for (const char ** e = envp ; *e ; e++) {
  270. switch ((*e)[0]) {
  271. case 'L': {
  272. if (!memcmp(*e, "LD_LIBRARY_PATH=", 16)) {
  273. int npaths = 0;
  274. for (const char * s = (*e) + 16 ; *s ; s++)
  275. if (*s == ':')
  276. npaths++;
  277. const char ** paths = malloc(sizeof(const char *) *
  278. (npaths + 1));
  279. if (!paths)
  280. return -ENOMEM;
  281. const char * s = (*e) + 16, * next;
  282. int cnt = 0;
  283. while (*s) {
  284. for (next = s ; *next && *next != ':' ; next++);
  285. int len = next - s;
  286. char * str = malloc(len + 1);
  287. if (!str)
  288. return -ENOMEM;
  289. memcpy(str, s, len);
  290. str[len] = 0;
  291. paths[cnt++] = str;
  292. s = *next ? next + 1 : next;
  293. }
  294. paths[cnt] = NULL;
  295. library_paths = paths;
  296. break;
  297. }
  298. break;
  299. }
  300. case 'I': {
  301. if (!memcmp(*e, "IN_GDB=1", 8)) {
  302. in_gdb = true;
  303. break;
  304. }
  305. break;
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. struct config_store * root_config = NULL;
  312. static void * __malloc (int size)
  313. {
  314. return malloc(size);
  315. }
  316. static void __free (void * mem)
  317. {
  318. free(mem);
  319. }
  320. int init_manifest (PAL_HANDLE manifest_handle)
  321. {
  322. PAL_STREAM_ATTR attr;
  323. if (!DkStreamAttributesQuerybyHandle(manifest_handle, &attr))
  324. return -PAL_ERRNO;
  325. size_t cfg_size = attr.pending_size;
  326. void * cfg_addr = (void *) DkStreamMap(manifest_handle, NULL,
  327. PAL_PROT_READ|PAL_PROT_WRITECOPY, 0,
  328. ALIGN_UP(cfg_size));
  329. if (!cfg_addr)
  330. return -PAL_ERRNO;
  331. root_config = malloc(sizeof(struct config_store));
  332. root_config->raw_data = cfg_addr;
  333. root_config->raw_size = cfg_size;
  334. root_config->malloc = __malloc;
  335. root_config->free = __free;
  336. const char * errstring = "Unexpected error";
  337. int ret = 0;
  338. if ((ret = read_config(root_config, NULL, &errstring)) < 0) {
  339. root_config = NULL;
  340. sys_printf("Unable to read manifest file: %s\n", errstring);
  341. return ret;
  342. }
  343. return 0;
  344. }
  345. #ifdef PROFILE
  346. struct shim_profile profile_root;
  347. #endif
  348. # define FIND_ARG_COMPONENTS(cookie, argc, argv, envp, auxp) \
  349. do { \
  350. void *_tmp = (cookie); \
  351. (argv) = _tmp; \
  352. _tmp += sizeof(char *) * ((argc) + 1); \
  353. (envp) = _tmp; \
  354. for ( ; *(char **) _tmp; _tmp += sizeof(char *)); \
  355. (auxp) = _tmp + sizeof(char *); \
  356. } while (0)
  357. static void * __process_auxv (elf_auxv_t * auxp)
  358. {
  359. elf_auxv_t * av;
  360. for (av = auxp; av->a_type != AT_NULL; av++)
  361. switch (av->a_type) {
  362. default: break;
  363. }
  364. return av + 1;
  365. }
  366. #define FIND_LAST_STACK(stack) \
  367. do { \
  368. /* check if exist a NULL end */ \
  369. assert(*(uint64_t *) stack == 0); \
  370. stack += sizeof(uint64_t); \
  371. } while (0)
  372. #ifdef PROFILE
  373. static void set_profile_enabled (const char ** envp)
  374. {
  375. const char ** p;
  376. for (p = envp ; (*p) ; p++)
  377. if (!memcmp(*p, "PROFILE_ENABLED=", 16))
  378. break;
  379. if (!(*p))
  380. return;
  381. for (int i = 0 ; i < N_PROFILE ; i++)
  382. PROFILES[i].disabled = true;
  383. const char * str = (*p) + 16;
  384. bool enabled = false;
  385. while (*str) {
  386. const char * next = str;
  387. for ( ; (*next) && (*next) != ',' ; next++);
  388. if (next > str) {
  389. int len = next - str;
  390. for (int i = 0 ; i < N_PROFILE ; i++) {
  391. struct shim_profile * profile = &PROFILES[i];
  392. if (!memcmp(profile->name, str, len) && !profile->name[len]) {
  393. profile->disabled = false;
  394. if (profile->type == CATAGORY)
  395. enabled = true;
  396. }
  397. }
  398. }
  399. str = (*next) ? next + 1 : next;
  400. }
  401. while (enabled) {
  402. enabled = false;
  403. for (int i = 0 ; i < N_PROFILE ; i++) {
  404. struct shim_profile * profile = &PROFILES[i];
  405. if (!profile->disabled || profile->root == &profile_)
  406. continue;
  407. if (!profile->root->disabled) {
  408. profile->disabled = false;
  409. if (profile->type == CATAGORY)
  410. enabled = true;
  411. }
  412. }
  413. }
  414. for (int i = 0 ; i < N_PROFILE ; i++) {
  415. struct shim_profile * profile = &PROFILES[i];
  416. if (profile->type == CATAGORY || profile->disabled)
  417. continue;
  418. for (profile = profile->root ;
  419. profile != &profile_ && profile->disabled ;
  420. profile = profile->root)
  421. profile->disabled = false;
  422. }
  423. }
  424. #endif
  425. static int init_newproc (struct newproc_header * hdr)
  426. {
  427. BEGIN_PROFILE_INTERVAL();
  428. int bytes = DkStreamRead(PAL_CB(parent_process), 0,
  429. sizeof(struct newproc_header), hdr,
  430. NULL, 0);
  431. if (!bytes)
  432. return -PAL_ERRNO;
  433. SAVE_PROFILE_INTERVAL(child_wait_header);
  434. SAVE_PROFILE_INTERVAL_SINCE(child_receive_header, hdr->write_proc_time);
  435. return hdr->failure;
  436. }
  437. DEFINE_PROFILE_CATAGORY(pal, );
  438. DEFINE_PROFILE_INTERVAL(pal_startup_time, pal);
  439. DEFINE_PROFILE_INTERVAL(pal_host_specific_startup_time, pal);
  440. DEFINE_PROFILE_INTERVAL(pal_relocation_time, pal);
  441. DEFINE_PROFILE_INTERVAL(pal_linking_time, pal);
  442. DEFINE_PROFILE_INTERVAL(pal_manifest_loading_time, pal);
  443. DEFINE_PROFILE_INTERVAL(pal_allocation_time, pal);
  444. DEFINE_PROFILE_INTERVAL(pal_tail_startup_time, pal);
  445. DEFINE_PROFILE_INTERVAL(pal_child_creation_time, pal);
  446. DEFINE_PROFILE_CATAGORY(init, );
  447. DEFINE_PROFILE_INTERVAL(init_randgen, init);
  448. DEFINE_PROFILE_INTERVAL(init_heap, init);
  449. DEFINE_PROFILE_INTERVAL(init_slab, init);
  450. DEFINE_PROFILE_INTERVAL(init_str_mgr, init);
  451. DEFINE_PROFILE_INTERVAL(init_internal_map, init);
  452. DEFINE_PROFILE_INTERVAL(init_vma, init);
  453. DEFINE_PROFILE_INTERVAL(init_fs, init);
  454. DEFINE_PROFILE_INTERVAL(init_dcache, init);
  455. DEFINE_PROFILE_INTERVAL(init_handle, init);
  456. DEFINE_PROFILE_INTERVAL(read_from_checkpoint, init);
  457. DEFINE_PROFILE_INTERVAL(read_from_file, init);
  458. DEFINE_PROFILE_INTERVAL(init_newproc, init);
  459. DEFINE_PROFILE_INTERVAL(init_mount_root, init);
  460. DEFINE_PROFILE_INTERVAL(init_from_checkpoint_file, init);
  461. DEFINE_PROFILE_INTERVAL(restore_from_file, init);
  462. DEFINE_PROFILE_INTERVAL(init_manifest, init);
  463. DEFINE_PROFILE_INTERVAL(init_ipc, init);
  464. DEFINE_PROFILE_INTERVAL(init_thread, init);
  465. DEFINE_PROFILE_INTERVAL(init_important_handles, init);
  466. DEFINE_PROFILE_INTERVAL(init_mount, init);
  467. DEFINE_PROFILE_INTERVAL(init_async, init);
  468. DEFINE_PROFILE_INTERVAL(init_stack, init);
  469. DEFINE_PROFILE_INTERVAL(read_environs, init);
  470. DEFINE_PROFILE_INTERVAL(init_loader, init);
  471. DEFINE_PROFILE_INTERVAL(init_ipc_helper, init);
  472. DEFINE_PROFILE_INTERVAL(init_signal, init);
  473. #define CALL_INIT(func, args ...) func(args)
  474. #define RUN_INIT(func, ...) \
  475. do { \
  476. int _err = CALL_INIT(func, ##__VA_ARGS__); \
  477. if (_err < 0) { \
  478. sys_printf("shim initialization failed in " #func " (%d)", \
  479. _err); \
  480. shim_terminate(); \
  481. } \
  482. SAVE_PROFILE_INTERVAL(func); \
  483. } while (0)
  484. extern PAL_HANDLE thread_start_event;
  485. int shim_init (int argc, void * args, void ** return_stack)
  486. {
  487. debug_handle = PAL_CB(debug_stream);
  488. cur_process.vmid = (IDTYPE) PAL_CB(process_id);
  489. /* create the initial TCB, shim can not be run without a tcb */
  490. __libc_tcb_t tcb;
  491. memset(&tcb, 0, sizeof(__libc_tcb_t));
  492. allocate_tls(&tcb, false, NULL);
  493. debug_setbuf(&tcb.shim_tcb, true);
  494. debug("set tcb to %p\n", &tcb);
  495. #ifdef PROFILE
  496. unsigned long begin_time = GET_PROFILE_INTERVAL();
  497. #endif
  498. DkSetExceptionHandler(&handle_failure, PAL_EVENT_FAILURE, 0);
  499. allocsize = PAL_CB(alloc_align);
  500. allocshift = allocsize - 1;
  501. allocmask = ~allocshift;
  502. create_lock(__master_lock);
  503. const char ** argv, ** envp, ** argp = NULL;
  504. elf_auxv_t * auxp;
  505. /* call to figure out where the arguments are */
  506. FIND_ARG_COMPONENTS(args, argc, argv, envp, auxp);
  507. initial_stack = __process_auxv(auxp);
  508. int nauxv = (elf_auxv_t *) initial_stack - auxp;
  509. FIND_LAST_STACK(initial_stack);
  510. #ifdef PROFILE
  511. set_profile_enabled(envp);
  512. #endif
  513. struct newproc_header hdr;
  514. void * cpaddr = NULL;
  515. #ifdef PROFILE
  516. unsigned long begin_create_time = 0;
  517. #endif
  518. BEGIN_PROFILE_INTERVAL();
  519. RUN_INIT(init_randgen);
  520. RUN_INIT(init_heap);
  521. RUN_INIT(init_slab);
  522. RUN_INIT(init_str_mgr);
  523. RUN_INIT(init_internal_map);
  524. RUN_INIT(init_vma);
  525. RUN_INIT(init_fs);
  526. RUN_INIT(init_dcache);
  527. RUN_INIT(init_handle);
  528. debug("shim loaded at %p, ready to initialize\n", &__load_address);
  529. if (argc && argv[0][0] == '-') {
  530. if (!memcmp(argv[0], "-resume", 8) && argc >= 2) {
  531. const char * filename = *(argv + 1);
  532. argc -= 2;
  533. argv += 2;
  534. RUN_INIT(init_mount_root);
  535. RUN_INIT(init_from_checkpoint_file, filename, &hdr.checkpoint,
  536. &cpaddr);
  537. goto restore;
  538. }
  539. }
  540. if (PAL_CB(parent_process)) {
  541. RUN_INIT(init_newproc, &hdr);
  542. SAVE_PROFILE_INTERVAL_SET(child_created_in_new_process,
  543. hdr.create_time, begin_time);
  544. #ifdef PROFILE
  545. begin_create_time = hdr.begin_create_time;
  546. #endif
  547. if (hdr.checkpoint.hdr.size)
  548. RUN_INIT(do_migration, &hdr.checkpoint, &cpaddr);
  549. }
  550. if (cpaddr) {
  551. restore:
  552. thread_start_event = DkNotificationEventCreate(0);
  553. RUN_INIT(restore_checkpoint,
  554. &hdr.checkpoint.hdr, &hdr.checkpoint.mem,
  555. (ptr_t) cpaddr, 0);
  556. }
  557. if (PAL_CB(manifest_handle))
  558. RUN_INIT(init_manifest, PAL_CB(manifest_handle));
  559. RUN_INIT(init_mount_root);
  560. RUN_INIT(init_ipc);
  561. RUN_INIT(init_thread);
  562. RUN_INIT(init_important_handles);
  563. RUN_INIT(init_mount);
  564. RUN_INIT(init_async);
  565. RUN_INIT(init_stack, argv, envp, &argp, nauxv, &auxp);
  566. RUN_INIT(read_environs, envp);
  567. RUN_INIT(init_loader);
  568. RUN_INIT(init_ipc_helper);
  569. RUN_INIT(init_signal);
  570. debug("shim process initialized\n");
  571. #ifdef PROFILE
  572. if (begin_create_time)
  573. SAVE_PROFILE_INTERVAL_SINCE(child_total_migration_time,
  574. begin_create_time);
  575. #endif
  576. SAVE_PROFILE_INTERVAL_SET(pal_startup_time, 0, pal_control.startup_time);
  577. SAVE_PROFILE_INTERVAL_SET(pal_host_specific_startup_time, 0,
  578. pal_control.host_specific_startup_time);
  579. SAVE_PROFILE_INTERVAL_SET(pal_relocation_time, 0,
  580. pal_control.relocation_time);
  581. SAVE_PROFILE_INTERVAL_SET(pal_linking_time, 0, pal_control.linking_time);
  582. SAVE_PROFILE_INTERVAL_SET(pal_manifest_loading_time, 0,
  583. pal_control.manifest_loading_time);
  584. SAVE_PROFILE_INTERVAL_SET(pal_allocation_time, 0,
  585. pal_control.allocation_time);
  586. SAVE_PROFILE_INTERVAL_SET(pal_tail_startup_time, 0,
  587. pal_control.tail_startup_time);
  588. SAVE_PROFILE_INTERVAL_SET(pal_child_creation_time, 0,
  589. pal_control.child_creation_time);
  590. if (thread_start_event)
  591. DkEventSet(thread_start_event);
  592. shim_tcb_t * cur_tcb = SHIM_GET_TLS();
  593. struct shim_thread * cur_thread = (struct shim_thread *) cur_tcb->tp;
  594. if (cur_tcb->context.sp)
  595. restore_context(&cur_tcb->context);
  596. if (cur_thread->exec)
  597. execute_elf_object(cur_thread->exec,
  598. argc, argp, nauxv, auxp);
  599. *return_stack = initial_stack;
  600. return 0;
  601. }
  602. static int create_unique (int (*mkname) (char *, size_t, void *),
  603. int (*create) (const char *, void *),
  604. int (*output) (char *, size_t, const void *,
  605. struct shim_qstr *),
  606. char * name, size_t size, void * id, void * obj,
  607. struct shim_qstr * qstr)
  608. {
  609. int ret, len;
  610. while (1) {
  611. len = mkname(name, size, id);
  612. if (len < 0)
  613. return len;
  614. if ((ret = create(name, obj)) < 0)
  615. return ret;
  616. if (ret)
  617. continue;
  618. if (output)
  619. return output(name, size, id, qstr);
  620. if (qstr)
  621. qstrsetstr(qstr, name, len);
  622. return len;
  623. }
  624. }
  625. static int name_pipe (char * uri, size_t size, void * id)
  626. {
  627. IDTYPE pipeid;
  628. int len;
  629. if (getrand(&pipeid, sizeof(IDTYPE)) < sizeof(IDTYPE))
  630. return -EACCES;
  631. if ((len = snprintf(uri, size, "pipe.srv:%u", pipeid)) == size)
  632. return -ERANGE;
  633. *((IDTYPE *) id) = pipeid;
  634. return len;
  635. }
  636. static int open_pipe (const char * uri, void * obj)
  637. {
  638. PAL_HANDLE pipe = DkStreamOpen(uri, 0, 0, 0, 0);
  639. if (!pipe)
  640. return PAL_NATIVE_ERRNO == PAL_ERROR_STREAMEXIST ? 1 :
  641. -PAL_ERRNO;
  642. if (obj)
  643. *((PAL_HANDLE *) obj) = pipe;
  644. else
  645. DkObjectClose(pipe);
  646. return 0;
  647. }
  648. static int pipe_addr (char * uri, size_t size, const void * id,
  649. struct shim_qstr * qstr)
  650. {
  651. IDTYPE pipeid = *((IDTYPE *) id);
  652. int len;
  653. if ((len = snprintf(uri, size, "pipe:%u", pipeid)) == size)
  654. return -ERANGE;
  655. if (qstr)
  656. qstrsetstr(qstr, uri, len);
  657. return len;
  658. }
  659. int create_pipe (IDTYPE * id, char * uri, size_t size, PAL_HANDLE * hdl,
  660. struct shim_qstr * qstr)
  661. {
  662. IDTYPE pipeid;
  663. int ret = create_unique(&name_pipe, &open_pipe, &pipe_addr,
  664. uri, size, &pipeid, hdl, qstr);
  665. if (ret > 0 && id)
  666. *id = pipeid;
  667. return ret;
  668. }
  669. static int name_path (char * path, size_t size, void * id)
  670. {
  671. unsigned int suffix;
  672. int prefix_len = strlen(path);
  673. int len;
  674. if (getrand(&suffix, sizeof(unsigned int)) < sizeof(unsigned int))
  675. return -EACCES;
  676. len = snprintf(path + prefix_len, size - prefix_len, "%08x", suffix);
  677. if (len == size)
  678. return -ERANGE;
  679. *((unsigned int *) id) = suffix;
  680. return prefix_len + len;
  681. }
  682. static int open_dir (const char * path, void * obj)
  683. {
  684. struct shim_handle * dir = NULL;
  685. if (obj) {
  686. dir = get_new_handle();
  687. if (!dir)
  688. return -ENOMEM;
  689. }
  690. int ret = open_namei(dir, NULL, path, O_CREAT|O_EXCL|O_DIRECTORY, 0700,
  691. NULL);
  692. if (ret < 0)
  693. return ret = -EEXIST ? 1 : ret;
  694. if (obj)
  695. *((struct shim_handle **) obj) = dir;
  696. return 0;
  697. }
  698. static int open_file (const char * path, void * obj)
  699. {
  700. struct shim_handle * file = NULL;
  701. if (obj) {
  702. file = get_new_handle();
  703. if (!file)
  704. return -ENOMEM;
  705. }
  706. int ret = open_namei(file, NULL, path, O_CREAT|O_EXCL|O_RDWR, 0600,
  707. NULL);
  708. if (ret < 0)
  709. return ret = -EEXIST ? 1 : ret;
  710. if (obj)
  711. *((struct shim_handle **) obj) = file;
  712. return 0;
  713. }
  714. static int open_pal_handle (const char * uri, void * obj)
  715. {
  716. PAL_HANDLE hdl;
  717. if (!memcmp(uri, "dir:", 4))
  718. hdl = DkStreamOpen(uri, 0,
  719. PAL_SHARE_OWNER_X|PAL_SHARE_OWNER_W|
  720. PAL_SHARE_OWNER_R,
  721. PAL_CREAT_TRY|PAL_CREAT_ALWAYS,
  722. 0);
  723. else
  724. hdl = DkStreamOpen(uri, PAL_ACCESS_RDWR,
  725. PAL_SHARE_OWNER_W|PAL_SHARE_OWNER_R,
  726. PAL_CREAT_TRY|PAL_CREAT_ALWAYS,
  727. 0);
  728. if (!hdl) {
  729. if (PAL_NATIVE_ERRNO == PAL_ERROR_STREAMEXIST)
  730. return 0;
  731. else
  732. return -PAL_ERRNO;
  733. }
  734. if (obj)
  735. *((PAL_HANDLE *) obj) = hdl;
  736. return 0;
  737. }
  738. static int output_path (char * path, size_t size, const void * id,
  739. struct shim_qstr * qstr)
  740. {
  741. int len = strlen(path);
  742. if (qstr)
  743. qstrsetstr(qstr, path, len);
  744. return len;
  745. }
  746. int create_dir (const char * prefix, char * path, size_t size,
  747. struct shim_handle ** hdl)
  748. {
  749. unsigned int suffix;
  750. if (prefix) {
  751. int len = strlen(prefix);
  752. if (len >= size)
  753. return -ERANGE;
  754. memcpy(path, prefix, len + 1);
  755. }
  756. return create_unique(&name_path, &open_dir, &output_path, path, size,
  757. &suffix, hdl, NULL);
  758. }
  759. int create_file (const char * prefix, char * path, size_t size,
  760. struct shim_handle ** hdl)
  761. {
  762. unsigned int suffix;
  763. if (prefix) {
  764. int len = strlen(prefix);
  765. if (len >= size)
  766. return -ERANGE;
  767. memcpy(path, prefix, len + 1);
  768. }
  769. return create_unique(&name_path, &open_file, &output_path, path, size,
  770. &suffix, hdl, NULL);
  771. }
  772. int create_handle (const char * prefix, char * uri, size_t size,
  773. PAL_HANDLE * hdl, unsigned int * id)
  774. {
  775. unsigned int suffix;
  776. if (prefix) {
  777. int len = strlen(prefix);
  778. if (len >= size)
  779. return -ERANGE;
  780. memcpy(uri, prefix, len + 1);
  781. }
  782. return create_unique(&name_path, &open_pal_handle, &output_path, uri, size,
  783. id ? : &suffix, hdl, NULL);
  784. }
  785. void check_stack_hook (void)
  786. {
  787. struct shim_thread * cur_thread = get_cur_thread();
  788. void * rsp;
  789. asm volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  790. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack) {
  791. if (rsp - cur_thread->stack < PAL_CB(pagesize))
  792. sys_printf("*** stack is almost drained (RSP = %p, stack = %p-%p) ***\n",
  793. rsp, cur_thread->stack, cur_thread->stack_top);
  794. } else {
  795. sys_printf("*** context dismateched with thread stack (RSP = %p, stack = %p-%p) ***\n",
  796. rsp, cur_thread->stack, cur_thread->stack_top);
  797. }
  798. }
  799. #ifdef PROFILE
  800. static void print_profile_result (PAL_HANDLE hdl, struct shim_profile * root,
  801. int level)
  802. {
  803. unsigned long total_interval_time = 0;
  804. unsigned long total_interval_count = 0;
  805. for (int i = 0 ; i < N_PROFILE ; i++) {
  806. struct shim_profile * profile = &PROFILES[i];
  807. if (profile->root != root || profile->disabled)
  808. continue;
  809. switch (profile->type) {
  810. case OCCURENCE: {
  811. unsigned int count =
  812. atomic_read(&profile->val.occurence.count);
  813. if (count) {
  814. for (int j = 0 ; j < level ; j++)
  815. __sys_fprintf(hdl, " ");
  816. __sys_fprintf(hdl, "- %s: %u times\n", profile->name, count);
  817. }
  818. break;
  819. }
  820. case INTERVAL: {
  821. unsigned int count =
  822. atomic_read(&profile->val.interval.count);
  823. if (count) {
  824. unsigned long time =
  825. atomic_read(&profile->val.interval.time);
  826. unsigned long ind_time = time / count;
  827. total_interval_time += time;
  828. total_interval_count += count;
  829. for (int j = 0 ; j < level ; j++)
  830. __sys_fprintf(hdl, " ");
  831. __sys_fprintf(hdl, "- (%11.11lu) %s: %u times, %lu msec\n",
  832. time, profile->name, count, ind_time);
  833. }
  834. break;
  835. }
  836. case CATAGORY:
  837. for (int j = 0 ; j < level ; j++)
  838. __sys_fprintf(hdl, " ");
  839. __sys_fprintf(hdl, "- %s:\n", profile->name);
  840. print_profile_result(hdl, profile, level + 1);
  841. break;
  842. }
  843. }
  844. if (total_interval_count) {
  845. __sys_fprintf(hdl, " - (%11.11u) total: %u times, %lu msec\n",
  846. total_interval_time, total_interval_count,
  847. total_interval_time / total_interval_count);
  848. }
  849. }
  850. #endif /* PROFILE */
  851. static struct shim_atomic in_terminate = { .counter = 0, };
  852. int shim_terminate (void)
  853. {
  854. debug("teminating the whole process\n");
  855. /* do last clean-up of the process */
  856. shim_clean();
  857. DkProcessExit(0);
  858. return 0;
  859. }
  860. int shim_clean (void)
  861. {
  862. /* preventing multiple cleanup, this is mostly caused by
  863. assertion in shim_clean */
  864. atomic_inc(&in_terminate);
  865. if (atomic_read(&in_terminate) > 1)
  866. return 0;
  867. store_all_msg_persist();
  868. #ifdef PROFILE
  869. if (ENTER_TIME) {
  870. switch (SHIM_GET_TLS()->context.syscall_nr) {
  871. case __NR_exit_group:
  872. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit_group, ENTER_TIME);
  873. break;
  874. case __NR_exit:
  875. SAVE_PROFILE_INTERVAL_SINCE(syscall_exit, ENTER_TIME);
  876. break;
  877. }
  878. }
  879. if (ipc_cld_profile_send()) {
  880. master_lock();
  881. PAL_HANDLE hdl = __open_shim_stdio();
  882. if (hdl) {
  883. __sys_fprintf(hdl, "******************************\n");
  884. __sys_fprintf(hdl, "profiling:\n");
  885. print_profile_result(hdl, &profile_root, 0);
  886. __sys_fprintf(hdl, "******************************\n");
  887. }
  888. master_unlock();
  889. }
  890. #endif
  891. del_all_ipc_ports(0);
  892. if (shim_stdio && shim_stdio != (PAL_HANDLE) -1)
  893. DkObjectClose(shim_stdio);
  894. shim_stdio = NULL;
  895. debug("process %u successfully terminated\n", cur_process.vmid);
  896. master_lock();
  897. DkProcessExit(cur_process.exit_code);
  898. return 0;
  899. }
  900. int message_confirm (const char * message, const char * options)
  901. {
  902. char answer;
  903. int noptions = strlen(options);
  904. char * option_str = __alloca(noptions * 2 + 3), * str = option_str;
  905. int ret = 0;
  906. *(str++) = ' ';
  907. *(str++) = '[';
  908. for (int i = 0 ; i < noptions ; i++) {
  909. *(str++) = options[i];
  910. *(str++) = '/';
  911. }
  912. str--;
  913. *(str++) = ']';
  914. *(str++) = ' ';
  915. master_lock();
  916. PAL_HANDLE hdl = __open_shim_stdio();
  917. if (!hdl) {
  918. master_unlock();
  919. return -EACCES;
  920. }
  921. #define WRITE(buf, len) \
  922. ({ int _ret = DkStreamWrite(hdl, 0, len, buf, NULL); \
  923. _ret ? : -PAL_ERRNO; })
  924. #define READ(buf, len) \
  925. ({ int _ret = DkStreamRead(hdl, 0, len, buf, NULL, 0); \
  926. _ret ? : -PAL_ERRNO; })
  927. if ((ret = WRITE(message, strlen(message))) < 0)
  928. goto out;
  929. if ((ret = WRITE(option_str, noptions * 2 + 3)) < 0)
  930. goto out;
  931. if ((ret = READ(&answer, 1)) < 0)
  932. goto out;
  933. out:
  934. master_unlock();
  935. return (ret < 0) ? ret : answer;
  936. }