paravirt.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. #include <asm/asm.h>
  8. /* Bitmask of what can be clobbered: usually at least eax. */
  9. #define CLBR_NONE 0
  10. #define CLBR_EAX (1 << 0)
  11. #define CLBR_ECX (1 << 1)
  12. #define CLBR_EDX (1 << 2)
  13. #ifdef CONFIG_X86_64
  14. #define CLBR_RSI (1 << 3)
  15. #define CLBR_RDI (1 << 4)
  16. #define CLBR_R8 (1 << 5)
  17. #define CLBR_R9 (1 << 6)
  18. #define CLBR_R10 (1 << 7)
  19. #define CLBR_R11 (1 << 8)
  20. #define CLBR_ANY ((1 << 9) - 1)
  21. #include <asm/desc_defs.h>
  22. #else
  23. /* CLBR_ANY should match all regs platform has. For i386, that's just it */
  24. #define CLBR_ANY ((1 << 3) - 1)
  25. #endif /* X86_64 */
  26. #ifndef __ASSEMBLY__
  27. #include <linux/types.h>
  28. #include <linux/cpumask.h>
  29. #include <asm/kmap_types.h>
  30. #include <asm/desc_defs.h>
  31. struct page;
  32. struct thread_struct;
  33. struct desc_ptr;
  34. struct tss_struct;
  35. struct mm_struct;
  36. struct desc_struct;
  37. /* general info */
  38. struct pv_info {
  39. unsigned int kernel_rpl;
  40. int shared_kernel_pmd;
  41. int paravirt_enabled;
  42. const char *name;
  43. };
  44. struct pv_init_ops {
  45. /*
  46. * Patch may replace one of the defined code sequences with
  47. * arbitrary code, subject to the same register constraints.
  48. * This generally means the code is not free to clobber any
  49. * registers other than EAX. The patch function should return
  50. * the number of bytes of code generated, as we nop pad the
  51. * rest in generic code.
  52. */
  53. unsigned (*patch)(u8 type, u16 clobber, void *insnbuf,
  54. unsigned long addr, unsigned len);
  55. /* Basic arch-specific setup */
  56. void (*arch_setup)(void);
  57. char *(*memory_setup)(void);
  58. void (*post_allocator_init)(void);
  59. /* Print a banner to identify the environment */
  60. void (*banner)(void);
  61. };
  62. struct pv_lazy_ops {
  63. /* Set deferred update mode, used for batching operations. */
  64. void (*enter)(void);
  65. void (*leave)(void);
  66. };
  67. struct pv_time_ops {
  68. void (*time_init)(void);
  69. /* Set and set time of day */
  70. unsigned long (*get_wallclock)(void);
  71. int (*set_wallclock)(unsigned long);
  72. unsigned long long (*sched_clock)(void);
  73. unsigned long (*get_cpu_khz)(void);
  74. };
  75. struct pv_cpu_ops {
  76. /* hooks for various privileged instructions */
  77. unsigned long (*get_debugreg)(int regno);
  78. void (*set_debugreg)(int regno, unsigned long value);
  79. void (*clts)(void);
  80. unsigned long (*read_cr0)(void);
  81. void (*write_cr0)(unsigned long);
  82. unsigned long (*read_cr4_safe)(void);
  83. unsigned long (*read_cr4)(void);
  84. void (*write_cr4)(unsigned long);
  85. #ifdef CONFIG_X86_64
  86. unsigned long (*read_cr8)(void);
  87. void (*write_cr8)(unsigned long);
  88. #endif
  89. /* Segment descriptor handling */
  90. void (*load_tr_desc)(void);
  91. void (*load_gdt)(const struct desc_ptr *);
  92. void (*load_idt)(const struct desc_ptr *);
  93. void (*store_gdt)(struct desc_ptr *);
  94. void (*store_idt)(struct desc_ptr *);
  95. void (*set_ldt)(const void *desc, unsigned entries);
  96. unsigned long (*store_tr)(void);
  97. void (*load_tls)(struct thread_struct *t, unsigned int cpu);
  98. void (*write_ldt_entry)(struct desc_struct *ldt, int entrynum,
  99. const void *desc);
  100. void (*write_gdt_entry)(struct desc_struct *,
  101. int entrynum, const void *desc, int size);
  102. void (*write_idt_entry)(gate_desc *,
  103. int entrynum, const gate_desc *gate);
  104. void (*load_sp0)(struct tss_struct *tss, struct thread_struct *t);
  105. void (*set_iopl_mask)(unsigned mask);
  106. void (*wbinvd)(void);
  107. void (*io_delay)(void);
  108. /* cpuid emulation, mostly so that caps bits can be disabled */
  109. void (*cpuid)(unsigned int *eax, unsigned int *ebx,
  110. unsigned int *ecx, unsigned int *edx);
  111. /* MSR, PMC and TSR operations.
  112. err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
  113. u64 (*read_msr)(unsigned int msr, int *err);
  114. int (*write_msr)(unsigned int msr, unsigned low, unsigned high);
  115. u64 (*read_tsc)(void);
  116. u64 (*read_pmc)(int counter);
  117. unsigned long long (*read_tscp)(unsigned int *aux);
  118. /* These two are jmp to, not actually called. */
  119. void (*irq_enable_syscall_ret)(void);
  120. void (*iret)(void);
  121. void (*swapgs)(void);
  122. struct pv_lazy_ops lazy_mode;
  123. };
  124. struct pv_irq_ops {
  125. void (*init_IRQ)(void);
  126. /*
  127. * Get/set interrupt state. save_fl and restore_fl are only
  128. * expected to use X86_EFLAGS_IF; all other bits
  129. * returned from save_fl are undefined, and may be ignored by
  130. * restore_fl.
  131. */
  132. unsigned long (*save_fl)(void);
  133. void (*restore_fl)(unsigned long);
  134. void (*irq_disable)(void);
  135. void (*irq_enable)(void);
  136. void (*safe_halt)(void);
  137. void (*halt)(void);
  138. };
  139. struct pv_apic_ops {
  140. #ifdef CONFIG_X86_LOCAL_APIC
  141. /*
  142. * Direct APIC operations, principally for VMI. Ideally
  143. * these shouldn't be in this interface.
  144. */
  145. void (*apic_write)(unsigned long reg, u32 v);
  146. void (*apic_write_atomic)(unsigned long reg, u32 v);
  147. u32 (*apic_read)(unsigned long reg);
  148. void (*setup_boot_clock)(void);
  149. void (*setup_secondary_clock)(void);
  150. void (*startup_ipi_hook)(int phys_apicid,
  151. unsigned long start_eip,
  152. unsigned long start_esp);
  153. #endif
  154. };
  155. struct pv_mmu_ops {
  156. /*
  157. * Called before/after init_mm pagetable setup. setup_start
  158. * may reset %cr3, and may pre-install parts of the pagetable;
  159. * pagetable setup is expected to preserve any existing
  160. * mapping.
  161. */
  162. void (*pagetable_setup_start)(pgd_t *pgd_base);
  163. void (*pagetable_setup_done)(pgd_t *pgd_base);
  164. unsigned long (*read_cr2)(void);
  165. void (*write_cr2)(unsigned long);
  166. unsigned long (*read_cr3)(void);
  167. void (*write_cr3)(unsigned long);
  168. /*
  169. * Hooks for intercepting the creation/use/destruction of an
  170. * mm_struct.
  171. */
  172. void (*activate_mm)(struct mm_struct *prev,
  173. struct mm_struct *next);
  174. void (*dup_mmap)(struct mm_struct *oldmm,
  175. struct mm_struct *mm);
  176. void (*exit_mmap)(struct mm_struct *mm);
  177. /* TLB operations */
  178. void (*flush_tlb_user)(void);
  179. void (*flush_tlb_kernel)(void);
  180. void (*flush_tlb_single)(unsigned long addr);
  181. void (*flush_tlb_others)(const cpumask_t *cpus, struct mm_struct *mm,
  182. unsigned long va);
  183. /* Hooks for allocating/releasing pagetable pages */
  184. void (*alloc_pt)(struct mm_struct *mm, u32 pfn);
  185. void (*alloc_pd)(struct mm_struct *mm, u32 pfn);
  186. void (*alloc_pd_clone)(u32 pfn, u32 clonepfn, u32 start, u32 count);
  187. void (*release_pt)(u32 pfn);
  188. void (*release_pd)(u32 pfn);
  189. /* Pagetable manipulation functions */
  190. void (*set_pte)(pte_t *ptep, pte_t pteval);
  191. void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
  192. pte_t *ptep, pte_t pteval);
  193. void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
  194. void (*pte_update)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  195. void (*pte_update_defer)(struct mm_struct *mm,
  196. unsigned long addr, pte_t *ptep);
  197. pteval_t (*pte_val)(pte_t);
  198. pte_t (*make_pte)(pteval_t pte);
  199. pgdval_t (*pgd_val)(pgd_t);
  200. pgd_t (*make_pgd)(pgdval_t pgd);
  201. #if PAGETABLE_LEVELS >= 3
  202. #ifdef CONFIG_X86_PAE
  203. void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
  204. void (*set_pte_present)(struct mm_struct *mm, unsigned long addr,
  205. pte_t *ptep, pte_t pte);
  206. void (*pte_clear)(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
  207. void (*pmd_clear)(pmd_t *pmdp);
  208. #endif /* CONFIG_X86_PAE */
  209. void (*set_pud)(pud_t *pudp, pud_t pudval);
  210. pmdval_t (*pmd_val)(pmd_t);
  211. pmd_t (*make_pmd)(pmdval_t pmd);
  212. #if PAGETABLE_LEVELS == 4
  213. pudval_t (*pud_val)(pud_t);
  214. pud_t (*make_pud)(pudval_t pud);
  215. void (*set_pgd)(pgd_t *pudp, pgd_t pgdval);
  216. #endif /* PAGETABLE_LEVELS == 4 */
  217. #endif /* PAGETABLE_LEVELS >= 3 */
  218. #ifdef CONFIG_HIGHPTE
  219. void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
  220. #endif
  221. struct pv_lazy_ops lazy_mode;
  222. };
  223. /* This contains all the paravirt structures: we get a convenient
  224. * number for each function using the offset which we use to indicate
  225. * what to patch. */
  226. struct paravirt_patch_template
  227. {
  228. struct pv_init_ops pv_init_ops;
  229. struct pv_time_ops pv_time_ops;
  230. struct pv_cpu_ops pv_cpu_ops;
  231. struct pv_irq_ops pv_irq_ops;
  232. struct pv_apic_ops pv_apic_ops;
  233. struct pv_mmu_ops pv_mmu_ops;
  234. };
  235. extern struct pv_info pv_info;
  236. extern struct pv_init_ops pv_init_ops;
  237. extern struct pv_time_ops pv_time_ops;
  238. extern struct pv_cpu_ops pv_cpu_ops;
  239. extern struct pv_irq_ops pv_irq_ops;
  240. extern struct pv_apic_ops pv_apic_ops;
  241. extern struct pv_mmu_ops pv_mmu_ops;
  242. #define PARAVIRT_PATCH(x) \
  243. (offsetof(struct paravirt_patch_template, x) / sizeof(void *))
  244. #define paravirt_type(op) \
  245. [paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \
  246. [paravirt_opptr] "m" (op)
  247. #define paravirt_clobber(clobber) \
  248. [paravirt_clobber] "i" (clobber)
  249. /*
  250. * Generate some code, and mark it as patchable by the
  251. * apply_paravirt() alternate instruction patcher.
  252. */
  253. #define _paravirt_alt(insn_string, type, clobber) \
  254. "771:\n\t" insn_string "\n" "772:\n" \
  255. ".pushsection .parainstructions,\"a\"\n" \
  256. _ASM_ALIGN "\n" \
  257. _ASM_PTR " 771b\n" \
  258. " .byte " type "\n" \
  259. " .byte 772b-771b\n" \
  260. " .short " clobber "\n" \
  261. ".popsection\n"
  262. /* Generate patchable code, with the default asm parameters. */
  263. #define paravirt_alt(insn_string) \
  264. _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
  265. /* Simple instruction patching code. */
  266. #define DEF_NATIVE(ops, name, code) \
  267. extern const char start_##ops##_##name[], end_##ops##_##name[]; \
  268. asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
  269. unsigned paravirt_patch_nop(void);
  270. unsigned paravirt_patch_ignore(unsigned len);
  271. unsigned paravirt_patch_call(void *insnbuf,
  272. const void *target, u16 tgt_clobbers,
  273. unsigned long addr, u16 site_clobbers,
  274. unsigned len);
  275. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  276. unsigned long addr, unsigned len);
  277. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  278. unsigned long addr, unsigned len);
  279. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  280. const char *start, const char *end);
  281. unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
  282. unsigned long addr, unsigned len);
  283. int paravirt_disable_iospace(void);
  284. /*
  285. * This generates an indirect call based on the operation type number.
  286. * The type number, computed in PARAVIRT_PATCH, is derived from the
  287. * offset into the paravirt_patch_template structure, and can therefore be
  288. * freely converted back into a structure offset.
  289. */
  290. #define PARAVIRT_CALL "call *%[paravirt_opptr];"
  291. /*
  292. * These macros are intended to wrap calls through one of the paravirt
  293. * ops structs, so that they can be later identified and patched at
  294. * runtime.
  295. *
  296. * Normally, a call to a pv_op function is a simple indirect call:
  297. * (pv_op_struct.operations)(args...).
  298. *
  299. * Unfortunately, this is a relatively slow operation for modern CPUs,
  300. * because it cannot necessarily determine what the destination
  301. * address is. In this case, the address is a runtime constant, so at
  302. * the very least we can patch the call to e a simple direct call, or
  303. * ideally, patch an inline implementation into the callsite. (Direct
  304. * calls are essentially free, because the call and return addresses
  305. * are completely predictable.)
  306. *
  307. * For i386, these macros rely on the standard gcc "regparm(3)" calling
  308. * convention, in which the first three arguments are placed in %eax,
  309. * %edx, %ecx (in that order), and the remaining arguments are placed
  310. * on the stack. All caller-save registers (eax,edx,ecx) are expected
  311. * to be modified (either clobbered or used for return values).
  312. * X86_64, on the other hand, already specifies a register-based calling
  313. * conventions, returning at %rax, with parameteres going on %rdi, %rsi,
  314. * %rdx, and %rcx. Note that for this reason, x86_64 does not need any
  315. * special handling for dealing with 4 arguments, unlike i386.
  316. * However, x86_64 also have to clobber all caller saved registers, which
  317. * unfortunately, are quite a bit (r8 - r11)
  318. *
  319. * The call instruction itself is marked by placing its start address
  320. * and size into the .parainstructions section, so that
  321. * apply_paravirt() in arch/i386/kernel/alternative.c can do the
  322. * appropriate patching under the control of the backend pv_init_ops
  323. * implementation.
  324. *
  325. * Unfortunately there's no way to get gcc to generate the args setup
  326. * for the call, and then allow the call itself to be generated by an
  327. * inline asm. Because of this, we must do the complete arg setup and
  328. * return value handling from within these macros. This is fairly
  329. * cumbersome.
  330. *
  331. * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
  332. * It could be extended to more arguments, but there would be little
  333. * to be gained from that. For each number of arguments, there are
  334. * the two VCALL and CALL variants for void and non-void functions.
  335. *
  336. * When there is a return value, the invoker of the macro must specify
  337. * the return type. The macro then uses sizeof() on that type to
  338. * determine whether its a 32 or 64 bit value, and places the return
  339. * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
  340. * 64-bit). For x86_64 machines, it just returns at %rax regardless of
  341. * the return value size.
  342. *
  343. * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
  344. * i386 also passes 64-bit arguments as a pair of adjacent 32-bit arguments
  345. * in low,high order
  346. *
  347. * Small structures are passed and returned in registers. The macro
  348. * calling convention can't directly deal with this, so the wrapper
  349. * functions must do this.
  350. *
  351. * These PVOP_* macros are only defined within this header. This
  352. * means that all uses must be wrapped in inline functions. This also
  353. * makes sure the incoming and outgoing types are always correct.
  354. */
  355. #ifdef CONFIG_X86_32
  356. #define PVOP_VCALL_ARGS unsigned long __eax, __edx, __ecx
  357. #define PVOP_CALL_ARGS PVOP_VCALL_ARGS
  358. #define PVOP_VCALL_CLOBBERS "=a" (__eax), "=d" (__edx), \
  359. "=c" (__ecx)
  360. #define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS
  361. #define EXTRA_CLOBBERS
  362. #define VEXTRA_CLOBBERS
  363. #else
  364. #define PVOP_VCALL_ARGS unsigned long __edi, __esi, __edx, __ecx
  365. #define PVOP_CALL_ARGS PVOP_VCALL_ARGS, __eax
  366. #define PVOP_VCALL_CLOBBERS "=D" (__edi), \
  367. "=S" (__esi), "=d" (__edx), \
  368. "=c" (__ecx)
  369. #define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS, "=a" (__eax)
  370. #define EXTRA_CLOBBERS , "r8", "r9", "r10", "r11"
  371. #define VEXTRA_CLOBBERS , "rax", "r8", "r9", "r10", "r11"
  372. #endif
  373. #define __PVOP_CALL(rettype, op, pre, post, ...) \
  374. ({ \
  375. rettype __ret; \
  376. PVOP_CALL_ARGS; \
  377. /* This is 32-bit specific, but is okay in 64-bit */ \
  378. /* since this condition will never hold */ \
  379. if (sizeof(rettype) > sizeof(unsigned long)) { \
  380. asm volatile(pre \
  381. paravirt_alt(PARAVIRT_CALL) \
  382. post \
  383. : PVOP_CALL_CLOBBERS \
  384. : paravirt_type(op), \
  385. paravirt_clobber(CLBR_ANY), \
  386. ##__VA_ARGS__ \
  387. : "memory", "cc" EXTRA_CLOBBERS); \
  388. __ret = (rettype)((((u64)__edx) << 32) | __eax); \
  389. } else { \
  390. asm volatile(pre \
  391. paravirt_alt(PARAVIRT_CALL) \
  392. post \
  393. : PVOP_CALL_CLOBBERS \
  394. : paravirt_type(op), \
  395. paravirt_clobber(CLBR_ANY), \
  396. ##__VA_ARGS__ \
  397. : "memory", "cc" EXTRA_CLOBBERS); \
  398. __ret = (rettype)__eax; \
  399. } \
  400. __ret; \
  401. })
  402. #define __PVOP_VCALL(op, pre, post, ...) \
  403. ({ \
  404. PVOP_VCALL_ARGS; \
  405. asm volatile(pre \
  406. paravirt_alt(PARAVIRT_CALL) \
  407. post \
  408. : PVOP_VCALL_CLOBBERS \
  409. : paravirt_type(op), \
  410. paravirt_clobber(CLBR_ANY), \
  411. ##__VA_ARGS__ \
  412. : "memory", "cc" VEXTRA_CLOBBERS); \
  413. })
  414. #define PVOP_CALL0(rettype, op) \
  415. __PVOP_CALL(rettype, op, "", "")
  416. #define PVOP_VCALL0(op) \
  417. __PVOP_VCALL(op, "", "")
  418. #define PVOP_CALL1(rettype, op, arg1) \
  419. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)))
  420. #define PVOP_VCALL1(op, arg1) \
  421. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)))
  422. #define PVOP_CALL2(rettype, op, arg1, arg2) \
  423. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  424. "1" ((unsigned long)(arg2)))
  425. #define PVOP_VCALL2(op, arg1, arg2) \
  426. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  427. "1" ((unsigned long)(arg2)))
  428. #define PVOP_CALL3(rettype, op, arg1, arg2, arg3) \
  429. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  430. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
  431. #define PVOP_VCALL3(op, arg1, arg2, arg3) \
  432. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  433. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
  434. /* This is the only difference in x86_64. We can make it much simpler */
  435. #ifdef CONFIG_X86_32
  436. #define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
  437. __PVOP_CALL(rettype, op, \
  438. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  439. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  440. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  441. #define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
  442. __PVOP_VCALL(op, \
  443. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  444. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  445. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  446. #else
  447. #define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
  448. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  449. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
  450. "3"((unsigned long)(arg4)))
  451. #define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
  452. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  453. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
  454. "3"((unsigned long)(arg4)))
  455. #endif
  456. static inline int paravirt_enabled(void)
  457. {
  458. return pv_info.paravirt_enabled;
  459. }
  460. static inline void load_sp0(struct tss_struct *tss,
  461. struct thread_struct *thread)
  462. {
  463. PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
  464. }
  465. #define ARCH_SETUP pv_init_ops.arch_setup();
  466. static inline unsigned long get_wallclock(void)
  467. {
  468. return PVOP_CALL0(unsigned long, pv_time_ops.get_wallclock);
  469. }
  470. static inline int set_wallclock(unsigned long nowtime)
  471. {
  472. return PVOP_CALL1(int, pv_time_ops.set_wallclock, nowtime);
  473. }
  474. static inline void (*choose_time_init(void))(void)
  475. {
  476. return pv_time_ops.time_init;
  477. }
  478. /* The paravirtualized CPUID instruction. */
  479. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  480. unsigned int *ecx, unsigned int *edx)
  481. {
  482. PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
  483. }
  484. /*
  485. * These special macros can be used to get or set a debugging register
  486. */
  487. static inline unsigned long paravirt_get_debugreg(int reg)
  488. {
  489. return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
  490. }
  491. #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
  492. static inline void set_debugreg(unsigned long val, int reg)
  493. {
  494. PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
  495. }
  496. static inline void clts(void)
  497. {
  498. PVOP_VCALL0(pv_cpu_ops.clts);
  499. }
  500. static inline unsigned long read_cr0(void)
  501. {
  502. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
  503. }
  504. static inline void write_cr0(unsigned long x)
  505. {
  506. PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
  507. }
  508. static inline unsigned long read_cr2(void)
  509. {
  510. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
  511. }
  512. static inline void write_cr2(unsigned long x)
  513. {
  514. PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
  515. }
  516. static inline unsigned long read_cr3(void)
  517. {
  518. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
  519. }
  520. static inline void write_cr3(unsigned long x)
  521. {
  522. PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
  523. }
  524. static inline unsigned long read_cr4(void)
  525. {
  526. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
  527. }
  528. static inline unsigned long read_cr4_safe(void)
  529. {
  530. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
  531. }
  532. static inline void write_cr4(unsigned long x)
  533. {
  534. PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
  535. }
  536. #ifdef CONFIG_X86_64
  537. static inline unsigned long read_cr8(void)
  538. {
  539. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr8);
  540. }
  541. static inline void write_cr8(unsigned long x)
  542. {
  543. PVOP_VCALL1(pv_cpu_ops.write_cr8, x);
  544. }
  545. #endif
  546. static inline void raw_safe_halt(void)
  547. {
  548. PVOP_VCALL0(pv_irq_ops.safe_halt);
  549. }
  550. static inline void halt(void)
  551. {
  552. PVOP_VCALL0(pv_irq_ops.safe_halt);
  553. }
  554. static inline void wbinvd(void)
  555. {
  556. PVOP_VCALL0(pv_cpu_ops.wbinvd);
  557. }
  558. #define get_kernel_rpl() (pv_info.kernel_rpl)
  559. static inline u64 paravirt_read_msr(unsigned msr, int *err)
  560. {
  561. return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
  562. }
  563. static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
  564. {
  565. return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
  566. }
  567. /* These should all do BUG_ON(_err), but our headers are too tangled. */
  568. #define rdmsr(msr,val1,val2) do { \
  569. int _err; \
  570. u64 _l = paravirt_read_msr(msr, &_err); \
  571. val1 = (u32)_l; \
  572. val2 = _l >> 32; \
  573. } while(0)
  574. #define wrmsr(msr,val1,val2) do { \
  575. paravirt_write_msr(msr, val1, val2); \
  576. } while(0)
  577. #define rdmsrl(msr,val) do { \
  578. int _err; \
  579. val = paravirt_read_msr(msr, &_err); \
  580. } while(0)
  581. #define wrmsrl(msr,val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
  582. #define wrmsr_safe(msr,a,b) paravirt_write_msr(msr, a, b)
  583. /* rdmsr with exception handling */
  584. #define rdmsr_safe(msr,a,b) ({ \
  585. int _err; \
  586. u64 _l = paravirt_read_msr(msr, &_err); \
  587. (*a) = (u32)_l; \
  588. (*b) = _l >> 32; \
  589. _err; })
  590. static inline u64 paravirt_read_tsc(void)
  591. {
  592. return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
  593. }
  594. #define rdtscl(low) do { \
  595. u64 _l = paravirt_read_tsc(); \
  596. low = (int)_l; \
  597. } while(0)
  598. #define rdtscll(val) (val = paravirt_read_tsc())
  599. static inline unsigned long long paravirt_sched_clock(void)
  600. {
  601. return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
  602. }
  603. #define calculate_cpu_khz() (pv_time_ops.get_cpu_khz())
  604. static inline unsigned long long paravirt_read_pmc(int counter)
  605. {
  606. return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
  607. }
  608. #define rdpmc(counter,low,high) do { \
  609. u64 _l = paravirt_read_pmc(counter); \
  610. low = (u32)_l; \
  611. high = _l >> 32; \
  612. } while(0)
  613. static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
  614. {
  615. return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
  616. }
  617. #define rdtscp(low, high, aux) \
  618. do { \
  619. int __aux; \
  620. unsigned long __val = paravirt_rdtscp(&__aux); \
  621. (low) = (u32)__val; \
  622. (high) = (u32)(__val >> 32); \
  623. (aux) = __aux; \
  624. } while (0)
  625. #define rdtscpll(val, aux) \
  626. do { \
  627. unsigned long __aux; \
  628. val = paravirt_rdtscp(&__aux); \
  629. (aux) = __aux; \
  630. } while (0)
  631. static inline void load_TR_desc(void)
  632. {
  633. PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
  634. }
  635. static inline void load_gdt(const struct desc_ptr *dtr)
  636. {
  637. PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
  638. }
  639. static inline void load_idt(const struct desc_ptr *dtr)
  640. {
  641. PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
  642. }
  643. static inline void set_ldt(const void *addr, unsigned entries)
  644. {
  645. PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
  646. }
  647. static inline void store_gdt(struct desc_ptr *dtr)
  648. {
  649. PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
  650. }
  651. static inline void store_idt(struct desc_ptr *dtr)
  652. {
  653. PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
  654. }
  655. static inline unsigned long paravirt_store_tr(void)
  656. {
  657. return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
  658. }
  659. #define store_tr(tr) ((tr) = paravirt_store_tr())
  660. static inline void load_TLS(struct thread_struct *t, unsigned cpu)
  661. {
  662. PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
  663. }
  664. static inline void write_ldt_entry(struct desc_struct *dt, int entry,
  665. const void *desc)
  666. {
  667. PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
  668. }
  669. static inline void write_gdt_entry(struct desc_struct *dt, int entry,
  670. void *desc, int type)
  671. {
  672. PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
  673. }
  674. static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
  675. {
  676. PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
  677. }
  678. static inline void set_iopl_mask(unsigned mask)
  679. {
  680. PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
  681. }
  682. /* The paravirtualized I/O functions */
  683. static inline void slow_down_io(void) {
  684. pv_cpu_ops.io_delay();
  685. #ifdef REALLY_SLOW_IO
  686. pv_cpu_ops.io_delay();
  687. pv_cpu_ops.io_delay();
  688. pv_cpu_ops.io_delay();
  689. #endif
  690. }
  691. #ifdef CONFIG_X86_LOCAL_APIC
  692. /*
  693. * Basic functions accessing APICs.
  694. */
  695. static inline void apic_write(unsigned long reg, u32 v)
  696. {
  697. PVOP_VCALL2(pv_apic_ops.apic_write, reg, v);
  698. }
  699. static inline void apic_write_atomic(unsigned long reg, u32 v)
  700. {
  701. PVOP_VCALL2(pv_apic_ops.apic_write_atomic, reg, v);
  702. }
  703. static inline u32 apic_read(unsigned long reg)
  704. {
  705. return PVOP_CALL1(unsigned long, pv_apic_ops.apic_read, reg);
  706. }
  707. static inline void setup_boot_clock(void)
  708. {
  709. PVOP_VCALL0(pv_apic_ops.setup_boot_clock);
  710. }
  711. static inline void setup_secondary_clock(void)
  712. {
  713. PVOP_VCALL0(pv_apic_ops.setup_secondary_clock);
  714. }
  715. #endif
  716. static inline void paravirt_post_allocator_init(void)
  717. {
  718. if (pv_init_ops.post_allocator_init)
  719. (*pv_init_ops.post_allocator_init)();
  720. }
  721. static inline void paravirt_pagetable_setup_start(pgd_t *base)
  722. {
  723. (*pv_mmu_ops.pagetable_setup_start)(base);
  724. }
  725. static inline void paravirt_pagetable_setup_done(pgd_t *base)
  726. {
  727. (*pv_mmu_ops.pagetable_setup_done)(base);
  728. }
  729. #ifdef CONFIG_SMP
  730. static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  731. unsigned long start_esp)
  732. {
  733. PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
  734. phys_apicid, start_eip, start_esp);
  735. }
  736. #endif
  737. static inline void paravirt_activate_mm(struct mm_struct *prev,
  738. struct mm_struct *next)
  739. {
  740. PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
  741. }
  742. static inline void arch_dup_mmap(struct mm_struct *oldmm,
  743. struct mm_struct *mm)
  744. {
  745. PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
  746. }
  747. static inline void arch_exit_mmap(struct mm_struct *mm)
  748. {
  749. PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
  750. }
  751. static inline void __flush_tlb(void)
  752. {
  753. PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
  754. }
  755. static inline void __flush_tlb_global(void)
  756. {
  757. PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
  758. }
  759. static inline void __flush_tlb_single(unsigned long addr)
  760. {
  761. PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
  762. }
  763. static inline void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
  764. unsigned long va)
  765. {
  766. PVOP_VCALL3(pv_mmu_ops.flush_tlb_others, &cpumask, mm, va);
  767. }
  768. static inline void paravirt_alloc_pt(struct mm_struct *mm, unsigned pfn)
  769. {
  770. PVOP_VCALL2(pv_mmu_ops.alloc_pt, mm, pfn);
  771. }
  772. static inline void paravirt_release_pt(unsigned pfn)
  773. {
  774. PVOP_VCALL1(pv_mmu_ops.release_pt, pfn);
  775. }
  776. static inline void paravirt_alloc_pd(struct mm_struct *mm, unsigned pfn)
  777. {
  778. PVOP_VCALL2(pv_mmu_ops.alloc_pd, mm, pfn);
  779. }
  780. static inline void paravirt_alloc_pd_clone(unsigned pfn, unsigned clonepfn,
  781. unsigned start, unsigned count)
  782. {
  783. PVOP_VCALL4(pv_mmu_ops.alloc_pd_clone, pfn, clonepfn, start, count);
  784. }
  785. static inline void paravirt_release_pd(unsigned pfn)
  786. {
  787. PVOP_VCALL1(pv_mmu_ops.release_pd, pfn);
  788. }
  789. #ifdef CONFIG_HIGHPTE
  790. static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
  791. {
  792. unsigned long ret;
  793. ret = PVOP_CALL2(unsigned long, pv_mmu_ops.kmap_atomic_pte, page, type);
  794. return (void *)ret;
  795. }
  796. #endif
  797. static inline void pte_update(struct mm_struct *mm, unsigned long addr,
  798. pte_t *ptep)
  799. {
  800. PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
  801. }
  802. static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
  803. pte_t *ptep)
  804. {
  805. PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
  806. }
  807. static inline pte_t __pte(pteval_t val)
  808. {
  809. pteval_t ret;
  810. if (sizeof(pteval_t) > sizeof(long))
  811. ret = PVOP_CALL2(pteval_t,
  812. pv_mmu_ops.make_pte,
  813. val, (u64)val >> 32);
  814. else
  815. ret = PVOP_CALL1(pteval_t,
  816. pv_mmu_ops.make_pte,
  817. val);
  818. return (pte_t) { .pte = ret };
  819. }
  820. static inline pteval_t pte_val(pte_t pte)
  821. {
  822. pteval_t ret;
  823. if (sizeof(pteval_t) > sizeof(long))
  824. ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_val,
  825. pte.pte, (u64)pte.pte >> 32);
  826. else
  827. ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_val,
  828. pte.pte);
  829. return ret;
  830. }
  831. static inline pgd_t __pgd(pgdval_t val)
  832. {
  833. pgdval_t ret;
  834. if (sizeof(pgdval_t) > sizeof(long))
  835. ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.make_pgd,
  836. val, (u64)val >> 32);
  837. else
  838. ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.make_pgd,
  839. val);
  840. return (pgd_t) { ret };
  841. }
  842. static inline pgdval_t pgd_val(pgd_t pgd)
  843. {
  844. pgdval_t ret;
  845. if (sizeof(pgdval_t) > sizeof(long))
  846. ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.pgd_val,
  847. pgd.pgd, (u64)pgd.pgd >> 32);
  848. else
  849. ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.pgd_val,
  850. pgd.pgd);
  851. return ret;
  852. }
  853. static inline void set_pte(pte_t *ptep, pte_t pte)
  854. {
  855. if (sizeof(pteval_t) > sizeof(long))
  856. PVOP_VCALL3(pv_mmu_ops.set_pte, ptep,
  857. pte.pte, (u64)pte.pte >> 32);
  858. else
  859. PVOP_VCALL2(pv_mmu_ops.set_pte, ptep,
  860. pte.pte);
  861. }
  862. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  863. pte_t *ptep, pte_t pte)
  864. {
  865. if (sizeof(pteval_t) > sizeof(long))
  866. /* 5 arg words */
  867. pv_mmu_ops.set_pte_at(mm, addr, ptep, pte);
  868. else
  869. PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pte.pte);
  870. }
  871. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  872. {
  873. pmdval_t val = native_pmd_val(pmd);
  874. if (sizeof(pmdval_t) > sizeof(long))
  875. PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
  876. else
  877. PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
  878. }
  879. #if PAGETABLE_LEVELS >= 3
  880. static inline pmd_t __pmd(pmdval_t val)
  881. {
  882. pmdval_t ret;
  883. if (sizeof(pmdval_t) > sizeof(long))
  884. ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.make_pmd,
  885. val, (u64)val >> 32);
  886. else
  887. ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.make_pmd,
  888. val);
  889. return (pmd_t) { ret };
  890. }
  891. static inline pmdval_t pmd_val(pmd_t pmd)
  892. {
  893. pmdval_t ret;
  894. if (sizeof(pmdval_t) > sizeof(long))
  895. ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.pmd_val,
  896. pmd.pmd, (u64)pmd.pmd >> 32);
  897. else
  898. ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.pmd_val,
  899. pmd.pmd);
  900. return ret;
  901. }
  902. static inline void set_pud(pud_t *pudp, pud_t pud)
  903. {
  904. pudval_t val = native_pud_val(pud);
  905. if (sizeof(pudval_t) > sizeof(long))
  906. PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
  907. val, (u64)val >> 32);
  908. else
  909. PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
  910. val);
  911. }
  912. #if PAGETABLE_LEVELS == 4
  913. static inline pud_t __pud(pudval_t val)
  914. {
  915. pudval_t ret;
  916. if (sizeof(pudval_t) > sizeof(long))
  917. ret = PVOP_CALL2(pudval_t, pv_mmu_ops.make_pud,
  918. val, (u64)val >> 32);
  919. else
  920. ret = PVOP_CALL1(pudval_t, pv_mmu_ops.make_pud,
  921. val);
  922. return (pud_t) { ret };
  923. }
  924. static inline pudval_t pud_val(pud_t pud)
  925. {
  926. pudval_t ret;
  927. if (sizeof(pudval_t) > sizeof(long))
  928. ret = PVOP_CALL2(pudval_t, pv_mmu_ops.pud_val,
  929. pud.pud, (u64)pud.pud >> 32);
  930. else
  931. ret = PVOP_CALL1(pudval_t, pv_mmu_ops.pud_val,
  932. pud.pud);
  933. return ret;
  934. }
  935. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  936. {
  937. pgdval_t val = native_pgd_val(pgd);
  938. if (sizeof(pgdval_t) > sizeof(long))
  939. PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
  940. val, (u64)val >> 32);
  941. else
  942. PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
  943. val);
  944. }
  945. static inline void pgd_clear(pgd_t *pgdp)
  946. {
  947. set_pgd(pgdp, __pgd(0));
  948. }
  949. static inline void pud_clear(pud_t *pudp)
  950. {
  951. set_pud(pudp, __pud(0));
  952. }
  953. #endif /* PAGETABLE_LEVELS == 4 */
  954. #endif /* PAGETABLE_LEVELS >= 3 */
  955. #ifdef CONFIG_X86_PAE
  956. /* Special-case pte-setting operations for PAE, which can't update a
  957. 64-bit pte atomically */
  958. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  959. {
  960. PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
  961. pte.pte, pte.pte >> 32);
  962. }
  963. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
  964. pte_t *ptep, pte_t pte)
  965. {
  966. /* 5 arg words */
  967. pv_mmu_ops.set_pte_present(mm, addr, ptep, pte);
  968. }
  969. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  970. pte_t *ptep)
  971. {
  972. PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
  973. }
  974. static inline void pmd_clear(pmd_t *pmdp)
  975. {
  976. PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
  977. }
  978. #else /* !CONFIG_X86_PAE */
  979. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  980. {
  981. set_pte(ptep, pte);
  982. }
  983. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
  984. pte_t *ptep, pte_t pte)
  985. {
  986. set_pte(ptep, pte);
  987. }
  988. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  989. pte_t *ptep)
  990. {
  991. set_pte_at(mm, addr, ptep, __pte(0));
  992. }
  993. static inline void pmd_clear(pmd_t *pmdp)
  994. {
  995. set_pmd(pmdp, __pmd(0));
  996. }
  997. #endif /* CONFIG_X86_PAE */
  998. /* Lazy mode for batching updates / context switch */
  999. enum paravirt_lazy_mode {
  1000. PARAVIRT_LAZY_NONE,
  1001. PARAVIRT_LAZY_MMU,
  1002. PARAVIRT_LAZY_CPU,
  1003. };
  1004. enum paravirt_lazy_mode paravirt_get_lazy_mode(void);
  1005. void paravirt_enter_lazy_cpu(void);
  1006. void paravirt_leave_lazy_cpu(void);
  1007. void paravirt_enter_lazy_mmu(void);
  1008. void paravirt_leave_lazy_mmu(void);
  1009. void paravirt_leave_lazy(enum paravirt_lazy_mode mode);
  1010. #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
  1011. static inline void arch_enter_lazy_cpu_mode(void)
  1012. {
  1013. PVOP_VCALL0(pv_cpu_ops.lazy_mode.enter);
  1014. }
  1015. static inline void arch_leave_lazy_cpu_mode(void)
  1016. {
  1017. PVOP_VCALL0(pv_cpu_ops.lazy_mode.leave);
  1018. }
  1019. static inline void arch_flush_lazy_cpu_mode(void)
  1020. {
  1021. if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)) {
  1022. arch_leave_lazy_cpu_mode();
  1023. arch_enter_lazy_cpu_mode();
  1024. }
  1025. }
  1026. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  1027. static inline void arch_enter_lazy_mmu_mode(void)
  1028. {
  1029. PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
  1030. }
  1031. static inline void arch_leave_lazy_mmu_mode(void)
  1032. {
  1033. PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
  1034. }
  1035. static inline void arch_flush_lazy_mmu_mode(void)
  1036. {
  1037. if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU)) {
  1038. arch_leave_lazy_mmu_mode();
  1039. arch_enter_lazy_mmu_mode();
  1040. }
  1041. }
  1042. void _paravirt_nop(void);
  1043. #define paravirt_nop ((void *)_paravirt_nop)
  1044. /* These all sit in the .parainstructions section to tell us what to patch. */
  1045. struct paravirt_patch_site {
  1046. u8 *instr; /* original instructions */
  1047. u8 instrtype; /* type of this instruction */
  1048. u8 len; /* length of original instruction */
  1049. u16 clobbers; /* what registers you may clobber */
  1050. };
  1051. extern struct paravirt_patch_site __parainstructions[],
  1052. __parainstructions_end[];
  1053. #ifdef CONFIG_X86_32
  1054. #define PV_SAVE_REGS "pushl %%ecx; pushl %%edx;"
  1055. #define PV_RESTORE_REGS "popl %%edx; popl %%ecx"
  1056. #define PV_FLAGS_ARG "0"
  1057. #define PV_EXTRA_CLOBBERS
  1058. #define PV_VEXTRA_CLOBBERS
  1059. #else
  1060. /* We save some registers, but all of them, that's too much. We clobber all
  1061. * caller saved registers but the argument parameter */
  1062. #define PV_SAVE_REGS "pushq %%rdi;"
  1063. #define PV_RESTORE_REGS "popq %%rdi;"
  1064. #define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx"
  1065. #define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx"
  1066. #define PV_FLAGS_ARG "D"
  1067. #endif
  1068. static inline unsigned long __raw_local_save_flags(void)
  1069. {
  1070. unsigned long f;
  1071. asm volatile(paravirt_alt(PV_SAVE_REGS
  1072. PARAVIRT_CALL
  1073. PV_RESTORE_REGS)
  1074. : "=a"(f)
  1075. : paravirt_type(pv_irq_ops.save_fl),
  1076. paravirt_clobber(CLBR_EAX)
  1077. : "memory", "cc" PV_VEXTRA_CLOBBERS);
  1078. return f;
  1079. }
  1080. static inline void raw_local_irq_restore(unsigned long f)
  1081. {
  1082. asm volatile(paravirt_alt(PV_SAVE_REGS
  1083. PARAVIRT_CALL
  1084. PV_RESTORE_REGS)
  1085. : "=a"(f)
  1086. : PV_FLAGS_ARG(f),
  1087. paravirt_type(pv_irq_ops.restore_fl),
  1088. paravirt_clobber(CLBR_EAX)
  1089. : "memory", "cc" PV_EXTRA_CLOBBERS);
  1090. }
  1091. static inline void raw_local_irq_disable(void)
  1092. {
  1093. asm volatile(paravirt_alt(PV_SAVE_REGS
  1094. PARAVIRT_CALL
  1095. PV_RESTORE_REGS)
  1096. :
  1097. : paravirt_type(pv_irq_ops.irq_disable),
  1098. paravirt_clobber(CLBR_EAX)
  1099. : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
  1100. }
  1101. static inline void raw_local_irq_enable(void)
  1102. {
  1103. asm volatile(paravirt_alt(PV_SAVE_REGS
  1104. PARAVIRT_CALL
  1105. PV_RESTORE_REGS)
  1106. :
  1107. : paravirt_type(pv_irq_ops.irq_enable),
  1108. paravirt_clobber(CLBR_EAX)
  1109. : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
  1110. }
  1111. static inline unsigned long __raw_local_irq_save(void)
  1112. {
  1113. unsigned long f;
  1114. f = __raw_local_save_flags();
  1115. raw_local_irq_disable();
  1116. return f;
  1117. }
  1118. /* Make sure as little as possible of this mess escapes. */
  1119. #undef PARAVIRT_CALL
  1120. #undef __PVOP_CALL
  1121. #undef __PVOP_VCALL
  1122. #undef PVOP_VCALL0
  1123. #undef PVOP_CALL0
  1124. #undef PVOP_VCALL1
  1125. #undef PVOP_CALL1
  1126. #undef PVOP_VCALL2
  1127. #undef PVOP_CALL2
  1128. #undef PVOP_VCALL3
  1129. #undef PVOP_CALL3
  1130. #undef PVOP_VCALL4
  1131. #undef PVOP_CALL4
  1132. #else /* __ASSEMBLY__ */
  1133. #define _PVSITE(ptype, clobbers, ops, word, algn) \
  1134. 771:; \
  1135. ops; \
  1136. 772:; \
  1137. .pushsection .parainstructions,"a"; \
  1138. .align algn; \
  1139. word 771b; \
  1140. .byte ptype; \
  1141. .byte 772b-771b; \
  1142. .short clobbers; \
  1143. .popsection
  1144. #ifdef CONFIG_X86_64
  1145. #define PV_SAVE_REGS pushq %rax; pushq %rdi; pushq %rcx; pushq %rdx
  1146. #define PV_RESTORE_REGS popq %rdx; popq %rcx; popq %rdi; popq %rax
  1147. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
  1148. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
  1149. #else
  1150. #define PV_SAVE_REGS pushl %eax; pushl %edi; pushl %ecx; pushl %edx
  1151. #define PV_RESTORE_REGS popl %edx; popl %ecx; popl %edi; popl %eax
  1152. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
  1153. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
  1154. #endif
  1155. #define INTERRUPT_RETURN \
  1156. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
  1157. jmp *%cs:pv_cpu_ops+PV_CPU_iret)
  1158. #define DISABLE_INTERRUPTS(clobbers) \
  1159. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
  1160. PV_SAVE_REGS; \
  1161. call *%cs:pv_irq_ops+PV_IRQ_irq_disable; \
  1162. PV_RESTORE_REGS;) \
  1163. #define ENABLE_INTERRUPTS(clobbers) \
  1164. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
  1165. PV_SAVE_REGS; \
  1166. call *%cs:pv_irq_ops+PV_IRQ_irq_enable; \
  1167. PV_RESTORE_REGS;)
  1168. #define ENABLE_INTERRUPTS_SYSCALL_RET \
  1169. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_syscall_ret),\
  1170. CLBR_NONE, \
  1171. jmp *%cs:pv_cpu_ops+PV_CPU_irq_enable_syscall_ret)
  1172. #ifdef CONFIG_X86_32
  1173. #define GET_CR0_INTO_EAX \
  1174. push %ecx; push %edx; \
  1175. call *pv_cpu_ops+PV_CPU_read_cr0; \
  1176. pop %edx; pop %ecx
  1177. #else
  1178. #define SWAPGS \
  1179. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  1180. PV_SAVE_REGS; \
  1181. call *pv_cpu_ops+PV_CPU_swapgs; \
  1182. PV_RESTORE_REGS \
  1183. )
  1184. #define GET_CR2_INTO_RCX \
  1185. call *pv_mmu_ops+PV_MMU_read_cr2; \
  1186. movq %rax, %rcx; \
  1187. xorq %rax, %rax;
  1188. #endif
  1189. #endif /* __ASSEMBLY__ */
  1190. #endif /* CONFIG_PARAVIRT */
  1191. #endif /* __ASM_PARAVIRT_H */