um_arch.c 10 KB

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