[PATCH] i386: PARAVIRT: add kmap_atomic_pte for mapping highpte pages
[sfrench/cifs-2.6.git] / include / asm-i386 / paravirt.h
1 #ifndef __ASM_PARAVIRT_H
2 #define __ASM_PARAVIRT_H
3 /* Various instructions on x86 need to be replaced for
4  * para-virtualization: those hooks are defined here. */
5
6 #ifdef CONFIG_PARAVIRT
7 #include <asm/page.h>
8
9 /* Bitmask of what can be clobbered: usually at least eax. */
10 #define CLBR_NONE 0x0
11 #define CLBR_EAX 0x1
12 #define CLBR_ECX 0x2
13 #define CLBR_EDX 0x4
14 #define CLBR_ANY 0x7
15
16 #ifndef __ASSEMBLY__
17 #include <linux/types.h>
18 #include <linux/cpumask.h>
19 #include <asm/kmap_types.h>
20
21 struct page;
22 struct thread_struct;
23 struct Xgt_desc_struct;
24 struct tss_struct;
25 struct mm_struct;
26 struct desc_struct;
27
28 /* Lazy mode for batching updates / context switch */
29 enum paravirt_lazy_mode {
30         PARAVIRT_LAZY_NONE = 0,
31         PARAVIRT_LAZY_MMU = 1,
32         PARAVIRT_LAZY_CPU = 2,
33 };
34
35 struct paravirt_ops
36 {
37         unsigned int kernel_rpl;
38         int shared_kernel_pmd;
39         int paravirt_enabled;
40         const char *name;
41
42         /*
43          * Patch may replace one of the defined code sequences with arbitrary
44          * code, subject to the same register constraints.  This generally
45          * means the code is not free to clobber any registers other than EAX.
46          * The patch function should return the number of bytes of code
47          * generated, as we nop pad the rest in generic code.
48          */
49         unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
50
51         /* Basic arch-specific setup */
52         void (*arch_setup)(void);
53         char *(*memory_setup)(void);
54         void (*init_IRQ)(void);
55         void (*time_init)(void);
56
57         /*
58          * Called before/after init_mm pagetable setup. setup_start
59          * may reset %cr3, and may pre-install parts of the pagetable;
60          * pagetable setup is expected to preserve any existing
61          * mapping.
62          */
63         void (*pagetable_setup_start)(pgd_t *pgd_base);
64         void (*pagetable_setup_done)(pgd_t *pgd_base);
65
66         /* Print a banner to identify the environment */
67         void (*banner)(void);
68
69         /* Set and set time of day */
70         unsigned long (*get_wallclock)(void);
71         int (*set_wallclock)(unsigned long);
72
73         /* cpuid emulation, mostly so that caps bits can be disabled */
74         void (*cpuid)(unsigned int *eax, unsigned int *ebx,
75                       unsigned int *ecx, unsigned int *edx);
76
77         /* hooks for various privileged instructions */
78         unsigned long (*get_debugreg)(int regno);
79         void (*set_debugreg)(int regno, unsigned long value);
80
81         void (*clts)(void);
82
83         unsigned long (*read_cr0)(void);
84         void (*write_cr0)(unsigned long);
85
86         unsigned long (*read_cr2)(void);
87         void (*write_cr2)(unsigned long);
88
89         unsigned long (*read_cr3)(void);
90         void (*write_cr3)(unsigned long);
91
92         unsigned long (*read_cr4_safe)(void);
93         unsigned long (*read_cr4)(void);
94         void (*write_cr4)(unsigned long);
95
96         /*
97          * Get/set interrupt state.  save_fl and restore_fl are only
98          * expected to use X86_EFLAGS_IF; all other bits
99          * returned from save_fl are undefined, and may be ignored by
100          * restore_fl.
101          */
102         unsigned long (*save_fl)(void);
103         void (*restore_fl)(unsigned long);
104         void (*irq_disable)(void);
105         void (*irq_enable)(void);
106         void (*safe_halt)(void);
107         void (*halt)(void);
108
109         void (*wbinvd)(void);
110
111         /* MSR, PMC and TSR operations.
112            err = 0/-EFAULT.  wrmsr returns 0/-EFAULT. */
113         u64 (*read_msr)(unsigned int msr, int *err);
114         int (*write_msr)(unsigned int msr, u64 val);
115
116         u64 (*read_tsc)(void);
117         u64 (*read_pmc)(void);
118         u64 (*get_scheduled_cycles)(void);
119         unsigned long (*get_cpu_khz)(void);
120
121         /* Segment descriptor handling */
122         void (*load_tr_desc)(void);
123         void (*load_gdt)(const struct Xgt_desc_struct *);
124         void (*load_idt)(const struct Xgt_desc_struct *);
125         void (*store_gdt)(struct Xgt_desc_struct *);
126         void (*store_idt)(struct Xgt_desc_struct *);
127         void (*set_ldt)(const void *desc, unsigned entries);
128         unsigned long (*store_tr)(void);
129         void (*load_tls)(struct thread_struct *t, unsigned int cpu);
130         void (*write_ldt_entry)(struct desc_struct *,
131                                 int entrynum, u32 low, u32 high);
132         void (*write_gdt_entry)(struct desc_struct *,
133                                 int entrynum, u32 low, u32 high);
134         void (*write_idt_entry)(struct desc_struct *,
135                                 int entrynum, u32 low, u32 high);
136         void (*load_esp0)(struct tss_struct *tss, struct thread_struct *t);
137
138         void (*set_iopl_mask)(unsigned mask);
139         void (*io_delay)(void);
140
141         /*
142          * Hooks for intercepting the creation/use/destruction of an
143          * mm_struct.
144          */
145         void (*activate_mm)(struct mm_struct *prev,
146                             struct mm_struct *next);
147         void (*dup_mmap)(struct mm_struct *oldmm,
148                          struct mm_struct *mm);
149         void (*exit_mmap)(struct mm_struct *mm);
150
151 #ifdef CONFIG_X86_LOCAL_APIC
152         /*
153          * Direct APIC operations, principally for VMI.  Ideally
154          * these shouldn't be in this interface.
155          */
156         void (*apic_write)(unsigned long reg, unsigned long v);
157         void (*apic_write_atomic)(unsigned long reg, unsigned long v);
158         unsigned long (*apic_read)(unsigned long reg);
159         void (*setup_boot_clock)(void);
160         void (*setup_secondary_clock)(void);
161
162         void (*startup_ipi_hook)(int phys_apicid,
163                                  unsigned long start_eip,
164                                  unsigned long start_esp);
165 #endif
166
167         /* TLB operations */
168         void (*flush_tlb_user)(void);
169         void (*flush_tlb_kernel)(void);
170         void (*flush_tlb_single)(unsigned long addr);
171         void (*flush_tlb_others)(const cpumask_t *cpus, struct mm_struct *mm,
172                                  unsigned long va);
173
174         /* Hooks for allocating/releasing pagetable pages */
175         void (*alloc_pt)(u32 pfn);
176         void (*alloc_pd)(u32 pfn);
177         void (*alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
178         void (*release_pt)(u32 pfn);
179         void (*release_pd)(u32 pfn);
180
181         /* Pagetable manipulation functions */
182         void (*set_pte)(pte_t *ptep, pte_t pteval);
183         void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
184                            pte_t *ptep, pte_t pteval);
185         void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
186         void (*pte_update)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
187         void (*pte_update_defer)(struct mm_struct *mm,
188                                  unsigned long addr, pte_t *ptep);
189
190         pte_t (*ptep_get_and_clear)(pte_t *ptep);
191
192 #ifdef CONFIG_HIGHPTE
193         void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
194 #endif
195
196 #ifdef CONFIG_X86_PAE
197         void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
198         void (*set_pte_present)(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte);
199         void (*set_pud)(pud_t *pudp, pud_t pudval);
200         void (*pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
201         void (*pmd_clear)(pmd_t *pmdp);
202
203         unsigned long long (*pte_val)(pte_t);
204         unsigned long long (*pmd_val)(pmd_t);
205         unsigned long long (*pgd_val)(pgd_t);
206
207         pte_t (*make_pte)(unsigned long long pte);
208         pmd_t (*make_pmd)(unsigned long long pmd);
209         pgd_t (*make_pgd)(unsigned long long pgd);
210 #else
211         unsigned long (*pte_val)(pte_t);
212         unsigned long (*pgd_val)(pgd_t);
213
214         pte_t (*make_pte)(unsigned long pte);
215         pgd_t (*make_pgd)(unsigned long pgd);
216 #endif
217
218         /* Set deferred update mode, used for batching operations. */
219         void (*set_lazy_mode)(enum paravirt_lazy_mode mode);
220
221         /* These two are jmp to, not actually called. */
222         void (*irq_enable_sysexit)(void);
223         void (*iret)(void);
224 };
225
226 /* Mark a paravirt probe function. */
227 #define paravirt_probe(fn)                                              \
228  static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
229                 __attribute__((__section__(".paravirtprobe"))) = fn
230
231 extern struct paravirt_ops paravirt_ops;
232
233 #define PARAVIRT_PATCH(x)                                       \
234         (offsetof(struct paravirt_ops, x) / sizeof(void *))
235
236 #define paravirt_type(type)                                     \
237         [paravirt_typenum] "i" (PARAVIRT_PATCH(type))
238 #define paravirt_clobber(clobber)               \
239         [paravirt_clobber] "i" (clobber)
240
241 /*
242  * Generate some code, and mark it as patchable by the
243  * apply_paravirt() alternate instruction patcher.
244  */
245 #define _paravirt_alt(insn_string, type, clobber)       \
246         "771:\n\t" insn_string "\n" "772:\n"            \
247         ".pushsection .parainstructions,\"a\"\n"        \
248         "  .long 771b\n"                                \
249         "  .byte " type "\n"                            \
250         "  .byte 772b-771b\n"                           \
251         "  .short " clobber "\n"                        \
252         ".popsection\n"
253
254 /* Generate patchable code, with the default asm parameters. */
255 #define paravirt_alt(insn_string)                                       \
256         _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
257
258 unsigned paravirt_patch_nop(void);
259 unsigned paravirt_patch_ignore(unsigned len);
260 unsigned paravirt_patch_call(void *target, u16 tgt_clobbers,
261                              void *site, u16 site_clobbers,
262                              unsigned len);
263 unsigned paravirt_patch_jmp(void *target, void *site, unsigned len);
264 unsigned paravirt_patch_default(u8 type, u16 clobbers, void *site, unsigned len);
265
266 unsigned paravirt_patch_insns(void *site, unsigned len,
267                               const char *start, const char *end);
268
269
270 /*
271  * This generates an indirect call based on the operation type number.
272  * The type number, computed in PARAVIRT_PATCH, is derived from the
273  * offset into the paravirt_ops structure, and can therefore be freely
274  * converted back into a structure offset.
275  */
276 #define PARAVIRT_CALL   "call *(paravirt_ops+%c[paravirt_typenum]*4);"
277
278 /*
279  * These macros are intended to wrap calls into a paravirt_ops
280  * operation, so that they can be later identified and patched at
281  * runtime.
282  *
283  * Normally, a call to a pv_op function is a simple indirect call:
284  * (paravirt_ops.operations)(args...).
285  *
286  * Unfortunately, this is a relatively slow operation for modern CPUs,
287  * because it cannot necessarily determine what the destination
288  * address is.  In this case, the address is a runtime constant, so at
289  * the very least we can patch the call to e a simple direct call, or
290  * ideally, patch an inline implementation into the callsite.  (Direct
291  * calls are essentially free, because the call and return addresses
292  * are completely predictable.)
293  *
294  * These macros rely on the standard gcc "regparm(3)" calling
295  * convention, in which the first three arguments are placed in %eax,
296  * %edx, %ecx (in that order), and the remaining arguments are placed
297  * on the stack.  All caller-save registers (eax,edx,ecx) are expected
298  * to be modified (either clobbered or used for return values).
299  *
300  * The call instruction itself is marked by placing its start address
301  * and size into the .parainstructions section, so that
302  * apply_paravirt() in arch/i386/kernel/alternative.c can do the
303  * appropriate patching under the control of the backend paravirt_ops
304  * implementation.
305  *
306  * Unfortunately there's no way to get gcc to generate the args setup
307  * for the call, and then allow the call itself to be generated by an
308  * inline asm.  Because of this, we must do the complete arg setup and
309  * return value handling from within these macros.  This is fairly
310  * cumbersome.
311  *
312  * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
313  * It could be extended to more arguments, but there would be little
314  * to be gained from that.  For each number of arguments, there are
315  * the two VCALL and CALL variants for void and non-void functions.
316  *
317  * When there is a return value, the invoker of the macro must specify
318  * the return type.  The macro then uses sizeof() on that type to
319  * determine whether its a 32 or 64 bit value, and places the return
320  * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
321  * 64-bit).
322  *
323  * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
324  * in low,high order.
325  *
326  * Small structures are passed and returned in registers.  The macro
327  * calling convention can't directly deal with this, so the wrapper
328  * functions must do this.
329  *
330  * These PVOP_* macros are only defined within this header.  This
331  * means that all uses must be wrapped in inline functions.  This also
332  * makes sure the incoming and outgoing types are always correct.
333  */
334 #define PVOP_CALL0(__rettype, __op)                                     \
335         ({                                                              \
336                 __rettype __ret;                                        \
337                 if (sizeof(__rettype) > sizeof(unsigned long)) {        \
338                         unsigned long long __tmp;                       \
339                         unsigned long __ecx;                            \
340                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
341                                      : "=A" (__tmp), "=c" (__ecx)       \
342                                      : paravirt_type(__op),             \
343                                        paravirt_clobber(CLBR_ANY)       \
344                                      : "memory", "cc");                 \
345                         __ret = (__rettype)__tmp;                       \
346                 } else {                                                \
347                         unsigned long __tmp, __edx, __ecx;              \
348                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
349                                      : "=a" (__tmp), "=d" (__edx),      \
350                                        "=c" (__ecx)                     \
351                                      : paravirt_type(__op),             \
352                                        paravirt_clobber(CLBR_ANY)       \
353                                      : "memory", "cc");                 \
354                         __ret = (__rettype)__tmp;                       \
355                 }                                                       \
356                 __ret;                                                  \
357         })
358 #define PVOP_VCALL0(__op)                                               \
359         ({                                                              \
360                 unsigned long __eax, __edx, __ecx;                      \
361                 asm volatile(paravirt_alt(PARAVIRT_CALL)                \
362                              : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
363                              : paravirt_type(__op),                     \
364                                paravirt_clobber(CLBR_ANY)               \
365                              : "memory", "cc");                         \
366         })
367
368 #define PVOP_CALL1(__rettype, __op, arg1)                               \
369         ({                                                              \
370                 __rettype __ret;                                        \
371                 if (sizeof(__rettype) > sizeof(unsigned long)) {        \
372                         unsigned long long __tmp;                       \
373                         unsigned long __ecx;                            \
374                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
375                                      : "=A" (__tmp), "=c" (__ecx)       \
376                                      : "a" ((u32)(arg1)),               \
377                                        paravirt_type(__op),             \
378                                        paravirt_clobber(CLBR_ANY)       \
379                                      : "memory", "cc");                 \
380                         __ret = (__rettype)__tmp;                       \
381                 } else {                                                \
382                         unsigned long __tmp, __edx, __ecx;              \
383                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
384                                      : "=a" (__tmp), "=d" (__edx),      \
385                                        "=c" (__ecx)                     \
386                                      : "0" ((u32)(arg1)),               \
387                                        paravirt_type(__op),             \
388                                        paravirt_clobber(CLBR_ANY)       \
389                                      : "memory", "cc");                 \
390                         __ret = (__rettype)__tmp;                       \
391                 }                                                       \
392                 __ret;                                                  \
393         })
394 #define PVOP_VCALL1(__op, arg1)                                         \
395         ({                                                              \
396                 unsigned long __eax, __edx, __ecx;                      \
397                 asm volatile(paravirt_alt(PARAVIRT_CALL)                \
398                              : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
399                              : "0" ((u32)(arg1)),                       \
400                                paravirt_type(__op),                     \
401                                paravirt_clobber(CLBR_ANY)               \
402                              : "memory", "cc");                         \
403         })
404
405 #define PVOP_CALL2(__rettype, __op, arg1, arg2)                         \
406         ({                                                              \
407                 __rettype __ret;                                        \
408                 if (sizeof(__rettype) > sizeof(unsigned long)) {        \
409                         unsigned long long __tmp;                       \
410                         unsigned long __ecx;                            \
411                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
412                                      : "=A" (__tmp), "=c" (__ecx)       \
413                                      : "a" ((u32)(arg1)),               \
414                                        "d" ((u32)(arg2)),               \
415                                        paravirt_type(__op),             \
416                                        paravirt_clobber(CLBR_ANY)       \
417                                      : "memory", "cc");                 \
418                         __ret = (__rettype)__tmp;                       \
419                 } else {                                                \
420                         unsigned long __tmp, __edx, __ecx;              \
421                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
422                                      : "=a" (__tmp), "=d" (__edx),      \
423                                        "=c" (__ecx)                     \
424                                      : "0" ((u32)(arg1)),               \
425                                        "1" ((u32)(arg2)),               \
426                                        paravirt_type(__op),             \
427                                        paravirt_clobber(CLBR_ANY)       \
428                                      : "memory", "cc");                 \
429                         __ret = (__rettype)__tmp;                       \
430                 }                                                       \
431                 __ret;                                                  \
432         })
433 #define PVOP_VCALL2(__op, arg1, arg2)                                   \
434         ({                                                              \
435                 unsigned long __eax, __edx, __ecx;                      \
436                 asm volatile(paravirt_alt(PARAVIRT_CALL)                \
437                              : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
438                              : "0" ((u32)(arg1)),                       \
439                                "1" ((u32)(arg2)),                       \
440                                paravirt_type(__op),                     \
441                                paravirt_clobber(CLBR_ANY)               \
442                              : "memory", "cc");                         \
443         })
444
445 #define PVOP_CALL3(__rettype, __op, arg1, arg2, arg3)                   \
446         ({                                                              \
447                 __rettype __ret;                                        \
448                 if (sizeof(__rettype) > sizeof(unsigned long)) {        \
449                         unsigned long long __tmp;                       \
450                         unsigned long __ecx;                            \
451                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
452                                      : "=A" (__tmp), "=c" (__ecx)       \
453                                      : "a" ((u32)(arg1)),               \
454                                        "d" ((u32)(arg2)),               \
455                                        "1" ((u32)(arg3)),               \
456                                        paravirt_type(__op),             \
457                                        paravirt_clobber(CLBR_ANY)       \
458                                      : "memory", "cc");                 \
459                         __ret = (__rettype)__tmp;                       \
460                 } else {                                                \
461                         unsigned long __tmp, __edx, __ecx;      \
462                         asm volatile(paravirt_alt(PARAVIRT_CALL)        \
463                                      : "=a" (__tmp), "=d" (__edx),      \
464                                        "=c" (__ecx)                     \
465                                      : "0" ((u32)(arg1)),               \
466                                        "1" ((u32)(arg2)),               \
467                                        "2" ((u32)(arg3)),               \
468                                        paravirt_type(__op),             \
469                                        paravirt_clobber(CLBR_ANY)       \
470                                      : "memory", "cc");                 \
471                         __ret = (__rettype)__tmp;                       \
472                 }                                                       \
473                 __ret;                                                  \
474         })
475 #define PVOP_VCALL3(__op, arg1, arg2, arg3)                             \
476         ({                                                              \
477                 unsigned long __eax, __edx, __ecx;                      \
478                 asm volatile(paravirt_alt(PARAVIRT_CALL)                \
479                              : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
480                              : "0" ((u32)(arg1)),                       \
481                                "1" ((u32)(arg2)),                       \
482                                "2" ((u32)(arg3)),                       \
483                                paravirt_type(__op),                     \
484                                paravirt_clobber(CLBR_ANY)               \
485                              : "memory", "cc");                         \
486         })
487
488 #define PVOP_CALL4(__rettype, __op, arg1, arg2, arg3, arg4)             \
489         ({                                                              \
490                 __rettype __ret;                                        \
491                 if (sizeof(__rettype) > sizeof(unsigned long)) {        \
492                         unsigned long long __tmp;                       \
493                         unsigned long __ecx;                            \
494                         asm volatile("push %[_arg4]; "                  \
495                                      paravirt_alt(PARAVIRT_CALL)        \
496                                      "lea 4(%%esp),%%esp"               \
497                                      : "=A" (__tmp), "=c" (__ecx)       \
498                                      : "a" ((u32)(arg1)),               \
499                                        "d" ((u32)(arg2)),               \
500                                        "1" ((u32)(arg3)),               \
501                                        [_arg4] "mr" ((u32)(arg4)),      \
502                                        paravirt_type(__op),             \
503                                        paravirt_clobber(CLBR_ANY)       \
504                                      : "memory", "cc",);                \
505                         __ret = (__rettype)__tmp;                       \
506                 } else {                                                \
507                         unsigned long __tmp, __edx, __ecx;              \
508                         asm volatile("push %[_arg4]; "                  \
509                                      paravirt_alt(PARAVIRT_CALL)        \
510                                      "lea 4(%%esp),%%esp"               \
511                                      : "=a" (__tmp), "=d" (__edx), "=c" (__ecx) \
512                                      : "0" ((u32)(arg1)),               \
513                                        "1" ((u32)(arg2)),               \
514                                        "2" ((u32)(arg3)),               \
515                                        [_arg4]"mr" ((u32)(arg4)),       \
516                                        paravirt_type(__op),             \
517                                        paravirt_clobber(CLBR_ANY)       \
518                                      : "memory", "cc");                 \
519                         __ret = (__rettype)__tmp;                       \
520                 }                                                       \
521                 __ret;                                                  \
522         })
523 #define PVOP_VCALL4(__op, arg1, arg2, arg3, arg4)                       \
524         ({                                                              \
525                 unsigned long __eax, __edx, __ecx;                      \
526                 asm volatile("push %[_arg4]; "                          \
527                              paravirt_alt(PARAVIRT_CALL)                \
528                              "lea 4(%%esp),%%esp"                       \
529                              : "=a" (__eax), "=d" (__edx), "=c" (__ecx) \
530                              : "0" ((u32)(arg1)),                       \
531                                "1" ((u32)(arg2)),                       \
532                                "2" ((u32)(arg3)),                       \
533                                [_arg4]"mr" ((u32)(arg4)),               \
534                                paravirt_type(__op),                     \
535                                paravirt_clobber(CLBR_ANY)               \
536                              : "memory", "cc");                         \
537         })
538
539 static inline int paravirt_enabled(void)
540 {
541         return paravirt_ops.paravirt_enabled;
542 }
543
544 static inline void load_esp0(struct tss_struct *tss,
545                              struct thread_struct *thread)
546 {
547         PVOP_VCALL2(load_esp0, tss, thread);
548 }
549
550 #define ARCH_SETUP                      paravirt_ops.arch_setup();
551 static inline unsigned long get_wallclock(void)
552 {
553         return PVOP_CALL0(unsigned long, get_wallclock);
554 }
555
556 static inline int set_wallclock(unsigned long nowtime)
557 {
558         return PVOP_CALL1(int, set_wallclock, nowtime);
559 }
560
561 static inline void (*choose_time_init(void))(void)
562 {
563         return paravirt_ops.time_init;
564 }
565
566 /* The paravirtualized CPUID instruction. */
567 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
568                            unsigned int *ecx, unsigned int *edx)
569 {
570         PVOP_VCALL4(cpuid, eax, ebx, ecx, edx);
571 }
572
573 /*
574  * These special macros can be used to get or set a debugging register
575  */
576 static inline unsigned long paravirt_get_debugreg(int reg)
577 {
578         return PVOP_CALL1(unsigned long, get_debugreg, reg);
579 }
580 #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
581 static inline void set_debugreg(unsigned long val, int reg)
582 {
583         PVOP_VCALL2(set_debugreg, reg, val);
584 }
585
586 static inline void clts(void)
587 {
588         PVOP_VCALL0(clts);
589 }
590
591 static inline unsigned long read_cr0(void)
592 {
593         return PVOP_CALL0(unsigned long, read_cr0);
594 }
595
596 static inline void write_cr0(unsigned long x)
597 {
598         PVOP_VCALL1(write_cr0, x);
599 }
600
601 static inline unsigned long read_cr2(void)
602 {
603         return PVOP_CALL0(unsigned long, read_cr2);
604 }
605
606 static inline void write_cr2(unsigned long x)
607 {
608         PVOP_VCALL1(write_cr2, x);
609 }
610
611 static inline unsigned long read_cr3(void)
612 {
613         return PVOP_CALL0(unsigned long, read_cr3);
614 }
615
616 static inline void write_cr3(unsigned long x)
617 {
618         PVOP_VCALL1(write_cr3, x);
619 }
620
621 static inline unsigned long read_cr4(void)
622 {
623         return PVOP_CALL0(unsigned long, read_cr4);
624 }
625 static inline unsigned long read_cr4_safe(void)
626 {
627         return PVOP_CALL0(unsigned long, read_cr4_safe);
628 }
629
630 static inline void write_cr4(unsigned long x)
631 {
632         PVOP_VCALL1(write_cr4, x);
633 }
634
635 static inline void raw_safe_halt(void)
636 {
637         PVOP_VCALL0(safe_halt);
638 }
639
640 static inline void halt(void)
641 {
642         PVOP_VCALL0(safe_halt);
643 }
644
645 static inline void wbinvd(void)
646 {
647         PVOP_VCALL0(wbinvd);
648 }
649
650 #define get_kernel_rpl()  (paravirt_ops.kernel_rpl)
651
652 static inline u64 paravirt_read_msr(unsigned msr, int *err)
653 {
654         return PVOP_CALL2(u64, read_msr, msr, err);
655 }
656 static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
657 {
658         return PVOP_CALL3(int, write_msr, msr, low, high);
659 }
660
661 /* These should all do BUG_ON(_err), but our headers are too tangled. */
662 #define rdmsr(msr,val1,val2) do {               \
663         int _err;                               \
664         u64 _l = paravirt_read_msr(msr, &_err); \
665         val1 = (u32)_l;                         \
666         val2 = _l >> 32;                        \
667 } while(0)
668
669 #define wrmsr(msr,val1,val2) do {               \
670         paravirt_write_msr(msr, val1, val2);    \
671 } while(0)
672
673 #define rdmsrl(msr,val) do {                    \
674         int _err;                               \
675         val = paravirt_read_msr(msr, &_err);    \
676 } while(0)
677
678 #define wrmsrl(msr,val)         ((void)paravirt_write_msr(msr, val, 0))
679 #define wrmsr_safe(msr,a,b)     paravirt_write_msr(msr, a, b)
680
681 /* rdmsr with exception handling */
682 #define rdmsr_safe(msr,a,b) ({                  \
683         int _err;                               \
684         u64 _l = paravirt_read_msr(msr, &_err); \
685         (*a) = (u32)_l;                         \
686         (*b) = _l >> 32;                        \
687         _err; })
688
689
690 static inline u64 paravirt_read_tsc(void)
691 {
692         return PVOP_CALL0(u64, read_tsc);
693 }
694 #define rdtsc(low,high) do {                    \
695         u64 _l = paravirt_read_tsc();           \
696         low = (u32)_l;                          \
697         high = _l >> 32;                        \
698 } while(0)
699
700 #define rdtscl(low) do {                        \
701         u64 _l = paravirt_read_tsc();           \
702         low = (int)_l;                          \
703 } while(0)
704
705 #define rdtscll(val) (val = paravirt_read_tsc())
706
707 #define get_scheduled_cycles(val) (val = paravirt_ops.get_scheduled_cycles())
708 #define calculate_cpu_khz() (paravirt_ops.get_cpu_khz())
709
710 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
711
712 static inline unsigned long long paravirt_read_pmc(int counter)
713 {
714         return PVOP_CALL1(u64, read_pmc, counter);
715 }
716
717 #define rdpmc(counter,low,high) do {            \
718         u64 _l = paravirt_read_pmc(counter);    \
719         low = (u32)_l;                          \
720         high = _l >> 32;                        \
721 } while(0)
722
723 static inline void load_TR_desc(void)
724 {
725         PVOP_VCALL0(load_tr_desc);
726 }
727 static inline void load_gdt(const struct Xgt_desc_struct *dtr)
728 {
729         PVOP_VCALL1(load_gdt, dtr);
730 }
731 static inline void load_idt(const struct Xgt_desc_struct *dtr)
732 {
733         PVOP_VCALL1(load_idt, dtr);
734 }
735 static inline void set_ldt(const void *addr, unsigned entries)
736 {
737         PVOP_VCALL2(set_ldt, addr, entries);
738 }
739 static inline void store_gdt(struct Xgt_desc_struct *dtr)
740 {
741         PVOP_VCALL1(store_gdt, dtr);
742 }
743 static inline void store_idt(struct Xgt_desc_struct *dtr)
744 {
745         PVOP_VCALL1(store_idt, dtr);
746 }
747 static inline unsigned long paravirt_store_tr(void)
748 {
749         return PVOP_CALL0(unsigned long, store_tr);
750 }
751 #define store_tr(tr)    ((tr) = paravirt_store_tr())
752 static inline void load_TLS(struct thread_struct *t, unsigned cpu)
753 {
754         PVOP_VCALL2(load_tls, t, cpu);
755 }
756 static inline void write_ldt_entry(void *dt, int entry, u32 low, u32 high)
757 {
758         PVOP_VCALL4(write_ldt_entry, dt, entry, low, high);
759 }
760 static inline void write_gdt_entry(void *dt, int entry, u32 low, u32 high)
761 {
762         PVOP_VCALL4(write_gdt_entry, dt, entry, low, high);
763 }
764 static inline void write_idt_entry(void *dt, int entry, u32 low, u32 high)
765 {
766         PVOP_VCALL4(write_idt_entry, dt, entry, low, high);
767 }
768 static inline void set_iopl_mask(unsigned mask)
769 {
770         PVOP_VCALL1(set_iopl_mask, mask);
771 }
772
773 /* The paravirtualized I/O functions */
774 static inline void slow_down_io(void) {
775         paravirt_ops.io_delay();
776 #ifdef REALLY_SLOW_IO
777         paravirt_ops.io_delay();
778         paravirt_ops.io_delay();
779         paravirt_ops.io_delay();
780 #endif
781 }
782
783 #ifdef CONFIG_X86_LOCAL_APIC
784 /*
785  * Basic functions accessing APICs.
786  */
787 static inline void apic_write(unsigned long reg, unsigned long v)
788 {
789         PVOP_VCALL2(apic_write, reg, v);
790 }
791
792 static inline void apic_write_atomic(unsigned long reg, unsigned long v)
793 {
794         PVOP_VCALL2(apic_write_atomic, reg, v);
795 }
796
797 static inline unsigned long apic_read(unsigned long reg)
798 {
799         return PVOP_CALL1(unsigned long, apic_read, reg);
800 }
801
802 static inline void setup_boot_clock(void)
803 {
804         PVOP_VCALL0(setup_boot_clock);
805 }
806
807 static inline void setup_secondary_clock(void)
808 {
809         PVOP_VCALL0(setup_secondary_clock);
810 }
811 #endif
812
813 static inline void paravirt_pagetable_setup_start(pgd_t *base)
814 {
815         if (paravirt_ops.pagetable_setup_start)
816                 (*paravirt_ops.pagetable_setup_start)(base);
817 }
818
819 static inline void paravirt_pagetable_setup_done(pgd_t *base)
820 {
821         if (paravirt_ops.pagetable_setup_done)
822                 (*paravirt_ops.pagetable_setup_done)(base);
823 }
824
825 #ifdef CONFIG_SMP
826 static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
827                                     unsigned long start_esp)
828 {
829         PVOP_VCALL3(startup_ipi_hook, phys_apicid, start_eip, start_esp);
830 }
831 #endif
832
833 static inline void paravirt_activate_mm(struct mm_struct *prev,
834                                         struct mm_struct *next)
835 {
836         PVOP_VCALL2(activate_mm, prev, next);
837 }
838
839 static inline void arch_dup_mmap(struct mm_struct *oldmm,
840                                  struct mm_struct *mm)
841 {
842         PVOP_VCALL2(dup_mmap, oldmm, mm);
843 }
844
845 static inline void arch_exit_mmap(struct mm_struct *mm)
846 {
847         PVOP_VCALL1(exit_mmap, mm);
848 }
849
850 static inline void __flush_tlb(void)
851 {
852         PVOP_VCALL0(flush_tlb_user);
853 }
854 static inline void __flush_tlb_global(void)
855 {
856         PVOP_VCALL0(flush_tlb_kernel);
857 }
858 static inline void __flush_tlb_single(unsigned long addr)
859 {
860         PVOP_VCALL1(flush_tlb_single, addr);
861 }
862
863 static inline void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
864                                     unsigned long va)
865 {
866         PVOP_VCALL3(flush_tlb_others, &cpumask, mm, va);
867 }
868
869 static inline void paravirt_alloc_pt(unsigned pfn)
870 {
871         PVOP_VCALL1(alloc_pt, pfn);
872 }
873 static inline void paravirt_release_pt(unsigned pfn)
874 {
875         PVOP_VCALL1(release_pt, pfn);
876 }
877
878 static inline void paravirt_alloc_pd(unsigned pfn)
879 {
880         PVOP_VCALL1(alloc_pd, pfn);
881 }
882
883 static inline void paravirt_alloc_pd_clone(unsigned pfn, unsigned clonepfn,
884                                            unsigned start, unsigned count)
885 {
886         PVOP_VCALL4(alloc_pd_clone, pfn, clonepfn, start, count);
887 }
888 static inline void paravirt_release_pd(unsigned pfn)
889 {
890         PVOP_VCALL1(release_pd, pfn);
891 }
892
893 #ifdef CONFIG_HIGHPTE
894 static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
895 {
896         unsigned long ret;
897         ret = PVOP_CALL2(unsigned long, kmap_atomic_pte, page, type);
898         return (void *)ret;
899 }
900 #endif
901
902 static inline void pte_update(struct mm_struct *mm, unsigned long addr,
903                               pte_t *ptep)
904 {
905         PVOP_VCALL3(pte_update, mm, addr, ptep);
906 }
907
908 static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
909                                     pte_t *ptep)
910 {
911         PVOP_VCALL3(pte_update_defer, mm, addr, ptep);
912 }
913
914 #ifdef CONFIG_X86_PAE
915 static inline pte_t __pte(unsigned long long val)
916 {
917         unsigned long long ret = PVOP_CALL2(unsigned long long, make_pte,
918                                             val, val >> 32);
919         return (pte_t) { ret, ret >> 32 };
920 }
921
922 static inline pmd_t __pmd(unsigned long long val)
923 {
924         return (pmd_t) { PVOP_CALL2(unsigned long long, make_pmd, val, val >> 32) };
925 }
926
927 static inline pgd_t __pgd(unsigned long long val)
928 {
929         return (pgd_t) { PVOP_CALL2(unsigned long long, make_pgd, val, val >> 32) };
930 }
931
932 static inline unsigned long long pte_val(pte_t x)
933 {
934         return PVOP_CALL2(unsigned long long, pte_val, x.pte_low, x.pte_high);
935 }
936
937 static inline unsigned long long pmd_val(pmd_t x)
938 {
939         return PVOP_CALL2(unsigned long long, pmd_val, x.pmd, x.pmd >> 32);
940 }
941
942 static inline unsigned long long pgd_val(pgd_t x)
943 {
944         return PVOP_CALL2(unsigned long long, pgd_val, x.pgd, x.pgd >> 32);
945 }
946
947 static inline void set_pte(pte_t *ptep, pte_t pteval)
948 {
949         PVOP_VCALL3(set_pte, ptep, pteval.pte_low, pteval.pte_high);
950 }
951
952 static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
953                               pte_t *ptep, pte_t pteval)
954 {
955         /* 5 arg words */
956         paravirt_ops.set_pte_at(mm, addr, ptep, pteval);
957 }
958
959 static inline void set_pte_atomic(pte_t *ptep, pte_t pteval)
960 {
961         PVOP_VCALL3(set_pte_atomic, ptep, pteval.pte_low, pteval.pte_high);
962 }
963
964 static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
965                                    pte_t *ptep, pte_t pte)
966 {
967         /* 5 arg words */
968         paravirt_ops.set_pte_present(mm, addr, ptep, pte);
969 }
970
971 static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
972 {
973         PVOP_VCALL3(set_pmd, pmdp, pmdval.pmd, pmdval.pmd >> 32);
974 }
975
976 static inline void set_pud(pud_t *pudp, pud_t pudval)
977 {
978         PVOP_VCALL3(set_pud, pudp, pudval.pgd.pgd, pudval.pgd.pgd >> 32);
979 }
980
981 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
982 {
983         PVOP_VCALL3(pte_clear, mm, addr, ptep);
984 }
985
986 static inline void pmd_clear(pmd_t *pmdp)
987 {
988         PVOP_VCALL1(pmd_clear, pmdp);
989 }
990
991 static inline pte_t raw_ptep_get_and_clear(pte_t *p)
992 {
993         unsigned long long val = PVOP_CALL1(unsigned long long, ptep_get_and_clear, p);
994         return (pte_t) { val, val >> 32 };
995 }
996 #else  /* !CONFIG_X86_PAE */
997 static inline pte_t __pte(unsigned long val)
998 {
999         return (pte_t) { PVOP_CALL1(unsigned long, make_pte, val) };
1000 }
1001
1002 static inline pgd_t __pgd(unsigned long val)
1003 {
1004         return (pgd_t) { PVOP_CALL1(unsigned long, make_pgd, val) };
1005 }
1006
1007 static inline unsigned long pte_val(pte_t x)
1008 {
1009         return PVOP_CALL1(unsigned long, pte_val, x.pte_low);
1010 }
1011
1012 static inline unsigned long pgd_val(pgd_t x)
1013 {
1014         return PVOP_CALL1(unsigned long, pgd_val, x.pgd);
1015 }
1016
1017 static inline void set_pte(pte_t *ptep, pte_t pteval)
1018 {
1019         PVOP_VCALL2(set_pte, ptep, pteval.pte_low);
1020 }
1021
1022 static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1023                               pte_t *ptep, pte_t pteval)
1024 {
1025         PVOP_VCALL4(set_pte_at, mm, addr, ptep, pteval.pte_low);
1026 }
1027
1028 static inline void set_pmd(pmd_t *pmdp, pmd_t pmdval)
1029 {
1030         PVOP_VCALL2(set_pmd, pmdp, pmdval.pud.pgd.pgd);
1031 }
1032
1033 static inline pte_t raw_ptep_get_and_clear(pte_t *p)
1034 {
1035         return (pte_t) { PVOP_CALL1(unsigned long, ptep_get_and_clear, p) };
1036 }
1037 #endif  /* CONFIG_X86_PAE */
1038
1039 /* Lazy mode for batching updates / context switch */
1040 #define PARAVIRT_LAZY_NONE 0
1041 #define PARAVIRT_LAZY_MMU  1
1042 #define PARAVIRT_LAZY_CPU  2
1043 #define PARAVIRT_LAZY_FLUSH 3
1044
1045 #define  __HAVE_ARCH_ENTER_LAZY_CPU_MODE
1046 static inline void arch_enter_lazy_cpu_mode(void)
1047 {
1048         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_CPU);
1049 }
1050
1051 static inline void arch_leave_lazy_cpu_mode(void)
1052 {
1053         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_NONE);
1054 }
1055
1056 static inline void arch_flush_lazy_cpu_mode(void)
1057 {
1058         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_FLUSH);
1059 }
1060
1061
1062 #define  __HAVE_ARCH_ENTER_LAZY_MMU_MODE
1063 static inline void arch_enter_lazy_mmu_mode(void)
1064 {
1065         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_MMU);
1066 }
1067
1068 static inline void arch_leave_lazy_mmu_mode(void)
1069 {
1070         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_NONE);
1071 }
1072
1073 static inline void arch_flush_lazy_mmu_mode(void)
1074 {
1075         PVOP_VCALL1(set_lazy_mode, PARAVIRT_LAZY_FLUSH);
1076 }
1077
1078 void _paravirt_nop(void);
1079 #define paravirt_nop    ((void *)_paravirt_nop)
1080
1081 /* These all sit in the .parainstructions section to tell us what to patch. */
1082 struct paravirt_patch_site {
1083         u8 *instr;              /* original instructions */
1084         u8 instrtype;           /* type of this instruction */
1085         u8 len;                 /* length of original instruction */
1086         u16 clobbers;           /* what registers you may clobber */
1087 };
1088
1089 extern struct paravirt_patch_site __parainstructions[],
1090         __parainstructions_end[];
1091
1092 static inline unsigned long __raw_local_save_flags(void)
1093 {
1094         unsigned long f;
1095
1096         asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
1097                                   PARAVIRT_CALL
1098                                   "popl %%edx; popl %%ecx")
1099                      : "=a"(f)
1100                      : paravirt_type(save_fl),
1101                        paravirt_clobber(CLBR_EAX)
1102                      : "memory", "cc");
1103         return f;
1104 }
1105
1106 static inline void raw_local_irq_restore(unsigned long f)
1107 {
1108         asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
1109                                   PARAVIRT_CALL
1110                                   "popl %%edx; popl %%ecx")
1111                      : "=a"(f)
1112                      : "0"(f),
1113                        paravirt_type(restore_fl),
1114                        paravirt_clobber(CLBR_EAX)
1115                      : "memory", "cc");
1116 }
1117
1118 static inline void raw_local_irq_disable(void)
1119 {
1120         asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
1121                                   PARAVIRT_CALL
1122                                   "popl %%edx; popl %%ecx")
1123                      :
1124                      : paravirt_type(irq_disable),
1125                        paravirt_clobber(CLBR_EAX)
1126                      : "memory", "eax", "cc");
1127 }
1128
1129 static inline void raw_local_irq_enable(void)
1130 {
1131         asm volatile(paravirt_alt("pushl %%ecx; pushl %%edx;"
1132                                   PARAVIRT_CALL
1133                                   "popl %%edx; popl %%ecx")
1134                      :
1135                      : paravirt_type(irq_enable),
1136                        paravirt_clobber(CLBR_EAX)
1137                      : "memory", "eax", "cc");
1138 }
1139
1140 static inline unsigned long __raw_local_irq_save(void)
1141 {
1142         unsigned long f;
1143
1144         f = __raw_local_save_flags();
1145         raw_local_irq_disable();
1146         return f;
1147 }
1148
1149 #define CLI_STRING                                                      \
1150         _paravirt_alt("pushl %%ecx; pushl %%edx;"                       \
1151                       "call *paravirt_ops+%c[paravirt_cli_type]*4;"     \
1152                       "popl %%edx; popl %%ecx",                         \
1153                       "%c[paravirt_cli_type]", "%c[paravirt_clobber]")
1154
1155 #define STI_STRING                                                      \
1156         _paravirt_alt("pushl %%ecx; pushl %%edx;"                       \
1157                       "call *paravirt_ops+%c[paravirt_sti_type]*4;"     \
1158                       "popl %%edx; popl %%ecx",                         \
1159                       "%c[paravirt_sti_type]", "%c[paravirt_clobber]")
1160
1161 #define CLI_STI_CLOBBERS , "%eax"
1162 #define CLI_STI_INPUT_ARGS                                              \
1163         ,                                                               \
1164         [paravirt_cli_type] "i" (PARAVIRT_PATCH(irq_disable)),          \
1165         [paravirt_sti_type] "i" (PARAVIRT_PATCH(irq_enable)),           \
1166         paravirt_clobber(CLBR_EAX)
1167
1168 /* Make sure as little as possible of this mess escapes. */
1169 #undef PARAVIRT_CALL
1170 #undef PVOP_VCALL0
1171 #undef PVOP_CALL0
1172 #undef PVOP_VCALL1
1173 #undef PVOP_CALL1
1174 #undef PVOP_VCALL2
1175 #undef PVOP_CALL2
1176 #undef PVOP_VCALL3
1177 #undef PVOP_CALL3
1178 #undef PVOP_VCALL4
1179 #undef PVOP_CALL4
1180
1181 #else  /* __ASSEMBLY__ */
1182
1183 #define PARA_PATCH(off) ((off) / 4)
1184
1185 #define PARA_SITE(ptype, clobbers, ops)         \
1186 771:;                                           \
1187         ops;                                    \
1188 772:;                                           \
1189         .pushsection .parainstructions,"a";     \
1190          .long 771b;                            \
1191          .byte ptype;                           \
1192          .byte 772b-771b;                       \
1193          .short clobbers;                       \
1194         .popsection
1195
1196 #define INTERRUPT_RETURN                                        \
1197         PARA_SITE(PARA_PATCH(PARAVIRT_iret), CLBR_NONE,         \
1198                   jmp *%cs:paravirt_ops+PARAVIRT_iret)
1199
1200 #define DISABLE_INTERRUPTS(clobbers)                                    \
1201         PARA_SITE(PARA_PATCH(PARAVIRT_irq_disable), clobbers,           \
1202                   pushl %eax; pushl %ecx; pushl %edx;                   \
1203                   call *%cs:paravirt_ops+PARAVIRT_irq_disable;          \
1204                   popl %edx; popl %ecx; popl %eax)                      \
1205
1206 #define ENABLE_INTERRUPTS(clobbers)                                     \
1207         PARA_SITE(PARA_PATCH(PARAVIRT_irq_enable), clobbers,            \
1208                   pushl %eax; pushl %ecx; pushl %edx;                   \
1209                   call *%cs:paravirt_ops+PARAVIRT_irq_enable;           \
1210                   popl %edx; popl %ecx; popl %eax)
1211
1212 #define ENABLE_INTERRUPTS_SYSEXIT                                       \
1213         PARA_SITE(PARA_PATCH(PARAVIRT_irq_enable_sysexit), CLBR_NONE,   \
1214                   jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
1215
1216 #define GET_CR0_INTO_EAX                        \
1217         push %ecx; push %edx;                   \
1218         call *paravirt_ops+PARAVIRT_read_cr0;   \
1219         pop %edx; pop %ecx
1220
1221 #endif /* __ASSEMBLY__ */
1222 #endif /* CONFIG_PARAVIRT */
1223 #endif  /* __ASM_PARAVIRT_H */