blob: 09b36e9d507a27faef564956802a55141558bae8 [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>
Chris Wright5d990b62009-12-04 12:15:21 -080030#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/gfp.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070032
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070033#include <xen/xen.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070034#include <xen/interface/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080035#include <xen/interface/version.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070036#include <xen/interface/physdev.h>
37#include <xen/interface/vcpu.h>
Sheng Yangbee6ab52010-05-14 12:39:33 +010038#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070039#include <xen/features.h>
40#include <xen/page.h>
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -070041#include <xen/hvc-console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070042
43#include <asm/paravirt.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010044#include <asm/apic.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070045#include <asm/page.h>
46#include <asm/xen/hypercall.h>
47#include <asm/xen/hypervisor.h>
48#include <asm/fixmap.h>
49#include <asm/processor.h>
Jeremy Fitzhardinge707ebbc2009-03-27 11:29:02 -070050#include <asm/proto.h>
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -070051#include <asm/msr-index.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070052#include <asm/traps.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070053#include <asm/setup.h>
54#include <asm/desc.h>
Ian Campbell817a8242010-02-26 17:16:00 +000055#include <asm/pgalloc.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070056#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070057#include <asm/tlbflush.h>
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -070058#include <asm/reboot.h>
Sheng Yangbee6ab52010-05-14 12:39:33 +010059#include <asm/setup.h>
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -070060#include <asm/stackprotector.h>
Sheng Yangbee6ab52010-05-14 12:39:33 +010061#include <asm/hypervisor.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070062
63#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070064#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070065#include "multicalls.h"
66
67EXPORT_SYMBOL_GPL(hypercall_page);
68
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070069DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
70DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -070071
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -070072enum xen_domain_type xen_domain_type = XEN_NATIVE;
73EXPORT_SYMBOL_GPL(xen_domain_type);
74
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070075struct start_info *xen_start_info;
76EXPORT_SYMBOL_GPL(xen_start_info);
77
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010078struct shared_info xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070079
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -080080void *xen_initial_gdt;
81
Sheng Yangbee6ab52010-05-14 12:39:33 +010082RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
83
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070084/*
85 * Point at some empty memory to start with. We map the real shared_info
86 * page as soon as fixmap is up and running.
87 */
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010088struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070089
90/*
91 * Flag to determine whether vcpu info placement is available on all
92 * VCPUs. We assume it is to start with, and then set it to zero on
93 * the first failure. This is because it can succeed on some VCPUs
94 * and not others, since it can involve hypervisor memory allocation,
95 * or because the guest failed to guarantee all the appropriate
96 * constraints on all VCPUs (ie buffer can't cross a page boundary).
97 *
98 * Note that any particular CPU may be using a placed vcpu structure,
99 * but we can only optimise if the all are.
100 *
101 * 0: not available, 1: available
102 */
Jeremy Fitzhardingee4d04072009-02-02 13:55:54 -0800103static int have_vcpu_info_placement = 1;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700104
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100105static void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700106{
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700107 struct vcpu_register_vcpu_info info;
108 int err;
109 struct vcpu_info *vcpup;
110
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +0100111 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700112 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700113
114 if (!have_vcpu_info_placement)
115 return; /* already tested, not available */
116
117 vcpup = &per_cpu(xen_vcpu_info, cpu);
118
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800119 info.mfn = arbitrary_virt_to_mfn(vcpup);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700120 info.offset = offset_in_page(vcpup);
121
Jeremy Fitzhardingee3d26972007-10-16 11:51:31 -0700122 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700123 cpu, vcpup, info.mfn, info.offset);
124
125 /* Check to see if the hypervisor will put the vcpu_info
126 structure where we want it, which allows direct access via
127 a percpu-variable. */
128 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
129
130 if (err) {
131 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
132 have_vcpu_info_placement = 0;
133 } else {
134 /* This cpu is using the registered vcpu info, even if
135 later ones fail to. */
136 per_cpu(xen_vcpu, cpu) = vcpup;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700137
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700138 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
139 cpu, vcpup);
140 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700141}
142
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100143/*
144 * On restore, set the vcpu placement up again.
145 * If it fails, then we're in a bad state, since
146 * we can't back out from using it...
147 */
148void xen_vcpu_restore(void)
149{
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800150 int cpu;
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100151
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800152 for_each_online_cpu(cpu) {
153 bool other_cpu = (cpu != smp_processor_id());
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100154
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800155 if (other_cpu &&
156 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
157 BUG();
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100158
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800159 xen_setup_runstate_info(cpu);
Ian Campbellbe012922009-11-21 08:35:55 +0800160
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800161 if (have_vcpu_info_placement)
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100162 xen_vcpu_setup(cpu);
163
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800164 if (other_cpu &&
165 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
166 BUG();
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100167 }
168}
169
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700170static void __init xen_banner(void)
171{
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700172 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
173 struct xen_extraversion extra;
174 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
175
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700176 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700177 pv_info.name);
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700178 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
179 version >> 16, version & 0xffff, extra.extraversion,
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700180 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700181}
182
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800183static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
184static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
185
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100186static void xen_cpuid(unsigned int *ax, unsigned int *bx,
187 unsigned int *cx, unsigned int *dx)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700188{
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700189 unsigned maskebx = ~0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800190 unsigned maskecx = ~0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700191 unsigned maskedx = ~0;
192
193 /*
194 * Mask out inconvenient features, to try and disable as many
195 * unsupported kernel subsystems as possible.
196 */
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700197 switch (*ax) {
198 case 1:
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800199 maskecx = cpuid_leaf1_ecx_mask;
200 maskedx = cpuid_leaf1_edx_mask;
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700201 break;
202
203 case 0xb:
204 /* Suppress extended topology stuff */
205 maskebx = 0;
206 break;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800207 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700208
209 asm(XEN_EMULATE_PREFIX "cpuid"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100210 : "=a" (*ax),
211 "=b" (*bx),
212 "=c" (*cx),
213 "=d" (*dx)
214 : "0" (*ax), "2" (*cx));
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800215
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700216 *bx &= maskebx;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800217 *cx &= maskecx;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100218 *dx &= maskedx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700219}
220
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800221static __init void xen_init_cpuid_mask(void)
222{
223 unsigned int ax, bx, cx, dx;
224
225 cpuid_leaf1_edx_mask =
226 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
227 (1 << X86_FEATURE_MCA) | /* disable MCA */
228 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
229
230 if (!xen_initial_domain())
231 cpuid_leaf1_edx_mask &=
232 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
233 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
234
235 ax = 1;
H. Peter Anvin7adb4df2009-08-25 21:06:03 -0700236 cx = 0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800237 xen_cpuid(&ax, &bx, &cx, &dx);
238
239 /* cpuid claims we support xsave; try enabling it to see what happens */
240 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
241 unsigned long cr4;
242
243 set_in_cr4(X86_CR4_OSXSAVE);
244
245 cr4 = read_cr4();
246
247 if ((cr4 & X86_CR4_OSXSAVE) == 0)
248 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
249
250 clear_in_cr4(X86_CR4_OSXSAVE);
251 }
252}
253
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700254static void xen_set_debugreg(int reg, unsigned long val)
255{
256 HYPERVISOR_set_debugreg(reg, val);
257}
258
259static unsigned long xen_get_debugreg(int reg)
260{
261 return HYPERVISOR_get_debugreg(reg);
262}
263
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800264static void xen_end_context_switch(struct task_struct *next)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700265{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700266 xen_mc_flush();
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800267 paravirt_end_context_switch(next);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700268}
269
270static unsigned long xen_store_tr(void)
271{
272 return 0;
273}
274
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700275/*
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700276 * Set the page permissions for a particular virtual address. If the
277 * address is a vmalloc mapping (or other non-linear mapping), then
278 * find the linear mapping of the page and also set its protections to
279 * match.
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700280 */
281static void set_aliased_prot(void *v, pgprot_t prot)
282{
283 int level;
284 pte_t *ptep;
285 pte_t pte;
286 unsigned long pfn;
287 struct page *page;
288
289 ptep = lookup_address((unsigned long)v, &level);
290 BUG_ON(ptep == NULL);
291
292 pfn = pte_pfn(*ptep);
293 page = pfn_to_page(pfn);
294
295 pte = pfn_pte(pfn, prot);
296
297 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
298 BUG();
299
300 if (!PageHighMem(page)) {
301 void *av = __va(PFN_PHYS(pfn));
302
303 if (av != v)
304 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
305 BUG();
306 } else
307 kmap_flush_unused();
308}
309
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700310static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
311{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700312 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700313 int i;
314
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700315 for(i = 0; i < entries; i += entries_per_page)
316 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700317}
318
319static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
320{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700321 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700322 int i;
323
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700324 for(i = 0; i < entries; i += entries_per_page)
325 set_aliased_prot(ldt + i, PAGE_KERNEL);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700326}
327
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700328static void xen_set_ldt(const void *addr, unsigned entries)
329{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700330 struct mmuext_op *op;
331 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
332
333 op = mcs.args;
334 op->cmd = MMUEXT_SET_LDT;
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100335 op->arg1.linear_addr = (unsigned long)addr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700336 op->arg2.nr_ents = entries;
337
338 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
339
340 xen_mc_issue(PARAVIRT_LAZY_CPU);
341}
342
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100343static void xen_load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700344{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700345 unsigned long va = dtr->address;
346 unsigned int size = dtr->size + 1;
347 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800348 unsigned long frames[pages];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700349 int f;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700350
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700351 /*
352 * A GDT can be up to 64k in size, which corresponds to 8192
353 * 8-byte entries, or 16 4k pages..
354 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700355
356 BUG_ON(size > 65536);
357 BUG_ON(va & ~PAGE_MASK);
358
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700359 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800360 int level;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700361 pte_t *ptep;
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800362 unsigned long pfn, mfn;
363 void *virt;
364
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700365 /*
366 * The GDT is per-cpu and is in the percpu data area.
367 * That can be virtually mapped, so we need to do a
368 * page-walk to get the underlying MFN for the
369 * hypercall. The page can also be in the kernel's
370 * linear range, so we need to RO that mapping too.
371 */
372 ptep = lookup_address(va, &level);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800373 BUG_ON(ptep == NULL);
374
375 pfn = pte_pfn(*ptep);
376 mfn = pfn_to_mfn(pfn);
377 virt = __va(PFN_PHYS(pfn));
378
379 frames[f] = mfn;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800380
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700381 make_lowmem_page_readonly((void *)va);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800382 make_lowmem_page_readonly(virt);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700383 }
384
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800385 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
386 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700387}
388
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700389/*
390 * load_gdt for early boot, when the gdt is only mapped once
391 */
392static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
393{
394 unsigned long va = dtr->address;
395 unsigned int size = dtr->size + 1;
396 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
397 unsigned long frames[pages];
398 int f;
399
400 /*
401 * A GDT can be up to 64k in size, which corresponds to 8192
402 * 8-byte entries, or 16 4k pages..
403 */
404
405 BUG_ON(size > 65536);
406 BUG_ON(va & ~PAGE_MASK);
407
408 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
409 pte_t pte;
410 unsigned long pfn, mfn;
411
412 pfn = virt_to_pfn(va);
413 mfn = pfn_to_mfn(pfn);
414
415 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
416
417 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
418 BUG();
419
420 frames[f] = mfn;
421 }
422
423 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
424 BUG();
425}
426
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700427static void load_TLS_descriptor(struct thread_struct *t,
428 unsigned int cpu, unsigned int i)
429{
430 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800431 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700432 struct multicall_space mc = __xen_mc_entry(0);
433
434 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
435}
436
437static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
438{
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700439 /*
Tejun Heoccbeed32009-02-09 22:17:40 +0900440 * XXX sleazy hack: If we're being called in a lazy-cpu zone
441 * and lazy gs handling is enabled, it means we're in a
442 * context switch, and %gs has just been saved. This means we
443 * can zero it out to prevent faults on exit from the
444 * hypervisor if the next process has no %gs. Either way, it
445 * has been saved, and the new value will get loaded properly.
446 * This will go away as soon as Xen has been modified to not
447 * save/restore %gs for normal hypercalls.
Eduardo Habkost8a954082008-07-08 15:07:10 -0700448 *
449 * On x86_64, this hack is not used for %gs, because gs points
450 * to KERNEL_GS_BASE (and uses it for PDA references), so we
451 * must not zero %gs on x86_64
452 *
453 * For x86_64, we need to zero %fs, otherwise we may get an
454 * exception between the new %fs descriptor being loaded and
455 * %fs being effectively cleared at __switch_to().
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700456 */
Eduardo Habkost8a954082008-07-08 15:07:10 -0700457 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
458#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900459 lazy_load_gs(0);
Eduardo Habkost8a954082008-07-08 15:07:10 -0700460#else
461 loadsegment(fs, 0);
462#endif
463 }
464
465 xen_mc_batch();
466
467 load_TLS_descriptor(t, cpu, 0);
468 load_TLS_descriptor(t, cpu, 1);
469 load_TLS_descriptor(t, cpu, 2);
470
471 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700472}
473
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700474#ifdef CONFIG_X86_64
475static void xen_load_gs_index(unsigned int idx)
476{
477 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
478 BUG();
479}
480#endif
481
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700482static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100483 const void *ptr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700484{
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700485 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100486 u64 entry = *(u64 *)ptr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700487
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700488 preempt_disable();
489
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700490 xen_mc_flush();
491 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
492 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700493
494 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700495}
496
Eduardo Habkoste176d362008-07-08 15:06:59 -0700497static int cvt_gate_to_trap(int vector, const gate_desc *val,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700498 struct trap_info *info)
499{
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700500 unsigned long addr;
501
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700502 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700503 return 0;
504
505 info->vector = vector;
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700506
507 addr = gate_offset(*val);
508#ifdef CONFIG_X86_64
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700509 /*
510 * Look for known traps using IST, and substitute them
511 * appropriately. The debugger ones are the only ones we care
512 * about. Xen will handle faults like double_fault and
513 * machine_check, so we should never see them. Warn if
514 * there's an unexpected IST-using fault handler.
515 */
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700516 if (addr == (unsigned long)debug)
517 addr = (unsigned long)xen_debug;
518 else if (addr == (unsigned long)int3)
519 addr = (unsigned long)xen_int3;
520 else if (addr == (unsigned long)stack_segment)
521 addr = (unsigned long)xen_stack_segment;
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700522 else if (addr == (unsigned long)double_fault ||
523 addr == (unsigned long)nmi) {
524 /* Don't need to handle these */
525 return 0;
526#ifdef CONFIG_X86_MCE
527 } else if (addr == (unsigned long)machine_check) {
528 return 0;
529#endif
530 } else {
531 /* Some other trap using IST? */
532 if (WARN_ON(val->ist != 0))
533 return 0;
534 }
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700535#endif /* CONFIG_X86_64 */
536 info->address = addr;
537
Eduardo Habkoste176d362008-07-08 15:06:59 -0700538 info->cs = gate_segment(*val);
539 info->flags = val->dpl;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700540 /* interrupt gates clear IF */
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700541 if (val->type == GATE_INTERRUPT)
542 info->flags |= 1 << 2;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700543
544 return 1;
545}
546
547/* Locations of each CPU's IDT */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100548static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700549
550/* Set an IDT entry. If the entry is part of the current IDT, then
551 also update Xen. */
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100552static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700553{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700554 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700555 unsigned long start, end;
556
557 preempt_disable();
558
559 start = __get_cpu_var(idt_desc).address;
560 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700561
562 xen_mc_flush();
563
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100564 native_write_idt_entry(dt, entrynum, g);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700565
566 if (p >= start && (p + 8) <= end) {
567 struct trap_info info[2];
568
569 info[1].address = 0;
570
Eduardo Habkoste176d362008-07-08 15:06:59 -0700571 if (cvt_gate_to_trap(entrynum, g, &info[0]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700572 if (HYPERVISOR_set_trap_table(info))
573 BUG();
574 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700575
576 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700577}
578
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100579static void xen_convert_trap_info(const struct desc_ptr *desc,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700580 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700581{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700582 unsigned in, out, count;
583
Eduardo Habkoste176d362008-07-08 15:06:59 -0700584 count = (desc->size+1) / sizeof(gate_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700585 BUG_ON(count > 256);
586
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700587 for (in = out = 0; in < count; in++) {
Eduardo Habkoste176d362008-07-08 15:06:59 -0700588 gate_desc *entry = (gate_desc*)(desc->address) + in;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700589
Eduardo Habkoste176d362008-07-08 15:06:59 -0700590 if (cvt_gate_to_trap(in, entry, &traps[out]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700591 out++;
592 }
593 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700594}
595
596void xen_copy_trap_info(struct trap_info *traps)
597{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100598 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700599
600 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700601}
602
603/* Load a new IDT into Xen. In principle this can be per-CPU, so we
604 hold a spinlock to protect the static traps[] array (static because
605 it avoids allocation, and saves stack space). */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100606static void xen_load_idt(const struct desc_ptr *desc)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700607{
608 static DEFINE_SPINLOCK(lock);
609 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700610
611 spin_lock(&lock);
612
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700613 __get_cpu_var(idt_desc) = *desc;
614
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700615 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700616
617 xen_mc_flush();
618 if (HYPERVISOR_set_trap_table(traps))
619 BUG();
620
621 spin_unlock(&lock);
622}
623
624/* Write a GDT descriptor entry. Ignore LDT descriptors, since
625 they're handled differently. */
626static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100627 const void *desc, int type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700628{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700629 preempt_disable();
630
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100631 switch (type) {
632 case DESC_LDT:
633 case DESC_TSS:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700634 /* ignore */
635 break;
636
637 default: {
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800638 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700639
640 xen_mc_flush();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100641 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700642 BUG();
643 }
644
645 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700646
647 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700648}
649
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700650/*
651 * Version of write_gdt_entry for use at early boot-time needed to
652 * update an entry as simply as possible.
653 */
654static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
655 const void *desc, int type)
656{
657 switch (type) {
658 case DESC_LDT:
659 case DESC_TSS:
660 /* ignore */
661 break;
662
663 default: {
664 xmaddr_t maddr = virt_to_machine(&dt[entry]);
665
666 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
667 dt[entry] = *(struct desc_struct *)desc;
668 }
669
670 }
671}
672
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100673static void xen_load_sp0(struct tss_struct *tss,
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700674 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700675{
676 struct multicall_space mcs = xen_mc_entry(0);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100677 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700678 xen_mc_issue(PARAVIRT_LAZY_CPU);
679}
680
681static void xen_set_iopl_mask(unsigned mask)
682{
683 struct physdev_set_iopl set_iopl;
684
685 /* Force the change at ring 0. */
686 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
687 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
688}
689
690static void xen_io_delay(void)
691{
692}
693
694#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700695static u32 xen_apic_read(u32 reg)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700696{
697 return 0;
698}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700699
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700700static void xen_apic_write(u32 reg, u32 val)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700701{
702 /* Warn to see if there's any stray references */
703 WARN_ON(1);
704}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700705
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700706static u64 xen_apic_icr_read(void)
707{
708 return 0;
709}
710
711static void xen_apic_icr_write(u32 low, u32 id)
712{
713 /* Warn to see if there's any stray references */
714 WARN_ON(1);
715}
716
717static void xen_apic_wait_icr_idle(void)
718{
719 return;
720}
721
Yinghai Lu94a8c3c2008-07-13 22:19:35 -0700722static u32 xen_safe_apic_wait_icr_idle(void)
723{
724 return 0;
725}
726
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800727static void set_xen_basic_apic_ops(void)
728{
729 apic->read = xen_apic_read;
730 apic->write = xen_apic_write;
731 apic->icr_read = xen_apic_icr_read;
732 apic->icr_write = xen_apic_icr_write;
733 apic->wait_icr_idle = xen_apic_wait_icr_idle;
734 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
735}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700736
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700737#endif
738
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700739
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100740static void xen_clts(void)
741{
742 struct multicall_space mcs;
743
744 mcs = xen_mc_entry(0);
745
746 MULTI_fpu_taskswitch(mcs.mc, 0);
747
748 xen_mc_issue(PARAVIRT_LAZY_CPU);
749}
750
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700751static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
752
753static unsigned long xen_read_cr0(void)
754{
755 unsigned long cr0 = percpu_read(xen_cr0_value);
756
757 if (unlikely(cr0 == 0)) {
758 cr0 = native_read_cr0();
759 percpu_write(xen_cr0_value, cr0);
760 }
761
762 return cr0;
763}
764
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100765static void xen_write_cr0(unsigned long cr0)
766{
767 struct multicall_space mcs;
768
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700769 percpu_write(xen_cr0_value, cr0);
770
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100771 /* Only pay attention to cr0.TS; everything else is
772 ignored. */
773 mcs = xen_mc_entry(0);
774
775 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
776
777 xen_mc_issue(PARAVIRT_LAZY_CPU);
778}
779
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700780static void xen_write_cr4(unsigned long cr4)
781{
Jeremy Fitzhardinge2956a352008-05-26 23:31:04 +0100782 cr4 &= ~X86_CR4_PGE;
783 cr4 &= ~X86_CR4_PSE;
784
785 native_write_cr4(cr4);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700786}
787
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700788static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
789{
790 int ret;
791
792 ret = 0;
793
Tejf63c2f22008-12-16 11:56:06 -0800794 switch (msr) {
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700795#ifdef CONFIG_X86_64
796 unsigned which;
797 u64 base;
798
799 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
800 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
801 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
802
803 set:
804 base = ((u64)high << 32) | low;
805 if (HYPERVISOR_set_segment_base(which, base) != 0)
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700806 ret = -EIO;
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700807 break;
808#endif
Jeremy Fitzhardinged89961e2008-07-24 13:48:58 -0700809
810 case MSR_STAR:
811 case MSR_CSTAR:
812 case MSR_LSTAR:
813 case MSR_SYSCALL_MASK:
814 case MSR_IA32_SYSENTER_CS:
815 case MSR_IA32_SYSENTER_ESP:
816 case MSR_IA32_SYSENTER_EIP:
817 /* Fast syscall setup is all done in hypercalls, so
818 these are all ignored. Stub them out here to stop
819 Xen console noise. */
820 break;
821
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700822 default:
823 ret = native_write_msr_safe(msr, low, high);
824 }
825
826 return ret;
827}
828
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100829void xen_setup_shared_info(void)
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700830{
831 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700832 set_fixmap(FIX_PARAVIRT_BOOTMAP,
833 xen_start_info->shared_info);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700834
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700835 HYPERVISOR_shared_info =
836 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700837 } else
838 HYPERVISOR_shared_info =
839 (struct shared_info *)__va(xen_start_info->shared_info);
840
841#ifndef CONFIG_SMP
842 /* In UP this is as good a place as any to set up shared info */
843 xen_setup_vcpu_info_placement();
844#endif
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100845
846 xen_setup_mfn_list_list();
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700847}
848
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700849/* This is called once we have the cpu_possible_map */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100850void xen_setup_vcpu_info_placement(void)
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700851{
852 int cpu;
853
854 for_each_possible_cpu(cpu)
855 xen_vcpu_setup(cpu);
856
857 /* xen_vcpu_setup managed to place the vcpu_info within the
858 percpu area for all cpus, so make use of it */
859 if (have_vcpu_info_placement) {
860 printk(KERN_INFO "Xen: using vcpu_info placement\n");
861
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800862 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
863 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
864 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
865 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700866 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700867 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700868}
869
Andi Kleenab144f52007-08-10 22:31:03 +0200870static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
871 unsigned long addr, unsigned len)
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700872{
873 char *start, *end, *reloc;
874 unsigned ret;
875
876 start = end = reloc = NULL;
877
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700878#define SITE(op, x) \
879 case PARAVIRT_PATCH(op.x): \
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700880 if (have_vcpu_info_placement) { \
881 start = (char *)xen_##x##_direct; \
882 end = xen_##x##_direct_end; \
883 reloc = xen_##x##_direct_reloc; \
884 } \
885 goto patch_site
886
887 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700888 SITE(pv_irq_ops, irq_enable);
889 SITE(pv_irq_ops, irq_disable);
890 SITE(pv_irq_ops, save_fl);
891 SITE(pv_irq_ops, restore_fl);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700892#undef SITE
893
894 patch_site:
895 if (start == NULL || (end-start) > len)
896 goto default_patch;
897
Andi Kleenab144f52007-08-10 22:31:03 +0200898 ret = paravirt_patch_insns(insnbuf, len, start, end);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700899
900 /* Note: because reloc is assigned from something that
901 appears to be an array, gcc assumes it's non-null,
902 but doesn't know its relationship with start and
903 end. */
904 if (reloc > start && reloc < end) {
905 int reloc_off = reloc - start;
Andi Kleenab144f52007-08-10 22:31:03 +0200906 long *relocp = (long *)(insnbuf + reloc_off);
907 long delta = start - (char *)addr;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700908
909 *relocp += delta;
910 }
911 break;
912
913 default_patch:
914 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200915 ret = paravirt_patch_default(type, clobbers, insnbuf,
916 addr, len);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700917 break;
918 }
919
920 return ret;
921}
922
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700923static const struct pv_info xen_info __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700924 .paravirt_enabled = 1,
925 .shared_kernel_pmd = 0,
926
927 .name = "Xen",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700928};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700929
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700930static const struct pv_init_ops xen_init_ops __initdata = {
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700931 .patch = xen_patch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700932};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700933
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700934static const struct pv_time_ops xen_time_ops __initdata = {
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700935 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700936};
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700937
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700938static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700939 .cpuid = xen_cpuid,
940
941 .set_debugreg = xen_set_debugreg,
942 .get_debugreg = xen_get_debugreg,
943
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100944 .clts = xen_clts,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700945
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700946 .read_cr0 = xen_read_cr0,
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100947 .write_cr0 = xen_write_cr0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700948
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700949 .read_cr4 = native_read_cr4,
950 .read_cr4_safe = native_read_cr4_safe,
951 .write_cr4 = xen_write_cr4,
952
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700953 .wbinvd = native_wbinvd,
954
955 .read_msr = native_read_msr_safe,
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700956 .write_msr = xen_write_msr_safe,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700957 .read_tsc = native_read_tsc,
958 .read_pmc = native_read_pmc,
959
Jeremy Fitzhardinge81e103f2008-04-17 17:40:51 +0200960 .iret = xen_iret,
Jeremy Fitzhardinged75cd22f2008-06-25 00:19:26 -0400961 .irq_enable_sysexit = xen_sysexit,
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700962#ifdef CONFIG_X86_64
963 .usergs_sysret32 = xen_sysret32,
964 .usergs_sysret64 = xen_sysret64,
965#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700966
967 .load_tr_desc = paravirt_nop,
968 .set_ldt = xen_set_ldt,
969 .load_gdt = xen_load_gdt,
970 .load_idt = xen_load_idt,
971 .load_tls = xen_load_tls,
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700972#ifdef CONFIG_X86_64
973 .load_gs_index = xen_load_gs_index,
974#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700975
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700976 .alloc_ldt = xen_alloc_ldt,
977 .free_ldt = xen_free_ldt,
978
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700979 .store_gdt = native_store_gdt,
980 .store_idt = native_store_idt,
981 .store_tr = xen_store_tr,
982
983 .write_ldt_entry = xen_write_ldt_entry,
984 .write_gdt_entry = xen_write_gdt_entry,
985 .write_idt_entry = xen_write_idt_entry,
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100986 .load_sp0 = xen_load_sp0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700987
988 .set_iopl_mask = xen_set_iopl_mask,
989 .io_delay = xen_io_delay,
990
Jeremy Fitzhardinge952d1d72008-07-08 15:07:01 -0700991 /* Xen takes care of %gs when switching to usermode for us */
992 .swapgs = paravirt_nop,
993
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800994 .start_context_switch = paravirt_start_context_switch,
995 .end_context_switch = xen_end_context_switch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700996};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700997
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700998static const struct pv_apic_ops xen_apic_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700999#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001000 .startup_ipi_hook = paravirt_nop,
1001#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001002};
1003
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001004static void xen_reboot(int reason)
1005{
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +01001006 struct sched_shutdown r = { .reason = reason };
1007
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001008#ifdef CONFIG_SMP
1009 smp_send_stop();
1010#endif
1011
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +01001012 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001013 BUG();
1014}
1015
1016static void xen_restart(char *msg)
1017{
1018 xen_reboot(SHUTDOWN_reboot);
1019}
1020
1021static void xen_emergency_restart(void)
1022{
1023 xen_reboot(SHUTDOWN_reboot);
1024}
1025
1026static void xen_machine_halt(void)
1027{
1028 xen_reboot(SHUTDOWN_poweroff);
1029}
1030
1031static void xen_crash_shutdown(struct pt_regs *regs)
1032{
1033 xen_reboot(SHUTDOWN_crash);
1034}
1035
1036static const struct machine_ops __initdata xen_machine_ops = {
1037 .restart = xen_restart,
1038 .halt = xen_machine_halt,
1039 .power_off = xen_machine_halt,
1040 .shutdown = xen_machine_halt,
1041 .crash_shutdown = xen_crash_shutdown,
1042 .emergency_restart = xen_emergency_restart,
1043};
1044
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001045/*
1046 * Set up the GDT and segment registers for -fstack-protector. Until
1047 * we do this, we have to be careful not to call any stack-protected
1048 * function, which is most of the kernel.
1049 */
1050static void __init xen_setup_stackprotector(void)
1051{
1052 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1053 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1054
1055 setup_stack_canary_segment(0);
1056 switch_to_new_gdt(0);
1057
1058 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1059 pv_cpu_ops.load_gdt = xen_load_gdt;
1060}
1061
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001062/* First C function to be called on Xen boot */
1063asmlinkage void __init xen_start_kernel(void)
1064{
1065 pgd_t *pgd;
1066
1067 if (!xen_start_info)
1068 return;
1069
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001070 xen_domain_type = XEN_PV_DOMAIN;
1071
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001072 /* Install Xen paravirt ops */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001073 pv_info = xen_info;
1074 pv_init_ops = xen_init_ops;
1075 pv_time_ops = xen_time_ops;
1076 pv_cpu_ops = xen_cpu_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001077 pv_apic_ops = xen_apic_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001078
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001079 x86_init.resources.memory_setup = xen_memory_setup;
Thomas Gleixner42bbdb42009-08-20 13:04:10 +02001080 x86_init.oem.arch_setup = xen_arch_setup;
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001081 x86_init.oem.banner = xen_banner;
Thomas Gleixner845b3942009-08-19 15:37:03 +02001082
1083 x86_init.timers.timer_init = xen_time_init;
Thomas Gleixner736deca2009-08-19 12:35:53 +02001084 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1085 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001086
Thomas Gleixner2d826402009-08-20 17:06:25 +02001087 x86_platform.calibrate_tsc = xen_tsc_khz;
Feng Tang7bd867d2009-09-10 10:48:56 +08001088 x86_platform.get_wallclock = xen_get_wallclock;
1089 x86_platform.set_wallclock = xen_set_wallclock;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001090
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001091 /*
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001092 * Set up some pagetable state before starting to set any ptes.
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001093 */
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001094
Jeremy Fitzhardinge973df352009-10-27 16:54:19 -07001095 xen_init_mmu_ops();
1096
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001097 /* Prevent unwanted bits from being set in PTEs. */
1098 __supported_pte_mask &= ~_PAGE_GLOBAL;
1099 if (!xen_initial_domain())
1100 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1101
1102 __supported_pte_mask |= _PAGE_IOMAP;
1103
Ian Campbell817a8242010-02-26 17:16:00 +00001104 /*
1105 * Prevent page tables from being allocated in highmem, even
1106 * if CONFIG_HIGHPTE is enabled.
1107 */
1108 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
1109
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001110 /* Work out if we support NX */
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001111 x86_configure_nx();
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001112
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001113 xen_setup_features();
1114
1115 /* Get mfn list */
1116 if (!xen_feature(XENFEAT_auto_translated_physmap))
1117 xen_build_dynamic_phys_to_machine();
1118
1119 /*
1120 * Set up kernel GDT and segment registers, mainly so that
1121 * -fstack-protector code can be executed.
1122 */
1123 xen_setup_stackprotector();
Jeremy Fitzhardinge0d1edf42008-07-28 11:53:57 -07001124
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001125 xen_init_irq_ops();
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -08001126 xen_init_cpuid_mask();
1127
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001128#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001129 /*
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001130 * set up the basic apic ops.
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001131 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -08001132 set_xen_basic_apic_ops();
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001133#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001134
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -07001135 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1136 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1137 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1138 }
1139
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001140 machine_ops = xen_machine_ops;
1141
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -08001142 /*
1143 * The only reliable way to retain the initial address of the
1144 * percpu gdt_page is to remember it here, so we can go and
1145 * mark it RW later, when the initial percpu area is freed.
1146 */
1147 xen_initial_gdt = &per_cpu(gdt_page, 0);
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +09001148
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -07001149 xen_smp_init();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001150
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001151 pgd = (pgd_t *)xen_start_info->pt_base;
1152
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001153 /* Don't do the full vcpu_info placement stuff until we have a
1154 possible map and a non-dummy shared_info. */
1155 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1156
Jeremy Fitzhardinge55d80852009-02-25 09:42:25 -08001157 local_irq_disable();
1158 early_boot_irqs_off();
1159
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001160 xen_raw_console_write("mapping kernel into physical memory\n");
Jeremy Fitzhardinged114e1982008-07-08 15:06:52 -07001161 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001162
1163 init_mm.pgd = pgd;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001164
1165 /* keep using Xen gdt for now; no urgent need to change it */
1166
Ian Campbelle68266b2010-01-13 10:16:08 +00001167#ifdef CONFIG_X86_32
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001168 pv_info.kernel_rpl = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001169 if (xen_feature(XENFEAT_supervisor_mode_kernel))
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001170 pv_info.kernel_rpl = 0;
Ian Campbelle68266b2010-01-13 10:16:08 +00001171#else
1172 pv_info.kernel_rpl = 0;
1173#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001174
1175 /* set the limit of our address space */
Jeremy Fitzhardingefb1d8402007-10-16 11:51:31 -07001176 xen_reserve_top();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001177
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001178#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001179 /* set up basic CPUID stuff */
1180 cpu_detect(&new_cpu_data);
1181 new_cpu_data.hard_math = 1;
Jeremy Fitzhardinged560bc62009-08-25 12:53:02 -07001182 new_cpu_data.wp_works_ok = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001183 new_cpu_data.x86_capability[0] = cpuid_edx(1);
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001184#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001185
1186 /* Poke various useful things into boot_params */
H. Peter Anvin30c82642007-10-15 17:13:22 -07001187 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1188 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1189 ? __pa(xen_start_info->mod_start) : 0;
1190 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
Jeremy Fitzhardingeb7c3c5c2008-07-08 15:07:07 -07001191 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001192
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001193 if (!xen_initial_domain()) {
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +01001194 add_preferred_console("xenboot", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001195 add_preferred_console("tty", 0, NULL);
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001196 add_preferred_console("hvc", 0, NULL);
Chris Wright5d990b62009-12-04 12:15:21 -08001197 } else {
1198 /* Make sure ACS will be enabled */
1199 pci_request_acs();
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001200 }
Chris Wright5d990b62009-12-04 12:15:21 -08001201
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001202
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001203 xen_raw_console_write("about to get started...\n");
1204
Jeremy Fitzhardinge499d19b2009-11-24 09:38:25 -08001205 xen_setup_runstate_info(0);
1206
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001207 /* Start the world */
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001208#ifdef CONFIG_X86_32
Yinghai Luf0d43102008-05-29 12:56:36 -07001209 i386_start_kernel();
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001210#else
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001211 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001212#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001213}
Sheng Yangbee6ab52010-05-14 12:39:33 +01001214
1215static uint32_t xen_cpuid_base(void)
1216{
1217 uint32_t base, eax, ebx, ecx, edx;
1218 char signature[13];
1219
1220 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
1221 cpuid(base, &eax, &ebx, &ecx, &edx);
1222 *(uint32_t *)(signature + 0) = ebx;
1223 *(uint32_t *)(signature + 4) = ecx;
1224 *(uint32_t *)(signature + 8) = edx;
1225 signature[12] = 0;
1226
1227 if (!strcmp("XenVMMXenVMM", signature) && ((eax - base) >= 2))
1228 return base;
1229 }
1230
1231 return 0;
1232}
1233
1234static int init_hvm_pv_info(int *major, int *minor)
1235{
1236 uint32_t eax, ebx, ecx, edx, pages, msr, base;
1237 u64 pfn;
1238
1239 base = xen_cpuid_base();
1240 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
1241
1242 *major = eax >> 16;
1243 *minor = eax & 0xffff;
1244 printk(KERN_INFO "Xen version %d.%d.\n", *major, *minor);
1245
1246 cpuid(base + 2, &pages, &msr, &ecx, &edx);
1247
1248 pfn = __pa(hypercall_page);
1249 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
1250
1251 xen_setup_features();
1252
1253 pv_info = xen_info;
1254 pv_info.kernel_rpl = 0;
1255
1256 xen_domain_type = XEN_HVM_DOMAIN;
1257
1258 return 0;
1259}
1260
1261static void __init init_shared_info(void)
1262{
1263 struct xen_add_to_physmap xatp;
1264 struct shared_info *shared_info_page;
1265
1266 shared_info_page = (struct shared_info *)
1267 extend_brk(PAGE_SIZE, PAGE_SIZE);
1268 xatp.domid = DOMID_SELF;
1269 xatp.idx = 0;
1270 xatp.space = XENMAPSPACE_shared_info;
1271 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
1272 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
1273 BUG();
1274
1275 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
1276
1277 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1278}
1279
1280static void __init xen_hvm_guest_init(void)
1281{
1282 int r;
1283 int major, minor;
1284
1285 r = init_hvm_pv_info(&major, &minor);
1286 if (r < 0)
1287 return;
1288
1289 init_shared_info();
1290}
1291
1292static bool __init xen_hvm_platform(void)
1293{
1294 if (xen_pv_domain())
1295 return false;
1296
1297 if (!xen_cpuid_base())
1298 return false;
1299
1300 return true;
1301}
1302
1303const __refconst struct hypervisor_x86 x86_hyper_xen_hvm = {
1304 .name = "Xen HVM",
1305 .detect = xen_hvm_platform,
1306 .init_platform = xen_hvm_guest_init,
1307};
1308EXPORT_SYMBOL(x86_hyper_xen_hvm);