enlighten.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/smp.h>
  16. #include <linux/preempt.h>
  17. #include <linux/hardirq.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>
  24. #include <linux/mm.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/highmem.h>
  27. #include <linux/console.h>
  28. #include <xen/interface/xen.h>
  29. #include <xen/interface/version.h>
  30. #include <xen/interface/physdev.h>
  31. #include <xen/interface/vcpu.h>
  32. #include <xen/features.h>
  33. #include <xen/page.h>
  34. #include <xen/hvc-console.h>
  35. #include <asm/paravirt.h>
  36. #include <asm/apic.h>
  37. #include <asm/page.h>
  38. #include <asm/xen/hypercall.h>
  39. #include <asm/xen/hypervisor.h>
  40. #include <asm/fixmap.h>
  41. #include <asm/processor.h>
  42. #include <asm/msr-index.h>
  43. #include <asm/setup.h>
  44. #include <asm/desc.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/reboot.h>
  48. #include "xen-ops.h"
  49. #include "mmu.h"
  50. #include "multicalls.h"
  51. EXPORT_SYMBOL_GPL(hypercall_page);
  52. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  53. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  54. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  55. EXPORT_SYMBOL_GPL(xen_domain_type);
  56. struct start_info *xen_start_info;
  57. EXPORT_SYMBOL_GPL(xen_start_info);
  58. struct shared_info xen_dummy_shared_info;
  59. void *xen_initial_gdt;
  60. /*
  61. * Point at some empty memory to start with. We map the real shared_info
  62. * page as soon as fixmap is up and running.
  63. */
  64. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  65. /*
  66. * Flag to determine whether vcpu info placement is available on all
  67. * VCPUs. We assume it is to start with, and then set it to zero on
  68. * the first failure. This is because it can succeed on some VCPUs
  69. * and not others, since it can involve hypervisor memory allocation,
  70. * or because the guest failed to guarantee all the appropriate
  71. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  72. *
  73. * Note that any particular CPU may be using a placed vcpu structure,
  74. * but we can only optimise if the all are.
  75. *
  76. * 0: not available, 1: available
  77. */
  78. static int have_vcpu_info_placement =
  79. #ifdef CONFIG_X86_32
  80. 1
  81. #else
  82. 0
  83. #endif
  84. ;
  85. static void xen_vcpu_setup(int cpu)
  86. {
  87. struct vcpu_register_vcpu_info info;
  88. int err;
  89. struct vcpu_info *vcpup;
  90. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  91. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  92. if (!have_vcpu_info_placement)
  93. return; /* already tested, not available */
  94. vcpup = &per_cpu(xen_vcpu_info, cpu);
  95. info.mfn = virt_to_mfn(vcpup);
  96. info.offset = offset_in_page(vcpup);
  97. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  98. cpu, vcpup, info.mfn, info.offset);
  99. /* Check to see if the hypervisor will put the vcpu_info
  100. structure where we want it, which allows direct access via
  101. a percpu-variable. */
  102. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  103. if (err) {
  104. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  105. have_vcpu_info_placement = 0;
  106. } else {
  107. /* This cpu is using the registered vcpu info, even if
  108. later ones fail to. */
  109. per_cpu(xen_vcpu, cpu) = vcpup;
  110. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  111. cpu, vcpup);
  112. }
  113. }
  114. /*
  115. * On restore, set the vcpu placement up again.
  116. * If it fails, then we're in a bad state, since
  117. * we can't back out from using it...
  118. */
  119. void xen_vcpu_restore(void)
  120. {
  121. if (have_vcpu_info_placement) {
  122. int cpu;
  123. for_each_online_cpu(cpu) {
  124. bool other_cpu = (cpu != smp_processor_id());
  125. if (other_cpu &&
  126. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  127. BUG();
  128. xen_vcpu_setup(cpu);
  129. if (other_cpu &&
  130. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  131. BUG();
  132. }
  133. BUG_ON(!have_vcpu_info_placement);
  134. }
  135. }
  136. static void __init xen_banner(void)
  137. {
  138. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  139. struct xen_extraversion extra;
  140. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  141. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  142. pv_info.name);
  143. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  144. version >> 16, version & 0xffff, extra.extraversion,
  145. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  146. }
  147. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  148. unsigned int *cx, unsigned int *dx)
  149. {
  150. unsigned maskedx = ~0;
  151. /*
  152. * Mask out inconvenient features, to try and disable as many
  153. * unsupported kernel subsystems as possible.
  154. */
  155. if (*ax == 1)
  156. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  157. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  158. (1 << X86_FEATURE_MCE) | /* disable MCE */
  159. (1 << X86_FEATURE_MCA) | /* disable MCA */
  160. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  161. asm(XEN_EMULATE_PREFIX "cpuid"
  162. : "=a" (*ax),
  163. "=b" (*bx),
  164. "=c" (*cx),
  165. "=d" (*dx)
  166. : "0" (*ax), "2" (*cx));
  167. *dx &= maskedx;
  168. }
  169. static void xen_set_debugreg(int reg, unsigned long val)
  170. {
  171. HYPERVISOR_set_debugreg(reg, val);
  172. }
  173. static unsigned long xen_get_debugreg(int reg)
  174. {
  175. return HYPERVISOR_get_debugreg(reg);
  176. }
  177. void xen_leave_lazy(void)
  178. {
  179. paravirt_leave_lazy(paravirt_get_lazy_mode());
  180. xen_mc_flush();
  181. }
  182. static unsigned long xen_store_tr(void)
  183. {
  184. return 0;
  185. }
  186. /*
  187. * Set the page permissions for a particular virtual address. If the
  188. * address is a vmalloc mapping (or other non-linear mapping), then
  189. * find the linear mapping of the page and also set its protections to
  190. * match.
  191. */
  192. static void set_aliased_prot(void *v, pgprot_t prot)
  193. {
  194. int level;
  195. pte_t *ptep;
  196. pte_t pte;
  197. unsigned long pfn;
  198. struct page *page;
  199. ptep = lookup_address((unsigned long)v, &level);
  200. BUG_ON(ptep == NULL);
  201. pfn = pte_pfn(*ptep);
  202. page = pfn_to_page(pfn);
  203. pte = pfn_pte(pfn, prot);
  204. if (HYPERVISOR_update_va_mapping((unsigned long)v, pte, 0))
  205. BUG();
  206. if (!PageHighMem(page)) {
  207. void *av = __va(PFN_PHYS(pfn));
  208. if (av != v)
  209. if (HYPERVISOR_update_va_mapping((unsigned long)av, pte, 0))
  210. BUG();
  211. } else
  212. kmap_flush_unused();
  213. }
  214. static void xen_alloc_ldt(struct desc_struct *ldt, unsigned entries)
  215. {
  216. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  217. int i;
  218. for(i = 0; i < entries; i += entries_per_page)
  219. set_aliased_prot(ldt + i, PAGE_KERNEL_RO);
  220. }
  221. static void xen_free_ldt(struct desc_struct *ldt, unsigned entries)
  222. {
  223. const unsigned entries_per_page = PAGE_SIZE / LDT_ENTRY_SIZE;
  224. int i;
  225. for(i = 0; i < entries; i += entries_per_page)
  226. set_aliased_prot(ldt + i, PAGE_KERNEL);
  227. }
  228. static void xen_set_ldt(const void *addr, unsigned entries)
  229. {
  230. struct mmuext_op *op;
  231. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  232. op = mcs.args;
  233. op->cmd = MMUEXT_SET_LDT;
  234. op->arg1.linear_addr = (unsigned long)addr;
  235. op->arg2.nr_ents = entries;
  236. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  237. xen_mc_issue(PARAVIRT_LAZY_CPU);
  238. }
  239. static void xen_load_gdt(const struct desc_ptr *dtr)
  240. {
  241. unsigned long *frames;
  242. unsigned long va = dtr->address;
  243. unsigned int size = dtr->size + 1;
  244. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  245. int f;
  246. struct multicall_space mcs;
  247. /* A GDT can be up to 64k in size, which corresponds to 8192
  248. 8-byte entries, or 16 4k pages.. */
  249. BUG_ON(size > 65536);
  250. BUG_ON(va & ~PAGE_MASK);
  251. mcs = xen_mc_entry(sizeof(*frames) * pages);
  252. frames = mcs.args;
  253. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  254. frames[f] = virt_to_mfn(va);
  255. make_lowmem_page_readonly((void *)va);
  256. }
  257. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  258. xen_mc_issue(PARAVIRT_LAZY_CPU);
  259. }
  260. static void load_TLS_descriptor(struct thread_struct *t,
  261. unsigned int cpu, unsigned int i)
  262. {
  263. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  264. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  265. struct multicall_space mc = __xen_mc_entry(0);
  266. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  267. }
  268. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  269. {
  270. /*
  271. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  272. * it means we're in a context switch, and %gs has just been
  273. * saved. This means we can zero it out to prevent faults on
  274. * exit from the hypervisor if the next process has no %gs.
  275. * Either way, it has been saved, and the new value will get
  276. * loaded properly. This will go away as soon as Xen has been
  277. * modified to not save/restore %gs for normal hypercalls.
  278. *
  279. * On x86_64, this hack is not used for %gs, because gs points
  280. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  281. * must not zero %gs on x86_64
  282. *
  283. * For x86_64, we need to zero %fs, otherwise we may get an
  284. * exception between the new %fs descriptor being loaded and
  285. * %fs being effectively cleared at __switch_to().
  286. */
  287. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  288. #ifdef CONFIG_X86_32
  289. loadsegment(gs, 0);
  290. #else
  291. loadsegment(fs, 0);
  292. #endif
  293. }
  294. xen_mc_batch();
  295. load_TLS_descriptor(t, cpu, 0);
  296. load_TLS_descriptor(t, cpu, 1);
  297. load_TLS_descriptor(t, cpu, 2);
  298. xen_mc_issue(PARAVIRT_LAZY_CPU);
  299. }
  300. #ifdef CONFIG_X86_64
  301. static void xen_load_gs_index(unsigned int idx)
  302. {
  303. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  304. BUG();
  305. }
  306. #endif
  307. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  308. const void *ptr)
  309. {
  310. xmaddr_t mach_lp = arbitrary_virt_to_machine(&dt[entrynum]);
  311. u64 entry = *(u64 *)ptr;
  312. preempt_disable();
  313. xen_mc_flush();
  314. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  315. BUG();
  316. preempt_enable();
  317. }
  318. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  319. struct trap_info *info)
  320. {
  321. if (val->type != 0xf && val->type != 0xe)
  322. return 0;
  323. info->vector = vector;
  324. info->address = gate_offset(*val);
  325. info->cs = gate_segment(*val);
  326. info->flags = val->dpl;
  327. /* interrupt gates clear IF */
  328. if (val->type == 0xe)
  329. info->flags |= 4;
  330. return 1;
  331. }
  332. /* Locations of each CPU's IDT */
  333. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  334. /* Set an IDT entry. If the entry is part of the current IDT, then
  335. also update Xen. */
  336. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  337. {
  338. unsigned long p = (unsigned long)&dt[entrynum];
  339. unsigned long start, end;
  340. preempt_disable();
  341. start = __get_cpu_var(idt_desc).address;
  342. end = start + __get_cpu_var(idt_desc).size + 1;
  343. xen_mc_flush();
  344. native_write_idt_entry(dt, entrynum, g);
  345. if (p >= start && (p + 8) <= end) {
  346. struct trap_info info[2];
  347. info[1].address = 0;
  348. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  349. if (HYPERVISOR_set_trap_table(info))
  350. BUG();
  351. }
  352. preempt_enable();
  353. }
  354. static void xen_convert_trap_info(const struct desc_ptr *desc,
  355. struct trap_info *traps)
  356. {
  357. unsigned in, out, count;
  358. count = (desc->size+1) / sizeof(gate_desc);
  359. BUG_ON(count > 256);
  360. for (in = out = 0; in < count; in++) {
  361. gate_desc *entry = (gate_desc*)(desc->address) + in;
  362. if (cvt_gate_to_trap(in, entry, &traps[out]))
  363. out++;
  364. }
  365. traps[out].address = 0;
  366. }
  367. void xen_copy_trap_info(struct trap_info *traps)
  368. {
  369. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  370. xen_convert_trap_info(desc, traps);
  371. }
  372. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  373. hold a spinlock to protect the static traps[] array (static because
  374. it avoids allocation, and saves stack space). */
  375. static void xen_load_idt(const struct desc_ptr *desc)
  376. {
  377. static DEFINE_SPINLOCK(lock);
  378. static struct trap_info traps[257];
  379. spin_lock(&lock);
  380. __get_cpu_var(idt_desc) = *desc;
  381. xen_convert_trap_info(desc, traps);
  382. xen_mc_flush();
  383. if (HYPERVISOR_set_trap_table(traps))
  384. BUG();
  385. spin_unlock(&lock);
  386. }
  387. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  388. they're handled differently. */
  389. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  390. const void *desc, int type)
  391. {
  392. preempt_disable();
  393. switch (type) {
  394. case DESC_LDT:
  395. case DESC_TSS:
  396. /* ignore */
  397. break;
  398. default: {
  399. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  400. xen_mc_flush();
  401. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  402. BUG();
  403. }
  404. }
  405. preempt_enable();
  406. }
  407. static void xen_load_sp0(struct tss_struct *tss,
  408. struct thread_struct *thread)
  409. {
  410. struct multicall_space mcs = xen_mc_entry(0);
  411. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  412. xen_mc_issue(PARAVIRT_LAZY_CPU);
  413. }
  414. static void xen_set_iopl_mask(unsigned mask)
  415. {
  416. struct physdev_set_iopl set_iopl;
  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. static void xen_io_delay(void)
  422. {
  423. }
  424. #ifdef CONFIG_X86_LOCAL_APIC
  425. static u32 xen_apic_read(u32 reg)
  426. {
  427. return 0;
  428. }
  429. static void xen_apic_write(u32 reg, u32 val)
  430. {
  431. /* Warn to see if there's any stray references */
  432. WARN_ON(1);
  433. }
  434. static u64 xen_apic_icr_read(void)
  435. {
  436. return 0;
  437. }
  438. static void xen_apic_icr_write(u32 low, u32 id)
  439. {
  440. /* Warn to see if there's any stray references */
  441. WARN_ON(1);
  442. }
  443. static void xen_apic_wait_icr_idle(void)
  444. {
  445. return;
  446. }
  447. static u32 xen_safe_apic_wait_icr_idle(void)
  448. {
  449. return 0;
  450. }
  451. static struct apic_ops xen_basic_apic_ops = {
  452. .read = xen_apic_read,
  453. .write = xen_apic_write,
  454. .icr_read = xen_apic_icr_read,
  455. .icr_write = xen_apic_icr_write,
  456. .wait_icr_idle = xen_apic_wait_icr_idle,
  457. .safe_wait_icr_idle = xen_safe_apic_wait_icr_idle,
  458. };
  459. #endif
  460. static void xen_clts(void)
  461. {
  462. struct multicall_space mcs;
  463. mcs = xen_mc_entry(0);
  464. MULTI_fpu_taskswitch(mcs.mc, 0);
  465. xen_mc_issue(PARAVIRT_LAZY_CPU);
  466. }
  467. static void xen_write_cr0(unsigned long cr0)
  468. {
  469. struct multicall_space mcs;
  470. /* Only pay attention to cr0.TS; everything else is
  471. ignored. */
  472. mcs = xen_mc_entry(0);
  473. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  474. xen_mc_issue(PARAVIRT_LAZY_CPU);
  475. }
  476. static void xen_write_cr4(unsigned long cr4)
  477. {
  478. cr4 &= ~X86_CR4_PGE;
  479. cr4 &= ~X86_CR4_PSE;
  480. native_write_cr4(cr4);
  481. }
  482. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  483. {
  484. int ret;
  485. ret = 0;
  486. switch (msr) {
  487. #ifdef CONFIG_X86_64
  488. unsigned which;
  489. u64 base;
  490. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  491. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  492. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  493. set:
  494. base = ((u64)high << 32) | low;
  495. if (HYPERVISOR_set_segment_base(which, base) != 0)
  496. ret = -EFAULT;
  497. break;
  498. #endif
  499. case MSR_STAR:
  500. case MSR_CSTAR:
  501. case MSR_LSTAR:
  502. case MSR_SYSCALL_MASK:
  503. case MSR_IA32_SYSENTER_CS:
  504. case MSR_IA32_SYSENTER_ESP:
  505. case MSR_IA32_SYSENTER_EIP:
  506. /* Fast syscall setup is all done in hypercalls, so
  507. these are all ignored. Stub them out here to stop
  508. Xen console noise. */
  509. break;
  510. default:
  511. ret = native_write_msr_safe(msr, low, high);
  512. }
  513. return ret;
  514. }
  515. void xen_setup_shared_info(void)
  516. {
  517. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  518. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  519. xen_start_info->shared_info);
  520. HYPERVISOR_shared_info =
  521. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  522. } else
  523. HYPERVISOR_shared_info =
  524. (struct shared_info *)__va(xen_start_info->shared_info);
  525. #ifndef CONFIG_SMP
  526. /* In UP this is as good a place as any to set up shared info */
  527. xen_setup_vcpu_info_placement();
  528. #endif
  529. xen_setup_mfn_list_list();
  530. }
  531. /* This is called once we have the cpu_possible_map */
  532. void xen_setup_vcpu_info_placement(void)
  533. {
  534. int cpu;
  535. for_each_possible_cpu(cpu)
  536. xen_vcpu_setup(cpu);
  537. /* xen_vcpu_setup managed to place the vcpu_info within the
  538. percpu area for all cpus, so make use of it */
  539. if (have_vcpu_info_placement) {
  540. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  541. pv_irq_ops.save_fl = __PV_IS_CALLEE_SAVE(xen_save_fl_direct);
  542. pv_irq_ops.restore_fl = __PV_IS_CALLEE_SAVE(xen_restore_fl_direct);
  543. pv_irq_ops.irq_disable = __PV_IS_CALLEE_SAVE(xen_irq_disable_direct);
  544. pv_irq_ops.irq_enable = __PV_IS_CALLEE_SAVE(xen_irq_enable_direct);
  545. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  546. }
  547. }
  548. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  549. unsigned long addr, unsigned len)
  550. {
  551. char *start, *end, *reloc;
  552. unsigned ret;
  553. start = end = reloc = NULL;
  554. #define SITE(op, x) \
  555. case PARAVIRT_PATCH(op.x): \
  556. if (have_vcpu_info_placement) { \
  557. start = (char *)xen_##x##_direct; \
  558. end = xen_##x##_direct_end; \
  559. reloc = xen_##x##_direct_reloc; \
  560. } \
  561. goto patch_site
  562. switch (type) {
  563. SITE(pv_irq_ops, irq_enable);
  564. SITE(pv_irq_ops, irq_disable);
  565. SITE(pv_irq_ops, save_fl);
  566. SITE(pv_irq_ops, restore_fl);
  567. #undef SITE
  568. patch_site:
  569. if (start == NULL || (end-start) > len)
  570. goto default_patch;
  571. ret = paravirt_patch_insns(insnbuf, len, start, end);
  572. /* Note: because reloc is assigned from something that
  573. appears to be an array, gcc assumes it's non-null,
  574. but doesn't know its relationship with start and
  575. end. */
  576. if (reloc > start && reloc < end) {
  577. int reloc_off = reloc - start;
  578. long *relocp = (long *)(insnbuf + reloc_off);
  579. long delta = start - (char *)addr;
  580. *relocp += delta;
  581. }
  582. break;
  583. default_patch:
  584. default:
  585. ret = paravirt_patch_default(type, clobbers, insnbuf,
  586. addr, len);
  587. break;
  588. }
  589. return ret;
  590. }
  591. static const struct pv_info xen_info __initdata = {
  592. .paravirt_enabled = 1,
  593. .shared_kernel_pmd = 0,
  594. .name = "Xen",
  595. };
  596. static const struct pv_init_ops xen_init_ops __initdata = {
  597. .patch = xen_patch,
  598. .banner = xen_banner,
  599. .memory_setup = xen_memory_setup,
  600. .arch_setup = xen_arch_setup,
  601. .post_allocator_init = xen_post_allocator_init,
  602. };
  603. static const struct pv_time_ops xen_time_ops __initdata = {
  604. .time_init = xen_time_init,
  605. .set_wallclock = xen_set_wallclock,
  606. .get_wallclock = xen_get_wallclock,
  607. .get_tsc_khz = xen_tsc_khz,
  608. .sched_clock = xen_sched_clock,
  609. };
  610. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  611. .cpuid = xen_cpuid,
  612. .set_debugreg = xen_set_debugreg,
  613. .get_debugreg = xen_get_debugreg,
  614. .clts = xen_clts,
  615. .read_cr0 = native_read_cr0,
  616. .write_cr0 = xen_write_cr0,
  617. .read_cr4 = native_read_cr4,
  618. .read_cr4_safe = native_read_cr4_safe,
  619. .write_cr4 = xen_write_cr4,
  620. .wbinvd = native_wbinvd,
  621. .read_msr = native_read_msr_safe,
  622. .write_msr = xen_write_msr_safe,
  623. .read_tsc = native_read_tsc,
  624. .read_pmc = native_read_pmc,
  625. .iret = xen_iret,
  626. .irq_enable_sysexit = xen_sysexit,
  627. #ifdef CONFIG_X86_64
  628. .usergs_sysret32 = xen_sysret32,
  629. .usergs_sysret64 = xen_sysret64,
  630. #endif
  631. .load_tr_desc = paravirt_nop,
  632. .set_ldt = xen_set_ldt,
  633. .load_gdt = xen_load_gdt,
  634. .load_idt = xen_load_idt,
  635. .load_tls = xen_load_tls,
  636. #ifdef CONFIG_X86_64
  637. .load_gs_index = xen_load_gs_index,
  638. #endif
  639. .alloc_ldt = xen_alloc_ldt,
  640. .free_ldt = xen_free_ldt,
  641. .store_gdt = native_store_gdt,
  642. .store_idt = native_store_idt,
  643. .store_tr = xen_store_tr,
  644. .write_ldt_entry = xen_write_ldt_entry,
  645. .write_gdt_entry = xen_write_gdt_entry,
  646. .write_idt_entry = xen_write_idt_entry,
  647. .load_sp0 = xen_load_sp0,
  648. .set_iopl_mask = xen_set_iopl_mask,
  649. .io_delay = xen_io_delay,
  650. /* Xen takes care of %gs when switching to usermode for us */
  651. .swapgs = paravirt_nop,
  652. .lazy_mode = {
  653. .enter = paravirt_enter_lazy_cpu,
  654. .leave = xen_leave_lazy,
  655. },
  656. };
  657. static const struct pv_apic_ops xen_apic_ops __initdata = {
  658. #ifdef CONFIG_X86_LOCAL_APIC
  659. .setup_boot_clock = paravirt_nop,
  660. .setup_secondary_clock = paravirt_nop,
  661. .startup_ipi_hook = paravirt_nop,
  662. #endif
  663. };
  664. static void xen_reboot(int reason)
  665. {
  666. struct sched_shutdown r = { .reason = reason };
  667. #ifdef CONFIG_SMP
  668. smp_send_stop();
  669. #endif
  670. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  671. BUG();
  672. }
  673. static void xen_restart(char *msg)
  674. {
  675. xen_reboot(SHUTDOWN_reboot);
  676. }
  677. static void xen_emergency_restart(void)
  678. {
  679. xen_reboot(SHUTDOWN_reboot);
  680. }
  681. static void xen_machine_halt(void)
  682. {
  683. xen_reboot(SHUTDOWN_poweroff);
  684. }
  685. static void xen_crash_shutdown(struct pt_regs *regs)
  686. {
  687. xen_reboot(SHUTDOWN_crash);
  688. }
  689. static const struct machine_ops __initdata xen_machine_ops = {
  690. .restart = xen_restart,
  691. .halt = xen_machine_halt,
  692. .power_off = xen_machine_halt,
  693. .shutdown = xen_machine_halt,
  694. .crash_shutdown = xen_crash_shutdown,
  695. .emergency_restart = xen_emergency_restart,
  696. };
  697. /* First C function to be called on Xen boot */
  698. asmlinkage void __init xen_start_kernel(void)
  699. {
  700. pgd_t *pgd;
  701. if (!xen_start_info)
  702. return;
  703. xen_domain_type = XEN_PV_DOMAIN;
  704. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  705. xen_setup_features();
  706. /* Install Xen paravirt ops */
  707. pv_info = xen_info;
  708. pv_init_ops = xen_init_ops;
  709. pv_time_ops = xen_time_ops;
  710. pv_cpu_ops = xen_cpu_ops;
  711. pv_apic_ops = xen_apic_ops;
  712. pv_mmu_ops = xen_mmu_ops;
  713. xen_init_irq_ops();
  714. #ifdef CONFIG_X86_LOCAL_APIC
  715. /*
  716. * set up the basic apic ops.
  717. */
  718. apic_ops = &xen_basic_apic_ops;
  719. #endif
  720. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  721. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  722. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  723. }
  724. machine_ops = xen_machine_ops;
  725. #ifdef CONFIG_X86_64
  726. /* Disable until direct per-cpu data access. */
  727. have_vcpu_info_placement = 0;
  728. #endif
  729. #ifdef CONFIG_X86_64
  730. /*
  731. * Setup percpu state. We only need to do this for 64-bit
  732. * because 32-bit already has %fs set properly.
  733. */
  734. load_percpu_segment(0);
  735. #endif
  736. /*
  737. * The only reliable way to retain the initial address of the
  738. * percpu gdt_page is to remember it here, so we can go and
  739. * mark it RW later, when the initial percpu area is freed.
  740. */
  741. xen_initial_gdt = &per_cpu(gdt_page, 0);
  742. xen_smp_init();
  743. /* Get mfn list */
  744. if (!xen_feature(XENFEAT_auto_translated_physmap))
  745. xen_build_dynamic_phys_to_machine();
  746. pgd = (pgd_t *)xen_start_info->pt_base;
  747. /* Prevent unwanted bits from being set in PTEs. */
  748. __supported_pte_mask &= ~_PAGE_GLOBAL;
  749. if (!xen_initial_domain())
  750. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  751. /* Don't do the full vcpu_info placement stuff until we have a
  752. possible map and a non-dummy shared_info. */
  753. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  754. xen_raw_console_write("mapping kernel into physical memory\n");
  755. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  756. init_mm.pgd = pgd;
  757. /* keep using Xen gdt for now; no urgent need to change it */
  758. pv_info.kernel_rpl = 1;
  759. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  760. pv_info.kernel_rpl = 0;
  761. /* set the limit of our address space */
  762. xen_reserve_top();
  763. #ifdef CONFIG_X86_32
  764. /* set up basic CPUID stuff */
  765. cpu_detect(&new_cpu_data);
  766. new_cpu_data.hard_math = 1;
  767. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  768. #endif
  769. /* Poke various useful things into boot_params */
  770. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  771. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  772. ? __pa(xen_start_info->mod_start) : 0;
  773. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  774. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  775. if (!xen_initial_domain()) {
  776. add_preferred_console("xenboot", 0, NULL);
  777. add_preferred_console("tty", 0, NULL);
  778. add_preferred_console("hvc", 0, NULL);
  779. }
  780. xen_raw_console_write("about to get started...\n");
  781. /* Start the world */
  782. #ifdef CONFIG_X86_32
  783. i386_start_kernel();
  784. #else
  785. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  786. #endif
  787. }