paravirt.h 36 KB

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