i386 Vmi inline implementation
RFC PATCH 4/24 i386 Vmi inline implementation
Macros to use VMI calls from assembly and C languages are introduced. The macros are quite complex, but the end result is rather impressive. The result is that when compiling a VMI kernel, the native code is emitted inline, with no function call overhead, and some wiggle room for register allocation. The hypervisor compatibility code is emitted out of line into a separate section, and patched dynamically during the process of preparing the kernel for boot. In the end, this task is envisioned as being done by a loader agent outside of the kernel proper, allowing for a completely transparent kernel start of day.
Please forgive some of the historical and clearly in-transition definitions in vmi.h; in particular, the MMU VMI calls have not yet been adapted to the inline interface, and the naming convention has not yet been Linux-ified. Also, there are artifacts remaining here from the compile time ability to choose a register convention. There are also some bugs with non-regparm compilation.
The set of calls in the VMI interface here needs some minor adjustments for Xen compatibility. In particular, a method to convert machine to physical mappings in PTEs is one missing interface, and with that and some other rather small changes, a VMI kernel is perfectly capable of running in a Xen hypervisor.
Many thanks to Joshua LeVasseur at the University of Karlsruhe, and Volkmar Uhlig, now at IBM, who verified this claim on a slightly different form of the interface. We are actively working on assembling all of those bits together right now to work on the Xen 3.0 interface. The end result is that the exact same kernel binary can run on native hardware as well as multiple hypervisors with near zero overhead, and absolutely minimal impact of the hypervisor based idioms on the native code base.
Note that hypervisor specific drivers need not ever be a part of the core interface, as drivers are already very well encapsulated by kernel modules, and are free to use any interface they wish to talk to the hardware (in this case, the hypervisor). Imposing an interface such as this on the driver is somewhat non-productive, and needlessly complicates both the driver and the core interface. There is a little bit of blending room for devices like timers, interrupt controllers, and other components that are closely coupled with the CPU, but with good modularization in the kernel, these devices can largely be factored out as well.
Forgive the strangeness introduced by compile time options for controlling the calling convention; currently, non-REGPARM builds are actually broken. I plan on fixing this so that smart inliners can take advantage of known constants; these constants are available in the decoded call stream as push immediate instructions, but they are not generated for the first three arguments if the regparm calling convention is used, since the register allocation causes that data to go out of our scope. The exact call ABI is still up for debate, so this is supposed to allow some tinkering for CPI and code overhead measurement. Eventually, one ABI will be chosen, and the macros can get a bit simpler.
include/asm-i386/mach-vmi/mach_asm.h +191 -0
@@ -0,0 +1,191 @@ +#ifndef __MACH_ASM_H +#define __MACH_ASM_H + +/* +* Take care to ensure that these labels do not overlap any in parent scope, +* as the breakage can be difficult to detect. I haven't found a flawless +* way to avoid collision because local \@ can not be saved for a future +* macro definitions, and we are stuck running cpp -traditional on entry.S +* +* To work around gas bugs, we must emit the native sequence here into a +* separate section first to measure the length. Some versions of gas have +* difficulty resolving vmi_native_end - vmi_native_begin during evaluation +* of an assembler conditional, which we use during the .rept directive +* below to generate the nop padding -- Zach +*/ + +/* First, measure the native instruction sequence length */ +#define vmi_native_start \ + .pushsection .vmi.native,"ax"; \ + 771:; +#define vmi_native_finish \ + 772:; \ + .popsection; +#define vmi_native_begin 771b +#define vmi_native_end 772b +#define vmi_native_len (vmi_native_end - vmi_native_begin) + +/* Now, measure and emit the vmi translation sequence */ +#define vmi_translation_start \ + .pushsection .vmi.translation,"ax"; \ + 781:; +#define vmi_translation_finish \ + 782:; \ + .popsection; +#define vmi_translation_begin 781b +#define vmi_translation_end 782b +#define vmi_translation_len (vmi_translation_end - vmi_translation_begin) + +/* Finally, emit the padded native sequence */ +#define vmi_padded_start \ + 791:; +#define vmi_padded_finish \ + 792:; +#define vmi_padded_begin 791b +#define vmi_padded_end 792b +#define vmi_padded_len (vmi_padded_end - vmi_padded_begin) + +#define vmi_call (name) \ + call .+5+name + +/* +* Pad out the current native instruction sequence with a series of nops; +* the nop used is 0x66* 0x90 which is data16 nop or xchg %ax, %ax. We +* pad out with up to 11 bytes of prefix at a time because beyond that +* both AMD and Intel processors start to show inefficiencies. +*/ +#define vmi_nop_pad \ +.equ vmi_pad_total, vmi_translation_len - vmi_native_len; \ +.equ vmi_pad, vmi_pad_total; \ +.rept (vmi_pad+11)/12; \ + .if vmi_pad > 12; \ + .equ vmi_cur_pad, 12; \ + .else; \ + .equ vmi_cur_pad, vmi_pad; \ + .endif; \ + .if vmi_cur_pad > 1; \ + .fill vmi_cur_pad-1, 1, 0x66; \ + .endif; \ + .byte 0x90; \ + .equ vmi_pad, vmi_pad - vmi_cur_pad; \ +.endr; + +/* +* Create an annotation for a VMI call; the VMI call currently must be +* wrapped in one of the vmi_raw_call (for assembler) or one of the +* family of defined wrappers for C code. +* XXXPara - use local labels +*/ +#define vmi_annotate (name) \ + .pushsection .vmi.annotation,"a"; \ + .align 4; \ + .long name; \ + .long vmi_padded_begin; \ + .long vmi_translation_begin; \ + .byte vmi_padded_len; \ + .byte vmi_translation_len; \ + .byte vmi_pad_total; \ + .byte 0; \ + .popsection; + +#define vmi_raw_call (name, native) \ + vmi_native_start; \ + native; \ + vmi_native_finish; \ + \ + vmi_translation_start; \ + vmi_call(name); \ + vmi_translation_finish; \ + \ + vmi_padded_start; \ + native; \ + vmi_nop_pad; \ + vmi_padded_finish; \ + \ + vmi_annotate(name); + +#include <vmiCalls.h> +#ifdef __ASSEMBLY__ +/* +* Create VMI_CALL_FuncName definitions for assembly code using +* equates; the C enumerations can not be used without propagating +* them in some fashion, and rather the obfuscate asm-offsets.c, it +* seems reasonable to confine this here. +*/ +.equ VMI_CALL_CUR, 0; +#define VDEF(call) \ + .equ VMI_CALL_/**/call, VMI_CALL_CUR; \ + .equ VMI_CALL_CUR, VMI_CALL_CUR+1; +VMI_CALLS +#undef VDEF +#endif /* __ASSEMBLY__ */ + +#define IRET vmi_raw_call(VMI_CALL_IRET, iret) +#define CLI vmi_raw_call(VMI_CALL_DisableInterrupts, cli) +#define STI vmi_raw_call(VMI_CALL_EnableInterrupts, sti) +#define STI_SYSEXIT vmi_raw_call(VMI_CALL_SYSEXIT, sti; sysexit) + +/* +* Due to the presence of "," in the instruction, and the use of +* -traditional to compile entry.S, we can not use a macro to +* encapsulate (mov %cr0, %eax); the full expansion must be +* written. +*/ +#define GET_CR0 vmi_native_start; \ + mov %cr0, %eax; \ + vmi_native_finish; \ + vmi_translation_start; \ + vmi_call(VMI_CALL_GetCR0); \ + vmi_translation_finish; \ + vmi_padded_start; \ + mov %cr0, %eax; \ + vmi_nop_pad; \ + vmi_padded_finish; \ + vmi_annotate(VMI_CALL_GetCR0); + +#ifndef __ASSEMBLY__ +/* +* Several handy macro definitions used to convert the raw assembler +* definitions here into quoted strings for use in inline assembler +* from C code. +* +* To convert the value of a defined token to a string, XSTR(x) +* To concatenate multiple parameters separated by commas, XCONC() +* To convert the value of a defined value with commas, XCSTR() +* +* These macros are incompatible with -traditional +*/ +#define MAKESTR (x) #x +#define XSTR (x) MAKESTR(x) +#define XCONC(args...) args +#define CONCSTR(x...) #x +#define XCSTR(x...) CONCSTR(x) + +/* +* Propagate these definitions as strings up to C code for convenient use +* in stringized assembler as pseudo-mnemonics; we must emit assembler +* directives to generate equates for the VMI_CALL_XXX symbols, since they +* will not be available otherwise to the assembler, and we can't emit +* the C versions of these functions from within an inline assembler +* string. +*/ +asm(".equ VMI_CALL_CUR, 0;\n\t" ); +#define VDEF (call) \ + asm (".equ VMI_CALL_" #call ", VMI_CALL_CUR;\n\t"); \ + asm (".equ VMI_CALL_CUR, VMI_CALL_CUR+1;\n\t"); +VMI_CALLS +#undef VDEF + +/* +* Sti and Cli are special cases, used in raw inline assembler strings; +* they do not need much of the machinery provided by C-code to do type +* checking or push arguments onto the stack, which means we can simply +* quote the assembler versions defined above rather than try to pry +* apart the call sites which use these raw strings. +*/ +#define CLI_STRING XCSTR(CLI) +#define STI_STRING XCSTR(STI) + +#endif /* !__ASSEMBLY__ */ + +#endif /* __MACH_ASM_H */
include/asm-i386/mach-vmi/vmi.h +249 -0
249 lines not on this page · raw message
arch/i386/Kconfig +41 -0
47 lines not on this page · raw message