um_arch.c 8.9 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/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. EXPORT_SYMBOL(uml_physmem);
  90. unsigned long uml_reserved; /* Also modified in mem_init */
  91. unsigned long start_vm;
  92. unsigned long end_vm;
  93. /* Set in uml_ncpus_setup */
  94. int ncpus = 1;
  95. /* Set in early boot */
  96. static int have_root __initdata = 0;
  97. /* Set in uml_mem_setup and modified in linux_main */
  98. long long physmem_size = 32 * 1024 * 1024;
  99. static const char *usage_string =
  100. "User Mode Linux v%s\n"
  101. " available at http://user-mode-linux.sourceforge.net/\n\n";
  102. static int __init uml_version_setup(char *line, int *add)
  103. {
  104. printf("%s\n", init_utsname()->release);
  105. exit(0);
  106. return 0;
  107. }
  108. __uml_setup("--version", uml_version_setup,
  109. "--version\n"
  110. " Prints the version number of the kernel.\n\n"
  111. );
  112. static int __init uml_root_setup(char *line, int *add)
  113. {
  114. have_root = 1;
  115. return 0;
  116. }
  117. __uml_setup("root=", uml_root_setup,
  118. "root=<file containing the root fs>\n"
  119. " This is actually used by the generic kernel in exactly the same\n"
  120. " way as in any other kernel. If you configure a number of block\n"
  121. " devices and want to boot off something other than ubd0, you \n"
  122. " would use something like:\n"
  123. " root=/dev/ubd5\n\n"
  124. );
  125. static int __init no_skas_debug_setup(char *line, int *add)
  126. {
  127. printf("'debug' is not necessary to gdb UML in skas mode - run \n");
  128. printf("'gdb linux'\n");
  129. return 0;
  130. }
  131. __uml_setup("debug", no_skas_debug_setup,
  132. "debug\n"
  133. " this flag is not needed to run gdb on UML in skas mode\n\n"
  134. );
  135. #ifdef CONFIG_SMP
  136. static int __init uml_ncpus_setup(char *line, int *add)
  137. {
  138. if (!sscanf(line, "%d", &ncpus)) {
  139. printf("Couldn't parse [%s]\n", line);
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. __uml_setup("ncpus=", uml_ncpus_setup,
  145. "ncpus=<# of desired CPUs>\n"
  146. " This tells an SMP kernel how many virtual processors to start.\n\n"
  147. );
  148. #endif
  149. static int __init Usage(char *line, int *add)
  150. {
  151. const char **p;
  152. printf(usage_string, init_utsname()->release);
  153. p = &__uml_help_start;
  154. while (p < &__uml_help_end) {
  155. printf("%s", *p);
  156. p++;
  157. }
  158. exit(0);
  159. return 0;
  160. }
  161. __uml_setup("--help", Usage,
  162. "--help\n"
  163. " Prints this message.\n\n"
  164. );
  165. static void __init uml_checksetup(char *line, int *add)
  166. {
  167. struct uml_param *p;
  168. p = &__uml_setup_start;
  169. while (p < &__uml_setup_end) {
  170. size_t n;
  171. n = strlen(p->str);
  172. if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
  173. return;
  174. p++;
  175. }
  176. }
  177. static void __init uml_postsetup(void)
  178. {
  179. initcall_t *p;
  180. p = &__uml_postsetup_start;
  181. while (p < &__uml_postsetup_end) {
  182. (*p)();
  183. p++;
  184. }
  185. return;
  186. }
  187. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  188. void *unused2)
  189. {
  190. bust_spinlocks(1);
  191. show_regs(&(current->thread.regs));
  192. bust_spinlocks(0);
  193. uml_exitcode = 1;
  194. os_dump_core();
  195. return 0;
  196. }
  197. static struct notifier_block panic_exit_notifier = {
  198. .notifier_call = panic_exit,
  199. .next = NULL,
  200. .priority = 0
  201. };
  202. /* Set during early boot */
  203. unsigned long task_size;
  204. EXPORT_SYMBOL(task_size);
  205. unsigned long host_task_size;
  206. unsigned long brk_start;
  207. unsigned long end_iomem;
  208. EXPORT_SYMBOL(end_iomem);
  209. #define MIN_VMALLOC (32 * 1024 * 1024)
  210. extern char __binary_start;
  211. int __init linux_main(int argc, char **argv)
  212. {
  213. unsigned long avail, diff;
  214. unsigned long virtmem_size, max_physmem;
  215. unsigned long stack;
  216. unsigned int i;
  217. int add;
  218. char * mode;
  219. for (i = 1; i < argc; i++) {
  220. if ((i == 1) && (argv[i][0] == ' '))
  221. continue;
  222. add = 1;
  223. uml_checksetup(argv[i], &add);
  224. if (add)
  225. add_arg(argv[i]);
  226. }
  227. if (have_root == 0)
  228. add_arg(DEFAULT_COMMAND_LINE);
  229. host_task_size = os_get_top_address();
  230. /*
  231. * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
  232. * out
  233. */
  234. task_size = host_task_size & PGDIR_MASK;
  235. /* OS sanity checks that need to happen before the kernel runs */
  236. os_early_checks();
  237. can_do_skas();
  238. if (proc_mm && ptrace_faultinfo)
  239. mode = "SKAS3";
  240. else
  241. mode = "SKAS0";
  242. printf("UML running in %s mode\n", mode);
  243. brk_start = (unsigned long) sbrk(0);
  244. /*
  245. * Increase physical memory size for exec-shield users
  246. * so they actually get what they asked for. This should
  247. * add zero for non-exec shield users
  248. */
  249. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  250. if (diff > 1024 * 1024) {
  251. printf("Adding %ld bytes to physical memory to account for "
  252. "exec-shield gap\n", diff);
  253. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  254. }
  255. uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
  256. /* Reserve up to 4M after the current brk */
  257. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  258. setup_machinename(init_utsname()->machine);
  259. highmem = 0;
  260. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  261. max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
  262. /*
  263. * Zones have to begin on a 1 << MAX_ORDER page boundary,
  264. * so this makes sure that's true for highmem
  265. */
  266. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  267. if (physmem_size + iomem_size > max_physmem) {
  268. highmem = physmem_size + iomem_size - max_physmem;
  269. physmem_size -= highmem;
  270. #ifndef CONFIG_HIGHMEM
  271. highmem = 0;
  272. printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
  273. "to %Lu bytes\n", physmem_size);
  274. #endif
  275. }
  276. high_physmem = uml_physmem + physmem_size;
  277. end_iomem = high_physmem + iomem_size;
  278. high_memory = (void *) end_iomem;
  279. start_vm = VMALLOC_START;
  280. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  281. if (init_maps(physmem_size, iomem_size, highmem)) {
  282. printf("Failed to allocate mem_map for %Lu bytes of physical "
  283. "memory and %Lu bytes of highmem\n", physmem_size,
  284. highmem);
  285. exit(1);
  286. }
  287. virtmem_size = physmem_size;
  288. stack = (unsigned long) argv;
  289. stack &= ~(1024 * 1024 - 1);
  290. avail = stack - start_vm;
  291. if (physmem_size > avail)
  292. virtmem_size = avail;
  293. end_vm = start_vm + virtmem_size;
  294. if (virtmem_size < physmem_size)
  295. printf("Kernel virtual memory size shrunk to %lu bytes\n",
  296. virtmem_size);
  297. atomic_notifier_chain_register(&panic_notifier_list,
  298. &panic_exit_notifier);
  299. uml_postsetup();
  300. stack_protections((unsigned long) &init_thread_info);
  301. os_flush_stdout();
  302. return start_uml();
  303. }
  304. void __init setup_arch(char **cmdline_p)
  305. {
  306. paging_init();
  307. strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  308. *cmdline_p = command_line;
  309. setup_hostinfo(host_info, sizeof host_info);
  310. }
  311. void __init check_bugs(void)
  312. {
  313. arch_check_bugs();
  314. os_check_bugs();
  315. }
  316. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  317. {
  318. }
  319. #ifdef CONFIG_SMP
  320. void alternatives_smp_module_add(struct module *mod, char *name,
  321. void *locks, void *locks_end,
  322. void *text, void *text_end)
  323. {
  324. }
  325. void alternatives_smp_module_del(struct module *mod)
  326. {
  327. }
  328. #endif