vmi.c 26 KB

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