paravirt.h 49 KB

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