vmi_32.c 26 KB

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