1 /* Paravirtualization interfaces
2 Copyright (C) 2006 Rusty Russell IBM Corporation
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/efi.h>
21 #include <linux/bcd.h>
22 #include <linux/start_kernel.h>
25 #include <asm/paravirt.h>
27 #include <asm/setup.h>
28 #include <asm/arch_hooks.h>
31 #include <asm/delay.h>
34 static void native_nop(void)
38 static void __init default_banner(void)
40 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
44 char *memory_setup(void)
46 return paravirt_ops.memory_setup();
49 /* Simple instruction patching code. */
50 #define DEF_NATIVE(name, code) \
51 extern const char start_##name[], end_##name[]; \
52 asm("start_" #name ": " code "; end_" #name ":")
53 DEF_NATIVE(cli, "cli");
54 DEF_NATIVE(sti, "sti");
55 DEF_NATIVE(popf, "push %eax; popf");
56 DEF_NATIVE(pushf, "pushf; pop %eax");
57 DEF_NATIVE(pushf_cli, "pushf; pop %eax; cli");
58 DEF_NATIVE(iret, "iret");
59 DEF_NATIVE(sti_sysexit, "sti; sysexit");
61 static const struct native_insns
63 const char *start, *end;
65 [PARAVIRT_IRQ_DISABLE] = { start_cli, end_cli },
66 [PARAVIRT_IRQ_ENABLE] = { start_sti, end_sti },
67 [PARAVIRT_RESTORE_FLAGS] = { start_popf, end_popf },
68 [PARAVIRT_SAVE_FLAGS] = { start_pushf, end_pushf },
69 [PARAVIRT_SAVE_FLAGS_IRQ_DISABLE] = { start_pushf_cli, end_pushf_cli },
70 [PARAVIRT_INTERRUPT_RETURN] = { start_iret, end_iret },
71 [PARAVIRT_STI_SYSEXIT] = { start_sti_sysexit, end_sti_sysexit },
74 static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
76 unsigned int insn_len;
78 /* Don't touch it if we don't have a replacement */
79 if (type >= ARRAY_SIZE(native_insns) || !native_insns[type].start)
82 insn_len = native_insns[type].end - native_insns[type].start;
84 /* Similarly if we can't fit replacement. */
88 memcpy(insns, native_insns[type].start, insn_len);
92 static fastcall unsigned long native_get_debugreg(int regno)
94 unsigned long val = 0; /* Damn you, gcc! */
98 asm("movl %%db0, %0" :"=r" (val)); break;
100 asm("movl %%db1, %0" :"=r" (val)); break;
102 asm("movl %%db2, %0" :"=r" (val)); break;
104 asm("movl %%db3, %0" :"=r" (val)); break;
106 asm("movl %%db6, %0" :"=r" (val)); break;
108 asm("movl %%db7, %0" :"=r" (val)); break;
115 static fastcall void native_set_debugreg(int regno, unsigned long value)
119 asm("movl %0,%%db0" : /* no output */ :"r" (value));
122 asm("movl %0,%%db1" : /* no output */ :"r" (value));
125 asm("movl %0,%%db2" : /* no output */ :"r" (value));
128 asm("movl %0,%%db3" : /* no output */ :"r" (value));
131 asm("movl %0,%%db6" : /* no output */ :"r" (value));
134 asm("movl %0,%%db7" : /* no output */ :"r" (value));
143 paravirt_ops.init_IRQ();
146 static fastcall void native_clts(void)
148 asm volatile ("clts");
151 static fastcall unsigned long native_read_cr0(void)
154 asm volatile("movl %%cr0,%0\n\t" :"=r" (val));
158 static fastcall void native_write_cr0(unsigned long val)
160 asm volatile("movl %0,%%cr0": :"r" (val));
163 static fastcall unsigned long native_read_cr2(void)
166 asm volatile("movl %%cr2,%0\n\t" :"=r" (val));
170 static fastcall void native_write_cr2(unsigned long val)
172 asm volatile("movl %0,%%cr2": :"r" (val));
175 static fastcall unsigned long native_read_cr3(void)
178 asm volatile("movl %%cr3,%0\n\t" :"=r" (val));
182 static fastcall void native_write_cr3(unsigned long val)
184 asm volatile("movl %0,%%cr3": :"r" (val));
187 static fastcall unsigned long native_read_cr4(void)
190 asm volatile("movl %%cr4,%0\n\t" :"=r" (val));
194 static fastcall unsigned long native_read_cr4_safe(void)
197 /* This could fault if %cr4 does not exist */
198 asm("1: movl %%cr4, %0 \n"
200 ".section __ex_table,\"a\" \n"
203 : "=r" (val): "0" (0));
207 static fastcall void native_write_cr4(unsigned long val)
209 asm volatile("movl %0,%%cr4": :"r" (val));
212 static fastcall unsigned long native_save_fl(void)
215 asm volatile("pushfl ; popl %0":"=g" (f): /* no input */);
219 static fastcall void native_restore_fl(unsigned long f)
221 asm volatile("pushl %0 ; popfl": /* no output */
226 static fastcall void native_irq_disable(void)
228 asm volatile("cli": : :"memory");
231 static fastcall void native_irq_enable(void)
233 asm volatile("sti": : :"memory");
236 static fastcall void native_safe_halt(void)
238 asm volatile("sti; hlt": : :"memory");
241 static fastcall void native_halt(void)
243 asm volatile("hlt": : :"memory");
246 static fastcall void native_wbinvd(void)
248 asm volatile("wbinvd": : :"memory");
251 static fastcall unsigned long long native_read_msr(unsigned int msr, int *err)
253 unsigned long long val;
255 asm volatile("2: rdmsr ; xorl %0,%0\n"
257 ".section .fixup,\"ax\"\n\t"
258 "3: movl %3,%0 ; jmp 1b\n\t"
260 ".section __ex_table,\"a\"\n"
264 : "=r" (*err), "=A" (val)
265 : "c" (msr), "i" (-EFAULT));
270 static fastcall int native_write_msr(unsigned int msr, unsigned long long val)
273 asm volatile("2: wrmsr ; xorl %0,%0\n"
275 ".section .fixup,\"ax\"\n\t"
276 "3: movl %4,%0 ; jmp 1b\n\t"
278 ".section __ex_table,\"a\"\n"
283 : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
288 static fastcall unsigned long long native_read_tsc(void)
290 unsigned long long val;
291 asm volatile("rdtsc" : "=A" (val));
295 static fastcall unsigned long long native_read_pmc(void)
297 unsigned long long val;
298 asm volatile("rdpmc" : "=A" (val));
302 static fastcall void native_load_tr_desc(void)
304 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
307 static fastcall void native_load_gdt(const struct Xgt_desc_struct *dtr)
309 asm volatile("lgdt %0"::"m" (*dtr));
312 static fastcall void native_load_idt(const struct Xgt_desc_struct *dtr)
314 asm volatile("lidt %0"::"m" (*dtr));
317 static fastcall void native_store_gdt(struct Xgt_desc_struct *dtr)
319 asm ("sgdt %0":"=m" (*dtr));
322 static fastcall void native_store_idt(struct Xgt_desc_struct *dtr)
324 asm ("sidt %0":"=m" (*dtr));
327 static fastcall unsigned long native_store_tr(void)
330 asm ("str %0":"=r" (tr));
334 static fastcall void native_load_tls(struct thread_struct *t, unsigned int cpu)
336 #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
341 static inline void native_write_dt_entry(void *dt, int entry, u32 entry_low, u32 entry_high)
343 u32 *lp = (u32 *)((char *)dt + entry*8);
348 static fastcall void native_write_ldt_entry(void *dt, int entrynum, u32 low, u32 high)
350 native_write_dt_entry(dt, entrynum, low, high);
353 static fastcall void native_write_gdt_entry(void *dt, int entrynum, u32 low, u32 high)
355 native_write_dt_entry(dt, entrynum, low, high);
358 static fastcall void native_write_idt_entry(void *dt, int entrynum, u32 low, u32 high)
360 native_write_dt_entry(dt, entrynum, low, high);
363 static fastcall void native_load_esp0(struct tss_struct *tss,
364 struct thread_struct *thread)
366 tss->esp0 = thread->esp0;
368 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
369 if (unlikely(tss->ss1 != thread->sysenter_cs)) {
370 tss->ss1 = thread->sysenter_cs;
371 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
375 static fastcall void native_io_delay(void)
377 asm volatile("outb %al,$0x80");
380 /* These are in entry.S */
381 extern fastcall void native_iret(void);
382 extern fastcall void native_irq_enable_sysexit(void);
384 static int __init print_banner(void)
386 paravirt_ops.banner();
389 core_initcall(print_banner);
391 /* We simply declare start_kernel to be the paravirt probe of last resort. */
392 paravirt_probe(start_kernel);
394 struct paravirt_ops paravirt_ops = {
395 .name = "bare hardware",
396 .paravirt_enabled = 0,
399 .patch = native_patch,
400 .banner = default_banner,
401 .arch_setup = native_nop,
402 .memory_setup = machine_specific_memory_setup,
403 .get_wallclock = native_get_wallclock,
404 .set_wallclock = native_set_wallclock,
405 .time_init = time_init_hook,
406 .init_IRQ = native_init_IRQ,
408 .cpuid = native_cpuid,
409 .get_debugreg = native_get_debugreg,
410 .set_debugreg = native_set_debugreg,
412 .read_cr0 = native_read_cr0,
413 .write_cr0 = native_write_cr0,
414 .read_cr2 = native_read_cr2,
415 .write_cr2 = native_write_cr2,
416 .read_cr3 = native_read_cr3,
417 .write_cr3 = native_write_cr3,
418 .read_cr4 = native_read_cr4,
419 .read_cr4_safe = native_read_cr4_safe,
420 .write_cr4 = native_write_cr4,
421 .save_fl = native_save_fl,
422 .restore_fl = native_restore_fl,
423 .irq_disable = native_irq_disable,
424 .irq_enable = native_irq_enable,
425 .safe_halt = native_safe_halt,
427 .wbinvd = native_wbinvd,
428 .read_msr = native_read_msr,
429 .write_msr = native_write_msr,
430 .read_tsc = native_read_tsc,
431 .read_pmc = native_read_pmc,
432 .load_tr_desc = native_load_tr_desc,
433 .set_ldt = native_set_ldt,
434 .load_gdt = native_load_gdt,
435 .load_idt = native_load_idt,
436 .store_gdt = native_store_gdt,
437 .store_idt = native_store_idt,
438 .store_tr = native_store_tr,
439 .load_tls = native_load_tls,
440 .write_ldt_entry = native_write_ldt_entry,
441 .write_gdt_entry = native_write_gdt_entry,
442 .write_idt_entry = native_write_idt_entry,
443 .load_esp0 = native_load_esp0,
445 .set_iopl_mask = native_set_iopl_mask,
446 .io_delay = native_io_delay,
447 .const_udelay = __const_udelay,
449 .irq_enable_sysexit = native_irq_enable_sysexit,
452 EXPORT_SYMBOL(paravirt_ops);