um_arch.c 10 KB

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