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