vmi.c 29 KB

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