paravirt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Paravirtualization interfaces
  2. Copyright (C) 2006 Rusty Russell IBM Corporation
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/efi.h>
  18. #include <linux/bcd.h>
  19. #include <linux/start_kernel.h>
  20. #include <asm/bug.h>
  21. #include <asm/paravirt.h>
  22. #include <asm/desc.h>
  23. #include <asm/setup.h>
  24. #include <asm/arch_hooks.h>
  25. #include <asm/time.h>
  26. #include <asm/irq.h>
  27. #include <asm/delay.h>
  28. #include <asm/fixmap.h>
  29. #include <asm/apic.h>
  30. /* nop stub */
  31. static void native_nop(void)
  32. {
  33. }
  34. static void __init default_banner(void)
  35. {
  36. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  37. paravirt_ops.name);
  38. }
  39. char *memory_setup(void)
  40. {
  41. return paravirt_ops.memory_setup();
  42. }
  43. /* Simple instruction patching code. */
  44. #define DEF_NATIVE(name, code) \
  45. extern const char start_##name[], end_##name[]; \
  46. asm("start_" #name ": " code "; end_" #name ":")
  47. DEF_NATIVE(cli, "cli");
  48. DEF_NATIVE(sti, "sti");
  49. DEF_NATIVE(popf, "push %eax; popf");
  50. DEF_NATIVE(pushf, "pushf; pop %eax");
  51. DEF_NATIVE(pushf_cli, "pushf; pop %eax; cli");
  52. DEF_NATIVE(iret, "iret");
  53. DEF_NATIVE(sti_sysexit, "sti; sysexit");
  54. static const struct native_insns
  55. {
  56. const char *start, *end;
  57. } native_insns[] = {
  58. [PARAVIRT_IRQ_DISABLE] = { start_cli, end_cli },
  59. [PARAVIRT_IRQ_ENABLE] = { start_sti, end_sti },
  60. [PARAVIRT_RESTORE_FLAGS] = { start_popf, end_popf },
  61. [PARAVIRT_SAVE_FLAGS] = { start_pushf, end_pushf },
  62. [PARAVIRT_SAVE_FLAGS_IRQ_DISABLE] = { start_pushf_cli, end_pushf_cli },
  63. [PARAVIRT_INTERRUPT_RETURN] = { start_iret, end_iret },
  64. [PARAVIRT_STI_SYSEXIT] = { start_sti_sysexit, end_sti_sysexit },
  65. };
  66. static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
  67. {
  68. unsigned int insn_len;
  69. /* Don't touch it if we don't have a replacement */
  70. if (type >= ARRAY_SIZE(native_insns) || !native_insns[type].start)
  71. return len;
  72. insn_len = native_insns[type].end - native_insns[type].start;
  73. /* Similarly if we can't fit replacement. */
  74. if (len < insn_len)
  75. return len;
  76. memcpy(insns, native_insns[type].start, insn_len);
  77. return insn_len;
  78. }
  79. static fastcall unsigned long native_get_debugreg(int regno)
  80. {
  81. unsigned long val = 0; /* Damn you, gcc! */
  82. switch (regno) {
  83. case 0:
  84. asm("movl %%db0, %0" :"=r" (val)); break;
  85. case 1:
  86. asm("movl %%db1, %0" :"=r" (val)); break;
  87. case 2:
  88. asm("movl %%db2, %0" :"=r" (val)); break;
  89. case 3:
  90. asm("movl %%db3, %0" :"=r" (val)); break;
  91. case 6:
  92. asm("movl %%db6, %0" :"=r" (val)); break;
  93. case 7:
  94. asm("movl %%db7, %0" :"=r" (val)); break;
  95. default:
  96. BUG();
  97. }
  98. return val;
  99. }
  100. static fastcall void native_set_debugreg(int regno, unsigned long value)
  101. {
  102. switch (regno) {
  103. case 0:
  104. asm("movl %0,%%db0" : /* no output */ :"r" (value));
  105. break;
  106. case 1:
  107. asm("movl %0,%%db1" : /* no output */ :"r" (value));
  108. break;
  109. case 2:
  110. asm("movl %0,%%db2" : /* no output */ :"r" (value));
  111. break;
  112. case 3:
  113. asm("movl %0,%%db3" : /* no output */ :"r" (value));
  114. break;
  115. case 6:
  116. asm("movl %0,%%db6" : /* no output */ :"r" (value));
  117. break;
  118. case 7:
  119. asm("movl %0,%%db7" : /* no output */ :"r" (value));
  120. break;
  121. default:
  122. BUG();
  123. }
  124. }
  125. void init_IRQ(void)
  126. {
  127. paravirt_ops.init_IRQ();
  128. }
  129. static fastcall void native_clts(void)
  130. {
  131. asm volatile ("clts");
  132. }
  133. static fastcall unsigned long native_read_cr0(void)
  134. {
  135. unsigned long val;
  136. asm volatile("movl %%cr0,%0\n\t" :"=r" (val));
  137. return val;
  138. }
  139. static fastcall void native_write_cr0(unsigned long val)
  140. {
  141. asm volatile("movl %0,%%cr0": :"r" (val));
  142. }
  143. static fastcall unsigned long native_read_cr2(void)
  144. {
  145. unsigned long val;
  146. asm volatile("movl %%cr2,%0\n\t" :"=r" (val));
  147. return val;
  148. }
  149. static fastcall void native_write_cr2(unsigned long val)
  150. {
  151. asm volatile("movl %0,%%cr2": :"r" (val));
  152. }
  153. static fastcall unsigned long native_read_cr3(void)
  154. {
  155. unsigned long val;
  156. asm volatile("movl %%cr3,%0\n\t" :"=r" (val));
  157. return val;
  158. }
  159. static fastcall void native_write_cr3(unsigned long val)
  160. {
  161. asm volatile("movl %0,%%cr3": :"r" (val));
  162. }
  163. static fastcall unsigned long native_read_cr4(void)
  164. {
  165. unsigned long val;
  166. asm volatile("movl %%cr4,%0\n\t" :"=r" (val));
  167. return val;
  168. }
  169. static fastcall unsigned long native_read_cr4_safe(void)
  170. {
  171. unsigned long val;
  172. /* This could fault if %cr4 does not exist */
  173. asm("1: movl %%cr4, %0 \n"
  174. "2: \n"
  175. ".section __ex_table,\"a\" \n"
  176. ".long 1b,2b \n"
  177. ".previous \n"
  178. : "=r" (val): "0" (0));
  179. return val;
  180. }
  181. static fastcall void native_write_cr4(unsigned long val)
  182. {
  183. asm volatile("movl %0,%%cr4": :"r" (val));
  184. }
  185. static fastcall unsigned long native_save_fl(void)
  186. {
  187. unsigned long f;
  188. asm volatile("pushfl ; popl %0":"=g" (f): /* no input */);
  189. return f;
  190. }
  191. static fastcall void native_restore_fl(unsigned long f)
  192. {
  193. asm volatile("pushl %0 ; popfl": /* no output */
  194. :"g" (f)
  195. :"memory", "cc");
  196. }
  197. static fastcall void native_irq_disable(void)
  198. {
  199. asm volatile("cli": : :"memory");
  200. }
  201. static fastcall void native_irq_enable(void)
  202. {
  203. asm volatile("sti": : :"memory");
  204. }
  205. static fastcall void native_safe_halt(void)
  206. {
  207. asm volatile("sti; hlt": : :"memory");
  208. }
  209. static fastcall void native_halt(void)
  210. {
  211. asm volatile("hlt": : :"memory");
  212. }
  213. static fastcall void native_wbinvd(void)
  214. {
  215. asm volatile("wbinvd": : :"memory");
  216. }
  217. static fastcall unsigned long long native_read_msr(unsigned int msr, int *err)
  218. {
  219. unsigned long long val;
  220. asm volatile("2: rdmsr ; xorl %0,%0\n"
  221. "1:\n\t"
  222. ".section .fixup,\"ax\"\n\t"
  223. "3: movl %3,%0 ; jmp 1b\n\t"
  224. ".previous\n\t"
  225. ".section __ex_table,\"a\"\n"
  226. " .align 4\n\t"
  227. " .long 2b,3b\n\t"
  228. ".previous"
  229. : "=r" (*err), "=A" (val)
  230. : "c" (msr), "i" (-EFAULT));
  231. return val;
  232. }
  233. static fastcall int native_write_msr(unsigned int msr, unsigned long long val)
  234. {
  235. int err;
  236. asm volatile("2: wrmsr ; xorl %0,%0\n"
  237. "1:\n\t"
  238. ".section .fixup,\"ax\"\n\t"
  239. "3: movl %4,%0 ; jmp 1b\n\t"
  240. ".previous\n\t"
  241. ".section __ex_table,\"a\"\n"
  242. " .align 4\n\t"
  243. " .long 2b,3b\n\t"
  244. ".previous"
  245. : "=a" (err)
  246. : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
  247. "i" (-EFAULT));
  248. return err;
  249. }
  250. static fastcall unsigned long long native_read_tsc(void)
  251. {
  252. unsigned long long val;
  253. asm volatile("rdtsc" : "=A" (val));
  254. return val;
  255. }
  256. static fastcall unsigned long long native_read_pmc(void)
  257. {
  258. unsigned long long val;
  259. asm volatile("rdpmc" : "=A" (val));
  260. return val;
  261. }
  262. static fastcall void native_load_tr_desc(void)
  263. {
  264. asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
  265. }
  266. static fastcall void native_load_gdt(const struct Xgt_desc_struct *dtr)
  267. {
  268. asm volatile("lgdt %0"::"m" (*dtr));
  269. }
  270. static fastcall void native_load_idt(const struct Xgt_desc_struct *dtr)
  271. {
  272. asm volatile("lidt %0"::"m" (*dtr));
  273. }
  274. static fastcall void native_store_gdt(struct Xgt_desc_struct *dtr)
  275. {
  276. asm ("sgdt %0":"=m" (*dtr));
  277. }
  278. static fastcall void native_store_idt(struct Xgt_desc_struct *dtr)
  279. {
  280. asm ("sidt %0":"=m" (*dtr));
  281. }
  282. static fastcall unsigned long native_store_tr(void)
  283. {
  284. unsigned long tr;
  285. asm ("str %0":"=r" (tr));
  286. return tr;
  287. }
  288. static fastcall void native_load_tls(struct thread_struct *t, unsigned int cpu)
  289. {
  290. #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
  291. C(0); C(1); C(2);
  292. #undef C
  293. }
  294. static inline void native_write_dt_entry(void *dt, int entry, u32 entry_low, u32 entry_high)
  295. {
  296. u32 *lp = (u32 *)((char *)dt + entry*8);
  297. lp[0] = entry_low;
  298. lp[1] = entry_high;
  299. }
  300. static fastcall void native_write_ldt_entry(void *dt, int entrynum, u32 low, u32 high)
  301. {
  302. native_write_dt_entry(dt, entrynum, low, high);
  303. }
  304. static fastcall void native_write_gdt_entry(void *dt, int entrynum, u32 low, u32 high)
  305. {
  306. native_write_dt_entry(dt, entrynum, low, high);
  307. }
  308. static fastcall void native_write_idt_entry(void *dt, int entrynum, u32 low, u32 high)
  309. {
  310. native_write_dt_entry(dt, entrynum, low, high);
  311. }
  312. static fastcall void native_load_esp0(struct tss_struct *tss,
  313. struct thread_struct *thread)
  314. {
  315. tss->esp0 = thread->esp0;
  316. /* This can only happen when SEP is enabled, no need to test "SEP"arately */
  317. if (unlikely(tss->ss1 != thread->sysenter_cs)) {
  318. tss->ss1 = thread->sysenter_cs;
  319. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  320. }
  321. }
  322. static fastcall void native_io_delay(void)
  323. {
  324. asm volatile("outb %al,$0x80");
  325. }
  326. /* These are in entry.S */
  327. extern fastcall void native_iret(void);
  328. extern fastcall void native_irq_enable_sysexit(void);
  329. static int __init print_banner(void)
  330. {
  331. paravirt_ops.banner();
  332. return 0;
  333. }
  334. core_initcall(print_banner);
  335. /* We simply declare start_kernel to be the paravirt probe of last resort. */
  336. paravirt_probe(start_kernel);
  337. struct paravirt_ops paravirt_ops = {
  338. .name = "bare hardware",
  339. .paravirt_enabled = 0,
  340. .kernel_rpl = 0,
  341. .patch = native_patch,
  342. .banner = default_banner,
  343. .arch_setup = native_nop,
  344. .memory_setup = machine_specific_memory_setup,
  345. .get_wallclock = native_get_wallclock,
  346. .set_wallclock = native_set_wallclock,
  347. .time_init = time_init_hook,
  348. .init_IRQ = native_init_IRQ,
  349. .cpuid = native_cpuid,
  350. .get_debugreg = native_get_debugreg,
  351. .set_debugreg = native_set_debugreg,
  352. .clts = native_clts,
  353. .read_cr0 = native_read_cr0,
  354. .write_cr0 = native_write_cr0,
  355. .read_cr2 = native_read_cr2,
  356. .write_cr2 = native_write_cr2,
  357. .read_cr3 = native_read_cr3,
  358. .write_cr3 = native_write_cr3,
  359. .read_cr4 = native_read_cr4,
  360. .read_cr4_safe = native_read_cr4_safe,
  361. .write_cr4 = native_write_cr4,
  362. .save_fl = native_save_fl,
  363. .restore_fl = native_restore_fl,
  364. .irq_disable = native_irq_disable,
  365. .irq_enable = native_irq_enable,
  366. .safe_halt = native_safe_halt,
  367. .halt = native_halt,
  368. .wbinvd = native_wbinvd,
  369. .read_msr = native_read_msr,
  370. .write_msr = native_write_msr,
  371. .read_tsc = native_read_tsc,
  372. .read_pmc = native_read_pmc,
  373. .load_tr_desc = native_load_tr_desc,
  374. .set_ldt = native_set_ldt,
  375. .load_gdt = native_load_gdt,
  376. .load_idt = native_load_idt,
  377. .store_gdt = native_store_gdt,
  378. .store_idt = native_store_idt,
  379. .store_tr = native_store_tr,
  380. .load_tls = native_load_tls,
  381. .write_ldt_entry = native_write_ldt_entry,
  382. .write_gdt_entry = native_write_gdt_entry,
  383. .write_idt_entry = native_write_idt_entry,
  384. .load_esp0 = native_load_esp0,
  385. .set_iopl_mask = native_set_iopl_mask,
  386. .io_delay = native_io_delay,
  387. .const_udelay = __const_udelay,
  388. #ifdef CONFIG_X86_LOCAL_APIC
  389. .apic_write = native_apic_write,
  390. .apic_write_atomic = native_apic_write_atomic,
  391. .apic_read = native_apic_read,
  392. #endif
  393. .irq_enable_sysexit = native_irq_enable_sysexit,
  394. .iret = native_iret,
  395. };
  396. EXPORT_SYMBOL(paravirt_ops);