um_arch.c 11 KB

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