ba9c13c6c754ea6ed9ecdc1a055f69f6c846a5b5
[linux-flexiantxendom0-3.2.10.git] / arch / x86_64 / kernel / setup64.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  * $Id: setup64.c,v 1.12 2002/03/21 10:09:17 ak Exp $
7  */ 
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>
14 #include <asm/pda.h>
15 #include <asm/pgtable.h>
16 #include <asm/processor.h>
17 #include <asm/desc.h>
18 #include <asm/bitops.h>
19 #include <asm/atomic.h>
20 #include <asm/mmu_context.h>
21 #include <asm/smp.h>
22 #include <asm/i387.h>
23 #include <asm/percpu.h>
24 #include <asm/mtrr.h>
25 #include <asm/proto.h>
26 #include <asm/mman.h>
27
28 char x86_boot_params[2048] __initdata = {0,};
29
30 unsigned long cpu_initialized __initdata = 0;
31
32 struct x8664_pda cpu_pda[NR_CPUS] __cacheline_aligned; 
33
34 extern struct task_struct init_task;
35
36 extern unsigned char __per_cpu_start[], __per_cpu_end[]; 
37
38 extern struct desc_ptr cpu_gdt_descr[];
39 struct desc_ptr idt_descr = { 256 * 16, (unsigned long) idt_table }; 
40
41 char boot_cpu_stack[IRQSTACKSIZE] __cacheline_aligned;
42
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; 
50
51 /* noexec=on|off
52 Control non executable mappings for 64bit processes.
53
54 on      Enable
55 off     Disable
56 noforce (default) Don't enable by default for heap/stack/data, 
57         but allow PROT_EXEC to be effective
58
59 */ 
60 static int __init nonx_setup(char *str)
61 {
62         if (!strncmp(str, "on",3)) { 
63                 __supported_pte_mask |= _PAGE_NX; 
64                 do_not_nx = 0; 
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');
69                 if (do_not_nx)
70                         __supported_pte_mask &= ~_PAGE_NX; 
71                 vm_data_default_flags |= VM_EXEC; 
72                 vm_stack_flags |= VM_EXEC;
73         } 
74         return 1;
75
76
77 __setup("noexec=", nonx_setup); 
78
79 /* noexec32=opt{,opt} 
80
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.
84
85 Valid options: 
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
91
92 */
93  static int __init nonx32_setup(char *str)
94  {
95         char *s;
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")) { 
107                         vm_force_exec32 = 0; 
108                 } else if (!strcmp(s, "compat")) { 
109                         vm_force_exec32 = PROT_EXEC;
110                 } 
111         } 
112         return 1;
113
114
115 __setup("noexec32=", nonx32_setup); 
116
117 /* 
118  * Great future plan: 
119  * Declare PDA itself and support (irqstack,tss,pml4) as per cpu data.
120  * Always point %gs to its beginning
121  */
122 void __init setup_per_cpu_areas(void)
123
124         int i;
125         unsigned long size;
126
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;
132 #endif
133
134         /* We don't support CPU hotplug, so only allocate as much as needed here */
135
136         int maxi = max_t(unsigned, numnodes, num_online_cpus()); 
137
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);
142                 if (!ptr)
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);
146         }
147
148
149 void pda_init(int cpu)
150
151         pml4_t *level4;
152         struct x8664_pda *pda = &cpu_pda[cpu];
153
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);
157
158         pda->me = pda;
159         pda->cpunumber = cpu; 
160         pda->irqcount = -1;
161         pda->data_offset = 0;
162         pda->kernelstack = 
163                 (unsigned long)stack_thread_info() - PDA_STACKOFFSET + THREAD_SIZE; 
164         pda->active_mm = &init_mm;
165         pda->mmu_state = 0;
166
167         if (cpu == 0) {
168                 /* others are initialized in smpboot.c */
169                 pda->pcurrent = &init_task;
170                 pda->irqstackptr = boot_cpu_stack; 
171                 level4 = init_level4_pgt; 
172         } else {
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); 
178         }
179         if (!level4) 
180                 panic("Cannot allocate top level page for cpu %d", cpu); 
181
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))); 
187
188         pda->irqstackptr += IRQSTACKSIZE-64;
189
190
191 #define EXCEPTION_STK_ORDER 0 /* >= N_EXCEPTION_STACKS*EXCEPTION_STKSZ */
192 char boot_exception_stacks[N_EXCEPTION_STACKS*EXCEPTION_STKSZ];
193
194 void syscall_init(void)
195 {
196         /* 
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.    
200          */ 
201         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
202         wrmsrl(MSR_LSTAR, system_call); 
203
204 #ifdef CONFIG_IA32_EMULATION            
205         wrmsrl(MSR_CSTAR, ia32_cstar_target); 
206 #endif
207
208         /* Flags to clear on syscall */
209         wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
210 }
211
212 /*
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.
218  */
219 void __init cpu_init (void)
220 {
221 #ifdef CONFIG_SMP
222         int cpu = stack_smp_processor_id();
223 #else
224         int cpu = smp_processor_id();
225 #endif
226         struct tss_struct * t = &init_tss[cpu];
227         unsigned long v, efer; 
228         char *estacks; 
229         struct task_struct *me;
230
231         /* CPU 0 is initialised in head64.c */
232         if (cpu != 0) {
233                 pda_init(cpu);
234                 estacks = (char *)__get_free_pages(GFP_ATOMIC, 0); 
235                 if (!estacks)
236                         panic("Can't allocate exception stacks for CPU %d\n",cpu);
237         } else 
238                 estacks = boot_exception_stacks; 
239
240         me = current;
241
242         if (test_and_set_bit(cpu, &cpu_initialized))
243                 panic("CPU#%d already initialized!\n", cpu);
244
245         printk("Initializing CPU#%d\n", cpu);
246
247                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
248
249         /*
250          * Initialize the per-CPU GDT with the boot GDT,
251          * and set up the GDT descriptor:
252          */
253         if (cpu) {
254                 memcpy(cpu_gdt_table[cpu], cpu_gdt_table[0], GDT_SIZE);
255         }       
256
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));
261
262         memcpy(me->thread.tls_array, cpu_gdt_table[cpu], GDT_ENTRY_TLS_ENTRIES * 8);
263
264         /*
265          * Delete NT
266          */
267
268         asm volatile("pushfq ; popq %%rax ; btr $14,%%rax ; pushq %%rax ; popfq" ::: "eax");
269
270         syscall_init();
271
272         wrmsrl(MSR_FS_BASE, 0);
273         wrmsrl(MSR_KERNEL_GS_BASE, 0);
274         barrier(); 
275
276         rdmsrl(MSR_EFER, efer); 
277         if (!(efer & EFER_NX) || do_not_nx) { 
278                 __supported_pte_mask &= ~_PAGE_NX; 
279         }       
280
281         /*
282          * set up and load the per-CPU TSS
283          */
284         estacks += EXCEPTION_STKSZ;
285         for (v = 0; v < N_EXCEPTION_STACKS; v++) {
286                 t->ist[v] = (unsigned long)estacks;
287                 estacks += EXCEPTION_STKSZ;
288         }
289
290         t->io_map_base = INVALID_IO_BITMAP_OFFSET;
291
292         atomic_inc(&init_mm.mm_count);
293         me->active_mm = &init_mm;
294         if (me->mm)
295                 BUG();
296         enter_lazy_tlb(&init_mm, me);
297
298         set_tss_desc(cpu, t);
299         load_TR_desc();
300         load_LDT(&init_mm.context);
301
302         /*
303          * Clear all 6 debug registers:
304          */
305
306         set_debug(0UL, 0);
307         set_debug(0UL, 1);
308         set_debug(0UL, 2);
309         set_debug(0UL, 3);
310         set_debug(0UL, 6);
311         set_debug(0UL, 7);
312
313         fpu_init(); 
314 }