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>
24 #include <asm/paravirt.h>
26 #include <asm/setup.h>
27 #include <asm/arch_hooks.h>
30 #include <asm/delay.h>
33 static void native_nop(void)
37 static void __init default_banner(void)
39 printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
43 char *memory_setup(void)
45 return paravirt_ops.memory_setup();
48 /* Simple instruction patching code. */
49 #define DEF_NATIVE(name, code) \
50 extern const char start_##name[], end_##name[]; \
51 asm("start_" #name ": " code "; end_" #name ":")
52 DEF_NATIVE(cli, "cli");
53 DEF_NATIVE(sti, "sti");
54 DEF_NATIVE(popf, "push %eax; popf");
55 DEF_NATIVE(pushf, "pushf; pop %eax");
56 DEF_NATIVE(pushf_cli, "pushf; pop %eax; cli");
57 DEF_NATIVE(iret, "iret");
58 DEF_NATIVE(sti_sysexit, "sti; sysexit");
60 static const struct native_insns
62 const char *start, *end;
64 [PARAVIRT_IRQ_DISABLE] = { start_cli, end_cli },
65 [PARAVIRT_IRQ_ENABLE] = { start_sti, end_sti },
66 [PARAVIRT_RESTORE_FLAGS] = { start_popf, end_popf },
67 [PARAVIRT_SAVE_FLAGS] = { start_pushf, end_pushf },
68 [PARAVIRT_SAVE_FLAGS_IRQ_DISABLE] = { start_pushf_cli, end_pushf_cli },
69 [PARAVIRT_INTERRUPT_RETURN] = { start_iret, end_iret },
70 [PARAVIRT_STI_SYSEXIT] = { start_sti_sysexit, end_sti_sysexit },
73 static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
75 unsigned int insn_len;
77 /* Don't touch it if we don't have a replacement */
78 if (type >= ARRAY_SIZE(native_insns) || !native_insns[type].start)
81 insn_len = native_insns[type].end - native_insns[type].start;
83 /* Similarly if we can't fit replacement. */
87 memcpy(insns, native_insns[type].start, insn_len);
91 static fastcall unsigned long native_get_debugreg(int regno)
93 unsigned long val = 0; /* Damn you, gcc! */
97 asm("movl %%db0, %0" :"=r" (val)); break;
99 asm("movl %%db1, %0" :"=r" (val)); break;
101 asm("movl %%db2, %0" :"=r" (val)); break;
103 asm("movl %%db3, %0" :"=r" (val)); break;
105 asm("movl %%db6, %0" :"=r" (val)); break;
107 asm("movl %%db7, %0" :"=r" (val)); break;
114 static fastcall void native_set_debugreg(int regno, unsigned long value)
118 asm("movl %0,%%db0" : /* no output */ :"r" (value));
121 asm("movl %0,%%db1" : /* no output */ :"r" (value));
124 asm("movl %0,%%db2" : /* no output */ :"r" (value));
127 asm("movl %0,%%db3" : /* no output */ :"r" (value));
130 asm("movl %0,%%db6" : /* no output */ :"r" (value));
133 asm("movl %0,%%db7" : /* no output */ :"r" (value));
142 paravirt_ops.init_IRQ();
145 static fastcall void native_clts(void)
147 asm volatile ("clts");
150 static fastcall unsigned long native_read_cr0(void)
153 asm volatile("movl %%cr0,%0\n\t" :"=r" (val));
157 static fastcall void native_write_cr0(unsigned long val)
159 asm volatile("movl %0,%%cr0": :"r" (val));
162 static fastcall unsigned long native_read_cr2(void)
165 asm volatile("movl %%cr2,%0\n\t" :"=r" (val));
169 static fastcall void native_write_cr2(unsigned long val)
171 asm volatile("movl %0,%%cr2": :"r" (val));
174 static fastcall unsigned long native_read_cr3(void)
177 asm volatile("movl %%cr3,%0\n\t" :"=r" (val));
181 static fastcall void native_write_cr3(unsigned long val)
183 asm volatile("movl %0,%%cr3": :"r" (val));
186 static fastcall unsigned long native_read_cr4(void)
189 asm volatile("movl %%cr4,%0\n\t" :"=r" (val));
193 static fastcall unsigned long native_read_cr4_safe(void)
196 /* This could fault if %cr4 does not exist */
197 asm("1: movl %%cr4, %0 \n"
199 ".section __ex_table,\"a\" \n"
202 : "=r" (val): "0" (0));
206 static fastcall void native_write_cr4(unsigned long val)
208 asm volatile("movl %0,%%cr4": :"r" (val));
211 static fastcall unsigned long native_save_fl(void)
214 asm volatile("pushfl ; popl %0":"=g" (f): /* no input */);
218 static fastcall void native_restore_fl(unsigned long f)
220 asm volatile("pushl %0 ; popfl": /* no output */
225 static fastcall void native_irq_disable(void)
227 asm volatile("cli": : :"memory");
230 static fastcall void native_irq_enable(void)
232 asm volatile("sti": : :"memory");
235 static fastcall void native_safe_halt(void)
237 asm volatile("sti; hlt": : :"memory");
240 static fastcall void native_halt(void)
242 asm volatile("hlt": : :"memory");
245 static fastcall void native_wbinvd(void)
247 asm volatile("wbinvd": : :"memory");
250 static fastcall unsigned long long native_read_msr(unsigned int msr, int *err)
252 unsigned long long val;
254 asm volatile("2: rdmsr ; xorl %0,%0\n"
256 ".section .fixup,\"ax\"\n\t"
257 "3: movl %3,%0 ; jmp 1b\n\t"
259 ".section __ex_table,\"a\"\n"
263 : "=r" (*err), "=A" (val)
264 : "c" (msr), "i" (-EFAULT));
269 static fastcall int native_write_msr(unsigned int msr, unsigned long long val)
272 asm volatile("2: wrmsr ; xorl %0,%0\n"
274 ".section .fixup,\"ax\"\n\t"
275 "3: movl %4,%0 ; jmp 1b\n\t"
277 ".section __ex_table,\"a\"\n"
282 : "c" (msr), "0" ((u32)val), "d" ((u32)(val>>32)),
287 static fastcall unsigned long long native_read_tsc(void)
289 unsigned long long val;
290 asm volatile("rdtsc" : "=A" (val));
294 static fastcall unsigned long long native_read_pmc(void)
296 unsigned long long val;
297 asm volatile("rdpmc" : "=A" (val));
301 static fastcall void native_load_tr_desc(void)
303 asm volatile("ltr %w0"::"q" (GDT_ENTRY_TSS*8));
306 static fastcall void native_load_gdt(const struct Xgt_desc_struct *dtr)
308 asm volatile("lgdt %0"::"m" (*dtr));
311 static fastcall void native_load_idt(const struct Xgt_desc_struct *dtr)
313 asm volatile("lidt %0"::"m" (*dtr));
316 static fastcall void native_store_gdt(struct Xgt_desc_struct *dtr)
318 asm ("sgdt %0":"=m" (*dtr));
321 static fastcall void native_store_idt(struct Xgt_desc_struct *dtr)
323 asm ("sidt %0":"=m" (*dtr));
326 static fastcall unsigned long native_store_tr(void)
329 asm ("str %0":"=r" (tr));
333 static fastcall void native_load_tls(struct thread_struct *t, unsigned int cpu)
335 #define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
340 static inline void native_write_dt_entry(void *dt, int entry, u32 entry_low, u32 entry_high)
342 u32 *lp = (u32 *)((char *)dt + entry*8);
347 static fastcall void native_write_ldt_entry(void *dt, int entrynum, u32 low, u32 high)
349 native_write_dt_entry(dt, entrynum, low, high);
352 static fastcall void native_write_gdt_entry(void *dt, int entrynum, u32 low, u32 high)
354 native_write_dt_entry(dt, entrynum, low, high);
357 static fastcall void native_write_idt_entry(void *dt, int entrynum, u32 low, u32 high)
359 native_write_dt_entry(dt, entrynum, low, high);
362 static fastcall void native_load_esp0(struct tss_struct *tss,
363 struct thread_struct *thread)
365 tss->esp0 = thread->esp0;
367 /* This can only happen when SEP is enabled, no need to test "SEP"arately */
368 if (unlikely(tss->ss1 != thread->sysenter_cs)) {
369 tss->ss1 = thread->sysenter_cs;
370 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
374 static fastcall void native_io_delay(void)
376 asm volatile("outb %al,$0x80");
379 /* These are in entry.S */
380 extern fastcall void native_iret(void);
381 extern fastcall void native_irq_enable_sysexit(void);
383 static int __init print_banner(void)
385 paravirt_ops.banner();
388 core_initcall(print_banner);
390 struct paravirt_ops paravirt_ops = {
391 .name = "bare hardware",
392 .paravirt_enabled = 0,
395 .patch = native_patch,
396 .banner = default_banner,
397 .arch_setup = native_nop,
398 .memory_setup = machine_specific_memory_setup,
399 .get_wallclock = native_get_wallclock,
400 .set_wallclock = native_set_wallclock,
401 .time_init = time_init_hook,
402 .init_IRQ = native_init_IRQ,
404 .cpuid = native_cpuid,
405 .get_debugreg = native_get_debugreg,
406 .set_debugreg = native_set_debugreg,
408 .read_cr0 = native_read_cr0,
409 .write_cr0 = native_write_cr0,
410 .read_cr2 = native_read_cr2,
411 .write_cr2 = native_write_cr2,
412 .read_cr3 = native_read_cr3,
413 .write_cr3 = native_write_cr3,
414 .read_cr4 = native_read_cr4,
415 .read_cr4_safe = native_read_cr4_safe,
416 .write_cr4 = native_write_cr4,
417 .save_fl = native_save_fl,
418 .restore_fl = native_restore_fl,
419 .irq_disable = native_irq_disable,
420 .irq_enable = native_irq_enable,
421 .safe_halt = native_safe_halt,
423 .wbinvd = native_wbinvd,
424 .read_msr = native_read_msr,
425 .write_msr = native_write_msr,
426 .read_tsc = native_read_tsc,
427 .read_pmc = native_read_pmc,
428 .load_tr_desc = native_load_tr_desc,
429 .set_ldt = native_set_ldt,
430 .load_gdt = native_load_gdt,
431 .load_idt = native_load_idt,
432 .store_gdt = native_store_gdt,
433 .store_idt = native_store_idt,
434 .store_tr = native_store_tr,
435 .load_tls = native_load_tls,
436 .write_ldt_entry = native_write_ldt_entry,
437 .write_gdt_entry = native_write_gdt_entry,
438 .write_idt_entry = native_write_idt_entry,
439 .load_esp0 = native_load_esp0,
441 .set_iopl_mask = native_set_iopl_mask,
442 .io_delay = native_io_delay,
443 .const_udelay = __const_udelay,
445 .irq_enable_sysexit = native_irq_enable_sysexit,
448 EXPORT_SYMBOL(paravirt_ops);