bootstrap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1992-1989 Carnegie Mellon University.
  4. * Copyright (c) 1995-1993 The University of Utah and
  5. * the Computer Systems Laboratory (CSL).
  6. * All rights reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
  15. * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
  16. * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
  17. * THIS SOFTWARE.
  18. *
  19. * Carnegie Mellon requests users of this software to return to
  20. *
  21. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  22. * School of Computer Science
  23. * Carnegie Mellon University
  24. * Pittsburgh PA 15213-3890
  25. *
  26. * any improvements or extensions that they make and grant Carnegie Mellon
  27. * the rights to redistribute these changes.
  28. */
  29. /*
  30. * Bootstrap the various built-in servers.
  31. */
  32. #include <alloca.h>
  33. #include <string.h>
  34. #include <mach/port.h>
  35. #include <mach/message.h>
  36. #include <machine/locore.h>
  37. #include <machine/vm_param.h>
  38. #include <machine/pcb.h>
  39. #include <ipc/ipc_port.h>
  40. #include <ipc/mach_port.h>
  41. #include <kern/debug.h>
  42. #include <kern/host.h>
  43. #include <kern/printf.h>
  44. #include <kern/kalloc.h>
  45. #include <kern/task.h>
  46. #include <kern/thread.h>
  47. #include <kern/lock.h>
  48. #include <vm/vm_kern.h>
  49. #include <vm/vm_user.h>
  50. #include <vm/pmap.h>
  51. #include <device/device_port.h>
  52. #if MACH_KDB
  53. #include <machine/db_machdep.h>
  54. #include <ddb/db_sym.h>
  55. #endif
  56. #if OSKIT_MACH
  57. #include <stddef.h>
  58. #include <oskit/machine/base_multiboot.h>
  59. #include <oskit/exec/exec.h>
  60. #include <oskit/c/stdio.h>
  61. #define safe_gets(s, n) fgets((s),(n),stdin)
  62. #else
  63. #include <mach/machine/multiboot.h>
  64. #include <mach/exec/exec.h>
  65. #ifdef MACH_XEN
  66. #include <mach/xen.h>
  67. extern struct start_info boot_info; /* XXX put this in a header! */
  68. #else /* MACH_XEN */
  69. extern struct multiboot_info boot_info; /* XXX put this in a header! */
  70. #endif /* MACH_XEN */
  71. #endif
  72. #include "boot_script.h"
  73. static mach_port_t boot_device_port; /* local name */
  74. static mach_port_t boot_host_port; /* local name */
  75. extern char *kernel_cmdline;
  76. static void user_bootstrap(void); /* forward */
  77. static void user_bootstrap_compat(void); /* forward */
  78. static void bootstrap_exec_compat(void *exec_data); /* forward */
  79. static void get_compat_strings(char *flags_str, char *root_str); /* forward */
  80. static mach_port_t
  81. task_insert_send_right(
  82. task_t task,
  83. ipc_port_t port)
  84. {
  85. mach_port_t name;
  86. for (name = 1;; name++) {
  87. kern_return_t kr;
  88. kr = mach_port_insert_right(task->itk_space, name,
  89. port, MACH_MSG_TYPE_PORT_SEND);
  90. if (kr == KERN_SUCCESS)
  91. break;
  92. assert(kr == KERN_NAME_EXISTS);
  93. }
  94. return name;
  95. }
  96. static void
  97. free_bootstrap_pages(phys_addr_t start, phys_addr_t end)
  98. {
  99. struct vm_page *page;
  100. while (start < end)
  101. {
  102. page = vm_page_lookup_pa(start);
  103. assert(page != NULL);
  104. vm_page_manage(page);
  105. start += PAGE_SIZE;
  106. }
  107. }
  108. void bootstrap_create(void)
  109. {
  110. int compat;
  111. unsigned n = 0;
  112. #ifdef MACH_XEN
  113. struct multiboot_module *bmods = ((struct multiboot_module *)
  114. boot_info.mod_start);
  115. if (bmods)
  116. for (n = 0; bmods[n].mod_start; n++) {
  117. bmods[n].mod_start = kvtophys(bmods[n].mod_start + (vm_offset_t) bmods);
  118. bmods[n].mod_end = kvtophys(bmods[n].mod_end + (vm_offset_t) bmods);
  119. bmods[n].string = kvtophys(bmods[n].string + (vm_offset_t) bmods);
  120. }
  121. boot_info.mods_count = n;
  122. boot_info.flags |= MULTIBOOT_MODS;
  123. #else /* MACH_XEN */
  124. struct multiboot_module *bmods = ((struct multiboot_module *)
  125. phystokv(boot_info.mods_addr));
  126. #endif /* MACH_XEN */
  127. if (!(boot_info.flags & MULTIBOOT_MODS)
  128. || (boot_info.mods_count == 0))
  129. panic ("No bootstrap code loaded with the kernel!");
  130. compat = boot_info.mods_count == 1;
  131. if (compat)
  132. {
  133. char *p = strchr((char*)phystokv(bmods[0].string), ' ');
  134. if (p != 0)
  135. do
  136. ++p;
  137. while (*p == ' ' || *p == '\n');
  138. compat = p == 0 || *p == '\0';
  139. }
  140. if (compat)
  141. {
  142. printf("Loading single multiboot module in compat mode: %s\n",
  143. (char*)phystokv(bmods[0].string));
  144. bootstrap_exec_compat(&bmods[0]);
  145. }
  146. else
  147. {
  148. unsigned i;
  149. int losers;
  150. /* Initialize boot script variables. We leak these send rights. */
  151. losers = boot_script_set_variable
  152. ("host-port", VAL_PORT,
  153. (long) realhost.host_priv_self);
  154. if (losers)
  155. panic ("cannot set boot-script variable host-port: %s",
  156. boot_script_error_string (losers));
  157. losers = boot_script_set_variable
  158. ("device-port", VAL_PORT,
  159. (long) master_device_port);
  160. if (losers)
  161. panic ("cannot set boot-script variable device-port: %s",
  162. boot_script_error_string (losers));
  163. losers = boot_script_set_variable
  164. ("kernel-task", VAL_PORT,
  165. (long) kernel_task->itk_self);
  166. if (losers)
  167. panic ("cannot set boot-script variable kernel-task: %s",
  168. boot_script_error_string (losers));
  169. losers = boot_script_set_variable ("kernel-command-line", VAL_STR,
  170. (long) kernel_cmdline);
  171. if (losers)
  172. panic ("cannot set boot-script variable %s: %s",
  173. "kernel-command-line", boot_script_error_string (losers));
  174. {
  175. /* Set the same boot script variables that the old Hurd's
  176. serverboot did, so an old Hurd and boot script previously
  177. used with serverboot can be used directly with this kernel. */
  178. char *flag_string = alloca(1024);
  179. char *root_string = alloca(1024);
  180. /*
  181. * Get the (compatibility) boot flags and root name strings.
  182. */
  183. get_compat_strings(flag_string, root_string);
  184. losers = boot_script_set_variable ("boot-args", VAL_STR,
  185. (long) flag_string);
  186. if (losers)
  187. panic ("cannot set boot-script variable %s: %s",
  188. "boot-args", boot_script_error_string (losers));
  189. losers = boot_script_set_variable ("root-device", VAL_STR,
  190. (long) root_string);
  191. if (losers)
  192. panic ("cannot set boot-script variable %s: %s",
  193. "root-device", boot_script_error_string (losers));
  194. }
  195. #if OSKIT_MACH
  196. {
  197. /* The oskit's "environ" array contains all the words from
  198. the multiboot command line that looked like VAR=VAL.
  199. We set each of these as boot-script variables, which
  200. can be used for things like ${root}. */
  201. extern char **environ;
  202. char **ep;
  203. for (ep = environ; *ep != 0; ++ep)
  204. {
  205. size_t len = strlen (*ep) + 1;
  206. char *var = memcpy (alloca (len), *ep, len);
  207. char *val = strchr (var, '=');
  208. *val++ = '\0';
  209. losers = boot_script_set_variable (var, VAL_STR, (long) val);
  210. if (losers)
  211. panic ("cannot set boot-script variable %s: %s",
  212. var, boot_script_error_string (losers));
  213. }
  214. }
  215. #else /* GNUmach, not oskit-mach */
  216. {
  217. /* Turn each `FOO=BAR' word in the command line into a boot script
  218. variable ${FOO} with value BAR. This matches what we get from
  219. oskit's environ in the oskit-mach case (above). */
  220. int len = strlen (kernel_cmdline) + 1;
  221. char *s = memcpy (alloca (len), kernel_cmdline, len);
  222. char *word;
  223. while ((word = strsep (&s, " \t")) != 0)
  224. {
  225. char *eq = strchr (word, '=');
  226. if (eq == 0)
  227. continue;
  228. *eq++ = '\0';
  229. losers = boot_script_set_variable (word, VAL_STR, (long) eq);
  230. if (losers)
  231. panic ("cannot set boot-script variable %s: %s",
  232. word, boot_script_error_string (losers));
  233. }
  234. }
  235. #endif
  236. for (i = 0; i < boot_info.mods_count; ++i)
  237. {
  238. int err;
  239. char *line = (char*)phystokv(bmods[i].string);
  240. printf ("module %d: %s\n", i, line);
  241. err = boot_script_parse_line (&bmods[i], line);
  242. if (err)
  243. {
  244. printf ("\n\tERROR: %s", boot_script_error_string (err));
  245. ++losers;
  246. }
  247. }
  248. printf ("%d multiboot modules\n", i);
  249. if (losers)
  250. panic ("%d of %d boot script commands could not be parsed",
  251. losers, boot_info.mods_count);
  252. losers = boot_script_exec ();
  253. if (losers)
  254. panic ("ERROR in executing boot script: %s",
  255. boot_script_error_string (losers));
  256. }
  257. /* XXX we could free the memory used
  258. by the boot loader's descriptors and such. */
  259. for (n = 0; n < boot_info.mods_count; n++)
  260. free_bootstrap_pages(bmods[n].mod_start, bmods[n].mod_end);
  261. }
  262. static void
  263. bootstrap_exec_compat(void *e)
  264. {
  265. task_t bootstrap_task;
  266. thread_t bootstrap_thread;
  267. /*
  268. * Create the bootstrap task.
  269. */
  270. (void) task_create(TASK_NULL, FALSE, &bootstrap_task);
  271. (void) thread_create(bootstrap_task, &bootstrap_thread);
  272. /*
  273. * Insert send rights to the master host and device ports.
  274. */
  275. boot_host_port =
  276. task_insert_send_right(bootstrap_task,
  277. ipc_port_make_send(realhost.host_priv_self));
  278. boot_device_port =
  279. task_insert_send_right(bootstrap_task,
  280. ipc_port_make_send(master_device_port));
  281. /*
  282. * Start the bootstrap thread.
  283. */
  284. bootstrap_thread->saved.other = e;
  285. thread_start(bootstrap_thread, user_bootstrap_compat);
  286. (void) thread_resume(bootstrap_thread);
  287. }
  288. /*
  289. * The following code runs as the kernel mode portion of the
  290. * first user thread.
  291. */
  292. /*
  293. * Convert an unsigned integer to its decimal representation.
  294. */
  295. static void
  296. itoa(
  297. char *str,
  298. vm_size_t num)
  299. {
  300. char buf[sizeof(vm_size_t)*2+3];
  301. char *np;
  302. np = buf + sizeof(buf);
  303. *--np = 0;
  304. do {
  305. *--np = '0' + num % 10;
  306. num /= 10;
  307. } while (num != 0);
  308. strcpy(str, np);
  309. }
  310. /*
  311. * Collect the boot flags into a single argument string,
  312. * for compatibility with existing bootstrap and startup code.
  313. * Format as a standard flag argument: '-qsdn...'
  314. */
  315. static void get_compat_strings(char *flags_str, char *root_str)
  316. {
  317. char *ip, *cp;
  318. strcpy (root_str, "UNKNOWN");
  319. cp = flags_str;
  320. *cp++ = '-';
  321. for (ip = kernel_cmdline; *ip; )
  322. {
  323. if (*ip == ' ')
  324. {
  325. ip++;
  326. }
  327. else if (*ip == '-')
  328. {
  329. ip++;
  330. while (*ip > ' ')
  331. *cp++ = *ip++;
  332. }
  333. else if (strncmp(ip, "root=", 5) == 0)
  334. {
  335. char *rp = root_str;
  336. ip += 5;
  337. if (strncmp(ip, "/dev/", 5) == 0)
  338. ip += 5;
  339. while (*ip > ' ')
  340. *rp++ = *ip++;
  341. *rp = '\0';
  342. }
  343. else
  344. {
  345. while (*ip > ' ')
  346. ip++;
  347. }
  348. }
  349. if (cp == &flags_str[1]) /* no flags */
  350. *cp++ = 'x';
  351. *cp = '\0';
  352. }
  353. #if 0
  354. /*
  355. * Copy boot_data (executable) to the user portion of this task.
  356. */
  357. static boolean_t load_protect_text = TRUE;
  358. #if MACH_KDB
  359. /* if set, fault in the text segment */
  360. static boolean_t load_fault_in_text = TRUE;
  361. #endif
  362. static vm_offset_t
  363. boot_map(
  364. void * data, /* private data */
  365. vm_offset_t offset) /* offset to map */
  366. {
  367. vm_offset_t start_offset = (vm_offset_t) data;
  368. return pmap_extract(kernel_pmap, start_offset + offset);
  369. }
  370. #if BOOTSTRAP_SYMBOLS
  371. static boolean_t load_bootstrap_symbols = TRUE;
  372. #else
  373. static boolean_t load_bootstrap_symbols = FALSE;
  374. #endif
  375. #endif
  376. static int
  377. boot_read(void *handle, vm_offset_t file_ofs, void *buf, vm_size_t size,
  378. vm_size_t *out_actual)
  379. {
  380. struct multiboot_module *mod = handle;
  381. if (mod->mod_start + file_ofs + size > mod->mod_end)
  382. return -1;
  383. memcpy(buf, (const char*) phystokv (mod->mod_start) + file_ofs, size);
  384. *out_actual = size;
  385. return 0;
  386. }
  387. static int
  388. read_exec(void *handle, vm_offset_t file_ofs, vm_size_t file_size,
  389. vm_offset_t mem_addr, vm_size_t mem_size,
  390. exec_sectype_t sec_type)
  391. {
  392. struct multiboot_module *mod = handle;
  393. vm_map_t user_map = current_task()->map;
  394. vm_offset_t start_page, end_page;
  395. vm_prot_t mem_prot = sec_type & EXEC_SECTYPE_PROT_MASK;
  396. int err;
  397. if (mod->mod_start + file_ofs + file_size > mod->mod_end)
  398. return -1;
  399. if (!(sec_type & EXEC_SECTYPE_ALLOC))
  400. return 0;
  401. assert(mem_size > 0);
  402. assert(mem_size >= file_size);
  403. start_page = trunc_page(mem_addr);
  404. end_page = round_page(mem_addr + mem_size);
  405. #if 0
  406. printf("reading bootstrap section %08x-%08x-%08x prot %d pages %08x-%08x\n",
  407. mem_addr, mem_addr+file_size, mem_addr+mem_size, mem_prot, start_page, end_page);
  408. #endif
  409. err = vm_allocate(user_map, &start_page, end_page - start_page, FALSE);
  410. assert(err == 0);
  411. assert(start_page == trunc_page(mem_addr));
  412. if (file_size > 0)
  413. {
  414. err = copyout((char *)phystokv (mod->mod_start) + file_ofs,
  415. (void *)mem_addr, file_size);
  416. assert(err == 0);
  417. }
  418. if (mem_prot != VM_PROT_ALL)
  419. {
  420. err = vm_protect(user_map, start_page, end_page - start_page, FALSE, mem_prot);
  421. assert(err == 0);
  422. }
  423. return 0;
  424. }
  425. static void copy_bootstrap(void *e, exec_info_t *boot_exec_info)
  426. {
  427. /* vm_map_t user_map = current_task()->map; */
  428. int err;
  429. if ((err = exec_load(boot_read, read_exec, e, boot_exec_info)))
  430. panic("Cannot load user-bootstrap image: error code %d", err);
  431. #if MACH_KDB
  432. /*
  433. * Enter the bootstrap symbol table.
  434. */
  435. #if 0 /*XXX*/
  436. if (load_bootstrap_symbols)
  437. (void) X_db_sym_init(
  438. (char*) boot_start+lp->sym_offset,
  439. (char*) boot_start+lp->sym_offset+lp->sym_size,
  440. "bootstrap",
  441. (char *) user_map);
  442. #endif
  443. #if 0 /*XXX*/
  444. if (load_fault_in_text)
  445. {
  446. vm_offset_t lenp = round_page(lp->text_start+lp->text_size) -
  447. trunc_page(lp->text_start);
  448. vm_offset_t i = 0;
  449. while (i < lenp)
  450. {
  451. vm_fault(user_map, text_page_start +i,
  452. load_protect_text ?
  453. VM_PROT_READ|VM_PROT_EXECUTE :
  454. VM_PROT_READ|VM_PROT_EXECUTE | VM_PROT_WRITE,
  455. 0,0,0);
  456. i = round_page (i+1);
  457. }
  458. }
  459. #endif
  460. #endif /* MACH_KDB */
  461. }
  462. /*
  463. * Allocate the stack, and build the argument list.
  464. */
  465. static void
  466. build_args_and_stack(struct exec_info *boot_exec_info,
  467. char **argv, char **envp)
  468. {
  469. vm_offset_t stack_base;
  470. vm_size_t stack_size;
  471. char * arg_ptr;
  472. int arg_count, envc;
  473. int arg_len;
  474. char * arg_pos;
  475. int arg_item_len;
  476. char * string_pos;
  477. char * zero = (char *)0;
  478. int i;
  479. #define STACK_SIZE (64*1024)
  480. /*
  481. * Calculate the size of the argument list.
  482. */
  483. arg_len = 0;
  484. arg_count = 0;
  485. while (argv[arg_count] != 0) {
  486. arg_ptr = argv[arg_count++];
  487. arg_len += strlen(arg_ptr) + 1;
  488. }
  489. envc = 0;
  490. if (envp != 0)
  491. while (envp[envc] != 0)
  492. arg_len += strlen (envp[envc++]) + 1;
  493. /*
  494. * Add space for:
  495. * arg count
  496. * pointers to arguments
  497. * trailing 0 pointer
  498. * pointers to environment variables
  499. * trailing 0 pointer
  500. * and align to integer boundary
  501. */
  502. arg_len += (sizeof(integer_t)
  503. + (arg_count + 1 + envc + 1) * sizeof(char *));
  504. arg_len = (arg_len + sizeof(integer_t) - 1) & ~(sizeof(integer_t)-1);
  505. /*
  506. * Allocate the stack.
  507. */
  508. stack_size = round_page(STACK_SIZE);
  509. stack_base = user_stack_low(stack_size);
  510. (void) vm_allocate(current_task()->map,
  511. &stack_base,
  512. stack_size,
  513. FALSE);
  514. arg_pos = (char *)
  515. set_user_regs(stack_base, stack_size, boot_exec_info, arg_len);
  516. /*
  517. * Start the strings after the arg-count and pointers
  518. */
  519. string_pos = (arg_pos
  520. + sizeof(integer_t)
  521. + (arg_count + 1 + envc + 1) * sizeof(char *));
  522. /*
  523. * first the argument count
  524. */
  525. (void) copyout(&arg_count,
  526. arg_pos,
  527. sizeof(integer_t));
  528. arg_pos += sizeof(integer_t);
  529. /*
  530. * Then the strings and string pointers for each argument
  531. */
  532. for (i = 0; i < arg_count; ++i) {
  533. arg_ptr = argv[i];
  534. arg_item_len = strlen(arg_ptr) + 1; /* include trailing 0 */
  535. /* set string pointer */
  536. (void) copyout(&string_pos,
  537. arg_pos,
  538. sizeof (char *));
  539. arg_pos += sizeof(char *);
  540. /* copy string */
  541. (void) copyout(arg_ptr, string_pos, arg_item_len);
  542. string_pos += arg_item_len;
  543. }
  544. /*
  545. * Null terminator for argv.
  546. */
  547. (void) copyout(&zero, arg_pos, sizeof(char *));
  548. arg_pos += sizeof(char *);
  549. /*
  550. * Then the strings and string pointers for each environment variable
  551. */
  552. for (i = 0; i < envc; ++i) {
  553. arg_ptr = envp[i];
  554. arg_item_len = strlen(arg_ptr) + 1; /* include trailing 0 */
  555. /* set string pointer */
  556. (void) copyout(&string_pos,
  557. arg_pos,
  558. sizeof (char *));
  559. arg_pos += sizeof(char *);
  560. /* copy string */
  561. (void) copyout(arg_ptr, string_pos, arg_item_len);
  562. string_pos += arg_item_len;
  563. }
  564. /*
  565. * Null terminator for envp.
  566. */
  567. (void) copyout(&zero, arg_pos, sizeof(char *));
  568. }
  569. static void
  570. user_bootstrap_compat(void)
  571. {
  572. exec_info_t boot_exec_info;
  573. char host_string[12];
  574. char device_string[12];
  575. char flag_string[1024];
  576. char root_string[1024];
  577. /*
  578. * Copy the bootstrap code from boot_exec into the user task.
  579. */
  580. copy_bootstrap(current_thread()->saved.other, &boot_exec_info);
  581. /*
  582. * Convert the host and device ports to strings,
  583. * to put in the argument list.
  584. */
  585. itoa(host_string, boot_host_port);
  586. itoa(device_string, boot_device_port);
  587. /*
  588. * Get the (compatibility) boot flags and root name strings.
  589. */
  590. get_compat_strings(flag_string, root_string);
  591. /*
  592. * Build the argument list and insert in the user task.
  593. * Argument list is
  594. * "bootstrap -<boothowto> <host_port> <device_port> <root_name>"
  595. $0 ${boot-args} ${host-port} ${device-port} ${root-device} $(task-create) $(task-resume)
  596. */
  597. {
  598. char *argv[] = { "bootstrap",
  599. flag_string,
  600. host_string,
  601. device_string,
  602. root_string,
  603. 0 };
  604. char *envp[] = { 0, 0 };
  605. if (kernel_cmdline[0] != '\0')
  606. {
  607. static const char cmdline_var[] = "MULTIBOOT_CMDLINE=";
  608. envp[0] = alloca (sizeof cmdline_var + strlen (kernel_cmdline));
  609. memcpy (envp[0], cmdline_var, sizeof cmdline_var - 1);
  610. strcpy (envp[0] + sizeof cmdline_var - 1, kernel_cmdline);
  611. }
  612. build_args_and_stack(&boot_exec_info, argv, envp);
  613. }
  614. /*
  615. * Exit to user thread.
  616. */
  617. thread_bootstrap_return();
  618. /*NOTREACHED*/
  619. }
  620. struct user_bootstrap_info
  621. {
  622. struct multiboot_module *mod;
  623. char **argv;
  624. int done;
  625. decl_simple_lock_data(,lock)
  626. };
  627. int
  628. boot_script_exec_cmd (void *hook, task_t task, char *path, int argc,
  629. char **argv, char *strings, int stringlen)
  630. {
  631. struct multiboot_module *mod = hook;
  632. int err;
  633. if (task != MACH_PORT_NULL)
  634. {
  635. thread_t thread;
  636. struct user_bootstrap_info info = { mod, argv, 0, };
  637. simple_lock_init (&info.lock);
  638. err = thread_create ((task_t)task, &thread);
  639. assert(err == 0);
  640. simple_lock (&info.lock);
  641. thread->saved.other = &info;
  642. thread_start (thread, user_bootstrap);
  643. err = thread_resume (thread);
  644. assert(err == 0);
  645. /* We need to synchronize with the new thread and block this
  646. main thread until it has finished referring to our local state. */
  647. while (! info.done)
  648. {
  649. thread_sleep ((event_t) &info, simple_lock_addr(info.lock), FALSE);
  650. simple_lock (&info.lock);
  651. }
  652. simple_unlock (&info.lock);
  653. thread_deallocate (thread);
  654. printf ("\n");
  655. }
  656. return 0;
  657. }
  658. static void user_bootstrap(void)
  659. {
  660. struct user_bootstrap_info *info = current_thread()->saved.other;
  661. exec_info_t boot_exec_info;
  662. int err;
  663. char **av;
  664. /* Load this task up from the executable file in the module. */
  665. err = exec_load(boot_read, read_exec, info->mod, &boot_exec_info);
  666. if (err)
  667. panic ("Cannot load user executable module (error code %d): %s",
  668. err, info->argv[0]);
  669. printf ("task loaded:");
  670. /* Set up the stack with arguments. */
  671. build_args_and_stack(&boot_exec_info, info->argv, 0);
  672. for (av = info->argv; *av != 0; ++av)
  673. printf (" %s", *av);
  674. task_suspend (current_task());
  675. /* Tell the bootstrap thread running boot_script_exec_cmd
  676. that we are done looking at INFO. */
  677. simple_lock (&info->lock);
  678. assert (!info->done);
  679. info->done = 1;
  680. simple_unlock (&info->lock);
  681. thread_wakeup ((event_t) info);
  682. /*
  683. * Exit to user thread.
  684. */
  685. thread_bootstrap_return();
  686. /*NOTREACHED*/
  687. }
  688. void *
  689. boot_script_malloc (unsigned int size)
  690. {
  691. return (void *) kalloc (size);
  692. }
  693. void
  694. boot_script_free (void *ptr, unsigned int size)
  695. {
  696. kfree ((vm_offset_t)ptr, size);
  697. }
  698. int
  699. boot_script_task_create (struct cmd *cmd)
  700. {
  701. kern_return_t rc = task_create_kernel(TASK_NULL, FALSE, &cmd->task);
  702. if (rc)
  703. {
  704. printf("boot_script_task_create failed with %x\n", rc);
  705. return BOOT_SCRIPT_MACH_ERROR;
  706. }
  707. task_set_name(cmd->task, cmd->path);
  708. return 0;
  709. }
  710. int
  711. boot_script_task_resume (struct cmd *cmd)
  712. {
  713. kern_return_t rc = task_resume (cmd->task);
  714. if (rc)
  715. {
  716. printf("boot_script_task_resume failed with %x\n", rc);
  717. return BOOT_SCRIPT_MACH_ERROR;
  718. }
  719. printf ("\nstart %s: ", cmd->path);
  720. return 0;
  721. }
  722. int
  723. boot_script_prompt_task_resume (struct cmd *cmd)
  724. {
  725. #if ! MACH_KDB
  726. char xx[5];
  727. #endif
  728. printf ("Pausing for %s...\n", cmd->path);
  729. #if ! MACH_KDB
  730. printf ("Hit <return> to resume bootstrap.");
  731. safe_gets (xx, sizeof xx);
  732. #else
  733. SoftDebugger("Hit `c<return>' to resume bootstrap.");
  734. #endif
  735. return boot_script_task_resume (cmd);
  736. }
  737. void
  738. boot_script_free_task (task_t task, int aborting)
  739. {
  740. if (aborting)
  741. task_terminate (task);
  742. task_deallocate (task);
  743. }
  744. int
  745. boot_script_insert_right (struct cmd *cmd, mach_port_t port, mach_port_t *name)
  746. {
  747. *name = task_insert_send_right (cmd->task,
  748. ipc_port_make_send((ipc_port_t) port));
  749. return 0;
  750. }
  751. int
  752. boot_script_insert_task_port (struct cmd *cmd, task_t task, mach_port_t *name)
  753. {
  754. *name = task_insert_send_right (cmd->task, task->itk_sself);
  755. return 0;
  756. }