paravirt.c 11 KB

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