paravirt.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. #include <asm/paravirt_types.h>
  9. #ifndef __ASSEMBLY__
  10. #include <linux/bug.h>
  11. #include <linux/types.h>
  12. #include <linux/cpumask.h>
  13. static inline int paravirt_enabled(void)
  14. {
  15. return pv_info.paravirt_enabled;
  16. }
  17. static inline void load_sp0(struct tss_struct *tss,
  18. struct thread_struct *thread)
  19. {
  20. PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
  21. }
  22. /* The paravirtualized CPUID instruction. */
  23. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  24. unsigned int *ecx, unsigned int *edx)
  25. {
  26. PVOP_VCALL4(pv_cpu_ops.cpuid, eax, ebx, ecx, edx);
  27. }
  28. /*
  29. * These special macros can be used to get or set a debugging register
  30. */
  31. static inline unsigned long paravirt_get_debugreg(int reg)
  32. {
  33. return PVOP_CALL1(unsigned long, pv_cpu_ops.get_debugreg, reg);
  34. }
  35. #define get_debugreg(var, reg) var = paravirt_get_debugreg(reg)
  36. static inline void set_debugreg(unsigned long val, int reg)
  37. {
  38. PVOP_VCALL2(pv_cpu_ops.set_debugreg, reg, val);
  39. }
  40. static inline void clts(void)
  41. {
  42. PVOP_VCALL0(pv_cpu_ops.clts);
  43. }
  44. static inline unsigned long read_cr0(void)
  45. {
  46. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr0);
  47. }
  48. static inline void write_cr0(unsigned long x)
  49. {
  50. PVOP_VCALL1(pv_cpu_ops.write_cr0, x);
  51. }
  52. static inline unsigned long read_cr2(void)
  53. {
  54. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr2);
  55. }
  56. static inline void write_cr2(unsigned long x)
  57. {
  58. PVOP_VCALL1(pv_mmu_ops.write_cr2, x);
  59. }
  60. static inline unsigned long read_cr3(void)
  61. {
  62. return PVOP_CALL0(unsigned long, pv_mmu_ops.read_cr3);
  63. }
  64. static inline void write_cr3(unsigned long x)
  65. {
  66. PVOP_VCALL1(pv_mmu_ops.write_cr3, x);
  67. }
  68. static inline unsigned long read_cr4(void)
  69. {
  70. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4);
  71. }
  72. static inline unsigned long read_cr4_safe(void)
  73. {
  74. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr4_safe);
  75. }
  76. static inline void write_cr4(unsigned long x)
  77. {
  78. PVOP_VCALL1(pv_cpu_ops.write_cr4, x);
  79. }
  80. #ifdef CONFIG_X86_64
  81. static inline unsigned long read_cr8(void)
  82. {
  83. return PVOP_CALL0(unsigned long, pv_cpu_ops.read_cr8);
  84. }
  85. static inline void write_cr8(unsigned long x)
  86. {
  87. PVOP_VCALL1(pv_cpu_ops.write_cr8, x);
  88. }
  89. #endif
  90. static inline void arch_safe_halt(void)
  91. {
  92. PVOP_VCALL0(pv_irq_ops.safe_halt);
  93. }
  94. static inline void halt(void)
  95. {
  96. PVOP_VCALL0(pv_irq_ops.halt);
  97. }
  98. static inline void wbinvd(void)
  99. {
  100. PVOP_VCALL0(pv_cpu_ops.wbinvd);
  101. }
  102. #define get_kernel_rpl() (pv_info.kernel_rpl)
  103. static inline u64 paravirt_read_msr(unsigned msr, int *err)
  104. {
  105. return PVOP_CALL2(u64, pv_cpu_ops.read_msr, msr, err);
  106. }
  107. static inline int paravirt_write_msr(unsigned msr, unsigned low, unsigned high)
  108. {
  109. return PVOP_CALL3(int, pv_cpu_ops.write_msr, msr, low, high);
  110. }
  111. /* These should all do BUG_ON(_err), but our headers are too tangled. */
  112. #define rdmsr(msr, val1, val2) \
  113. do { \
  114. int _err; \
  115. u64 _l = paravirt_read_msr(msr, &_err); \
  116. val1 = (u32)_l; \
  117. val2 = _l >> 32; \
  118. } while (0)
  119. #define wrmsr(msr, val1, val2) \
  120. do { \
  121. paravirt_write_msr(msr, val1, val2); \
  122. } while (0)
  123. #define rdmsrl(msr, val) \
  124. do { \
  125. int _err; \
  126. val = paravirt_read_msr(msr, &_err); \
  127. } while (0)
  128. #define wrmsrl(msr, val) wrmsr(msr, (u32)((u64)(val)), ((u64)(val))>>32)
  129. #define wrmsr_safe(msr, a, b) paravirt_write_msr(msr, a, b)
  130. /* rdmsr with exception handling */
  131. #define rdmsr_safe(msr, a, b) \
  132. ({ \
  133. int _err; \
  134. u64 _l = paravirt_read_msr(msr, &_err); \
  135. (*a) = (u32)_l; \
  136. (*b) = _l >> 32; \
  137. _err; \
  138. })
  139. static inline int rdmsrl_safe(unsigned msr, unsigned long long *p)
  140. {
  141. int err;
  142. *p = paravirt_read_msr(msr, &err);
  143. return err;
  144. }
  145. static inline u64 paravirt_read_tsc(void)
  146. {
  147. return PVOP_CALL0(u64, pv_cpu_ops.read_tsc);
  148. }
  149. #define rdtscl(low) \
  150. do { \
  151. u64 _l = paravirt_read_tsc(); \
  152. low = (int)_l; \
  153. } while (0)
  154. #define rdtscll(val) (val = paravirt_read_tsc())
  155. static inline unsigned long long paravirt_sched_clock(void)
  156. {
  157. return PVOP_CALL0(unsigned long long, pv_time_ops.sched_clock);
  158. }
  159. struct static_key;
  160. extern struct static_key paravirt_steal_enabled;
  161. extern struct static_key paravirt_steal_rq_enabled;
  162. static inline u64 paravirt_steal_clock(int cpu)
  163. {
  164. return PVOP_CALL1(u64, pv_time_ops.steal_clock, cpu);
  165. }
  166. static inline unsigned long long paravirt_read_pmc(int counter)
  167. {
  168. return PVOP_CALL1(u64, pv_cpu_ops.read_pmc, counter);
  169. }
  170. #define rdpmc(counter, low, high) \
  171. do { \
  172. u64 _l = paravirt_read_pmc(counter); \
  173. low = (u32)_l; \
  174. high = _l >> 32; \
  175. } while (0)
  176. #define rdpmcl(counter, val) ((val) = paravirt_read_pmc(counter))
  177. static inline unsigned long long paravirt_rdtscp(unsigned int *aux)
  178. {
  179. return PVOP_CALL1(u64, pv_cpu_ops.read_tscp, aux);
  180. }
  181. #define rdtscp(low, high, aux) \
  182. do { \
  183. int __aux; \
  184. unsigned long __val = paravirt_rdtscp(&__aux); \
  185. (low) = (u32)__val; \
  186. (high) = (u32)(__val >> 32); \
  187. (aux) = __aux; \
  188. } while (0)
  189. #define rdtscpll(val, aux) \
  190. do { \
  191. unsigned long __aux; \
  192. val = paravirt_rdtscp(&__aux); \
  193. (aux) = __aux; \
  194. } while (0)
  195. static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  196. {
  197. PVOP_VCALL2(pv_cpu_ops.alloc_ldt, ldt, entries);
  198. }
  199. static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
  200. {
  201. PVOP_VCALL2(pv_cpu_ops.free_ldt, ldt, entries);
  202. }
  203. static inline void load_TR_desc(void)
  204. {
  205. PVOP_VCALL0(pv_cpu_ops.load_tr_desc);
  206. }
  207. static inline void load_gdt(const struct desc_ptr *dtr)
  208. {
  209. PVOP_VCALL1(pv_cpu_ops.load_gdt, dtr);
  210. }
  211. static inline void load_idt(const struct desc_ptr *dtr)
  212. {
  213. PVOP_VCALL1(pv_cpu_ops.load_idt, dtr);
  214. }
  215. static inline void set_ldt(const void *addr, unsigned entries)
  216. {
  217. PVOP_VCALL2(pv_cpu_ops.set_ldt, addr, entries);
  218. }
  219. static inline void store_gdt(struct desc_ptr *dtr)
  220. {
  221. PVOP_VCALL1(pv_cpu_ops.store_gdt, dtr);
  222. }
  223. static inline void store_idt(struct desc_ptr *dtr)
  224. {
  225. PVOP_VCALL1(pv_cpu_ops.store_idt, dtr);
  226. }
  227. static inline unsigned long paravirt_store_tr(void)
  228. {
  229. return PVOP_CALL0(unsigned long, pv_cpu_ops.store_tr);
  230. }
  231. #define store_tr(tr) ((tr) = paravirt_store_tr())
  232. static inline void load_TLS(struct thread_struct *t, unsigned cpu)
  233. {
  234. PVOP_VCALL2(pv_cpu_ops.load_tls, t, cpu);
  235. }
  236. #ifdef CONFIG_X86_64
  237. static inline void load_gs_index(unsigned int gs)
  238. {
  239. PVOP_VCALL1(pv_cpu_ops.load_gs_index, gs);
  240. }
  241. #endif
  242. static inline void write_ldt_entry(struct desc_struct *dt, int entry,
  243. const void *desc)
  244. {
  245. PVOP_VCALL3(pv_cpu_ops.write_ldt_entry, dt, entry, desc);
  246. }
  247. static inline void write_gdt_entry(struct desc_struct *dt, int entry,
  248. void *desc, int type)
  249. {
  250. PVOP_VCALL4(pv_cpu_ops.write_gdt_entry, dt, entry, desc, type);
  251. }
  252. static inline void write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
  253. {
  254. PVOP_VCALL3(pv_cpu_ops.write_idt_entry, dt, entry, g);
  255. }
  256. static inline void set_iopl_mask(unsigned mask)
  257. {
  258. PVOP_VCALL1(pv_cpu_ops.set_iopl_mask, mask);
  259. }
  260. /* The paravirtualized I/O functions */
  261. static inline void slow_down_io(void)
  262. {
  263. pv_cpu_ops.io_delay();
  264. #ifdef REALLY_SLOW_IO
  265. pv_cpu_ops.io_delay();
  266. pv_cpu_ops.io_delay();
  267. pv_cpu_ops.io_delay();
  268. #endif
  269. }
  270. #ifdef CONFIG_SMP
  271. static inline void startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  272. unsigned long start_esp)
  273. {
  274. PVOP_VCALL3(pv_apic_ops.startup_ipi_hook,
  275. phys_apicid, start_eip, start_esp);
  276. }
  277. #endif
  278. static inline void paravirt_activate_mm(struct mm_struct *prev,
  279. struct mm_struct *next)
  280. {
  281. PVOP_VCALL2(pv_mmu_ops.activate_mm, prev, next);
  282. }
  283. static inline void arch_dup_mmap(struct mm_struct *oldmm,
  284. struct mm_struct *mm)
  285. {
  286. PVOP_VCALL2(pv_mmu_ops.dup_mmap, oldmm, mm);
  287. }
  288. static inline void arch_exit_mmap(struct mm_struct *mm)
  289. {
  290. PVOP_VCALL1(pv_mmu_ops.exit_mmap, mm);
  291. }
  292. static inline void __flush_tlb(void)
  293. {
  294. PVOP_VCALL0(pv_mmu_ops.flush_tlb_user);
  295. }
  296. static inline void __flush_tlb_global(void)
  297. {
  298. PVOP_VCALL0(pv_mmu_ops.flush_tlb_kernel);
  299. }
  300. static inline void __flush_tlb_single(unsigned long addr)
  301. {
  302. PVOP_VCALL1(pv_mmu_ops.flush_tlb_single, addr);
  303. }
  304. static inline void flush_tlb_others(const struct cpumask *cpumask,
  305. struct mm_struct *mm,
  306. unsigned long va)
  307. {
  308. PVOP_VCALL3(pv_mmu_ops.flush_tlb_others, cpumask, mm, va);
  309. }
  310. static inline int paravirt_pgd_alloc(struct mm_struct *mm)
  311. {
  312. return PVOP_CALL1(int, pv_mmu_ops.pgd_alloc, mm);
  313. }
  314. static inline void paravirt_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  315. {
  316. PVOP_VCALL2(pv_mmu_ops.pgd_free, mm, pgd);
  317. }
  318. static inline void paravirt_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  319. {
  320. PVOP_VCALL2(pv_mmu_ops.alloc_pte, mm, pfn);
  321. }
  322. static inline void paravirt_release_pte(unsigned long pfn)
  323. {
  324. PVOP_VCALL1(pv_mmu_ops.release_pte, pfn);
  325. }
  326. static inline void paravirt_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  327. {
  328. PVOP_VCALL2(pv_mmu_ops.alloc_pmd, mm, pfn);
  329. }
  330. static inline void paravirt_release_pmd(unsigned long pfn)
  331. {
  332. PVOP_VCALL1(pv_mmu_ops.release_pmd, pfn);
  333. }
  334. static inline void paravirt_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  335. {
  336. PVOP_VCALL2(pv_mmu_ops.alloc_pud, mm, pfn);
  337. }
  338. static inline void paravirt_release_pud(unsigned long pfn)
  339. {
  340. PVOP_VCALL1(pv_mmu_ops.release_pud, pfn);
  341. }
  342. static inline void pte_update(struct mm_struct *mm, unsigned long addr,
  343. pte_t *ptep)
  344. {
  345. PVOP_VCALL3(pv_mmu_ops.pte_update, mm, addr, ptep);
  346. }
  347. static inline void pmd_update(struct mm_struct *mm, unsigned long addr,
  348. pmd_t *pmdp)
  349. {
  350. PVOP_VCALL3(pv_mmu_ops.pmd_update, mm, addr, pmdp);
  351. }
  352. static inline void pte_update_defer(struct mm_struct *mm, unsigned long addr,
  353. pte_t *ptep)
  354. {
  355. PVOP_VCALL3(pv_mmu_ops.pte_update_defer, mm, addr, ptep);
  356. }
  357. static inline void pmd_update_defer(struct mm_struct *mm, unsigned long addr,
  358. pmd_t *pmdp)
  359. {
  360. PVOP_VCALL3(pv_mmu_ops.pmd_update_defer, mm, addr, pmdp);
  361. }
  362. static inline pte_t __pte(pteval_t val)
  363. {
  364. pteval_t ret;
  365. if (sizeof(pteval_t) > sizeof(long))
  366. ret = PVOP_CALLEE2(pteval_t,
  367. pv_mmu_ops.make_pte,
  368. val, (u64)val >> 32);
  369. else
  370. ret = PVOP_CALLEE1(pteval_t,
  371. pv_mmu_ops.make_pte,
  372. val);
  373. return (pte_t) { .pte = ret };
  374. }
  375. static inline pteval_t pte_val(pte_t pte)
  376. {
  377. pteval_t ret;
  378. if (sizeof(pteval_t) > sizeof(long))
  379. ret = PVOP_CALLEE2(pteval_t, pv_mmu_ops.pte_val,
  380. pte.pte, (u64)pte.pte >> 32);
  381. else
  382. ret = PVOP_CALLEE1(pteval_t, pv_mmu_ops.pte_val,
  383. pte.pte);
  384. return ret;
  385. }
  386. static inline pgd_t __pgd(pgdval_t val)
  387. {
  388. pgdval_t ret;
  389. if (sizeof(pgdval_t) > sizeof(long))
  390. ret = PVOP_CALLEE2(pgdval_t, pv_mmu_ops.make_pgd,
  391. val, (u64)val >> 32);
  392. else
  393. ret = PVOP_CALLEE1(pgdval_t, pv_mmu_ops.make_pgd,
  394. val);
  395. return (pgd_t) { ret };
  396. }
  397. static inline pgdval_t pgd_val(pgd_t pgd)
  398. {
  399. pgdval_t ret;
  400. if (sizeof(pgdval_t) > sizeof(long))
  401. ret = PVOP_CALLEE2(pgdval_t, pv_mmu_ops.pgd_val,
  402. pgd.pgd, (u64)pgd.pgd >> 32);
  403. else
  404. ret = PVOP_CALLEE1(pgdval_t, pv_mmu_ops.pgd_val,
  405. pgd.pgd);
  406. return ret;
  407. }
  408. #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION
  409. static inline pte_t ptep_modify_prot_start(struct mm_struct *mm, unsigned long addr,
  410. pte_t *ptep)
  411. {
  412. pteval_t ret;
  413. ret = PVOP_CALL3(pteval_t, pv_mmu_ops.ptep_modify_prot_start,
  414. mm, addr, ptep);
  415. return (pte_t) { .pte = ret };
  416. }
  417. static inline void ptep_modify_prot_commit(struct mm_struct *mm, unsigned long addr,
  418. pte_t *ptep, pte_t pte)
  419. {
  420. if (sizeof(pteval_t) > sizeof(long))
  421. /* 5 arg words */
  422. pv_mmu_ops.ptep_modify_prot_commit(mm, addr, ptep, pte);
  423. else
  424. PVOP_VCALL4(pv_mmu_ops.ptep_modify_prot_commit,
  425. mm, addr, ptep, pte.pte);
  426. }
  427. static inline void set_pte(pte_t *ptep, pte_t pte)
  428. {
  429. if (sizeof(pteval_t) > sizeof(long))
  430. PVOP_VCALL3(pv_mmu_ops.set_pte, ptep,
  431. pte.pte, (u64)pte.pte >> 32);
  432. else
  433. PVOP_VCALL2(pv_mmu_ops.set_pte, ptep,
  434. pte.pte);
  435. }
  436. static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
  437. pte_t *ptep, pte_t pte)
  438. {
  439. if (sizeof(pteval_t) > sizeof(long))
  440. /* 5 arg words */
  441. pv_mmu_ops.set_pte_at(mm, addr, ptep, pte);
  442. else
  443. PVOP_VCALL4(pv_mmu_ops.set_pte_at, mm, addr, ptep, pte.pte);
  444. }
  445. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  446. static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
  447. pmd_t *pmdp, pmd_t pmd)
  448. {
  449. if (sizeof(pmdval_t) > sizeof(long))
  450. /* 5 arg words */
  451. pv_mmu_ops.set_pmd_at(mm, addr, pmdp, pmd);
  452. else
  453. PVOP_VCALL4(pv_mmu_ops.set_pmd_at, mm, addr, pmdp,
  454. native_pmd_val(pmd));
  455. }
  456. #endif
  457. static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
  458. {
  459. pmdval_t val = native_pmd_val(pmd);
  460. if (sizeof(pmdval_t) > sizeof(long))
  461. PVOP_VCALL3(pv_mmu_ops.set_pmd, pmdp, val, (u64)val >> 32);
  462. else
  463. PVOP_VCALL2(pv_mmu_ops.set_pmd, pmdp, val);
  464. }
  465. #if PAGETABLE_LEVELS >= 3
  466. static inline pmd_t __pmd(pmdval_t val)
  467. {
  468. pmdval_t ret;
  469. if (sizeof(pmdval_t) > sizeof(long))
  470. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.make_pmd,
  471. val, (u64)val >> 32);
  472. else
  473. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.make_pmd,
  474. val);
  475. return (pmd_t) { ret };
  476. }
  477. static inline pmdval_t pmd_val(pmd_t pmd)
  478. {
  479. pmdval_t ret;
  480. if (sizeof(pmdval_t) > sizeof(long))
  481. ret = PVOP_CALLEE2(pmdval_t, pv_mmu_ops.pmd_val,
  482. pmd.pmd, (u64)pmd.pmd >> 32);
  483. else
  484. ret = PVOP_CALLEE1(pmdval_t, pv_mmu_ops.pmd_val,
  485. pmd.pmd);
  486. return ret;
  487. }
  488. static inline void set_pud(pud_t *pudp, pud_t pud)
  489. {
  490. pudval_t val = native_pud_val(pud);
  491. if (sizeof(pudval_t) > sizeof(long))
  492. PVOP_VCALL3(pv_mmu_ops.set_pud, pudp,
  493. val, (u64)val >> 32);
  494. else
  495. PVOP_VCALL2(pv_mmu_ops.set_pud, pudp,
  496. val);
  497. }
  498. #if PAGETABLE_LEVELS == 4
  499. static inline pud_t __pud(pudval_t val)
  500. {
  501. pudval_t ret;
  502. if (sizeof(pudval_t) > sizeof(long))
  503. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.make_pud,
  504. val, (u64)val >> 32);
  505. else
  506. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.make_pud,
  507. val);
  508. return (pud_t) { ret };
  509. }
  510. static inline pudval_t pud_val(pud_t pud)
  511. {
  512. pudval_t ret;
  513. if (sizeof(pudval_t) > sizeof(long))
  514. ret = PVOP_CALLEE2(pudval_t, pv_mmu_ops.pud_val,
  515. pud.pud, (u64)pud.pud >> 32);
  516. else
  517. ret = PVOP_CALLEE1(pudval_t, pv_mmu_ops.pud_val,
  518. pud.pud);
  519. return ret;
  520. }
  521. static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
  522. {
  523. pgdval_t val = native_pgd_val(pgd);
  524. if (sizeof(pgdval_t) > sizeof(long))
  525. PVOP_VCALL3(pv_mmu_ops.set_pgd, pgdp,
  526. val, (u64)val >> 32);
  527. else
  528. PVOP_VCALL2(pv_mmu_ops.set_pgd, pgdp,
  529. val);
  530. }
  531. static inline void pgd_clear(pgd_t *pgdp)
  532. {
  533. set_pgd(pgdp, __pgd(0));
  534. }
  535. static inline void pud_clear(pud_t *pudp)
  536. {
  537. set_pud(pudp, __pud(0));
  538. }
  539. #endif /* PAGETABLE_LEVELS == 4 */
  540. #endif /* PAGETABLE_LEVELS >= 3 */
  541. #ifdef CONFIG_X86_PAE
  542. /* Special-case pte-setting operations for PAE, which can't update a
  543. 64-bit pte atomically */
  544. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  545. {
  546. PVOP_VCALL3(pv_mmu_ops.set_pte_atomic, ptep,
  547. pte.pte, pte.pte >> 32);
  548. }
  549. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  550. pte_t *ptep)
  551. {
  552. PVOP_VCALL3(pv_mmu_ops.pte_clear, mm, addr, ptep);
  553. }
  554. static inline void pmd_clear(pmd_t *pmdp)
  555. {
  556. PVOP_VCALL1(pv_mmu_ops.pmd_clear, pmdp);
  557. }
  558. #else /* !CONFIG_X86_PAE */
  559. static inline void set_pte_atomic(pte_t *ptep, pte_t pte)
  560. {
  561. set_pte(ptep, pte);
  562. }
  563. static inline void pte_clear(struct mm_struct *mm, unsigned long addr,
  564. pte_t *ptep)
  565. {
  566. set_pte_at(mm, addr, ptep, __pte(0));
  567. }
  568. static inline void pmd_clear(pmd_t *pmdp)
  569. {
  570. set_pmd(pmdp, __pmd(0));
  571. }
  572. #endif /* CONFIG_X86_PAE */
  573. #define __HAVE_ARCH_START_CONTEXT_SWITCH
  574. static inline void arch_start_context_switch(struct task_struct *prev)
  575. {
  576. PVOP_VCALL1(pv_cpu_ops.start_context_switch, prev);
  577. }
  578. static inline void arch_end_context_switch(struct task_struct *next)
  579. {
  580. PVOP_VCALL1(pv_cpu_ops.end_context_switch, next);
  581. }
  582. #define __HAVE_ARCH_ENTER_LAZY_MMU_MODE
  583. static inline void arch_enter_lazy_mmu_mode(void)
  584. {
  585. PVOP_VCALL0(pv_mmu_ops.lazy_mode.enter);
  586. }
  587. static inline void arch_leave_lazy_mmu_mode(void)
  588. {
  589. PVOP_VCALL0(pv_mmu_ops.lazy_mode.leave);
  590. }
  591. void arch_flush_lazy_mmu_mode(void);
  592. static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx,
  593. phys_addr_t phys, pgprot_t flags)
  594. {
  595. pv_mmu_ops.set_fixmap(idx, phys, flags);
  596. }
  597. #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  598. static inline int arch_spin_is_locked(struct arch_spinlock *lock)
  599. {
  600. return PVOP_CALL1(int, pv_lock_ops.spin_is_locked, lock);
  601. }
  602. static inline int arch_spin_is_contended(struct arch_spinlock *lock)
  603. {
  604. return PVOP_CALL1(int, pv_lock_ops.spin_is_contended, lock);
  605. }
  606. #define arch_spin_is_contended arch_spin_is_contended
  607. static __always_inline void arch_spin_lock(struct arch_spinlock *lock)
  608. {
  609. PVOP_VCALL1(pv_lock_ops.spin_lock, lock);
  610. }
  611. static __always_inline void arch_spin_lock_flags(struct arch_spinlock *lock,
  612. unsigned long flags)
  613. {
  614. PVOP_VCALL2(pv_lock_ops.spin_lock_flags, lock, flags);
  615. }
  616. static __always_inline int arch_spin_trylock(struct arch_spinlock *lock)
  617. {
  618. return PVOP_CALL1(int, pv_lock_ops.spin_trylock, lock);
  619. }
  620. static __always_inline void arch_spin_unlock(struct arch_spinlock *lock)
  621. {
  622. PVOP_VCALL1(pv_lock_ops.spin_unlock, lock);
  623. }
  624. #endif
  625. #ifdef CONFIG_X86_32
  626. #define PV_SAVE_REGS "pushl %ecx; pushl %edx;"
  627. #define PV_RESTORE_REGS "popl %edx; popl %ecx;"
  628. /* save and restore all caller-save registers, except return value */
  629. #define PV_SAVE_ALL_CALLER_REGS "pushl %ecx;"
  630. #define PV_RESTORE_ALL_CALLER_REGS "popl %ecx;"
  631. #define PV_FLAGS_ARG "0"
  632. #define PV_EXTRA_CLOBBERS
  633. #define PV_VEXTRA_CLOBBERS
  634. #else
  635. /* save and restore all caller-save registers, except return value */
  636. #define PV_SAVE_ALL_CALLER_REGS \
  637. "push %rcx;" \
  638. "push %rdx;" \
  639. "push %rsi;" \
  640. "push %rdi;" \
  641. "push %r8;" \
  642. "push %r9;" \
  643. "push %r10;" \
  644. "push %r11;"
  645. #define PV_RESTORE_ALL_CALLER_REGS \
  646. "pop %r11;" \
  647. "pop %r10;" \
  648. "pop %r9;" \
  649. "pop %r8;" \
  650. "pop %rdi;" \
  651. "pop %rsi;" \
  652. "pop %rdx;" \
  653. "pop %rcx;"
  654. /* We save some registers, but all of them, that's too much. We clobber all
  655. * caller saved registers but the argument parameter */
  656. #define PV_SAVE_REGS "pushq %%rdi;"
  657. #define PV_RESTORE_REGS "popq %%rdi;"
  658. #define PV_EXTRA_CLOBBERS EXTRA_CLOBBERS, "rcx" , "rdx", "rsi"
  659. #define PV_VEXTRA_CLOBBERS EXTRA_CLOBBERS, "rdi", "rcx" , "rdx", "rsi"
  660. #define PV_FLAGS_ARG "D"
  661. #endif
  662. /*
  663. * Generate a thunk around a function which saves all caller-save
  664. * registers except for the return value. This allows C functions to
  665. * be called from assembler code where fewer than normal registers are
  666. * available. It may also help code generation around calls from C
  667. * code if the common case doesn't use many registers.
  668. *
  669. * When a callee is wrapped in a thunk, the caller can assume that all
  670. * arg regs and all scratch registers are preserved across the
  671. * call. The return value in rax/eax will not be saved, even for void
  672. * functions.
  673. */
  674. #define PV_CALLEE_SAVE_REGS_THUNK(func) \
  675. extern typeof(func) __raw_callee_save_##func; \
  676. static void *__##func##__ __used = func; \
  677. \
  678. asm(".pushsection .text;" \
  679. "__raw_callee_save_" #func ": " \
  680. PV_SAVE_ALL_CALLER_REGS \
  681. "call " #func ";" \
  682. PV_RESTORE_ALL_CALLER_REGS \
  683. "ret;" \
  684. ".popsection")
  685. /* Get a reference to a callee-save function */
  686. #define PV_CALLEE_SAVE(func) \
  687. ((struct paravirt_callee_save) { __raw_callee_save_##func })
  688. /* Promise that "func" already uses the right calling convention */
  689. #define __PV_IS_CALLEE_SAVE(func) \
  690. ((struct paravirt_callee_save) { func })
  691. static inline notrace unsigned long arch_local_save_flags(void)
  692. {
  693. return PVOP_CALLEE0(unsigned long, pv_irq_ops.save_fl);
  694. }
  695. static inline notrace void arch_local_irq_restore(unsigned long f)
  696. {
  697. PVOP_VCALLEE1(pv_irq_ops.restore_fl, f);
  698. }
  699. static inline notrace void arch_local_irq_disable(void)
  700. {
  701. PVOP_VCALLEE0(pv_irq_ops.irq_disable);
  702. }
  703. static inline notrace void arch_local_irq_enable(void)
  704. {
  705. PVOP_VCALLEE0(pv_irq_ops.irq_enable);
  706. }
  707. static inline notrace unsigned long arch_local_irq_save(void)
  708. {
  709. unsigned long f;
  710. f = arch_local_save_flags();
  711. arch_local_irq_disable();
  712. return f;
  713. }
  714. /* Make sure as little as possible of this mess escapes. */
  715. #undef PARAVIRT_CALL
  716. #undef __PVOP_CALL
  717. #undef __PVOP_VCALL
  718. #undef PVOP_VCALL0
  719. #undef PVOP_CALL0
  720. #undef PVOP_VCALL1
  721. #undef PVOP_CALL1
  722. #undef PVOP_VCALL2
  723. #undef PVOP_CALL2
  724. #undef PVOP_VCALL3
  725. #undef PVOP_CALL3
  726. #undef PVOP_VCALL4
  727. #undef PVOP_CALL4
  728. extern void default_banner(void);
  729. #else /* __ASSEMBLY__ */
  730. #define _PVSITE(ptype, clobbers, ops, word, algn) \
  731. 771:; \
  732. ops; \
  733. 772:; \
  734. .pushsection .parainstructions,"a"; \
  735. .align algn; \
  736. word 771b; \
  737. .byte ptype; \
  738. .byte 772b-771b; \
  739. .short clobbers; \
  740. .popsection
  741. #define COND_PUSH(set, mask, reg) \
  742. .if ((~(set)) & mask); push %reg; .endif
  743. #define COND_POP(set, mask, reg) \
  744. .if ((~(set)) & mask); pop %reg; .endif
  745. #ifdef CONFIG_X86_64
  746. #define PV_SAVE_REGS(set) \
  747. COND_PUSH(set, CLBR_RAX, rax); \
  748. COND_PUSH(set, CLBR_RCX, rcx); \
  749. COND_PUSH(set, CLBR_RDX, rdx); \
  750. COND_PUSH(set, CLBR_RSI, rsi); \
  751. COND_PUSH(set, CLBR_RDI, rdi); \
  752. COND_PUSH(set, CLBR_R8, r8); \
  753. COND_PUSH(set, CLBR_R9, r9); \
  754. COND_PUSH(set, CLBR_R10, r10); \
  755. COND_PUSH(set, CLBR_R11, r11)
  756. #define PV_RESTORE_REGS(set) \
  757. COND_POP(set, CLBR_R11, r11); \
  758. COND_POP(set, CLBR_R10, r10); \
  759. COND_POP(set, CLBR_R9, r9); \
  760. COND_POP(set, CLBR_R8, r8); \
  761. COND_POP(set, CLBR_RDI, rdi); \
  762. COND_POP(set, CLBR_RSI, rsi); \
  763. COND_POP(set, CLBR_RDX, rdx); \
  764. COND_POP(set, CLBR_RCX, rcx); \
  765. COND_POP(set, CLBR_RAX, rax)
  766. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 8)
  767. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .quad, 8)
  768. #define PARA_INDIRECT(addr) *addr(%rip)
  769. #else
  770. #define PV_SAVE_REGS(set) \
  771. COND_PUSH(set, CLBR_EAX, eax); \
  772. COND_PUSH(set, CLBR_EDI, edi); \
  773. COND_PUSH(set, CLBR_ECX, ecx); \
  774. COND_PUSH(set, CLBR_EDX, edx)
  775. #define PV_RESTORE_REGS(set) \
  776. COND_POP(set, CLBR_EDX, edx); \
  777. COND_POP(set, CLBR_ECX, ecx); \
  778. COND_POP(set, CLBR_EDI, edi); \
  779. COND_POP(set, CLBR_EAX, eax)
  780. #define PARA_PATCH(struct, off) ((PARAVIRT_PATCH_##struct + (off)) / 4)
  781. #define PARA_SITE(ptype, clobbers, ops) _PVSITE(ptype, clobbers, ops, .long, 4)
  782. #define PARA_INDIRECT(addr) *%cs:addr
  783. #endif
  784. #define INTERRUPT_RETURN \
  785. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_iret), CLBR_NONE, \
  786. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_iret))
  787. #define DISABLE_INTERRUPTS(clobbers) \
  788. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_disable), clobbers, \
  789. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  790. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_disable); \
  791. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  792. #define ENABLE_INTERRUPTS(clobbers) \
  793. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_irq_enable), clobbers, \
  794. PV_SAVE_REGS(clobbers | CLBR_CALLEE_SAVE); \
  795. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_irq_enable); \
  796. PV_RESTORE_REGS(clobbers | CLBR_CALLEE_SAVE);)
  797. #define USERGS_SYSRET32 \
  798. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret32), \
  799. CLBR_NONE, \
  800. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret32))
  801. #ifdef CONFIG_X86_32
  802. #define GET_CR0_INTO_EAX \
  803. push %ecx; push %edx; \
  804. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_read_cr0); \
  805. pop %edx; pop %ecx
  806. #define ENABLE_INTERRUPTS_SYSEXIT \
  807. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  808. CLBR_NONE, \
  809. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  810. #else /* !CONFIG_X86_32 */
  811. /*
  812. * If swapgs is used while the userspace stack is still current,
  813. * there's no way to call a pvop. The PV replacement *must* be
  814. * inlined, or the swapgs instruction must be trapped and emulated.
  815. */
  816. #define SWAPGS_UNSAFE_STACK \
  817. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  818. swapgs)
  819. /*
  820. * Note: swapgs is very special, and in practise is either going to be
  821. * implemented with a single "swapgs" instruction or something very
  822. * special. Either way, we don't need to save any registers for
  823. * it.
  824. */
  825. #define SWAPGS \
  826. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_swapgs), CLBR_NONE, \
  827. call PARA_INDIRECT(pv_cpu_ops+PV_CPU_swapgs) \
  828. )
  829. #define GET_CR2_INTO_RAX \
  830. call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2)
  831. #define PARAVIRT_ADJUST_EXCEPTION_FRAME \
  832. PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_adjust_exception_frame), \
  833. CLBR_NONE, \
  834. call PARA_INDIRECT(pv_irq_ops+PV_IRQ_adjust_exception_frame))
  835. #define USERGS_SYSRET64 \
  836. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
  837. CLBR_NONE, \
  838. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_usergs_sysret64))
  839. #define ENABLE_INTERRUPTS_SYSEXIT32 \
  840. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_irq_enable_sysexit), \
  841. CLBR_NONE, \
  842. jmp PARA_INDIRECT(pv_cpu_ops+PV_CPU_irq_enable_sysexit))
  843. #endif /* CONFIG_X86_32 */
  844. #endif /* __ASSEMBLY__ */
  845. #else /* CONFIG_PARAVIRT */
  846. # define default_banner x86_init_noop
  847. #endif /* !CONFIG_PARAVIRT */
  848. #endif /* _ASM_X86_PARAVIRT_H */