1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Paravirtualization interfaces
3 Copyright (C) 2006 Rusty Russell IBM Corporation
4
5
6 2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
7 */
8
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/export.h>
12 #include <linux/efi.h>
13 #include <linux/bcd.h>
14 #include <linux/highmem.h>
15 #include <linux/kprobes.h>
16 #include <linux/pgtable.h>
17 #include <linux/static_call.h>
18
19 #include <asm/bug.h>
20 #include <asm/paravirt.h>
21 #include <asm/debugreg.h>
22 #include <asm/desc.h>
23 #include <asm/setup.h>
24 #include <asm/time.h>
25 #include <asm/pgalloc.h>
26 #include <asm/irq.h>
27 #include <asm/delay.h>
28 #include <asm/fixmap.h>
29 #include <asm/apic.h>
30 #include <asm/tlbflush.h>
31 #include <asm/timer.h>
32 #include <asm/special_insns.h>
33 #include <asm/tlb.h>
34 #include <asm/io_bitmap.h>
35
36 /*
37 * nop stub, which must not clobber anything *including the stack* to
38 * avoid confusing the entry prologues.
39 */
40 extern void _paravirt_nop(void);
41 asm (".pushsection .entry.text, \"ax\"\n"
42 ".global _paravirt_nop\n"
43 "_paravirt_nop:\n\t"
44 ASM_RET
45 ".size _paravirt_nop, . - _paravirt_nop\n\t"
46 ".type _paravirt_nop, @function\n\t"
47 ".popsection");
48
default_banner(void)49 void __init default_banner(void)
50 {
51 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
52 pv_info.name);
53 }
54
55 /* Undefined instruction for dealing with missing ops pointers. */
paravirt_BUG(void)56 static void paravirt_BUG(void)
57 {
58 BUG();
59 }
60
paravirt_patch_call(void * insn_buff,const void * target,unsigned long addr,unsigned len)61 static unsigned paravirt_patch_call(void *insn_buff, const void *target,
62 unsigned long addr, unsigned len)
63 {
64 __text_gen_insn(insn_buff, CALL_INSN_OPCODE,
65 (void *)addr, target, CALL_INSN_SIZE);
66 return CALL_INSN_SIZE;
67 }
68
69 #ifdef CONFIG_PARAVIRT_XXL
70 /* identity function, which can be inlined */
_paravirt_ident_64(u64 x)71 u64 notrace _paravirt_ident_64(u64 x)
72 {
73 return x;
74 }
75 #endif
76
77 DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
78
native_pv_lock_init(void)79 void __init native_pv_lock_init(void)
80 {
81 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
82 static_branch_disable(&virt_spin_lock_key);
83 }
84
paravirt_patch(u8 type,void * insn_buff,unsigned long addr,unsigned int len)85 unsigned int paravirt_patch(u8 type, void *insn_buff, unsigned long addr,
86 unsigned int len)
87 {
88 /*
89 * Neat trick to map patch type back to the call within the
90 * corresponding structure.
91 */
92 void *opfunc = *((void **)&pv_ops + type);
93 unsigned ret;
94
95 if (opfunc == NULL)
96 /* If there's no function, patch it with paravirt_BUG() */
97 ret = paravirt_patch_call(insn_buff, paravirt_BUG, addr, len);
98 else if (opfunc == _paravirt_nop)
99 ret = 0;
100 else
101 /* Otherwise call the function. */
102 ret = paravirt_patch_call(insn_buff, opfunc, addr, len);
103
104 return ret;
105 }
106
107 struct static_key paravirt_steal_enabled;
108 struct static_key paravirt_steal_rq_enabled;
109
native_steal_clock(int cpu)110 static u64 native_steal_clock(int cpu)
111 {
112 return 0;
113 }
114
115 DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
116 DEFINE_STATIC_CALL(pv_sched_clock, native_sched_clock);
117
paravirt_set_sched_clock(u64 (* func)(void))118 void paravirt_set_sched_clock(u64 (*func)(void))
119 {
120 static_call_update(pv_sched_clock, func);
121 }
122
123 /* These are in entry.S */
124 extern void native_iret(void);
125
126 static struct resource reserve_ioports = {
127 .start = 0,
128 .end = IO_SPACE_LIMIT,
129 .name = "paravirt-ioport",
130 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
131 };
132
133 /*
134 * Reserve the whole legacy IO space to prevent any legacy drivers
135 * from wasting time probing for their hardware. This is a fairly
136 * brute-force approach to disabling all non-virtual drivers.
137 *
138 * Note that this must be called very early to have any effect.
139 */
paravirt_disable_iospace(void)140 int paravirt_disable_iospace(void)
141 {
142 return request_resource(&ioport_resource, &reserve_ioports);
143 }
144
145 static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
146
enter_lazy(enum paravirt_lazy_mode mode)147 static inline void enter_lazy(enum paravirt_lazy_mode mode)
148 {
149 BUG_ON(this_cpu_read(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
150
151 this_cpu_write(paravirt_lazy_mode, mode);
152 }
153
leave_lazy(enum paravirt_lazy_mode mode)154 static void leave_lazy(enum paravirt_lazy_mode mode)
155 {
156 BUG_ON(this_cpu_read(paravirt_lazy_mode) != mode);
157
158 this_cpu_write(paravirt_lazy_mode, PARAVIRT_LAZY_NONE);
159 }
160
paravirt_enter_lazy_mmu(void)161 void paravirt_enter_lazy_mmu(void)
162 {
163 enter_lazy(PARAVIRT_LAZY_MMU);
164 }
165
paravirt_leave_lazy_mmu(void)166 void paravirt_leave_lazy_mmu(void)
167 {
168 leave_lazy(PARAVIRT_LAZY_MMU);
169 }
170
paravirt_flush_lazy_mmu(void)171 void paravirt_flush_lazy_mmu(void)
172 {
173 preempt_disable();
174
175 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
176 arch_leave_lazy_mmu_mode();
177 arch_enter_lazy_mmu_mode();
178 }
179
180 preempt_enable();
181 }
182
183 #ifdef CONFIG_PARAVIRT_XXL
paravirt_start_context_switch(struct task_struct * prev)184 void paravirt_start_context_switch(struct task_struct *prev)
185 {
186 BUG_ON(preemptible());
187
188 if (this_cpu_read(paravirt_lazy_mode) == PARAVIRT_LAZY_MMU) {
189 arch_leave_lazy_mmu_mode();
190 set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
191 }
192 enter_lazy(PARAVIRT_LAZY_CPU);
193 }
194
paravirt_end_context_switch(struct task_struct * next)195 void paravirt_end_context_switch(struct task_struct *next)
196 {
197 BUG_ON(preemptible());
198
199 leave_lazy(PARAVIRT_LAZY_CPU);
200
201 if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
202 arch_enter_lazy_mmu_mode();
203 }
204 #endif
205
paravirt_get_lazy_mode(void)206 enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
207 {
208 if (in_interrupt())
209 return PARAVIRT_LAZY_NONE;
210
211 return this_cpu_read(paravirt_lazy_mode);
212 }
213
214 struct pv_info pv_info = {
215 .name = "bare hardware",
216 #ifdef CONFIG_PARAVIRT_XXL
217 .extra_user_64bit_cs = __USER_CS,
218 #endif
219 };
220
221 /* 64-bit pagetable entries */
222 #define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
223
224 struct paravirt_patch_template pv_ops = {
225 /* Cpu ops. */
226 .cpu.io_delay = native_io_delay,
227
228 #ifdef CONFIG_PARAVIRT_XXL
229 .cpu.cpuid = native_cpuid,
230 .cpu.get_debugreg = native_get_debugreg,
231 .cpu.set_debugreg = native_set_debugreg,
232 .cpu.read_cr0 = native_read_cr0,
233 .cpu.write_cr0 = native_write_cr0,
234 .cpu.write_cr4 = native_write_cr4,
235 .cpu.wbinvd = native_wbinvd,
236 .cpu.read_msr = native_read_msr,
237 .cpu.write_msr = native_write_msr,
238 .cpu.read_msr_safe = native_read_msr_safe,
239 .cpu.write_msr_safe = native_write_msr_safe,
240 .cpu.read_pmc = native_read_pmc,
241 .cpu.load_tr_desc = native_load_tr_desc,
242 .cpu.set_ldt = native_set_ldt,
243 .cpu.load_gdt = native_load_gdt,
244 .cpu.load_idt = native_load_idt,
245 .cpu.store_tr = native_store_tr,
246 .cpu.load_tls = native_load_tls,
247 .cpu.load_gs_index = native_load_gs_index,
248 .cpu.write_ldt_entry = native_write_ldt_entry,
249 .cpu.write_gdt_entry = native_write_gdt_entry,
250 .cpu.write_idt_entry = native_write_idt_entry,
251
252 .cpu.alloc_ldt = paravirt_nop,
253 .cpu.free_ldt = paravirt_nop,
254
255 .cpu.load_sp0 = native_load_sp0,
256
257 #ifdef CONFIG_X86_IOPL_IOPERM
258 .cpu.invalidate_io_bitmap = native_tss_invalidate_io_bitmap,
259 .cpu.update_io_bitmap = native_tss_update_io_bitmap,
260 #endif
261
262 .cpu.start_context_switch = paravirt_nop,
263 .cpu.end_context_switch = paravirt_nop,
264
265 /* Irq ops. */
266 .irq.save_fl = __PV_IS_CALLEE_SAVE(native_save_fl),
267 .irq.irq_disable = __PV_IS_CALLEE_SAVE(native_irq_disable),
268 .irq.irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable),
269 .irq.safe_halt = native_safe_halt,
270 .irq.halt = native_halt,
271 #endif /* CONFIG_PARAVIRT_XXL */
272
273 /* Mmu ops. */
274 .mmu.flush_tlb_user = native_flush_tlb_local,
275 .mmu.flush_tlb_kernel = native_flush_tlb_global,
276 .mmu.flush_tlb_one_user = native_flush_tlb_one_user,
277 .mmu.flush_tlb_multi = native_flush_tlb_multi,
278 .mmu.tlb_remove_table =
279 (void (*)(struct mmu_gather *, void *))tlb_remove_page,
280
281 .mmu.exit_mmap = paravirt_nop,
282
283 #ifdef CONFIG_PARAVIRT_XXL
284 .mmu.read_cr2 = __PV_IS_CALLEE_SAVE(native_read_cr2),
285 .mmu.write_cr2 = native_write_cr2,
286 .mmu.read_cr3 = __native_read_cr3,
287 .mmu.write_cr3 = native_write_cr3,
288
289 .mmu.pgd_alloc = __paravirt_pgd_alloc,
290 .mmu.pgd_free = paravirt_nop,
291
292 .mmu.alloc_pte = paravirt_nop,
293 .mmu.alloc_pmd = paravirt_nop,
294 .mmu.alloc_pud = paravirt_nop,
295 .mmu.alloc_p4d = paravirt_nop,
296 .mmu.release_pte = paravirt_nop,
297 .mmu.release_pmd = paravirt_nop,
298 .mmu.release_pud = paravirt_nop,
299 .mmu.release_p4d = paravirt_nop,
300
301 .mmu.set_pte = native_set_pte,
302 .mmu.set_pmd = native_set_pmd,
303
304 .mmu.ptep_modify_prot_start = __ptep_modify_prot_start,
305 .mmu.ptep_modify_prot_commit = __ptep_modify_prot_commit,
306
307 .mmu.set_pud = native_set_pud,
308
309 .mmu.pmd_val = PTE_IDENT,
310 .mmu.make_pmd = PTE_IDENT,
311
312 .mmu.pud_val = PTE_IDENT,
313 .mmu.make_pud = PTE_IDENT,
314
315 .mmu.set_p4d = native_set_p4d,
316
317 #if CONFIG_PGTABLE_LEVELS >= 5
318 .mmu.p4d_val = PTE_IDENT,
319 .mmu.make_p4d = PTE_IDENT,
320
321 .mmu.set_pgd = native_set_pgd,
322 #endif /* CONFIG_PGTABLE_LEVELS >= 5 */
323
324 .mmu.pte_val = PTE_IDENT,
325 .mmu.pgd_val = PTE_IDENT,
326
327 .mmu.make_pte = PTE_IDENT,
328 .mmu.make_pgd = PTE_IDENT,
329
330 .mmu.dup_mmap = paravirt_nop,
331 .mmu.activate_mm = paravirt_nop,
332
333 .mmu.lazy_mode = {
334 .enter = paravirt_nop,
335 .leave = paravirt_nop,
336 .flush = paravirt_nop,
337 },
338
339 .mmu.set_fixmap = native_set_fixmap,
340 #endif /* CONFIG_PARAVIRT_XXL */
341
342 #if defined(CONFIG_PARAVIRT_SPINLOCKS)
343 /* Lock ops. */
344 #ifdef CONFIG_SMP
345 .lock.queued_spin_lock_slowpath = native_queued_spin_lock_slowpath,
346 .lock.queued_spin_unlock =
347 PV_CALLEE_SAVE(__native_queued_spin_unlock),
348 .lock.wait = paravirt_nop,
349 .lock.kick = paravirt_nop,
350 .lock.vcpu_is_preempted =
351 PV_CALLEE_SAVE(__native_vcpu_is_preempted),
352 #endif /* SMP */
353 #endif
354 };
355
356 #ifdef CONFIG_PARAVIRT_XXL
357 /* At this point, native_get/set_debugreg has real function entries */
358 NOKPROBE_SYMBOL(native_get_debugreg);
359 NOKPROBE_SYMBOL(native_set_debugreg);
360 NOKPROBE_SYMBOL(native_load_idt);
361
362 void (*paravirt_iret)(void) = native_iret;
363 #endif
364
365 EXPORT_SYMBOL(pv_ops);
366 EXPORT_SYMBOL_GPL(pv_info);
367