vmi_32.c 30 KB

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