blob: dd707d8c8270fdea82bc40bbbd5cc6d7bbdc5ef4 [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. */
5#include <linux/linkage.h>
Rusty Russell139ec7c2006-12-07 02:14:08 +01006#include <linux/stringify.h>
Rusty Russelld3561b72006-12-07 02:14:07 +01007
8#ifdef CONFIG_PARAVIRT
Rusty Russell139ec7c2006-12-07 02:14:08 +01009/* 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
Rusty Russelld3561b72006-12-07 02:14:07 +010026#ifndef __ASSEMBLY__
27struct thread_struct;
28struct Xgt_desc_struct;
29struct tss_struct;
30struct paravirt_ops
31{
32 unsigned int kernel_rpl;
33 int paravirt_enabled;
34 const char *name;
35
Rusty Russell139ec7c2006-12-07 02:14:08 +010036 /*
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
Rusty Russelld3561b72006-12-07 02:14:07 +010045 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
Rusty Russellc9ccf302006-12-07 02:14:08 +0100123/* Mark a paravirt probe function. */
124#define paravirt_probe(fn) \
125 static asmlinkage void (*__paravirtprobe_##fn)(void) __attribute_used__ \
126 __attribute__((__section__(".paravirtprobe"))) = fn
127
Rusty Russelld3561b72006-12-07 02:14:07 +0100128extern struct paravirt_ops paravirt_ops;
129
130#define paravirt_enabled() (paravirt_ops.paravirt_enabled)
131
132static inline void load_esp0(struct tss_struct *tss,
133 struct thread_struct *thread)
134{
135 paravirt_ops.load_esp0(tss, thread);
136}
137
138#define ARCH_SETUP paravirt_ops.arch_setup();
139static inline unsigned long get_wallclock(void)
140{
141 return paravirt_ops.get_wallclock();
142}
143
144static inline int set_wallclock(unsigned long nowtime)
145{
146 return paravirt_ops.set_wallclock(nowtime);
147}
148
149static inline void do_time_init(void)
150{
151 return paravirt_ops.time_init();
152}
153
154/* The paravirtualized CPUID instruction. */
155static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
156 unsigned int *ecx, unsigned int *edx)
157{
158 paravirt_ops.cpuid(eax, ebx, ecx, edx);
159}
160
161/*
162 * These special macros can be used to get or set a debugging register
163 */
164#define get_debugreg(var, reg) var = paravirt_ops.get_debugreg(reg)
165#define set_debugreg(val, reg) paravirt_ops.set_debugreg(reg, val)
166
167#define clts() paravirt_ops.clts()
168
169#define read_cr0() paravirt_ops.read_cr0()
170#define write_cr0(x) paravirt_ops.write_cr0(x)
171
172#define read_cr2() paravirt_ops.read_cr2()
173#define write_cr2(x) paravirt_ops.write_cr2(x)
174
175#define read_cr3() paravirt_ops.read_cr3()
176#define write_cr3(x) paravirt_ops.write_cr3(x)
177
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)
181
Rusty Russelld3561b72006-12-07 02:14:07 +0100182static inline void raw_safe_halt(void)
183{
184 paravirt_ops.safe_halt();
185}
186
187static inline void halt(void)
188{
189 paravirt_ops.safe_halt();
190}
191#define wbinvd() paravirt_ops.wbinvd()
192
193#define get_kernel_rpl() (paravirt_ops.kernel_rpl)
194
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)
201
202#define wrmsr(msr,val1,val2) do { \
203 u64 _l = ((u64)(val2) << 32) | (val1); \
204 paravirt_ops.write_msr((msr), _l); \
205} while(0)
206
207#define rdmsrl(msr,val) do { \
208 int _err; \
209 val = paravirt_ops.read_msr((msr),&_err); \
210} while(0)
211
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); \
216})
217
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; })
225
226#define rdtsc(low,high) do { \
227 u64 _l = paravirt_ops.read_tsc(); \
228 low = (u32)_l; \
229 high = _l >> 32; \
230} while(0)
231
232#define rdtscl(low) do { \
233 u64 _l = paravirt_ops.read_tsc(); \
234 low = (int)_l; \
235} while(0)
236
237#define rdtscll(val) (val = paravirt_ops.read_tsc())
238
239#define write_tsc(val1,val2) wrmsr(0x10, val1, val2)
240
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)
246
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))
262
263/* The paravirtualized I/O functions */
264static 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
271}
272
Rusty Russell139ec7c2006-12-07 02:14:08 +0100273/* These all sit in the .parainstructions section to tell us what to patch. */
274struct 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 */
279};
280
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"
289
290static inline unsigned long __raw_local_save_flags(void)
291{
292 unsigned long f;
293
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;
301}
302
303static inline void raw_local_irq_restore(unsigned long f)
304{
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");
311}
312
313static inline void raw_local_irq_disable(void)
314{
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");
321}
322
323static inline void raw_local_irq_enable(void)
324{
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");
331}
332
333static inline unsigned long __raw_local_irq_save(void)
334{
335 unsigned long f;
336
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;
348}
349
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)
354
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 \
361 , \
362 [irq_disable] "i" (offsetof(struct paravirt_ops, irq_disable)), \
363 [irq_enable] "i" (offsetof(struct paravirt_ops, irq_enable))
364
Rusty Russelld3561b72006-12-07 02:14:07 +0100365#else /* __ASSEMBLY__ */
366
Rusty Russell139ec7c2006-12-07 02:14:08 +0100367#define PARA_PATCH(ptype, clobbers, ops) \
368771:; \
369 ops; \
370772:; \
371 .pushsection .parainstructions,"a"; \
372 .long 771b; \
373 .byte ptype; \
374 .byte 772b-771b; \
375 .short clobbers; \
376 .popsection
377
378#define INTERRUPT_RETURN \
379 PARA_PATCH(PARAVIRT_INTERRUPT_RETURN, CLBR_ANY, \
380 jmp *%cs:paravirt_ops+PARAVIRT_iret)
381
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) \
387
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)
393
394#define ENABLE_INTERRUPTS_SYSEXIT \
395 PARA_PATCH(PARAVIRT_STI_SYSEXIT, CLBR_ANY, \
396 jmp *%cs:paravirt_ops+PARAVIRT_irq_enable_sysexit)
397
398#define GET_CR0_INTO_EAX \
399 call *paravirt_ops+PARAVIRT_read_cr0
400
Rusty Russelld3561b72006-12-07 02:14:07 +0100401#endif /* __ASSEMBLY__ */
402#endif /* CONFIG_PARAVIRT */
403#endif /* __ASM_PARAVIRT_H */