vmi.c 28 KB

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