Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 18 | #include <linux/hardirq.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 19 | #include <linux/percpu.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/start_kernel.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/bootmem.h> |
| 24 | #include <linux/module.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 25 | #include <linux/mm.h> |
| 26 | #include <linux/page-flags.h> |
| 27 | #include <linux/highmem.h> |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 28 | #include <linux/smp.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 29 | |
| 30 | #include <xen/interface/xen.h> |
| 31 | #include <xen/interface/physdev.h> |
| 32 | #include <xen/interface/vcpu.h> |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 33 | #include <xen/interface/sched.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 34 | #include <xen/features.h> |
| 35 | #include <xen/page.h> |
| 36 | |
| 37 | #include <asm/paravirt.h> |
| 38 | #include <asm/page.h> |
| 39 | #include <asm/xen/hypercall.h> |
| 40 | #include <asm/xen/hypervisor.h> |
| 41 | #include <asm/fixmap.h> |
| 42 | #include <asm/processor.h> |
| 43 | #include <asm/setup.h> |
| 44 | #include <asm/desc.h> |
| 45 | #include <asm/pgtable.h> |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | #include <asm/tlbflush.h> |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 47 | #include <asm/reboot.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 48 | |
| 49 | #include "xen-ops.h" |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 50 | #include "mmu.h" |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 51 | #include "multicalls.h" |
| 52 | |
| 53 | EXPORT_SYMBOL_GPL(hypercall_page); |
| 54 | |
| 55 | DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode); |
| 56 | |
| 57 | DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu); |
| 58 | DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info); |
| 59 | DEFINE_PER_CPU(unsigned long, xen_cr3); |
| 60 | |
| 61 | struct start_info *xen_start_info; |
| 62 | EXPORT_SYMBOL_GPL(xen_start_info); |
| 63 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 64 | static /* __initdata */ struct shared_info dummy_shared_info; |
| 65 | |
| 66 | /* |
| 67 | * Point at some empty memory to start with. We map the real shared_info |
| 68 | * page as soon as fixmap is up and running. |
| 69 | */ |
| 70 | struct shared_info *HYPERVISOR_shared_info = (void *)&dummy_shared_info; |
| 71 | |
| 72 | /* |
| 73 | * Flag to determine whether vcpu info placement is available on all |
| 74 | * VCPUs. We assume it is to start with, and then set it to zero on |
| 75 | * the first failure. This is because it can succeed on some VCPUs |
| 76 | * and not others, since it can involve hypervisor memory allocation, |
| 77 | * or because the guest failed to guarantee all the appropriate |
| 78 | * constraints on all VCPUs (ie buffer can't cross a page boundary). |
| 79 | * |
| 80 | * Note that any particular CPU may be using a placed vcpu structure, |
| 81 | * but we can only optimise if the all are. |
| 82 | * |
| 83 | * 0: not available, 1: available |
| 84 | */ |
| 85 | static int have_vcpu_info_placement = 1; |
| 86 | |
| 87 | static void __init xen_vcpu_setup(int cpu) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 88 | { |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 89 | struct vcpu_register_vcpu_info info; |
| 90 | int err; |
| 91 | struct vcpu_info *vcpup; |
| 92 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 93 | per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu]; |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 94 | |
| 95 | if (!have_vcpu_info_placement) |
| 96 | return; /* already tested, not available */ |
| 97 | |
| 98 | vcpup = &per_cpu(xen_vcpu_info, cpu); |
| 99 | |
| 100 | info.mfn = virt_to_mfn(vcpup); |
| 101 | info.offset = offset_in_page(vcpup); |
| 102 | |
| 103 | printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %x, offset %d\n", |
| 104 | cpu, vcpup, info.mfn, info.offset); |
| 105 | |
| 106 | /* Check to see if the hypervisor will put the vcpu_info |
| 107 | structure where we want it, which allows direct access via |
| 108 | a percpu-variable. */ |
| 109 | err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info); |
| 110 | |
| 111 | if (err) { |
| 112 | printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err); |
| 113 | have_vcpu_info_placement = 0; |
| 114 | } else { |
| 115 | /* This cpu is using the registered vcpu info, even if |
| 116 | later ones fail to. */ |
| 117 | per_cpu(xen_vcpu, cpu) = vcpup; |
| 118 | printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n", |
| 119 | cpu, vcpup); |
| 120 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void __init xen_banner(void) |
| 124 | { |
| 125 | printk(KERN_INFO "Booting paravirtualized kernel on %s\n", |
| 126 | paravirt_ops.name); |
| 127 | printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic); |
| 128 | } |
| 129 | |
| 130 | static void xen_cpuid(unsigned int *eax, unsigned int *ebx, |
| 131 | unsigned int *ecx, unsigned int *edx) |
| 132 | { |
| 133 | unsigned maskedx = ~0; |
| 134 | |
| 135 | /* |
| 136 | * Mask out inconvenient features, to try and disable as many |
| 137 | * unsupported kernel subsystems as possible. |
| 138 | */ |
| 139 | if (*eax == 1) |
| 140 | maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */ |
| 141 | (1 << X86_FEATURE_ACPI) | /* disable ACPI */ |
| 142 | (1 << X86_FEATURE_ACC)); /* thermal monitoring */ |
| 143 | |
| 144 | asm(XEN_EMULATE_PREFIX "cpuid" |
| 145 | : "=a" (*eax), |
| 146 | "=b" (*ebx), |
| 147 | "=c" (*ecx), |
| 148 | "=d" (*edx) |
| 149 | : "0" (*eax), "2" (*ecx)); |
| 150 | *edx &= maskedx; |
| 151 | } |
| 152 | |
| 153 | static void xen_set_debugreg(int reg, unsigned long val) |
| 154 | { |
| 155 | HYPERVISOR_set_debugreg(reg, val); |
| 156 | } |
| 157 | |
| 158 | static unsigned long xen_get_debugreg(int reg) |
| 159 | { |
| 160 | return HYPERVISOR_get_debugreg(reg); |
| 161 | } |
| 162 | |
| 163 | static unsigned long xen_save_fl(void) |
| 164 | { |
| 165 | struct vcpu_info *vcpu; |
| 166 | unsigned long flags; |
| 167 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 168 | vcpu = x86_read_percpu(xen_vcpu); |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 169 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 170 | /* flag has opposite sense of mask */ |
| 171 | flags = !vcpu->evtchn_upcall_mask; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 172 | |
| 173 | /* convert to IF type flag |
| 174 | -0 -> 0x00000000 |
| 175 | -1 -> 0xffffffff |
| 176 | */ |
| 177 | return (-flags) & X86_EFLAGS_IF; |
| 178 | } |
| 179 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 180 | static unsigned long xen_save_fl_direct(void) |
| 181 | { |
| 182 | unsigned long flags; |
| 183 | |
| 184 | /* flag has opposite sense of mask */ |
| 185 | flags = !x86_read_percpu(xen_vcpu_info.evtchn_upcall_mask); |
| 186 | |
| 187 | /* convert to IF type flag |
| 188 | -0 -> 0x00000000 |
| 189 | -1 -> 0xffffffff |
| 190 | */ |
| 191 | return (-flags) & X86_EFLAGS_IF; |
| 192 | } |
| 193 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 194 | static void xen_restore_fl(unsigned long flags) |
| 195 | { |
| 196 | struct vcpu_info *vcpu; |
| 197 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 198 | /* convert from IF type flag */ |
| 199 | flags = !(flags & X86_EFLAGS_IF); |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 200 | |
| 201 | /* There's a one instruction preempt window here. We need to |
| 202 | make sure we're don't switch CPUs between getting the vcpu |
| 203 | pointer and updating the mask. */ |
| 204 | preempt_disable(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 205 | vcpu = x86_read_percpu(xen_vcpu); |
| 206 | vcpu->evtchn_upcall_mask = flags; |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 207 | preempt_enable_no_resched(); |
| 208 | |
| 209 | /* Doesn't matter if we get preempted here, because any |
| 210 | pending event will get dealt with anyway. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 211 | |
| 212 | if (flags == 0) { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 213 | preempt_check_resched(); |
| 214 | barrier(); /* unmask then check (avoid races) */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 215 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 216 | force_evtchn_callback(); |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 217 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 220 | static void xen_restore_fl_direct(unsigned long flags) |
| 221 | { |
| 222 | /* convert from IF type flag */ |
| 223 | flags = !(flags & X86_EFLAGS_IF); |
| 224 | |
| 225 | /* This is an atomic update, so no need to worry about |
| 226 | preemption. */ |
| 227 | x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, flags); |
| 228 | |
| 229 | /* If we get preempted here, then any pending event will be |
| 230 | handled anyway. */ |
| 231 | |
| 232 | if (flags == 0) { |
| 233 | barrier(); /* unmask then check (avoid races) */ |
| 234 | if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending))) |
| 235 | force_evtchn_callback(); |
| 236 | } |
| 237 | } |
| 238 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 239 | static void xen_irq_disable(void) |
| 240 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 241 | /* There's a one instruction preempt window here. We need to |
| 242 | make sure we're don't switch CPUs between getting the vcpu |
| 243 | pointer and updating the mask. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 244 | preempt_disable(); |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 245 | x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 246 | preempt_enable_no_resched(); |
| 247 | } |
| 248 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 249 | static void xen_irq_disable_direct(void) |
| 250 | { |
| 251 | /* Atomic update, so preemption not a concern. */ |
| 252 | x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 1); |
| 253 | } |
| 254 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 255 | static void xen_irq_enable(void) |
| 256 | { |
| 257 | struct vcpu_info *vcpu; |
| 258 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 259 | /* There's a one instruction preempt window here. We need to |
| 260 | make sure we're don't switch CPUs between getting the vcpu |
| 261 | pointer and updating the mask. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 262 | preempt_disable(); |
| 263 | vcpu = x86_read_percpu(xen_vcpu); |
| 264 | vcpu->evtchn_upcall_mask = 0; |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 265 | preempt_enable_no_resched(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 266 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 267 | /* Doesn't matter if we get preempted here, because any |
| 268 | pending event will get dealt with anyway. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 269 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 270 | barrier(); /* unmask then check (avoid races) */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 271 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 272 | force_evtchn_callback(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 275 | static void xen_irq_enable_direct(void) |
| 276 | { |
| 277 | /* Atomic update, so preemption not a concern. */ |
| 278 | x86_write_percpu(xen_vcpu_info.evtchn_upcall_mask, 0); |
| 279 | |
| 280 | /* Doesn't matter if we get preempted here, because any |
| 281 | pending event will get dealt with anyway. */ |
| 282 | |
| 283 | barrier(); /* unmask then check (avoid races) */ |
| 284 | if (unlikely(x86_read_percpu(xen_vcpu_info.evtchn_upcall_pending))) |
| 285 | force_evtchn_callback(); |
| 286 | } |
| 287 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 288 | static void xen_safe_halt(void) |
| 289 | { |
| 290 | /* Blocking includes an implicit local_irq_enable(). */ |
| 291 | if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0) |
| 292 | BUG(); |
| 293 | } |
| 294 | |
| 295 | static void xen_halt(void) |
| 296 | { |
| 297 | if (irqs_disabled()) |
| 298 | HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL); |
| 299 | else |
| 300 | xen_safe_halt(); |
| 301 | } |
| 302 | |
| 303 | static void xen_set_lazy_mode(enum paravirt_lazy_mode mode) |
| 304 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 305 | BUG_ON(preemptible()); |
| 306 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 307 | switch (mode) { |
| 308 | case PARAVIRT_LAZY_NONE: |
| 309 | BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE); |
| 310 | break; |
| 311 | |
| 312 | case PARAVIRT_LAZY_MMU: |
| 313 | case PARAVIRT_LAZY_CPU: |
| 314 | BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE); |
| 315 | break; |
| 316 | |
| 317 | case PARAVIRT_LAZY_FLUSH: |
| 318 | /* flush if necessary, but don't change state */ |
| 319 | if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE) |
| 320 | xen_mc_flush(); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | xen_mc_flush(); |
| 325 | x86_write_percpu(xen_lazy_mode, mode); |
| 326 | } |
| 327 | |
| 328 | static unsigned long xen_store_tr(void) |
| 329 | { |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static void xen_set_ldt(const void *addr, unsigned entries) |
| 334 | { |
| 335 | unsigned long linear_addr = (unsigned long)addr; |
| 336 | struct mmuext_op *op; |
| 337 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
| 338 | |
| 339 | op = mcs.args; |
| 340 | op->cmd = MMUEXT_SET_LDT; |
| 341 | if (linear_addr) { |
| 342 | /* ldt my be vmalloced, use arbitrary_virt_to_machine */ |
| 343 | xmaddr_t maddr; |
| 344 | maddr = arbitrary_virt_to_machine((unsigned long)addr); |
| 345 | linear_addr = (unsigned long)maddr.maddr; |
| 346 | } |
| 347 | op->arg1.linear_addr = linear_addr; |
| 348 | op->arg2.nr_ents = entries; |
| 349 | |
| 350 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 351 | |
| 352 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 353 | } |
| 354 | |
| 355 | static void xen_load_gdt(const struct Xgt_desc_struct *dtr) |
| 356 | { |
| 357 | unsigned long *frames; |
| 358 | unsigned long va = dtr->address; |
| 359 | unsigned int size = dtr->size + 1; |
| 360 | unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 361 | int f; |
| 362 | struct multicall_space mcs; |
| 363 | |
| 364 | /* A GDT can be up to 64k in size, which corresponds to 8192 |
| 365 | 8-byte entries, or 16 4k pages.. */ |
| 366 | |
| 367 | BUG_ON(size > 65536); |
| 368 | BUG_ON(va & ~PAGE_MASK); |
| 369 | |
| 370 | mcs = xen_mc_entry(sizeof(*frames) * pages); |
| 371 | frames = mcs.args; |
| 372 | |
| 373 | for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) { |
| 374 | frames[f] = virt_to_mfn(va); |
| 375 | make_lowmem_page_readonly((void *)va); |
| 376 | } |
| 377 | |
| 378 | MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct)); |
| 379 | |
| 380 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 381 | } |
| 382 | |
| 383 | static void load_TLS_descriptor(struct thread_struct *t, |
| 384 | unsigned int cpu, unsigned int i) |
| 385 | { |
| 386 | struct desc_struct *gdt = get_cpu_gdt_table(cpu); |
| 387 | xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]); |
| 388 | struct multicall_space mc = __xen_mc_entry(0); |
| 389 | |
| 390 | MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]); |
| 391 | } |
| 392 | |
| 393 | static void xen_load_tls(struct thread_struct *t, unsigned int cpu) |
| 394 | { |
| 395 | xen_mc_batch(); |
| 396 | |
| 397 | load_TLS_descriptor(t, cpu, 0); |
| 398 | load_TLS_descriptor(t, cpu, 1); |
| 399 | load_TLS_descriptor(t, cpu, 2); |
| 400 | |
| 401 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
Jeremy Fitzhardinge | 8b84ad9 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * XXX sleazy hack: If we're being called in a lazy-cpu zone, |
| 405 | * it means we're in a context switch, and %gs has just been |
| 406 | * saved. This means we can zero it out to prevent faults on |
| 407 | * exit from the hypervisor if the next process has no %gs. |
| 408 | * Either way, it has been saved, and the new value will get |
| 409 | * loaded properly. This will go away as soon as Xen has been |
| 410 | * modified to not save/restore %gs for normal hypercalls. |
| 411 | */ |
| 412 | if (xen_get_lazy_mode() == PARAVIRT_LAZY_CPU) |
| 413 | loadsegment(gs, 0); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum, |
| 417 | u32 low, u32 high) |
| 418 | { |
| 419 | unsigned long lp = (unsigned long)&dt[entrynum]; |
| 420 | xmaddr_t mach_lp = virt_to_machine(lp); |
| 421 | u64 entry = (u64)high << 32 | low; |
| 422 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 423 | preempt_disable(); |
| 424 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 425 | xen_mc_flush(); |
| 426 | if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry)) |
| 427 | BUG(); |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 428 | |
| 429 | preempt_enable(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int cvt_gate_to_trap(int vector, u32 low, u32 high, |
| 433 | struct trap_info *info) |
| 434 | { |
| 435 | u8 type, dpl; |
| 436 | |
| 437 | type = (high >> 8) & 0x1f; |
| 438 | dpl = (high >> 13) & 3; |
| 439 | |
| 440 | if (type != 0xf && type != 0xe) |
| 441 | return 0; |
| 442 | |
| 443 | info->vector = vector; |
| 444 | info->address = (high & 0xffff0000) | (low & 0x0000ffff); |
| 445 | info->cs = low >> 16; |
| 446 | info->flags = dpl; |
| 447 | /* interrupt gates clear IF */ |
| 448 | if (type == 0xe) |
| 449 | info->flags |= 4; |
| 450 | |
| 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | /* Locations of each CPU's IDT */ |
| 455 | static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc); |
| 456 | |
| 457 | /* Set an IDT entry. If the entry is part of the current IDT, then |
| 458 | also update Xen. */ |
| 459 | static void xen_write_idt_entry(struct desc_struct *dt, int entrynum, |
| 460 | u32 low, u32 high) |
| 461 | { |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 462 | unsigned long p = (unsigned long)&dt[entrynum]; |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 463 | unsigned long start, end; |
| 464 | |
| 465 | preempt_disable(); |
| 466 | |
| 467 | start = __get_cpu_var(idt_desc).address; |
| 468 | end = start + __get_cpu_var(idt_desc).size + 1; |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 469 | |
| 470 | xen_mc_flush(); |
| 471 | |
| 472 | write_dt_entry(dt, entrynum, low, high); |
| 473 | |
| 474 | if (p >= start && (p + 8) <= end) { |
| 475 | struct trap_info info[2]; |
| 476 | |
| 477 | info[1].address = 0; |
| 478 | |
| 479 | if (cvt_gate_to_trap(entrynum, low, high, &info[0])) |
| 480 | if (HYPERVISOR_set_trap_table(info)) |
| 481 | BUG(); |
| 482 | } |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 483 | |
| 484 | preempt_enable(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 487 | static void xen_convert_trap_info(const struct Xgt_desc_struct *desc, |
| 488 | struct trap_info *traps) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 489 | { |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 490 | unsigned in, out, count; |
| 491 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 492 | count = (desc->size+1) / 8; |
| 493 | BUG_ON(count > 256); |
| 494 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 495 | for (in = out = 0; in < count; in++) { |
| 496 | const u32 *entry = (u32 *)(desc->address + in * 8); |
| 497 | |
| 498 | if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out])) |
| 499 | out++; |
| 500 | } |
| 501 | traps[out].address = 0; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void xen_copy_trap_info(struct trap_info *traps) |
| 505 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 506 | const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 507 | |
| 508 | xen_convert_trap_info(desc, traps); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Load a new IDT into Xen. In principle this can be per-CPU, so we |
| 512 | hold a spinlock to protect the static traps[] array (static because |
| 513 | it avoids allocation, and saves stack space). */ |
| 514 | static void xen_load_idt(const struct Xgt_desc_struct *desc) |
| 515 | { |
| 516 | static DEFINE_SPINLOCK(lock); |
| 517 | static struct trap_info traps[257]; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 518 | |
| 519 | spin_lock(&lock); |
| 520 | |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 521 | __get_cpu_var(idt_desc) = *desc; |
| 522 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 523 | xen_convert_trap_info(desc, traps); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 524 | |
| 525 | xen_mc_flush(); |
| 526 | if (HYPERVISOR_set_trap_table(traps)) |
| 527 | BUG(); |
| 528 | |
| 529 | spin_unlock(&lock); |
| 530 | } |
| 531 | |
| 532 | /* Write a GDT descriptor entry. Ignore LDT descriptors, since |
| 533 | they're handled differently. */ |
| 534 | static void xen_write_gdt_entry(struct desc_struct *dt, int entry, |
| 535 | u32 low, u32 high) |
| 536 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 537 | preempt_disable(); |
| 538 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 539 | switch ((high >> 8) & 0xff) { |
| 540 | case DESCTYPE_LDT: |
| 541 | case DESCTYPE_TSS: |
| 542 | /* ignore */ |
| 543 | break; |
| 544 | |
| 545 | default: { |
| 546 | xmaddr_t maddr = virt_to_machine(&dt[entry]); |
| 547 | u64 desc = (u64)high << 32 | low; |
| 548 | |
| 549 | xen_mc_flush(); |
| 550 | if (HYPERVISOR_update_descriptor(maddr.maddr, desc)) |
| 551 | BUG(); |
| 552 | } |
| 553 | |
| 554 | } |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 555 | |
| 556 | preempt_enable(); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static void xen_load_esp0(struct tss_struct *tss, |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 560 | struct thread_struct *thread) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 561 | { |
| 562 | struct multicall_space mcs = xen_mc_entry(0); |
| 563 | MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0); |
| 564 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 565 | } |
| 566 | |
| 567 | static void xen_set_iopl_mask(unsigned mask) |
| 568 | { |
| 569 | struct physdev_set_iopl set_iopl; |
| 570 | |
| 571 | /* Force the change at ring 0. */ |
| 572 | set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3; |
| 573 | HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl); |
| 574 | } |
| 575 | |
| 576 | static void xen_io_delay(void) |
| 577 | { |
| 578 | } |
| 579 | |
| 580 | #ifdef CONFIG_X86_LOCAL_APIC |
| 581 | static unsigned long xen_apic_read(unsigned long reg) |
| 582 | { |
| 583 | return 0; |
| 584 | } |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 585 | |
| 586 | static void xen_apic_write(unsigned long reg, unsigned long val) |
| 587 | { |
| 588 | /* Warn to see if there's any stray references */ |
| 589 | WARN_ON(1); |
| 590 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 591 | #endif |
| 592 | |
| 593 | static void xen_flush_tlb(void) |
| 594 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 595 | struct mmuext_op *op; |
| 596 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 597 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 598 | op = mcs.args; |
| 599 | op->cmd = MMUEXT_TLB_FLUSH_LOCAL; |
| 600 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 601 | |
| 602 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void xen_flush_tlb_single(unsigned long addr) |
| 606 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 607 | struct mmuext_op *op; |
| 608 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 609 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 610 | op = mcs.args; |
| 611 | op->cmd = MMUEXT_INVLPG_LOCAL; |
| 612 | op->arg1.linear_addr = addr & PAGE_MASK; |
| 613 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 614 | |
| 615 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 618 | static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm, |
| 619 | unsigned long va) |
| 620 | { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 621 | struct { |
| 622 | struct mmuext_op op; |
| 623 | cpumask_t mask; |
| 624 | } *args; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 625 | cpumask_t cpumask = *cpus; |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 626 | struct multicall_space mcs; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * A couple of (to be removed) sanity checks: |
| 630 | * |
| 631 | * - current CPU must not be in mask |
| 632 | * - mask must exist :) |
| 633 | */ |
| 634 | BUG_ON(cpus_empty(cpumask)); |
| 635 | BUG_ON(cpu_isset(smp_processor_id(), cpumask)); |
| 636 | BUG_ON(!mm); |
| 637 | |
| 638 | /* If a CPU which we ran on has gone down, OK. */ |
| 639 | cpus_and(cpumask, cpumask, cpu_online_map); |
| 640 | if (cpus_empty(cpumask)) |
| 641 | return; |
| 642 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 643 | mcs = xen_mc_entry(sizeof(*args)); |
| 644 | args = mcs.args; |
| 645 | args->mask = cpumask; |
| 646 | args->op.arg2.vcpumask = &args->mask; |
| 647 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 648 | if (va == TLB_FLUSH_ALL) { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 649 | args->op.cmd = MMUEXT_TLB_FLUSH_MULTI; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 650 | } else { |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 651 | args->op.cmd = MMUEXT_INVLPG_MULTI; |
| 652 | args->op.arg1.linear_addr = va; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jeremy Fitzhardinge | d66bf8f | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 655 | MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF); |
| 656 | |
| 657 | xen_mc_issue(PARAVIRT_LAZY_MMU); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 660 | static void xen_write_cr2(unsigned long cr2) |
| 661 | { |
| 662 | x86_read_percpu(xen_vcpu)->arch.cr2 = cr2; |
| 663 | } |
| 664 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 665 | static unsigned long xen_read_cr2(void) |
| 666 | { |
| 667 | return x86_read_percpu(xen_vcpu)->arch.cr2; |
| 668 | } |
| 669 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 670 | static unsigned long xen_read_cr2_direct(void) |
| 671 | { |
| 672 | return x86_read_percpu(xen_vcpu_info.arch.cr2); |
| 673 | } |
| 674 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 675 | static void xen_write_cr4(unsigned long cr4) |
| 676 | { |
| 677 | /* never allow TSC to be disabled */ |
| 678 | native_write_cr4(cr4 & ~X86_CR4_TSD); |
| 679 | } |
| 680 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 681 | static unsigned long xen_read_cr3(void) |
| 682 | { |
| 683 | return x86_read_percpu(xen_cr3); |
| 684 | } |
| 685 | |
| 686 | static void xen_write_cr3(unsigned long cr3) |
| 687 | { |
Jeremy Fitzhardinge | f120f13 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 688 | BUG_ON(preemptible()); |
| 689 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 690 | if (cr3 == x86_read_percpu(xen_cr3)) { |
| 691 | /* just a simple tlb flush */ |
| 692 | xen_flush_tlb(); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | x86_write_percpu(xen_cr3, cr3); |
| 697 | |
| 698 | |
| 699 | { |
| 700 | struct mmuext_op *op; |
| 701 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
| 702 | unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3)); |
| 703 | |
| 704 | op = mcs.args; |
| 705 | op->cmd = MMUEXT_NEW_BASEPTR; |
| 706 | op->arg1.mfn = mfn; |
| 707 | |
| 708 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 709 | |
| 710 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 711 | } |
| 712 | } |
| 713 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 714 | /* Early in boot, while setting up the initial pagetable, assume |
| 715 | everything is pinned. */ |
Jeremy Fitzhardinge | 9a4029f | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 716 | static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn) |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 717 | { |
| 718 | BUG_ON(mem_map); /* should only be used early */ |
| 719 | make_lowmem_page_readonly(__va(PFN_PHYS(pfn))); |
| 720 | } |
| 721 | |
| 722 | /* This needs to make sure the new pte page is pinned iff its being |
| 723 | attached to a pinned pagetable. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 724 | static void xen_alloc_pt(struct mm_struct *mm, u32 pfn) |
| 725 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 726 | struct page *page = pfn_to_page(pfn); |
| 727 | |
| 728 | if (PagePinned(virt_to_page(mm->pgd))) { |
| 729 | SetPagePinned(page); |
| 730 | |
| 731 | if (!PageHighMem(page)) |
| 732 | make_lowmem_page_readonly(__va(PFN_PHYS(pfn))); |
| 733 | else |
| 734 | /* make sure there are no stray mappings of |
| 735 | this page */ |
| 736 | kmap_flush_unused(); |
| 737 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 740 | /* This should never happen until we're OK to use struct page */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 741 | static void xen_release_pt(u32 pfn) |
| 742 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 743 | struct page *page = pfn_to_page(pfn); |
| 744 | |
| 745 | if (PagePinned(page)) { |
| 746 | if (!PageHighMem(page)) |
| 747 | make_lowmem_page_readwrite(__va(PFN_PHYS(pfn))); |
| 748 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 751 | #ifdef CONFIG_HIGHPTE |
| 752 | static void *xen_kmap_atomic_pte(struct page *page, enum km_type type) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 753 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 754 | pgprot_t prot = PAGE_KERNEL; |
| 755 | |
| 756 | if (PagePinned(page)) |
| 757 | prot = PAGE_KERNEL_RO; |
| 758 | |
| 759 | if (0 && PageHighMem(page)) |
| 760 | printk("mapping highpte %lx type %d prot %s\n", |
| 761 | page_to_pfn(page), type, |
| 762 | (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ"); |
| 763 | |
| 764 | return kmap_atomic_prot(page, type, prot); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 765 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 766 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 767 | |
Jeremy Fitzhardinge | 9a4029f | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 768 | static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte) |
| 769 | { |
| 770 | /* If there's an existing pte, then don't allow _PAGE_RW to be set */ |
| 771 | if (pte_val_ma(*ptep) & _PAGE_PRESENT) |
| 772 | pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) & |
| 773 | pte_val_ma(pte)); |
| 774 | |
| 775 | return pte; |
| 776 | } |
| 777 | |
| 778 | /* Init-time set_pte while constructing initial pagetables, which |
| 779 | doesn't allow RO pagetable pages to be remapped RW */ |
| 780 | static __init void xen_set_pte_init(pte_t *ptep, pte_t pte) |
| 781 | { |
| 782 | pte = mask_rw_pte(ptep, pte); |
| 783 | |
| 784 | xen_set_pte(ptep, pte); |
| 785 | } |
| 786 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 787 | static __init void xen_pagetable_setup_start(pgd_t *base) |
| 788 | { |
| 789 | pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base; |
| 790 | |
Jeremy Fitzhardinge | 9a4029f | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 791 | /* special set_pte for pagetable initialization */ |
| 792 | paravirt_ops.set_pte = xen_set_pte_init; |
| 793 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 794 | init_mm.pgd = base; |
| 795 | /* |
| 796 | * copy top-level of Xen-supplied pagetable into place. For |
| 797 | * !PAE we can use this as-is, but for PAE it is a stand-in |
| 798 | * while we copy the pmd pages. |
| 799 | */ |
| 800 | memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t)); |
| 801 | |
| 802 | if (PTRS_PER_PMD > 1) { |
| 803 | int i; |
| 804 | /* |
| 805 | * For PAE, need to allocate new pmds, rather than |
| 806 | * share Xen's, since Xen doesn't like pmd's being |
| 807 | * shared between address spaces. |
| 808 | */ |
| 809 | for (i = 0; i < PTRS_PER_PGD; i++) { |
| 810 | if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) { |
| 811 | pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE); |
| 812 | |
| 813 | memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]), |
| 814 | PAGE_SIZE); |
| 815 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 816 | make_lowmem_page_readonly(pmd); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 817 | |
| 818 | set_pgd(&base[i], __pgd(1 + __pa(pmd))); |
| 819 | } else |
| 820 | pgd_clear(&base[i]); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* make sure zero_page is mapped RO so we can use it in pagetables */ |
| 825 | make_lowmem_page_readonly(empty_zero_page); |
| 826 | make_lowmem_page_readonly(base); |
| 827 | /* |
| 828 | * Switch to new pagetable. This is done before |
| 829 | * pagetable_init has done anything so that the new pages |
| 830 | * added to the table can be prepared properly for Xen. |
| 831 | */ |
| 832 | xen_write_cr3(__pa(base)); |
| 833 | } |
| 834 | |
| 835 | static __init void xen_pagetable_setup_done(pgd_t *base) |
| 836 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 837 | /* This will work as long as patching hasn't happened yet |
| 838 | (which it hasn't) */ |
| 839 | paravirt_ops.alloc_pt = xen_alloc_pt; |
Jeremy Fitzhardinge | 9a4029f | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 840 | paravirt_ops.set_pte = xen_set_pte; |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 841 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 842 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
| 843 | /* |
| 844 | * Create a mapping for the shared info page. |
| 845 | * Should be set_fixmap(), but shared_info is a machine |
| 846 | * address with no corresponding pseudo-phys address. |
| 847 | */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 848 | set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP), |
| 849 | PFN_DOWN(xen_start_info->shared_info), |
| 850 | PAGE_KERNEL); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 851 | |
| 852 | HYPERVISOR_shared_info = |
| 853 | (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP); |
| 854 | |
| 855 | } else |
| 856 | HYPERVISOR_shared_info = |
| 857 | (struct shared_info *)__va(xen_start_info->shared_info); |
| 858 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 859 | /* Actually pin the pagetable down, but we can't set PG_pinned |
| 860 | yet because the page structures don't exist yet. */ |
| 861 | { |
| 862 | struct mmuext_op op; |
| 863 | #ifdef CONFIG_X86_PAE |
| 864 | op.cmd = MMUEXT_PIN_L3_TABLE; |
| 865 | #else |
| 866 | op.cmd = MMUEXT_PIN_L3_TABLE; |
| 867 | #endif |
| 868 | op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base))); |
| 869 | if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) |
| 870 | BUG(); |
| 871 | } |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 872 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 873 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 874 | /* This is called once we have the cpu_possible_map */ |
| 875 | void __init xen_setup_vcpu_info_placement(void) |
| 876 | { |
| 877 | int cpu; |
| 878 | |
| 879 | for_each_possible_cpu(cpu) |
| 880 | xen_vcpu_setup(cpu); |
| 881 | |
| 882 | /* xen_vcpu_setup managed to place the vcpu_info within the |
| 883 | percpu area for all cpus, so make use of it */ |
| 884 | if (have_vcpu_info_placement) { |
| 885 | printk(KERN_INFO "Xen: using vcpu_info placement\n"); |
| 886 | |
| 887 | paravirt_ops.save_fl = xen_save_fl_direct; |
| 888 | paravirt_ops.restore_fl = xen_restore_fl_direct; |
| 889 | paravirt_ops.irq_disable = xen_irq_disable_direct; |
| 890 | paravirt_ops.irq_enable = xen_irq_enable_direct; |
| 891 | paravirt_ops.read_cr2 = xen_read_cr2_direct; |
| 892 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static const struct paravirt_ops xen_paravirt_ops __initdata = { |
| 896 | .paravirt_enabled = 1, |
| 897 | .shared_kernel_pmd = 0, |
| 898 | |
| 899 | .name = "Xen", |
| 900 | .banner = xen_banner, |
| 901 | |
| 902 | .patch = paravirt_patch_default, |
| 903 | |
| 904 | .memory_setup = xen_memory_setup, |
| 905 | .arch_setup = xen_arch_setup, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 906 | .init_IRQ = xen_init_IRQ, |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 907 | .post_allocator_init = xen_mark_init_mm_pinned, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 908 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 909 | .time_init = xen_time_init, |
| 910 | .set_wallclock = xen_set_wallclock, |
| 911 | .get_wallclock = xen_get_wallclock, |
| 912 | .get_cpu_khz = xen_cpu_khz, |
Jeremy Fitzhardinge | ab55028 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 913 | .sched_clock = xen_sched_clock, |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 914 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 915 | .cpuid = xen_cpuid, |
| 916 | |
| 917 | .set_debugreg = xen_set_debugreg, |
| 918 | .get_debugreg = xen_get_debugreg, |
| 919 | |
| 920 | .clts = native_clts, |
| 921 | |
| 922 | .read_cr0 = native_read_cr0, |
| 923 | .write_cr0 = native_write_cr0, |
| 924 | |
| 925 | .read_cr2 = xen_read_cr2, |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 926 | .write_cr2 = xen_write_cr2, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 927 | |
| 928 | .read_cr3 = xen_read_cr3, |
| 929 | .write_cr3 = xen_write_cr3, |
| 930 | |
| 931 | .read_cr4 = native_read_cr4, |
| 932 | .read_cr4_safe = native_read_cr4_safe, |
| 933 | .write_cr4 = xen_write_cr4, |
| 934 | |
| 935 | .save_fl = xen_save_fl, |
| 936 | .restore_fl = xen_restore_fl, |
| 937 | .irq_disable = xen_irq_disable, |
| 938 | .irq_enable = xen_irq_enable, |
| 939 | .safe_halt = xen_safe_halt, |
| 940 | .halt = xen_halt, |
| 941 | .wbinvd = native_wbinvd, |
| 942 | |
| 943 | .read_msr = native_read_msr_safe, |
| 944 | .write_msr = native_write_msr_safe, |
| 945 | .read_tsc = native_read_tsc, |
| 946 | .read_pmc = native_read_pmc, |
| 947 | |
| 948 | .iret = (void *)&hypercall_page[__HYPERVISOR_iret], |
| 949 | .irq_enable_sysexit = NULL, /* never called */ |
| 950 | |
| 951 | .load_tr_desc = paravirt_nop, |
| 952 | .set_ldt = xen_set_ldt, |
| 953 | .load_gdt = xen_load_gdt, |
| 954 | .load_idt = xen_load_idt, |
| 955 | .load_tls = xen_load_tls, |
| 956 | |
| 957 | .store_gdt = native_store_gdt, |
| 958 | .store_idt = native_store_idt, |
| 959 | .store_tr = xen_store_tr, |
| 960 | |
| 961 | .write_ldt_entry = xen_write_ldt_entry, |
| 962 | .write_gdt_entry = xen_write_gdt_entry, |
| 963 | .write_idt_entry = xen_write_idt_entry, |
| 964 | .load_esp0 = xen_load_esp0, |
| 965 | |
| 966 | .set_iopl_mask = xen_set_iopl_mask, |
| 967 | .io_delay = xen_io_delay, |
| 968 | |
| 969 | #ifdef CONFIG_X86_LOCAL_APIC |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 970 | .apic_write = xen_apic_write, |
| 971 | .apic_write_atomic = xen_apic_write, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 972 | .apic_read = xen_apic_read, |
| 973 | .setup_boot_clock = paravirt_nop, |
| 974 | .setup_secondary_clock = paravirt_nop, |
| 975 | .startup_ipi_hook = paravirt_nop, |
| 976 | #endif |
| 977 | |
| 978 | .flush_tlb_user = xen_flush_tlb, |
| 979 | .flush_tlb_kernel = xen_flush_tlb, |
| 980 | .flush_tlb_single = xen_flush_tlb_single, |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 981 | .flush_tlb_others = xen_flush_tlb_others, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 982 | |
| 983 | .pte_update = paravirt_nop, |
| 984 | .pte_update_defer = paravirt_nop, |
| 985 | |
| 986 | .pagetable_setup_start = xen_pagetable_setup_start, |
| 987 | .pagetable_setup_done = xen_pagetable_setup_done, |
| 988 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 989 | .alloc_pt = xen_alloc_pt_init, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 990 | .release_pt = xen_release_pt, |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 991 | .alloc_pd = paravirt_nop, |
| 992 | .alloc_pd_clone = paravirt_nop, |
| 993 | .release_pd = paravirt_nop, |
| 994 | |
| 995 | #ifdef CONFIG_HIGHPTE |
| 996 | .kmap_atomic_pte = xen_kmap_atomic_pte, |
| 997 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 998 | |
Jeremy Fitzhardinge | 9a4029f | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 999 | .set_pte = NULL, /* see xen_pagetable_setup_* */ |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1000 | .set_pte_at = xen_set_pte_at, |
| 1001 | .set_pmd = xen_set_pmd, |
| 1002 | |
| 1003 | .pte_val = xen_pte_val, |
| 1004 | .pgd_val = xen_pgd_val, |
| 1005 | |
| 1006 | .make_pte = xen_make_pte, |
| 1007 | .make_pgd = xen_make_pgd, |
| 1008 | |
| 1009 | #ifdef CONFIG_X86_PAE |
| 1010 | .set_pte_atomic = xen_set_pte_atomic, |
| 1011 | .set_pte_present = xen_set_pte_at, |
| 1012 | .set_pud = xen_set_pud, |
| 1013 | .pte_clear = xen_pte_clear, |
| 1014 | .pmd_clear = xen_pmd_clear, |
| 1015 | |
| 1016 | .make_pmd = xen_make_pmd, |
| 1017 | .pmd_val = xen_pmd_val, |
| 1018 | #endif /* PAE */ |
| 1019 | |
| 1020 | .activate_mm = xen_activate_mm, |
| 1021 | .dup_mmap = xen_dup_mmap, |
| 1022 | .exit_mmap = xen_exit_mmap, |
| 1023 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1024 | .set_lazy_mode = xen_set_lazy_mode, |
| 1025 | }; |
| 1026 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1027 | #ifdef CONFIG_SMP |
| 1028 | static const struct smp_ops xen_smp_ops __initdata = { |
| 1029 | .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu, |
| 1030 | .smp_prepare_cpus = xen_smp_prepare_cpus, |
| 1031 | .cpu_up = xen_cpu_up, |
| 1032 | .smp_cpus_done = xen_smp_cpus_done, |
| 1033 | |
| 1034 | .smp_send_stop = xen_smp_send_stop, |
| 1035 | .smp_send_reschedule = xen_smp_send_reschedule, |
| 1036 | .smp_call_function_mask = xen_smp_call_function_mask, |
| 1037 | }; |
| 1038 | #endif /* CONFIG_SMP */ |
| 1039 | |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 1040 | static void xen_reboot(int reason) |
| 1041 | { |
| 1042 | #ifdef CONFIG_SMP |
| 1043 | smp_send_stop(); |
| 1044 | #endif |
| 1045 | |
| 1046 | if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason)) |
| 1047 | BUG(); |
| 1048 | } |
| 1049 | |
| 1050 | static void xen_restart(char *msg) |
| 1051 | { |
| 1052 | xen_reboot(SHUTDOWN_reboot); |
| 1053 | } |
| 1054 | |
| 1055 | static void xen_emergency_restart(void) |
| 1056 | { |
| 1057 | xen_reboot(SHUTDOWN_reboot); |
| 1058 | } |
| 1059 | |
| 1060 | static void xen_machine_halt(void) |
| 1061 | { |
| 1062 | xen_reboot(SHUTDOWN_poweroff); |
| 1063 | } |
| 1064 | |
| 1065 | static void xen_crash_shutdown(struct pt_regs *regs) |
| 1066 | { |
| 1067 | xen_reboot(SHUTDOWN_crash); |
| 1068 | } |
| 1069 | |
| 1070 | static const struct machine_ops __initdata xen_machine_ops = { |
| 1071 | .restart = xen_restart, |
| 1072 | .halt = xen_machine_halt, |
| 1073 | .power_off = xen_machine_halt, |
| 1074 | .shutdown = xen_machine_halt, |
| 1075 | .crash_shutdown = xen_crash_shutdown, |
| 1076 | .emergency_restart = xen_emergency_restart, |
| 1077 | }; |
| 1078 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1079 | /* First C function to be called on Xen boot */ |
| 1080 | asmlinkage void __init xen_start_kernel(void) |
| 1081 | { |
| 1082 | pgd_t *pgd; |
| 1083 | |
| 1084 | if (!xen_start_info) |
| 1085 | return; |
| 1086 | |
| 1087 | BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0); |
| 1088 | |
| 1089 | /* Install Xen paravirt ops */ |
| 1090 | paravirt_ops = xen_paravirt_ops; |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 1091 | machine_ops = xen_machine_ops; |
| 1092 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1093 | #ifdef CONFIG_SMP |
| 1094 | smp_ops = xen_smp_ops; |
| 1095 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1096 | |
| 1097 | xen_setup_features(); |
| 1098 | |
| 1099 | /* Get mfn list */ |
| 1100 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 1101 | phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list; |
| 1102 | |
| 1103 | pgd = (pgd_t *)xen_start_info->pt_base; |
| 1104 | |
| 1105 | init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE; |
| 1106 | |
| 1107 | init_mm.pgd = pgd; /* use the Xen pagetables to start */ |
| 1108 | |
| 1109 | /* keep using Xen gdt for now; no urgent need to change it */ |
| 1110 | |
| 1111 | x86_write_percpu(xen_cr3, __pa(pgd)); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame^] | 1112 | |
| 1113 | #ifdef CONFIG_SMP |
| 1114 | /* Don't do the full vcpu_info placement stuff until we have a |
| 1115 | possible map. */ |
| 1116 | per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0]; |
| 1117 | #else |
| 1118 | /* May as well do it now, since there's no good time to call |
| 1119 | it later on UP. */ |
| 1120 | xen_setup_vcpu_info_placement(); |
| 1121 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1122 | |
| 1123 | paravirt_ops.kernel_rpl = 1; |
| 1124 | if (xen_feature(XENFEAT_supervisor_mode_kernel)) |
| 1125 | paravirt_ops.kernel_rpl = 0; |
| 1126 | |
| 1127 | /* set the limit of our address space */ |
| 1128 | reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE); |
| 1129 | |
| 1130 | /* set up basic CPUID stuff */ |
| 1131 | cpu_detect(&new_cpu_data); |
| 1132 | new_cpu_data.hard_math = 1; |
| 1133 | new_cpu_data.x86_capability[0] = cpuid_edx(1); |
| 1134 | |
| 1135 | /* Poke various useful things into boot_params */ |
| 1136 | LOADER_TYPE = (9 << 4) | 0; |
| 1137 | INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0; |
| 1138 | INITRD_SIZE = xen_start_info->mod_len; |
| 1139 | |
| 1140 | /* Start the world */ |
| 1141 | start_kernel(); |
| 1142 | } |