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> |
| 18 | #include <linux/percpu.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/start_kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/bootmem.h> |
| 23 | #include <linux/module.h> |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 24 | #include <linux/mm.h> |
| 25 | #include <linux/page-flags.h> |
| 26 | #include <linux/highmem.h> |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 27 | |
| 28 | #include <xen/interface/xen.h> |
| 29 | #include <xen/interface/physdev.h> |
| 30 | #include <xen/interface/vcpu.h> |
| 31 | #include <xen/features.h> |
| 32 | #include <xen/page.h> |
| 33 | |
| 34 | #include <asm/paravirt.h> |
| 35 | #include <asm/page.h> |
| 36 | #include <asm/xen/hypercall.h> |
| 37 | #include <asm/xen/hypervisor.h> |
| 38 | #include <asm/fixmap.h> |
| 39 | #include <asm/processor.h> |
| 40 | #include <asm/setup.h> |
| 41 | #include <asm/desc.h> |
| 42 | #include <asm/pgtable.h> |
| 43 | |
| 44 | #include "xen-ops.h" |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 45 | #include "mmu.h" |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 46 | #include "multicalls.h" |
| 47 | |
| 48 | EXPORT_SYMBOL_GPL(hypercall_page); |
| 49 | |
| 50 | DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode); |
| 51 | |
| 52 | DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu); |
| 53 | DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info); |
| 54 | DEFINE_PER_CPU(unsigned long, xen_cr3); |
| 55 | |
| 56 | struct start_info *xen_start_info; |
| 57 | EXPORT_SYMBOL_GPL(xen_start_info); |
| 58 | |
| 59 | static void xen_vcpu_setup(int cpu) |
| 60 | { |
| 61 | per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu]; |
| 62 | } |
| 63 | |
| 64 | static void __init xen_banner(void) |
| 65 | { |
| 66 | printk(KERN_INFO "Booting paravirtualized kernel on %s\n", |
| 67 | paravirt_ops.name); |
| 68 | printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic); |
| 69 | } |
| 70 | |
| 71 | static void xen_cpuid(unsigned int *eax, unsigned int *ebx, |
| 72 | unsigned int *ecx, unsigned int *edx) |
| 73 | { |
| 74 | unsigned maskedx = ~0; |
| 75 | |
| 76 | /* |
| 77 | * Mask out inconvenient features, to try and disable as many |
| 78 | * unsupported kernel subsystems as possible. |
| 79 | */ |
| 80 | if (*eax == 1) |
| 81 | maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */ |
| 82 | (1 << X86_FEATURE_ACPI) | /* disable ACPI */ |
| 83 | (1 << X86_FEATURE_ACC)); /* thermal monitoring */ |
| 84 | |
| 85 | asm(XEN_EMULATE_PREFIX "cpuid" |
| 86 | : "=a" (*eax), |
| 87 | "=b" (*ebx), |
| 88 | "=c" (*ecx), |
| 89 | "=d" (*edx) |
| 90 | : "0" (*eax), "2" (*ecx)); |
| 91 | *edx &= maskedx; |
| 92 | } |
| 93 | |
| 94 | static void xen_set_debugreg(int reg, unsigned long val) |
| 95 | { |
| 96 | HYPERVISOR_set_debugreg(reg, val); |
| 97 | } |
| 98 | |
| 99 | static unsigned long xen_get_debugreg(int reg) |
| 100 | { |
| 101 | return HYPERVISOR_get_debugreg(reg); |
| 102 | } |
| 103 | |
| 104 | static unsigned long xen_save_fl(void) |
| 105 | { |
| 106 | struct vcpu_info *vcpu; |
| 107 | unsigned long flags; |
| 108 | |
| 109 | preempt_disable(); |
| 110 | vcpu = x86_read_percpu(xen_vcpu); |
| 111 | /* flag has opposite sense of mask */ |
| 112 | flags = !vcpu->evtchn_upcall_mask; |
| 113 | preempt_enable(); |
| 114 | |
| 115 | /* convert to IF type flag |
| 116 | -0 -> 0x00000000 |
| 117 | -1 -> 0xffffffff |
| 118 | */ |
| 119 | return (-flags) & X86_EFLAGS_IF; |
| 120 | } |
| 121 | |
| 122 | static void xen_restore_fl(unsigned long flags) |
| 123 | { |
| 124 | struct vcpu_info *vcpu; |
| 125 | |
| 126 | preempt_disable(); |
| 127 | |
| 128 | /* convert from IF type flag */ |
| 129 | flags = !(flags & X86_EFLAGS_IF); |
| 130 | vcpu = x86_read_percpu(xen_vcpu); |
| 131 | vcpu->evtchn_upcall_mask = flags; |
| 132 | |
| 133 | if (flags == 0) { |
| 134 | /* Unmask then check (avoid races). We're only protecting |
| 135 | against updates by this CPU, so there's no need for |
| 136 | anything stronger. */ |
| 137 | barrier(); |
| 138 | |
| 139 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 140 | force_evtchn_callback(); |
| 141 | preempt_enable(); |
| 142 | } else |
| 143 | preempt_enable_no_resched(); |
| 144 | } |
| 145 | |
| 146 | static void xen_irq_disable(void) |
| 147 | { |
| 148 | struct vcpu_info *vcpu; |
| 149 | preempt_disable(); |
| 150 | vcpu = x86_read_percpu(xen_vcpu); |
| 151 | vcpu->evtchn_upcall_mask = 1; |
| 152 | preempt_enable_no_resched(); |
| 153 | } |
| 154 | |
| 155 | static void xen_irq_enable(void) |
| 156 | { |
| 157 | struct vcpu_info *vcpu; |
| 158 | |
| 159 | preempt_disable(); |
| 160 | vcpu = x86_read_percpu(xen_vcpu); |
| 161 | vcpu->evtchn_upcall_mask = 0; |
| 162 | |
| 163 | /* Unmask then check (avoid races). We're only protecting |
| 164 | against updates by this CPU, so there's no need for |
| 165 | anything stronger. */ |
| 166 | barrier(); |
| 167 | |
| 168 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 169 | force_evtchn_callback(); |
| 170 | preempt_enable(); |
| 171 | } |
| 172 | |
| 173 | static void xen_safe_halt(void) |
| 174 | { |
| 175 | /* Blocking includes an implicit local_irq_enable(). */ |
| 176 | if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0) |
| 177 | BUG(); |
| 178 | } |
| 179 | |
| 180 | static void xen_halt(void) |
| 181 | { |
| 182 | if (irqs_disabled()) |
| 183 | HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL); |
| 184 | else |
| 185 | xen_safe_halt(); |
| 186 | } |
| 187 | |
| 188 | static void xen_set_lazy_mode(enum paravirt_lazy_mode mode) |
| 189 | { |
| 190 | switch (mode) { |
| 191 | case PARAVIRT_LAZY_NONE: |
| 192 | BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE); |
| 193 | break; |
| 194 | |
| 195 | case PARAVIRT_LAZY_MMU: |
| 196 | case PARAVIRT_LAZY_CPU: |
| 197 | BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE); |
| 198 | break; |
| 199 | |
| 200 | case PARAVIRT_LAZY_FLUSH: |
| 201 | /* flush if necessary, but don't change state */ |
| 202 | if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE) |
| 203 | xen_mc_flush(); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | xen_mc_flush(); |
| 208 | x86_write_percpu(xen_lazy_mode, mode); |
| 209 | } |
| 210 | |
| 211 | static unsigned long xen_store_tr(void) |
| 212 | { |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void xen_set_ldt(const void *addr, unsigned entries) |
| 217 | { |
| 218 | unsigned long linear_addr = (unsigned long)addr; |
| 219 | struct mmuext_op *op; |
| 220 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
| 221 | |
| 222 | op = mcs.args; |
| 223 | op->cmd = MMUEXT_SET_LDT; |
| 224 | if (linear_addr) { |
| 225 | /* ldt my be vmalloced, use arbitrary_virt_to_machine */ |
| 226 | xmaddr_t maddr; |
| 227 | maddr = arbitrary_virt_to_machine((unsigned long)addr); |
| 228 | linear_addr = (unsigned long)maddr.maddr; |
| 229 | } |
| 230 | op->arg1.linear_addr = linear_addr; |
| 231 | op->arg2.nr_ents = entries; |
| 232 | |
| 233 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 234 | |
| 235 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 236 | } |
| 237 | |
| 238 | static void xen_load_gdt(const struct Xgt_desc_struct *dtr) |
| 239 | { |
| 240 | unsigned long *frames; |
| 241 | unsigned long va = dtr->address; |
| 242 | unsigned int size = dtr->size + 1; |
| 243 | unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 244 | int f; |
| 245 | struct multicall_space mcs; |
| 246 | |
| 247 | /* A GDT can be up to 64k in size, which corresponds to 8192 |
| 248 | 8-byte entries, or 16 4k pages.. */ |
| 249 | |
| 250 | BUG_ON(size > 65536); |
| 251 | BUG_ON(va & ~PAGE_MASK); |
| 252 | |
| 253 | mcs = xen_mc_entry(sizeof(*frames) * pages); |
| 254 | frames = mcs.args; |
| 255 | |
| 256 | for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) { |
| 257 | frames[f] = virt_to_mfn(va); |
| 258 | make_lowmem_page_readonly((void *)va); |
| 259 | } |
| 260 | |
| 261 | MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct)); |
| 262 | |
| 263 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 264 | } |
| 265 | |
| 266 | static void load_TLS_descriptor(struct thread_struct *t, |
| 267 | unsigned int cpu, unsigned int i) |
| 268 | { |
| 269 | struct desc_struct *gdt = get_cpu_gdt_table(cpu); |
| 270 | xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]); |
| 271 | struct multicall_space mc = __xen_mc_entry(0); |
| 272 | |
| 273 | MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]); |
| 274 | } |
| 275 | |
| 276 | static void xen_load_tls(struct thread_struct *t, unsigned int cpu) |
| 277 | { |
| 278 | xen_mc_batch(); |
| 279 | |
| 280 | load_TLS_descriptor(t, cpu, 0); |
| 281 | load_TLS_descriptor(t, cpu, 1); |
| 282 | load_TLS_descriptor(t, cpu, 2); |
| 283 | |
| 284 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 285 | } |
| 286 | |
| 287 | static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum, |
| 288 | u32 low, u32 high) |
| 289 | { |
| 290 | unsigned long lp = (unsigned long)&dt[entrynum]; |
| 291 | xmaddr_t mach_lp = virt_to_machine(lp); |
| 292 | u64 entry = (u64)high << 32 | low; |
| 293 | |
| 294 | xen_mc_flush(); |
| 295 | if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry)) |
| 296 | BUG(); |
| 297 | } |
| 298 | |
| 299 | static int cvt_gate_to_trap(int vector, u32 low, u32 high, |
| 300 | struct trap_info *info) |
| 301 | { |
| 302 | u8 type, dpl; |
| 303 | |
| 304 | type = (high >> 8) & 0x1f; |
| 305 | dpl = (high >> 13) & 3; |
| 306 | |
| 307 | if (type != 0xf && type != 0xe) |
| 308 | return 0; |
| 309 | |
| 310 | info->vector = vector; |
| 311 | info->address = (high & 0xffff0000) | (low & 0x0000ffff); |
| 312 | info->cs = low >> 16; |
| 313 | info->flags = dpl; |
| 314 | /* interrupt gates clear IF */ |
| 315 | if (type == 0xe) |
| 316 | info->flags |= 4; |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /* Locations of each CPU's IDT */ |
| 322 | static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc); |
| 323 | |
| 324 | /* Set an IDT entry. If the entry is part of the current IDT, then |
| 325 | also update Xen. */ |
| 326 | static void xen_write_idt_entry(struct desc_struct *dt, int entrynum, |
| 327 | u32 low, u32 high) |
| 328 | { |
| 329 | |
| 330 | int cpu = smp_processor_id(); |
| 331 | unsigned long p = (unsigned long)&dt[entrynum]; |
| 332 | unsigned long start = per_cpu(idt_desc, cpu).address; |
| 333 | unsigned long end = start + per_cpu(idt_desc, cpu).size + 1; |
| 334 | |
| 335 | xen_mc_flush(); |
| 336 | |
| 337 | write_dt_entry(dt, entrynum, low, high); |
| 338 | |
| 339 | if (p >= start && (p + 8) <= end) { |
| 340 | struct trap_info info[2]; |
| 341 | |
| 342 | info[1].address = 0; |
| 343 | |
| 344 | if (cvt_gate_to_trap(entrynum, low, high, &info[0])) |
| 345 | if (HYPERVISOR_set_trap_table(info)) |
| 346 | BUG(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* Load a new IDT into Xen. In principle this can be per-CPU, so we |
| 351 | hold a spinlock to protect the static traps[] array (static because |
| 352 | it avoids allocation, and saves stack space). */ |
| 353 | static void xen_load_idt(const struct Xgt_desc_struct *desc) |
| 354 | { |
| 355 | static DEFINE_SPINLOCK(lock); |
| 356 | static struct trap_info traps[257]; |
| 357 | |
| 358 | int cpu = smp_processor_id(); |
| 359 | unsigned in, out, count; |
| 360 | |
| 361 | per_cpu(idt_desc, cpu) = *desc; |
| 362 | |
| 363 | count = (desc->size+1) / 8; |
| 364 | BUG_ON(count > 256); |
| 365 | |
| 366 | spin_lock(&lock); |
| 367 | for (in = out = 0; in < count; in++) { |
| 368 | const u32 *entry = (u32 *)(desc->address + in * 8); |
| 369 | |
| 370 | if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out])) |
| 371 | out++; |
| 372 | } |
| 373 | traps[out].address = 0; |
| 374 | |
| 375 | xen_mc_flush(); |
| 376 | if (HYPERVISOR_set_trap_table(traps)) |
| 377 | BUG(); |
| 378 | |
| 379 | spin_unlock(&lock); |
| 380 | } |
| 381 | |
| 382 | /* Write a GDT descriptor entry. Ignore LDT descriptors, since |
| 383 | they're handled differently. */ |
| 384 | static void xen_write_gdt_entry(struct desc_struct *dt, int entry, |
| 385 | u32 low, u32 high) |
| 386 | { |
| 387 | switch ((high >> 8) & 0xff) { |
| 388 | case DESCTYPE_LDT: |
| 389 | case DESCTYPE_TSS: |
| 390 | /* ignore */ |
| 391 | break; |
| 392 | |
| 393 | default: { |
| 394 | xmaddr_t maddr = virt_to_machine(&dt[entry]); |
| 395 | u64 desc = (u64)high << 32 | low; |
| 396 | |
| 397 | xen_mc_flush(); |
| 398 | if (HYPERVISOR_update_descriptor(maddr.maddr, desc)) |
| 399 | BUG(); |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | static void xen_load_esp0(struct tss_struct *tss, |
| 406 | struct thread_struct *thread) |
| 407 | { |
| 408 | struct multicall_space mcs = xen_mc_entry(0); |
| 409 | MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0); |
| 410 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 411 | } |
| 412 | |
| 413 | static void xen_set_iopl_mask(unsigned mask) |
| 414 | { |
| 415 | struct physdev_set_iopl set_iopl; |
| 416 | |
| 417 | /* Force the change at ring 0. */ |
| 418 | set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3; |
| 419 | HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl); |
| 420 | } |
| 421 | |
| 422 | static void xen_io_delay(void) |
| 423 | { |
| 424 | } |
| 425 | |
| 426 | #ifdef CONFIG_X86_LOCAL_APIC |
| 427 | static unsigned long xen_apic_read(unsigned long reg) |
| 428 | { |
| 429 | return 0; |
| 430 | } |
| 431 | #endif |
| 432 | |
| 433 | static void xen_flush_tlb(void) |
| 434 | { |
| 435 | struct mmuext_op op; |
| 436 | |
| 437 | op.cmd = MMUEXT_TLB_FLUSH_LOCAL; |
| 438 | if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) |
| 439 | BUG(); |
| 440 | } |
| 441 | |
| 442 | static void xen_flush_tlb_single(unsigned long addr) |
| 443 | { |
| 444 | struct mmuext_op op; |
| 445 | |
| 446 | op.cmd = MMUEXT_INVLPG_LOCAL; |
| 447 | op.arg1.linear_addr = addr & PAGE_MASK; |
| 448 | if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) |
| 449 | BUG(); |
| 450 | } |
| 451 | |
| 452 | static unsigned long xen_read_cr2(void) |
| 453 | { |
| 454 | return x86_read_percpu(xen_vcpu)->arch.cr2; |
| 455 | } |
| 456 | |
| 457 | static void xen_write_cr4(unsigned long cr4) |
| 458 | { |
| 459 | /* never allow TSC to be disabled */ |
| 460 | native_write_cr4(cr4 & ~X86_CR4_TSD); |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Page-directory addresses above 4GB do not fit into architectural %cr3. |
| 465 | * When accessing %cr3, or equivalent field in vcpu_guest_context, guests |
| 466 | * must use the following accessor macros to pack/unpack valid MFNs. |
| 467 | * |
| 468 | * Note that Xen is using the fact that the pagetable base is always |
| 469 | * page-aligned, and putting the 12 MSB of the address into the 12 LSB |
| 470 | * of cr3. |
| 471 | */ |
| 472 | #define xen_pfn_to_cr3(pfn) (((unsigned)(pfn) << 12) | ((unsigned)(pfn) >> 20)) |
| 473 | #define xen_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 20)) |
| 474 | |
| 475 | static unsigned long xen_read_cr3(void) |
| 476 | { |
| 477 | return x86_read_percpu(xen_cr3); |
| 478 | } |
| 479 | |
| 480 | static void xen_write_cr3(unsigned long cr3) |
| 481 | { |
| 482 | if (cr3 == x86_read_percpu(xen_cr3)) { |
| 483 | /* just a simple tlb flush */ |
| 484 | xen_flush_tlb(); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | x86_write_percpu(xen_cr3, cr3); |
| 489 | |
| 490 | |
| 491 | { |
| 492 | struct mmuext_op *op; |
| 493 | struct multicall_space mcs = xen_mc_entry(sizeof(*op)); |
| 494 | unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3)); |
| 495 | |
| 496 | op = mcs.args; |
| 497 | op->cmd = MMUEXT_NEW_BASEPTR; |
| 498 | op->arg1.mfn = mfn; |
| 499 | |
| 500 | MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF); |
| 501 | |
| 502 | xen_mc_issue(PARAVIRT_LAZY_CPU); |
| 503 | } |
| 504 | } |
| 505 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 506 | /* Early in boot, while setting up the initial pagetable, assume |
| 507 | everything is pinned. */ |
| 508 | static void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn) |
| 509 | { |
| 510 | BUG_ON(mem_map); /* should only be used early */ |
| 511 | make_lowmem_page_readonly(__va(PFN_PHYS(pfn))); |
| 512 | } |
| 513 | |
| 514 | /* This needs to make sure the new pte page is pinned iff its being |
| 515 | attached to a pinned pagetable. */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 516 | static void xen_alloc_pt(struct mm_struct *mm, u32 pfn) |
| 517 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 518 | struct page *page = pfn_to_page(pfn); |
| 519 | |
| 520 | if (PagePinned(virt_to_page(mm->pgd))) { |
| 521 | SetPagePinned(page); |
| 522 | |
| 523 | if (!PageHighMem(page)) |
| 524 | make_lowmem_page_readonly(__va(PFN_PHYS(pfn))); |
| 525 | else |
| 526 | /* make sure there are no stray mappings of |
| 527 | this page */ |
| 528 | kmap_flush_unused(); |
| 529 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 532 | /* This should never happen until we're OK to use struct page */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 533 | static void xen_release_pt(u32 pfn) |
| 534 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 535 | struct page *page = pfn_to_page(pfn); |
| 536 | |
| 537 | if (PagePinned(page)) { |
| 538 | if (!PageHighMem(page)) |
| 539 | make_lowmem_page_readwrite(__va(PFN_PHYS(pfn))); |
| 540 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 543 | #ifdef CONFIG_HIGHPTE |
| 544 | 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] | 545 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 546 | pgprot_t prot = PAGE_KERNEL; |
| 547 | |
| 548 | if (PagePinned(page)) |
| 549 | prot = PAGE_KERNEL_RO; |
| 550 | |
| 551 | if (0 && PageHighMem(page)) |
| 552 | printk("mapping highpte %lx type %d prot %s\n", |
| 553 | page_to_pfn(page), type, |
| 554 | (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ"); |
| 555 | |
| 556 | return kmap_atomic_prot(page, type, prot); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 557 | } |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 558 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 559 | |
| 560 | static __init void xen_pagetable_setup_start(pgd_t *base) |
| 561 | { |
| 562 | pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base; |
| 563 | |
| 564 | init_mm.pgd = base; |
| 565 | /* |
| 566 | * copy top-level of Xen-supplied pagetable into place. For |
| 567 | * !PAE we can use this as-is, but for PAE it is a stand-in |
| 568 | * while we copy the pmd pages. |
| 569 | */ |
| 570 | memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t)); |
| 571 | |
| 572 | if (PTRS_PER_PMD > 1) { |
| 573 | int i; |
| 574 | /* |
| 575 | * For PAE, need to allocate new pmds, rather than |
| 576 | * share Xen's, since Xen doesn't like pmd's being |
| 577 | * shared between address spaces. |
| 578 | */ |
| 579 | for (i = 0; i < PTRS_PER_PGD; i++) { |
| 580 | if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) { |
| 581 | pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE); |
| 582 | |
| 583 | memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]), |
| 584 | PAGE_SIZE); |
| 585 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 586 | make_lowmem_page_readonly(pmd); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 587 | |
| 588 | set_pgd(&base[i], __pgd(1 + __pa(pmd))); |
| 589 | } else |
| 590 | pgd_clear(&base[i]); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* make sure zero_page is mapped RO so we can use it in pagetables */ |
| 595 | make_lowmem_page_readonly(empty_zero_page); |
| 596 | make_lowmem_page_readonly(base); |
| 597 | /* |
| 598 | * Switch to new pagetable. This is done before |
| 599 | * pagetable_init has done anything so that the new pages |
| 600 | * added to the table can be prepared properly for Xen. |
| 601 | */ |
| 602 | xen_write_cr3(__pa(base)); |
| 603 | } |
| 604 | |
| 605 | static __init void xen_pagetable_setup_done(pgd_t *base) |
| 606 | { |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 607 | /* This will work as long as patching hasn't happened yet |
| 608 | (which it hasn't) */ |
| 609 | paravirt_ops.alloc_pt = xen_alloc_pt; |
| 610 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 611 | if (!xen_feature(XENFEAT_auto_translated_physmap)) { |
| 612 | /* |
| 613 | * Create a mapping for the shared info page. |
| 614 | * Should be set_fixmap(), but shared_info is a machine |
| 615 | * address with no corresponding pseudo-phys address. |
| 616 | */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 617 | set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP), |
| 618 | PFN_DOWN(xen_start_info->shared_info), |
| 619 | PAGE_KERNEL); |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 620 | |
| 621 | HYPERVISOR_shared_info = |
| 622 | (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP); |
| 623 | |
| 624 | } else |
| 625 | HYPERVISOR_shared_info = |
| 626 | (struct shared_info *)__va(xen_start_info->shared_info); |
| 627 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 628 | /* Actually pin the pagetable down, but we can't set PG_pinned |
| 629 | yet because the page structures don't exist yet. */ |
| 630 | { |
| 631 | struct mmuext_op op; |
| 632 | #ifdef CONFIG_X86_PAE |
| 633 | op.cmd = MMUEXT_PIN_L3_TABLE; |
| 634 | #else |
| 635 | op.cmd = MMUEXT_PIN_L3_TABLE; |
| 636 | #endif |
| 637 | op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base))); |
| 638 | if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) |
| 639 | BUG(); |
| 640 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 641 | |
| 642 | xen_vcpu_setup(smp_processor_id()); |
| 643 | } |
| 644 | |
| 645 | static const struct paravirt_ops xen_paravirt_ops __initdata = { |
| 646 | .paravirt_enabled = 1, |
| 647 | .shared_kernel_pmd = 0, |
| 648 | |
| 649 | .name = "Xen", |
| 650 | .banner = xen_banner, |
| 651 | |
| 652 | .patch = paravirt_patch_default, |
| 653 | |
| 654 | .memory_setup = xen_memory_setup, |
| 655 | .arch_setup = xen_arch_setup, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 656 | .init_IRQ = xen_init_IRQ, |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 657 | .post_allocator_init = xen_mark_init_mm_pinned, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 658 | |
Jeremy Fitzhardinge | 15c8473 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 659 | .time_init = xen_time_init, |
| 660 | .set_wallclock = xen_set_wallclock, |
| 661 | .get_wallclock = xen_get_wallclock, |
| 662 | .get_cpu_khz = xen_cpu_khz, |
| 663 | .sched_clock = xen_clocksource_read, |
| 664 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 665 | .cpuid = xen_cpuid, |
| 666 | |
| 667 | .set_debugreg = xen_set_debugreg, |
| 668 | .get_debugreg = xen_get_debugreg, |
| 669 | |
| 670 | .clts = native_clts, |
| 671 | |
| 672 | .read_cr0 = native_read_cr0, |
| 673 | .write_cr0 = native_write_cr0, |
| 674 | |
| 675 | .read_cr2 = xen_read_cr2, |
| 676 | .write_cr2 = native_write_cr2, |
| 677 | |
| 678 | .read_cr3 = xen_read_cr3, |
| 679 | .write_cr3 = xen_write_cr3, |
| 680 | |
| 681 | .read_cr4 = native_read_cr4, |
| 682 | .read_cr4_safe = native_read_cr4_safe, |
| 683 | .write_cr4 = xen_write_cr4, |
| 684 | |
| 685 | .save_fl = xen_save_fl, |
| 686 | .restore_fl = xen_restore_fl, |
| 687 | .irq_disable = xen_irq_disable, |
| 688 | .irq_enable = xen_irq_enable, |
| 689 | .safe_halt = xen_safe_halt, |
| 690 | .halt = xen_halt, |
| 691 | .wbinvd = native_wbinvd, |
| 692 | |
| 693 | .read_msr = native_read_msr_safe, |
| 694 | .write_msr = native_write_msr_safe, |
| 695 | .read_tsc = native_read_tsc, |
| 696 | .read_pmc = native_read_pmc, |
| 697 | |
| 698 | .iret = (void *)&hypercall_page[__HYPERVISOR_iret], |
| 699 | .irq_enable_sysexit = NULL, /* never called */ |
| 700 | |
| 701 | .load_tr_desc = paravirt_nop, |
| 702 | .set_ldt = xen_set_ldt, |
| 703 | .load_gdt = xen_load_gdt, |
| 704 | .load_idt = xen_load_idt, |
| 705 | .load_tls = xen_load_tls, |
| 706 | |
| 707 | .store_gdt = native_store_gdt, |
| 708 | .store_idt = native_store_idt, |
| 709 | .store_tr = xen_store_tr, |
| 710 | |
| 711 | .write_ldt_entry = xen_write_ldt_entry, |
| 712 | .write_gdt_entry = xen_write_gdt_entry, |
| 713 | .write_idt_entry = xen_write_idt_entry, |
| 714 | .load_esp0 = xen_load_esp0, |
| 715 | |
| 716 | .set_iopl_mask = xen_set_iopl_mask, |
| 717 | .io_delay = xen_io_delay, |
| 718 | |
| 719 | #ifdef CONFIG_X86_LOCAL_APIC |
| 720 | .apic_write = paravirt_nop, |
| 721 | .apic_write_atomic = paravirt_nop, |
| 722 | .apic_read = xen_apic_read, |
| 723 | .setup_boot_clock = paravirt_nop, |
| 724 | .setup_secondary_clock = paravirt_nop, |
| 725 | .startup_ipi_hook = paravirt_nop, |
| 726 | #endif |
| 727 | |
| 728 | .flush_tlb_user = xen_flush_tlb, |
| 729 | .flush_tlb_kernel = xen_flush_tlb, |
| 730 | .flush_tlb_single = xen_flush_tlb_single, |
| 731 | |
| 732 | .pte_update = paravirt_nop, |
| 733 | .pte_update_defer = paravirt_nop, |
| 734 | |
| 735 | .pagetable_setup_start = xen_pagetable_setup_start, |
| 736 | .pagetable_setup_done = xen_pagetable_setup_done, |
| 737 | |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 738 | .alloc_pt = xen_alloc_pt_init, |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 739 | .release_pt = xen_release_pt, |
Jeremy Fitzhardinge | f4f97b3 | 2007-07-17 18:37:05 -0700 | [diff] [blame^] | 740 | .alloc_pd = paravirt_nop, |
| 741 | .alloc_pd_clone = paravirt_nop, |
| 742 | .release_pd = paravirt_nop, |
| 743 | |
| 744 | #ifdef CONFIG_HIGHPTE |
| 745 | .kmap_atomic_pte = xen_kmap_atomic_pte, |
| 746 | #endif |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 747 | |
Jeremy Fitzhardinge | 3b827c1 | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 748 | .set_pte = xen_set_pte, |
| 749 | .set_pte_at = xen_set_pte_at, |
| 750 | .set_pmd = xen_set_pmd, |
| 751 | |
| 752 | .pte_val = xen_pte_val, |
| 753 | .pgd_val = xen_pgd_val, |
| 754 | |
| 755 | .make_pte = xen_make_pte, |
| 756 | .make_pgd = xen_make_pgd, |
| 757 | |
| 758 | #ifdef CONFIG_X86_PAE |
| 759 | .set_pte_atomic = xen_set_pte_atomic, |
| 760 | .set_pte_present = xen_set_pte_at, |
| 761 | .set_pud = xen_set_pud, |
| 762 | .pte_clear = xen_pte_clear, |
| 763 | .pmd_clear = xen_pmd_clear, |
| 764 | |
| 765 | .make_pmd = xen_make_pmd, |
| 766 | .pmd_val = xen_pmd_val, |
| 767 | #endif /* PAE */ |
| 768 | |
| 769 | .activate_mm = xen_activate_mm, |
| 770 | .dup_mmap = xen_dup_mmap, |
| 771 | .exit_mmap = xen_exit_mmap, |
| 772 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 773 | .set_lazy_mode = xen_set_lazy_mode, |
| 774 | }; |
| 775 | |
| 776 | /* First C function to be called on Xen boot */ |
| 777 | asmlinkage void __init xen_start_kernel(void) |
| 778 | { |
| 779 | pgd_t *pgd; |
| 780 | |
| 781 | if (!xen_start_info) |
| 782 | return; |
| 783 | |
| 784 | BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0); |
| 785 | |
| 786 | /* Install Xen paravirt ops */ |
| 787 | paravirt_ops = xen_paravirt_ops; |
| 788 | |
| 789 | xen_setup_features(); |
| 790 | |
| 791 | /* Get mfn list */ |
| 792 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 793 | phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list; |
| 794 | |
| 795 | pgd = (pgd_t *)xen_start_info->pt_base; |
| 796 | |
| 797 | init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE; |
| 798 | |
| 799 | init_mm.pgd = pgd; /* use the Xen pagetables to start */ |
| 800 | |
| 801 | /* keep using Xen gdt for now; no urgent need to change it */ |
| 802 | |
| 803 | x86_write_percpu(xen_cr3, __pa(pgd)); |
| 804 | xen_vcpu_setup(0); |
| 805 | |
| 806 | paravirt_ops.kernel_rpl = 1; |
| 807 | if (xen_feature(XENFEAT_supervisor_mode_kernel)) |
| 808 | paravirt_ops.kernel_rpl = 0; |
| 809 | |
| 810 | /* set the limit of our address space */ |
| 811 | reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE); |
| 812 | |
| 813 | /* set up basic CPUID stuff */ |
| 814 | cpu_detect(&new_cpu_data); |
| 815 | new_cpu_data.hard_math = 1; |
| 816 | new_cpu_data.x86_capability[0] = cpuid_edx(1); |
| 817 | |
| 818 | /* Poke various useful things into boot_params */ |
| 819 | LOADER_TYPE = (9 << 4) | 0; |
| 820 | INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0; |
| 821 | INITRD_SIZE = xen_start_info->mod_len; |
| 822 | |
| 823 | /* Start the world */ |
| 824 | start_kernel(); |
| 825 | } |