vmi_32.c 26 KB

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