blob: e6fa06fee72ab7fbe79a1813bc9a88d5f7a64c3c [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 Costa2f66dcc2008-01-30 13:31:57 +010010#include <asm/vm86.h>
11#include <asm/math_emu.h>
12#include <asm/segment.h>
13#include <asm/page.h>
14#include <asm/types.h>
15#include <asm/sigcontext.h>
16#include <asm/current.h>
17#include <asm/cpufeature.h>
18#include <asm/system.h>
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010019#include <asm/page.h>
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +010020#include <asm/percpu.h>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010021#include <asm/msr.h>
22#include <asm/desc_defs.h>
23#include <linux/personality.h>
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010024#include <linux/cpumask.h>
25#include <linux/cache.h>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010026#include <linux/threads.h>
27#include <linux/init.h>
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010028
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010029/*
30 * Default implementation of macro that returns current
31 * instruction pointer ("program counter").
32 */
33static inline void *current_text_addr(void)
34{
35 void *pc;
36 asm volatile("mov $1f,%0\n1:":"=r" (pc));
37 return pc;
38}
39
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010040#ifdef CONFIG_X86_VSMP
41#define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
42#define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
43#else
44#define ARCH_MIN_TASKALIGN 16
45#define ARCH_MIN_MMSTRUCT_ALIGN 0
46#endif
47
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010048/*
49 * CPU type and hardware bug flags. Kept separately for each CPU.
50 * Members of this structure are referenced in head.S, so think twice
51 * before touching them. [mj]
52 */
53
54struct cpuinfo_x86 {
55 __u8 x86; /* CPU family */
56 __u8 x86_vendor; /* CPU vendor */
57 __u8 x86_model;
58 __u8 x86_mask;
59#ifdef CONFIG_X86_32
60 char wp_works_ok; /* It doesn't on 386's */
61 char hlt_works_ok; /* Problems on some 486Dx4's and old 386's */
62 char hard_math;
63 char rfu;
64 char fdiv_bug;
65 char f00f_bug;
66 char coma_bug;
67 char pad0;
68#else
69 /* number of 4K pages in DTLB/ITLB combined(in pages)*/
70 int x86_tlbsize;
71 __u8 x86_virt_bits, x86_phys_bits;
72 /* cpuid returned core id bits */
73 __u8 x86_coreid_bits;
74 /* Max extended CPUID function supported */
75 __u32 extended_cpuid_level;
76#endif
77 int cpuid_level; /* Maximum supported CPUID level, -1=no CPUID */
78 __u32 x86_capability[NCAPINTS];
79 char x86_vendor_id[16];
80 char x86_model_id[64];
81 int x86_cache_size; /* in KB - valid for CPUS which support this
82 call */
83 int x86_cache_alignment; /* In bytes */
84 int x86_power;
85 unsigned long loops_per_jiffy;
86#ifdef CONFIG_SMP
87 cpumask_t llc_shared_map; /* cpus sharing the last level cache */
88#endif
89 unsigned char x86_max_cores; /* cpuid returned max cores value */
90 unsigned char apicid;
91 unsigned short x86_clflush_size;
92#ifdef CONFIG_SMP
93 unsigned char booted_cores; /* number of cores as seen by OS */
94 __u8 phys_proc_id; /* Physical processor id. */
95 __u8 cpu_core_id; /* Core id */
96 __u8 cpu_index; /* index into per_cpu list */
97#endif
98} __attribute__((__aligned__(SMP_CACHE_BYTES)));
99
100#define X86_VENDOR_INTEL 0
101#define X86_VENDOR_CYRIX 1
102#define X86_VENDOR_AMD 2
103#define X86_VENDOR_UMC 3
104#define X86_VENDOR_NEXGEN 4
105#define X86_VENDOR_CENTAUR 5
106#define X86_VENDOR_TRANSMETA 7
107#define X86_VENDOR_NSC 8
108#define X86_VENDOR_NUM 9
109#define X86_VENDOR_UNKNOWN 0xff
110
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100111/*
112 * capabilities of CPUs
113 */
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100114extern struct cpuinfo_x86 boot_cpu_data;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100115extern struct cpuinfo_x86 new_cpu_data;
116extern struct tss_struct doublefault_tss;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100117
118#ifdef CONFIG_SMP
119DECLARE_PER_CPU(struct cpuinfo_x86, cpu_info);
120#define cpu_data(cpu) per_cpu(cpu_info, cpu)
121#define current_cpu_data cpu_data(smp_processor_id())
122#else
123#define cpu_data(cpu) boot_cpu_data
124#define current_cpu_data boot_cpu_data
125#endif
126
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100127void cpu_detect(struct cpuinfo_x86 *c);
128
129extern void identify_cpu(struct cpuinfo_x86 *);
130extern void identify_boot_cpu(void);
131extern void identify_secondary_cpu(struct cpuinfo_x86 *);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100132extern void print_cpu_info(struct cpuinfo_x86 *);
133extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
134extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
135extern unsigned short num_cache_leaves;
136
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100137#if defined(CONFIG_X86_HT) || defined(CONFIG_X86_64)
138extern void detect_ht(struct cpuinfo_x86 *c);
139#else
140static inline void detect_ht(struct cpuinfo_x86 *c) {}
141#endif
142
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100143static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
144 unsigned int *ecx, unsigned int *edx)
145{
146 /* ecx is often an input as well as an output. */
147 __asm__("cpuid"
148 : "=a" (*eax),
149 "=b" (*ebx),
150 "=c" (*ecx),
151 "=d" (*edx)
152 : "0" (*eax), "2" (*ecx));
153}
154
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +0100155static inline void load_cr3(pgd_t *pgdir)
156{
157 write_cr3(__pa(pgdir));
158}
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100159
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200160#ifdef CONFIG_X86_32
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100161/* This is the TSS defined by the hardware. */
162struct x86_hw_tss {
163 unsigned short back_link, __blh;
164 unsigned long sp0;
165 unsigned short ss0, __ss0h;
166 unsigned long sp1;
167 unsigned short ss1, __ss1h; /* ss1 caches MSR_IA32_SYSENTER_CS */
168 unsigned long sp2;
169 unsigned short ss2, __ss2h;
170 unsigned long __cr3;
171 unsigned long ip;
172 unsigned long flags;
173 unsigned long ax, cx, dx, bx;
174 unsigned long sp, bp, si, di;
175 unsigned short es, __esh;
176 unsigned short cs, __csh;
177 unsigned short ss, __ssh;
178 unsigned short ds, __dsh;
179 unsigned short fs, __fsh;
180 unsigned short gs, __gsh;
181 unsigned short ldt, __ldth;
182 unsigned short trace, io_bitmap_base;
183} __attribute__((packed));
184#else
185struct x86_hw_tss {
186 u32 reserved1;
187 u64 sp0;
188 u64 sp1;
189 u64 sp2;
190 u64 reserved2;
191 u64 ist[7];
192 u32 reserved3;
193 u32 reserved4;
194 u16 reserved5;
195 u16 io_bitmap_base;
196} __attribute__((packed)) ____cacheline_aligned;
197#endif
198
199/*
200 * Size of io_bitmap.
201 */
202#define IO_BITMAP_BITS 65536
203#define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
204#define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
205#define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
206#define INVALID_IO_BITMAP_OFFSET 0x8000
207#define INVALID_IO_BITMAP_OFFSET_LAZY 0x9000
208
209struct tss_struct {
210 struct x86_hw_tss x86_tss;
211
212 /*
213 * The extra 1 is there because the CPU will access an
214 * additional byte beyond the end of the IO permission
215 * bitmap. The extra byte must be all 1 bits, and must
216 * be within the limit.
217 */
218 unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
219 /*
220 * Cache the current maximum and the last task that used the bitmap:
221 */
222 unsigned long io_bitmap_max;
223 struct thread_struct *io_bitmap_owner;
224 /*
225 * pads the TSS to be cacheline-aligned (size is 0x100)
226 */
227 unsigned long __cacheline_filler[35];
228 /*
229 * .. and then another 0x100 bytes for emergency kernel stack
230 */
231 unsigned long stack[64];
232} __attribute__((packed));
233
234DECLARE_PER_CPU(struct tss_struct, init_tss);
235
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100236/* Save the original ist values for checking stack pointers during debugging */
237struct orig_ist {
238 unsigned long ist[7];
239};
240
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100241#define MXCSR_DEFAULT 0x1f80
242
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100243struct i387_fsave_struct {
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100244 u32 cwd;
245 u32 swd;
246 u32 twd;
247 u32 fip;
248 u32 fcs;
249 u32 foo;
250 u32 fos;
251 u32 st_space[20]; /* 8*10 bytes for each FP-reg = 80 bytes */
252 u32 status; /* software status information */
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100253};
254
255struct i387_fxsave_struct {
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100256 u16 cwd;
257 u16 swd;
258 u16 twd;
259 u16 fop;
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100260 union {
261 struct {
262 u64 rip;
263 u64 rdp;
264 };
265 struct {
266 u32 fip;
267 u32 fcs;
268 u32 foo;
269 u32 fos;
270 };
271 };
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100272 u32 mxcsr;
273 u32 mxcsr_mask;
274 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
275 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
276 u32 padding[24];
277} __attribute__((aligned(16)));
278
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100279struct i387_soft_struct {
280 u32 cwd;
281 u32 swd;
282 u32 twd;
283 u32 fip;
284 u32 fcs;
285 u32 foo;
286 u32 fos;
287 u32 st_space[20]; /* 8*10 bytes for each FP-reg = 80 bytes */
288 u8 ftop, changed, lookahead, no_update, rm, alimit;
289 struct info *info;
290 u32 entry_eip;
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100291};
292
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100293union i387_union {
294 struct i387_fsave_struct fsave;
295 struct i387_fxsave_struct fxsave;
296 struct i387_soft_struct soft;
297};
298
299#ifdef CONFIG_X86_32
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100300/*
301 * the following now lives in the per cpu area:
302 * extern int cpu_llc_id[NR_CPUS];
303 */
304DECLARE_PER_CPU(u8, cpu_llc_id);
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100305#else
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100306DECLARE_PER_CPU(struct orig_ist, orig_ist);
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200307#endif
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100308
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100309extern void print_cpu_info(struct cpuinfo_x86 *);
310extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
311extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
312extern unsigned short num_cache_leaves;
313
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100314struct thread_struct {
315/* cached TLS descriptors. */
316 struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
317 unsigned long sp0;
318 unsigned long sp;
319#ifdef CONFIG_X86_32
320 unsigned long sysenter_cs;
321#else
322 unsigned long usersp; /* Copy from PDA */
323 unsigned short es, ds, fsindex, gsindex;
324#endif
325 unsigned long ip;
326 unsigned long fs;
327 unsigned long gs;
328/* Hardware debugging registers */
329 unsigned long debugreg0;
330 unsigned long debugreg1;
331 unsigned long debugreg2;
332 unsigned long debugreg3;
333 unsigned long debugreg6;
334 unsigned long debugreg7;
335/* fault info */
336 unsigned long cr2, trap_no, error_code;
337/* floating point info */
338 union i387_union i387 __attribute__((aligned(16)));;
339#ifdef CONFIG_X86_32
340/* virtual 86 mode info */
341 struct vm86_struct __user *vm86_info;
342 unsigned long screen_bitmap;
343 unsigned long v86flags, v86mask, saved_sp0;
344 unsigned int saved_fs, saved_gs;
345#endif
346/* IO permissions */
347 unsigned long *io_bitmap_ptr;
348 unsigned long iopl;
349/* max allowed port in the bitmap, in bytes: */
350 unsigned io_bitmap_max;
351/* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
352 unsigned long debugctlmsr;
353/* Debug Store - if not 0 points to a DS Save Area configuration;
354 * goes into MSR_IA32_DS_AREA */
355 unsigned long ds_area_msr;
356};
357
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100358static inline unsigned long native_get_debugreg(int regno)
359{
360 unsigned long val = 0; /* Damn you, gcc! */
361
362 switch (regno) {
363 case 0:
364 asm("mov %%db0, %0" :"=r" (val)); break;
365 case 1:
366 asm("mov %%db1, %0" :"=r" (val)); break;
367 case 2:
368 asm("mov %%db2, %0" :"=r" (val)); break;
369 case 3:
370 asm("mov %%db3, %0" :"=r" (val)); break;
371 case 6:
372 asm("mov %%db6, %0" :"=r" (val)); break;
373 case 7:
374 asm("mov %%db7, %0" :"=r" (val)); break;
375 default:
376 BUG();
377 }
378 return val;
379}
380
381static inline void native_set_debugreg(int regno, unsigned long value)
382{
383 switch (regno) {
384 case 0:
385 asm("mov %0,%%db0" : /* no output */ :"r" (value));
386 break;
387 case 1:
388 asm("mov %0,%%db1" : /* no output */ :"r" (value));
389 break;
390 case 2:
391 asm("mov %0,%%db2" : /* no output */ :"r" (value));
392 break;
393 case 3:
394 asm("mov %0,%%db3" : /* no output */ :"r" (value));
395 break;
396 case 6:
397 asm("mov %0,%%db6" : /* no output */ :"r" (value));
398 break;
399 case 7:
400 asm("mov %0,%%db7" : /* no output */ :"r" (value));
401 break;
402 default:
403 BUG();
404 }
405}
406
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100407/*
408 * Set IOPL bits in EFLAGS from given mask
409 */
410static inline void native_set_iopl_mask(unsigned mask)
411{
412#ifdef CONFIG_X86_32
413 unsigned int reg;
414 __asm__ __volatile__ ("pushfl;"
415 "popl %0;"
416 "andl %1, %0;"
417 "orl %2, %0;"
418 "pushl %0;"
419 "popfl"
420 : "=&r" (reg)
421 : "i" (~X86_EFLAGS_IOPL), "r" (mask));
422#endif
423}
424
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100425static inline void native_load_sp0(struct tss_struct *tss,
426 struct thread_struct *thread)
427{
428 tss->x86_tss.sp0 = thread->sp0;
429#ifdef CONFIG_X86_32
430 /* Only happens when SEP is enabled, no need to test "SEP"arately */
431 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
432 tss->x86_tss.ss1 = thread->sysenter_cs;
433 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
434 }
435#endif
436}
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100437
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100438#ifdef CONFIG_PARAVIRT
439#include <asm/paravirt.h>
440#else
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100441#define __cpuid native_cpuid
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100442#define paravirt_enabled() 0
443
444/*
445 * These special macros can be used to get or set a debugging register
446 */
447#define get_debugreg(var, register) \
448 (var) = native_get_debugreg(register)
449#define set_debugreg(value, register) \
450 native_set_debugreg(register, value)
451
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100452static inline void load_sp0(struct tss_struct *tss,
453 struct thread_struct *thread)
454{
455 native_load_sp0(tss, thread);
456}
457
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100458#define set_iopl_mask native_set_iopl_mask
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100459#endif /* CONFIG_PARAVIRT */
460
461/*
462 * Save the cr4 feature set we're using (ie
463 * Pentium 4MB enable and PPro Global page
464 * enable), so that any CPU's that boot up
465 * after us can get the correct flags.
466 */
467extern unsigned long mmu_cr4_features;
468
469static inline void set_in_cr4(unsigned long mask)
470{
471 unsigned cr4;
472 mmu_cr4_features |= mask;
473 cr4 = read_cr4();
474 cr4 |= mask;
475 write_cr4(cr4);
476}
477
478static inline void clear_in_cr4(unsigned long mask)
479{
480 unsigned cr4;
481 mmu_cr4_features &= ~mask;
482 cr4 = read_cr4();
483 cr4 &= ~mask;
484 write_cr4(cr4);
485}
486
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100487struct microcode_header {
488 unsigned int hdrver;
489 unsigned int rev;
490 unsigned int date;
491 unsigned int sig;
492 unsigned int cksum;
493 unsigned int ldrver;
494 unsigned int pf;
495 unsigned int datasize;
496 unsigned int totalsize;
497 unsigned int reserved[3];
498};
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100499
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100500struct microcode {
501 struct microcode_header hdr;
502 unsigned int bits[0];
503};
504
505typedef struct microcode microcode_t;
506typedef struct microcode_header microcode_header_t;
507
508/* microcode format is extended from prescott processors */
509struct extended_signature {
510 unsigned int sig;
511 unsigned int pf;
512 unsigned int cksum;
513};
514
515struct extended_sigtable {
516 unsigned int count;
517 unsigned int cksum;
518 unsigned int reserved[3];
519 struct extended_signature sigs[0];
520};
521
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100522typedef struct {
523 unsigned long seg;
524} mm_segment_t;
525
526
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100527/*
528 * create a kernel thread without removing it from tasklists
529 */
530extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
531
532/* Free all resources held by a thread. */
533extern void release_thread(struct task_struct *);
534
535/* Prepare to copy thread state - unlazy all lazy status */
536extern void prepare_to_copy(struct task_struct *tsk);
537
538unsigned long get_wchan(struct task_struct *p);
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100539
540/*
541 * Generic CPUID function
542 * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
543 * resulting in stale register contents being returned.
544 */
545static inline void cpuid(unsigned int op,
546 unsigned int *eax, unsigned int *ebx,
547 unsigned int *ecx, unsigned int *edx)
548{
549 *eax = op;
550 *ecx = 0;
551 __cpuid(eax, ebx, ecx, edx);
552}
553
554/* Some CPUID calls want 'count' to be placed in ecx */
555static inline void cpuid_count(unsigned int op, int count,
556 unsigned int *eax, unsigned int *ebx,
557 unsigned int *ecx, unsigned int *edx)
558{
559 *eax = op;
560 *ecx = count;
561 __cpuid(eax, ebx, ecx, edx);
562}
563
564/*
565 * CPUID functions returning a single datum
566 */
567static inline unsigned int cpuid_eax(unsigned int op)
568{
569 unsigned int eax, ebx, ecx, edx;
570
571 cpuid(op, &eax, &ebx, &ecx, &edx);
572 return eax;
573}
574static inline unsigned int cpuid_ebx(unsigned int op)
575{
576 unsigned int eax, ebx, ecx, edx;
577
578 cpuid(op, &eax, &ebx, &ecx, &edx);
579 return ebx;
580}
581static inline unsigned int cpuid_ecx(unsigned int op)
582{
583 unsigned int eax, ebx, ecx, edx;
584
585 cpuid(op, &eax, &ebx, &ecx, &edx);
586 return ecx;
587}
588static inline unsigned int cpuid_edx(unsigned int op)
589{
590 unsigned int eax, ebx, ecx, edx;
591
592 cpuid(op, &eax, &ebx, &ecx, &edx);
593 return edx;
594}
595
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100596/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
597static inline void rep_nop(void)
598{
599 __asm__ __volatile__("rep;nop": : :"memory");
600}
601
602/* Stop speculative execution */
603static inline void sync_core(void)
604{
605 int tmp;
606 asm volatile("cpuid" : "=a" (tmp) : "0" (1)
607 : "ebx", "ecx", "edx", "memory");
608}
609
610#define cpu_relax() rep_nop()
611
612static inline void __monitor(const void *eax, unsigned long ecx,
613 unsigned long edx)
614{
615 /* "monitor %eax,%ecx,%edx;" */
616 asm volatile(
617 ".byte 0x0f,0x01,0xc8;"
618 : :"a" (eax), "c" (ecx), "d"(edx));
619}
620
621static inline void __mwait(unsigned long eax, unsigned long ecx)
622{
623 /* "mwait %eax,%ecx;" */
624 asm volatile(
625 ".byte 0x0f,0x01,0xc9;"
626 : :"a" (eax), "c" (ecx));
627}
628
629static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
630{
631 /* "mwait %eax,%ecx;" */
632 asm volatile(
633 "sti; .byte 0x0f,0x01,0xc9;"
634 : :"a" (eax), "c" (ecx));
635}
636
637extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
638
639extern int force_mwait;
640
641extern void select_idle_routine(const struct cpuinfo_x86 *c);
642
643extern unsigned long boot_option_idle_override;
644
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100645extern void enable_sep_cpu(void);
646extern int sysenter_setup(void);
647
648/* Defined in head.S */
649extern struct desc_ptr early_gdt_descr;
650
651extern void cpu_set_gdt(int);
652extern void switch_to_new_gdt(void);
653extern void cpu_init(void);
654extern void init_gdt(int cpu);
655
656/* from system description table in BIOS. Mostly for MCA use, but
657 * others may find it useful. */
658extern unsigned int machine_id;
659extern unsigned int machine_submodel_id;
660extern unsigned int BIOS_revision;
661extern unsigned int mca_pentium_flag;
662
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100663/* Boot loader type from the setup header */
664extern int bootloader_type;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100665
666extern char ignore_fpu_irq;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100667#define cache_line_size() (boot_cpu_data.x86_cache_alignment)
668
Glauber de Oliveira Costaea5e3592008-01-30 13:31:40 +0100669/* generic versions from gas */
670#define GENERIC_NOP1 ".byte 0x90\n"
671#define GENERIC_NOP2 ".byte 0x89,0xf6\n"
672#define GENERIC_NOP3 ".byte 0x8d,0x76,0x00\n"
673#define GENERIC_NOP4 ".byte 0x8d,0x74,0x26,0x00\n"
674#define GENERIC_NOP5 GENERIC_NOP1 GENERIC_NOP4
675#define GENERIC_NOP6 ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00\n"
676#define GENERIC_NOP7 ".byte 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00\n"
677#define GENERIC_NOP8 GENERIC_NOP1 GENERIC_NOP7
678
679/* Opteron nops */
680#define K8_NOP1 GENERIC_NOP1
681#define K8_NOP2 ".byte 0x66,0x90\n"
682#define K8_NOP3 ".byte 0x66,0x66,0x90\n"
683#define K8_NOP4 ".byte 0x66,0x66,0x66,0x90\n"
684#define K8_NOP5 K8_NOP3 K8_NOP2
685#define K8_NOP6 K8_NOP3 K8_NOP3
686#define K8_NOP7 K8_NOP4 K8_NOP3
687#define K8_NOP8 K8_NOP4 K8_NOP4
688
689/* K7 nops */
690/* uses eax dependencies (arbitary choice) */
691#define K7_NOP1 GENERIC_NOP1
692#define K7_NOP2 ".byte 0x8b,0xc0\n"
693#define K7_NOP3 ".byte 0x8d,0x04,0x20\n"
694#define K7_NOP4 ".byte 0x8d,0x44,0x20,0x00\n"
695#define K7_NOP5 K7_NOP4 ASM_NOP1
696#define K7_NOP6 ".byte 0x8d,0x80,0,0,0,0\n"
697#define K7_NOP7 ".byte 0x8D,0x04,0x05,0,0,0,0\n"
698#define K7_NOP8 K7_NOP7 ASM_NOP1
699
700/* P6 nops */
701/* uses eax dependencies (Intel-recommended choice) */
702#define P6_NOP1 GENERIC_NOP1
703#define P6_NOP2 ".byte 0x66,0x90\n"
704#define P6_NOP3 ".byte 0x0f,0x1f,0x00\n"
705#define P6_NOP4 ".byte 0x0f,0x1f,0x40,0\n"
706#define P6_NOP5 ".byte 0x0f,0x1f,0x44,0x00,0\n"
707#define P6_NOP6 ".byte 0x66,0x0f,0x1f,0x44,0x00,0\n"
708#define P6_NOP7 ".byte 0x0f,0x1f,0x80,0,0,0,0\n"
709#define P6_NOP8 ".byte 0x0f,0x1f,0x84,0x00,0,0,0,0\n"
710
711#ifdef CONFIG_MK7
712#define ASM_NOP1 K7_NOP1
713#define ASM_NOP2 K7_NOP2
714#define ASM_NOP3 K7_NOP3
715#define ASM_NOP4 K7_NOP4
716#define ASM_NOP5 K7_NOP5
717#define ASM_NOP6 K7_NOP6
718#define ASM_NOP7 K7_NOP7
719#define ASM_NOP8 K7_NOP8
720#elif defined(CONFIG_M686) || defined(CONFIG_MPENTIUMII) || \
721 defined(CONFIG_MPENTIUMIII) || defined(CONFIG_MPENTIUMM) || \
722 defined(CONFIG_MCORE2) || defined(CONFIG_PENTIUM4) || \
723 defined(CONFIG_MPSC)
724#define ASM_NOP1 P6_NOP1
725#define ASM_NOP2 P6_NOP2
726#define ASM_NOP3 P6_NOP3
727#define ASM_NOP4 P6_NOP4
728#define ASM_NOP5 P6_NOP5
729#define ASM_NOP6 P6_NOP6
730#define ASM_NOP7 P6_NOP7
731#define ASM_NOP8 P6_NOP8
732#elif defined(CONFIG_MK8) || defined(CONFIG_X86_64)
733#define ASM_NOP1 K8_NOP1
734#define ASM_NOP2 K8_NOP2
735#define ASM_NOP3 K8_NOP3
736#define ASM_NOP4 K8_NOP4
737#define ASM_NOP5 K8_NOP5
738#define ASM_NOP6 K8_NOP6
739#define ASM_NOP7 K8_NOP7
740#define ASM_NOP8 K8_NOP8
741#else
742#define ASM_NOP1 GENERIC_NOP1
743#define ASM_NOP2 GENERIC_NOP2
744#define ASM_NOP3 GENERIC_NOP3
745#define ASM_NOP4 GENERIC_NOP4
746#define ASM_NOP5 GENERIC_NOP5
747#define ASM_NOP6 GENERIC_NOP6
748#define ASM_NOP7 GENERIC_NOP7
749#define ASM_NOP8 GENERIC_NOP8
750#endif
751
752#define ASM_NOP_MAX 8
753
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100754#define HAVE_ARCH_PICK_MMAP_LAYOUT 1
755#define ARCH_HAS_PREFETCHW
756#define ARCH_HAS_SPINLOCK_PREFETCH
757
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100758#ifdef CONFIG_X86_32
759#define BASE_PREFETCH ASM_NOP4
760#define ARCH_HAS_PREFETCH
761#else
762#define BASE_PREFETCH "prefetcht0 (%1)"
763#endif
764
765/* Prefetch instructions for Pentium III and AMD Athlon */
766/* It's not worth to care about 3dnow! prefetches for the K6
767 because they are microcoded there and very slow.
768 However we don't do prefetches for pre XP Athlons currently
769 That should be fixed. */
770static inline void prefetch(const void *x)
771{
772 alternative_input(BASE_PREFETCH,
773 "prefetchnta (%1)",
774 X86_FEATURE_XMM,
775 "r" (x));
776}
777
778/* 3dnow! prefetch to get an exclusive cache line. Useful for
779 spinlocks to avoid one state transition in the cache coherency protocol. */
780static inline void prefetchw(const void *x)
781{
782 alternative_input(BASE_PREFETCH,
783 "prefetchw (%1)",
784 X86_FEATURE_3DNOW,
785 "r" (x));
786}
787
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100788#define spin_lock_prefetch(x) prefetchw(x)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100789#ifdef CONFIG_X86_32
790/*
791 * User space process size: 3GB (default).
792 */
793#define TASK_SIZE (PAGE_OFFSET)
794
795#define INIT_THREAD { \
796 .sp0 = sizeof(init_stack) + (long)&init_stack, \
797 .vm86_info = NULL, \
798 .sysenter_cs = __KERNEL_CS, \
799 .io_bitmap_ptr = NULL, \
800 .fs = __KERNEL_PERCPU, \
801}
802
803/*
804 * Note that the .io_bitmap member must be extra-big. This is because
805 * the CPU will access an additional byte beyond the end of the IO
806 * permission bitmap. The extra byte must be all 1 bits, and must
807 * be within the limit.
808 */
809#define INIT_TSS { \
810 .x86_tss = { \
811 .sp0 = sizeof(init_stack) + (long)&init_stack, \
812 .ss0 = __KERNEL_DS, \
813 .ss1 = __KERNEL_CS, \
814 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, \
815 }, \
816 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, \
817}
818
819#define start_thread(regs, new_eip, new_esp) do { \
820 __asm__("movl %0,%%gs": :"r" (0)); \
821 regs->fs = 0; \
822 set_fs(USER_DS); \
823 regs->ds = __USER_DS; \
824 regs->es = __USER_DS; \
825 regs->ss = __USER_DS; \
826 regs->cs = __USER_CS; \
827 regs->ip = new_eip; \
828 regs->sp = new_esp; \
829} while (0)
830
831
832extern unsigned long thread_saved_pc(struct task_struct *tsk);
833
834#define THREAD_SIZE_LONGS (THREAD_SIZE/sizeof(unsigned long))
835#define KSTK_TOP(info) \
836({ \
837 unsigned long *__ptr = (unsigned long *)(info); \
838 (unsigned long)(&__ptr[THREAD_SIZE_LONGS]); \
839})
840
841/*
842 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
843 * This is necessary to guarantee that the entire "struct pt_regs"
844 * is accessable even if the CPU haven't stored the SS/ESP registers
845 * on the stack (interrupt gate does not save these registers
846 * when switching to the same priv ring).
847 * Therefore beware: accessing the ss/esp fields of the
848 * "struct pt_regs" is possible, but they may contain the
849 * completely wrong values.
850 */
851#define task_pt_regs(task) \
852({ \
853 struct pt_regs *__regs__; \
854 __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
855 __regs__ - 1; \
856})
857
858#define KSTK_ESP(task) (task_pt_regs(task)->sp)
859
860#else
861/*
862 * User space process size. 47bits minus one guard page.
863 */
864#define TASK_SIZE64 (0x800000000000UL - 4096)
865
866/* This decides where the kernel will search for a free chunk of vm
867 * space during mmap's.
868 */
869#define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? \
870 0xc0000000 : 0xFFFFe000)
871
872#define TASK_SIZE (test_thread_flag(TIF_IA32) ? \
873 IA32_PAGE_OFFSET : TASK_SIZE64)
874#define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? \
875 IA32_PAGE_OFFSET : TASK_SIZE64)
876
877#define INIT_THREAD { \
878 .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
879}
880
881#define INIT_TSS { \
882 .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
883}
884
885#define start_thread(regs, new_rip, new_rsp) do { \
886 asm volatile("movl %0,%%fs; movl %0,%%es; movl %0,%%ds": :"r" (0)); \
887 load_gs_index(0); \
888 (regs)->ip = (new_rip); \
889 (regs)->sp = (new_rsp); \
890 write_pda(oldrsp, (new_rsp)); \
891 (regs)->cs = __USER_CS; \
892 (regs)->ss = __USER_DS; \
893 (regs)->flags = 0x200; \
894 set_fs(USER_DS); \
895} while (0)
896
897/*
898 * Return saved PC of a blocked thread.
899 * What is this good for? it will be always the scheduler or ret_from_fork.
900 */
901#define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
902
903#define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
904#define KSTK_ESP(tsk) -1 /* sorry. doesn't work for syscall. */
905#endif /* CONFIG_X86_64 */
906
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100907/* This decides where the kernel will search for a free chunk of vm
908 * space during mmap's.
909 */
910#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
911
912#define KSTK_EIP(task) (task_pt_regs(task)->ip)
913
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100914#endif