um_arch.c 11 KB

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