um_arch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/kernel.h"
  6. #include "linux/sched.h"
  7. #include "linux/notifier.h"
  8. #include "linux/mm.h"
  9. #include "linux/types.h"
  10. #include "linux/tty.h"
  11. #include "linux/init.h"
  12. #include "linux/bootmem.h"
  13. #include "linux/spinlock.h"
  14. #include "linux/utsname.h"
  15. #include "linux/sysrq.h"
  16. #include "linux/seq_file.h"
  17. #include "linux/delay.h"
  18. #include "linux/module.h"
  19. #include "linux/utsname.h"
  20. #include "asm/page.h"
  21. #include "asm/pgtable.h"
  22. #include "asm/ptrace.h"
  23. #include "asm/elf.h"
  24. #include "asm/user.h"
  25. #include "asm/setup.h"
  26. #include "ubd_user.h"
  27. #include "asm/current.h"
  28. #include "kern_util.h"
  29. #include "as-layout.h"
  30. #include "arch.h"
  31. #include "kern.h"
  32. #include "mem_user.h"
  33. #include "mem.h"
  34. #include "initrd.h"
  35. #include "init.h"
  36. #include "os.h"
  37. #include "choose-mode.h"
  38. #include "mode_kern.h"
  39. #include "mode.h"
  40. #ifdef UML_CONFIG_MODE_SKAS
  41. #include "skas.h"
  42. #endif
  43. #define DEFAULT_COMMAND_LINE "root=98:0"
  44. /* Changed in add_arg and setup_arch, which run before SMP is started */
  45. static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
  46. static void __init add_arg(char *arg)
  47. {
  48. if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
  49. printf("add_arg: Too many command line arguments!\n");
  50. exit(1);
  51. }
  52. if(strlen(command_line) > 0)
  53. strcat(command_line, " ");
  54. strcat(command_line, arg);
  55. }
  56. /*
  57. * These fields are initialized at boot time and not changed.
  58. * XXX This structure is used only in the non-SMP case. Maybe this
  59. * should be moved to smp.c.
  60. */
  61. struct cpuinfo_um boot_cpu_data = {
  62. .loops_per_jiffy = 0,
  63. .ipi_pipe = { -1, -1 }
  64. };
  65. unsigned long thread_saved_pc(struct task_struct *task)
  66. {
  67. return os_process_pc(CHOOSE_MODE_PROC(thread_pid_tt, thread_pid_skas,
  68. task));
  69. }
  70. /* Changed in setup_arch, which is called in early boot */
  71. static char host_info[(__NEW_UTS_LEN + 1) * 5];
  72. static int show_cpuinfo(struct seq_file *m, void *v)
  73. {
  74. int index = 0;
  75. #ifdef CONFIG_SMP
  76. index = (struct cpuinfo_um *) v - cpu_data;
  77. if (!cpu_online(index))
  78. return 0;
  79. #endif
  80. seq_printf(m, "processor\t: %d\n", index);
  81. seq_printf(m, "vendor_id\t: User Mode Linux\n");
  82. seq_printf(m, "model name\t: UML\n");
  83. seq_printf(m, "mode\t\t: %s\n", CHOOSE_MODE("tt", "skas"));
  84. seq_printf(m, "host\t\t: %s\n", host_info);
  85. seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
  86. loops_per_jiffy/(500000/HZ),
  87. (loops_per_jiffy/(5000/HZ)) % 100);
  88. return 0;
  89. }
  90. static void *c_start(struct seq_file *m, loff_t *pos)
  91. {
  92. return *pos < NR_CPUS ? cpu_data + *pos : NULL;
  93. }
  94. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  95. {
  96. ++*pos;
  97. return c_start(m, pos);
  98. }
  99. static void c_stop(struct seq_file *m, void *v)
  100. {
  101. }
  102. const struct seq_operations cpuinfo_op = {
  103. .start = c_start,
  104. .next = c_next,
  105. .stop = c_stop,
  106. .show = show_cpuinfo,
  107. };
  108. /* Set in linux_main */
  109. unsigned long host_task_size;
  110. unsigned long task_size;
  111. unsigned long uml_physmem;
  112. unsigned long uml_reserved; /* Also modified in mem_init */
  113. unsigned long start_vm;
  114. unsigned long end_vm;
  115. /* Set in uml_ncpus_setup */
  116. int ncpus = 1;
  117. #ifdef CONFIG_CMDLINE_ON_HOST
  118. /* Pointer set in linux_main, the array itself is private to each thread,
  119. * and changed at address space creation time so this poses no concurrency
  120. * problems.
  121. */
  122. static char *argv1_begin = NULL;
  123. static char *argv1_end = NULL;
  124. #endif
  125. /* Set in early boot */
  126. static int have_root __initdata = 0;
  127. /* Set in uml_mem_setup and modified in linux_main */
  128. long long physmem_size = 32 * 1024 * 1024;
  129. void set_cmdline(char *cmd)
  130. {
  131. #ifdef CONFIG_CMDLINE_ON_HOST
  132. char *umid, *ptr;
  133. if(CHOOSE_MODE(honeypot, 0)) return;
  134. umid = get_umid();
  135. if(*umid != '\0'){
  136. snprintf(argv1_begin,
  137. (argv1_end - argv1_begin) * sizeof(*ptr),
  138. "(%s) ", umid);
  139. ptr = &argv1_begin[strlen(argv1_begin)];
  140. }
  141. else ptr = argv1_begin;
  142. snprintf(ptr, (argv1_end - ptr) * sizeof(*ptr), "[%s]", cmd);
  143. memset(argv1_begin + strlen(argv1_begin), '\0',
  144. argv1_end - argv1_begin - strlen(argv1_begin));
  145. #endif
  146. }
  147. static char *usage_string =
  148. "User Mode Linux v%s\n"
  149. " available at http://user-mode-linux.sourceforge.net/\n\n";
  150. static int __init uml_version_setup(char *line, int *add)
  151. {
  152. printf("%s\n", init_utsname()->release);
  153. exit(0);
  154. return 0;
  155. }
  156. __uml_setup("--version", uml_version_setup,
  157. "--version\n"
  158. " Prints the version number of the kernel.\n\n"
  159. );
  160. static int __init uml_root_setup(char *line, int *add)
  161. {
  162. have_root = 1;
  163. return 0;
  164. }
  165. __uml_setup("root=", uml_root_setup,
  166. "root=<file containing the root fs>\n"
  167. " This is actually used by the generic kernel in exactly the same\n"
  168. " way as in any other kernel. If you configure a number of block\n"
  169. " devices and want to boot off something other than ubd0, you \n"
  170. " would use something like:\n"
  171. " root=/dev/ubd5\n\n"
  172. );
  173. #ifndef CONFIG_MODE_TT
  174. static int __init no_skas_debug_setup(char *line, int *add)
  175. {
  176. printf("'debug' is not necessary to gdb UML in skas mode - run \n");
  177. printf("'gdb linux' and disable CONFIG_CMDLINE_ON_HOST if gdb \n");
  178. printf("doesn't work as expected\n");
  179. return 0;
  180. }
  181. __uml_setup("debug", no_skas_debug_setup,
  182. "debug\n"
  183. " this flag is not needed to run gdb on UML in skas mode\n\n"
  184. );
  185. #endif
  186. #ifdef CONFIG_SMP
  187. static int __init uml_ncpus_setup(char *line, int *add)
  188. {
  189. if (!sscanf(line, "%d", &ncpus)) {
  190. printf("Couldn't parse [%s]\n", line);
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. __uml_setup("ncpus=", uml_ncpus_setup,
  196. "ncpus=<# of desired CPUs>\n"
  197. " This tells an SMP kernel how many virtual processors to start.\n\n"
  198. );
  199. #endif
  200. static int force_tt = 0;
  201. #if defined(CONFIG_MODE_TT) && defined(CONFIG_MODE_SKAS)
  202. #define DEFAULT_TT 0
  203. static int __init mode_tt_setup(char *line, int *add)
  204. {
  205. force_tt = 1;
  206. return 0;
  207. }
  208. #else
  209. #ifdef CONFIG_MODE_SKAS
  210. #define DEFAULT_TT 0
  211. static int __init mode_tt_setup(char *line, int *add)
  212. {
  213. printf("CONFIG_MODE_TT disabled - 'mode=tt' ignored\n");
  214. return 0;
  215. }
  216. #else
  217. #ifdef CONFIG_MODE_TT
  218. #define DEFAULT_TT 1
  219. static int __init mode_tt_setup(char *line, int *add)
  220. {
  221. printf("CONFIG_MODE_SKAS disabled - 'mode=tt' redundant\n");
  222. return 0;
  223. }
  224. #endif
  225. #endif
  226. #endif
  227. __uml_setup("mode=tt", mode_tt_setup,
  228. "mode=tt\n"
  229. " When both CONFIG_MODE_TT and CONFIG_MODE_SKAS are enabled, this option\n"
  230. " forces UML to run in tt (tracing thread) mode. It is not the default\n"
  231. " because it's slower and less secure than skas mode.\n\n"
  232. );
  233. int mode_tt = DEFAULT_TT;
  234. static int __init Usage(char *line, int *add)
  235. {
  236. const char **p;
  237. printf(usage_string, init_utsname()->release);
  238. p = &__uml_help_start;
  239. while (p < &__uml_help_end) {
  240. printf("%s", *p);
  241. p++;
  242. }
  243. exit(0);
  244. return 0;
  245. }
  246. __uml_setup("--help", Usage,
  247. "--help\n"
  248. " Prints this message.\n\n"
  249. );
  250. static int __init uml_checksetup(char *line, int *add)
  251. {
  252. struct uml_param *p;
  253. p = &__uml_setup_start;
  254. while(p < &__uml_setup_end) {
  255. int n;
  256. n = strlen(p->str);
  257. if(!strncmp(line, p->str, n)){
  258. if (p->setup_func(line + n, add)) return 1;
  259. }
  260. p++;
  261. }
  262. return 0;
  263. }
  264. static void __init uml_postsetup(void)
  265. {
  266. initcall_t *p;
  267. p = &__uml_postsetup_start;
  268. while(p < &__uml_postsetup_end){
  269. (*p)();
  270. p++;
  271. }
  272. return;
  273. }
  274. /* Set during early boot */
  275. unsigned long brk_start;
  276. unsigned long end_iomem;
  277. EXPORT_SYMBOL(end_iomem);
  278. #define MIN_VMALLOC (32 * 1024 * 1024)
  279. extern char __binary_start;
  280. int __init linux_main(int argc, char **argv)
  281. {
  282. unsigned long avail, diff;
  283. unsigned long virtmem_size, max_physmem;
  284. unsigned int i, add;
  285. char * mode;
  286. for (i = 1; i < argc; i++){
  287. if((i == 1) && (argv[i][0] == ' ')) continue;
  288. add = 1;
  289. uml_checksetup(argv[i], &add);
  290. if (add)
  291. add_arg(argv[i]);
  292. }
  293. if(have_root == 0)
  294. add_arg(DEFAULT_COMMAND_LINE);
  295. os_early_checks();
  296. if (force_tt)
  297. clear_can_do_skas();
  298. mode_tt = force_tt ? 1 : !can_do_skas();
  299. #ifndef CONFIG_MODE_TT
  300. if (mode_tt) {
  301. /*Since CONFIG_MODE_TT is #undef'ed, force_tt cannot be 1. So,
  302. * can_do_skas() returned 0, and the message is correct. */
  303. printf("Support for TT mode is disabled, and no SKAS support is present on the host.\n");
  304. exit(1);
  305. }
  306. #endif
  307. #ifndef CONFIG_MODE_SKAS
  308. mode = "TT";
  309. #else
  310. /* Show to the user the result of selection */
  311. if (mode_tt)
  312. mode = "TT";
  313. else if (proc_mm && ptrace_faultinfo)
  314. mode = "SKAS3";
  315. else
  316. mode = "SKAS0";
  317. #endif
  318. printf("UML running in %s mode\n", mode);
  319. host_task_size = CHOOSE_MODE_PROC(set_task_sizes_tt,
  320. set_task_sizes_skas, &task_size);
  321. /*
  322. * Setting up handlers to 'sig_info' struct
  323. */
  324. os_fill_handlinfo(handlinfo_kern);
  325. brk_start = (unsigned long) sbrk(0);
  326. CHOOSE_MODE_PROC(before_mem_tt, before_mem_skas, brk_start);
  327. /* Increase physical memory size for exec-shield users
  328. so they actually get what they asked for. This should
  329. add zero for non-exec shield users */
  330. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  331. if(diff > 1024 * 1024){
  332. printf("Adding %ld bytes to physical memory to account for "
  333. "exec-shield gap\n", diff);
  334. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  335. }
  336. uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
  337. /* Reserve up to 4M after the current brk */
  338. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  339. setup_machinename(init_utsname()->machine);
  340. #ifdef CONFIG_CMDLINE_ON_HOST
  341. argv1_begin = argv[1];
  342. argv1_end = &argv[1][strlen(argv[1])];
  343. #endif
  344. highmem = 0;
  345. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  346. max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;
  347. /* Zones have to begin on a 1 << MAX_ORDER page boundary,
  348. * so this makes sure that's true for highmem
  349. */
  350. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  351. if(physmem_size + iomem_size > max_physmem){
  352. highmem = physmem_size + iomem_size - max_physmem;
  353. physmem_size -= highmem;
  354. #ifndef CONFIG_HIGHMEM
  355. highmem = 0;
  356. printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
  357. "to %Lu bytes\n", physmem_size);
  358. #endif
  359. }
  360. high_physmem = uml_physmem + physmem_size;
  361. end_iomem = high_physmem + iomem_size;
  362. high_memory = (void *) end_iomem;
  363. start_vm = VMALLOC_START;
  364. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  365. if(init_maps(physmem_size, iomem_size, highmem)){
  366. printf("Failed to allocate mem_map for %Lu bytes of physical "
  367. "memory and %Lu bytes of highmem\n", physmem_size,
  368. highmem);
  369. exit(1);
  370. }
  371. virtmem_size = physmem_size;
  372. avail = get_kmem_end() - start_vm;
  373. if(physmem_size > avail) virtmem_size = avail;
  374. end_vm = start_vm + virtmem_size;
  375. if(virtmem_size < physmem_size)
  376. printf("Kernel virtual memory size shrunk to %lu bytes\n",
  377. virtmem_size);
  378. uml_postsetup();
  379. stack_protections((unsigned long) &init_thread_info);
  380. os_flush_stdout();
  381. return CHOOSE_MODE(start_uml_tt(), start_uml_skas());
  382. }
  383. extern int uml_exitcode;
  384. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  385. void *unused2)
  386. {
  387. bust_spinlocks(1);
  388. show_regs(&(current->thread.regs));
  389. bust_spinlocks(0);
  390. uml_exitcode = 1;
  391. os_dump_core();
  392. return 0;
  393. }
  394. static struct notifier_block panic_exit_notifier = {
  395. .notifier_call = panic_exit,
  396. .next = NULL,
  397. .priority = 0
  398. };
  399. void __init setup_arch(char **cmdline_p)
  400. {
  401. atomic_notifier_chain_register(&panic_notifier_list,
  402. &panic_exit_notifier);
  403. paging_init();
  404. strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  405. *cmdline_p = command_line;
  406. setup_hostinfo(host_info, sizeof host_info);
  407. }
  408. void __init check_bugs(void)
  409. {
  410. arch_check_bugs();
  411. os_check_bugs();
  412. }
  413. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  414. {
  415. }
  416. #ifdef CONFIG_SMP
  417. void alternatives_smp_module_add(struct module *mod, char *name,
  418. void *locks, void *locks_end,
  419. void *text, void *text_end)
  420. {
  421. }
  422. void alternatives_smp_module_del(struct module *mod)
  423. {
  424. }
  425. #endif