vmi.c 27 KB

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