setup64.c 6.6 KB

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