vmi.c 29 KB

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