paravirt.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. #ifndef _ASM_X86_PARAVIRT_H
  2. #define _ASM_X86_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_tsc_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. #ifdef CONFIG_X86_64
  99. void (*load_gs_index)(unsigned int idx);
  100. #endif
  101. void (*write_ldt_entry)(struct desc_struct *ldt, int entrynum,
  102. const void *desc);
  103. void (*write_gdt_entry)(struct desc_struct *,
  104. int entrynum, const void *desc, int size);
  105. void (*write_idt_entry)(gate_desc *,
  106. int entrynum, const gate_desc *gate);
  107. void (*alloc_ldt)(struct desc_struct *ldt, unsigned entries);
  108. void (*free_ldt)(struct desc_struct *ldt, unsigned entries);
  109. void (*load_sp0)(struct tss_struct *tss, struct thread_struct *t);
  110. void (*set_iopl_mask)(unsigned mask);
  111. void (*wbinvd)(void);
  112. void (*io_delay)(void);
  113. /* cpuid emulation, mostly so that caps bits can be disabled */
  114. void (*cpuid)(unsigned int *eax, unsigned int *ebx,
  115. unsigned int *ecx, unsigned int *edx);
  116. /* MSR, PMC and TSR operations.
  117. err = 0/-EFAULT. wrmsr returns 0/-EFAULT. */
  118. u64 (*read_msr_amd)(unsigned int msr, int *err);
  119. u64 (*read_msr)(unsigned int msr, int *err);
  120. int (*write_msr)(unsigned int msr, unsigned low, unsigned high);
  121. u64 (*read_tsc)(void);
  122. u64 (*read_pmc)(int counter);
  123. unsigned long long (*read_tscp)(unsigned int *aux);
  124. /*
  125. * Atomically enable interrupts and return to userspace. This
  126. * is only ever used to return to 32-bit processes; in a
  127. * 64-bit kernel, it's used for 32-on-64 compat processes, but
  128. * never native 64-bit processes. (Jump, not call.)
  129. */
  130. void (*irq_enable_sysexit)(void);
  131. /*
  132. * Switch to usermode gs and return to 64-bit usermode using
  133. * sysret. Only used in 64-bit kernels to return to 64-bit
  134. * processes. Usermode register state, including %rsp, must
  135. * already be restored.
  136. */
  137. void (*usergs_sysret64)(void);
  138. /*
  139. * Switch to usermode gs and return to 32-bit usermode using
  140. * sysret. Used to return to 32-on-64 compat processes.
  141. * Other usermode register state, including %esp, must already
  142. * be restored.
  143. */
  144. void (*usergs_sysret32)(void);
  145. /* Normal iret. Jump to this with the standard iret stack
  146. frame set up. */
  147. void (*iret)(void);
  148. void (*swapgs)(void);
  149. struct pv_lazy_ops lazy_mode;
  150. };
  151. struct pv_irq_ops {
  152. void (*init_IRQ)(void);
  153. /*
  154. * Get/set interrupt state. save_fl and restore_fl are only
  155. * expected to use X86_EFLAGS_IF; all other bits
  156. * returned from save_fl are undefined, and may be ignored by
  157. * restore_fl.
  158. */
  159. unsigned long (*save_fl)(void);
  160. void (*restore_fl)(unsigned long);
  161. void (*irq_disable)(void);
  162. void (*irq_enable)(void);
  163. void (*safe_halt)(void);
  164. void (*halt)(void);
  165. #ifdef CONFIG_X86_64
  166. void (*adjust_exception_frame)(void);
  167. #endif
  168. };
  169. struct pv_apic_ops {
  170. #ifdef CONFIG_X86_LOCAL_APIC
  171. void (*setup_boot_clock)(void);
  172. void (*setup_secondary_clock)(void);
  173. void (*startup_ipi_hook)(int phys_apicid,
  174. unsigned long start_eip,
  175. unsigned long start_esp);
  176. #endif
  177. };
  178. struct pv_mmu_ops {
  179. /*
  180. * Called before/after init_mm pagetable setup. setup_start
  181. * may reset %cr3, and may pre-install parts of the pagetable;
  182. * pagetable setup is expected to preserve any existing
  183. * mapping.
  184. */
  185. void (*pagetable_setup_start)(pgd_t *pgd_base);
  186. void (*pagetable_setup_done)(pgd_t *pgd_base);
  187. unsigned long (*read_cr2)(void);
  188. void (*write_cr2)(unsigned long);
  189. unsigned long (*read_cr3)(void);
  190. void (*write_cr3)(unsigned long);
  191. /*
  192. * Hooks for intercepting the creation/use/destruction of an
  193. * mm_struct.
  194. */
  195. void (*activate_mm)(struct mm_struct *prev,
  196. struct mm_struct *next);
  197. void (*dup_mmap)(struct mm_struct *oldmm,
  198. struct mm_struct *mm);
  199. void (*exit_mmap)(struct mm_struct *mm);
  200. /* TLB operations */
  201. void (*flush_tlb_user)(void);
  202. void (*flush_tlb_kernel)(void);
  203. void (*flush_tlb_single)(unsigned long addr);
  204. void (*flush_tlb_others)(const struct cpumask *cpus,
  205. struct mm_struct *mm,
  206. unsigned long va);
  207. /* Hooks for allocating and freeing a pagetable top-level */
  208. int (*pgd_alloc)(struct mm_struct *mm);
  209. void (*pgd_free)(struct mm_struct *mm, pgd_t *pgd);
  210. /*
  211. * Hooks for allocating/releasing pagetable pages when they're
  212. * attached to a pagetable
  213. */
  214. void (*alloc_pte)(struct mm_struct *mm, unsigned long pfn);
  215. void (*alloc_pmd)(struct mm_struct *mm, unsigned long pfn);
  216. void (*alloc_pmd_clone)(unsigned long pfn, unsigned long clonepfn, unsigned long start, unsigned long count);
  217. void (*alloc_pud)(struct mm_struct *mm, unsigned long pfn);
  218. void (*release_pte)(unsigned long pfn);
  219. void (*release_pmd)(unsigned long pfn);
  220. void (*release_pud)(unsigned long pfn);
  221. /* Pagetable manipulation functions */
  222. void (*set_pte)(pte_t *ptep, pte_t pteval);
  223. void (*set_pte_at)(struct mm_struct *mm, unsigned long addr,
  224. pte_t *ptep, pte_t pteval);
  225. void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
  226. void (*pte_update)(struct mm_struct *mm, unsigned long addr,
  227. pte_t *ptep);
  228. void (*pte_update_defer)(struct mm_struct *mm,
  229. unsigned long addr, pte_t *ptep);
  230. pte_t (*ptep_modify_prot_start)(struct mm_struct *mm, unsigned long addr,
  231. pte_t *ptep);
  232. void (*ptep_modify_prot_commit)(struct mm_struct *mm, unsigned long addr,
  233. pte_t *ptep, pte_t pte);
  234. pteval_t (*pte_val)(pte_t);
  235. pteval_t (*pte_flags)(pte_t);
  236. pte_t (*make_pte)(pteval_t pte);
  237. pgdval_t (*pgd_val)(pgd_t);
  238. pgd_t (*make_pgd)(pgdval_t pgd);
  239. #if PAGETABLE_LEVELS >= 3
  240. #ifdef CONFIG_X86_PAE
  241. void (*set_pte_atomic)(pte_t *ptep, pte_t pteval);
  242. void (*set_pte_present)(struct mm_struct *mm, unsigned long addr,
  243. pte_t *ptep, pte_t pte);
  244. void (*pte_clear)(struct mm_struct *mm, unsigned long addr,
  245. pte_t *ptep);
  246. void (*pmd_clear)(pmd_t *pmdp);
  247. #endif /* CONFIG_X86_PAE */
  248. void (*set_pud)(pud_t *pudp, pud_t pudval);
  249. pmdval_t (*pmd_val)(pmd_t);
  250. pmd_t (*make_pmd)(pmdval_t pmd);
  251. #if PAGETABLE_LEVELS == 4
  252. pudval_t (*pud_val)(pud_t);
  253. pud_t (*make_pud)(pudval_t pud);
  254. void (*set_pgd)(pgd_t *pudp, pgd_t pgdval);
  255. #endif /* PAGETABLE_LEVELS == 4 */
  256. #endif /* PAGETABLE_LEVELS >= 3 */
  257. #ifdef CONFIG_HIGHPTE
  258. void *(*kmap_atomic_pte)(struct page *page, enum km_type type);
  259. #endif
  260. struct pv_lazy_ops lazy_mode;
  261. /* dom0 ops */
  262. /* Sometimes the physical address is a pfn, and sometimes its
  263. an mfn. We can tell which is which from the index. */
  264. void (*set_fixmap)(unsigned /* enum fixed_addresses */ idx,
  265. unsigned long phys, pgprot_t flags);
  266. };
  267. struct raw_spinlock;
  268. struct pv_lock_ops {
  269. int (*spin_is_locked)(struct raw_spinlock *lock);
  270. int (*spin_is_contended)(struct raw_spinlock *lock);
  271. void (*spin_lock)(struct raw_spinlock *lock);
  272. void (*spin_lock_flags)(struct raw_spinlock *lock, unsigned long flags);
  273. int (*spin_trylock)(struct raw_spinlock *lock);
  274. void (*spin_unlock)(struct raw_spinlock *lock);
  275. };
  276. /* This contains all the paravirt structures: we get a convenient
  277. * number for each function using the offset which we use to indicate
  278. * what to patch. */
  279. struct paravirt_patch_template {
  280. struct pv_init_ops pv_init_ops;
  281. struct pv_time_ops pv_time_ops;
  282. struct pv_cpu_ops pv_cpu_ops;
  283. struct pv_irq_ops pv_irq_ops;
  284. struct pv_apic_ops pv_apic_ops;
  285. struct pv_mmu_ops pv_mmu_ops;
  286. struct pv_lock_ops pv_lock_ops;
  287. };
  288. extern struct pv_info pv_info;
  289. extern struct pv_init_ops pv_init_ops;
  290. extern struct pv_time_ops pv_time_ops;
  291. extern struct pv_cpu_ops pv_cpu_ops;
  292. extern struct pv_irq_ops pv_irq_ops;
  293. extern struct pv_apic_ops pv_apic_ops;
  294. extern struct pv_mmu_ops pv_mmu_ops;
  295. extern struct pv_lock_ops pv_lock_ops;
  296. #define PARAVIRT_PATCH(x) \
  297. (offsetof(struct paravirt_patch_template, x) / sizeof(void *))
  298. #define paravirt_type(op) \
  299. [paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \
  300. [paravirt_opptr] "m" (op)
  301. #define paravirt_clobber(clobber) \
  302. [paravirt_clobber] "i" (clobber)
  303. /*
  304. * Generate some code, and mark it as patchable by the
  305. * apply_paravirt() alternate instruction patcher.
  306. */
  307. #define _paravirt_alt(insn_string, type, clobber) \
  308. "771:\n\t" insn_string "\n" "772:\n" \
  309. ".pushsection .parainstructions,\"a\"\n" \
  310. _ASM_ALIGN "\n" \
  311. _ASM_PTR " 771b\n" \
  312. " .byte " type "\n" \
  313. " .byte 772b-771b\n" \
  314. " .short " clobber "\n" \
  315. ".popsection\n"
  316. /* Generate patchable code, with the default asm parameters. */
  317. #define paravirt_alt(insn_string) \
  318. _paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]")
  319. /* Simple instruction patching code. */
  320. #define DEF_NATIVE(ops, name, code) \
  321. extern const char start_##ops##_##name[], end_##ops##_##name[]; \
  322. asm("start_" #ops "_" #name ": " code "; end_" #ops "_" #name ":")
  323. unsigned paravirt_patch_nop(void);
  324. unsigned paravirt_patch_ignore(unsigned len);
  325. unsigned paravirt_patch_call(void *insnbuf,
  326. const void *target, u16 tgt_clobbers,
  327. unsigned long addr, u16 site_clobbers,
  328. unsigned len);
  329. unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
  330. unsigned long addr, unsigned len);
  331. unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
  332. unsigned long addr, unsigned len);
  333. unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
  334. const char *start, const char *end);
  335. unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
  336. unsigned long addr, unsigned len);
  337. int paravirt_disable_iospace(void);
  338. /*
  339. * This generates an indirect call based on the operation type number.
  340. * The type number, computed in PARAVIRT_PATCH, is derived from the
  341. * offset into the paravirt_patch_template structure, and can therefore be
  342. * freely converted back into a structure offset.
  343. */
  344. #define PARAVIRT_CALL "call *%[paravirt_opptr];"
  345. /*
  346. * These macros are intended to wrap calls through one of the paravirt
  347. * ops structs, so that they can be later identified and patched at
  348. * runtime.
  349. *
  350. * Normally, a call to a pv_op function is a simple indirect call:
  351. * (pv_op_struct.operations)(args...).
  352. *
  353. * Unfortunately, this is a relatively slow operation for modern CPUs,
  354. * because it cannot necessarily determine what the destination
  355. * address is. In this case, the address is a runtime constant, so at
  356. * the very least we can patch the call to e a simple direct call, or
  357. * ideally, patch an inline implementation into the callsite. (Direct
  358. * calls are essentially free, because the call and return addresses
  359. * are completely predictable.)
  360. *
  361. * For i386, these macros rely on the standard gcc "regparm(3)" calling
  362. * convention, in which the first three arguments are placed in %eax,
  363. * %edx, %ecx (in that order), and the remaining arguments are placed
  364. * on the stack. All caller-save registers (eax,edx,ecx) are expected
  365. * to be modified (either clobbered or used for return values).
  366. * X86_64, on the other hand, already specifies a register-based calling
  367. * conventions, returning at %rax, with parameteres going on %rdi, %rsi,
  368. * %rdx, and %rcx. Note that for this reason, x86_64 does not need any
  369. * special handling for dealing with 4 arguments, unlike i386.
  370. * However, x86_64 also have to clobber all caller saved registers, which
  371. * unfortunately, are quite a bit (r8 - r11)
  372. *
  373. * The call instruction itself is marked by placing its start address
  374. * and size into the .parainstructions section, so that
  375. * apply_paravirt() in arch/i386/kernel/alternative.c can do the
  376. * appropriate patching under the control of the backend pv_init_ops
  377. * implementation.
  378. *
  379. * Unfortunately there's no way to get gcc to generate the args setup
  380. * for the call, and then allow the call itself to be generated by an
  381. * inline asm. Because of this, we must do the complete arg setup and
  382. * return value handling from within these macros. This is fairly
  383. * cumbersome.
  384. *
  385. * There are 5 sets of PVOP_* macros for dealing with 0-4 arguments.
  386. * It could be extended to more arguments, but there would be little
  387. * to be gained from that. For each number of arguments, there are
  388. * the two VCALL and CALL variants for void and non-void functions.
  389. *
  390. * When there is a return value, the invoker of the macro must specify
  391. * the return type. The macro then uses sizeof() on that type to
  392. * determine whether its a 32 or 64 bit value, and places the return
  393. * in the right register(s) (just %eax for 32-bit, and %edx:%eax for
  394. * 64-bit). For x86_64 machines, it just returns at %rax regardless of
  395. * the return value size.
  396. *
  397. * 64-bit arguments are passed as a pair of adjacent 32-bit arguments
  398. * i386 also passes 64-bit arguments as a pair of adjacent 32-bit arguments
  399. * in low,high order
  400. *
  401. * Small structures are passed and returned in registers. The macro
  402. * calling convention can't directly deal with this, so the wrapper
  403. * functions must do this.
  404. *
  405. * These PVOP_* macros are only defined within this header. This
  406. * means that all uses must be wrapped in inline functions. This also
  407. * makes sure the incoming and outgoing types are always correct.
  408. */
  409. #ifdef CONFIG_X86_32
  410. #define PVOP_VCALL_ARGS unsigned long __eax, __edx, __ecx
  411. #define PVOP_CALL_ARGS PVOP_VCALL_ARGS
  412. #define PVOP_VCALL_CLOBBERS "=a" (__eax), "=d" (__edx), \
  413. "=c" (__ecx)
  414. #define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS
  415. #define EXTRA_CLOBBERS
  416. #define VEXTRA_CLOBBERS
  417. #else
  418. #define PVOP_VCALL_ARGS unsigned long __edi, __esi, __edx, __ecx
  419. #define PVOP_CALL_ARGS PVOP_VCALL_ARGS, __eax
  420. #define PVOP_VCALL_CLOBBERS "=D" (__edi), \
  421. "=S" (__esi), "=d" (__edx), \
  422. "=c" (__ecx)
  423. #define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS, "=a" (__eax)
  424. #define EXTRA_CLOBBERS , "r8", "r9", "r10", "r11"
  425. #define VEXTRA_CLOBBERS , "rax", "r8", "r9", "r10", "r11"
  426. #endif
  427. #ifdef CONFIG_PARAVIRT_DEBUG
  428. #define PVOP_TEST_NULL(op) BUG_ON(op == NULL)
  429. #else
  430. #define PVOP_TEST_NULL(op) ((void)op)
  431. #endif
  432. #define __PVOP_CALL(rettype, op, pre, post, ...) \
  433. ({ \
  434. rettype __ret; \
  435. PVOP_CALL_ARGS; \
  436. PVOP_TEST_NULL(op); \
  437. /* This is 32-bit specific, but is okay in 64-bit */ \
  438. /* since this condition will never hold */ \
  439. if (sizeof(rettype) > sizeof(unsigned long)) { \
  440. asm volatile(pre \
  441. paravirt_alt(PARAVIRT_CALL) \
  442. post \
  443. : PVOP_CALL_CLOBBERS \
  444. : paravirt_type(op), \
  445. paravirt_clobber(CLBR_ANY), \
  446. ##__VA_ARGS__ \
  447. : "memory", "cc" EXTRA_CLOBBERS); \
  448. __ret = (rettype)((((u64)__edx) << 32) | __eax); \
  449. } else { \
  450. asm volatile(pre \
  451. paravirt_alt(PARAVIRT_CALL) \
  452. post \
  453. : PVOP_CALL_CLOBBERS \
  454. : paravirt_type(op), \
  455. paravirt_clobber(CLBR_ANY), \
  456. ##__VA_ARGS__ \
  457. : "memory", "cc" EXTRA_CLOBBERS); \
  458. __ret = (rettype)__eax; \
  459. } \
  460. __ret; \
  461. })
  462. #define __PVOP_VCALL(op, pre, post, ...) \
  463. ({ \
  464. PVOP_VCALL_ARGS; \
  465. PVOP_TEST_NULL(op); \
  466. asm volatile(pre \
  467. paravirt_alt(PARAVIRT_CALL) \
  468. post \
  469. : PVOP_VCALL_CLOBBERS \
  470. : paravirt_type(op), \
  471. paravirt_clobber(CLBR_ANY), \
  472. ##__VA_ARGS__ \
  473. : "memory", "cc" VEXTRA_CLOBBERS); \
  474. })
  475. #define PVOP_CALL0(rettype, op) \
  476. __PVOP_CALL(rettype, op, "", "")
  477. #define PVOP_VCALL0(op) \
  478. __PVOP_VCALL(op, "", "")
  479. #define PVOP_CALL1(rettype, op, arg1) \
  480. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)))
  481. #define PVOP_VCALL1(op, arg1) \
  482. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)))
  483. #define PVOP_CALL2(rettype, op, arg1, arg2) \
  484. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  485. "1" ((unsigned long)(arg2)))
  486. #define PVOP_VCALL2(op, arg1, arg2) \
  487. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  488. "1" ((unsigned long)(arg2)))
  489. #define PVOP_CALL3(rettype, op, arg1, arg2, arg3) \
  490. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  491. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
  492. #define PVOP_VCALL3(op, arg1, arg2, arg3) \
  493. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  494. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)))
  495. /* This is the only difference in x86_64. We can make it much simpler */
  496. #ifdef CONFIG_X86_32
  497. #define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
  498. __PVOP_CALL(rettype, op, \
  499. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  500. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  501. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  502. #define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
  503. __PVOP_VCALL(op, \
  504. "push %[_arg4];", "lea 4(%%esp),%%esp;", \
  505. "0" ((u32)(arg1)), "1" ((u32)(arg2)), \
  506. "2" ((u32)(arg3)), [_arg4] "mr" ((u32)(arg4)))
  507. #else
  508. #define PVOP_CALL4(rettype, op, arg1, arg2, arg3, arg4) \
  509. __PVOP_CALL(rettype, op, "", "", "0" ((unsigned long)(arg1)), \
  510. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
  511. "3"((unsigned long)(arg4)))
  512. #define PVOP_VCALL4(op, arg1, arg2, arg3, arg4) \
  513. __PVOP_VCALL(op, "", "", "0" ((unsigned long)(arg1)), \
  514. "1"((unsigned long)(arg2)), "2"((unsigned long)(arg3)), \
  515. "3"((unsigned long)(arg4)))
  516. #endif
  517. static inline int paravirt_enabled(void)
  518. {
  519. return pv_info.paravirt_enabled;
  520. }
  521. static inline void load_sp0(struct tss_struct *tss,
  522. struct thread_struct *thread)
  523. {
  524. PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
  525. }
  526. #define ARCH_SETUP pv_init_ops.arch_setup();
  527. static inline unsigned long get_wallclock(void)
  528. {
  529. return PVOP_CALL0(unsigned long, pv_time_ops.get_wallclock);
  530. }
  531. static inline int set_wallclock(unsigned long nowtime)
  532. {
  533. return PVOP_CALL1(int, pv_time_ops.set_wallclock, nowtime);
  534. }
  535. static inline void (*choose_time_init(void))(void)
  536. {
  537. return pv_time_ops.time_init;
  538. }
  539. /* The paravirtualized CPUID instruction. */
  540. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  541. unsigned int *ecx, unsigned int *edx)
  542. {
  543. PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
  544. }
  545. /*
  546. * These special macros can be used to get or set a debugging register
  547. */
  548. static inline unsigned long paravirt_get_debugreg(int reg)
  549. {
  550. return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
  551. }
  552. #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
  553. static inline void set_debugreg(unsigned long val, int reg)
  554. {
  555. PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
  556. }
  557. static inline void clts(void)
  558. {
  559. PVOP_VCALL0(pv_cpu_ops.clts);
  560. }
  561. static inline unsigned long read_cr0(void)
  562. {
  563. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
  564. }
  565. static inline void write_cr0(unsigned long x)
  566. {
  567. PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
  568. }
  569. static inline unsigned long read_cr2(void)
  570. {
  571. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
  572. }
  573. static inline void write_cr2(unsigned long x)
  574. {
  575. PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
  576. }
  577. static inline unsigned long read_cr3(void)
  578. {
  579. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
  580. }
  581. static inline void write_cr3(unsigned long x)
  582. {
  583. PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
  584. }
  585. static inline unsigned long read_cr4(void)
  586. {
  587. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
  588. }
  589. static inline unsigned long read_cr4_safe(void)
  590. {
  591. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
  592. }
  593. static inline void write_cr4(unsigned long x)
  594. {
  595. PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
  596. }
  597. #ifdef CONFIG_X86_64
  598. static inline unsigned long read_cr8(void)
  599. {
  600. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr8);
  601. }
  602. static inline void write_cr8(unsigned long x)
  603. {
  604. PVOP_VCALL1(pv_cpu_ops.write_cr8, x);
  605. }
  606. #endif
  607. static inline void raw_safe_halt(void)
  608. {
  609. PVOP_VCALL0(pv_irq_ops.safe_halt);
  610. }
  611. static inline void halt(void)
  612. {
  613. PVOP_VCALL0(pv_irq_ops.safe_halt);
  614. }
  615. static inline void wbinvd(void)
  616. {
  617. PVOP_VCALL0(pv_cpu_ops.wbinvd);
  618. }
  619. #define get_kernel_rpl() (pv_info.kernel_rpl)
  620. static inline u64 paravirt_read_msr(unsigned msr, int *err)
  621. {
  622. return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
  623. }
  624. static inline u64 paravirt_read_msr_amd(unsigned msr, int *err)
  625. {
  626. return PVOP_CALL2(u64, pv_cpu_ops.read_msr_amd, msr, err);
  627. }
  628. static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
  629. {
  630. return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
  631. }
  632. /* These should all do BUG_ON(_err), but our headers are too tangled. */
  633. #define rdmsr(msr, val1, val2) \
  634. do { \
  635. int _err; \
  636. u64 _l = paravirt_read_msr(msr, &_err); \
  637. val1 = (u32)_l; \
  638. val2 = _l >> 32; \
  639. } while (0)
  640. #define wrmsr(msr, val1, val2) \
  641. do { \
  642. paravirt_write_msr(msr, val1, val2); \
  643. } while (0)
  644. #define rdmsrl(msr, val) \
  645. do { \
  646. int _err; \
  647. val = paravirt_read_msr(msr, &_err); \
  648. } while (0)
  649. #define wrmsrl(msr, val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
  650. #define wrmsr_safe(msr, a, b) paravirt_write_msr(msr, a, b)
  651. /* rdmsr with exception handling */
  652. #define rdmsr_safe(msr, a, b) \
  653. ({ \
  654. int _err; \
  655. u64 _l = paravirt_read_msr(msr, &_err); \
  656. (*a) = (u32)_l; \
  657. (*b) = _l >> 32; \
  658. _err; \
  659. })
  660. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  661. {
  662. int err;
  663. *p = paravirt_read_msr(msr, &err);
  664. return err;
  665. }
  666. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  667. {
  668. int err;
  669. *p = paravirt_read_msr_amd(msr, &err);
  670. return err;
  671. }
  672. static inline u64 paravirt_read_tsc(void)
  673. {
  674. return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
  675. }
  676. #define rdtscl(low) \
  677. do { \
  678. u64 _l = paravirt_read_tsc(); \
  679. low = (int)_l; \
  680. } while (0)
  681. #define rdtscll(val) (val = paravirt_read_tsc())
  682. static inline unsigned long long paravirt_sched_clock(void)
  683. {
  684. return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
  685. }
  686. #define calibrate_tsc() (pv_time_ops.get_tsc_khz())
  687. static inline unsigned long long paravirt_read_pmc(int counter)
  688. {
  689. return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
  690. }
  691. #define rdpmc(counter, low, high) \
  692. do { \
  693. u64 _l = paravirt_read_pmc(counter); \
  694. low = (u32)_l; \
  695. high = _l >> 32; \
  696. } while (0)
  697. static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
  698. {
  699. return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
  700. }
  701. #define rdtscp(low, high, aux) \
  702. do { \
  703. int __aux; \
  704. unsigned long __val = paravirt_rdtscp(&__aux); \
  705. (low) = (u32)__val; \
  706. (high) = (u32)(__val >> 32); \
  707. (aux) = __aux; \
  708. } while (0)
  709. #define rdtscpll(val, aux) \
  710. do { \
  711. unsigned long __aux; \
  712. val = paravirt_rdtscp(&__aux); \
  713. (aux) = __aux; \
  714. } while (0)
  715. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  716. {
  717. PVOP_VCALL2(pv_cpu_ops.alloc_ldt, ldt, entries);
  718. }
  719. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  720. {
  721. PVOP_VCALL2(pv_cpu_ops.free_ldt, ldt, entries);
  722. }
  723. static inline void load_TR_desc(void)
  724. {
  725. PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
  726. }
  727. static inline void load_gdt(const struct desc_ptr *dtr)
  728. {
  729. PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
  730. }
  731. static inline void load_idt(const struct desc_ptr *dtr)
  732. {
  733. PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
  734. }
  735. static inline void set_ldt(const void *addr, unsigned entries)
  736. {
  737. PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
  738. }
  739. static inline void store_gdt(struct desc_ptr *dtr)
  740. {
  741. PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
  742. }
  743. static inline void store_idt(struct desc_ptr *dtr)
  744. {
  745. PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
  746. }
  747. static inline unsigned long paravirt_store_tr(void)
  748. {
  749. return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
  750. }
  751. #define store_tr(tr) ((tr) = paravirt_store_tr())
  752. static inline void load_TLS(struct thread_struct *t, unsigned cpu)
  753. {
  754. PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
  755. }
  756. #ifdef CONFIG_X86_64
  757. static inline void load_gs_index(unsigned int gs)
  758. {
  759. PVOP_VCALL1(pv_cpu_ops.load_gs_index, gs);
  760. }
  761. #endif
  762. static inline void write_ldt_entry(struct desc_struct *dt, int entry,
  763. const void *desc)
  764. {
  765. PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
  766. }
  767. static inline void write_gdt_entry(struct desc_struct *dt, int entry,
  768. void *desc, int type)
  769. {
  770. PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
  771. }
  772. static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
  773. {
  774. PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
  775. }
  776. static inline void set_iopl_mask(unsigned mask)
  777. {
  778. PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
  779. }
  780. /* The paravirtualized I/O functions */
  781. static inline void slow_down_io(void)
  782. {
  783. pv_cpu_ops.io_delay();
  784. #ifdef REALLY_SLOW_IO
  785. pv_cpu_ops.io_delay();
  786. pv_cpu_ops.io_delay();
  787. pv_cpu_ops.io_delay();
  788. #endif
  789. }
  790. #ifdef CONFIG_X86_LOCAL_APIC
  791. static inline void setup_boot_clock(void)
  792. {
  793. PVOP_VCALL0(pv_apic_ops.setup_boot_clock);
  794. }
  795. static inline void setup_secondary_clock(void)
  796. {
  797. PVOP_VCALL0(pv_apic_ops.setup_secondary_clock);
  798. }
  799. #endif
  800. static inline void paravirt_post_allocator_init(void)
  801. {
  802. if (pv_init_ops.post_allocator_init)
  803. (*pv_init_ops.post_allocator_init)();
  804. }
  805. static inline void paravirt_pagetable_setup_start(pgd_t *base)
  806. {
  807. (*pv_mmu_ops.pagetable_setup_start)(base);
  808. }
  809. static inline void paravirt_pagetable_setup_done(pgd_t *base)
  810. {
  811. (*pv_mmu_ops.pagetable_setup_done)(base);
  812. }
  813. #ifdef CONFIG_SMP
  814. static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  815. unsigned long start_esp)
  816. {
  817. PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
  818. phys_apicid, start_eip, start_esp);
  819. }
  820. #endif
  821. static inline void paravirt_activate_mm(struct mm_struct *prev,
  822. struct mm_struct *next)
  823. {
  824. PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
  825. }
  826. static inline void arch_dup_mmap(struct mm_struct *oldmm,
  827. struct mm_struct *mm)
  828. {
  829. PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
  830. }
  831. static inline void arch_exit_mmap(struct mm_struct *mm)
  832. {
  833. PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
  834. }
  835. static inline void __flush_tlb(void)
  836. {
  837. PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
  838. }
  839. static inline void __flush_tlb_global(void)
  840. {
  841. PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
  842. }
  843. static inline void __flush_tlb_single(unsigned long addr)
  844. {
  845. PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
  846. }
  847. static inline void flush_tlb_others(const struct cpumask *cpumask,
  848. struct mm_struct *mm,
  849. unsigned long va)
  850. {
  851. PVOP_VCALL3(pv_mmu_ops.flush_tlb_others, cpumask, mm, va);
  852. }
  853. static inline int paravirt_pgd_alloc(struct mm_struct *mm)
  854. {
  855. return PVOP_CALL1(int, pv_mmu_ops.pgd_alloc, mm);
  856. }
  857. static inline void paravirt_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  858. {
  859. PVOP_VCALL2(pv_mmu_ops.pgd_free, mm, pgd);
  860. }
  861. static inline void paravirt_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  862. {
  863. PVOP_VCALL2(pv_mmu_ops.alloc_pte, mm, pfn);
  864. }
  865. static inline void paravirt_release_pte(unsigned long pfn)
  866. {
  867. PVOP_VCALL1(pv_mmu_ops.release_pte, pfn);
  868. }
  869. static inline void paravirt_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  870. {
  871. PVOP_VCALL2(pv_mmu_ops.alloc_pmd, mm, pfn);
  872. }
  873. static inline void paravirt_alloc_pmd_clone(unsigned long pfn, unsigned long clonepfn,
  874. unsigned long start, unsigned long count)
  875. {
  876. PVOP_VCALL4(pv_mmu_ops.alloc_pmd_clone, pfn, clonepfn, start, count);
  877. }
  878. static inline void paravirt_release_pmd(unsigned long pfn)
  879. {
  880. PVOP_VCALL1(pv_mmu_ops.release_pmd, pfn);
  881. }
  882. static inline void paravirt_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  883. {
  884. PVOP_VCALL2(pv_mmu_ops.alloc_pud, mm, pfn);
  885. }
  886. static inline void paravirt_release_pud(unsigned long pfn)
  887. {
  888. PVOP_VCALL1(pv_mmu_ops.release_pud, pfn);
  889. }
  890. #ifdef CONFIG_HIGHPTE
  891. static inline void *kmap_atomic_pte(struct page *page, enum km_type type)
  892. {
  893. unsigned long ret;
  894. ret = PVOP_CALL2(unsigned long, pv_mmu_ops.kmap_atomic_pte, page, type);
  895. return (void *)ret;
  896. }
  897. #endif
  898. static inline void pte_update(struct mm_struct *mm, unsigned long addr,
  899. pte_t *ptep)
  900. {
  901. PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
  902. }
  903. static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
  904. pte_t *ptep)
  905. {
  906. PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
  907. }
  908. static inline pte_t __pte(pteval_t val)
  909. {
  910. pteval_t ret;
  911. if (sizeof(pteval_t) > sizeof(long))
  912. ret = PVOP_CALL2(pteval_t,
  913. pv_mmu_ops.make_pte,
  914. val, (u64)val >> 32);
  915. else
  916. ret = PVOP_CALL1(pteval_t,
  917. pv_mmu_ops.make_pte,
  918. val);
  919. return (pte_t) { .pte = ret };
  920. }
  921. static inline pteval_t pte_val(pte_t pte)
  922. {
  923. pteval_t ret;
  924. if (sizeof(pteval_t) > sizeof(long))
  925. ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_val,
  926. pte.pte, (u64)pte.pte >> 32);
  927. else
  928. ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_val,
  929. pte.pte);
  930. return ret;
  931. }
  932. static inline pteval_t pte_flags(pte_t pte)
  933. {
  934. pteval_t ret;
  935. if (sizeof(pteval_t) > sizeof(long))
  936. ret = PVOP_CALL2(pteval_t, pv_mmu_ops.pte_flags,
  937. pte.pte, (u64)pte.pte >> 32);
  938. else
  939. ret = PVOP_CALL1(pteval_t, pv_mmu_ops.pte_flags,
  940. pte.pte);
  941. #ifdef CONFIG_PARAVIRT_DEBUG
  942. BUG_ON(ret & PTE_PFN_MASK);
  943. #endif
  944. return ret;
  945. }
  946. static inline pgd_t __pgd(pgdval_t val)
  947. {
  948. pgdval_t ret;
  949. if (sizeof(pgdval_t) > sizeof(long))
  950. ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.make_pgd,
  951. val, (u64)val >> 32);
  952. else
  953. ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.make_pgd,
  954. val);
  955. return (pgd_t) { ret };
  956. }
  957. static inline pgdval_t pgd_val(pgd_t pgd)
  958. {
  959. pgdval_t ret;
  960. if (sizeof(pgdval_t) > sizeof(long))
  961. ret = PVOP_CALL2(pgdval_t, pv_mmu_ops.pgd_val,
  962. pgd.pgd, (u64)pgd.pgd >> 32);
  963. else
  964. ret = PVOP_CALL1(pgdval_t, pv_mmu_ops.pgd_val,
  965. pgd.pgd);
  966. return ret;
  967. }
  968. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  969. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
  970. pte_t *ptep)
  971. {
  972. pteval_t ret;
  973. ret = PVOP_CALL3(pteval_t, pv_mmu_ops.ptep_modify_prot_start,
  974. mm, addr, ptep);
  975. return (pte_t) { .pte = ret };
  976. }
  977. static inline void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  978. pte_t *ptep, pte_t pte)
  979. {
  980. if (sizeof(pteval_t) > sizeof(long))
  981. /* 5 arg words */
  982. pv_mmu_ops.ptep_modify_prot_commit(mm, addr, ptep, pte);
  983. else
  984. PVOP_VCALL4(pv_mmu_ops.ptep_modify_prot_commit,
  985. mm, addr, ptep, pte.pte);
  986. }
  987. static inline void set_pte(pte_t *ptep, pte_t pte)
  988. {
  989. if (sizeof(pteval_t) > sizeof(long))
  990. PVOP_VCALL3(pv_mmu_ops.set_pte, ptep,
  991. pte.pte, (u64)pte.pte >> 32);
  992. else
  993. PVOP_VCALL2(pv_mmu_ops.set_pte, ptep,
  994. pte.pte);
  995. }
  996. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  997. pte_t *ptep, pte_t pte)
  998. {
  999. if (sizeof(pteval_t) > sizeof(long))
  1000. /* 5 arg words */
  1001. pv_mmu_ops.set_pte_at(mm, addr, ptep, pte);
  1002. else
  1003. PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pte.pte);
  1004. }
  1005. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  1006. {
  1007. pmdval_t val = native_pmd_val(pmd);
  1008. if (sizeof(pmdval_t) > sizeof(long))
  1009. PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
  1010. else
  1011. PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
  1012. }
  1013. #if PAGETABLE_LEVELS >= 3
  1014. static inline pmd_t __pmd(pmdval_t val)
  1015. {
  1016. pmdval_t ret;
  1017. if (sizeof(pmdval_t) > sizeof(long))
  1018. ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.make_pmd,
  1019. val, (u64)val >> 32);
  1020. else
  1021. ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.make_pmd,
  1022. val);
  1023. return (pmd_t) { ret };
  1024. }
  1025. static inline pmdval_t pmd_val(pmd_t pmd)
  1026. {
  1027. pmdval_t ret;
  1028. if (sizeof(pmdval_t) > sizeof(long))
  1029. ret = PVOP_CALL2(pmdval_t, pv_mmu_ops.pmd_val,
  1030. pmd.pmd, (u64)pmd.pmd >> 32);
  1031. else
  1032. ret = PVOP_CALL1(pmdval_t, pv_mmu_ops.pmd_val,
  1033. pmd.pmd);
  1034. return ret;
  1035. }
  1036. static inline void set_pud(pud_t *pudp, pud_t pud)
  1037. {
  1038. pudval_t val = native_pud_val(pud);
  1039. if (sizeof(pudval_t) > sizeof(long))
  1040. PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
  1041. val, (u64)val >> 32);
  1042. else
  1043. PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
  1044. val);
  1045. }
  1046. #if PAGETABLE_LEVELS == 4
  1047. static inline pud_t __pud(pudval_t val)
  1048. {
  1049. pudval_t ret;
  1050. if (sizeof(pudval_t) > sizeof(long))
  1051. ret = PVOP_CALL2(pudval_t, pv_mmu_ops.make_pud,
  1052. val, (u64)val >> 32);
  1053. else
  1054. ret = PVOP_CALL1(pudval_t, pv_mmu_ops.make_pud,
  1055. val);
  1056. return (pud_t) { ret };
  1057. }
  1058. static inline pudval_t pud_val(pud_t pud)
  1059. {
  1060. pudval_t ret;
  1061. if (sizeof(pudval_t) > sizeof(long))
  1062. ret = PVOP_CALL2(pudval_t, pv_mmu_ops.pud_val,
  1063. pud.pud, (u64)pud.pud >> 32);
  1064. else
  1065. ret = PVOP_CALL1(pudval_t, pv_mmu_ops.pud_val,
  1066. pud.pud);
  1067. return ret;
  1068. }
  1069. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  1070. {
  1071. pgdval_t val = native_pgd_val(pgd);
  1072. if (sizeof(pgdval_t) > sizeof(long))
  1073. PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
  1074. val, (u64)val >> 32);
  1075. else
  1076. PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
  1077. val);
  1078. }
  1079. static inline void pgd_clear(pgd_t *pgdp)
  1080. {
  1081. set_pgd(pgdp, __pgd(0));
  1082. }
  1083. static inline void pud_clear(pud_t *pudp)
  1084. {
  1085. set_pud(pudp, __pud(0));
  1086. }
  1087. #endif /* PAGETABLE_LEVELS == 4 */
  1088. #endif /* PAGETABLE_LEVELS >= 3 */
  1089. #ifdef CONFIG_X86_PAE
  1090. /* Special-case pte-setting operations for PAE, which can't update a
  1091. 64-bit pte atomically */
  1092. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  1093. {
  1094. PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
  1095. pte.pte, pte.pte >> 32);
  1096. }
  1097. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
  1098. pte_t *ptep, pte_t pte)
  1099. {
  1100. /* 5 arg words */
  1101. pv_mmu_ops.set_pte_present(mm, addr, ptep, pte);
  1102. }
  1103. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  1104. pte_t *ptep)
  1105. {
  1106. PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
  1107. }
  1108. static inline void pmd_clear(pmd_t *pmdp)
  1109. {
  1110. PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
  1111. }
  1112. #else /* !CONFIG_X86_PAE */
  1113. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  1114. {
  1115. set_pte(ptep, pte);
  1116. }
  1117. static inline void set_pte_present(struct mm_struct *mm, unsigned long addr,
  1118. pte_t *ptep, pte_t pte)
  1119. {
  1120. set_pte(ptep, pte);
  1121. }
  1122. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  1123. pte_t *ptep)
  1124. {
  1125. set_pte_at(mm, addr, ptep, __pte(0));
  1126. }
  1127. static inline void pmd_clear(pmd_t *pmdp)
  1128. {
  1129. set_pmd(pmdp, __pmd(0));
  1130. }
  1131. #endif /* CONFIG_X86_PAE */
  1132. /* Lazy mode for batching updates / context switch */
  1133. enum paravirt_lazy_mode {
  1134. PARAVIRT_LAZY_NONE,
  1135. PARAVIRT_LAZY_MMU,
  1136. PARAVIRT_LAZY_CPU,
  1137. };
  1138. enum paravirt_lazy_mode paravirt_get_lazy_mode(void);
  1139. void paravirt_enter_lazy_cpu(void);
  1140. void paravirt_leave_lazy_cpu(void);
  1141. void paravirt_enter_lazy_mmu(void);
  1142. void paravirt_leave_lazy_mmu(void);
  1143. void paravirt_leave_lazy(enum paravirt_lazy_mode mode);
  1144. #define __HAVE_ARCH_ENTER_LAZY_CPU_MODE
  1145. static inline void arch_enter_lazy_cpu_mode(void)
  1146. {
  1147. PVOP_VCALL0(pv_cpu_ops.lazy_mode.enter);
  1148. }
  1149. static inline void arch_leave_lazy_cpu_mode(void)
  1150. {
  1151. PVOP_VCALL0(pv_cpu_ops.lazy_mode.leave);
  1152. }
  1153. static inline void arch_flush_lazy_cpu_mode(void)
  1154. {
  1155. if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)) {
  1156. arch_leave_lazy_cpu_mode();
  1157. arch_enter_lazy_cpu_mode();
  1158. }
  1159. }
  1160. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  1161. static inline void arch_enter_lazy_mmu_mode(void)
  1162. {
  1163. PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
  1164. }
  1165. static inline void arch_leave_lazy_mmu_mode(void)
  1166. {
  1167. PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
  1168. }
  1169. static inline void arch_flush_lazy_mmu_mode(void)
  1170. {
  1171. if (unlikely(paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU)) {
  1172. arch_leave_lazy_mmu_mode();
  1173. arch_enter_lazy_mmu_mode();
  1174. }
  1175. }
  1176. static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx,
  1177. unsigned long phys, pgprot_t flags)
  1178. {
  1179. pv_mmu_ops.set_fixmap(idx, phys, flags);
  1180. }
  1181. void _paravirt_nop(void);
  1182. #define paravirt_nop ((void *)_paravirt_nop)
  1183. void paravirt_use_bytelocks(void);
  1184. #ifdef CONFIG_SMP
  1185. static inline int __raw_spin_is_locked(struct raw_spinlock *lock)
  1186. {
  1187. return PVOP_CALL1(int, pv_lock_ops.spin_is_locked, lock);
  1188. }
  1189. static inline int __raw_spin_is_contended(struct raw_spinlock *lock)
  1190. {
  1191. return PVOP_CALL1(int, pv_lock_ops.spin_is_contended, lock);
  1192. }
  1193. #define __raw_spin_is_contended __raw_spin_is_contended
  1194. static __always_inline void __raw_spin_lock(struct raw_spinlock *lock)
  1195. {
  1196. PVOP_VCALL1(pv_lock_ops.spin_lock, lock);
  1197. }
  1198. static __always_inline void __raw_spin_lock_flags(struct raw_spinlock *lock,
  1199. unsigned long flags)
  1200. {
  1201. PVOP_VCALL2(pv_lock_ops.spin_lock_flags, lock, flags);
  1202. }
  1203. static __always_inline int __raw_spin_trylock(struct raw_spinlock *lock)
  1204. {
  1205. return PVOP_CALL1(int, pv_lock_ops.spin_trylock, lock);
  1206. }
  1207. static __always_inline void __raw_spin_unlock(struct raw_spinlock *lock)
  1208. {
  1209. PVOP_VCALL1(pv_lock_ops.spin_unlock, lock);
  1210. }
  1211. #endif
  1212. /* These all sit in the .parainstructions section to tell us what to patch. */
  1213. struct paravirt_patch_site {
  1214. u8 *instr; /* original instructions */
  1215. u8 instrtype; /* type of this instruction */
  1216. u8 len; /* length of original instruction */
  1217. u16 clobbers; /* what registers you may clobber */
  1218. };
  1219. extern struct paravirt_patch_site __parainstructions[],
  1220. __parainstructions_end[];
  1221. #ifdef CONFIG_X86_32
  1222. #define PV_SAVE_REGS "pushl %%ecx; pushl %%edx;"
  1223. #define PV_RESTORE_REGS "popl %%edx; popl %%ecx"
  1224. #define PV_FLAGS_ARG "0"
  1225. #define PV_EXTRA_CLOBBERS
  1226. #define PV_VEXTRA_CLOBBERS
  1227. #else
  1228. /* We save some registers, but all of them, that's too much. We clobber all
  1229. * caller saved registers but the argument parameter */
  1230. #define PV_SAVE_REGS "pushq %%rdi;"
  1231. #define PV_RESTORE_REGS "popq %%rdi;"
  1232. #define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx", "rsi"
  1233. #define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx", "rsi"
  1234. #define PV_FLAGS_ARG "D"
  1235. #endif
  1236. static inline unsigned long __raw_local_save_flags(void)
  1237. {
  1238. unsigned long f;
  1239. asm volatile(paravirt_alt(PV_SAVE_REGS
  1240. PARAVIRT_CALL
  1241. PV_RESTORE_REGS)
  1242. : "=a"(f)
  1243. : paravirt_type(pv_irq_ops.save_fl),
  1244. paravirt_clobber(CLBR_EAX)
  1245. : "memory", "cc" PV_VEXTRA_CLOBBERS);
  1246. return f;
  1247. }
  1248. static inline void raw_local_irq_restore(unsigned long f)
  1249. {
  1250. asm volatile(paravirt_alt(PV_SAVE_REGS
  1251. PARAVIRT_CALL
  1252. PV_RESTORE_REGS)
  1253. : "=a"(f)
  1254. : PV_FLAGS_ARG(f),
  1255. paravirt_type(pv_irq_ops.restore_fl),
  1256. paravirt_clobber(CLBR_EAX)
  1257. : "memory", "cc" PV_EXTRA_CLOBBERS);
  1258. }
  1259. static inline void raw_local_irq_disable(void)
  1260. {
  1261. asm volatile(paravirt_alt(PV_SAVE_REGS
  1262. PARAVIRT_CALL
  1263. PV_RESTORE_REGS)
  1264. :
  1265. : paravirt_type(pv_irq_ops.irq_disable),
  1266. paravirt_clobber(CLBR_EAX)
  1267. : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
  1268. }
  1269. static inline void raw_local_irq_enable(void)
  1270. {
  1271. asm volatile(paravirt_alt(PV_SAVE_REGS
  1272. PARAVIRT_CALL
  1273. PV_RESTORE_REGS)
  1274. :
  1275. : paravirt_type(pv_irq_ops.irq_enable),
  1276. paravirt_clobber(CLBR_EAX)
  1277. : "memory", "eax", "cc" PV_EXTRA_CLOBBERS);
  1278. }
  1279. static inline unsigned long __raw_local_irq_save(void)
  1280. {
  1281. unsigned long f;
  1282. f = __raw_local_save_flags();
  1283. raw_local_irq_disable();
  1284. return f;
  1285. }
  1286. /* Make sure as little as possible of this mess escapes. */
  1287. #undef PARAVIRT_CALL
  1288. #undef __PVOP_CALL
  1289. #undef __PVOP_VCALL
  1290. #undef PVOP_VCALL0
  1291. #undef PVOP_CALL0
  1292. #undef PVOP_VCALL1
  1293. #undef PVOP_CALL1
  1294. #undef PVOP_VCALL2
  1295. #undef PVOP_CALL2
  1296. #undef PVOP_VCALL3
  1297. #undef PVOP_CALL3
  1298. #undef PVOP_VCALL4
  1299. #undef PVOP_CALL4
  1300. #else /* __ASSEMBLY__ */
  1301. #define _PVSITE(ptype, clobbers, ops, word, algn) \
  1302. 771:; \
  1303. ops; \
  1304. 772:; \
  1305. .pushsection .parainstructions,"a"; \
  1306. .align algn; \
  1307. word 771b; \
  1308. .byte ptype; \
  1309. .byte 772b-771b; \
  1310. .short clobbers; \
  1311. .popsection
  1312. #ifdef CONFIG_X86_64
  1313. #define PV_SAVE_REGS \
  1314. push %rax; \
  1315. push %rcx; \
  1316. push %rdx; \
  1317. push %rsi; \
  1318. push %rdi; \
  1319. push %r8; \
  1320. push %r9; \
  1321. push %r10; \
  1322. push %r11
  1323. #define PV_RESTORE_REGS \
  1324. pop %r11; \
  1325. pop %r10; \
  1326. pop %r9; \
  1327. pop %r8; \
  1328. pop %rdi; \
  1329. pop %rsi; \
  1330. pop %rdx; \
  1331. pop %rcx; \
  1332. pop %rax
  1333. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
  1334. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
  1335. #define PARA_INDIRECT(addr) *addr(%rip)
  1336. #else
  1337. #define PV_SAVE_REGS pushl %eax; pushl %edi; pushl %ecx; pushl %edx
  1338. #define PV_RESTORE_REGS popl %edx; popl %ecx; popl %edi; popl %eax
  1339. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
  1340. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
  1341. #define PARA_INDIRECT(addr) *%cs:addr
  1342. #endif
  1343. #define INTERRUPT_RETURN \
  1344. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
  1345. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_iret))
  1346. #define DISABLE_INTERRUPTS(clobbers) \
  1347. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
  1348. PV_SAVE_REGS; \
  1349. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_disable); \
  1350. PV_RESTORE_REGS;) \
  1351. #define ENABLE_INTERRUPTS(clobbers) \
  1352. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
  1353. PV_SAVE_REGS; \
  1354. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable); \
  1355. PV_RESTORE_REGS;)
  1356. #define USERGS_SYSRET32 \
  1357. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32), \
  1358. CLBR_NONE, \
  1359. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
  1360. #ifdef CONFIG_X86_32
  1361. #define GET_CR0_INTO_EAX \
  1362. push %ecx; push %edx; \
  1363. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0); \
  1364. pop %edx; pop %ecx
  1365. #define ENABLE_INTERRUPTS_SYSEXIT \
  1366. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  1367. CLBR_NONE, \
  1368. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  1369. #else /* !CONFIG_X86_32 */
  1370. /*
  1371. * If swapgs is used while the userspace stack is still current,
  1372. * there's no way to call a pvop. The PV replacement *must* be
  1373. * inlined, or the swapgs instruction must be trapped and emulated.
  1374. */
  1375. #define SWAPGS_UNSAFE_STACK \
  1376. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  1377. swapgs)
  1378. #define SWAPGS \
  1379. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  1380. PV_SAVE_REGS; \
  1381. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_swapgs); \
  1382. PV_RESTORE_REGS \
  1383. )
  1384. #define GET_CR2_INTO_RCX \
  1385. call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2); \
  1386. movq %rax, %rcx; \
  1387. xorq %rax, %rax;
  1388. #define PARAVIRT_ADJUST_EXCEPTION_FRAME \
  1389. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_adjust_exception_frame), \
  1390. CLBR_NONE, \
  1391. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_adjust_exception_frame))
  1392. #define USERGS_SYSRET64 \
  1393. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
  1394. CLBR_NONE, \
  1395. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
  1396. #define ENABLE_INTERRUPTS_SYSEXIT32 \
  1397. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  1398. CLBR_NONE, \
  1399. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  1400. #endif /* CONFIG_X86_32 */
  1401. #endif /* __ASSEMBLY__ */
  1402. #endif /* CONFIG_PARAVIRT */
  1403. #endif /* _ASM_X86_PARAVIRT_H */