db_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_main.c
  15. *
  16. * This file contains the main function of the PAL loader, which loads and
  17. * processes environment, arguments and manifest.
  18. */
  19. #include "pal_defs.h"
  20. #include "pal_freebsd_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_freebsd.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "pal_security.h"
  27. #include "api.h"
  28. #include <sys/mman.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <elf/elf.h>
  32. #include <sysdeps/generic/ldsodefs.h>
  33. #include <sys/types.h>
  34. /* At the begining of entry point, rsp starts at argc, then argvs,
  35. envps and auxvs. Here we store rsp to rdi, so it will not be
  36. messed up by function calls */
  37. asm (".global pal_start \n"
  38. " .type pal_start,@function \n"
  39. "pal_start: \n "
  40. " movq %rsp, %rdi \n"
  41. " call pal_bsd_main@PLT \n");
  42. #define RTLD_BOOTSTRAP
  43. /* pal_start is the entry point of libpal.so, which calls pal_main */
  44. #define _ENTRY pal_start
  45. struct pal_bsd_state bsd_state;
  46. struct pal_sec pal_sec;
  47. static size_t g_page_size = PRESET_PAGESIZE;
  48. static uid_t uid;
  49. static gid_t gid;
  50. static void pal_init_bootstrap (void * args, const char ** pal_name,
  51. int * pargc,
  52. const char *** pargv,
  53. const char *** penvp,
  54. ElfW(Addr) * baseaddr)
  55. {
  56. /*
  57. * fetch arguments and environment variables, the previous stack
  58. * pointer is in rdi (arg). The stack structure starting at rdi
  59. * will look like:
  60. * auxv[m - 1] = AT_NULL
  61. * ...
  62. * auxv[0]
  63. * envp[n - 1] = NULL
  64. * ...
  65. * envp[0]
  66. * argv[argc] = NULL
  67. * argv[argc - 1]
  68. * ...
  69. * argv[0]
  70. * argc
  71. * ---------------------------------------
  72. * user stack
  73. */
  74. const char ** all_args = (const char **) args;
  75. /* Workaround because sometimes BSD misaligns arguments on stack */
  76. if (all_args[0] == 0)
  77. all_args++;
  78. int argc = (uintptr_t) all_args[0];
  79. const char ** argv = &all_args[1];
  80. const char ** envp = argv + argc + 1;
  81. /* fetch environment information from aux vectors */
  82. void ** auxv = (void **) envp + 1;
  83. for (; *(auxv - 1); auxv++);
  84. ElfW(auxv_t) *av;
  85. ElfW(Addr) base = 0;
  86. for (av = (ElfW(auxv_t) *)auxv ; av->a_type != AT_NULL ; av++)
  87. switch (av->a_type) {
  88. case AT_PAGESZ:
  89. g_page_size = av->a_un.a_val;
  90. break;
  91. case AT_UID:
  92. case AT_EUID:
  93. uid ^= av->a_un.a_val;
  94. break;
  95. case AT_GID:
  96. case AT_EGID:
  97. gid ^= av->a_un.a_val;
  98. break;
  99. case AT_BASE:
  100. base = (ElfW(Addr)) av->a_un.a_val;
  101. break;
  102. }
  103. *pal_name = argv[0];
  104. argv++;
  105. argc--;
  106. *pargc = argc;
  107. *pargv = argv;
  108. *penvp = envp;
  109. *baseaddr = base;
  110. }
  111. unsigned long _DkGetPagesize (void)
  112. {
  113. return g_page_size;
  114. }
  115. unsigned long _DkGetAllocationAlignment (void)
  116. {
  117. return g_page_size;
  118. }
  119. void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end,
  120. PAL_PTR * hole_start, PAL_PTR * hole_end)
  121. {
  122. void * end_addr, * start_addr;
  123. if ((void *) TEXT_START - (void *) USER_ADDRESS_LOWEST >
  124. (void *) USER_ADDRESS_HIGHEST - (void *) DATA_END){
  125. end_addr = (void*)ALLOC_ALIGN_DOWN(TEXT_START);
  126. start_addr = pal_sec.user_addr_base ? :
  127. (void *) USER_ADDRESS_LOWEST;
  128. } else {
  129. end_addr = (void*)USER_ADDRESS_HIGHEST;
  130. start_addr = (void*)ALLOC_ALIGN_UP(DATA_END);
  131. }
  132. assert(IS_ALLOC_ALIGNED(start_addr) && IS_ALLOC_ALIGNED(end_addr));
  133. while (1) {
  134. if (start_addr >= end_addr)
  135. INIT_FAIL(PAL_ERROR_NOMEM, "no user memory available");
  136. void * mem = (void *) ARCH_MMAP(start_addr,
  137. pal_state.alloc_align,
  138. PROT_NONE,
  139. MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
  140. -1, 0);
  141. if (!IS_ERR_P(mem)) {
  142. INLINE_SYSCALL(munmap, 2, mem, pal_state.alloc_align);
  143. if (mem == start_addr)
  144. break;
  145. }
  146. start_addr = (void *) ((unsigned long) start_addr << 1);
  147. }
  148. *end = (PAL_PTR) end_addr;
  149. *start = (PAL_PTR) start_addr;
  150. // Not used, so set it to an empty range.
  151. *hole_start = start_addr;
  152. *hole_end = start_addr;
  153. }
  154. PAL_NUM _DkGetProcessId (void)
  155. {
  156. return (bsd_state.start_time & (~0xffff)) | bsd_state.pid;
  157. }
  158. PAL_NUM _DkGetHostId (void)
  159. {
  160. return 0;
  161. }
  162. #include "dynamic_link.h"
  163. void setup_pal_map (struct link_map * map);
  164. static struct link_map pal_map;
  165. #ifdef __x86_64__
  166. # include "elf-x86_64.h"
  167. #endif
  168. void pal_bsd_main (void * args)
  169. {
  170. const char * pal_name = NULL;
  171. PAL_HANDLE parent = NULL, exec = NULL, manifest = NULL;
  172. const char ** argv, ** envp;
  173. int argc;
  174. struct timeval time;
  175. INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
  176. /* parse argc, argv, envp and auxv */
  177. pal_init_bootstrap(args, &pal_name, &argc, &argv, &envp, &pal_map.l_addr);
  178. pal_map.l_name = pal_name;
  179. elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
  180. pal_map.l_info, pal_map.l_addr);
  181. ELF_DYNAMIC_RELOCATE(&pal_map);
  182. init_slab_mgr(g_page_size);
  183. setup_pal_map(&pal_map);
  184. bsd_state.start_time = 1000000ULL * time.tv_sec + time.tv_usec;
  185. bsd_state.pid = INLINE_SYSCALL(getpid, 0);
  186. bsd_state.uid = uid;
  187. bsd_state.gid = gid;
  188. if (!bsd_state.parent_pid)
  189. bsd_state.parent_pid = bsd_state.pid;
  190. PAL_HANDLE first_thread = malloc(HANDLE_SIZE(thread));
  191. SET_HANDLE_TYPE(first_thread, thread);
  192. first_thread->thread.tid = bsd_state.pid;
  193. init_child_process(&parent, &exec, &manifest);
  194. if (parent)
  195. goto done_init;
  196. int fd = INLINE_SYSCALL(open, 3, argv[0], O_RDONLY|O_CLOEXEC, 0);
  197. if (IS_ERR(fd))
  198. goto done_init;
  199. size_t len = strlen(argv[0]) + 1;
  200. PAL_HANDLE file = malloc(HANDLE_SIZE(file) + len);
  201. SET_HANDLE_TYPE(file, file);
  202. file->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
  203. file->file.fd = fd;
  204. char * path = (void *) file + HANDLE_SIZE(file);
  205. int ret = get_norm_path(argv[0], path, &len);
  206. if (ret < 0) {
  207. printf("Could not normalize path (%s): %s\n", argv[0], pal_strerror(ret));
  208. goto done_init;
  209. }
  210. file->file.realpath = path;
  211. if (!check_elf_object(file)) {
  212. exec = file;
  213. goto done_init;
  214. }
  215. manifest = file;
  216. done_init:
  217. if (!parent && !exec && !manifest) {
  218. printf("USAGE: %s [executable|manifest] args ...\n", pal_name);
  219. _DkProcessExit(0);
  220. return;
  221. }
  222. signal_setup();
  223. /* jump to main function */
  224. pal_main(bsd_state.parent_pid, manifest, exec, NULL, parent, first_thread, argv, envp);
  225. }
  226. /* the following code is borrowed from CPUID */
  227. static void cpuid (int cpuid_fd, unsigned int reg,
  228. unsigned int words[], unsigned int ecx)
  229. {
  230. asm("cpuid"
  231. : "=a" (words[PAL_CPUID_WORD_EAX]),
  232. "=b" (words[PAL_CPUID_WORD_EBX]),
  233. "=c" (words[PAL_CPUID_WORD_ECX]),
  234. "=d" (words[PAL_CPUID_WORD_EDX])
  235. : "a" (reg),
  236. "c" (ecx));
  237. }
  238. #define FOUR_CHARS_VALUE(s, w) \
  239. (s)[0] = (w) & 0xff; \
  240. (s)[1] = ((w) >> 8) & 0xff; \
  241. (s)[2] = ((w) >> 16) & 0xff; \
  242. (s)[3] = ((w) >> 24) & 0xff;
  243. #define BPI 32
  244. #define POWER2(power) (1ULL << (power))
  245. #define RIGHTMASK(width) \
  246. (((unsigned long)(width) >= BPI) ? ~0ULL : POWER2(width) - 1ULL)
  247. #define BIT_EXTRACT_LE(value, start, after) \
  248. (((unsigned long)(value) & RIGHTMASK(after)) >> (start))
  249. static char * cpu_flags[]
  250. = { "fpu", // "x87 FPU on chip"
  251. "vme", // "virtual-8086 mode enhancement"
  252. "de", // "debugging extensions"
  253. "pse", // "page size extensions"
  254. "tsc", // "time stamp counter"
  255. "msr", // "RDMSR and WRMSR support"
  256. "pae", // "physical address extensions"
  257. "mce", // "machine check exception"
  258. "cx8", // "CMPXCHG8B inst."
  259. "apic", // "APIC on chip"
  260. NULL,
  261. "sep", // "SYSENTER and SYSEXIT"
  262. "mtrr", // "memory type range registers"
  263. "pge", // "PTE global bit"
  264. "mca", // "machine check architecture"
  265. "cmov", // "conditional move/compare instruction"
  266. "pat", // "page attribute table"
  267. "pse36", // "page size extension"
  268. "pn", // "processor serial number"
  269. "clflush", // "CLFLUSH instruction"
  270. NULL,
  271. "dts" // "debug store"
  272. "tm", // "thermal monitor and clock ctrl"
  273. "mmx", // "MMX Technology"
  274. "fxsr", // "FXSAVE/FXRSTOR"
  275. "sse", // "SSE extensions"
  276. "sse2", // "SSE2 extensions"
  277. "ss", // "self snoop"
  278. "ht", // "hyper-threading / multi-core supported"
  279. "tm", // "therm. monitor"
  280. "ia64", // "IA64"
  281. "pbe", // "pending break event"
  282. };
  283. /*
  284. * Returns the number of online CPUs read from sysctl hw.ncpus, -errno on failure.
  285. */
  286. int get_cpu_count(void) {
  287. int mib[2], cores;
  288. size_t len = sizeof(cores);
  289. mib[0] = CTL_HW;
  290. mib[1] = HW_NCPU;
  291. int rv = sysctl(mib, 2, &cores, &len, NULL, 0);
  292. if (rv < 0)
  293. return unix_to_pal_error(ERRNO(rv));
  294. return cores;
  295. }
  296. int _DkGetCPUInfo (PAL_CPU_INFO * ci)
  297. {
  298. unsigned int words[PAL_CPUID_WORD_NUM];
  299. const size_t VENDOR_ID_SIZE = 13;
  300. char* vendor_id = malloc(VENDOR_ID_SIZE);
  301. cpuid(2, 0, words, 0);
  302. FOUR_CHARS_VALUE(&vendor_id[0], words[PAL_CPUID_WORD_EBX]);
  303. FOUR_CHARS_VALUE(&vendor_id[4], words[PAL_CPUID_WORD_EDX]);
  304. FOUR_CHARS_VALUE(&vendor_id[8], words[PAL_CPUID_WORD_ECX]);
  305. vendor_id[VENDOR_ID_SIZE - 1] = '\0';
  306. ci->cpu_vendor = vendor_id;
  307. const size_t BRAND_SIZE = 49;
  308. char* brand = malloc(BRAND_SIZE);
  309. cpuid(-2, 0x80000002, words, 0);
  310. memcpy(&brand[ 0], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
  311. cpuid(-2, 0x80000003, words, 0);
  312. memcpy(&brand[16], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
  313. cpuid(-2, 0x80000004, words, 0);
  314. memcpy(&brand[32], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
  315. brand[BRAND_SIZE - 1] = '\0';
  316. ci->cpu_brand = brand;
  317. cpuid(2, 1, words, 0);
  318. ci->cpu_family = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 8, 12) + 1;
  319. ci->cpu_model = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 4, 8);
  320. ci->cpu_stepping = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX], 0, 4);
  321. /* we cannot use CPUID(0xb) because it counts even disabled-by-BIOS cores (e.g. HT cores);
  322. * instead we extract info on number of online CPUs by parsing sysfs pseudo-files */
  323. int cores = get_cpu_count();
  324. if (cores < 0) {
  325. free(brand);
  326. free(vendor_id);
  327. return cores;
  328. }
  329. ci->cpu_num = cores;
  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[PAL_CPUID_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. }