blob: 33f72f8fe7573600602127fff473a1445e91c948 [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
Glauber de Oliveira Costa4c9890c2008-01-30 13:33:19 +0100104#ifdef CONFIG_X86_64
105 unsigned long (*read_cr8)(void);
106 void (*write_cr8)(unsigned long);
107#endif
108
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200109 /* Segment descriptor handling */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100110 void (*load_tr_desc)(void);
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100111 void (*load_gdt)(const struct desc_ptr *);
112 void (*load_idt)(const struct desc_ptr *);
113 void (*store_gdt)(struct desc_ptr *);
114 void (*store_idt)(struct desc_ptr *);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100115 void (*set_ldt)(const void *desc, unsigned entries);
116 unsigned long (*store_tr)(void);
117 void (*load_tls)(struct thread_struct *t, unsigned int cpu);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100118 void (*write_ldt_entry)(struct desc_struct *ldt, int entrynum,
119 const void *desc);
Rusty Russell90a0a062007-05-02 19:27:10 +0200120 void (*write_gdt_entry)(struct desc_struct *,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100121 int entrynum, const void *desc, int size);
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100122 void (*write_idt_entry)(gate_desc *,
123 int entrynum, const gate_desc *gate);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100124 void (*load_sp0)(struct tss_struct *tss, struct thread_struct *t);
Rusty Russelld3561b72006-12-07 02:14:07 +0100125
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100126 void (*set_iopl_mask)(unsigned mask);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700127
128 void (*wbinvd)(void);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100129 void (*io_delay)(void);
Rusty Russelld3561b72006-12-07 02:14:07 +0100130
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700131 /* cpuid emulation, mostly so that caps bits can be disabled */
132 void (*cpuid)(unsigned int *eax, unsigned int *ebx,
133 unsigned int *ecx, unsigned int *edx);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200134
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700135 /* MSR, PMC and TSR operations.
136 err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
137 u64 (*read_msr)(unsigned int msr, int *err);
Glauber de Oliveira Costac9dcda52008-01-30 13:31:07 +0100138 int (*write_msr)(unsigned int msr, unsigned low, unsigned high);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700139
140 u64 (*read_tsc)(void);
Glauber de Oliveira Costab8d1fae2008-01-30 13:31:07 +0100141 u64 (*read_pmc)(int counter);
Glauber de Oliveira Costae5aaac42008-01-30 13:32:05 +0100142 unsigned long long (*read_tscp)(unsigned int *aux);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700143
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400144 /*
145 * Atomically enable interrupts and return to userspace. This
146 * is only ever used to return to 32-bit processes; in a
147 * 64-bit kernel, it's used for 32-on-64 compat processes, but
148 * never native 64-bit processes. (Jump, not call.)
149 */
Jeremy Fitzhardinged75cd22f2008-06-25 00:19:26 -0400150 void (*irq_enable_sysexit)(void);
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -0400151
152 /*
153 * Switch to usermode gs and return to 64-bit usermode using
154 * sysret. Only used in 64-bit kernels to return to 64-bit
155 * processes. Usermode register state, including %rsp, must
156 * already be restored.
157 */
158 void (*usergs_sysret64)(void);
159
160 /*
161 * Switch to usermode gs and return to 32-bit usermode using
162 * sysret. Used to return to 32-on-64 compat processes.
163 * Other usermode register state, including %esp, must already
164 * be restored.
165 */
166 void (*usergs_sysret32)(void);
167
168 /* Normal iret. Jump to this with the standard iret stack
169 frame set up. */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700170 void (*iret)(void);
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700171
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +0100172 void (*swapgs)(void);
173
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700174 struct pv_lazy_ops lazy_mode;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700175};
176
177struct pv_irq_ops {
178 void (*init_IRQ)(void);
179
180 /*
181 * Get/set interrupt state. save_fl and restore_fl are only
182 * expected to use X86_EFLAGS_IF; all other bits
183 * returned from save_fl are undefined, and may be ignored by
184 * restore_fl.
185 */
186 unsigned long (*save_fl)(void);
187 void (*restore_fl)(unsigned long);
188 void (*irq_disable)(void);
189 void (*irq_enable)(void);
190 void (*safe_halt)(void);
191 void (*halt)(void);
192};
193
194struct pv_apic_ops {
Rusty Russell13623d72006-12-07 02:14:08 +0100195#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200196 /*
197 * Direct APIC operations, principally for VMI. Ideally
198 * these shouldn't be in this interface.
199 */
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100200 void (*apic_write)(unsigned long reg, u32 v);
201 void (*apic_write_atomic)(unsigned long reg, u32 v);
202 u32 (*apic_read)(unsigned long reg);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100203 void (*setup_boot_clock)(void);
204 void (*setup_secondary_clock)(void);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200205
206 void (*startup_ipi_hook)(int phys_apicid,
207 unsigned long start_eip,
208 unsigned long start_esp);
Rusty Russell13623d72006-12-07 02:14:08 +0100209#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700210};
211
212struct pv_mmu_ops {
213 /*
214 * Called before/after init_mm pagetable setup. setup_start
215 * may reset %cr3, and may pre-install parts of the pagetable;
216 * pagetable setup is expected to preserve any existing
217 * mapping.
218 */
219 void (*pagetable_setup_start)(pgd_t *pgd_base);
220 void (*pagetable_setup_done)(pgd_t *pgd_base);
221
222 unsigned long (*read_cr2)(void);
223 void (*write_cr2)(unsigned long);
224
225 unsigned long (*read_cr3)(void);
226 void (*write_cr3)(unsigned long);
227
228 /*
229 * Hooks for intercepting the creation/use/destruction of an
230 * mm_struct.
231 */
232 void (*activate_mm)(struct mm_struct *prev,
233 struct mm_struct *next);
234 void (*dup_mmap)(struct mm_struct *oldmm,
235 struct mm_struct *mm);
236 void (*exit_mmap)(struct mm_struct *mm);
237
Rusty Russell13623d72006-12-07 02:14:08 +0100238
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200239 /* TLB operations */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100240 void (*flush_tlb_user)(void);
241 void (*flush_tlb_kernel)(void);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200242 void (*flush_tlb_single)(unsigned long addr);
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200243 void (*flush_tlb_others)(const cpumask_t *cpus, struct mm_struct *mm,
244 unsigned long va);
Rusty Russellda181a82006-12-07 02:14:08 +0100245
Jeremy Fitzhardingeeba00452008-06-25 00:19:12 -0400246 /* Hooks for allocating and freeing a pagetable top-level */
247 int (*pgd_alloc)(struct mm_struct *mm);
248 void (*pgd_free)(struct mm_struct *mm, pgd_t *pgd);
249
250 /*
251 * Hooks for allocating/releasing pagetable pages when they're
252 * attached to a pagetable
253 */
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700254 void (*alloc_pte)(struct mm_struct *mm, u32 pfn);
255 void (*alloc_pmd)(struct mm_struct *mm, u32 pfn);
256 void (*alloc_pmd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
Jeremy Fitzhardinge2761fa02008-03-17 16:37:02 -0700257 void (*alloc_pud)(struct mm_struct *mm, u32 pfn);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700258 void (*release_pte)(u32 pfn);
259 void (*release_pmd)(u32 pfn);
Jeremy Fitzhardinge2761fa02008-03-17 16:37:02 -0700260 void (*release_pud)(u32 pfn);
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100261
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200262 /* Pagetable manipulation functions */
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100263 void (*set_pte)(pte_t *ptep, pte_t pteval);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200264 void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
265 pte_t *ptep, pte_t pteval);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100266 void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
Joe Perches49cd7402008-03-23 01:03:00 -0700267 void (*pte_update)(struct mm_struct *mm, unsigned long addr,
268 pte_t *ptep);
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200269 void (*pte_update_defer)(struct mm_struct *mm,
270 unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200271
Jeremy Fitzhardinge08b882c2008-06-16 04:30:01 -0700272 pte_t (*ptep_modify_prot_start)(struct mm_struct *mm, unsigned long addr,
273 pte_t *ptep);
274 void (*ptep_modify_prot_commit)(struct mm_struct *mm, unsigned long addr,
275 pte_t *ptep, pte_t pte);
276
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100277 pteval_t (*pte_val)(pte_t);
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100278 pteval_t (*pte_flags)(pte_t);
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100279 pte_t (*make_pte)(pteval_t pte);
280
281 pgdval_t (*pgd_val)(pgd_t);
282 pgd_t (*make_pgd)(pgdval_t pgd);
283
284#if PAGETABLE_LEVELS >= 3
Rusty Russellda181a82006-12-07 02:14:08 +0100285#ifdef CONFIG_X86_PAE
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100286 void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700287 void (*set_pte_present)(struct mm_struct *mm, unsigned long addr,
288 pte_t *ptep, pte_t pte);
Joe Perches49cd7402008-03-23 01:03:00 -0700289 void (*pte_clear)(struct mm_struct *mm, unsigned long addr,
290 pte_t *ptep);
Andi Kleen1a1eecd2007-02-13 13:26:25 +0100291 void (*pmd_clear)(pmd_t *pmdp);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200292
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100293#endif /* CONFIG_X86_PAE */
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200294
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100295 void (*set_pud)(pud_t *pudp, pud_t pudval);
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200296
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100297 pmdval_t (*pmd_val)(pmd_t);
298 pmd_t (*make_pmd)(pmdval_t pmd);
299
300#if PAGETABLE_LEVELS == 4
301 pudval_t (*pud_val)(pud_t);
302 pud_t (*make_pud)(pudval_t pud);
Eduardo Habkost90422192008-01-30 13:33:20 +0100303
304 void (*set_pgd)(pgd_t *pudp, pgd_t pgdval);
Jeremy Fitzhardinge5b8dd1e2008-01-30 13:33:15 +0100305#endif /* PAGETABLE_LEVELS == 4 */
306#endif /* PAGETABLE_LEVELS >= 3 */
Rusty Russellda181a82006-12-07 02:14:08 +0100307
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700308#ifdef CONFIG_HIGHPTE
309 void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
310#endif
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -0700311
312 struct pv_lazy_ops lazy_mode;
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700313
314 /* dom0 ops */
315
316 /* Sometimes the physical address is a pfn, and sometimes its
317 an mfn. We can tell which is which from the index. */
318 void (*set_fixmap)(unsigned /* enum fixed_addresses */ idx,
319 unsigned long phys, pgprot_t flags);
Rusty Russelld3561b72006-12-07 02:14:07 +0100320};
321
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700322/* This contains all the paravirt structures: we get a convenient
323 * number for each function using the offset which we use to indicate
324 * what to patch. */
Joe Perches49cd7402008-03-23 01:03:00 -0700325struct paravirt_patch_template {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700326 struct pv_init_ops pv_init_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700327 struct pv_time_ops pv_time_ops;
328 struct pv_cpu_ops pv_cpu_ops;
329 struct pv_irq_ops pv_irq_ops;
330 struct pv_apic_ops pv_apic_ops;
331 struct pv_mmu_ops pv_mmu_ops;
332};
333
334extern struct pv_info pv_info;
335extern struct pv_init_ops pv_init_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700336extern struct pv_time_ops pv_time_ops;
337extern struct pv_cpu_ops pv_cpu_ops;
338extern struct pv_irq_ops pv_irq_ops;
339extern struct pv_apic_ops pv_apic_ops;
340extern struct pv_mmu_ops pv_mmu_ops;
Rusty Russelld3561b72006-12-07 02:14:07 +0100341
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200342#define PARAVIRT_PATCH(x) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700343 (offsetof(struct paravirt_patch_template, x) / sizeof(void *))
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200344
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700345#define paravirt_type(op) \
346 [paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \
347 [paravirt_opptr] "m" (op)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200348#define paravirt_clobber(clobber) \
349 [paravirt_clobber] "i" (clobber)
350
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200351/*
352 * Generate some code, and mark it as patchable by the
353 * apply_paravirt() alternate instruction patcher.
354 */
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200355#define _paravirt_alt(insn_string, type, clobber) \
356 "771:\n\t" insn_string "\n" "772:\n" \
357 ".pushsection .parainstructions,\"a\"\n" \
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +0100358 _ASM_ALIGN "\n" \
359 _ASM_PTR " 771b\n" \
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200360 " .byte " type "\n" \
361 " .byte 772b-771b\n" \
362 " .short " clobber "\n" \
363 ".popsection\n"
364
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200365/* Generate patchable code, with the default asm parameters. */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200366#define paravirt_alt(insn_string) \
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +0200367 _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
368
Glauber de Oliveira Costa2f485ef2008-01-30 13:32:10 +0100369/* Simple instruction patching code. */
370#define DEF_NATIVE(ops, name, code) \
371 extern const char start_##ops##_##name[], end_##ops##_##name[]; \
372 asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
373
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200374unsigned paravirt_patch_nop(void);
375unsigned paravirt_patch_ignore(unsigned len);
Andi Kleenab144f52007-08-10 22:31:03 +0200376unsigned paravirt_patch_call(void *insnbuf,
377 const void *target, u16 tgt_clobbers,
378 unsigned long addr, u16 site_clobbers,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200379 unsigned len);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700380unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
Andi Kleenab144f52007-08-10 22:31:03 +0200381 unsigned long addr, unsigned len);
382unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
383 unsigned long addr, unsigned len);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200384
Andi Kleenab144f52007-08-10 22:31:03 +0200385unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200386 const char *start, const char *end);
387
Glauber de Oliveira Costa2f485ef2008-01-30 13:32:10 +0100388unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
389 unsigned long addr, unsigned len);
390
Jeremy Fitzhardinged5729292007-07-17 18:37:04 -0700391int paravirt_disable_iospace(void);
Jeremy Fitzhardinge63f70272007-05-02 19:27:14 +0200392
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200393/*
394 * This generates an indirect call based on the operation type number.
395 * The type number, computed in PARAVIRT_PATCH, is derived from the
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700396 * offset into the paravirt_patch_template structure, and can therefore be
397 * freely converted back into a structure offset.
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200398 */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700399#define PARAVIRT_CALL "call *%[paravirt_opptr];"
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200400
401/*
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700402 * These macros are intended to wrap calls through one of the paravirt
403 * ops structs, so that they can be later identified and patched at
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200404 * runtime.
405 *
406 * Normally, a call to a pv_op function is a simple indirect call:
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100407 * (pv_op_struct.operations)(args...).
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200408 *
409 * Unfortunately, this is a relatively slow operation for modern CPUs,
410 * because it cannot necessarily determine what the destination
411 * address is. In this case, the address is a runtime constant, so at
412 * the very least we can patch the call to e a simple direct call, or
413 * ideally, patch an inline implementation into the callsite. (Direct
414 * calls are essentially free, because the call and return addresses
415 * are completely predictable.)
416 *
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100417 * For i386, these macros rely on the standard gcc "regparm(3)" calling
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200418 * convention, in which the first three arguments are placed in %eax,
419 * %edx, %ecx (in that order), and the remaining arguments are placed
420 * on the stack. All caller-save registers (eax,edx,ecx) are expected
421 * to be modified (either clobbered or used for return values).
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100422 * X86_64, on the other hand, already specifies a register-based calling
423 * conventions, returning at %rax, with parameteres going on %rdi, %rsi,
424 * %rdx, and %rcx. Note that for this reason, x86_64 does not need any
425 * special handling for dealing with 4 arguments, unlike i386.
426 * However, x86_64 also have to clobber all caller saved registers, which
427 * unfortunately, are quite a bit (r8 - r11)
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200428 *
429 * The call instruction itself is marked by placing its start address
430 * and size into the .parainstructions section, so that
431 * apply_paravirt() in arch/i386/kernel/alternative.c can do the
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700432 * appropriate patching under the control of the backend pv_init_ops
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200433 * implementation.
434 *
435 * Unfortunately there's no way to get gcc to generate the args setup
436 * for the call, and then allow the call itself to be generated by an
437 * inline asm. Because of this, we must do the complete arg setup and
438 * return value handling from within these macros. This is fairly
439 * cumbersome.
440 *
441 * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
442 * It could be extended to more arguments, but there would be little
443 * to be gained from that. For each number of arguments, there are
444 * the two VCALL and CALL variants for void and non-void functions.
445 *
446 * When there is a return value, the invoker of the macro must specify
447 * the return type. The macro then uses sizeof() on that type to
448 * determine whether its a 32 or 64 bit value, and places the return
449 * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100450 * 64-bit). For x86_64 machines, it just returns at %rax regardless of
451 * the return value size.
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200452 *
453 * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100454 * i386 also passes 64-bit arguments as a pair of adjacent 32-bit arguments
455 * in low,high order
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +0200456 *
457 * Small structures are passed and returned in registers. The macro
458 * calling convention can't directly deal with this, so the wrapper
459 * functions must do this.
460 *
461 * These PVOP_* macros are only defined within this header. This
462 * means that all uses must be wrapped in inline functions. This also
463 * makes sure the incoming and outgoing types are always correct.
464 */
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100465#ifdef CONFIG_X86_32
466#define PVOP_VCALL_ARGS unsigned long __eax, __edx, __ecx
467#define PVOP_CALL_ARGS PVOP_VCALL_ARGS
468#define PVOP_VCALL_CLOBBERS "=a" (__eax), "=d" (__edx), \
469 "=c" (__ecx)
470#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS
471#define EXTRA_CLOBBERS
472#define VEXTRA_CLOBBERS
473#else
474#define PVOP_VCALL_ARGS unsigned long __edi, __esi, __edx, __ecx
475#define PVOP_CALL_ARGS PVOP_VCALL_ARGS, __eax
476#define PVOP_VCALL_CLOBBERS "=D" (__edi), \
477 "=S" (__esi), "=d" (__edx), \
478 "=c" (__ecx)
479
480#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS, "=a" (__eax)
481
482#define EXTRA_CLOBBERS , "r8", "r9", "r10", "r11"
483#define VEXTRA_CLOBBERS , "rax", "r8", "r9", "r10", "r11"
484#endif
485
Jeremy Fitzhardinge97349132008-06-25 00:19:14 -0400486#ifdef CONFIG_PARAVIRT_DEBUG
487#define PVOP_TEST_NULL(op) BUG_ON(op == NULL)
488#else
489#define PVOP_TEST_NULL(op) ((void)op)
490#endif
491
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200492#define __PVOP_CALL(rettype, op, pre, post, ...) \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200493 ({ \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200494 rettype __ret; \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100495 PVOP_CALL_ARGS; \
Jeremy Fitzhardinge97349132008-06-25 00:19:14 -0400496 PVOP_TEST_NULL(op); \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100497 /* This is 32-bit specific, but is okay in 64-bit */ \
498 /* since this condition will never hold */ \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200499 if (sizeof(rettype) > sizeof(unsigned long)) { \
500 asm volatile(pre \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200501 paravirt_alt(PARAVIRT_CALL) \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200502 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100503 : PVOP_CALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200504 : paravirt_type(op), \
505 paravirt_clobber(CLBR_ANY), \
506 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100507 : "memory", "cc" EXTRA_CLOBBERS); \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200508 __ret = (rettype)((((u64)__edx) << 32) | __eax); \
509 } else { \
510 asm volatile(pre \
511 paravirt_alt(PARAVIRT_CALL) \
512 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100513 : PVOP_CALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200514 : paravirt_type(op), \
515 paravirt_clobber(CLBR_ANY), \
516 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100517 : "memory", "cc" EXTRA_CLOBBERS); \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200518 __ret = (rettype)__eax; \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200519 } \
520 __ret; \
521 })
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200522#define __PVOP_VCALL(op, pre, post, ...) \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200523 ({ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100524 PVOP_VCALL_ARGS; \
Jeremy Fitzhardinge97349132008-06-25 00:19:14 -0400525 PVOP_TEST_NULL(op); \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200526 asm volatile(pre \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200527 paravirt_alt(PARAVIRT_CALL) \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200528 post \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100529 : PVOP_VCALL_CLOBBERS \
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200530 : paravirt_type(op), \
531 paravirt_clobber(CLBR_ANY), \
532 ##__VA_ARGS__ \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100533 : "memory", "cc" VEXTRA_CLOBBERS); \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200534 })
535
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200536#define PVOP_CALL0(rettype, op) \
537 __PVOP_CALL(rettype, op, "", "")
538#define PVOP_VCALL0(op) \
539 __PVOP_VCALL(op, "", "")
540
541#define PVOP_CALL1(rettype, op, arg1) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100542 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200543#define PVOP_VCALL1(op, arg1) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100544 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200545
546#define PVOP_CALL2(rettype, op, arg1, arg2) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100547 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
548 "1" ((unsigned long)(arg2)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200549#define PVOP_VCALL2(op, arg1, arg2) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100550 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
551 "1" ((unsigned long)(arg2)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200552
553#define PVOP_CALL3(rettype, op, arg1, arg2, arg3) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100554 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
555 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200556#define PVOP_VCALL3(op, arg1, arg2, arg3) \
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100557 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
558 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200559
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100560/* This is the only difference in x86_64. We can make it much simpler */
561#ifdef CONFIG_X86_32
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200562#define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
563 __PVOP_CALL(rettype, op, \
564 "push %[_arg4];", "lea 4(%%esp),%%esp;", \
565 "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
566 "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
567#define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
568 __PVOP_VCALL(op, \
569 "push %[_arg4];", "lea 4(%%esp),%%esp;", \
570 "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
571 "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
Glauber de Oliveira Costaa4746362008-01-30 13:32:05 +0100572#else
573#define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
574 __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
575 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
576 "3"((unsigned long)(arg4)))
577#define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
578 __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
579 "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
580 "3"((unsigned long)(arg4)))
581#endif
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +0200582
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200583static inline int paravirt_enabled(void)
584{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700585 return pv_info.paravirt_enabled;
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200586}
Rusty Russelld3561b72006-12-07 02:14:07 +0100587
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100588static inline void load_sp0(struct tss_struct *tss,
Rusty Russelld3561b72006-12-07 02:14:07 +0100589 struct thread_struct *thread)
590{
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100591 PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
Rusty Russelld3561b72006-12-07 02:14:07 +0100592}
593
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700594#define ARCH_SETUP pv_init_ops.arch_setup();
Rusty Russelld3561b72006-12-07 02:14:07 +0100595static inline unsigned long get_wallclock(void)
596{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700597 return PVOP_CALL0(unsigned long, pv_time_ops.get_wallclock);
Rusty Russelld3561b72006-12-07 02:14:07 +0100598}
599
600static inline int set_wallclock(unsigned long nowtime)
601{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700602 return PVOP_CALL1(int, pv_time_ops.set_wallclock, nowtime);
Rusty Russelld3561b72006-12-07 02:14:07 +0100603}
604
Zachary Amsdene30fab32007-03-05 00:30:39 -0800605static inline void (*choose_time_init(void))(void)
Rusty Russelld3561b72006-12-07 02:14:07 +0100606{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700607 return pv_time_ops.time_init;
Rusty Russelld3561b72006-12-07 02:14:07 +0100608}
609
610/* The paravirtualized CPUID instruction. */
611static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
612 unsigned int *ecx, unsigned int *edx)
613{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700614 PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
Rusty Russelld3561b72006-12-07 02:14:07 +0100615}
616
617/*
618 * These special macros can be used to get or set a debugging register
619 */
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200620static inline unsigned long paravirt_get_debugreg(int reg)
621{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700622 return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200623}
624#define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
625static inline void set_debugreg(unsigned long val, int reg)
626{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700627 PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200628}
Rusty Russelld3561b72006-12-07 02:14:07 +0100629
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200630static inline void clts(void)
631{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700632 PVOP_VCALL0(pv_cpu_ops.clts);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200633}
Rusty Russelld3561b72006-12-07 02:14:07 +0100634
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200635static inline unsigned long read_cr0(void)
636{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700637 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200638}
Rusty Russelld3561b72006-12-07 02:14:07 +0100639
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200640static inline void write_cr0(unsigned long x)
641{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700642 PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200643}
Rusty Russelld3561b72006-12-07 02:14:07 +0100644
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200645static inline unsigned long read_cr2(void)
646{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700647 return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200648}
Rusty Russelld3561b72006-12-07 02:14:07 +0100649
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200650static inline void write_cr2(unsigned long x)
651{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700652 PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200653}
Rusty Russelld3561b72006-12-07 02:14:07 +0100654
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200655static inline unsigned long read_cr3(void)
656{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700657 return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200658}
659
660static inline void write_cr3(unsigned long x)
661{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700662 PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200663}
664
665static inline unsigned long read_cr4(void)
666{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700667 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200668}
669static inline unsigned long read_cr4_safe(void)
670{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700671 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200672}
673
674static inline void write_cr4(unsigned long x)
675{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700676 PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200677}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200678
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100679#ifdef CONFIG_X86_64
Glauber de Oliveira Costa4c9890c2008-01-30 13:33:19 +0100680static inline unsigned long read_cr8(void)
681{
682 return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr8);
683}
684
685static inline void write_cr8(unsigned long x)
686{
687 PVOP_VCALL1(pv_cpu_ops.write_cr8, x);
688}
Glauber de Oliveira Costa94ea03c2008-01-30 13:33:19 +0100689#endif
Glauber de Oliveira Costa4c9890c2008-01-30 13:33:19 +0100690
Rusty Russelld3561b72006-12-07 02:14:07 +0100691static inline void raw_safe_halt(void)
692{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700693 PVOP_VCALL0(pv_irq_ops.safe_halt);
Rusty Russelld3561b72006-12-07 02:14:07 +0100694}
695
696static inline void halt(void)
697{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700698 PVOP_VCALL0(pv_irq_ops.safe_halt);
Rusty Russelld3561b72006-12-07 02:14:07 +0100699}
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200700
701static inline void wbinvd(void)
702{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700703 PVOP_VCALL0(pv_cpu_ops.wbinvd);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200704}
Rusty Russelld3561b72006-12-07 02:14:07 +0100705
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700706#define get_kernel_rpl() (pv_info.kernel_rpl)
Rusty Russelld3561b72006-12-07 02:14:07 +0100707
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200708static inline u64 paravirt_read_msr(unsigned msr, int *err)
709{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700710 return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200711}
712static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
713{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700714 return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200715}
716
Rusty Russell90a0a062007-05-02 19:27:10 +0200717/* These should all do BUG_ON(_err), but our headers are too tangled. */
Joe Perches49cd7402008-03-23 01:03:00 -0700718#define rdmsr(msr, val1, val2) \
719do { \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200720 int _err; \
721 u64 _l = paravirt_read_msr(msr, &_err); \
722 val1 = (u32)_l; \
723 val2 = _l >> 32; \
Joe Perches49cd7402008-03-23 01:03:00 -0700724} while (0)
Rusty Russelld3561b72006-12-07 02:14:07 +0100725
Joe Perches49cd7402008-03-23 01:03:00 -0700726#define wrmsr(msr, val1, val2) \
727do { \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200728 paravirt_write_msr(msr, val1, val2); \
Joe Perches49cd7402008-03-23 01:03:00 -0700729} while (0)
Rusty Russelld3561b72006-12-07 02:14:07 +0100730
Joe Perches49cd7402008-03-23 01:03:00 -0700731#define rdmsrl(msr, val) \
732do { \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200733 int _err; \
734 val = paravirt_read_msr(msr, &_err); \
Joe Perches49cd7402008-03-23 01:03:00 -0700735} while (0)
Rusty Russelld3561b72006-12-07 02:14:07 +0100736
Joe Perches49cd7402008-03-23 01:03:00 -0700737#define wrmsrl(msr, val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
738#define wrmsr_safe(msr, a, b) paravirt_write_msr(msr, a, b)
Rusty Russelld3561b72006-12-07 02:14:07 +0100739
740/* rdmsr with exception handling */
Joe Perches49cd7402008-03-23 01:03:00 -0700741#define rdmsr_safe(msr, a, b) \
742({ \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200743 int _err; \
744 u64 _l = paravirt_read_msr(msr, &_err); \
745 (*a) = (u32)_l; \
746 (*b) = _l >> 32; \
Joe Perches49cd7402008-03-23 01:03:00 -0700747 _err; \
748})
Rusty Russelld3561b72006-12-07 02:14:07 +0100749
Andi Kleen1de87bd2008-03-22 10:59:28 +0100750static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
751{
752 int err;
753
754 *p = paravirt_read_msr(msr, &err);
755 return err;
756}
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200757
758static inline u64 paravirt_read_tsc(void)
759{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700760 return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200761}
Rusty Russelld3561b72006-12-07 02:14:07 +0100762
Joe Perches49cd7402008-03-23 01:03:00 -0700763#define rdtscl(low) \
764do { \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200765 u64 _l = paravirt_read_tsc(); \
766 low = (int)_l; \
Joe Perches49cd7402008-03-23 01:03:00 -0700767} while (0)
Rusty Russelld3561b72006-12-07 02:14:07 +0100768
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200769#define rdtscll(val) (val = paravirt_read_tsc())
Rusty Russelld3561b72006-12-07 02:14:07 +0100770
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700771static inline unsigned long long paravirt_sched_clock(void)
772{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700773 return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700774}
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700775#define calculate_cpu_khz() (pv_time_ops.get_cpu_khz())
Zachary Amsden6cb9a832007-03-05 00:30:35 -0800776
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200777static inline unsigned long long paravirt_read_pmc(int counter)
778{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700779 return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200780}
781
Joe Perches49cd7402008-03-23 01:03:00 -0700782#define rdpmc(counter, low, high) \
783do { \
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200784 u64 _l = paravirt_read_pmc(counter); \
785 low = (u32)_l; \
786 high = _l >> 32; \
Joe Perches49cd7402008-03-23 01:03:00 -0700787} while (0)
Rusty Russelld3561b72006-12-07 02:14:07 +0100788
Glauber de Oliveira Costae5aaac42008-01-30 13:32:05 +0100789static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
790{
791 return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
792}
793
794#define rdtscp(low, high, aux) \
795do { \
796 int __aux; \
797 unsigned long __val = paravirt_rdtscp(&__aux); \
798 (low) = (u32)__val; \
799 (high) = (u32)(__val >> 32); \
800 (aux) = __aux; \
801} while (0)
802
803#define rdtscpll(val, aux) \
804do { \
805 unsigned long __aux; \
806 val = paravirt_rdtscp(&__aux); \
807 (aux) = __aux; \
808} while (0)
809
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200810static inline void load_TR_desc(void)
811{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700812 PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200813}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100814static inline void load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200815{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700816 PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200817}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100818static inline void load_idt(const struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200819{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700820 PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200821}
822static inline void set_ldt(const void *addr, unsigned entries)
823{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700824 PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200825}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100826static inline void store_gdt(struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200827{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700828 PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200829}
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100830static inline void store_idt(struct desc_ptr *dtr)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200831{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700832 PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200833}
834static inline unsigned long paravirt_store_tr(void)
835{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700836 return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200837}
838#define store_tr(tr) ((tr) = paravirt_store_tr())
839static inline void load_TLS(struct thread_struct *t, unsigned cpu)
840{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700841 PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200842}
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100843
844static inline void write_ldt_entry(struct desc_struct *dt, int entry,
845 const void *desc)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200846{
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100847 PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200848}
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100849
850static inline void write_gdt_entry(struct desc_struct *dt, int entry,
851 void *desc, int type)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200852{
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100853 PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200854}
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100855
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100856static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200857{
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100858 PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200859}
860static inline void set_iopl_mask(unsigned mask)
861{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700862 PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200863}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200864
Rusty Russelld3561b72006-12-07 02:14:07 +0100865/* The paravirtualized I/O functions */
Joe Perches49cd7402008-03-23 01:03:00 -0700866static inline void slow_down_io(void)
867{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700868 pv_cpu_ops.io_delay();
Rusty Russelld3561b72006-12-07 02:14:07 +0100869#ifdef REALLY_SLOW_IO
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700870 pv_cpu_ops.io_delay();
871 pv_cpu_ops.io_delay();
872 pv_cpu_ops.io_delay();
Rusty Russelld3561b72006-12-07 02:14:07 +0100873#endif
874}
875
Rusty Russell13623d72006-12-07 02:14:08 +0100876#ifdef CONFIG_X86_LOCAL_APIC
877/*
878 * Basic functions accessing APICs.
879 */
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100880static inline void apic_write(unsigned long reg, u32 v)
Rusty Russell13623d72006-12-07 02:14:08 +0100881{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700882 PVOP_VCALL2(pv_apic_ops.apic_write, reg, v);
Rusty Russell13623d72006-12-07 02:14:08 +0100883}
884
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100885static inline void apic_write_atomic(unsigned long reg, u32 v)
Rusty Russell13623d72006-12-07 02:14:08 +0100886{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700887 PVOP_VCALL2(pv_apic_ops.apic_write_atomic, reg, v);
Rusty Russell13623d72006-12-07 02:14:08 +0100888}
889
Thomas Gleixner42e0a9aa2008-01-30 13:30:15 +0100890static inline u32 apic_read(unsigned long reg)
Rusty Russell13623d72006-12-07 02:14:08 +0100891{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700892 return PVOP_CALL1(unsigned long, pv_apic_ops.apic_read, reg);
Rusty Russell13623d72006-12-07 02:14:08 +0100893}
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100894
895static inline void setup_boot_clock(void)
896{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700897 PVOP_VCALL0(pv_apic_ops.setup_boot_clock);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100898}
899
900static inline void setup_secondary_clock(void)
901{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700902 PVOP_VCALL0(pv_apic_ops.setup_secondary_clock);
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100903}
Rusty Russell13623d72006-12-07 02:14:08 +0100904#endif
905
Jeremy Fitzhardinge6996d3b2007-07-17 18:37:03 -0700906static inline void paravirt_post_allocator_init(void)
907{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700908 if (pv_init_ops.post_allocator_init)
909 (*pv_init_ops.post_allocator_init)();
Jeremy Fitzhardinge6996d3b2007-07-17 18:37:03 -0700910}
911
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200912static inline void paravirt_pagetable_setup_start(pgd_t *base)
913{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700914 (*pv_mmu_ops.pagetable_setup_start)(base);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200915}
916
917static inline void paravirt_pagetable_setup_done(pgd_t *base)
918{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700919 (*pv_mmu_ops.pagetable_setup_done)(base);
Jeremy Fitzhardingeb239fb22007-05-02 19:27:13 +0200920}
Jeremy Fitzhardinge3dc494e2007-05-02 19:27:13 +0200921
Zachary Amsdenae5da272007-02-13 13:26:21 +0100922#ifdef CONFIG_SMP
923static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
924 unsigned long start_esp)
925{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700926 PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
927 phys_apicid, start_eip, start_esp);
Zachary Amsdenae5da272007-02-13 13:26:21 +0100928}
929#endif
Rusty Russell13623d72006-12-07 02:14:08 +0100930
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200931static inline void paravirt_activate_mm(struct mm_struct *prev,
932 struct mm_struct *next)
933{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700934 PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200935}
936
937static inline void arch_dup_mmap(struct mm_struct *oldmm,
938 struct mm_struct *mm)
939{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700940 PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200941}
942
943static inline void arch_exit_mmap(struct mm_struct *mm)
944{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700945 PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +0200946}
947
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200948static inline void __flush_tlb(void)
949{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700950 PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200951}
952static inline void __flush_tlb_global(void)
953{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700954 PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200955}
956static inline void __flush_tlb_single(unsigned long addr)
957{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700958 PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200959}
Rusty Russellda181a82006-12-07 02:14:08 +0100960
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200961static inline void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
962 unsigned long va)
963{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700964 PVOP_VCALL3(pv_mmu_ops.flush_tlb_others, &cpumask, mm, va);
Jeremy Fitzhardinged4c10472007-05-02 19:27:15 +0200965}
966
Jeremy Fitzhardingeeba00452008-06-25 00:19:12 -0400967static inline int paravirt_pgd_alloc(struct mm_struct *mm)
968{
969 return PVOP_CALL1(int, pv_mmu_ops.pgd_alloc, mm);
970}
971
972static inline void paravirt_pgd_free(struct mm_struct *mm, pgd_t *pgd)
973{
974 PVOP_VCALL2(pv_mmu_ops.pgd_free, mm, pgd);
975}
976
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700977static inline void paravirt_alloc_pte(struct mm_struct *mm, unsigned pfn)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200978{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700979 PVOP_VCALL2(pv_mmu_ops.alloc_pte, mm, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200980}
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700981static inline void paravirt_release_pte(unsigned pfn)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200982{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700983 PVOP_VCALL1(pv_mmu_ops.release_pte, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200984}
Zachary Amsdenc119ecc2007-02-13 13:26:21 +0100985
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700986static inline void paravirt_alloc_pmd(struct mm_struct *mm, unsigned pfn)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200987{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700988 PVOP_VCALL2(pv_mmu_ops.alloc_pmd, mm, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200989}
990
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700991static inline void paravirt_alloc_pmd_clone(unsigned pfn, unsigned clonepfn,
992 unsigned start, unsigned count)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200993{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700994 PVOP_VCALL4(pv_mmu_ops.alloc_pmd_clone, pfn, clonepfn, start, count);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200995}
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700996static inline void paravirt_release_pmd(unsigned pfn)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200997{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700998 PVOP_VCALL1(pv_mmu_ops.release_pmd, pfn);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +0200999}
1000
Jeremy Fitzhardinge2761fa02008-03-17 16:37:02 -07001001static inline void paravirt_alloc_pud(struct mm_struct *mm, unsigned pfn)
1002{
1003 PVOP_VCALL2(pv_mmu_ops.alloc_pud, mm, pfn);
1004}
1005static inline void paravirt_release_pud(unsigned pfn)
1006{
1007 PVOP_VCALL1(pv_mmu_ops.release_pud, pfn);
1008}
1009
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +02001010#ifdef CONFIG_HIGHPTE
1011static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
1012{
1013 unsigned long ret;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001014 ret = PVOP_CALL2(unsigned long, pv_mmu_ops.kmap_atomic_pte, page, type);
Jeremy Fitzhardingece6234b2007-05-02 19:27:15 +02001015 return (void *)ret;
1016}
1017#endif
1018
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001019static inline void pte_update(struct mm_struct *mm, unsigned long addr,
1020 pte_t *ptep)
1021{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001022 PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001023}
1024
1025static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
1026 pte_t *ptep)
1027{
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001028 PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001029}
1030
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +01001031static inline pte_t __pte(pteval_t val)
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001032{
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +01001033 pteval_t ret;
1034
1035 if (sizeof(pteval_t) > sizeof(long))
1036 ret = PVOP_CALL2(pteval_t,
1037 pv_mmu_ops.make_pte,
1038 val, (u64)val >> 32);
1039 else
1040 ret = PVOP_CALL1(pteval_t,
1041 pv_mmu_ops.make_pte,
1042 val);
1043
Jeremy Fitzhardingec8e53932008-01-30 13:32:57 +01001044 return (pte_t) { .pte = ret };
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001045}
1046
Jeremy Fitzhardinge773221f2008-01-30 13:33:15 +01001047static inline pteval_t pte_val(pte_t pte)
1048{
1049 pteval_t ret;
1050
1051 if (sizeof(pteval_t) > sizeof(long))
1052 ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_val,
1053 pte.pte, (u64)pte.pte >> 32);
1054 else
1055 ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_val,
1056 pte.pte);
1057
1058 return ret;
1059}
1060
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +01001061static inline pteval_t pte_flags(pte_t pte)
1062{
1063 pteval_t ret;
1064
1065 if (sizeof(pteval_t) > sizeof(long))
1066 ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_flags,
1067 pte.pte, (u64)pte.pte >> 32);
1068 else
1069 ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_flags,
1070 pte.pte);
1071
1072 return ret;
1073}
1074
Jeremy Fitzhardingeef385032008-01-30 13:33:15 +01001075static inline pgd_t __pgd(pgdval_t val)
1076{
1077 pgdval_t ret;
1078
1079 if (sizeof(pgdval_t) > sizeof(long))
1080 ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.make_pgd,
1081 val, (u64)val >> 32);
1082 else
1083 ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.make_pgd,
1084 val);
1085
1086 return (pgd_t) { ret };
1087}
1088
1089static inline pgdval_t pgd_val(pgd_t pgd)
1090{
1091 pgdval_t ret;
1092
1093 if (sizeof(pgdval_t) > sizeof(long))
1094 ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.pgd_val,
1095 pgd.pgd, (u64)pgd.pgd >> 32);
1096 else
1097 ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.pgd_val,
1098 pgd.pgd);
1099
1100 return ret;
1101}
1102
Jeremy Fitzhardinge08b882c2008-06-16 04:30:01 -07001103#define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
1104static inline pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
1105 pte_t *ptep)
1106{
1107 pteval_t ret;
1108
1109 ret = PVOP_CALL3(pteval_t, pv_mmu_ops.ptep_modify_prot_start,
1110 mm, addr, ptep);
1111
1112 return (pte_t) { .pte = ret };
1113}
1114
1115static inline void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
1116 pte_t *ptep, pte_t pte)
1117{
1118 if (sizeof(pteval_t) > sizeof(long))
1119 /* 5 arg words */
1120 pv_mmu_ops.ptep_modify_prot_commit(mm, addr, ptep, pte);
1121 else
1122 PVOP_VCALL4(pv_mmu_ops.ptep_modify_prot_commit,
1123 mm, addr, ptep, pte.pte);
1124}
1125
Jeremy Fitzhardinge4eed80c2008-01-30 13:33:15 +01001126static inline void set_pte(pte_t *ptep, pte_t pte)
1127{
1128 if (sizeof(pteval_t) > sizeof(long))
1129 PVOP_VCALL3(pv_mmu_ops.set_pte, ptep,
1130 pte.pte, (u64)pte.pte >> 32);
1131 else
1132 PVOP_VCALL2(pv_mmu_ops.set_pte, ptep,
1133 pte.pte);
1134}
1135
1136static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1137 pte_t *ptep, pte_t pte)
1138{
1139 if (sizeof(pteval_t) > sizeof(long))
1140 /* 5 arg words */
1141 pv_mmu_ops.set_pte_at(mm, addr, ptep, pte);
1142 else
1143 PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pte.pte);
1144}
1145
Jeremy Fitzhardinge60b3f622008-01-30 13:33:15 +01001146static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
1147{
1148 pmdval_t val = native_pmd_val(pmd);
1149
1150 if (sizeof(pmdval_t) > sizeof(long))
1151 PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
1152 else
1153 PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
1154}
1155
Glauber de Oliveira Costa1fe91512008-01-30 13:33:19 +01001156#if PAGETABLE_LEVELS >= 3
1157static inline pmd_t __pmd(pmdval_t val)
1158{
1159 pmdval_t ret;
1160
1161 if (sizeof(pmdval_t) > sizeof(long))
1162 ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.make_pmd,
1163 val, (u64)val >> 32);
1164 else
1165 ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.make_pmd,
1166 val);
1167
1168 return (pmd_t) { ret };
1169}
1170
1171static inline pmdval_t pmd_val(pmd_t pmd)
1172{
1173 pmdval_t ret;
1174
1175 if (sizeof(pmdval_t) > sizeof(long))
1176 ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.pmd_val,
1177 pmd.pmd, (u64)pmd.pmd >> 32);
1178 else
1179 ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.pmd_val,
1180 pmd.pmd);
1181
1182 return ret;
1183}
1184
1185static inline void set_pud(pud_t *pudp, pud_t pud)
1186{
1187 pudval_t val = native_pud_val(pud);
1188
1189 if (sizeof(pudval_t) > sizeof(long))
1190 PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
1191 val, (u64)val >> 32);
1192 else
1193 PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
1194 val);
1195}
Eduardo Habkost90422192008-01-30 13:33:20 +01001196#if PAGETABLE_LEVELS == 4
1197static inline pud_t __pud(pudval_t val)
1198{
1199 pudval_t ret;
1200
1201 if (sizeof(pudval_t) > sizeof(long))
1202 ret = PVOP_CALL2(pudval_t, pv_mmu_ops.make_pud,
1203 val, (u64)val >> 32);
1204 else
1205 ret = PVOP_CALL1(pudval_t, pv_mmu_ops.make_pud,
1206 val);
1207
1208 return (pud_t) { ret };
1209}
1210
1211static inline pudval_t pud_val(pud_t pud)
1212{
1213 pudval_t ret;
1214
1215 if (sizeof(pudval_t) > sizeof(long))
1216 ret = PVOP_CALL2(pudval_t, pv_mmu_ops.pud_val,
1217 pud.pud, (u64)pud.pud >> 32);
1218 else
1219 ret = PVOP_CALL1(pudval_t, pv_mmu_ops.pud_val,
1220 pud.pud);
1221
1222 return ret;
1223}
1224
1225static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
1226{
1227 pgdval_t val = native_pgd_val(pgd);
1228
1229 if (sizeof(pgdval_t) > sizeof(long))
1230 PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
1231 val, (u64)val >> 32);
1232 else
1233 PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
1234 val);
1235}
1236
1237static inline void pgd_clear(pgd_t *pgdp)
1238{
1239 set_pgd(pgdp, __pgd(0));
1240}
1241
1242static inline void pud_clear(pud_t *pudp)
1243{
1244 set_pud(pudp, __pud(0));
1245}
1246
1247#endif /* PAGETABLE_LEVELS == 4 */
1248
Glauber de Oliveira Costa1fe91512008-01-30 13:33:19 +01001249#endif /* PAGETABLE_LEVELS >= 3 */
1250
Jeremy Fitzhardinge4eed80c2008-01-30 13:33:15 +01001251#ifdef CONFIG_X86_PAE
1252/* Special-case pte-setting operations for PAE, which can't update a
1253 64-bit pte atomically */
1254static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
1255{
1256 PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
1257 pte.pte, pte.pte >> 32);
1258}
1259
1260static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
1261 pte_t *ptep, pte_t pte)
1262{
1263 /* 5 arg words */
1264 pv_mmu_ops.set_pte_present(mm, addr, ptep, pte);
1265}
1266
1267static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
1268 pte_t *ptep)
1269{
1270 PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
1271}
Jeremy Fitzhardinge60b3f622008-01-30 13:33:15 +01001272
1273static inline void pmd_clear(pmd_t *pmdp)
1274{
1275 PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
1276}
Jeremy Fitzhardinge4eed80c2008-01-30 13:33:15 +01001277#else /* !CONFIG_X86_PAE */
1278static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
1279{
1280 set_pte(ptep, pte);
1281}
1282
1283static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
1284 pte_t *ptep, pte_t pte)
1285{
1286 set_pte(ptep, pte);
1287}
1288
1289static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
1290 pte_t *ptep)
1291{
1292 set_pte_at(mm, addr, ptep, __pte(0));
1293}
Jeremy Fitzhardinge60b3f622008-01-30 13:33:15 +01001294
1295static inline void pmd_clear(pmd_t *pmdp)
1296{
1297 set_pmd(pmdp, __pmd(0));
1298}
Jeremy Fitzhardinge4eed80c2008-01-30 13:33:15 +01001299#endif /* CONFIG_X86_PAE */
1300
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001301/* Lazy mode for batching updates / context switch */
1302enum paravirt_lazy_mode {
1303 PARAVIRT_LAZY_NONE,
1304 PARAVIRT_LAZY_MMU,
1305 PARAVIRT_LAZY_CPU,
1306};
1307
1308enum paravirt_lazy_mode paravirt_get_lazy_mode(void);
1309void paravirt_enter_lazy_cpu(void);
1310void paravirt_leave_lazy_cpu(void);
1311void paravirt_enter_lazy_mmu(void);
1312void paravirt_leave_lazy_mmu(void);
1313void paravirt_leave_lazy(enum paravirt_lazy_mode mode);
1314
Zachary Amsden9226d122007-02-13 13:26:21 +01001315#define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001316static inline void arch_enter_lazy_cpu_mode(void)
1317{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001318 PVOP_VCALL0(pv_cpu_ops.lazy_mode.enter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001319}
1320
1321static inline void arch_leave_lazy_cpu_mode(void)
1322{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001323 PVOP_VCALL0(pv_cpu_ops.lazy_mode.leave);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001324}
1325
1326static inline void arch_flush_lazy_cpu_mode(void)
1327{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001328 if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)) {
1329 arch_leave_lazy_cpu_mode();
1330 arch_enter_lazy_cpu_mode();
1331 }
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001332}
1333
Zachary Amsden9226d122007-02-13 13:26:21 +01001334
1335#define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001336static inline void arch_enter_lazy_mmu_mode(void)
1337{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001338 PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001339}
1340
1341static inline void arch_leave_lazy_mmu_mode(void)
1342{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001343 PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001344}
1345
1346static inline void arch_flush_lazy_mmu_mode(void)
1347{
Jeremy Fitzhardinge8965c1c2007-10-16 11:51:29 -07001348 if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU)) {
1349 arch_leave_lazy_mmu_mode();
1350 arch_enter_lazy_mmu_mode();
1351 }
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001352}
Zachary Amsden9226d122007-02-13 13:26:21 +01001353
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -07001354static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx,
1355 unsigned long phys, pgprot_t flags)
1356{
1357 pv_mmu_ops.set_fixmap(idx, phys, flags);
1358}
1359
Jeremy Fitzhardinge45876232007-05-02 19:27:13 +02001360void _paravirt_nop(void);
1361#define paravirt_nop ((void *)_paravirt_nop)
1362
Rusty Russell139ec7c2006-12-07 02:14:08 +01001363/* These all sit in the .parainstructions section to tell us what to patch. */
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +02001364struct paravirt_patch_site {
Rusty Russell139ec7c2006-12-07 02:14:08 +01001365 u8 *instr; /* original instructions */
1366 u8 instrtype; /* type of this instruction */
1367 u8 len; /* length of original instruction */
1368 u16 clobbers; /* what registers you may clobber */
1369};
1370
Jeremy Fitzhardinge98de0322007-05-02 19:27:14 +02001371extern struct paravirt_patch_site __parainstructions[],
1372 __parainstructions_end[];
1373
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001374#ifdef CONFIG_X86_32
1375#define PV_SAVE_REGS "pushl %%ecx; pushl %%edx;"
1376#define PV_RESTORE_REGS "popl %%edx; popl %%ecx"
1377#define PV_FLAGS_ARG "0"
1378#define PV_EXTRA_CLOBBERS
1379#define PV_VEXTRA_CLOBBERS
1380#else
1381/* We save some registers, but all of them, that's too much. We clobber all
1382 * caller saved registers but the argument parameter */
1383#define PV_SAVE_REGS "pushq %%rdi;"
1384#define PV_RESTORE_REGS "popq %%rdi;"
1385#define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx"
1386#define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx"
1387#define PV_FLAGS_ARG "D"
1388#endif
1389
Rusty Russell139ec7c2006-12-07 02:14:08 +01001390static inline unsigned long __raw_local_save_flags(void)
1391{
1392 unsigned long f;
1393
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001394 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001395 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001396 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001397 : "=a"(f)
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001398 : paravirt_type(pv_irq_ops.save_fl),
Jeremy Fitzhardinge42c24fa2007-05-02 19:27:14 +02001399 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001400 : "memory", "cc" PV_VEXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001401 return f;
1402}
1403
1404static inline void raw_local_irq_restore(unsigned long f)
1405{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001406 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001407 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001408 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001409 : "=a"(f)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001410 : PV_FLAGS_ARG(f),
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001411 paravirt_type(pv_irq_ops.restore_fl),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001412 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001413 : "memory", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001414}
1415
1416static inline void raw_local_irq_disable(void)
1417{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001418 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001419 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001420 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001421 :
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001422 : paravirt_type(pv_irq_ops.irq_disable),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001423 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001424 : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001425}
1426
1427static inline void raw_local_irq_enable(void)
1428{
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001429 asm volatile(paravirt_alt(PV_SAVE_REGS
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001430 PARAVIRT_CALL
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001431 PV_RESTORE_REGS)
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001432 :
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001433 : paravirt_type(pv_irq_ops.irq_enable),
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001434 paravirt_clobber(CLBR_EAX)
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001435 : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
Rusty Russell139ec7c2006-12-07 02:14:08 +01001436}
1437
1438static inline unsigned long __raw_local_irq_save(void)
1439{
1440 unsigned long f;
1441
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001442 f = __raw_local_save_flags();
1443 raw_local_irq_disable();
Rusty Russell139ec7c2006-12-07 02:14:08 +01001444 return f;
1445}
1446
Jeremy Fitzhardinge294688c2007-05-02 19:27:14 +02001447/* Make sure as little as possible of this mess escapes. */
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001448#undef PARAVIRT_CALL
Jeremy Fitzhardinge1a45b7a2007-05-02 19:27:15 +02001449#undef __PVOP_CALL
1450#undef __PVOP_VCALL
Jeremy Fitzhardingef8822f42007-05-02 19:27:14 +02001451#undef PVOP_VCALL0
1452#undef PVOP_CALL0
1453#undef PVOP_VCALL1
1454#undef PVOP_CALL1
1455#undef PVOP_VCALL2
1456#undef PVOP_CALL2
1457#undef PVOP_VCALL3
1458#undef PVOP_CALL3
1459#undef PVOP_VCALL4
1460#undef PVOP_CALL4
Rusty Russell139ec7c2006-12-07 02:14:08 +01001461
Rusty Russelld3561b72006-12-07 02:14:07 +01001462#else /* __ASSEMBLY__ */
1463
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001464#define _PVSITE(ptype, clobbers, ops, word, algn) \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001465771:; \
1466 ops; \
1467772:; \
1468 .pushsection .parainstructions,"a"; \
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001469 .align algn; \
1470 word 771b; \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001471 .byte ptype; \
1472 .byte 772b-771b; \
1473 .short clobbers; \
1474 .popsection
1475
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001476
1477#ifdef CONFIG_X86_64
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001478#define PV_SAVE_REGS pushq %rax; pushq %rdi; pushq %rcx; pushq %rdx
1479#define PV_RESTORE_REGS popq %rdx; popq %rcx; popq %rdi; popq %rax
1480#define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001481#define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001482#define PARA_INDIRECT(addr) *addr(%rip)
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001483#else
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001484#define PV_SAVE_REGS pushl %eax; pushl %edi; pushl %ecx; pushl %edx
1485#define PV_RESTORE_REGS popl %edx; popl %ecx; popl %edi; popl %eax
1486#define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001487#define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001488#define PARA_INDIRECT(addr) *%cs:addr
Glauber de Oliveira Costa658be9d2008-01-30 13:32:06 +01001489#endif
1490
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001491#define INTERRUPT_RETURN \
1492 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001493 jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_iret))
Rusty Russell139ec7c2006-12-07 02:14:08 +01001494
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001495#define DISABLE_INTERRUPTS(clobbers) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001496 PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001497 PV_SAVE_REGS; \
1498 call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_disable); \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001499 PV_RESTORE_REGS;) \
Rusty Russell139ec7c2006-12-07 02:14:08 +01001500
Jeremy Fitzhardinged5822032007-05-02 19:27:14 +02001501#define ENABLE_INTERRUPTS(clobbers) \
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001502 PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001503 PV_SAVE_REGS; \
1504 call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable); \
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001505 PV_RESTORE_REGS;)
Rusty Russell139ec7c2006-12-07 02:14:08 +01001506
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -04001507#define USERGS_SYSRET32 \
1508 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32), \
Glauber de Oliveira Costa6abcd982008-01-30 13:30:33 +01001509 CLBR_NONE, \
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -04001510 jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
Glauber de Oliveira Costa2e47d3e2008-01-30 13:32:07 +01001511
Glauber de Oliveira Costa6057fc82008-01-30 13:32:06 +01001512#ifdef CONFIG_X86_32
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001513#define GET_CR0_INTO_EAX \
1514 push %ecx; push %edx; \
1515 call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0); \
Jeremy Fitzhardinge42c24fa2007-05-02 19:27:14 +02001516 pop %edx; pop %ecx
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -04001517
1518#define ENABLE_INTERRUPTS_SYSEXIT \
1519 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
1520 CLBR_NONE, \
1521 jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
1522
1523
1524#else /* !CONFIG_X86_32 */
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +01001525#define SWAPGS \
1526 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
1527 PV_SAVE_REGS; \
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001528 call PARA_INDIRECT(pv_cpu_ops+PV_CPU_swapgs); \
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +01001529 PV_RESTORE_REGS \
1530 )
1531
Jeremy Fitzhardinge491eccb2008-06-25 00:19:15 -04001532#define GET_CR2_INTO_RCX \
1533 call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2); \
1534 movq %rax, %rcx; \
Glauber de Oliveira Costa4a8c4c42008-01-30 13:32:07 +01001535 xorq %rax, %rax;
1536
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -04001537#define USERGS_SYSRET64 \
1538 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
Jeremy Fitzhardinged75cd22f2008-06-25 00:19:26 -04001539 CLBR_NONE, \
Jeremy Fitzhardinge2be29982008-06-25 00:19:28 -04001540 jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
1541
1542#define ENABLE_INTERRUPTS_SYSEXIT32 \
1543 PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
1544 CLBR_NONE, \
1545 jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
1546#endif /* CONFIG_X86_32 */
Rusty Russell139ec7c2006-12-07 02:14:08 +01001547
Rusty Russelld3561b72006-12-07 02:14:07 +01001548#endif /* __ASSEMBLY__ */
1549#endif /* CONFIG_PARAVIRT */
1550#endif /* __ASM_PARAVIRT_H */