um_arch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/config.h"
  6. #include "linux/kernel.h"
  7. #include "linux/sched.h"
  8. #include "linux/notifier.h"
  9. #include "linux/mm.h"
  10. #include "linux/types.h"
  11. #include "linux/tty.h"
  12. #include "linux/init.h"
  13. #include "linux/bootmem.h"
  14. #include "linux/spinlock.h"
  15. #include "linux/utsname.h"
  16. #include "linux/sysrq.h"
  17. #include "linux/seq_file.h"
  18. #include "linux/delay.h"
  19. #include "linux/module.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 "user_util.h"
  29. #include "kern_util.h"
  30. #include "kern.h"
  31. #include "mem_user.h"
  32. #include "mem.h"
  33. #include "umid.h"
  34. #include "initrd.h"
  35. #include "init.h"
  36. #include "os.h"
  37. #include "choose-mode.h"
  38. #include "mode_kern.h"
  39. #include "mode.h"
  40. #ifdef UML_CONFIG_MODE_SKAS
  41. #include "skas.h"
  42. #endif
  43. #define DEFAULT_COMMAND_LINE "root=98:0"
  44. /* Changed in linux_main and setup_arch, which run before SMP is started */
  45. static char command_line[COMMAND_LINE_SIZE] = { 0 };
  46. static void add_arg(char *arg)
  47. {
  48. if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
  49. printf("add_arg: Too many command line arguments!\n");
  50. exit(1);
  51. }
  52. if(strlen(command_line) > 0)
  53. strcat(command_line, " ");
  54. strcat(command_line, arg);
  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. return(os_process_pc(CHOOSE_MODE_PROC(thread_pid_tt, thread_pid_skas,
  63. task)));
  64. }
  65. static int show_cpuinfo(struct seq_file *m, void *v)
  66. {
  67. int index = 0;
  68. #ifdef CONFIG_SMP
  69. index = (struct cpuinfo_um *) v - cpu_data;
  70. if (!cpu_online(index))
  71. return 0;
  72. #endif
  73. seq_printf(m, "processor\t: %d\n", index);
  74. seq_printf(m, "vendor_id\t: User Mode Linux\n");
  75. seq_printf(m, "model name\t: UML\n");
  76. seq_printf(m, "mode\t\t: %s\n", CHOOSE_MODE("tt", "skas"));
  77. seq_printf(m, "host\t\t: %s\n", host_info);
  78. seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
  79. loops_per_jiffy/(500000/HZ),
  80. (loops_per_jiffy/(5000/HZ)) % 100);
  81. return(0);
  82. }
  83. static void *c_start(struct seq_file *m, loff_t *pos)
  84. {
  85. return *pos < NR_CPUS ? cpu_data + *pos : NULL;
  86. }
  87. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  88. {
  89. ++*pos;
  90. return c_start(m, pos);
  91. }
  92. static void c_stop(struct seq_file *m, void *v)
  93. {
  94. }
  95. struct seq_operations cpuinfo_op = {
  96. .start = c_start,
  97. .next = c_next,
  98. .stop = c_stop,
  99. .show = show_cpuinfo,
  100. };
  101. /* Set in linux_main */
  102. unsigned long host_task_size;
  103. unsigned long task_size;
  104. unsigned long uml_start;
  105. /* Set in early boot */
  106. unsigned long uml_physmem;
  107. unsigned long uml_reserved;
  108. unsigned long start_vm;
  109. unsigned long end_vm;
  110. int ncpus = 1;
  111. #ifdef CONFIG_CMDLINE_ON_HOST
  112. /* Pointer set in linux_main, the array itself is private to each thread,
  113. * and changed at address space creation time so this poses no concurrency
  114. * problems.
  115. */
  116. static char *argv1_begin = NULL;
  117. static char *argv1_end = NULL;
  118. #endif
  119. /* Set in early boot */
  120. static int have_root __initdata = 0;
  121. long physmem_size = 32 * 1024 * 1024;
  122. void set_cmdline(char *cmd)
  123. {
  124. #ifdef CONFIG_CMDLINE_ON_HOST
  125. char *umid, *ptr;
  126. if(CHOOSE_MODE(honeypot, 0)) return;
  127. umid = get_umid(1);
  128. if(umid != NULL){
  129. snprintf(argv1_begin,
  130. (argv1_end - argv1_begin) * sizeof(*ptr),
  131. "(%s) ", umid);
  132. ptr = &argv1_begin[strlen(argv1_begin)];
  133. }
  134. else ptr = argv1_begin;
  135. snprintf(ptr, (argv1_end - ptr) * sizeof(*ptr), "[%s]", cmd);
  136. memset(argv1_begin + strlen(argv1_begin), '\0',
  137. argv1_end - argv1_begin - strlen(argv1_begin));
  138. #endif
  139. }
  140. static char *usage_string =
  141. "User Mode Linux v%s\n"
  142. " available at http://user-mode-linux.sourceforge.net/\n\n";
  143. static int __init uml_version_setup(char *line, int *add)
  144. {
  145. printf("%s\n", system_utsname.release);
  146. exit(0);
  147. return 0;
  148. }
  149. __uml_setup("--version", uml_version_setup,
  150. "--version\n"
  151. " Prints the version number of the kernel.\n\n"
  152. );
  153. static int __init uml_root_setup(char *line, int *add)
  154. {
  155. have_root = 1;
  156. return 0;
  157. }
  158. __uml_setup("root=", uml_root_setup,
  159. "root=<file containing the root fs>\n"
  160. " This is actually used by the generic kernel in exactly the same\n"
  161. " way as in any other kernel. If you configure a number of block\n"
  162. " devices and want to boot off something other than ubd0, you \n"
  163. " would use something like:\n"
  164. " root=/dev/ubd5\n\n"
  165. );
  166. #ifdef CONFIG_SMP
  167. static int __init uml_ncpus_setup(char *line, int *add)
  168. {
  169. if (!sscanf(line, "%d", &ncpus)) {
  170. printf("Couldn't parse [%s]\n", line);
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. __uml_setup("ncpus=", uml_ncpus_setup,
  176. "ncpus=<# of desired CPUs>\n"
  177. " This tells an SMP kernel how many virtual processors to start.\n\n"
  178. );
  179. #endif
  180. static int force_tt = 0;
  181. #if defined(CONFIG_MODE_TT) && defined(CONFIG_MODE_SKAS)
  182. #define DEFAULT_TT 0
  183. static int __init mode_tt_setup(char *line, int *add)
  184. {
  185. force_tt = 1;
  186. return(0);
  187. }
  188. #else
  189. #ifdef CONFIG_MODE_SKAS
  190. #define DEFAULT_TT 0
  191. static int __init mode_tt_setup(char *line, int *add)
  192. {
  193. printf("CONFIG_MODE_TT disabled - 'mode=tt' ignored\n");
  194. return(0);
  195. }
  196. #else
  197. #ifdef CONFIG_MODE_TT
  198. #define DEFAULT_TT 1
  199. static int __init mode_tt_setup(char *line, int *add)
  200. {
  201. printf("CONFIG_MODE_SKAS disabled - 'mode=tt' redundant\n");
  202. return(0);
  203. }
  204. #else
  205. #error Either CONFIG_MODE_TT or CONFIG_MODE_SKAS must be enabled
  206. #endif
  207. #endif
  208. #endif
  209. __uml_setup("mode=tt", mode_tt_setup,
  210. "mode=tt\n"
  211. " When both CONFIG_MODE_TT and CONFIG_MODE_SKAS are enabled, this option\n"
  212. " forces UML to run in tt (tracing thread) mode. It is not the default\n"
  213. " because it's slower and less secure than skas mode.\n\n"
  214. );
  215. int mode_tt = DEFAULT_TT;
  216. static int __init Usage(char *line, int *add)
  217. {
  218. const char **p;
  219. printf(usage_string, system_utsname.release);
  220. p = &__uml_help_start;
  221. while (p < &__uml_help_end) {
  222. printf("%s", *p);
  223. p++;
  224. }
  225. exit(0);
  226. return 0;
  227. }
  228. __uml_setup("--help", Usage,
  229. "--help\n"
  230. " Prints this message.\n\n"
  231. );
  232. static int __init uml_checksetup(char *line, int *add)
  233. {
  234. struct uml_param *p;
  235. p = &__uml_setup_start;
  236. while(p < &__uml_setup_end) {
  237. int n;
  238. n = strlen(p->str);
  239. if(!strncmp(line, p->str, n)){
  240. if (p->setup_func(line + n, add)) return 1;
  241. }
  242. p++;
  243. }
  244. return 0;
  245. }
  246. static void __init uml_postsetup(void)
  247. {
  248. initcall_t *p;
  249. p = &__uml_postsetup_start;
  250. while(p < &__uml_postsetup_end){
  251. (*p)();
  252. p++;
  253. }
  254. return;
  255. }
  256. /* Set during early boot */
  257. unsigned long brk_start;
  258. unsigned long end_iomem;
  259. EXPORT_SYMBOL(end_iomem);
  260. #define MIN_VMALLOC (32 * 1024 * 1024)
  261. int linux_main(int argc, char **argv)
  262. {
  263. unsigned long avail, diff;
  264. unsigned long virtmem_size, max_physmem;
  265. unsigned int i, add;
  266. char * mode;
  267. for (i = 1; i < argc; i++){
  268. if((i == 1) && (argv[i][0] == ' ')) continue;
  269. add = 1;
  270. uml_checksetup(argv[i], &add);
  271. if (add)
  272. add_arg(argv[i]);
  273. }
  274. if(have_root == 0)
  275. add_arg(DEFAULT_COMMAND_LINE);
  276. os_early_checks();
  277. mode_tt = force_tt ? 1 : !can_do_skas();
  278. #ifndef CONFIG_MODE_TT
  279. if (mode_tt) {
  280. /*Since CONFIG_MODE_TT is #undef'ed, force_tt cannot be 1. So,
  281. * can_do_skas() returned 0, and the message is correct. */
  282. printf("Support for TT mode is disabled, and no SKAS support is present on the host.\n");
  283. exit(1);
  284. }
  285. #endif
  286. #ifndef CONFIG_MODE_SKAS
  287. mode = "TT";
  288. #else
  289. /* Show to the user the result of selection */
  290. if (mode_tt)
  291. mode = "TT";
  292. else if (proc_mm && ptrace_faultinfo)
  293. mode = "SKAS3";
  294. else
  295. mode = "SKAS0";
  296. #endif
  297. printf("UML running in %s mode\n", mode);
  298. uml_start = CHOOSE_MODE_PROC(set_task_sizes_tt, set_task_sizes_skas, 0,
  299. &host_task_size, &task_size);
  300. /* Need to check this early because mmapping happens before the
  301. * kernel is running.
  302. */
  303. check_tmpexec();
  304. brk_start = (unsigned long) sbrk(0);
  305. CHOOSE_MODE_PROC(before_mem_tt, before_mem_skas, brk_start);
  306. /* Increase physical memory size for exec-shield users
  307. so they actually get what they asked for. This should
  308. add zero for non-exec shield users */
  309. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  310. if(diff > 1024 * 1024){
  311. printf("Adding %ld bytes to physical memory to account for "
  312. "exec-shield gap\n", diff);
  313. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  314. }
  315. uml_physmem = uml_start;
  316. /* Reserve up to 4M after the current brk */
  317. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  318. setup_machinename(system_utsname.machine);
  319. #ifdef CONFIG_CMDLINE_ON_HOST
  320. argv1_begin = argv[1];
  321. argv1_end = &argv[1][strlen(argv[1])];
  322. #endif
  323. highmem = 0;
  324. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  325. max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;
  326. /* Zones have to begin on a 1 << MAX_ORDER page boundary,
  327. * so this makes sure that's true for highmem
  328. */
  329. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
  330. if(physmem_size + iomem_size > max_physmem){
  331. highmem = physmem_size + iomem_size - max_physmem;
  332. physmem_size -= highmem;
  333. #ifndef CONFIG_HIGHMEM
  334. highmem = 0;
  335. printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
  336. "to %ld bytes\n", physmem_size);
  337. #endif
  338. }
  339. high_physmem = uml_physmem + physmem_size;
  340. end_iomem = high_physmem + iomem_size;
  341. high_memory = (void *) end_iomem;
  342. start_vm = VMALLOC_START;
  343. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  344. if(init_maps(physmem_size, iomem_size, highmem)){
  345. printf("Failed to allocate mem_map for %ld bytes of physical "
  346. "memory and %ld bytes of highmem\n", physmem_size,
  347. highmem);
  348. exit(1);
  349. }
  350. virtmem_size = physmem_size;
  351. avail = get_kmem_end() - start_vm;
  352. if(physmem_size > avail) virtmem_size = avail;
  353. end_vm = start_vm + virtmem_size;
  354. if(virtmem_size < physmem_size)
  355. printf("Kernel virtual memory size shrunk to %ld bytes\n",
  356. virtmem_size);
  357. uml_postsetup();
  358. task_protections((unsigned long) &init_thread_info);
  359. os_flush_stdout();
  360. return(CHOOSE_MODE(start_uml_tt(), start_uml_skas()));
  361. }
  362. extern int uml_exitcode;
  363. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  364. void *unused2)
  365. {
  366. bust_spinlocks(1);
  367. show_regs(&(current->thread.regs));
  368. bust_spinlocks(0);
  369. uml_exitcode = 1;
  370. machine_halt();
  371. return(0);
  372. }
  373. static struct notifier_block panic_exit_notifier = {
  374. .notifier_call = panic_exit,
  375. .next = NULL,
  376. .priority = 0
  377. };
  378. void __init setup_arch(char **cmdline_p)
  379. {
  380. notifier_chain_register(&panic_notifier_list, &panic_exit_notifier);
  381. paging_init();
  382. strlcpy(saved_command_line, command_line, COMMAND_LINE_SIZE);
  383. *cmdline_p = command_line;
  384. setup_hostinfo();
  385. }
  386. void __init check_bugs(void)
  387. {
  388. arch_check_bugs();
  389. check_sigio();
  390. check_devanon();
  391. }
  392. void apply_alternatives(void *start, void *end)
  393. {
  394. }