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>
10 /* These are the most performance critical ops, so we want to be able to patch
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
20 /* Bitmask of what can be clobbered: usually at least eax. */
29 struct Xgt_desc_struct;
34 unsigned int kernel_rpl;
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.
45 unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
47 void (*arch_setup)(void);
48 char *(*memory_setup)(void);
49 void (*init_IRQ)(void);
53 unsigned long (*get_wallclock)(void);
54 int (*set_wallclock)(unsigned long);
55 void (*time_init)(void);
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. */
62 void (fastcall *cpuid)(unsigned int *eax, unsigned int *ebx,
63 unsigned int *ecx, unsigned int *edx);
65 unsigned long (fastcall *get_debugreg)(int regno);
66 void (fastcall *set_debugreg)(int regno, unsigned long value);
68 void (fastcall *clts)(void);
70 unsigned long (fastcall *read_cr0)(void);
71 void (fastcall *write_cr0)(unsigned long);
73 unsigned long (fastcall *read_cr2)(void);
74 void (fastcall *write_cr2)(unsigned long);
76 unsigned long (fastcall *read_cr3)(void);
77 void (fastcall *write_cr3)(unsigned long);
79 unsigned long (fastcall *read_cr4_safe)(void);
80 unsigned long (fastcall *read_cr4)(void);
81 void (fastcall *write_cr4)(unsigned long);
83 unsigned long (fastcall *save_fl)(void);
84 void (fastcall *restore_fl)(unsigned long);
85 void (fastcall *irq_disable)(void);
86 void (fastcall *irq_enable)(void);
87 void (fastcall *safe_halt)(void);
88 void (fastcall *halt)(void);
89 void (fastcall *wbinvd)(void);
91 /* err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
92 u64 (fastcall *read_msr)(unsigned int msr, int *err);
93 int (fastcall *write_msr)(unsigned int msr, u64 val);
95 u64 (fastcall *read_tsc)(void);
96 u64 (fastcall *read_pmc)(void);
98 void (fastcall *load_tr_desc)(void);
99 void (fastcall *load_gdt)(const struct Xgt_desc_struct *);
100 void (fastcall *load_idt)(const struct Xgt_desc_struct *);
101 void (fastcall *store_gdt)(struct Xgt_desc_struct *);
102 void (fastcall *store_idt)(struct Xgt_desc_struct *);
103 void (fastcall *set_ldt)(const void *desc, unsigned entries);
104 unsigned long (fastcall *store_tr)(void);
105 void (fastcall *load_tls)(struct thread_struct *t, unsigned int cpu);
106 void (fastcall *write_ldt_entry)(void *dt, int entrynum,
108 void (fastcall *write_gdt_entry)(void *dt, int entrynum,
110 void (fastcall *write_idt_entry)(void *dt, int entrynum,
112 void (fastcall *load_esp0)(struct tss_struct *tss,
113 struct thread_struct *thread);
115 void (fastcall *set_iopl_mask)(unsigned mask);
117 void (fastcall *io_delay)(void);
118 void (*const_udelay)(unsigned long loops);
120 #ifdef CONFIG_X86_LOCAL_APIC
121 void (fastcall *apic_write)(unsigned long reg, unsigned long v);
122 void (fastcall *apic_write_atomic)(unsigned long reg, unsigned long v);
123 unsigned long (fastcall *apic_read)(unsigned long reg);
126 void (fastcall *flush_tlb_user)(void);
127 void (fastcall *flush_tlb_kernel)(void);
128 void (fastcall *flush_tlb_single)(u32 addr);
130 void (fastcall *alloc_pt)(u32 pfn);
131 void (fastcall *alloc_pd)(u32 pfn);
132 void (fastcall *alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
133 void (fastcall *release_pt)(u32 pfn);
134 void (fastcall *release_pd)(u32 pfn);
136 void (fastcall *set_pte)(pte_t *ptep, pte_t pteval);
137 void (fastcall *set_pte_at)(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval);
138 void (fastcall *set_pmd)(pmd_t *pmdp, pmd_t pmdval);
139 void (fastcall *pte_update)(struct mm_struct *mm, u32 addr, pte_t *ptep);
140 void (fastcall *pte_update_defer)(struct mm_struct *mm, u32 addr, pte_t *ptep);
141 #ifdef CONFIG_X86_PAE
142 void (fastcall *set_pte_atomic)(pte_t *ptep, pte_t pteval);
143 void (fastcall *set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
144 void (fastcall *set_pud)(pud_t *pudp, pud_t pudval);
145 void (fastcall *pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
146 void (fastcall *pmd_clear)(pmd_t *pmdp);
149 void (fastcall *set_lazy_mode)(int mode);
151 /* These two are jmp to, not actually called. */
152 void (fastcall *irq_enable_sysexit)(void);
153 void (fastcall *iret)(void);
155 void (fastcall *startup_ipi_hook)(int phys_apicid, unsigned long start_eip, unsigned long start_esp);
158 /* Mark a paravirt probe function. */
159 #define paravirt_probe(fn) \
160 static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
161 __attribute__((__section__(".paravirtprobe"))) = fn
163 extern struct paravirt_ops paravirt_ops;
165 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
167 static inline void load_esp0(struct tss_struct *tss,
168 struct thread_struct *thread)
170 paravirt_ops.load_esp0(tss, thread);
173 #define ARCH_SETUP paravirt_ops.arch_setup();
174 static inline unsigned long get_wallclock(void)
176 return paravirt_ops.get_wallclock();
179 static inline int set_wallclock(unsigned long nowtime)
181 return paravirt_ops.set_wallclock(nowtime);
184 static inline void do_time_init(void)
186 return paravirt_ops.time_init();
189 /* The paravirtualized CPUID instruction. */
190 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
191 unsigned int *ecx, unsigned int *edx)
193 paravirt_ops.cpuid(eax, ebx, ecx, edx);
197 * These special macros can be used to get or set a debugging register
199 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
200 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
202 #define clts() paravirt_ops.clts()
204 #define read_cr0() paravirt_ops.read_cr0()
205 #define write_cr0(x) paravirt_ops.write_cr0(x)
207 #define read_cr2() paravirt_ops.read_cr2()
208 #define write_cr2(x) paravirt_ops.write_cr2(x)
210 #define read_cr3() paravirt_ops.read_cr3()
211 #define write_cr3(x) paravirt_ops.write_cr3(x)
213 #define read_cr4() paravirt_ops.read_cr4()
214 #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
215 #define write_cr4(x) paravirt_ops.write_cr4(x)
217 static inline void raw_safe_halt(void)
219 paravirt_ops.safe_halt();
222 static inline void halt(void)
224 paravirt_ops.safe_halt();
226 #define wbinvd() paravirt_ops.wbinvd()
228 #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
230 #define rdmsr(msr,val1,val2) do { \
232 u64 _l = paravirt_ops.read_msr(msr,&_err); \
237 #define wrmsr(msr,val1,val2) do { \
238 u64 _l = ((u64)(val2) << 32) | (val1); \
239 paravirt_ops.write_msr((msr), _l); \
242 #define rdmsrl(msr,val) do { \
244 val = paravirt_ops.read_msr((msr),&_err); \
247 #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
248 #define wrmsr_safe(msr,a,b) ({ \
249 u64 _l = ((u64)(b) << 32) | (a); \
250 paravirt_ops.write_msr((msr),_l); \
253 /* rdmsr with exception handling */
254 #define rdmsr_safe(msr,a,b) ({ \
256 u64 _l = paravirt_ops.read_msr(msr,&_err); \
261 #define rdtsc(low,high) do { \
262 u64 _l = paravirt_ops.read_tsc(); \
267 #define rdtscl(low) do { \
268 u64 _l = paravirt_ops.read_tsc(); \
272 #define rdtscll(val) (val = paravirt_ops.read_tsc())
274 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
276 #define rdpmc(counter,low,high) do { \
277 u64 _l = paravirt_ops.read_pmc(); \
282 #define load_TR_desc() (paravirt_ops.load_tr_desc())
283 #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
284 #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
285 #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
286 #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
287 #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
288 #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
289 #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
290 #define write_ldt_entry(dt, entry, low, high) \
291 (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
292 #define write_gdt_entry(dt, entry, low, high) \
293 (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
294 #define write_idt_entry(dt, entry, low, high) \
295 (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
296 #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
298 /* The paravirtualized I/O functions */
299 static inline void slow_down_io(void) {
300 paravirt_ops.io_delay();
301 #ifdef REALLY_SLOW_IO
302 paravirt_ops.io_delay();
303 paravirt_ops.io_delay();
304 paravirt_ops.io_delay();
308 #ifdef CONFIG_X86_LOCAL_APIC
310 * Basic functions accessing APICs.
312 static inline void apic_write(unsigned long reg, unsigned long v)
314 paravirt_ops.apic_write(reg,v);
317 static inline void apic_write_atomic(unsigned long reg, unsigned long v)
319 paravirt_ops.apic_write_atomic(reg,v);
322 static inline unsigned long apic_read(unsigned long reg)
324 return paravirt_ops.apic_read(reg);
329 static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
330 unsigned long start_esp)
332 return paravirt_ops.startup_ipi_hook(phys_apicid, start_eip, start_esp);
336 #define __flush_tlb() paravirt_ops.flush_tlb_user()
337 #define __flush_tlb_global() paravirt_ops.flush_tlb_kernel()
338 #define __flush_tlb_single(addr) paravirt_ops.flush_tlb_single(addr)
340 #define paravirt_alloc_pt(pfn) paravirt_ops.alloc_pt(pfn)
341 #define paravirt_release_pt(pfn) paravirt_ops.release_pt(pfn)
343 #define paravirt_alloc_pd(pfn) paravirt_ops.alloc_pd(pfn)
344 #define paravirt_alloc_pd_clone(pfn, clonepfn, start, count) \
345 paravirt_ops.alloc_pd_clone(pfn, clonepfn, start, count)
346 #define paravirt_release_pd(pfn) paravirt_ops.release_pd(pfn)
348 static inline void set_pte(pte_t *ptep, pte_t pteval)
350 paravirt_ops.set_pte(ptep, pteval);
353 static inline void set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval)
355 paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
358 static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
360 paravirt_ops.set_pmd(pmdp, pmdval);
363 static inline void pte_update(struct mm_struct *mm, u32 addr, pte_t *ptep)
365 paravirt_ops.pte_update(mm, addr, ptep);
368 static inline void pte_update_defer(struct mm_struct *mm, u32 addr, pte_t *ptep)
370 paravirt_ops.pte_update_defer(mm, addr, ptep);
373 #ifdef CONFIG_X86_PAE
374 static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
376 paravirt_ops.set_pte_atomic(ptep, pteval);
379 static inline void set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
381 paravirt_ops.set_pte_present(mm, addr, ptep, pte);
384 static inline void set_pud(pud_t *pudp, pud_t pudval)
386 paravirt_ops.set_pud(pudp, pudval);
389 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
391 paravirt_ops.pte_clear(mm, addr, ptep);
394 static inline void pmd_clear(pmd_t *pmdp)
396 paravirt_ops.pmd_clear(pmdp);
400 /* Lazy mode for batching updates / context switch */
401 #define PARAVIRT_LAZY_NONE 0
402 #define PARAVIRT_LAZY_MMU 1
403 #define PARAVIRT_LAZY_CPU 2
405 #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
406 #define arch_enter_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_CPU)
407 #define arch_leave_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
409 #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
410 #define arch_enter_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_MMU)
411 #define arch_leave_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
413 /* These all sit in the .parainstructions section to tell us what to patch. */
414 struct paravirt_patch {
415 u8 *instr; /* original instructions */
416 u8 instrtype; /* type of this instruction */
417 u8 len; /* length of original instruction */
418 u16 clobbers; /* what registers you may clobber */
421 #define paravirt_alt(insn_string, typenum, clobber) \
422 "771:\n\t" insn_string "\n" "772:\n" \
423 ".pushsection .parainstructions,\"a\"\n" \
425 " .byte " __stringify(typenum) "\n" \
426 " .byte 772b-771b\n" \
427 " .short " __stringify(clobber) "\n" \
430 static inline unsigned long __raw_local_save_flags(void)
434 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
436 "popl %%edx; popl %%ecx",
437 PARAVIRT_SAVE_FLAGS, CLBR_NONE)
438 : "=a"(f): "m"(paravirt_ops.save_fl)
443 static inline void raw_local_irq_restore(unsigned long f)
445 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
447 "popl %%edx; popl %%ecx",
448 PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
449 : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
453 static inline void raw_local_irq_disable(void)
455 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
457 "popl %%edx; popl %%ecx",
458 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
459 : : "m" (paravirt_ops.irq_disable)
460 : "memory", "eax", "cc");
463 static inline void raw_local_irq_enable(void)
465 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
467 "popl %%edx; popl %%ecx",
468 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
469 : : "m" (paravirt_ops.irq_enable)
470 : "memory", "eax", "cc");
473 static inline unsigned long __raw_local_irq_save(void)
477 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
478 "call *%1; pushl %%eax;"
479 "call *%2; popl %%eax;"
480 "popl %%edx; popl %%ecx",
481 PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
484 : "m" (paravirt_ops.save_fl),
485 "m" (paravirt_ops.irq_disable)
490 #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
491 "call *paravirt_ops+%c[irq_disable];" \
492 "popl %%edx; popl %%ecx", \
493 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
495 #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
496 "call *paravirt_ops+%c[irq_enable];" \
497 "popl %%edx; popl %%ecx", \
498 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
499 #define CLI_STI_CLOBBERS , "%eax"
500 #define CLI_STI_INPUT_ARGS \
502 [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
503 [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
505 #else /* __ASSEMBLY__ */
507 #define PARA_PATCH(ptype, clobbers, ops) \
511 .pushsection .parainstructions,"a"; \
518 #define INTERRUPT_RETURN \
519 PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
520 jmp *%cs:paravirt_ops+PARAVIRT_iret)
522 #define DISABLE_INTERRUPTS(clobbers) \
523 PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
524 pushl %ecx; pushl %edx; \
525 call *paravirt_ops+PARAVIRT_irq_disable; \
526 popl %edx; popl %ecx) \
528 #define ENABLE_INTERRUPTS(clobbers) \
529 PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
530 pushl %ecx; pushl %edx; \
531 call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
532 popl %edx; popl %ecx)
534 #define ENABLE_INTERRUPTS_SYSEXIT \
535 PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
536 jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
538 #define GET_CR0_INTO_EAX \
539 call *paravirt_ops+PARAVIRT_read_cr0
541 #endif /* __ASSEMBLY__ */
542 #endif /* CONFIG_PARAVIRT */
543 #endif /* __ASM_PARAVIRT_H */