um_arch.c 8.8 KB

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