]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/asm-i386/paravirt.h
[PATCH] vmi: cpu cycles fix
[linux-2.6-omap-h63xx.git] / include / asm-i386 / paravirt.h
1 #ifndef __ASM_PARAVIRT_H
2 #define __ASM_PARAVIRT_H
3 /* Various instructions on x86 need to be replaced for
4  * para-virtualization: those hooks are defined here. */
5 #include <linux/linkage.h>
6 #include <linux/stringify.h>
7 #include <asm/page.h>
8
9 #ifdef CONFIG_PARAVIRT
10 /* These are the most performance critical ops, so we want to be able to patch
11  * callers */
12 #define PARAVIRT_IRQ_DISABLE 0
13 #define PARAVIRT_IRQ_ENABLE 1
14 #define PARAVIRT_RESTORE_FLAGS 2
15 #define PARAVIRT_SAVE_FLAGS 3
16 #define PARAVIRT_SAVE_FLAGS_IRQ_DISABLE 4
17 #define PARAVIRT_INTERRUPT_RETURN 5
18 #define PARAVIRT_STI_SYSEXIT 6
19
20 /* Bitmask of what can be clobbered: usually at least eax. */
21 #define CLBR_NONE 0x0
22 #define CLBR_EAX 0x1
23 #define CLBR_ECX 0x2
24 #define CLBR_EDX 0x4
25 #define CLBR_ANY 0x7
26
27 #ifndef __ASSEMBLY__
28 struct thread_struct;
29 struct Xgt_desc_struct;
30 struct tss_struct;
31 struct mm_struct;
32 struct paravirt_ops
33 {
34         unsigned int kernel_rpl;
35         int paravirt_enabled;
36         const char *name;
37
38         /*
39          * Patch may replace one of the defined code sequences with arbitrary
40          * code, subject to the same register constraints.  This generally
41          * means the code is not free to clobber any registers other than EAX.
42          * The patch function should return the number of bytes of code
43          * generated, as we nop pad the rest in generic code.
44          */
45         unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
46
47         void (*arch_setup)(void);
48         char *(*memory_setup)(void);
49         void (*init_IRQ)(void);
50
51         void (*banner)(void);
52
53         unsigned long (*get_wallclock)(void);
54         int (*set_wallclock)(unsigned long);
55         void (*time_init)(void);
56
57         /* All the function pointers here are declared as "fastcall"
58            so that we get a specific register-based calling
59            convention.  This makes it easier to implement inline
60            assembler replacements. */
61
62         void (*cpuid)(unsigned int *eax, unsigned int *ebx,
63                       unsigned int *ecx, unsigned int *edx);
64
65         unsigned long (*get_debugreg)(int regno);
66         void (*set_debugreg)(int regno, unsigned long value);
67
68         void (*clts)(void);
69
70         unsigned long (*read_cr0)(void);
71         void (*write_cr0)(unsigned long);
72
73         unsigned long (*read_cr2)(void);
74         void (*write_cr2)(unsigned long);
75
76         unsigned long (*read_cr3)(void);
77         void (*write_cr3)(unsigned long);
78
79         unsigned long (*read_cr4_safe)(void);
80         unsigned long (*read_cr4)(void);
81         void (*write_cr4)(unsigned long);
82
83         unsigned long (*save_fl)(void);
84         void (*restore_fl)(unsigned long);
85         void (*irq_disable)(void);
86         void (*irq_enable)(void);
87         void (*safe_halt)(void);
88         void (*halt)(void);
89         void (*wbinvd)(void);
90
91         /* err = 0/-EFAULT.  wrmsr returns 0/-EFAULT. */
92         u64 (*read_msr)(unsigned int msr, int *err);
93         int (*write_msr)(unsigned int msr, u64 val);
94
95         u64 (*read_tsc)(void);
96         u64 (*read_pmc)(void);
97         u64 (*get_scheduled_cycles)(void);
98         unsigned long (*get_cpu_khz)(void);
99
100         void (*load_tr_desc)(void);
101         void (*load_gdt)(const struct Xgt_desc_struct *);
102         void (*load_idt)(const struct Xgt_desc_struct *);
103         void (*store_gdt)(struct Xgt_desc_struct *);
104         void (*store_idt)(struct Xgt_desc_struct *);
105         void (*set_ldt)(const void *desc, unsigned entries);
106         unsigned long (*store_tr)(void);
107         void (*load_tls)(struct thread_struct *t, unsigned int cpu);
108         void (*write_ldt_entry)(void *dt, int entrynum,
109                                          u32 low, u32 high);
110         void (*write_gdt_entry)(void *dt, int entrynum,
111                                          u32 low, u32 high);
112         void (*write_idt_entry)(void *dt, int entrynum,
113                                          u32 low, u32 high);
114         void (*load_esp0)(struct tss_struct *tss,
115                                    struct thread_struct *thread);
116
117         void (*set_iopl_mask)(unsigned mask);
118
119         void (*io_delay)(void);
120         void (*const_udelay)(unsigned long loops);
121
122 #ifdef CONFIG_X86_LOCAL_APIC
123         void (*apic_write)(unsigned long reg, unsigned long v);
124         void (*apic_write_atomic)(unsigned long reg, unsigned long v);
125         unsigned long (*apic_read)(unsigned long reg);
126         void (*setup_boot_clock)(void);
127         void (*setup_secondary_clock)(void);
128 #endif
129
130         void (*flush_tlb_user)(void);
131         void (*flush_tlb_kernel)(void);
132         void (*flush_tlb_single)(u32 addr);
133
134         void (*alloc_pt)(u32 pfn);
135         void (*alloc_pd)(u32 pfn);
136         void (*alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
137         void (*release_pt)(u32 pfn);
138         void (*release_pd)(u32 pfn);
139
140         void (*set_pte)(pte_t *ptep, pte_t pteval);
141         void (*set_pte_at)(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval);
142         void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
143         void (*pte_update)(struct mm_struct *mm, u32 addr, pte_t *ptep);
144         void (*pte_update_defer)(struct mm_struct *mm, u32 addr, pte_t *ptep);
145 #ifdef CONFIG_X86_PAE
146         void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
147         void (*set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
148         void (*set_pud)(pud_t *pudp, pud_t pudval);
149         void (*pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
150         void (*pmd_clear)(pmd_t *pmdp);
151 #endif
152
153         void (*set_lazy_mode)(int mode);
154
155         /* These two are jmp to, not actually called. */
156         void (*irq_enable_sysexit)(void);
157         void (*iret)(void);
158
159         void (*startup_ipi_hook)(int phys_apicid, unsigned long start_eip, unsigned long start_esp);
160 };
161
162 /* Mark a paravirt probe function. */
163 #define paravirt_probe(fn)                                              \
164  static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
165                 __attribute__((__section__(".paravirtprobe"))) = fn
166
167 extern struct paravirt_ops paravirt_ops;
168
169 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
170
171 static inline void load_esp0(struct tss_struct *tss,
172                              struct thread_struct *thread)
173 {
174         paravirt_ops.load_esp0(tss, thread);
175 }
176
177 #define ARCH_SETUP                      paravirt_ops.arch_setup();
178 static inline unsigned long get_wallclock(void)
179 {
180         return paravirt_ops.get_wallclock();
181 }
182
183 static inline int set_wallclock(unsigned long nowtime)
184 {
185         return paravirt_ops.set_wallclock(nowtime);
186 }
187
188 static inline void do_time_init(void)
189 {
190         return paravirt_ops.time_init();
191 }
192
193 /* The paravirtualized CPUID instruction. */
194 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
195                            unsigned int *ecx, unsigned int *edx)
196 {
197         paravirt_ops.cpuid(eax, ebx, ecx, edx);
198 }
199
200 /*
201  * These special macros can be used to get or set a debugging register
202  */
203 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
204 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
205
206 #define clts() paravirt_ops.clts()
207
208 #define read_cr0() paravirt_ops.read_cr0()
209 #define write_cr0(x) paravirt_ops.write_cr0(x)
210
211 #define read_cr2() paravirt_ops.read_cr2()
212 #define write_cr2(x) paravirt_ops.write_cr2(x)
213
214 #define read_cr3() paravirt_ops.read_cr3()
215 #define write_cr3(x) paravirt_ops.write_cr3(x)
216
217 #define read_cr4() paravirt_ops.read_cr4()
218 #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
219 #define write_cr4(x) paravirt_ops.write_cr4(x)
220
221 static inline void raw_safe_halt(void)
222 {
223         paravirt_ops.safe_halt();
224 }
225
226 static inline void halt(void)
227 {
228         paravirt_ops.safe_halt();
229 }
230 #define wbinvd() paravirt_ops.wbinvd()
231
232 #define get_kernel_rpl()  (paravirt_ops.kernel_rpl)
233
234 #define rdmsr(msr,val1,val2) do {                               \
235         int _err;                                               \
236         u64 _l = paravirt_ops.read_msr(msr,&_err);              \
237         val1 = (u32)_l;                                         \
238         val2 = _l >> 32;                                        \
239 } while(0)
240
241 #define wrmsr(msr,val1,val2) do {                               \
242         u64 _l = ((u64)(val2) << 32) | (val1);                  \
243         paravirt_ops.write_msr((msr), _l);                      \
244 } while(0)
245
246 #define rdmsrl(msr,val) do {                                    \
247         int _err;                                               \
248         val = paravirt_ops.read_msr((msr),&_err);               \
249 } while(0)
250
251 #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
252 #define wrmsr_safe(msr,a,b) ({                                  \
253         u64 _l = ((u64)(b) << 32) | (a);                        \
254         paravirt_ops.write_msr((msr),_l);                       \
255 })
256
257 /* rdmsr with exception handling */
258 #define rdmsr_safe(msr,a,b) ({                                  \
259         int _err;                                               \
260         u64 _l = paravirt_ops.read_msr(msr,&_err);              \
261         (*a) = (u32)_l;                                         \
262         (*b) = _l >> 32;                                        \
263         _err; })
264
265 #define rdtsc(low,high) do {                                    \
266         u64 _l = paravirt_ops.read_tsc();                       \
267         low = (u32)_l;                                          \
268         high = _l >> 32;                                        \
269 } while(0)
270
271 #define rdtscl(low) do {                                        \
272         u64 _l = paravirt_ops.read_tsc();                       \
273         low = (int)_l;                                          \
274 } while(0)
275
276 #define rdtscll(val) (val = paravirt_ops.read_tsc())
277
278 #define get_scheduled_cycles(val) (val = paravirt_ops.get_scheduled_cycles())
279 #define calculate_cpu_khz() (paravirt_ops.get_cpu_khz())
280
281 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
282
283 #define rdpmc(counter,low,high) do {                            \
284         u64 _l = paravirt_ops.read_pmc();                       \
285         low = (u32)_l;                                          \
286         high = _l >> 32;                                        \
287 } while(0)
288
289 #define load_TR_desc() (paravirt_ops.load_tr_desc())
290 #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
291 #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
292 #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
293 #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
294 #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
295 #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
296 #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
297 #define write_ldt_entry(dt, entry, low, high)                           \
298         (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
299 #define write_gdt_entry(dt, entry, low, high)                           \
300         (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
301 #define write_idt_entry(dt, entry, low, high)                           \
302         (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
303 #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
304
305 /* The paravirtualized I/O functions */
306 static inline void slow_down_io(void) {
307         paravirt_ops.io_delay();
308 #ifdef REALLY_SLOW_IO
309         paravirt_ops.io_delay();
310         paravirt_ops.io_delay();
311         paravirt_ops.io_delay();
312 #endif
313 }
314
315 #ifdef CONFIG_X86_LOCAL_APIC
316 /*
317  * Basic functions accessing APICs.
318  */
319 static inline void apic_write(unsigned long reg, unsigned long v)
320 {
321         paravirt_ops.apic_write(reg,v);
322 }
323
324 static inline void apic_write_atomic(unsigned long reg, unsigned long v)
325 {
326         paravirt_ops.apic_write_atomic(reg,v);
327 }
328
329 static inline unsigned long apic_read(unsigned long reg)
330 {
331         return paravirt_ops.apic_read(reg);
332 }
333
334 static inline void setup_boot_clock(void)
335 {
336         paravirt_ops.setup_boot_clock();
337 }
338
339 static inline void setup_secondary_clock(void)
340 {
341         paravirt_ops.setup_secondary_clock();
342 }
343 #endif
344
345 #ifdef CONFIG_SMP
346 static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
347                                     unsigned long start_esp)
348 {
349         return paravirt_ops.startup_ipi_hook(phys_apicid, start_eip, start_esp);
350 }
351 #endif
352
353 #define __flush_tlb() paravirt_ops.flush_tlb_user()
354 #define __flush_tlb_global() paravirt_ops.flush_tlb_kernel()
355 #define __flush_tlb_single(addr) paravirt_ops.flush_tlb_single(addr)
356
357 #define paravirt_alloc_pt(pfn) paravirt_ops.alloc_pt(pfn)
358 #define paravirt_release_pt(pfn) paravirt_ops.release_pt(pfn)
359
360 #define paravirt_alloc_pd(pfn) paravirt_ops.alloc_pd(pfn)
361 #define paravirt_alloc_pd_clone(pfn, clonepfn, start, count) \
362         paravirt_ops.alloc_pd_clone(pfn, clonepfn, start, count)
363 #define paravirt_release_pd(pfn) paravirt_ops.release_pd(pfn)
364
365 static inline void set_pte(pte_t *ptep, pte_t pteval)
366 {
367         paravirt_ops.set_pte(ptep, pteval);
368 }
369
370 static inline void set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
371 {
372         paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
373 }
374
375 static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
376 {
377         paravirt_ops.set_pmd(pmdp, pmdval);
378 }
379
380 static inline void pte_update(struct mm_struct *mm, u32 addr, pte_t *ptep)
381 {
382         paravirt_ops.pte_update(mm, addr, ptep);
383 }
384
385 static inline void pte_update_defer(struct mm_struct *mm, u32 addr, pte_t *ptep)
386 {
387         paravirt_ops.pte_update_defer(mm, addr, ptep);
388 }
389
390 #ifdef CONFIG_X86_PAE
391 static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
392 {
393         paravirt_ops.set_pte_atomic(ptep, pteval);
394 }
395
396 static inline void set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
397 {
398         paravirt_ops.set_pte_present(mm, addr, ptep, pte);
399 }
400
401 static inline void set_pud(pud_t *pudp, pud_t pudval)
402 {
403         paravirt_ops.set_pud(pudp, pudval);
404 }
405
406 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
407 {
408         paravirt_ops.pte_clear(mm, addr, ptep);
409 }
410
411 static inline void pmd_clear(pmd_t *pmdp)
412 {
413         paravirt_ops.pmd_clear(pmdp);
414 }
415 #endif
416
417 /* Lazy mode for batching updates / context switch */
418 #define PARAVIRT_LAZY_NONE 0
419 #define PARAVIRT_LAZY_MMU  1
420 #define PARAVIRT_LAZY_CPU  2
421
422 #define  __HAVE_ARCH_ENTER_LAZY_CPU_MODE
423 #define arch_enter_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_CPU)
424 #define arch_leave_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
425
426 #define  __HAVE_ARCH_ENTER_LAZY_MMU_MODE
427 #define arch_enter_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_MMU)
428 #define arch_leave_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
429
430 /* These all sit in the .parainstructions section to tell us what to patch. */
431 struct paravirt_patch {
432         u8 *instr;              /* original instructions */
433         u8 instrtype;           /* type of this instruction */
434         u8 len;                 /* length of original instruction */
435         u16 clobbers;           /* what registers you may clobber */
436 };
437
438 #define paravirt_alt(insn_string, typenum, clobber)     \
439         "771:\n\t" insn_string "\n" "772:\n"            \
440         ".pushsection .parainstructions,\"a\"\n"        \
441         "  .long 771b\n"                                \
442         "  .byte " __stringify(typenum) "\n"            \
443         "  .byte 772b-771b\n"                           \
444         "  .short " __stringify(clobber) "\n"           \
445         ".popsection"
446
447 static inline unsigned long __raw_local_save_flags(void)
448 {
449         unsigned long f;
450
451         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
452                                            "call *%1;"
453                                            "popl %%edx; popl %%ecx",
454                                           PARAVIRT_SAVE_FLAGS, CLBR_NONE)
455                              : "=a"(f): "m"(paravirt_ops.save_fl)
456                              : "memory", "cc");
457         return f;
458 }
459
460 static inline void raw_local_irq_restore(unsigned long f)
461 {
462         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
463                                            "call *%1;"
464                                            "popl %%edx; popl %%ecx",
465                                           PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
466                              : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
467                              : "memory", "cc");
468 }
469
470 static inline void raw_local_irq_disable(void)
471 {
472         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
473                                            "call *%0;"
474                                            "popl %%edx; popl %%ecx",
475                                           PARAVIRT_IRQ_DISABLE, CLBR_EAX)
476                              : : "m" (paravirt_ops.irq_disable)
477                              : "memory", "eax", "cc");
478 }
479
480 static inline void raw_local_irq_enable(void)
481 {
482         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
483                                            "call *%0;"
484                                            "popl %%edx; popl %%ecx",
485                                           PARAVIRT_IRQ_ENABLE, CLBR_EAX)
486                              : : "m" (paravirt_ops.irq_enable)
487                              : "memory", "eax", "cc");
488 }
489
490 static inline unsigned long __raw_local_irq_save(void)
491 {
492         unsigned long f;
493
494         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
495                                            "call *%1; pushl %%eax;"
496                                            "call *%2; popl %%eax;"
497                                            "popl %%edx; popl %%ecx",
498                                           PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
499                                           CLBR_NONE)
500                              : "=a"(f)
501                              : "m" (paravirt_ops.save_fl),
502                                "m" (paravirt_ops.irq_disable)
503                              : "memory", "cc");
504         return f;
505 }
506
507 #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;"             \
508                      "call *paravirt_ops+%c[irq_disable];"              \
509                      "popl %%edx; popl %%ecx",                          \
510                      PARAVIRT_IRQ_DISABLE, CLBR_EAX)
511
512 #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;"             \
513                      "call *paravirt_ops+%c[irq_enable];"               \
514                      "popl %%edx; popl %%ecx",                          \
515                      PARAVIRT_IRQ_ENABLE, CLBR_EAX)
516 #define CLI_STI_CLOBBERS , "%eax"
517 #define CLI_STI_INPUT_ARGS \
518         ,                                                               \
519         [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
520         [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
521
522 #else  /* __ASSEMBLY__ */
523
524 #define PARA_PATCH(ptype, clobbers, ops)        \
525 771:;                                           \
526         ops;                                    \
527 772:;                                           \
528         .pushsection .parainstructions,"a";     \
529          .long 771b;                            \
530          .byte ptype;                           \
531          .byte 772b-771b;                       \
532          .short clobbers;                       \
533         .popsection
534
535 #define INTERRUPT_RETURN                                \
536         PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
537         jmp *%cs:paravirt_ops+PARAVIRT_iret)
538
539 #define DISABLE_INTERRUPTS(clobbers)                    \
540         PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers,      \
541         pushl %ecx; pushl %edx;                         \
542         call *paravirt_ops+PARAVIRT_irq_disable;        \
543         popl %edx; popl %ecx)                           \
544
545 #define ENABLE_INTERRUPTS(clobbers)                     \
546         PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers,       \
547         pushl %ecx; pushl %edx;                         \
548         call *%cs:paravirt_ops+PARAVIRT_irq_enable;     \
549         popl %edx; popl %ecx)
550
551 #define ENABLE_INTERRUPTS_SYSEXIT                       \
552         PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY,      \
553         jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
554
555 #define GET_CR0_INTO_EAX                        \
556         call *paravirt_ops+PARAVIRT_read_cr0
557
558 #endif /* __ASSEMBLY__ */
559 #endif /* CONFIG_PARAVIRT */
560 #endif  /* __ASM_PARAVIRT_H */