vmi_32.c 29 KB

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