vmi_32.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. * Helper macros for MMU update flags. We can defer updates until a flush
  286. * or page invalidation only if the update is to the current address space
  287. * (otherwise, there is no flush). We must check against init_mm, since
  288. * this could be a kernel update, which usually passes init_mm, although
  289. * sometimes this check can be skipped if we know the particular function
  290. * is only called on user mode PTEs. We could change the kernel to pass
  291. * current->active_mm here, but in particular, I was unsure if changing
  292. * mm/highmem.c to do this would still be correct on other architectures.
  293. */
  294. #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm || \
  295. (!mustbeuser && (mm) == &init_mm))
  296. #define vmi_flags_addr(mm, addr, level, user) \
  297. ((level) | (is_current_as(mm, user) ? \
  298. (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
  299. #define vmi_flags_addr_defer(mm, addr, level, user) \
  300. ((level) | (is_current_as(mm, user) ? \
  301. (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
  302. static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  303. {
  304. vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  305. }
  306. static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  307. {
  308. vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
  309. }
  310. static void vmi_set_pte(pte_t *ptep, pte_t pte)
  311. {
  312. /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
  313. vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
  314. }
  315. static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  316. {
  317. vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  318. }
  319. static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
  320. {
  321. #ifdef CONFIG_X86_PAE
  322. const pte_t pte = { .pte = pmdval.pmd };
  323. #else
  324. const pte_t pte = { pmdval.pud.pgd.pgd };
  325. #endif
  326. vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
  327. }
  328. #ifdef CONFIG_X86_PAE
  329. static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
  330. {
  331. /*
  332. * XXX This is called from set_pmd_pte, but at both PT
  333. * and PD layers so the VMI_PAGE_PT flag is wrong. But
  334. * it is only called for large page mapping changes,
  335. * the Xen backend, doesn't support large pages, and the
  336. * ESX backend doesn't depend on the flag.
  337. */
  338. set_64bit((unsigned long long *)ptep,pte_val(pteval));
  339. vmi_ops.update_pte(ptep, VMI_PAGE_PT);
  340. }
  341. static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
  342. {
  343. vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
  344. }
  345. static void vmi_set_pud(pud_t *pudp, pud_t pudval)
  346. {
  347. /* Um, eww */
  348. const pte_t pte = { .pte = pudval.pgd.pgd };
  349. vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
  350. }
  351. static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
  352. {
  353. const pte_t pte = { .pte = 0 };
  354. vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
  355. }
  356. static void vmi_pmd_clear(pmd_t *pmd)
  357. {
  358. const pte_t pte = { .pte = 0 };
  359. vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
  360. }
  361. #endif
  362. #ifdef CONFIG_SMP
  363. static void __devinit
  364. vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
  365. unsigned long start_esp)
  366. {
  367. struct vmi_ap_state ap;
  368. /* Default everything to zero. This is fine for most GPRs. */
  369. memset(&ap, 0, sizeof(struct vmi_ap_state));
  370. ap.gdtr_limit = GDT_SIZE - 1;
  371. ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
  372. ap.idtr_limit = IDT_ENTRIES * 8 - 1;
  373. ap.idtr_base = (unsigned long) idt_table;
  374. ap.ldtr = 0;
  375. ap.cs = __KERNEL_CS;
  376. ap.eip = (unsigned long) start_eip;
  377. ap.ss = __KERNEL_DS;
  378. ap.esp = (unsigned long) start_esp;
  379. ap.ds = __USER_DS;
  380. ap.es = __USER_DS;
  381. ap.fs = __KERNEL_PERCPU;
  382. ap.gs = 0;
  383. ap.eflags = 0;
  384. #ifdef CONFIG_X86_PAE
  385. /* efer should match BSP efer. */
  386. if (cpu_has_nx) {
  387. unsigned l, h;
  388. rdmsr(MSR_EFER, l, h);
  389. ap.efer = (unsigned long long) h << 32 | l;
  390. }
  391. #endif
  392. ap.cr3 = __pa(swapper_pg_dir);
  393. /* Protected mode, paging, AM, WP, NE, MP. */
  394. ap.cr0 = 0x80050023;
  395. ap.cr4 = mmu_cr4_features;
  396. vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
  397. }
  398. #endif
  399. static void vmi_enter_lazy_cpu(void)
  400. {
  401. paravirt_enter_lazy_cpu();
  402. vmi_ops.set_lazy_mode(2);
  403. }
  404. static void vmi_enter_lazy_mmu(void)
  405. {
  406. paravirt_enter_lazy_mmu();
  407. vmi_ops.set_lazy_mode(1);
  408. }
  409. static void vmi_leave_lazy(void)
  410. {
  411. paravirt_leave_lazy(paravirt_get_lazy_mode());
  412. vmi_ops.set_lazy_mode(0);
  413. }
  414. static inline int __init check_vmi_rom(struct vrom_header *rom)
  415. {
  416. struct pci_header *pci;
  417. struct pnp_header *pnp;
  418. const char *manufacturer = "UNKNOWN";
  419. const char *product = "UNKNOWN";
  420. const char *license = "unspecified";
  421. if (rom->rom_signature != 0xaa55)
  422. return 0;
  423. if (rom->vrom_signature != VMI_SIGNATURE)
  424. return 0;
  425. if (rom->api_version_maj != VMI_API_REV_MAJOR ||
  426. rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
  427. printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
  428. rom->api_version_maj,
  429. rom->api_version_min);
  430. return 0;
  431. }
  432. /*
  433. * Relying on the VMI_SIGNATURE field is not 100% safe, so check
  434. * the PCI header and device type to make sure this is really a
  435. * VMI device.
  436. */
  437. if (!rom->pci_header_offs) {
  438. printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
  439. return 0;
  440. }
  441. pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
  442. if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
  443. pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
  444. /* Allow it to run... anyways, but warn */
  445. printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
  446. }
  447. if (rom->pnp_header_offs) {
  448. pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
  449. if (pnp->manufacturer_offset)
  450. manufacturer = (const char *)rom+pnp->manufacturer_offset;
  451. if (pnp->product_offset)
  452. product = (const char *)rom+pnp->product_offset;
  453. }
  454. if (rom->license_offs)
  455. license = (char *)rom+rom->license_offs;
  456. printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
  457. manufacturer, product,
  458. rom->api_version_maj, rom->api_version_min,
  459. pci->rom_version_maj, pci->rom_version_min);
  460. /* Don't allow BSD/MIT here for now because we don't want to end up
  461. with any binary only shim layers */
  462. if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
  463. printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
  464. license);
  465. return 0;
  466. }
  467. return 1;
  468. }
  469. /*
  470. * Probe for the VMI option ROM
  471. */
  472. static inline int __init probe_vmi_rom(void)
  473. {
  474. unsigned long base;
  475. /* VMI ROM is in option ROM area, check signature */
  476. for (base = 0xC0000; base < 0xE0000; base += 2048) {
  477. struct vrom_header *romstart;
  478. romstart = (struct vrom_header *)isa_bus_to_virt(base);
  479. if (check_vmi_rom(romstart)) {
  480. vmi_rom = romstart;
  481. return 1;
  482. }
  483. }
  484. return 0;
  485. }
  486. /*
  487. * VMI setup common to all processors
  488. */
  489. void vmi_bringup(void)
  490. {
  491. /* We must establish the lowmem mapping for MMU ops to work */
  492. if (vmi_ops.set_linear_mapping)
  493. vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, MAXMEM_PFN, 0);
  494. }
  495. /*
  496. * Return a pointer to a VMI function or NULL if unimplemented
  497. */
  498. static void *vmi_get_function(int vmicall)
  499. {
  500. u64 reloc;
  501. const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
  502. reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
  503. BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
  504. if (rel->type == VMI_RELOCATION_CALL_REL)
  505. return (void *)rel->eip;
  506. else
  507. return NULL;
  508. }
  509. /*
  510. * Helper macro for making the VMI paravirt-ops fill code readable.
  511. * For unimplemented operations, fall back to default, unless nop
  512. * is returned by the ROM.
  513. */
  514. #define para_fill(opname, vmicall) \
  515. do { \
  516. reloc = call_vrom_long_func(vmi_rom, get_reloc, \
  517. VMI_CALL_##vmicall); \
  518. if (rel->type == VMI_RELOCATION_CALL_REL) \
  519. opname = (void *)rel->eip; \
  520. else if (rel->type == VMI_RELOCATION_NOP) \
  521. opname = (void *)vmi_nop; \
  522. else if (rel->type != VMI_RELOCATION_NONE) \
  523. printk(KERN_WARNING "VMI: Unknown relocation " \
  524. "type %d for " #vmicall"\n",\
  525. rel->type); \
  526. } while (0)
  527. /*
  528. * Helper macro for making the VMI paravirt-ops fill code readable.
  529. * For cached operations which do not match the VMI ROM ABI and must
  530. * go through a tranlation stub. Ignore NOPs, since it is not clear
  531. * a NOP * VMI function corresponds to a NOP paravirt-op when the
  532. * functions are not in 1-1 correspondence.
  533. */
  534. #define para_wrap(opname, wrapper, cache, vmicall) \
  535. do { \
  536. reloc = call_vrom_long_func(vmi_rom, get_reloc, \
  537. VMI_CALL_##vmicall); \
  538. BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL); \
  539. if (rel->type == VMI_RELOCATION_CALL_REL) { \
  540. opname = wrapper; \
  541. vmi_ops.cache = (void *)rel->eip; \
  542. } \
  543. } while (0)
  544. /*
  545. * Activate the VMI interface and switch into paravirtualized mode
  546. */
  547. static inline int __init activate_vmi(void)
  548. {
  549. short kernel_cs;
  550. u64 reloc;
  551. const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
  552. if (call_vrom_func(vmi_rom, vmi_init) != 0) {
  553. printk(KERN_ERR "VMI ROM failed to initialize!");
  554. return 0;
  555. }
  556. savesegment(cs, kernel_cs);
  557. pv_info.paravirt_enabled = 1;
  558. pv_info.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
  559. pv_info.name = "vmi";
  560. pv_init_ops.patch = vmi_patch;
  561. /*
  562. * Many of these operations are ABI compatible with VMI.
  563. * This means we can fill in the paravirt-ops with direct
  564. * pointers into the VMI ROM. If the calling convention for
  565. * these operations changes, this code needs to be updated.
  566. *
  567. * Exceptions
  568. * CPUID paravirt-op uses pointers, not the native ISA
  569. * halt has no VMI equivalent; all VMI halts are "safe"
  570. * no MSR support yet - just trap and emulate. VMI uses the
  571. * same ABI as the native ISA, but Linux wants exceptions
  572. * from bogus MSR read / write handled
  573. * rdpmc is not yet used in Linux
  574. */
  575. /* CPUID is special, so very special it gets wrapped like a present */
  576. para_wrap(pv_cpu_ops.cpuid, vmi_cpuid, cpuid, CPUID);
  577. para_fill(pv_cpu_ops.clts, CLTS);
  578. para_fill(pv_cpu_ops.get_debugreg, GetDR);
  579. para_fill(pv_cpu_ops.set_debugreg, SetDR);
  580. para_fill(pv_cpu_ops.read_cr0, GetCR0);
  581. para_fill(pv_mmu_ops.read_cr2, GetCR2);
  582. para_fill(pv_mmu_ops.read_cr3, GetCR3);
  583. para_fill(pv_cpu_ops.read_cr4, GetCR4);
  584. para_fill(pv_cpu_ops.write_cr0, SetCR0);
  585. para_fill(pv_mmu_ops.write_cr2, SetCR2);
  586. para_fill(pv_mmu_ops.write_cr3, SetCR3);
  587. para_fill(pv_cpu_ops.write_cr4, SetCR4);
  588. para_fill(pv_irq_ops.save_fl, GetInterruptMask);
  589. para_fill(pv_irq_ops.restore_fl, SetInterruptMask);
  590. para_fill(pv_irq_ops.irq_disable, DisableInterrupts);
  591. para_fill(pv_irq_ops.irq_enable, EnableInterrupts);
  592. para_fill(pv_cpu_ops.wbinvd, WBINVD);
  593. para_fill(pv_cpu_ops.read_tsc, RDTSC);
  594. /* The following we emulate with trap and emulate for now */
  595. /* paravirt_ops.read_msr = vmi_rdmsr */
  596. /* paravirt_ops.write_msr = vmi_wrmsr */
  597. /* paravirt_ops.rdpmc = vmi_rdpmc */
  598. /* TR interface doesn't pass TR value, wrap */
  599. para_wrap(pv_cpu_ops.load_tr_desc, vmi_set_tr, set_tr, SetTR);
  600. /* LDT is special, too */
  601. para_wrap(pv_cpu_ops.set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
  602. para_fill(pv_cpu_ops.load_gdt, SetGDT);
  603. para_fill(pv_cpu_ops.load_idt, SetIDT);
  604. para_fill(pv_cpu_ops.store_gdt, GetGDT);
  605. para_fill(pv_cpu_ops.store_idt, GetIDT);
  606. para_fill(pv_cpu_ops.store_tr, GetTR);
  607. pv_cpu_ops.load_tls = vmi_load_tls;
  608. para_wrap(pv_cpu_ops.write_ldt_entry, vmi_write_ldt_entry,
  609. write_ldt_entry, WriteLDTEntry);
  610. para_wrap(pv_cpu_ops.write_gdt_entry, vmi_write_gdt_entry,
  611. write_gdt_entry, WriteGDTEntry);
  612. para_wrap(pv_cpu_ops.write_idt_entry, vmi_write_idt_entry,
  613. write_idt_entry, WriteIDTEntry);
  614. para_wrap(pv_cpu_ops.load_sp0, vmi_load_sp0, set_kernel_stack, UpdateKernelStack);
  615. para_fill(pv_cpu_ops.set_iopl_mask, SetIOPLMask);
  616. para_fill(pv_cpu_ops.io_delay, IODelay);
  617. para_wrap(pv_cpu_ops.lazy_mode.enter, vmi_enter_lazy_cpu,
  618. set_lazy_mode, SetLazyMode);
  619. para_wrap(pv_cpu_ops.lazy_mode.leave, vmi_leave_lazy,
  620. set_lazy_mode, SetLazyMode);
  621. para_wrap(pv_mmu_ops.lazy_mode.enter, vmi_enter_lazy_mmu,
  622. set_lazy_mode, SetLazyMode);
  623. para_wrap(pv_mmu_ops.lazy_mode.leave, vmi_leave_lazy,
  624. set_lazy_mode, SetLazyMode);
  625. /* user and kernel flush are just handled with different flags to FlushTLB */
  626. para_wrap(pv_mmu_ops.flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
  627. para_wrap(pv_mmu_ops.flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
  628. para_fill(pv_mmu_ops.flush_tlb_single, InvalPage);
  629. /*
  630. * Until a standard flag format can be agreed on, we need to
  631. * implement these as wrappers in Linux. Get the VMI ROM
  632. * function pointers for the two backend calls.
  633. */
  634. #ifdef CONFIG_X86_PAE
  635. vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
  636. vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
  637. #else
  638. vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
  639. vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
  640. #endif
  641. if (vmi_ops.set_pte) {
  642. pv_mmu_ops.set_pte = vmi_set_pte;
  643. pv_mmu_ops.set_pte_at = vmi_set_pte_at;
  644. pv_mmu_ops.set_pmd = vmi_set_pmd;
  645. #ifdef CONFIG_X86_PAE
  646. pv_mmu_ops.set_pte_atomic = vmi_set_pte_atomic;
  647. pv_mmu_ops.set_pte_present = vmi_set_pte_present;
  648. pv_mmu_ops.set_pud = vmi_set_pud;
  649. pv_mmu_ops.pte_clear = vmi_pte_clear;
  650. pv_mmu_ops.pmd_clear = vmi_pmd_clear;
  651. #endif
  652. }
  653. if (vmi_ops.update_pte) {
  654. pv_mmu_ops.pte_update = vmi_update_pte;
  655. pv_mmu_ops.pte_update_defer = vmi_update_pte_defer;
  656. }
  657. vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
  658. if (vmi_ops.allocate_page) {
  659. pv_mmu_ops.alloc_pte = vmi_allocate_pte;
  660. pv_mmu_ops.alloc_pmd = vmi_allocate_pmd;
  661. pv_mmu_ops.alloc_pmd_clone = vmi_allocate_pmd_clone;
  662. }
  663. vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
  664. if (vmi_ops.release_page) {
  665. pv_mmu_ops.release_pte = vmi_release_pte;
  666. pv_mmu_ops.release_pmd = vmi_release_pmd;
  667. }
  668. /* Set linear is needed in all cases */
  669. vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
  670. #ifdef CONFIG_HIGHPTE
  671. if (vmi_ops.set_linear_mapping)
  672. pv_mmu_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
  673. #endif
  674. /*
  675. * These MUST always be patched. Don't support indirect jumps
  676. * through these operations, as the VMI interface may use either
  677. * a jump or a call to get to these operations, depending on
  678. * the backend. They are performance critical anyway, so requiring
  679. * a patch is not a big problem.
  680. */
  681. pv_cpu_ops.irq_enable_sysexit = (void *)0xfeedbab0;
  682. pv_cpu_ops.iret = (void *)0xbadbab0;
  683. #ifdef CONFIG_SMP
  684. para_wrap(pv_apic_ops.startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
  685. #endif
  686. #ifdef CONFIG_X86_LOCAL_APIC
  687. para_fill(apic_ops->read, APICRead);
  688. para_fill(apic_ops->write, APICWrite);
  689. #endif
  690. /*
  691. * Check for VMI timer functionality by probing for a cycle frequency method
  692. */
  693. reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
  694. if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
  695. vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
  696. vmi_timer_ops.get_cycle_counter =
  697. vmi_get_function(VMI_CALL_GetCycleCounter);
  698. vmi_timer_ops.get_wallclock =
  699. vmi_get_function(VMI_CALL_GetWallclockTime);
  700. vmi_timer_ops.wallclock_updated =
  701. vmi_get_function(VMI_CALL_WallclockUpdated);
  702. vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
  703. vmi_timer_ops.cancel_alarm =
  704. vmi_get_function(VMI_CALL_CancelAlarm);
  705. pv_time_ops.time_init = vmi_time_init;
  706. pv_time_ops.get_wallclock = vmi_get_wallclock;
  707. pv_time_ops.set_wallclock = vmi_set_wallclock;
  708. #ifdef CONFIG_X86_LOCAL_APIC
  709. pv_apic_ops.setup_boot_clock = vmi_time_bsp_init;
  710. pv_apic_ops.setup_secondary_clock = vmi_time_ap_init;
  711. #endif
  712. pv_time_ops.sched_clock = vmi_sched_clock;
  713. pv_time_ops.get_tsc_khz = vmi_tsc_khz;
  714. /* We have true wallclock functions; disable CMOS clock sync */
  715. no_sync_cmos_clock = 1;
  716. } else {
  717. disable_noidle = 1;
  718. disable_vmi_timer = 1;
  719. }
  720. para_fill(pv_irq_ops.safe_halt, Halt);
  721. /*
  722. * Alternative instruction rewriting doesn't happen soon enough
  723. * to convert VMI_IRET to a call instead of a jump; so we have
  724. * to do this before IRQs get reenabled. Fortunately, it is
  725. * idempotent.
  726. */
  727. apply_paravirt(__parainstructions, __parainstructions_end);
  728. vmi_bringup();
  729. return 1;
  730. }
  731. #undef para_fill
  732. void __init vmi_init(void)
  733. {
  734. if (!vmi_rom)
  735. probe_vmi_rom();
  736. else
  737. check_vmi_rom(vmi_rom);
  738. /* In case probing for or validating the ROM failed, basil */
  739. if (!vmi_rom)
  740. return;
  741. reserve_top_address(-vmi_rom->virtual_top);
  742. #ifdef CONFIG_X86_IO_APIC
  743. /* This is virtual hardware; timer routing is wired correctly */
  744. no_timer_check = 1;
  745. #endif
  746. }
  747. void vmi_activate(void)
  748. {
  749. unsigned long flags;
  750. if (!vmi_rom)
  751. return;
  752. local_irq_save(flags);
  753. activate_vmi();
  754. local_irq_restore(flags & X86_EFLAGS_IF);
  755. }
  756. static int __init parse_vmi(char *arg)
  757. {
  758. if (!arg)
  759. return -EINVAL;
  760. if (!strcmp(arg, "disable_pge")) {
  761. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PGE);
  762. disable_pge = 1;
  763. } else if (!strcmp(arg, "disable_pse")) {
  764. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
  765. disable_pse = 1;
  766. } else if (!strcmp(arg, "disable_sep")) {
  767. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_SEP);
  768. disable_sep = 1;
  769. } else if (!strcmp(arg, "disable_tsc")) {
  770. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC);
  771. disable_tsc = 1;
  772. } else if (!strcmp(arg, "disable_mtrr")) {
  773. clear_cpu_cap(&boot_cpu_data, X86_FEATURE_MTRR);
  774. disable_mtrr = 1;
  775. } else if (!strcmp(arg, "disable_timer")) {
  776. disable_vmi_timer = 1;
  777. disable_noidle = 1;
  778. } else if (!strcmp(arg, "disable_noidle"))
  779. disable_noidle = 1;
  780. return 0;
  781. }
  782. early_param("vmi", parse_vmi);