db_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_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. #ifdef DEBUG
  47. asm (".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\r\n"
  48. ".byte 1\r\n"
  49. ".asciz \"" XSTRINGIFY(GDB_SCRIPT) "\"\r\n"
  50. ".popsection\r\n");
  51. #endif
  52. struct pal_linux_state linux_state;
  53. struct pal_sec pal_sec;
  54. static int pagesz = PRESET_PAGESIZE;
  55. static int uid, gid;
  56. #if USE_VDSO_GETTIME == 1
  57. static ElfW(Addr) sysinfo_ehdr;
  58. #endif
  59. static void pal_init_bootstrap (void * args, const char ** pal_name,
  60. int * pargc,
  61. const char *** pargv,
  62. const char *** penvp)
  63. {
  64. /*
  65. * fetch arguments and environment variables, the previous stack
  66. * pointer is in rdi (arg). The stack structure starting at rdi
  67. * will look like:
  68. * auxv[m - 1] = AT_NULL
  69. * ...
  70. * auxv[0]
  71. * envp[n - 1] = NULL
  72. * ...
  73. * envp[0]
  74. * argv[argc] = NULL
  75. * argv[argc - 1]
  76. * ...
  77. * argv[0]
  78. * argc
  79. * ---------------------------------------
  80. * user stack
  81. */
  82. const char ** all_args = (const char **) args;
  83. int argc = (uintptr_t) all_args[0];
  84. const char ** argv = &all_args[1];
  85. const char ** envp = argv + argc + 1;
  86. /* fetch environment information from aux vectors */
  87. const char ** e = envp;
  88. #ifdef DEBUG
  89. for (; *e ; e++)
  90. if ((*e)[0] == 'I' && (*e)[1] == 'N' && (*e)[2] == '_' &&
  91. (*e)[3] == 'G' && (*e)[4] == 'D' && (*e)[5] == 'B' &&
  92. (*e)[6] == '=' && (*e)[7] == '1' && !(*e)[8])
  93. linux_state.in_gdb = true;
  94. #else
  95. for (; *e ; e++);
  96. #endif
  97. ElfW(auxv_t) *av;
  98. for (av = (ElfW(auxv_t) *) (e + 1) ; av->a_type != AT_NULL ; av++)
  99. switch (av->a_type) {
  100. case AT_PAGESZ:
  101. pagesz = av->a_un.a_val;
  102. break;
  103. case AT_UID:
  104. case AT_EUID:
  105. uid ^= av->a_un.a_val;
  106. break;
  107. case AT_GID:
  108. case AT_EGID:
  109. gid ^= av->a_un.a_val;
  110. break;
  111. #if USE_VDSO_GETTIME == 1
  112. case AT_SYSINFO_EHDR:
  113. sysinfo_ehdr = av->a_un.a_val;
  114. break;
  115. #endif
  116. }
  117. *pal_name = argv[0];
  118. argv++;
  119. argc--;
  120. *pargc = argc;
  121. *pargv = argv;
  122. *penvp = envp;
  123. }
  124. unsigned long _DkGetPagesize (void)
  125. {
  126. return pagesz;
  127. }
  128. unsigned long _DkGetAllocationAlignment (void)
  129. {
  130. return pagesz;
  131. }
  132. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end)
  133. {
  134. void * end_addr = (void *) ALLOC_ALIGNDOWN(TEXT_START);
  135. void * start_addr = pal_sec.user_addr_base ? :
  136. (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. #endif
  173. void pal_linux_main (void * args)
  174. {
  175. const char * pal_name = NULL;
  176. PAL_HANDLE parent = NULL, exec = NULL, manifest = NULL;
  177. const char ** argv, ** envp;
  178. int argc;
  179. PAL_HANDLE first_thread;
  180. unsigned long start_time = _DkSystemTimeQueryEarly();
  181. /* parse argc, argv, envp and auxv */
  182. pal_init_bootstrap(args, &pal_name, &argc, &argv, &envp);
  183. pal_map.l_addr = elf_machine_load_address();
  184. pal_map.l_name = pal_name;
  185. elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
  186. pal_map.l_info, pal_map.l_addr);
  187. ELF_DYNAMIC_RELOCATE(&pal_map);
  188. linux_state.environ = envp;
  189. init_slab_mgr(pagesz);
  190. setup_pal_map(&pal_map);
  191. #if USE_VDSO_GETTIME == 1
  192. if (sysinfo_ehdr)
  193. setup_vdso_map(sysinfo_ehdr);
  194. #endif
  195. pal_state.start_time = start_time;
  196. init_child_process(&parent, &exec, &manifest);
  197. if (pal_sec.current_pid)
  198. linux_state.pid = pal_sec.current_pid;
  199. else
  200. linux_state.pid = INLINE_SYSCALL(getpid, 0);
  201. linux_state.uid = uid;
  202. linux_state.gid = gid;
  203. linux_state.process_id = (start_time & (~0xffff)) | linux_state.pid;
  204. if (!linux_state.parent_process_id)
  205. linux_state.parent_process_id = linux_state.process_id;
  206. if (parent)
  207. goto done_init;
  208. /* check if it's a shebang */
  209. int fd = INLINE_SYSCALL(open, 3, argv[0], O_RDONLY|O_CLOEXEC, 0);
  210. if (IS_ERR(fd))
  211. goto done_init;
  212. int len = strlen(argv[0]);
  213. PAL_HANDLE file = malloc(HANDLE_SIZE(file) + len + 1);
  214. SET_HANDLE_TYPE(file, file);
  215. file->__in.flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  216. file->file.fd = fd;
  217. char * path = (void *) file + HANDLE_SIZE(file);
  218. memcpy(path, argv[0], len + 1);
  219. file->file.realpath = path;
  220. if (!check_elf_object(file)) {
  221. exec = file;
  222. goto done_init;
  223. }
  224. #if 0
  225. /* the maximun length for shebang path is 80 chars */
  226. char buffer[80];
  227. int bytes = INLINE_SYSCALL(read, 3, fd, buffer, 80);
  228. if (IS_ERR(bytes))
  229. goto done_init;
  230. /* the format of shebang should be '#!/absoulte/path/of/pal' */
  231. if (buffer[0] != '#' || buffer[1] != '!')
  232. goto done_init;
  233. #endif
  234. manifest = file;
  235. done_init:
  236. first_thread = malloc(HANDLE_SIZE(thread));
  237. SET_HANDLE_TYPE(first_thread, thread);
  238. first_thread->thread.tid = linux_state.pid;
  239. signal_setup();
  240. /* jump to main function */
  241. pal_main(linux_state.parent_process_id,
  242. (void *) pal_map.l_addr, pal_name, argc, argv, envp,
  243. parent, first_thread, exec, manifest);
  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. }