Clean up duplicate includes in arch/i386/xen/
[deliverable/linux.git] / arch / x86 / xen / enlighten.c
CommitLineData
5ead97c8
JF
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>
f120f13e 18#include <linux/hardirq.h>
5ead97c8
JF
19#include <linux/percpu.h>
20#include <linux/delay.h>
21#include <linux/start_kernel.h>
22#include <linux/sched.h>
23#include <linux/bootmem.h>
24#include <linux/module.h>
f4f97b3e
JF
25#include <linux/mm.h>
26#include <linux/page-flags.h>
27#include <linux/highmem.h>
5ead97c8
JF
28
29#include <xen/interface/xen.h>
30#include <xen/interface/physdev.h>
31#include <xen/interface/vcpu.h>
fefa629a 32#include <xen/interface/sched.h>
5ead97c8
JF
33#include <xen/features.h>
34#include <xen/page.h>
35
36#include <asm/paravirt.h>
37#include <asm/page.h>
38#include <asm/xen/hypercall.h>
39#include <asm/xen/hypervisor.h>
40#include <asm/fixmap.h>
41#include <asm/processor.h>
42#include <asm/setup.h>
43#include <asm/desc.h>
44#include <asm/pgtable.h>
f87e4cac 45#include <asm/tlbflush.h>
fefa629a 46#include <asm/reboot.h>
5ead97c8
JF
47
48#include "xen-ops.h"
3b827c1b 49#include "mmu.h"
5ead97c8
JF
50#include "multicalls.h"
51
52EXPORT_SYMBOL_GPL(hypercall_page);
53
5ead97c8
JF
54DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
55DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
56DEFINE_PER_CPU(unsigned long, xen_cr3);
57
58struct start_info *xen_start_info;
59EXPORT_SYMBOL_GPL(xen_start_info);
60
60223a32
JF
61static /* __initdata */ struct shared_info dummy_shared_info;
62
63/*
64 * Point at some empty memory to start with. We map the real shared_info
65 * page as soon as fixmap is up and running.
66 */
67struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info;
68
69/*
70 * Flag to determine whether vcpu info placement is available on all
71 * VCPUs. We assume it is to start with, and then set it to zero on
72 * the first failure. This is because it can succeed on some VCPUs
73 * and not others, since it can involve hypervisor memory allocation,
74 * or because the guest failed to guarantee all the appropriate
75 * constraints on all VCPUs (ie buffer can't cross a page boundary).
76 *
77 * Note that any particular CPU may be using a placed vcpu structure,
78 * but we can only optimise if the all are.
79 *
80 * 0: not available, 1: available
81 */
82static int have_vcpu_info_placement = 1;
83
84static void __init xen_vcpu_setup(int cpu)
5ead97c8 85{
60223a32
JF
86 struct vcpu_register_vcpu_info info;
87 int err;
88 struct vcpu_info *vcpup;
89
5ead97c8 90 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
60223a32
JF
91
92 if (!have_vcpu_info_placement)
93 return; /* already tested, not available */
94
95 vcpup = &per_cpu(xen_vcpu_info, cpu);
96
97 info.mfn = virt_to_mfn(vcpup);
98 info.offset = offset_in_page(vcpup);
99
100 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n",
101 cpu, vcpup, info.mfn, info.offset);
102
103 /* Check to see if the hypervisor will put the vcpu_info
104 structure where we want it, which allows direct access via
105 a percpu-variable. */
106 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
107
108 if (err) {
109 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
110 have_vcpu_info_placement = 0;
111 } else {
112 /* This cpu is using the registered vcpu info, even if
113 later ones fail to. */
114 per_cpu(xen_vcpu, cpu) = vcpup;
6487673b 115
60223a32
JF
116 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
117 cpu, vcpup);
118 }
5ead97c8
JF
119}
120
121static void __init xen_banner(void)
122{
123 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
93b1eab3 124 pv_info.name);
5ead97c8
JF
125 printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
126}
127
128static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
129 unsigned int *ecx, unsigned int *edx)
130{
131 unsigned maskedx = ~0;
132
133 /*
134 * Mask out inconvenient features, to try and disable as many
135 * unsupported kernel subsystems as possible.
136 */
137 if (*eax == 1)
138 maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
139 (1 << X86_FEATURE_ACPI) | /* disable ACPI */
140 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
141
142 asm(XEN_EMULATE_PREFIX "cpuid"
143 : "=a" (*eax),
144 "=b" (*ebx),
145 "=c" (*ecx),
146 "=d" (*edx)
147 : "0" (*eax), "2" (*ecx));
148 *edx &= maskedx;
149}
150
151static void xen_set_debugreg(int reg, unsigned long val)
152{
153 HYPERVISOR_set_debugreg(reg, val);
154}
155
156static unsigned long xen_get_debugreg(int reg)
157{
158 return HYPERVISOR_get_debugreg(reg);
159}
160
161static unsigned long xen_save_fl(void)
162{
163 struct vcpu_info *vcpu;
164 unsigned long flags;
165
5ead97c8 166 vcpu = x86_read_percpu(xen_vcpu);
f120f13e 167
5ead97c8
JF
168 /* flag has opposite sense of mask */
169 flags = !vcpu->evtchn_upcall_mask;
5ead97c8
JF
170
171 /* convert to IF type flag
172 -0 -> 0x00000000
173 -1 -> 0xffffffff
174 */
175 return (-flags) & X86_EFLAGS_IF;
176}
177
178static void xen_restore_fl(unsigned long flags)
179{
180 struct vcpu_info *vcpu;
181
5ead97c8
JF
182 /* convert from IF type flag */
183 flags = !(flags & X86_EFLAGS_IF);
f120f13e
JF
184
185 /* There's a one instruction preempt window here. We need to
186 make sure we're don't switch CPUs between getting the vcpu
187 pointer and updating the mask. */
188 preempt_disable();
5ead97c8
JF
189 vcpu = x86_read_percpu(xen_vcpu);
190 vcpu->evtchn_upcall_mask = flags;
f120f13e 191 preempt_enable_no_resched();
5ead97c8 192
f120f13e
JF
193 /* Doesn't matter if we get preempted here, because any
194 pending event will get dealt with anyway. */
5ead97c8 195
f120f13e
JF
196 if (flags == 0) {
197 preempt_check_resched();
198 barrier(); /* unmask then check (avoid races) */
5ead97c8
JF
199 if (unlikely(vcpu->evtchn_upcall_pending))
200 force_evtchn_callback();
f120f13e 201 }
5ead97c8
JF
202}
203
204static void xen_irq_disable(void)
205{
f120f13e
JF
206 /* There's a one instruction preempt window here. We need to
207 make sure we're don't switch CPUs between getting the vcpu
208 pointer and updating the mask. */
5ead97c8 209 preempt_disable();
f120f13e 210 x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
5ead97c8
JF
211 preempt_enable_no_resched();
212}
213
214static void xen_irq_enable(void)
215{
216 struct vcpu_info *vcpu;
217
f120f13e
JF
218 /* There's a one instruction preempt window here. We need to
219 make sure we're don't switch CPUs between getting the vcpu
220 pointer and updating the mask. */
5ead97c8
JF
221 preempt_disable();
222 vcpu = x86_read_percpu(xen_vcpu);
223 vcpu->evtchn_upcall_mask = 0;
f120f13e 224 preempt_enable_no_resched();
5ead97c8 225
f120f13e
JF
226 /* Doesn't matter if we get preempted here, because any
227 pending event will get dealt with anyway. */
5ead97c8 228
f120f13e 229 barrier(); /* unmask then check (avoid races) */
5ead97c8
JF
230 if (unlikely(vcpu->evtchn_upcall_pending))
231 force_evtchn_callback();
5ead97c8
JF
232}
233
234static void xen_safe_halt(void)
235{
236 /* Blocking includes an implicit local_irq_enable(). */
237 if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
238 BUG();
239}
240
241static void xen_halt(void)
242{
243 if (irqs_disabled())
244 HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
245 else
246 xen_safe_halt();
247}
248
8965c1c0 249static void xen_leave_lazy(void)
5ead97c8 250{
8965c1c0 251 paravirt_leave_lazy(paravirt_get_lazy_mode());
5ead97c8 252 xen_mc_flush();
5ead97c8
JF
253}
254
255static unsigned long xen_store_tr(void)
256{
257 return 0;
258}
259
260static void xen_set_ldt(const void *addr, unsigned entries)
261{
262 unsigned long linear_addr = (unsigned long)addr;
263 struct mmuext_op *op;
264 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
265
266 op = mcs.args;
267 op->cmd = MMUEXT_SET_LDT;
268 if (linear_addr) {
269 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
270 xmaddr_t maddr;
271 maddr = arbitrary_virt_to_machine((unsigned long)addr);
272 linear_addr = (unsigned long)maddr.maddr;
273 }
274 op->arg1.linear_addr = linear_addr;
275 op->arg2.nr_ents = entries;
276
277 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
278
279 xen_mc_issue(PARAVIRT_LAZY_CPU);
280}
281
282static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
283{
284 unsigned long *frames;
285 unsigned long va = dtr->address;
286 unsigned int size = dtr->size + 1;
287 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
288 int f;
289 struct multicall_space mcs;
290
291 /* A GDT can be up to 64k in size, which corresponds to 8192
292 8-byte entries, or 16 4k pages.. */
293
294 BUG_ON(size > 65536);
295 BUG_ON(va & ~PAGE_MASK);
296
297 mcs = xen_mc_entry(sizeof(*frames) * pages);
298 frames = mcs.args;
299
300 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
301 frames[f] = virt_to_mfn(va);
302 make_lowmem_page_readonly((void *)va);
303 }
304
305 MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
306
307 xen_mc_issue(PARAVIRT_LAZY_CPU);
308}
309
310static void load_TLS_descriptor(struct thread_struct *t,
311 unsigned int cpu, unsigned int i)
312{
313 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
314 xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
315 struct multicall_space mc = __xen_mc_entry(0);
316
317 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
318}
319
320static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
321{
322 xen_mc_batch();
323
324 load_TLS_descriptor(t, cpu, 0);
325 load_TLS_descriptor(t, cpu, 1);
326 load_TLS_descriptor(t, cpu, 2);
327
328 xen_mc_issue(PARAVIRT_LAZY_CPU);
8b84ad94
JF
329
330 /*
331 * XXX sleazy hack: If we're being called in a lazy-cpu zone,
332 * it means we're in a context switch, and %gs has just been
333 * saved. This means we can zero it out to prevent faults on
334 * exit from the hypervisor if the next process has no %gs.
335 * Either way, it has been saved, and the new value will get
336 * loaded properly. This will go away as soon as Xen has been
337 * modified to not save/restore %gs for normal hypercalls.
338 */
8965c1c0 339 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU)
8b84ad94 340 loadsegment(gs, 0);
5ead97c8
JF
341}
342
343static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
344 u32 low, u32 high)
345{
346 unsigned long lp = (unsigned long)&dt[entrynum];
347 xmaddr_t mach_lp = virt_to_machine(lp);
348 u64 entry = (u64)high << 32 | low;
349
f120f13e
JF
350 preempt_disable();
351
5ead97c8
JF
352 xen_mc_flush();
353 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
354 BUG();
f120f13e
JF
355
356 preempt_enable();
5ead97c8
JF
357}
358
359static int cvt_gate_to_trap(int vector, u32 low, u32 high,
360 struct trap_info *info)
361{
362 u8 type, dpl;
363
364 type = (high >> 8) & 0x1f;
365 dpl = (high >> 13) & 3;
366
367 if (type != 0xf && type != 0xe)
368 return 0;
369
370 info->vector = vector;
371 info->address = (high & 0xffff0000) | (low & 0x0000ffff);
372 info->cs = low >> 16;
373 info->flags = dpl;
374 /* interrupt gates clear IF */
375 if (type == 0xe)
376 info->flags |= 4;
377
378 return 1;
379}
380
381/* Locations of each CPU's IDT */
382static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
383
384/* Set an IDT entry. If the entry is part of the current IDT, then
385 also update Xen. */
386static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
387 u32 low, u32 high)
388{
5ead97c8 389 unsigned long p = (unsigned long)&dt[entrynum];
f120f13e
JF
390 unsigned long start, end;
391
392 preempt_disable();
393
394 start = __get_cpu_var(idt_desc).address;
395 end = start + __get_cpu_var(idt_desc).size + 1;
5ead97c8
JF
396
397 xen_mc_flush();
398
399 write_dt_entry(dt, entrynum, low, high);
400
401 if (p >= start && (p + 8) <= end) {
402 struct trap_info info[2];
403
404 info[1].address = 0;
405
406 if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
407 if (HYPERVISOR_set_trap_table(info))
408 BUG();
409 }
f120f13e
JF
410
411 preempt_enable();
5ead97c8
JF
412}
413
f87e4cac
JF
414static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
415 struct trap_info *traps)
5ead97c8 416{
5ead97c8
JF
417 unsigned in, out, count;
418
5ead97c8
JF
419 count = (desc->size+1) / 8;
420 BUG_ON(count > 256);
421
5ead97c8
JF
422 for (in = out = 0; in < count; in++) {
423 const u32 *entry = (u32 *)(desc->address + in * 8);
424
425 if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
426 out++;
427 }
428 traps[out].address = 0;
f87e4cac
JF
429}
430
431void xen_copy_trap_info(struct trap_info *traps)
432{
f120f13e 433 const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
f87e4cac
JF
434
435 xen_convert_trap_info(desc, traps);
f87e4cac
JF
436}
437
438/* Load a new IDT into Xen. In principle this can be per-CPU, so we
439 hold a spinlock to protect the static traps[] array (static because
440 it avoids allocation, and saves stack space). */
441static void xen_load_idt(const struct Xgt_desc_struct *desc)
442{
443 static DEFINE_SPINLOCK(lock);
444 static struct trap_info traps[257];
f87e4cac
JF
445
446 spin_lock(&lock);
447
f120f13e
JF
448 __get_cpu_var(idt_desc) = *desc;
449
f87e4cac 450 xen_convert_trap_info(desc, traps);
5ead97c8
JF
451
452 xen_mc_flush();
453 if (HYPERVISOR_set_trap_table(traps))
454 BUG();
455
456 spin_unlock(&lock);
457}
458
459/* Write a GDT descriptor entry. Ignore LDT descriptors, since
460 they're handled differently. */
461static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
462 u32 low, u32 high)
463{
f120f13e
JF
464 preempt_disable();
465
5ead97c8
JF
466 switch ((high >> 8) & 0xff) {
467 case DESCTYPE_LDT:
468 case DESCTYPE_TSS:
469 /* ignore */
470 break;
471
472 default: {
473 xmaddr_t maddr = virt_to_machine(&dt[entry]);
474 u64 desc = (u64)high << 32 | low;
475
476 xen_mc_flush();
477 if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
478 BUG();
479 }
480
481 }
f120f13e
JF
482
483 preempt_enable();
5ead97c8
JF
484}
485
486static void xen_load_esp0(struct tss_struct *tss,
f120f13e 487 struct thread_struct *thread)
5ead97c8
JF
488{
489 struct multicall_space mcs = xen_mc_entry(0);
490 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
491 xen_mc_issue(PARAVIRT_LAZY_CPU);
492}
493
494static void xen_set_iopl_mask(unsigned mask)
495{
496 struct physdev_set_iopl set_iopl;
497
498 /* Force the change at ring 0. */
499 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
500 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
501}
502
503static void xen_io_delay(void)
504{
505}
506
507#ifdef CONFIG_X86_LOCAL_APIC
508static unsigned long xen_apic_read(unsigned long reg)
509{
510 return 0;
511}
f87e4cac
JF
512
513static void xen_apic_write(unsigned long reg, unsigned long val)
514{
515 /* Warn to see if there's any stray references */
516 WARN_ON(1);
517}
5ead97c8
JF
518#endif
519
520static void xen_flush_tlb(void)
521{
d66bf8fc
JF
522 struct mmuext_op *op;
523 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
5ead97c8 524
d66bf8fc
JF
525 op = mcs.args;
526 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
527 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
528
529 xen_mc_issue(PARAVIRT_LAZY_MMU);
5ead97c8
JF
530}
531
532static void xen_flush_tlb_single(unsigned long addr)
533{
d66bf8fc
JF
534 struct mmuext_op *op;
535 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
5ead97c8 536
d66bf8fc
JF
537 op = mcs.args;
538 op->cmd = MMUEXT_INVLPG_LOCAL;
539 op->arg1.linear_addr = addr & PAGE_MASK;
540 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
541
542 xen_mc_issue(PARAVIRT_LAZY_MMU);
5ead97c8
JF
543}
544
f87e4cac
JF
545static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
546 unsigned long va)
547{
d66bf8fc
JF
548 struct {
549 struct mmuext_op op;
550 cpumask_t mask;
551 } *args;
f87e4cac 552 cpumask_t cpumask = *cpus;
d66bf8fc 553 struct multicall_space mcs;
f87e4cac
JF
554
555 /*
556 * A couple of (to be removed) sanity checks:
557 *
558 * - current CPU must not be in mask
559 * - mask must exist :)
560 */
561 BUG_ON(cpus_empty(cpumask));
562 BUG_ON(cpu_isset(smp_processor_id(), cpumask));
563 BUG_ON(!mm);
564
565 /* If a CPU which we ran on has gone down, OK. */
566 cpus_and(cpumask, cpumask, cpu_online_map);
567 if (cpus_empty(cpumask))
568 return;
569
d66bf8fc
JF
570 mcs = xen_mc_entry(sizeof(*args));
571 args = mcs.args;
572 args->mask = cpumask;
573 args->op.arg2.vcpumask = &args->mask;
574
f87e4cac 575 if (va == TLB_FLUSH_ALL) {
d66bf8fc 576 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
f87e4cac 577 } else {
d66bf8fc
JF
578 args->op.cmd = MMUEXT_INVLPG_MULTI;
579 args->op.arg1.linear_addr = va;
f87e4cac
JF
580 }
581
d66bf8fc
JF
582 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
583
584 xen_mc_issue(PARAVIRT_LAZY_MMU);
f87e4cac
JF
585}
586
60223a32
JF
587static void xen_write_cr2(unsigned long cr2)
588{
589 x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
590}
591
5ead97c8
JF
592static unsigned long xen_read_cr2(void)
593{
594 return x86_read_percpu(xen_vcpu)->arch.cr2;
595}
596
60223a32
JF
597static unsigned long xen_read_cr2_direct(void)
598{
599 return x86_read_percpu(xen_vcpu_info.arch.cr2);
600}
601
5ead97c8
JF
602static void xen_write_cr4(unsigned long cr4)
603{
389a3c02
JF
604 /* Just ignore cr4 changes; Xen doesn't allow us to do
605 anything anyway. */
5ead97c8
JF
606}
607
5ead97c8
JF
608static unsigned long xen_read_cr3(void)
609{
610 return x86_read_percpu(xen_cr3);
611}
612
613static void xen_write_cr3(unsigned long cr3)
614{
f120f13e
JF
615 BUG_ON(preemptible());
616
5ead97c8
JF
617 if (cr3 == x86_read_percpu(xen_cr3)) {
618 /* just a simple tlb flush */
619 xen_flush_tlb();
620 return;
621 }
622
623 x86_write_percpu(xen_cr3, cr3);
624
625
626 {
627 struct mmuext_op *op;
628 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
629 unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
630
631 op = mcs.args;
632 op->cmd = MMUEXT_NEW_BASEPTR;
633 op->arg1.mfn = mfn;
634
635 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
636
637 xen_mc_issue(PARAVIRT_LAZY_CPU);
638 }
639}
640
f4f97b3e
JF
641/* Early in boot, while setting up the initial pagetable, assume
642 everything is pinned. */
9a4029fd 643static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
5ead97c8 644{
f4f97b3e 645 BUG_ON(mem_map); /* should only be used early */
5ead97c8
JF
646 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
647}
648
f4f97b3e
JF
649/* This needs to make sure the new pte page is pinned iff its being
650 attached to a pinned pagetable. */
651static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
5ead97c8 652{
f4f97b3e 653 struct page *page = pfn_to_page(pfn);
5ead97c8 654
f4f97b3e
JF
655 if (PagePinned(virt_to_page(mm->pgd))) {
656 SetPagePinned(page);
657
658 if (!PageHighMem(page))
659 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
660 else
661 /* make sure there are no stray mappings of
662 this page */
663 kmap_flush_unused();
664 }
5ead97c8
JF
665}
666
f4f97b3e 667/* This should never happen until we're OK to use struct page */
5ead97c8
JF
668static void xen_release_pt(u32 pfn)
669{
f4f97b3e
JF
670 struct page *page = pfn_to_page(pfn);
671
672 if (PagePinned(page)) {
673 if (!PageHighMem(page))
674 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
675 }
5ead97c8
JF
676}
677
f4f97b3e
JF
678#ifdef CONFIG_HIGHPTE
679static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
5ead97c8 680{
f4f97b3e
JF
681 pgprot_t prot = PAGE_KERNEL;
682
683 if (PagePinned(page))
684 prot = PAGE_KERNEL_RO;
685
686 if (0 && PageHighMem(page))
687 printk("mapping highpte %lx type %d prot %s\n",
688 page_to_pfn(page), type,
689 (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
690
691 return kmap_atomic_prot(page, type, prot);
5ead97c8 692}
f4f97b3e 693#endif
5ead97c8 694
9a4029fd
JF
695static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
696{
697 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
698 if (pte_val_ma(*ptep) & _PAGE_PRESENT)
699 pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
700 pte_val_ma(pte));
701
702 return pte;
703}
704
705/* Init-time set_pte while constructing initial pagetables, which
706 doesn't allow RO pagetable pages to be remapped RW */
707static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
708{
709 pte = mask_rw_pte(ptep, pte);
710
711 xen_set_pte(ptep, pte);
712}
713
5ead97c8
JF
714static __init void xen_pagetable_setup_start(pgd_t *base)
715{
716 pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
717
9a4029fd 718 /* special set_pte for pagetable initialization */
93b1eab3 719 pv_mmu_ops.set_pte = xen_set_pte_init;
9a4029fd 720
5ead97c8
JF
721 init_mm.pgd = base;
722 /*
723 * copy top-level of Xen-supplied pagetable into place. For
724 * !PAE we can use this as-is, but for PAE it is a stand-in
725 * while we copy the pmd pages.
726 */
727 memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
728
729 if (PTRS_PER_PMD > 1) {
730 int i;
731 /*
732 * For PAE, need to allocate new pmds, rather than
733 * share Xen's, since Xen doesn't like pmd's being
734 * shared between address spaces.
735 */
736 for (i = 0; i < PTRS_PER_PGD; i++) {
737 if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
738 pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
739
740 memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
741 PAGE_SIZE);
742
f4f97b3e 743 make_lowmem_page_readonly(pmd);
5ead97c8
JF
744
745 set_pgd(&base[i], __pgd(1 + __pa(pmd)));
746 } else
747 pgd_clear(&base[i]);
748 }
749 }
750
751 /* make sure zero_page is mapped RO so we can use it in pagetables */
752 make_lowmem_page_readonly(empty_zero_page);
753 make_lowmem_page_readonly(base);
754 /*
755 * Switch to new pagetable. This is done before
756 * pagetable_init has done anything so that the new pages
757 * added to the table can be prepared properly for Xen.
758 */
759 xen_write_cr3(__pa(base));
760}
761
762static __init void xen_pagetable_setup_done(pgd_t *base)
763{
f4f97b3e
JF
764 /* This will work as long as patching hasn't happened yet
765 (which it hasn't) */
93b1eab3
JF
766 pv_mmu_ops.alloc_pt = xen_alloc_pt;
767 pv_mmu_ops.set_pte = xen_set_pte;
f4f97b3e 768
5ead97c8
JF
769 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
770 /*
771 * Create a mapping for the shared info page.
772 * Should be set_fixmap(), but shared_info is a machine
773 * address with no corresponding pseudo-phys address.
774 */
5ead97c8
JF
775 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
776 PFN_DOWN(xen_start_info->shared_info),
777 PAGE_KERNEL);
5ead97c8
JF
778
779 HYPERVISOR_shared_info =
780 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
781
782 } else
783 HYPERVISOR_shared_info =
784 (struct shared_info *)__va(xen_start_info->shared_info);
785
f4f97b3e
JF
786 /* Actually pin the pagetable down, but we can't set PG_pinned
787 yet because the page structures don't exist yet. */
788 {
789 struct mmuext_op op;
790#ifdef CONFIG_X86_PAE
791 op.cmd = MMUEXT_PIN_L3_TABLE;
792#else
793 op.cmd = MMUEXT_PIN_L3_TABLE;
794#endif
795 op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
796 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
797 BUG();
798 }
60223a32 799}
5ead97c8 800
60223a32
JF
801/* This is called once we have the cpu_possible_map */
802void __init xen_setup_vcpu_info_placement(void)
803{
804 int cpu;
805
806 for_each_possible_cpu(cpu)
807 xen_vcpu_setup(cpu);
808
809 /* xen_vcpu_setup managed to place the vcpu_info within the
810 percpu area for all cpus, so make use of it */
811 if (have_vcpu_info_placement) {
812 printk(KERN_INFO "Xen: using vcpu_info placement\n");
813
93b1eab3
JF
814 pv_irq_ops.save_fl = xen_save_fl_direct;
815 pv_irq_ops.restore_fl = xen_restore_fl_direct;
816 pv_irq_ops.irq_disable = xen_irq_disable_direct;
817 pv_irq_ops.irq_enable = xen_irq_enable_direct;
818 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
819 pv_cpu_ops.iret = xen_iret_direct;
60223a32 820 }
5ead97c8
JF
821}
822
ab144f5e
AK
823static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
824 unsigned long addr, unsigned len)
6487673b
JF
825{
826 char *start, *end, *reloc;
827 unsigned ret;
828
829 start = end = reloc = NULL;
830
93b1eab3
JF
831#define SITE(op, x) \
832 case PARAVIRT_PATCH(op.x): \
6487673b
JF
833 if (have_vcpu_info_placement) { \
834 start = (char *)xen_##x##_direct; \
835 end = xen_##x##_direct_end; \
836 reloc = xen_##x##_direct_reloc; \
837 } \
838 goto patch_site
839
840 switch (type) {
93b1eab3
JF
841 SITE(pv_irq_ops, irq_enable);
842 SITE(pv_irq_ops, irq_disable);
843 SITE(pv_irq_ops, save_fl);
844 SITE(pv_irq_ops, restore_fl);
6487673b
JF
845#undef SITE
846
847 patch_site:
848 if (start == NULL || (end-start) > len)
849 goto default_patch;
850
ab144f5e 851 ret = paravirt_patch_insns(insnbuf, len, start, end);
6487673b
JF
852
853 /* Note: because reloc is assigned from something that
854 appears to be an array, gcc assumes it's non-null,
855 but doesn't know its relationship with start and
856 end. */
857 if (reloc > start && reloc < end) {
858 int reloc_off = reloc - start;
ab144f5e
AK
859 long *relocp = (long *)(insnbuf + reloc_off);
860 long delta = start - (char *)addr;
6487673b
JF
861
862 *relocp += delta;
863 }
864 break;
865
866 default_patch:
867 default:
ab144f5e
AK
868 ret = paravirt_patch_default(type, clobbers, insnbuf,
869 addr, len);
6487673b
JF
870 break;
871 }
872
873 return ret;
874}
875
93b1eab3 876static const struct pv_info xen_info __initdata = {
5ead97c8
JF
877 .paravirt_enabled = 1,
878 .shared_kernel_pmd = 0,
879
880 .name = "Xen",
93b1eab3 881};
5ead97c8 882
93b1eab3 883static const struct pv_init_ops xen_init_ops __initdata = {
6487673b 884 .patch = xen_patch,
5ead97c8 885
93b1eab3 886 .banner = xen_banner,
5ead97c8
JF
887 .memory_setup = xen_memory_setup,
888 .arch_setup = xen_arch_setup,
f4f97b3e 889 .post_allocator_init = xen_mark_init_mm_pinned,
93b1eab3 890};
5ead97c8 891
93b1eab3 892static const struct pv_time_ops xen_time_ops __initdata = {
15c84731 893 .time_init = xen_time_init,
93b1eab3 894
15c84731
JF
895 .set_wallclock = xen_set_wallclock,
896 .get_wallclock = xen_get_wallclock,
897 .get_cpu_khz = xen_cpu_khz,
ab550288 898 .sched_clock = xen_sched_clock,
93b1eab3 899};
15c84731 900
93b1eab3 901static const struct pv_cpu_ops xen_cpu_ops __initdata = {
5ead97c8
JF
902 .cpuid = xen_cpuid,
903
904 .set_debugreg = xen_set_debugreg,
905 .get_debugreg = xen_get_debugreg,
906
907 .clts = native_clts,
908
909 .read_cr0 = native_read_cr0,
910 .write_cr0 = native_write_cr0,
911
5ead97c8
JF
912 .read_cr4 = native_read_cr4,
913 .read_cr4_safe = native_read_cr4_safe,
914 .write_cr4 = xen_write_cr4,
915
5ead97c8
JF
916 .wbinvd = native_wbinvd,
917
918 .read_msr = native_read_msr_safe,
919 .write_msr = native_write_msr_safe,
920 .read_tsc = native_read_tsc,
921 .read_pmc = native_read_pmc,
922
923 .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
924 .irq_enable_sysexit = NULL, /* never called */
925
926 .load_tr_desc = paravirt_nop,
927 .set_ldt = xen_set_ldt,
928 .load_gdt = xen_load_gdt,
929 .load_idt = xen_load_idt,
930 .load_tls = xen_load_tls,
931
932 .store_gdt = native_store_gdt,
933 .store_idt = native_store_idt,
934 .store_tr = xen_store_tr,
935
936 .write_ldt_entry = xen_write_ldt_entry,
937 .write_gdt_entry = xen_write_gdt_entry,
938 .write_idt_entry = xen_write_idt_entry,
939 .load_esp0 = xen_load_esp0,
940
941 .set_iopl_mask = xen_set_iopl_mask,
942 .io_delay = xen_io_delay,
8965c1c0
JF
943
944 .lazy_mode = {
945 .enter = paravirt_enter_lazy_cpu,
946 .leave = xen_leave_lazy,
947 },
93b1eab3
JF
948};
949
950static const struct pv_irq_ops xen_irq_ops __initdata = {
951 .init_IRQ = xen_init_IRQ,
952 .save_fl = xen_save_fl,
953 .restore_fl = xen_restore_fl,
954 .irq_disable = xen_irq_disable,
955 .irq_enable = xen_irq_enable,
956 .safe_halt = xen_safe_halt,
957 .halt = xen_halt,
958};
5ead97c8 959
93b1eab3 960static const struct pv_apic_ops xen_apic_ops __initdata = {
5ead97c8 961#ifdef CONFIG_X86_LOCAL_APIC
f87e4cac
JF
962 .apic_write = xen_apic_write,
963 .apic_write_atomic = xen_apic_write,
5ead97c8
JF
964 .apic_read = xen_apic_read,
965 .setup_boot_clock = paravirt_nop,
966 .setup_secondary_clock = paravirt_nop,
967 .startup_ipi_hook = paravirt_nop,
968#endif
93b1eab3
JF
969};
970
971static const struct pv_mmu_ops xen_mmu_ops __initdata = {
972 .pagetable_setup_start = xen_pagetable_setup_start,
973 .pagetable_setup_done = xen_pagetable_setup_done,
974
975 .read_cr2 = xen_read_cr2,
976 .write_cr2 = xen_write_cr2,
977
978 .read_cr3 = xen_read_cr3,
979 .write_cr3 = xen_write_cr3,
5ead97c8
JF
980
981 .flush_tlb_user = xen_flush_tlb,
982 .flush_tlb_kernel = xen_flush_tlb,
983 .flush_tlb_single = xen_flush_tlb_single,
f87e4cac 984 .flush_tlb_others = xen_flush_tlb_others,
5ead97c8
JF
985
986 .pte_update = paravirt_nop,
987 .pte_update_defer = paravirt_nop,
988
f4f97b3e 989 .alloc_pt = xen_alloc_pt_init,
5ead97c8 990 .release_pt = xen_release_pt,
f4f97b3e
JF
991 .alloc_pd = paravirt_nop,
992 .alloc_pd_clone = paravirt_nop,
993 .release_pd = paravirt_nop,
994
995#ifdef CONFIG_HIGHPTE
996 .kmap_atomic_pte = xen_kmap_atomic_pte,
997#endif
5ead97c8 998
9a4029fd 999 .set_pte = NULL, /* see xen_pagetable_setup_* */
3b827c1b
JF
1000 .set_pte_at = xen_set_pte_at,
1001 .set_pmd = xen_set_pmd,
1002
1003 .pte_val = xen_pte_val,
1004 .pgd_val = xen_pgd_val,
1005
1006 .make_pte = xen_make_pte,
1007 .make_pgd = xen_make_pgd,
1008
1009#ifdef CONFIG_X86_PAE
1010 .set_pte_atomic = xen_set_pte_atomic,
1011 .set_pte_present = xen_set_pte_at,
1012 .set_pud = xen_set_pud,
1013 .pte_clear = xen_pte_clear,
1014 .pmd_clear = xen_pmd_clear,
1015
1016 .make_pmd = xen_make_pmd,
1017 .pmd_val = xen_pmd_val,
1018#endif /* PAE */
1019
1020 .activate_mm = xen_activate_mm,
1021 .dup_mmap = xen_dup_mmap,
1022 .exit_mmap = xen_exit_mmap,
1023
8965c1c0
JF
1024 .lazy_mode = {
1025 .enter = paravirt_enter_lazy_mmu,
1026 .leave = xen_leave_lazy,
1027 },
5ead97c8
JF
1028};
1029
f87e4cac
JF
1030#ifdef CONFIG_SMP
1031static const struct smp_ops xen_smp_ops __initdata = {
1032 .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
1033 .smp_prepare_cpus = xen_smp_prepare_cpus,
1034 .cpu_up = xen_cpu_up,
1035 .smp_cpus_done = xen_smp_cpus_done,
1036
1037 .smp_send_stop = xen_smp_send_stop,
1038 .smp_send_reschedule = xen_smp_send_reschedule,
1039 .smp_call_function_mask = xen_smp_call_function_mask,
1040};
1041#endif /* CONFIG_SMP */
1042
fefa629a
JF
1043static void xen_reboot(int reason)
1044{
1045#ifdef CONFIG_SMP
1046 smp_send_stop();
1047#endif
1048
1049 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
1050 BUG();
1051}
1052
1053static void xen_restart(char *msg)
1054{
1055 xen_reboot(SHUTDOWN_reboot);
1056}
1057
1058static void xen_emergency_restart(void)
1059{
1060 xen_reboot(SHUTDOWN_reboot);
1061}
1062
1063static void xen_machine_halt(void)
1064{
1065 xen_reboot(SHUTDOWN_poweroff);
1066}
1067
1068static void xen_crash_shutdown(struct pt_regs *regs)
1069{
1070 xen_reboot(SHUTDOWN_crash);
1071}
1072
1073static const struct machine_ops __initdata xen_machine_ops = {
1074 .restart = xen_restart,
1075 .halt = xen_machine_halt,
1076 .power_off = xen_machine_halt,
1077 .shutdown = xen_machine_halt,
1078 .crash_shutdown = xen_crash_shutdown,
1079 .emergency_restart = xen_emergency_restart,
1080};
1081
6487673b 1082
5ead97c8
JF
1083/* First C function to be called on Xen boot */
1084asmlinkage void __init xen_start_kernel(void)
1085{
1086 pgd_t *pgd;
1087
1088 if (!xen_start_info)
1089 return;
1090
1091 BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
1092
1093 /* Install Xen paravirt ops */
93b1eab3
JF
1094 pv_info = xen_info;
1095 pv_init_ops = xen_init_ops;
1096 pv_time_ops = xen_time_ops;
1097 pv_cpu_ops = xen_cpu_ops;
1098 pv_irq_ops = xen_irq_ops;
1099 pv_apic_ops = xen_apic_ops;
1100 pv_mmu_ops = xen_mmu_ops;
93b1eab3 1101
fefa629a
JF
1102 machine_ops = xen_machine_ops;
1103
f87e4cac
JF
1104#ifdef CONFIG_SMP
1105 smp_ops = xen_smp_ops;
1106#endif
5ead97c8
JF
1107
1108 xen_setup_features();
1109
1110 /* Get mfn list */
1111 if (!xen_feature(XENFEAT_auto_translated_physmap))
1112 phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
1113
1114 pgd = (pgd_t *)xen_start_info->pt_base;
1115
1116 init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
1117
1118 init_mm.pgd = pgd; /* use the Xen pagetables to start */
1119
1120 /* keep using Xen gdt for now; no urgent need to change it */
1121
1122 x86_write_percpu(xen_cr3, __pa(pgd));
60223a32
JF
1123
1124#ifdef CONFIG_SMP
1125 /* Don't do the full vcpu_info placement stuff until we have a
1126 possible map. */
1127 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1128#else
1129 /* May as well do it now, since there's no good time to call
1130 it later on UP. */
1131 xen_setup_vcpu_info_placement();
1132#endif
5ead97c8 1133
93b1eab3 1134 pv_info.kernel_rpl = 1;
5ead97c8 1135 if (xen_feature(XENFEAT_supervisor_mode_kernel))
93b1eab3 1136 pv_info.kernel_rpl = 0;
5ead97c8
JF
1137
1138 /* set the limit of our address space */
1139 reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
1140
1141 /* set up basic CPUID stuff */
1142 cpu_detect(&new_cpu_data);
1143 new_cpu_data.hard_math = 1;
1144 new_cpu_data.x86_capability[0] = cpuid_edx(1);
1145
1146 /* Poke various useful things into boot_params */
1147 LOADER_TYPE = (9 << 4) | 0;
1148 INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
1149 INITRD_SIZE = xen_start_info->mod_len;
1150
1151 /* Start the world */
1152 start_kernel();
1153}
This page took 0.151958 seconds and 5 git commands to generate.