enlighten.c 24 KB

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