vserver 1.9.3
[linux-2.6.git] / include / asm-x86_64 / processor.h
1 /*
2  * include/asm-x86_64/processor.h
3  *
4  * Copyright (C) 1994 Linus Torvalds
5  */
6
7 #ifndef __ASM_X86_64_PROCESSOR_H
8 #define __ASM_X86_64_PROCESSOR_H
9
10 #include <asm/segment.h>
11 #include <asm/page.h>
12 #include <asm/types.h>
13 #include <asm/sigcontext.h>
14 #include <asm/cpufeature.h>
15 #include <linux/config.h>
16 #include <linux/threads.h>
17 #include <asm/msr.h>
18 #include <asm/current.h>
19 #include <asm/system.h>
20 #include <asm/mmsegment.h>
21 #include <asm/percpu.h>
22 #include <linux/personality.h>
23
24 #define TF_MASK         0x00000100
25 #define IF_MASK         0x00000200
26 #define IOPL_MASK       0x00003000
27 #define NT_MASK         0x00004000
28 #define VM_MASK         0x00020000
29 #define AC_MASK         0x00040000
30 #define VIF_MASK        0x00080000      /* virtual interrupt flag */
31 #define VIP_MASK        0x00100000      /* virtual interrupt pending */
32 #define ID_MASK         0x00200000
33
34 #define desc_empty(desc) \
35                (!((desc)->a + (desc)->b))
36
37 #define desc_equal(desc1, desc2) \
38                (((desc1)->a == (desc2)->a) && ((desc1)->b == (desc2)->b))
39
40 /*
41  * Default implementation of macro that returns current
42  * instruction pointer ("program counter").
43  */
44 #define current_text_addr() ({ void *pc; asm volatile("leaq 1f(%%rip),%0\n1:":"=r"(pc)); pc; })
45
46 /*
47  *  CPU type and hardware bug flags. Kept separately for each CPU.
48  */
49
50 struct cpuinfo_x86 {
51         __u8    x86;            /* CPU family */
52         __u8    x86_vendor;     /* CPU vendor */
53         __u8    x86_model;
54         __u8    x86_mask;
55         int     cpuid_level;    /* Maximum supported CPUID level, -1=no CPUID */
56         __u32   x86_capability[NCAPINTS];
57         char    x86_vendor_id[16];
58         char    x86_model_id[64];
59         int     x86_cache_size;  /* in KB */
60         int     x86_clflush_size;
61         int     x86_cache_alignment;
62         int     x86_tlbsize;    /* number of 4K pages in DTLB/ITLB combined(in pages)*/
63         __u8    x86_virt_bits, x86_phys_bits;
64         __u8    x86_num_cores;
65         __u8    x86_apicid;
66         __u32   x86_power;      
67         unsigned long loops_per_jiffy;
68 } ____cacheline_aligned;
69
70 #define X86_VENDOR_INTEL 0
71 #define X86_VENDOR_CYRIX 1
72 #define X86_VENDOR_AMD 2
73 #define X86_VENDOR_UMC 3
74 #define X86_VENDOR_NEXGEN 4
75 #define X86_VENDOR_CENTAUR 5
76 #define X86_VENDOR_RISE 6
77 #define X86_VENDOR_TRANSMETA 7
78 #define X86_VENDOR_NUM 8
79 #define X86_VENDOR_UNKNOWN 0xff
80
81 #ifdef CONFIG_SMP
82 extern struct cpuinfo_x86 cpu_data[];
83 #define current_cpu_data cpu_data[smp_processor_id()]
84 #else
85 #define cpu_data (&boot_cpu_data)
86 #define current_cpu_data boot_cpu_data
87 #endif
88
89 extern char ignore_irq13;
90
91 extern void identify_cpu(struct cpuinfo_x86 *);
92 extern void print_cpu_info(struct cpuinfo_x86 *);
93 extern void dodgy_tsc(void);
94
95 /*
96  * EFLAGS bits
97  */
98 #define X86_EFLAGS_CF   0x00000001 /* Carry Flag */
99 #define X86_EFLAGS_PF   0x00000004 /* Parity Flag */
100 #define X86_EFLAGS_AF   0x00000010 /* Auxillary carry Flag */
101 #define X86_EFLAGS_ZF   0x00000040 /* Zero Flag */
102 #define X86_EFLAGS_SF   0x00000080 /* Sign Flag */
103 #define X86_EFLAGS_TF   0x00000100 /* Trap Flag */
104 #define X86_EFLAGS_IF   0x00000200 /* Interrupt Flag */
105 #define X86_EFLAGS_DF   0x00000400 /* Direction Flag */
106 #define X86_EFLAGS_OF   0x00000800 /* Overflow Flag */
107 #define X86_EFLAGS_IOPL 0x00003000 /* IOPL mask */
108 #define X86_EFLAGS_NT   0x00004000 /* Nested Task */
109 #define X86_EFLAGS_RF   0x00010000 /* Resume Flag */
110 #define X86_EFLAGS_VM   0x00020000 /* Virtual Mode */
111 #define X86_EFLAGS_AC   0x00040000 /* Alignment Check */
112 #define X86_EFLAGS_VIF  0x00080000 /* Virtual Interrupt Flag */
113 #define X86_EFLAGS_VIP  0x00100000 /* Virtual Interrupt Pending */
114 #define X86_EFLAGS_ID   0x00200000 /* CPUID detection flag */
115
116 /*
117  * Intel CPU features in CR4
118  */
119 #define X86_CR4_VME             0x0001  /* enable vm86 extensions */
120 #define X86_CR4_PVI             0x0002  /* virtual interrupts flag enable */
121 #define X86_CR4_TSD             0x0004  /* disable time stamp at ipl 3 */
122 #define X86_CR4_DE              0x0008  /* enable debugging extensions */
123 #define X86_CR4_PSE             0x0010  /* enable page size extensions */
124 #define X86_CR4_PAE             0x0020  /* enable physical address extensions */
125 #define X86_CR4_MCE             0x0040  /* Machine check enable */
126 #define X86_CR4_PGE             0x0080  /* enable global pages */
127 #define X86_CR4_PCE             0x0100  /* enable performance counters at ipl 3 */
128 #define X86_CR4_OSFXSR          0x0200  /* enable fast FPU save and restore */
129 #define X86_CR4_OSXMMEXCPT      0x0400  /* enable unmasked SSE exceptions */
130
131 /*
132  * Save the cr4 feature set we're using (ie
133  * Pentium 4MB enable and PPro Global page
134  * enable), so that any CPU's that boot up
135  * after us can get the correct flags.
136  */
137 extern unsigned long mmu_cr4_features;
138
139 static inline void set_in_cr4 (unsigned long mask)
140 {
141         mmu_cr4_features |= mask;
142         __asm__("movq %%cr4,%%rax\n\t"
143                 "orq %0,%%rax\n\t"
144                 "movq %%rax,%%cr4\n"
145                 : : "irg" (mask)
146                 :"ax");
147 }
148
149 static inline void clear_in_cr4 (unsigned long mask)
150 {
151         mmu_cr4_features &= ~mask;
152         __asm__("movq %%cr4,%%rax\n\t"
153                 "andq %0,%%rax\n\t"
154                 "movq %%rax,%%cr4\n"
155                 : : "irg" (~mask)
156                 :"ax");
157 }
158
159 /*
160  * Bus types
161  */
162 #define MCA_bus 0
163 #define MCA_bus__is_a_macro
164
165
166 /*
167  * User space process size: 512GB - 1GB (default).
168  */
169 #define TASK_SIZE       (0x0000007fc0000000UL)
170
171 /* This decides where the kernel will search for a free chunk of vm
172  * space during mmap's.
173  */
174 #define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? 0xc0000000 : 0xFFFFe000)
175 #define TASK_UNMAPPED_32 PAGE_ALIGN(IA32_PAGE_OFFSET/3)
176 #define TASK_UNMAPPED_64 PAGE_ALIGN(TASK_SIZE/3) 
177 #define TASK_UNMAPPED_BASE      \
178         (test_thread_flag(TIF_IA32) ? TASK_UNMAPPED_32 : TASK_UNMAPPED_64)  
179
180 /*
181  * Size of io_bitmap.
182  */
183 #define IO_BITMAP_BITS  65536
184 #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
185 #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
186 #define IO_BITMAP_OFFSET offsetof(struct tss_struct,io_bitmap)
187 #define INVALID_IO_BITMAP_OFFSET 0x8000
188
189 struct i387_fxsave_struct {
190         u16     cwd;
191         u16     swd;
192         u16     twd;
193         u16     fop;
194         u64     rip;
195         u64     rdp; 
196         u32     mxcsr;
197         u32     mxcsr_mask;
198         u32     st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
199         u32     xmm_space[64];  /* 16*16 bytes for each XMM-reg = 128 bytes */
200         u32     padding[24];
201 } __attribute__ ((aligned (16)));
202
203 union i387_union {
204         struct i387_fxsave_struct       fxsave;
205 };
206
207 struct tss_struct {
208         u32 reserved1;
209         u64 rsp0;       
210         u64 rsp1;
211         u64 rsp2;
212         u64 reserved2;
213         u64 ist[7];
214         u32 reserved3;
215         u32 reserved4;
216         u16 reserved5;
217         u16 io_bitmap_base;
218         /*
219          * The extra 1 is there because the CPU will access an
220          * additional byte beyond the end of the IO permission
221          * bitmap. The extra byte must be all 1 bits, and must
222          * be within the limit. Thus we have:
223          *
224          * 128 bytes, the bitmap itself, for ports 0..0x3ff
225          * 8 bytes, for an extra "long" of ~0UL
226          */
227         unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
228 } __attribute__((packed)) ____cacheline_aligned;
229
230 extern struct cpuinfo_x86 boot_cpu_data;
231 DECLARE_PER_CPU(struct tss_struct,init_tss);
232
233 #define ARCH_MIN_TASKALIGN      16
234
235 struct thread_struct {
236         unsigned long   rsp0;
237         unsigned long   rsp;
238         unsigned long   userrsp;        /* Copy from PDA */ 
239         unsigned long   fs;
240         unsigned long   gs;
241         unsigned short  es, ds, fsindex, gsindex;       
242 /* Hardware debugging registers */
243         unsigned long   debugreg0;  
244         unsigned long   debugreg1;  
245         unsigned long   debugreg2;  
246         unsigned long   debugreg3;  
247         unsigned long   debugreg6;  
248         unsigned long   debugreg7;  
249 /* fault info */
250         unsigned long   cr2, trap_no, error_code;
251 /* floating point info */
252         union i387_union        i387  __attribute__((aligned(16)));
253 /* IO permissions. the bitmap could be moved into the GDT, that would make
254    switch faster for a limited number of ioperm using tasks. -AK */
255         int             ioperm;
256         unsigned long   *io_bitmap_ptr;
257         unsigned io_bitmap_max;
258 /* cached TLS descriptors. */
259         u64 tls_array[GDT_ENTRY_TLS_ENTRIES];
260 } __attribute__((aligned(16)));
261
262 #define INIT_THREAD  {}
263
264 #define INIT_MMAP \
265 { &init_mm, 0, 0, NULL, PAGE_SHARED, VM_READ | VM_WRITE | VM_EXEC, 1, NULL, NULL }
266
267 #define STACKFAULT_STACK 1
268 #define DOUBLEFAULT_STACK 2 
269 #define NMI_STACK 3 
270 #define DEBUG_STACK 4 
271 #define MCE_STACK 5
272 #define N_EXCEPTION_STACKS 5  /* hw limit: 7 */
273 #define EXCEPTION_STKSZ (PAGE_SIZE << EXCEPTION_STACK_ORDER)
274 #define EXCEPTION_STACK_ORDER 0 
275
276 #define start_thread(regs,new_rip,new_rsp) do { \
277         asm volatile("movl %0,%%fs; movl %0,%%es; movl %0,%%ds": :"r" (0));      \
278         load_gs_index(0);                                                       \
279         (regs)->rip = (new_rip);                                                 \
280         (regs)->rsp = (new_rsp);                                                 \
281         write_pda(oldrsp, (new_rsp));                                            \
282         (regs)->cs = __USER_CS;                                                  \
283         (regs)->ss = __USER_DS;                                                  \
284         (regs)->eflags = 0x200;                                                  \
285         set_fs(USER_DS);                                                         \
286 } while(0) 
287
288 struct task_struct;
289 struct mm_struct;
290
291 /* Free all resources held by a thread. */
292 extern void release_thread(struct task_struct *);
293
294 /* Prepare to copy thread state - unlazy all lazy status */
295 extern void prepare_to_copy(struct task_struct *tsk);
296
297 /*
298  * create a kernel thread without removing it from tasklists
299  */
300 extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
301
302 /*
303  * Return saved PC of a blocked thread.
304  * What is this good for? it will be always the scheduler or ret_from_fork.
305  */
306 #define thread_saved_pc(t) (*(unsigned long *)((t)->thread.rsp - 8))
307
308 extern unsigned long get_wchan(struct task_struct *p);
309 #define KSTK_EIP(tsk) \
310         (((struct pt_regs *)(tsk->thread.rsp0 - sizeof(struct pt_regs)))->rip)
311 #define KSTK_ESP(tsk) -1 /* sorry. doesn't work for syscall. */
312
313
314 struct microcode_header {
315         unsigned int hdrver;
316         unsigned int rev;
317         unsigned int date;
318         unsigned int sig;
319         unsigned int cksum;
320         unsigned int ldrver;
321         unsigned int pf;
322         unsigned int datasize;
323         unsigned int totalsize;
324         unsigned int reserved[3];
325 };
326
327 struct microcode {
328         struct microcode_header hdr;
329         unsigned int bits[0];
330 };
331
332 typedef struct microcode microcode_t;
333 typedef struct microcode_header microcode_header_t;
334
335 /* microcode format is extended from prescott processors */
336 struct extended_signature {
337         unsigned int sig;
338         unsigned int pf;
339         unsigned int cksum;
340 };
341
342 struct extended_sigtable {
343         unsigned int count;
344         unsigned int cksum;
345         unsigned int reserved[3];
346         struct extended_signature sigs[0];
347 };
348
349 /* '6' because it used to be for P6 only (but now covers Pentium 4 as well) */
350 #define MICROCODE_IOCFREE       _IO('6',0)
351
352
353 #define ASM_NOP1 K8_NOP1
354 #define ASM_NOP2 K8_NOP2
355 #define ASM_NOP3 K8_NOP3
356 #define ASM_NOP4 K8_NOP4
357 #define ASM_NOP5 K8_NOP5
358 #define ASM_NOP6 K8_NOP6
359 #define ASM_NOP7 K8_NOP7
360 #define ASM_NOP8 K8_NOP8
361
362 /* Opteron nops */
363 #define K8_NOP1 ".byte 0x90\n"
364 #define K8_NOP2 ".byte 0x66,0x90\n" 
365 #define K8_NOP3 ".byte 0x66,0x66,0x90\n" 
366 #define K8_NOP4 ".byte 0x66,0x66,0x66,0x90\n" 
367 #define K8_NOP5 K8_NOP3 K8_NOP2 
368 #define K8_NOP6 K8_NOP3 K8_NOP3
369 #define K8_NOP7 K8_NOP4 K8_NOP3
370 #define K8_NOP8 K8_NOP4 K8_NOP4
371
372 #define ASM_NOP_MAX 8
373
374 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
375 extern inline void rep_nop(void)
376 {
377         __asm__ __volatile__("rep;nop": : :"memory");
378 }
379
380 /* Stop speculative execution */
381 extern inline void sync_core(void)
382
383         int tmp;
384         asm volatile("cpuid" : "=a" (tmp) : "0" (1) : "ebx","ecx","edx","memory");
385
386
387 #define cpu_has_fpu 1
388
389 #define ARCH_HAS_PREFETCH
390 static inline void prefetch(void *x) 
391
392         asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
393
394
395 #define ARCH_HAS_PREFETCHW 1
396 static inline void prefetchw(void *x) 
397
398         alternative_input(ASM_NOP5,
399                           "prefetchw (%1)",
400                           X86_FEATURE_3DNOW,
401                           "r" (x));
402
403
404 #define ARCH_HAS_SPINLOCK_PREFETCH 1
405
406 #define spin_lock_prefetch(x)  prefetchw(x)
407
408 #define cpu_relax()   rep_nop()
409
410 /*
411  *      NSC/Cyrix CPU configuration register indexes
412  */
413 #define CX86_CCR0 0xc0
414 #define CX86_CCR1 0xc1
415 #define CX86_CCR2 0xc2
416 #define CX86_CCR3 0xc3
417 #define CX86_CCR4 0xe8
418 #define CX86_CCR5 0xe9
419 #define CX86_CCR6 0xea
420 #define CX86_CCR7 0xeb
421 #define CX86_DIR0 0xfe
422 #define CX86_DIR1 0xff
423 #define CX86_ARR_BASE 0xc4
424 #define CX86_RCR_BASE 0xdc
425
426 /*
427  *      NSC/Cyrix CPU indexed register access macros
428  */
429
430 #define getCx86(reg) ({ outb((reg), 0x22); inb(0x23); })
431
432 #define setCx86(reg, data) do { \
433         outb((reg), 0x22); \
434         outb((data), 0x23); \
435 } while (0)
436
437 static inline void __monitor(const void *eax, unsigned long ecx,
438                 unsigned long edx)
439 {
440         /* "monitor %eax,%ecx,%edx;" */
441         asm volatile(
442                 ".byte 0x0f,0x01,0xc8;"
443                 : :"a" (eax), "c" (ecx), "d"(edx));
444 }
445
446 static inline void __mwait(unsigned long eax, unsigned long ecx)
447 {
448         /* "mwait %eax,%ecx;" */
449         asm volatile(
450                 ".byte 0x0f,0x01,0xc9;"
451                 : :"a" (eax), "c" (ecx));
452 }
453
454 #define stack_current() \
455 ({                                                              \
456         struct thread_info *ti;                                 \
457         asm("andq %%rsp,%0; ":"=r" (ti) : "0" (CURRENT_MASK));  \
458         ti->task;                                       \
459 })
460
461 #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
462
463 #endif /* __ASM_X86_64_PROCESSOR_H */