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