[PATCH] paravirt: Add startup infrastructure for paravirtualization
[linux-2.6/zen-sources.git] / include / asm-i386 / paravirt.h
blobdd707d8c8270fdea82bc40bbbd5cc6d7bbdc5ef4
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>
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
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
26 #ifndef __ASSEMBLY__
27 struct thread_struct;
28 struct Xgt_desc_struct;
29 struct tss_struct;
30 struct paravirt_ops
32 unsigned int kernel_rpl;
33 int paravirt_enabled;
34 const char *name;
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.
43 unsigned (*patch)(u8 type, u16 clobber, void *firstinsn, unsigned len);
45 void (*arch_setup)(void);
46 char *(*memory_setup)(void);
47 void (*init_IRQ)(void);
49 void (*banner)(void);
51 unsigned long (*get_wallclock)(void);
52 int (*set_wallclock)(unsigned long);
53 void (*time_init)(void);
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. */
60 void (fastcall *cpuid)(unsigned int *eax, unsigned int *ebx,
61 unsigned int *ecx, unsigned int *edx);
63 unsigned long (fastcall *get_debugreg)(int regno);
64 void (fastcall *set_debugreg)(int regno, unsigned long value);
66 void (fastcall *clts)(void);
68 unsigned long (fastcall *read_cr0)(void);
69 void (fastcall *write_cr0)(unsigned long);
71 unsigned long (fastcall *read_cr2)(void);
72 void (fastcall *write_cr2)(unsigned long);
74 unsigned long (fastcall *read_cr3)(void);
75 void (fastcall *write_cr3)(unsigned long);
77 unsigned long (fastcall *read_cr4_safe)(void);
78 unsigned long (fastcall *read_cr4)(void);
79 void (fastcall *write_cr4)(unsigned long);
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);
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);
93 u64 (fastcall *read_tsc)(void);
94 u64 (fastcall *read_pmc)(void);
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);
113 void (fastcall *set_iopl_mask)(unsigned mask);
115 void (fastcall *io_delay)(void);
116 void (*const_udelay)(unsigned long loops);
118 /* These two are jmp to, not actually called. */
119 void (fastcall *irq_enable_sysexit)(void);
120 void (fastcall *iret)(void);
123 /* Mark a paravirt probe function. */
124 #define paravirt_probe(fn) \
125 static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
126 __attribute__((__section__(".paravirtprobe"))) = fn
128 extern struct paravirt_ops paravirt_ops;
130 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
132 static inline void load_esp0(struct tss_struct *tss,
133 struct thread_struct *thread)
135 paravirt_ops.load_esp0(tss, thread);
138 #define ARCH_SETUP paravirt_ops.arch_setup();
139 static inline unsigned long get_wallclock(void)
141 return paravirt_ops.get_wallclock();
144 static inline int set_wallclock(unsigned long nowtime)
146 return paravirt_ops.set_wallclock(nowtime);
149 static inline void do_time_init(void)
151 return paravirt_ops.time_init();
154 /* The paravirtualized CPUID instruction. */
155 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
156 unsigned int *ecx, unsigned int *edx)
158 paravirt_ops.cpuid(eax, ebx, ecx, edx);
162 * These special macros can be used to get or set a debugging register
164 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
165 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
167 #define clts() paravirt_ops.clts()
169 #define read_cr0() paravirt_ops.read_cr0()
170 #define write_cr0(x) paravirt_ops.write_cr0(x)
172 #define read_cr2() paravirt_ops.read_cr2()
173 #define write_cr2(x) paravirt_ops.write_cr2(x)
175 #define read_cr3() paravirt_ops.read_cr3()
176 #define write_cr3(x) paravirt_ops.write_cr3(x)
178 #define read_cr4() paravirt_ops.read_cr4()
179 #define read_cr4_safe(x) paravirt_ops.read_cr4_safe()
180 #define write_cr4(x) paravirt_ops.write_cr4(x)
182 static inline void raw_safe_halt(void)
184 paravirt_ops.safe_halt();
187 static inline void halt(void)
189 paravirt_ops.safe_halt();
191 #define wbinvd() paravirt_ops.wbinvd()
193 #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
195 #define rdmsr(msr,val1,val2) do { \
196 int _err; \
197 u64 _l = paravirt_ops.read_msr(msr,&_err); \
198 val1 = (u32)_l; \
199 val2 = _l >> 32; \
200 } while(0)
202 #define wrmsr(msr,val1,val2) do { \
203 u64 _l = ((u64)(val2) << 32) | (val1); \
204 paravirt_ops.write_msr((msr), _l); \
205 } while(0)
207 #define rdmsrl(msr,val) do { \
208 int _err; \
209 val = paravirt_ops.read_msr((msr),&_err); \
210 } while(0)
212 #define wrmsrl(msr,val) (paravirt_ops.write_msr((msr),(val)))
213 #define wrmsr_safe(msr,a,b) ({ \
214 u64 _l = ((u64)(b) << 32) | (a); \
215 paravirt_ops.write_msr((msr),_l); \
218 /* rdmsr with exception handling */
219 #define rdmsr_safe(msr,a,b) ({ \
220 int _err; \
221 u64 _l = paravirt_ops.read_msr(msr,&_err); \
222 (*a) = (u32)_l; \
223 (*b) = _l >> 32; \
224 _err; })
226 #define rdtsc(low,high) do { \
227 u64 _l = paravirt_ops.read_tsc(); \
228 low = (u32)_l; \
229 high = _l >> 32; \
230 } while(0)
232 #define rdtscl(low) do { \
233 u64 _l = paravirt_ops.read_tsc(); \
234 low = (int)_l; \
235 } while(0)
237 #define rdtscll(val) (val = paravirt_ops.read_tsc())
239 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
241 #define rdpmc(counter,low,high) do { \
242 u64 _l = paravirt_ops.read_pmc(); \
243 low = (u32)_l; \
244 high = _l >> 32; \
245 } while(0)
247 #define load_TR_desc() (paravirt_ops.load_tr_desc())
248 #define load_gdt(dtr) (paravirt_ops.load_gdt(dtr))
249 #define load_idt(dtr) (paravirt_ops.load_idt(dtr))
250 #define set_ldt(addr, entries) (paravirt_ops.set_ldt((addr), (entries)))
251 #define store_gdt(dtr) (paravirt_ops.store_gdt(dtr))
252 #define store_idt(dtr) (paravirt_ops.store_idt(dtr))
253 #define store_tr(tr) ((tr) = paravirt_ops.store_tr())
254 #define load_TLS(t,cpu) (paravirt_ops.load_tls((t),(cpu)))
255 #define write_ldt_entry(dt, entry, low, high) \
256 (paravirt_ops.write_ldt_entry((dt), (entry), (low), (high)))
257 #define write_gdt_entry(dt, entry, low, high) \
258 (paravirt_ops.write_gdt_entry((dt), (entry), (low), (high)))
259 #define write_idt_entry(dt, entry, low, high) \
260 (paravirt_ops.write_idt_entry((dt), (entry), (low), (high)))
261 #define set_iopl_mask(mask) (paravirt_ops.set_iopl_mask(mask))
263 /* The paravirtualized I/O functions */
264 static inline void slow_down_io(void) {
265 paravirt_ops.io_delay();
266 #ifdef REALLY_SLOW_IO
267 paravirt_ops.io_delay();
268 paravirt_ops.io_delay();
269 paravirt_ops.io_delay();
270 #endif
273 /* These all sit in the .parainstructions section to tell us what to patch. */
274 struct paravirt_patch {
275 u8 *instr; /* original instructions */
276 u8 instrtype; /* type of this instruction */
277 u8 len; /* length of original instruction */
278 u16 clobbers; /* what registers you may clobber */
281 #define paravirt_alt(insn_string, typenum, clobber) \
282 "771:\n\t" insn_string "\n" "772:\n" \
283 ".pushsection .parainstructions,\"a\"\n" \
284 " .long 771b\n" \
285 " .byte " __stringify(typenum) "\n" \
286 " .byte 772b-771b\n" \
287 " .short " __stringify(clobber) "\n" \
288 ".popsection"
290 static inline unsigned long __raw_local_save_flags(void)
292 unsigned long f;
294 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
295 "call *%1;"
296 "popl %%edx; popl %%ecx",
297 PARAVIRT_SAVE_FLAGS, CLBR_NONE)
298 : "=a"(f): "m"(paravirt_ops.save_fl)
299 : "memory", "cc");
300 return f;
303 static inline void raw_local_irq_restore(unsigned long f)
305 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
306 "call *%1;"
307 "popl %%edx; popl %%ecx",
308 PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
309 : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
310 : "memory", "cc");
313 static inline void raw_local_irq_disable(void)
315 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
316 "call *%0;"
317 "popl %%edx; popl %%ecx",
318 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
319 : : "m" (paravirt_ops.irq_disable)
320 : "memory", "eax", "cc");
323 static inline void raw_local_irq_enable(void)
325 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
326 "call *%0;"
327 "popl %%edx; popl %%ecx",
328 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
329 : : "m" (paravirt_ops.irq_enable)
330 : "memory", "eax", "cc");
333 static inline unsigned long __raw_local_irq_save(void)
335 unsigned long f;
337 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
338 "call *%1; pushl %%eax;"
339 "call *%2; popl %%eax;"
340 "popl %%edx; popl %%ecx",
341 PARAVIRT_SAVE_FLAGS_IRQ_DISABLE,
342 CLBR_NONE)
343 : "=a"(f)
344 : "m" (paravirt_ops.save_fl),
345 "m" (paravirt_ops.irq_disable)
346 : "memory", "cc");
347 return f;
350 #define CLI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
351 "call *paravirt_ops+%c[irq_disable];" \
352 "popl %%edx; popl %%ecx", \
353 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
355 #define STI_STRING paravirt_alt("pushl %%ecx; pushl %%edx;" \
356 "call *paravirt_ops+%c[irq_enable];" \
357 "popl %%edx; popl %%ecx", \
358 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
359 #define CLI_STI_CLOBBERS , "%eax"
360 #define CLI_STI_INPUT_ARGS \
362 [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
363 [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
365 #else /* __ASSEMBLY__ */
367 #define PARA_PATCH(ptype, clobbers, ops) \
368 771:; \
369 ops; \
370 772:; \
371 .pushsection .parainstructions,"a"; \
372 .long 771b; \
373 .byte ptype; \
374 .byte 772b-771b; \
375 .short clobbers; \
376 .popsection
378 #define INTERRUPT_RETURN \
379 PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
380 jmp *%cs:paravirt_ops+PARAVIRT_iret)
382 #define DISABLE_INTERRUPTS(clobbers) \
383 PARA_PATCH(PARAVIRT_IRQ_DISABLE, clobbers, \
384 pushl %ecx; pushl %edx; \
385 call *paravirt_ops+PARAVIRT_irq_disable; \
386 popl %edx; popl %ecx) \
388 #define ENABLE_INTERRUPTS(clobbers) \
389 PARA_PATCH(PARAVIRT_IRQ_ENABLE, clobbers, \
390 pushl %ecx; pushl %edx; \
391 call *%cs:paravirt_ops+PARAVIRT_irq_enable; \
392 popl %edx; popl %ecx)
394 #define ENABLE_INTERRUPTS_SYSEXIT \
395 PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
396 jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
398 #define GET_CR0_INTO_EAX \
399 call *paravirt_ops+PARAVIRT_read_cr0
401 #endif /* __ASSEMBLY__ */
402 #endif /* CONFIG_PARAVIRT */
403 #endif /* __ASM_PARAVIRT_H */