enlighten.c 23 KB

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