paravirt.h 23 KB

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