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 * $Id: setup64.c,v 1.12 2002/03/21 10:09:17 ak Exp $
8 #include <linux/config.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/bootmem.h>
15 #include <asm/pgtable.h>
16 #include <asm/processor.h>
18 #include <asm/bitops.h>
19 #include <asm/atomic.h>
20 #include <asm/mmu_context.h>
23 #include <asm/percpu.h>
25 #include <asm/proto.h>
28 char x86_boot_params[2048] __initdata = {0,};
30 unsigned long cpu_initialized __initdata = 0;
32 struct x8664_pda cpu_pda[NR_CPUS] __cacheline_aligned;
34 extern struct task_struct init_task;
36 extern unsigned char __per_cpu_start[], __per_cpu_end[];
38 extern struct desc_ptr cpu_gdt_descr[];
39 struct desc_ptr idt_descr = { 256 * 16, (unsigned long) idt_table };
41 char boot_cpu_stack[IRQSTACKSIZE] __cacheline_aligned;
43 unsigned long __supported_pte_mask = ~0UL;
44 static int do_not_nx __initdata = 0;
45 unsigned long vm_stack_flags = __VM_STACK_FLAGS;
46 unsigned long vm_stack_flags32 = __VM_STACK_FLAGS;
47 unsigned long vm_data_default_flags = __VM_DATA_DEFAULT_FLAGS;
48 unsigned long vm_data_default_flags32 = __VM_DATA_DEFAULT_FLAGS;
49 unsigned long vm_force_exec32 = PROT_EXEC;
52 Control non executable mappings for 64bit processes.
56 noforce (default) Don't enable by default for heap/stack/data,
57 but allow PROT_EXEC to be effective
60 static int __init nonx_setup(char *str)
62 if (!strncmp(str, "on",3)) {
63 __supported_pte_mask |= _PAGE_NX;
65 vm_data_default_flags &= ~VM_EXEC;
66 vm_stack_flags &= ~VM_EXEC;
67 } else if (!strncmp(str, "noforce",7) || !strncmp(str,"off",3)) {
68 do_not_nx = (str[0] == 'o');
70 __supported_pte_mask &= ~_PAGE_NX;
71 vm_data_default_flags |= VM_EXEC;
72 vm_stack_flags |= VM_EXEC;
77 __setup("noexec=", nonx_setup);
81 Control the no exec default for 32bit processes. Can be also overwritten
82 per executable using ELF header flags (e.g. needed for the X server)
83 Requires noexec=on or noexec=noforce to be effective.
86 all,on Heap,stack,data is non executable.
87 off (default) Heap,stack,data is executable
88 stack Stack is non executable, heap/data is.
89 force Don't imply PROT_EXEC for PROT_READ
90 compat (default) Imply PROT_EXEC for PROT_READ
93 static int __init nonx32_setup(char *str)
96 while ((s = strsep(&str, ",")) != NULL) {
97 if (!strcmp(s, "all") || !strcmp(s,"on")) {
98 vm_data_default_flags32 &= ~VM_EXEC;
99 vm_stack_flags32 &= ~VM_EXEC;
100 } else if (!strcmp(s, "off")) {
101 vm_data_default_flags32 |= VM_EXEC;
102 vm_stack_flags32 |= VM_EXEC;
103 } else if (!strcmp(s, "stack")) {
104 vm_data_default_flags32 |= VM_EXEC;
105 vm_stack_flags32 &= ~VM_EXEC;
106 } else if (!strcmp(s, "force")) {
108 } else if (!strcmp(s, "compat")) {
109 vm_force_exec32 = PROT_EXEC;
115 __setup("noexec32=", nonx32_setup);
119 * Declare PDA itself and support (irqstack,tss,pml4) as per cpu data.
120 * Always point %gs to its beginning
122 void __init setup_per_cpu_areas(void)
127 /* Copy section for each CPU (we discard the original) */
128 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
129 #ifdef CONFIG_MODULES
130 if (size < PERCPU_ENOUGH_ROOM)
131 size = PERCPU_ENOUGH_ROOM;
134 /* We don't support CPU hotplug, so only allocate as much as needed here */
136 int maxi = max_t(unsigned, numnodes, num_online_cpus());
138 for (i = 0; i < maxi; i++) {
139 /* If possible allocate on the node of the CPU.
140 In case it doesn't exist round-robin nodes. */
141 unsigned char *ptr = alloc_bootmem_node(NODE_DATA(i % numnodes), size);
143 panic("Cannot allocate cpu data for CPU %d\n", i);
144 cpu_pda[i].data_offset = ptr - __per_cpu_start;
145 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
149 void pda_init(int cpu)
152 struct x8664_pda *pda = &cpu_pda[cpu];
154 /* Setup up data that may be needed in __get_free_pages early */
155 asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0));
156 wrmsrl(MSR_GS_BASE, cpu_pda + cpu);
159 pda->cpunumber = cpu;
161 pda->data_offset = 0;
163 (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE;
164 pda->active_mm = &init_mm;
168 /* others are initialized in smpboot.c */
169 pda->pcurrent = &init_task;
170 pda->irqstackptr = boot_cpu_stack;
171 level4 = init_level4_pgt;
173 pda->irqstackptr = (char *)
174 __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
175 if (!pda->irqstackptr)
176 panic("cannot allocate irqstack for cpu %d\n", cpu);
177 level4 = (pml4_t *)__get_free_pages(GFP_ATOMIC, 0);
180 panic("Cannot allocate top level page for cpu %d", cpu);
182 pda->level4_pgt = (unsigned long *)level4;
183 if (level4 != init_level4_pgt)
184 memcpy(level4, &init_level4_pgt, PAGE_SIZE);
185 set_pml4(level4 + 510, mk_kernel_pml4(__pa_symbol(boot_vmalloc_pgt)));
186 asm volatile("movq %0,%%cr3" :: "r" (__pa(level4)));
188 pda->irqstackptr += IRQSTACKSIZE-64;
191 #define EXCEPTION_STK_ORDER 0 /* >= N_EXCEPTION_STACKS*EXCEPTION_STKSZ */
192 char boot_exception_stacks[N_EXCEPTION_STACKS*EXCEPTION_STKSZ];
194 void syscall_init(void)
197 * LSTAR and STAR live in a bit strange symbiosis.
198 * They both write to the same internal register. STAR allows to set CS/DS
199 * but only a 32bit target. LSTAR sets the 64bit rip.
201 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
202 wrmsrl(MSR_LSTAR, system_call);
204 #ifdef CONFIG_IA32_EMULATION
205 wrmsrl(MSR_CSTAR, ia32_cstar_target);
208 /* Flags to clear on syscall */
209 wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000);
213 * cpu_init() initializes state that is per-CPU. Some data is already
214 * initialized (naturally) in the bootstrap process, such as the GDT
215 * and IDT. We reload them nevertheless, this function acts as a
216 * 'CPU state barrier', nothing should get across.
217 * A lot of state is already set up in PDA init.
219 void __init cpu_init (void)
222 int cpu = stack_smp_processor_id();
224 int cpu = smp_processor_id();
226 struct tss_struct * t = &init_tss[cpu];
227 unsigned long v, efer;
229 struct task_struct *me;
231 /* CPU 0 is initialised in head64.c */
234 estacks = (char *)__get_free_pages(GFP_ATOMIC, 0);
236 panic("Can't allocate exception stacks for CPU %d\n",cpu);
238 estacks = boot_exception_stacks;
242 if (test_and_set_bit(cpu, &cpu_initialized))
243 panic("CPU#%d already initialized!\n", cpu);
245 printk("Initializing CPU#%d\n", cpu);
247 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
250 * Initialize the per-CPU GDT with the boot GDT,
251 * and set up the GDT descriptor:
254 memcpy(cpu_gdt_table[cpu], cpu_gdt_table[0], GDT_SIZE);
257 cpu_gdt_descr[cpu].size = GDT_SIZE;
258 cpu_gdt_descr[cpu].address = (unsigned long)cpu_gdt_table[cpu];
259 __asm__ __volatile__("lgdt %0": "=m" (cpu_gdt_descr[cpu]));
260 __asm__ __volatile__("lidt %0": "=m" (idt_descr));
262 memcpy(me->thread.tls_array, cpu_gdt_table[cpu], GDT_ENTRY_TLS_ENTRIES * 8);
268 asm volatile("pushfq ; popq %%rax ; btr $14,%%rax ; pushq %%rax ; popfq" ::: "eax");
272 wrmsrl(MSR_FS_BASE, 0);
273 wrmsrl(MSR_KERNEL_GS_BASE, 0);
276 rdmsrl(MSR_EFER, efer);
277 if (!(efer & EFER_NX) || do_not_nx) {
278 __supported_pte_mask &= ~_PAGE_NX;
282 * set up and load the per-CPU TSS
284 estacks += EXCEPTION_STKSZ;
285 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
286 t->ist[v] = (unsigned long)estacks;
287 estacks += EXCEPTION_STKSZ;
290 t->io_map_base = INVALID_IO_BITMAP_OFFSET;
292 atomic_inc(&init_mm.mm_count);
293 me->active_mm = &init_mm;
296 enter_lazy_tlb(&init_mm, me);
298 set_tss_desc(cpu, t);
300 load_LDT(&init_mm.context);
303 * Clear all 6 debug registers: