vmi_32.c 28 KB

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