um_arch.c 9.3 KB

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