blob: 5bccd706232c21c3cae9afbf97242fdfe2ad60af [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
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>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070018#include <linux/hardirq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070019#include <linux/percpu.h>
20#include <linux/delay.h>
21#include <linux/start_kernel.h>
22#include <linux/sched.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070023#include <linux/kprobes.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070024#include <linux/bootmem.h>
25#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070026#include <linux/mm.h>
27#include <linux/page-flags.h>
28#include <linux/highmem.h>
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +010029#include <linux/console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070030
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070031#include <xen/xen.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070032#include <xen/interface/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080033#include <xen/interface/version.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070034#include <xen/interface/physdev.h>
35#include <xen/interface/vcpu.h>
36#include <xen/features.h>
37#include <xen/page.h>
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -070038#include <xen/hvc-console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070039
40#include <asm/paravirt.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010041#include <asm/apic.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070042#include <asm/page.h>
43#include <asm/xen/hypercall.h>
44#include <asm/xen/hypervisor.h>
45#include <asm/fixmap.h>
46#include <asm/processor.h>
Jeremy Fitzhardinge707ebbc2009-03-27 11:29:02 -070047#include <asm/proto.h>
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -070048#include <asm/msr-index.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070049#include <asm/traps.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070050#include <asm/setup.h>
51#include <asm/desc.h>
52#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070053#include <asm/tlbflush.h>
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -070054#include <asm/reboot.h>
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -070055#include <asm/stackprotector.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070056
57#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070058#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070059#include "multicalls.h"
60
61EXPORT_SYMBOL_GPL(hypercall_page);
62
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070063DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
64DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -070065
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -070066enum xen_domain_type xen_domain_type = XEN_NATIVE;
67EXPORT_SYMBOL_GPL(xen_domain_type);
68
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070069struct start_info *xen_start_info;
70EXPORT_SYMBOL_GPL(xen_start_info);
71
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010072struct shared_info xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070073
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -080074void *xen_initial_gdt;
75
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070076/*
77 * Point at some empty memory to start with. We map the real shared_info
78 * page as soon as fixmap is up and running.
79 */
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010080struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070081
82/*
83 * Flag to determine whether vcpu info placement is available on all
84 * VCPUs. We assume it is to start with, and then set it to zero on
85 * the first failure. This is because it can succeed on some VCPUs
86 * and not others, since it can involve hypervisor memory allocation,
87 * or because the guest failed to guarantee all the appropriate
88 * constraints on all VCPUs (ie buffer can't cross a page boundary).
89 *
90 * Note that any particular CPU may be using a placed vcpu structure,
91 * but we can only optimise if the all are.
92 *
93 * 0: not available, 1: available
94 */
Jeremy Fitzhardingee4d04072009-02-02 13:55:54 -080095static int have_vcpu_info_placement = 1;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070096
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +010097static void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070098{
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070099 struct vcpu_register_vcpu_info info;
100 int err;
101 struct vcpu_info *vcpup;
102
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +0100103 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700104 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700105
106 if (!have_vcpu_info_placement)
107 return; /* already tested, not available */
108
109 vcpup = &per_cpu(xen_vcpu_info, cpu);
110
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800111 info.mfn = arbitrary_virt_to_mfn(vcpup);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700112 info.offset = offset_in_page(vcpup);
113
Jeremy Fitzhardingee3d26972007-10-16 11:51:31 -0700114 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700115 cpu, vcpup, info.mfn, info.offset);
116
117 /* Check to see if the hypervisor will put the vcpu_info
118 structure where we want it, which allows direct access via
119 a percpu-variable. */
120 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
121
122 if (err) {
123 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
124 have_vcpu_info_placement = 0;
125 } else {
126 /* This cpu is using the registered vcpu info, even if
127 later ones fail to. */
128 per_cpu(xen_vcpu, cpu) = vcpup;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700129
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700130 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
131 cpu, vcpup);
132 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700133}
134
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100135/*
136 * On restore, set the vcpu placement up again.
137 * If it fails, then we're in a bad state, since
138 * we can't back out from using it...
139 */
140void xen_vcpu_restore(void)
141{
142 if (have_vcpu_info_placement) {
143 int cpu;
144
145 for_each_online_cpu(cpu) {
146 bool other_cpu = (cpu != smp_processor_id());
147
148 if (other_cpu &&
149 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
150 BUG();
151
152 xen_vcpu_setup(cpu);
153
154 if (other_cpu &&
155 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
156 BUG();
157 }
158
159 BUG_ON(!have_vcpu_info_placement);
160 }
161}
162
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700163static void __init xen_banner(void)
164{
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700165 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
166 struct xen_extraversion extra;
167 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
168
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700169 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700170 pv_info.name);
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700171 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
172 version >> 16, version & 0xffff, extra.extraversion,
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700173 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700174}
175
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800176static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
177static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
178
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100179static void xen_cpuid(unsigned int *ax, unsigned int *bx,
180 unsigned int *cx, unsigned int *dx)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700181{
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800182 unsigned maskecx = ~0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700183 unsigned maskedx = ~0;
184
185 /*
186 * Mask out inconvenient features, to try and disable as many
187 * unsupported kernel subsystems as possible.
188 */
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800189 if (*ax == 1) {
190 maskecx = cpuid_leaf1_ecx_mask;
191 maskedx = cpuid_leaf1_edx_mask;
192 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700193
194 asm(XEN_EMULATE_PREFIX "cpuid"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100195 : "=a" (*ax),
196 "=b" (*bx),
197 "=c" (*cx),
198 "=d" (*dx)
199 : "0" (*ax), "2" (*cx));
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800200
201 *cx &= maskecx;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100202 *dx &= maskedx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700203}
204
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800205static __init void xen_init_cpuid_mask(void)
206{
207 unsigned int ax, bx, cx, dx;
208
209 cpuid_leaf1_edx_mask =
210 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
211 (1 << X86_FEATURE_MCA) | /* disable MCA */
212 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
213
214 if (!xen_initial_domain())
215 cpuid_leaf1_edx_mask &=
216 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
217 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
218
219 ax = 1;
H. Peter Anvin7adb4df2009-08-25 21:06:03 -0700220 cx = 0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800221 xen_cpuid(&ax, &bx, &cx, &dx);
222
223 /* cpuid claims we support xsave; try enabling it to see what happens */
224 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
225 unsigned long cr4;
226
227 set_in_cr4(X86_CR4_OSXSAVE);
228
229 cr4 = read_cr4();
230
231 if ((cr4 & X86_CR4_OSXSAVE) == 0)
232 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
233
234 clear_in_cr4(X86_CR4_OSXSAVE);
235 }
236}
237
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700238static void xen_set_debugreg(int reg, unsigned long val)
239{
240 HYPERVISOR_set_debugreg(reg, val);
241}
242
243static unsigned long xen_get_debugreg(int reg)
244{
245 return HYPERVISOR_get_debugreg(reg);
246}
247
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800248static void xen_end_context_switch(struct task_struct *next)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700249{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700250 xen_mc_flush();
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800251 paravirt_end_context_switch(next);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700252}
253
254static unsigned long xen_store_tr(void)
255{
256 return 0;
257}
258
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700259/*
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700260 * Set the page permissions for a particular virtual address. If the
261 * address is a vmalloc mapping (or other non-linear mapping), then
262 * find the linear mapping of the page and also set its protections to
263 * match.
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700264 */
265static void set_aliased_prot(void *v, pgprot_t prot)
266{
267 int level;
268 pte_t *ptep;
269 pte_t pte;
270 unsigned long pfn;
271 struct page *page;
272
273 ptep = lookup_address((unsigned long)v, &level);
274 BUG_ON(ptep == NULL);
275
276 pfn = pte_pfn(*ptep);
277 page = pfn_to_page(pfn);
278
279 pte = pfn_pte(pfn, prot);
280
281 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
282 BUG();
283
284 if (!PageHighMem(page)) {
285 void *av = __va(PFN_PHYS(pfn));
286
287 if (av != v)
288 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
289 BUG();
290 } else
291 kmap_flush_unused();
292}
293
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700294static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
295{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700296 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700297 int i;
298
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700299 for(i = 0; i < entries; i += entries_per_page)
300 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700301}
302
303static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
304{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700305 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700306 int i;
307
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700308 for(i = 0; i < entries; i += entries_per_page)
309 set_aliased_prot(ldt + i, PAGE_KERNEL);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700310}
311
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700312static void xen_set_ldt(const void *addr, unsigned entries)
313{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700314 struct mmuext_op *op;
315 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
316
317 op = mcs.args;
318 op->cmd = MMUEXT_SET_LDT;
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100319 op->arg1.linear_addr = (unsigned long)addr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700320 op->arg2.nr_ents = entries;
321
322 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
323
324 xen_mc_issue(PARAVIRT_LAZY_CPU);
325}
326
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100327static void xen_load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700328{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700329 unsigned long va = dtr->address;
330 unsigned int size = dtr->size + 1;
331 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800332 unsigned long frames[pages];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700333 int f;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700334
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700335 /*
336 * A GDT can be up to 64k in size, which corresponds to 8192
337 * 8-byte entries, or 16 4k pages..
338 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700339
340 BUG_ON(size > 65536);
341 BUG_ON(va & ~PAGE_MASK);
342
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700343 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800344 int level;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700345 pte_t *ptep;
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800346 unsigned long pfn, mfn;
347 void *virt;
348
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700349 /*
350 * The GDT is per-cpu and is in the percpu data area.
351 * That can be virtually mapped, so we need to do a
352 * page-walk to get the underlying MFN for the
353 * hypercall. The page can also be in the kernel's
354 * linear range, so we need to RO that mapping too.
355 */
356 ptep = lookup_address(va, &level);
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800357 BUG_ON(ptep == NULL);
358
359 pfn = pte_pfn(*ptep);
360 mfn = pfn_to_mfn(pfn);
361 virt = __va(PFN_PHYS(pfn));
362
363 frames[f] = mfn;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800364
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700365 make_lowmem_page_readonly((void *)va);
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800366 make_lowmem_page_readonly(virt);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700367 }
368
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800369 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
370 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700371}
372
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700373/*
374 * load_gdt for early boot, when the gdt is only mapped once
375 */
376static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
377{
378 unsigned long va = dtr->address;
379 unsigned int size = dtr->size + 1;
380 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
381 unsigned long frames[pages];
382 int f;
383
384 /*
385 * A GDT can be up to 64k in size, which corresponds to 8192
386 * 8-byte entries, or 16 4k pages..
387 */
388
389 BUG_ON(size > 65536);
390 BUG_ON(va & ~PAGE_MASK);
391
392 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
393 pte_t pte;
394 unsigned long pfn, mfn;
395
396 pfn = virt_to_pfn(va);
397 mfn = pfn_to_mfn(pfn);
398
399 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
400
401 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
402 BUG();
403
404 frames[f] = mfn;
405 }
406
407 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
408 BUG();
409}
410
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700411static void load_TLS_descriptor(struct thread_struct *t,
412 unsigned int cpu, unsigned int i)
413{
414 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800415 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700416 struct multicall_space mc = __xen_mc_entry(0);
417
418 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
419}
420
421static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
422{
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700423 /*
Tejun Heoccbeed32009-02-09 22:17:40 +0900424 * XXX sleazy hack: If we're being called in a lazy-cpu zone
425 * and lazy gs handling is enabled, it means we're in a
426 * context switch, and %gs has just been saved. This means we
427 * can zero it out to prevent faults on exit from the
428 * hypervisor if the next process has no %gs. Either way, it
429 * has been saved, and the new value will get loaded properly.
430 * This will go away as soon as Xen has been modified to not
431 * save/restore %gs for normal hypercalls.
Eduardo Habkost8a954082008-07-08 15:07:10 -0700432 *
433 * On x86_64, this hack is not used for %gs, because gs points
434 * to KERNEL_GS_BASE (and uses it for PDA references), so we
435 * must not zero %gs on x86_64
436 *
437 * For x86_64, we need to zero %fs, otherwise we may get an
438 * exception between the new %fs descriptor being loaded and
439 * %fs being effectively cleared at __switch_to().
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700440 */
Eduardo Habkost8a954082008-07-08 15:07:10 -0700441 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
442#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900443 lazy_load_gs(0);
Eduardo Habkost8a954082008-07-08 15:07:10 -0700444#else
445 loadsegment(fs, 0);
446#endif
447 }
448
449 xen_mc_batch();
450
451 load_TLS_descriptor(t, cpu, 0);
452 load_TLS_descriptor(t, cpu, 1);
453 load_TLS_descriptor(t, cpu, 2);
454
455 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700456}
457
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700458#ifdef CONFIG_X86_64
459static void xen_load_gs_index(unsigned int idx)
460{
461 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
462 BUG();
463}
464#endif
465
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700466static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100467 const void *ptr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700468{
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700469 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100470 u64 entry = *(u64 *)ptr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700471
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700472 preempt_disable();
473
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700474 xen_mc_flush();
475 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
476 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700477
478 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700479}
480
Eduardo Habkoste176d362008-07-08 15:06:59 -0700481static int cvt_gate_to_trap(int vector, const gate_desc *val,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700482 struct trap_info *info)
483{
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700484 unsigned long addr;
485
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700486 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700487 return 0;
488
489 info->vector = vector;
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700490
491 addr = gate_offset(*val);
492#ifdef CONFIG_X86_64
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700493 /*
494 * Look for known traps using IST, and substitute them
495 * appropriately. The debugger ones are the only ones we care
496 * about. Xen will handle faults like double_fault and
497 * machine_check, so we should never see them. Warn if
498 * there's an unexpected IST-using fault handler.
499 */
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700500 if (addr == (unsigned long)debug)
501 addr = (unsigned long)xen_debug;
502 else if (addr == (unsigned long)int3)
503 addr = (unsigned long)xen_int3;
504 else if (addr == (unsigned long)stack_segment)
505 addr = (unsigned long)xen_stack_segment;
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700506 else if (addr == (unsigned long)double_fault ||
507 addr == (unsigned long)nmi) {
508 /* Don't need to handle these */
509 return 0;
510#ifdef CONFIG_X86_MCE
511 } else if (addr == (unsigned long)machine_check) {
512 return 0;
513#endif
514 } else {
515 /* Some other trap using IST? */
516 if (WARN_ON(val->ist != 0))
517 return 0;
518 }
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700519#endif /* CONFIG_X86_64 */
520 info->address = addr;
521
Eduardo Habkoste176d362008-07-08 15:06:59 -0700522 info->cs = gate_segment(*val);
523 info->flags = val->dpl;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700524 /* interrupt gates clear IF */
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700525 if (val->type == GATE_INTERRUPT)
526 info->flags |= 1 << 2;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700527
528 return 1;
529}
530
531/* Locations of each CPU's IDT */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100532static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700533
534/* Set an IDT entry. If the entry is part of the current IDT, then
535 also update Xen. */
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100536static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700537{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700538 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700539 unsigned long start, end;
540
541 preempt_disable();
542
543 start = __get_cpu_var(idt_desc).address;
544 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700545
546 xen_mc_flush();
547
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100548 native_write_idt_entry(dt, entrynum, g);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700549
550 if (p >= start && (p + 8) <= end) {
551 struct trap_info info[2];
552
553 info[1].address = 0;
554
Eduardo Habkoste176d362008-07-08 15:06:59 -0700555 if (cvt_gate_to_trap(entrynum, g, &info[0]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700556 if (HYPERVISOR_set_trap_table(info))
557 BUG();
558 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700559
560 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700561}
562
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100563static void xen_convert_trap_info(const struct desc_ptr *desc,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700564 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700565{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700566 unsigned in, out, count;
567
Eduardo Habkoste176d362008-07-08 15:06:59 -0700568 count = (desc->size+1) / sizeof(gate_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700569 BUG_ON(count > 256);
570
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700571 for (in = out = 0; in < count; in++) {
Eduardo Habkoste176d362008-07-08 15:06:59 -0700572 gate_desc *entry = (gate_desc*)(desc->address) + in;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700573
Eduardo Habkoste176d362008-07-08 15:06:59 -0700574 if (cvt_gate_to_trap(in, entry, &traps[out]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700575 out++;
576 }
577 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700578}
579
580void xen_copy_trap_info(struct trap_info *traps)
581{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100582 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700583
584 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700585}
586
587/* Load a new IDT into Xen. In principle this can be per-CPU, so we
588 hold a spinlock to protect the static traps[] array (static because
589 it avoids allocation, and saves stack space). */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100590static void xen_load_idt(const struct desc_ptr *desc)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700591{
592 static DEFINE_SPINLOCK(lock);
593 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700594
595 spin_lock(&lock);
596
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700597 __get_cpu_var(idt_desc) = *desc;
598
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700599 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700600
601 xen_mc_flush();
602 if (HYPERVISOR_set_trap_table(traps))
603 BUG();
604
605 spin_unlock(&lock);
606}
607
608/* Write a GDT descriptor entry. Ignore LDT descriptors, since
609 they're handled differently. */
610static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100611 const void *desc, int type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700612{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700613 preempt_disable();
614
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100615 switch (type) {
616 case DESC_LDT:
617 case DESC_TSS:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700618 /* ignore */
619 break;
620
621 default: {
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800622 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700623
624 xen_mc_flush();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100625 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700626 BUG();
627 }
628
629 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700630
631 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700632}
633
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700634/*
635 * Version of write_gdt_entry for use at early boot-time needed to
636 * update an entry as simply as possible.
637 */
638static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
639 const void *desc, int type)
640{
641 switch (type) {
642 case DESC_LDT:
643 case DESC_TSS:
644 /* ignore */
645 break;
646
647 default: {
648 xmaddr_t maddr = virt_to_machine(&dt[entry]);
649
650 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
651 dt[entry] = *(struct desc_struct *)desc;
652 }
653
654 }
655}
656
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100657static void xen_load_sp0(struct tss_struct *tss,
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700658 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700659{
660 struct multicall_space mcs = xen_mc_entry(0);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100661 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700662 xen_mc_issue(PARAVIRT_LAZY_CPU);
663}
664
665static void xen_set_iopl_mask(unsigned mask)
666{
667 struct physdev_set_iopl set_iopl;
668
669 /* Force the change at ring 0. */
670 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
671 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
672}
673
674static void xen_io_delay(void)
675{
676}
677
678#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700679static u32 xen_apic_read(u32 reg)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700680{
681 return 0;
682}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700683
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700684static void xen_apic_write(u32 reg, u32 val)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700685{
686 /* Warn to see if there's any stray references */
687 WARN_ON(1);
688}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700689
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700690static u64 xen_apic_icr_read(void)
691{
692 return 0;
693}
694
695static void xen_apic_icr_write(u32 low, u32 id)
696{
697 /* Warn to see if there's any stray references */
698 WARN_ON(1);
699}
700
701static void xen_apic_wait_icr_idle(void)
702{
703 return;
704}
705
Yinghai Lu94a8c3c2008-07-13 22:19:35 -0700706static u32 xen_safe_apic_wait_icr_idle(void)
707{
708 return 0;
709}
710
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800711static void set_xen_basic_apic_ops(void)
712{
713 apic->read = xen_apic_read;
714 apic->write = xen_apic_write;
715 apic->icr_read = xen_apic_icr_read;
716 apic->icr_write = xen_apic_icr_write;
717 apic->wait_icr_idle = xen_apic_wait_icr_idle;
718 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
719}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700720
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700721#endif
722
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700723
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100724static void xen_clts(void)
725{
726 struct multicall_space mcs;
727
728 mcs = xen_mc_entry(0);
729
730 MULTI_fpu_taskswitch(mcs.mc, 0);
731
732 xen_mc_issue(PARAVIRT_LAZY_CPU);
733}
734
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700735static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
736
737static unsigned long xen_read_cr0(void)
738{
739 unsigned long cr0 = percpu_read(xen_cr0_value);
740
741 if (unlikely(cr0 == 0)) {
742 cr0 = native_read_cr0();
743 percpu_write(xen_cr0_value, cr0);
744 }
745
746 return cr0;
747}
748
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100749static void xen_write_cr0(unsigned long cr0)
750{
751 struct multicall_space mcs;
752
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700753 percpu_write(xen_cr0_value, cr0);
754
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100755 /* Only pay attention to cr0.TS; everything else is
756 ignored. */
757 mcs = xen_mc_entry(0);
758
759 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
760
761 xen_mc_issue(PARAVIRT_LAZY_CPU);
762}
763
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700764static void xen_write_cr4(unsigned long cr4)
765{
Jeremy Fitzhardinge2956a352008-05-26 23:31:04 +0100766 cr4 &= ~X86_CR4_PGE;
767 cr4 &= ~X86_CR4_PSE;
768
769 native_write_cr4(cr4);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700770}
771
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700772static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
773{
774 int ret;
775
776 ret = 0;
777
Tejf63c2f22008-12-16 11:56:06 -0800778 switch (msr) {
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700779#ifdef CONFIG_X86_64
780 unsigned which;
781 u64 base;
782
783 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
784 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
785 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
786
787 set:
788 base = ((u64)high << 32) | low;
789 if (HYPERVISOR_set_segment_base(which, base) != 0)
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700790 ret = -EIO;
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700791 break;
792#endif
Jeremy Fitzhardinged89961e2008-07-24 13:48:58 -0700793
794 case MSR_STAR:
795 case MSR_CSTAR:
796 case MSR_LSTAR:
797 case MSR_SYSCALL_MASK:
798 case MSR_IA32_SYSENTER_CS:
799 case MSR_IA32_SYSENTER_ESP:
800 case MSR_IA32_SYSENTER_EIP:
801 /* Fast syscall setup is all done in hypercalls, so
802 these are all ignored. Stub them out here to stop
803 Xen console noise. */
804 break;
805
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700806 default:
807 ret = native_write_msr_safe(msr, low, high);
808 }
809
810 return ret;
811}
812
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100813void xen_setup_shared_info(void)
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700814{
815 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700816 set_fixmap(FIX_PARAVIRT_BOOTMAP,
817 xen_start_info->shared_info);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700818
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700819 HYPERVISOR_shared_info =
820 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700821 } else
822 HYPERVISOR_shared_info =
823 (struct shared_info *)__va(xen_start_info->shared_info);
824
825#ifndef CONFIG_SMP
826 /* In UP this is as good a place as any to set up shared info */
827 xen_setup_vcpu_info_placement();
828#endif
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100829
830 xen_setup_mfn_list_list();
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700831}
832
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700833/* This is called once we have the cpu_possible_map */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100834void xen_setup_vcpu_info_placement(void)
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700835{
836 int cpu;
837
838 for_each_possible_cpu(cpu)
839 xen_vcpu_setup(cpu);
840
841 /* xen_vcpu_setup managed to place the vcpu_info within the
842 percpu area for all cpus, so make use of it */
843 if (have_vcpu_info_placement) {
844 printk(KERN_INFO "Xen: using vcpu_info placement\n");
845
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800846 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
847 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
848 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
849 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700850 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700851 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700852}
853
Andi Kleenab144f52007-08-10 22:31:03 +0200854static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
855 unsigned long addr, unsigned len)
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700856{
857 char *start, *end, *reloc;
858 unsigned ret;
859
860 start = end = reloc = NULL;
861
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700862#define SITE(op, x) \
863 case PARAVIRT_PATCH(op.x): \
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700864 if (have_vcpu_info_placement) { \
865 start = (char *)xen_##x##_direct; \
866 end = xen_##x##_direct_end; \
867 reloc = xen_##x##_direct_reloc; \
868 } \
869 goto patch_site
870
871 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700872 SITE(pv_irq_ops, irq_enable);
873 SITE(pv_irq_ops, irq_disable);
874 SITE(pv_irq_ops, save_fl);
875 SITE(pv_irq_ops, restore_fl);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700876#undef SITE
877
878 patch_site:
879 if (start == NULL || (end-start) > len)
880 goto default_patch;
881
Andi Kleenab144f52007-08-10 22:31:03 +0200882 ret = paravirt_patch_insns(insnbuf, len, start, end);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700883
884 /* Note: because reloc is assigned from something that
885 appears to be an array, gcc assumes it's non-null,
886 but doesn't know its relationship with start and
887 end. */
888 if (reloc > start && reloc < end) {
889 int reloc_off = reloc - start;
Andi Kleenab144f52007-08-10 22:31:03 +0200890 long *relocp = (long *)(insnbuf + reloc_off);
891 long delta = start - (char *)addr;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700892
893 *relocp += delta;
894 }
895 break;
896
897 default_patch:
898 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200899 ret = paravirt_patch_default(type, clobbers, insnbuf,
900 addr, len);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700901 break;
902 }
903
904 return ret;
905}
906
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700907static const struct pv_info xen_info __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700908 .paravirt_enabled = 1,
909 .shared_kernel_pmd = 0,
910
911 .name = "Xen",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700912};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700913
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700914static const struct pv_init_ops xen_init_ops __initdata = {
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700915 .patch = xen_patch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700916};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700917
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700918static const struct pv_time_ops xen_time_ops __initdata = {
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700919 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700920};
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700921
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700922static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700923 .cpuid = xen_cpuid,
924
925 .set_debugreg = xen_set_debugreg,
926 .get_debugreg = xen_get_debugreg,
927
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100928 .clts = xen_clts,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700929
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700930 .read_cr0 = xen_read_cr0,
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100931 .write_cr0 = xen_write_cr0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700932
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700933 .read_cr4 = native_read_cr4,
934 .read_cr4_safe = native_read_cr4_safe,
935 .write_cr4 = xen_write_cr4,
936
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700937 .wbinvd = native_wbinvd,
938
939 .read_msr = native_read_msr_safe,
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700940 .write_msr = xen_write_msr_safe,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700941 .read_tsc = native_read_tsc,
942 .read_pmc = native_read_pmc,
943
Jeremy Fitzhardinge81e103f2008-04-17 17:40:51 +0200944 .iret = xen_iret,
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400945 .irq_enable_sysexit = xen_sysexit,
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700946#ifdef CONFIG_X86_64
947 .usergs_sysret32 = xen_sysret32,
948 .usergs_sysret64 = xen_sysret64,
949#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700950
951 .load_tr_desc = paravirt_nop,
952 .set_ldt = xen_set_ldt,
953 .load_gdt = xen_load_gdt,
954 .load_idt = xen_load_idt,
955 .load_tls = xen_load_tls,
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700956#ifdef CONFIG_X86_64
957 .load_gs_index = xen_load_gs_index,
958#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700959
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700960 .alloc_ldt = xen_alloc_ldt,
961 .free_ldt = xen_free_ldt,
962
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700963 .store_gdt = native_store_gdt,
964 .store_idt = native_store_idt,
965 .store_tr = xen_store_tr,
966
967 .write_ldt_entry = xen_write_ldt_entry,
968 .write_gdt_entry = xen_write_gdt_entry,
969 .write_idt_entry = xen_write_idt_entry,
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100970 .load_sp0 = xen_load_sp0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700971
972 .set_iopl_mask = xen_set_iopl_mask,
973 .io_delay = xen_io_delay,
974
Jeremy Fitzhardinge952d1d72008-07-08 15:07:01 -0700975 /* Xen takes care of %gs when switching to usermode for us */
976 .swapgs = paravirt_nop,
977
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800978 .start_context_switch = paravirt_start_context_switch,
979 .end_context_switch = xen_end_context_switch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700980};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700981
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700982static const struct pv_apic_ops xen_apic_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700983#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700984 .startup_ipi_hook = paravirt_nop,
985#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700986};
987
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700988static void xen_reboot(int reason)
989{
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +0100990 struct sched_shutdown r = { .reason = reason };
991
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700992#ifdef CONFIG_SMP
993 smp_send_stop();
994#endif
995
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +0100996 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700997 BUG();
998}
999
1000static void xen_restart(char *msg)
1001{
1002 xen_reboot(SHUTDOWN_reboot);
1003}
1004
1005static void xen_emergency_restart(void)
1006{
1007 xen_reboot(SHUTDOWN_reboot);
1008}
1009
1010static void xen_machine_halt(void)
1011{
1012 xen_reboot(SHUTDOWN_poweroff);
1013}
1014
1015static void xen_crash_shutdown(struct pt_regs *regs)
1016{
1017 xen_reboot(SHUTDOWN_crash);
1018}
1019
1020static const struct machine_ops __initdata xen_machine_ops = {
1021 .restart = xen_restart,
1022 .halt = xen_machine_halt,
1023 .power_off = xen_machine_halt,
1024 .shutdown = xen_machine_halt,
1025 .crash_shutdown = xen_crash_shutdown,
1026 .emergency_restart = xen_emergency_restart,
1027};
1028
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001029/*
1030 * Set up the GDT and segment registers for -fstack-protector. Until
1031 * we do this, we have to be careful not to call any stack-protected
1032 * function, which is most of the kernel.
1033 */
1034static void __init xen_setup_stackprotector(void)
1035{
1036 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1037 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1038
1039 setup_stack_canary_segment(0);
1040 switch_to_new_gdt(0);
1041
1042 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1043 pv_cpu_ops.load_gdt = xen_load_gdt;
1044}
1045
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001046/* First C function to be called on Xen boot */
1047asmlinkage void __init xen_start_kernel(void)
1048{
1049 pgd_t *pgd;
1050
1051 if (!xen_start_info)
1052 return;
1053
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001054 xen_domain_type = XEN_PV_DOMAIN;
1055
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001056 /* Install Xen paravirt ops */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001057 pv_info = xen_info;
1058 pv_init_ops = xen_init_ops;
1059 pv_time_ops = xen_time_ops;
1060 pv_cpu_ops = xen_cpu_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001061 pv_apic_ops = xen_apic_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001062
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001063 x86_init.resources.memory_setup = xen_memory_setup;
Thomas Gleixner42bbdb42009-08-20 13:04:10 +02001064 x86_init.oem.arch_setup = xen_arch_setup;
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001065 x86_init.oem.banner = xen_banner;
Thomas Gleixner845b3942009-08-19 15:37:03 +02001066
1067 x86_init.timers.timer_init = xen_time_init;
Thomas Gleixner736deca2009-08-19 12:35:53 +02001068 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1069 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001070
Thomas Gleixner2d826402009-08-20 17:06:25 +02001071 x86_platform.calibrate_tsc = xen_tsc_khz;
Feng Tang7bd867d2009-09-10 10:48:56 +08001072 x86_platform.get_wallclock = xen_get_wallclock;
1073 x86_platform.set_wallclock = xen_set_wallclock;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001074
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001075 /*
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001076 * Set up some pagetable state before starting to set any ptes.
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001077 */
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001078
Jeremy Fitzhardinge973df352009-10-27 16:54:19 -07001079 xen_init_mmu_ops();
1080
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001081 /* Prevent unwanted bits from being set in PTEs. */
1082 __supported_pte_mask &= ~_PAGE_GLOBAL;
1083 if (!xen_initial_domain())
1084 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1085
1086 __supported_pte_mask |= _PAGE_IOMAP;
1087
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001088#ifdef CONFIG_X86_64
1089 /* Work out if we support NX */
1090 check_efer();
1091#endif
1092
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001093 xen_setup_features();
1094
1095 /* Get mfn list */
1096 if (!xen_feature(XENFEAT_auto_translated_physmap))
1097 xen_build_dynamic_phys_to_machine();
1098
1099 /*
1100 * Set up kernel GDT and segment registers, mainly so that
1101 * -fstack-protector code can be executed.
1102 */
1103 xen_setup_stackprotector();
Jeremy Fitzhardinge0d1edf42008-07-28 11:53:57 -07001104
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001105 xen_init_irq_ops();
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -08001106 xen_init_cpuid_mask();
1107
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001108#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001109 /*
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001110 * set up the basic apic ops.
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001111 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -08001112 set_xen_basic_apic_ops();
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001113#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001114
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -07001115 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1116 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1117 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1118 }
1119
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001120 machine_ops = xen_machine_ops;
1121
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -08001122 /*
1123 * The only reliable way to retain the initial address of the
1124 * percpu gdt_page is to remember it here, so we can go and
1125 * mark it RW later, when the initial percpu area is freed.
1126 */
1127 xen_initial_gdt = &per_cpu(gdt_page, 0);
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +09001128
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -07001129 xen_smp_init();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001130
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001131 pgd = (pgd_t *)xen_start_info->pt_base;
1132
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001133 /* Don't do the full vcpu_info placement stuff until we have a
1134 possible map and a non-dummy shared_info. */
1135 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1136
Jeremy Fitzhardinge55d80852009-02-25 09:42:25 -08001137 local_irq_disable();
1138 early_boot_irqs_off();
1139
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001140 xen_raw_console_write("mapping kernel into physical memory\n");
Jeremy Fitzhardinged114e1982008-07-08 15:06:52 -07001141 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001142
1143 init_mm.pgd = pgd;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001144
1145 /* keep using Xen gdt for now; no urgent need to change it */
1146
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001147 pv_info.kernel_rpl = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001148 if (xen_feature(XENFEAT_supervisor_mode_kernel))
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001149 pv_info.kernel_rpl = 0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001150
1151 /* set the limit of our address space */
Jeremy Fitzhardingefb1d8402007-10-16 11:51:31 -07001152 xen_reserve_top();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001153
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001154#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001155 /* set up basic CPUID stuff */
1156 cpu_detect(&new_cpu_data);
1157 new_cpu_data.hard_math = 1;
Jeremy Fitzhardinged560bc62009-08-25 12:53:02 -07001158 new_cpu_data.wp_works_ok = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001159 new_cpu_data.x86_capability[0] = cpuid_edx(1);
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001160#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001161
1162 /* Poke various useful things into boot_params */
H. Peter Anvin30c82642007-10-15 17:13:22 -07001163 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1164 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1165 ? __pa(xen_start_info->mod_start) : 0;
1166 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
Jeremy Fitzhardingeb7c3c5c2008-07-08 15:07:07 -07001167 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001168
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001169 if (!xen_initial_domain()) {
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +01001170 add_preferred_console("xenboot", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001171 add_preferred_console("tty", 0, NULL);
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001172 add_preferred_console("hvc", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001173 }
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001174
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001175 xen_raw_console_write("about to get started...\n");
1176
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001177 /* Start the world */
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001178#ifdef CONFIG_X86_32
Yinghai Luf0d43102008-05-29 12:56:36 -07001179 i386_start_kernel();
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001180#else
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001181 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001182#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001183}