vmi.c 28 KB

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