paravirt.h 29 KB

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