Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / x86_64 / kernel / setup64-xen.c
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  * Jun Nakajima <jun.nakajima@intel.com> 
8  *   Modified for Xen
9  *
10  */ 
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/string.h>
15 #include <linux/bootmem.h>
16 #include <linux/bitops.h>
17 #include <linux/module.h>
18 #include <asm/bootsetup.h>
19 #include <asm/pda.h>
20 #include <asm/pgtable.h>
21 #include <asm/processor.h>
22 #include <asm/desc.h>
23 #include <asm/atomic.h>
24 #include <asm/mmu_context.h>
25 #include <asm/smp.h>
26 #include <asm/i387.h>
27 #include <asm/percpu.h>
28 #include <asm/proto.h>
29 #include <asm/sections.h>
30 #ifdef CONFIG_XEN
31 #include <asm/hypervisor.h>
32 #endif
33
34 char x86_boot_params[BOOT_PARAM_SIZE] __initdata = {0,};
35
36 cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
37
38 struct x8664_pda *_cpu_pda[NR_CPUS] __read_mostly;
39 EXPORT_SYMBOL(_cpu_pda);
40 struct x8664_pda boot_cpu_pda[NR_CPUS] __cacheline_aligned;
41
42 #ifndef CONFIG_X86_NO_IDT
43 struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table }; 
44 #endif
45
46 char boot_cpu_stack[IRQSTACKSIZE] __attribute__((section(".bss.page_aligned")));
47
48 unsigned long __supported_pte_mask __read_mostly = ~0UL;
49 EXPORT_SYMBOL(__supported_pte_mask);
50 static int do_not_nx __cpuinitdata = 0;
51
52 /* noexec=on|off
53 Control non executable mappings for 64bit processes.
54
55 on      Enable(default)
56 off     Disable
57 */ 
58 void __init nonx_setup(char *str)
59 {
60         if (!strncmp(str, "on", 2)) {
61                 __supported_pte_mask |= _PAGE_NX; 
62                 do_not_nx = 0; 
63         } else if (!strncmp(str, "off", 3)) {
64                 do_not_nx = 1;
65                 __supported_pte_mask &= ~_PAGE_NX;
66         }
67 }
68
69 /*
70  * Great future plan:
71  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
72  * Always point %gs to its beginning
73  */
74 void __init setup_per_cpu_areas(void)
75
76         int i;
77         unsigned long size;
78
79 #ifdef CONFIG_HOTPLUG_CPU
80         prefill_possible_map();
81 #endif
82
83         /* Copy section for each CPU (we discard the original) */
84         size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
85 #ifdef CONFIG_MODULES
86         if (size < PERCPU_ENOUGH_ROOM)
87                 size = PERCPU_ENOUGH_ROOM;
88 #endif
89
90         for_each_cpu_mask (i, cpu_possible_map) {
91                 char *ptr;
92
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);
97                 } else { 
98                         ptr = alloc_bootmem_node(NODE_DATA(cpu_to_node(i)), size);
99                 }
100                 if (!ptr)
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);
104         }
105
106
107 #ifdef CONFIG_XEN
108 static void switch_pt(void)
109 {
110         xen_pt_switch(__pa(init_level4_pgt));
111         xen_new_user_pt(__pa(init_level4_user_pgt));
112 }
113
114 void __cpuinit cpu_gdt_init(struct desc_ptr *gdt_descr)
115 {
116         unsigned long frames[16];
117         unsigned long va;
118         int f;
119
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);
124                 make_page_readonly(
125                         (void *)va, XENFEAT_writable_descriptor_tables);
126         }
127         if (HYPERVISOR_set_gdt(frames, gdt_descr->size /
128                                sizeof (struct desc_struct)))
129                 BUG();
130 }
131 #else
132 static void switch_pt(void)
133 {
134         asm volatile("movq %0,%%cr3" :: "r" (__pa_symbol(&init_level4_pgt)));
135 }
136
137 void __init cpu_gdt_init(struct desc_ptr *gdt_descr)
138 {
139         asm volatile("lgdt %0" :: "m" (*gdt_descr));
140         asm volatile("lidt %0" :: "m" (idt_descr));
141 }
142 #endif
143
144 void pda_init(int cpu)
145
146         struct x8664_pda *pda = cpu_pda(cpu);
147
148         /* Setup up data that may be needed in __get_free_pages early */
149         asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0)); 
150 #ifndef CONFIG_XEN
151         wrmsrl(MSR_GS_BASE, pda);
152 #else
153         HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL, (unsigned long)pda);
154 #endif
155         pda->cpunumber = cpu; 
156         pda->irqcount = -1;
157         pda->kernelstack = 
158                 (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE; 
159         pda->active_mm = &init_mm;
160         pda->mmu_state = 0;
161
162         if (cpu == 0) {
163 #ifdef CONFIG_XEN
164                 xen_init_pt();
165 #endif
166                 /* others are initialized in smpboot.c */
167                 pda->pcurrent = &init_task;
168                 pda->irqstackptr = boot_cpu_stack; 
169         } else {
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); 
174         }
175
176         switch_pt();
177
178         pda->irqstackptr += IRQSTACKSIZE-64;
179
180
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")));
184 #endif
185
186 /* May not be marked __init: used by software suspend */
187 void syscall_init(void)
188 {
189 #ifndef CONFIG_XEN
190         /* 
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.    
194          */ 
195         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
196         wrmsrl(MSR_LSTAR, system_call); 
197
198         /* Flags to clear on syscall */
199         wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
200 #endif
201 #ifdef CONFIG_IA32_EMULATION            
202         syscall32_cpu_init ();
203 #endif
204 }
205
206 void __cpuinit check_efer(void)
207 {
208         unsigned long efer;
209
210         rdmsrl(MSR_EFER, efer); 
211         if (!(efer & EFER_NX) || do_not_nx) { 
212                 __supported_pte_mask &= ~_PAGE_NX; 
213         }       
214 }
215
216 /*
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.
222  */
223 void __cpuinit cpu_init (void)
224 {
225         int cpu = stack_smp_processor_id();
226 #ifndef CONFIG_X86_NO_TSS
227         struct tss_struct *t = &per_cpu(init_tss, cpu);
228         struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
229         unsigned long v; 
230         char *estacks = NULL; 
231         unsigned i;
232 #endif
233         struct task_struct *me;
234
235         /* CPU 0 is initialised in head64.c */
236         if (cpu != 0) {
237                 pda_init(cpu);
238                 zap_low_mappings(cpu);
239         }
240 #ifndef CONFIG_X86_NO_TSS
241         else
242                 estacks = boot_exception_stacks; 
243 #endif
244
245         me = current;
246
247         if (cpu_test_and_set(cpu, cpu_initialized))
248                 panic("CPU#%d already initialized!\n", cpu);
249
250         printk("Initializing CPU#%d\n", cpu);
251
252         clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
253
254         /*
255          * Initialize the per-CPU GDT with the boot GDT,
256          * and set up the GDT descriptor:
257          */
258 #ifndef CONFIG_XEN 
259         if (cpu)
260                 memcpy(cpu_gdt(cpu), cpu_gdt_table, GDT_SIZE);
261 #endif
262
263         cpu_gdt_descr[cpu].size = GDT_SIZE;
264         cpu_gdt_init(&cpu_gdt_descr[cpu]);
265
266         memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
267         syscall_init();
268
269         wrmsrl(MSR_FS_BASE, 0);
270         wrmsrl(MSR_KERNEL_GS_BASE, 0);
271         barrier(); 
272
273         check_efer();
274
275 #ifndef CONFIG_X86_NO_TSS
276         /*
277          * set up and load the per-CPU TSS
278          */
279         for (v = 0; v < N_EXCEPTION_STACKS; v++) {
280                 if (cpu) {
281                         static const unsigned int order[N_EXCEPTION_STACKS] = {
282                                 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
283                                 [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
284                         };
285
286                         estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
287                         if (!estacks)
288                                 panic("Cannot allocate exception stack %ld %d\n",
289                                       v, cpu); 
290                 }
291                 switch (v + 1) {
292 #if DEBUG_STKSZ > EXCEPTION_STKSZ
293                 case DEBUG_STACK:
294                         cpu_pda(cpu)->debugstack = (unsigned long)estacks;
295                         estacks += DEBUG_STKSZ;
296                         break;
297 #endif
298                 default:
299                         estacks += EXCEPTION_STKSZ;
300                         break;
301                 }
302                 orig_ist->ist[v] = t->ist[v] = (unsigned long)estacks;
303         }
304
305         t->io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
306         /*
307          * <= is required because the CPU will access up to
308          * 8 bits beyond the end of the IO permission bitmap.
309          */
310         for (i = 0; i <= IO_BITMAP_LONGS; i++)
311                 t->io_bitmap[i] = ~0UL;
312 #endif
313
314         atomic_inc(&init_mm.mm_count);
315         me->active_mm = &init_mm;
316         if (me->mm)
317                 BUG();
318         enter_lazy_tlb(&init_mm, me);
319
320 #ifndef CONFIG_X86_NO_TSS
321         set_tss_desc(cpu, t);
322 #endif
323 #ifndef CONFIG_XEN
324         load_TR_desc();
325 #endif
326         load_LDT(&init_mm.context);
327
328         /*
329          * Clear all 6 debug registers:
330          */
331
332         set_debugreg(0UL, 0);
333         set_debugreg(0UL, 1);
334         set_debugreg(0UL, 2);
335         set_debugreg(0UL, 3);
336         set_debugreg(0UL, 6);
337         set_debugreg(0UL, 7);
338
339         fpu_init(); 
340 }