blob: d48a32b10a3cdbd6c3bb8b9af75de8a3a55c0ab1 [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
Sheng Yang38e20b02010-05-14 12:40:51 +010014#include <linux/cpu.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070015#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/smp.h>
18#include <linux/preempt.h>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070019#include <linux/hardirq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070020#include <linux/percpu.h>
21#include <linux/delay.h>
22#include <linux/start_kernel.h>
23#include <linux/sched.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070024#include <linux/kprobes.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070025#include <linux/bootmem.h>
26#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070027#include <linux/mm.h>
28#include <linux/page-flags.h>
29#include <linux/highmem.h>
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +010030#include <linux/console.h>
Chris Wright5d990b62009-12-04 12:15:21 -080031#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070033
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070034#include <xen/xen.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070035#include <xen/interface/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080036#include <xen/interface/version.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070037#include <xen/interface/physdev.h>
38#include <xen/interface/vcpu.h>
Sheng Yangbee6ab532010-05-14 12:39:33 +010039#include <xen/interface/memory.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070040#include <xen/features.h>
41#include <xen/page.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010042#include <xen/hvm.h>
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -070043#include <xen/hvc-console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070044
45#include <asm/paravirt.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010046#include <asm/apic.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070047#include <asm/page.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040048#include <asm/xen/pci.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070049#include <asm/xen/hypercall.h>
50#include <asm/xen/hypervisor.h>
51#include <asm/fixmap.h>
52#include <asm/processor.h>
Jeremy Fitzhardinge707ebbc2009-03-27 11:29:02 -070053#include <asm/proto.h>
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -070054#include <asm/msr-index.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070055#include <asm/traps.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070056#include <asm/setup.h>
57#include <asm/desc.h>
Ian Campbell817a8242010-02-26 17:16:00 +000058#include <asm/pgalloc.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070059#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070060#include <asm/tlbflush.h>
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -070061#include <asm/reboot.h>
Sheng Yangbee6ab532010-05-14 12:39:33 +010062#include <asm/setup.h>
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -070063#include <asm/stackprotector.h>
Sheng Yangbee6ab532010-05-14 12:39:33 +010064#include <asm/hypervisor.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070065
66#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070067#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070068#include "multicalls.h"
69
70EXPORT_SYMBOL_GPL(hypercall_page);
71
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070072DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
73DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -070074
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -070075enum xen_domain_type xen_domain_type = XEN_NATIVE;
76EXPORT_SYMBOL_GPL(xen_domain_type);
77
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070078struct start_info *xen_start_info;
79EXPORT_SYMBOL_GPL(xen_start_info);
80
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010081struct shared_info xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070082
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -080083void *xen_initial_gdt;
84
Sheng Yangbee6ab532010-05-14 12:39:33 +010085RESERVE_BRK(shared_info_page_brk, PAGE_SIZE);
Sheng Yang38e20b02010-05-14 12:40:51 +010086__read_mostly int xen_have_vector_callback;
87EXPORT_SYMBOL_GPL(xen_have_vector_callback);
Sheng Yangbee6ab532010-05-14 12:39:33 +010088
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070089/*
90 * Point at some empty memory to start with. We map the real shared_info
91 * page as soon as fixmap is up and running.
92 */
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010093struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070094
95/*
96 * Flag to determine whether vcpu info placement is available on all
97 * VCPUs. We assume it is to start with, and then set it to zero on
98 * the first failure. This is because it can succeed on some VCPUs
99 * and not others, since it can involve hypervisor memory allocation,
100 * or because the guest failed to guarantee all the appropriate
101 * constraints on all VCPUs (ie buffer can't cross a page boundary).
102 *
103 * Note that any particular CPU may be using a placed vcpu structure,
104 * but we can only optimise if the all are.
105 *
106 * 0: not available, 1: available
107 */
Jeremy Fitzhardingee4d04072009-02-02 13:55:54 -0800108static int have_vcpu_info_placement = 1;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700109
Mukesh Rathorc06ee782010-07-19 10:25:08 -0700110static void clamp_max_cpus(void)
111{
112#ifdef CONFIG_SMP
113 if (setup_max_cpus > MAX_VIRT_CPUS)
114 setup_max_cpus = MAX_VIRT_CPUS;
115#endif
116}
117
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100118static void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700119{
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700120 struct vcpu_register_vcpu_info info;
121 int err;
122 struct vcpu_info *vcpup;
123
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +0100124 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700125
Mukesh Rathorc06ee782010-07-19 10:25:08 -0700126 if (cpu < MAX_VIRT_CPUS)
127 per_cpu(xen_vcpu,cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
128
129 if (!have_vcpu_info_placement) {
130 if (cpu >= MAX_VIRT_CPUS)
131 clamp_max_cpus();
132 return;
133 }
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700134
135 vcpup = &per_cpu(xen_vcpu_info, cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800136 info.mfn = arbitrary_virt_to_mfn(vcpup);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700137 info.offset = offset_in_page(vcpup);
138
Jeremy Fitzhardingee3d26972007-10-16 11:51:31 -0700139 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700140 cpu, vcpup, info.mfn, info.offset);
141
142 /* Check to see if the hypervisor will put the vcpu_info
143 structure where we want it, which allows direct access via
144 a percpu-variable. */
145 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
146
147 if (err) {
148 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
149 have_vcpu_info_placement = 0;
Mukesh Rathorc06ee782010-07-19 10:25:08 -0700150 clamp_max_cpus();
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700151 } else {
152 /* This cpu is using the registered vcpu info, even if
153 later ones fail to. */
154 per_cpu(xen_vcpu, cpu) = vcpup;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700155
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700156 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
157 cpu, vcpup);
158 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700159}
160
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100161/*
162 * On restore, set the vcpu placement up again.
163 * If it fails, then we're in a bad state, since
164 * we can't back out from using it...
165 */
166void xen_vcpu_restore(void)
167{
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800168 int cpu;
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100169
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800170 for_each_online_cpu(cpu) {
171 bool other_cpu = (cpu != smp_processor_id());
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100172
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800173 if (other_cpu &&
174 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
175 BUG();
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100176
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800177 xen_setup_runstate_info(cpu);
Ian Campbellbe012922009-11-21 08:35:55 +0800178
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800179 if (have_vcpu_info_placement)
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100180 xen_vcpu_setup(cpu);
181
Jeremy Fitzhardinge3905bb22009-11-21 08:46:29 +0800182 if (other_cpu &&
183 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
184 BUG();
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100185 }
186}
187
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700188static void __init xen_banner(void)
189{
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700190 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
191 struct xen_extraversion extra;
192 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
193
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700194 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700195 pv_info.name);
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700196 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
197 version >> 16, version & 0xffff, extra.extraversion,
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700198 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700199}
200
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800201static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
202static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
203
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100204static void xen_cpuid(unsigned int *ax, unsigned int *bx,
205 unsigned int *cx, unsigned int *dx)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700206{
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700207 unsigned maskebx = ~0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800208 unsigned maskecx = ~0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700209 unsigned maskedx = ~0;
210
211 /*
212 * Mask out inconvenient features, to try and disable as many
213 * unsupported kernel subsystems as possible.
214 */
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700215 switch (*ax) {
216 case 1:
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800217 maskecx = cpuid_leaf1_ecx_mask;
218 maskedx = cpuid_leaf1_edx_mask;
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700219 break;
220
221 case 0xb:
222 /* Suppress extended topology stuff */
223 maskebx = 0;
224 break;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800225 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700226
227 asm(XEN_EMULATE_PREFIX "cpuid"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100228 : "=a" (*ax),
229 "=b" (*bx),
230 "=c" (*cx),
231 "=d" (*dx)
232 : "0" (*ax), "2" (*cx));
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800233
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700234 *bx &= maskebx;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800235 *cx &= maskecx;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100236 *dx &= maskedx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700237}
238
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800239static __init void xen_init_cpuid_mask(void)
240{
241 unsigned int ax, bx, cx, dx;
242
243 cpuid_leaf1_edx_mask =
244 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
245 (1 << X86_FEATURE_MCA) | /* disable MCA */
Stefano Stabelliniff128492010-09-28 16:45:51 +0100246 (1 << X86_FEATURE_MTRR) | /* disable MTRR */
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800247 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
248
249 if (!xen_initial_domain())
250 cpuid_leaf1_edx_mask &=
251 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
252 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
253
254 ax = 1;
H. Peter Anvin7adb4df2009-08-25 21:06:03 -0700255 cx = 0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800256 xen_cpuid(&ax, &bx, &cx, &dx);
257
258 /* cpuid claims we support xsave; try enabling it to see what happens */
259 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
260 unsigned long cr4;
261
262 set_in_cr4(X86_CR4_OSXSAVE);
263
264 cr4 = read_cr4();
265
266 if ((cr4 & X86_CR4_OSXSAVE) == 0)
267 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
268
269 clear_in_cr4(X86_CR4_OSXSAVE);
270 }
271}
272
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700273static void xen_set_debugreg(int reg, unsigned long val)
274{
275 HYPERVISOR_set_debugreg(reg, val);
276}
277
278static unsigned long xen_get_debugreg(int reg)
279{
280 return HYPERVISOR_get_debugreg(reg);
281}
282
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800283static void xen_end_context_switch(struct task_struct *next)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700284{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700285 xen_mc_flush();
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800286 paravirt_end_context_switch(next);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700287}
288
289static unsigned long xen_store_tr(void)
290{
291 return 0;
292}
293
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700294/*
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700295 * Set the page permissions for a particular virtual address. If the
296 * address is a vmalloc mapping (or other non-linear mapping), then
297 * find the linear mapping of the page and also set its protections to
298 * match.
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700299 */
300static void set_aliased_prot(void *v, pgprot_t prot)
301{
302 int level;
303 pte_t *ptep;
304 pte_t pte;
305 unsigned long pfn;
306 struct page *page;
307
308 ptep = lookup_address((unsigned long)v, &level);
309 BUG_ON(ptep == NULL);
310
311 pfn = pte_pfn(*ptep);
312 page = pfn_to_page(pfn);
313
314 pte = pfn_pte(pfn, prot);
315
316 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
317 BUG();
318
319 if (!PageHighMem(page)) {
320 void *av = __va(PFN_PHYS(pfn));
321
322 if (av != v)
323 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
324 BUG();
325 } else
326 kmap_flush_unused();
327}
328
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700329static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
330{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700331 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700332 int i;
333
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700334 for(i = 0; i < entries; i += entries_per_page)
335 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700336}
337
338static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
339{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700340 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700341 int i;
342
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700343 for(i = 0; i < entries; i += entries_per_page)
344 set_aliased_prot(ldt + i, PAGE_KERNEL);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700345}
346
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700347static void xen_set_ldt(const void *addr, unsigned entries)
348{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700349 struct mmuext_op *op;
350 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
351
352 op = mcs.args;
353 op->cmd = MMUEXT_SET_LDT;
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100354 op->arg1.linear_addr = (unsigned long)addr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700355 op->arg2.nr_ents = entries;
356
357 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
358
359 xen_mc_issue(PARAVIRT_LAZY_CPU);
360}
361
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100362static void xen_load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700363{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700364 unsigned long va = dtr->address;
365 unsigned int size = dtr->size + 1;
366 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800367 unsigned long frames[pages];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700368 int f;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700369
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700370 /*
371 * A GDT can be up to 64k in size, which corresponds to 8192
372 * 8-byte entries, or 16 4k pages..
373 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700374
375 BUG_ON(size > 65536);
376 BUG_ON(va & ~PAGE_MASK);
377
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700378 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800379 int level;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700380 pte_t *ptep;
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800381 unsigned long pfn, mfn;
382 void *virt;
383
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700384 /*
385 * The GDT is per-cpu and is in the percpu data area.
386 * That can be virtually mapped, so we need to do a
387 * page-walk to get the underlying MFN for the
388 * hypercall. The page can also be in the kernel's
389 * linear range, so we need to RO that mapping too.
390 */
391 ptep = lookup_address(va, &level);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800392 BUG_ON(ptep == NULL);
393
394 pfn = pte_pfn(*ptep);
395 mfn = pfn_to_mfn(pfn);
396 virt = __va(PFN_PHYS(pfn));
397
398 frames[f] = mfn;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800399
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700400 make_lowmem_page_readonly((void *)va);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800401 make_lowmem_page_readonly(virt);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700402 }
403
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800404 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
405 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700406}
407
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700408/*
409 * load_gdt for early boot, when the gdt is only mapped once
410 */
411static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
412{
413 unsigned long va = dtr->address;
414 unsigned int size = dtr->size + 1;
415 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
416 unsigned long frames[pages];
417 int f;
418
419 /*
420 * A GDT can be up to 64k in size, which corresponds to 8192
421 * 8-byte entries, or 16 4k pages..
422 */
423
424 BUG_ON(size > 65536);
425 BUG_ON(va & ~PAGE_MASK);
426
427 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
428 pte_t pte;
429 unsigned long pfn, mfn;
430
431 pfn = virt_to_pfn(va);
432 mfn = pfn_to_mfn(pfn);
433
434 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
435
436 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
437 BUG();
438
439 frames[f] = mfn;
440 }
441
442 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
443 BUG();
444}
445
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700446static void load_TLS_descriptor(struct thread_struct *t,
447 unsigned int cpu, unsigned int i)
448{
449 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800450 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700451 struct multicall_space mc = __xen_mc_entry(0);
452
453 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
454}
455
456static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
457{
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700458 /*
Tejun Heoccbeed32009-02-09 22:17:40 +0900459 * XXX sleazy hack: If we're being called in a lazy-cpu zone
460 * and lazy gs handling is enabled, it means we're in a
461 * context switch, and %gs has just been saved. This means we
462 * can zero it out to prevent faults on exit from the
463 * hypervisor if the next process has no %gs. Either way, it
464 * has been saved, and the new value will get loaded properly.
465 * This will go away as soon as Xen has been modified to not
466 * save/restore %gs for normal hypercalls.
Eduardo Habkost8a954082008-07-08 15:07:10 -0700467 *
468 * On x86_64, this hack is not used for %gs, because gs points
469 * to KERNEL_GS_BASE (and uses it for PDA references), so we
470 * must not zero %gs on x86_64
471 *
472 * For x86_64, we need to zero %fs, otherwise we may get an
473 * exception between the new %fs descriptor being loaded and
474 * %fs being effectively cleared at __switch_to().
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700475 */
Eduardo Habkost8a954082008-07-08 15:07:10 -0700476 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
477#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900478 lazy_load_gs(0);
Eduardo Habkost8a954082008-07-08 15:07:10 -0700479#else
480 loadsegment(fs, 0);
481#endif
482 }
483
484 xen_mc_batch();
485
486 load_TLS_descriptor(t, cpu, 0);
487 load_TLS_descriptor(t, cpu, 1);
488 load_TLS_descriptor(t, cpu, 2);
489
490 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700491}
492
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700493#ifdef CONFIG_X86_64
494static void xen_load_gs_index(unsigned int idx)
495{
496 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
497 BUG();
498}
499#endif
500
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700501static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100502 const void *ptr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700503{
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700504 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100505 u64 entry = *(u64 *)ptr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700506
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700507 preempt_disable();
508
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700509 xen_mc_flush();
510 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
511 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700512
513 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700514}
515
Eduardo Habkoste176d362008-07-08 15:06:59 -0700516static int cvt_gate_to_trap(int vector, const gate_desc *val,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700517 struct trap_info *info)
518{
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700519 unsigned long addr;
520
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700521 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700522 return 0;
523
524 info->vector = vector;
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700525
526 addr = gate_offset(*val);
527#ifdef CONFIG_X86_64
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700528 /*
529 * Look for known traps using IST, and substitute them
530 * appropriately. The debugger ones are the only ones we care
531 * about. Xen will handle faults like double_fault and
532 * machine_check, so we should never see them. Warn if
533 * there's an unexpected IST-using fault handler.
534 */
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700535 if (addr == (unsigned long)debug)
536 addr = (unsigned long)xen_debug;
537 else if (addr == (unsigned long)int3)
538 addr = (unsigned long)xen_int3;
539 else if (addr == (unsigned long)stack_segment)
540 addr = (unsigned long)xen_stack_segment;
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700541 else if (addr == (unsigned long)double_fault ||
542 addr == (unsigned long)nmi) {
543 /* Don't need to handle these */
544 return 0;
545#ifdef CONFIG_X86_MCE
546 } else if (addr == (unsigned long)machine_check) {
547 return 0;
548#endif
549 } else {
550 /* Some other trap using IST? */
551 if (WARN_ON(val->ist != 0))
552 return 0;
553 }
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700554#endif /* CONFIG_X86_64 */
555 info->address = addr;
556
Eduardo Habkoste176d362008-07-08 15:06:59 -0700557 info->cs = gate_segment(*val);
558 info->flags = val->dpl;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700559 /* interrupt gates clear IF */
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700560 if (val->type == GATE_INTERRUPT)
561 info->flags |= 1 << 2;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700562
563 return 1;
564}
565
566/* Locations of each CPU's IDT */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100567static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700568
569/* Set an IDT entry. If the entry is part of the current IDT, then
570 also update Xen. */
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100571static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700572{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700573 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700574 unsigned long start, end;
575
576 preempt_disable();
577
578 start = __get_cpu_var(idt_desc).address;
579 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700580
581 xen_mc_flush();
582
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100583 native_write_idt_entry(dt, entrynum, g);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700584
585 if (p >= start && (p + 8) <= end) {
586 struct trap_info info[2];
587
588 info[1].address = 0;
589
Eduardo Habkoste176d362008-07-08 15:06:59 -0700590 if (cvt_gate_to_trap(entrynum, g, &info[0]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700591 if (HYPERVISOR_set_trap_table(info))
592 BUG();
593 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700594
595 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700596}
597
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100598static void xen_convert_trap_info(const struct desc_ptr *desc,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700599 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700600{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700601 unsigned in, out, count;
602
Eduardo Habkoste176d362008-07-08 15:06:59 -0700603 count = (desc->size+1) / sizeof(gate_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700604 BUG_ON(count > 256);
605
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700606 for (in = out = 0; in < count; in++) {
Eduardo Habkoste176d362008-07-08 15:06:59 -0700607 gate_desc *entry = (gate_desc*)(desc->address) + in;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700608
Eduardo Habkoste176d362008-07-08 15:06:59 -0700609 if (cvt_gate_to_trap(in, entry, &traps[out]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700610 out++;
611 }
612 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700613}
614
615void xen_copy_trap_info(struct trap_info *traps)
616{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100617 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700618
619 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700620}
621
622/* Load a new IDT into Xen. In principle this can be per-CPU, so we
623 hold a spinlock to protect the static traps[] array (static because
624 it avoids allocation, and saves stack space). */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100625static void xen_load_idt(const struct desc_ptr *desc)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700626{
627 static DEFINE_SPINLOCK(lock);
628 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700629
630 spin_lock(&lock);
631
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700632 __get_cpu_var(idt_desc) = *desc;
633
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700634 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700635
636 xen_mc_flush();
637 if (HYPERVISOR_set_trap_table(traps))
638 BUG();
639
640 spin_unlock(&lock);
641}
642
643/* Write a GDT descriptor entry. Ignore LDT descriptors, since
644 they're handled differently. */
645static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100646 const void *desc, int type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700647{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700648 preempt_disable();
649
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100650 switch (type) {
651 case DESC_LDT:
652 case DESC_TSS:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700653 /* ignore */
654 break;
655
656 default: {
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800657 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700658
659 xen_mc_flush();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100660 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700661 BUG();
662 }
663
664 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700665
666 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700667}
668
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700669/*
670 * Version of write_gdt_entry for use at early boot-time needed to
671 * update an entry as simply as possible.
672 */
673static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
674 const void *desc, int type)
675{
676 switch (type) {
677 case DESC_LDT:
678 case DESC_TSS:
679 /* ignore */
680 break;
681
682 default: {
683 xmaddr_t maddr = virt_to_machine(&dt[entry]);
684
685 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
686 dt[entry] = *(struct desc_struct *)desc;
687 }
688
689 }
690}
691
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100692static void xen_load_sp0(struct tss_struct *tss,
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700693 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700694{
695 struct multicall_space mcs = xen_mc_entry(0);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100696 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700697 xen_mc_issue(PARAVIRT_LAZY_CPU);
698}
699
700static void xen_set_iopl_mask(unsigned mask)
701{
702 struct physdev_set_iopl set_iopl;
703
704 /* Force the change at ring 0. */
705 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
706 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
707}
708
709static void xen_io_delay(void)
710{
711}
712
713#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700714static u32 xen_apic_read(u32 reg)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700715{
716 return 0;
717}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700718
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700719static void xen_apic_write(u32 reg, u32 val)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700720{
721 /* Warn to see if there's any stray references */
722 WARN_ON(1);
723}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700724
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700725static u64 xen_apic_icr_read(void)
726{
727 return 0;
728}
729
730static void xen_apic_icr_write(u32 low, u32 id)
731{
732 /* Warn to see if there's any stray references */
733 WARN_ON(1);
734}
735
736static void xen_apic_wait_icr_idle(void)
737{
738 return;
739}
740
Yinghai Lu94a8c3c2008-07-13 22:19:35 -0700741static u32 xen_safe_apic_wait_icr_idle(void)
742{
743 return 0;
744}
745
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800746static void set_xen_basic_apic_ops(void)
747{
748 apic->read = xen_apic_read;
749 apic->write = xen_apic_write;
750 apic->icr_read = xen_apic_icr_read;
751 apic->icr_write = xen_apic_icr_write;
752 apic->wait_icr_idle = xen_apic_wait_icr_idle;
753 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
754}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700755
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700756#endif
757
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100758static void xen_clts(void)
759{
760 struct multicall_space mcs;
761
762 mcs = xen_mc_entry(0);
763
764 MULTI_fpu_taskswitch(mcs.mc, 0);
765
766 xen_mc_issue(PARAVIRT_LAZY_CPU);
767}
768
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700769static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
770
771static unsigned long xen_read_cr0(void)
772{
773 unsigned long cr0 = percpu_read(xen_cr0_value);
774
775 if (unlikely(cr0 == 0)) {
776 cr0 = native_read_cr0();
777 percpu_write(xen_cr0_value, cr0);
778 }
779
780 return cr0;
781}
782
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100783static void xen_write_cr0(unsigned long cr0)
784{
785 struct multicall_space mcs;
786
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700787 percpu_write(xen_cr0_value, cr0);
788
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100789 /* Only pay attention to cr0.TS; everything else is
790 ignored. */
791 mcs = xen_mc_entry(0);
792
793 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
794
795 xen_mc_issue(PARAVIRT_LAZY_CPU);
796}
797
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700798static void xen_write_cr4(unsigned long cr4)
799{
Jeremy Fitzhardinge2956a352008-05-26 23:31:04 +0100800 cr4 &= ~X86_CR4_PGE;
801 cr4 &= ~X86_CR4_PSE;
802
803 native_write_cr4(cr4);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700804}
805
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700806static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
807{
808 int ret;
809
810 ret = 0;
811
Tejf63c2f22008-12-16 11:56:06 -0800812 switch (msr) {
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700813#ifdef CONFIG_X86_64
814 unsigned which;
815 u64 base;
816
817 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
818 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
819 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
820
821 set:
822 base = ((u64)high << 32) | low;
823 if (HYPERVISOR_set_segment_base(which, base) != 0)
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700824 ret = -EIO;
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700825 break;
826#endif
Jeremy Fitzhardinged89961e2008-07-24 13:48:58 -0700827
828 case MSR_STAR:
829 case MSR_CSTAR:
830 case MSR_LSTAR:
831 case MSR_SYSCALL_MASK:
832 case MSR_IA32_SYSENTER_CS:
833 case MSR_IA32_SYSENTER_ESP:
834 case MSR_IA32_SYSENTER_EIP:
835 /* Fast syscall setup is all done in hypercalls, so
836 these are all ignored. Stub them out here to stop
837 Xen console noise. */
838 break;
839
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700840 default:
841 ret = native_write_msr_safe(msr, low, high);
842 }
843
844 return ret;
845}
846
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100847void xen_setup_shared_info(void)
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700848{
849 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700850 set_fixmap(FIX_PARAVIRT_BOOTMAP,
851 xen_start_info->shared_info);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700852
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700853 HYPERVISOR_shared_info =
854 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700855 } else
856 HYPERVISOR_shared_info =
857 (struct shared_info *)__va(xen_start_info->shared_info);
858
859#ifndef CONFIG_SMP
860 /* In UP this is as good a place as any to set up shared info */
861 xen_setup_vcpu_info_placement();
862#endif
Jeremy Fitzhardinged5edbc1f2008-05-26 23:31:22 +0100863
864 xen_setup_mfn_list_list();
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700865}
866
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700867/* This is called once we have the cpu_possible_map */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100868void xen_setup_vcpu_info_placement(void)
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700869{
870 int cpu;
871
872 for_each_possible_cpu(cpu)
873 xen_vcpu_setup(cpu);
874
875 /* xen_vcpu_setup managed to place the vcpu_info within the
876 percpu area for all cpus, so make use of it */
877 if (have_vcpu_info_placement) {
878 printk(KERN_INFO "Xen: using vcpu_info placement\n");
879
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800880 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
881 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
882 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
883 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700884 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700885 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700886}
887
Andi Kleenab144f52007-08-10 22:31:03 +0200888static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
889 unsigned long addr, unsigned len)
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700890{
891 char *start, *end, *reloc;
892 unsigned ret;
893
894 start = end = reloc = NULL;
895
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700896#define SITE(op, x) \
897 case PARAVIRT_PATCH(op.x): \
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700898 if (have_vcpu_info_placement) { \
899 start = (char *)xen_##x##_direct; \
900 end = xen_##x##_direct_end; \
901 reloc = xen_##x##_direct_reloc; \
902 } \
903 goto patch_site
904
905 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700906 SITE(pv_irq_ops, irq_enable);
907 SITE(pv_irq_ops, irq_disable);
908 SITE(pv_irq_ops, save_fl);
909 SITE(pv_irq_ops, restore_fl);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700910#undef SITE
911
912 patch_site:
913 if (start == NULL || (end-start) > len)
914 goto default_patch;
915
Andi Kleenab144f52007-08-10 22:31:03 +0200916 ret = paravirt_patch_insns(insnbuf, len, start, end);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700917
918 /* Note: because reloc is assigned from something that
919 appears to be an array, gcc assumes it's non-null,
920 but doesn't know its relationship with start and
921 end. */
922 if (reloc > start && reloc < end) {
923 int reloc_off = reloc - start;
Andi Kleenab144f52007-08-10 22:31:03 +0200924 long *relocp = (long *)(insnbuf + reloc_off);
925 long delta = start - (char *)addr;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700926
927 *relocp += delta;
928 }
929 break;
930
931 default_patch:
932 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200933 ret = paravirt_patch_default(type, clobbers, insnbuf,
934 addr, len);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700935 break;
936 }
937
938 return ret;
939}
940
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700941static const struct pv_info xen_info __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700942 .paravirt_enabled = 1,
943 .shared_kernel_pmd = 0,
944
945 .name = "Xen",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700946};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700947
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700948static const struct pv_init_ops xen_init_ops __initdata = {
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700949 .patch = xen_patch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700950};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700951
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700952static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700953 .cpuid = xen_cpuid,
954
955 .set_debugreg = xen_set_debugreg,
956 .get_debugreg = xen_get_debugreg,
957
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100958 .clts = xen_clts,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700959
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700960 .read_cr0 = xen_read_cr0,
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100961 .write_cr0 = xen_write_cr0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700962
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700963 .read_cr4 = native_read_cr4,
964 .read_cr4_safe = native_read_cr4_safe,
965 .write_cr4 = xen_write_cr4,
966
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700967 .wbinvd = native_wbinvd,
968
969 .read_msr = native_read_msr_safe,
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700970 .write_msr = xen_write_msr_safe,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700971 .read_tsc = native_read_tsc,
972 .read_pmc = native_read_pmc,
973
Jeremy Fitzhardinge81e103f2008-04-17 17:40:51 +0200974 .iret = xen_iret,
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400975 .irq_enable_sysexit = xen_sysexit,
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700976#ifdef CONFIG_X86_64
977 .usergs_sysret32 = xen_sysret32,
978 .usergs_sysret64 = xen_sysret64,
979#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700980
981 .load_tr_desc = paravirt_nop,
982 .set_ldt = xen_set_ldt,
983 .load_gdt = xen_load_gdt,
984 .load_idt = xen_load_idt,
985 .load_tls = xen_load_tls,
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700986#ifdef CONFIG_X86_64
987 .load_gs_index = xen_load_gs_index,
988#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700989
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700990 .alloc_ldt = xen_alloc_ldt,
991 .free_ldt = xen_free_ldt,
992
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700993 .store_gdt = native_store_gdt,
994 .store_idt = native_store_idt,
995 .store_tr = xen_store_tr,
996
997 .write_ldt_entry = xen_write_ldt_entry,
998 .write_gdt_entry = xen_write_gdt_entry,
999 .write_idt_entry = xen_write_idt_entry,
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001000 .load_sp0 = xen_load_sp0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001001
1002 .set_iopl_mask = xen_set_iopl_mask,
1003 .io_delay = xen_io_delay,
1004
Jeremy Fitzhardinge952d1d72008-07-08 15:07:01 -07001005 /* Xen takes care of %gs when switching to usermode for us */
1006 .swapgs = paravirt_nop,
1007
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08001008 .start_context_switch = paravirt_start_context_switch,
1009 .end_context_switch = xen_end_context_switch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001010};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001011
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001012static const struct pv_apic_ops xen_apic_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001013#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001014 .startup_ipi_hook = paravirt_nop,
1015#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001016};
1017
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001018static void xen_reboot(int reason)
1019{
Jeremy Fitzhardinge349c709f2008-05-26 23:31:02 +01001020 struct sched_shutdown r = { .reason = reason };
1021
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001022#ifdef CONFIG_SMP
1023 smp_send_stop();
1024#endif
1025
Jeremy Fitzhardinge349c709f2008-05-26 23:31:02 +01001026 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001027 BUG();
1028}
1029
1030static void xen_restart(char *msg)
1031{
1032 xen_reboot(SHUTDOWN_reboot);
1033}
1034
1035static void xen_emergency_restart(void)
1036{
1037 xen_reboot(SHUTDOWN_reboot);
1038}
1039
1040static void xen_machine_halt(void)
1041{
1042 xen_reboot(SHUTDOWN_poweroff);
1043}
1044
1045static void xen_crash_shutdown(struct pt_regs *regs)
1046{
1047 xen_reboot(SHUTDOWN_crash);
1048}
1049
Donald Dutilef09f6d12010-07-15 14:56:49 -04001050static int
1051xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
1052{
Ian Campbell086748e2010-08-03 14:55:14 -07001053 xen_reboot(SHUTDOWN_crash);
Donald Dutilef09f6d12010-07-15 14:56:49 -04001054 return NOTIFY_DONE;
1055}
1056
1057static struct notifier_block xen_panic_block = {
1058 .notifier_call= xen_panic_event,
1059};
1060
1061int xen_panic_handler_init(void)
1062{
1063 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
1064 return 0;
1065}
1066
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001067static const struct machine_ops __initdata xen_machine_ops = {
1068 .restart = xen_restart,
1069 .halt = xen_machine_halt,
1070 .power_off = xen_machine_halt,
1071 .shutdown = xen_machine_halt,
1072 .crash_shutdown = xen_crash_shutdown,
1073 .emergency_restart = xen_emergency_restart,
1074};
1075
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001076/*
1077 * Set up the GDT and segment registers for -fstack-protector. Until
1078 * we do this, we have to be careful not to call any stack-protected
1079 * function, which is most of the kernel.
1080 */
1081static void __init xen_setup_stackprotector(void)
1082{
1083 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1084 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1085
1086 setup_stack_canary_segment(0);
1087 switch_to_new_gdt(0);
1088
1089 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1090 pv_cpu_ops.load_gdt = xen_load_gdt;
1091}
1092
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001093/* First C function to be called on Xen boot */
1094asmlinkage void __init xen_start_kernel(void)
1095{
1096 pgd_t *pgd;
1097
1098 if (!xen_start_info)
1099 return;
1100
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001101 xen_domain_type = XEN_PV_DOMAIN;
1102
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001103 /* Install Xen paravirt ops */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001104 pv_info = xen_info;
1105 pv_init_ops = xen_init_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001106 pv_cpu_ops = xen_cpu_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001107 pv_apic_ops = xen_apic_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001108
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001109 x86_init.resources.memory_setup = xen_memory_setup;
Thomas Gleixner42bbdb42009-08-20 13:04:10 +02001110 x86_init.oem.arch_setup = xen_arch_setup;
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001111 x86_init.oem.banner = xen_banner;
Thomas Gleixner845b3942009-08-19 15:37:03 +02001112
Stefano Stabellini409771d2010-05-14 12:48:19 +01001113 xen_init_time_ops();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001114
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001115 /*
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001116 * Set up some pagetable state before starting to set any ptes.
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001117 */
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001118
Jeremy Fitzhardinge973df352009-10-27 16:54:19 -07001119 xen_init_mmu_ops();
1120
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001121 /* Prevent unwanted bits from being set in PTEs. */
1122 __supported_pte_mask &= ~_PAGE_GLOBAL;
1123 if (!xen_initial_domain())
1124 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1125
1126 __supported_pte_mask |= _PAGE_IOMAP;
1127
Ian Campbell817a8242010-02-26 17:16:00 +00001128 /*
1129 * Prevent page tables from being allocated in highmem, even
1130 * if CONFIG_HIGHPTE is enabled.
1131 */
1132 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
1133
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001134 /* Work out if we support NX */
H. Peter Anvin4763ed42009-11-13 15:28:16 -08001135 x86_configure_nx();
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001136
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001137 xen_setup_features();
1138
1139 /* Get mfn list */
1140 if (!xen_feature(XENFEAT_auto_translated_physmap))
1141 xen_build_dynamic_phys_to_machine();
1142
1143 /*
1144 * Set up kernel GDT and segment registers, mainly so that
1145 * -fstack-protector code can be executed.
1146 */
1147 xen_setup_stackprotector();
Jeremy Fitzhardinge0d1edf42008-07-28 11:53:57 -07001148
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001149 xen_init_irq_ops();
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -08001150 xen_init_cpuid_mask();
1151
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001152#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001153 /*
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001154 * set up the basic apic ops.
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001155 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -08001156 set_xen_basic_apic_ops();
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001157#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001158
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -07001159 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1160 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1161 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1162 }
1163
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001164 machine_ops = xen_machine_ops;
1165
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -08001166 /*
1167 * The only reliable way to retain the initial address of the
1168 * percpu gdt_page is to remember it here, so we can go and
1169 * mark it RW later, when the initial percpu area is freed.
1170 */
1171 xen_initial_gdt = &per_cpu(gdt_page, 0);
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +09001172
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -07001173 xen_smp_init();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001174
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001175 pgd = (pgd_t *)xen_start_info->pt_base;
1176
Alex Nixon7347b402010-02-19 13:31:06 -05001177 if (!xen_initial_domain())
1178 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1179
1180 __supported_pte_mask |= _PAGE_IOMAP;
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001181 /* Don't do the full vcpu_info placement stuff until we have a
1182 possible map and a non-dummy shared_info. */
1183 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1184
Jeremy Fitzhardinge55d80852009-02-25 09:42:25 -08001185 local_irq_disable();
1186 early_boot_irqs_off();
1187
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001188 xen_raw_console_write("mapping kernel into physical memory\n");
Jeremy Fitzhardinged114e1982008-07-08 15:06:52 -07001189 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
Juan Quintela4ec53872010-09-02 15:45:43 +01001190 xen_ident_map_ISA();
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001191
1192 init_mm.pgd = pgd;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001193
1194 /* keep using Xen gdt for now; no urgent need to change it */
1195
Ian Campbelle68266b2010-01-13 10:16:08 +00001196#ifdef CONFIG_X86_32
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001197 pv_info.kernel_rpl = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001198 if (xen_feature(XENFEAT_supervisor_mode_kernel))
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001199 pv_info.kernel_rpl = 0;
Ian Campbelle68266b2010-01-13 10:16:08 +00001200#else
1201 pv_info.kernel_rpl = 0;
1202#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001203
1204 /* set the limit of our address space */
Jeremy Fitzhardingefb1d8402007-10-16 11:51:31 -07001205 xen_reserve_top();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001206
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001207#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001208 /* set up basic CPUID stuff */
1209 cpu_detect(&new_cpu_data);
1210 new_cpu_data.hard_math = 1;
Jeremy Fitzhardinged560bc612009-08-25 12:53:02 -07001211 new_cpu_data.wp_works_ok = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001212 new_cpu_data.x86_capability[0] = cpuid_edx(1);
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001213#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001214
1215 /* Poke various useful things into boot_params */
H. Peter Anvin30c82642007-10-15 17:13:22 -07001216 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1217 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1218 ? __pa(xen_start_info->mod_start) : 0;
1219 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
Jeremy Fitzhardingeb7c3c5c2008-07-08 15:07:07 -07001220 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001221
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001222 if (!xen_initial_domain()) {
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +01001223 add_preferred_console("xenboot", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001224 add_preferred_console("tty", 0, NULL);
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001225 add_preferred_console("hvc", 0, NULL);
Alex Nixonb5401a92010-03-18 16:31:34 -04001226 if (pci_xen)
1227 x86_init.pci.arch_init = pci_xen_init;
Chris Wright5d990b62009-12-04 12:15:21 -08001228 } else {
1229 /* Make sure ACS will be enabled */
1230 pci_request_acs();
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001231 }
Chris Wright5d990b62009-12-04 12:15:21 -08001232
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001233
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001234 xen_raw_console_write("about to get started...\n");
1235
Jeremy Fitzhardinge499d19b2009-11-24 09:38:25 -08001236 xen_setup_runstate_info(0);
1237
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001238 /* Start the world */
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001239#ifdef CONFIG_X86_32
Yinghai Luf0d43102008-05-29 12:56:36 -07001240 i386_start_kernel();
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001241#else
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001242 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001243#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001244}
Sheng Yangbee6ab532010-05-14 12:39:33 +01001245
1246static uint32_t xen_cpuid_base(void)
1247{
1248 uint32_t base, eax, ebx, ecx, edx;
1249 char signature[13];
1250
1251 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
1252 cpuid(base, &eax, &ebx, &ecx, &edx);
1253 *(uint32_t *)(signature + 0) = ebx;
1254 *(uint32_t *)(signature + 4) = ecx;
1255 *(uint32_t *)(signature + 8) = edx;
1256 signature[12] = 0;
1257
1258 if (!strcmp("XenVMMXenVMM", signature) && ((eax - base) >= 2))
1259 return base;
1260 }
1261
1262 return 0;
1263}
1264
1265static int init_hvm_pv_info(int *major, int *minor)
1266{
1267 uint32_t eax, ebx, ecx, edx, pages, msr, base;
1268 u64 pfn;
1269
1270 base = xen_cpuid_base();
1271 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
1272
1273 *major = eax >> 16;
1274 *minor = eax & 0xffff;
1275 printk(KERN_INFO "Xen version %d.%d.\n", *major, *minor);
1276
1277 cpuid(base + 2, &pages, &msr, &ecx, &edx);
1278
1279 pfn = __pa(hypercall_page);
1280 wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32));
1281
1282 xen_setup_features();
1283
1284 pv_info = xen_info;
1285 pv_info.kernel_rpl = 0;
1286
1287 xen_domain_type = XEN_HVM_DOMAIN;
1288
1289 return 0;
1290}
1291
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001292void xen_hvm_init_shared_info(void)
Sheng Yangbee6ab532010-05-14 12:39:33 +01001293{
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001294 int cpu;
Sheng Yangbee6ab532010-05-14 12:39:33 +01001295 struct xen_add_to_physmap xatp;
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001296 static struct shared_info *shared_info_page = 0;
Sheng Yangbee6ab532010-05-14 12:39:33 +01001297
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001298 if (!shared_info_page)
1299 shared_info_page = (struct shared_info *)
1300 extend_brk(PAGE_SIZE, PAGE_SIZE);
Sheng Yangbee6ab532010-05-14 12:39:33 +01001301 xatp.domid = DOMID_SELF;
1302 xatp.idx = 0;
1303 xatp.space = XENMAPSPACE_shared_info;
1304 xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
1305 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
1306 BUG();
1307
1308 HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
1309
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001310 /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
1311 * page, we use it in the event channel upcall and in some pvclock
1312 * related functions. We don't need the vcpu_info placement
1313 * optimizations because we don't use any pv_mmu or pv_irq op on
1314 * HVM.
1315 * When xen_hvm_init_shared_info is run at boot time only vcpu 0 is
1316 * online but xen_hvm_init_shared_info is run at resume time too and
1317 * in that case multiple vcpus might be online. */
1318 for_each_online_cpu(cpu) {
1319 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
1320 }
Sheng Yangbee6ab532010-05-14 12:39:33 +01001321}
1322
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001323#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001324static int __cpuinit xen_hvm_cpu_notify(struct notifier_block *self,
1325 unsigned long action, void *hcpu)
1326{
1327 int cpu = (long)hcpu;
1328 switch (action) {
1329 case CPU_UP_PREPARE:
1330 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
1331 break;
1332 default:
1333 break;
1334 }
1335 return NOTIFY_OK;
1336}
1337
1338static struct notifier_block __cpuinitdata xen_hvm_cpu_notifier = {
1339 .notifier_call = xen_hvm_cpu_notify,
1340};
1341
Sheng Yangbee6ab532010-05-14 12:39:33 +01001342static void __init xen_hvm_guest_init(void)
1343{
1344 int r;
1345 int major, minor;
1346
1347 r = init_hvm_pv_info(&major, &minor);
1348 if (r < 0)
1349 return;
1350
Stefano Stabellini016b6f52010-05-14 12:45:07 +01001351 xen_hvm_init_shared_info();
Sheng Yang38e20b02010-05-14 12:40:51 +01001352
1353 if (xen_feature(XENFEAT_hvm_callback_vector))
1354 xen_have_vector_callback = 1;
1355 register_cpu_notifier(&xen_hvm_cpu_notifier);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001356 xen_unplug_emulated_devices();
Sheng Yang38e20b02010-05-14 12:40:51 +01001357 have_vcpu_info_placement = 0;
1358 x86_init.irqs.intr_init = xen_init_IRQ;
Stefano Stabellini409771d2010-05-14 12:48:19 +01001359 xen_hvm_init_time_ops();
Stefano Stabellini59151002010-06-17 14:22:52 +01001360 xen_hvm_init_mmu_ops();
Sheng Yangbee6ab532010-05-14 12:39:33 +01001361}
1362
1363static bool __init xen_hvm_platform(void)
1364{
1365 if (xen_pv_domain())
1366 return false;
1367
1368 if (!xen_cpuid_base())
1369 return false;
1370
1371 return true;
1372}
1373
1374const __refconst struct hypervisor_x86 x86_hyper_xen_hvm = {
1375 .name = "Xen HVM",
1376 .detect = xen_hvm_platform,
1377 .init_platform = xen_hvm_guest_init,
1378};
1379EXPORT_SYMBOL(x86_hyper_xen_hvm);
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001380#endif