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. */
7 #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. */
28 #include <linux/types.h>
31 struct Xgt_desc_struct
;
37 unsigned int kernel_rpl
;
38 int shared_kernel_pmd
;
43 * Patch may replace one of the defined code sequences with arbitrary
44 * code, subject to the same register constraints. This generally
45 * means the code is not free to clobber any registers other than EAX.
46 * The patch function should return the number of bytes of code
47 * generated, as we nop pad the rest in generic code.
49 unsigned (*patch
)(u8 type
, u16 clobber
, void *firstinsn
, unsigned len
);
51 void (*arch_setup
)(void);
52 char *(*memory_setup
)(void);
53 void (*init_IRQ
)(void);
55 void (*pagetable_setup_start
)(pgd_t
*pgd_base
);
56 void (*pagetable_setup_done
)(pgd_t
*pgd_base
);
60 unsigned long (*get_wallclock
)(void);
61 int (*set_wallclock
)(unsigned long);
62 void (*time_init
)(void);
64 void (*cpuid
)(unsigned int *eax
, unsigned int *ebx
,
65 unsigned int *ecx
, unsigned int *edx
);
67 unsigned long (*get_debugreg
)(int regno
);
68 void (*set_debugreg
)(int regno
, unsigned long value
);
72 unsigned long (*read_cr0
)(void);
73 void (*write_cr0
)(unsigned long);
75 unsigned long (*read_cr2
)(void);
76 void (*write_cr2
)(unsigned long);
78 unsigned long (*read_cr3
)(void);
79 void (*write_cr3
)(unsigned long);
81 unsigned long (*read_cr4_safe
)(void);
82 unsigned long (*read_cr4
)(void);
83 void (*write_cr4
)(unsigned long);
85 unsigned long (*save_fl
)(void);
86 void (*restore_fl
)(unsigned long);
87 void (*irq_disable
)(void);
88 void (*irq_enable
)(void);
89 void (*safe_halt
)(void);
93 /* err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
94 u64 (*read_msr
)(unsigned int msr
, int *err
);
95 int (*write_msr
)(unsigned int msr
, u64 val
);
97 u64 (*read_tsc
)(void);
98 u64 (*read_pmc
)(void);
99 u64 (*get_scheduled_cycles
)(void);
100 unsigned long (*get_cpu_khz
)(void);
102 void (*load_tr_desc
)(void);
103 void (*load_gdt
)(const struct Xgt_desc_struct
*);
104 void (*load_idt
)(const struct Xgt_desc_struct
*);
105 void (*store_gdt
)(struct Xgt_desc_struct
*);
106 void (*store_idt
)(struct Xgt_desc_struct
*);
107 void (*set_ldt
)(const void *desc
, unsigned entries
);
108 unsigned long (*store_tr
)(void);
109 void (*load_tls
)(struct thread_struct
*t
, unsigned int cpu
);
110 void (*write_ldt_entry
)(struct desc_struct
*,
111 int entrynum
, u32 low
, u32 high
);
112 void (*write_gdt_entry
)(struct desc_struct
*,
113 int entrynum
, u32 low
, u32 high
);
114 void (*write_idt_entry
)(struct desc_struct
*,
115 int entrynum
, u32 low
, u32 high
);
116 void (*load_esp0
)(struct tss_struct
*tss
, struct thread_struct
*t
);
118 void (*set_iopl_mask
)(unsigned mask
);
120 void (*io_delay
)(void);
122 void (*activate_mm
)(struct mm_struct
*prev
,
123 struct mm_struct
*next
);
124 void (*dup_mmap
)(struct mm_struct
*oldmm
,
125 struct mm_struct
*mm
);
126 void (*exit_mmap
)(struct mm_struct
*mm
);
128 #ifdef CONFIG_X86_LOCAL_APIC
129 void (*apic_write
)(unsigned long reg
, unsigned long v
);
130 void (*apic_write_atomic
)(unsigned long reg
, unsigned long v
);
131 unsigned long (*apic_read
)(unsigned long reg
);
132 void (*setup_boot_clock
)(void);
133 void (*setup_secondary_clock
)(void);
136 void (*flush_tlb_user
)(void);
137 void (*flush_tlb_kernel
)(void);
138 void (*flush_tlb_single
)(u32 addr
);
140 void (*map_pt_hook
)(int type
, pte_t
*va
, u32 pfn
);
142 void (*alloc_pt
)(u32 pfn
);
143 void (*alloc_pd
)(u32 pfn
);
144 void (*alloc_pd_clone
)(u32 pfn
, u32 clonepfn
, u32 start
, u32 count
);
145 void (*release_pt
)(u32 pfn
);
146 void (*release_pd
)(u32 pfn
);
148 void (*set_pte
)(pte_t
*ptep
, pte_t pteval
);
149 void (*set_pte_at
)(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
, pte_t pteval
);
150 void (*set_pmd
)(pmd_t
*pmdp
, pmd_t pmdval
);
151 void (*pte_update
)(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
);
152 void (*pte_update_defer
)(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
);
154 pte_t (*ptep_get_and_clear
)(pte_t
*ptep
);
156 #ifdef CONFIG_X86_PAE
157 void (*set_pte_atomic
)(pte_t
*ptep
, pte_t pteval
);
158 void (*set_pte_present
)(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
, pte_t pte
);
159 void (*set_pud
)(pud_t
*pudp
, pud_t pudval
);
160 void (*pte_clear
)(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
);
161 void (*pmd_clear
)(pmd_t
*pmdp
);
163 unsigned long long (*pte_val
)(pte_t
);
164 unsigned long long (*pmd_val
)(pmd_t
);
165 unsigned long long (*pgd_val
)(pgd_t
);
167 pte_t (*make_pte
)(unsigned long long pte
);
168 pmd_t (*make_pmd
)(unsigned long long pmd
);
169 pgd_t (*make_pgd
)(unsigned long long pgd
);
171 unsigned long (*pte_val
)(pte_t
);
172 unsigned long (*pgd_val
)(pgd_t
);
174 pte_t (*make_pte
)(unsigned long pte
);
175 pgd_t (*make_pgd
)(unsigned long pgd
);
178 void (*set_lazy_mode
)(int mode
);
180 /* These two are jmp to, not actually called. */
181 void (*irq_enable_sysexit
)(void);
184 void (*startup_ipi_hook
)(int phys_apicid
, unsigned long start_eip
, unsigned long start_esp
);
187 /* Mark a paravirt probe function. */
188 #define paravirt_probe(fn) \
189 static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
190 __attribute__((__section__(".paravirtprobe"))) = fn
192 extern struct paravirt_ops paravirt_ops
;
194 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
196 static inline void load_esp0(struct tss_struct
*tss
,
197 struct thread_struct
*thread
)
199 paravirt_ops
.load_esp0(tss
, thread
);
202 #define ARCH_SETUP paravirt_ops.arch_setup();
203 static inline unsigned long get_wallclock(void)
205 return paravirt_ops
.get_wallclock();
208 static inline int set_wallclock(unsigned long nowtime
)
210 return paravirt_ops
.set_wallclock(nowtime
);
213 static inline void (*choose_time_init(void))(void)
215 return paravirt_ops
.time_init
;
218 /* The paravirtualized CPUID instruction. */
219 static inline void __cpuid(unsigned int *eax
, unsigned int *ebx
,
220 unsigned int *ecx
, unsigned int *edx
)
222 paravirt_ops
.cpuid(eax
, ebx
, ecx
, edx
);
226 * These special macros can be used to get or set a debugging register
228 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
229 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
231 #define clts() paravirt_ops.clts()
233 #define read_cr0() paravirt_ops.read_cr0()
234 #define write_cr0(x) paravirt_ops.write_cr0(x)
236 #define read_cr2() paravirt_ops.read_cr2()
237 #define write_cr2(x) paravirt_ops.write_cr2(x)
239 #define read_cr3() paravirt_ops.read_cr3()
240 #define write_cr3(x) paravirt_ops.write_cr3(x)
242 #define read_cr4() paravirt_ops.read_cr4()
243 #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
244 #define write_cr4(x) paravirt_ops.write_cr4(x)
246 #define raw_ptep_get_and_clear(xp) (paravirt_ops.ptep_get_and_clear(xp))
248 static inline void raw_safe_halt(void)
250 paravirt_ops
.safe_halt();
253 static inline void halt(void)
255 paravirt_ops
.safe_halt();
257 #define wbinvd() paravirt_ops.wbinvd()
259 #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
261 /* These should all do BUG_ON(_err), but our headers are too tangled. */
262 #define rdmsr(msr,val1,val2) do { \
264 u64 _l = paravirt_ops.read_msr(msr,&_err); \
269 #define wrmsr(msr,val1,val2) do { \
270 u64 _l = ((u64)(val2) << 32) | (val1); \
271 paravirt_ops.write_msr((msr), _l); \
274 #define rdmsrl(msr,val) do { \
276 val = paravirt_ops.read_msr((msr),&_err); \
279 #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
280 #define wrmsr_safe(msr,a,b) ({ \
281 u64 _l = ((u64)(b) << 32) | (a); \
282 paravirt_ops.write_msr((msr),_l); \
285 /* rdmsr with exception handling */
286 #define rdmsr_safe(msr,a,b) ({ \
288 u64 _l = paravirt_ops.read_msr(msr,&_err); \
293 #define rdtsc(low,high) do { \
294 u64 _l = paravirt_ops.read_tsc(); \
299 #define rdtscl(low) do { \
300 u64 _l = paravirt_ops.read_tsc(); \
304 #define rdtscll(val) (val = paravirt_ops.read_tsc())
306 #define get_scheduled_cycles(val) (val = paravirt_ops.get_scheduled_cycles())
307 #define calculate_cpu_khz() (paravirt_ops.get_cpu_khz())
309 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
311 #define rdpmc(counter,low,high) do { \
312 u64 _l = paravirt_ops.read_pmc(); \
317 #define load_TR_desc() (paravirt_ops.load_tr_desc())
318 #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
319 #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
320 #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
321 #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
322 #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
323 #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
324 #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
325 #define write_ldt_entry(dt, entry, low, high) \
326 (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
327 #define write_gdt_entry(dt, entry, low, high) \
328 (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
329 #define write_idt_entry(dt, entry, low, high) \
330 (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
331 #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
333 #define __pte(x) paravirt_ops.make_pte(x)
334 #define __pgd(x) paravirt_ops.make_pgd(x)
336 #define pte_val(x) paravirt_ops.pte_val(x)
337 #define pgd_val(x) paravirt_ops.pgd_val(x)
339 #ifdef CONFIG_X86_PAE
340 #define __pmd(x) paravirt_ops.make_pmd(x)
341 #define pmd_val(x) paravirt_ops.pmd_val(x)
344 /* The paravirtualized I/O functions */
345 static inline void slow_down_io(void) {
346 paravirt_ops
.io_delay();
347 #ifdef REALLY_SLOW_IO
348 paravirt_ops
.io_delay();
349 paravirt_ops
.io_delay();
350 paravirt_ops
.io_delay();
354 #ifdef CONFIG_X86_LOCAL_APIC
356 * Basic functions accessing APICs.
358 static inline void apic_write(unsigned long reg
, unsigned long v
)
360 paravirt_ops
.apic_write(reg
,v
);
363 static inline void apic_write_atomic(unsigned long reg
, unsigned long v
)
365 paravirt_ops
.apic_write_atomic(reg
,v
);
368 static inline unsigned long apic_read(unsigned long reg
)
370 return paravirt_ops
.apic_read(reg
);
373 static inline void setup_boot_clock(void)
375 paravirt_ops
.setup_boot_clock();
378 static inline void setup_secondary_clock(void)
380 paravirt_ops
.setup_secondary_clock();
384 static inline void paravirt_pagetable_setup_start(pgd_t
*base
)
386 if (paravirt_ops
.pagetable_setup_start
)
387 (*paravirt_ops
.pagetable_setup_start
)(base
);
390 static inline void paravirt_pagetable_setup_done(pgd_t
*base
)
392 if (paravirt_ops
.pagetable_setup_done
)
393 (*paravirt_ops
.pagetable_setup_done
)(base
);
397 static inline void startup_ipi_hook(int phys_apicid
, unsigned long start_eip
,
398 unsigned long start_esp
)
400 return paravirt_ops
.startup_ipi_hook(phys_apicid
, start_eip
, start_esp
);
404 static inline void paravirt_activate_mm(struct mm_struct
*prev
,
405 struct mm_struct
*next
)
407 paravirt_ops
.activate_mm(prev
, next
);
410 static inline void arch_dup_mmap(struct mm_struct
*oldmm
,
411 struct mm_struct
*mm
)
413 paravirt_ops
.dup_mmap(oldmm
, mm
);
416 static inline void arch_exit_mmap(struct mm_struct
*mm
)
418 paravirt_ops
.exit_mmap(mm
);
421 #define __flush_tlb() paravirt_ops.flush_tlb_user()
422 #define __flush_tlb_global() paravirt_ops.flush_tlb_kernel()
423 #define __flush_tlb_single(addr) paravirt_ops.flush_tlb_single(addr)
425 #define paravirt_map_pt_hook(type, va, pfn) paravirt_ops.map_pt_hook(type, va, pfn)
427 #define paravirt_alloc_pt(pfn) paravirt_ops.alloc_pt(pfn)
428 #define paravirt_release_pt(pfn) paravirt_ops.release_pt(pfn)
430 #define paravirt_alloc_pd(pfn) paravirt_ops.alloc_pd(pfn)
431 #define paravirt_alloc_pd_clone(pfn, clonepfn, start, count) \
432 paravirt_ops.alloc_pd_clone(pfn, clonepfn, start, count)
433 #define paravirt_release_pd(pfn) paravirt_ops.release_pd(pfn)
435 static inline void set_pte(pte_t
*ptep
, pte_t pteval
)
437 paravirt_ops
.set_pte(ptep
, pteval
);
440 static inline void set_pte_at(struct mm_struct
*mm
, unsigned long addr
,
441 pte_t
*ptep
, pte_t pteval
)
443 paravirt_ops
.set_pte_at(mm
, addr
, ptep
, pteval
);
446 static inline void set_pmd(pmd_t
*pmdp
, pmd_t pmdval
)
448 paravirt_ops
.set_pmd(pmdp
, pmdval
);
451 static inline void pte_update(struct mm_struct
*mm
, u32 addr
, pte_t
*ptep
)
453 paravirt_ops
.pte_update(mm
, addr
, ptep
);
456 static inline void pte_update_defer(struct mm_struct
*mm
, u32 addr
, pte_t
*ptep
)
458 paravirt_ops
.pte_update_defer(mm
, addr
, ptep
);
461 #ifdef CONFIG_X86_PAE
462 static inline void set_pte_atomic(pte_t
*ptep
, pte_t pteval
)
464 paravirt_ops
.set_pte_atomic(ptep
, pteval
);
467 static inline void set_pte_present(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
, pte_t pte
)
469 paravirt_ops
.set_pte_present(mm
, addr
, ptep
, pte
);
472 static inline void set_pud(pud_t
*pudp
, pud_t pudval
)
474 paravirt_ops
.set_pud(pudp
, pudval
);
477 static inline void pte_clear(struct mm_struct
*mm
, unsigned long addr
, pte_t
*ptep
)
479 paravirt_ops
.pte_clear(mm
, addr
, ptep
);
482 static inline void pmd_clear(pmd_t
*pmdp
)
484 paravirt_ops
.pmd_clear(pmdp
);
488 /* Lazy mode for batching updates / context switch */
489 #define PARAVIRT_LAZY_NONE 0
490 #define PARAVIRT_LAZY_MMU 1
491 #define PARAVIRT_LAZY_CPU 2
492 #define PARAVIRT_LAZY_FLUSH 3
494 #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
495 #define arch_enter_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_CPU)
496 #define arch_leave_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
497 #define arch_flush_lazy_cpu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_FLUSH)
499 #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
500 #define arch_enter_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_MMU)
501 #define arch_leave_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_NONE)
502 #define arch_flush_lazy_mmu_mode() paravirt_ops.set_lazy_mode(PARAVIRT_LAZY_FLUSH)
504 void _paravirt_nop(void);
505 #define paravirt_nop ((void *)_paravirt_nop)
507 /* These all sit in the .parainstructions section to tell us what to patch. */
508 struct paravirt_patch
{
509 u8
*instr
; /* original instructions */
510 u8 instrtype
; /* type of this instruction */
511 u8 len
; /* length of original instruction */
512 u16 clobbers
; /* what registers you may clobber */
515 #define paravirt_alt(insn_string, typenum, clobber) \
516 "771:\n\t" insn_string "\n" "772:\n" \
517 ".pushsection .parainstructions,\"a\"\n" \
519 " .byte " __stringify(typenum) "\n" \
520 " .byte 772b-771b\n" \
521 " .short " __stringify(clobber) "\n" \
524 static inline unsigned long __raw_local_save_flags(void)
528 __asm__
__volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
530 "popl %%edx; popl %%ecx",
531 PARAVIRT_SAVE_FLAGS
, CLBR_NONE
)
532 : "=a"(f
): "m"(paravirt_ops
.save_fl
)
537 static inline void raw_local_irq_restore(unsigned long f
)
539 __asm__
__volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
541 "popl %%edx; popl %%ecx",
542 PARAVIRT_RESTORE_FLAGS
, CLBR_EAX
)
543 : "=a"(f
) : "m" (paravirt_ops
.restore_fl
), "0"(f
)
547 static inline void raw_local_irq_disable(void)
549 __asm__
__volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
551 "popl %%edx; popl %%ecx",
552 PARAVIRT_IRQ_DISABLE
, CLBR_EAX
)
553 : : "m" (paravirt_ops
.irq_disable
)
554 : "memory", "eax", "cc");
557 static inline void raw_local_irq_enable(void)
559 __asm__
__volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
561 "popl %%edx; popl %%ecx",
562 PARAVIRT_IRQ_ENABLE
, CLBR_EAX
)
563 : : "m" (paravirt_ops
.irq_enable
)
564 : "memory", "eax", "cc");
567 static inline unsigned long __raw_local_irq_save(void)
571 __asm__
__volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
572 "call *%1; pushl %%eax;"
573 "call *%2; popl %%eax;"
574 "popl %%edx; popl %%ecx",
575 PARAVIRT_SAVE_FLAGS_IRQ_DISABLE
,
578 : "m" (paravirt_ops
.save_fl
),
579 "m" (paravirt_ops
.irq_disable
)
584 #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
585 "call *paravirt_ops+%c[irq_disable];" \
586 "popl %%edx; popl %%ecx", \
587 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
589 #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
590 "call *paravirt_ops+%c[irq_enable];" \
591 "popl %%edx; popl %%ecx", \
592 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
593 #define CLI_STI_CLOBBERS , "%eax"
594 #define CLI_STI_INPUT_ARGS \
596 [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
597 [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
599 #else /* __ASSEMBLY__ */
601 #define PARA_PATCH(ptype, clobbers, ops) \
605 .pushsection .parainstructions,"a"; \
612 #define INTERRUPT_RETURN \
613 PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
614 jmp *%cs:paravirt_ops+PARAVIRT_iret)
616 #define DISABLE_INTERRUPTS(clobbers) \
617 PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
618 pushl %ecx; pushl %edx; \
619 call *paravirt_ops+PARAVIRT_irq_disable; \
620 popl %edx; popl %ecx) \
622 #define ENABLE_INTERRUPTS(clobbers) \
623 PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
624 pushl %ecx; pushl %edx; \
625 call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
626 popl %edx; popl %ecx)
628 #define ENABLE_INTERRUPTS_SYSEXIT \
629 PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
630 jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
632 #define GET_CR0_INTO_EAX \
633 call *paravirt_ops+PARAVIRT_read_cr0
635 #endif /* __ASSEMBLY__ */
636 #endif /* CONFIG_PARAVIRT */
637 #endif /* __ASM_PARAVIRT_H */