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