um_arch.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/mm.h"
  7. #include "linux/module.h"
  8. #include "linux/seq_file.h"
  9. #include "linux/string.h"
  10. #include "linux/utsname.h"
  11. #include "asm/pgtable.h"
  12. #include "asm/processor.h"
  13. #include "asm/setup.h"
  14. #include "arch.h"
  15. #include "as-layout.h"
  16. #include "init.h"
  17. #include "kern.h"
  18. #include "kern_util.h"
  19. #include "mem_user.h"
  20. #include "os.h"
  21. #include "skas.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 host_task_size;
  89. unsigned long task_size;
  90. unsigned long 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 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'");
  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. #ifdef CONFIG_SMP
  137. static int __init uml_ncpus_setup(char *line, int *add)
  138. {
  139. if (!sscanf(line, "%d", &ncpus)) {
  140. printf("Couldn't parse [%s]\n", line);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. __uml_setup("ncpus=", uml_ncpus_setup,
  146. "ncpus=<# of desired CPUs>\n"
  147. " This tells an SMP kernel how many virtual processors to start.\n\n"
  148. );
  149. #endif
  150. static int __init Usage(char *line, int *add)
  151. {
  152. const char **p;
  153. printf(usage_string, init_utsname()->release);
  154. p = &__uml_help_start;
  155. while (p < &__uml_help_end) {
  156. printf("%s", *p);
  157. p++;
  158. }
  159. exit(0);
  160. return 0;
  161. }
  162. __uml_setup("--help", Usage,
  163. "--help\n"
  164. " Prints this message.\n\n"
  165. );
  166. static void __init uml_checksetup(char *line, int *add)
  167. {
  168. struct uml_param *p;
  169. p = &__uml_setup_start;
  170. while(p < &__uml_setup_end) {
  171. int n;
  172. n = strlen(p->str);
  173. if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
  174. return;
  175. p++;
  176. }
  177. }
  178. static void __init uml_postsetup(void)
  179. {
  180. initcall_t *p;
  181. p = &__uml_postsetup_start;
  182. while(p < &__uml_postsetup_end) {
  183. (*p)();
  184. p++;
  185. }
  186. return;
  187. }
  188. /* Set during early boot */
  189. unsigned long brk_start;
  190. unsigned long end_iomem;
  191. EXPORT_SYMBOL(end_iomem);
  192. #define MIN_VMALLOC (32 * 1024 * 1024)
  193. extern char __binary_start;
  194. static unsigned long set_task_sizes_skas(unsigned long *task_size_out)
  195. {
  196. /* Round up to the nearest 4M */
  197. unsigned long host_task_size = ROUND_4M((unsigned long)
  198. &host_task_size);
  199. if (!skas_needs_stub)
  200. *task_size_out = host_task_size;
  201. else
  202. *task_size_out = STUB_START & PGDIR_MASK;
  203. return host_task_size;
  204. }
  205. int __init linux_main(int argc, char **argv)
  206. {
  207. unsigned long avail, diff;
  208. unsigned long virtmem_size, max_physmem;
  209. unsigned int i, add;
  210. char * mode;
  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. /* OS sanity checks that need to happen before the kernel runs */
  222. os_early_checks();
  223. can_do_skas();
  224. if (proc_mm && ptrace_faultinfo)
  225. mode = "SKAS3";
  226. else
  227. mode = "SKAS0";
  228. printf("UML running in %s mode\n", mode);
  229. host_task_size = set_task_sizes_skas(&task_size);
  230. brk_start = (unsigned long) sbrk(0);
  231. /*
  232. * Increase physical memory size for exec-shield users
  233. * so they actually get what they asked for. This should
  234. * add zero for non-exec shield users
  235. */
  236. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  237. if (diff > 1024 * 1024) {
  238. printf("Adding %ld bytes to physical memory to account for "
  239. "exec-shield gap\n", diff);
  240. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  241. }
  242. uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
  243. /* Reserve up to 4M after the current brk */
  244. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  245. setup_machinename(init_utsname()->machine);
  246. highmem = 0;
  247. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  248. max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;
  249. /*
  250. * Zones have to begin on a 1 << MAX_ORDER page boundary,
  251. * so this makes sure that's true for highmem
  252. */
  253. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  254. if (physmem_size + iomem_size > max_physmem) {
  255. highmem = physmem_size + iomem_size - max_physmem;
  256. physmem_size -= highmem;
  257. #ifndef CONFIG_HIGHMEM
  258. highmem = 0;
  259. printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
  260. "to %Lu bytes\n", physmem_size);
  261. #endif
  262. }
  263. high_physmem = uml_physmem + physmem_size;
  264. end_iomem = high_physmem + iomem_size;
  265. high_memory = (void *) end_iomem;
  266. start_vm = VMALLOC_START;
  267. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  268. if (init_maps(physmem_size, iomem_size, highmem)) {
  269. printf("Failed to allocate mem_map for %Lu bytes of physical "
  270. "memory and %Lu bytes of highmem\n", physmem_size,
  271. highmem);
  272. exit(1);
  273. }
  274. virtmem_size = physmem_size;
  275. avail = get_kmem_end() - start_vm;
  276. if (physmem_size > avail)
  277. virtmem_size = avail;
  278. end_vm = start_vm + virtmem_size;
  279. if (virtmem_size < physmem_size)
  280. printf("Kernel virtual memory size shrunk to %lu bytes\n",
  281. virtmem_size);
  282. uml_postsetup();
  283. stack_protections((unsigned long) &init_thread_info);
  284. os_flush_stdout();
  285. return start_uml();
  286. }
  287. extern int uml_exitcode;
  288. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  289. void *unused2)
  290. {
  291. bust_spinlocks(1);
  292. show_regs(&(current->thread.regs));
  293. bust_spinlocks(0);
  294. uml_exitcode = 1;
  295. os_dump_core();
  296. return 0;
  297. }
  298. static struct notifier_block panic_exit_notifier = {
  299. .notifier_call = panic_exit,
  300. .next = NULL,
  301. .priority = 0
  302. };
  303. void __init setup_arch(char **cmdline_p)
  304. {
  305. atomic_notifier_chain_register(&panic_notifier_list,
  306. &panic_exit_notifier);
  307. paging_init();
  308. strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  309. *cmdline_p = command_line;
  310. setup_hostinfo(host_info, sizeof host_info);
  311. }
  312. void __init check_bugs(void)
  313. {
  314. arch_check_bugs();
  315. os_check_bugs();
  316. }
  317. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  318. {
  319. }
  320. #ifdef CONFIG_SMP
  321. void alternatives_smp_module_add(struct module *mod, char *name,
  322. void *locks, void *locks_end,
  323. void *text, void *text_end)
  324. {
  325. }
  326. void alternatives_smp_module_del(struct module *mod)
  327. {
  328. }
  329. #endif