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 * Jun Nakajima <jun.nakajima@intel.com>
12 #include <linux/config.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/string.h>
17 #include <linux/bootmem.h>
18 #include <linux/bitops.h>
19 #include <linux/module.h>
20 #include <asm/bootsetup.h>
22 #include <asm/pgtable.h>
23 #include <asm/processor.h>
25 #include <asm/atomic.h>
26 #include <asm/mmu_context.h>
29 #include <asm/percpu.h>
30 #include <asm/proto.h>
31 #include <asm/sections.h>
33 #include <asm/hypervisor.h>
36 char x86_boot_params[BOOT_PARAM_SIZE] __initdata = {0,};
38 cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
40 struct x8664_pda *_cpu_pda[NR_CPUS] __read_mostly;
41 struct x8664_pda boot_cpu_pda[NR_CPUS] __cacheline_aligned;
43 #ifndef CONFIG_X86_NO_IDT
44 struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
47 char boot_cpu_stack[IRQSTACKSIZE] __attribute__((section(".bss.page_aligned")));
49 unsigned long __supported_pte_mask __read_mostly = ~0UL;
50 static int do_not_nx __cpuinitdata = 0;
53 Control non executable mappings for 64bit processes.
58 void __init nonx_setup(const char *str)
60 if (!strncmp(str, "on", 2)) {
61 __supported_pte_mask |= _PAGE_NX;
63 } else if (!strncmp(str, "off", 3)) {
65 __supported_pte_mask &= ~_PAGE_NX;
71 * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
72 * Always point %gs to its beginning
74 void __init setup_per_cpu_areas(void)
79 #ifdef CONFIG_HOTPLUG_CPU
80 prefill_possible_map();
83 /* Copy section for each CPU (we discard the original) */
84 size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
86 if (size < PERCPU_ENOUGH_ROOM)
87 size = PERCPU_ENOUGH_ROOM;
90 for_each_cpu_mask (i, cpu_possible_map) {
93 if (!NODE_DATA(cpu_to_node(i))) {
94 printk("cpu with no node %d, num_online_nodes %d\n",
95 i, num_online_nodes());
96 ptr = alloc_bootmem(size);
98 ptr = alloc_bootmem_node(NODE_DATA(cpu_to_node(i)), size);
101 panic("Cannot allocate cpu data for CPU %d\n", i);
102 cpu_pda(i)->data_offset = ptr - __per_cpu_start;
103 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
108 static void switch_pt(void)
110 xen_pt_switch(__pa(init_level4_pgt));
111 xen_new_user_pt(__pa(init_level4_user_pgt));
114 void __cpuinit cpu_gdt_init(struct desc_ptr *gdt_descr)
116 unsigned long frames[16];
120 for (va = gdt_descr->address, f = 0;
121 va < gdt_descr->address + gdt_descr->size;
122 va += PAGE_SIZE, f++) {
123 frames[f] = virt_to_mfn(va);
125 (void *)va, XENFEAT_writable_descriptor_tables);
127 if (HYPERVISOR_set_gdt(frames, gdt_descr->size /
128 sizeof (struct desc_struct)))
132 static void switch_pt(void)
134 asm volatile("movq %0,%%cr3" :: "r" (__pa_symbol(&init_level4_pgt)));
137 void __init cpu_gdt_init(struct desc_ptr *gdt_descr)
139 asm volatile("lgdt %0" :: "m" (*gdt_descr));
140 asm volatile("lidt %0" :: "m" (idt_descr));
144 void pda_init(int cpu)
146 struct x8664_pda *pda = cpu_pda(cpu);
148 /* Setup up data that may be needed in __get_free_pages early */
149 asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0));
151 wrmsrl(MSR_GS_BASE, pda);
153 HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL, (unsigned long)pda);
155 pda->cpunumber = cpu;
158 (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE;
159 pda->active_mm = &init_mm;
166 /* others are initialized in smpboot.c */
167 pda->pcurrent = &init_task;
168 pda->irqstackptr = boot_cpu_stack;
170 pda->irqstackptr = (char *)
171 __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
172 if (!pda->irqstackptr)
173 panic("cannot allocate irqstack for cpu %d", cpu);
178 pda->irqstackptr += IRQSTACKSIZE-64;
181 #ifndef CONFIG_X86_NO_TSS
182 char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]
183 __attribute__((section(".bss.page_aligned")));
186 /* May not be marked __init: used by software suspend */
187 void syscall_init(void)
191 * LSTAR and STAR live in a bit strange symbiosis.
192 * They both write to the same internal register. STAR allows to set CS/DS
193 * but only a 32bit target. LSTAR sets the 64bit rip.
195 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
196 wrmsrl(MSR_LSTAR, system_call);
198 /* Flags to clear on syscall */
199 wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000);
201 #ifdef CONFIG_IA32_EMULATION
202 syscall32_cpu_init ();
206 void __cpuinit check_efer(void)
210 rdmsrl(MSR_EFER, efer);
211 if (!(efer & EFER_NX) || do_not_nx) {
212 __supported_pte_mask &= ~_PAGE_NX;
217 * cpu_init() initializes state that is per-CPU. Some data is already
218 * initialized (naturally) in the bootstrap process, such as the GDT
219 * and IDT. We reload them nevertheless, this function acts as a
220 * 'CPU state barrier', nothing should get across.
221 * A lot of state is already set up in PDA init.
223 void __cpuinit cpu_init (void)
225 int cpu = stack_smp_processor_id();
226 #ifndef CONFIG_X86_NO_TSS
227 struct tss_struct *t = &per_cpu(init_tss, cpu);
229 char *estacks = NULL;
232 struct task_struct *me;
234 /* CPU 0 is initialised in head64.c */
237 zap_low_mappings(cpu);
239 #ifndef CONFIG_X86_NO_TSS
241 estacks = boot_exception_stacks;
246 if (cpu_test_and_set(cpu, cpu_initialized))
247 panic("CPU#%d already initialized!\n", cpu);
249 printk("Initializing CPU#%d\n", cpu);
251 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
254 * Initialize the per-CPU GDT with the boot GDT,
255 * and set up the GDT descriptor:
259 memcpy(cpu_gdt(cpu), cpu_gdt_table, GDT_SIZE);
262 cpu_gdt_descr[cpu].size = GDT_SIZE;
263 cpu_gdt_init(&cpu_gdt_descr[cpu]);
265 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
268 wrmsrl(MSR_FS_BASE, 0);
269 wrmsrl(MSR_KERNEL_GS_BASE, 0);
274 #ifndef CONFIG_X86_NO_TSS
276 * set up and load the per-CPU TSS
278 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
280 static const unsigned int order[N_EXCEPTION_STACKS] = {
281 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
282 [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
285 estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
287 panic("Cannot allocate exception stack %ld %d\n",
291 #if DEBUG_STKSZ > EXCEPTION_STKSZ
293 cpu_pda(cpu)->debugstack = (unsigned long)estacks;
294 estacks += DEBUG_STKSZ;
298 estacks += EXCEPTION_STKSZ;
301 t->ist[v] = (unsigned long)estacks;
304 t->io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
306 * <= is required because the CPU will access up to
307 * 8 bits beyond the end of the IO permission bitmap.
309 for (i = 0; i <= IO_BITMAP_LONGS; i++)
310 t->io_bitmap[i] = ~0UL;
313 atomic_inc(&init_mm.mm_count);
314 me->active_mm = &init_mm;
317 enter_lazy_tlb(&init_mm, me);
319 #ifndef CONFIG_X86_NO_TSS
320 set_tss_desc(cpu, t);
325 load_LDT(&init_mm.context);
328 * Clear all 6 debug registers:
331 set_debugreg(0UL, 0);
332 set_debugreg(0UL, 1);
333 set_debugreg(0UL, 2);
334 set_debugreg(0UL, 3);
335 set_debugreg(0UL, 6);
336 set_debugreg(0UL, 7);