vmi.c 29 KB

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