blob: 87466b6d3b9297d828eb22df9bd6870d744ce2d7 [file] [log] [blame]
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +01001#ifndef __ASM_X86_PROCESSOR_H
2#define __ASM_X86_PROCESSOR_H
3
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004#include <asm/processor-flags.h>
5
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +01006/* Forward declaration, a strange C thing */
7struct task_struct;
8struct mm_struct;
9
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010010#include <asm/page.h>
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +010011#include <asm/percpu.h>
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010012#include <asm/system.h>
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010013#include <asm/percpu.h>
14#include <linux/cpumask.h>
15#include <linux/cache.h>
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010016
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010017/*
18 * Default implementation of macro that returns current
19 * instruction pointer ("program counter").
20 */
21static inline void *current_text_addr(void)
22{
23 void *pc;
24 asm volatile("mov $1f,%0\n1:":"=r" (pc));
25 return pc;
26}
27
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010028#ifdef CONFIG_X86_VSMP
29#define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
30#define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
31#else
32#define ARCH_MIN_TASKALIGN 16
33#define ARCH_MIN_MMSTRUCT_ALIGN 0
34#endif
35
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010036/*
37 * CPU type and hardware bug flags. Kept separately for each CPU.
38 * Members of this structure are referenced in head.S, so think twice
39 * before touching them. [mj]
40 */
41
42struct cpuinfo_x86 {
43 __u8 x86; /* CPU family */
44 __u8 x86_vendor; /* CPU vendor */
45 __u8 x86_model;
46 __u8 x86_mask;
47#ifdef CONFIG_X86_32
48 char wp_works_ok; /* It doesn't on 386's */
49 char hlt_works_ok; /* Problems on some 486Dx4's and old 386's */
50 char hard_math;
51 char rfu;
52 char fdiv_bug;
53 char f00f_bug;
54 char coma_bug;
55 char pad0;
56#else
57 /* number of 4K pages in DTLB/ITLB combined(in pages)*/
58 int x86_tlbsize;
59 __u8 x86_virt_bits, x86_phys_bits;
60 /* cpuid returned core id bits */
61 __u8 x86_coreid_bits;
62 /* Max extended CPUID function supported */
63 __u32 extended_cpuid_level;
64#endif
65 int cpuid_level; /* Maximum supported CPUID level, -1=no CPUID */
66 __u32 x86_capability[NCAPINTS];
67 char x86_vendor_id[16];
68 char x86_model_id[64];
69 int x86_cache_size; /* in KB - valid for CPUS which support this
70 call */
71 int x86_cache_alignment; /* In bytes */
72 int x86_power;
73 unsigned long loops_per_jiffy;
74#ifdef CONFIG_SMP
75 cpumask_t llc_shared_map; /* cpus sharing the last level cache */
76#endif
77 unsigned char x86_max_cores; /* cpuid returned max cores value */
78 unsigned char apicid;
79 unsigned short x86_clflush_size;
80#ifdef CONFIG_SMP
81 unsigned char booted_cores; /* number of cores as seen by OS */
82 __u8 phys_proc_id; /* Physical processor id. */
83 __u8 cpu_core_id; /* Core id */
84 __u8 cpu_index; /* index into per_cpu list */
85#endif
86} __attribute__((__aligned__(SMP_CACHE_BYTES)));
87
88#define X86_VENDOR_INTEL 0
89#define X86_VENDOR_CYRIX 1
90#define X86_VENDOR_AMD 2
91#define X86_VENDOR_UMC 3
92#define X86_VENDOR_NEXGEN 4
93#define X86_VENDOR_CENTAUR 5
94#define X86_VENDOR_TRANSMETA 7
95#define X86_VENDOR_NSC 8
96#define X86_VENDOR_NUM 9
97#define X86_VENDOR_UNKNOWN 0xff
98
99extern struct cpuinfo_x86 boot_cpu_data;
100
101#ifdef CONFIG_SMP
102DECLARE_PER_CPU(struct cpuinfo_x86, cpu_info);
103#define cpu_data(cpu) per_cpu(cpu_info, cpu)
104#define current_cpu_data cpu_data(smp_processor_id())
105#else
106#define cpu_data(cpu) boot_cpu_data
107#define current_cpu_data boot_cpu_data
108#endif
109
110extern void print_cpu_info(struct cpuinfo_x86 *);
111extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
112extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
113extern unsigned short num_cache_leaves;
114
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100115static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
116 unsigned int *ecx, unsigned int *edx)
117{
118 /* ecx is often an input as well as an output. */
119 __asm__("cpuid"
120 : "=a" (*eax),
121 "=b" (*ebx),
122 "=c" (*ecx),
123 "=d" (*edx)
124 : "0" (*eax), "2" (*ecx));
125}
126
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +0100127static inline void load_cr3(pgd_t *pgdir)
128{
129 write_cr3(__pa(pgdir));
130}
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100131
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200132#ifdef CONFIG_X86_32
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100133/* This is the TSS defined by the hardware. */
134struct x86_hw_tss {
135 unsigned short back_link, __blh;
136 unsigned long sp0;
137 unsigned short ss0, __ss0h;
138 unsigned long sp1;
139 unsigned short ss1, __ss1h; /* ss1 caches MSR_IA32_SYSENTER_CS */
140 unsigned long sp2;
141 unsigned short ss2, __ss2h;
142 unsigned long __cr3;
143 unsigned long ip;
144 unsigned long flags;
145 unsigned long ax, cx, dx, bx;
146 unsigned long sp, bp, si, di;
147 unsigned short es, __esh;
148 unsigned short cs, __csh;
149 unsigned short ss, __ssh;
150 unsigned short ds, __dsh;
151 unsigned short fs, __fsh;
152 unsigned short gs, __gsh;
153 unsigned short ldt, __ldth;
154 unsigned short trace, io_bitmap_base;
155} __attribute__((packed));
156#else
157struct x86_hw_tss {
158 u32 reserved1;
159 u64 sp0;
160 u64 sp1;
161 u64 sp2;
162 u64 reserved2;
163 u64 ist[7];
164 u32 reserved3;
165 u32 reserved4;
166 u16 reserved5;
167 u16 io_bitmap_base;
168} __attribute__((packed)) ____cacheline_aligned;
169#endif
170
171/*
172 * Size of io_bitmap.
173 */
174#define IO_BITMAP_BITS 65536
175#define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
176#define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
177#define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
178#define INVALID_IO_BITMAP_OFFSET 0x8000
179#define INVALID_IO_BITMAP_OFFSET_LAZY 0x9000
180
181struct tss_struct {
182 struct x86_hw_tss x86_tss;
183
184 /*
185 * The extra 1 is there because the CPU will access an
186 * additional byte beyond the end of the IO permission
187 * bitmap. The extra byte must be all 1 bits, and must
188 * be within the limit.
189 */
190 unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
191 /*
192 * Cache the current maximum and the last task that used the bitmap:
193 */
194 unsigned long io_bitmap_max;
195 struct thread_struct *io_bitmap_owner;
196 /*
197 * pads the TSS to be cacheline-aligned (size is 0x100)
198 */
199 unsigned long __cacheline_filler[35];
200 /*
201 * .. and then another 0x100 bytes for emergency kernel stack
202 */
203 unsigned long stack[64];
204} __attribute__((packed));
205
206DECLARE_PER_CPU(struct tss_struct, init_tss);
207
208#ifdef CONFIG_X86_32
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200209# include "processor_32.h"
210#else
211# include "processor_64.h"
212#endif
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100213
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100214extern void print_cpu_info(struct cpuinfo_x86 *);
215extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
216extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
217extern unsigned short num_cache_leaves;
218
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100219struct thread_struct {
220/* cached TLS descriptors. */
221 struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
222 unsigned long sp0;
223 unsigned long sp;
224#ifdef CONFIG_X86_32
225 unsigned long sysenter_cs;
226#else
227 unsigned long usersp; /* Copy from PDA */
228 unsigned short es, ds, fsindex, gsindex;
229#endif
230 unsigned long ip;
231 unsigned long fs;
232 unsigned long gs;
233/* Hardware debugging registers */
234 unsigned long debugreg0;
235 unsigned long debugreg1;
236 unsigned long debugreg2;
237 unsigned long debugreg3;
238 unsigned long debugreg6;
239 unsigned long debugreg7;
240/* fault info */
241 unsigned long cr2, trap_no, error_code;
242/* floating point info */
243 union i387_union i387 __attribute__((aligned(16)));;
244#ifdef CONFIG_X86_32
245/* virtual 86 mode info */
246 struct vm86_struct __user *vm86_info;
247 unsigned long screen_bitmap;
248 unsigned long v86flags, v86mask, saved_sp0;
249 unsigned int saved_fs, saved_gs;
250#endif
251/* IO permissions */
252 unsigned long *io_bitmap_ptr;
253 unsigned long iopl;
254/* max allowed port in the bitmap, in bytes: */
255 unsigned io_bitmap_max;
256/* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
257 unsigned long debugctlmsr;
258/* Debug Store - if not 0 points to a DS Save Area configuration;
259 * goes into MSR_IA32_DS_AREA */
260 unsigned long ds_area_msr;
261};
262
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100263static inline unsigned long native_get_debugreg(int regno)
264{
265 unsigned long val = 0; /* Damn you, gcc! */
266
267 switch (regno) {
268 case 0:
269 asm("mov %%db0, %0" :"=r" (val)); break;
270 case 1:
271 asm("mov %%db1, %0" :"=r" (val)); break;
272 case 2:
273 asm("mov %%db2, %0" :"=r" (val)); break;
274 case 3:
275 asm("mov %%db3, %0" :"=r" (val)); break;
276 case 6:
277 asm("mov %%db6, %0" :"=r" (val)); break;
278 case 7:
279 asm("mov %%db7, %0" :"=r" (val)); break;
280 default:
281 BUG();
282 }
283 return val;
284}
285
286static inline void native_set_debugreg(int regno, unsigned long value)
287{
288 switch (regno) {
289 case 0:
290 asm("mov %0,%%db0" : /* no output */ :"r" (value));
291 break;
292 case 1:
293 asm("mov %0,%%db1" : /* no output */ :"r" (value));
294 break;
295 case 2:
296 asm("mov %0,%%db2" : /* no output */ :"r" (value));
297 break;
298 case 3:
299 asm("mov %0,%%db3" : /* no output */ :"r" (value));
300 break;
301 case 6:
302 asm("mov %0,%%db6" : /* no output */ :"r" (value));
303 break;
304 case 7:
305 asm("mov %0,%%db7" : /* no output */ :"r" (value));
306 break;
307 default:
308 BUG();
309 }
310}
311
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100312/*
313 * Set IOPL bits in EFLAGS from given mask
314 */
315static inline void native_set_iopl_mask(unsigned mask)
316{
317#ifdef CONFIG_X86_32
318 unsigned int reg;
319 __asm__ __volatile__ ("pushfl;"
320 "popl %0;"
321 "andl %1, %0;"
322 "orl %2, %0;"
323 "pushl %0;"
324 "popfl"
325 : "=&r" (reg)
326 : "i" (~X86_EFLAGS_IOPL), "r" (mask));
327#endif
328}
329
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100330static inline void native_load_sp0(struct tss_struct *tss,
331 struct thread_struct *thread)
332{
333 tss->x86_tss.sp0 = thread->sp0;
334#ifdef CONFIG_X86_32
335 /* Only happens when SEP is enabled, no need to test "SEP"arately */
336 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
337 tss->x86_tss.ss1 = thread->sysenter_cs;
338 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
339 }
340#endif
341}
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100342
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100343#ifdef CONFIG_PARAVIRT
344#include <asm/paravirt.h>
345#else
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100346#define __cpuid native_cpuid
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100347#define paravirt_enabled() 0
348
349/*
350 * These special macros can be used to get or set a debugging register
351 */
352#define get_debugreg(var, register) \
353 (var) = native_get_debugreg(register)
354#define set_debugreg(value, register) \
355 native_set_debugreg(register, value)
356
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100357static inline void load_sp0(struct tss_struct *tss,
358 struct thread_struct *thread)
359{
360 native_load_sp0(tss, thread);
361}
362
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100363#define set_iopl_mask native_set_iopl_mask
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100364#endif /* CONFIG_PARAVIRT */
365
366/*
367 * Save the cr4 feature set we're using (ie
368 * Pentium 4MB enable and PPro Global page
369 * enable), so that any CPU's that boot up
370 * after us can get the correct flags.
371 */
372extern unsigned long mmu_cr4_features;
373
374static inline void set_in_cr4(unsigned long mask)
375{
376 unsigned cr4;
377 mmu_cr4_features |= mask;
378 cr4 = read_cr4();
379 cr4 |= mask;
380 write_cr4(cr4);
381}
382
383static inline void clear_in_cr4(unsigned long mask)
384{
385 unsigned cr4;
386 mmu_cr4_features &= ~mask;
387 cr4 = read_cr4();
388 cr4 &= ~mask;
389 write_cr4(cr4);
390}
391
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100392struct microcode_header {
393 unsigned int hdrver;
394 unsigned int rev;
395 unsigned int date;
396 unsigned int sig;
397 unsigned int cksum;
398 unsigned int ldrver;
399 unsigned int pf;
400 unsigned int datasize;
401 unsigned int totalsize;
402 unsigned int reserved[3];
403};
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100404
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100405struct microcode {
406 struct microcode_header hdr;
407 unsigned int bits[0];
408};
409
410typedef struct microcode microcode_t;
411typedef struct microcode_header microcode_header_t;
412
413/* microcode format is extended from prescott processors */
414struct extended_signature {
415 unsigned int sig;
416 unsigned int pf;
417 unsigned int cksum;
418};
419
420struct extended_sigtable {
421 unsigned int count;
422 unsigned int cksum;
423 unsigned int reserved[3];
424 struct extended_signature sigs[0];
425};
426
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100427typedef struct {
428 unsigned long seg;
429} mm_segment_t;
430
431
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100432/*
433 * create a kernel thread without removing it from tasklists
434 */
435extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
436
437/* Free all resources held by a thread. */
438extern void release_thread(struct task_struct *);
439
440/* Prepare to copy thread state - unlazy all lazy status */
441extern void prepare_to_copy(struct task_struct *tsk);
442
443unsigned long get_wchan(struct task_struct *p);
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100444
445/*
446 * Generic CPUID function
447 * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
448 * resulting in stale register contents being returned.
449 */
450static inline void cpuid(unsigned int op,
451 unsigned int *eax, unsigned int *ebx,
452 unsigned int *ecx, unsigned int *edx)
453{
454 *eax = op;
455 *ecx = 0;
456 __cpuid(eax, ebx, ecx, edx);
457}
458
459/* Some CPUID calls want 'count' to be placed in ecx */
460static inline void cpuid_count(unsigned int op, int count,
461 unsigned int *eax, unsigned int *ebx,
462 unsigned int *ecx, unsigned int *edx)
463{
464 *eax = op;
465 *ecx = count;
466 __cpuid(eax, ebx, ecx, edx);
467}
468
469/*
470 * CPUID functions returning a single datum
471 */
472static inline unsigned int cpuid_eax(unsigned int op)
473{
474 unsigned int eax, ebx, ecx, edx;
475
476 cpuid(op, &eax, &ebx, &ecx, &edx);
477 return eax;
478}
479static inline unsigned int cpuid_ebx(unsigned int op)
480{
481 unsigned int eax, ebx, ecx, edx;
482
483 cpuid(op, &eax, &ebx, &ecx, &edx);
484 return ebx;
485}
486static inline unsigned int cpuid_ecx(unsigned int op)
487{
488 unsigned int eax, ebx, ecx, edx;
489
490 cpuid(op, &eax, &ebx, &ecx, &edx);
491 return ecx;
492}
493static inline unsigned int cpuid_edx(unsigned int op)
494{
495 unsigned int eax, ebx, ecx, edx;
496
497 cpuid(op, &eax, &ebx, &ecx, &edx);
498 return edx;
499}
500
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100501/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
502static inline void rep_nop(void)
503{
504 __asm__ __volatile__("rep;nop": : :"memory");
505}
506
507/* Stop speculative execution */
508static inline void sync_core(void)
509{
510 int tmp;
511 asm volatile("cpuid" : "=a" (tmp) : "0" (1)
512 : "ebx", "ecx", "edx", "memory");
513}
514
515#define cpu_relax() rep_nop()
516
517static inline void __monitor(const void *eax, unsigned long ecx,
518 unsigned long edx)
519{
520 /* "monitor %eax,%ecx,%edx;" */
521 asm volatile(
522 ".byte 0x0f,0x01,0xc8;"
523 : :"a" (eax), "c" (ecx), "d"(edx));
524}
525
526static inline void __mwait(unsigned long eax, unsigned long ecx)
527{
528 /* "mwait %eax,%ecx;" */
529 asm volatile(
530 ".byte 0x0f,0x01,0xc9;"
531 : :"a" (eax), "c" (ecx));
532}
533
534static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
535{
536 /* "mwait %eax,%ecx;" */
537 asm volatile(
538 "sti; .byte 0x0f,0x01,0xc9;"
539 : :"a" (eax), "c" (ecx));
540}
541
542extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
543
544extern int force_mwait;
545
546extern void select_idle_routine(const struct cpuinfo_x86 *c);
547
548extern unsigned long boot_option_idle_override;
549
550/* Boot loader type from the setup header */
551extern int bootloader_type;
552#define cache_line_size() (boot_cpu_data.x86_cache_alignment)
553
554#define HAVE_ARCH_PICK_MMAP_LAYOUT 1
555#define ARCH_HAS_PREFETCHW
556#define ARCH_HAS_SPINLOCK_PREFETCH
557
558#define spin_lock_prefetch(x) prefetchw(x)
559/* This decides where the kernel will search for a free chunk of vm
560 * space during mmap's.
561 */
562#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
563
564#define KSTK_EIP(task) (task_pt_regs(task)->ip)
565
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100566#endif