]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/cpu/common.c
x86/tlb: fall back to flush all when meet a THP large page
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / cpu / common.c
CommitLineData
f0fc4aff 1#include <linux/bootmem.h>
9766cdbc 2#include <linux/linkage.h>
f0fc4aff 3#include <linux/bitops.h>
9766cdbc 4#include <linux/kernel.h>
f0fc4aff 5#include <linux/module.h>
9766cdbc
JSR
6#include <linux/percpu.h>
7#include <linux/string.h>
1da177e4 8#include <linux/delay.h>
9766cdbc
JSR
9#include <linux/sched.h>
10#include <linux/init.h>
11#include <linux/kgdb.h>
1da177e4 12#include <linux/smp.h>
9766cdbc
JSR
13#include <linux/io.h>
14
15#include <asm/stackprotector.h>
cdd6c482 16#include <asm/perf_event.h>
1da177e4 17#include <asm/mmu_context.h>
49d859d7 18#include <asm/archrandom.h>
9766cdbc
JSR
19#include <asm/hypervisor.h>
20#include <asm/processor.h>
f649e938 21#include <asm/debugreg.h>
9766cdbc 22#include <asm/sections.h>
8bdbd962
AC
23#include <linux/topology.h>
24#include <linux/cpumask.h>
9766cdbc 25#include <asm/pgtable.h>
60063497 26#include <linux/atomic.h>
9766cdbc
JSR
27#include <asm/proto.h>
28#include <asm/setup.h>
29#include <asm/apic.h>
30#include <asm/desc.h>
31#include <asm/i387.h>
1361b83a 32#include <asm/fpu-internal.h>
27b07da7 33#include <asm/mtrr.h>
8bdbd962 34#include <linux/numa.h>
9766cdbc
JSR
35#include <asm/asm.h>
36#include <asm/cpu.h>
a03a3e28 37#include <asm/mce.h>
9766cdbc 38#include <asm/msr.h>
8d4a4300 39#include <asm/pat.h>
e641f5f5
IM
40
41#ifdef CONFIG_X86_LOCAL_APIC
bdbcdd48 42#include <asm/uv/uv.h>
1da177e4
LT
43#endif
44
45#include "cpu.h"
46
c2d1cec1 47/* all of these masks are initialized in setup_cpu_local_masks() */
c2d1cec1 48cpumask_var_t cpu_initialized_mask;
9766cdbc
JSR
49cpumask_var_t cpu_callout_mask;
50cpumask_var_t cpu_callin_mask;
c2d1cec1
MT
51
52/* representing cpus for which sibling maps can be computed */
53cpumask_var_t cpu_sibling_setup_mask;
54
2f2f52ba 55/* correctly size the local cpu masks */
4369f1fb 56void __init setup_cpu_local_masks(void)
2f2f52ba
BG
57{
58 alloc_bootmem_cpumask_var(&cpu_initialized_mask);
59 alloc_bootmem_cpumask_var(&cpu_callin_mask);
60 alloc_bootmem_cpumask_var(&cpu_callout_mask);
61 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
62}
63
e8055139
OZ
64static void __cpuinit default_init(struct cpuinfo_x86 *c)
65{
66#ifdef CONFIG_X86_64
27c13ece 67 cpu_detect_cache_sizes(c);
e8055139
OZ
68#else
69 /* Not much we can do here... */
70 /* Check if at least it has cpuid */
71 if (c->cpuid_level == -1) {
72 /* No cpuid. It must be an ancient CPU */
73 if (c->x86 == 4)
74 strcpy(c->x86_model_id, "486");
75 else if (c->x86 == 3)
76 strcpy(c->x86_model_id, "386");
77 }
78#endif
79}
80
81static const struct cpu_dev __cpuinitconst default_cpu = {
82 .c_init = default_init,
83 .c_vendor = "Unknown",
84 .c_x86_vendor = X86_VENDOR_UNKNOWN,
85};
86
87static const struct cpu_dev *this_cpu __cpuinitdata = &default_cpu;
0a488a53 88
06deef89 89DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
950ad7ff 90#ifdef CONFIG_X86_64
06deef89
BG
91 /*
92 * We need valid kernel segments for data and code in long mode too
93 * IRET will check the segment types kkeil 2000/10/28
94 * Also sysret mandates a special GDT layout
95 *
9766cdbc 96 * TLS descriptors are currently at a different place compared to i386.
06deef89
BG
97 * Hopefully nobody expects them at a fixed place (Wine?)
98 */
1e5de182
AM
99 [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
100 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
101 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
102 [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
103 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
104 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
950ad7ff 105#else
1e5de182
AM
106 [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
107 [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
108 [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
109 [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
bf504672
RR
110 /*
111 * Segments used for calling PnP BIOS have byte granularity.
112 * They code segments and data segments have fixed 64k limits,
113 * the transfer segment sizes are set at run time.
114 */
6842ef0e 115 /* 32-bit code */
1e5de182 116 [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
6842ef0e 117 /* 16-bit code */
1e5de182 118 [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 119 /* 16-bit data */
1e5de182 120 [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff),
6842ef0e 121 /* 16-bit data */
1e5de182 122 [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0),
6842ef0e 123 /* 16-bit data */
1e5de182 124 [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0),
bf504672
RR
125 /*
126 * The APM segments have byte granularity and their bases
127 * are set at run time. All have 64k limits.
128 */
6842ef0e 129 /* 32-bit code */
1e5de182 130 [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff),
bf504672 131 /* 16-bit code */
1e5de182 132 [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e 133 /* data */
72c4d853 134 [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff),
bf504672 135
1e5de182
AM
136 [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
137 [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
60a5317f 138 GDT_STACK_CANARY_INIT
950ad7ff 139#endif
06deef89 140} };
7a61d35d 141EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
ae1ee11b 142
0c752a93
SS
143static int __init x86_xsave_setup(char *s)
144{
145 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
6bad06b7 146 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
0c752a93
SS
147 return 1;
148}
149__setup("noxsave", x86_xsave_setup);
150
6bad06b7
SS
151static int __init x86_xsaveopt_setup(char *s)
152{
153 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
154 return 1;
155}
156__setup("noxsaveopt", x86_xsaveopt_setup);
157
ba51dced 158#ifdef CONFIG_X86_32
3bc9b76b 159static int cachesize_override __cpuinitdata = -1;
3bc9b76b 160static int disable_x86_serial_nr __cpuinitdata = 1;
1da177e4 161
0a488a53
YL
162static int __init cachesize_setup(char *str)
163{
164 get_option(&str, &cachesize_override);
165 return 1;
166}
167__setup("cachesize=", cachesize_setup);
168
0a488a53
YL
169static int __init x86_fxsr_setup(char *s)
170{
171 setup_clear_cpu_cap(X86_FEATURE_FXSR);
172 setup_clear_cpu_cap(X86_FEATURE_XMM);
173 return 1;
174}
175__setup("nofxsr", x86_fxsr_setup);
176
177static int __init x86_sep_setup(char *s)
178{
179 setup_clear_cpu_cap(X86_FEATURE_SEP);
180 return 1;
181}
182__setup("nosep", x86_sep_setup);
183
184/* Standard macro to see if a specific flag is changeable */
185static inline int flag_is_changeable_p(u32 flag)
186{
187 u32 f1, f2;
188
94f6bac1
KH
189 /*
190 * Cyrix and IDT cpus allow disabling of CPUID
191 * so the code below may return different results
192 * when it is executed before and after enabling
193 * the CPUID. Add "volatile" to not allow gcc to
194 * optimize the subsequent calls to this function.
195 */
0f3fa48a
IM
196 asm volatile ("pushfl \n\t"
197 "pushfl \n\t"
198 "popl %0 \n\t"
199 "movl %0, %1 \n\t"
200 "xorl %2, %0 \n\t"
201 "pushl %0 \n\t"
202 "popfl \n\t"
203 "pushfl \n\t"
204 "popl %0 \n\t"
205 "popfl \n\t"
206
94f6bac1
KH
207 : "=&r" (f1), "=&r" (f2)
208 : "ir" (flag));
0a488a53
YL
209
210 return ((f1^f2) & flag) != 0;
211}
212
213/* Probe for the CPUID instruction */
214static int __cpuinit have_cpuid_p(void)
215{
216 return flag_is_changeable_p(X86_EFLAGS_ID);
217}
218
219static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
220{
0f3fa48a
IM
221 unsigned long lo, hi;
222
223 if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
224 return;
225
226 /* Disable processor serial number: */
227
228 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
229 lo |= 0x200000;
230 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
231
232 printk(KERN_NOTICE "CPU serial number disabled.\n");
233 clear_cpu_cap(c, X86_FEATURE_PN);
234
235 /* Disabling the serial number may affect the cpuid level */
236 c->cpuid_level = cpuid_eax(0);
0a488a53
YL
237}
238
239static int __init x86_serial_nr_setup(char *s)
240{
241 disable_x86_serial_nr = 0;
242 return 1;
243}
244__setup("serialnumber", x86_serial_nr_setup);
ba51dced 245#else
102bbe3a
YL
246static inline int flag_is_changeable_p(u32 flag)
247{
248 return 1;
249}
ba51dced
YL
250/* Probe for the CPUID instruction */
251static inline int have_cpuid_p(void)
252{
253 return 1;
254}
102bbe3a
YL
255static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
256{
257}
ba51dced 258#endif
0a488a53 259
82da65da 260static int disable_smep __cpuinitdata;
de5397ad
FY
261static __init int setup_disable_smep(char *arg)
262{
263 disable_smep = 1;
264 return 1;
265}
266__setup("nosmep", setup_disable_smep);
267
82da65da 268static __cpuinit void setup_smep(struct cpuinfo_x86 *c)
de5397ad
FY
269{
270 if (cpu_has(c, X86_FEATURE_SMEP)) {
271 if (unlikely(disable_smep)) {
272 setup_clear_cpu_cap(X86_FEATURE_SMEP);
273 clear_in_cr4(X86_CR4_SMEP);
274 } else
275 set_in_cr4(X86_CR4_SMEP);
276 }
277}
278
b38b0665
PA
279/*
280 * Some CPU features depend on higher CPUID levels, which may not always
281 * be available due to CPUID level capping or broken virtualization
282 * software. Add those features to this table to auto-disable them.
283 */
284struct cpuid_dependent_feature {
285 u32 feature;
286 u32 level;
287};
0f3fa48a 288
b38b0665
PA
289static const struct cpuid_dependent_feature __cpuinitconst
290cpuid_dependent_features[] = {
291 { X86_FEATURE_MWAIT, 0x00000005 },
292 { X86_FEATURE_DCA, 0x00000009 },
293 { X86_FEATURE_XSAVE, 0x0000000d },
294 { 0, 0 }
295};
296
297static void __cpuinit filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
298{
299 const struct cpuid_dependent_feature *df;
9766cdbc 300
b38b0665 301 for (df = cpuid_dependent_features; df->feature; df++) {
0f3fa48a
IM
302
303 if (!cpu_has(c, df->feature))
304 continue;
b38b0665
PA
305 /*
306 * Note: cpuid_level is set to -1 if unavailable, but
307 * extended_extended_level is set to 0 if unavailable
308 * and the legitimate extended levels are all negative
309 * when signed; hence the weird messing around with
310 * signs here...
311 */
0f3fa48a 312 if (!((s32)df->level < 0 ?
f6db44df 313 (u32)df->level > (u32)c->extended_cpuid_level :
0f3fa48a
IM
314 (s32)df->level > (s32)c->cpuid_level))
315 continue;
316
317 clear_cpu_cap(c, df->feature);
318 if (!warn)
319 continue;
320
321 printk(KERN_WARNING
322 "CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
323 x86_cap_flags[df->feature], df->level);
b38b0665 324 }
f6db44df 325}
b38b0665 326
102bbe3a
YL
327/*
328 * Naming convention should be: <Name> [(<Codename>)]
329 * This table only is used unless init_<vendor>() below doesn't set it;
0f3fa48a
IM
330 * in particular, if CPUID levels 0x80000002..4 are supported, this
331 * isn't used
102bbe3a
YL
332 */
333
334/* Look up CPU names by table lookup. */
02dde8b4 335static const char *__cpuinit table_lookup_model(struct cpuinfo_x86 *c)
102bbe3a 336{
02dde8b4 337 const struct cpu_model_info *info;
102bbe3a
YL
338
339 if (c->x86_model >= 16)
340 return NULL; /* Range check */
341
342 if (!this_cpu)
343 return NULL;
344
345 info = this_cpu->c_models;
346
347 while (info && info->family) {
348 if (info->family == c->x86)
349 return info->model_names[c->x86_model];
350 info++;
351 }
352 return NULL; /* Not found */
353}
354
3e0c3737
YL
355__u32 cpu_caps_cleared[NCAPINTS] __cpuinitdata;
356__u32 cpu_caps_set[NCAPINTS] __cpuinitdata;
7d851c8d 357
11e3a840
JF
358void load_percpu_segment(int cpu)
359{
360#ifdef CONFIG_X86_32
361 loadsegment(fs, __KERNEL_PERCPU);
362#else
363 loadsegment(gs, 0);
364 wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
365#endif
60a5317f 366 load_stack_canary_segment();
11e3a840
JF
367}
368
0f3fa48a
IM
369/*
370 * Current gdt points %fs at the "master" per-cpu area: after this,
371 * it's on the real one.
372 */
552be871 373void switch_to_new_gdt(int cpu)
9d31d35b
YL
374{
375 struct desc_ptr gdt_descr;
376
2697fbd5 377 gdt_descr.address = (long)get_cpu_gdt_table(cpu);
9d31d35b
YL
378 gdt_descr.size = GDT_SIZE - 1;
379 load_gdt(&gdt_descr);
2697fbd5 380 /* Reload the per-cpu base */
11e3a840
JF
381
382 load_percpu_segment(cpu);
9d31d35b
YL
383}
384
02dde8b4 385static const struct cpu_dev *__cpuinitdata cpu_devs[X86_VENDOR_NUM] = {};
1da177e4 386
1b05d60d 387static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
1da177e4
LT
388{
389 unsigned int *v;
390 char *p, *q;
391
3da99c97 392 if (c->extended_cpuid_level < 0x80000004)
1b05d60d 393 return;
1da177e4 394
0f3fa48a 395 v = (unsigned int *)c->x86_model_id;
1da177e4
LT
396 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
397 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
398 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
399 c->x86_model_id[48] = 0;
400
0f3fa48a
IM
401 /*
402 * Intel chips right-justify this string for some dumb reason;
403 * undo that brain damage:
404 */
1da177e4 405 p = q = &c->x86_model_id[0];
34048c9e 406 while (*p == ' ')
9766cdbc 407 p++;
34048c9e 408 if (p != q) {
9766cdbc
JSR
409 while (*p)
410 *q++ = *p++;
411 while (q <= &c->x86_model_id[48])
412 *q++ = '\0'; /* Zero-pad the rest */
1da177e4 413 }
1da177e4
LT
414}
415
27c13ece 416void __cpuinit cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
1da177e4 417{
9d31d35b 418 unsigned int n, dummy, ebx, ecx, edx, l2size;
1da177e4 419
3da99c97 420 n = c->extended_cpuid_level;
1da177e4
LT
421
422 if (n >= 0x80000005) {
9d31d35b 423 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
9d31d35b 424 c->x86_cache_size = (ecx>>24) + (edx>>24);
140fc727
YL
425#ifdef CONFIG_X86_64
426 /* On K8 L1 TLB is inclusive, so don't count it */
427 c->x86_tlbsize = 0;
428#endif
1da177e4
LT
429 }
430
431 if (n < 0x80000006) /* Some chips just has a large L1. */
432 return;
433
0a488a53 434 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
1da177e4 435 l2size = ecx >> 16;
34048c9e 436
140fc727
YL
437#ifdef CONFIG_X86_64
438 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
439#else
1da177e4
LT
440 /* do processor-specific cache resizing */
441 if (this_cpu->c_size_cache)
34048c9e 442 l2size = this_cpu->c_size_cache(c, l2size);
1da177e4
LT
443
444 /* Allow user to override all this if necessary. */
445 if (cachesize_override != -1)
446 l2size = cachesize_override;
447
34048c9e 448 if (l2size == 0)
1da177e4 449 return; /* Again, no L2 cache is possible */
140fc727 450#endif
1da177e4
LT
451
452 c->x86_cache_size = l2size;
1da177e4
LT
453}
454
e0ba94f1
AS
455u16 __read_mostly tlb_lli_4k[NR_INFO];
456u16 __read_mostly tlb_lli_2m[NR_INFO];
457u16 __read_mostly tlb_lli_4m[NR_INFO];
458u16 __read_mostly tlb_lld_4k[NR_INFO];
459u16 __read_mostly tlb_lld_2m[NR_INFO];
460u16 __read_mostly tlb_lld_4m[NR_INFO];
461
462void __cpuinit cpu_detect_tlb(struct cpuinfo_x86 *c)
463{
464 if (this_cpu->c_detect_tlb)
465 this_cpu->c_detect_tlb(c);
466
467 printk(KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n" \
468 "Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d\n",
469 tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
470 tlb_lli_4m[ENTRIES], tlb_lld_4k[ENTRIES],
471 tlb_lld_2m[ENTRIES], tlb_lld_4m[ENTRIES]);
472}
473
9d31d35b 474void __cpuinit detect_ht(struct cpuinfo_x86 *c)
1da177e4 475{
97e4db7c 476#ifdef CONFIG_X86_HT
0a488a53
YL
477 u32 eax, ebx, ecx, edx;
478 int index_msb, core_bits;
2eaad1fd 479 static bool printed;
1da177e4 480
0a488a53 481 if (!cpu_has(c, X86_FEATURE_HT))
9d31d35b 482 return;
1da177e4 483
0a488a53
YL
484 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
485 goto out;
1da177e4 486
1cd78776
YL
487 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
488 return;
1da177e4 489
0a488a53 490 cpuid(1, &eax, &ebx, &ecx, &edx);
1da177e4 491
9d31d35b
YL
492 smp_num_siblings = (ebx & 0xff0000) >> 16;
493
494 if (smp_num_siblings == 1) {
2eaad1fd 495 printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled\n");
0f3fa48a
IM
496 goto out;
497 }
9d31d35b 498
0f3fa48a
IM
499 if (smp_num_siblings <= 1)
500 goto out;
9d31d35b 501
0f3fa48a
IM
502 index_msb = get_count_order(smp_num_siblings);
503 c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
9d31d35b 504
0f3fa48a 505 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
9d31d35b 506
0f3fa48a 507 index_msb = get_count_order(smp_num_siblings);
9d31d35b 508
0f3fa48a 509 core_bits = get_count_order(c->x86_max_cores);
9d31d35b 510
0f3fa48a
IM
511 c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
512 ((1 << core_bits) - 1);
1da177e4 513
0a488a53 514out:
2eaad1fd 515 if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
0a488a53
YL
516 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
517 c->phys_proc_id);
518 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
519 c->cpu_core_id);
2eaad1fd 520 printed = 1;
9d31d35b 521 }
9d31d35b 522#endif
97e4db7c 523}
1da177e4 524
3da99c97 525static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
1da177e4
LT
526{
527 char *v = c->x86_vendor_id;
0f3fa48a 528 int i;
1da177e4
LT
529
530 for (i = 0; i < X86_VENDOR_NUM; i++) {
10a434fc
YL
531 if (!cpu_devs[i])
532 break;
533
534 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
535 (cpu_devs[i]->c_ident[1] &&
536 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
0f3fa48a 537
10a434fc
YL
538 this_cpu = cpu_devs[i];
539 c->x86_vendor = this_cpu->c_x86_vendor;
540 return;
1da177e4
LT
541 }
542 }
10a434fc 543
a9c56953
MK
544 printk_once(KERN_ERR
545 "CPU: vendor_id '%s' unknown, using generic init.\n" \
546 "CPU: Your system may be unstable.\n", v);
10a434fc 547
fe38d855
CE
548 c->x86_vendor = X86_VENDOR_UNKNOWN;
549 this_cpu = &default_cpu;
1da177e4
LT
550}
551
9d31d35b 552void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
1da177e4 553{
1da177e4 554 /* Get vendor name */
4a148513
HH
555 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
556 (unsigned int *)&c->x86_vendor_id[0],
557 (unsigned int *)&c->x86_vendor_id[8],
558 (unsigned int *)&c->x86_vendor_id[4]);
1da177e4 559
1da177e4 560 c->x86 = 4;
9d31d35b 561 /* Intel-defined flags: level 0x00000001 */
1da177e4
LT
562 if (c->cpuid_level >= 0x00000001) {
563 u32 junk, tfms, cap0, misc;
0f3fa48a 564
1da177e4 565 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
9d31d35b
YL
566 c->x86 = (tfms >> 8) & 0xf;
567 c->x86_model = (tfms >> 4) & 0xf;
568 c->x86_mask = tfms & 0xf;
0f3fa48a 569
f5f786d0 570 if (c->x86 == 0xf)
1da177e4 571 c->x86 += (tfms >> 20) & 0xff;
f5f786d0 572 if (c->x86 >= 0x6)
9d31d35b 573 c->x86_model += ((tfms >> 16) & 0xf) << 4;
0f3fa48a 574
d4387bd3 575 if (cap0 & (1<<19)) {
d4387bd3 576 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
9d31d35b 577 c->x86_cache_alignment = c->x86_clflush_size;
d4387bd3 578 }
1da177e4 579 }
1da177e4 580}
3da99c97 581
d900329e 582void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
093af8d7
YL
583{
584 u32 tfms, xlvl;
3da99c97 585 u32 ebx;
093af8d7 586
3da99c97
YL
587 /* Intel-defined flags: level 0x00000001 */
588 if (c->cpuid_level >= 0x00000001) {
589 u32 capability, excap;
0f3fa48a 590
3da99c97
YL
591 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
592 c->x86_capability[0] = capability;
593 c->x86_capability[4] = excap;
594 }
093af8d7 595
bdc802dc
PA
596 /* Additional Intel-defined flags: level 0x00000007 */
597 if (c->cpuid_level >= 0x00000007) {
598 u32 eax, ebx, ecx, edx;
599
600 cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
601
2494b030 602 c->x86_capability[9] = ebx;
bdc802dc
PA
603 }
604
3da99c97
YL
605 /* AMD-defined flags: level 0x80000001 */
606 xlvl = cpuid_eax(0x80000000);
607 c->extended_cpuid_level = xlvl;
0f3fa48a 608
3da99c97
YL
609 if ((xlvl & 0xffff0000) == 0x80000000) {
610 if (xlvl >= 0x80000001) {
611 c->x86_capability[1] = cpuid_edx(0x80000001);
612 c->x86_capability[6] = cpuid_ecx(0x80000001);
093af8d7 613 }
093af8d7 614 }
093af8d7 615
5122c890
YL
616 if (c->extended_cpuid_level >= 0x80000008) {
617 u32 eax = cpuid_eax(0x80000008);
618
619 c->x86_virt_bits = (eax >> 8) & 0xff;
620 c->x86_phys_bits = eax & 0xff;
093af8d7 621 }
13c6c532
JB
622#ifdef CONFIG_X86_32
623 else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
624 c->x86_phys_bits = 36;
5122c890 625#endif
e3224234
YL
626
627 if (c->extended_cpuid_level >= 0x80000007)
628 c->x86_power = cpuid_edx(0x80000007);
093af8d7 629
1dedefd1 630 init_scattered_cpuid_features(c);
093af8d7 631}
1da177e4 632
aef93c8b
YL
633static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
634{
635#ifdef CONFIG_X86_32
636 int i;
637
638 /*
639 * First of all, decide if this is a 486 or higher
640 * It's a 486 if we can modify the AC flag
641 */
642 if (flag_is_changeable_p(X86_EFLAGS_AC))
643 c->x86 = 4;
644 else
645 c->x86 = 3;
646
647 for (i = 0; i < X86_VENDOR_NUM; i++)
648 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
649 c->x86_vendor_id[0] = 0;
650 cpu_devs[i]->c_identify(c);
651 if (c->x86_vendor_id[0]) {
652 get_cpu_vendor(c);
653 break;
654 }
655 }
656#endif
657}
658
34048c9e
PC
659/*
660 * Do minimum CPU detection early.
661 * Fields really needed: vendor, cpuid_level, family, model, mask,
662 * cache alignment.
663 * The others are not touched to avoid unwanted side effects.
664 *
665 * WARNING: this function is only called on the BP. Don't add code here
666 * that is supposed to run on all CPUs.
667 */
3da99c97 668static void __init early_identify_cpu(struct cpuinfo_x86 *c)
d7cd5611 669{
6627d242
YL
670#ifdef CONFIG_X86_64
671 c->x86_clflush_size = 64;
13c6c532
JB
672 c->x86_phys_bits = 36;
673 c->x86_virt_bits = 48;
6627d242 674#else
d4387bd3 675 c->x86_clflush_size = 32;
13c6c532
JB
676 c->x86_phys_bits = 32;
677 c->x86_virt_bits = 32;
6627d242 678#endif
0a488a53 679 c->x86_cache_alignment = c->x86_clflush_size;
d7cd5611 680
3da99c97 681 memset(&c->x86_capability, 0, sizeof c->x86_capability);
0a488a53 682 c->extended_cpuid_level = 0;
d7cd5611 683
aef93c8b
YL
684 if (!have_cpuid_p())
685 identify_cpu_without_cpuid(c);
686
687 /* cyrix could have cpuid enabled via c_identify()*/
d7cd5611
RR
688 if (!have_cpuid_p())
689 return;
690
691 cpu_detect(c);
692
3da99c97 693 get_cpu_vendor(c);
2b16a235 694
3da99c97 695 get_cpu_cap(c);
12cf105c 696
10a434fc
YL
697 if (this_cpu->c_early_init)
698 this_cpu->c_early_init(c);
093af8d7 699
f6e9456c 700 c->cpu_index = 0;
b38b0665 701 filter_cpuid_features(c, false);
de5397ad
FY
702
703 setup_smep(c);
a110b5ec
BP
704
705 if (this_cpu->c_bsp_init)
706 this_cpu->c_bsp_init(c);
d7cd5611
RR
707}
708
9d31d35b
YL
709void __init early_cpu_init(void)
710{
02dde8b4 711 const struct cpu_dev *const *cdev;
10a434fc
YL
712 int count = 0;
713
ac23f253 714#ifdef CONFIG_PROCESSOR_SELECT
9766cdbc 715 printk(KERN_INFO "KERNEL supported cpus:\n");
31c997ca
IM
716#endif
717
10a434fc 718 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
02dde8b4 719 const struct cpu_dev *cpudev = *cdev;
9d31d35b 720
10a434fc
YL
721 if (count >= X86_VENDOR_NUM)
722 break;
723 cpu_devs[count] = cpudev;
724 count++;
725
ac23f253 726#ifdef CONFIG_PROCESSOR_SELECT
31c997ca
IM
727 {
728 unsigned int j;
729
730 for (j = 0; j < 2; j++) {
731 if (!cpudev->c_ident[j])
732 continue;
733 printk(KERN_INFO " %s %s\n", cpudev->c_vendor,
734 cpudev->c_ident[j]);
735 }
10a434fc 736 }
0388423d 737#endif
10a434fc 738 }
9d31d35b 739 early_identify_cpu(&boot_cpu_data);
d7cd5611 740}
093af8d7 741
b6734c35 742/*
366d4a43
BP
743 * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
744 * unfortunately, that's not true in practice because of early VIA
745 * chips and (more importantly) broken virtualizers that are not easy
746 * to detect. In the latter case it doesn't even *fail* reliably, so
747 * probing for it doesn't even work. Disable it completely on 32-bit
ba0593bf 748 * unless we can find a reliable way to detect all the broken cases.
366d4a43 749 * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
b6734c35
PA
750 */
751static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
752{
366d4a43 753#ifdef CONFIG_X86_32
b6734c35 754 clear_cpu_cap(c, X86_FEATURE_NOPL);
366d4a43
BP
755#else
756 set_cpu_cap(c, X86_FEATURE_NOPL);
757#endif
d7cd5611
RR
758}
759
34048c9e 760static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
1da177e4 761{
aef93c8b 762 c->extended_cpuid_level = 0;
1da177e4 763
3da99c97 764 if (!have_cpuid_p())
aef93c8b 765 identify_cpu_without_cpuid(c);
1d67953f 766
aef93c8b 767 /* cyrix could have cpuid enabled via c_identify()*/
a9853dd6 768 if (!have_cpuid_p())
aef93c8b 769 return;
1da177e4 770
3da99c97 771 cpu_detect(c);
1da177e4 772
3da99c97 773 get_cpu_vendor(c);
1da177e4 774
3da99c97 775 get_cpu_cap(c);
1da177e4 776
3da99c97
YL
777 if (c->cpuid_level >= 0x00000001) {
778 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
b89d3b3e
YL
779#ifdef CONFIG_X86_32
780# ifdef CONFIG_X86_HT
cb8cc442 781 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
b89d3b3e 782# else
3da99c97 783 c->apicid = c->initial_apicid;
b89d3b3e
YL
784# endif
785#endif
b89d3b3e 786 c->phys_proc_id = c->initial_apicid;
3da99c97 787 }
1da177e4 788
de5397ad
FY
789 setup_smep(c);
790
1b05d60d 791 get_model_name(c); /* Default name */
1da177e4 792
3da99c97 793 detect_nopl(c);
1da177e4 794}
1da177e4
LT
795
796/*
797 * This does the hard work of actually picking apart the CPU stuff...
798 */
9a250347 799static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
1da177e4
LT
800{
801 int i;
802
803 c->loops_per_jiffy = loops_per_jiffy;
804 c->x86_cache_size = -1;
805 c->x86_vendor = X86_VENDOR_UNKNOWN;
1da177e4
LT
806 c->x86_model = c->x86_mask = 0; /* So far unknown... */
807 c->x86_vendor_id[0] = '\0'; /* Unset */
808 c->x86_model_id[0] = '\0'; /* Unset */
94605eff 809 c->x86_max_cores = 1;
102bbe3a 810 c->x86_coreid_bits = 0;
11fdd252 811#ifdef CONFIG_X86_64
102bbe3a 812 c->x86_clflush_size = 64;
13c6c532
JB
813 c->x86_phys_bits = 36;
814 c->x86_virt_bits = 48;
102bbe3a
YL
815#else
816 c->cpuid_level = -1; /* CPUID not detected */
770d132f 817 c->x86_clflush_size = 32;
13c6c532
JB
818 c->x86_phys_bits = 32;
819 c->x86_virt_bits = 32;
102bbe3a
YL
820#endif
821 c->x86_cache_alignment = c->x86_clflush_size;
1da177e4
LT
822 memset(&c->x86_capability, 0, sizeof c->x86_capability);
823
1da177e4
LT
824 generic_identify(c);
825
3898534d 826 if (this_cpu->c_identify)
1da177e4
LT
827 this_cpu->c_identify(c);
828
2759c328
YL
829 /* Clear/Set all flags overriden by options, after probe */
830 for (i = 0; i < NCAPINTS; i++) {
831 c->x86_capability[i] &= ~cpu_caps_cleared[i];
832 c->x86_capability[i] |= cpu_caps_set[i];
833 }
834
102bbe3a 835#ifdef CONFIG_X86_64
cb8cc442 836 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
102bbe3a
YL
837#endif
838
1da177e4
LT
839 /*
840 * Vendor-specific initialization. In this section we
841 * canonicalize the feature flags, meaning if there are
842 * features a certain CPU supports which CPUID doesn't
843 * tell us, CPUID claiming incorrect flags, or other bugs,
844 * we handle them here.
845 *
846 * At the end of this section, c->x86_capability better
847 * indicate the features this CPU genuinely supports!
848 */
849 if (this_cpu->c_init)
850 this_cpu->c_init(c);
851
852 /* Disable the PN if appropriate */
853 squash_the_stupid_serial_number(c);
854
855 /*
0f3fa48a
IM
856 * The vendor-specific functions might have changed features.
857 * Now we do "generic changes."
1da177e4
LT
858 */
859
b38b0665
PA
860 /* Filter out anything that depends on CPUID levels we don't have */
861 filter_cpuid_features(c, true);
862
1da177e4 863 /* If the model name is still unset, do table lookup. */
34048c9e 864 if (!c->x86_model_id[0]) {
02dde8b4 865 const char *p;
1da177e4 866 p = table_lookup_model(c);
34048c9e 867 if (p)
1da177e4
LT
868 strcpy(c->x86_model_id, p);
869 else
870 /* Last resort... */
871 sprintf(c->x86_model_id, "%02x/%02x",
54a20f8c 872 c->x86, c->x86_model);
1da177e4
LT
873 }
874
102bbe3a
YL
875#ifdef CONFIG_X86_64
876 detect_ht(c);
877#endif
878
88b094fb 879 init_hypervisor(c);
49d859d7 880 x86_init_rdrand(c);
3e0c3737
YL
881
882 /*
883 * Clear/Set all flags overriden by options, need do it
884 * before following smp all cpus cap AND.
885 */
886 for (i = 0; i < NCAPINTS; i++) {
887 c->x86_capability[i] &= ~cpu_caps_cleared[i];
888 c->x86_capability[i] |= cpu_caps_set[i];
889 }
890
1da177e4
LT
891 /*
892 * On SMP, boot_cpu_data holds the common feature set between
893 * all CPUs; so make sure that we indicate which features are
894 * common between the CPUs. The first time this routine gets
895 * executed, c == &boot_cpu_data.
896 */
34048c9e 897 if (c != &boot_cpu_data) {
1da177e4 898 /* AND the already accumulated flags with these */
9d31d35b 899 for (i = 0; i < NCAPINTS; i++)
1da177e4
LT
900 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
901 }
902
903 /* Init Machine Check Exception if available. */
5e09954a 904 mcheck_cpu_init(c);
30d432df
AK
905
906 select_idle_routine(c);
102bbe3a 907
de2d9445 908#ifdef CONFIG_NUMA
102bbe3a
YL
909 numa_add_cpu(smp_processor_id());
910#endif
a6c4e076 911}
31ab269a 912
e04d645f
GC
913#ifdef CONFIG_X86_64
914static void vgetcpu_set_mode(void)
915{
916 if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
917 vgetcpu_mode = VGETCPU_RDTSCP;
918 else
919 vgetcpu_mode = VGETCPU_LSL;
920}
921#endif
922
a6c4e076
JF
923void __init identify_boot_cpu(void)
924{
925 identify_cpu(&boot_cpu_data);
02c68a02 926 init_amd_e400_c1e_mask();
102bbe3a 927#ifdef CONFIG_X86_32
a6c4e076 928 sysenter_setup();
6fe940d6 929 enable_sep_cpu();
e04d645f
GC
930#else
931 vgetcpu_set_mode();
102bbe3a 932#endif
e0ba94f1
AS
933 if (boot_cpu_data.cpuid_level >= 2)
934 cpu_detect_tlb(&boot_cpu_data);
a6c4e076 935}
3b520b23 936
a6c4e076
JF
937void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
938{
939 BUG_ON(c == &boot_cpu_data);
940 identify_cpu(c);
102bbe3a 941#ifdef CONFIG_X86_32
a6c4e076 942 enable_sep_cpu();
102bbe3a 943#endif
a6c4e076 944 mtrr_ap_init();
1da177e4
LT
945}
946
a0854a46 947struct msr_range {
0f3fa48a
IM
948 unsigned min;
949 unsigned max;
a0854a46 950};
1da177e4 951
02dde8b4 952static const struct msr_range msr_range_array[] __cpuinitconst = {
a0854a46
YL
953 { 0x00000000, 0x00000418},
954 { 0xc0000000, 0xc000040b},
955 { 0xc0010000, 0xc0010142},
956 { 0xc0011000, 0xc001103b},
957};
1da177e4 958
21c3fcf3 959static void __cpuinit __print_cpu_msr(void)
a0854a46 960{
0f3fa48a 961 unsigned index_min, index_max;
a0854a46
YL
962 unsigned index;
963 u64 val;
964 int i;
a0854a46
YL
965
966 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
967 index_min = msr_range_array[i].min;
968 index_max = msr_range_array[i].max;
0f3fa48a 969
a0854a46
YL
970 for (index = index_min; index < index_max; index++) {
971 if (rdmsrl_amd_safe(index, &val))
972 continue;
973 printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
1da177e4 974 }
a0854a46
YL
975 }
976}
94605eff 977
a0854a46 978static int show_msr __cpuinitdata;
0f3fa48a 979
a0854a46
YL
980static __init int setup_show_msr(char *arg)
981{
982 int num;
3dd9d514 983
a0854a46 984 get_option(&arg, &num);
3dd9d514 985
a0854a46
YL
986 if (num > 0)
987 show_msr = num;
988 return 1;
1da177e4 989}
a0854a46 990__setup("show_msr=", setup_show_msr);
1da177e4 991
191679fd
AK
992static __init int setup_noclflush(char *arg)
993{
994 setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
995 return 1;
996}
997__setup("noclflush", setup_noclflush);
998
3bc9b76b 999void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
1da177e4 1000{
02dde8b4 1001 const char *vendor = NULL;
1da177e4 1002
0f3fa48a 1003 if (c->x86_vendor < X86_VENDOR_NUM) {
1da177e4 1004 vendor = this_cpu->c_vendor;
0f3fa48a
IM
1005 } else {
1006 if (c->cpuid_level >= 0)
1007 vendor = c->x86_vendor_id;
1008 }
1da177e4 1009
bd32a8cf 1010 if (vendor && !strstr(c->x86_model_id, vendor))
9d31d35b 1011 printk(KERN_CONT "%s ", vendor);
1da177e4 1012
9d31d35b
YL
1013 if (c->x86_model_id[0])
1014 printk(KERN_CONT "%s", c->x86_model_id);
1da177e4 1015 else
9d31d35b 1016 printk(KERN_CONT "%d86", c->x86);
1da177e4 1017
34048c9e 1018 if (c->x86_mask || c->cpuid_level >= 0)
9d31d35b 1019 printk(KERN_CONT " stepping %02x\n", c->x86_mask);
1da177e4 1020 else
9d31d35b 1021 printk(KERN_CONT "\n");
a0854a46 1022
0b8b8078 1023 print_cpu_msr(c);
21c3fcf3
YL
1024}
1025
1026void __cpuinit print_cpu_msr(struct cpuinfo_x86 *c)
1027{
a0854a46 1028 if (c->cpu_index < show_msr)
21c3fcf3 1029 __print_cpu_msr();
1da177e4
LT
1030}
1031
ac72e788
AK
1032static __init int setup_disablecpuid(char *arg)
1033{
1034 int bit;
0f3fa48a 1035
ac72e788
AK
1036 if (get_option(&arg, &bit) && bit < NCAPINTS*32)
1037 setup_clear_cpu_cap(bit);
1038 else
1039 return 0;
0f3fa48a 1040
ac72e788
AK
1041 return 1;
1042}
1043__setup("clearcpuid=", setup_disablecpuid);
1044
d5494d4f 1045#ifdef CONFIG_X86_64
9ff80942 1046struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
228bdaa9
SR
1047struct desc_ptr nmi_idt_descr = { NR_VECTORS * 16 - 1,
1048 (unsigned long) nmi_idt_table };
d5494d4f 1049
947e76cd
BG
1050DEFINE_PER_CPU_FIRST(union irq_stack_union,
1051 irq_stack_union) __aligned(PAGE_SIZE);
0f3fa48a 1052
bdf977b3
TH
1053/*
1054 * The following four percpu variables are hot. Align current_task to
1055 * cacheline size such that all four fall in the same cacheline.
1056 */
1057DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1058 &init_task;
1059EXPORT_PER_CPU_SYMBOL(current_task);
d5494d4f 1060
9af45651
BG
1061DEFINE_PER_CPU(unsigned long, kernel_stack) =
1062 (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
1063EXPORT_PER_CPU_SYMBOL(kernel_stack);
1064
bdf977b3
TH
1065DEFINE_PER_CPU(char *, irq_stack_ptr) =
1066 init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
1067
56895530 1068DEFINE_PER_CPU(unsigned int, irq_count) = -1;
d5494d4f 1069
7e16838d
LT
1070DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
1071
0f3fa48a
IM
1072/*
1073 * Special IST stacks which the CPU switches to when it calls
1074 * an IST-marked descriptor entry. Up to 7 stacks (hardware
1075 * limit), all of them are 4K, except the debug stack which
1076 * is 8K.
1077 */
1078static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
1079 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
1080 [DEBUG_STACK - 1] = DEBUG_STKSZ
1081};
1082
92d65b23 1083static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
3e352aa8 1084 [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
d5494d4f 1085
d5494d4f
YL
1086/* May not be marked __init: used by software suspend */
1087void syscall_init(void)
1da177e4 1088{
d5494d4f
YL
1089 /*
1090 * LSTAR and STAR live in a bit strange symbiosis.
1091 * They both write to the same internal register. STAR allows to
1092 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1093 */
1094 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
1095 wrmsrl(MSR_LSTAR, system_call);
1096 wrmsrl(MSR_CSTAR, ignore_sysret);
03ae5768 1097
d5494d4f
YL
1098#ifdef CONFIG_IA32_EMULATION
1099 syscall32_cpu_init();
1100#endif
03ae5768 1101
d5494d4f
YL
1102 /* Flags to clear on syscall */
1103 wrmsrl(MSR_SYSCALL_MASK,
1104 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
1da177e4 1105}
62111195 1106
d5494d4f
YL
1107unsigned long kernel_eflags;
1108
1109/*
1110 * Copies of the original ist values from the tss are only accessed during
1111 * debugging, no special alignment required.
1112 */
1113DEFINE_PER_CPU(struct orig_ist, orig_ist);
1114
228bdaa9 1115static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
42181186 1116DEFINE_PER_CPU(int, debug_stack_usage);
228bdaa9
SR
1117
1118int is_debug_stack(unsigned long addr)
1119{
42181186
SR
1120 return __get_cpu_var(debug_stack_usage) ||
1121 (addr <= __get_cpu_var(debug_stack_addr) &&
1122 addr > (__get_cpu_var(debug_stack_addr) - DEBUG_STKSZ));
228bdaa9
SR
1123}
1124
f8988175
SR
1125static DEFINE_PER_CPU(u32, debug_stack_use_ctr);
1126
228bdaa9
SR
1127void debug_stack_set_zero(void)
1128{
f8988175 1129 this_cpu_inc(debug_stack_use_ctr);
228bdaa9
SR
1130 load_idt((const struct desc_ptr *)&nmi_idt_descr);
1131}
1132
1133void debug_stack_reset(void)
1134{
f8988175
SR
1135 if (WARN_ON(!this_cpu_read(debug_stack_use_ctr)))
1136 return;
1137 if (this_cpu_dec_return(debug_stack_use_ctr) == 0)
1138 load_idt((const struct desc_ptr *)&idt_descr);
228bdaa9
SR
1139}
1140
0f3fa48a 1141#else /* CONFIG_X86_64 */
d5494d4f 1142
bdf977b3
TH
1143DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
1144EXPORT_PER_CPU_SYMBOL(current_task);
27e74da9 1145DEFINE_PER_CPU(struct task_struct *, fpu_owner_task);
bdf977b3 1146
60a5317f 1147#ifdef CONFIG_CC_STACKPROTECTOR
53f82452 1148DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
60a5317f 1149#endif
d5494d4f 1150
60a5317f 1151/* Make sure %fs and %gs are initialized properly in idle threads */
6b2fb3c6 1152struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
f95d47ca
JF
1153{
1154 memset(regs, 0, sizeof(struct pt_regs));
65ea5b03 1155 regs->fs = __KERNEL_PERCPU;
60a5317f 1156 regs->gs = __KERNEL_STACK_CANARY;
0f3fa48a 1157
f95d47ca
JF
1158 return regs;
1159}
0f3fa48a 1160#endif /* CONFIG_X86_64 */
c5413fbe 1161
9766cdbc
JSR
1162/*
1163 * Clear all 6 debug registers:
1164 */
1165static void clear_all_debug_regs(void)
1166{
1167 int i;
1168
1169 for (i = 0; i < 8; i++) {
1170 /* Ignore db4, db5 */
1171 if ((i == 4) || (i == 5))
1172 continue;
1173
1174 set_debugreg(0, i);
1175 }
1176}
c5413fbe 1177
0bb9fef9
JW
1178#ifdef CONFIG_KGDB
1179/*
1180 * Restore debug regs if using kgdbwait and you have a kernel debugger
1181 * connection established.
1182 */
1183static void dbg_restore_debug_regs(void)
1184{
1185 if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
1186 arch_kgdb_ops.correct_hw_break();
1187}
1188#else /* ! CONFIG_KGDB */
1189#define dbg_restore_debug_regs()
1190#endif /* ! CONFIG_KGDB */
1191
d2cbcc49
RR
1192/*
1193 * cpu_init() initializes state that is per-CPU. Some data is already
1194 * initialized (naturally) in the bootstrap process, such as the GDT
1195 * and IDT. We reload them nevertheless, this function acts as a
1196 * 'CPU state barrier', nothing should get across.
1ba76586 1197 * A lot of state is already set up in PDA init for 64 bit
d2cbcc49 1198 */
1ba76586 1199#ifdef CONFIG_X86_64
0f3fa48a 1200
1ba76586
YL
1201void __cpuinit cpu_init(void)
1202{
0fe1e009 1203 struct orig_ist *oist;
1ba76586 1204 struct task_struct *me;
0f3fa48a
IM
1205 struct tss_struct *t;
1206 unsigned long v;
1207 int cpu;
1ba76586
YL
1208 int i;
1209
0f3fa48a
IM
1210 cpu = stack_smp_processor_id();
1211 t = &per_cpu(init_tss, cpu);
0fe1e009 1212 oist = &per_cpu(orig_ist, cpu);
0f3fa48a 1213
e7a22c1e 1214#ifdef CONFIG_NUMA
c6ae41e7 1215 if (cpu != 0 && this_cpu_read(numa_node) == 0 &&
e534c7c5
LS
1216 early_cpu_to_node(cpu) != NUMA_NO_NODE)
1217 set_numa_node(early_cpu_to_node(cpu));
e7a22c1e 1218#endif
1ba76586
YL
1219
1220 me = current;
1221
c2d1cec1 1222 if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
1ba76586
YL
1223 panic("CPU#%d already initialized!\n", cpu);
1224
2eaad1fd 1225 pr_debug("Initializing CPU#%d\n", cpu);
1ba76586
YL
1226
1227 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1228
1229 /*
1230 * Initialize the per-CPU GDT with the boot GDT,
1231 * and set up the GDT descriptor:
1232 */
1233
552be871 1234 switch_to_new_gdt(cpu);
2697fbd5
BG
1235 loadsegment(fs, 0);
1236
1ba76586
YL
1237 load_idt((const struct desc_ptr *)&idt_descr);
1238
1239 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1240 syscall_init();
1241
1242 wrmsrl(MSR_FS_BASE, 0);
1243 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1244 barrier();
1245
4763ed4d 1246 x86_configure_nx();
06cd9a7d 1247 if (cpu != 0)
1ba76586
YL
1248 enable_x2apic();
1249
1250 /*
1251 * set up and load the per-CPU TSS
1252 */
0fe1e009 1253 if (!oist->ist[0]) {
92d65b23 1254 char *estacks = per_cpu(exception_stacks, cpu);
0f3fa48a 1255
1ba76586 1256 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
0f3fa48a 1257 estacks += exception_stack_sizes[v];
0fe1e009 1258 oist->ist[v] = t->x86_tss.ist[v] =
1ba76586 1259 (unsigned long)estacks;
228bdaa9
SR
1260 if (v == DEBUG_STACK-1)
1261 per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1ba76586
YL
1262 }
1263 }
1264
1265 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
0f3fa48a 1266
1ba76586
YL
1267 /*
1268 * <= is required because the CPU will access up to
1269 * 8 bits beyond the end of the IO permission bitmap.
1270 */
1271 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1272 t->io_bitmap[i] = ~0UL;
1273
1274 atomic_inc(&init_mm.mm_count);
1275 me->active_mm = &init_mm;
8c5dfd25 1276 BUG_ON(me->mm);
1ba76586
YL
1277 enter_lazy_tlb(&init_mm, me);
1278
1279 load_sp0(t, &current->thread);
1280 set_tss_desc(cpu, t);
1281 load_TR_desc();
1282 load_LDT(&init_mm.context);
1283
0bb9fef9
JW
1284 clear_all_debug_regs();
1285 dbg_restore_debug_regs();
1ba76586
YL
1286
1287 fpu_init();
0e49bf66 1288 xsave_init();
1ba76586
YL
1289
1290 raw_local_save_flags(kernel_eflags);
1291
1292 if (is_uv_system())
1293 uv_cpu_init();
1294}
1295
1296#else
1297
d2cbcc49 1298void __cpuinit cpu_init(void)
9ee79a3d 1299{
d2cbcc49
RR
1300 int cpu = smp_processor_id();
1301 struct task_struct *curr = current;
34048c9e 1302 struct tss_struct *t = &per_cpu(init_tss, cpu);
9ee79a3d 1303 struct thread_struct *thread = &curr->thread;
62111195 1304
c2d1cec1 1305 if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
62111195 1306 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
9766cdbc
JSR
1307 for (;;)
1308 local_irq_enable();
62111195
JF
1309 }
1310
1311 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1312
1313 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
1314 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
62111195 1315
4d37e7e3 1316 load_idt(&idt_descr);
552be871 1317 switch_to_new_gdt(cpu);
1da177e4 1318
1da177e4
LT
1319 /*
1320 * Set up and load the per-CPU TSS and LDT
1321 */
1322 atomic_inc(&init_mm.mm_count);
62111195 1323 curr->active_mm = &init_mm;
8c5dfd25 1324 BUG_ON(curr->mm);
62111195 1325 enter_lazy_tlb(&init_mm, curr);
1da177e4 1326
faca6227 1327 load_sp0(t, thread);
34048c9e 1328 set_tss_desc(cpu, t);
1da177e4
LT
1329 load_TR_desc();
1330 load_LDT(&init_mm.context);
1331
f9a196b8
TG
1332 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1333
22c4e308 1334#ifdef CONFIG_DOUBLEFAULT
1da177e4
LT
1335 /* Set up doublefault TSS pointer in the GDT */
1336 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
22c4e308 1337#endif
1da177e4 1338
9766cdbc 1339 clear_all_debug_regs();
0bb9fef9 1340 dbg_restore_debug_regs();
1da177e4 1341
0e49bf66 1342 fpu_init();
dc1e35c6 1343 xsave_init();
1da177e4 1344}
1ba76586 1345#endif