x86/xen: Add SPDX identifier in arch/x86/xen files
[linux-block.git] / arch / x86 / kernel / paravirt.c
CommitLineData
d3561b7f
RR
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
b1df07bd
GOC
17
18 2007 - x86_64 support added by Glauber de Oliveira Costa, Red Hat Inc
d3561b7f 19*/
b1df07bd 20
d3561b7f 21#include <linux/errno.h>
186f4360
PG
22#include <linux/init.h>
23#include <linux/export.h>
d3561b7f
RR
24#include <linux/efi.h>
25#include <linux/bcd.h>
ce6234b5 26#include <linux/highmem.h>
376e2424 27#include <linux/kprobes.h>
d3561b7f
RR
28
29#include <asm/bug.h>
30#include <asm/paravirt.h>
50af5ead 31#include <asm/debugreg.h>
d3561b7f
RR
32#include <asm/desc.h>
33#include <asm/setup.h>
a312b37b 34#include <asm/pgtable.h>
d3561b7f 35#include <asm/time.h>
eba0045f 36#include <asm/pgalloc.h>
d3561b7f
RR
37#include <asm/irq.h>
38#include <asm/delay.h>
13623d79
RR
39#include <asm/fixmap.h>
40#include <asm/apic.h>
da181a8b 41#include <asm/tlbflush.h>
6cb9a835 42#include <asm/timer.h>
f05e798a 43#include <asm/special_insns.h>
48a8b97c 44#include <asm/tlb.h>
d3561b7f 45
fc57a7c6
AL
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");
d3561b7f 58
41edafdb 59/* identity function, which can be inlined */
15301a57 60u32 notrace _paravirt_ident_32(u32 x)
41edafdb
JF
61{
62 return x;
63}
64
15301a57 65u64 notrace _paravirt_ident_64(u64 x)
41edafdb
JF
66{
67 return x;
68}
69
6f30c1ac 70void __init default_banner(void)
d3561b7f
RR
71{
72 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
93b1eab3 73 pv_info.name);
d3561b7f
RR
74}
75
93b1eab3
JF
76/* Undefined instruction for dealing with missing ops pointers. */
77static const unsigned char ud2a[] = { 0x0f, 0x0b };
139ec7c4 78
19d36ccd
AK
79struct branch {
80 unsigned char opcode;
81 u32 delta;
82} __attribute__((packed));
83
ab144f5e
AK
84unsigned paravirt_patch_call(void *insnbuf,
85 const void *target, u16 tgt_clobbers,
86 unsigned long addr, u16 site_clobbers,
63f70270
JF
87 unsigned len)
88{
ab144f5e
AK
89 struct branch *b = insnbuf;
90 unsigned long delta = (unsigned long)target - (addr+5);
63f70270 91
5800dc5c
PZ
92 if (len < 5) {
93#ifdef CONFIG_RETPOLINE
94 WARN_ONCE("Failing to patch indirect CALL in %ps\n", (void *)addr);
95#endif
63f70270 96 return len; /* call too long for patch site */
5800dc5c 97 }
139ec7c4 98
ab144f5e
AK
99 b->opcode = 0xe8; /* call */
100 b->delta = delta;
101 BUILD_BUG_ON(sizeof(*b) != 5);
139ec7c4 102
63f70270
JF
103 return 5;
104}
105
93b1eab3 106unsigned paravirt_patch_jmp(void *insnbuf, const void *target,
ab144f5e 107 unsigned long addr, unsigned len)
63f70270 108{
ab144f5e
AK
109 struct branch *b = insnbuf;
110 unsigned long delta = (unsigned long)target - (addr+5);
63f70270 111
5800dc5c
PZ
112 if (len < 5) {
113#ifdef CONFIG_RETPOLINE
114 WARN_ONCE("Failing to patch indirect JMP in %ps\n", (void *)addr);
115#endif
63f70270 116 return len; /* call too long for patch site */
5800dc5c 117 }
63f70270 118
ab144f5e
AK
119 b->opcode = 0xe9; /* jmp */
120 b->delta = delta;
63f70270
JF
121
122 return 5;
123}
124
9043442b
JG
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 */
93b1eab3
JF
137static void *get_call_destination(u8 type)
138{
139 struct paravirt_patch_template tmpl = {
140 .pv_init_ops = pv_init_ops,
93b1eab3
JF
141 .pv_time_ops = pv_time_ops,
142 .pv_cpu_ops = pv_cpu_ops,
143 .pv_irq_ops = pv_irq_ops,
93b1eab3 144 .pv_mmu_ops = pv_mmu_ops,
b4ecc126 145#ifdef CONFIG_PARAVIRT_SPINLOCKS
74d4affd 146 .pv_lock_ops = pv_lock_ops,
b4ecc126 147#endif
93b1eab3
JF
148 };
149 return *((void **)&tmpl + type);
150}
151
ab144f5e
AK
152unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
153 unsigned long addr, unsigned len)
63f70270 154{
93b1eab3 155 void *opfunc = get_call_destination(type);
63f70270
JF
156 unsigned ret;
157
158 if (opfunc == NULL)
159 /* If there's no function, patch it with a ud2a (BUG) */
93b1eab3 160 ret = paravirt_patch_insns(insnbuf, len, ud2a, ud2a+sizeof(ud2a));
41edafdb 161 else if (opfunc == _paravirt_nop)
79f1d836 162 ret = 0;
41edafdb
JF
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
93b1eab3 170 else if (type == PARAVIRT_PATCH(pv_cpu_ops.iret) ||
2be29982 171 type == PARAVIRT_PATCH(pv_cpu_ops.usergs_sysret64))
63f70270 172 /* If operation requires a jmp, then jmp */
93b1eab3 173 ret = paravirt_patch_jmp(insnbuf, opfunc, addr, len);
63f70270
JF
174 else
175 /* Otherwise call the function; assume target could
176 clobber any caller-save reg */
ab144f5e
AK
177 ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
178 addr, clobbers, len);
63f70270
JF
179
180 return ret;
181}
182
ab144f5e 183unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
63f70270
JF
184 const char *start, const char *end)
185{
186 unsigned insn_len = end - start;
139ec7c4 187
63f70270
JF
188 if (insn_len > len || start == NULL)
189 insn_len = len;
190 else
ab144f5e 191 memcpy(insnbuf, start, insn_len);
139ec7c4 192
139ec7c4
RR
193 return insn_len;
194}
195
1a1eecd1 196static void native_flush_tlb(void)
da181a8b
RR
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 */
1a1eecd1 205static void native_flush_tlb_global(void)
da181a8b
RR
206{
207 __native_flush_tlb_global();
208}
209
1299ef1d 210static void native_flush_tlb_one_user(unsigned long addr)
da181a8b 211{
1299ef1d 212 __native_flush_tlb_one_user(addr);
da181a8b
RR
213}
214
c5905afb
IM
215struct static_key paravirt_steal_enabled;
216struct static_key paravirt_steal_rq_enabled;
3c404b57
GC
217
218static u64 native_steal_clock(int cpu)
219{
220 return 0;
221}
222
d3561b7f 223/* These are in entry.S */
1a1eecd1 224extern void native_iret(void);
2be29982 225extern void native_usergs_sysret64(void);
d3561b7f 226
d572929c
JF
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
d572929c
JF
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{
f7743fe6 243 return request_resource(&ioport_resource, &reserve_ioports);
d572929c
JF
244}
245
8965c1c0
JF
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{
c6ae41e7 250 BUG_ON(this_cpu_read(paravirt_lazy_mode) != PARAVIRT_LAZY_NONE);
8965c1c0 251
c6ae41e7 252 this_cpu_write(paravirt_lazy_mode, mode);
8965c1c0
JF
253}
254
b407fc57 255static void leave_lazy(enum paravirt_lazy_mode mode)
8965c1c0 256{
c6ae41e7 257 BUG_ON(this_cpu_read(paravirt_lazy_mode) != mode);
8965c1c0 258
c6ae41e7 259 this_cpu_write(paravirt_lazy_mode, PARAVIRT_LAZY_NONE);
8965c1c0
JF
260}
261
262void paravirt_enter_lazy_mmu(void)
263{
264 enter_lazy(PARAVIRT_LAZY_MMU);
265}
266
267void paravirt_leave_lazy_mmu(void)
268{
b407fc57 269 leave_lazy(PARAVIRT_LAZY_MMU);
8965c1c0
JF
270}
271
511ba86e
BO
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
224101ed 284void paravirt_start_context_switch(struct task_struct *prev)
8965c1c0 285{
2829b449
JF
286 BUG_ON(preemptible());
287
c6ae41e7 288 if (this_cpu_read(paravirt_lazy_mode) == PARAVIRT_LAZY_MMU) {
b407fc57 289 arch_leave_lazy_mmu_mode();
224101ed 290 set_ti_thread_flag(task_thread_info(prev), TIF_LAZY_MMU_UPDATES);
b407fc57 291 }
8965c1c0
JF
292 enter_lazy(PARAVIRT_LAZY_CPU);
293}
294
224101ed 295void paravirt_end_context_switch(struct task_struct *next)
8965c1c0 296{
2829b449
JF
297 BUG_ON(preemptible());
298
b407fc57
JF
299 leave_lazy(PARAVIRT_LAZY_CPU);
300
224101ed 301 if (test_and_clear_ti_thread_flag(task_thread_info(next), TIF_LAZY_MMU_UPDATES))
b407fc57 302 arch_enter_lazy_mmu_mode();
8965c1c0
JF
303}
304
305enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
306{
b8bcfe99
JF
307 if (in_interrupt())
308 return PARAVIRT_LAZY_NONE;
309
c6ae41e7 310 return this_cpu_read(paravirt_lazy_mode);
8965c1c0
JF
311}
312
93b1eab3 313struct pv_info pv_info = {
d3561b7f 314 .name = "bare hardware",
d3561b7f 315 .kernel_rpl = 0,
5311ab62 316 .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
318f5a2a
AL
317
318#ifdef CONFIG_X86_64
319 .extra_user_64bit_cs = __USER_CS,
320#endif
93b1eab3 321};
d3561b7f 322
93b1eab3
JF
323struct pv_init_ops pv_init_ops = {
324 .patch = native_patch,
93b1eab3
JF
325};
326
327struct pv_time_ops pv_time_ops = {
93b1eab3 328 .sched_clock = native_sched_clock,
3c404b57 329 .steal_clock = native_steal_clock,
93b1eab3
JF
330};
331
9a55fdbe 332__visible struct pv_irq_ops pv_irq_ops = {
ecb93d1c
JF
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),
93b1eab3
JF
337 .safe_halt = native_safe_halt,
338 .halt = native_halt,
339};
d3561b7f 340
9a55fdbe 341__visible struct pv_cpu_ops pv_cpu_ops = {
d3561b7f
RR
342 .cpuid = native_cpuid,
343 .get_debugreg = native_get_debugreg,
344 .set_debugreg = native_set_debugreg,
d3561b7f
RR
345 .read_cr0 = native_read_cr0,
346 .write_cr0 = native_write_cr0,
d3561b7f 347 .write_cr4 = native_write_cr4,
88b4755f
GOC
348#ifdef CONFIG_X86_64
349 .read_cr8 = native_read_cr8,
350 .write_cr8 = native_write_cr8,
351#endif
d3561b7f 352 .wbinvd = native_wbinvd,
dd2f4a00
AL
353 .read_msr = native_read_msr,
354 .write_msr = native_write_msr,
c2ee03b2
AL
355 .read_msr_safe = native_read_msr_safe,
356 .write_msr_safe = native_write_msr_safe,
d3561b7f
RR
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,
d3561b7f
RR
362 .store_tr = native_store_tr,
363 .load_tls = native_load_tls,
9f9d489a
JF
364#ifdef CONFIG_X86_64
365 .load_gs_index = native_load_gs_index,
366#endif
75b8bb3e 367 .write_ldt_entry = native_write_ldt_entry,
014b15be 368 .write_gdt_entry = native_write_gdt_entry,
8d947344 369 .write_idt_entry = native_write_idt_entry,
38ffbe66
JF
370
371 .alloc_ldt = paravirt_nop,
372 .free_ldt = paravirt_nop,
373
faca6227 374 .load_sp0 = native_load_sp0,
d3561b7f 375
2be29982 376#ifdef CONFIG_X86_64
2be29982 377 .usergs_sysret64 = native_usergs_sysret64,
d75cd22f 378#endif
93b1eab3 379 .iret = native_iret,
e801f864 380 .swapgs = native_swapgs,
93b1eab3 381
d3561b7f
RR
382 .set_iopl_mask = native_set_iopl_mask,
383 .io_delay = native_io_delay,
8965c1c0 384
224101ed
JF
385 .start_context_switch = paravirt_nop,
386 .end_context_switch = paravirt_nop,
93b1eab3 387};
d3561b7f 388
80271972 389/* At this point, native_get/set_debugreg has real function entries */
376e2424 390NOKPROBE_SYMBOL(native_get_debugreg);
80271972
MH
391NOKPROBE_SYMBOL(native_set_debugreg);
392NOKPROBE_SYMBOL(native_load_idt);
376e2424 393
41edafdb
JF
394#if defined(CONFIG_X86_32) && !defined(CONFIG_X86_PAE)
395/* 32-bit pagetable entries */
da5de7c2 396#define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_32)
41edafdb
JF
397#else
398/* 64-bit pagetable entries */
da5de7c2 399#define PTE_IDENT __PV_IS_CALLEE_SAVE(_paravirt_ident_64)
41edafdb
JF
400#endif
401
404f6aac 402struct pv_mmu_ops pv_mmu_ops __ro_after_init = {
b239fb25 403
93b1eab3
JF
404 .read_cr2 = native_read_cr2,
405 .write_cr2 = native_write_cr2,
6c690ee1 406 .read_cr3 = __native_read_cr3,
93b1eab3
JF
407 .write_cr3 = native_write_cr3,
408
da181a8b
RR
409 .flush_tlb_user = native_flush_tlb,
410 .flush_tlb_kernel = native_flush_tlb_global,
1299ef1d 411 .flush_tlb_one_user = native_flush_tlb_one_user,
d4c10477 412 .flush_tlb_others = native_flush_tlb_others,
48a8b97c 413 .tlb_remove_table = (void (*)(struct mmu_gather *, void *))tlb_remove_page,
da181a8b 414
eba0045f
JF
415 .pgd_alloc = __paravirt_pgd_alloc,
416 .pgd_free = paravirt_nop,
417
6944a9c8
JF
418 .alloc_pte = paravirt_nop,
419 .alloc_pmd = paravirt_nop,
2761fa09 420 .alloc_pud = paravirt_nop,
335437fb 421 .alloc_p4d = paravirt_nop,
6944a9c8
JF
422 .release_pte = paravirt_nop,
423 .release_pmd = paravirt_nop,
2761fa09 424 .release_pud = paravirt_nop,
335437fb 425 .release_p4d = paravirt_nop,
c119ecce 426
da181a8b
RR
427 .set_pte = native_set_pte,
428 .set_pte_at = native_set_pte_at,
429 .set_pmd = native_set_pmd,
3dc494e8 430
08b882c6
JF
431 .ptep_modify_prot_start = __ptep_modify_prot_start,
432 .ptep_modify_prot_commit = __ptep_modify_prot_commit,
433
98233368 434#if CONFIG_PGTABLE_LEVELS >= 3
da181a8b
RR
435#ifdef CONFIG_X86_PAE
436 .set_pte_atomic = native_set_pte_atomic,
da181a8b
RR
437 .pte_clear = native_pte_clear,
438 .pmd_clear = native_pmd_clear,
f95f2f7b
EH
439#endif
440 .set_pud = native_set_pud,
da5de7c2
JF
441
442 .pmd_val = PTE_IDENT,
443 .make_pmd = PTE_IDENT,
f95f2f7b 444
f2a6a705 445#if CONFIG_PGTABLE_LEVELS >= 4
da5de7c2
JF
446 .pud_val = PTE_IDENT,
447 .make_pud = PTE_IDENT,
448
f2a6a705
KS
449 .set_p4d = native_set_p4d,
450
451#if CONFIG_PGTABLE_LEVELS >= 5
335437fb
KS
452 .p4d_val = PTE_IDENT,
453 .make_p4d = PTE_IDENT,
454
455 .set_pgd = native_set_pgd,
456#endif /* CONFIG_PGTABLE_LEVELS >= 5 */
f2a6a705 457#endif /* CONFIG_PGTABLE_LEVELS >= 4 */
98233368 458#endif /* CONFIG_PGTABLE_LEVELS >= 3 */
da181a8b 459
da5de7c2
JF
460 .pte_val = PTE_IDENT,
461 .pgd_val = PTE_IDENT,
3dc494e8 462
da5de7c2
JF
463 .make_pte = PTE_IDENT,
464 .make_pgd = PTE_IDENT,
3dc494e8 465
d6dd61c8
JF
466 .dup_mmap = paravirt_nop,
467 .exit_mmap = paravirt_nop,
468 .activate_mm = paravirt_nop,
8965c1c0
JF
469
470 .lazy_mode = {
471 .enter = paravirt_nop,
472 .leave = paravirt_nop,
511ba86e 473 .flush = paravirt_nop,
8965c1c0 474 },
aeaaa59c
JF
475
476 .set_fixmap = native_set_fixmap,
d3561b7f 477};
0dbe5a11 478
93b1eab3 479EXPORT_SYMBOL_GPL(pv_time_ops);
f97b8954
JF
480EXPORT_SYMBOL (pv_cpu_ops);
481EXPORT_SYMBOL (pv_mmu_ops);
93b1eab3
JF
482EXPORT_SYMBOL_GPL(pv_info);
483EXPORT_SYMBOL (pv_irq_ops);