um_arch.c 9.0 KB

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