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