db_main.c 12 KB

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