x86: unify mm_segment_t definition
[linux-2.6] / include / asm-x86 / processor.h
1 #ifndef __ASM_X86_PROCESSOR_H
2 #define __ASM_X86_PROCESSOR_H
3
4 #include <asm/processor-flags.h>
5
6 /* Forward declaration, a strange C thing */
7 struct task_struct;
8 struct mm_struct;
9
10 #include <asm/page.h>
11 #include <asm/percpu.h>
12 #include <asm/system.h>
13 #include <asm/percpu.h>
14 #include <linux/cpumask.h>
15 #include <linux/cache.h>
16
17 /*
18  * Default implementation of macro that returns current
19  * instruction pointer ("program counter").
20  */
21 static inline void *current_text_addr(void)
22 {
23         void *pc;
24         asm volatile("mov $1f,%0\n1:":"=r" (pc));
25         return pc;
26 }
27
28 #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
36 /*
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
42 struct 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
99 extern struct cpuinfo_x86 boot_cpu_data;
100
101 #ifdef CONFIG_SMP
102 DECLARE_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
110 extern void print_cpu_info(struct cpuinfo_x86 *);
111 extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
112 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
113 extern unsigned short num_cache_leaves;
114
115 static 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
127 static inline void load_cr3(pgd_t *pgdir)
128 {
129         write_cr3(__pa(pgdir));
130 }
131
132 #ifdef CONFIG_X86_32
133 /* This is the TSS defined by the hardware. */
134 struct 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
157 struct 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
181 struct 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
206 DECLARE_PER_CPU(struct tss_struct, init_tss);
207
208 #ifdef CONFIG_X86_32
209 # include "processor_32.h"
210 #else
211 # include "processor_64.h"
212 #endif
213
214 extern void print_cpu_info(struct cpuinfo_x86 *);
215 extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
216 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
217 extern unsigned short num_cache_leaves;
218
219 struct 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
263 static 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
286 static 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
312 /*
313  * Set IOPL bits in EFLAGS from given mask
314  */
315 static 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
330 static 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 }
342
343 #ifdef CONFIG_PARAVIRT
344 #include <asm/paravirt.h>
345 #else
346 #define __cpuid native_cpuid
347 #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
357 static inline void load_sp0(struct tss_struct *tss,
358                             struct thread_struct *thread)
359 {
360         native_load_sp0(tss, thread);
361 }
362
363 #define set_iopl_mask native_set_iopl_mask
364 #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  */
372 extern unsigned long mmu_cr4_features;
373
374 static 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
383 static 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
392 struct 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 };
404
405 struct microcode {
406         struct microcode_header hdr;
407         unsigned int bits[0];
408 };
409
410 typedef struct microcode microcode_t;
411 typedef struct microcode_header microcode_header_t;
412
413 /* microcode format is extended from prescott processors */
414 struct extended_signature {
415         unsigned int sig;
416         unsigned int pf;
417         unsigned int cksum;
418 };
419
420 struct extended_sigtable {
421         unsigned int count;
422         unsigned int cksum;
423         unsigned int reserved[3];
424         struct extended_signature sigs[0];
425 };
426
427 typedef struct {
428         unsigned long seg;
429 } mm_segment_t;
430
431
432 /*
433  * create a kernel thread without removing it from tasklists
434  */
435 extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
436
437 /* Free all resources held by a thread. */
438 extern void release_thread(struct task_struct *);
439
440 /* Prepare to copy thread state - unlazy all lazy status */
441 extern void prepare_to_copy(struct task_struct *tsk);
442
443 unsigned long get_wchan(struct task_struct *p);
444
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  */
450 static 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 */
460 static 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  */
472 static 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 }
479 static 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 }
486 static 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 }
493 static 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
501 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
502 static inline void rep_nop(void)
503 {
504         __asm__ __volatile__("rep;nop": : :"memory");
505 }
506
507 /* Stop speculative execution */
508 static 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
517 static 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
526 static 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
534 static 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
542 extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
543
544 extern int force_mwait;
545
546 extern void select_idle_routine(const struct cpuinfo_x86 *c);
547
548 extern unsigned long boot_option_idle_override;
549
550 /* Boot loader type from the setup header */
551 extern 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
566 #endif