]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - arch/x86/xen/enlighten.c
xen: mask extended topology info in cpuid
[mirror_ubuntu-hirsute-kernel.git] / arch / x86 / xen / enlighten.c
1 /*
2 * Core of Xen paravirt_ops implementation.
3 *
4 * This file contains the xen_paravirt_ops structure itself, and the
5 * implementations for:
6 * - privileged instructions
7 * - interrupt flags
8 * - segment operations
9 * - booting and setup
10 *
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/smp.h>
17 #include <linux/preempt.h>
18 #include <linux/hardirq.h>
19 #include <linux/percpu.h>
20 #include <linux/delay.h>
21 #include <linux/start_kernel.h>
22 #include <linux/sched.h>
23 #include <linux/kprobes.h>
24 #include <linux/bootmem.h>
25 #include <linux/module.h>
26 #include <linux/mm.h>
27 #include <linux/page-flags.h>
28 #include <linux/highmem.h>
29 #include <linux/console.h>
30
31 #include <xen/interface/xen.h>
32 #include <xen/interface/version.h>
33 #include <xen/interface/physdev.h>
34 #include <xen/interface/vcpu.h>
35 #include <xen/features.h>
36 #include <xen/page.h>
37 #include <xen/hvc-console.h>
38
39 #include <asm/paravirt.h>
40 #include <asm/apic.h>
41 #include <asm/page.h>
42 #include <asm/xen/hypercall.h>
43 #include <asm/xen/hypervisor.h>
44 #include <asm/fixmap.h>
45 #include <asm/processor.h>
46 #include <asm/proto.h>
47 #include <asm/msr-index.h>
48 #include <asm/traps.h>
49 #include <asm/setup.h>
50 #include <asm/desc.h>
51 #include <asm/pgtable.h>
52 #include <asm/tlbflush.h>
53 #include <asm/reboot.h>
54 #include <asm/stackprotector.h>
55
56 #include "xen-ops.h"
57 #include "mmu.h"
58 #include "multicalls.h"
59
60 EXPORT_SYMBOL_GPL(hypercall_page);
61
62 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
63 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
64
65 enum xen_domain_type xen_domain_type = XEN_NATIVE;
66 EXPORT_SYMBOL_GPL(xen_domain_type);
67
68 struct start_info *xen_start_info;
69 EXPORT_SYMBOL_GPL(xen_start_info);
70
71 struct shared_info xen_dummy_shared_info;
72
73 void *xen_initial_gdt;
74
75 /*
76 * Point at some empty memory to start with. We map the real shared_info
77 * page as soon as fixmap is up and running.
78 */
79 struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
80
81 /*
82 * Flag to determine whether vcpu info placement is available on all
83 * VCPUs. We assume it is to start with, and then set it to zero on
84 * the first failure. This is because it can succeed on some VCPUs
85 * and not others, since it can involve hypervisor memory allocation,
86 * or because the guest failed to guarantee all the appropriate
87 * constraints on all VCPUs (ie buffer can't cross a page boundary).
88 *
89 * Note that any particular CPU may be using a placed vcpu structure,
90 * but we can only optimise if the all are.
91 *
92 * 0: not available, 1: available
93 */
94 static int have_vcpu_info_placement = 1;
95
96 static void xen_vcpu_setup(int cpu)
97 {
98 struct vcpu_register_vcpu_info info;
99 int err;
100 struct vcpu_info *vcpup;
101
102 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
103 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
104
105 if (!have_vcpu_info_placement)
106 return; /* already tested, not available */
107
108 vcpup = &per_cpu(xen_vcpu_info, cpu);
109
110 info.mfn = arbitrary_virt_to_mfn(vcpup);
111 info.offset = offset_in_page(vcpup);
112
113 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
114 cpu, vcpup, info.mfn, info.offset);
115
116 /* Check to see if the hypervisor will put the vcpu_info
117 structure where we want it, which allows direct access via
118 a percpu-variable. */
119 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
120
121 if (err) {
122 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
123 have_vcpu_info_placement = 0;
124 } else {
125 /* This cpu is using the registered vcpu info, even if
126 later ones fail to. */
127 per_cpu(xen_vcpu, cpu) = vcpup;
128
129 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
130 cpu, vcpup);
131 }
132 }
133
134 /*
135 * On restore, set the vcpu placement up again.
136 * If it fails, then we're in a bad state, since
137 * we can't back out from using it...
138 */
139 void xen_vcpu_restore(void)
140 {
141 if (have_vcpu_info_placement) {
142 int cpu;
143
144 for_each_online_cpu(cpu) {
145 bool other_cpu = (cpu != smp_processor_id());
146
147 if (other_cpu &&
148 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
149 BUG();
150
151 xen_vcpu_setup(cpu);
152
153 if (other_cpu &&
154 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
155 BUG();
156 }
157
158 BUG_ON(!have_vcpu_info_placement);
159 }
160 }
161
162 static void __init xen_banner(void)
163 {
164 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
165 struct xen_extraversion extra;
166 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
167
168 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
169 pv_info.name);
170 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
171 version >> 16, version & 0xffff, extra.extraversion,
172 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
173 }
174
175 static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
176 static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
177
178 static void xen_cpuid(unsigned int *ax, unsigned int *bx,
179 unsigned int *cx, unsigned int *dx)
180 {
181 unsigned maskebx = ~0;
182 unsigned maskecx = ~0;
183 unsigned maskedx = ~0;
184
185 /*
186 * Mask out inconvenient features, to try and disable as many
187 * unsupported kernel subsystems as possible.
188 */
189 switch (*ax) {
190 case 1:
191 maskecx = cpuid_leaf1_ecx_mask;
192 maskedx = cpuid_leaf1_edx_mask;
193 break;
194
195 case 0xb:
196 /* Suppress extended topology stuff */
197 maskebx = 0;
198 break;
199 }
200
201 asm(XEN_EMULATE_PREFIX "cpuid"
202 : "=a" (*ax),
203 "=b" (*bx),
204 "=c" (*cx),
205 "=d" (*dx)
206 : "0" (*ax), "2" (*cx));
207
208 *bx &= maskebx;
209 *cx &= maskecx;
210 *dx &= maskedx;
211 }
212
213 static __init void xen_init_cpuid_mask(void)
214 {
215 unsigned int ax, bx, cx, dx;
216
217 cpuid_leaf1_edx_mask =
218 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
219 (1 << X86_FEATURE_MCA) | /* disable MCA */
220 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
221
222 if (!xen_initial_domain())
223 cpuid_leaf1_edx_mask &=
224 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
225 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
226
227 ax = 1;
228 cx = 0;
229 xen_cpuid(&ax, &bx, &cx, &dx);
230
231 /* cpuid claims we support xsave; try enabling it to see what happens */
232 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
233 unsigned long cr4;
234
235 set_in_cr4(X86_CR4_OSXSAVE);
236
237 cr4 = read_cr4();
238
239 if ((cr4 & X86_CR4_OSXSAVE) == 0)
240 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
241
242 clear_in_cr4(X86_CR4_OSXSAVE);
243 }
244 }
245
246 static void xen_set_debugreg(int reg, unsigned long val)
247 {
248 HYPERVISOR_set_debugreg(reg, val);
249 }
250
251 static unsigned long xen_get_debugreg(int reg)
252 {
253 return HYPERVISOR_get_debugreg(reg);
254 }
255
256 static void xen_end_context_switch(struct task_struct *next)
257 {
258 xen_mc_flush();
259 paravirt_end_context_switch(next);
260 }
261
262 static unsigned long xen_store_tr(void)
263 {
264 return 0;
265 }
266
267 /*
268 * Set the page permissions for a particular virtual address. If the
269 * address is a vmalloc mapping (or other non-linear mapping), then
270 * find the linear mapping of the page and also set its protections to
271 * match.
272 */
273 static void set_aliased_prot(void *v, pgprot_t prot)
274 {
275 int level;
276 pte_t *ptep;
277 pte_t pte;
278 unsigned long pfn;
279 struct page *page;
280
281 ptep = lookup_address((unsigned long)v, &level);
282 BUG_ON(ptep == NULL);
283
284 pfn = pte_pfn(*ptep);
285 page = pfn_to_page(pfn);
286
287 pte = pfn_pte(pfn, prot);
288
289 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
290 BUG();
291
292 if (!PageHighMem(page)) {
293 void *av = __va(PFN_PHYS(pfn));
294
295 if (av != v)
296 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
297 BUG();
298 } else
299 kmap_flush_unused();
300 }
301
302 static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
303 {
304 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
305 int i;
306
307 for(i = 0; i < entries; i += entries_per_page)
308 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
309 }
310
311 static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
312 {
313 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
314 int i;
315
316 for(i = 0; i < entries; i += entries_per_page)
317 set_aliased_prot(ldt + i, PAGE_KERNEL);
318 }
319
320 static void xen_set_ldt(const void *addr, unsigned entries)
321 {
322 struct mmuext_op *op;
323 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
324
325 op = mcs.args;
326 op->cmd = MMUEXT_SET_LDT;
327 op->arg1.linear_addr = (unsigned long)addr;
328 op->arg2.nr_ents = entries;
329
330 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
331
332 xen_mc_issue(PARAVIRT_LAZY_CPU);
333 }
334
335 static void xen_load_gdt(const struct desc_ptr *dtr)
336 {
337 unsigned long va = dtr->address;
338 unsigned int size = dtr->size + 1;
339 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
340 unsigned long frames[pages];
341 int f;
342
343 /*
344 * A GDT can be up to 64k in size, which corresponds to 8192
345 * 8-byte entries, or 16 4k pages..
346 */
347
348 BUG_ON(size > 65536);
349 BUG_ON(va & ~PAGE_MASK);
350
351 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
352 int level;
353 pte_t *ptep;
354 unsigned long pfn, mfn;
355 void *virt;
356
357 /*
358 * The GDT is per-cpu and is in the percpu data area.
359 * That can be virtually mapped, so we need to do a
360 * page-walk to get the underlying MFN for the
361 * hypercall. The page can also be in the kernel's
362 * linear range, so we need to RO that mapping too.
363 */
364 ptep = lookup_address(va, &level);
365 BUG_ON(ptep == NULL);
366
367 pfn = pte_pfn(*ptep);
368 mfn = pfn_to_mfn(pfn);
369 virt = __va(PFN_PHYS(pfn));
370
371 frames[f] = mfn;
372
373 make_lowmem_page_readonly((void *)va);
374 make_lowmem_page_readonly(virt);
375 }
376
377 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
378 BUG();
379 }
380
381 /*
382 * load_gdt for early boot, when the gdt is only mapped once
383 */
384 static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
385 {
386 unsigned long va = dtr->address;
387 unsigned int size = dtr->size + 1;
388 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
389 unsigned long frames[pages];
390 int f;
391
392 /*
393 * A GDT can be up to 64k in size, which corresponds to 8192
394 * 8-byte entries, or 16 4k pages..
395 */
396
397 BUG_ON(size > 65536);
398 BUG_ON(va & ~PAGE_MASK);
399
400 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
401 pte_t pte;
402 unsigned long pfn, mfn;
403
404 pfn = virt_to_pfn(va);
405 mfn = pfn_to_mfn(pfn);
406
407 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
408
409 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
410 BUG();
411
412 frames[f] = mfn;
413 }
414
415 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
416 BUG();
417 }
418
419 static void load_TLS_descriptor(struct thread_struct *t,
420 unsigned int cpu, unsigned int i)
421 {
422 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
423 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
424 struct multicall_space mc = __xen_mc_entry(0);
425
426 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
427 }
428
429 static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
430 {
431 /*
432 * XXX sleazy hack: If we're being called in a lazy-cpu zone
433 * and lazy gs handling is enabled, it means we're in a
434 * context switch, and %gs has just been saved. This means we
435 * can zero it out to prevent faults on exit from the
436 * hypervisor if the next process has no %gs. Either way, it
437 * has been saved, and the new value will get loaded properly.
438 * This will go away as soon as Xen has been modified to not
439 * save/restore %gs for normal hypercalls.
440 *
441 * On x86_64, this hack is not used for %gs, because gs points
442 * to KERNEL_GS_BASE (and uses it for PDA references), so we
443 * must not zero %gs on x86_64
444 *
445 * For x86_64, we need to zero %fs, otherwise we may get an
446 * exception between the new %fs descriptor being loaded and
447 * %fs being effectively cleared at __switch_to().
448 */
449 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
450 #ifdef CONFIG_X86_32
451 lazy_load_gs(0);
452 #else
453 loadsegment(fs, 0);
454 #endif
455 }
456
457 xen_mc_batch();
458
459 load_TLS_descriptor(t, cpu, 0);
460 load_TLS_descriptor(t, cpu, 1);
461 load_TLS_descriptor(t, cpu, 2);
462
463 xen_mc_issue(PARAVIRT_LAZY_CPU);
464 }
465
466 #ifdef CONFIG_X86_64
467 static void xen_load_gs_index(unsigned int idx)
468 {
469 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
470 BUG();
471 }
472 #endif
473
474 static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
475 const void *ptr)
476 {
477 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
478 u64 entry = *(u64 *)ptr;
479
480 preempt_disable();
481
482 xen_mc_flush();
483 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
484 BUG();
485
486 preempt_enable();
487 }
488
489 static int cvt_gate_to_trap(int vector, const gate_desc *val,
490 struct trap_info *info)
491 {
492 unsigned long addr;
493
494 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
495 return 0;
496
497 info->vector = vector;
498
499 addr = gate_offset(*val);
500 #ifdef CONFIG_X86_64
501 /*
502 * Look for known traps using IST, and substitute them
503 * appropriately. The debugger ones are the only ones we care
504 * about. Xen will handle faults like double_fault and
505 * machine_check, so we should never see them. Warn if
506 * there's an unexpected IST-using fault handler.
507 */
508 if (addr == (unsigned long)debug)
509 addr = (unsigned long)xen_debug;
510 else if (addr == (unsigned long)int3)
511 addr = (unsigned long)xen_int3;
512 else if (addr == (unsigned long)stack_segment)
513 addr = (unsigned long)xen_stack_segment;
514 else if (addr == (unsigned long)double_fault ||
515 addr == (unsigned long)nmi) {
516 /* Don't need to handle these */
517 return 0;
518 #ifdef CONFIG_X86_MCE
519 } else if (addr == (unsigned long)machine_check) {
520 return 0;
521 #endif
522 } else {
523 /* Some other trap using IST? */
524 if (WARN_ON(val->ist != 0))
525 return 0;
526 }
527 #endif /* CONFIG_X86_64 */
528 info->address = addr;
529
530 info->cs = gate_segment(*val);
531 info->flags = val->dpl;
532 /* interrupt gates clear IF */
533 if (val->type == GATE_INTERRUPT)
534 info->flags |= 1 << 2;
535
536 return 1;
537 }
538
539 /* Locations of each CPU's IDT */
540 static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
541
542 /* Set an IDT entry. If the entry is part of the current IDT, then
543 also update Xen. */
544 static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
545 {
546 unsigned long p = (unsigned long)&dt[entrynum];
547 unsigned long start, end;
548
549 preempt_disable();
550
551 start = __get_cpu_var(idt_desc).address;
552 end = start + __get_cpu_var(idt_desc).size + 1;
553
554 xen_mc_flush();
555
556 native_write_idt_entry(dt, entrynum, g);
557
558 if (p >= start && (p + 8) <= end) {
559 struct trap_info info[2];
560
561 info[1].address = 0;
562
563 if (cvt_gate_to_trap(entrynum, g, &info[0]))
564 if (HYPERVISOR_set_trap_table(info))
565 BUG();
566 }
567
568 preempt_enable();
569 }
570
571 static void xen_convert_trap_info(const struct desc_ptr *desc,
572 struct trap_info *traps)
573 {
574 unsigned in, out, count;
575
576 count = (desc->size+1) / sizeof(gate_desc);
577 BUG_ON(count > 256);
578
579 for (in = out = 0; in < count; in++) {
580 gate_desc *entry = (gate_desc*)(desc->address) + in;
581
582 if (cvt_gate_to_trap(in, entry, &traps[out]))
583 out++;
584 }
585 traps[out].address = 0;
586 }
587
588 void xen_copy_trap_info(struct trap_info *traps)
589 {
590 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
591
592 xen_convert_trap_info(desc, traps);
593 }
594
595 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
596 hold a spinlock to protect the static traps[] array (static because
597 it avoids allocation, and saves stack space). */
598 static void xen_load_idt(const struct desc_ptr *desc)
599 {
600 static DEFINE_SPINLOCK(lock);
601 static struct trap_info traps[257];
602
603 spin_lock(&lock);
604
605 __get_cpu_var(idt_desc) = *desc;
606
607 xen_convert_trap_info(desc, traps);
608
609 xen_mc_flush();
610 if (HYPERVISOR_set_trap_table(traps))
611 BUG();
612
613 spin_unlock(&lock);
614 }
615
616 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
617 they're handled differently. */
618 static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
619 const void *desc, int type)
620 {
621 preempt_disable();
622
623 switch (type) {
624 case DESC_LDT:
625 case DESC_TSS:
626 /* ignore */
627 break;
628
629 default: {
630 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
631
632 xen_mc_flush();
633 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
634 BUG();
635 }
636
637 }
638
639 preempt_enable();
640 }
641
642 /*
643 * Version of write_gdt_entry for use at early boot-time needed to
644 * update an entry as simply as possible.
645 */
646 static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
647 const void *desc, int type)
648 {
649 switch (type) {
650 case DESC_LDT:
651 case DESC_TSS:
652 /* ignore */
653 break;
654
655 default: {
656 xmaddr_t maddr = virt_to_machine(&dt[entry]);
657
658 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
659 dt[entry] = *(struct desc_struct *)desc;
660 }
661
662 }
663 }
664
665 static void xen_load_sp0(struct tss_struct *tss,
666 struct thread_struct *thread)
667 {
668 struct multicall_space mcs = xen_mc_entry(0);
669 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
670 xen_mc_issue(PARAVIRT_LAZY_CPU);
671 }
672
673 static void xen_set_iopl_mask(unsigned mask)
674 {
675 struct physdev_set_iopl set_iopl;
676
677 /* Force the change at ring 0. */
678 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
679 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
680 }
681
682 static void xen_io_delay(void)
683 {
684 }
685
686 #ifdef CONFIG_X86_LOCAL_APIC
687 static u32 xen_apic_read(u32 reg)
688 {
689 return 0;
690 }
691
692 static void xen_apic_write(u32 reg, u32 val)
693 {
694 /* Warn to see if there's any stray references */
695 WARN_ON(1);
696 }
697
698 static u64 xen_apic_icr_read(void)
699 {
700 return 0;
701 }
702
703 static void xen_apic_icr_write(u32 low, u32 id)
704 {
705 /* Warn to see if there's any stray references */
706 WARN_ON(1);
707 }
708
709 static void xen_apic_wait_icr_idle(void)
710 {
711 return;
712 }
713
714 static u32 xen_safe_apic_wait_icr_idle(void)
715 {
716 return 0;
717 }
718
719 static void set_xen_basic_apic_ops(void)
720 {
721 apic->read = xen_apic_read;
722 apic->write = xen_apic_write;
723 apic->icr_read = xen_apic_icr_read;
724 apic->icr_write = xen_apic_icr_write;
725 apic->wait_icr_idle = xen_apic_wait_icr_idle;
726 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
727 }
728
729 #endif
730
731
732 static void xen_clts(void)
733 {
734 struct multicall_space mcs;
735
736 mcs = xen_mc_entry(0);
737
738 MULTI_fpu_taskswitch(mcs.mc, 0);
739
740 xen_mc_issue(PARAVIRT_LAZY_CPU);
741 }
742
743 static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
744
745 static unsigned long xen_read_cr0(void)
746 {
747 unsigned long cr0 = percpu_read(xen_cr0_value);
748
749 if (unlikely(cr0 == 0)) {
750 cr0 = native_read_cr0();
751 percpu_write(xen_cr0_value, cr0);
752 }
753
754 return cr0;
755 }
756
757 static void xen_write_cr0(unsigned long cr0)
758 {
759 struct multicall_space mcs;
760
761 percpu_write(xen_cr0_value, cr0);
762
763 /* Only pay attention to cr0.TS; everything else is
764 ignored. */
765 mcs = xen_mc_entry(0);
766
767 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
768
769 xen_mc_issue(PARAVIRT_LAZY_CPU);
770 }
771
772 static void xen_write_cr4(unsigned long cr4)
773 {
774 cr4 &= ~X86_CR4_PGE;
775 cr4 &= ~X86_CR4_PSE;
776
777 native_write_cr4(cr4);
778 }
779
780 static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
781 {
782 int ret;
783
784 ret = 0;
785
786 switch (msr) {
787 #ifdef CONFIG_X86_64
788 unsigned which;
789 u64 base;
790
791 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
792 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
793 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
794
795 set:
796 base = ((u64)high << 32) | low;
797 if (HYPERVISOR_set_segment_base(which, base) != 0)
798 ret = -EIO;
799 break;
800 #endif
801
802 case MSR_STAR:
803 case MSR_CSTAR:
804 case MSR_LSTAR:
805 case MSR_SYSCALL_MASK:
806 case MSR_IA32_SYSENTER_CS:
807 case MSR_IA32_SYSENTER_ESP:
808 case MSR_IA32_SYSENTER_EIP:
809 /* Fast syscall setup is all done in hypercalls, so
810 these are all ignored. Stub them out here to stop
811 Xen console noise. */
812 break;
813
814 default:
815 ret = native_write_msr_safe(msr, low, high);
816 }
817
818 return ret;
819 }
820
821 void xen_setup_shared_info(void)
822 {
823 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
824 set_fixmap(FIX_PARAVIRT_BOOTMAP,
825 xen_start_info->shared_info);
826
827 HYPERVISOR_shared_info =
828 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
829 } else
830 HYPERVISOR_shared_info =
831 (struct shared_info *)__va(xen_start_info->shared_info);
832
833 #ifndef CONFIG_SMP
834 /* In UP this is as good a place as any to set up shared info */
835 xen_setup_vcpu_info_placement();
836 #endif
837
838 xen_setup_mfn_list_list();
839 }
840
841 /* This is called once we have the cpu_possible_map */
842 void xen_setup_vcpu_info_placement(void)
843 {
844 int cpu;
845
846 for_each_possible_cpu(cpu)
847 xen_vcpu_setup(cpu);
848
849 /* xen_vcpu_setup managed to place the vcpu_info within the
850 percpu area for all cpus, so make use of it */
851 if (have_vcpu_info_placement) {
852 printk(KERN_INFO "Xen: using vcpu_info placement\n");
853
854 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
855 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
856 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
857 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
858 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
859 }
860 }
861
862 static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
863 unsigned long addr, unsigned len)
864 {
865 char *start, *end, *reloc;
866 unsigned ret;
867
868 start = end = reloc = NULL;
869
870 #define SITE(op, x) \
871 case PARAVIRT_PATCH(op.x): \
872 if (have_vcpu_info_placement) { \
873 start = (char *)xen_##x##_direct; \
874 end = xen_##x##_direct_end; \
875 reloc = xen_##x##_direct_reloc; \
876 } \
877 goto patch_site
878
879 switch (type) {
880 SITE(pv_irq_ops, irq_enable);
881 SITE(pv_irq_ops, irq_disable);
882 SITE(pv_irq_ops, save_fl);
883 SITE(pv_irq_ops, restore_fl);
884 #undef SITE
885
886 patch_site:
887 if (start == NULL || (end-start) > len)
888 goto default_patch;
889
890 ret = paravirt_patch_insns(insnbuf, len, start, end);
891
892 /* Note: because reloc is assigned from something that
893 appears to be an array, gcc assumes it's non-null,
894 but doesn't know its relationship with start and
895 end. */
896 if (reloc > start && reloc < end) {
897 int reloc_off = reloc - start;
898 long *relocp = (long *)(insnbuf + reloc_off);
899 long delta = start - (char *)addr;
900
901 *relocp += delta;
902 }
903 break;
904
905 default_patch:
906 default:
907 ret = paravirt_patch_default(type, clobbers, insnbuf,
908 addr, len);
909 break;
910 }
911
912 return ret;
913 }
914
915 static const struct pv_info xen_info __initdata = {
916 .paravirt_enabled = 1,
917 .shared_kernel_pmd = 0,
918
919 .name = "Xen",
920 };
921
922 static const struct pv_init_ops xen_init_ops __initdata = {
923 .patch = xen_patch,
924 };
925
926 static const struct pv_time_ops xen_time_ops __initdata = {
927 .sched_clock = xen_sched_clock,
928 };
929
930 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
931 .cpuid = xen_cpuid,
932
933 .set_debugreg = xen_set_debugreg,
934 .get_debugreg = xen_get_debugreg,
935
936 .clts = xen_clts,
937
938 .read_cr0 = xen_read_cr0,
939 .write_cr0 = xen_write_cr0,
940
941 .read_cr4 = native_read_cr4,
942 .read_cr4_safe = native_read_cr4_safe,
943 .write_cr4 = xen_write_cr4,
944
945 .wbinvd = native_wbinvd,
946
947 .read_msr = native_read_msr_safe,
948 .write_msr = xen_write_msr_safe,
949 .read_tsc = native_read_tsc,
950 .read_pmc = native_read_pmc,
951
952 .iret = xen_iret,
953 .irq_enable_sysexit = xen_sysexit,
954 #ifdef CONFIG_X86_64
955 .usergs_sysret32 = xen_sysret32,
956 .usergs_sysret64 = xen_sysret64,
957 #endif
958
959 .load_tr_desc = paravirt_nop,
960 .set_ldt = xen_set_ldt,
961 .load_gdt = xen_load_gdt,
962 .load_idt = xen_load_idt,
963 .load_tls = xen_load_tls,
964 #ifdef CONFIG_X86_64
965 .load_gs_index = xen_load_gs_index,
966 #endif
967
968 .alloc_ldt = xen_alloc_ldt,
969 .free_ldt = xen_free_ldt,
970
971 .store_gdt = native_store_gdt,
972 .store_idt = native_store_idt,
973 .store_tr = xen_store_tr,
974
975 .write_ldt_entry = xen_write_ldt_entry,
976 .write_gdt_entry = xen_write_gdt_entry,
977 .write_idt_entry = xen_write_idt_entry,
978 .load_sp0 = xen_load_sp0,
979
980 .set_iopl_mask = xen_set_iopl_mask,
981 .io_delay = xen_io_delay,
982
983 /* Xen takes care of %gs when switching to usermode for us */
984 .swapgs = paravirt_nop,
985
986 .start_context_switch = paravirt_start_context_switch,
987 .end_context_switch = xen_end_context_switch,
988 };
989
990 static const struct pv_apic_ops xen_apic_ops __initdata = {
991 #ifdef CONFIG_X86_LOCAL_APIC
992 .startup_ipi_hook = paravirt_nop,
993 #endif
994 };
995
996 static void xen_reboot(int reason)
997 {
998 struct sched_shutdown r = { .reason = reason };
999
1000 #ifdef CONFIG_SMP
1001 smp_send_stop();
1002 #endif
1003
1004 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
1005 BUG();
1006 }
1007
1008 static void xen_restart(char *msg)
1009 {
1010 xen_reboot(SHUTDOWN_reboot);
1011 }
1012
1013 static void xen_emergency_restart(void)
1014 {
1015 xen_reboot(SHUTDOWN_reboot);
1016 }
1017
1018 static void xen_machine_halt(void)
1019 {
1020 xen_reboot(SHUTDOWN_poweroff);
1021 }
1022
1023 static void xen_crash_shutdown(struct pt_regs *regs)
1024 {
1025 xen_reboot(SHUTDOWN_crash);
1026 }
1027
1028 static const struct machine_ops __initdata xen_machine_ops = {
1029 .restart = xen_restart,
1030 .halt = xen_machine_halt,
1031 .power_off = xen_machine_halt,
1032 .shutdown = xen_machine_halt,
1033 .crash_shutdown = xen_crash_shutdown,
1034 .emergency_restart = xen_emergency_restart,
1035 };
1036
1037 /*
1038 * Set up the GDT and segment registers for -fstack-protector. Until
1039 * we do this, we have to be careful not to call any stack-protected
1040 * function, which is most of the kernel.
1041 */
1042 static void __init xen_setup_stackprotector(void)
1043 {
1044 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1045 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1046
1047 setup_stack_canary_segment(0);
1048 switch_to_new_gdt(0);
1049
1050 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1051 pv_cpu_ops.load_gdt = xen_load_gdt;
1052 }
1053
1054 /* First C function to be called on Xen boot */
1055 asmlinkage void __init xen_start_kernel(void)
1056 {
1057 pgd_t *pgd;
1058
1059 if (!xen_start_info)
1060 return;
1061
1062 xen_domain_type = XEN_PV_DOMAIN;
1063
1064 /* Install Xen paravirt ops */
1065 pv_info = xen_info;
1066 pv_init_ops = xen_init_ops;
1067 pv_time_ops = xen_time_ops;
1068 pv_cpu_ops = xen_cpu_ops;
1069 pv_apic_ops = xen_apic_ops;
1070
1071 x86_init.resources.memory_setup = xen_memory_setup;
1072 x86_init.oem.arch_setup = xen_arch_setup;
1073 x86_init.oem.banner = xen_banner;
1074
1075 x86_init.timers.timer_init = xen_time_init;
1076 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1077 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
1078
1079 x86_platform.calibrate_tsc = xen_tsc_khz;
1080 x86_platform.get_wallclock = xen_get_wallclock;
1081 x86_platform.set_wallclock = xen_set_wallclock;
1082
1083 /*
1084 * Set up some pagetable state before starting to set any ptes.
1085 */
1086
1087 xen_init_mmu_ops();
1088
1089 /* Prevent unwanted bits from being set in PTEs. */
1090 __supported_pte_mask &= ~_PAGE_GLOBAL;
1091 if (!xen_initial_domain())
1092 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1093
1094 __supported_pte_mask |= _PAGE_IOMAP;
1095
1096 #ifdef CONFIG_X86_64
1097 /* Work out if we support NX */
1098 check_efer();
1099 #endif
1100
1101 xen_setup_features();
1102
1103 /* Get mfn list */
1104 if (!xen_feature(XENFEAT_auto_translated_physmap))
1105 xen_build_dynamic_phys_to_machine();
1106
1107 /*
1108 * Set up kernel GDT and segment registers, mainly so that
1109 * -fstack-protector code can be executed.
1110 */
1111 xen_setup_stackprotector();
1112
1113 xen_init_irq_ops();
1114 xen_init_cpuid_mask();
1115
1116 #ifdef CONFIG_X86_LOCAL_APIC
1117 /*
1118 * set up the basic apic ops.
1119 */
1120 set_xen_basic_apic_ops();
1121 #endif
1122
1123 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1124 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1125 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1126 }
1127
1128 machine_ops = xen_machine_ops;
1129
1130 /*
1131 * The only reliable way to retain the initial address of the
1132 * percpu gdt_page is to remember it here, so we can go and
1133 * mark it RW later, when the initial percpu area is freed.
1134 */
1135 xen_initial_gdt = &per_cpu(gdt_page, 0);
1136
1137 xen_smp_init();
1138
1139 pgd = (pgd_t *)xen_start_info->pt_base;
1140
1141 /* Don't do the full vcpu_info placement stuff until we have a
1142 possible map and a non-dummy shared_info. */
1143 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1144
1145 local_irq_disable();
1146 early_boot_irqs_off();
1147
1148 xen_raw_console_write("mapping kernel into physical memory\n");
1149 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
1150
1151 init_mm.pgd = pgd;
1152
1153 /* keep using Xen gdt for now; no urgent need to change it */
1154
1155 pv_info.kernel_rpl = 1;
1156 if (xen_feature(XENFEAT_supervisor_mode_kernel))
1157 pv_info.kernel_rpl = 0;
1158
1159 /* set the limit of our address space */
1160 xen_reserve_top();
1161
1162 #ifdef CONFIG_X86_32
1163 /* set up basic CPUID stuff */
1164 cpu_detect(&new_cpu_data);
1165 new_cpu_data.hard_math = 1;
1166 new_cpu_data.wp_works_ok = 1;
1167 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1168 #endif
1169
1170 /* Poke various useful things into boot_params */
1171 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1172 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1173 ? __pa(xen_start_info->mod_start) : 0;
1174 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
1175 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
1176
1177 if (!xen_initial_domain()) {
1178 add_preferred_console("xenboot", 0, NULL);
1179 add_preferred_console("tty", 0, NULL);
1180 add_preferred_console("hvc", 0, NULL);
1181 }
1182
1183 xen_raw_console_write("about to get started...\n");
1184
1185 /* Start the world */
1186 #ifdef CONFIG_X86_32
1187 i386_start_kernel();
1188 #else
1189 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
1190 #endif
1191 }