paravirt.c 9.3 KB

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