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