um_arch.c 8.0 KB

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