xref: /openbmc/linux/arch/x86/kernel/paravirt.c (revision 9fb29c73)
1 /*  Paravirtualization interfaces
2     Copyright (C) 2006 Rusty Russell IBM Corporation
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 
18     2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
19 */
20 
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/export.h>
24 #include <linux/efi.h>
25 #include <linux/bcd.h>
26 #include <linux/highmem.h>
27 #include <linux/kprobes.h>
28 
29 #include <asm/bug.h>
30 #include <asm/paravirt.h>
31 #include <asm/debugreg.h>
32 #include <asm/desc.h>
33 #include <asm/setup.h>
34 #include <asm/pgtable.h>
35 #include <asm/time.h>
36 #include <asm/pgalloc.h>
37 #include <asm/irq.h>
38 #include <asm/delay.h>
39 #include <asm/fixmap.h>
40 #include <asm/apic.h>
41 #include <asm/tlbflush.h>
42 #include <asm/timer.h>
43 #include <asm/special_insns.h>
44 #include <asm/tlb.h>
45 
46 /*
47  * nop stub, which must not clobber anything *including the stack* to
48  * avoid confusing the entry prologues.
49  */
50 extern void _paravirt_nop(void);
51 asm (".pushsection .entry.text, \"ax\"\n"
52      ".global _paravirt_nop\n"
53      "_paravirt_nop:\n\t"
54      "ret\n\t"
55      ".size _paravirt_nop, . - _paravirt_nop\n\t"
56      ".type _paravirt_nop, @function\n\t"
57      ".popsection");
58 
59 void __init default_banner(void)
60 {
61 	printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
62 	       pv_info.name);
63 }
64 
65 /* Undefined instruction for dealing with missing ops pointers. */
66 static const unsigned char ud2a[] = { 0x0f, 0x0b };
67 
68 struct branch {
69 	unsigned char opcode;
70 	u32 delta;
71 } __attribute__((packed));
72 
73 static unsigned paravirt_patch_call(void *insnbuf, const void *target,
74 				    unsigned long addr, unsigned len)
75 {
76 	struct branch *b = insnbuf;
77 	unsigned long delta = (unsigned long)target - (addr+5);
78 
79 	if (len < 5) {
80 #ifdef CONFIG_RETPOLINE
81 		WARN_ONCE(1, "Failing to patch indirect CALL in %ps\n", (void *)addr);
82 #endif
83 		return len;	/* call too long for patch site */
84 	}
85 
86 	b->opcode = 0xe8; /* call */
87 	b->delta = delta;
88 	BUILD_BUG_ON(sizeof(*b) != 5);
89 
90 	return 5;
91 }
92 
93 #ifdef CONFIG_PARAVIRT_XXL
94 /* identity function, which can be inlined */
95 u64 notrace _paravirt_ident_64(u64 x)
96 {
97 	return x;
98 }
99 
100 static unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
101 				   unsigned long addr, unsigned len)
102 {
103 	struct branch *b = insnbuf;
104 	unsigned long delta = (unsigned long)target - (addr+5);
105 
106 	if (len < 5) {
107 #ifdef CONFIG_RETPOLINE
108 		WARN_ONCE(1, "Failing to patch indirect JMP in %ps\n", (void *)addr);
109 #endif
110 		return len;	/* call too long for patch site */
111 	}
112 
113 	b->opcode = 0xe9;	/* jmp */
114 	b->delta = delta;
115 
116 	return 5;
117 }
118 #endif
119 
120 DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
121 
122 void __init native_pv_lock_init(void)
123 {
124 	if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
125 		static_branch_disable(&virt_spin_lock_key);
126 }
127 
128 unsigned paravirt_patch_default(u8 type, void *insnbuf,
129 				unsigned long addr, unsigned len)
130 {
131 	/*
132 	 * Neat trick to map patch type back to the call within the
133 	 * corresponding structure.
134 	 */
135 	void *opfunc = *((void **)&pv_ops + type);
136 	unsigned ret;
137 
138 	if (opfunc == NULL)
139 		/* If there's no function, patch it with a ud2a (BUG) */
140 		ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
141 	else if (opfunc == _paravirt_nop)
142 		ret = 0;
143 
144 #ifdef CONFIG_PARAVIRT_XXL
145 	/* identity functions just return their single argument */
146 	else if (opfunc == _paravirt_ident_64)
147 		ret = paravirt_patch_ident_64(insnbuf, len);
148 
149 	else if (type == PARAVIRT_PATCH(cpu.iret) ||
150 		 type == PARAVIRT_PATCH(cpu.usergs_sysret64))
151 		/* If operation requires a jmp, then jmp */
152 		ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
153 #endif
154 	else
155 		/* Otherwise call the function. */
156 		ret = paravirt_patch_call(insnbuf, opfunc, addr, len);
157 
158 	return ret;
159 }
160 
161 unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
162 			      const char *start, const char *end)
163 {
164 	unsigned insn_len = end - start;
165 
166 	if (insn_len > len || start == NULL)
167 		insn_len = len;
168 	else
169 		memcpy(insnbuf, start, insn_len);
170 
171 	return insn_len;
172 }
173 
174 static void native_flush_tlb(void)
175 {
176 	__native_flush_tlb();
177 }
178 
179 /*
180  * Global pages have to be flushed a bit differently. Not a real
181  * performance problem because this does not happen often.
182  */
183 static void native_flush_tlb_global(void)
184 {
185 	__native_flush_tlb_global();
186 }
187 
188 static void native_flush_tlb_one_user(unsigned long addr)
189 {
190 	__native_flush_tlb_one_user(addr);
191 }
192 
193 struct static_key paravirt_steal_enabled;
194 struct static_key paravirt_steal_rq_enabled;
195 
196 static u64 native_steal_clock(int cpu)
197 {
198 	return 0;
199 }
200 
201 /* These are in entry.S */
202 extern void native_iret(void);
203 extern void native_usergs_sysret64(void);
204 
205 static struct resource reserve_ioports = {
206 	.start = 0,
207 	.end = IO_SPACE_LIMIT,
208 	.name = "paravirt-ioport",
209 	.flags = IORESOURCE_IO | IORESOURCE_BUSY,
210 };
211 
212 /*
213  * Reserve the whole legacy IO space to prevent any legacy drivers
214  * from wasting time probing for their hardware.  This is a fairly
215  * brute-force approach to disabling all non-virtual drivers.
216  *
217  * Note that this must be called very early to have any effect.
218  */
219 int paravirt_disable_iospace(void)
220 {
221 	return request_resource(&ioport_resource, &reserve_ioports);
222 }
223 
224 static DEFINE_PER_CPU(enum paravirt_lazy_mode, paravirt_lazy_mode) = PARAVIRT_LAZY_NONE;
225 
226 static inline void enter_lazy(enum paravirt_lazy_mode mode)
227 {
228 	BUG_ON(this_cpu_read(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
229 
230 	this_cpu_write(paravirt_lazy_mode, mode);
231 }
232 
233 static void leave_lazy(enum paravirt_lazy_mode mode)
234 {
235 	BUG_ON(this_cpu_read(paravirt_lazy_mode) != mode);
236 
237 	this_cpu_write(paravirt_lazy_mode, PARAVIRT_LAZY_NONE);
238 }
239 
240 void paravirt_enter_lazy_mmu(void)
241 {
242 	enter_lazy(PARAVIRT_LAZY_MMU);
243 }
244 
245 void paravirt_leave_lazy_mmu(void)
246 {
247 	leave_lazy(PARAVIRT_LAZY_MMU);
248 }
249 
250 void paravirt_flush_lazy_mmu(void)
251 {
252 	preempt_disable();
253 
254 	if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_MMU) {
255 		arch_leave_lazy_mmu_mode();
256 		arch_enter_lazy_mmu_mode();
257 	}
258 
259 	preempt_enable();
260 }
261 
262 #ifdef CONFIG_PARAVIRT_XXL
263 void paravirt_start_context_switch(struct task_struct *prev)
264 {
265 	BUG_ON(preemptible());
266 
267 	if (this_cpu_read(paravirt_lazy_mode) == PARAVIRT_LAZY_MMU) {
268 		arch_leave_lazy_mmu_mode();
269 		set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
270 	}
271 	enter_lazy(PARAVIRT_LAZY_CPU);
272 }
273 
274 void paravirt_end_context_switch(struct task_struct *next)
275 {
276 	BUG_ON(preemptible());
277 
278 	leave_lazy(PARAVIRT_LAZY_CPU);
279 
280 	if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
281 		arch_enter_lazy_mmu_mode();
282 }
283 #endif
284 
285 enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
286 {
287 	if (in_interrupt())
288 		return PARAVIRT_LAZY_NONE;
289 
290 	return this_cpu_read(paravirt_lazy_mode);
291 }
292 
293 struct pv_info pv_info = {
294 	.name = "bare hardware",
295 #ifdef CONFIG_PARAVIRT_XXL
296 	.kernel_rpl = 0,
297 	.shared_kernel_pmd = 1,	/* Only used when CONFIG_X86_PAE is set */
298 
299 #ifdef CONFIG_X86_64
300 	.extra_user_64bit_cs = __USER_CS,
301 #endif
302 #endif
303 };
304 
305 /* 64-bit pagetable entries */
306 #define PTE_IDENT	__PV_IS_CALLEE_SAVE(_paravirt_ident_64)
307 
308 struct paravirt_patch_template pv_ops = {
309 	/* Init ops. */
310 	.init.patch		= native_patch,
311 
312 	/* Time ops. */
313 	.time.sched_clock	= native_sched_clock,
314 	.time.steal_clock	= native_steal_clock,
315 
316 	/* Cpu ops. */
317 	.cpu.io_delay		= native_io_delay,
318 
319 #ifdef CONFIG_PARAVIRT_XXL
320 	.cpu.cpuid		= native_cpuid,
321 	.cpu.get_debugreg	= native_get_debugreg,
322 	.cpu.set_debugreg	= native_set_debugreg,
323 	.cpu.read_cr0		= native_read_cr0,
324 	.cpu.write_cr0		= native_write_cr0,
325 	.cpu.write_cr4		= native_write_cr4,
326 #ifdef CONFIG_X86_64
327 	.cpu.read_cr8		= native_read_cr8,
328 	.cpu.write_cr8		= native_write_cr8,
329 #endif
330 	.cpu.wbinvd		= native_wbinvd,
331 	.cpu.read_msr		= native_read_msr,
332 	.cpu.write_msr		= native_write_msr,
333 	.cpu.read_msr_safe	= native_read_msr_safe,
334 	.cpu.write_msr_safe	= native_write_msr_safe,
335 	.cpu.read_pmc		= native_read_pmc,
336 	.cpu.load_tr_desc	= native_load_tr_desc,
337 	.cpu.set_ldt		= native_set_ldt,
338 	.cpu.load_gdt		= native_load_gdt,
339 	.cpu.load_idt		= native_load_idt,
340 	.cpu.store_tr		= native_store_tr,
341 	.cpu.load_tls		= native_load_tls,
342 #ifdef CONFIG_X86_64
343 	.cpu.load_gs_index	= native_load_gs_index,
344 #endif
345 	.cpu.write_ldt_entry	= native_write_ldt_entry,
346 	.cpu.write_gdt_entry	= native_write_gdt_entry,
347 	.cpu.write_idt_entry	= native_write_idt_entry,
348 
349 	.cpu.alloc_ldt		= paravirt_nop,
350 	.cpu.free_ldt		= paravirt_nop,
351 
352 	.cpu.load_sp0		= native_load_sp0,
353 
354 #ifdef CONFIG_X86_64
355 	.cpu.usergs_sysret64	= native_usergs_sysret64,
356 #endif
357 	.cpu.iret		= native_iret,
358 	.cpu.swapgs		= native_swapgs,
359 
360 	.cpu.set_iopl_mask	= native_set_iopl_mask,
361 
362 	.cpu.start_context_switch	= paravirt_nop,
363 	.cpu.end_context_switch		= paravirt_nop,
364 
365 	/* Irq ops. */
366 	.irq.save_fl		= __PV_IS_CALLEE_SAVE(native_save_fl),
367 	.irq.restore_fl		= __PV_IS_CALLEE_SAVE(native_restore_fl),
368 	.irq.irq_disable	= __PV_IS_CALLEE_SAVE(native_irq_disable),
369 	.irq.irq_enable		= __PV_IS_CALLEE_SAVE(native_irq_enable),
370 	.irq.safe_halt		= native_safe_halt,
371 	.irq.halt		= native_halt,
372 #endif /* CONFIG_PARAVIRT_XXL */
373 
374 	/* Mmu ops. */
375 	.mmu.flush_tlb_user	= native_flush_tlb,
376 	.mmu.flush_tlb_kernel	= native_flush_tlb_global,
377 	.mmu.flush_tlb_one_user	= native_flush_tlb_one_user,
378 	.mmu.flush_tlb_others	= native_flush_tlb_others,
379 	.mmu.tlb_remove_table	=
380 			(void (*)(struct mmu_gather *, void *))tlb_remove_page,
381 
382 	.mmu.exit_mmap		= paravirt_nop,
383 
384 #ifdef CONFIG_PARAVIRT_XXL
385 	.mmu.read_cr2		= native_read_cr2,
386 	.mmu.write_cr2		= native_write_cr2,
387 	.mmu.read_cr3		= __native_read_cr3,
388 	.mmu.write_cr3		= native_write_cr3,
389 
390 	.mmu.pgd_alloc		= __paravirt_pgd_alloc,
391 	.mmu.pgd_free		= paravirt_nop,
392 
393 	.mmu.alloc_pte		= paravirt_nop,
394 	.mmu.alloc_pmd		= paravirt_nop,
395 	.mmu.alloc_pud		= paravirt_nop,
396 	.mmu.alloc_p4d		= paravirt_nop,
397 	.mmu.release_pte	= paravirt_nop,
398 	.mmu.release_pmd	= paravirt_nop,
399 	.mmu.release_pud	= paravirt_nop,
400 	.mmu.release_p4d	= paravirt_nop,
401 
402 	.mmu.set_pte		= native_set_pte,
403 	.mmu.set_pte_at		= native_set_pte_at,
404 	.mmu.set_pmd		= native_set_pmd,
405 
406 	.mmu.ptep_modify_prot_start	= __ptep_modify_prot_start,
407 	.mmu.ptep_modify_prot_commit	= __ptep_modify_prot_commit,
408 
409 #if CONFIG_PGTABLE_LEVELS >= 3
410 #ifdef CONFIG_X86_PAE
411 	.mmu.set_pte_atomic	= native_set_pte_atomic,
412 	.mmu.pte_clear		= native_pte_clear,
413 	.mmu.pmd_clear		= native_pmd_clear,
414 #endif
415 	.mmu.set_pud		= native_set_pud,
416 
417 	.mmu.pmd_val		= PTE_IDENT,
418 	.mmu.make_pmd		= PTE_IDENT,
419 
420 #if CONFIG_PGTABLE_LEVELS >= 4
421 	.mmu.pud_val		= PTE_IDENT,
422 	.mmu.make_pud		= PTE_IDENT,
423 
424 	.mmu.set_p4d		= native_set_p4d,
425 
426 #if CONFIG_PGTABLE_LEVELS >= 5
427 	.mmu.p4d_val		= PTE_IDENT,
428 	.mmu.make_p4d		= PTE_IDENT,
429 
430 	.mmu.set_pgd		= native_set_pgd,
431 #endif /* CONFIG_PGTABLE_LEVELS >= 5 */
432 #endif /* CONFIG_PGTABLE_LEVELS >= 4 */
433 #endif /* CONFIG_PGTABLE_LEVELS >= 3 */
434 
435 	.mmu.pte_val		= PTE_IDENT,
436 	.mmu.pgd_val		= PTE_IDENT,
437 
438 	.mmu.make_pte		= PTE_IDENT,
439 	.mmu.make_pgd		= PTE_IDENT,
440 
441 	.mmu.dup_mmap		= paravirt_nop,
442 	.mmu.activate_mm	= paravirt_nop,
443 
444 	.mmu.lazy_mode = {
445 		.enter		= paravirt_nop,
446 		.leave		= paravirt_nop,
447 		.flush		= paravirt_nop,
448 	},
449 
450 	.mmu.set_fixmap		= native_set_fixmap,
451 #endif /* CONFIG_PARAVIRT_XXL */
452 
453 #if defined(CONFIG_PARAVIRT_SPINLOCKS)
454 	/* Lock ops. */
455 #ifdef CONFIG_SMP
456 	.lock.queued_spin_lock_slowpath	= native_queued_spin_lock_slowpath,
457 	.lock.queued_spin_unlock	=
458 				PV_CALLEE_SAVE(__native_queued_spin_unlock),
459 	.lock.wait			= paravirt_nop,
460 	.lock.kick			= paravirt_nop,
461 	.lock.vcpu_is_preempted		=
462 				PV_CALLEE_SAVE(__native_vcpu_is_preempted),
463 #endif /* SMP */
464 #endif
465 };
466 
467 #ifdef CONFIG_PARAVIRT_XXL
468 /* At this point, native_get/set_debugreg has real function entries */
469 NOKPROBE_SYMBOL(native_get_debugreg);
470 NOKPROBE_SYMBOL(native_set_debugreg);
471 NOKPROBE_SYMBOL(native_load_idt);
472 #endif
473 
474 EXPORT_SYMBOL(pv_ops);
475 EXPORT_SYMBOL_GPL(pv_info);
476