blob: e2511bccbc8d9b581b2e9367413c40ee58199861 [file] [log] [blame]
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001/*
2 * Core of Xen paravirt_ops implementation.
3 *
4 * This file contains the xen_paravirt_ops structure itself, and the
5 * implementations for:
6 * - privileged instructions
7 * - interrupt flags
8 * - segment operations
9 * - booting and setup
10 *
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/smp.h>
17#include <linux/preempt.h>
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -070018#include <linux/hardirq.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070019#include <linux/percpu.h>
20#include <linux/delay.h>
21#include <linux/start_kernel.h>
22#include <linux/sched.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070023#include <linux/kprobes.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070024#include <linux/bootmem.h>
25#include <linux/module.h>
Jeremy Fitzhardingef4f97b32007-07-17 18:37:05 -070026#include <linux/mm.h>
27#include <linux/page-flags.h>
28#include <linux/highmem.h>
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +010029#include <linux/console.h>
Chris Wright5d990b62009-12-04 12:15:21 -080030#include <linux/pci.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070031
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070032#include <xen/xen.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070033#include <xen/interface/xen.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080034#include <xen/interface/version.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070035#include <xen/interface/physdev.h>
36#include <xen/interface/vcpu.h>
37#include <xen/features.h>
38#include <xen/page.h>
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -070039#include <xen/hvc-console.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070040
41#include <asm/paravirt.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010042#include <asm/apic.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070043#include <asm/page.h>
44#include <asm/xen/hypercall.h>
45#include <asm/xen/hypervisor.h>
46#include <asm/fixmap.h>
47#include <asm/processor.h>
Jeremy Fitzhardinge707ebbc2009-03-27 11:29:02 -070048#include <asm/proto.h>
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -070049#include <asm/msr-index.h>
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -070050#include <asm/traps.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070051#include <asm/setup.h>
52#include <asm/desc.h>
53#include <asm/pgtable.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070054#include <asm/tlbflush.h>
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -070055#include <asm/reboot.h>
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -070056#include <asm/stackprotector.h>
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070057
58#include "xen-ops.h"
Jeremy Fitzhardinge3b827c12007-07-17 18:37:04 -070059#include "mmu.h"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070060#include "multicalls.h"
61
62EXPORT_SYMBOL_GPL(hypercall_page);
63
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070064DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
65DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
Jeremy Fitzhardinge9f799912007-10-16 11:51:30 -070066
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -070067enum xen_domain_type xen_domain_type = XEN_NATIVE;
68EXPORT_SYMBOL_GPL(xen_domain_type);
69
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070070struct start_info *xen_start_info;
71EXPORT_SYMBOL_GPL(xen_start_info);
72
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010073struct shared_info xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070074
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -080075void *xen_initial_gdt;
76
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070077/*
78 * Point at some empty memory to start with. We map the real shared_info
79 * page as soon as fixmap is up and running.
80 */
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +010081struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070082
83/*
84 * Flag to determine whether vcpu info placement is available on all
85 * VCPUs. We assume it is to start with, and then set it to zero on
86 * the first failure. This is because it can succeed on some VCPUs
87 * and not others, since it can involve hypervisor memory allocation,
88 * or because the guest failed to guarantee all the appropriate
89 * constraints on all VCPUs (ie buffer can't cross a page boundary).
90 *
91 * Note that any particular CPU may be using a placed vcpu structure,
92 * but we can only optimise if the all are.
93 *
94 * 0: not available, 1: available
95 */
Jeremy Fitzhardingee4d04072009-02-02 13:55:54 -080096static int have_vcpu_info_placement = 1;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -070097
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +010098static void xen_vcpu_setup(int cpu)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -070099{
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700100 struct vcpu_register_vcpu_info info;
101 int err;
102 struct vcpu_info *vcpup;
103
Jeremy Fitzhardingea0d695c2008-05-26 23:31:21 +0100104 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700105 per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700106
107 if (!have_vcpu_info_placement)
108 return; /* already tested, not available */
109
110 vcpup = &per_cpu(xen_vcpu_info, cpu);
111
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800112 info.mfn = arbitrary_virt_to_mfn(vcpup);
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700113 info.offset = offset_in_page(vcpup);
114
Jeremy Fitzhardingee3d26972007-10-16 11:51:31 -0700115 printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700116 cpu, vcpup, info.mfn, info.offset);
117
118 /* Check to see if the hypervisor will put the vcpu_info
119 structure where we want it, which allows direct access via
120 a percpu-variable. */
121 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
122
123 if (err) {
124 printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
125 have_vcpu_info_placement = 0;
126 } else {
127 /* This cpu is using the registered vcpu info, even if
128 later ones fail to. */
129 per_cpu(xen_vcpu, cpu) = vcpup;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700130
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700131 printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
132 cpu, vcpup);
133 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700134}
135
Jeremy Fitzhardinge9c7a7942008-05-31 01:33:02 +0100136/*
137 * On restore, set the vcpu placement up again.
138 * If it fails, then we're in a bad state, since
139 * we can't back out from using it...
140 */
141void xen_vcpu_restore(void)
142{
143 if (have_vcpu_info_placement) {
144 int cpu;
145
146 for_each_online_cpu(cpu) {
147 bool other_cpu = (cpu != smp_processor_id());
148
149 if (other_cpu &&
150 HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
151 BUG();
152
153 xen_vcpu_setup(cpu);
154
155 if (other_cpu &&
156 HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
157 BUG();
158 }
159
160 BUG_ON(!have_vcpu_info_placement);
161 }
162}
163
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700164static void __init xen_banner(void)
165{
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700166 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
167 struct xen_extraversion extra;
168 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
169
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700170 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700171 pv_info.name);
Jeremy Fitzhardinge95c7c232008-07-15 13:42:34 -0700172 printk(KERN_INFO "Xen version: %d.%d%s%s\n",
173 version >> 16, version & 0xffff, extra.extraversion,
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -0700174 xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700175}
176
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800177static __read_mostly unsigned int cpuid_leaf1_edx_mask = ~0;
178static __read_mostly unsigned int cpuid_leaf1_ecx_mask = ~0;
179
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100180static void xen_cpuid(unsigned int *ax, unsigned int *bx,
181 unsigned int *cx, unsigned int *dx)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700182{
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800183 unsigned maskecx = ~0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700184 unsigned maskedx = ~0;
185
186 /*
187 * Mask out inconvenient features, to try and disable as many
188 * unsupported kernel subsystems as possible.
189 */
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800190 if (*ax == 1) {
191 maskecx = cpuid_leaf1_ecx_mask;
192 maskedx = cpuid_leaf1_edx_mask;
193 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700194
195 asm(XEN_EMULATE_PREFIX "cpuid"
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100196 : "=a" (*ax),
197 "=b" (*bx),
198 "=c" (*cx),
199 "=d" (*dx)
200 : "0" (*ax), "2" (*cx));
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800201
202 *cx &= maskecx;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100203 *dx &= maskedx;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700204}
205
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800206static __init void xen_init_cpuid_mask(void)
207{
208 unsigned int ax, bx, cx, dx;
209
210 cpuid_leaf1_edx_mask =
211 ~((1 << X86_FEATURE_MCE) | /* disable MCE */
212 (1 << X86_FEATURE_MCA) | /* disable MCA */
213 (1 << X86_FEATURE_ACC)); /* thermal monitoring */
214
215 if (!xen_initial_domain())
216 cpuid_leaf1_edx_mask &=
217 ~((1 << X86_FEATURE_APIC) | /* disable local APIC */
218 (1 << X86_FEATURE_ACPI)); /* disable ACPI */
219
220 ax = 1;
H. Peter Anvin7adb4df2009-08-25 21:06:03 -0700221 cx = 0;
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -0800222 xen_cpuid(&ax, &bx, &cx, &dx);
223
224 /* cpuid claims we support xsave; try enabling it to see what happens */
225 if (cx & (1 << (X86_FEATURE_XSAVE % 32))) {
226 unsigned long cr4;
227
228 set_in_cr4(X86_CR4_OSXSAVE);
229
230 cr4 = read_cr4();
231
232 if ((cr4 & X86_CR4_OSXSAVE) == 0)
233 cpuid_leaf1_ecx_mask &= ~(1 << (X86_FEATURE_XSAVE % 32));
234
235 clear_in_cr4(X86_CR4_OSXSAVE);
236 }
237}
238
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700239static void xen_set_debugreg(int reg, unsigned long val)
240{
241 HYPERVISOR_set_debugreg(reg, val);
242}
243
244static unsigned long xen_get_debugreg(int reg)
245{
246 return HYPERVISOR_get_debugreg(reg);
247}
248
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800249static void xen_end_context_switch(struct task_struct *next)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700250{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700251 xen_mc_flush();
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800252 paravirt_end_context_switch(next);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700253}
254
255static unsigned long xen_store_tr(void)
256{
257 return 0;
258}
259
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700260/*
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700261 * Set the page permissions for a particular virtual address. If the
262 * address is a vmalloc mapping (or other non-linear mapping), then
263 * find the linear mapping of the page and also set its protections to
264 * match.
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700265 */
266static void set_aliased_prot(void *v, pgprot_t prot)
267{
268 int level;
269 pte_t *ptep;
270 pte_t pte;
271 unsigned long pfn;
272 struct page *page;
273
274 ptep = lookup_address((unsigned long)v, &level);
275 BUG_ON(ptep == NULL);
276
277 pfn = pte_pfn(*ptep);
278 page = pfn_to_page(pfn);
279
280 pte = pfn_pte(pfn, prot);
281
282 if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
283 BUG();
284
285 if (!PageHighMem(page)) {
286 void *av = __va(PFN_PHYS(pfn));
287
288 if (av != v)
289 if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
290 BUG();
291 } else
292 kmap_flush_unused();
293}
294
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700295static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
296{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700297 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700298 int i;
299
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700300 for(i = 0; i < entries; i += entries_per_page)
301 set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700302}
303
304static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
305{
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700306 const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700307 int i;
308
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700309 for(i = 0; i < entries; i += entries_per_page)
310 set_aliased_prot(ldt + i, PAGE_KERNEL);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700311}
312
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700313static void xen_set_ldt(const void *addr, unsigned entries)
314{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700315 struct mmuext_op *op;
316 struct multicall_space mcs = xen_mc_entry(sizeof(*op));
317
318 op = mcs.args;
319 op->cmd = MMUEXT_SET_LDT;
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100320 op->arg1.linear_addr = (unsigned long)addr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700321 op->arg2.nr_ents = entries;
322
323 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
324
325 xen_mc_issue(PARAVIRT_LAZY_CPU);
326}
327
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100328static void xen_load_gdt(const struct desc_ptr *dtr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700329{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700330 unsigned long va = dtr->address;
331 unsigned int size = dtr->size + 1;
332 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800333 unsigned long frames[pages];
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700334 int f;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700335
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700336 /*
337 * A GDT can be up to 64k in size, which corresponds to 8192
338 * 8-byte entries, or 16 4k pages..
339 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700340
341 BUG_ON(size > 65536);
342 BUG_ON(va & ~PAGE_MASK);
343
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700344 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800345 int level;
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700346 pte_t *ptep;
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800347 unsigned long pfn, mfn;
348 void *virt;
349
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700350 /*
351 * The GDT is per-cpu and is in the percpu data area.
352 * That can be virtually mapped, so we need to do a
353 * page-walk to get the underlying MFN for the
354 * hypercall. The page can also be in the kernel's
355 * linear range, so we need to RO that mapping too.
356 */
357 ptep = lookup_address(va, &level);
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800358 BUG_ON(ptep == NULL);
359
360 pfn = pte_pfn(*ptep);
361 mfn = pfn_to_mfn(pfn);
362 virt = __va(PFN_PHYS(pfn));
363
364 frames[f] = mfn;
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800365
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700366 make_lowmem_page_readonly((void *)va);
Jeremy Fitzhardinge6ed6bf42009-03-04 13:02:18 -0800367 make_lowmem_page_readonly(virt);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700368 }
369
Jeremy Fitzhardinge3ce5fa72009-03-04 15:26:00 -0800370 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
371 BUG();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700372}
373
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700374/*
375 * load_gdt for early boot, when the gdt is only mapped once
376 */
377static __init void xen_load_gdt_boot(const struct desc_ptr *dtr)
378{
379 unsigned long va = dtr->address;
380 unsigned int size = dtr->size + 1;
381 unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
382 unsigned long frames[pages];
383 int f;
384
385 /*
386 * A GDT can be up to 64k in size, which corresponds to 8192
387 * 8-byte entries, or 16 4k pages..
388 */
389
390 BUG_ON(size > 65536);
391 BUG_ON(va & ~PAGE_MASK);
392
393 for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
394 pte_t pte;
395 unsigned long pfn, mfn;
396
397 pfn = virt_to_pfn(va);
398 mfn = pfn_to_mfn(pfn);
399
400 pte = pfn_pte(pfn, PAGE_KERNEL_RO);
401
402 if (HYPERVISOR_update_va_mapping((unsigned long)va, pte, 0))
403 BUG();
404
405 frames[f] = mfn;
406 }
407
408 if (HYPERVISOR_set_gdt(frames, size / sizeof(struct desc_struct)))
409 BUG();
410}
411
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700412static void load_TLS_descriptor(struct thread_struct *t,
413 unsigned int cpu, unsigned int i)
414{
415 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800416 xmaddr_t maddr = arbitrary_virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700417 struct multicall_space mc = __xen_mc_entry(0);
418
419 MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
420}
421
422static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
423{
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700424 /*
Tejun Heoccbeed32009-02-09 22:17:40 +0900425 * XXX sleazy hack: If we're being called in a lazy-cpu zone
426 * and lazy gs handling is enabled, it means we're in a
427 * context switch, and %gs has just been saved. This means we
428 * can zero it out to prevent faults on exit from the
429 * hypervisor if the next process has no %gs. Either way, it
430 * has been saved, and the new value will get loaded properly.
431 * This will go away as soon as Xen has been modified to not
432 * save/restore %gs for normal hypercalls.
Eduardo Habkost8a954082008-07-08 15:07:10 -0700433 *
434 * On x86_64, this hack is not used for %gs, because gs points
435 * to KERNEL_GS_BASE (and uses it for PDA references), so we
436 * must not zero %gs on x86_64
437 *
438 * For x86_64, we need to zero %fs, otherwise we may get an
439 * exception between the new %fs descriptor being loaded and
440 * %fs being effectively cleared at __switch_to().
Jeremy Fitzhardinge8b84ad92007-07-17 18:37:06 -0700441 */
Eduardo Habkost8a954082008-07-08 15:07:10 -0700442 if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
443#ifdef CONFIG_X86_32
Tejun Heoccbeed32009-02-09 22:17:40 +0900444 lazy_load_gs(0);
Eduardo Habkost8a954082008-07-08 15:07:10 -0700445#else
446 loadsegment(fs, 0);
447#endif
448 }
449
450 xen_mc_batch();
451
452 load_TLS_descriptor(t, cpu, 0);
453 load_TLS_descriptor(t, cpu, 1);
454 load_TLS_descriptor(t, cpu, 2);
455
456 xen_mc_issue(PARAVIRT_LAZY_CPU);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700457}
458
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700459#ifdef CONFIG_X86_64
460static void xen_load_gs_index(unsigned int idx)
461{
462 if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
463 BUG();
464}
465#endif
466
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700467static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100468 const void *ptr)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700469{
Jeremy Fitzhardingecef43bf2008-07-28 13:33:44 -0700470 xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100471 u64 entry = *(u64 *)ptr;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700472
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700473 preempt_disable();
474
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700475 xen_mc_flush();
476 if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
477 BUG();
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700478
479 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700480}
481
Eduardo Habkoste176d362008-07-08 15:06:59 -0700482static int cvt_gate_to_trap(int vector, const gate_desc *val,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700483 struct trap_info *info)
484{
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700485 unsigned long addr;
486
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700487 if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700488 return 0;
489
490 info->vector = vector;
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700491
492 addr = gate_offset(*val);
493#ifdef CONFIG_X86_64
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700494 /*
495 * Look for known traps using IST, and substitute them
496 * appropriately. The debugger ones are the only ones we care
497 * about. Xen will handle faults like double_fault and
498 * machine_check, so we should never see them. Warn if
499 * there's an unexpected IST-using fault handler.
500 */
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700501 if (addr == (unsigned long)debug)
502 addr = (unsigned long)xen_debug;
503 else if (addr == (unsigned long)int3)
504 addr = (unsigned long)xen_int3;
505 else if (addr == (unsigned long)stack_segment)
506 addr = (unsigned long)xen_stack_segment;
Jeremy Fitzhardingeb80119b2009-04-24 00:22:08 -0700507 else if (addr == (unsigned long)double_fault ||
508 addr == (unsigned long)nmi) {
509 /* Don't need to handle these */
510 return 0;
511#ifdef CONFIG_X86_MCE
512 } else if (addr == (unsigned long)machine_check) {
513 return 0;
514#endif
515 } else {
516 /* Some other trap using IST? */
517 if (WARN_ON(val->ist != 0))
518 return 0;
519 }
Jeremy Fitzhardinge6cac5a92009-03-29 19:56:29 -0700520#endif /* CONFIG_X86_64 */
521 info->address = addr;
522
Eduardo Habkoste176d362008-07-08 15:06:59 -0700523 info->cs = gate_segment(*val);
524 info->flags = val->dpl;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700525 /* interrupt gates clear IF */
Jeremy Fitzhardinge6d02c422009-03-29 22:57:15 -0700526 if (val->type == GATE_INTERRUPT)
527 info->flags |= 1 << 2;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700528
529 return 1;
530}
531
532/* Locations of each CPU's IDT */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100533static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700534
535/* Set an IDT entry. If the entry is part of the current IDT, then
536 also update Xen. */
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100537static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700538{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700539 unsigned long p = (unsigned long)&dt[entrynum];
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700540 unsigned long start, end;
541
542 preempt_disable();
543
544 start = __get_cpu_var(idt_desc).address;
545 end = start + __get_cpu_var(idt_desc).size + 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700546
547 xen_mc_flush();
548
Glauber de Oliveira Costa8d947342008-01-30 13:31:12 +0100549 native_write_idt_entry(dt, entrynum, g);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700550
551 if (p >= start && (p + 8) <= end) {
552 struct trap_info info[2];
553
554 info[1].address = 0;
555
Eduardo Habkoste176d362008-07-08 15:06:59 -0700556 if (cvt_gate_to_trap(entrynum, g, &info[0]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700557 if (HYPERVISOR_set_trap_table(info))
558 BUG();
559 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700560
561 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700562}
563
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100564static void xen_convert_trap_info(const struct desc_ptr *desc,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700565 struct trap_info *traps)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700566{
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700567 unsigned in, out, count;
568
Eduardo Habkoste176d362008-07-08 15:06:59 -0700569 count = (desc->size+1) / sizeof(gate_desc);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700570 BUG_ON(count > 256);
571
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700572 for (in = out = 0; in < count; in++) {
Eduardo Habkoste176d362008-07-08 15:06:59 -0700573 gate_desc *entry = (gate_desc*)(desc->address) + in;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700574
Eduardo Habkoste176d362008-07-08 15:06:59 -0700575 if (cvt_gate_to_trap(in, entry, &traps[out]))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700576 out++;
577 }
578 traps[out].address = 0;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700579}
580
581void xen_copy_trap_info(struct trap_info *traps)
582{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100583 const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700584
585 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700586}
587
588/* Load a new IDT into Xen. In principle this can be per-CPU, so we
589 hold a spinlock to protect the static traps[] array (static because
590 it avoids allocation, and saves stack space). */
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100591static void xen_load_idt(const struct desc_ptr *desc)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700592{
593 static DEFINE_SPINLOCK(lock);
594 static struct trap_info traps[257];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700595
596 spin_lock(&lock);
597
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700598 __get_cpu_var(idt_desc) = *desc;
599
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700600 xen_convert_trap_info(desc, traps);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700601
602 xen_mc_flush();
603 if (HYPERVISOR_set_trap_table(traps))
604 BUG();
605
606 spin_unlock(&lock);
607}
608
609/* Write a GDT descriptor entry. Ignore LDT descriptors, since
610 they're handled differently. */
611static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100612 const void *desc, int type)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700613{
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700614 preempt_disable();
615
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100616 switch (type) {
617 case DESC_LDT:
618 case DESC_TSS:
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700619 /* ignore */
620 break;
621
622 default: {
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800623 xmaddr_t maddr = arbitrary_virt_to_machine(&dt[entry]);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700624
625 xen_mc_flush();
Glauber de Oliveira Costa014b15b2008-01-30 13:31:13 +0100626 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700627 BUG();
628 }
629
630 }
Jeremy Fitzhardingef120f132007-07-17 18:37:06 -0700631
632 preempt_enable();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700633}
634
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -0700635/*
636 * Version of write_gdt_entry for use at early boot-time needed to
637 * update an entry as simply as possible.
638 */
639static __init void xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
640 const void *desc, int type)
641{
642 switch (type) {
643 case DESC_LDT:
644 case DESC_TSS:
645 /* ignore */
646 break;
647
648 default: {
649 xmaddr_t maddr = virt_to_machine(&dt[entry]);
650
651 if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
652 dt[entry] = *(struct desc_struct *)desc;
653 }
654
655 }
656}
657
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100658static void xen_load_sp0(struct tss_struct *tss,
Jeremy Fitzhardingea05d2eb2008-07-27 08:45:02 -0700659 struct thread_struct *thread)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700660{
661 struct multicall_space mcs = xen_mc_entry(0);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100662 MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700663 xen_mc_issue(PARAVIRT_LAZY_CPU);
664}
665
666static void xen_set_iopl_mask(unsigned mask)
667{
668 struct physdev_set_iopl set_iopl;
669
670 /* Force the change at ring 0. */
671 set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
672 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
673}
674
675static void xen_io_delay(void)
676{
677}
678
679#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700680static u32 xen_apic_read(u32 reg)
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700681{
682 return 0;
683}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700684
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700685static void xen_apic_write(u32 reg, u32 val)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700686{
687 /* Warn to see if there's any stray references */
688 WARN_ON(1);
689}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700690
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700691static u64 xen_apic_icr_read(void)
692{
693 return 0;
694}
695
696static void xen_apic_icr_write(u32 low, u32 id)
697{
698 /* Warn to see if there's any stray references */
699 WARN_ON(1);
700}
701
702static void xen_apic_wait_icr_idle(void)
703{
704 return;
705}
706
Yinghai Lu94a8c3c2008-07-13 22:19:35 -0700707static u32 xen_safe_apic_wait_icr_idle(void)
708{
709 return 0;
710}
711
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800712static void set_xen_basic_apic_ops(void)
713{
714 apic->read = xen_apic_read;
715 apic->write = xen_apic_write;
716 apic->icr_read = xen_apic_icr_read;
717 apic->icr_write = xen_apic_icr_write;
718 apic->wait_icr_idle = xen_apic_wait_icr_idle;
719 apic->safe_wait_icr_idle = xen_safe_apic_wait_icr_idle;
720}
Suresh Siddhaad66dd32008-07-11 13:11:56 -0700721
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700722#endif
723
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700724
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100725static void xen_clts(void)
726{
727 struct multicall_space mcs;
728
729 mcs = xen_mc_entry(0);
730
731 MULTI_fpu_taskswitch(mcs.mc, 0);
732
733 xen_mc_issue(PARAVIRT_LAZY_CPU);
734}
735
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700736static DEFINE_PER_CPU(unsigned long, xen_cr0_value);
737
738static unsigned long xen_read_cr0(void)
739{
740 unsigned long cr0 = percpu_read(xen_cr0_value);
741
742 if (unlikely(cr0 == 0)) {
743 cr0 = native_read_cr0();
744 percpu_write(xen_cr0_value, cr0);
745 }
746
747 return cr0;
748}
749
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100750static void xen_write_cr0(unsigned long cr0)
751{
752 struct multicall_space mcs;
753
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700754 percpu_write(xen_cr0_value, cr0);
755
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100756 /* Only pay attention to cr0.TS; everything else is
757 ignored. */
758 mcs = xen_mc_entry(0);
759
760 MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
761
762 xen_mc_issue(PARAVIRT_LAZY_CPU);
763}
764
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700765static void xen_write_cr4(unsigned long cr4)
766{
Jeremy Fitzhardinge2956a352008-05-26 23:31:04 +0100767 cr4 &= ~X86_CR4_PGE;
768 cr4 &= ~X86_CR4_PSE;
769
770 native_write_cr4(cr4);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700771}
772
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700773static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
774{
775 int ret;
776
777 ret = 0;
778
Tejf63c2f22008-12-16 11:56:06 -0800779 switch (msr) {
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700780#ifdef CONFIG_X86_64
781 unsigned which;
782 u64 base;
783
784 case MSR_FS_BASE: which = SEGBASE_FS; goto set;
785 case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
786 case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
787
788 set:
789 base = ((u64)high << 32) | low;
790 if (HYPERVISOR_set_segment_base(which, base) != 0)
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700791 ret = -EIO;
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700792 break;
793#endif
Jeremy Fitzhardinged89961e2008-07-24 13:48:58 -0700794
795 case MSR_STAR:
796 case MSR_CSTAR:
797 case MSR_LSTAR:
798 case MSR_SYSCALL_MASK:
799 case MSR_IA32_SYSENTER_CS:
800 case MSR_IA32_SYSENTER_ESP:
801 case MSR_IA32_SYSENTER_EIP:
802 /* Fast syscall setup is all done in hypercalls, so
803 these are all ignored. Stub them out here to stop
804 Xen console noise. */
805 break;
806
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700807 default:
808 ret = native_write_msr_safe(msr, low, high);
809 }
810
811 return ret;
812}
813
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100814void xen_setup_shared_info(void)
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700815{
816 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700817 set_fixmap(FIX_PARAVIRT_BOOTMAP,
818 xen_start_info->shared_info);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700819
Jeremy Fitzhardinge15664f92008-07-08 15:06:47 -0700820 HYPERVISOR_shared_info =
821 (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700822 } else
823 HYPERVISOR_shared_info =
824 (struct shared_info *)__va(xen_start_info->shared_info);
825
826#ifndef CONFIG_SMP
827 /* In UP this is as good a place as any to set up shared info */
828 xen_setup_vcpu_info_placement();
829#endif
Jeremy Fitzhardinged5edbc12008-05-26 23:31:22 +0100830
831 xen_setup_mfn_list_list();
Jeremy Fitzhardinge2e8fe712008-03-17 16:36:53 -0700832}
833
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700834/* This is called once we have the cpu_possible_map */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100835void xen_setup_vcpu_info_placement(void)
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700836{
837 int cpu;
838
839 for_each_possible_cpu(cpu)
840 xen_vcpu_setup(cpu);
841
842 /* xen_vcpu_setup managed to place the vcpu_info within the
843 percpu area for all cpus, so make use of it */
844 if (have_vcpu_info_placement) {
845 printk(KERN_INFO "Xen: using vcpu_info placement\n");
846
Jeremy Fitzhardingeecb93d12009-01-28 14:35:05 -0800847 pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
848 pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
849 pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
850 pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700851 pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
Jeremy Fitzhardinge60223a32007-07-17 18:37:07 -0700852 }
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700853}
854
Andi Kleenab144f52007-08-10 22:31:03 +0200855static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
856 unsigned long addr, unsigned len)
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700857{
858 char *start, *end, *reloc;
859 unsigned ret;
860
861 start = end = reloc = NULL;
862
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700863#define SITE(op, x) \
864 case PARAVIRT_PATCH(op.x): \
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700865 if (have_vcpu_info_placement) { \
866 start = (char *)xen_##x##_direct; \
867 end = xen_##x##_direct_end; \
868 reloc = xen_##x##_direct_reloc; \
869 } \
870 goto patch_site
871
872 switch (type) {
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700873 SITE(pv_irq_ops, irq_enable);
874 SITE(pv_irq_ops, irq_disable);
875 SITE(pv_irq_ops, save_fl);
876 SITE(pv_irq_ops, restore_fl);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700877#undef SITE
878
879 patch_site:
880 if (start == NULL || (end-start) > len)
881 goto default_patch;
882
Andi Kleenab144f52007-08-10 22:31:03 +0200883 ret = paravirt_patch_insns(insnbuf, len, start, end);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700884
885 /* Note: because reloc is assigned from something that
886 appears to be an array, gcc assumes it's non-null,
887 but doesn't know its relationship with start and
888 end. */
889 if (reloc > start && reloc < end) {
890 int reloc_off = reloc - start;
Andi Kleenab144f52007-08-10 22:31:03 +0200891 long *relocp = (long *)(insnbuf + reloc_off);
892 long delta = start - (char *)addr;
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700893
894 *relocp += delta;
895 }
896 break;
897
898 default_patch:
899 default:
Andi Kleenab144f52007-08-10 22:31:03 +0200900 ret = paravirt_patch_default(type, clobbers, insnbuf,
901 addr, len);
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700902 break;
903 }
904
905 return ret;
906}
907
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700908static const struct pv_info xen_info __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700909 .paravirt_enabled = 1,
910 .shared_kernel_pmd = 0,
911
912 .name = "Xen",
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700913};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700914
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700915static const struct pv_init_ops xen_init_ops __initdata = {
Jeremy Fitzhardinge64876732007-07-17 18:37:07 -0700916 .patch = xen_patch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700917};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700918
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700919static const struct pv_time_ops xen_time_ops __initdata = {
Jeremy Fitzhardingeab550282007-07-17 18:37:05 -0700920 .sched_clock = xen_sched_clock,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700921};
Jeremy Fitzhardinge15c84732007-07-17 18:37:05 -0700922
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700923static const struct pv_cpu_ops xen_cpu_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700924 .cpuid = xen_cpuid,
925
926 .set_debugreg = xen_set_debugreg,
927 .get_debugreg = xen_get_debugreg,
928
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100929 .clts = xen_clts,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700930
Jeremy Fitzhardingea789ed52009-04-24 00:26:50 -0700931 .read_cr0 = xen_read_cr0,
Jeremy Fitzhardinge7b1333a2008-05-26 23:31:01 +0100932 .write_cr0 = xen_write_cr0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700933
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700934 .read_cr4 = native_read_cr4,
935 .read_cr4_safe = native_read_cr4_safe,
936 .write_cr4 = xen_write_cr4,
937
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700938 .wbinvd = native_wbinvd,
939
940 .read_msr = native_read_msr_safe,
Jeremy Fitzhardinge11539682008-07-08 15:07:16 -0700941 .write_msr = xen_write_msr_safe,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700942 .read_tsc = native_read_tsc,
943 .read_pmc = native_read_pmc,
944
Jeremy Fitzhardinge81e103f2008-04-17 17:40:51 +0200945 .iret = xen_iret,
Jeremy Fitzhardinged75cd222008-06-25 00:19:26 -0400946 .irq_enable_sysexit = xen_sysexit,
Jeremy Fitzhardinge6fcac6d2008-07-08 15:07:14 -0700947#ifdef CONFIG_X86_64
948 .usergs_sysret32 = xen_sysret32,
949 .usergs_sysret64 = xen_sysret64,
950#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700951
952 .load_tr_desc = paravirt_nop,
953 .set_ldt = xen_set_ldt,
954 .load_gdt = xen_load_gdt,
955 .load_idt = xen_load_idt,
956 .load_tls = xen_load_tls,
Eduardo Habkosta8fc1082008-07-08 15:07:05 -0700957#ifdef CONFIG_X86_64
958 .load_gs_index = xen_load_gs_index,
959#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700960
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700961 .alloc_ldt = xen_alloc_ldt,
962 .free_ldt = xen_free_ldt,
963
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700964 .store_gdt = native_store_gdt,
965 .store_idt = native_store_idt,
966 .store_tr = xen_store_tr,
967
968 .write_ldt_entry = xen_write_ldt_entry,
969 .write_gdt_entry = xen_write_gdt_entry,
970 .write_idt_entry = xen_write_idt_entry,
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100971 .load_sp0 = xen_load_sp0,
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700972
973 .set_iopl_mask = xen_set_iopl_mask,
974 .io_delay = xen_io_delay,
975
Jeremy Fitzhardinge952d1d72008-07-08 15:07:01 -0700976 /* Xen takes care of %gs when switching to usermode for us */
977 .swapgs = paravirt_nop,
978
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800979 .start_context_switch = paravirt_start_context_switch,
980 .end_context_switch = xen_end_context_switch,
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700981};
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700982
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700983static const struct pv_apic_ops xen_apic_ops __initdata = {
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700984#ifdef CONFIG_X86_LOCAL_APIC
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700985 .startup_ipi_hook = paravirt_nop,
986#endif
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -0700987};
988
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700989static void xen_reboot(int reason)
990{
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +0100991 struct sched_shutdown r = { .reason = reason };
992
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700993#ifdef CONFIG_SMP
994 smp_send_stop();
995#endif
996
Jeremy Fitzhardinge349c7092008-05-26 23:31:02 +0100997 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -0700998 BUG();
999}
1000
1001static void xen_restart(char *msg)
1002{
1003 xen_reboot(SHUTDOWN_reboot);
1004}
1005
1006static void xen_emergency_restart(void)
1007{
1008 xen_reboot(SHUTDOWN_reboot);
1009}
1010
1011static void xen_machine_halt(void)
1012{
1013 xen_reboot(SHUTDOWN_poweroff);
1014}
1015
1016static void xen_crash_shutdown(struct pt_regs *regs)
1017{
1018 xen_reboot(SHUTDOWN_crash);
1019}
1020
1021static const struct machine_ops __initdata xen_machine_ops = {
1022 .restart = xen_restart,
1023 .halt = xen_machine_halt,
1024 .power_off = xen_machine_halt,
1025 .shutdown = xen_machine_halt,
1026 .crash_shutdown = xen_crash_shutdown,
1027 .emergency_restart = xen_emergency_restart,
1028};
1029
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001030/*
1031 * Set up the GDT and segment registers for -fstack-protector. Until
1032 * we do this, we have to be careful not to call any stack-protected
1033 * function, which is most of the kernel.
1034 */
1035static void __init xen_setup_stackprotector(void)
1036{
1037 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry_boot;
1038 pv_cpu_ops.load_gdt = xen_load_gdt_boot;
1039
1040 setup_stack_canary_segment(0);
1041 switch_to_new_gdt(0);
1042
1043 pv_cpu_ops.write_gdt_entry = xen_write_gdt_entry;
1044 pv_cpu_ops.load_gdt = xen_load_gdt;
1045}
1046
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001047/* First C function to be called on Xen boot */
1048asmlinkage void __init xen_start_kernel(void)
1049{
1050 pgd_t *pgd;
1051
1052 if (!xen_start_info)
1053 return;
1054
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001055 xen_domain_type = XEN_PV_DOMAIN;
1056
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001057 /* Install Xen paravirt ops */
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001058 pv_info = xen_info;
1059 pv_init_ops = xen_init_ops;
1060 pv_time_ops = xen_time_ops;
1061 pv_cpu_ops = xen_cpu_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001062 pv_apic_ops = xen_apic_ops;
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001063
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001064 x86_init.resources.memory_setup = xen_memory_setup;
Thomas Gleixner42bbdb42009-08-20 13:04:10 +02001065 x86_init.oem.arch_setup = xen_arch_setup;
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001066 x86_init.oem.banner = xen_banner;
Thomas Gleixner845b3942009-08-19 15:37:03 +02001067
1068 x86_init.timers.timer_init = xen_time_init;
Thomas Gleixner736deca2009-08-19 12:35:53 +02001069 x86_init.timers.setup_percpu_clockev = x86_init_noop;
1070 x86_cpuinit.setup_percpu_clockev = x86_init_noop;
Thomas Gleixner6b18ae32009-08-20 10:19:54 +02001071
Thomas Gleixner2d826402009-08-20 17:06:25 +02001072 x86_platform.calibrate_tsc = xen_tsc_khz;
Feng Tang7bd867d2009-09-10 10:48:56 +08001073 x86_platform.get_wallclock = xen_get_wallclock;
1074 x86_platform.set_wallclock = xen_set_wallclock;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001075
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001076 /*
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001077 * Set up some pagetable state before starting to set any ptes.
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001078 */
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001079
Jeremy Fitzhardinge973df352009-10-27 16:54:19 -07001080 xen_init_mmu_ops();
1081
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001082 /* Prevent unwanted bits from being set in PTEs. */
1083 __supported_pte_mask &= ~_PAGE_GLOBAL;
1084 if (!xen_initial_domain())
1085 __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
1086
1087 __supported_pte_mask |= _PAGE_IOMAP;
1088
Jeremy Fitzhardingeb75fe4e2009-09-21 13:34:06 -07001089#ifdef CONFIG_X86_64
1090 /* Work out if we support NX */
1091 check_efer();
1092#endif
1093
Jeremy Fitzhardinge577eebe2009-08-27 12:46:35 -07001094 xen_setup_features();
1095
1096 /* Get mfn list */
1097 if (!xen_feature(XENFEAT_auto_translated_physmap))
1098 xen_build_dynamic_phys_to_machine();
1099
1100 /*
1101 * Set up kernel GDT and segment registers, mainly so that
1102 * -fstack-protector code can be executed.
1103 */
1104 xen_setup_stackprotector();
Jeremy Fitzhardinge0d1edf42008-07-28 11:53:57 -07001105
Jeremy Fitzhardingece2eef32009-08-17 12:26:53 -07001106 xen_init_irq_ops();
Jeremy Fitzhardingee826fe12009-03-07 17:09:27 -08001107 xen_init_cpuid_mask();
1108
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001109#ifdef CONFIG_X86_LOCAL_APIC
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001110 /*
Yinghai Lu94a8c3c2008-07-13 22:19:35 -07001111 * set up the basic apic ops.
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001112 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -08001113 set_xen_basic_apic_ops();
Suresh Siddhaad66dd32008-07-11 13:11:56 -07001114#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001115
Jeremy Fitzhardingee57778a2008-06-16 04:30:02 -07001116 if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
1117 pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
1118 pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
1119 }
1120
Jeremy Fitzhardingefefa6292007-07-17 18:37:07 -07001121 machine_ops = xen_machine_ops;
1122
Jeremy Fitzhardinge38341432009-02-02 13:55:31 -08001123 /*
1124 * The only reliable way to retain the initial address of the
1125 * percpu gdt_page is to remember it here, so we can go and
1126 * mark it RW later, when the initial percpu area is freed.
1127 */
1128 xen_initial_gdt = &per_cpu(gdt_page, 0);
Jeremy Fitzhardinge795f99b2009-01-30 17:47:54 +09001129
Jeremy Fitzhardingea9e70622008-07-08 15:06:41 -07001130 xen_smp_init();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001131
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001132 pgd = (pgd_t *)xen_start_info->pt_base;
1133
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001134 /* Don't do the full vcpu_info placement stuff until we have a
1135 possible map and a non-dummy shared_info. */
1136 per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
1137
Jeremy Fitzhardinge55d80852009-02-25 09:42:25 -08001138 local_irq_disable();
1139 early_boot_irqs_off();
1140
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001141 xen_raw_console_write("mapping kernel into physical memory\n");
Jeremy Fitzhardinged114e1982008-07-08 15:06:52 -07001142 pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001143
1144 init_mm.pgd = pgd;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001145
1146 /* keep using Xen gdt for now; no urgent need to change it */
1147
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001148 pv_info.kernel_rpl = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001149 if (xen_feature(XENFEAT_supervisor_mode_kernel))
Jeremy Fitzhardinge93b1eab2007-10-16 11:51:29 -07001150 pv_info.kernel_rpl = 0;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001151
1152 /* set the limit of our address space */
Jeremy Fitzhardingefb1d8402007-10-16 11:51:31 -07001153 xen_reserve_top();
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001154
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001155#ifdef CONFIG_X86_32
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001156 /* set up basic CPUID stuff */
1157 cpu_detect(&new_cpu_data);
1158 new_cpu_data.hard_math = 1;
Jeremy Fitzhardinged560bc62009-08-25 12:53:02 -07001159 new_cpu_data.wp_works_ok = 1;
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001160 new_cpu_data.x86_capability[0] = cpuid_edx(1);
Jeremy Fitzhardinge7d087b62008-07-08 15:06:48 -07001161#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001162
1163 /* Poke various useful things into boot_params */
H. Peter Anvin30c82642007-10-15 17:13:22 -07001164 boot_params.hdr.type_of_loader = (9 << 4) | 0;
1165 boot_params.hdr.ramdisk_image = xen_start_info->mod_start
1166 ? __pa(xen_start_info->mod_start) : 0;
1167 boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
Jeremy Fitzhardingeb7c3c5c2008-07-08 15:07:07 -07001168 boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001169
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001170 if (!xen_initial_domain()) {
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +01001171 add_preferred_console("xenboot", 0, NULL);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001172 add_preferred_console("tty", 0, NULL);
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001173 add_preferred_console("hvc", 0, NULL);
Chris Wright5d990b62009-12-04 12:15:21 -08001174 } else {
1175 /* Make sure ACS will be enabled */
1176 pci_request_acs();
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001177 }
Chris Wright5d990b62009-12-04 12:15:21 -08001178
Markus Armbrusterb8c2d3d2008-02-27 14:56:35 +01001179
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001180 xen_raw_console_write("about to get started...\n");
1181
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001182 /* Start the world */
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001183#ifdef CONFIG_X86_32
Yinghai Luf0d43102008-05-29 12:56:36 -07001184 i386_start_kernel();
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001185#else
Jeremy Fitzhardinge084a2a42008-07-08 15:06:50 -07001186 x86_64_start_reservations((char *)__pa_symbol(&boot_params));
Jeremy Fitzhardingef5d36de2008-07-08 15:06:39 -07001187#endif
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -07001188}