blob: eac25b5323a256e51a1c279e8569c0b57d4bd541 [file] [log] [blame]
Rusty Russelld3561b72006-12-07 02:14:07 +01001#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. */
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +02005
6#ifdef CONFIG_PARAVIRT
Rusty Russellda181a82006-12-07 02:14:08 +01007#include <asm/page.h>
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01008#include <asm/asm.h>
Rusty Russelld3561b72006-12-07 02:14:07 +01009
Rusty Russell139ec7c2006-12-07 02:14:08 +010010/* Bitmask of what can be clobbered: usually at least eax. */
Glauber de Oliveira Costa21438f72008-01-30 13:32:09 +010011#define CLBR_NONE 0
12#define CLBR_EAX (1 << 0)
13#define CLBR_ECX (1 << 1)
14#define CLBR_EDX (1 << 2)
15
16#ifdef CONFIG_X86_64
17#define CLBR_RSI (1 << 3)
18#define CLBR_RDI (1 << 4)
19#define CLBR_R8 (1 << 5)
20#define CLBR_R9 (1 << 6)
21#define CLBR_R10 (1 << 7)
22#define CLBR_R11 (1 << 8)
23#define CLBR_ANY ((1 << 9) - 1)
24#include <asm/desc_defs.h>
25#else
26/* CLBR_ANY should match all regs platform has. For i386, that's just it */
27#define CLBR_ANY ((1 << 3) - 1)
28#endif /* X86_64 */
Rusty Russell139ec7c2006-12-07 02:14:08 +010029
Rusty Russelld3561b72006-12-07 02:14:07 +010030#ifndef __ASSEMBLY__
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +020031#include <linux/types.h>
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +020032#include <linux/cpumask.h>
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020033#include <asm/kmap_types.h>
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +010034#include <asm/desc_defs.h>
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +020035
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +020036struct page;
Rusty Russelld3561b72006-12-07 02:14:07 +010037struct thread_struct;
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010038struct desc_ptr;
Rusty Russelld3561b72006-12-07 02:14:07 +010039struct tss_struct;
Rusty Russellda181a82006-12-07 02:14:08 +010040struct mm_struct;
Rusty Russell90a0a062007-05-02 19:27:10 +020041struct desc_struct;
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +020042
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070043/* general info */
44struct pv_info {
Rusty Russelld3561b72006-12-07 02:14:07 +010045 unsigned int kernel_rpl;
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +020046 int shared_kernel_pmd;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070047 int paravirt_enabled;
Rusty Russelld3561b72006-12-07 02:14:07 +010048 const char *name;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070049};
Rusty Russelld3561b72006-12-07 02:14:07 +010050
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070051struct pv_init_ops {
Rusty Russell139ec7c2006-12-07 02:14:08 +010052 /*
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070053 * Patch may replace one of the defined code sequences with
54 * arbitrary code, subject to the same register constraints.
55 * This generally means the code is not free to clobber any
56 * registers other than EAX. The patch function should return
57 * the number of bytes of code generated, as we nop pad the
58 * rest in generic code.
Rusty Russell139ec7c2006-12-07 02:14:08 +010059 */
Andi Kleenab144f52007-08-10 22:31:03 +020060 unsigned (*patch)(u8 type, u16 clobber, void *insnbuf,
61 unsigned long addr, unsigned len);
Rusty Russell139ec7c2006-12-07 02:14:08 +010062
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +020063 /* Basic arch-specific setup */
Rusty Russelld3561b72006-12-07 02:14:07 +010064 void (*arch_setup)(void);
65 char *(*memory_setup)(void);
Jeremy Fitzhardinge6996d3b2007-07-17 18:37:03 -070066 void (*post_allocator_init)(void);
67
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +020068 /* Print a banner to identify the environment */
Rusty Russelld3561b72006-12-07 02:14:07 +010069 void (*banner)(void);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070070};
71
72
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -070073struct pv_lazy_ops {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070074 /* Set deferred update mode, used for batching operations. */
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -070075 void (*enter)(void);
76 void (*leave)(void);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070077};
78
79struct pv_time_ops {
80 void (*time_init)(void);
Rusty Russelld3561b72006-12-07 02:14:07 +010081
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +020082 /* Set and set time of day */
Rusty Russelld3561b72006-12-07 02:14:07 +010083 unsigned long (*get_wallclock)(void);
84 int (*set_wallclock)(unsigned long);
Rusty Russelld3561b72006-12-07 02:14:07 +010085
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070086 unsigned long long (*sched_clock)(void);
87 unsigned long (*get_cpu_khz)(void);
88};
Rusty Russelld3561b72006-12-07 02:14:07 +010089
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -070090struct pv_cpu_ops {
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +020091 /* hooks for various privileged instructions */
Andi Kleen1a1eecd2007-02-13 13:26:25 +010092 unsigned long (*get_debugreg)(int regno);
93 void (*set_debugreg)(int regno, unsigned long value);
Rusty Russelld3561b72006-12-07 02:14:07 +010094
Andi Kleen1a1eecd2007-02-13 13:26:25 +010095 void (*clts)(void);
Rusty Russelld3561b72006-12-07 02:14:07 +010096
Andi Kleen1a1eecd2007-02-13 13:26:25 +010097 unsigned long (*read_cr0)(void);
98 void (*write_cr0)(unsigned long);
Rusty Russelld3561b72006-12-07 02:14:07 +010099
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100100 unsigned long (*read_cr4_safe)(void);
101 unsigned long (*read_cr4)(void);
102 void (*write_cr4)(unsigned long);
Rusty Russelld3561b72006-12-07 02:14:07 +0100103
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200104 /* Segment descriptor handling */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100105 void (*load_tr_desc)(void);
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100106 void (*load_gdt)(const struct desc_ptr *);
107 void (*load_idt)(const struct desc_ptr *);
108 void (*store_gdt)(struct desc_ptr *);
109 void (*store_idt)(struct desc_ptr *);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100110 void (*set_ldt)(const void *desc, unsigned entries);
111 unsigned long (*store_tr)(void);
112 void (*load_tls)(struct thread_struct *t, unsigned int cpu);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100113 void (*write_ldt_entry)(struct desc_struct *ldt, int entrynum,
114 const void *desc);
Rusty Russell90a0a062007-05-02 19:27:10 +0200115 void (*write_gdt_entry)(struct desc_struct *,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100116 int entrynum, const void *desc, int size);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100117 void (*write_idt_entry)(gate_desc *,
118 int entrynum, const gate_desc *gate);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100119 void (*load_sp0)(struct tss_struct *tss, struct thread_struct *t);
Rusty Russelld3561b72006-12-07 02:14:07 +0100120
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100121 void (*set_iopl_mask)(unsigned mask);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700122
123 void (*wbinvd)(void);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100124 void (*io_delay)(void);
Rusty Russelld3561b72006-12-07 02:14:07 +0100125
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700126 /* cpuid emulation, mostly so that caps bits can be disabled */
127 void (*cpuid)(unsigned int *eax, unsigned int *ebx,
128 unsigned int *ecx, unsigned int *edx);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200129
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700130 /* MSR, PMC and TSR operations.
131 err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
132 u64 (*read_msr)(unsigned int msr, int *err);
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100133 int (*write_msr)(unsigned int msr, unsigned low, unsigned high);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700134
135 u64 (*read_tsc)(void);
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100136 u64 (*read_pmc)(int counter);
Glauber de Oliveira Costae5aaac42008-01-30 13:32:05 +0100137 unsigned long long (*read_tscp)(unsigned int *aux);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700138
139 /* These two are jmp to, not actually called. */
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +0100140 void (*irq_enable_syscall_ret)(void);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700141 void (*iret)(void);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700142
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +0100143 void (*swapgs)(void);
144
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700145 struct pv_lazy_ops lazy_mode;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700146};
147
148struct pv_irq_ops {
149 void (*init_IRQ)(void);
150
151 /*
152 * Get/set interrupt state. save_fl and restore_fl are only
153 * expected to use X86_EFLAGS_IF; all other bits
154 * returned from save_fl are undefined, and may be ignored by
155 * restore_fl.
156 */
157 unsigned long (*save_fl)(void);
158 void (*restore_fl)(unsigned long);
159 void (*irq_disable)(void);
160 void (*irq_enable)(void);
161 void (*safe_halt)(void);
162 void (*halt)(void);
163};
164
165struct pv_apic_ops {
Rusty Russell13623d72006-12-07 02:14:08 +0100166#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200167 /*
168 * Direct APIC operations, principally for VMI. Ideally
169 * these shouldn't be in this interface.
170 */
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100171 void (*apic_write)(unsigned long reg, u32 v);
172 void (*apic_write_atomic)(unsigned long reg, u32 v);
173 u32 (*apic_read)(unsigned long reg);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100174 void (*setup_boot_clock)(void);
175 void (*setup_secondary_clock)(void);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200176
177 void (*startup_ipi_hook)(int phys_apicid,
178 unsigned long start_eip,
179 unsigned long start_esp);
Rusty Russell13623d72006-12-07 02:14:08 +0100180#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700181};
182
183struct pv_mmu_ops {
184 /*
185 * Called before/after init_mm pagetable setup. setup_start
186 * may reset %cr3, and may pre-install parts of the pagetable;
187 * pagetable setup is expected to preserve any existing
188 * mapping.
189 */
190 void (*pagetable_setup_start)(pgd_t *pgd_base);
191 void (*pagetable_setup_done)(pgd_t *pgd_base);
192
193 unsigned long (*read_cr2)(void);
194 void (*write_cr2)(unsigned long);
195
196 unsigned long (*read_cr3)(void);
197 void (*write_cr3)(unsigned long);
198
199 /*
200 * Hooks for intercepting the creation/use/destruction of an
201 * mm_struct.
202 */
203 void (*activate_mm)(struct mm_struct *prev,
204 struct mm_struct *next);
205 void (*dup_mmap)(struct mm_struct *oldmm,
206 struct mm_struct *mm);
207 void (*exit_mmap)(struct mm_struct *mm);
208
Rusty Russell13623d72006-12-07 02:14:08 +0100209
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200210 /* TLB operations */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100211 void (*flush_tlb_user)(void);
212 void (*flush_tlb_kernel)(void);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200213 void (*flush_tlb_single)(unsigned long addr);
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200214 void (*flush_tlb_others)(const cpumask_t *cpus, struct mm_struct *mm,
215 unsigned long va);
Rusty Russellda181a82006-12-07 02:14:08 +0100216
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200217 /* Hooks for allocating/releasing pagetable pages */
Jeremy Fitzhardingefdb4c332007-07-17 18:37:03 -0700218 void (*alloc_pt)(struct mm_struct *mm, u32 pfn);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100219 void (*alloc_pd)(u32 pfn);
220 void (*alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
221 void (*release_pt)(u32 pfn);
222 void (*release_pd)(u32 pfn);
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100223
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200224 /* Pagetable manipulation functions */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100225 void (*set_pte)(pte_t *ptep, pte_t pteval);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200226 void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
227 pte_t *ptep, pte_t pteval);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100228 void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200229 void (*pte_update)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200230 void (*pte_update_defer)(struct mm_struct *mm,
231 unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200232
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100233 pteval_t (*pte_val)(pte_t);
234 pte_t (*make_pte)(pteval_t pte);
235
236 pgdval_t (*pgd_val)(pgd_t);
237 pgd_t (*make_pgd)(pgdval_t pgd);
238
239#if PAGETABLE_LEVELS >= 3
Rusty Russellda181a82006-12-07 02:14:08 +0100240#ifdef CONFIG_X86_PAE
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100241 void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700242 void (*set_pte_present)(struct mm_struct *mm, unsigned long addr,
243 pte_t *ptep, pte_t pte);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700244 void (*pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100245 void (*pmd_clear)(pmd_t *pmdp);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200246
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100247#endif /* CONFIG_X86_PAE */
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200248
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100249 void (*set_pud)(pud_t *pudp, pud_t pudval);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200250
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100251 pmdval_t (*pmd_val)(pmd_t);
252 pmd_t (*make_pmd)(pmdval_t pmd);
253
254#if PAGETABLE_LEVELS == 4
255 pudval_t (*pud_val)(pud_t);
256 pud_t (*make_pud)(pudval_t pud);
257#endif /* PAGETABLE_LEVELS == 4 */
258#endif /* PAGETABLE_LEVELS >= 3 */
Rusty Russellda181a82006-12-07 02:14:08 +0100259
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700260#ifdef CONFIG_HIGHPTE
261 void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
262#endif
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700263
264 struct pv_lazy_ops lazy_mode;
Rusty Russelld3561b72006-12-07 02:14:07 +0100265};
266
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700267/* This contains all the paravirt structures: we get a convenient
268 * number for each function using the offset which we use to indicate
269 * what to patch. */
270struct paravirt_patch_template
271{
272 struct pv_init_ops pv_init_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700273 struct pv_time_ops pv_time_ops;
274 struct pv_cpu_ops pv_cpu_ops;
275 struct pv_irq_ops pv_irq_ops;
276 struct pv_apic_ops pv_apic_ops;
277 struct pv_mmu_ops pv_mmu_ops;
278};
279
280extern struct pv_info pv_info;
281extern struct pv_init_ops pv_init_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700282extern struct pv_time_ops pv_time_ops;
283extern struct pv_cpu_ops pv_cpu_ops;
284extern struct pv_irq_ops pv_irq_ops;
285extern struct pv_apic_ops pv_apic_ops;
286extern struct pv_mmu_ops pv_mmu_ops;
Rusty Russelld3561b72006-12-07 02:14:07 +0100287
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200288#define PARAVIRT_PATCH(x) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700289 (offsetof(struct paravirt_patch_template, x) / sizeof(void *))
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200290
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700291#define paravirt_type(op) \
292 [paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \
293 [paravirt_opptr] "m" (op)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200294#define paravirt_clobber(clobber) \
295 [paravirt_clobber] "i" (clobber)
296
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200297/*
298 * Generate some code, and mark it as patchable by the
299 * apply_paravirt() alternate instruction patcher.
300 */
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200301#define _paravirt_alt(insn_string, type, clobber) \
302 "771:\n\t" insn_string "\n" "772:\n" \
303 ".pushsection .parainstructions,\"a\"\n" \
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +0100304 _ASM_ALIGN "\n" \
305 _ASM_PTR " 771b\n" \
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200306 " .byte " type "\n" \
307 " .byte 772b-771b\n" \
308 " .short " clobber "\n" \
309 ".popsection\n"
310
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200311/* Generate patchable code, with the default asm parameters. */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200312#define paravirt_alt(insn_string) \
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200313 _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
314
Glauber de Oliveira Costa2f485ef2008-01-30 13:32:10 +0100315/* Simple instruction patching code. */
316#define DEF_NATIVE(ops, name, code) \
317 extern const char start_##ops##_##name[], end_##ops##_##name[]; \
318 asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
319
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200320unsigned paravirt_patch_nop(void);
321unsigned paravirt_patch_ignore(unsigned len);
Andi Kleenab144f52007-08-10 22:31:03 +0200322unsigned paravirt_patch_call(void *insnbuf,
323 const void *target, u16 tgt_clobbers,
324 unsigned long addr, u16 site_clobbers,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200325 unsigned len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700326unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
Andi Kleenab144f52007-08-10 22:31:03 +0200327 unsigned long addr, unsigned len);
328unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
329 unsigned long addr, unsigned len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200330
Andi Kleenab144f52007-08-10 22:31:03 +0200331unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200332 const char *start, const char *end);
333
Glauber de Oliveira Costa2f485ef2008-01-30 13:32:10 +0100334unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
335 unsigned long addr, unsigned len);
336
Jeremy Fitzhardinged5729292007-07-17 18:37:04 -0700337int paravirt_disable_iospace(void);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200338
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200339/*
340 * This generates an indirect call based on the operation type number.
341 * The type number, computed in PARAVIRT_PATCH, is derived from the
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700342 * offset into the paravirt_patch_template structure, and can therefore be
343 * freely converted back into a structure offset.
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200344 */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700345#define PARAVIRT_CALL "call *%[paravirt_opptr];"
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200346
347/*
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700348 * These macros are intended to wrap calls through one of the paravirt
349 * ops structs, so that they can be later identified and patched at
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200350 * runtime.
351 *
352 * Normally, a call to a pv_op function is a simple indirect call:
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100353 * (pv_op_struct.operations)(args...).
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200354 *
355 * Unfortunately, this is a relatively slow operation for modern CPUs,
356 * because it cannot necessarily determine what the destination
357 * address is. In this case, the address is a runtime constant, so at
358 * the very least we can patch the call to e a simple direct call, or
359 * ideally, patch an inline implementation into the callsite. (Direct
360 * calls are essentially free, because the call and return addresses
361 * are completely predictable.)
362 *
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100363 * For i386, these macros rely on the standard gcc "regparm(3)" calling
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200364 * convention, in which the first three arguments are placed in %eax,
365 * %edx, %ecx (in that order), and the remaining arguments are placed
366 * on the stack. All caller-save registers (eax,edx,ecx) are expected
367 * to be modified (either clobbered or used for return values).
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100368 * X86_64, on the other hand, already specifies a register-based calling
369 * conventions, returning at %rax, with parameteres going on %rdi, %rsi,
370 * %rdx, and %rcx. Note that for this reason, x86_64 does not need any
371 * special handling for dealing with 4 arguments, unlike i386.
372 * However, x86_64 also have to clobber all caller saved registers, which
373 * unfortunately, are quite a bit (r8 - r11)
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200374 *
375 * The call instruction itself is marked by placing its start address
376 * and size into the .parainstructions section, so that
377 * apply_paravirt() in arch/i386/kernel/alternative.c can do the
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700378 * appropriate patching under the control of the backend pv_init_ops
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200379 * implementation.
380 *
381 * Unfortunately there's no way to get gcc to generate the args setup
382 * for the call, and then allow the call itself to be generated by an
383 * inline asm. Because of this, we must do the complete arg setup and
384 * return value handling from within these macros. This is fairly
385 * cumbersome.
386 *
387 * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
388 * It could be extended to more arguments, but there would be little
389 * to be gained from that. For each number of arguments, there are
390 * the two VCALL and CALL variants for void and non-void functions.
391 *
392 * When there is a return value, the invoker of the macro must specify
393 * the return type. The macro then uses sizeof() on that type to
394 * determine whether its a 32 or 64 bit value, and places the return
395 * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100396 * 64-bit). For x86_64 machines, it just returns at %rax regardless of
397 * the return value size.
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200398 *
399 * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100400 * i386 also passes 64-bit arguments as a pair of adjacent 32-bit arguments
401 * in low,high order
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200402 *
403 * Small structures are passed and returned in registers. The macro
404 * calling convention can't directly deal with this, so the wrapper
405 * functions must do this.
406 *
407 * These PVOP_* macros are only defined within this header. This
408 * means that all uses must be wrapped in inline functions. This also
409 * makes sure the incoming and outgoing types are always correct.
410 */
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100411#ifdef CONFIG_X86_32
412#define PVOP_VCALL_ARGS unsigned long __eax, __edx, __ecx
413#define PVOP_CALL_ARGS PVOP_VCALL_ARGS
414#define PVOP_VCALL_CLOBBERS "=a" (__eax), "=d" (__edx), \
415 "=c" (__ecx)
416#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS
417#define EXTRA_CLOBBERS
418#define VEXTRA_CLOBBERS
419#else
420#define PVOP_VCALL_ARGS unsigned long __edi, __esi, __edx, __ecx
421#define PVOP_CALL_ARGS PVOP_VCALL_ARGS, __eax
422#define PVOP_VCALL_CLOBBERS "=D" (__edi), \
423 "=S" (__esi), "=d" (__edx), \
424 "=c" (__ecx)
425
426#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS, "=a" (__eax)
427
428#define EXTRA_CLOBBERS , "r8", "r9", "r10", "r11"
429#define VEXTRA_CLOBBERS , "rax", "r8", "r9", "r10", "r11"
430#endif
431
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200432#define __PVOP_CALL(rettype, op, pre, post, ...) \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200433 ({ \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200434 rettype __ret; \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100435 PVOP_CALL_ARGS; \
436 /* This is 32-bit specific, but is okay in 64-bit */ \
437 /* since this condition will never hold */ \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200438 if (sizeof(rettype) > sizeof(unsigned long)) { \
439 asm volatile(pre \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200440 paravirt_alt(PARAVIRT_CALL) \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200441 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100442 : PVOP_CALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200443 : paravirt_type(op), \
444 paravirt_clobber(CLBR_ANY), \
445 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100446 : "memory", "cc" EXTRA_CLOBBERS); \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200447 __ret = (rettype)((((u64)__edx) << 32) | __eax); \
448 } else { \
449 asm volatile(pre \
450 paravirt_alt(PARAVIRT_CALL) \
451 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100452 : PVOP_CALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200453 : paravirt_type(op), \
454 paravirt_clobber(CLBR_ANY), \
455 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100456 : "memory", "cc" EXTRA_CLOBBERS); \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200457 __ret = (rettype)__eax; \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200458 } \
459 __ret; \
460 })
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200461#define __PVOP_VCALL(op, pre, post, ...) \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200462 ({ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100463 PVOP_VCALL_ARGS; \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200464 asm volatile(pre \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200465 paravirt_alt(PARAVIRT_CALL) \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200466 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100467 : PVOP_VCALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200468 : paravirt_type(op), \
469 paravirt_clobber(CLBR_ANY), \
470 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100471 : "memory", "cc" VEXTRA_CLOBBERS); \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200472 })
473
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200474#define PVOP_CALL0(rettype, op) \
475 __PVOP_CALL(rettype, op, "", "")
476#define PVOP_VCALL0(op) \
477 __PVOP_VCALL(op, "", "")
478
479#define PVOP_CALL1(rettype, op, arg1) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100480 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200481#define PVOP_VCALL1(op, arg1) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100482 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200483
484#define PVOP_CALL2(rettype, op, arg1, arg2) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100485 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
486 "1" ((unsigned long)(arg2)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200487#define PVOP_VCALL2(op, arg1, arg2) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100488 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
489 "1" ((unsigned long)(arg2)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200490
491#define PVOP_CALL3(rettype, op, arg1, arg2, arg3) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100492 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
493 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200494#define PVOP_VCALL3(op, arg1, arg2, arg3) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100495 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
496 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200497
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100498/* This is the only difference in x86_64. We can make it much simpler */
499#ifdef CONFIG_X86_32
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200500#define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
501 __PVOP_CALL(rettype, op, \
502 "push %[_arg4];", "lea 4(%%esp),%%esp;", \
503 "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
504 "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
505#define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
506 __PVOP_VCALL(op, \
507 "push %[_arg4];", "lea 4(%%esp),%%esp;", \
508 "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
509 "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100510#else
511#define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
512 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
513 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
514 "3"((unsigned long)(arg4)))
515#define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
516 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
517 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
518 "3"((unsigned long)(arg4)))
519#endif
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200520
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200521static inline int paravirt_enabled(void)
522{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700523 return pv_info.paravirt_enabled;
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200524}
Rusty Russelld3561b72006-12-07 02:14:07 +0100525
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100526static inline void load_sp0(struct tss_struct *tss,
Rusty Russelld3561b72006-12-07 02:14:07 +0100527 struct thread_struct *thread)
528{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100529 PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
Rusty Russelld3561b72006-12-07 02:14:07 +0100530}
531
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700532#define ARCH_SETUP pv_init_ops.arch_setup();
Rusty Russelld3561b72006-12-07 02:14:07 +0100533static inline unsigned long get_wallclock(void)
534{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700535 return PVOP_CALL0(unsigned long, pv_time_ops.get_wallclock);
Rusty Russelld3561b72006-12-07 02:14:07 +0100536}
537
538static inline int set_wallclock(unsigned long nowtime)
539{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700540 return PVOP_CALL1(int, pv_time_ops.set_wallclock, nowtime);
Rusty Russelld3561b72006-12-07 02:14:07 +0100541}
542
Zachary Amsdene30fab32007-03-05 00:30:39 -0800543static inline void (*choose_time_init(void))(void)
Rusty Russelld3561b72006-12-07 02:14:07 +0100544{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700545 return pv_time_ops.time_init;
Rusty Russelld3561b72006-12-07 02:14:07 +0100546}
547
548/* The paravirtualized CPUID instruction. */
549static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
550 unsigned int *ecx, unsigned int *edx)
551{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700552 PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
Rusty Russelld3561b72006-12-07 02:14:07 +0100553}
554
555/*
556 * These special macros can be used to get or set a debugging register
557 */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200558static inline unsigned long paravirt_get_debugreg(int reg)
559{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700560 return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200561}
562#define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
563static inline void set_debugreg(unsigned long val, int reg)
564{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700565 PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200566}
Rusty Russelld3561b72006-12-07 02:14:07 +0100567
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200568static inline void clts(void)
569{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700570 PVOP_VCALL0(pv_cpu_ops.clts);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200571}
Rusty Russelld3561b72006-12-07 02:14:07 +0100572
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200573static inline unsigned long read_cr0(void)
574{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700575 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200576}
Rusty Russelld3561b72006-12-07 02:14:07 +0100577
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200578static inline void write_cr0(unsigned long x)
579{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700580 PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200581}
Rusty Russelld3561b72006-12-07 02:14:07 +0100582
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200583static inline unsigned long read_cr2(void)
584{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700585 return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200586}
Rusty Russelld3561b72006-12-07 02:14:07 +0100587
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200588static inline void write_cr2(unsigned long x)
589{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700590 PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200591}
Rusty Russelld3561b72006-12-07 02:14:07 +0100592
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200593static inline unsigned long read_cr3(void)
594{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700595 return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200596}
597
598static inline void write_cr3(unsigned long x)
599{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700600 PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200601}
602
603static inline unsigned long read_cr4(void)
604{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700605 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200606}
607static inline unsigned long read_cr4_safe(void)
608{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700609 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200610}
611
612static inline void write_cr4(unsigned long x)
613{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700614 PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200615}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200616
Rusty Russelld3561b72006-12-07 02:14:07 +0100617static inline void raw_safe_halt(void)
618{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700619 PVOP_VCALL0(pv_irq_ops.safe_halt);
Rusty Russelld3561b72006-12-07 02:14:07 +0100620}
621
622static inline void halt(void)
623{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700624 PVOP_VCALL0(pv_irq_ops.safe_halt);
Rusty Russelld3561b72006-12-07 02:14:07 +0100625}
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200626
627static inline void wbinvd(void)
628{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700629 PVOP_VCALL0(pv_cpu_ops.wbinvd);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200630}
Rusty Russelld3561b72006-12-07 02:14:07 +0100631
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700632#define get_kernel_rpl() (pv_info.kernel_rpl)
Rusty Russelld3561b72006-12-07 02:14:07 +0100633
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200634static inline u64 paravirt_read_msr(unsigned msr, int *err)
635{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700636 return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200637}
638static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
639{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700640 return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200641}
642
Rusty Russell90a0a062007-05-02 19:27:10 +0200643/* These should all do BUG_ON(_err), but our headers are too tangled. */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200644#define rdmsr(msr,val1,val2) do { \
645 int _err; \
646 u64 _l = paravirt_read_msr(msr, &_err); \
647 val1 = (u32)_l; \
648 val2 = _l >> 32; \
Rusty Russelld3561b72006-12-07 02:14:07 +0100649} while(0)
650
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200651#define wrmsr(msr,val1,val2) do { \
652 paravirt_write_msr(msr, val1, val2); \
Rusty Russelld3561b72006-12-07 02:14:07 +0100653} while(0)
654
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200655#define rdmsrl(msr,val) do { \
656 int _err; \
657 val = paravirt_read_msr(msr, &_err); \
Rusty Russelld3561b72006-12-07 02:14:07 +0100658} while(0)
659
Björn Steinbrinkb9e36142007-06-25 23:04:37 +0200660#define wrmsrl(msr,val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200661#define wrmsr_safe(msr,a,b) paravirt_write_msr(msr, a, b)
Rusty Russelld3561b72006-12-07 02:14:07 +0100662
663/* rdmsr with exception handling */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200664#define rdmsr_safe(msr,a,b) ({ \
665 int _err; \
666 u64 _l = paravirt_read_msr(msr, &_err); \
667 (*a) = (u32)_l; \
668 (*b) = _l >> 32; \
Rusty Russelld3561b72006-12-07 02:14:07 +0100669 _err; })
670
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200671
672static inline u64 paravirt_read_tsc(void)
673{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700674 return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200675}
Rusty Russelld3561b72006-12-07 02:14:07 +0100676
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200677#define rdtscl(low) do { \
678 u64 _l = paravirt_read_tsc(); \
679 low = (int)_l; \
Rusty Russelld3561b72006-12-07 02:14:07 +0100680} while(0)
681
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200682#define rdtscll(val) (val = paravirt_read_tsc())
Rusty Russelld3561b72006-12-07 02:14:07 +0100683
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700684static inline unsigned long long paravirt_sched_clock(void)
685{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700686 return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700687}
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700688#define calculate_cpu_khz() (pv_time_ops.get_cpu_khz())
Zachary Amsden6cb9a832007-03-05 00:30:35 -0800689
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200690static inline unsigned long long paravirt_read_pmc(int counter)
691{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700692 return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200693}
694
695#define rdpmc(counter,low,high) do { \
696 u64 _l = paravirt_read_pmc(counter); \
697 low = (u32)_l; \
698 high = _l >> 32; \
Rusty Russelld3561b72006-12-07 02:14:07 +0100699} while(0)
700
Glauber de Oliveira Costae5aaac42008-01-30 13:32:05 +0100701static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
702{
703 return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
704}
705
706#define rdtscp(low, high, aux) \
707do { \
708 int __aux; \
709 unsigned long __val = paravirt_rdtscp(&__aux); \
710 (low) = (u32)__val; \
711 (high) = (u32)(__val >> 32); \
712 (aux) = __aux; \
713} while (0)
714
715#define rdtscpll(val, aux) \
716do { \
717 unsigned long __aux; \
718 val = paravirt_rdtscp(&__aux); \
719 (aux) = __aux; \
720} while (0)
721
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200722static inline void load_TR_desc(void)
723{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700724 PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200725}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100726static inline void load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200727{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700728 PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200729}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100730static inline void load_idt(const struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200731{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700732 PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200733}
734static inline void set_ldt(const void *addr, unsigned entries)
735{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700736 PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200737}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100738static inline void store_gdt(struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200739{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700740 PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200741}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100742static inline void store_idt(struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200743{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700744 PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200745}
746static inline unsigned long paravirt_store_tr(void)
747{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700748 return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200749}
750#define store_tr(tr) ((tr) = paravirt_store_tr())
751static inline void load_TLS(struct thread_struct *t, unsigned cpu)
752{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700753 PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200754}
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100755
756static inline void write_ldt_entry(struct desc_struct *dt, int entry,
757 const void *desc)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200758{
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100759 PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200760}
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100761
762static inline void write_gdt_entry(struct desc_struct *dt, int entry,
763 void *desc, int type)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200764{
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100765 PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200766}
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100767
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100768static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200769{
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100770 PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200771}
772static inline void set_iopl_mask(unsigned mask)
773{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700774 PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200775}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200776
Rusty Russelld3561b72006-12-07 02:14:07 +0100777/* The paravirtualized I/O functions */
778static inline void slow_down_io(void) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700779 pv_cpu_ops.io_delay();
Rusty Russelld3561b72006-12-07 02:14:07 +0100780#ifdef REALLY_SLOW_IO
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700781 pv_cpu_ops.io_delay();
782 pv_cpu_ops.io_delay();
783 pv_cpu_ops.io_delay();
Rusty Russelld3561b72006-12-07 02:14:07 +0100784#endif
785}
786
Rusty Russell13623d72006-12-07 02:14:08 +0100787#ifdef CONFIG_X86_LOCAL_APIC
788/*
789 * Basic functions accessing APICs.
790 */
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100791static inline void apic_write(unsigned long reg, u32 v)
Rusty Russell13623d72006-12-07 02:14:08 +0100792{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700793 PVOP_VCALL2(pv_apic_ops.apic_write, reg, v);
Rusty Russell13623d72006-12-07 02:14:08 +0100794}
795
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100796static inline void apic_write_atomic(unsigned long reg, u32 v)
Rusty Russell13623d72006-12-07 02:14:08 +0100797{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700798 PVOP_VCALL2(pv_apic_ops.apic_write_atomic, reg, v);
Rusty Russell13623d72006-12-07 02:14:08 +0100799}
800
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100801static inline u32 apic_read(unsigned long reg)
Rusty Russell13623d72006-12-07 02:14:08 +0100802{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700803 return PVOP_CALL1(unsigned long, pv_apic_ops.apic_read, reg);
Rusty Russell13623d72006-12-07 02:14:08 +0100804}
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100805
806static inline void setup_boot_clock(void)
807{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700808 PVOP_VCALL0(pv_apic_ops.setup_boot_clock);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100809}
810
811static inline void setup_secondary_clock(void)
812{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700813 PVOP_VCALL0(pv_apic_ops.setup_secondary_clock);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100814}
Rusty Russell13623d72006-12-07 02:14:08 +0100815#endif
816
Jeremy Fitzhardinge6996d3b2007-07-17 18:37:03 -0700817static inline void paravirt_post_allocator_init(void)
818{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700819 if (pv_init_ops.post_allocator_init)
820 (*pv_init_ops.post_allocator_init)();
Jeremy Fitzhardinge6996d3b2007-07-17 18:37:03 -0700821}
822
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200823static inline void paravirt_pagetable_setup_start(pgd_t *base)
824{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700825 (*pv_mmu_ops.pagetable_setup_start)(base);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200826}
827
828static inline void paravirt_pagetable_setup_done(pgd_t *base)
829{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700830 (*pv_mmu_ops.pagetable_setup_done)(base);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200831}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200832
Zachary Amsdenae5da272007-02-13 13:26:21 +0100833#ifdef CONFIG_SMP
834static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
835 unsigned long start_esp)
836{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700837 PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
838 phys_apicid, start_eip, start_esp);
Zachary Amsdenae5da272007-02-13 13:26:21 +0100839}
840#endif
Rusty Russell13623d72006-12-07 02:14:08 +0100841
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200842static inline void paravirt_activate_mm(struct mm_struct *prev,
843 struct mm_struct *next)
844{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700845 PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200846}
847
848static inline void arch_dup_mmap(struct mm_struct *oldmm,
849 struct mm_struct *mm)
850{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700851 PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200852}
853
854static inline void arch_exit_mmap(struct mm_struct *mm)
855{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700856 PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200857}
858
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200859static inline void __flush_tlb(void)
860{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700861 PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200862}
863static inline void __flush_tlb_global(void)
864{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700865 PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200866}
867static inline void __flush_tlb_single(unsigned long addr)
868{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700869 PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200870}
Rusty Russellda181a82006-12-07 02:14:08 +0100871
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200872static inline void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
873 unsigned long va)
874{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700875 PVOP_VCALL3(pv_mmu_ops.flush_tlb_others, &cpumask, mm, va);
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200876}
877
Jeremy Fitzhardingefdb4c332007-07-17 18:37:03 -0700878static inline void paravirt_alloc_pt(struct mm_struct *mm, unsigned pfn)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200879{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700880 PVOP_VCALL2(pv_mmu_ops.alloc_pt, mm, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200881}
882static inline void paravirt_release_pt(unsigned pfn)
883{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700884 PVOP_VCALL1(pv_mmu_ops.release_pt, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200885}
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100886
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200887static inline void paravirt_alloc_pd(unsigned pfn)
888{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700889 PVOP_VCALL1(pv_mmu_ops.alloc_pd, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200890}
891
892static inline void paravirt_alloc_pd_clone(unsigned pfn, unsigned clonepfn,
893 unsigned start, unsigned count)
894{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700895 PVOP_VCALL4(pv_mmu_ops.alloc_pd_clone, pfn, clonepfn, start, count);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200896}
897static inline void paravirt_release_pd(unsigned pfn)
898{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700899 PVOP_VCALL1(pv_mmu_ops.release_pd, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200900}
901
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200902#ifdef CONFIG_HIGHPTE
903static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
904{
905 unsigned long ret;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700906 ret = PVOP_CALL2(unsigned long, pv_mmu_ops.kmap_atomic_pte, page, type);
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +0200907 return (void *)ret;
908}
909#endif
910
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200911static inline void pte_update(struct mm_struct *mm, unsigned long addr,
912 pte_t *ptep)
913{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700914 PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200915}
916
917static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
918 pte_t *ptep)
919{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700920 PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200921}
922
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +0100923static inline pte_t __pte(pteval_t val)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200924{
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +0100925 pteval_t ret;
926
927 if (sizeof(pteval_t) > sizeof(long))
928 ret = PVOP_CALL2(pteval_t,
929 pv_mmu_ops.make_pte,
930 val, (u64)val >> 32);
931 else
932 ret = PVOP_CALL1(pteval_t,
933 pv_mmu_ops.make_pte,
934 val);
935
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +0100936 return (pte_t) { .pte = ret };
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200937}
938
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +0100939static inline pteval_t pte_val(pte_t pte)
940{
941 pteval_t ret;
942
943 if (sizeof(pteval_t) > sizeof(long))
944 ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_val,
945 pte.pte, (u64)pte.pte >> 32);
946 else
947 ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_val,
948 pte.pte);
949
950 return ret;
951}
952
Jeremy Fitzhardingeef385032008-01-30 13:33:15 +0100953static inline pgd_t __pgd(pgdval_t val)
954{
955 pgdval_t ret;
956
957 if (sizeof(pgdval_t) > sizeof(long))
958 ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.make_pgd,
959 val, (u64)val >> 32);
960 else
961 ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.make_pgd,
962 val);
963
964 return (pgd_t) { ret };
965}
966
967static inline pgdval_t pgd_val(pgd_t pgd)
968{
969 pgdval_t ret;
970
971 if (sizeof(pgdval_t) > sizeof(long))
972 ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.pgd_val,
973 pgd.pgd, (u64)pgd.pgd >> 32);
974 else
975 ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.pgd_val,
976 pgd.pgd);
977
978 return ret;
979}
980
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +0100981#ifdef CONFIG_X86_PAE
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200982static inline pmd_t __pmd(unsigned long long val)
983{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700984 return (pmd_t) { PVOP_CALL2(unsigned long long, pv_mmu_ops.make_pmd,
985 val, val >> 32) };
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200986}
987
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200988static inline unsigned long long pmd_val(pmd_t x)
989{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700990 return PVOP_CALL2(unsigned long long, pv_mmu_ops.pmd_val,
991 x.pmd, x.pmd >> 32);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200992}
993
Rusty Russellda181a82006-12-07 02:14:08 +0100994static inline void set_pte(pte_t *ptep, pte_t pteval)
995{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700996 PVOP_VCALL3(pv_mmu_ops.set_pte, ptep, pteval.pte_low, pteval.pte_high);
Rusty Russellda181a82006-12-07 02:14:08 +0100997}
998
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200999static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1000 pte_t *ptep, pte_t pteval)
Rusty Russellda181a82006-12-07 02:14:08 +01001001{
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001002 /* 5 arg words */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001003 pv_mmu_ops.set_pte_at(mm, addr, ptep, pteval);
Rusty Russellda181a82006-12-07 02:14:08 +01001004}
1005
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001006static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
1007{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001008 PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
1009 pteval.pte_low, pteval.pte_high);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001010}
1011
1012static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
1013 pte_t *ptep, pte_t pte)
1014{
1015 /* 5 arg words */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001016 pv_mmu_ops.set_pte_present(mm, addr, ptep, pte);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001017}
1018
Rusty Russellda181a82006-12-07 02:14:08 +01001019static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
1020{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001021 PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp,
1022 pmdval.pmd, pmdval.pmd >> 32);
Rusty Russellda181a82006-12-07 02:14:08 +01001023}
1024
1025static inline void set_pud(pud_t *pudp, pud_t pudval)
1026{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001027 PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
1028 pudval.pgd.pgd, pudval.pgd.pgd >> 32);
Rusty Russellda181a82006-12-07 02:14:08 +01001029}
1030
1031static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
1032{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001033 PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
Rusty Russellda181a82006-12-07 02:14:08 +01001034}
1035
1036static inline void pmd_clear(pmd_t *pmdp)
1037{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001038 PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
Rusty Russellda181a82006-12-07 02:14:08 +01001039}
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001040
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001041#else /* !CONFIG_X86_PAE */
Jeremy Fitzhardinge4cdd9c82007-05-02 19:27:15 +02001042
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001043static inline void set_pte(pte_t *ptep, pte_t pteval)
1044{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001045 PVOP_VCALL2(pv_mmu_ops.set_pte, ptep, pteval.pte_low);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001046}
1047
1048static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1049 pte_t *ptep, pte_t pteval)
1050{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001051 PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pteval.pte_low);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001052}
1053
1054static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
1055{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001056 PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, pmdval.pud.pgd.pgd);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001057}
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +01001058
1059static inline void pmd_clear(pmd_t *pmdp)
1060{
1061 set_pmd(pmdp, __pmd(0));
1062}
1063
1064static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
1065{
1066 set_pte_at(mm, addr, ptep, __pte(0));
1067}
1068
1069static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
1070{
1071 set_pte(ptep, pte);
1072}
1073
1074static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
1075 pte_t *ptep, pte_t pte)
1076{
1077 set_pte(ptep, pte);
1078}
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001079#endif /* CONFIG_X86_PAE */
Rusty Russellda181a82006-12-07 02:14:08 +01001080
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001081/* Lazy mode for batching updates / context switch */
1082enum paravirt_lazy_mode {
1083 PARAVIRT_LAZY_NONE,
1084 PARAVIRT_LAZY_MMU,
1085 PARAVIRT_LAZY_CPU,
1086};
1087
1088enum paravirt_lazy_mode paravirt_get_lazy_mode(void);
1089void paravirt_enter_lazy_cpu(void);
1090void paravirt_leave_lazy_cpu(void);
1091void paravirt_enter_lazy_mmu(void);
1092void paravirt_leave_lazy_mmu(void);
1093void paravirt_leave_lazy(enum paravirt_lazy_mode mode);
1094
Zachary Amsden9226d122007-02-13 13:26:21 +01001095#define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001096static inline void arch_enter_lazy_cpu_mode(void)
1097{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001098 PVOP_VCALL0(pv_cpu_ops.lazy_mode.enter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001099}
1100
1101static inline void arch_leave_lazy_cpu_mode(void)
1102{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001103 PVOP_VCALL0(pv_cpu_ops.lazy_mode.leave);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001104}
1105
1106static inline void arch_flush_lazy_cpu_mode(void)
1107{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001108 if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)) {
1109 arch_leave_lazy_cpu_mode();
1110 arch_enter_lazy_cpu_mode();
1111 }
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001112}
1113
Zachary Amsden9226d122007-02-13 13:26:21 +01001114
1115#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001116static inline void arch_enter_lazy_mmu_mode(void)
1117{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001118 PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001119}
1120
1121static inline void arch_leave_lazy_mmu_mode(void)
1122{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001123 PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001124}
1125
1126static inline void arch_flush_lazy_mmu_mode(void)
1127{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001128 if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU)) {
1129 arch_leave_lazy_mmu_mode();
1130 arch_enter_lazy_mmu_mode();
1131 }
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001132}
Zachary Amsden9226d122007-02-13 13:26:21 +01001133
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +02001134void _paravirt_nop(void);
1135#define paravirt_nop ((void *)_paravirt_nop)
1136
Rusty Russell139ec7c2006-12-07 02:14:08 +01001137/* These all sit in the .parainstructions section to tell us what to patch. */
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +02001138struct paravirt_patch_site {
Rusty Russell139ec7c2006-12-07 02:14:08 +01001139 u8 *instr; /* original instructions */
1140 u8 instrtype; /* type of this instruction */
1141 u8 len; /* length of original instruction */
1142 u16 clobbers; /* what registers you may clobber */
1143};
1144
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +02001145extern struct paravirt_patch_site __parainstructions[],
1146 __parainstructions_end[];
1147
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001148#ifdef CONFIG_X86_32
1149#define PV_SAVE_REGS "pushl %%ecx; pushl %%edx;"
1150#define PV_RESTORE_REGS "popl %%edx; popl %%ecx"
1151#define PV_FLAGS_ARG "0"
1152#define PV_EXTRA_CLOBBERS
1153#define PV_VEXTRA_CLOBBERS
1154#else
1155/* We save some registers, but all of them, that's too much. We clobber all
1156 * caller saved registers but the argument parameter */
1157#define PV_SAVE_REGS "pushq %%rdi;"
1158#define PV_RESTORE_REGS "popq %%rdi;"
1159#define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx"
1160#define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx"
1161#define PV_FLAGS_ARG "D"
1162#endif
1163
Rusty Russell139ec7c2006-12-07 02:14:08 +01001164static inline unsigned long __raw_local_save_flags(void)
1165{
1166 unsigned long f;
1167
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001168 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001169 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001170 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001171 : "=a"(f)
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001172 : paravirt_type(pv_irq_ops.save_fl),
Jeremy Fitzhardinge42c24fa2007-05-02 19:27:14 +02001173 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001174 : "memory", "cc" PV_VEXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001175 return f;
1176}
1177
1178static inline void raw_local_irq_restore(unsigned long f)
1179{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001180 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001181 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001182 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001183 : "=a"(f)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001184 : PV_FLAGS_ARG(f),
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001185 paravirt_type(pv_irq_ops.restore_fl),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001186 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001187 : "memory", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001188}
1189
1190static inline void raw_local_irq_disable(void)
1191{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001192 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001193 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001194 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001195 :
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001196 : paravirt_type(pv_irq_ops.irq_disable),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001197 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001198 : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001199}
1200
1201static inline void raw_local_irq_enable(void)
1202{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001203 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001204 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001205 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001206 :
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001207 : paravirt_type(pv_irq_ops.irq_enable),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001208 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001209 : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001210}
1211
1212static inline unsigned long __raw_local_irq_save(void)
1213{
1214 unsigned long f;
1215
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001216 f = __raw_local_save_flags();
1217 raw_local_irq_disable();
Rusty Russell139ec7c2006-12-07 02:14:08 +01001218 return f;
1219}
1220
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +02001221/* Make sure as little as possible of this mess escapes. */
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001222#undef PARAVIRT_CALL
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +02001223#undef __PVOP_CALL
1224#undef __PVOP_VCALL
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001225#undef PVOP_VCALL0
1226#undef PVOP_CALL0
1227#undef PVOP_VCALL1
1228#undef PVOP_CALL1
1229#undef PVOP_VCALL2
1230#undef PVOP_CALL2
1231#undef PVOP_VCALL3
1232#undef PVOP_CALL3
1233#undef PVOP_VCALL4
1234#undef PVOP_CALL4
Rusty Russell139ec7c2006-12-07 02:14:08 +01001235
Rusty Russelld3561b72006-12-07 02:14:07 +01001236#else /* __ASSEMBLY__ */
1237
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001238#define _PVSITE(ptype, clobbers, ops, word, algn) \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001239771:; \
1240 ops; \
1241772:; \
1242 .pushsection .parainstructions,"a"; \
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001243 .align algn; \
1244 word 771b; \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001245 .byte ptype; \
1246 .byte 772b-771b; \
1247 .short clobbers; \
1248 .popsection
1249
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001250
1251#ifdef CONFIG_X86_64
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001252#define PV_SAVE_REGS pushq %rax; pushq %rdi; pushq %rcx; pushq %rdx
1253#define PV_RESTORE_REGS popq %rdx; popq %rcx; popq %rdi; popq %rax
1254#define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001255#define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
1256#else
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001257#define PV_SAVE_REGS pushl %eax; pushl %edi; pushl %ecx; pushl %edx
1258#define PV_RESTORE_REGS popl %edx; popl %ecx; popl %edi; popl %eax
1259#define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001260#define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
1261#endif
1262
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001263#define INTERRUPT_RETURN \
1264 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
1265 jmp *%cs:pv_cpu_ops+PV_CPU_iret)
Rusty Russell139ec7c2006-12-07 02:14:08 +01001266
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001267#define DISABLE_INTERRUPTS(clobbers) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001268 PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001269 PV_SAVE_REGS; \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001270 call *%cs:pv_irq_ops+PV_IRQ_irq_disable; \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001271 PV_RESTORE_REGS;) \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001272
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001273#define ENABLE_INTERRUPTS(clobbers) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001274 PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001275 PV_SAVE_REGS; \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001276 call *%cs:pv_irq_ops+PV_IRQ_irq_enable; \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001277 PV_RESTORE_REGS;)
Rusty Russell139ec7c2006-12-07 02:14:08 +01001278
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +01001279#define ENABLE_INTERRUPTS_SYSCALL_RET \
1280 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_syscall_ret),\
1281 CLBR_NONE, \
1282 jmp *%cs:pv_cpu_ops+PV_CPU_irq_enable_syscall_ret)
Rusty Russell139ec7c2006-12-07 02:14:08 +01001283
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001284
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001285#ifdef CONFIG_X86_32
Rusty Russell139ec7c2006-12-07 02:14:08 +01001286#define GET_CR0_INTO_EAX \
Jeremy Fitzhardinge42c24fa2007-05-02 19:27:14 +02001287 push %ecx; push %edx; \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001288 call *pv_cpu_ops+PV_CPU_read_cr0; \
Jeremy Fitzhardinge42c24fa2007-05-02 19:27:14 +02001289 pop %edx; pop %ecx
Glauber de Oliveira Costa4a8c4c42008-01-30 13:32:07 +01001290#else
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +01001291#define SWAPGS \
1292 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
1293 PV_SAVE_REGS; \
1294 call *pv_cpu_ops+PV_CPU_swapgs; \
1295 PV_RESTORE_REGS \
1296 )
1297
Glauber de Oliveira Costa4a8c4c42008-01-30 13:32:07 +01001298#define GET_CR2_INTO_RCX \
1299 call *pv_mmu_ops+PV_MMU_read_cr2; \
1300 movq %rax, %rcx; \
1301 xorq %rax, %rax;
1302
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001303#endif
Rusty Russell139ec7c2006-12-07 02:14:08 +01001304
Rusty Russelld3561b72006-12-07 02:14:07 +01001305#endif /* __ASSEMBLY__ */
1306#endif /* CONFIG_PARAVIRT */
1307#endif /* __ASM_PARAVIRT_H */