db_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <asm/errno.h>
  33. #include <elf/elf.h>
  34. #include <sysdeps/generic/ldsodefs.h>
  35. /* At the begining of entry point, rsp starts at argc, then argvs,
  36. envps and auxvs. Here we store rsp to rdi, so it will not be
  37. messed up by function calls */
  38. asm (".global pal_start \n"
  39. " .type pal_start,@function \n"
  40. "pal_start: \n"
  41. " movq %rsp, %rdi \n"
  42. " call pal_linux_main \n");
  43. #define RTLD_BOOTSTRAP
  44. /* pal_start is the entry point of libpal.so, which calls pal_main */
  45. #define _ENTRY pal_start
  46. /* use objfile-gdb convention instead of .debug_gdb_scripts */
  47. #ifdef DEBUG
  48. asm (".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\r\n"
  49. ".byte 1\r\n"
  50. ".asciz \"" PAL_FILE("host/Linux/pal-gdb.py") "\"\r\n"
  51. ".popsection\r\n");
  52. #endif
  53. struct pal_linux_state linux_state;
  54. struct pal_sec pal_sec;
  55. static int pagesz = PRESET_PAGESIZE;
  56. static int uid, gid;
  57. #if USE_VDSO_GETTIME == 1
  58. static ElfW(Addr) sysinfo_ehdr;
  59. #endif
  60. static void pal_init_bootstrap (void * args, const char ** pal_name,
  61. int * pargc,
  62. const char *** pargv,
  63. const char *** penvp)
  64. {
  65. /*
  66. * fetch arguments and environment variables, the previous stack
  67. * pointer is in rdi (arg). The stack structure starting at rdi
  68. * will look like:
  69. * auxv[m - 1] = AT_NULL
  70. * ...
  71. * auxv[0]
  72. * envp[n - 1] = NULL
  73. * ...
  74. * envp[0]
  75. * argv[argc] = NULL
  76. * argv[argc - 1]
  77. * ...
  78. * argv[0]
  79. * argc
  80. * ---------------------------------------
  81. * user stack
  82. */
  83. const char ** all_args = (const char **) args;
  84. int argc = (uintptr_t) all_args[0];
  85. const char ** argv = &all_args[1];
  86. const char ** envp = argv + argc + 1;
  87. /* fetch environment information from aux vectors */
  88. const char ** e = envp;
  89. #ifdef DEBUG
  90. for (; *e ; e++)
  91. if ((*e)[0] == 'I' && (*e)[1] == 'N' && (*e)[2] == '_' &&
  92. (*e)[3] == 'G' && (*e)[4] == 'D' && (*e)[5] == 'B' &&
  93. (*e)[6] == '=' && (*e)[7] == '1' && !(*e)[8])
  94. linux_state.in_gdb = true;
  95. #else
  96. for (; *e ; e++);
  97. #endif
  98. ElfW(auxv_t) *av;
  99. for (av = (ElfW(auxv_t) *) (e + 1) ; av->a_type != AT_NULL ; av++)
  100. switch (av->a_type) {
  101. case AT_PAGESZ:
  102. pagesz = av->a_un.a_val;
  103. break;
  104. case AT_UID:
  105. case AT_EUID:
  106. uid ^= av->a_un.a_val;
  107. break;
  108. case AT_GID:
  109. case AT_EGID:
  110. gid ^= av->a_un.a_val;
  111. break;
  112. #if USE_VDSO_GETTIME == 1
  113. case AT_SYSINFO_EHDR:
  114. sysinfo_ehdr = av->a_un.a_val;
  115. break;
  116. #endif
  117. }
  118. *pal_name = argv[0];
  119. argv++;
  120. argc--;
  121. *pargc = argc;
  122. *pargv = argv;
  123. *penvp = envp;
  124. }
  125. unsigned long _DkGetPagesize (void)
  126. {
  127. return pagesz;
  128. }
  129. unsigned long _DkGetAllocationAlignment (void)
  130. {
  131. return pagesz;
  132. }
  133. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end)
  134. {
  135. void * end_addr = (void *) ALLOC_ALIGNDOWN(TEXT_START);
  136. void * start_addr = (void *) USER_ADDRESS_LOWEST;
  137. assert(ALLOC_ALIGNED(start_addr) && ALLOC_ALIGNED(end_addr));
  138. while (1) {
  139. if (start_addr >= end_addr)
  140. init_fail(PAL_ERROR_NOMEM, "no user memory available");
  141. void * mem = (void *) ARCH_MMAP(start_addr,
  142. pal_state.alloc_align,
  143. PROT_NONE,
  144. MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
  145. -1, 0);
  146. if (!IS_ERR_P(mem)) {
  147. INLINE_SYSCALL(munmap, 2, mem, pal_state.alloc_align);
  148. if (mem == start_addr)
  149. break;
  150. }
  151. start_addr = (void *) ((unsigned long) start_addr << 1);
  152. }
  153. *end = (PAL_PTR) end_addr - USER_ADDRESS_RESERVED;
  154. *start = (PAL_PTR) start_addr;
  155. }
  156. PAL_NUM _DkGetProcessId (void)
  157. {
  158. return linux_state.process_id;
  159. }
  160. PAL_NUM _DkGetHostId (void)
  161. {
  162. return 0;
  163. }
  164. #include "dynamic_link.h"
  165. void setup_pal_map (struct link_map * map);
  166. #if USE_VDSO_GETTIME == 1
  167. void setup_vdso_map (ElfW(Addr) addr);
  168. #endif
  169. static struct link_map pal_map;
  170. #ifdef __x86_64__
  171. # include "elf-x86_64.h"
  172. #else
  173. # error "unsupported architecture"
  174. #endif
  175. void pal_linux_main (void * args)
  176. {
  177. const char * pal_name = NULL;
  178. PAL_HANDLE parent = NULL, exec = NULL, manifest = NULL;
  179. const char ** argv, ** envp;
  180. int argc;
  181. PAL_HANDLE first_thread;
  182. unsigned long start_time = _DkSystemTimeQueryEarly();
  183. /* parse argc, argv, envp and auxv */
  184. pal_init_bootstrap(args, &pal_name, &argc, &argv, &envp);
  185. pal_map.l_addr = elf_machine_load_address();
  186. pal_map.l_name = pal_name;
  187. elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
  188. pal_map.l_info, pal_map.l_addr);
  189. ELF_DYNAMIC_RELOCATE(&pal_map);
  190. linux_state.environ = envp;
  191. init_slab_mgr(pagesz);
  192. setup_pal_map(&pal_map);
  193. #if USE_VDSO_GETTIME == 1
  194. if (sysinfo_ehdr)
  195. setup_vdso_map(sysinfo_ehdr);
  196. #endif
  197. pal_state.start_time = start_time;
  198. init_child_process(&parent, &exec, &manifest);
  199. if (!pal_sec.process_id)
  200. pal_sec.process_id = INLINE_SYSCALL(getpid, 0);
  201. linux_state.pid = pal_sec.process_id;
  202. linux_state.uid = uid;
  203. linux_state.gid = gid;
  204. linux_state.process_id = (start_time & (~0xffff)) | linux_state.pid;
  205. if (!linux_state.parent_process_id)
  206. linux_state.parent_process_id = linux_state.process_id;
  207. if (parent)
  208. goto done_init;
  209. int fd = INLINE_SYSCALL(open, 3, argv[0], O_RDONLY|O_CLOEXEC, 0);
  210. if (IS_ERR(fd)) {
  211. // DEP 10/20/16: Don't silently swallow permission errors
  212. // accessing the manifest
  213. if (fd == -13) {
  214. printf("Warning: Attempt to open file %s failed with permission denied\n", argv[0]);
  215. }
  216. goto done_init;
  217. }
  218. int len = strlen(argv[0]);
  219. PAL_HANDLE file = malloc(HANDLE_SIZE(file) + len + 1);
  220. SET_HANDLE_TYPE(file, file);
  221. HANDLE_HDR(file)->flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  222. file->file.fd = fd;
  223. char * path = (void *) file + HANDLE_SIZE(file);
  224. get_norm_path(argv[0], path, 0, len + 1);
  225. file->file.realpath = path;
  226. if (!check_elf_object(file)) {
  227. exec = file;
  228. goto done_init;
  229. }
  230. manifest = file;
  231. done_init:
  232. if (!parent && !exec && !manifest) {
  233. printf("USAGE: %s [executable|manifest] args ...\n", pal_name);
  234. _DkProcessExit(0);
  235. return;
  236. }
  237. first_thread = malloc(HANDLE_SIZE(thread));
  238. SET_HANDLE_TYPE(first_thread, thread);
  239. first_thread->thread.tid = linux_state.pid;
  240. signal_setup();
  241. /* call to main function */
  242. pal_main((PAL_NUM) linux_state.parent_process_id,
  243. manifest, exec, NULL, parent, first_thread, argv, envp);
  244. }
  245. /* the following code is borrowed from CPUID */
  246. #define WORD_EAX 0
  247. #define WORD_EBX 1
  248. #define WORD_ECX 2
  249. #define WORD_EDX 3
  250. #define WORD_NUM 4
  251. static void cpuid (int cpuid_fd, unsigned int reg,
  252. unsigned int words[], unsigned int ecx)
  253. {
  254. asm("cpuid"
  255. : "=a" (words[WORD_EAX]),
  256. "=b" (words[WORD_EBX]),
  257. "=c" (words[WORD_ECX]),
  258. "=d" (words[WORD_EDX])
  259. : "a" (reg),
  260. "c" (ecx));
  261. }
  262. #define FOUR_CHARS_VALUE(s, w) \
  263. (s)[0] = (w) & 0xff; \
  264. (s)[1] = ((w) >> 8) & 0xff; \
  265. (s)[2] = ((w) >> 16) & 0xff; \
  266. (s)[3] = ((w) >> 24) & 0xff;
  267. #define BPI 32
  268. #define POWER2(power) \
  269. (1ULL << (power))
  270. #define RIGHTMASK(width) \
  271. (((unsigned long) (width) >= BPI) ? ~0ULL : POWER2(width)-1ULL)
  272. #define BIT_EXTRACT_LE(value, start, after) \
  273. (((unsigned long) (value) & RIGHTMASK(after)) >> start)
  274. static char * cpu_flags[]
  275. = { "fpu", // "x87 FPU on chip"
  276. "vme", // "virtual-8086 mode enhancement"
  277. "de", // "debugging extensions"
  278. "pse", // "page size extensions"
  279. "tsc", // "time stamp counter"
  280. "msr", // "RDMSR and WRMSR support"
  281. "pae", // "physical address extensions"
  282. "mce", // "machine check exception"
  283. "cx8", // "CMPXCHG8B inst."
  284. "apic", // "APIC on chip"
  285. NULL,
  286. "sep", // "SYSENTER and SYSEXIT"
  287. "mtrr", // "memory type range registers"
  288. "pge", // "PTE global bit"
  289. "mca", // "machine check architecture"
  290. "cmov", // "conditional move/compare instruction"
  291. "pat", // "page attribute table"
  292. "pse36", // "page size extension"
  293. "pn", // "processor serial number"
  294. "clflush", // "CLFLUSH instruction"
  295. NULL,
  296. "dts" // "debug store"
  297. "tm", // "thermal monitor and clock ctrl"
  298. "mmx", // "MMX Technology"
  299. "fxsr", // "FXSAVE/FXRSTOR"
  300. "sse", // "SSE extensions"
  301. "sse2", // "SSE2 extensions"
  302. "ss", // "self snoop"
  303. "ht", // "hyper-threading / multi-core supported"
  304. "tm", // "therm. monitor"
  305. "ia64", // "IA64"
  306. "pbe", // "pending break event"
  307. };
  308. void _DkGetCPUInfo (PAL_CPU_INFO * ci)
  309. {
  310. unsigned int words[WORD_NUM];
  311. char * vendor_id = malloc(12);
  312. cpuid(2, 0, words, 0);
  313. FOUR_CHARS_VALUE(&vendor_id[0], words[WORD_EBX]);
  314. FOUR_CHARS_VALUE(&vendor_id[4], words[WORD_EDX]);
  315. FOUR_CHARS_VALUE(&vendor_id[8], words[WORD_ECX]);
  316. ci->cpu_vendor = vendor_id;
  317. char * brand = malloc(48);
  318. cpuid(-2, 0x80000002, words, 0);
  319. memcpy(&brand[ 0], words, sizeof(unsigned int) * WORD_NUM);
  320. cpuid(-2, 0x80000003, words, 0);
  321. memcpy(&brand[16], words, sizeof(unsigned int) * WORD_NUM);
  322. cpuid(-2, 0x80000004, words, 0);
  323. memcpy(&brand[32], words, sizeof(unsigned int) * WORD_NUM);
  324. ci->cpu_brand = brand;
  325. cpuid(2, 1, words, 0);
  326. ci->cpu_num = BIT_EXTRACT_LE(words[WORD_EBX], 16, 24);
  327. ci->cpu_family = BIT_EXTRACT_LE(words[WORD_EAX], 8, 12);
  328. ci->cpu_model = BIT_EXTRACT_LE(words[WORD_EAX], 4, 8);
  329. ci->cpu_stepping = BIT_EXTRACT_LE(words[WORD_EAX], 0, 4);
  330. int flen = 0, fmax = 80;
  331. char * flags = malloc(fmax);
  332. for (int i = 0 ; i < 32 ; i++) {
  333. if (!cpu_flags[i])
  334. break;
  335. if (BIT_EXTRACT_LE(words[WORD_EDX], i, i + 1)) {
  336. int len = strlen(cpu_flags[i]);
  337. if (flen + len + 1 > fmax) {
  338. char * new_flags = malloc(fmax * 2);
  339. memcpy(new_flags, flags, flen);
  340. free(flags);
  341. fmax *= 2;
  342. flags = new_flags;
  343. }
  344. memcpy(flags + flen, cpu_flags[i], len);
  345. flen += len;
  346. flags[flen++] = ' ';
  347. }
  348. }
  349. flags[flen ? flen - 1 : 0] = 0;
  350. ci->cpu_flags = flags;
  351. }