- patches.rt/0001-sched-count-of-queued-RT-tasks.patch: Delete.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / 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/pda.h>
19 #include <asm/pgtable.h>
20 #include <asm/processor.h>
21 #include <asm/desc.h>
22 #include <asm/atomic.h>
23 #include <asm/mmu_context.h>
24 #include <asm/smp.h>
25 #include <asm/i387.h>
26 #include <asm/percpu.h>
27 #include <asm/proto.h>
28 #include <asm/sections.h>
29 #include <asm/setup.h>
30 #ifdef CONFIG_XEN
31 #include <asm/hypervisor.h>
32 #endif
33
34 struct boot_params __initdata boot_params;
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 static int __init nonx_setup(char *str)
59 {
60         if (!str)
61                 return -EINVAL;
62         if (!strncmp(str, "on", 2)) {
63                 __supported_pte_mask |= _PAGE_NX; 
64                 do_not_nx = 0; 
65         } else if (!strncmp(str, "off", 3)) {
66                 do_not_nx = 1;
67                 __supported_pte_mask &= ~_PAGE_NX;
68         }
69         return 0;
70
71 early_param("noexec", nonx_setup);
72
73 int force_personality32 = 0; 
74
75 /* noexec32=on|off
76 Control non executable heap for 32bit processes.
77 To control the stack too use noexec=off
78
79 on      PROT_READ does not imply PROT_EXEC for 32bit processes
80 off     PROT_READ implies PROT_EXEC (default)
81 */
82 static int __init nonx32_setup(char *str)
83 {
84         if (!strcmp(str, "on"))
85                 force_personality32 &= ~READ_IMPLIES_EXEC;
86         else if (!strcmp(str, "off"))
87                 force_personality32 |= READ_IMPLIES_EXEC;
88         return 1;
89 }
90 __setup("noexec32=", nonx32_setup);
91
92 /*
93  * Great future plan:
94  * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
95  * Always point %gs to its beginning
96  */
97 void __init setup_per_cpu_areas(void)
98
99         int i;
100         unsigned long size;
101
102 #ifdef CONFIG_HOTPLUG_CPU
103         prefill_possible_map();
104 #endif
105
106         /* Copy section for each CPU (we discard the original) */
107         size = PERCPU_ENOUGH_ROOM;
108
109         printk(KERN_INFO "PERCPU: Allocating %lu bytes of per cpu data\n", size);
110         for_each_cpu_mask (i, cpu_possible_map) {
111                 char *ptr;
112
113                 if (!NODE_DATA(cpu_to_node(i))) {
114                         printk("cpu with no node %d, num_online_nodes %d\n",
115                                i, num_online_nodes());
116                         ptr = alloc_bootmem_pages(size);
117                 } else { 
118                         ptr = alloc_bootmem_pages_node(NODE_DATA(cpu_to_node(i)), size);
119                 }
120                 if (!ptr)
121                         panic("Cannot allocate cpu data for CPU %d\n", i);
122                 cpu_pda(i)->data_offset = ptr - __per_cpu_start;
123                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
124         }
125
126
127 #ifdef CONFIG_XEN
128 static void __init_refok switch_pt(int cpu)
129 {
130         if (cpu == 0)
131                 xen_init_pt();
132         xen_pt_switch(__pa_symbol(init_level4_pgt));
133         xen_new_user_pt(__pa_symbol(__user_pgd(init_level4_pgt)));
134 }
135 #define switch_pt() switch_pt(cpu)
136
137 static void __cpuinit cpu_gdt_init(const struct desc_ptr *gdt_descr)
138 {
139         unsigned long frames[16];
140         unsigned long va;
141         int f;
142
143         for (va = gdt_descr->address, f = 0;
144              va < gdt_descr->address + gdt_descr->size;
145              va += PAGE_SIZE, f++) {
146                 frames[f] = virt_to_mfn(va);
147                 make_page_readonly(
148                         (void *)va, XENFEAT_writable_descriptor_tables);
149         }
150         if (HYPERVISOR_set_gdt(frames, (gdt_descr->size + 1) /
151                                sizeof (struct desc_struct)))
152                 BUG();
153 }
154 #else
155 static void switch_pt(void)
156 {
157         asm volatile("movq %0,%%cr3" :: "r" (__pa_symbol(&init_level4_pgt)));
158 }
159
160 static void __cpuinit cpu_gdt_init(const struct desc_ptr *gdt_descr)
161 {
162         load_gdt(gdt_descr);
163         load_idt(idt_descr);
164 }
165 #endif
166
167 void pda_init(int cpu)
168
169         struct x8664_pda *pda = cpu_pda(cpu);
170
171         /* Setup up data that may be needed in __get_free_pages early */
172         asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0)); 
173 #ifndef CONFIG_XEN
174         /* Memory clobbers used to order PDA accessed */
175         mb();
176         wrmsrl(MSR_GS_BASE, pda);
177         mb();
178 #else
179         if (HYPERVISOR_set_segment_base(SEGBASE_GS_KERNEL,
180                                         (unsigned long)pda))
181                 BUG();
182 #endif
183         pda->cpunumber = cpu; 
184         pda->irqcount = -1;
185         pda->kernelstack = 
186                 (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE; 
187         pda->active_mm = &init_mm;
188         pda->mmu_state = 0;
189
190         if (cpu == 0) {
191                 /* others are initialized in smpboot.c */
192                 pda->pcurrent = &init_task;
193                 pda->irqstackptr = boot_cpu_stack; 
194         } else {
195                 pda->irqstackptr = (char *)
196                         __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
197                 if (!pda->irqstackptr)
198                         panic("cannot allocate irqstack for cpu %d", cpu); 
199         }
200
201         switch_pt();
202
203         pda->irqstackptr += IRQSTACKSIZE-64;
204
205
206 #ifndef CONFIG_X86_NO_TSS
207 char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]
208 __attribute__((section(".bss.page_aligned")));
209 #endif
210
211 extern asmlinkage void ignore_sysret(void);
212
213 /* May not be marked __init: used by software suspend */
214 void syscall_init(void)
215 {
216 #ifndef CONFIG_XEN
217         /* 
218          * LSTAR and STAR live in a bit strange symbiosis.
219          * They both write to the same internal register. STAR allows to set CS/DS
220          * but only a 32bit target. LSTAR sets the 64bit rip.    
221          */ 
222         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
223         wrmsrl(MSR_LSTAR, system_call); 
224         wrmsrl(MSR_CSTAR, ignore_sysret);
225
226         /* Flags to clear on syscall */
227         wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
228 #endif
229 #ifdef CONFIG_IA32_EMULATION            
230         syscall32_cpu_init ();
231 #else
232         {
233                 static const struct callback_register cstar = {
234                         .type = CALLBACKTYPE_syscall32,
235                         .address = (unsigned long)ignore_sysret
236                 };
237                 if (HYPERVISOR_callback_op(CALLBACKOP_register, &cstar))
238                         printk(KERN_WARN "Unable to register CSTAR callback\n");
239         }
240 #endif
241 }
242
243 void __cpuinit check_efer(void)
244 {
245         unsigned long efer;
246
247         rdmsrl(MSR_EFER, efer); 
248         if (!(efer & EFER_NX) || do_not_nx) { 
249                 __supported_pte_mask &= ~_PAGE_NX; 
250         }       
251 }
252
253 unsigned long kernel_eflags;
254
255 #ifndef CONFIG_X86_NO_TSS
256 /*
257  * Copies of the original ist values from the tss are only accessed during
258  * debugging, no special alignment required.
259  */
260 DEFINE_PER_CPU(struct orig_ist, orig_ist);
261 #endif
262
263 /*
264  * cpu_init() initializes state that is per-CPU. Some data is already
265  * initialized (naturally) in the bootstrap process, such as the GDT
266  * and IDT. We reload them nevertheless, this function acts as a
267  * 'CPU state barrier', nothing should get across.
268  * A lot of state is already set up in PDA init.
269  */
270 void __cpuinit cpu_init (void)
271 {
272         int cpu = stack_smp_processor_id();
273 #ifndef CONFIG_X86_NO_TSS
274         struct tss_struct *t = &per_cpu(init_tss, cpu);
275         struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
276         unsigned long v; 
277         char *estacks = NULL; 
278         unsigned i;
279 #endif
280         struct task_struct *me;
281
282         /* CPU 0 is initialised in head64.c */
283         if (cpu != 0) {
284                 pda_init(cpu);
285         }
286 #ifndef CONFIG_X86_NO_TSS
287         else
288                 estacks = boot_exception_stacks; 
289 #endif
290
291         me = current;
292
293         if (cpu_test_and_set(cpu, cpu_initialized))
294                 panic("CPU#%d already initialized!\n", cpu);
295
296         printk("Initializing CPU#%d\n", cpu);
297
298         clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
299
300         /*
301          * Initialize the per-CPU GDT with the boot GDT,
302          * and set up the GDT descriptor:
303          */
304 #ifndef CONFIG_XEN 
305         if (cpu)
306                 memcpy(cpu_gdt(cpu), cpu_gdt_table, GDT_SIZE);
307 #endif
308
309         cpu_gdt_descr[cpu].size = GDT_SIZE;
310         cpu_gdt_init(&cpu_gdt_descr[cpu]);
311
312         memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
313         syscall_init();
314
315         wrmsrl(MSR_FS_BASE, 0);
316         wrmsrl(MSR_KERNEL_GS_BASE, 0);
317         barrier(); 
318
319         check_efer();
320
321 #ifndef CONFIG_X86_NO_TSS
322         /*
323          * set up and load the per-CPU TSS
324          */
325         for (v = 0; v < N_EXCEPTION_STACKS; v++) {
326                 static const unsigned int order[N_EXCEPTION_STACKS] = {
327                         [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
328                         [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
329                 };
330                 if (cpu) {
331                         estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
332                         if (!estacks)
333                                 panic("Cannot allocate exception stack %ld %d\n",
334                                       v, cpu); 
335                 }
336                 estacks += PAGE_SIZE << order[v];
337                 orig_ist->ist[v] = t->ist[v] = (unsigned long)estacks;
338         }
339
340         t->io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
341         /*
342          * <= is required because the CPU will access up to
343          * 8 bits beyond the end of the IO permission bitmap.
344          */
345         for (i = 0; i <= IO_BITMAP_LONGS; i++)
346                 t->io_bitmap[i] = ~0UL;
347 #endif
348
349         atomic_inc(&init_mm.mm_count);
350         me->active_mm = &init_mm;
351         if (me->mm)
352                 BUG();
353         enter_lazy_tlb(&init_mm, me);
354
355 #ifndef CONFIG_X86_NO_TSS
356         set_tss_desc(cpu, t);
357 #endif
358 #ifndef CONFIG_XEN
359         load_TR_desc();
360 #endif
361         load_LDT(&init_mm.context);
362
363         /*
364          * Clear all 6 debug registers:
365          */
366
367         set_debugreg(0UL, 0);
368         set_debugreg(0UL, 1);
369         set_debugreg(0UL, 2);
370         set_debugreg(0UL, 3);
371         set_debugreg(0UL, 6);
372         set_debugreg(0UL, 7);
373
374         fpu_init(); 
375
376         raw_local_save_flags(kernel_eflags);
377 }