enlighten.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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/physdev.h>
  30. #include <xen/interface/vcpu.h>
  31. #include <xen/interface/sched.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/page.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/processor.h>
  41. #include <asm/setup.h>
  42. #include <asm/desc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/reboot.h>
  46. #include "xen-ops.h"
  47. #include "mmu.h"
  48. #include "multicalls.h"
  49. EXPORT_SYMBOL_GPL(hypercall_page);
  50. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  51. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  52. /*
  53. * Note about cr3 (pagetable base) values:
  54. *
  55. * xen_cr3 contains the current logical cr3 value; it contains the
  56. * last set cr3. This may not be the current effective cr3, because
  57. * its update may be being lazily deferred. However, a vcpu looking
  58. * at its own cr3 can use this value knowing that it everything will
  59. * be self-consistent.
  60. *
  61. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  62. * hypercall to set the vcpu cr3 is complete (so it may be a little
  63. * out of date, but it will never be set early). If one vcpu is
  64. * looking at another vcpu's cr3 value, it should use this variable.
  65. */
  66. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  67. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  68. struct start_info *xen_start_info;
  69. EXPORT_SYMBOL_GPL(xen_start_info);
  70. struct shared_info xen_dummy_shared_info;
  71. /*
  72. * Point at some empty memory to start with. We map the real shared_info
  73. * page as soon as fixmap is up and running.
  74. */
  75. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  76. /*
  77. * Flag to determine whether vcpu info placement is available on all
  78. * VCPUs. We assume it is to start with, and then set it to zero on
  79. * the first failure. This is because it can succeed on some VCPUs
  80. * and not others, since it can involve hypervisor memory allocation,
  81. * or because the guest failed to guarantee all the appropriate
  82. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  83. *
  84. * Note that any particular CPU may be using a placed vcpu structure,
  85. * but we can only optimise if the all are.
  86. *
  87. * 0: not available, 1: available
  88. */
  89. static int have_vcpu_info_placement = 1;
  90. static void xen_vcpu_setup(int cpu)
  91. {
  92. struct vcpu_register_vcpu_info info;
  93. int err;
  94. struct vcpu_info *vcpup;
  95. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  96. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  97. if (!have_vcpu_info_placement)
  98. return; /* already tested, not available */
  99. vcpup = &per_cpu(xen_vcpu_info, cpu);
  100. info.mfn = virt_to_mfn(vcpup);
  101. info.offset = offset_in_page(vcpup);
  102. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  103. cpu, vcpup, info.mfn, info.offset);
  104. /* Check to see if the hypervisor will put the vcpu_info
  105. structure where we want it, which allows direct access via
  106. a percpu-variable. */
  107. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  108. if (err) {
  109. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  110. have_vcpu_info_placement = 0;
  111. } else {
  112. /* This cpu is using the registered vcpu info, even if
  113. later ones fail to. */
  114. per_cpu(xen_vcpu, cpu) = vcpup;
  115. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  116. cpu, vcpup);
  117. }
  118. }
  119. /*
  120. * On restore, set the vcpu placement up again.
  121. * If it fails, then we're in a bad state, since
  122. * we can't back out from using it...
  123. */
  124. void xen_vcpu_restore(void)
  125. {
  126. if (have_vcpu_info_placement) {
  127. int cpu;
  128. for_each_online_cpu(cpu) {
  129. bool other_cpu = (cpu != smp_processor_id());
  130. if (other_cpu &&
  131. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  132. BUG();
  133. xen_vcpu_setup(cpu);
  134. if (other_cpu &&
  135. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  136. BUG();
  137. }
  138. BUG_ON(!have_vcpu_info_placement);
  139. }
  140. }
  141. static void __init xen_banner(void)
  142. {
  143. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  144. pv_info.name);
  145. printk(KERN_INFO "Hypervisor signature: %s%s\n",
  146. xen_start_info->magic,
  147. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  148. }
  149. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  150. unsigned int *cx, unsigned int *dx)
  151. {
  152. unsigned maskedx = ~0;
  153. /*
  154. * Mask out inconvenient features, to try and disable as many
  155. * unsupported kernel subsystems as possible.
  156. */
  157. if (*ax == 1)
  158. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  159. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  160. (1 << X86_FEATURE_MCE) | /* disable MCE */
  161. (1 << X86_FEATURE_MCA) | /* disable MCA */
  162. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  163. asm(XEN_EMULATE_PREFIX "cpuid"
  164. : "=a" (*ax),
  165. "=b" (*bx),
  166. "=c" (*cx),
  167. "=d" (*dx)
  168. : "0" (*ax), "2" (*cx));
  169. *dx &= maskedx;
  170. }
  171. static void xen_set_debugreg(int reg, unsigned long val)
  172. {
  173. HYPERVISOR_set_debugreg(reg, val);
  174. }
  175. static unsigned long xen_get_debugreg(int reg)
  176. {
  177. return HYPERVISOR_get_debugreg(reg);
  178. }
  179. static unsigned long xen_save_fl(void)
  180. {
  181. struct vcpu_info *vcpu;
  182. unsigned long flags;
  183. vcpu = x86_read_percpu(xen_vcpu);
  184. /* flag has opposite sense of mask */
  185. flags = !vcpu->evtchn_upcall_mask;
  186. /* convert to IF type flag
  187. -0 -> 0x00000000
  188. -1 -> 0xffffffff
  189. */
  190. return (-flags) & X86_EFLAGS_IF;
  191. }
  192. static void xen_restore_fl(unsigned long flags)
  193. {
  194. struct vcpu_info *vcpu;
  195. /* convert from IF type flag */
  196. flags = !(flags & X86_EFLAGS_IF);
  197. /* There's a one instruction preempt window here. We need to
  198. make sure we're don't switch CPUs between getting the vcpu
  199. pointer and updating the mask. */
  200. preempt_disable();
  201. vcpu = x86_read_percpu(xen_vcpu);
  202. vcpu->evtchn_upcall_mask = flags;
  203. preempt_enable_no_resched();
  204. /* Doesn't matter if we get preempted here, because any
  205. pending event will get dealt with anyway. */
  206. if (flags == 0) {
  207. preempt_check_resched();
  208. barrier(); /* unmask then check (avoid races) */
  209. if (unlikely(vcpu->evtchn_upcall_pending))
  210. force_evtchn_callback();
  211. }
  212. }
  213. static void xen_irq_disable(void)
  214. {
  215. /* There's a one instruction preempt window here. We need to
  216. make sure we're don't switch CPUs between getting the vcpu
  217. pointer and updating the mask. */
  218. preempt_disable();
  219. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  220. preempt_enable_no_resched();
  221. }
  222. static void xen_irq_enable(void)
  223. {
  224. struct vcpu_info *vcpu;
  225. /* We don't need to worry about being preempted here, since
  226. either a) interrupts are disabled, so no preemption, or b)
  227. the caller is confused and is trying to re-enable interrupts
  228. on an indeterminate processor. */
  229. vcpu = x86_read_percpu(xen_vcpu);
  230. vcpu->evtchn_upcall_mask = 0;
  231. /* Doesn't matter if we get preempted here, because any
  232. pending event will get dealt with anyway. */
  233. barrier(); /* unmask then check (avoid races) */
  234. if (unlikely(vcpu->evtchn_upcall_pending))
  235. force_evtchn_callback();
  236. }
  237. static void xen_safe_halt(void)
  238. {
  239. /* Blocking includes an implicit local_irq_enable(). */
  240. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  241. BUG();
  242. }
  243. static void xen_halt(void)
  244. {
  245. if (irqs_disabled())
  246. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  247. else
  248. xen_safe_halt();
  249. }
  250. static void xen_leave_lazy(void)
  251. {
  252. paravirt_leave_lazy(paravirt_get_lazy_mode());
  253. xen_mc_flush();
  254. }
  255. static unsigned long xen_store_tr(void)
  256. {
  257. return 0;
  258. }
  259. static void xen_set_ldt(const void *addr, unsigned entries)
  260. {
  261. struct mmuext_op *op;
  262. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  263. op = mcs.args;
  264. op->cmd = MMUEXT_SET_LDT;
  265. op->arg1.linear_addr = (unsigned long)addr;
  266. op->arg2.nr_ents = entries;
  267. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  268. xen_mc_issue(PARAVIRT_LAZY_CPU);
  269. }
  270. static void xen_load_gdt(const struct desc_ptr *dtr)
  271. {
  272. unsigned long *frames;
  273. unsigned long va = dtr->address;
  274. unsigned int size = dtr->size + 1;
  275. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  276. int f;
  277. struct multicall_space mcs;
  278. /* A GDT can be up to 64k in size, which corresponds to 8192
  279. 8-byte entries, or 16 4k pages.. */
  280. BUG_ON(size > 65536);
  281. BUG_ON(va & ~PAGE_MASK);
  282. mcs = xen_mc_entry(sizeof(*frames) * pages);
  283. frames = mcs.args;
  284. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  285. frames[f] = virt_to_mfn(va);
  286. make_lowmem_page_readonly((void *)va);
  287. }
  288. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  289. xen_mc_issue(PARAVIRT_LAZY_CPU);
  290. }
  291. static void load_TLS_descriptor(struct thread_struct *t,
  292. unsigned int cpu, unsigned int i)
  293. {
  294. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  295. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  296. struct multicall_space mc = __xen_mc_entry(0);
  297. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  298. }
  299. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  300. {
  301. /*
  302. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  303. * it means we're in a context switch, and %gs has just been
  304. * saved. This means we can zero it out to prevent faults on
  305. * exit from the hypervisor if the next process has no %gs.
  306. * Either way, it has been saved, and the new value will get
  307. * loaded properly. This will go away as soon as Xen has been
  308. * modified to not save/restore %gs for normal hypercalls.
  309. *
  310. * On x86_64, this hack is not used for %gs, because gs points
  311. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  312. * must not zero %gs on x86_64
  313. *
  314. * For x86_64, we need to zero %fs, otherwise we may get an
  315. * exception between the new %fs descriptor being loaded and
  316. * %fs being effectively cleared at __switch_to().
  317. */
  318. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  319. #ifdef CONFIG_X86_32
  320. loadsegment(gs, 0);
  321. #else
  322. loadsegment(fs, 0);
  323. #endif
  324. }
  325. xen_mc_batch();
  326. load_TLS_descriptor(t, cpu, 0);
  327. load_TLS_descriptor(t, cpu, 1);
  328. load_TLS_descriptor(t, cpu, 2);
  329. xen_mc_issue(PARAVIRT_LAZY_CPU);
  330. }
  331. #ifdef CONFIG_X86_64
  332. static void xen_load_gs_index(unsigned int idx)
  333. {
  334. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  335. BUG();
  336. }
  337. #endif
  338. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  339. const void *ptr)
  340. {
  341. unsigned long lp = (unsigned long)&dt[entrynum];
  342. xmaddr_t mach_lp = virt_to_machine(lp);
  343. u64 entry = *(u64 *)ptr;
  344. preempt_disable();
  345. xen_mc_flush();
  346. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  347. BUG();
  348. preempt_enable();
  349. }
  350. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  351. struct trap_info *info)
  352. {
  353. if (val->type != 0xf && val->type != 0xe)
  354. return 0;
  355. info->vector = vector;
  356. info->address = gate_offset(*val);
  357. info->cs = gate_segment(*val);
  358. info->flags = val->dpl;
  359. /* interrupt gates clear IF */
  360. if (val->type == 0xe)
  361. info->flags |= 4;
  362. return 1;
  363. }
  364. /* Locations of each CPU's IDT */
  365. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  366. /* Set an IDT entry. If the entry is part of the current IDT, then
  367. also update Xen. */
  368. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  369. {
  370. unsigned long p = (unsigned long)&dt[entrynum];
  371. unsigned long start, end;
  372. preempt_disable();
  373. start = __get_cpu_var(idt_desc).address;
  374. end = start + __get_cpu_var(idt_desc).size + 1;
  375. xen_mc_flush();
  376. native_write_idt_entry(dt, entrynum, g);
  377. if (p >= start && (p + 8) <= end) {
  378. struct trap_info info[2];
  379. info[1].address = 0;
  380. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  381. if (HYPERVISOR_set_trap_table(info))
  382. BUG();
  383. }
  384. preempt_enable();
  385. }
  386. static void xen_convert_trap_info(const struct desc_ptr *desc,
  387. struct trap_info *traps)
  388. {
  389. unsigned in, out, count;
  390. count = (desc->size+1) / sizeof(gate_desc);
  391. BUG_ON(count > 256);
  392. for (in = out = 0; in < count; in++) {
  393. gate_desc *entry = (gate_desc*)(desc->address) + in;
  394. if (cvt_gate_to_trap(in, entry, &traps[out]))
  395. out++;
  396. }
  397. traps[out].address = 0;
  398. }
  399. void xen_copy_trap_info(struct trap_info *traps)
  400. {
  401. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  402. xen_convert_trap_info(desc, traps);
  403. }
  404. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  405. hold a spinlock to protect the static traps[] array (static because
  406. it avoids allocation, and saves stack space). */
  407. static void xen_load_idt(const struct desc_ptr *desc)
  408. {
  409. static DEFINE_SPINLOCK(lock);
  410. static struct trap_info traps[257];
  411. spin_lock(&lock);
  412. __get_cpu_var(idt_desc) = *desc;
  413. xen_convert_trap_info(desc, traps);
  414. xen_mc_flush();
  415. if (HYPERVISOR_set_trap_table(traps))
  416. BUG();
  417. spin_unlock(&lock);
  418. }
  419. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  420. they're handled differently. */
  421. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  422. const void *desc, int type)
  423. {
  424. preempt_disable();
  425. switch (type) {
  426. case DESC_LDT:
  427. case DESC_TSS:
  428. /* ignore */
  429. break;
  430. default: {
  431. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  432. xen_mc_flush();
  433. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  434. BUG();
  435. }
  436. }
  437. preempt_enable();
  438. }
  439. static void xen_load_sp0(struct tss_struct *tss,
  440. struct thread_struct *thread)
  441. {
  442. struct multicall_space mcs = xen_mc_entry(0);
  443. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  444. xen_mc_issue(PARAVIRT_LAZY_CPU);
  445. }
  446. static void xen_set_iopl_mask(unsigned mask)
  447. {
  448. struct physdev_set_iopl set_iopl;
  449. /* Force the change at ring 0. */
  450. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  451. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  452. }
  453. static void xen_io_delay(void)
  454. {
  455. }
  456. #ifdef CONFIG_X86_LOCAL_APIC
  457. static u32 xen_apic_read(unsigned long reg)
  458. {
  459. return 0;
  460. }
  461. static void xen_apic_write(unsigned long reg, u32 val)
  462. {
  463. /* Warn to see if there's any stray references */
  464. WARN_ON(1);
  465. }
  466. #endif
  467. static void xen_flush_tlb(void)
  468. {
  469. struct mmuext_op *op;
  470. struct multicall_space mcs;
  471. preempt_disable();
  472. mcs = xen_mc_entry(sizeof(*op));
  473. op = mcs.args;
  474. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  475. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  476. xen_mc_issue(PARAVIRT_LAZY_MMU);
  477. preempt_enable();
  478. }
  479. static void xen_flush_tlb_single(unsigned long addr)
  480. {
  481. struct mmuext_op *op;
  482. struct multicall_space mcs;
  483. preempt_disable();
  484. mcs = xen_mc_entry(sizeof(*op));
  485. op = mcs.args;
  486. op->cmd = MMUEXT_INVLPG_LOCAL;
  487. op->arg1.linear_addr = addr & PAGE_MASK;
  488. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  489. xen_mc_issue(PARAVIRT_LAZY_MMU);
  490. preempt_enable();
  491. }
  492. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  493. unsigned long va)
  494. {
  495. struct {
  496. struct mmuext_op op;
  497. cpumask_t mask;
  498. } *args;
  499. cpumask_t cpumask = *cpus;
  500. struct multicall_space mcs;
  501. /*
  502. * A couple of (to be removed) sanity checks:
  503. *
  504. * - current CPU must not be in mask
  505. * - mask must exist :)
  506. */
  507. BUG_ON(cpus_empty(cpumask));
  508. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  509. BUG_ON(!mm);
  510. /* If a CPU which we ran on has gone down, OK. */
  511. cpus_and(cpumask, cpumask, cpu_online_map);
  512. if (cpus_empty(cpumask))
  513. return;
  514. mcs = xen_mc_entry(sizeof(*args));
  515. args = mcs.args;
  516. args->mask = cpumask;
  517. args->op.arg2.vcpumask = &args->mask;
  518. if (va == TLB_FLUSH_ALL) {
  519. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  520. } else {
  521. args->op.cmd = MMUEXT_INVLPG_MULTI;
  522. args->op.arg1.linear_addr = va;
  523. }
  524. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  525. xen_mc_issue(PARAVIRT_LAZY_MMU);
  526. }
  527. static void xen_clts(void)
  528. {
  529. struct multicall_space mcs;
  530. mcs = xen_mc_entry(0);
  531. MULTI_fpu_taskswitch(mcs.mc, 0);
  532. xen_mc_issue(PARAVIRT_LAZY_CPU);
  533. }
  534. static void xen_write_cr0(unsigned long cr0)
  535. {
  536. struct multicall_space mcs;
  537. /* Only pay attention to cr0.TS; everything else is
  538. ignored. */
  539. mcs = xen_mc_entry(0);
  540. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  541. xen_mc_issue(PARAVIRT_LAZY_CPU);
  542. }
  543. static void xen_write_cr2(unsigned long cr2)
  544. {
  545. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  546. }
  547. static unsigned long xen_read_cr2(void)
  548. {
  549. return x86_read_percpu(xen_vcpu)->arch.cr2;
  550. }
  551. static unsigned long xen_read_cr2_direct(void)
  552. {
  553. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  554. }
  555. static void xen_write_cr4(unsigned long cr4)
  556. {
  557. cr4 &= ~X86_CR4_PGE;
  558. cr4 &= ~X86_CR4_PSE;
  559. native_write_cr4(cr4);
  560. }
  561. static unsigned long xen_read_cr3(void)
  562. {
  563. return x86_read_percpu(xen_cr3);
  564. }
  565. static void set_current_cr3(void *v)
  566. {
  567. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  568. }
  569. static void __xen_write_cr3(bool kernel, unsigned long cr3)
  570. {
  571. struct mmuext_op *op;
  572. struct multicall_space mcs;
  573. unsigned long mfn;
  574. if (cr3)
  575. mfn = pfn_to_mfn(PFN_DOWN(cr3));
  576. else
  577. mfn = 0;
  578. WARN_ON(mfn == 0 && kernel);
  579. mcs = __xen_mc_entry(sizeof(*op));
  580. op = mcs.args;
  581. op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
  582. op->arg1.mfn = mfn;
  583. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  584. if (kernel) {
  585. x86_write_percpu(xen_cr3, cr3);
  586. /* Update xen_current_cr3 once the batch has actually
  587. been submitted. */
  588. xen_mc_callback(set_current_cr3, (void *)cr3);
  589. }
  590. }
  591. static void xen_write_cr3(unsigned long cr3)
  592. {
  593. BUG_ON(preemptible());
  594. xen_mc_batch(); /* disables interrupts */
  595. /* Update while interrupts are disabled, so its atomic with
  596. respect to ipis */
  597. x86_write_percpu(xen_cr3, cr3);
  598. __xen_write_cr3(true, cr3);
  599. #ifdef CONFIG_X86_64
  600. {
  601. pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
  602. if (user_pgd)
  603. __xen_write_cr3(false, __pa(user_pgd));
  604. else
  605. __xen_write_cr3(false, 0);
  606. }
  607. #endif
  608. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  609. }
  610. /* Early in boot, while setting up the initial pagetable, assume
  611. everything is pinned. */
  612. static __init void xen_alloc_pte_init(struct mm_struct *mm, u32 pfn)
  613. {
  614. #ifdef CONFIG_FLATMEM
  615. BUG_ON(mem_map); /* should only be used early */
  616. #endif
  617. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  618. }
  619. /* Early release_pte assumes that all pts are pinned, since there's
  620. only init_mm and anything attached to that is pinned. */
  621. static void xen_release_pte_init(u32 pfn)
  622. {
  623. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  624. }
  625. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  626. {
  627. struct mmuext_op op;
  628. op.cmd = cmd;
  629. op.arg1.mfn = pfn_to_mfn(pfn);
  630. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  631. BUG();
  632. }
  633. /* This needs to make sure the new pte page is pinned iff its being
  634. attached to a pinned pagetable. */
  635. static void xen_alloc_ptpage(struct mm_struct *mm, u32 pfn, unsigned level)
  636. {
  637. struct page *page = pfn_to_page(pfn);
  638. if (PagePinned(virt_to_page(mm->pgd))) {
  639. SetPagePinned(page);
  640. if (!PageHighMem(page)) {
  641. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  642. if (level == PT_PTE)
  643. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  644. } else
  645. /* make sure there are no stray mappings of
  646. this page */
  647. kmap_flush_unused();
  648. }
  649. }
  650. static void xen_alloc_pte(struct mm_struct *mm, u32 pfn)
  651. {
  652. xen_alloc_ptpage(mm, pfn, PT_PTE);
  653. }
  654. static void xen_alloc_pmd(struct mm_struct *mm, u32 pfn)
  655. {
  656. xen_alloc_ptpage(mm, pfn, PT_PMD);
  657. }
  658. static int xen_pgd_alloc(struct mm_struct *mm)
  659. {
  660. pgd_t *pgd = mm->pgd;
  661. int ret = 0;
  662. BUG_ON(PagePinned(virt_to_page(pgd)));
  663. #ifdef CONFIG_X86_64
  664. {
  665. struct page *page = virt_to_page(pgd);
  666. BUG_ON(page->private != 0);
  667. page->private = __get_free_page(GFP_KERNEL | __GFP_ZERO);
  668. if (page->private == 0)
  669. ret = -ENOMEM;
  670. BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
  671. }
  672. #endif
  673. return ret;
  674. }
  675. static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  676. {
  677. #ifdef CONFIG_X86_64
  678. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  679. if (user_pgd)
  680. free_page((unsigned long)user_pgd);
  681. #endif
  682. }
  683. /* This should never happen until we're OK to use struct page */
  684. static void xen_release_ptpage(u32 pfn, unsigned level)
  685. {
  686. struct page *page = pfn_to_page(pfn);
  687. if (PagePinned(page)) {
  688. if (!PageHighMem(page)) {
  689. if (level == PT_PTE)
  690. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  691. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  692. }
  693. ClearPagePinned(page);
  694. }
  695. }
  696. static void xen_release_pte(u32 pfn)
  697. {
  698. xen_release_ptpage(pfn, PT_PTE);
  699. }
  700. static void xen_release_pmd(u32 pfn)
  701. {
  702. xen_release_ptpage(pfn, PT_PMD);
  703. }
  704. #if PAGETABLE_LEVELS == 4
  705. static void xen_alloc_pud(struct mm_struct *mm, u32 pfn)
  706. {
  707. xen_alloc_ptpage(mm, pfn, PT_PUD);
  708. }
  709. static void xen_release_pud(u32 pfn)
  710. {
  711. xen_release_ptpage(pfn, PT_PUD);
  712. }
  713. #endif
  714. #ifdef CONFIG_HIGHPTE
  715. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  716. {
  717. pgprot_t prot = PAGE_KERNEL;
  718. if (PagePinned(page))
  719. prot = PAGE_KERNEL_RO;
  720. if (0 && PageHighMem(page))
  721. printk("mapping highpte %lx type %d prot %s\n",
  722. page_to_pfn(page), type,
  723. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  724. return kmap_atomic_prot(page, type, prot);
  725. }
  726. #endif
  727. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  728. {
  729. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  730. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  731. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  732. pte_val_ma(pte));
  733. return pte;
  734. }
  735. /* Init-time set_pte while constructing initial pagetables, which
  736. doesn't allow RO pagetable pages to be remapped RW */
  737. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  738. {
  739. pte = mask_rw_pte(ptep, pte);
  740. xen_set_pte(ptep, pte);
  741. }
  742. static __init void xen_pagetable_setup_start(pgd_t *base)
  743. {
  744. }
  745. void xen_setup_shared_info(void)
  746. {
  747. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  748. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  749. xen_start_info->shared_info);
  750. HYPERVISOR_shared_info =
  751. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  752. } else
  753. HYPERVISOR_shared_info =
  754. (struct shared_info *)__va(xen_start_info->shared_info);
  755. #ifndef CONFIG_SMP
  756. /* In UP this is as good a place as any to set up shared info */
  757. xen_setup_vcpu_info_placement();
  758. #endif
  759. xen_setup_mfn_list_list();
  760. }
  761. static __init void xen_pagetable_setup_done(pgd_t *base)
  762. {
  763. xen_setup_shared_info();
  764. }
  765. static __init void xen_post_allocator_init(void)
  766. {
  767. pv_mmu_ops.set_pte = xen_set_pte;
  768. pv_mmu_ops.set_pmd = xen_set_pmd;
  769. pv_mmu_ops.set_pud = xen_set_pud;
  770. #if PAGETABLE_LEVELS == 4
  771. pv_mmu_ops.set_pgd = xen_set_pgd;
  772. #endif
  773. /* This will work as long as patching hasn't happened yet
  774. (which it hasn't) */
  775. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  776. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  777. pv_mmu_ops.release_pte = xen_release_pte;
  778. pv_mmu_ops.release_pmd = xen_release_pmd;
  779. #if PAGETABLE_LEVELS == 4
  780. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  781. pv_mmu_ops.release_pud = xen_release_pud;
  782. #endif
  783. xen_mark_init_mm_pinned();
  784. }
  785. /* This is called once we have the cpu_possible_map */
  786. void xen_setup_vcpu_info_placement(void)
  787. {
  788. int cpu;
  789. for_each_possible_cpu(cpu)
  790. xen_vcpu_setup(cpu);
  791. /* xen_vcpu_setup managed to place the vcpu_info within the
  792. percpu area for all cpus, so make use of it */
  793. #ifdef CONFIG_X86_32
  794. if (have_vcpu_info_placement) {
  795. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  796. pv_irq_ops.save_fl = xen_save_fl_direct;
  797. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  798. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  799. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  800. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  801. }
  802. #endif
  803. }
  804. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  805. unsigned long addr, unsigned len)
  806. {
  807. char *start, *end, *reloc;
  808. unsigned ret;
  809. start = end = reloc = NULL;
  810. #define SITE(op, x) \
  811. case PARAVIRT_PATCH(op.x): \
  812. if (have_vcpu_info_placement) { \
  813. start = (char *)xen_##x##_direct; \
  814. end = xen_##x##_direct_end; \
  815. reloc = xen_##x##_direct_reloc; \
  816. } \
  817. goto patch_site
  818. switch (type) {
  819. #ifdef CONFIG_X86_32
  820. SITE(pv_irq_ops, irq_enable);
  821. SITE(pv_irq_ops, irq_disable);
  822. SITE(pv_irq_ops, save_fl);
  823. SITE(pv_irq_ops, restore_fl);
  824. #endif /* CONFIG_X86_32 */
  825. #undef SITE
  826. patch_site:
  827. if (start == NULL || (end-start) > len)
  828. goto default_patch;
  829. ret = paravirt_patch_insns(insnbuf, len, start, end);
  830. /* Note: because reloc is assigned from something that
  831. appears to be an array, gcc assumes it's non-null,
  832. but doesn't know its relationship with start and
  833. end. */
  834. if (reloc > start && reloc < end) {
  835. int reloc_off = reloc - start;
  836. long *relocp = (long *)(insnbuf + reloc_off);
  837. long delta = start - (char *)addr;
  838. *relocp += delta;
  839. }
  840. break;
  841. default_patch:
  842. default:
  843. ret = paravirt_patch_default(type, clobbers, insnbuf,
  844. addr, len);
  845. break;
  846. }
  847. return ret;
  848. }
  849. static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
  850. {
  851. pte_t pte;
  852. phys >>= PAGE_SHIFT;
  853. switch (idx) {
  854. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  855. #ifdef CONFIG_X86_F00F_BUG
  856. case FIX_F00F_IDT:
  857. #endif
  858. #ifdef CONFIG_X86_32
  859. case FIX_WP_TEST:
  860. case FIX_VDSO:
  861. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  862. #else
  863. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  864. #endif
  865. #ifdef CONFIG_X86_LOCAL_APIC
  866. case FIX_APIC_BASE: /* maps dummy local APIC */
  867. #endif
  868. pte = pfn_pte(phys, prot);
  869. break;
  870. default:
  871. pte = mfn_pte(phys, prot);
  872. break;
  873. }
  874. __native_set_fixmap(idx, pte);
  875. }
  876. static const struct pv_info xen_info __initdata = {
  877. .paravirt_enabled = 1,
  878. .shared_kernel_pmd = 0,
  879. .name = "Xen",
  880. };
  881. static const struct pv_init_ops xen_init_ops __initdata = {
  882. .patch = xen_patch,
  883. .banner = xen_banner,
  884. .memory_setup = xen_memory_setup,
  885. .arch_setup = xen_arch_setup,
  886. .post_allocator_init = xen_post_allocator_init,
  887. };
  888. static const struct pv_time_ops xen_time_ops __initdata = {
  889. .time_init = xen_time_init,
  890. .set_wallclock = xen_set_wallclock,
  891. .get_wallclock = xen_get_wallclock,
  892. .get_tsc_khz = xen_tsc_khz,
  893. .sched_clock = xen_sched_clock,
  894. };
  895. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  896. .cpuid = xen_cpuid,
  897. .set_debugreg = xen_set_debugreg,
  898. .get_debugreg = xen_get_debugreg,
  899. .clts = xen_clts,
  900. .read_cr0 = native_read_cr0,
  901. .write_cr0 = xen_write_cr0,
  902. .read_cr4 = native_read_cr4,
  903. .read_cr4_safe = native_read_cr4_safe,
  904. .write_cr4 = xen_write_cr4,
  905. .wbinvd = native_wbinvd,
  906. .read_msr = native_read_msr_safe,
  907. .write_msr = native_write_msr_safe,
  908. .read_tsc = native_read_tsc,
  909. .read_pmc = native_read_pmc,
  910. .iret = xen_iret,
  911. .irq_enable_sysexit = xen_sysexit,
  912. #ifdef CONFIG_X86_64
  913. .usergs_sysret32 = xen_sysret32,
  914. .usergs_sysret64 = xen_sysret64,
  915. #endif
  916. .load_tr_desc = paravirt_nop,
  917. .set_ldt = xen_set_ldt,
  918. .load_gdt = xen_load_gdt,
  919. .load_idt = xen_load_idt,
  920. .load_tls = xen_load_tls,
  921. #ifdef CONFIG_X86_64
  922. .load_gs_index = xen_load_gs_index,
  923. #endif
  924. .store_gdt = native_store_gdt,
  925. .store_idt = native_store_idt,
  926. .store_tr = xen_store_tr,
  927. .write_ldt_entry = xen_write_ldt_entry,
  928. .write_gdt_entry = xen_write_gdt_entry,
  929. .write_idt_entry = xen_write_idt_entry,
  930. .load_sp0 = xen_load_sp0,
  931. .set_iopl_mask = xen_set_iopl_mask,
  932. .io_delay = xen_io_delay,
  933. /* Xen takes care of %gs when switching to usermode for us */
  934. .swapgs = paravirt_nop,
  935. .lazy_mode = {
  936. .enter = paravirt_enter_lazy_cpu,
  937. .leave = xen_leave_lazy,
  938. },
  939. };
  940. static void __init __xen_init_IRQ(void)
  941. {
  942. #ifdef CONFIG_X86_64
  943. int i;
  944. /* Create identity vector->irq map */
  945. for(i = 0; i < NR_VECTORS; i++) {
  946. int cpu;
  947. for_each_possible_cpu(cpu)
  948. per_cpu(vector_irq, cpu)[i] = i;
  949. }
  950. #endif /* CONFIG_X86_64 */
  951. xen_init_IRQ();
  952. }
  953. static const struct pv_irq_ops xen_irq_ops __initdata = {
  954. .init_IRQ = __xen_init_IRQ,
  955. .save_fl = xen_save_fl,
  956. .restore_fl = xen_restore_fl,
  957. .irq_disable = xen_irq_disable,
  958. .irq_enable = xen_irq_enable,
  959. .safe_halt = xen_safe_halt,
  960. .halt = xen_halt,
  961. #ifdef CONFIG_X86_64
  962. .adjust_exception_frame = xen_adjust_exception_frame,
  963. #endif
  964. };
  965. static const struct pv_apic_ops xen_apic_ops __initdata = {
  966. #ifdef CONFIG_X86_LOCAL_APIC
  967. .apic_write = xen_apic_write,
  968. .apic_write_atomic = xen_apic_write,
  969. .apic_read = xen_apic_read,
  970. .setup_boot_clock = paravirt_nop,
  971. .setup_secondary_clock = paravirt_nop,
  972. .startup_ipi_hook = paravirt_nop,
  973. #endif
  974. };
  975. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  976. .pagetable_setup_start = xen_pagetable_setup_start,
  977. .pagetable_setup_done = xen_pagetable_setup_done,
  978. .read_cr2 = xen_read_cr2,
  979. .write_cr2 = xen_write_cr2,
  980. .read_cr3 = xen_read_cr3,
  981. .write_cr3 = xen_write_cr3,
  982. .flush_tlb_user = xen_flush_tlb,
  983. .flush_tlb_kernel = xen_flush_tlb,
  984. .flush_tlb_single = xen_flush_tlb_single,
  985. .flush_tlb_others = xen_flush_tlb_others,
  986. .pte_update = paravirt_nop,
  987. .pte_update_defer = paravirt_nop,
  988. .pgd_alloc = xen_pgd_alloc,
  989. .pgd_free = xen_pgd_free,
  990. .alloc_pte = xen_alloc_pte_init,
  991. .release_pte = xen_release_pte_init,
  992. .alloc_pmd = xen_alloc_pte_init,
  993. .alloc_pmd_clone = paravirt_nop,
  994. .release_pmd = xen_release_pte_init,
  995. #ifdef CONFIG_HIGHPTE
  996. .kmap_atomic_pte = xen_kmap_atomic_pte,
  997. #endif
  998. #ifdef CONFIG_X86_64
  999. .set_pte = xen_set_pte,
  1000. #else
  1001. .set_pte = xen_set_pte_init,
  1002. #endif
  1003. .set_pte_at = xen_set_pte_at,
  1004. .set_pmd = xen_set_pmd_hyper,
  1005. .ptep_modify_prot_start = __ptep_modify_prot_start,
  1006. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  1007. .pte_val = xen_pte_val,
  1008. .pte_flags = native_pte_val,
  1009. .pgd_val = xen_pgd_val,
  1010. .make_pte = xen_make_pte,
  1011. .make_pgd = xen_make_pgd,
  1012. #ifdef CONFIG_X86_PAE
  1013. .set_pte_atomic = xen_set_pte_atomic,
  1014. .set_pte_present = xen_set_pte_at,
  1015. .pte_clear = xen_pte_clear,
  1016. .pmd_clear = xen_pmd_clear,
  1017. #endif /* CONFIG_X86_PAE */
  1018. .set_pud = xen_set_pud_hyper,
  1019. .make_pmd = xen_make_pmd,
  1020. .pmd_val = xen_pmd_val,
  1021. #if PAGETABLE_LEVELS == 4
  1022. .pud_val = xen_pud_val,
  1023. .make_pud = xen_make_pud,
  1024. .set_pgd = xen_set_pgd_hyper,
  1025. .alloc_pud = xen_alloc_pte_init,
  1026. .release_pud = xen_release_pte_init,
  1027. #endif /* PAGETABLE_LEVELS == 4 */
  1028. .activate_mm = xen_activate_mm,
  1029. .dup_mmap = xen_dup_mmap,
  1030. .exit_mmap = xen_exit_mmap,
  1031. .lazy_mode = {
  1032. .enter = paravirt_enter_lazy_mmu,
  1033. .leave = xen_leave_lazy,
  1034. },
  1035. .set_fixmap = xen_set_fixmap,
  1036. };
  1037. static void xen_reboot(int reason)
  1038. {
  1039. struct sched_shutdown r = { .reason = reason };
  1040. #ifdef CONFIG_SMP
  1041. smp_send_stop();
  1042. #endif
  1043. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  1044. BUG();
  1045. }
  1046. static void xen_restart(char *msg)
  1047. {
  1048. xen_reboot(SHUTDOWN_reboot);
  1049. }
  1050. static void xen_emergency_restart(void)
  1051. {
  1052. xen_reboot(SHUTDOWN_reboot);
  1053. }
  1054. static void xen_machine_halt(void)
  1055. {
  1056. xen_reboot(SHUTDOWN_poweroff);
  1057. }
  1058. static void xen_crash_shutdown(struct pt_regs *regs)
  1059. {
  1060. xen_reboot(SHUTDOWN_crash);
  1061. }
  1062. static const struct machine_ops __initdata xen_machine_ops = {
  1063. .restart = xen_restart,
  1064. .halt = xen_machine_halt,
  1065. .power_off = xen_machine_halt,
  1066. .shutdown = xen_machine_halt,
  1067. .crash_shutdown = xen_crash_shutdown,
  1068. .emergency_restart = xen_emergency_restart,
  1069. };
  1070. static void __init xen_reserve_top(void)
  1071. {
  1072. #ifdef CONFIG_X86_32
  1073. unsigned long top = HYPERVISOR_VIRT_START;
  1074. struct xen_platform_parameters pp;
  1075. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1076. top = pp.virt_start;
  1077. reserve_top_address(-top + 2 * PAGE_SIZE);
  1078. #endif /* CONFIG_X86_32 */
  1079. }
  1080. /*
  1081. * Like __va(), but returns address in the kernel mapping (which is
  1082. * all we have until the physical memory mapping has been set up.
  1083. */
  1084. static void *__ka(phys_addr_t paddr)
  1085. {
  1086. #ifdef CONFIG_X86_64
  1087. return (void *)(paddr + __START_KERNEL_map);
  1088. #else
  1089. return __va(paddr);
  1090. #endif
  1091. }
  1092. /* Convert a machine address to physical address */
  1093. static unsigned long m2p(phys_addr_t maddr)
  1094. {
  1095. phys_addr_t paddr;
  1096. maddr &= PTE_MASK;
  1097. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1098. return paddr;
  1099. }
  1100. /* Convert a machine address to kernel virtual */
  1101. static void *m2v(phys_addr_t maddr)
  1102. {
  1103. return __ka(m2p(maddr));
  1104. }
  1105. #ifdef CONFIG_X86_64
  1106. static void walk(pgd_t *pgd, unsigned long addr)
  1107. {
  1108. unsigned l4idx = pgd_index(addr);
  1109. unsigned l3idx = pud_index(addr);
  1110. unsigned l2idx = pmd_index(addr);
  1111. unsigned l1idx = pte_index(addr);
  1112. pgd_t l4;
  1113. pud_t l3;
  1114. pmd_t l2;
  1115. pte_t l1;
  1116. xen_raw_printk("walk %p, %lx -> %d %d %d %d\n",
  1117. pgd, addr, l4idx, l3idx, l2idx, l1idx);
  1118. l4 = pgd[l4idx];
  1119. xen_raw_printk(" l4: %016lx\n", l4.pgd);
  1120. xen_raw_printk(" %016lx\n", pgd_val(l4));
  1121. l3 = ((pud_t *)(m2v(l4.pgd)))[l3idx];
  1122. xen_raw_printk(" l3: %016lx\n", l3.pud);
  1123. xen_raw_printk(" %016lx\n", pud_val(l3));
  1124. l2 = ((pmd_t *)(m2v(l3.pud)))[l2idx];
  1125. xen_raw_printk(" l2: %016lx\n", l2.pmd);
  1126. xen_raw_printk(" %016lx\n", pmd_val(l2));
  1127. l1 = ((pte_t *)(m2v(l2.pmd)))[l1idx];
  1128. xen_raw_printk(" l1: %016lx\n", l1.pte);
  1129. xen_raw_printk(" %016lx\n", pte_val(l1));
  1130. }
  1131. #endif
  1132. static void set_page_prot(void *addr, pgprot_t prot)
  1133. {
  1134. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1135. pte_t pte = pfn_pte(pfn, prot);
  1136. xen_raw_printk("addr=%p pfn=%lx mfn=%lx prot=%016llx pte=%016llx\n",
  1137. addr, pfn, get_phys_to_machine(pfn),
  1138. pgprot_val(prot), pte.pte);
  1139. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1140. BUG();
  1141. }
  1142. /*
  1143. * Identity map, in addition to plain kernel map. This needs to be
  1144. * large enough to allocate page table pages to allocate the rest.
  1145. * Each page can map 2MB.
  1146. */
  1147. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  1148. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1149. {
  1150. unsigned pmdidx, pteidx;
  1151. unsigned ident_pte;
  1152. unsigned long pfn;
  1153. ident_pte = 0;
  1154. pfn = 0;
  1155. for(pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1156. pte_t *pte_page;
  1157. /* Reuse or allocate a page of ptes */
  1158. if (pmd_present(pmd[pmdidx]))
  1159. pte_page = m2v(pmd[pmdidx].pmd);
  1160. else {
  1161. /* Check for free pte pages */
  1162. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1163. break;
  1164. pte_page = &level1_ident_pgt[ident_pte];
  1165. ident_pte += PTRS_PER_PTE;
  1166. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1167. }
  1168. /* Install mappings */
  1169. for(pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1170. pte_t pte;
  1171. if (pfn > max_pfn_mapped)
  1172. max_pfn_mapped = pfn;
  1173. if (!pte_none(pte_page[pteidx]))
  1174. continue;
  1175. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1176. pte_page[pteidx] = pte;
  1177. }
  1178. }
  1179. for(pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1180. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1181. set_page_prot(pmd, PAGE_KERNEL_RO);
  1182. }
  1183. #ifdef CONFIG_X86_64
  1184. static void convert_pfn_mfn(void *v)
  1185. {
  1186. pte_t *pte = v;
  1187. int i;
  1188. /* All levels are converted the same way, so just treat them
  1189. as ptes. */
  1190. for(i = 0; i < PTRS_PER_PTE; i++)
  1191. pte[i] = xen_make_pte(pte[i].pte);
  1192. }
  1193. /*
  1194. * Set up the inital kernel pagetable.
  1195. *
  1196. * We can construct this by grafting the Xen provided pagetable into
  1197. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1198. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1199. * means that only the kernel has a physical mapping to start with -
  1200. * but that's enough to get __va working. We need to fill in the rest
  1201. * of the physical mapping once some sort of allocator has been set
  1202. * up.
  1203. */
  1204. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1205. {
  1206. pud_t *l3;
  1207. pmd_t *l2;
  1208. /* Zap identity mapping */
  1209. init_level4_pgt[0] = __pgd(0);
  1210. /* Pre-constructed entries are in pfn, so convert to mfn */
  1211. convert_pfn_mfn(init_level4_pgt);
  1212. convert_pfn_mfn(level3_ident_pgt);
  1213. convert_pfn_mfn(level3_kernel_pgt);
  1214. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1215. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1216. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1217. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1218. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1219. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1220. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1221. /* Set up identity map */
  1222. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1223. /* Make pagetable pieces RO */
  1224. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1225. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1226. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1227. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1228. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1229. /* Pin down new L4 */
  1230. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1231. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1232. /* Unpin Xen-provided one */
  1233. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1234. /* Switch over */
  1235. pgd = init_level4_pgt;
  1236. /*
  1237. * At this stage there can be no user pgd, and no page
  1238. * structure to attach it to, so make sure we just set kernel
  1239. * pgd.
  1240. */
  1241. xen_mc_batch();
  1242. __xen_write_cr3(true, __pa(pgd));
  1243. xen_mc_issue(PARAVIRT_LAZY_CPU);
  1244. reserve_early(__pa(xen_start_info->pt_base),
  1245. __pa(xen_start_info->pt_base +
  1246. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1247. "XEN PAGETABLES");
  1248. return pgd;
  1249. }
  1250. #else /* !CONFIG_X86_64 */
  1251. static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
  1252. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1253. {
  1254. pmd_t *kernel_pmd;
  1255. init_pg_tables_start = __pa(pgd);
  1256. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  1257. max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
  1258. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1259. memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1260. xen_map_identity_early(level2_kernel_pgt, max_pfn);
  1261. memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1262. set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
  1263. __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
  1264. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1265. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1266. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1267. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1268. xen_write_cr3(__pa(swapper_pg_dir));
  1269. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
  1270. return swapper_pg_dir;
  1271. }
  1272. #endif /* CONFIG_X86_64 */
  1273. /* First C function to be called on Xen boot */
  1274. asmlinkage void __init xen_start_kernel(void)
  1275. {
  1276. pgd_t *pgd;
  1277. if (!xen_start_info)
  1278. return;
  1279. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  1280. xen_setup_features();
  1281. /* Install Xen paravirt ops */
  1282. pv_info = xen_info;
  1283. pv_init_ops = xen_init_ops;
  1284. pv_time_ops = xen_time_ops;
  1285. pv_cpu_ops = xen_cpu_ops;
  1286. pv_irq_ops = xen_irq_ops;
  1287. pv_apic_ops = xen_apic_ops;
  1288. pv_mmu_ops = xen_mmu_ops;
  1289. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1290. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1291. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1292. }
  1293. machine_ops = xen_machine_ops;
  1294. #ifdef CONFIG_X86_64
  1295. /* Disable until direct per-cpu data access. */
  1296. have_vcpu_info_placement = 0;
  1297. x86_64_init_pda();
  1298. #endif
  1299. xen_smp_init();
  1300. /* Get mfn list */
  1301. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1302. xen_build_dynamic_phys_to_machine();
  1303. pgd = (pgd_t *)xen_start_info->pt_base;
  1304. /* Prevent unwanted bits from being set in PTEs. */
  1305. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1306. if (!is_initial_xendomain())
  1307. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1308. /* Don't do the full vcpu_info placement stuff until we have a
  1309. possible map and a non-dummy shared_info. */
  1310. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1311. xen_raw_console_write("mapping kernel into physical memory\n");
  1312. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1313. init_mm.pgd = pgd;
  1314. /* keep using Xen gdt for now; no urgent need to change it */
  1315. pv_info.kernel_rpl = 1;
  1316. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1317. pv_info.kernel_rpl = 0;
  1318. /* set the limit of our address space */
  1319. xen_reserve_top();
  1320. #ifdef CONFIG_X86_32
  1321. /* set up basic CPUID stuff */
  1322. cpu_detect(&new_cpu_data);
  1323. new_cpu_data.hard_math = 1;
  1324. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1325. #endif
  1326. /* Poke various useful things into boot_params */
  1327. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1328. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1329. ? __pa(xen_start_info->mod_start) : 0;
  1330. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1331. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1332. if (!is_initial_xendomain()) {
  1333. add_preferred_console("xenboot", 0, NULL);
  1334. add_preferred_console("tty", 0, NULL);
  1335. add_preferred_console("hvc", 0, NULL);
  1336. }
  1337. xen_raw_console_write("about to get started...\n");
  1338. #if 0
  1339. xen_raw_printk("&boot_params=%p __pa(&boot_params)=%lx __va(__pa(&boot_params))=%lx\n",
  1340. &boot_params, __pa_symbol(&boot_params),
  1341. __va(__pa_symbol(&boot_params)));
  1342. walk(pgd, &boot_params);
  1343. walk(pgd, __va(__pa(&boot_params)));
  1344. #endif
  1345. /* Start the world */
  1346. #ifdef CONFIG_X86_32
  1347. i386_start_kernel();
  1348. #else
  1349. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1350. #endif
  1351. }