Blame view

arch/x86/kernel/cpu/common.c 31.1 KB
f0fc4aff1   Yinghai Lu   x86: make header ...
1
  #include <linux/bootmem.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
2
  #include <linux/linkage.h>
f0fc4aff1   Yinghai Lu   x86: make header ...
3
  #include <linux/bitops.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
4
  #include <linux/kernel.h>
f0fc4aff1   Yinghai Lu   x86: make header ...
5
  #include <linux/module.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
6
7
  #include <linux/percpu.h>
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
  #include <linux/delay.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
9
10
11
  #include <linux/sched.h>
  #include <linux/init.h>
  #include <linux/kgdb.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
  #include <linux/smp.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
13
14
15
  #include <linux/io.h>
  
  #include <asm/stackprotector.h>
cdd6c482c   Ingo Molnar   perf: Do the big ...
16
  #include <asm/perf_event.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <asm/mmu_context.h>
49d859d78   H. Peter Anvin   x86, random: Veri...
18
  #include <asm/archrandom.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
19
20
21
  #include <asm/hypervisor.h>
  #include <asm/processor.h>
  #include <asm/sections.h>
8bdbd962e   Alan Cox   x86/cpu: Clean up...
22
23
  #include <linux/topology.h>
  #include <linux/cpumask.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
24
  #include <asm/pgtable.h>
60063497a   Arun Sharma   atomic: use <linu...
25
  #include <linux/atomic.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
26
27
28
29
30
  #include <asm/proto.h>
  #include <asm/setup.h>
  #include <asm/apic.h>
  #include <asm/desc.h>
  #include <asm/i387.h>
27b07da73   Alexey Dobriyan   [PATCH] Don't tri...
31
  #include <asm/mtrr.h>
8bdbd962e   Alan Cox   x86/cpu: Clean up...
32
  #include <linux/numa.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
33
34
  #include <asm/asm.h>
  #include <asm/cpu.h>
a03a3e287   Alexey Dobriyan   [PATCH] Don't tri...
35
  #include <asm/mce.h>
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
36
  #include <asm/msr.h>
8d4a43008   Thomas Gleixner   x86: cleanup PAT ...
37
  #include <asm/pat.h>
e641f5f52   Ingo Molnar   x86, apic: remove...
38
39
  
  #ifdef CONFIG_X86_LOCAL_APIC
bdbcdd488   Tejun Heo   x86: uv cleanup
40
  #include <asm/uv/uv.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
  #endif
  
  #include "cpu.h"
c2d1cec1c   Mike Travis   x86: cleanup rema...
44
  /* all of these masks are initialized in setup_cpu_local_masks() */
c2d1cec1c   Mike Travis   x86: cleanup rema...
45
  cpumask_var_t cpu_initialized_mask;
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
46
47
  cpumask_var_t cpu_callout_mask;
  cpumask_var_t cpu_callin_mask;
c2d1cec1c   Mike Travis   x86: cleanup rema...
48
49
50
  
  /* representing cpus for which sibling maps can be computed */
  cpumask_var_t cpu_sibling_setup_mask;
2f2f52bad   Brian Gerst   x86: move setup_c...
51
  /* correctly size the local cpu masks */
4369f1fb7   Ingo Molnar   Merge branch 'tj-...
52
  void __init setup_cpu_local_masks(void)
2f2f52bad   Brian Gerst   x86: move setup_c...
53
54
55
56
57
58
  {
  	alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  	alloc_bootmem_cpumask_var(&cpu_callin_mask);
  	alloc_bootmem_cpumask_var(&cpu_callout_mask);
  	alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  }
e8055139d   Ondrej Zary   x86: Fix oops in ...
59
60
61
  static void __cpuinit default_init(struct cpuinfo_x86 *c)
  {
  #ifdef CONFIG_X86_64
27c13ecec   Borislav Petkov   x86, cpu: mv disp...
62
  	cpu_detect_cache_sizes(c);
e8055139d   Ondrej Zary   x86: Fix oops in ...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #else
  	/* Not much we can do here... */
  	/* Check if at least it has cpuid */
  	if (c->cpuid_level == -1) {
  		/* No cpuid. It must be an ancient CPU */
  		if (c->x86 == 4)
  			strcpy(c->x86_model_id, "486");
  		else if (c->x86 == 3)
  			strcpy(c->x86_model_id, "386");
  	}
  #endif
  }
  
  static const struct cpu_dev __cpuinitconst default_cpu = {
  	.c_init		= default_init,
  	.c_vendor	= "Unknown",
  	.c_x86_vendor	= X86_VENDOR_UNKNOWN,
  };
  
  static const struct cpu_dev *this_cpu __cpuinitdata = &default_cpu;
0a488a53d   Yinghai Lu   x86: move 32bit r...
83

06deef892   Brian Gerst   x86: clean up gdt...
84
  DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
950ad7ff6   Yinghai Lu   x86: same gdt_pag...
85
  #ifdef CONFIG_X86_64
06deef892   Brian Gerst   x86: clean up gdt...
86
87
88
89
90
  	/*
  	 * We need valid kernel segments for data and code in long mode too
  	 * IRET will check the segment types  kkeil 2000/10/28
  	 * Also sysret mandates a special GDT layout
  	 *
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
91
  	 * TLS descriptors are currently at a different place compared to i386.
06deef892   Brian Gerst   x86: clean up gdt...
92
93
  	 * Hopefully nobody expects them at a fixed place (Wine?)
  	 */
1e5de1827   Akinobu Mita   x86: Introduce GD...
94
95
96
97
98
99
  	[GDT_ENTRY_KERNEL32_CS]		= GDT_ENTRY_INIT(0xc09b, 0, 0xfffff),
  	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xa09b, 0, 0xfffff),
  	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc093, 0, 0xfffff),
  	[GDT_ENTRY_DEFAULT_USER32_CS]	= GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff),
  	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff),
  	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff),
950ad7ff6   Yinghai Lu   x86: same gdt_pag...
100
  #else
1e5de1827   Akinobu Mita   x86: Introduce GD...
101
102
103
104
  	[GDT_ENTRY_KERNEL_CS]		= GDT_ENTRY_INIT(0xc09a, 0, 0xfffff),
  	[GDT_ENTRY_KERNEL_DS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  	[GDT_ENTRY_DEFAULT_USER_CS]	= GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff),
  	[GDT_ENTRY_DEFAULT_USER_DS]	= GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff),
bf5046720   Rusty Russell   [PATCH] i386: Use...
105
106
107
108
109
  	/*
  	 * Segments used for calling PnP BIOS have byte granularity.
  	 * They code segments and data segments have fixed 64k limits,
  	 * the transfer segment sizes are set at run time.
  	 */
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
110
  	/* 32-bit code */
1e5de1827   Akinobu Mita   x86: Introduce GD...
111
  	[GDT_ENTRY_PNPBIOS_CS32]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
112
  	/* 16-bit code */
1e5de1827   Akinobu Mita   x86: Introduce GD...
113
  	[GDT_ENTRY_PNPBIOS_CS16]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
114
  	/* 16-bit data */
1e5de1827   Akinobu Mita   x86: Introduce GD...
115
  	[GDT_ENTRY_PNPBIOS_DS]		= GDT_ENTRY_INIT(0x0092, 0, 0xffff),
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
116
  	/* 16-bit data */
1e5de1827   Akinobu Mita   x86: Introduce GD...
117
  	[GDT_ENTRY_PNPBIOS_TS1]		= GDT_ENTRY_INIT(0x0092, 0, 0),
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
118
  	/* 16-bit data */
1e5de1827   Akinobu Mita   x86: Introduce GD...
119
  	[GDT_ENTRY_PNPBIOS_TS2]		= GDT_ENTRY_INIT(0x0092, 0, 0),
bf5046720   Rusty Russell   [PATCH] i386: Use...
120
121
122
123
  	/*
  	 * The APM segments have byte granularity and their bases
  	 * are set at run time.  All have 64k limits.
  	 */
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
124
  	/* 32-bit code */
1e5de1827   Akinobu Mita   x86: Introduce GD...
125
  	[GDT_ENTRY_APMBIOS_BASE]	= GDT_ENTRY_INIT(0x409a, 0, 0xffff),
bf5046720   Rusty Russell   [PATCH] i386: Use...
126
  	/* 16-bit code */
1e5de1827   Akinobu Mita   x86: Introduce GD...
127
  	[GDT_ENTRY_APMBIOS_BASE+1]	= GDT_ENTRY_INIT(0x009a, 0, 0xffff),
6842ef0e8   Glauber de Oliveira Costa   x86: unify desc_s...
128
  	/* data */
72c4d8530   Ingo Molnar   x86: Introduce GD...
129
  	[GDT_ENTRY_APMBIOS_BASE+2]	= GDT_ENTRY_INIT(0x4092, 0, 0xffff),
bf5046720   Rusty Russell   [PATCH] i386: Use...
130

1e5de1827   Akinobu Mita   x86: Introduce GD...
131
132
  	[GDT_ENTRY_ESPFIX_SS]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
  	[GDT_ENTRY_PERCPU]		= GDT_ENTRY_INIT(0xc092, 0, 0xfffff),
60a5317ff   Tejun Heo   x86: implement x8...
133
  	GDT_STACK_CANARY_INIT
950ad7ff6   Yinghai Lu   x86: same gdt_pag...
134
  #endif
06deef892   Brian Gerst   x86: clean up gdt...
135
  } };
7a61d35d4   Jeremy Fitzhardinge   [PATCH] i386: Pag...
136
  EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
ae1ee11be   Rusty Russell   [PATCH] i386: Use...
137

0c752a933   Suresh Siddha   x86: introduce no...
138
139
140
  static int __init x86_xsave_setup(char *s)
  {
  	setup_clear_cpu_cap(X86_FEATURE_XSAVE);
6bad06b76   Suresh Siddha   x86, xsave: Use x...
141
  	setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
0c752a933   Suresh Siddha   x86: introduce no...
142
143
144
  	return 1;
  }
  __setup("noxsave", x86_xsave_setup);
6bad06b76   Suresh Siddha   x86, xsave: Use x...
145
146
147
148
149
150
  static int __init x86_xsaveopt_setup(char *s)
  {
  	setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  	return 1;
  }
  __setup("noxsaveopt", x86_xsaveopt_setup);
ba51dced0   Yinghai Lu   x86: cpu/common.c...
151
  #ifdef CONFIG_X86_32
3bc9b76be   Chuck Ebbert   [PATCH] i386: __d...
152
  static int cachesize_override __cpuinitdata = -1;
3bc9b76be   Chuck Ebbert   [PATCH] i386: __d...
153
  static int disable_x86_serial_nr __cpuinitdata = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154

0a488a53d   Yinghai Lu   x86: move 32bit r...
155
156
157
158
159
160
  static int __init cachesize_setup(char *str)
  {
  	get_option(&str, &cachesize_override);
  	return 1;
  }
  __setup("cachesize=", cachesize_setup);
0a488a53d   Yinghai Lu   x86: move 32bit r...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  static int __init x86_fxsr_setup(char *s)
  {
  	setup_clear_cpu_cap(X86_FEATURE_FXSR);
  	setup_clear_cpu_cap(X86_FEATURE_XMM);
  	return 1;
  }
  __setup("nofxsr", x86_fxsr_setup);
  
  static int __init x86_sep_setup(char *s)
  {
  	setup_clear_cpu_cap(X86_FEATURE_SEP);
  	return 1;
  }
  __setup("nosep", x86_sep_setup);
  
  /* Standard macro to see if a specific flag is changeable */
  static inline int flag_is_changeable_p(u32 flag)
  {
  	u32 f1, f2;
94f6bac10   Krzysztof Helt   x86: do not allow...
180
181
182
183
184
185
186
  	/*
  	 * Cyrix and IDT cpus allow disabling of CPUID
  	 * so the code below may return different results
  	 * when it is executed before and after enabling
  	 * the CPUID. Add "volatile" to not allow gcc to
  	 * optimize the subsequent calls to this function.
  	 */
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  	asm volatile ("pushfl		
  \t"
  		      "pushfl		
  \t"
  		      "popl %0		
  \t"
  		      "movl %0, %1	
  \t"
  		      "xorl %2, %0	
  \t"
  		      "pushl %0		
  \t"
  		      "popfl		
  \t"
  		      "pushfl		
  \t"
  		      "popl %0		
  \t"
  		      "popfl		
  \t"
94f6bac10   Krzysztof Helt   x86: do not allow...
207
208
  		      : "=&r" (f1), "=&r" (f2)
  		      : "ir" (flag));
0a488a53d   Yinghai Lu   x86: move 32bit r...
209
210
211
212
213
214
215
216
217
218
219
220
  
  	return ((f1^f2) & flag) != 0;
  }
  
  /* Probe for the CPUID instruction */
  static int __cpuinit have_cpuid_p(void)
  {
  	return flag_is_changeable_p(X86_EFLAGS_ID);
  }
  
  static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  	unsigned long lo, hi;
  
  	if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
  		return;
  
  	/* Disable processor serial number: */
  
  	rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  	lo |= 0x200000;
  	wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  
  	printk(KERN_NOTICE "CPU serial number disabled.
  ");
  	clear_cpu_cap(c, X86_FEATURE_PN);
  
  	/* Disabling the serial number may affect the cpuid level */
  	c->cpuid_level = cpuid_eax(0);
0a488a53d   Yinghai Lu   x86: move 32bit r...
238
239
240
241
242
243
244
245
  }
  
  static int __init x86_serial_nr_setup(char *s)
  {
  	disable_x86_serial_nr = 0;
  	return 1;
  }
  __setup("serialnumber", x86_serial_nr_setup);
ba51dced0   Yinghai Lu   x86: cpu/common.c...
246
  #else
102bbe3ab   Yinghai Lu   x86: cpu/common*....
247
248
249
250
  static inline int flag_is_changeable_p(u32 flag)
  {
  	return 1;
  }
ba51dced0   Yinghai Lu   x86: cpu/common.c...
251
252
253
254
255
  /* Probe for the CPUID instruction */
  static inline int have_cpuid_p(void)
  {
  	return 1;
  }
102bbe3ab   Yinghai Lu   x86: cpu/common*....
256
257
258
  static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  {
  }
ba51dced0   Yinghai Lu   x86: cpu/common.c...
259
  #endif
0a488a53d   Yinghai Lu   x86: move 32bit r...
260

82da65dab   Linus Torvalds   x86: setup_smep n...
261
  static int disable_smep __cpuinitdata;
de5397ad5   Fenghua Yu   x86, cpu: Enable/...
262
263
264
265
266
267
  static __init int setup_disable_smep(char *arg)
  {
  	disable_smep = 1;
  	return 1;
  }
  __setup("nosmep", setup_disable_smep);
82da65dab   Linus Torvalds   x86: setup_smep n...
268
  static __cpuinit void setup_smep(struct cpuinfo_x86 *c)
de5397ad5   Fenghua Yu   x86, cpu: Enable/...
269
270
271
272
273
274
275
276
277
  {
  	if (cpu_has(c, X86_FEATURE_SMEP)) {
  		if (unlikely(disable_smep)) {
  			setup_clear_cpu_cap(X86_FEATURE_SMEP);
  			clear_in_cr4(X86_CR4_SMEP);
  		} else
  			set_in_cr4(X86_CR4_SMEP);
  	}
  }
102bbe3ab   Yinghai Lu   x86: cpu/common*....
278
  /*
b38b06659   H. Peter Anvin   x86: filter CPU f...
279
280
281
282
283
284
285
286
   * Some CPU features depend on higher CPUID levels, which may not always
   * be available due to CPUID level capping or broken virtualization
   * software.  Add those features to this table to auto-disable them.
   */
  struct cpuid_dependent_feature {
  	u32 feature;
  	u32 level;
  };
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
287

b38b06659   H. Peter Anvin   x86: filter CPU f...
288
289
290
291
292
293
294
295
296
297
298
  static const struct cpuid_dependent_feature __cpuinitconst
  cpuid_dependent_features[] = {
  	{ X86_FEATURE_MWAIT,		0x00000005 },
  	{ X86_FEATURE_DCA,		0x00000009 },
  	{ X86_FEATURE_XSAVE,		0x0000000d },
  	{ 0, 0 }
  };
  
  static void __cpuinit filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  {
  	const struct cpuid_dependent_feature *df;
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
299

b38b06659   H. Peter Anvin   x86: filter CPU f...
300
  	for (df = cpuid_dependent_features; df->feature; df++) {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
301
302
303
  
  		if (!cpu_has(c, df->feature))
  			continue;
b38b06659   H. Peter Anvin   x86: filter CPU f...
304
305
306
307
308
309
310
  		/*
  		 * Note: cpuid_level is set to -1 if unavailable, but
  		 * extended_extended_level is set to 0 if unavailable
  		 * and the legitimate extended levels are all negative
  		 * when signed; hence the weird messing around with
  		 * signs here...
  		 */
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
311
  		if (!((s32)df->level < 0 ?
f6db44df5   Yinghai Lu   x86: fix typo in ...
312
  		     (u32)df->level > (u32)c->extended_cpuid_level :
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
313
314
315
316
317
318
319
320
321
322
323
  		     (s32)df->level > (s32)c->cpuid_level))
  			continue;
  
  		clear_cpu_cap(c, df->feature);
  		if (!warn)
  			continue;
  
  		printk(KERN_WARNING
  		       "CPU: CPU feature %s disabled, no CPUID level 0x%x
  ",
  				x86_cap_flags[df->feature], df->level);
b38b06659   H. Peter Anvin   x86: filter CPU f...
324
  	}
f6db44df5   Yinghai Lu   x86: fix typo in ...
325
  }
b38b06659   H. Peter Anvin   x86: filter CPU f...
326
327
  
  /*
102bbe3ab   Yinghai Lu   x86: cpu/common*....
328
329
   * Naming convention should be: <Name> [(<Codename>)]
   * This table only is used unless init_<vendor>() below doesn't set it;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
330
331
   * in particular, if CPUID levels 0x80000002..4 are supported, this
   * isn't used
102bbe3ab   Yinghai Lu   x86: cpu/common*....
332
333
334
   */
  
  /* Look up CPU names by table lookup. */
02dde8b45   Jan Beulich   x86: move various...
335
  static const char *__cpuinit table_lookup_model(struct cpuinfo_x86 *c)
102bbe3ab   Yinghai Lu   x86: cpu/common*....
336
  {
02dde8b45   Jan Beulich   x86: move various...
337
  	const struct cpu_model_info *info;
102bbe3ab   Yinghai Lu   x86: cpu/common*....
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  
  	if (c->x86_model >= 16)
  		return NULL;	/* Range check */
  
  	if (!this_cpu)
  		return NULL;
  
  	info = this_cpu->c_models;
  
  	while (info && info->family) {
  		if (info->family == c->x86)
  			return info->model_names[c->x86_model];
  		info++;
  	}
  	return NULL;		/* Not found */
  }
3e0c37374   Yinghai Lu   x86: clean up and...
354
355
  __u32 cpu_caps_cleared[NCAPINTS] __cpuinitdata;
  __u32 cpu_caps_set[NCAPINTS] __cpuinitdata;
7d851c8d3   Andi Kleen   x86: add framewor...
356

11e3a840c   Jeremy Fitzhardinge   x86: split loadin...
357
358
359
360
361
362
363
364
  void load_percpu_segment(int cpu)
  {
  #ifdef CONFIG_X86_32
  	loadsegment(fs, __KERNEL_PERCPU);
  #else
  	loadsegment(gs, 0);
  	wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  #endif
60a5317ff   Tejun Heo   x86: implement x8...
365
  	load_stack_canary_segment();
11e3a840c   Jeremy Fitzhardinge   x86: split loadin...
366
  }
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
367
368
369
370
  /*
   * Current gdt points %fs at the "master" per-cpu area: after this,
   * it's on the real one.
   */
552be871e   Brian Gerst   x86: pass in cpu ...
371
  void switch_to_new_gdt(int cpu)
9d31d35b5   Yinghai Lu   x86: order functi...
372
373
  {
  	struct desc_ptr gdt_descr;
2697fbd5f   Brian Gerst   x86: load new GDT...
374
  	gdt_descr.address = (long)get_cpu_gdt_table(cpu);
9d31d35b5   Yinghai Lu   x86: order functi...
375
376
  	gdt_descr.size = GDT_SIZE - 1;
  	load_gdt(&gdt_descr);
2697fbd5f   Brian Gerst   x86: load new GDT...
377
  	/* Reload the per-cpu base */
11e3a840c   Jeremy Fitzhardinge   x86: split loadin...
378
379
  
  	load_percpu_segment(cpu);
9d31d35b5   Yinghai Lu   x86: order functi...
380
  }
02dde8b45   Jan Beulich   x86: move various...
381
  static const struct cpu_dev *__cpuinitdata cpu_devs[X86_VENDOR_NUM] = {};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382

1b05d60d6   Yinghai Lu   x86: remove dupli...
383
  static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
386
  {
  	unsigned int *v;
  	char *p, *q;
3da99c977   Yinghai Lu   x86: make (early)...
387
  	if (c->extended_cpuid_level < 0x80000004)
1b05d60d6   Yinghai Lu   x86: remove dupli...
388
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
390
  	v = (unsigned int *)c->x86_model_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
  	cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  	cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  	cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  	c->x86_model_id[48] = 0;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
395
396
397
398
  	/*
  	 * Intel chips right-justify this string for some dumb reason;
  	 * undo that brain damage:
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  	p = q = &c->x86_model_id[0];
34048c9e9   Paolo Ciarrocchi   x86: coding style...
400
  	while (*p == ' ')
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
401
  		p++;
34048c9e9   Paolo Ciarrocchi   x86: coding style...
402
  	if (p != q) {
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
403
404
405
406
  		while (*p)
  			*q++ = *p++;
  		while (q <= &c->x86_model_id[48])
  			*q++ = '\0';	/* Zero-pad the rest */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  }
27c13ecec   Borislav Petkov   x86, cpu: mv disp...
409
  void __cpuinit cpu_detect_cache_sizes(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
  {
9d31d35b5   Yinghai Lu   x86: order functi...
411
  	unsigned int n, dummy, ebx, ecx, edx, l2size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412

3da99c977   Yinghai Lu   x86: make (early)...
413
  	n = c->extended_cpuid_level;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
415
  
  	if (n >= 0x80000005) {
9d31d35b5   Yinghai Lu   x86: order functi...
416
  		cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
9d31d35b5   Yinghai Lu   x86: order functi...
417
  		c->x86_cache_size = (ecx>>24) + (edx>>24);
140fc7270   Yinghai Lu   x86: cpu/common*....
418
419
420
421
  #ifdef CONFIG_X86_64
  		/* On K8 L1 TLB is inclusive, so don't count it */
  		c->x86_tlbsize = 0;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
  	}
  
  	if (n < 0x80000006)	/* Some chips just has a large L1. */
  		return;
0a488a53d   Yinghai Lu   x86: move 32bit r...
426
  	cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
  	l2size = ecx >> 16;
34048c9e9   Paolo Ciarrocchi   x86: coding style...
428

140fc7270   Yinghai Lu   x86: cpu/common*....
429
430
431
  #ifdef CONFIG_X86_64
  	c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  #else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
  	/* do processor-specific cache resizing */
  	if (this_cpu->c_size_cache)
34048c9e9   Paolo Ciarrocchi   x86: coding style...
434
  		l2size = this_cpu->c_size_cache(c, l2size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
438
  
  	/* Allow user to override all this if necessary. */
  	if (cachesize_override != -1)
  		l2size = cachesize_override;
34048c9e9   Paolo Ciarrocchi   x86: coding style...
439
  	if (l2size == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  		return;		/* Again, no L2 cache is possible */
140fc7270   Yinghai Lu   x86: cpu/common*....
441
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
  
  	c->x86_cache_size = l2size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
  }
9d31d35b5   Yinghai Lu   x86: order functi...
445
  void __cpuinit detect_ht(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
  {
97e4db7c8   Yinghai Lu   x86: make detect_...
447
  #ifdef CONFIG_X86_HT
0a488a53d   Yinghai Lu   x86: move 32bit r...
448
449
  	u32 eax, ebx, ecx, edx;
  	int index_msb, core_bits;
2eaad1fdd   Mike Travis   x86: Limit the nu...
450
  	static bool printed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451

0a488a53d   Yinghai Lu   x86: move 32bit r...
452
  	if (!cpu_has(c, X86_FEATURE_HT))
9d31d35b5   Yinghai Lu   x86: order functi...
453
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454

0a488a53d   Yinghai Lu   x86: move 32bit r...
455
456
  	if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457

1cd78776c   Yinghai Lu   x86: cpu/common*....
458
459
  	if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

0a488a53d   Yinghai Lu   x86: move 32bit r...
461
  	cpuid(1, &eax, &ebx, &ecx, &edx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462

9d31d35b5   Yinghai Lu   x86: order functi...
463
464
465
  	smp_num_siblings = (ebx & 0xff0000) >> 16;
  
  	if (smp_num_siblings == 1) {
2eaad1fdd   Mike Travis   x86: Limit the nu...
466
467
  		printk_once(KERN_INFO "CPU0: Hyper-Threading is disabled
  ");
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
468
469
  		goto out;
  	}
9d31d35b5   Yinghai Lu   x86: order functi...
470

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
471
472
  	if (smp_num_siblings <= 1)
  		goto out;
9d31d35b5   Yinghai Lu   x86: order functi...
473

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
474
475
  	index_msb = get_count_order(smp_num_siblings);
  	c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
9d31d35b5   Yinghai Lu   x86: order functi...
476

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
477
  	smp_num_siblings = smp_num_siblings / c->x86_max_cores;
9d31d35b5   Yinghai Lu   x86: order functi...
478

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
479
  	index_msb = get_count_order(smp_num_siblings);
9d31d35b5   Yinghai Lu   x86: order functi...
480

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
481
  	core_bits = get_count_order(c->x86_max_cores);
9d31d35b5   Yinghai Lu   x86: order functi...
482

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
483
484
  	c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  				       ((1 << core_bits) - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485

0a488a53d   Yinghai Lu   x86: move 32bit r...
486
  out:
2eaad1fdd   Mike Travis   x86: Limit the nu...
487
  	if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) {
0a488a53d   Yinghai Lu   x86: move 32bit r...
488
489
490
491
492
493
  		printk(KERN_INFO  "CPU: Physical Processor ID: %d
  ",
  		       c->phys_proc_id);
  		printk(KERN_INFO  "CPU: Processor Core ID: %d
  ",
  		       c->cpu_core_id);
2eaad1fdd   Mike Travis   x86: Limit the nu...
494
  		printed = 1;
9d31d35b5   Yinghai Lu   x86: order functi...
495
  	}
9d31d35b5   Yinghai Lu   x86: order functi...
496
  #endif
97e4db7c8   Yinghai Lu   x86: make detect_...
497
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498

3da99c977   Yinghai Lu   x86: make (early)...
499
  static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
501
  {
  	char *v = c->x86_vendor_id;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
502
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
504
  
  	for (i = 0; i < X86_VENDOR_NUM; i++) {
10a434fcb   Yinghai Lu   x86: remove cpu_v...
505
506
507
508
509
510
  		if (!cpu_devs[i])
  			break;
  
  		if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  		    (cpu_devs[i]->c_ident[1] &&
  		     !strcmp(v, cpu_devs[i]->c_ident[1]))) {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
511

10a434fcb   Yinghai Lu   x86: remove cpu_v...
512
513
514
  			this_cpu = cpu_devs[i];
  			c->x86_vendor = this_cpu->c_x86_vendor;
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
  		}
  	}
10a434fcb   Yinghai Lu   x86: remove cpu_v...
517

a9c569539   Minchan Kim   use printk_once()...
518
519
520
521
522
  	printk_once(KERN_ERR
  			"CPU: vendor_id '%s' unknown, using generic init.
  " \
  			"CPU: Your system may be unstable.
  ", v);
10a434fcb   Yinghai Lu   x86: remove cpu_v...
523

fe38d8553   Chuck Ebbert   [PATCH] i386 cpu ...
524
525
  	c->x86_vendor = X86_VENDOR_UNKNOWN;
  	this_cpu = &default_cpu;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  }
9d31d35b5   Yinghai Lu   x86: order functi...
527
  void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
  	/* Get vendor name */
4a1485131   Harvey Harrison   x86: fix sparse w...
530
531
532
533
  	cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  	      (unsigned int *)&c->x86_vendor_id[0],
  	      (unsigned int *)&c->x86_vendor_id[8],
  	      (unsigned int *)&c->x86_vendor_id[4]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
  	c->x86 = 4;
9d31d35b5   Yinghai Lu   x86: order functi...
536
  	/* Intel-defined flags: level 0x00000001 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
538
  	if (c->cpuid_level >= 0x00000001) {
  		u32 junk, tfms, cap0, misc;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
539

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
  		cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
9d31d35b5   Yinghai Lu   x86: order functi...
541
542
543
  		c->x86 = (tfms >> 8) & 0xf;
  		c->x86_model = (tfms >> 4) & 0xf;
  		c->x86_mask = tfms & 0xf;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
544

f5f786d04   Suresh Siddha   [PATCH] x86-64/i3...
545
  		if (c->x86 == 0xf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  			c->x86 += (tfms >> 20) & 0xff;
f5f786d04   Suresh Siddha   [PATCH] x86-64/i3...
547
  		if (c->x86 >= 0x6)
9d31d35b5   Yinghai Lu   x86: order functi...
548
  			c->x86_model += ((tfms >> 16) & 0xf) << 4;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
549

d4387bd3f   Huang, Ying   x86: c_p_a clflus...
550
  		if (cap0 & (1<<19)) {
d4387bd3f   Huang, Ying   x86: c_p_a clflus...
551
  			c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
9d31d35b5   Yinghai Lu   x86: order functi...
552
  			c->x86_cache_alignment = c->x86_clflush_size;
d4387bd3f   Huang, Ying   x86: c_p_a clflus...
553
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
  }
3da99c977   Yinghai Lu   x86: make (early)...
556

d900329e2   H. Peter Anvin   x86, cpu: After u...
557
  void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
093af8d7f   Yinghai Lu   x86_32: trim memo...
558
559
  {
  	u32 tfms, xlvl;
3da99c977   Yinghai Lu   x86: make (early)...
560
  	u32 ebx;
093af8d7f   Yinghai Lu   x86_32: trim memo...
561

3da99c977   Yinghai Lu   x86: make (early)...
562
563
564
  	/* Intel-defined flags: level 0x00000001 */
  	if (c->cpuid_level >= 0x00000001) {
  		u32 capability, excap;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
565

3da99c977   Yinghai Lu   x86: make (early)...
566
567
568
569
  		cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  		c->x86_capability[0] = capability;
  		c->x86_capability[4] = excap;
  	}
093af8d7f   Yinghai Lu   x86_32: trim memo...
570

bdc802dcc   H. Peter Anvin   x86, cpu: Support...
571
572
573
574
575
  	/* Additional Intel-defined flags: level 0x00000007 */
  	if (c->cpuid_level >= 0x00000007) {
  		u32 eax, ebx, ecx, edx;
  
  		cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx);
2494b030b   Fenghua Yu   x86, cpufeature: ...
576
  		c->x86_capability[9] = ebx;
bdc802dcc   H. Peter Anvin   x86, cpu: Support...
577
  	}
3da99c977   Yinghai Lu   x86: make (early)...
578
579
580
  	/* AMD-defined flags: level 0x80000001 */
  	xlvl = cpuid_eax(0x80000000);
  	c->extended_cpuid_level = xlvl;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
581

3da99c977   Yinghai Lu   x86: make (early)...
582
583
584
585
  	if ((xlvl & 0xffff0000) == 0x80000000) {
  		if (xlvl >= 0x80000001) {
  			c->x86_capability[1] = cpuid_edx(0x80000001);
  			c->x86_capability[6] = cpuid_ecx(0x80000001);
093af8d7f   Yinghai Lu   x86_32: trim memo...
586
  		}
093af8d7f   Yinghai Lu   x86_32: trim memo...
587
  	}
093af8d7f   Yinghai Lu   x86_32: trim memo...
588

5122c890b   Yinghai Lu   x86: cpu/common.c...
589
590
591
592
593
  	if (c->extended_cpuid_level >= 0x80000008) {
  		u32 eax = cpuid_eax(0x80000008);
  
  		c->x86_virt_bits = (eax >> 8) & 0xff;
  		c->x86_phys_bits = eax & 0xff;
093af8d7f   Yinghai Lu   x86_32: trim memo...
594
  	}
13c6c5328   Jan Beulich   x86, 32-bit: also...
595
596
597
  #ifdef CONFIG_X86_32
  	else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
  		c->x86_phys_bits = 36;
5122c890b   Yinghai Lu   x86: cpu/common.c...
598
  #endif
e32242347   Yinghai Lu   x86, cpu init: ca...
599
600
601
  
  	if (c->extended_cpuid_level >= 0x80000007)
  		c->x86_power = cpuid_edx(0x80000007);
093af8d7f   Yinghai Lu   x86_32: trim memo...
602

1dedefd1a   Jacob Pan   x86: detect scatt...
603
  	init_scattered_cpuid_features(c);
093af8d7f   Yinghai Lu   x86_32: trim memo...
604
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605

aef93c8bd   Yinghai Lu   x86: identify_cpu...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  {
  #ifdef CONFIG_X86_32
  	int i;
  
  	/*
  	 * First of all, decide if this is a 486 or higher
  	 * It's a 486 if we can modify the AC flag
  	 */
  	if (flag_is_changeable_p(X86_EFLAGS_AC))
  		c->x86 = 4;
  	else
  		c->x86 = 3;
  
  	for (i = 0; i < X86_VENDOR_NUM; i++)
  		if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  			c->x86_vendor_id[0] = 0;
  			cpu_devs[i]->c_identify(c);
  			if (c->x86_vendor_id[0]) {
  				get_cpu_vendor(c);
  				break;
  			}
  		}
  #endif
  }
34048c9e9   Paolo Ciarrocchi   x86: coding style...
631
632
633
634
635
636
637
638
639
  /*
   * Do minimum CPU detection early.
   * Fields really needed: vendor, cpuid_level, family, model, mask,
   * cache alignment.
   * The others are not touched to avoid unwanted side effects.
   *
   * WARNING: this function is only called on the BP.  Don't add code here
   * that is supposed to run on all CPUs.
   */
3da99c977   Yinghai Lu   x86: make (early)...
640
  static void __init early_identify_cpu(struct cpuinfo_x86 *c)
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
641
  {
6627d2423   Yinghai Lu   x86: cpu/common*....
642
643
  #ifdef CONFIG_X86_64
  	c->x86_clflush_size = 64;
13c6c5328   Jan Beulich   x86, 32-bit: also...
644
645
  	c->x86_phys_bits = 36;
  	c->x86_virt_bits = 48;
6627d2423   Yinghai Lu   x86: cpu/common*....
646
  #else
d4387bd3f   Huang, Ying   x86: c_p_a clflus...
647
  	c->x86_clflush_size = 32;
13c6c5328   Jan Beulich   x86, 32-bit: also...
648
649
  	c->x86_phys_bits = 32;
  	c->x86_virt_bits = 32;
6627d2423   Yinghai Lu   x86: cpu/common*....
650
  #endif
0a488a53d   Yinghai Lu   x86: move 32bit r...
651
  	c->x86_cache_alignment = c->x86_clflush_size;
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
652

3da99c977   Yinghai Lu   x86: make (early)...
653
  	memset(&c->x86_capability, 0, sizeof c->x86_capability);
0a488a53d   Yinghai Lu   x86: move 32bit r...
654
  	c->extended_cpuid_level = 0;
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
655
656
  
  	if (!have_cpuid_p())
aef93c8bd   Yinghai Lu   x86: identify_cpu...
657
658
659
  		identify_cpu_without_cpuid(c);
  
  	/* cyrix could have cpuid enabled via c_identify()*/
a9853dd6d   Ingo Molnar   x86: cpuid, fix typo
660
  	if (!have_cpuid_p())
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
661
662
663
  		return;
  
  	cpu_detect(c);
3da99c977   Yinghai Lu   x86: make (early)...
664
  	get_cpu_vendor(c);
2b16a2353   Andi Kleen   x86: move X86_FEA...
665

3da99c977   Yinghai Lu   x86: make (early)...
666
  	get_cpu_cap(c);
12cf105cd   Krzysztof Helt   x86: delay early ...
667

10a434fcb   Yinghai Lu   x86: remove cpu_v...
668
669
  	if (this_cpu->c_early_init)
  		this_cpu->c_early_init(c);
093af8d7f   Yinghai Lu   x86_32: trim memo...
670

f6e9456c9   Robert Richter   x86, cleanup: Rem...
671
  	c->cpu_index = 0;
b38b06659   H. Peter Anvin   x86: filter CPU f...
672
  	filter_cpuid_features(c, false);
de5397ad5   Fenghua Yu   x86, cpu: Enable/...
673
674
  
  	setup_smep(c);
a110b5ec7   Borislav Petkov   x86: Add a BSP cp...
675
676
677
  
  	if (this_cpu->c_bsp_init)
  		this_cpu->c_bsp_init(c);
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
678
  }
9d31d35b5   Yinghai Lu   x86: order functi...
679
680
  void __init early_cpu_init(void)
  {
02dde8b45   Jan Beulich   x86: move various...
681
  	const struct cpu_dev *const *cdev;
10a434fcb   Yinghai Lu   x86: remove cpu_v...
682
  	int count = 0;
ac23f2535   Jan Beulich   x86: Really print...
683
  #ifdef CONFIG_PROCESSOR_SELECT
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
684
685
  	printk(KERN_INFO "KERNEL supported cpus:
  ");
31c997cac   Ingo Molnar   x86: Fix cpu_devs...
686
  #endif
10a434fcb   Yinghai Lu   x86: remove cpu_v...
687
  	for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
02dde8b45   Jan Beulich   x86: move various...
688
  		const struct cpu_dev *cpudev = *cdev;
9d31d35b5   Yinghai Lu   x86: order functi...
689

10a434fcb   Yinghai Lu   x86: remove cpu_v...
690
691
692
693
  		if (count >= X86_VENDOR_NUM)
  			break;
  		cpu_devs[count] = cpudev;
  		count++;
ac23f2535   Jan Beulich   x86: Really print...
694
  #ifdef CONFIG_PROCESSOR_SELECT
31c997cac   Ingo Molnar   x86: Fix cpu_devs...
695
696
697
698
699
700
701
702
703
704
  		{
  			unsigned int j;
  
  			for (j = 0; j < 2; j++) {
  				if (!cpudev->c_ident[j])
  					continue;
  				printk(KERN_INFO "  %s %s
  ", cpudev->c_vendor,
  					cpudev->c_ident[j]);
  			}
10a434fcb   Yinghai Lu   x86: remove cpu_v...
705
  		}
0388423db   Dave Jones   x86: Minimise pri...
706
  #endif
10a434fcb   Yinghai Lu   x86: remove cpu_v...
707
  	}
9d31d35b5   Yinghai Lu   x86: order functi...
708
  	early_identify_cpu(&boot_cpu_data);
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
709
  }
093af8d7f   Yinghai Lu   x86_32: trim memo...
710

b6734c35a   H. Peter Anvin   x86: add NOPL as ...
711
  /*
366d4a43b   Borislav Petkov   x86, cpu: Fix X86...
712
713
714
715
716
   * The NOPL instruction is supposed to exist on all CPUs of family >= 6;
   * unfortunately, that's not true in practice because of early VIA
   * chips and (more importantly) broken virtualizers that are not easy
   * to detect. In the latter case it doesn't even *fail* reliably, so
   * probing for it doesn't even work. Disable it completely on 32-bit
ba0593bf5   H. Peter Anvin   x86: completely d...
717
   * unless we can find a reliable way to detect all the broken cases.
366d4a43b   Borislav Petkov   x86, cpu: Fix X86...
718
   * Enable it explicitly on 64-bit for non-constant inputs of cpu_has().
b6734c35a   H. Peter Anvin   x86: add NOPL as ...
719
720
721
   */
  static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  {
366d4a43b   Borislav Petkov   x86, cpu: Fix X86...
722
  #ifdef CONFIG_X86_32
b6734c35a   H. Peter Anvin   x86: add NOPL as ...
723
  	clear_cpu_cap(c, X86_FEATURE_NOPL);
366d4a43b   Borislav Petkov   x86, cpu: Fix X86...
724
725
726
  #else
  	set_cpu_cap(c, X86_FEATURE_NOPL);
  #endif
d7cd56111   Rusty Russell   [PATCH] i386: cpu...
727
  }
34048c9e9   Paolo Ciarrocchi   x86: coding style...
728
  static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  {
aef93c8bd   Yinghai Lu   x86: identify_cpu...
730
  	c->extended_cpuid_level = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731

3da99c977   Yinghai Lu   x86: make (early)...
732
  	if (!have_cpuid_p())
aef93c8bd   Yinghai Lu   x86: identify_cpu...
733
  		identify_cpu_without_cpuid(c);
1d67953f2   Venki Pallipadi   Use a new CPU fea...
734

aef93c8bd   Yinghai Lu   x86: identify_cpu...
735
  	/* cyrix could have cpuid enabled via c_identify()*/
a9853dd6d   Ingo Molnar   x86: cpuid, fix typo
736
  	if (!have_cpuid_p())
aef93c8bd   Yinghai Lu   x86: identify_cpu...
737
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738

3da99c977   Yinghai Lu   x86: make (early)...
739
  	cpu_detect(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740

3da99c977   Yinghai Lu   x86: make (early)...
741
  	get_cpu_vendor(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
742

3da99c977   Yinghai Lu   x86: make (early)...
743
  	get_cpu_cap(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744

3da99c977   Yinghai Lu   x86: make (early)...
745
746
  	if (c->cpuid_level >= 0x00000001) {
  		c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
b89d3b3e2   Yinghai Lu   x86: cpu/common*....
747
748
  #ifdef CONFIG_X86_32
  # ifdef CONFIG_X86_HT
cb8cc442d   Ingo Molnar   x86, apic: refact...
749
  		c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
b89d3b3e2   Yinghai Lu   x86: cpu/common*....
750
  # else
3da99c977   Yinghai Lu   x86: make (early)...
751
  		c->apicid = c->initial_apicid;
b89d3b3e2   Yinghai Lu   x86: cpu/common*....
752
753
  # endif
  #endif
b89d3b3e2   Yinghai Lu   x86: cpu/common*....
754
  		c->phys_proc_id = c->initial_apicid;
3da99c977   Yinghai Lu   x86: make (early)...
755
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756

de5397ad5   Fenghua Yu   x86, cpu: Enable/...
757
  	setup_smep(c);
1b05d60d6   Yinghai Lu   x86: remove dupli...
758
  	get_model_name(c); /* Default name */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759

3da99c977   Yinghai Lu   x86: make (early)...
760
  	detect_nopl(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
764
765
  
  /*
   * This does the hard work of actually picking apart the CPU stuff...
   */
9a2503475   Yinghai Lu   x86: change ident...
766
  static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
769
770
771
772
  {
  	int i;
  
  	c->loops_per_jiffy = loops_per_jiffy;
  	c->x86_cache_size = -1;
  	c->x86_vendor = X86_VENDOR_UNKNOWN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
774
775
  	c->x86_model = c->x86_mask = 0;	/* So far unknown... */
  	c->x86_vendor_id[0] = '\0'; /* Unset */
  	c->x86_model_id[0] = '\0';  /* Unset */
94605eff5   Siddha, Suresh B   [PATCH] x86-64/i3...
776
  	c->x86_max_cores = 1;
102bbe3ab   Yinghai Lu   x86: cpu/common*....
777
  	c->x86_coreid_bits = 0;
11fdd252b   Yinghai Lu   x86: cpu make amd...
778
  #ifdef CONFIG_X86_64
102bbe3ab   Yinghai Lu   x86: cpu/common*....
779
  	c->x86_clflush_size = 64;
13c6c5328   Jan Beulich   x86, 32-bit: also...
780
781
  	c->x86_phys_bits = 36;
  	c->x86_virt_bits = 48;
102bbe3ab   Yinghai Lu   x86: cpu/common*....
782
783
  #else
  	c->cpuid_level = -1;	/* CPUID not detected */
770d132f0   Andi Kleen   [PATCH] i386: Ret...
784
  	c->x86_clflush_size = 32;
13c6c5328   Jan Beulich   x86, 32-bit: also...
785
786
  	c->x86_phys_bits = 32;
  	c->x86_virt_bits = 32;
102bbe3ab   Yinghai Lu   x86: cpu/common*....
787
788
  #endif
  	c->x86_cache_alignment = c->x86_clflush_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  	memset(&c->x86_capability, 0, sizeof c->x86_capability);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
  	generic_identify(c);
3898534d8   Andi Kleen   x86: remove CPU c...
791
  	if (this_cpu->c_identify)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
  		this_cpu->c_identify(c);
2759c3287   Yinghai Lu   x86: don't call r...
793
794
795
796
797
  	/* Clear/Set all flags overriden by options, after probe */
  	for (i = 0; i < NCAPINTS; i++) {
  		c->x86_capability[i] &= ~cpu_caps_cleared[i];
  		c->x86_capability[i] |= cpu_caps_set[i];
  	}
102bbe3ab   Yinghai Lu   x86: cpu/common*....
798
  #ifdef CONFIG_X86_64
cb8cc442d   Ingo Molnar   x86, apic: refact...
799
  	c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
102bbe3ab   Yinghai Lu   x86: cpu/common*....
800
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
  	/*
  	 * Vendor-specific initialization.  In this section we
  	 * canonicalize the feature flags, meaning if there are
  	 * features a certain CPU supports which CPUID doesn't
  	 * tell us, CPUID claiming incorrect flags, or other bugs,
  	 * we handle them here.
  	 *
  	 * At the end of this section, c->x86_capability better
  	 * indicate the features this CPU genuinely supports!
  	 */
  	if (this_cpu->c_init)
  		this_cpu->c_init(c);
  
  	/* Disable the PN if appropriate */
  	squash_the_stupid_serial_number(c);
  
  	/*
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
818
819
  	 * The vendor-specific functions might have changed features.
  	 * Now we do "generic changes."
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
820
  	 */
b38b06659   H. Peter Anvin   x86: filter CPU f...
821
822
  	/* Filter out anything that depends on CPUID levels we don't have */
  	filter_cpuid_features(c, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  	/* If the model name is still unset, do table lookup. */
34048c9e9   Paolo Ciarrocchi   x86: coding style...
824
  	if (!c->x86_model_id[0]) {
02dde8b45   Jan Beulich   x86: move various...
825
  		const char *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
  		p = table_lookup_model(c);
34048c9e9   Paolo Ciarrocchi   x86: coding style...
827
  		if (p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
  			strcpy(c->x86_model_id, p);
  		else
  			/* Last resort... */
  			sprintf(c->x86_model_id, "%02x/%02x",
54a20f8c5   Chuck Ebbert   [PATCH] i386: fal...
832
  				c->x86, c->x86_model);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
  	}
102bbe3ab   Yinghai Lu   x86: cpu/common*....
834
835
836
  #ifdef CONFIG_X86_64
  	detect_ht(c);
  #endif
88b094fb8   Alok Kataria   x86: Hypervisor d...
837
  	init_hypervisor(c);
49d859d78   H. Peter Anvin   x86, random: Veri...
838
  	x86_init_rdrand(c);
3e0c37374   Yinghai Lu   x86: clean up and...
839
840
841
842
843
844
845
846
847
  
  	/*
  	 * Clear/Set all flags overriden by options, need do it
  	 * before following smp all cpus cap AND.
  	 */
  	for (i = 0; i < NCAPINTS; i++) {
  		c->x86_capability[i] &= ~cpu_caps_cleared[i];
  		c->x86_capability[i] |= cpu_caps_set[i];
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
849
850
851
852
853
  	/*
  	 * On SMP, boot_cpu_data holds the common feature set between
  	 * all CPUs; so make sure that we indicate which features are
  	 * common between the CPUs.  The first time this routine gets
  	 * executed, c == &boot_cpu_data.
  	 */
34048c9e9   Paolo Ciarrocchi   x86: coding style...
854
  	if (c != &boot_cpu_data) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
  		/* AND the already accumulated flags with these */
9d31d35b5   Yinghai Lu   x86: order functi...
856
  		for (i = 0; i < NCAPINTS; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857
858
859
860
  			boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  	}
  
  	/* Init Machine Check Exception if available. */
5e09954a9   Borislav Petkov   x86, mce: Fix up ...
861
  	mcheck_cpu_init(c);
30d432dfa   Andi Kleen   x86: move MWAIT i...
862
863
  
  	select_idle_routine(c);
102bbe3ab   Yinghai Lu   x86: cpu/common*....
864

de2d9445f   Tejun Heo   x86: Unify node_t...
865
  #ifdef CONFIG_NUMA
102bbe3ab   Yinghai Lu   x86: cpu/common*....
866
867
  	numa_add_cpu(smp_processor_id());
  #endif
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
868
  }
31ab269a0   Shaohua Li   [PATCH] x86: add ...
869

e04d645f3   Glauber Costa   x86: move vgetcpu...
870
871
872
873
874
875
876
877
878
  #ifdef CONFIG_X86_64
  static void vgetcpu_set_mode(void)
  {
  	if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  		vgetcpu_mode = VGETCPU_RDTSCP;
  	else
  		vgetcpu_mode = VGETCPU_LSL;
  }
  #endif
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
879
880
881
  void __init identify_boot_cpu(void)
  {
  	identify_cpu(&boot_cpu_data);
02c68a020   Len Brown   x86 idle: clarify...
882
  	init_amd_e400_c1e_mask();
102bbe3ab   Yinghai Lu   x86: cpu/common*....
883
  #ifdef CONFIG_X86_32
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
884
  	sysenter_setup();
6fe940d6c   Li Shaohua   [PATCH] sep initi...
885
  	enable_sep_cpu();
e04d645f3   Glauber Costa   x86: move vgetcpu...
886
887
  #else
  	vgetcpu_set_mode();
102bbe3ab   Yinghai Lu   x86: cpu/common*....
888
  #endif
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
889
  }
3b520b238   Shaohua Li   [PATCH] MTRR susp...
890

a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
891
892
893
894
  void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  {
  	BUG_ON(c == &boot_cpu_data);
  	identify_cpu(c);
102bbe3ab   Yinghai Lu   x86: cpu/common*....
895
  #ifdef CONFIG_X86_32
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
896
  	enable_sep_cpu();
102bbe3ab   Yinghai Lu   x86: cpu/common*....
897
  #endif
a6c4e076e   Jeremy Fitzhardinge   [PATCH] i386: cle...
898
  	mtrr_ap_init();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
  }
a0854a46c   Yinghai Lu   x86: make 32bit s...
900
  struct msr_range {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
901
902
  	unsigned	min;
  	unsigned	max;
a0854a46c   Yinghai Lu   x86: make 32bit s...
903
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904

02dde8b45   Jan Beulich   x86: move various...
905
  static const struct msr_range msr_range_array[] __cpuinitconst = {
a0854a46c   Yinghai Lu   x86: make 32bit s...
906
907
908
909
910
  	{ 0x00000000, 0x00000418},
  	{ 0xc0000000, 0xc000040b},
  	{ 0xc0010000, 0xc0010142},
  	{ 0xc0011000, 0xc001103b},
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
911

a0854a46c   Yinghai Lu   x86: make 32bit s...
912
913
  static void __cpuinit print_cpu_msr(void)
  {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
914
  	unsigned index_min, index_max;
a0854a46c   Yinghai Lu   x86: make 32bit s...
915
916
917
  	unsigned index;
  	u64 val;
  	int i;
a0854a46c   Yinghai Lu   x86: make 32bit s...
918
919
920
921
  
  	for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  		index_min = msr_range_array[i].min;
  		index_max = msr_range_array[i].max;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
922

a0854a46c   Yinghai Lu   x86: make 32bit s...
923
924
925
926
927
  		for (index = index_min; index < index_max; index++) {
  			if (rdmsrl_amd_safe(index, &val))
  				continue;
  			printk(KERN_INFO " MSR%08x: %016llx
  ", index, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
  		}
a0854a46c   Yinghai Lu   x86: make 32bit s...
929
930
  	}
  }
94605eff5   Siddha, Suresh B   [PATCH] x86-64/i3...
931

a0854a46c   Yinghai Lu   x86: make 32bit s...
932
  static int show_msr __cpuinitdata;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
933

a0854a46c   Yinghai Lu   x86: make 32bit s...
934
935
936
  static __init int setup_show_msr(char *arg)
  {
  	int num;
3dd9d5148   Andi Kleen   [PATCH] x86_64: a...
937

a0854a46c   Yinghai Lu   x86: make 32bit s...
938
  	get_option(&arg, &num);
3dd9d5148   Andi Kleen   [PATCH] x86_64: a...
939

a0854a46c   Yinghai Lu   x86: make 32bit s...
940
941
942
  	if (num > 0)
  		show_msr = num;
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
  }
a0854a46c   Yinghai Lu   x86: make 32bit s...
944
  __setup("show_msr=", setup_show_msr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945

191679fdf   Andi Kleen   x86: add noclflus...
946
947
948
949
950
951
  static __init int setup_noclflush(char *arg)
  {
  	setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  	return 1;
  }
  __setup("noclflush", setup_noclflush);
3bc9b76be   Chuck Ebbert   [PATCH] i386: __d...
952
  void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
953
  {
02dde8b45   Jan Beulich   x86: move various...
954
  	const char *vendor = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
955

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
956
  	if (c->x86_vendor < X86_VENDOR_NUM) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
957
  		vendor = this_cpu->c_vendor;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
958
959
960
961
  	} else {
  		if (c->cpuid_level >= 0)
  			vendor = c->x86_vendor_id;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962

bd32a8cfa   Yinghai Lu   x86: cpu don't pr...
963
  	if (vendor && !strstr(c->x86_model_id, vendor))
9d31d35b5   Yinghai Lu   x86: order functi...
964
  		printk(KERN_CONT "%s ", vendor);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965

9d31d35b5   Yinghai Lu   x86: order functi...
966
967
  	if (c->x86_model_id[0])
  		printk(KERN_CONT "%s", c->x86_model_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
  	else
9d31d35b5   Yinghai Lu   x86: order functi...
969
  		printk(KERN_CONT "%d86", c->x86);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
970

34048c9e9   Paolo Ciarrocchi   x86: coding style...
971
  	if (c->x86_mask || c->cpuid_level >= 0)
9d31d35b5   Yinghai Lu   x86: order functi...
972
973
  		printk(KERN_CONT " stepping %02x
  ", c->x86_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
  	else
9d31d35b5   Yinghai Lu   x86: order functi...
975
976
  		printk(KERN_CONT "
  ");
a0854a46c   Yinghai Lu   x86: make 32bit s...
977
978
979
980
981
982
983
984
  
  #ifdef CONFIG_SMP
  	if (c->cpu_index < show_msr)
  		print_cpu_msr();
  #else
  	if (show_msr)
  		print_cpu_msr();
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985
  }
ac72e7888   Andi Kleen   x86: add generic ...
986
987
988
  static __init int setup_disablecpuid(char *arg)
  {
  	int bit;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
989

ac72e7888   Andi Kleen   x86: add generic ...
990
991
992
993
  	if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  		setup_clear_cpu_cap(bit);
  	else
  		return 0;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
994

ac72e7888   Andi Kleen   x86: add generic ...
995
996
997
  	return 1;
  }
  __setup("clearcpuid=", setup_disablecpuid);
d5494d4f5   Yinghai Lu   x86: cpu/common*....
998
  #ifdef CONFIG_X86_64
9ff809429   Cyrill Gorcunov   x86: Clean up idt...
999
  struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
228bdaa95   Steven Rostedt   x86: Keep current...
1000
1001
  struct desc_ptr nmi_idt_descr = { NR_VECTORS * 16 - 1,
  				    (unsigned long) nmi_idt_table };
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1002

947e76cdc   Brian Gerst   x86: move stack_c...
1003
1004
  DEFINE_PER_CPU_FIRST(union irq_stack_union,
  		     irq_stack_union) __aligned(PAGE_SIZE);
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1005

bdf977b37   Tejun Heo   x86, percpu: Coll...
1006
1007
1008
1009
1010
1011
1012
  /*
   * The following four percpu variables are hot.  Align current_task to
   * cacheline size such that all four fall in the same cacheline.
   */
  DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
  	&init_task;
  EXPORT_PER_CPU_SYMBOL(current_task);
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1013

9af45651f   Brian Gerst   x86-64: Move kern...
1014
1015
1016
  DEFINE_PER_CPU(unsigned long, kernel_stack) =
  	(unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
  EXPORT_PER_CPU_SYMBOL(kernel_stack);
bdf977b37   Tejun Heo   x86, percpu: Coll...
1017
1018
  DEFINE_PER_CPU(char *, irq_stack_ptr) =
  	init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
568955307   Brian Gerst   x86-64: Move irqc...
1019
  DEFINE_PER_CPU(unsigned int, irq_count) = -1;
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1020

0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  /*
   * Special IST stacks which the CPU switches to when it calls
   * an IST-marked descriptor entry. Up to 7 stacks (hardware
   * limit), all of them are 4K, except the debug stack which
   * is 8K.
   */
  static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
  	  [0 ... N_EXCEPTION_STACKS - 1]	= EXCEPTION_STKSZ,
  	  [DEBUG_STACK - 1]			= DEBUG_STKSZ
  };
92d65b237   Brian Gerst   x86-64: Convert e...
1031
  static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
3e352aa8e   Tejun Heo   x86, percpu: Fix ...
1032
  	[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]);
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1033

d5494d4f5   Yinghai Lu   x86: cpu/common*....
1034
1035
  /* May not be marked __init: used by software suspend */
  void syscall_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
  {
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1037
1038
1039
1040
1041
1042
1043
1044
  	/*
  	 * LSTAR and STAR live in a bit strange symbiosis.
  	 * They both write to the same internal register. STAR allows to
  	 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  	 */
  	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
  	wrmsrl(MSR_LSTAR, system_call);
  	wrmsrl(MSR_CSTAR, ignore_sysret);
03ae5768b   Thomas Petazzoni   x86: use ELF sect...
1045

d5494d4f5   Yinghai Lu   x86: cpu/common*....
1046
1047
1048
  #ifdef CONFIG_IA32_EMULATION
  	syscall32_cpu_init();
  #endif
03ae5768b   Thomas Petazzoni   x86: use ELF sect...
1049

d5494d4f5   Yinghai Lu   x86: cpu/common*....
1050
1051
1052
  	/* Flags to clear on syscall */
  	wrmsrl(MSR_SYSCALL_MASK,
  	       X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1053
  }
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1054

d5494d4f5   Yinghai Lu   x86: cpu/common*....
1055
1056
1057
1058
1059
1060
1061
  unsigned long kernel_eflags;
  
  /*
   * Copies of the original ist values from the tss are only accessed during
   * debugging, no special alignment required.
   */
  DEFINE_PER_CPU(struct orig_ist, orig_ist);
228bdaa95   Steven Rostedt   x86: Keep current...
1062
  static DEFINE_PER_CPU(unsigned long, debug_stack_addr);
42181186a   Steven Rostedt   x86: Add counter ...
1063
  DEFINE_PER_CPU(int, debug_stack_usage);
228bdaa95   Steven Rostedt   x86: Keep current...
1064
1065
1066
  
  int is_debug_stack(unsigned long addr)
  {
42181186a   Steven Rostedt   x86: Add counter ...
1067
1068
1069
  	return __get_cpu_var(debug_stack_usage) ||
  		(addr <= __get_cpu_var(debug_stack_addr) &&
  		 addr > (__get_cpu_var(debug_stack_addr) - DEBUG_STKSZ));
228bdaa95   Steven Rostedt   x86: Keep current...
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  }
  
  void debug_stack_set_zero(void)
  {
  	load_idt((const struct desc_ptr *)&nmi_idt_descr);
  }
  
  void debug_stack_reset(void)
  {
  	load_idt((const struct desc_ptr *)&idt_descr);
  }
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1081
  #else	/* CONFIG_X86_64 */
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1082

bdf977b37   Tejun Heo   x86, percpu: Coll...
1083
1084
  DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  EXPORT_PER_CPU_SYMBOL(current_task);
60a5317ff   Tejun Heo   x86: implement x8...
1085
  #ifdef CONFIG_CC_STACKPROTECTOR
53f824520   Jeremy Fitzhardinge   x86/i386: Put ali...
1086
  DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
60a5317ff   Tejun Heo   x86: implement x8...
1087
  #endif
d5494d4f5   Yinghai Lu   x86: cpu/common*....
1088

60a5317ff   Tejun Heo   x86: implement x8...
1089
  /* Make sure %fs and %gs are initialized properly in idle threads */
6b2fb3c65   Adrian Bunk   idle_regs() must ...
1090
  struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
f95d47caa   Jeremy Fitzhardinge   [PATCH] i386: Use...
1091
1092
  {
  	memset(regs, 0, sizeof(struct pt_regs));
65ea5b034   H. Peter Anvin   x86: rename the s...
1093
  	regs->fs = __KERNEL_PERCPU;
60a5317ff   Tejun Heo   x86: implement x8...
1094
  	regs->gs = __KERNEL_STACK_CANARY;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1095

f95d47caa   Jeremy Fitzhardinge   [PATCH] i386: Use...
1096
1097
  	return regs;
  }
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1098
  #endif	/* CONFIG_X86_64 */
c5413fbe8   Jeremy Fitzhardinge   [PATCH] i386: Fix...
1099

d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1100
  /*
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
   * Clear all 6 debug registers:
   */
  static void clear_all_debug_regs(void)
  {
  	int i;
  
  	for (i = 0; i < 8; i++) {
  		/* Ignore db4, db5 */
  		if ((i == 4) || (i == 5))
  			continue;
  
  		set_debugreg(0, i);
  	}
  }
c5413fbe8   Jeremy Fitzhardinge   [PATCH] i386: Fix...
1115

0bb9fef91   Jason Wessel   x86,early dr regs...
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  #ifdef CONFIG_KGDB
  /*
   * Restore debug regs if using kgdbwait and you have a kernel debugger
   * connection established.
   */
  static void dbg_restore_debug_regs(void)
  {
  	if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break))
  		arch_kgdb_ops.correct_hw_break();
  }
  #else /* ! CONFIG_KGDB */
  #define dbg_restore_debug_regs()
  #endif /* ! CONFIG_KGDB */
d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1129
  /*
64be4c1c2   Daniel J Blueman   x86: Add x86_init...
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
   * Prints an error where the NUMA and configured core-number mismatch and the
   * platform didn't override this to fix it up
   */
  void __cpuinit x86_default_fixup_cpu_id(struct cpuinfo_x86 *c, int node)
  {
  	pr_err("NUMA core number %d differs from configured core number %d
  ", node, c->phys_proc_id);
  }
  
  /*
d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1140
1141
1142
1143
   * cpu_init() initializes state that is per-CPU. Some data is already
   * initialized (naturally) in the bootstrap process, such as the GDT
   * and IDT. We reload them nevertheless, this function acts as a
   * 'CPU state barrier', nothing should get across.
1ba76586f   Yinghai Lu   x86: cpu/common*....
1144
   * A lot of state is already set up in PDA init for 64 bit
d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1145
   */
1ba76586f   Yinghai Lu   x86: cpu/common*....
1146
  #ifdef CONFIG_X86_64
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1147

1ba76586f   Yinghai Lu   x86: cpu/common*....
1148
1149
  void __cpuinit cpu_init(void)
  {
0fe1e0095   Tejun Heo   percpu: make perc...
1150
  	struct orig_ist *oist;
1ba76586f   Yinghai Lu   x86: cpu/common*....
1151
  	struct task_struct *me;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1152
1153
1154
  	struct tss_struct *t;
  	unsigned long v;
  	int cpu;
1ba76586f   Yinghai Lu   x86: cpu/common*....
1155
  	int i;
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1156
1157
  	cpu = stack_smp_processor_id();
  	t = &per_cpu(init_tss, cpu);
0fe1e0095   Tejun Heo   percpu: make perc...
1158
  	oist = &per_cpu(orig_ist, cpu);
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1159

e7a22c1eb   Brian Gerst   x86-64: Move node...
1160
  #ifdef CONFIG_NUMA
e534c7c5f   Lee Schermerhorn   numa: x86_64: use...
1161
1162
1163
  	if (cpu != 0 && percpu_read(numa_node) == 0 &&
  	    early_cpu_to_node(cpu) != NUMA_NO_NODE)
  		set_numa_node(early_cpu_to_node(cpu));
e7a22c1eb   Brian Gerst   x86-64: Move node...
1164
  #endif
1ba76586f   Yinghai Lu   x86: cpu/common*....
1165
1166
  
  	me = current;
c2d1cec1c   Mike Travis   x86: cleanup rema...
1167
  	if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
1ba76586f   Yinghai Lu   x86: cpu/common*....
1168
1169
  		panic("CPU#%d already initialized!
  ", cpu);
2eaad1fdd   Mike Travis   x86: Limit the nu...
1170
1171
  	pr_debug("Initializing CPU#%d
  ", cpu);
1ba76586f   Yinghai Lu   x86: cpu/common*....
1172
1173
1174
1175
1176
1177
1178
  
  	clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  
  	/*
  	 * Initialize the per-CPU GDT with the boot GDT,
  	 * and set up the GDT descriptor:
  	 */
552be871e   Brian Gerst   x86: pass in cpu ...
1179
  	switch_to_new_gdt(cpu);
2697fbd5f   Brian Gerst   x86: load new GDT...
1180
  	loadsegment(fs, 0);
1ba76586f   Yinghai Lu   x86: cpu/common*....
1181
1182
1183
1184
1185
1186
1187
1188
  	load_idt((const struct desc_ptr *)&idt_descr);
  
  	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  	syscall_init();
  
  	wrmsrl(MSR_FS_BASE, 0);
  	wrmsrl(MSR_KERNEL_GS_BASE, 0);
  	barrier();
4763ed4d4   H. Peter Anvin   x86, mm: Clean up...
1189
  	x86_configure_nx();
06cd9a7dc   Yinghai Lu   x86: add x2apic c...
1190
  	if (cpu != 0)
1ba76586f   Yinghai Lu   x86: cpu/common*....
1191
1192
1193
1194
1195
  		enable_x2apic();
  
  	/*
  	 * set up and load the per-CPU TSS
  	 */
0fe1e0095   Tejun Heo   percpu: make perc...
1196
  	if (!oist->ist[0]) {
92d65b237   Brian Gerst   x86-64: Convert e...
1197
  		char *estacks = per_cpu(exception_stacks, cpu);
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1198

1ba76586f   Yinghai Lu   x86: cpu/common*....
1199
  		for (v = 0; v < N_EXCEPTION_STACKS; v++) {
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1200
  			estacks += exception_stack_sizes[v];
0fe1e0095   Tejun Heo   percpu: make perc...
1201
  			oist->ist[v] = t->x86_tss.ist[v] =
1ba76586f   Yinghai Lu   x86: cpu/common*....
1202
  					(unsigned long)estacks;
228bdaa95   Steven Rostedt   x86: Keep current...
1203
1204
  			if (v == DEBUG_STACK-1)
  				per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks;
1ba76586f   Yinghai Lu   x86: cpu/common*....
1205
1206
1207
1208
  		}
  	}
  
  	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
0f3fa48a7   Ingo Molnar   x86: cpu/common.c...
1209

1ba76586f   Yinghai Lu   x86: cpu/common*....
1210
1211
1212
1213
1214
1215
1216
1217
1218
  	/*
  	 * <= is required because the CPU will access up to
  	 * 8 bits beyond the end of the IO permission bitmap.
  	 */
  	for (i = 0; i <= IO_BITMAP_LONGS; i++)
  		t->io_bitmap[i] = ~0UL;
  
  	atomic_inc(&init_mm.mm_count);
  	me->active_mm = &init_mm;
8c5dfd255   Stoyan Gaydarov   x86: BUG to BUG_O...
1219
  	BUG_ON(me->mm);
1ba76586f   Yinghai Lu   x86: cpu/common*....
1220
1221
1222
1223
1224
1225
  	enter_lazy_tlb(&init_mm, me);
  
  	load_sp0(t, &current->thread);
  	set_tss_desc(cpu, t);
  	load_TR_desc();
  	load_LDT(&init_mm.context);
0bb9fef91   Jason Wessel   x86,early dr regs...
1226
1227
  	clear_all_debug_regs();
  	dbg_restore_debug_regs();
1ba76586f   Yinghai Lu   x86: cpu/common*....
1228
1229
  
  	fpu_init();
0e49bf66d   Robert Richter   x86, xsave: Separ...
1230
  	xsave_init();
1ba76586f   Yinghai Lu   x86: cpu/common*....
1231
1232
1233
1234
1235
1236
1237
1238
  
  	raw_local_save_flags(kernel_eflags);
  
  	if (is_uv_system())
  		uv_cpu_init();
  }
  
  #else
d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1239
  void __cpuinit cpu_init(void)
9ee79a3d3   James Bottomley   [PATCH] x86: fix ...
1240
  {
d2cbcc49e   Rusty Russell   [PATCH] i386: cle...
1241
1242
  	int cpu = smp_processor_id();
  	struct task_struct *curr = current;
34048c9e9   Paolo Ciarrocchi   x86: coding style...
1243
  	struct tss_struct *t = &per_cpu(init_tss, cpu);
9ee79a3d3   James Bottomley   [PATCH] x86: fix ...
1244
  	struct thread_struct *thread = &curr->thread;
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1245

c2d1cec1c   Mike Travis   x86: cleanup rema...
1246
  	if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1247
1248
  		printk(KERN_WARNING "CPU#%d already initialized!
  ", cpu);
9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
1249
1250
  		for (;;)
  			local_irq_enable();
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1251
1252
1253
1254
1255
1256
1257
  	}
  
  	printk(KERN_INFO "Initializing CPU#%d
  ", cpu);
  
  	if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  		clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1258

4d37e7e3f   Zachary Amsden   [PATCH] i386: inl...
1259
  	load_idt(&idt_descr);
552be871e   Brian Gerst   x86: pass in cpu ...
1260
  	switch_to_new_gdt(cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1261

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1262
1263
1264
1265
  	/*
  	 * Set up and load the per-CPU TSS and LDT
  	 */
  	atomic_inc(&init_mm.mm_count);
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1266
  	curr->active_mm = &init_mm;
8c5dfd255   Stoyan Gaydarov   x86: BUG to BUG_O...
1267
  	BUG_ON(curr->mm);
621111958   Jeremy Fitzhardinge   [PATCH] i386: Ini...
1268
  	enter_lazy_tlb(&init_mm, curr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1269

faca62273   H. Peter Anvin   x86: use generic ...
1270
  	load_sp0(t, thread);
34048c9e9   Paolo Ciarrocchi   x86: coding style...
1271
  	set_tss_desc(cpu, t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272
1273
  	load_TR_desc();
  	load_LDT(&init_mm.context);
f9a196b8d   Thomas Gleixner   x86: initialize i...
1274
  	t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
22c4e3084   Matt Mackall   [PATCH] tiny: Mak...
1275
  #ifdef CONFIG_DOUBLEFAULT
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
1277
  	/* Set up doublefault TSS pointer in the GDT */
  	__set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
22c4e3084   Matt Mackall   [PATCH] tiny: Mak...
1278
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1279

9766cdbcb   Jaswinder Singh Rajput   x86: cpu/common.c...
1280
  	clear_all_debug_regs();
0bb9fef91   Jason Wessel   x86,early dr regs...
1281
  	dbg_restore_debug_regs();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282

0e49bf66d   Robert Richter   x86, xsave: Separ...
1283
  	fpu_init();
dc1e35c6e   Suresh Siddha   x86, xsave: enabl...
1284
  	xsave_init();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1285
  }
1ba76586f   Yinghai Lu   x86: cpu/common*....
1286
  #endif