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>
9 /* These are the most performance critical ops, so we want to be able to patch
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. */
28 struct Xgt_desc_struct;
32 unsigned int kernel_rpl;
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);
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,
106 void (fastcall *write_gdt_entry)(void *dt, int entrynum,
108 void (fastcall *write_idt_entry)(void *dt, int entrynum,
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 extern struct paravirt_ops paravirt_ops;
125 #define paravirt_enabled() (paravirt_ops.paravirt_enabled)
127 static inline void load_esp0(struct tss_struct *tss,
128 struct thread_struct *thread)
130 paravirt_ops.load_esp0(tss, thread);
133 #define ARCH_SETUP paravirt_ops.arch_setup();
134 static inline unsigned long get_wallclock(void)
136 return paravirt_ops.get_wallclock();
139 static inline int set_wallclock(unsigned long nowtime)
141 return paravirt_ops.set_wallclock(nowtime);
144 static inline void do_time_init(void)
146 return paravirt_ops.time_init();
149 /* The paravirtualized CPUID instruction. */
150 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
151 unsigned int *ecx, unsigned int *edx)
153 paravirt_ops.cpuid(eax, ebx, ecx, edx);
157 * These special macros can be used to get or set a debugging register
159 #define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
160 #define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
162 #define clts() paravirt_ops.clts()
164 #define read_cr0() paravirt_ops.read_cr0()
165 #define write_cr0(x) paravirt_ops.write_cr0(x)
167 #define read_cr2() paravirt_ops.read_cr2()
168 #define write_cr2(x) paravirt_ops.write_cr2(x)
170 #define read_cr3() paravirt_ops.read_cr3()
171 #define write_cr3(x) paravirt_ops.write_cr3(x)
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)
177 static inline void raw_safe_halt(void)
179 paravirt_ops.safe_halt();
182 static inline void halt(void)
184 paravirt_ops.safe_halt();
186 #define wbinvd() paravirt_ops.wbinvd()
188 #define get_kernel_rpl() (paravirt_ops.kernel_rpl)
190 #define rdmsr(msr,val1,val2) do { \
192 u64 _l = paravirt_ops.read_msr(msr,&_err); \
197 #define wrmsr(msr,val1,val2) do { \
198 u64 _l = ((u64)(val2) << 32) | (val1); \
199 paravirt_ops.write_msr((msr), _l); \
202 #define rdmsrl(msr,val) do { \
204 val = paravirt_ops.read_msr((msr),&_err); \
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); \
213 /* rdmsr with exception handling */
214 #define rdmsr_safe(msr,a,b) ({ \
216 u64 _l = paravirt_ops.read_msr(msr,&_err); \
221 #define rdtsc(low,high) do { \
222 u64 _l = paravirt_ops.read_tsc(); \
227 #define rdtscl(low) do { \
228 u64 _l = paravirt_ops.read_tsc(); \
232 #define rdtscll(val) (val = paravirt_ops.read_tsc())
234 #define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
236 #define rdpmc(counter,low,high) do { \
237 u64 _l = paravirt_ops.read_pmc(); \
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))
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();
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 */
276 #define paravirt_alt(insn_string, typenum, clobber) \
277 "771:\n\t" insn_string "\n" "772:\n" \
278 ".pushsection .parainstructions,\"a\"\n" \
280 " .byte " __stringify(typenum) "\n" \
281 " .byte 772b-771b\n" \
282 " .short " __stringify(clobber) "\n" \
285 static inline unsigned long __raw_local_save_flags(void)
289 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
291 "popl %%edx; popl %%ecx",
292 PARAVIRT_SAVE_FLAGS, CLBR_NONE)
293 : "=a"(f): "m"(paravirt_ops.save_fl)
298 static inline void raw_local_irq_restore(unsigned long f)
300 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
302 "popl %%edx; popl %%ecx",
303 PARAVIRT_RESTORE_FLAGS, CLBR_EAX)
304 : "=a"(f) : "m" (paravirt_ops.restore_fl), "0"(f)
308 static inline void raw_local_irq_disable(void)
310 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
312 "popl %%edx; popl %%ecx",
313 PARAVIRT_IRQ_DISABLE, CLBR_EAX)
314 : : "m" (paravirt_ops.irq_disable)
315 : "memory", "eax", "cc");
318 static inline void raw_local_irq_enable(void)
320 __asm__ __volatile__(paravirt_alt( "pushl %%ecx; pushl %%edx;"
322 "popl %%edx; popl %%ecx",
323 PARAVIRT_IRQ_ENABLE, CLBR_EAX)
324 : : "m" (paravirt_ops.irq_enable)
325 : "memory", "eax", "cc");
328 static inline unsigned long __raw_local_irq_save(void)
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,
339 : "m" (paravirt_ops.save_fl),
340 "m" (paravirt_ops.irq_disable)
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)
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 \
357 [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
358 [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
360 #else /* __ASSEMBLY__ */
362 #define PARA_PATCH(ptype, clobbers, ops) \
366 .pushsection .parainstructions,"a"; \
373 #define INTERRUPT_RETURN \
374 PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
375 jmp *%cs:paravirt_ops+PARAVIRT_iret)
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) \
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)
389 #define ENABLE_INTERRUPTS_SYSEXIT \
390 PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
391 jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
393 #define GET_CR0_INTO_EAX \
394 call *paravirt_ops+PARAVIRT_read_cr0
396 #endif /* __ASSEMBLY__ */
397 #endif /* CONFIG_PARAVIRT */
398 #endif /* __ASM_PARAVIRT_H */