um_arch.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "mem_user.h"
  19. #include "os.h"
  20. #include "skas.h"
  21. #define DEFAULT_COMMAND_LINE "root=98:0"
  22. /* Changed in add_arg and setup_arch, which run before SMP is started */
  23. static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
  24. static void __init add_arg(char *arg)
  25. {
  26. if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
  27. printf("add_arg: Too many command line arguments!\n");
  28. exit(1);
  29. }
  30. if (strlen(command_line) > 0)
  31. strcat(command_line, " ");
  32. strcat(command_line, arg);
  33. }
  34. /*
  35. * These fields are initialized at boot time and not changed.
  36. * XXX This structure is used only in the non-SMP case. Maybe this
  37. * should be moved to smp.c.
  38. */
  39. struct cpuinfo_um boot_cpu_data = {
  40. .loops_per_jiffy = 0,
  41. .ipi_pipe = { -1, -1 }
  42. };
  43. unsigned long thread_saved_pc(struct task_struct *task)
  44. {
  45. /* FIXME: Need to look up userspace_pid by cpu */
  46. return os_process_pc(userspace_pid[0]);
  47. }
  48. /* Changed in setup_arch, which is called in early boot */
  49. static char host_info[(__NEW_UTS_LEN + 1) * 5];
  50. static int show_cpuinfo(struct seq_file *m, void *v)
  51. {
  52. int index = 0;
  53. #ifdef CONFIG_SMP
  54. index = (struct cpuinfo_um *) v - cpu_data;
  55. if (!cpu_online(index))
  56. return 0;
  57. #endif
  58. seq_printf(m, "processor\t: %d\n", index);
  59. seq_printf(m, "vendor_id\t: User Mode Linux\n");
  60. seq_printf(m, "model name\t: UML\n");
  61. seq_printf(m, "mode\t\t: skas\n");
  62. seq_printf(m, "host\t\t: %s\n", host_info);
  63. seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
  64. loops_per_jiffy/(500000/HZ),
  65. (loops_per_jiffy/(5000/HZ)) % 100);
  66. return 0;
  67. }
  68. static void *c_start(struct seq_file *m, loff_t *pos)
  69. {
  70. return *pos < NR_CPUS ? cpu_data + *pos : NULL;
  71. }
  72. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  73. {
  74. ++*pos;
  75. return c_start(m, pos);
  76. }
  77. static void c_stop(struct seq_file *m, void *v)
  78. {
  79. }
  80. const struct seq_operations cpuinfo_op = {
  81. .start = c_start,
  82. .next = c_next,
  83. .stop = c_stop,
  84. .show = show_cpuinfo,
  85. };
  86. /* Set in linux_main */
  87. unsigned long host_task_size;
  88. unsigned long task_size;
  89. unsigned long 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 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'");
  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 int __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. int n;
  171. n = strlen(p->str);
  172. if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
  173. return 1;
  174. p++;
  175. }
  176. return 0;
  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. /*
  231. * Setting up handlers to 'sig_info' struct
  232. */
  233. os_fill_handlinfo(handlinfo_kern);
  234. brk_start = (unsigned long) sbrk(0);
  235. /*
  236. * Increase physical memory size for exec-shield users
  237. * so they actually get what they asked for. This should
  238. * add zero for non-exec shield users
  239. */
  240. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  241. if (diff > 1024 * 1024) {
  242. printf("Adding %ld bytes to physical memory to account for "
  243. "exec-shield gap\n", diff);
  244. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  245. }
  246. uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
  247. /* Reserve up to 4M after the current brk */
  248. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  249. setup_machinename(init_utsname()->machine);
  250. highmem = 0;
  251. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  252. max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;
  253. /*
  254. * Zones have to begin on a 1 << MAX_ORDER page boundary,
  255. * so this makes sure that's true for highmem
  256. */
  257. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  258. if (physmem_size + iomem_size > max_physmem) {
  259. highmem = physmem_size + iomem_size - max_physmem;
  260. physmem_size -= highmem;
  261. #ifndef CONFIG_HIGHMEM
  262. highmem = 0;
  263. printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
  264. "to %Lu bytes\n", physmem_size);
  265. #endif
  266. }
  267. high_physmem = uml_physmem + physmem_size;
  268. end_iomem = high_physmem + iomem_size;
  269. high_memory = (void *) end_iomem;
  270. start_vm = VMALLOC_START;
  271. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  272. if (init_maps(physmem_size, iomem_size, highmem)) {
  273. printf("Failed to allocate mem_map for %Lu bytes of physical "
  274. "memory and %Lu bytes of highmem\n", physmem_size,
  275. highmem);
  276. exit(1);
  277. }
  278. virtmem_size = physmem_size;
  279. avail = get_kmem_end() - start_vm;
  280. if (physmem_size > avail)
  281. virtmem_size = avail;
  282. end_vm = start_vm + virtmem_size;
  283. if (virtmem_size < physmem_size)
  284. printf("Kernel virtual memory size shrunk to %lu bytes\n",
  285. virtmem_size);
  286. uml_postsetup();
  287. stack_protections((unsigned long) &init_thread_info);
  288. os_flush_stdout();
  289. return start_uml();
  290. }
  291. extern int uml_exitcode;
  292. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  293. void *unused2)
  294. {
  295. bust_spinlocks(1);
  296. show_regs(&(current->thread.regs));
  297. bust_spinlocks(0);
  298. uml_exitcode = 1;
  299. os_dump_core();
  300. return 0;
  301. }
  302. static struct notifier_block panic_exit_notifier = {
  303. .notifier_call = panic_exit,
  304. .next = NULL,
  305. .priority = 0
  306. };
  307. void __init setup_arch(char **cmdline_p)
  308. {
  309. atomic_notifier_chain_register(&panic_notifier_list,
  310. &panic_exit_notifier);
  311. paging_init();
  312. strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  313. *cmdline_p = command_line;
  314. setup_hostinfo(host_info, sizeof host_info);
  315. }
  316. void __init check_bugs(void)
  317. {
  318. arch_check_bugs();
  319. os_check_bugs();
  320. }
  321. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  322. {
  323. }
  324. #ifdef CONFIG_SMP
  325. void alternatives_smp_module_add(struct module *mod, char *name,
  326. void *locks, void *locks_end,
  327. void *text, void *text_end)
  328. {
  329. }
  330. void alternatives_smp_module_del(struct module *mod)
  331. {
  332. }
  333. #endif