blob: dfbf70e65860dc768c255e07ac5d2c2b63e3a159 [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
2 * Core of Xen paravirt_ops implementation.
3 *
4 * This file contains the xen_paravirt_ops structure itself, and the
5 * implementations for:
6 * - privileged instructions
7 * - interrupt flags
8 * - segment operations
9 * - booting and setup
10 *
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/smp.h>
17#include <linux/preempt.h>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070018#include <linux/hardirq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070019#include <linux/percpu.h>
20#include <linux/delay.h>
21#include <linux/start_kernel.h>
22#include <linux/sched.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070023#include <linux/kprobes.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070024#include <linux/bootmem.h>
25#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070026#include <linux/mm.h>
27#include <linux/page-flags.h>
28#include <linux/highmem.h>
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +010029#include <linux/console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070030
31#include <xen/interface/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080032#include <xen/interface/version.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070033#include <xen/interface/physdev.h>
34#include <xen/interface/vcpu.h>
35#include <xen/features.h>
36#include <xen/page.h>
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -070037#include <xen/hvc-console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070038
39#include <asm/paravirt.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010040#include <asm/apic.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070041#include <asm/page.h>
42#include <asm/xen/hypercall.h>
43#include <asm/xen/hypervisor.h>
44#include <asm/fixmap.h>
45#include <asm/processor.h>
Jeremy Fitzhardinge707ebbc2009-03-27 11:29:02 -070046#include <asm/proto.h>
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -070047#include <asm/msr-index.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070048#include <asm/traps.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070049#include <asm/setup.h>
50#include <asm/desc.h>
51#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070052#include <asm/tlbflush.h>
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -070053#include <asm/reboot.h>
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -070054#include <asm/stackprotector.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070055
56#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070057#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070058#include "multicalls.h"
59
60EXPORT_SYMBOL_GPL(hypercall_page);
61
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070062DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
63DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -070064
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -070065enum xen_domain_type xen_domain_type = XEN_NATIVE;
66EXPORT_SYMBOL_GPL(xen_domain_type);
67
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070068struct start_info *xen_start_info;
69EXPORT_SYMBOL_GPL(xen_start_info);
70
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010071struct shared_info xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070072
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -080073void *xen_initial_gdt;
74
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070075/*
76 * Point at some empty memory to start with. We map the real shared_info
77 * page as soon as fixmap is up and running.
78 */
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010079struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070080
81/*
82 * Flag to determine whether vcpu info placement is available on all
83 * VCPUs. We assume it is to start with, and then set it to zero on
84 * the first failure. This is because it can succeed on some VCPUs
85 * and not others, since it can involve hypervisor memory allocation,
86 * or because the guest failed to guarantee all the appropriate
87 * constraints on all VCPUs (ie buffer can't cross a page boundary).
88 *
89 * Note that any particular CPU may be using a placed vcpu structure,
90 * but we can only optimise if the all are.
91 *
92 * 0: not available, 1: available
93 */
Jeremy Fitzhardingee4d04072009-02-02 13:55:54 -080094static int have_vcpu_info_placement = 1;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070095
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +010096static void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070097{
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070098 struct vcpu_register_vcpu_info info;
99 int err;
100 struct vcpu_info *vcpup;
101
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +0100102 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700103 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700104
105 if (!have_vcpu_info_placement)
106 return; /* already tested, not available */
107
108 vcpup = &per_cpu(xen_vcpu_info, cpu);
109
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800110 info.mfn = arbitrary_virt_to_mfn(vcpup);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700111 info.offset = offset_in_page(vcpup);
112
Jeremy Fitzhardingee3d26972007-10-16 11:51:31 -0700113 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700114 cpu, vcpup, info.mfn, info.offset);
115
116 /* Check to see if the hypervisor will put the vcpu_info
117 structure where we want it, which allows direct access via
118 a percpu-variable. */
119 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
120
121 if (err) {
122 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
123 have_vcpu_info_placement = 0;
124 } else {
125 /* This cpu is using the registered vcpu info, even if
126 later ones fail to. */
127 per_cpu(xen_vcpu, cpu) = vcpup;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700128
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700129 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
130 cpu, vcpup);
131 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700132}
133
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100134/*
135 * On restore, set the vcpu placement up again.
136 * If it fails, then we're in a bad state, since
137 * we can't back out from using it...
138 */
139void xen_vcpu_restore(void)
140{
141 if (have_vcpu_info_placement) {
142 int cpu;
143
144 for_each_online_cpu(cpu) {
145 bool other_cpu = (cpu != smp_processor_id());
146
147 if (other_cpu &&
148 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
149 BUG();
150
151 xen_vcpu_setup(cpu);
152
153 if (other_cpu &&
154 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
155 BUG();
156 }
157
158 BUG_ON(!have_vcpu_info_placement);
159 }
160}
161
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700162static void __init xen_banner(void)
163{
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700164 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
165 struct xen_extraversion extra;
166 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
167
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700168 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700169 pv_info.name);
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700170 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
171 version >> 16, version & 0xffff, extra.extraversion,
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700172 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700173}
174
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800175static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
176static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
177
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100178static void xen_cpuid(unsigned int *ax, unsigned int *bx,
179 unsigned int *cx, unsigned int *dx)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700180{
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700181 unsigned maskebx = ~0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800182 unsigned maskecx = ~0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700183 unsigned maskedx = ~0;
184
185 /*
186 * Mask out inconvenient features, to try and disable as many
187 * unsupported kernel subsystems as possible.
188 */
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700189 switch (*ax) {
190 case 1:
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800191 maskecx = cpuid_leaf1_ecx_mask;
192 maskedx = cpuid_leaf1_edx_mask;
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700193 break;
194
195 case 0xb:
196 /* Suppress extended topology stuff */
197 maskebx = 0;
198 break;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800199 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700200
201 asm(XEN_EMULATE_PREFIX "cpuid"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100202 : "=a" (*ax),
203 "=b" (*bx),
204 "=c" (*cx),
205 "=d" (*dx)
206 : "0" (*ax), "2" (*cx));
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800207
Jeremy Fitzhardinge82d64692009-10-22 16:41:15 -0700208 *bx &= maskebx;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800209 *cx &= maskecx;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100210 *dx &= maskedx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700211}
212
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800213static __init void xen_init_cpuid_mask(void)
214{
215 unsigned int ax, bx, cx, dx;
216
217 cpuid_leaf1_edx_mask =
218 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
219 (1 << X86_FEATURE_MCA) | /* disable MCA */
220 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
221
222 if (!xen_initial_domain())
223 cpuid_leaf1_edx_mask &=
224 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
225 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
226
227 ax = 1;
H. Peter Anvin7adb4df2009-08-25 21:06:03 -0700228 cx = 0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800229 xen_cpuid(&ax, &bx, &cx, &dx);
230
231 /* cpuid claims we support xsave; try enabling it to see what happens */
232 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
233 unsigned long cr4;
234
235 set_in_cr4(X86_CR4_OSXSAVE);
236
237 cr4 = read_cr4();
238
239 if ((cr4 & X86_CR4_OSXSAVE) == 0)
240 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
241
242 clear_in_cr4(X86_CR4_OSXSAVE);
243 }
244}
245
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700246static void xen_set_debugreg(int reg, unsigned long val)
247{
248 HYPERVISOR_set_debugreg(reg, val);
249}
250
251static unsigned long xen_get_debugreg(int reg)
252{
253 return HYPERVISOR_get_debugreg(reg);
254}
255
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800256static void xen_end_context_switch(struct task_struct *next)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700257{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700258 xen_mc_flush();
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800259 paravirt_end_context_switch(next);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700260}
261
262static unsigned long xen_store_tr(void)
263{
264 return 0;
265}
266
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700267/*
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700268 * Set the page permissions for a particular virtual address. If the
269 * address is a vmalloc mapping (or other non-linear mapping), then
270 * find the linear mapping of the page and also set its protections to
271 * match.
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700272 */
273static void set_aliased_prot(void *v, pgprot_t prot)
274{
275 int level;
276 pte_t *ptep;
277 pte_t pte;
278 unsigned long pfn;
279 struct page *page;
280
281 ptep = lookup_address((unsigned long)v, &level);
282 BUG_ON(ptep == NULL);
283
284 pfn = pte_pfn(*ptep);
285 page = pfn_to_page(pfn);
286
287 pte = pfn_pte(pfn, prot);
288
289 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
290 BUG();
291
292 if (!PageHighMem(page)) {
293 void *av = __va(PFN_PHYS(pfn));
294
295 if (av != v)
296 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
297 BUG();
298 } else
299 kmap_flush_unused();
300}
301
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700302static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
303{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700304 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700305 int i;
306
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700307 for(i = 0; i < entries; i += entries_per_page)
308 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700309}
310
311static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
312{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700313 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700314 int i;
315
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700316 for(i = 0; i < entries; i += entries_per_page)
317 set_aliased_prot(ldt + i, PAGE_KERNEL);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700318}
319
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700320static void xen_set_ldt(const void *addr, unsigned entries)
321{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700322 struct mmuext_op *op;
323 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
324
325 op = mcs.args;
326 op->cmd = MMUEXT_SET_LDT;
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100327 op->arg1.linear_addr = (unsigned long)addr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700328 op->arg2.nr_ents = entries;
329
330 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
331
332 xen_mc_issue(PARAVIRT_LAZY_CPU);
333}
334
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100335static void xen_load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700336{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700337 unsigned long va = dtr->address;
338 unsigned int size = dtr->size + 1;
339 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800340 unsigned long frames[pages];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700341 int f;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700342
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700343 /*
344 * A GDT can be up to 64k in size, which corresponds to 8192
345 * 8-byte entries, or 16 4k pages..
346 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700347
348 BUG_ON(size > 65536);
349 BUG_ON(va & ~PAGE_MASK);
350
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700351 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800352 int level;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700353 pte_t *ptep;
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800354 unsigned long pfn, mfn;
355 void *virt;
356
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700357 /*
358 * The GDT is per-cpu and is in the percpu data area.
359 * That can be virtually mapped, so we need to do a
360 * page-walk to get the underlying MFN for the
361 * hypercall. The page can also be in the kernel's
362 * linear range, so we need to RO that mapping too.
363 */
364 ptep = lookup_address(va, &level);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800365 BUG_ON(ptep == NULL);
366
367 pfn = pte_pfn(*ptep);
368 mfn = pfn_to_mfn(pfn);
369 virt = __va(PFN_PHYS(pfn));
370
371 frames[f] = mfn;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800372
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700373 make_lowmem_page_readonly((void *)va);
Jeremy Fitzhardinge6ed6bf422009-03-04 13:02:18 -0800374 make_lowmem_page_readonly(virt);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700375 }
376
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800377 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
378 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700379}
380
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700381/*
382 * load_gdt for early boot, when the gdt is only mapped once
383 */
384static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
385{
386 unsigned long va = dtr->address;
387 unsigned int size = dtr->size + 1;
388 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
389 unsigned long frames[pages];
390 int f;
391
392 /*
393 * A GDT can be up to 64k in size, which corresponds to 8192
394 * 8-byte entries, or 16 4k pages..
395 */
396
397 BUG_ON(size > 65536);
398 BUG_ON(va & ~PAGE_MASK);
399
400 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
401 pte_t pte;
402 unsigned long pfn, mfn;
403
404 pfn = virt_to_pfn(va);
405 mfn = pfn_to_mfn(pfn);
406
407 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
408
409 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
410 BUG();
411
412 frames[f] = mfn;
413 }
414
415 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
416 BUG();
417}
418
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700419static void load_TLS_descriptor(struct thread_struct *t,
420 unsigned int cpu, unsigned int i)
421{
422 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800423 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700424 struct multicall_space mc = __xen_mc_entry(0);
425
426 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
427}
428
429static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
430{
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700431 /*
Tejun Heoccbeed32009-02-09 22:17:40 +0900432 * XXX sleazy hack: If we're being called in a lazy-cpu zone
433 * and lazy gs handling is enabled, it means we're in a
434 * context switch, and %gs has just been saved. This means we
435 * can zero it out to prevent faults on exit from the
436 * hypervisor if the next process has no %gs. Either way, it
437 * has been saved, and the new value will get loaded properly.
438 * This will go away as soon as Xen has been modified to not
439 * save/restore %gs for normal hypercalls.
Eduardo Habkost8a954082008-07-08 15:07:10 -0700440 *
441 * On x86_64, this hack is not used for %gs, because gs points
442 * to KERNEL_GS_BASE (and uses it for PDA references), so we
443 * must not zero %gs on x86_64
444 *
445 * For x86_64, we need to zero %fs, otherwise we may get an
446 * exception between the new %fs descriptor being loaded and
447 * %fs being effectively cleared at __switch_to().
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700448 */
Eduardo Habkost8a954082008-07-08 15:07:10 -0700449 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
450#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900451 lazy_load_gs(0);
Eduardo Habkost8a954082008-07-08 15:07:10 -0700452#else
453 loadsegment(fs, 0);
454#endif
455 }
456
457 xen_mc_batch();
458
459 load_TLS_descriptor(t, cpu, 0);
460 load_TLS_descriptor(t, cpu, 1);
461 load_TLS_descriptor(t, cpu, 2);
462
463 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700464}
465
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700466#ifdef CONFIG_X86_64
467static void xen_load_gs_index(unsigned int idx)
468{
469 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
470 BUG();
471}
472#endif
473
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700474static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100475 const void *ptr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700476{
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700477 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100478 u64 entry = *(u64 *)ptr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700479
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700480 preempt_disable();
481
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700482 xen_mc_flush();
483 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
484 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700485
486 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700487}
488
Eduardo Habkoste176d362008-07-08 15:06:59 -0700489static int cvt_gate_to_trap(int vector, const gate_desc *val,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700490 struct trap_info *info)
491{
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700492 unsigned long addr;
493
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700494 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700495 return 0;
496
497 info->vector = vector;
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700498
499 addr = gate_offset(*val);
500#ifdef CONFIG_X86_64
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700501 /*
502 * Look for known traps using IST, and substitute them
503 * appropriately. The debugger ones are the only ones we care
504 * about. Xen will handle faults like double_fault and
505 * machine_check, so we should never see them. Warn if
506 * there's an unexpected IST-using fault handler.
507 */
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700508 if (addr == (unsigned long)debug)
509 addr = (unsigned long)xen_debug;
510 else if (addr == (unsigned long)int3)
511 addr = (unsigned long)xen_int3;
512 else if (addr == (unsigned long)stack_segment)
513 addr = (unsigned long)xen_stack_segment;
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700514 else if (addr == (unsigned long)double_fault ||
515 addr == (unsigned long)nmi) {
516 /* Don't need to handle these */
517 return 0;
518#ifdef CONFIG_X86_MCE
519 } else if (addr == (unsigned long)machine_check) {
520 return 0;
521#endif
522 } else {
523 /* Some other trap using IST? */
524 if (WARN_ON(val->ist != 0))
525 return 0;
526 }
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700527#endif /* CONFIG_X86_64 */
528 info->address = addr;
529
Eduardo Habkoste176d362008-07-08 15:06:59 -0700530 info->cs = gate_segment(*val);
531 info->flags = val->dpl;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700532 /* interrupt gates clear IF */
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700533 if (val->type == GATE_INTERRUPT)
534 info->flags |= 1 << 2;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700535
536 return 1;
537}
538
539/* Locations of each CPU's IDT */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100540static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700541
542/* Set an IDT entry. If the entry is part of the current IDT, then
543 also update Xen. */
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100544static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700545{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700546 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700547 unsigned long start, end;
548
549 preempt_disable();
550
551 start = __get_cpu_var(idt_desc).address;
552 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700553
554 xen_mc_flush();
555
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100556 native_write_idt_entry(dt, entrynum, g);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700557
558 if (p >= start && (p + 8) <= end) {
559 struct trap_info info[2];
560
561 info[1].address = 0;
562
Eduardo Habkoste176d362008-07-08 15:06:59 -0700563 if (cvt_gate_to_trap(entrynum, g, &info[0]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700564 if (HYPERVISOR_set_trap_table(info))
565 BUG();
566 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700567
568 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700569}
570
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100571static void xen_convert_trap_info(const struct desc_ptr *desc,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700572 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700573{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700574 unsigned in, out, count;
575
Eduardo Habkoste176d362008-07-08 15:06:59 -0700576 count = (desc->size+1) / sizeof(gate_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700577 BUG_ON(count > 256);
578
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700579 for (in = out = 0; in < count; in++) {
Eduardo Habkoste176d362008-07-08 15:06:59 -0700580 gate_desc *entry = (gate_desc*)(desc->address) + in;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700581
Eduardo Habkoste176d362008-07-08 15:06:59 -0700582 if (cvt_gate_to_trap(in, entry, &traps[out]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700583 out++;
584 }
585 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700586}
587
588void xen_copy_trap_info(struct trap_info *traps)
589{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100590 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700591
592 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700593}
594
595/* Load a new IDT into Xen. In principle this can be per-CPU, so we
596 hold a spinlock to protect the static traps[] array (static because
597 it avoids allocation, and saves stack space). */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100598static void xen_load_idt(const struct desc_ptr *desc)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700599{
600 static DEFINE_SPINLOCK(lock);
601 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700602
603 spin_lock(&lock);
604
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700605 __get_cpu_var(idt_desc) = *desc;
606
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700607 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700608
609 xen_mc_flush();
610 if (HYPERVISOR_set_trap_table(traps))
611 BUG();
612
613 spin_unlock(&lock);
614}
615
616/* Write a GDT descriptor entry. Ignore LDT descriptors, since
617 they're handled differently. */
618static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100619 const void *desc, int type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700620{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700621 preempt_disable();
622
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100623 switch (type) {
624 case DESC_LDT:
625 case DESC_TSS:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700626 /* ignore */
627 break;
628
629 default: {
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800630 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700631
632 xen_mc_flush();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100633 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700634 BUG();
635 }
636
637 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700638
639 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700640}
641
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700642/*
643 * Version of write_gdt_entry for use at early boot-time needed to
644 * update an entry as simply as possible.
645 */
646static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
647 const void *desc, int type)
648{
649 switch (type) {
650 case DESC_LDT:
651 case DESC_TSS:
652 /* ignore */
653 break;
654
655 default: {
656 xmaddr_t maddr = virt_to_machine(&dt[entry]);
657
658 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
659 dt[entry] = *(struct desc_struct *)desc;
660 }
661
662 }
663}
664
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100665static void xen_load_sp0(struct tss_struct *tss,
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700666 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700667{
668 struct multicall_space mcs = xen_mc_entry(0);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100669 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700670 xen_mc_issue(PARAVIRT_LAZY_CPU);
671}
672
673static void xen_set_iopl_mask(unsigned mask)
674{
675 struct physdev_set_iopl set_iopl;
676
677 /* Force the change at ring 0. */
678 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
679 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
680}
681
682static void xen_io_delay(void)
683{
684}
685
686#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700687static u32 xen_apic_read(u32 reg)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700688{
689 return 0;
690}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700691
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700692static void xen_apic_write(u32 reg, u32 val)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700693{
694 /* Warn to see if there's any stray references */
695 WARN_ON(1);
696}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700697
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700698static u64 xen_apic_icr_read(void)
699{
700 return 0;
701}
702
703static void xen_apic_icr_write(u32 low, u32 id)
704{
705 /* Warn to see if there's any stray references */
706 WARN_ON(1);
707}
708
709static void xen_apic_wait_icr_idle(void)
710{
711 return;
712}
713
Yinghai Lu94a8c3c2008-07-13 22:19:35 -0700714static u32 xen_safe_apic_wait_icr_idle(void)
715{
716 return 0;
717}
718
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800719static void set_xen_basic_apic_ops(void)
720{
721 apic->read = xen_apic_read;
722 apic->write = xen_apic_write;
723 apic->icr_read = xen_apic_icr_read;
724 apic->icr_write = xen_apic_icr_write;
725 apic->wait_icr_idle = xen_apic_wait_icr_idle;
726 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
727}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700728
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700729#endif
730
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700731
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100732static void xen_clts(void)
733{
734 struct multicall_space mcs;
735
736 mcs = xen_mc_entry(0);
737
738 MULTI_fpu_taskswitch(mcs.mc, 0);
739
740 xen_mc_issue(PARAVIRT_LAZY_CPU);
741}
742
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700743static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
744
745static unsigned long xen_read_cr0(void)
746{
747 unsigned long cr0 = percpu_read(xen_cr0_value);
748
749 if (unlikely(cr0 == 0)) {
750 cr0 = native_read_cr0();
751 percpu_write(xen_cr0_value, cr0);
752 }
753
754 return cr0;
755}
756
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100757static void xen_write_cr0(unsigned long cr0)
758{
759 struct multicall_space mcs;
760
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700761 percpu_write(xen_cr0_value, cr0);
762
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100763 /* Only pay attention to cr0.TS; everything else is
764 ignored. */
765 mcs = xen_mc_entry(0);
766
767 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
768
769 xen_mc_issue(PARAVIRT_LAZY_CPU);
770}
771
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700772static void xen_write_cr4(unsigned long cr4)
773{
Jeremy Fitzhardinge2956a352008-05-26 23:31:04 +0100774 cr4 &= ~X86_CR4_PGE;
775 cr4 &= ~X86_CR4_PSE;
776
777 native_write_cr4(cr4);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700778}
779
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700780static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
781{
782 int ret;
783
784 ret = 0;
785
Tejf63c2f22008-12-16 11:56:06 -0800786 switch (msr) {
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700787#ifdef CONFIG_X86_64
788 unsigned which;
789 u64 base;
790
791 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
792 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
793 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
794
795 set:
796 base = ((u64)high << 32) | low;
797 if (HYPERVISOR_set_segment_base(which, base) != 0)
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700798 ret = -EIO;
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700799 break;
800#endif
Jeremy Fitzhardinged89961e2008-07-24 13:48:58 -0700801
802 case MSR_STAR:
803 case MSR_CSTAR:
804 case MSR_LSTAR:
805 case MSR_SYSCALL_MASK:
806 case MSR_IA32_SYSENTER_CS:
807 case MSR_IA32_SYSENTER_ESP:
808 case MSR_IA32_SYSENTER_EIP:
809 /* Fast syscall setup is all done in hypercalls, so
810 these are all ignored. Stub them out here to stop
811 Xen console noise. */
812 break;
813
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700814 default:
815 ret = native_write_msr_safe(msr, low, high);
816 }
817
818 return ret;
819}
820
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100821void xen_setup_shared_info(void)
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700822{
823 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700824 set_fixmap(FIX_PARAVIRT_BOOTMAP,
825 xen_start_info->shared_info);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700826
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700827 HYPERVISOR_shared_info =
828 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700829 } else
830 HYPERVISOR_shared_info =
831 (struct shared_info *)__va(xen_start_info->shared_info);
832
833#ifndef CONFIG_SMP
834 /* In UP this is as good a place as any to set up shared info */
835 xen_setup_vcpu_info_placement();
836#endif
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100837
838 xen_setup_mfn_list_list();
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700839}
840
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700841/* This is called once we have the cpu_possible_map */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100842void xen_setup_vcpu_info_placement(void)
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700843{
844 int cpu;
845
846 for_each_possible_cpu(cpu)
847 xen_vcpu_setup(cpu);
848
849 /* xen_vcpu_setup managed to place the vcpu_info within the
850 percpu area for all cpus, so make use of it */
851 if (have_vcpu_info_placement) {
852 printk(KERN_INFO "Xen: using vcpu_info placement\n");
853
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800854 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
855 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
856 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
857 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700858 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700859 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700860}
861
Andi Kleenab144f52007-08-10 22:31:03 +0200862static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
863 unsigned long addr, unsigned len)
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700864{
865 char *start, *end, *reloc;
866 unsigned ret;
867
868 start = end = reloc = NULL;
869
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700870#define SITE(op, x) \
871 case PARAVIRT_PATCH(op.x): \
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700872 if (have_vcpu_info_placement) { \
873 start = (char *)xen_##x##_direct; \
874 end = xen_##x##_direct_end; \
875 reloc = xen_##x##_direct_reloc; \
876 } \
877 goto patch_site
878
879 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700880 SITE(pv_irq_ops, irq_enable);
881 SITE(pv_irq_ops, irq_disable);
882 SITE(pv_irq_ops, save_fl);
883 SITE(pv_irq_ops, restore_fl);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700884#undef SITE
885
886 patch_site:
887 if (start == NULL || (end-start) > len)
888 goto default_patch;
889
Andi Kleenab144f52007-08-10 22:31:03 +0200890 ret = paravirt_patch_insns(insnbuf, len, start, end);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700891
892 /* Note: because reloc is assigned from something that
893 appears to be an array, gcc assumes it's non-null,
894 but doesn't know its relationship with start and
895 end. */
896 if (reloc > start && reloc < end) {
897 int reloc_off = reloc - start;
Andi Kleenab144f52007-08-10 22:31:03 +0200898 long *relocp = (long *)(insnbuf + reloc_off);
899 long delta = start - (char *)addr;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700900
901 *relocp += delta;
902 }
903 break;
904
905 default_patch:
906 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200907 ret = paravirt_patch_default(type, clobbers, insnbuf,
908 addr, len);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700909 break;
910 }
911
912 return ret;
913}
914
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700915static const struct pv_info xen_info __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700916 .paravirt_enabled = 1,
917 .shared_kernel_pmd = 0,
918
919 .name = "Xen",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700920};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700921
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700922static const struct pv_init_ops xen_init_ops __initdata = {
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700923 .patch = xen_patch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700924};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700925
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700926static const struct pv_time_ops xen_time_ops __initdata = {
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700927 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700928};
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700929
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700930static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700931 .cpuid = xen_cpuid,
932
933 .set_debugreg = xen_set_debugreg,
934 .get_debugreg = xen_get_debugreg,
935
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100936 .clts = xen_clts,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700937
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700938 .read_cr0 = xen_read_cr0,
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100939 .write_cr0 = xen_write_cr0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700940
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700941 .read_cr4 = native_read_cr4,
942 .read_cr4_safe = native_read_cr4_safe,
943 .write_cr4 = xen_write_cr4,
944
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700945 .wbinvd = native_wbinvd,
946
947 .read_msr = native_read_msr_safe,
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700948 .write_msr = xen_write_msr_safe,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700949 .read_tsc = native_read_tsc,
950 .read_pmc = native_read_pmc,
951
Jeremy Fitzhardinge81e103f2008-04-17 17:40:51 +0200952 .iret = xen_iret,
Jeremy Fitzhardinged75cd22f2008-06-25 00:19:26 -0400953 .irq_enable_sysexit = xen_sysexit,
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700954#ifdef CONFIG_X86_64
955 .usergs_sysret32 = xen_sysret32,
956 .usergs_sysret64 = xen_sysret64,
957#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700958
959 .load_tr_desc = paravirt_nop,
960 .set_ldt = xen_set_ldt,
961 .load_gdt = xen_load_gdt,
962 .load_idt = xen_load_idt,
963 .load_tls = xen_load_tls,
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700964#ifdef CONFIG_X86_64
965 .load_gs_index = xen_load_gs_index,
966#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700967
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700968 .alloc_ldt = xen_alloc_ldt,
969 .free_ldt = xen_free_ldt,
970
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700971 .store_gdt = native_store_gdt,
972 .store_idt = native_store_idt,
973 .store_tr = xen_store_tr,
974
975 .write_ldt_entry = xen_write_ldt_entry,
976 .write_gdt_entry = xen_write_gdt_entry,
977 .write_idt_entry = xen_write_idt_entry,
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100978 .load_sp0 = xen_load_sp0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700979
980 .set_iopl_mask = xen_set_iopl_mask,
981 .io_delay = xen_io_delay,
982
Jeremy Fitzhardinge952d1d72008-07-08 15:07:01 -0700983 /* Xen takes care of %gs when switching to usermode for us */
984 .swapgs = paravirt_nop,
985
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800986 .start_context_switch = paravirt_start_context_switch,
987 .end_context_switch = xen_end_context_switch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700988};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700989
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700990static const struct pv_apic_ops xen_apic_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700991#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700992 .startup_ipi_hook = paravirt_nop,
993#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700994};
995
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700996static void xen_reboot(int reason)
997{
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +0100998 struct sched_shutdown r = { .reason = reason };
999
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001000#ifdef CONFIG_SMP
1001 smp_send_stop();
1002#endif
1003
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +01001004 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001005 BUG();
1006}
1007
1008static void xen_restart(char *msg)
1009{
1010 xen_reboot(SHUTDOWN_reboot);
1011}
1012
1013static void xen_emergency_restart(void)
1014{
1015 xen_reboot(SHUTDOWN_reboot);
1016}
1017
1018static void xen_machine_halt(void)
1019{
1020 xen_reboot(SHUTDOWN_poweroff);
1021}
1022
1023static void xen_crash_shutdown(struct pt_regs *regs)
1024{
1025 xen_reboot(SHUTDOWN_crash);
1026}
1027
1028static const struct machine_ops __initdata xen_machine_ops = {
1029 .restart = xen_restart,
1030 .halt = xen_machine_halt,
1031 .power_off = xen_machine_halt,
1032 .shutdown = xen_machine_halt,
1033 .crash_shutdown = xen_crash_shutdown,
1034 .emergency_restart = xen_emergency_restart,
1035};
1036
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001037/*
1038 * Set up the GDT and segment registers for -fstack-protector. Until
1039 * we do this, we have to be careful not to call any stack-protected
1040 * function, which is most of the kernel.
1041 */
1042static void __init xen_setup_stackprotector(void)
1043{
1044 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1045 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1046
1047 setup_stack_canary_segment(0);
1048 switch_to_new_gdt(0);
1049
1050 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1051 pv_cpu_ops.load_gdt = xen_load_gdt;
1052}
1053
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001054/* First C function to be called on Xen boot */
1055asmlinkage void __init xen_start_kernel(void)
1056{
1057 pgd_t *pgd;
1058
1059 if (!xen_start_info)
1060 return;
1061
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001062 xen_domain_type = XEN_PV_DOMAIN;
1063
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001064 /* Install Xen paravirt ops */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001065 pv_info = xen_info;
1066 pv_init_ops = xen_init_ops;
1067 pv_time_ops = xen_time_ops;
1068 pv_cpu_ops = xen_cpu_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001069 pv_apic_ops = xen_apic_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001070
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001071 x86_init.resources.memory_setup = xen_memory_setup;
Thomas Gleixner42bbdb42009-08-20 13:04:10 +02001072 x86_init.oem.arch_setup = xen_arch_setup;
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001073 x86_init.oem.banner = xen_banner;
Thomas Gleixner845b3942009-08-19 15:37:03 +02001074
1075 x86_init.timers.timer_init = xen_time_init;
Thomas Gleixner736deca2009-08-19 12:35:53 +02001076 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1077 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001078
Thomas Gleixner2d826402009-08-20 17:06:25 +02001079 x86_platform.calibrate_tsc = xen_tsc_khz;
Feng Tang7bd867d2009-09-10 10:48:56 +08001080 x86_platform.get_wallclock = xen_get_wallclock;
1081 x86_platform.set_wallclock = xen_set_wallclock;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001082
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001083 /*
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001084 * Set up some pagetable state before starting to set any ptes.
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001085 */
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001086
Jeremy Fitzhardinge973df352009-10-27 16:54:19 -07001087 xen_init_mmu_ops();
1088
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001089 /* Prevent unwanted bits from being set in PTEs. */
1090 __supported_pte_mask &= ~_PAGE_GLOBAL;
1091 if (!xen_initial_domain())
1092 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1093
1094 __supported_pte_mask |= _PAGE_IOMAP;
1095
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001096#ifdef CONFIG_X86_64
1097 /* Work out if we support NX */
1098 check_efer();
1099#endif
1100
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001101 xen_setup_features();
1102
1103 /* Get mfn list */
1104 if (!xen_feature(XENFEAT_auto_translated_physmap))
1105 xen_build_dynamic_phys_to_machine();
1106
1107 /*
1108 * Set up kernel GDT and segment registers, mainly so that
1109 * -fstack-protector code can be executed.
1110 */
1111 xen_setup_stackprotector();
Jeremy Fitzhardinge0d1edf42008-07-28 11:53:57 -07001112
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001113 xen_init_irq_ops();
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -08001114 xen_init_cpuid_mask();
1115
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001116#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001117 /*
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001118 * set up the basic apic ops.
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001119 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -08001120 set_xen_basic_apic_ops();
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001121#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001122
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -07001123 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1124 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1125 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1126 }
1127
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001128 machine_ops = xen_machine_ops;
1129
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -08001130 /*
1131 * The only reliable way to retain the initial address of the
1132 * percpu gdt_page is to remember it here, so we can go and
1133 * mark it RW later, when the initial percpu area is freed.
1134 */
1135 xen_initial_gdt = &per_cpu(gdt_page, 0);
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +09001136
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -07001137 xen_smp_init();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001138
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001139 pgd = (pgd_t *)xen_start_info->pt_base;
1140
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001141 /* Don't do the full vcpu_info placement stuff until we have a
1142 possible map and a non-dummy shared_info. */
1143 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1144
Jeremy Fitzhardinge55d80852009-02-25 09:42:25 -08001145 local_irq_disable();
1146 early_boot_irqs_off();
1147
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001148 xen_raw_console_write("mapping kernel into physical memory\n");
Jeremy Fitzhardinged114e1982008-07-08 15:06:52 -07001149 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001150
1151 init_mm.pgd = pgd;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001152
1153 /* keep using Xen gdt for now; no urgent need to change it */
1154
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001155 pv_info.kernel_rpl = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001156 if (xen_feature(XENFEAT_supervisor_mode_kernel))
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001157 pv_info.kernel_rpl = 0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001158
1159 /* set the limit of our address space */
Jeremy Fitzhardingefb1d8402007-10-16 11:51:31 -07001160 xen_reserve_top();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001161
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001162#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001163 /* set up basic CPUID stuff */
1164 cpu_detect(&new_cpu_data);
1165 new_cpu_data.hard_math = 1;
Jeremy Fitzhardinged560bc62009-08-25 12:53:02 -07001166 new_cpu_data.wp_works_ok = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001167 new_cpu_data.x86_capability[0] = cpuid_edx(1);
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001168#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001169
1170 /* Poke various useful things into boot_params */
H. Peter Anvin30c82642007-10-15 17:13:22 -07001171 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1172 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1173 ? __pa(xen_start_info->mod_start) : 0;
1174 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
Jeremy Fitzhardingeb7c3c5c2008-07-08 15:07:07 -07001175 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001176
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001177 if (!xen_initial_domain()) {
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +01001178 add_preferred_console("xenboot", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001179 add_preferred_console("tty", 0, NULL);
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001180 add_preferred_console("hvc", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001181 }
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001182
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001183 xen_raw_console_write("about to get started...\n");
1184
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001185 /* Start the world */
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001186#ifdef CONFIG_X86_32
Yinghai Luf0d43102008-05-29 12:56:36 -07001187 i386_start_kernel();
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001188#else
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001189 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001190#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001191}