lguest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Lguest specific paravirt-ops implementation
  3. *
  4. * Copyright (C) 2006, Rusty Russell <rusty@rustcorp.com.au> IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/start_kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/console.h>
  25. #include <linux/screen_info.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/lguest.h>
  29. #include <linux/lguest_launcher.h>
  30. #include <linux/lguest_bus.h>
  31. #include <asm/paravirt.h>
  32. #include <asm/param.h>
  33. #include <asm/page.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/desc.h>
  36. #include <asm/setup.h>
  37. #include <asm/e820.h>
  38. #include <asm/mce.h>
  39. #include <asm/io.h>
  40. /* Declarations for definitions in lguest_guest.S */
  41. extern char lguest_noirq_start[], lguest_noirq_end[];
  42. extern const char lgstart_cli[], lgend_cli[];
  43. extern const char lgstart_sti[], lgend_sti[];
  44. extern const char lgstart_popf[], lgend_popf[];
  45. extern const char lgstart_pushf[], lgend_pushf[];
  46. extern const char lgstart_iret[], lgend_iret[];
  47. extern void lguest_iret(void);
  48. struct lguest_data lguest_data = {
  49. .hcall_status = { [0 ... LHCALL_RING_SIZE-1] = 0xFF },
  50. .noirq_start = (u32)lguest_noirq_start,
  51. .noirq_end = (u32)lguest_noirq_end,
  52. .blocked_interrupts = { 1 }, /* Block timer interrupts */
  53. };
  54. struct lguest_device_desc *lguest_devices;
  55. static __initdata const struct lguest_boot_info *boot = __va(0);
  56. static enum paravirt_lazy_mode lazy_mode;
  57. static void lguest_lazy_mode(enum paravirt_lazy_mode mode)
  58. {
  59. if (mode == PARAVIRT_LAZY_FLUSH) {
  60. if (unlikely(lazy_mode != PARAVIRT_LAZY_NONE))
  61. hcall(LHCALL_FLUSH_ASYNC, 0, 0, 0);
  62. } else {
  63. lazy_mode = mode;
  64. if (mode == PARAVIRT_LAZY_NONE)
  65. hcall(LHCALL_FLUSH_ASYNC, 0, 0, 0);
  66. }
  67. }
  68. static void lazy_hcall(unsigned long call,
  69. unsigned long arg1,
  70. unsigned long arg2,
  71. unsigned long arg3)
  72. {
  73. if (lazy_mode == PARAVIRT_LAZY_NONE)
  74. hcall(call, arg1, arg2, arg3);
  75. else
  76. async_hcall(call, arg1, arg2, arg3);
  77. }
  78. void async_hcall(unsigned long call,
  79. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  80. {
  81. /* Note: This code assumes we're uniprocessor. */
  82. static unsigned int next_call;
  83. unsigned long flags;
  84. local_irq_save(flags);
  85. if (lguest_data.hcall_status[next_call] != 0xFF) {
  86. /* Table full, so do normal hcall which will flush table. */
  87. hcall(call, arg1, arg2, arg3);
  88. } else {
  89. lguest_data.hcalls[next_call].eax = call;
  90. lguest_data.hcalls[next_call].edx = arg1;
  91. lguest_data.hcalls[next_call].ebx = arg2;
  92. lguest_data.hcalls[next_call].ecx = arg3;
  93. /* Make sure host sees arguments before "valid" flag. */
  94. wmb();
  95. lguest_data.hcall_status[next_call] = 0;
  96. if (++next_call == LHCALL_RING_SIZE)
  97. next_call = 0;
  98. }
  99. local_irq_restore(flags);
  100. }
  101. void lguest_send_dma(unsigned long key, struct lguest_dma *dma)
  102. {
  103. dma->used_len = 0;
  104. hcall(LHCALL_SEND_DMA, key, __pa(dma), 0);
  105. }
  106. int lguest_bind_dma(unsigned long key, struct lguest_dma *dmas,
  107. unsigned int num, u8 irq)
  108. {
  109. if (!hcall(LHCALL_BIND_DMA, key, __pa(dmas), (num << 8) | irq))
  110. return -ENOMEM;
  111. return 0;
  112. }
  113. void lguest_unbind_dma(unsigned long key, struct lguest_dma *dmas)
  114. {
  115. hcall(LHCALL_BIND_DMA, key, __pa(dmas), 0);
  116. }
  117. /* For guests, device memory can be used as normal memory, so we cast away the
  118. * __iomem to quieten sparse. */
  119. void *lguest_map(unsigned long phys_addr, unsigned long pages)
  120. {
  121. return (__force void *)ioremap(phys_addr, PAGE_SIZE*pages);
  122. }
  123. void lguest_unmap(void *addr)
  124. {
  125. iounmap((__force void __iomem *)addr);
  126. }
  127. static unsigned long save_fl(void)
  128. {
  129. return lguest_data.irq_enabled;
  130. }
  131. static void restore_fl(unsigned long flags)
  132. {
  133. /* FIXME: Check if interrupt pending... */
  134. lguest_data.irq_enabled = flags;
  135. }
  136. static void irq_disable(void)
  137. {
  138. lguest_data.irq_enabled = 0;
  139. }
  140. static void irq_enable(void)
  141. {
  142. /* FIXME: Check if interrupt pending... */
  143. lguest_data.irq_enabled = X86_EFLAGS_IF;
  144. }
  145. static void lguest_write_idt_entry(struct desc_struct *dt,
  146. int entrynum, u32 low, u32 high)
  147. {
  148. write_dt_entry(dt, entrynum, low, high);
  149. hcall(LHCALL_LOAD_IDT_ENTRY, entrynum, low, high);
  150. }
  151. static void lguest_load_idt(const struct Xgt_desc_struct *desc)
  152. {
  153. unsigned int i;
  154. struct desc_struct *idt = (void *)desc->address;
  155. for (i = 0; i < (desc->size+1)/8; i++)
  156. hcall(LHCALL_LOAD_IDT_ENTRY, i, idt[i].a, idt[i].b);
  157. }
  158. static void lguest_load_gdt(const struct Xgt_desc_struct *desc)
  159. {
  160. BUG_ON((desc->size+1)/8 != GDT_ENTRIES);
  161. hcall(LHCALL_LOAD_GDT, __pa(desc->address), GDT_ENTRIES, 0);
  162. }
  163. static void lguest_write_gdt_entry(struct desc_struct *dt,
  164. int entrynum, u32 low, u32 high)
  165. {
  166. write_dt_entry(dt, entrynum, low, high);
  167. hcall(LHCALL_LOAD_GDT, __pa(dt), GDT_ENTRIES, 0);
  168. }
  169. static void lguest_load_tls(struct thread_struct *t, unsigned int cpu)
  170. {
  171. lazy_hcall(LHCALL_LOAD_TLS, __pa(&t->tls_array), cpu, 0);
  172. }
  173. static void lguest_set_ldt(const void *addr, unsigned entries)
  174. {
  175. }
  176. static void lguest_load_tr_desc(void)
  177. {
  178. }
  179. static void lguest_cpuid(unsigned int *eax, unsigned int *ebx,
  180. unsigned int *ecx, unsigned int *edx)
  181. {
  182. int function = *eax;
  183. native_cpuid(eax, ebx, ecx, edx);
  184. switch (function) {
  185. case 1: /* Basic feature request. */
  186. /* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */
  187. *ecx &= 0x00002201;
  188. /* Similarly: SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, FPU. */
  189. *edx &= 0x07808101;
  190. /* Host wants to know when we flush kernel pages: set PGE. */
  191. *edx |= 0x00002000;
  192. break;
  193. case 0x80000000:
  194. /* Futureproof this a little: if they ask how much extended
  195. * processor information, limit it to known fields. */
  196. if (*eax > 0x80000008)
  197. *eax = 0x80000008;
  198. break;
  199. }
  200. }
  201. static unsigned long current_cr0, current_cr3;
  202. static void lguest_write_cr0(unsigned long val)
  203. {
  204. lazy_hcall(LHCALL_TS, val & 8, 0, 0);
  205. current_cr0 = val;
  206. }
  207. static unsigned long lguest_read_cr0(void)
  208. {
  209. return current_cr0;
  210. }
  211. static void lguest_clts(void)
  212. {
  213. lazy_hcall(LHCALL_TS, 0, 0, 0);
  214. current_cr0 &= ~8U;
  215. }
  216. static unsigned long lguest_read_cr2(void)
  217. {
  218. return lguest_data.cr2;
  219. }
  220. static void lguest_write_cr3(unsigned long cr3)
  221. {
  222. lazy_hcall(LHCALL_NEW_PGTABLE, cr3, 0, 0);
  223. current_cr3 = cr3;
  224. }
  225. static unsigned long lguest_read_cr3(void)
  226. {
  227. return current_cr3;
  228. }
  229. /* Used to enable/disable PGE, but we don't care. */
  230. static unsigned long lguest_read_cr4(void)
  231. {
  232. return 0;
  233. }
  234. static void lguest_write_cr4(unsigned long val)
  235. {
  236. }
  237. static void lguest_set_pte_at(struct mm_struct *mm, unsigned long addr,
  238. pte_t *ptep, pte_t pteval)
  239. {
  240. *ptep = pteval;
  241. lazy_hcall(LHCALL_SET_PTE, __pa(mm->pgd), addr, pteval.pte_low);
  242. }
  243. /* We only support two-level pagetables at the moment. */
  244. static void lguest_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  245. {
  246. *pmdp = pmdval;
  247. lazy_hcall(LHCALL_SET_PMD, __pa(pmdp)&PAGE_MASK,
  248. (__pa(pmdp)&(PAGE_SIZE-1))/4, 0);
  249. }
  250. /* FIXME: Eliminate all callers of this. */
  251. static void lguest_set_pte(pte_t *ptep, pte_t pteval)
  252. {
  253. *ptep = pteval;
  254. /* Don't bother with hypercall before initial setup. */
  255. if (current_cr3)
  256. lazy_hcall(LHCALL_FLUSH_TLB, 1, 0, 0);
  257. }
  258. static void lguest_flush_tlb_single(unsigned long addr)
  259. {
  260. /* Simply set it to zero, and it will fault back in. */
  261. lazy_hcall(LHCALL_SET_PTE, current_cr3, addr, 0);
  262. }
  263. static void lguest_flush_tlb_user(void)
  264. {
  265. lazy_hcall(LHCALL_FLUSH_TLB, 0, 0, 0);
  266. }
  267. static void lguest_flush_tlb_kernel(void)
  268. {
  269. lazy_hcall(LHCALL_FLUSH_TLB, 1, 0, 0);
  270. }
  271. static void disable_lguest_irq(unsigned int irq)
  272. {
  273. set_bit(irq, lguest_data.blocked_interrupts);
  274. }
  275. static void enable_lguest_irq(unsigned int irq)
  276. {
  277. clear_bit(irq, lguest_data.blocked_interrupts);
  278. /* FIXME: If it's pending? */
  279. }
  280. static struct irq_chip lguest_irq_controller = {
  281. .name = "lguest",
  282. .mask = disable_lguest_irq,
  283. .mask_ack = disable_lguest_irq,
  284. .unmask = enable_lguest_irq,
  285. };
  286. static void __init lguest_init_IRQ(void)
  287. {
  288. unsigned int i;
  289. for (i = 0; i < LGUEST_IRQS; i++) {
  290. int vector = FIRST_EXTERNAL_VECTOR + i;
  291. if (vector != SYSCALL_VECTOR) {
  292. set_intr_gate(vector, interrupt[i]);
  293. set_irq_chip_and_handler(i, &lguest_irq_controller,
  294. handle_level_irq);
  295. }
  296. }
  297. irq_ctx_init(smp_processor_id());
  298. }
  299. static unsigned long lguest_get_wallclock(void)
  300. {
  301. return hcall(LHCALL_GET_WALLCLOCK, 0, 0, 0);
  302. }
  303. static void lguest_time_irq(unsigned int irq, struct irq_desc *desc)
  304. {
  305. do_timer(hcall(LHCALL_TIMER_READ, 0, 0, 0));
  306. update_process_times(user_mode_vm(get_irq_regs()));
  307. }
  308. static u64 sched_clock_base;
  309. static void lguest_time_init(void)
  310. {
  311. set_irq_handler(0, lguest_time_irq);
  312. hcall(LHCALL_TIMER_READ, 0, 0, 0);
  313. sched_clock_base = jiffies_64;
  314. enable_lguest_irq(0);
  315. }
  316. static unsigned long long lguest_sched_clock(void)
  317. {
  318. return (jiffies_64 - sched_clock_base) * (1000000000 / HZ);
  319. }
  320. static void lguest_load_esp0(struct tss_struct *tss,
  321. struct thread_struct *thread)
  322. {
  323. lazy_hcall(LHCALL_SET_STACK, __KERNEL_DS|0x1, thread->esp0,
  324. THREAD_SIZE/PAGE_SIZE);
  325. }
  326. static void lguest_set_debugreg(int regno, unsigned long value)
  327. {
  328. /* FIXME: Implement */
  329. }
  330. static void lguest_wbinvd(void)
  331. {
  332. }
  333. #ifdef CONFIG_X86_LOCAL_APIC
  334. static void lguest_apic_write(unsigned long reg, unsigned long v)
  335. {
  336. }
  337. static unsigned long lguest_apic_read(unsigned long reg)
  338. {
  339. return 0;
  340. }
  341. #endif
  342. static void lguest_safe_halt(void)
  343. {
  344. hcall(LHCALL_HALT, 0, 0, 0);
  345. }
  346. static void lguest_power_off(void)
  347. {
  348. hcall(LHCALL_CRASH, __pa("Power down"), 0, 0);
  349. }
  350. static int lguest_panic(struct notifier_block *nb, unsigned long l, void *p)
  351. {
  352. hcall(LHCALL_CRASH, __pa(p), 0, 0);
  353. return NOTIFY_DONE;
  354. }
  355. static struct notifier_block paniced = {
  356. .notifier_call = lguest_panic
  357. };
  358. static __init char *lguest_memory_setup(void)
  359. {
  360. /* We do this here because lockcheck barfs if before start_kernel */
  361. atomic_notifier_chain_register(&panic_notifier_list, &paniced);
  362. e820.nr_map = 0;
  363. add_memory_region(0, PFN_PHYS(boot->max_pfn), E820_RAM);
  364. return "LGUEST";
  365. }
  366. static const struct lguest_insns
  367. {
  368. const char *start, *end;
  369. } lguest_insns[] = {
  370. [PARAVIRT_PATCH(irq_disable)] = { lgstart_cli, lgend_cli },
  371. [PARAVIRT_PATCH(irq_enable)] = { lgstart_sti, lgend_sti },
  372. [PARAVIRT_PATCH(restore_fl)] = { lgstart_popf, lgend_popf },
  373. [PARAVIRT_PATCH(save_fl)] = { lgstart_pushf, lgend_pushf },
  374. };
  375. static unsigned lguest_patch(u8 type, u16 clobber, void *insns, unsigned len)
  376. {
  377. unsigned int insn_len;
  378. /* Don't touch it if we don't have a replacement */
  379. if (type >= ARRAY_SIZE(lguest_insns) || !lguest_insns[type].start)
  380. return paravirt_patch_default(type, clobber, insns, len);
  381. insn_len = lguest_insns[type].end - lguest_insns[type].start;
  382. /* Similarly if we can't fit replacement. */
  383. if (len < insn_len)
  384. return paravirt_patch_default(type, clobber, insns, len);
  385. memcpy(insns, lguest_insns[type].start, insn_len);
  386. return insn_len;
  387. }
  388. __init void lguest_init(void)
  389. {
  390. paravirt_ops.name = "lguest";
  391. paravirt_ops.paravirt_enabled = 1;
  392. paravirt_ops.kernel_rpl = 1;
  393. paravirt_ops.save_fl = save_fl;
  394. paravirt_ops.restore_fl = restore_fl;
  395. paravirt_ops.irq_disable = irq_disable;
  396. paravirt_ops.irq_enable = irq_enable;
  397. paravirt_ops.load_gdt = lguest_load_gdt;
  398. paravirt_ops.memory_setup = lguest_memory_setup;
  399. paravirt_ops.cpuid = lguest_cpuid;
  400. paravirt_ops.write_cr3 = lguest_write_cr3;
  401. paravirt_ops.flush_tlb_user = lguest_flush_tlb_user;
  402. paravirt_ops.flush_tlb_single = lguest_flush_tlb_single;
  403. paravirt_ops.flush_tlb_kernel = lguest_flush_tlb_kernel;
  404. paravirt_ops.set_pte = lguest_set_pte;
  405. paravirt_ops.set_pte_at = lguest_set_pte_at;
  406. paravirt_ops.set_pmd = lguest_set_pmd;
  407. #ifdef CONFIG_X86_LOCAL_APIC
  408. paravirt_ops.apic_write = lguest_apic_write;
  409. paravirt_ops.apic_write_atomic = lguest_apic_write;
  410. paravirt_ops.apic_read = lguest_apic_read;
  411. #endif
  412. paravirt_ops.load_idt = lguest_load_idt;
  413. paravirt_ops.iret = lguest_iret;
  414. paravirt_ops.load_esp0 = lguest_load_esp0;
  415. paravirt_ops.load_tr_desc = lguest_load_tr_desc;
  416. paravirt_ops.set_ldt = lguest_set_ldt;
  417. paravirt_ops.load_tls = lguest_load_tls;
  418. paravirt_ops.set_debugreg = lguest_set_debugreg;
  419. paravirt_ops.clts = lguest_clts;
  420. paravirt_ops.read_cr0 = lguest_read_cr0;
  421. paravirt_ops.write_cr0 = lguest_write_cr0;
  422. paravirt_ops.init_IRQ = lguest_init_IRQ;
  423. paravirt_ops.read_cr2 = lguest_read_cr2;
  424. paravirt_ops.read_cr3 = lguest_read_cr3;
  425. paravirt_ops.read_cr4 = lguest_read_cr4;
  426. paravirt_ops.write_cr4 = lguest_write_cr4;
  427. paravirt_ops.write_gdt_entry = lguest_write_gdt_entry;
  428. paravirt_ops.write_idt_entry = lguest_write_idt_entry;
  429. paravirt_ops.patch = lguest_patch;
  430. paravirt_ops.safe_halt = lguest_safe_halt;
  431. paravirt_ops.get_wallclock = lguest_get_wallclock;
  432. paravirt_ops.time_init = lguest_time_init;
  433. paravirt_ops.set_lazy_mode = lguest_lazy_mode;
  434. paravirt_ops.wbinvd = lguest_wbinvd;
  435. paravirt_ops.sched_clock = lguest_sched_clock;
  436. hcall(LHCALL_LGUEST_INIT, __pa(&lguest_data), 0, 0);
  437. strncpy(boot_command_line, boot->cmdline, COMMAND_LINE_SIZE);
  438. /* We use top of mem for initial pagetables. */
  439. init_pg_tables_end = __pa(pg0);
  440. asm volatile ("mov %0, %%fs" : : "r" (__KERNEL_DS) : "memory");
  441. reserve_top_address(lguest_data.reserve_mem);
  442. lockdep_init();
  443. paravirt_disable_iospace();
  444. cpu_detect(&new_cpu_data);
  445. /* head.S usually sets up the first capability word, so do it here. */
  446. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  447. /* Math is always hard! */
  448. new_cpu_data.hard_math = 1;
  449. #ifdef CONFIG_X86_MCE
  450. mce_disabled = 1;
  451. #endif
  452. #ifdef CONFIG_ACPI
  453. acpi_disabled = 1;
  454. acpi_ht = 0;
  455. #endif
  456. add_preferred_console("hvc", 0, NULL);
  457. if (boot->initrd_size) {
  458. /* We stash this at top of memory. */
  459. INITRD_START = boot->max_pfn*PAGE_SIZE - boot->initrd_size;
  460. INITRD_SIZE = boot->initrd_size;
  461. LOADER_TYPE = 0xFF;
  462. }
  463. pm_power_off = lguest_power_off;
  464. start_kernel();
  465. }