um_arch.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/string.h>
  11. #include <linux/utsname.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/task.h>
  14. #include <linux/kmsg_dump.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/processor.h>
  17. #include <asm/sections.h>
  18. #include <asm/setup.h>
  19. #include <as-layout.h>
  20. #include <arch.h>
  21. #include <init.h>
  22. #include <kern.h>
  23. #include <kern_util.h>
  24. #include <mem_user.h>
  25. #include <os.h>
  26. #define DEFAULT_COMMAND_LINE "root=98:0"
  27. /* Changed in add_arg and setup_arch, which run before SMP is started */
  28. static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
  29. static void __init add_arg(char *arg)
  30. {
  31. if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
  32. os_warn("add_arg: Too many command line arguments!\n");
  33. exit(1);
  34. }
  35. if (strlen(command_line) > 0)
  36. strcat(command_line, " ");
  37. strcat(command_line, arg);
  38. }
  39. /*
  40. * These fields are initialized at boot time and not changed.
  41. * XXX This structure is used only in the non-SMP case. Maybe this
  42. * should be moved to smp.c.
  43. */
  44. struct cpuinfo_um boot_cpu_data = {
  45. .loops_per_jiffy = 0,
  46. .ipi_pipe = { -1, -1 }
  47. };
  48. union thread_union cpu0_irqstack
  49. __attribute__((__section__(".data..init_irqstack"))) =
  50. { .thread_info = INIT_THREAD_INFO(init_task) };
  51. /* Changed in setup_arch, which is called in early boot */
  52. static char host_info[(__NEW_UTS_LEN + 1) * 5];
  53. static int show_cpuinfo(struct seq_file *m, void *v)
  54. {
  55. int index = 0;
  56. seq_printf(m, "processor\t: %d\n", index);
  57. seq_printf(m, "vendor_id\t: User Mode Linux\n");
  58. seq_printf(m, "model name\t: UML\n");
  59. seq_printf(m, "mode\t\t: skas\n");
  60. seq_printf(m, "host\t\t: %s\n", host_info);
  61. seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
  62. loops_per_jiffy/(500000/HZ),
  63. (loops_per_jiffy/(5000/HZ)) % 100);
  64. return 0;
  65. }
  66. static void *c_start(struct seq_file *m, loff_t *pos)
  67. {
  68. return *pos < NR_CPUS ? cpu_data + *pos : NULL;
  69. }
  70. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  71. {
  72. ++*pos;
  73. return c_start(m, pos);
  74. }
  75. static void c_stop(struct seq_file *m, void *v)
  76. {
  77. }
  78. const struct seq_operations cpuinfo_op = {
  79. .start = c_start,
  80. .next = c_next,
  81. .stop = c_stop,
  82. .show = show_cpuinfo,
  83. };
  84. /* Set in linux_main */
  85. unsigned long uml_physmem;
  86. EXPORT_SYMBOL(uml_physmem);
  87. unsigned long uml_reserved; /* Also modified in mem_init */
  88. unsigned long start_vm;
  89. unsigned long end_vm;
  90. /* Set in uml_ncpus_setup */
  91. int ncpus = 1;
  92. /* Set in early boot */
  93. static int have_root __initdata = 0;
  94. /* Set in uml_mem_setup and modified in linux_main */
  95. long long physmem_size = 32 * 1024 * 1024;
  96. static const char *usage_string =
  97. "User Mode Linux v%s\n"
  98. " available at http://user-mode-linux.sourceforge.net/\n\n";
  99. static int __init uml_version_setup(char *line, int *add)
  100. {
  101. /* Explicitly use printf() to show version in stdout */
  102. printf("%s\n", init_utsname()->release);
  103. exit(0);
  104. return 0;
  105. }
  106. __uml_setup("--version", uml_version_setup,
  107. "--version\n"
  108. " Prints the version number of the kernel.\n\n"
  109. );
  110. static int __init uml_root_setup(char *line, int *add)
  111. {
  112. have_root = 1;
  113. return 0;
  114. }
  115. __uml_setup("root=", uml_root_setup,
  116. "root=<file containing the root fs>\n"
  117. " This is actually used by the generic kernel in exactly the same\n"
  118. " way as in any other kernel. If you configure a number of block\n"
  119. " devices and want to boot off something other than ubd0, you \n"
  120. " would use something like:\n"
  121. " root=/dev/ubd5\n\n"
  122. );
  123. static int __init no_skas_debug_setup(char *line, int *add)
  124. {
  125. os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
  126. os_warn("'gdb linux'\n");
  127. return 0;
  128. }
  129. __uml_setup("debug", no_skas_debug_setup,
  130. "debug\n"
  131. " this flag is not needed to run gdb on UML in skas mode\n\n"
  132. );
  133. static int __init Usage(char *line, int *add)
  134. {
  135. const char **p;
  136. printf(usage_string, init_utsname()->release);
  137. p = &__uml_help_start;
  138. /* Explicitly use printf() to show help in stdout */
  139. while (p < &__uml_help_end) {
  140. printf("%s", *p);
  141. p++;
  142. }
  143. exit(0);
  144. return 0;
  145. }
  146. __uml_setup("--help", Usage,
  147. "--help\n"
  148. " Prints this message.\n\n"
  149. );
  150. static void __init uml_checksetup(char *line, int *add)
  151. {
  152. struct uml_param *p;
  153. p = &__uml_setup_start;
  154. while (p < &__uml_setup_end) {
  155. size_t n;
  156. n = strlen(p->str);
  157. if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
  158. return;
  159. p++;
  160. }
  161. }
  162. static void __init uml_postsetup(void)
  163. {
  164. initcall_t *p;
  165. p = &__uml_postsetup_start;
  166. while (p < &__uml_postsetup_end) {
  167. (*p)();
  168. p++;
  169. }
  170. return;
  171. }
  172. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  173. void *unused2)
  174. {
  175. kmsg_dump(KMSG_DUMP_PANIC);
  176. bust_spinlocks(1);
  177. bust_spinlocks(0);
  178. uml_exitcode = 1;
  179. os_dump_core();
  180. return 0;
  181. }
  182. static struct notifier_block panic_exit_notifier = {
  183. .notifier_call = panic_exit,
  184. .next = NULL,
  185. .priority = 0
  186. };
  187. void uml_finishsetup(void)
  188. {
  189. atomic_notifier_chain_register(&panic_notifier_list,
  190. &panic_exit_notifier);
  191. uml_postsetup();
  192. new_thread_handler();
  193. }
  194. /* Set during early boot */
  195. unsigned long task_size;
  196. EXPORT_SYMBOL(task_size);
  197. unsigned long host_task_size;
  198. unsigned long brk_start;
  199. unsigned long end_iomem;
  200. EXPORT_SYMBOL(end_iomem);
  201. #define MIN_VMALLOC (32 * 1024 * 1024)
  202. int __init linux_main(int argc, char **argv)
  203. {
  204. unsigned long avail, diff;
  205. unsigned long virtmem_size, max_physmem;
  206. unsigned long stack;
  207. unsigned int i;
  208. int add;
  209. for (i = 1; i < argc; i++) {
  210. if ((i == 1) && (argv[i][0] == ' '))
  211. continue;
  212. add = 1;
  213. uml_checksetup(argv[i], &add);
  214. if (add)
  215. add_arg(argv[i]);
  216. }
  217. if (have_root == 0)
  218. add_arg(DEFAULT_COMMAND_LINE);
  219. host_task_size = os_get_top_address();
  220. /*
  221. * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
  222. * out
  223. */
  224. task_size = host_task_size & PGDIR_MASK;
  225. /* OS sanity checks that need to happen before the kernel runs */
  226. os_early_checks();
  227. brk_start = (unsigned long) sbrk(0);
  228. /*
  229. * Increase physical memory size for exec-shield users
  230. * so they actually get what they asked for. This should
  231. * add zero for non-exec shield users
  232. */
  233. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  234. if (diff > 1024 * 1024) {
  235. os_info("Adding %ld bytes to physical memory to account for "
  236. "exec-shield gap\n", diff);
  237. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  238. }
  239. uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
  240. /* Reserve up to 4M after the current brk */
  241. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  242. setup_machinename(init_utsname()->machine);
  243. highmem = 0;
  244. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  245. max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
  246. /*
  247. * Zones have to begin on a 1 << MAX_ORDER page boundary,
  248. * so this makes sure that's true for highmem
  249. */
  250. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  251. if (physmem_size + iomem_size > max_physmem) {
  252. highmem = physmem_size + iomem_size - max_physmem;
  253. physmem_size -= highmem;
  254. }
  255. high_physmem = uml_physmem + physmem_size;
  256. end_iomem = high_physmem + iomem_size;
  257. high_memory = (void *) end_iomem;
  258. start_vm = VMALLOC_START;
  259. virtmem_size = physmem_size;
  260. stack = (unsigned long) argv;
  261. stack &= ~(1024 * 1024 - 1);
  262. avail = stack - start_vm;
  263. if (physmem_size > avail)
  264. virtmem_size = avail;
  265. end_vm = start_vm + virtmem_size;
  266. if (virtmem_size < physmem_size)
  267. os_info("Kernel virtual memory size shrunk to %lu bytes\n",
  268. virtmem_size);
  269. os_flush_stdout();
  270. return start_uml();
  271. }
  272. int __init __weak read_initrd(void)
  273. {
  274. return 0;
  275. }
  276. void __init setup_arch(char **cmdline_p)
  277. {
  278. stack_protections((unsigned long) &init_thread_info);
  279. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  280. mem_total_pages(physmem_size, iomem_size, highmem);
  281. read_initrd();
  282. paging_init();
  283. strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  284. *cmdline_p = command_line;
  285. setup_hostinfo(host_info, sizeof host_info);
  286. }
  287. void __init check_bugs(void)
  288. {
  289. arch_check_bugs();
  290. os_check_bugs();
  291. }
  292. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  293. {
  294. }