vmi_32.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * VMI specific paravirt-ops implementation
  3. *
  4. * Copyright (C) 2005, VMware, Inc.
  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. * Send feedback to zach@vmware.com
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/cpu.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/mm.h>
  28. #include <linux/highmem.h>
  29. #include <linux/sched.h>
  30. #include <asm/vmi.h>
  31. #include <asm/io.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/apicdef.h>
  34. #include <asm/apic.h>
  35. #include <asm/processor.h>
  36. #include <asm/timer.h>
  37. #include <asm/vmi_time.h>
  38. #include <asm/kmap_types.h>
  39. #include <asm/setup.h>
  40. /* Convenient for calling VMI functions indirectly in the ROM */
  41. typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
  42. typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
  43. #define call_vrom_func(rom,func) \
  44. (((VROMFUNC *)(rom->func))())
  45. #define call_vrom_long_func(rom,func,arg) \
  46. (((VROMLONGFUNC *)(rom->func)) (arg))
  47. static struct vrom_header *vmi_rom;
  48. static int disable_pge;
  49. static int disable_pse;
  50. static int disable_sep;
  51. static int disable_tsc;
  52. static int disable_mtrr;
  53. static int disable_noidle;
  54. static int disable_vmi_timer;
  55. /* Cached VMI operations */
  56. static struct {
  57. void (*cpuid)(void /* non-c */);
  58. void (*_set_ldt)(u32 selector);
  59. void (*set_tr)(u32 selector);
  60. void (*write_idt_entry)(struct desc_struct *, int, u32, u32);
  61. void (*write_gdt_entry)(struct desc_struct *, int, u32, u32);
  62. void (*write_ldt_entry)(struct desc_struct *, int, u32, u32);
  63. void (*set_kernel_stack)(u32 selector, u32 sp0);
  64. void (*allocate_page)(u32, u32, u32, u32, u32);
  65. void (*release_page)(u32, u32);
  66. void (*set_pte)(pte_t, pte_t *, unsigned);
  67. void (*update_pte)(pte_t *, unsigned);
  68. void (*set_linear_mapping)(int, void *, u32, u32);
  69. void (*_flush_tlb)(int);
  70. void (*set_initial_ap_state)(int, int);
  71. void (*halt)(void);
  72. void (*set_lazy_mode)(int mode);
  73. } vmi_ops;
  74. /* Cached VMI operations */
  75. struct vmi_timer_ops vmi_timer_ops;
  76. /*
  77. * VMI patching routines.
  78. */
  79. #define MNEM_CALL 0xe8
  80. #define MNEM_JMP 0xe9
  81. #define MNEM_RET 0xc3
  82. #define IRQ_PATCH_INT_MASK 0
  83. #define IRQ_PATCH_DISABLE 5
  84. static inline void patch_offset(void *insnbuf,
  85. unsigned long ip, unsigned long dest)
  86. {
  87. *(unsigned long *)(insnbuf+1) = dest-ip-5;
  88. }
  89. static unsigned patch_internal(int call, unsigned len, void *insnbuf,
  90. unsigned long ip)
  91. {
  92. u64 reloc;
  93. struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
  94. reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
  95. switch(rel->type) {
  96. case VMI_RELOCATION_CALL_REL:
  97. BUG_ON(len < 5);
  98. *(char *)insnbuf = MNEM_CALL;
  99. patch_offset(insnbuf, ip, (unsigned long)rel->eip);
  100. return 5;
  101. case VMI_RELOCATION_JUMP_REL:
  102. BUG_ON(len < 5);
  103. *(char *)insnbuf = MNEM_JMP;
  104. patch_offset(insnbuf, ip, (unsigned long)rel->eip);
  105. return 5;
  106. case VMI_RELOCATION_NOP:
  107. /* obliterate the whole thing */
  108. return 0;
  109. case VMI_RELOCATION_NONE:
  110. /* leave native code in place */
  111. break;
  112. default:
  113. BUG();
  114. }
  115. return len;
  116. }
  117. /*
  118. * Apply patch if appropriate, return length of new instruction
  119. * sequence. The callee does nop padding for us.
  120. */
  121. static unsigned vmi_patch(u8 type, u16 clobbers, void *insns,
  122. unsigned long ip, unsigned len)
  123. {
  124. switch (type) {
  125. case PARAVIRT_PATCH(pv_irq_ops.irq_disable):
  126. return patch_internal(VMI_CALL_DisableInterrupts, len,
  127. insns, ip);
  128. case PARAVIRT_PATCH(pv_irq_ops.irq_enable):
  129. return patch_internal(VMI_CALL_EnableInterrupts, len,
  130. insns, ip);
  131. case PARAVIRT_PATCH(pv_irq_ops.restore_fl):
  132. return patch_internal(VMI_CALL_SetInterruptMask, len,
  133. insns, ip);
  134. case PARAVIRT_PATCH(pv_irq_ops.save_fl):
  135. return patch_internal(VMI_CALL_GetInterruptMask, len,
  136. insns, ip);
  137. case PARAVIRT_PATCH(pv_cpu_ops.iret):
  138. return patch_internal(VMI_CALL_IRET, len, insns, ip);
  139. case PARAVIRT_PATCH(pv_cpu_ops.irq_enable_sysexit):
  140. return patch_internal(VMI_CALL_SYSEXIT, len, insns, ip);
  141. default:
  142. break;
  143. }
  144. return len;
  145. }
  146. /* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
  147. static void vmi_cpuid(unsigned int *ax, unsigned int *bx,
  148. unsigned int *cx, unsigned int *dx)
  149. {
  150. int override = 0;
  151. if (*ax == 1)
  152. override = 1;
  153. asm volatile ("call *%6"
  154. : "=a" (*ax),
  155. "=b" (*bx),
  156. "=c" (*cx),
  157. "=d" (*dx)
  158. : "0" (*ax), "2" (*cx), "r" (vmi_ops.cpuid));
  159. if (override) {
  160. if (disable_pse)
  161. *dx &= ~X86_FEATURE_PSE;
  162. if (disable_pge)
  163. *dx &= ~X86_FEATURE_PGE;
  164. if (disable_sep)
  165. *dx &= ~X86_FEATURE_SEP;
  166. if (disable_tsc)
  167. *dx &= ~X86_FEATURE_TSC;
  168. if (disable_mtrr)
  169. *dx &= ~X86_FEATURE_MTRR;
  170. }
  171. }
  172. static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
  173. {
  174. if (gdt[nr].a != new->a || gdt[nr].b != new->b)
  175. write_gdt_entry(gdt, nr, new, 0);
  176. }
  177. static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
  178. {
  179. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  180. vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
  181. vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
  182. vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
  183. }
  184. static void vmi_set_ldt(const void *addr, unsigned entries)
  185. {
  186. unsigned cpu = smp_processor_id();
  187. struct desc_struct desc;
  188. pack_descriptor(&desc, (unsigned long)addr,
  189. entries * sizeof(struct desc_struct) - 1,
  190. DESC_LDT, 0);
  191. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, &desc, DESC_LDT);
  192. vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
  193. }
  194. static void vmi_set_tr(void)
  195. {
  196. vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
  197. }
  198. static void vmi_write_idt_entry(gate_desc *dt, int entry, const gate_desc *g)
  199. {
  200. u32 *idt_entry = (u32 *)g;
  201. vmi_ops.write_idt_entry(dt, entry, idt_entry[0], idt_entry[1]);
  202. }
  203. static void vmi_write_gdt_entry(struct desc_struct *dt, int entry,
  204. const void *desc, int type)
  205. {
  206. u32 *gdt_entry = (u32 *)desc;
  207. vmi_ops.write_gdt_entry(dt, entry, gdt_entry[0], gdt_entry[1]);
  208. }
  209. static void vmi_write_ldt_entry(struct desc_struct *dt, int entry,
  210. const void *desc)
  211. {
  212. u32 *ldt_entry = (u32 *)desc;
  213. vmi_ops.write_ldt_entry(dt, entry, ldt_entry[0], ldt_entry[1]);
  214. }
  215. static void vmi_load_sp0(struct tss_struct *tss,
  216. struct thread_struct *thread)
  217. {
  218. tss->x86_tss.sp0 = thread->sp0;
  219. /* This can only happen when SEP is enabled, no need to test "SEP"arately */
  220. if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
  221. tss->x86_tss.ss1 = thread->sysenter_cs;
  222. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  223. }
  224. vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.sp0);
  225. }
  226. static void vmi_flush_tlb_user(void)
  227. {
  228. vmi_ops._flush_tlb(VMI_FLUSH_TLB);
  229. }
  230. static void vmi_flush_tlb_kernel(void)
  231. {
  232. vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
  233. }
  234. /* Stub to do nothing at all; used for delays and unimplemented calls */
  235. static void vmi_nop(void)
  236. {
  237. }
  238. #ifdef CONFIG_HIGHPTE
  239. static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
  240. {
  241. void *va = kmap_atomic(page, type);
  242. /*
  243. * Internally, the VMI ROM must map virtual addresses to physical
  244. * addresses for processing MMU updates. By the time MMU updates
  245. * are issued, this information is typically already lost.
  246. * Fortunately, the VMI provides a cache of mapping slots for active
  247. * page tables.
  248. *
  249. * We use slot zero for the linear mapping of physical memory, and
  250. * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
  251. *
  252. * args: SLOT VA COUNT PFN
  253. */
  254. BUG_ON(type != KM_PTE0 && type != KM_PTE1);
  255. vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
  256. return va;
  257. }
  258. #endif
  259. static void vmi_allocate_pte(struct mm_struct *mm, unsigned long pfn)
  260. {
  261. vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
  262. }
  263. static void vmi_allocate_pmd(struct mm_struct *mm, unsigned long pfn)
  264. {
  265. /*
  266. * This call comes in very early, before mem_map is setup.
  267. * It is called only for swapper_pg_dir, which already has
  268. * data on it.
  269. */
  270. vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
  271. }
  272. static void vmi_allocate_pmd_clone(unsigned long pfn, unsigned long clonepfn, unsigned long start, unsigned long count)
  273. {
  274. vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
  275. }
  276. static void vmi_release_pte(unsigned long pfn)
  277. {
  278. vmi_ops.release_page(pfn, VMI_PAGE_L1);
  279. }
  280. static void vmi_release_pmd(unsigned long pfn)
  281. {
  282. vmi_ops.release_page(pfn, VMI_PAGE_L2);
  283. }
  284. /*
  285. * We use the pgd_free hook for releasing the pgd page:
  286. */
  287. static void vmi_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  288. {
  289. unsigned long pfn = __pa(pgd) >> PAGE_SHIFT;
  290. vmi_ops.release_page(pfn, VMI_PAGE_L2);
  291. }
  292. /*
  293. * Helper macros for MMU update flags. We can defer updates until a flush
  294. * or page invalidation only if the update is to the current address space
  295. * (otherwise, there is no flush). We must check against init_mm, since
  296. * this could be a kernel update, which usually passes init_mm, although
  297. * sometimes this check can be skipped if we know the particular function
  298. * is only called on user mode PTEs. We could change the kernel to pass
  299. * current->active_mm here, but in particular, I was unsure if changing
  300. * mm/highmem.c to do this would still be correct on other architectures.
  301. */
  302. #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
  303. (!mustbeuser && (mm) == &init_mm))
  304. #define vmi_flags_addr(mm, addr, level, user) \
  305. ((level) | (is_current_as(mm, user) ? \
  306. (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
  307. #define vmi_flags_addr_defer(mm, addr, level, user) \
  308. ((level) | (is_current_as(mm, user) ? \
  309. (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
  310. static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  311. {
  312. vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  313. }
  314. static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  315. {
  316. vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
  317. }
  318. static void vmi_set_pte(pte_t *ptep, pte_t pte)
  319. {
  320. /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
  321. vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
  322. }
  323. static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  324. {
  325. vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  326. }
  327. static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  328. {
  329. #ifdef CONFIG_X86_PAE
  330. const pte_t pte = { .pte = pmdval.pmd };
  331. #else
  332. const pte_t pte = { pmdval.pud.pgd.pgd };
  333. #endif
  334. vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
  335. }
  336. #ifdef CONFIG_X86_PAE
  337. static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
  338. {
  339. /*
  340. * XXX This is called from set_pmd_pte, but at both PT
  341. * and PD layers so the VMI_PAGE_PT flag is wrong. But
  342. * it is only called for large page mapping changes,
  343. * the Xen backend, doesn't support large pages, and the
  344. * ESX backend doesn't depend on the flag.
  345. */
  346. set_64bit((unsigned long long *)ptep,pte_val(pteval));
  347. vmi_ops.update_pte(ptep, VMI_PAGE_PT);
  348. }
  349. static void vmi_set_pud(pud_t *pudp, pud_t pudval)
  350. {
  351. /* Um, eww */
  352. const pte_t pte = { .pte = pudval.pgd.pgd };
  353. vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
  354. }
  355. static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  356. {
  357. const pte_t pte = { .pte = 0 };
  358. vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  359. }
  360. static void vmi_pmd_clear(pmd_t *pmd)
  361. {
  362. const pte_t pte = { .pte = 0 };
  363. vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
  364. }
  365. #endif
  366. #ifdef CONFIG_SMP
  367. static void __devinit
  368. vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  369. unsigned long start_esp)
  370. {
  371. struct vmi_ap_state ap;
  372. /* Default everything to zero. This is fine for most GPRs. */
  373. memset(&ap, 0, sizeof(struct vmi_ap_state));
  374. ap.gdtr_limit = GDT_SIZE - 1;
  375. ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
  376. ap.idtr_limit = IDT_ENTRIES * 8 - 1;
  377. ap.idtr_base = (unsigned long) idt_table;
  378. ap.ldtr = 0;
  379. ap.cs = __KERNEL_CS;
  380. ap.eip = (unsigned long) start_eip;
  381. ap.ss = __KERNEL_DS;
  382. ap.esp = (unsigned long) start_esp;
  383. ap.ds = __USER_DS;
  384. ap.es = __USER_DS;
  385. ap.fs = __KERNEL_PERCPU;
  386. ap.gs = 0;
  387. ap.eflags = 0;
  388. #ifdef CONFIG_X86_PAE
  389. /* efer should match BSP efer. */
  390. if (cpu_has_nx) {
  391. unsigned l, h;
  392. rdmsr(MSR_EFER, l, h);
  393. ap.efer = (unsigned long long) h << 32 | l;
  394. }
  395. #endif
  396. ap.cr3 = __pa(swapper_pg_dir);
  397. /* Protected mode, paging, AM, WP, NE, MP. */
  398. ap.cr0 = 0x80050023;
  399. ap.cr4 = mmu_cr4_features;
  400. vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
  401. }
  402. #endif
  403. static void vmi_enter_lazy_cpu(void)
  404. {
  405. paravirt_enter_lazy_cpu();
  406. vmi_ops.set_lazy_mode(2);
  407. }
  408. static void vmi_enter_lazy_mmu(void)
  409. {
  410. paravirt_enter_lazy_mmu();
  411. vmi_ops.set_lazy_mode(1);
  412. }
  413. static void vmi_leave_lazy(void)
  414. {
  415. paravirt_leave_lazy(paravirt_get_lazy_mode());
  416. vmi_ops.set_lazy_mode(0);
  417. }
  418. static inline int __init check_vmi_rom(struct vrom_header *rom)
  419. {
  420. struct pci_header *pci;
  421. struct pnp_header *pnp;
  422. const char *manufacturer = "UNKNOWN";
  423. const char *product = "UNKNOWN";
  424. const char *license = "unspecified";
  425. if (rom->rom_signature != 0xaa55)
  426. return 0;
  427. if (rom->vrom_signature != VMI_SIGNATURE)
  428. return 0;
  429. if (rom->api_version_maj != VMI_API_REV_MAJOR ||
  430. rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
  431. printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
  432. rom->api_version_maj,
  433. rom->api_version_min);
  434. return 0;
  435. }
  436. /*
  437. * Relying on the VMI_SIGNATURE field is not 100% safe, so check
  438. * the PCI header and device type to make sure this is really a
  439. * VMI device.
  440. */
  441. if (!rom->pci_header_offs) {
  442. printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
  443. return 0;
  444. }
  445. pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
  446. if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
  447. pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
  448. /* Allow it to run... anyways, but warn */
  449. printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
  450. }
  451. if (rom->pnp_header_offs) {
  452. pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
  453. if (pnp->manufacturer_offset)
  454. manufacturer = (const char *)rom+pnp->manufacturer_offset;
  455. if (pnp->product_offset)
  456. product = (const char *)rom+pnp->product_offset;
  457. }
  458. if (rom->license_offs)
  459. license = (char *)rom+rom->license_offs;
  460. printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
  461. manufacturer, product,
  462. rom->api_version_maj, rom->api_version_min,
  463. pci->rom_version_maj, pci->rom_version_min);
  464. /* Don't allow BSD/MIT here for now because we don't want to end up
  465. with any binary only shim layers */
  466. if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
  467. printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
  468. license);
  469. return 0;
  470. }
  471. return 1;
  472. }
  473. /*
  474. * Probe for the VMI option ROM
  475. */
  476. static inline int __init probe_vmi_rom(void)
  477. {
  478. unsigned long base;
  479. /* VMI ROM is in option ROM area, check signature */
  480. for (base = 0xC0000; base < 0xE0000; base += 2048) {
  481. struct vrom_header *romstart;
  482. romstart = (struct vrom_header *)isa_bus_to_virt(base);
  483. if (check_vmi_rom(romstart)) {
  484. vmi_rom = romstart;
  485. return 1;
  486. }
  487. }
  488. return 0;
  489. }
  490. /*
  491. * VMI setup common to all processors
  492. */
  493. void vmi_bringup(void)
  494. {
  495. /* We must establish the lowmem mapping for MMU ops to work */
  496. if (vmi_ops.set_linear_mapping)
  497. vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, MAXMEM_PFN, 0);
  498. }
  499. /*
  500. * Return a pointer to a VMI function or NULL if unimplemented
  501. */
  502. static void *vmi_get_function(int vmicall)
  503. {
  504. u64 reloc;
  505. const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
  506. reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
  507. BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
  508. if (rel->type == VMI_RELOCATION_CALL_REL)
  509. return (void *)rel->eip;
  510. else
  511. return NULL;
  512. }
  513. /*
  514. * Helper macro for making the VMI paravirt-ops fill code readable.
  515. * For unimplemented operations, fall back to default, unless nop
  516. * is returned by the ROM.
  517. */
  518. #define para_fill(opname, vmicall) \
  519. do { \
  520. reloc = call_vrom_long_func(vmi_rom, get_reloc, \
  521. VMI_CALL_##vmicall); \
  522. if (rel->type == VMI_RELOCATION_CALL_REL) \
  523. opname = (void *)rel->eip; \
  524. else if (rel->type == VMI_RELOCATION_NOP) \
  525. opname = (void *)vmi_nop; \
  526. else if (rel->type != VMI_RELOCATION_NONE) \
  527. printk(KERN_WARNING "VMI: Unknown relocation " \
  528. "type %d for " #vmicall"\n",\
  529. rel->type); \
  530. } while (0)
  531. /*
  532. * Helper macro for making the VMI paravirt-ops fill code readable.
  533. * For cached operations which do not match the VMI ROM ABI and must
  534. * go through a tranlation stub. Ignore NOPs, since it is not clear
  535. * a NOP * VMI function corresponds to a NOP paravirt-op when the
  536. * functions are not in 1-1 correspondence.
  537. */
  538. #define para_wrap(opname, wrapper, cache, vmicall) \
  539. do { \
  540. reloc = call_vrom_long_func(vmi_rom, get_reloc, \
  541. VMI_CALL_##vmicall); \
  542. BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
  543. if (rel->type == VMI_RELOCATION_CALL_REL) { \
  544. opname = wrapper; \
  545. vmi_ops.cache = (void *)rel->eip; \
  546. } \
  547. } while (0)
  548. /*
  549. * Activate the VMI interface and switch into paravirtualized mode
  550. */
  551. static inline int __init activate_vmi(void)
  552. {
  553. short kernel_cs;
  554. u64 reloc;
  555. const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
  556. if (call_vrom_func(vmi_rom, vmi_init) != 0) {
  557. printk(KERN_ERR "VMI ROM failed to initialize!");
  558. return 0;
  559. }
  560. savesegment(cs, kernel_cs);
  561. pv_info.paravirt_enabled = 1;
  562. pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
  563. pv_info.name = "vmi";
  564. pv_init_ops.patch = vmi_patch;
  565. /*
  566. * Many of these operations are ABI compatible with VMI.
  567. * This means we can fill in the paravirt-ops with direct
  568. * pointers into the VMI ROM. If the calling convention for
  569. * these operations changes, this code needs to be updated.
  570. *
  571. * Exceptions
  572. * CPUID paravirt-op uses pointers, not the native ISA
  573. * halt has no VMI equivalent; all VMI halts are "safe"
  574. * no MSR support yet - just trap and emulate. VMI uses the
  575. * same ABI as the native ISA, but Linux wants exceptions
  576. * from bogus MSR read / write handled
  577. * rdpmc is not yet used in Linux
  578. */
  579. /* CPUID is special, so very special it gets wrapped like a present */
  580. para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
  581. para_fill(pv_cpu_ops.clts, CLTS);
  582. para_fill(pv_cpu_ops.get_debugreg, GetDR);
  583. para_fill(pv_cpu_ops.set_debugreg, SetDR);
  584. para_fill(pv_cpu_ops.read_cr0, GetCR0);
  585. para_fill(pv_mmu_ops.read_cr2, GetCR2);
  586. para_fill(pv_mmu_ops.read_cr3, GetCR3);
  587. para_fill(pv_cpu_ops.read_cr4, GetCR4);
  588. para_fill(pv_cpu_ops.write_cr0, SetCR0);
  589. para_fill(pv_mmu_ops.write_cr2, SetCR2);
  590. para_fill(pv_mmu_ops.write_cr3, SetCR3);
  591. para_fill(pv_cpu_ops.write_cr4, SetCR4);
  592. para_fill(pv_irq_ops.save_fl.func, GetInterruptMask);
  593. para_fill(pv_irq_ops.restore_fl.func, SetInterruptMask);
  594. para_fill(pv_irq_ops.irq_disable.func, DisableInterrupts);
  595. para_fill(pv_irq_ops.irq_enable.func, EnableInterrupts);
  596. para_fill(pv_cpu_ops.wbinvd, WBINVD);
  597. para_fill(pv_cpu_ops.read_tsc, RDTSC);
  598. /* The following we emulate with trap and emulate for now */
  599. /* paravirt_ops.read_msr = vmi_rdmsr */
  600. /* paravirt_ops.write_msr = vmi_wrmsr */
  601. /* paravirt_ops.rdpmc = vmi_rdpmc */
  602. /* TR interface doesn't pass TR value, wrap */
  603. para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
  604. /* LDT is special, too */
  605. para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
  606. para_fill(pv_cpu_ops.load_gdt, SetGDT);
  607. para_fill(pv_cpu_ops.load_idt, SetIDT);
  608. para_fill(pv_cpu_ops.store_gdt, GetGDT);
  609. para_fill(pv_cpu_ops.store_idt, GetIDT);
  610. para_fill(pv_cpu_ops.store_tr, GetTR);
  611. pv_cpu_ops.load_tls = vmi_load_tls;
  612. para_wrap(pv_cpu_ops.write_ldt_entry, vmi_write_ldt_entry,
  613. write_ldt_entry, WriteLDTEntry);
  614. para_wrap(pv_cpu_ops.write_gdt_entry, vmi_write_gdt_entry,
  615. write_gdt_entry, WriteGDTEntry);
  616. para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
  617. write_idt_entry, WriteIDTEntry);
  618. para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
  619. para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
  620. para_fill(pv_cpu_ops.io_delay, IODelay);
  621. para_wrap(pv_cpu_ops.lazy_mode.enter, vmi_enter_lazy_cpu,
  622. set_lazy_mode, SetLazyMode);
  623. para_wrap(pv_cpu_ops.lazy_mode.leave, vmi_leave_lazy,
  624. set_lazy_mode, SetLazyMode);
  625. para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
  626. set_lazy_mode, SetLazyMode);
  627. para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy,
  628. set_lazy_mode, SetLazyMode);
  629. /* user and kernel flush are just handled with different flags to FlushTLB */
  630. para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
  631. para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
  632. para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
  633. /*
  634. * Until a standard flag format can be agreed on, we need to
  635. * implement these as wrappers in Linux. Get the VMI ROM
  636. * function pointers for the two backend calls.
  637. */
  638. #ifdef CONFIG_X86_PAE
  639. vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
  640. vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
  641. #else
  642. vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
  643. vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
  644. #endif
  645. if (vmi_ops.set_pte) {
  646. pv_mmu_ops.set_pte = vmi_set_pte;
  647. pv_mmu_ops.set_pte_at = vmi_set_pte_at;
  648. pv_mmu_ops.set_pmd = vmi_set_pmd;
  649. #ifdef CONFIG_X86_PAE
  650. pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
  651. pv_mmu_ops.set_pud = vmi_set_pud;
  652. pv_mmu_ops.pte_clear = vmi_pte_clear;
  653. pv_mmu_ops.pmd_clear = vmi_pmd_clear;
  654. #endif
  655. }
  656. if (vmi_ops.update_pte) {
  657. pv_mmu_ops.pte_update = vmi_update_pte;
  658. pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
  659. }
  660. vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
  661. if (vmi_ops.allocate_page) {
  662. pv_mmu_ops.alloc_pte = vmi_allocate_pte;
  663. pv_mmu_ops.alloc_pmd = vmi_allocate_pmd;
  664. pv_mmu_ops.alloc_pmd_clone = vmi_allocate_pmd_clone;
  665. }
  666. vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
  667. if (vmi_ops.release_page) {
  668. pv_mmu_ops.release_pte = vmi_release_pte;
  669. pv_mmu_ops.release_pmd = vmi_release_pmd;
  670. pv_mmu_ops.pgd_free = vmi_pgd_free;
  671. }
  672. /* Set linear is needed in all cases */
  673. vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
  674. #ifdef CONFIG_HIGHPTE
  675. if (vmi_ops.set_linear_mapping)
  676. pv_mmu_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
  677. #endif
  678. /*
  679. * These MUST always be patched. Don't support indirect jumps
  680. * through these operations, as the VMI interface may use either
  681. * a jump or a call to get to these operations, depending on
  682. * the backend. They are performance critical anyway, so requiring
  683. * a patch is not a big problem.
  684. */
  685. pv_cpu_ops.irq_enable_sysexit = (void *)0xfeedbab0;
  686. pv_cpu_ops.iret = (void *)0xbadbab0;
  687. #ifdef CONFIG_SMP
  688. para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
  689. #endif
  690. #ifdef CONFIG_X86_LOCAL_APIC
  691. para_fill(apic->read, APICRead);
  692. para_fill(apic->write, APICWrite);
  693. #endif
  694. /*
  695. * Check for VMI timer functionality by probing for a cycle frequency method
  696. */
  697. reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
  698. if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
  699. vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
  700. vmi_timer_ops.get_cycle_counter =
  701. vmi_get_function(VMI_CALL_GetCycleCounter);
  702. vmi_timer_ops.get_wallclock =
  703. vmi_get_function(VMI_CALL_GetWallclockTime);
  704. vmi_timer_ops.wallclock_updated =
  705. vmi_get_function(VMI_CALL_WallclockUpdated);
  706. vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
  707. vmi_timer_ops.cancel_alarm =
  708. vmi_get_function(VMI_CALL_CancelAlarm);
  709. pv_time_ops.time_init = vmi_time_init;
  710. pv_time_ops.get_wallclock = vmi_get_wallclock;
  711. pv_time_ops.set_wallclock = vmi_set_wallclock;
  712. #ifdef CONFIG_X86_LOCAL_APIC
  713. pv_apic_ops.setup_boot_clock = vmi_time_bsp_init;
  714. pv_apic_ops.setup_secondary_clock = vmi_time_ap_init;
  715. #endif
  716. pv_time_ops.sched_clock = vmi_sched_clock;
  717. pv_time_ops.get_tsc_khz = vmi_tsc_khz;
  718. /* We have true wallclock functions; disable CMOS clock sync */
  719. no_sync_cmos_clock = 1;
  720. } else {
  721. disable_noidle = 1;
  722. disable_vmi_timer = 1;
  723. }
  724. para_fill(pv_irq_ops.safe_halt, Halt);
  725. /*
  726. * Alternative instruction rewriting doesn't happen soon enough
  727. * to convert VMI_IRET to a call instead of a jump; so we have
  728. * to do this before IRQs get reenabled. Fortunately, it is
  729. * idempotent.
  730. */
  731. apply_paravirt(__parainstructions, __parainstructions_end);
  732. vmi_bringup();
  733. return 1;
  734. }
  735. #undef para_fill
  736. void __init vmi_init(void)
  737. {
  738. if (!vmi_rom)
  739. probe_vmi_rom();
  740. else
  741. check_vmi_rom(vmi_rom);
  742. /* In case probing for or validating the ROM failed, basil */
  743. if (!vmi_rom)
  744. return;
  745. reserve_top_address(-vmi_rom->virtual_top);
  746. #ifdef CONFIG_X86_IO_APIC
  747. /* This is virtual hardware; timer routing is wired correctly */
  748. no_timer_check = 1;
  749. #endif
  750. }
  751. void __init vmi_activate(void)
  752. {
  753. unsigned long flags;
  754. if (!vmi_rom)
  755. return;
  756. local_irq_save(flags);
  757. activate_vmi();
  758. local_irq_restore(flags & X86_EFLAGS_IF);
  759. }
  760. static int __init parse_vmi(char *arg)
  761. {
  762. if (!arg)
  763. return -EINVAL;
  764. if (!strcmp(arg, "disable_pge")) {
  765. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
  766. disable_pge = 1;
  767. } else if (!strcmp(arg, "disable_pse")) {
  768. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
  769. disable_pse = 1;
  770. } else if (!strcmp(arg, "disable_sep")) {
  771. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
  772. disable_sep = 1;
  773. } else if (!strcmp(arg, "disable_tsc")) {
  774. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
  775. disable_tsc = 1;
  776. } else if (!strcmp(arg, "disable_mtrr")) {
  777. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
  778. disable_mtrr = 1;
  779. } else if (!strcmp(arg, "disable_timer")) {
  780. disable_vmi_timer = 1;
  781. disable_noidle = 1;
  782. } else if (!strcmp(arg, "disable_noidle"))
  783. disable_noidle = 1;
  784. return 0;
  785. }
  786. early_param("vmi", parse_vmi);