[PATCH] paravirt: Patch inline replacements for paravirt intercepts
[linux-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 #include <linux/linkage.h>
6 #include <linux/stringify.h>
7
8 #ifdef CONFIG_PARAVIRT
9 /* These are the most performance critical ops, so we want to be able to patch
10  * callers */
11 #define PARAVIRT_IRQ_DISABLE 0
12 #define PARAVIRT_IRQ_ENABLE 1
13 #define PARAVIRT_RESTORE_FLAGS 2
14 #define PARAVIRT_SAVE_FLAGS 3
15 #define PARAVIRT_SAVE_FLAGS_IRQ_DISABLE 4
16 #define PARAVIRT_INTERRUPT_RETURN 5
17 #define PARAVIRT_STI_SYSEXIT 6
18
19 /* Bitmask of what can be clobbered: usually at least eax. */
20 #define CLBR_NONE 0x0
21 #define CLBR_EAX 0x1
22 #define CLBR_ECX 0x2
23 #define CLBR_EDX 0x4
24 #define CLBR_ANY 0x7
25
26 #ifndef __ASSEMBLY__
27 struct thread_struct;
28 struct Xgt_desc_struct;
29 struct tss_struct;
30 struct paravirt_ops
31 {
32         unsigned int kernel_rpl;
33         int paravirt_enabled;
34         const char *name;
35
36         /*
37          * Patch may replace one of the defined code sequences with arbitrary
38          * code, subject to the same register constraints.  This generally
39          * means the code is not free to clobber any registers other than EAX.
40          * The patch function should return the number of bytes of code
41          * generated, as we nop pad the rest in generic code.
42          */
43         unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
44
45         void (*arch_setup)(void);
46         char *(*memory_setup)(void);
47         void (*init_IRQ)(void);
48
49         void (*banner)(void);
50
51         unsigned long (*get_wallclock)(void);
52         int (*set_wallclock)(unsigned long);
53         void (*time_init)(void);
54
55         /* All the function pointers here are declared as "fastcall"
56            so that we get a specific register-based calling
57            convention.  This makes it easier to implement inline
58            assembler replacements. */
59
60         void (fastcall *cpuid)(unsigned int *eax, unsigned int *ebx,
61                       unsigned int *ecx, unsigned int *edx);
62
63         unsigned long (fastcall *get_debugreg)(int regno);
64         void (fastcall *set_debugreg)(int regno, unsigned long value);
65
66         void (fastcall *clts)(void);
67
68         unsigned long (fastcall *read_cr0)(void);
69         void (fastcall *write_cr0)(unsigned long);
70
71         unsigned long (fastcall *read_cr2)(void);
72         void (fastcall *write_cr2)(unsigned long);
73
74         unsigned long (fastcall *read_cr3)(void);
75         void (fastcall *write_cr3)(unsigned long);
76
77         unsigned long (fastcall *read_cr4_safe)(void);
78         unsigned long (fastcall *read_cr4)(void);
79         void (fastcall *write_cr4)(unsigned long);
80
81         unsigned long (fastcall *save_fl)(void);
82         void (fastcall *restore_fl)(unsigned long);
83         void (fastcall *irq_disable)(void);
84         void (fastcall *irq_enable)(void);
85         void (fastcall *safe_halt)(void);
86         void (fastcall *halt)(void);
87         void (fastcall *wbinvd)(void);
88
89         /* err = 0/-EFAULT.  wrmsr returns 0/-EFAULT. */
90         u64 (fastcall *read_msr)(unsigned int msr, int *err);
91         int (fastcall *write_msr)(unsigned int msr, u64 val);
92
93         u64 (fastcall *read_tsc)(void);
94         u64 (fastcall *read_pmc)(void);
95
96         void (fastcall *load_tr_desc)(void);
97         void (fastcall *load_gdt)(const struct Xgt_desc_struct *);
98         void (fastcall *load_idt)(const struct Xgt_desc_struct *);
99         void (fastcall *store_gdt)(struct Xgt_desc_struct *);
100         void (fastcall *store_idt)(struct Xgt_desc_struct *);
101         void (fastcall *set_ldt)(const void *desc, unsigned entries);
102         unsigned long (fastcall *store_tr)(void);
103         void (fastcall *load_tls)(struct thread_struct *t, unsigned int cpu);
104         void (fastcall *write_ldt_entry)(void *dt, int entrynum,
105                                          u32 low, u32 high);
106         void (fastcall *write_gdt_entry)(void *dt, int entrynum,
107                                          u32 low, u32 high);
108         void (fastcall *write_idt_entry)(void *dt, int entrynum,
109                                          u32 low, u32 high);
110         void (fastcall *load_esp0)(struct tss_struct *tss,
111                                    struct thread_struct *thread);
112
113         void (fastcall *set_iopl_mask)(unsigned mask);
114
115         void (fastcall *io_delay)(void);
116         void (*const_udelay)(unsigned long loops);
117
118         /* These two are jmp to, not actually called. */
119         void (fastcall *irq_enable_sysexit)(void);
120         void (fastcall *iret)(void);
121 };
122
123 extern struct paravirt_ops paravirt_ops;
124
125 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
126
127 static inline void load_esp0(struct tss_struct *tss,
128                              struct thread_struct *thread)
129 {
130         paravirt_ops.load_esp0(tss, thread);
131 }
132
133 #define ARCH_SETUP                      paravirt_ops.arch_setup();
134 static inline unsigned long get_wallclock(void)
135 {
136         return paravirt_ops.get_wallclock();
137 }
138
139 static inline int set_wallclock(unsigned long nowtime)
140 {
141         return paravirt_ops.set_wallclock(nowtime);
142 }
143
144 static inline void do_time_init(void)
145 {
146         return paravirt_ops.time_init();
147 }
148
149 /* The paravirtualized CPUID instruction. */
150 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
151                            unsigned int *ecx, unsigned int *edx)
152 {
153         paravirt_ops.cpuid(eax, ebx, ecx, edx);
154 }
155
156 /*
157  * These special macros can be used to get or set a debugging register
158  */
159 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
160 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
161
162 #define clts() paravirt_ops.clts()
163
164 #define read_cr0() paravirt_ops.read_cr0()
165 #define write_cr0(x) paravirt_ops.write_cr0(x)
166
167 #define read_cr2() paravirt_ops.read_cr2()
168 #define write_cr2(x) paravirt_ops.write_cr2(x)
169
170 #define read_cr3() paravirt_ops.read_cr3()
171 #define write_cr3(x) paravirt_ops.write_cr3(x)
172
173 #define read_cr4() paravirt_ops.read_cr4()
174 #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
175 #define write_cr4(x) paravirt_ops.write_cr4(x)
176
177 static inline void raw_safe_halt(void)
178 {
179         paravirt_ops.safe_halt();
180 }
181
182 static inline void halt(void)
183 {
184         paravirt_ops.safe_halt();
185 }
186 #define wbinvd() paravirt_ops.wbinvd()
187
188 #define get_kernel_rpl()  (paravirt_ops.kernel_rpl)
189
190 #define rdmsr(msr,val1,val2) do {                               \
191         int _err;                                               \
192         u64 _l = paravirt_ops.read_msr(msr,&_err);              \
193         val1 = (u32)_l;                                         \
194         val2 = _l >> 32;                                        \
195 } while(0)
196
197 #define wrmsr(msr,val1,val2) do {                               \
198         u64 _l = ((u64)(val2) << 32) | (val1);                  \
199         paravirt_ops.write_msr((msr), _l);                      \
200 } while(0)
201
202 #define rdmsrl(msr,val) do {                                    \
203         int _err;                                               \
204         val = paravirt_ops.read_msr((msr),&_err);               \
205 } while(0)
206
207 #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
208 #define wrmsr_safe(msr,a,b) ({                                  \
209         u64 _l = ((u64)(b) << 32) | (a);                        \
210         paravirt_ops.write_msr((msr),_l);                       \
211 })
212
213 /* rdmsr with exception handling */
214 #define rdmsr_safe(msr,a,b) ({                                  \
215         int _err;                                               \
216         u64 _l = paravirt_ops.read_msr(msr,&_err);              \
217         (*a) = (u32)_l;                                         \
218         (*b) = _l >> 32;                                        \
219         _err; })
220
221 #define rdtsc(low,high) do {                                    \
222         u64 _l = paravirt_ops.read_tsc();                       \
223         low = (u32)_l;                                          \
224         high = _l >> 32;                                        \
225 } while(0)
226
227 #define rdtscl(low) do {                                        \
228         u64 _l = paravirt_ops.read_tsc();                       \
229         low = (int)_l;                                          \
230 } while(0)
231
232 #define rdtscll(val) (val = paravirt_ops.read_tsc())
233
234 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
235
236 #define rdpmc(counter,low,high) do {                            \
237         u64 _l = paravirt_ops.read_pmc();                       \
238         low = (u32)_l;                                          \
239         high = _l >> 32;                                        \
240 } while(0)
241
242 #define load_TR_desc() (paravirt_ops.load_tr_desc())
243 #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
244 #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
245 #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
246 #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
247 #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
248 #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
249 #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
250 #define write_ldt_entry(dt, entry, low, high)                           \
251         (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
252 #define write_gdt_entry(dt, entry, low, high)                           \
253         (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
254 #define write_idt_entry(dt, entry, low, high)                           \
255         (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
256 #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
257
258 /* The paravirtualized I/O functions */
259 static inline void slow_down_io(void) {
260         paravirt_ops.io_delay();
261 #ifdef REALLY_SLOW_IO
262         paravirt_ops.io_delay();
263         paravirt_ops.io_delay();
264         paravirt_ops.io_delay();
265 #endif
266 }
267
268 /* These all sit in the .parainstructions section to tell us what to patch. */
269 struct paravirt_patch {
270         u8 *instr;              /* original instructions */
271         u8 instrtype;           /* type of this instruction */
272         u8 len;                 /* length of original instruction */
273         u16 clobbers;           /* what registers you may clobber */
274 };
275
276 #define paravirt_alt(insn_string, typenum, clobber)     \
277         "771:\n\t" insn_string "\n" "772:\n"            \
278         ".pushsection .parainstructions,\"a\"\n"        \
279         "  .long 771b\n"                                \
280         "  .byte " __stringify(typenum) "\n"            \
281         "  .byte 772b-771b\n"                           \
282         "  .short " __stringify(clobber) "\n"           \
283         ".popsection"
284
285 static inline unsigned long __raw_local_save_flags(void)
286 {
287         unsigned long f;
288
289         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
290                                            "call *%1;"
291                                            "popl %%edx; popl %%ecx",
292                                           PARAVIRT_SAVE_FLAGS, CLBR_NONE)
293                              : "=a"(f): "m"(paravirt_ops.save_fl)
294                              : "memory", "cc");
295         return f;
296 }
297
298 static inline void raw_local_irq_restore(unsigned long f)
299 {
300         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
301                                            "call *%1;"
302                                            "popl %%edx; popl %%ecx",
303                                           PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
304                              : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
305                              : "memory", "cc");
306 }
307
308 static inline void raw_local_irq_disable(void)
309 {
310         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
311                                            "call *%0;"
312                                            "popl %%edx; popl %%ecx",
313                                           PARAVIRT_IRQ_DISABLE, CLBR_EAX)
314                              : : "m" (paravirt_ops.irq_disable)
315                              : "memory", "eax", "cc");
316 }
317
318 static inline void raw_local_irq_enable(void)
319 {
320         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
321                                            "call *%0;"
322                                            "popl %%edx; popl %%ecx",
323                                           PARAVIRT_IRQ_ENABLE, CLBR_EAX)
324                              : : "m" (paravirt_ops.irq_enable)
325                              : "memory", "eax", "cc");
326 }
327
328 static inline unsigned long __raw_local_irq_save(void)
329 {
330         unsigned long f;
331
332         __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
333                                            "call *%1; pushl %%eax;"
334                                            "call *%2; popl %%eax;"
335                                            "popl %%edx; popl %%ecx",
336                                           PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
337                                           CLBR_NONE)
338                              : "=a"(f)
339                              : "m" (paravirt_ops.save_fl),
340                                "m" (paravirt_ops.irq_disable)
341                              : "memory", "cc");
342         return f;
343 }
344
345 #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;"             \
346                      "call *paravirt_ops+%c[irq_disable];"              \
347                      "popl %%edx; popl %%ecx",                          \
348                      PARAVIRT_IRQ_DISABLE, CLBR_EAX)
349
350 #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;"             \
351                      "call *paravirt_ops+%c[irq_enable];"               \
352                      "popl %%edx; popl %%ecx",                          \
353                      PARAVIRT_IRQ_ENABLE, CLBR_EAX)
354 #define CLI_STI_CLOBBERS , "%eax"
355 #define CLI_STI_INPUT_ARGS \
356         ,                                                               \
357         [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
358         [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
359
360 #else  /* __ASSEMBLY__ */
361
362 #define PARA_PATCH(ptype, clobbers, ops)        \
363 771:;                                           \
364         ops;                                    \
365 772:;                                           \
366         .pushsection .parainstructions,"a";     \
367          .long 771b;                            \
368          .byte ptype;                           \
369          .byte 772b-771b;                       \
370          .short clobbers;                       \
371         .popsection
372
373 #define INTERRUPT_RETURN                                \
374         PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
375         jmp *%cs:paravirt_ops+PARAVIRT_iret)
376
377 #define DISABLE_INTERRUPTS(clobbers)                    \
378         PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers,      \
379         pushl %ecx; pushl %edx;                         \
380         call *paravirt_ops+PARAVIRT_irq_disable;        \
381         popl %edx; popl %ecx)                           \
382
383 #define ENABLE_INTERRUPTS(clobbers)                     \
384         PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers,       \
385         pushl %ecx; pushl %edx;                         \
386         call *%cs:paravirt_ops+PARAVIRT_irq_enable;     \
387         popl %edx; popl %ecx)
388
389 #define ENABLE_INTERRUPTS_SYSEXIT                       \
390         PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY,      \
391         jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
392
393 #define GET_CR0_INTO_EAX                        \
394         call *paravirt_ops+PARAVIRT_read_cr0
395
396 #endif /* __ASSEMBLY__ */
397 #endif /* CONFIG_PARAVIRT */
398 #endif  /* __ASM_PARAVIRT_H */