paravirt.h 29 KB

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