setup64.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * X86-64 specific CPU setup.
  3. * Copyright (C) 1995 Linus Torvalds
  4. * Copyright 2001, 2002, 2003 SuSE Labs / Andi Kleen.
  5. * See setup.c for older changelog.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/bitops.h>
  13. #include <linux/module.h>
  14. #include <linux/kgdb.h>
  15. #include <asm/pda.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/processor.h>
  18. #include <asm/desc.h>
  19. #include <asm/atomic.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/smp.h>
  22. #include <asm/i387.h>
  23. #include <asm/percpu.h>
  24. #include <asm/proto.h>
  25. #include <asm/sections.h>
  26. #include <asm/setup.h>
  27. #include <asm/genapic.h>
  28. #ifndef CONFIG_DEBUG_BOOT_PARAMS
  29. struct boot_params __initdata boot_params;
  30. #else
  31. struct boot_params boot_params;
  32. #endif
  33. cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
  34. struct x8664_pda *_cpu_pda[NR_CPUS] __read_mostly;
  35. EXPORT_SYMBOL(_cpu_pda);
  36. struct x8664_pda boot_cpu_pda[NR_CPUS] __cacheline_aligned;
  37. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  38. char boot_cpu_stack[IRQSTACKSIZE] __attribute__((section(".bss.page_aligned")));
  39. unsigned long __supported_pte_mask __read_mostly = ~0UL;
  40. EXPORT_SYMBOL_GPL(__supported_pte_mask);
  41. static int do_not_nx __cpuinitdata = 0;
  42. /* noexec=on|off
  43. Control non executable mappings for 64bit processes.
  44. on Enable(default)
  45. off Disable
  46. */
  47. static int __init nonx_setup(char *str)
  48. {
  49. if (!str)
  50. return -EINVAL;
  51. if (!strncmp(str, "on", 2)) {
  52. __supported_pte_mask |= _PAGE_NX;
  53. do_not_nx = 0;
  54. } else if (!strncmp(str, "off", 3)) {
  55. do_not_nx = 1;
  56. __supported_pte_mask &= ~_PAGE_NX;
  57. }
  58. return 0;
  59. }
  60. early_param("noexec", nonx_setup);
  61. int force_personality32 = 0;
  62. /* noexec32=on|off
  63. Control non executable heap for 32bit processes.
  64. To control the stack too use noexec=off
  65. on PROT_READ does not imply PROT_EXEC for 32bit processes (default)
  66. off PROT_READ implies PROT_EXEC
  67. */
  68. static int __init nonx32_setup(char *str)
  69. {
  70. if (!strcmp(str, "on"))
  71. force_personality32 &= ~READ_IMPLIES_EXEC;
  72. else if (!strcmp(str, "off"))
  73. force_personality32 |= READ_IMPLIES_EXEC;
  74. return 1;
  75. }
  76. __setup("noexec32=", nonx32_setup);
  77. void pda_init(int cpu)
  78. {
  79. struct x8664_pda *pda = cpu_pda(cpu);
  80. /* Setup up data that may be needed in __get_free_pages early */
  81. asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0));
  82. /* Memory clobbers used to order PDA accessed */
  83. mb();
  84. wrmsrl(MSR_GS_BASE, pda);
  85. mb();
  86. pda->cpunumber = cpu;
  87. pda->irqcount = -1;
  88. pda->kernelstack =
  89. (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE;
  90. pda->active_mm = &init_mm;
  91. pda->mmu_state = 0;
  92. if (cpu == 0) {
  93. /* others are initialized in smpboot.c */
  94. pda->pcurrent = &init_task;
  95. pda->irqstackptr = boot_cpu_stack;
  96. } else {
  97. pda->irqstackptr = (char *)
  98. __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
  99. if (!pda->irqstackptr)
  100. panic("cannot allocate irqstack for cpu %d", cpu);
  101. }
  102. pda->irqstackptr += IRQSTACKSIZE-64;
  103. }
  104. char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]
  105. __attribute__((section(".bss.page_aligned")));
  106. extern asmlinkage void ignore_sysret(void);
  107. /* May not be marked __init: used by software suspend */
  108. void syscall_init(void)
  109. {
  110. /*
  111. * LSTAR and STAR live in a bit strange symbiosis.
  112. * They both write to the same internal register. STAR allows to set CS/DS
  113. * but only a 32bit target. LSTAR sets the 64bit rip.
  114. */
  115. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  116. wrmsrl(MSR_LSTAR, system_call);
  117. wrmsrl(MSR_CSTAR, ignore_sysret);
  118. #ifdef CONFIG_IA32_EMULATION
  119. syscall32_cpu_init ();
  120. #endif
  121. /* Flags to clear on syscall */
  122. wrmsrl(MSR_SYSCALL_MASK,
  123. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  124. }
  125. void __cpuinit check_efer(void)
  126. {
  127. unsigned long efer;
  128. rdmsrl(MSR_EFER, efer);
  129. if (!(efer & EFER_NX) || do_not_nx) {
  130. __supported_pte_mask &= ~_PAGE_NX;
  131. }
  132. }
  133. unsigned long kernel_eflags;
  134. /*
  135. * Copies of the original ist values from the tss are only accessed during
  136. * debugging, no special alignment required.
  137. */
  138. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  139. /*
  140. * cpu_init() initializes state that is per-CPU. Some data is already
  141. * initialized (naturally) in the bootstrap process, such as the GDT
  142. * and IDT. We reload them nevertheless, this function acts as a
  143. * 'CPU state barrier', nothing should get across.
  144. * A lot of state is already set up in PDA init.
  145. */
  146. void __cpuinit cpu_init (void)
  147. {
  148. int cpu = stack_smp_processor_id();
  149. struct tss_struct *t = &per_cpu(init_tss, cpu);
  150. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  151. unsigned long v;
  152. char *estacks = NULL;
  153. struct task_struct *me;
  154. int i;
  155. /* CPU 0 is initialised in head64.c */
  156. if (cpu != 0) {
  157. pda_init(cpu);
  158. } else
  159. estacks = boot_exception_stacks;
  160. me = current;
  161. if (cpu_test_and_set(cpu, cpu_initialized))
  162. panic("CPU#%d already initialized!\n", cpu);
  163. printk("Initializing CPU#%d\n", cpu);
  164. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  165. /*
  166. * Initialize the per-CPU GDT with the boot GDT,
  167. * and set up the GDT descriptor:
  168. */
  169. if (cpu)
  170. memcpy(get_cpu_gdt_table(cpu), cpu_gdt_table, GDT_SIZE);
  171. cpu_gdt_descr[cpu].size = GDT_SIZE;
  172. load_gdt((const struct desc_ptr *)&cpu_gdt_descr[cpu]);
  173. load_idt((const struct desc_ptr *)&idt_descr);
  174. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  175. syscall_init();
  176. wrmsrl(MSR_FS_BASE, 0);
  177. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  178. barrier();
  179. check_efer();
  180. /*
  181. * set up and load the per-CPU TSS
  182. */
  183. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  184. static const unsigned int order[N_EXCEPTION_STACKS] = {
  185. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
  186. [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
  187. };
  188. if (cpu) {
  189. estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
  190. if (!estacks)
  191. panic("Cannot allocate exception stack %ld %d\n",
  192. v, cpu);
  193. }
  194. estacks += PAGE_SIZE << order[v];
  195. orig_ist->ist[v] = t->x86_tss.ist[v] = (unsigned long)estacks;
  196. }
  197. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  198. /*
  199. * <= is required because the CPU will access up to
  200. * 8 bits beyond the end of the IO permission bitmap.
  201. */
  202. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  203. t->io_bitmap[i] = ~0UL;
  204. atomic_inc(&init_mm.mm_count);
  205. me->active_mm = &init_mm;
  206. if (me->mm)
  207. BUG();
  208. enter_lazy_tlb(&init_mm, me);
  209. set_tss_desc(cpu, t);
  210. load_TR_desc();
  211. load_LDT(&init_mm.context);
  212. #ifdef CONFIG_KGDB
  213. /*
  214. * If the kgdb is connected no debug regs should be altered. This
  215. * is only applicable when KGDB and a KGDB I/O module are built
  216. * into the kernel and you are using early debugging with
  217. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  218. */
  219. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  220. arch_kgdb_ops.correct_hw_break();
  221. else {
  222. #endif
  223. /*
  224. * Clear all 6 debug registers:
  225. */
  226. set_debugreg(0UL, 0);
  227. set_debugreg(0UL, 1);
  228. set_debugreg(0UL, 2);
  229. set_debugreg(0UL, 3);
  230. set_debugreg(0UL, 6);
  231. set_debugreg(0UL, 7);
  232. #ifdef CONFIG_KGDB
  233. /* If the kgdb is connected no debug regs should be altered. */
  234. }
  235. #endif
  236. fpu_init();
  237. raw_local_save_flags(kernel_eflags);
  238. if (is_uv_system())
  239. uv_cpu_init();
  240. }