lguest
Subsystems (14)
cover lguest
This patch series is against 2.6.20; some things are in flux, so there might be issues as other things flow into the latest -git tree.
From the documentation:
Lguest is designed to be a minimal hypervisor for the Linux kernel, for Linux developers and users to experiment with virtualization with the minimum of complexity. Nonetheless, it should have sufficient features to make it useful for specific tasks, and, of course, you are encouraged to fork and enhance it.
Features:
- Kernel module which runs in a normal kernel.
- Simple I/O model for communication.
- Simple program to create new guests.
- Logo contains cute puppies: http://lguest.ozlabs.org
Developer features:
- Fun to hack on.
- No ABI: being tied to a specific kernel anyway, you can change anything.
- Many opportunities for improvement or feature implementation.
Cheers!
Rusty
PATCH 1/10 lguest: Don't rely on last-linked fallthru when no paravirt handler
The current code simply calls "start_kernel" directly if we're under a hypervisor and no paravirt_ops backend wants us, because paravirt.c registers that as a backend and it's linked last.
This was always a vain hope; start_kernel won't get far without setup. It's also impossible for paravirt_ops backends which don't sit in the arch/i386/kernel directory: they can't link before paravirt.o anyway.
This implements a real fallthrough if we pass all the registered paravirt probes.
===================================================================
arch/i386/kernel/Makefile +0 -2
@@ -39,8 +39,6 @@ obj-$(CONFIG_EARLY_PRINTK) += early_prin obj-$(CONFIG_EARLY_PRINTK) += early_printk.o obj-$(CONFIG_HPET_TIMER) += hpet.o obj-$(CONFIG_K8_NB) += k8.o - -# Make sure this is linked after any other paravirt_ops structs: see head.S obj-$(CONFIG_PARAVIRT) += paravirt.o EXTRA_AFLAGS := -traditional
===================================================================
arch/i386/kernel/head.S +8 -1
@@ -502,10 +502,11 @@ startup_paravirt: pushl %ecx pushl %eax - /* paravirt.o is last in link, and that probe fn never returns */ pushl $__start_paravirtprobe 1: movl 0(%esp), %eax + cmpl $__stop_paravirtprobe, %eax + je unhandled_paravirt pushl (%eax) movl 8(%esp), %eax call *(%esp)
@@ -517,6 +518,12 @@ 1: addl $4, (%esp) jmp 1b + +unhandled_paravirt: + /* Nothing wanted us: try to die with dignity (impossible trap). */ + movl $0x1F, %edx + pushl $0 + jmp early_fault #endif /*
===================================================================
arch/i386/kernel/paravirt.c +0 -3
@@ -481,9 +481,6 @@ static int __init print_banner(void)return 0 ; }core_initcall (print_banner); - -/* We simply declare start_kernel to be the paravirt probe of last resort. */ -paravirt_probe (start_kernel);struct paravirt_ops paravirt_ops = { .name ="bare hardware" ,
PATCH 2/10 lguest: Export symbols for lguest as a module
lguest does some fairly lowlevel things to support a host, which normal modules don't need:
math_state_restore:
When the guest triggers a Device Not Available fault, we need to be able to restore the FPU
tsc_khz:
Simplest way of telling the guest how to interpret the TSC counter.
__put_task_struct:
We need to hold a reference to another task for inter-guest I/O, and put_task_struct() is an inline function which calls __put_task_struct.
access_process_vm:
We need to access another task for inter-guest I/O.
map_vm_area & __get_vm_area:
We need to map the switcher shim (ie. monitor) at 0xFFC01000.
===================================================================
arch/i386/kernel/traps.c +1 -0
@@ -1054,6 +1054,7 @@ asmlinkage void math_state_restore(void) thread->status |=TS_USEDFPU ;/* So we fnsave on switch_to() */ tsk->fpu_counter++; } +EXPORT_SYMBOL_GPL (math_state_restore);#ifndef CONFIG_MATH_EMULATION
===================================================================
arch/i386/kernel/tsc.c +1 -0
@@ -475,3 +475,4 @@ static int __init init_tsc_clocksource(v }module_init (init_tsc_clocksource); +EXPORT_SYMBOL_GPL (tsc_khz);
===================================================================
kernel/fork.c +1 -0
@@ -126,6 +126,7 @@ void __put_task_struct(struct task_strucif (!profile_handoff_task (tsk))free_task (tsk); } +EXPORT_SYMBOL_GPL (__put_task_struct);void __init fork_init (unsigned long mempages) {
===================================================================
mm/memory.c +1 -0
@@ -2692,3 +2692,4 @@ int access_process_vm(struct task_structreturn buf - old_buf; } +EXPORT_SYMBOL_GPL (access_process_vm);
===================================================================
mm/vmalloc.c +2 -0
@@ -159,6 +159,7 @@ int map_vm_area(struct vm_struct *area,flush_cache_vmap ((unsigned long ) area->addr, end);return err; } +EXPORT_SYMBOL_GPL (map_vm_area); static struct vm_struct *__get_vm_area_node(unsigned long size, unsigned long flags, unsigned long start, unsigned long end,
@@ -237,6 +238,7 @@ struct vm_struct *__get_vm_area(unsigned {return __get_vm_area_node (size, flags, start, end,-1 ,GFP_KERNEL ); } +EXPORT_SYMBOL_GPL (__get_vm_area);/** * get_vm_area - reserve a contingous kernel virtual area
Re: [PATCH 2/10] lguest: Export symbols for lguest as a module
Quoted 3 lines · Rusty Russell
tsc_khz:
Simplest way of telling the guest how to interpret the TSC counter.
Are you sure this will work with varying TSC frequencies?
In general you should get this from cpufreq.
-Andi
Re: [PATCH 2/10] lguest: Export symbols for lguest as a module
Quoted 8 lines · Andi Kleen
Quoted 3 lines · Rusty Russell
tsc_khz:
Simplest way of telling the guest how to interpret the TSC counter.Are you sure this will work with varying TSC frequencies?
I'm actually quite sure it doesn't (there's a FIXME in the lguest code). Given the debate over how useful the TSC was, I originally didn't use it, but (1) it's simple, and (2) when it doesn't change, it's pretty accurate.
Quoted 1 line
In general you should get this from cpufreq.
Hmm, ok, I'll bite: how? Time is a mystery I've avoided so far 8)
Thanks!
Rusty.
Re: [PATCH 2/10] lguest: Export symbols for lguest as a module
Quoted 8 lines · Rusty Russell
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Rusty Russell
tsc_khz:
Simplest way of telling the guest how to interpret the TSC counter.
Quoted 6 lines
Quoted 1 line
Are you sure this will work with varying TSC frequencies?
I'm actually quite sure it doesn't (there's a FIXME in the lguest code). Given the debate over how useful the TSC was, I originally didn't use it, but (1) it's simple, and (2) when it doesn't change, it's pretty accurate.
But when it changes users become pretty unhappy
Quoted 4 lines
Quoted 2 lines
In general you should get this from cpufreq.
Hmm, ok, I'll bite: how? Time is a mystery I've avoided so far 8)
the old x86-64 time.c (before -mm) has a example in #ifdef CONFIG_CPUFREQ
-Andi
Re: [PATCH 2/10] lguest: Export symbols for lguest as a module
Quoted 10 lines · Andi Kleen
Quoted 7 lines · Rusty Russell
Quoted 1 line · Andi Kleen
Are you sure this will work with varying TSC frequencies?
I'm actually quite sure it doesn't (there's a FIXME in the lguest code). Given the debate over how useful the TSC was, I originally didn't use it, but (1) it's simple, and (2) when it doesn't change, it's pretty accurate.
But when it changes users become pretty unhappy
True. Simplest fix is below. There are several time issues on the TODO list, and I will simply add this one.
Thanks!
Rusty.
lguest: Don't use the TSC in guest.
Andi complained that lguest guests don't deal with host TSC speed changing. Close this can of worms by not using the TSC in the guest. Later on we could do something clever when we overhaul this to deal with stolen time (also on the TODO list).
===================================================================
arch/i386/kernel/tsc.c +0 -1
@@ -475,4 +475,3 @@ static int __init init_tsc_clocksource(v }module_init (init_tsc_clocksource); -EXPORT_SYMBOL_GPL (tsc_khz);
===================================================================
arch/i386/lguest/hypercalls.c +0 -3
@@ -18,7 +18,6 @@#include <linux/uaccess.h> #include <linux/syscalls.h> #include <linux/mm.h> -#include <linux/clocksource.h> #include <asm/lguest.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -179,8 +178,6 @@ int hypercall(struct lguest *lg, struct/* We reserve the top pgd entry. */ put_user (4U *1024 *1024 , &lg->lguest_data->reserve_mem);put_user (lg->guestid, &lg->lguest_data->guestid); -put_user (clocksource_khz2mult (tsc_khz,22 ), - &lg->lguest_data->clock_mult);return 0 ; } pending =do_hcall (lg, regs);
===================================================================
arch/i386/lguest/lguest.c +0 -20
@@ -25,7 +25,6 @@#include <linux/screen_info.h> #include <linux/irq.h> #include <linux/interrupt.h> -#include <linux/clocksource.h> #include <asm/paravirt.h> #include <asm/lguest.h> #include <asm/lguest_user.h>
@@ -138,29 +137,10 @@ static struct notifier_block paniced = { .notifier_call = lguest_panic }; -static cycle_t lguest_clock_read (void ) -{ -/* FIXME: This is just the native one. Account stolen time! */ -return paravirt_ops.read_tsc (); -} - -/* FIXME: Update iff tsc rate changes. */ -static struct clocksource lguest_clock = { - .name ="lguest" , - .rating =400 , - .read = lguest_clock_read, - .mask =CLOCKSOURCE_MASK (64 ), - .mult =0 ,/* to be set */ - .shift =22 , - .is_continuous =1 , -}; -static char *lguest_memory_setup (void ) {/* We do these here because lockcheck barfs if before start_kernel */ atomic_notifier_chain_register (&panic_notifier_list, &paniced); - lguest_clock.mult = lguest_data.clock_mult; -clocksource_register (&lguest_clock); e820.nr_map =0 ;add_memory_region (0 ,PFN_PHYS (boot->max_pfn),E820_RAM );
===================================================================
include/asm-i386/lguest.h +0 -2
@@ -74,8 +74,6 @@ struct lguest_dataunsigned long reserve_mem;/* ID of this guest (used by network driver to set ethernet address) */ u16 guestid; -/* Multiplier for TSC clock. */ -u32 clock_mult;/* Fields initialized by the guest at boot: */ /* Instruction range to suppress interrupts even if enabled */
PATCH 3/10 lguest: Expose get_futex_key, get_key_refs and drop_key_refs.
In reply to Rusty RussellName: Expose get_futex_key, get_key_refs and drop_key_refs.
lguest uses the convenient futex infrastructure for inter-domain I/O, so expose get_futex_key, get_key_refs (renamed get_futex_key_refs) and drop_key_refs (renamed drop_futex_key_refs). Also means we need to expose the union that these use.
No code changes.
===================================================================
include/linux/futex.h +29 -0
@@ -100,6 +100,35 @@ extern intextern int handle_futex_death (u32 __user *uaddr,struct task_struct *curr,int pi); +/* +* Futexes are matched on equal values of this key. +* The key type depends on whether it's a shared or private mapping. +* Don't rearrange members without looking at hash_futex(). +* +* offset is aligned to a multiple of sizeof(u32) (== 4) by definition. +* We set bit 0 to indicate if it's an inode-based key. +*/ +union futex_key { +struct { +unsigned long pgoff; +struct inode *inode; +int offset; + } shared; +struct { +unsigned long address; +struct mm_struct *mm; +int offset; + } private; +struct { +unsigned long word; +void *ptr; +int offset; + } both; +}; +int get_futex_key (u32 __user *uaddr,union futex_key *key); +void get_futex_key_refs (union futex_key *key); +void drop_futex_key_refs (union futex_key *key); +#ifdef CONFIG_FUTEX extern void exit_robust_list (struct task_struct *curr);extern void exit_pi_state_list (struct task_struct *curr);
===================================================================
kernel/futex.c +14 -36
133 lines not on this page · raw message
PATCH 4/10 lguest: Initialize esp0 properly all the time
In reply to Rusty RussellWhenever we schedule, __switch_to calls load_esp0 which does:
tss->esp0 = thread->esp0;
This is never initialized for the initial thread (ie "swapper"), so when we're scheduling that, we end up setting esp0 to 0. This is
fine: the swapper never leaves ring 0, so this field is never used.
lguest, however, gets upset that we're trying to used an unmapped page as our kernel stack. Rather than work around it there, let's initialize it.
===================================================================
include/asm-i386/processor.h +1 -0
@@ -421,6 +421,7 @@ struct thread_struct { };#define INIT_THREAD { \ + .esp0 = sizeof(init_stack) + (long)&init_stack, \ .vm86_info = NULL, \ .sysenter_cs = __KERNEL_CS, \ .io_bitmap_ptr = NULL, \
PATCH 5/10 Make hvc_console.c compile on non-PowerPC
There's a really nice console helper (esp. for virtual console drivers) in drivers/char/hvc_console.c. It has only ever been used for PowerPC, though, so it uses NO_IRQ which is only defined there.
Let's fix that so it's more widely useful. By, say, lguest.
===================================================================
drivers/char/hvc_console.c +4 -0
@@ -48,6 +48,10 @@#define HVC_MINOR 0#define TIMEOUT (10) + +#ifndef NO_IRQ +#define NO_IRQ 0 +#endif /* * Wait this long per iteration while trying to push buffered data to the
PATCH 6/10 lguest code: the little linux hypervisor.
This is the core of lguest: both the guest code (always compiled in to the image so it can boot under lguest), and the host code (lg.ko).
There is only one config prompt at the moment: lguest is currently designed to run exactly the same guest and host kernels so we can frob the ABI freely.
Unfortunately, we don't have the build infrastructure for "private" asm-offsets.h files, so there's a not-so-neat include in arch/i386/kernel/asm-offsets.c.
===================================================================
arch/i386/Kconfig +21 -0
@@ -226,6 +226,27 @@ config ES7000_CLUSTERED_APIC depends on SMP && X86_ES7000 && MPENTIUMIII source "arch/i386/Kconfig.cpu" + +config LGUEST + tristate "Linux hypervisor example code" + depends on X86 && PARAVIRT && EXPERIMENTAL && !X86_PAE + select LGUEST_GUEST + select HVC_DRIVER + ---help--- + This is a very simple module which allows you to run + multiple instances of the same Linux kernel, using the + "lguest" command found in the Documentation/lguest directory. + Note that "lguest" is pronounced to rhyme with "fell quest", + not "rustyvisor". See Documentation/lguest/lguest.txt. + + If unsure, say N. If curious, say M. If masochistic, say Y. + +config LGUEST_GUEST + bool + help + The guest needs code built-in, even if the host has lguest + support as a module. The drivers are tiny, so we build them + in too. config HPET_TIMER bool "HPET Timer Support"
===================================================================
arch/i386/Makefile +1 -0
@@ -108,6 +108,7 @@ drivers-$(CONFIG_PCI) += arch/i386/pci # must be linked after kernel/ drivers-$(CONFIG_OPROFILE) += arch/i386/oprofile/ drivers-$(CONFIG_PM) += arch/i386/power/ +drivers-$(CONFIG_LGUEST_GUEST) += arch/i386/lguest/ CFLAGS += $(mflags-y) AFLAGS += $(mflags-y)
===================================================================
arch/i386/kernel/asm-offsets.c +19 -0
@@ -16,6 +16,10 @@#include <asm/thread_info.h> #include <asm/elf.h> #include <asm/pda.h> +#ifdef CONFIG_LGUEST_GUEST +#include <asm/lguest.h> +#include "../lguest/lg.h" +#endif #define DEFINE (sym, val) \ asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -111,4 +115,19 @@ void foo(void)OFFSET (PARAVIRT_iret, paravirt_ops, iret);OFFSET (PARAVIRT_read_cr0, paravirt_ops, read_cr0);#endif + +#ifdef CONFIG_LGUEST_GUEST +BLANK (); +OFFSET (LGUEST_DATA_irq_enabled, lguest_data, irq_enabled); +OFFSET (LGUEST_STATE_host_stackptr, lguest_state, host.stackptr); +OFFSET (LGUEST_STATE_host_pgdir, lguest_state, host.pgdir); +OFFSET (LGUEST_STATE_host_gdt, lguest_state, host.gdt); +OFFSET (LGUEST_STATE_host_idt, lguest_state, host.idt); +OFFSET (LGUEST_STATE_regs, lguest_state, regs); +OFFSET (LGUEST_STATE_gdt, lguest_state, gdt); +OFFSET (LGUEST_STATE_idt, lguest_state, idt); +OFFSET (LGUEST_STATE_gdt_table, lguest_state, gdt_table); +OFFSET (LGUEST_STATE_trapnum, lguest_state, regs.trapnum); +OFFSET (LGUEST_STATE_errcode, lguest_state, regs.errcode); +#endif }
===================================================================
arch/i386/lguest/Makefile new file +22 -0
@@ -0,0 +1,22 @@ +# Guest requires the paravirt_ops replacement and the bus driver. +obj-$(CONFIG_LGUEST_GUEST) += lguest.o lguest_bus.o + +# Host requires the other files, which can be a module. +obj-$(CONFIG_LGUEST) += lg.o +lg-objs := core.o hypercalls.o page_tables.o interrupts_and_traps.o \ + segments.o io.o lguest_user.o + +# We use top 4MB for guest traps page, then hypervisor. */ +HYPE_ADDR := (0xFFC00000+4096) +# The data is only 1k (256 interrupt handler pointers) +HYPE_DATA_SIZE := 1024 +CFLAGS += -DHYPE_ADDR="$(HYPE_ADDR)" -DHYPE_DATA_SIZE="$(HYPE_DATA_SIZE)" + +$(obj)/core.o: $(obj)/hypervisor-blob.c +# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@ + +clean-files := hypervisor-blob.c hypervisor-raw
===================================================================
arch/i386/lguest/core.c new file +425 -0
425 lines not on this page · raw message
===================================================================
arch/i386/lguest/hypercalls.c new file +199 -0
199 lines not on this page · raw message
===================================================================
arch/i386/lguest/hypervisor.S new file +170 -0
170 lines not on this page · raw message
===================================================================
arch/i386/lguest/interrupts_and_traps.c new file +221 -0
221 lines not on this page · raw message
===================================================================
arch/i386/lguest/io.c new file +413 -0
413 lines not on this page · raw message
===================================================================
arch/i386/lguest/lg.h new file +274 -0
274 lines not on this page · raw message
===================================================================
arch/i386/lguest/lguest.c new file +595 -0
595 lines not on this page · raw message
===================================================================
arch/i386/lguest/lguest_bus.c new file +180 -0
180 lines not on this page · raw message
===================================================================
arch/i386/lguest/lguest_user.c new file +242 -0
242 lines not on this page · raw message
===================================================================
arch/i386/lguest/page_tables.c new file +374 -0
196 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
===================================================================
arch/i386/lguest/segments.c new file +171 -0
171 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
===================================================================
include/asm-i386/lguest.h new file +86 -0
86 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
===================================================================
include/asm-i386/lguest_device.h new file +31 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
===================================================================
include/asm-i386/lguest_user.h new file +86 -0
86 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
PATCH 7/10 lguest: Simple lguest network driver.
This network driver operates both to the host process, and to other guests. It's pretty trivial.
===================================================================
drivers/net/Makefile +1 -0
@@ -217,3 +217,4 @@ obj-$(CONFIG_FS_ENET) += fs_enet/ obj-$(CONFIG_FS_ENET) += fs_enet/ obj-$(CONFIG_NETXEN_NIC) += netxen/ +obj-$(CONFIG_LGUEST_GUEST) += lguest_net.o
===================================================================
drivers/net/lguest_net.c new file +400 -0
400 lines not on this page · raw message
PATCH 8/10 lguest: console driver.
A trivial driver to have a basic lguest console, using the hvc_console infrastructure.
===================================================================
drivers/char/Makefile +1 -0
@@ -45,6 +45,7 @@ obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o h obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o obj-$(CONFIG_HVC_RTAS) += hvc_rtas.o +obj-$(CONFIG_LGUEST_GUEST) += hvc_lguest.o obj-$(CONFIG_HVC_DRIVER) += hvc_console.o obj-$(CONFIG_RAW_DRIVER) += raw.o obj-$(CONFIG_SGI_SNSC) += snsc.o snsc_event.o
===================================================================
drivers/char/hvc_lguest.c new file +99 -0
99 lines not on this page · raw message
PATCH 9/10 lguest: block driver
A simple block driver for lguest (/dev/lgbX). Only does one request at once.
===================================================================
drivers/block/Makefile +1 -0
@@ -28,4 +28,5 @@ obj-$(CONFIG_VIODASD) += viodasd.o obj-$(CONFIG_VIODASD) += viodasd.o obj-$(CONFIG_BLK_DEV_SX8) += sx8.o obj-$(CONFIG_BLK_DEV_UB) += ub.o +obj-$(CONFIG_LGUEST_GUEST) += lguest_blk.o
===================================================================
drivers/block/lguest_blk.c new file +260 -0
260 lines not on this page · raw message
PATCH 10/10 lguest: documentatation including example launcher
Name: Lguest documentatation and example launcher
Fairly complete documentation for lguest. I actually want to get rid of the "coding" part of lguest.txt and roll it into the code itself, literary-programming-style.
The launcher utility is also here: I don't have delusions of interface stability, so it makes sense to have it here as an example, and it's only 1000 lines.
===================================================================
Documentation/dontdiff +3 -0
@@ -144,3 +144,6 @@ wanxlfw.inc wanxlfw.inc uImage zImage +hypervisor-blob.c +lguest.lds +hypervisor-raw
===================================================================
Documentation/lguest/Makefile new file +21 -0
@@ -0,0 +1,21 @@ +# This creates the demonstration utility "lguest" which runs a Linux guest. + +# We rely on CONFIG_PAGE_OFFSET to know where to put lguest binary. +# Some shells (dash - ubunu) can't handle numbers that big so we cheat. +include ../../.config +LGUEST_GUEST_TOP := ($(CONFIG_PAGE_OFFSET) - 0x08000000) + +CFLAGS:=-Wall -Wmissing-declarations -Wmissing-prototypes -O3 \ + -static -DLGUEST_GUEST_TOP="$(LGUEST_GUEST_TOP)" -Wl,-T,lguest.lds +LDLIBS:=-lz + +all: lguest + +# The linker script on x86 is so complex the only way of creating one +# which will link our binary in the right place is to mangle the +# default one. +lguest.lds: + $(LD) --verbose | awk '/^==========/ { PRINT=1; next; } /SIZEOF_HEADERS/ { gsub(/0x[0-9A-F]*/, "$(LGUEST_GUEST_TOP)") } { if (PRINT) print $$0; }' > $@ + +clean: + rm -f lguest.lds lguest
===================================================================
Documentation/lguest/lguest.c new file +989 -0
989 lines not on this page · raw message
===================================================================
Documentation/lguest/lguest.txt new file +355 -0
355 lines not on this page · raw message
Andrew Morton
maintainerRe: [PATCH 6/10] lguest code: the little linux hypervisor.
In reply to Rusty Russellarch/i386/lguest/hypercalls.c line 133
Quoted 9 lines · On Fri, 09 Feb 2007 20:20:27 +1100 Rusty Russell <[email protected]> wrote:
arch/i386/lguest/hypercalls.c new file +9
-0+#define log (...) \ + do { \ + mm_segment_t oldfs = get_fs(); \ + char buf[100]; \ + sprintf(buf, "lguest:" __VA_ARGS__); \ + set_fs(KERNEL_DS); \ + sys_write(1, buf, strlen(buf)); \ + set_fs(oldfs); \ + } while(0)
Due to gcc shortcomings, each instance of this will chew an additional 100 bytes of stack. Unless they fixed it recently. Is a bit of a timebomb. I guess ksaprintf() could be used.
It also looks a bit, umm, innovative.
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 16 lines · Andrew Morton
arch/i386/lguest/hypercalls.c line 133
Quoted 9 lines · On Fri, 09 Feb 2007 20:20:27 +1100 Rusty Russell <[email protected]> wrote:
arch/i386/lguest/hypercalls.c new file +9
-0+#define log (...) \ + do { \ + mm_segment_t oldfs = get_fs(); \ + char buf[100]; \ + sprintf(buf, "lguest:" __VA_ARGS__); \ + set_fs(KERNEL_DS); \ + sys_write(1, buf, strlen(buf)); \ + set_fs(oldfs); \ + } while(0)Due to gcc shortcomings, each instance of this will chew an additional 100 bytes of stack. Unless they fixed it recently. Is a bit of a timebomb. I guess
ksaprintf()could be used.
Quoted 1 line
It also looks a bit, umm, innovative.
It's also unused 8)
It's an extremely useful macro for doing grossly invasive logging of the guest. I'll drop it if you prefer.
Cheers,
Rusty.
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 19 lines · Rusty Russell
Quoted 17 lines · Andrew Morton
arch/i386/lguest/hypercalls.c line 133
Quoted 10 lines · On Fri, 09 Feb 2007 20:20:27 +1100 Rusty Russell <[email protected]> wrote:
arch/i386/lguest/hypercalls.c new file +9
-0+#define log (...) \ + do { \ + mm_segment_t oldfs = get_fs(); \ + char buf[100]; \ + sprintf(buf, "lguest:" __VA_ARGS__); \ + set_fs(KERNEL_DS); \ + sys_write(1, buf, strlen(buf)); \ + set_fs(oldfs); \ + } while(0)Due to gcc shortcomings, each instance of this will chew an additional 100 bytes of stack. Unless they fixed it recently. Is a bit of a timebomb. I guess
ksaprintf()could be used.
Quoted 8 lines
Quoted 2 lines
It also looks a bit, umm, innovative.
It's also unused 8)
It's an extremely useful macro for doing grossly invasive logging of the guest. I'll drop it if you prefer.
Yes, it is a bit, umm, innovative. If it is going to be kept, even if just for devel logging, you should disable interrupts around it. Changing segments is not a normal thing to do.
Zach
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 4 lines
Yes, it is a bit, umm, innovative. If it is going to be kept, even if just for devel logging, you should disable interrupts around it. Changing segments is not a normal thing to do.
Actually that wouldn't be needed because interrupts are not allowed to do any user accesses. And contrary to the name it doesn't actually change the segment registers, only state used by *_user.
-Andi
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 6 lines · Andi Kleen
Quoted 2 lines
Yes, it is a bit, umm, innovative. If it is going to be kept, even if just for devel logging, you should disable interrupts around it. Changing segments is not a normal thing to do.
Actually that wouldn't be needed because interrupts are not allowed to do any user accesses. And contrary to the name it doesn't actually change the segment registers, only state used by
*_user.
My bad, I fell for the same mistake as everyone. Set_fs is just way too confusing of a name now. But good to know interrupts must be disable in such a circumstance.
Zach
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 6 lines · Zachary Amsden
Quoted 5 lines · Andi Kleen
Quoted 4 lines
Yes, it is a bit, umm, innovative. If it is going to be kept, even if just for devel logging, you should disable interrupts around it. Changing segments is not a normal thing to do.
Quoted 6 lines
Quoted 4 lines
Actually that wouldn't be needed because interrupts are not allowed to do any user accesses. And contrary to the name it doesn't actually change the segment registers, only state used by
*_user.My bad, I fell for the same mistake as everyone.
Set_fsis just way too
You could change the name. Only 654 occurrences all over the tree @)
Quoted 2 lines
confusing of a name now. But good to know interrupts must be disable in such a circumstance.
+not
-Andi
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 7 lines · Andi Kleen
Quoted 4 lines
Quoted 2 lines
My bad, I fell for the same mistake as everyone.
Set_fsis just way tooYou could change the name. Only 654 occurrences all over the tree @)
I'm preparing a patch. +not. This legacy thing is certainly taking a long time to disappear. Long live x86!
Quoted 6 lines
Quoted 3 lines
confusing of a name now. But good to know interrupts must be disable in such a circumstance.
+not
+ack
Zach
David Miller
maintainerRe: [PATCH 6/10] lguest code: the little linux hypervisor.
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Date: Fri, 9 Feb 2007 12:50:06 +0100
Quoted 8 lines
Quoted 4 lines
Yes, it is a bit, umm, innovative. If it is going to be kept, even if just for devel logging, you should disable interrupts around it. Changing segments is not a normal thing to do.
Actually that wouldn't be needed because interrupts are not allowed to do any user accesses. And contrary to the name it doesn't actually change the segment registers, only state used by
*_user.
That's right and we use this construct all throughout the syscall compatibility layer for 64-bit platforms.
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
In reply to Rusty RussellQuoted 10 lines · Rusty Russell
This is the core of lguest: both the guest code (always compiled in to the image so it can boot under lguest), and the host code (lg.ko).
There is only one config prompt at the moment: lguest is currently designed to run exactly the same guest and host kernels so we can frob the ABI freely.
Unfortunately, we don't have the build infrastructure for "private"
asm-offsets.hfiles, so there's a not-so-neat include inarch/i386/kernel/asm-offsets.c.
Ask the kbuild people to fix that?
It indeed looks ugly.
I bet Xen et.al. could make good use of that too.
arch/i386/lguest/Makefile line 20
Quoted 5 lines
arch/i386/lguest/Makefile new file +5
-0+# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@
an .S file with .incbin is more efficient and simpler
(note it has to be an separate .S file, otherwise icecream/distcc break)
It won't allow to show off any sed skills, but I guess we can live with that ;-)
arch/i386/lguest/core.c line 33
Quoted 7 lines
arch/i386/lguest/core.c new file +7
-0+static struct vm_struct *hypervisor_vma; +static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; +struct page *hype_pages;/* Contiguous pages. */
Statics? looks funky. Why only a single hypervisor_vma?
Quoted 9 lines
arch/i386/lguest/core.c new file +9
-0+struct lguest lguests[MAX_LGUEST_GUESTS ]; +DECLARE_MUTEX (lguest_lock); + +/* IDT entries are at start of hypervisor. */ +const unsigned long *__lguest_default_idt_entries (void ) +{ +return (void *)HYPE_ADDR ; +} +
arch/i386/lguest/core.c line 52
Quoted 10 lines
arch/i386/lguest/core.c new file +10
-0+/* Next is switch_to_guest */ +static void *__lguest_switch_to_guest (void ) +{ +return (void *)HYPE_ADDR +HYPE_DATA_SIZE ; +} + +/* Then we use everything else to hold guest state. */ +struct lguest_state *__lguest_states (void ) +{ +return (void *)HYPE_ADDR +sizeof (hypervisor_blob);
This cries for asm_offsets.h too, doesn't it?
arch/i386/lguest/core.c line 62
Quoted 10 lines
arch/i386/lguest/core.c new file +10
-0+} + +static __init int map_hypervisor (void ) +{ +unsigned int i; +int err; +struct page *pages[HYPERVISOR_PAGES ], **pagep = pages; + + hype_pages =alloc_pages (GFP_KERNEL |__GFP_ZERO, +get_order (HYPERVISOR_SIZE ));
Wasteful because of the rounding. Probably wants reintroduction of alloc_pages_exact()
arch/i386/lguest/core.c line 107
Quoted 5 lines
arch/i386/lguest/core.c new file +5
-0+ +static __exit void unmap_hypervisor(void) +{ +vunmap (hypervisor_vma->addr); +__free_pages (hype_pages,get_order (HYPERVISOR_SIZE ));
Shouldn't you clean up the GDTs too?
arch/i386/lguest/core.c line 119
Quoted 12 lines
arch/i386/lguest/core.c new file +12
-0+} + +/* IN/OUT insns: enough to get us past boot-time probing. */ +static int emulate_insn (struct lguest *lg) +{ +u8 insn; +unsigned int insnlen =0 , in =0 , shift =0 ; +unsigned long physaddr =guest_pa (lg, lg->state->regs.eip); + +/* This only works for addresses in linear mapping... */ +if (lg->state->regs.eip < lg->page_offset) +return 0 ;
Shouldn't there be a printk here?
arch/i386/lguest/core.c line 215
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+/* Saves exporting idt_table from kernel */ +static struct desc_struct *get_idt_table (void ) +{ +struct Xgt_desc_struct idt; + + asm("sidt %0" :"=m" (idt));
Nasty, but ok.
arch/i386/lguest/core.c line 219
Quoted 4 lines
arch/i386/lguest/core.c new file +4
-0+return (void *)idt.address; +} + +extern asmlinkage void math_state_restore(void);
No externs in .c files
arch/i386/lguest/core.c line 233
Quoted 9 lines
arch/i386/lguest/core.c new file +9
-0+ +/* Trap page resets this when it reloads gs. */ +static int new_gfp_eip (struct lguest *lg,struct lguest_regs *regs) +{ +u32 eip; +get_user (eip, &lg->lguest_data->gs_gpf_eip); +if (eip == regs->eip) +return 0 ; +put_user (regs->eip, &lg->lguest_data->gs_gpf_eip);
No fault checking?
lhread/write use probably also needs to be double checked that a malicious guest can't put the kernel into a loop.
arch/i386/lguest/core.c line 244
Quoted 11 lines
arch/i386/lguest/core.c new file +11
-0+return 1 ; +} + +static void set_ts (unsigned int guest_ts) +{ +u32 cr0; +if (guest_ts) { + asm("movl %%cr0,%0" :"=r" (cr0)); +if (!(cr0 &8 )) + asm("movl %0,%%cr0" : :"r" (cr0|8 )); + }
We have macros and defines for this in standard headers. \
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+while (!lg->dead) { +unsigned int cr2 =0 ;/* Damn gcc */ + +/* Hypercalls first: we might have been out to userspace */ +if (do_async_hcalls (lg)) + goto pending_dma;
arch/i386/lguest/core.c
Quoted 10 lines
arch/i386/lguest/core.c new file +10
-0+ +if (regs->trapnum ==LGUEST_TRAP_ENTRY ) { +/* Only do hypercall once. */ + regs->trapnum =255 ; +if (hypercall (lg, regs)) + goto pending_dma; + } + +if (signal_pending (current)) +return -EINTR
Probably needs freezer checking here somewhere.
arch/i386/lguest/core.c line 285
Quoted 9 lines
;
arch/i386/lguest/core.c new file +8
-0+maybe_do_interrupt (lg); + +if (lg->dead) +break ; + +if (lg->halted) { +set_current_state (TASK_INTERRUPTIBLE ); +schedule_timeout (1 );
1? And what is that good for anyways?
arch/i386/lguest/core.c line 322
Quoted 1 line
arch/i386/lguest/core.c new file +1
-0+/* FIXME: If it's reloading %gs in a loop? */
Yes what then? Have you tried it?
In general i miss printks when things go wrong. Do you expect all users to have a gdbstub ready? @)
arch/i386/lguest/core.c line 365
Quoted 3 lines
arch/i386/lguest/core.c new file +3
-0+pending_dma: +put_user (lg->pending_dma, (unsigned long *)user); +put_user (lg->pending_addr, (unsigned long *)user+1 );
error checking? How do you avoid loops?
arch/i386/lguest/core.c line 405
Quoted 3 lines
arch/i386/lguest/core.c new file +3
-0+if (cpu_has_pge) {/* We have a broader idea of "global". */ + cpu_had_pge =1 ; +on_each_cpu (adjust_pge,0 ,0 ,1 );
cpu hotplug?
arch/i386/lguest/hypercalls.c line 54
Quoted 9 lines
arch/i386/lguest/hypercalls.c new file +8
-0+clear_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); + } +return 0 ; +} +case LHCALL_CRASH : { +char msg[128 ]; +lhread (lg, msg, regs->edx,sizeof (msg)); + msg[sizeof (msg)-1 ] ='\0' ;
Might be safer to vet for isprint here
arch/i386/lguest/hypercalls.c line 128
Quoted 4 lines
arch/i386/lguest/hypercalls.c new file +4
-0+#define log (...) \ + do { \ + mm_segment_t oldfs = get_fs(); \ + char buf[100]; \
At least older gccs will accumulate the bufs in a function, eventually possibly blowing the stack. Better use a function.
arch/i386/lguest/interrupts_and_traps.c line 80
Quoted 4 lines
arch/i386/lguest/interrupts_and_traps.c new file +4
-0+/* If they're halted, we re-enable interrupts. */ +if (lg->halted) { +/* Re-enable interrupts. */ +put_user (512 , &lg->lguest_data->irq_enabled);
interesting magic number
Quoted 9 lines
arch/i386/lguest/interrupts_and_traps.c new file +9
-0+/* Ignore NMI, doublefault, hypercall, spurious interrupt. */ +if (i ==2 || i ==8 || i ==15 || i ==LGUEST_TRAP_ENTRY ) +return ; +/* FIXME: We should handle debug and int3 */ +else if (i ==1 || i ==3 ) +return ; +/* We intercept page fault, general protection fault and fpu missing */ +else if (i ==13 ) +copy_trap (lg, &lg->gpf_trap, &d);
arch/i386/lguest/interrupts_and_traps.c
Quoted 10 lines
arch/i386/lguest/interrupts_and_traps.c new file +10
-0+else if (i ==14 ) +copy_trap (lg, &lg->page_trap, &d); +else if (i ==7 ) +copy_trap (lg, &lg->fpu_trap, &d); +/* Other traps go straight to guest. */ +else if (i <FIRST_EXTERNAL_VECTOR || i ==SYSCALL_VECTOR ) +setup_idt (lg, i, &d); +/* A virtual interrupt */ +else if (i <FIRST_EXTERNAL_VECTOR +LGUEST_IRQS ) +copy_trap (lg, &lg->interrupt[i-FIRST_EXTERNAL_VECTOR ], &d);\
switch is not cool enough anymore?
Review
Quoted 2 lines
(quoted diff) +1
-0+ down(&lguest_lock);
i suspect mutexes are the new way to do this
arch/i386/lguest/io.c line 105
Quoted 4 lines
arch/i386/lguest/io.c new file +4
-0+down_read (¤t->mm->mmap_sem); +if (get_futex_key ((u32 __user *)addr, &key) !=0 ) { +kill_guest (lg,"bad dma address %#lx" , addr); + goto unlock;
Risky? Use probe_kernel_address et.al.?
arch/i386/lguest/lg.h line 15
Quoted 2 lines
arch/i386/lguest/lg.h new file +2
-0+#if 0 +/* FIXME: Use asm-offsets here... */
Remove?
Quoted 1 line
+extern int
mce_disabled;
tststs
arch/i386/lguest/lguest.c line 148
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+ +/* FIXME: Update iff tsc rate changes. */
It does.
arch/i386/lguest/lguest.c line 181
Quoted 11 lines
arch/i386/lguest/lguest.c new file +11
-0+static fastcall void lguest_cpuid(unsigned int *eax, unsigned int *ebx, +unsigned int *ecx,unsigned int *edx) +{ +int is_feature = (*eax ==1 ); + + asmvolatile ("cpuid" + :"=a" (*eax), +"=b" (*ebx), +"=c" (*ecx), +"=d" (*edx) + :"0" (*eax),"2" (*ecx));
What's wrong with the standard cpuid*() macros?
arch/i386/lguest/lguest.c line 485
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+extern struct Xgt_desc_struct cpu_gdt_descr; +extern struct i386_pda boot_pda;
No externs in .c
arch/i386/lguest/lguest.c line 487
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+ + paravirt_ops.name ="lguest" ;
Can you just statically initialize this and then copy over?
arch/i386/lguest/lguest.c line 551
Quoted 1 line
arch/i386/lguest/lguest.c new file +1
-0+ asmvolatile ("mov %0, %%gs" : :"r" (__KERNEL_PDA) :"memory" );
This will be %fs soon.
... haven't read everything else. the IO driver earlier was also not very closely looked at.
-Andi
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 10 lines · Andi Kleen
Quoted 3 lines · Rusty Russell
Unfortunately, we don't have the build infrastructure for "private"
asm-offsets.hfiles, so there's a not-so-neat include inarch/i386/kernel/asm-offsets.c.Ask the kbuild people to fix that?
It indeed looks ugly.
I bet Xen et.al. could make good use of that too.
Yes. I originally had the constants #defined in the header and a whole
heap of BUILD_BUG_ON(XYZ_offset != offsetof(xyz)) in my module, which was even uglier (but at least contained in my code).
Quoted 10 lines
arch/i386/lguest/Makefile line 20
Quoted 5 lines
arch/i386/lguest/Makefile new file +5
-0+# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@an .S file with .incbin is more efficient and simpler
(note it has to be an separate .S file, otherwise icecream/distcc break)It won't allow to show off any sed skills, but I guess we can live with that ;-)
Good idea, except I currently use sizeof(hypervisor_blob): I'd have to extract the size separately and hand it in the CFLAGS 8(
Quoted 9 lines
arch/i386/lguest/core.c line 33
Quoted 7 lines
arch/i386/lguest/core.c new file +7
-0+static struct vm_struct *hypervisor_vma; +static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; +struct page *hype_pages;/* Contiguous pages. */ Statics? looks funky. Why only a single
hypervisor_vma?
We only have one switcher: it contains an array of "struct lguest_state"; one for each guest. (This is host code we're looking at here).
Quoted 10 lines
Quoted 10 lines
arch/i386/lguest/core.c new file +10
-0+/* IDT entries are at start of hypervisor. */ +const unsigned long *__lguest_default_idt_entries (void ) +{ +return (void *)HYPE_ADDR ; +} + +/* Next is switch_to_guest */ +static void *__lguest_switch_to_guest (void ) +{ +return (void *)HYPE_ADDR +HYPE_DATA_SIZE ;
Quoted 8 lines
arch/i386/lguest/core.c line 52
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+} + +/* Then we use everything else to hold guest state. */ +struct lguest_state *__lguest_states (void ) +{ +return (void *)HYPE_ADDR +sizeof (hypervisor_blob);This cries for
asm_offsets.htoo, doesn't it?
HYPE_DATA_SIZE is the size of the hypervisor.S data segment, I'm not sure how I'd get that into asm_offsets....
Quoted 5 lines
Quoted 5 lines
arch/i386/lguest/core.c new file +5
-0+} + +static __init int map_hypervisor (void ) +{ +unsigned int i;
Quoted 8 lines
arch/i386/lguest/core.c line 62
Quoted 5 lines
arch/i386/lguest/core.c new file +5
-0+int err; +struct page *pages[HYPERVISOR_PAGES ], **pagep = pages; + + hype_pages =alloc_pages (GFP_KERNEL |__GFP_ZERO, +get_order (HYPERVISOR_SIZE ));Wasteful because of the rounding. Probably wants reintroduction of
alloc_pages_exact()
HYPERVISOR_SIZE here is 64k, so it's actually OK (we use the space after the ~3k text & data from hypervisor.S to hold as many struct lguest_state's as we can). The name is bad tho; SWITCHER_MAP_SIZE would probably be better...
Quoted 7 lines
arch/i386/lguest/core.c line 107
Quoted 5 lines
arch/i386/lguest/core.c new file +5
-0+ +static __exit void unmap_hypervisor(void) +{ +vunmap (hypervisor_vma->addr); +__free_pages (hype_pages,get_order (HYPERVISOR_SIZE ));Shouldn't you clean up the GDTs too?
I could, but there doesn't seem a great deal of point. If anyone else is using them, we're in trouble already...
Quoted 4 lines
Quoted 4 lines
arch/i386/lguest/core.c new file +4
-0+/* IN/OUT insns: enough to get us past boot-time probing. */ +static int emulate_insn (struct lguest *lg) +{ +u8 insn;
Quoted 8 lines
arch/i386/lguest/core.c line 119
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+unsigned int insnlen =0 , in =0 , shift =0 ; +unsigned long physaddr =guest_pa (lg, lg->state->regs.eip); + +/* This only works for addresses in linear mapping... */ +if (lg->state->regs.eip < lg->page_offset) +return 0 ;Shouldn't there be a printk here?
No, the guest should not be able to evoke a printk from the host kernel. In this case, however, we'll reflect the trap to the kernel, or if the kernel hasn't installed an IDT yet we'll kill the guest and the lguest program will get the string "unhandled trap 13 at <EIP> (err=0)".
Basically this emulation code is simply to get us through all that annoying early boot probing (paravirt_ops does *not* override in/out).
Quoted 9 lines
arch/i386/lguest/core.c line 215
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+/* Saves exporting idt_table from kernel */ +static struct desc_struct *get_idt_table (void ) +{ +struct Xgt_desc_struct idt; + + asm("sidt %0" :"=m" (idt));Nasty, but ok.
Quoted 6 lines
arch/i386/lguest/core.c line 219
Quoted 4 lines
arch/i386/lguest/core.c new file +4
-0+return (void *)idt.address; +} + +extern asmlinkage void math_state_restore(void);No externs in .c files
Agreed, will fix.
Quoted 11 lines
arch/i386/lguest/core.c line 233
Quoted 9 lines
arch/i386/lguest/core.c new file +9
-0+ +/* Trap page resets this when it reloads gs. */ +static int new_gfp_eip (struct lguest *lg,struct lguest_regs *regs) +{ +u32 eip; +get_user (eip, &lg->lguest_data->gs_gpf_eip); +if (eip == regs->eip) +return 0 ; +put_user (regs->eip, &lg->lguest_data->gs_gpf_eip);No fault checking?
What do we care if the guest process has unmapped the lguest_data page:
it's his funeral. We could put a kill_guest there, but it seems a waste of code to me.
Quoted 2 lines
lhread/write use probably also needs to be double checked that a malicious guest can't put the kernel into a loop.
How?
Quoted 10 lines
arch/i386/lguest/core.c line 244
Quoted 8 lines
arch/i386/lguest/core.c new file +8
-0+static void set_ts (unsigned int guest_ts) +{ +u32 cr0; +if (guest_ts) { + asm("movl %%cr0,%0" :"=r" (cr0)); +if (!(cr0 &8 )) + asm("movl %0,%%cr0" : :"r" (cr0|8 )); + }We have macros and defines for this in standard headers.
This was in prep to unexport paravirt_ops, but you threatened the freeze on me so I pushed this ahead.
Some of this code will get cleaned up along with others when that patch goes through (my idea is that we'll make sure to expose the native versions as inlines, so this code can use native_read_cr0 etc. directly. lguest hosts cannot be paravirtualized anyway 8).
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (signal_pending(current)) + return -EINTRProbably needs freezer checking here somewhere.
Yes, that code always mystified me...
Quoted 5 lines
arch/i386/lguest/core.c line 285
Quoted 3 lines
arch/i386/lguest/core.c new file +3
-0+if (lg->halted) { +set_current_state (TASK_INTERRUPTIBLE ); +schedule_timeout (1 );1? And what is that good for anyways?
It's waiting for the dynamic tick code to go in. Again, I was hoping that that would go in and I could go in behind it, but you made it clear that waiting longer isn't an option.
Quoted 6 lines
arch/i386/lguest/core.c line 322
Quoted 1 line
arch/i386/lguest/core.c new file +1
-0+/* FIXME: If it's reloading %gs in a loop? */ Yes what then? Have you tried it?
In general i miss printks when things go wrong. Do you expect all users to have a gdbstub ready? @)
If this happens then the process inside the guest which is doing this will get a Segmentation Fault, because we'll deliver a GPF to the kernel at that EIP. In practice, glibc doesn't do this that I have found. The note is there so I think harder about it if anyone reports strange segvs under lguest 8)
Quoted 5 lines
arch/i386/lguest/core.c line 365
Quoted 3 lines
arch/i386/lguest/core.c new file +3
-0+pending_dma: +put_user (lg->pending_dma, (unsigned long *)user); +put_user (lg->pending_addr, (unsigned long *)user+1 );error checking? How do you avoid loops?
Why? And I still don't get what loops?
Quoted 5 lines
arch/i386/lguest/core.c line 405
Quoted 3 lines
arch/i386/lguest/core.c new file +3
-0+if (cpu_has_pge) {/* We have a broader idea of "global". */ + cpu_had_pge =1 ; +on_each_cpu (adjust_pge,0 ,0 ,1 );cpu hotplug?
Good catch. Should lock out cpu hotplug during this. Once it's done, we're fine, because we clear the feature bit. Same when putting it back.
Quoted 11 lines
arch/i386/lguest/hypercalls.c line 54
Quoted 9 lines
arch/i386/lguest/hypercalls.c new file +8
-0+clear_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); + } +return 0 ; +} +case LHCALL_CRASH : { +char msg[128 ]; +lhread (lg, msg, regs->edx,sizeof (msg)); + msg[sizeof (msg)-1 ] ='\0' ;Might be safer to vet for isprint here.
Hmm, I hope the kasprintf won't barf... this buffer gets handed straight to the lguest process on its next read.
Quoted 7 lines
arch/i386/lguest/hypercalls.c line 128
Quoted 4 lines
arch/i386/lguest/hypercalls.c new file +4
-0+#define log (...) \ + do { \ + mm_segment_t oldfs = get_fs(); \ + char buf[100]; \At least older gccs will accumulate the bufs in a function, eventually possibly blowing the stack. Better use a function.
Or I'll kill the macro altogether. It's useful for shotgun debugging a guest, but it's unused.
Quoted 6 lines
arch/i386/lguest/interrupts_and_traps.c line 80
Quoted 4 lines
arch/i386/lguest/interrupts_and_traps.c new file +4
-0+/* If they're halted, we re-enable interrupts. */ +if (lg->halted) { +/* Re-enable interrupts. */ +put_user (512 , &lg->lguest_data->irq_enabled);interesting magic number
Yeah, we don't define it anywhere, and we probably should. From irqflags.h:
static inline int raw_irqs_disabled_flags(unsigned long flags)
{
return !(flags & (1 << 9));
}8(
Quoted 13 lines
Quoted 13 lines
arch/i386/lguest/interrupts_and_traps.c new file +13
-0+/* Ignore NMI, doublefault, hypercall, spurious interrupt. */ +if (i ==2 || i ==8 || i ==15 || i ==LGUEST_TRAP_ENTRY ) +return ; +/* FIXME: We should handle debug and int3 */ +else if (i ==1 || i ==3 ) +return ; +/* We intercept page fault, general protection fault and fpu missing */ +else if (i ==13 ) +copy_trap (lg, &lg->gpf_trap, &d); +else if (i ==14 ) +copy_trap (lg, &lg->page_trap, &d); +else if (i ==7 ) +copy_trap (lg, &lg->fpu_trap, &d);
Quoted 8 lines
arch/i386/lguest/interrupts_and_traps.c
Quoted 6 lines
arch/i386/lguest/interrupts_and_traps.c new file +6
-0+/* Other traps go straight to guest. */ + else if (i < FIRST_EXTERNAL_VECTOR || i == SYSCALL_VECTOR) + setup_idt(lg, i, &d); +/* A virtual interrupt */ + else if (i < FIRST_EXTERNAL_VECTOR + LGUEST_IRQS) + copy_trap(lg, &lg->interrupt[i-FIRST_EXTERNAL_VECTOR], &d);\switch is not cool enough anymore?
It would have to be a switch then gunk at the bottom, because those last two tests don't switch-ify. IIRC I changed back from a switch because of that.
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ down(&lguest_lock);i suspect mutexes are the new way to do this
Yep, will replace.
Quoted 6 lines
arch/i386/lguest/io.c line 105
Quoted 4 lines
arch/i386/lguest/io.c new file +4
-0+down_read (¤t->mm->mmap_sem); +if (get_futex_key ((u32 __user *)addr, &key) !=0 ) { +kill_guest (lg,"bad dma address %#lx" , addr); + goto unlock;Risky? Use
probe_kernel_addresset.al.?
No, get_futex_key handles anything; we rely on that in futex.c
Quoted 4 lines
arch/i386/lguest/lg.h line 15
Quoted 2 lines
arch/i386/lguest/lg.h new file +2
-0+#if 0 +/* FIXME: Use asm-offsets here... */ Remove?
Yep, done in the split-up version...
Quoted 3 lines
Quoted 1 line
+extern int
mce_disabled;tststs
I'll get that too 8)
Quoted 4 lines
arch/i386/lguest/lguest.c line 148
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+ +/* FIXME: Update iff tsc rate changes. */ It does.
Hey, but I have a FIXME for it!
Quoted 5 lines
Quoted 5 lines
arch/i386/lguest/lguest.c new file +5
-0+static fastcall void lguest_cpuid(unsigned int *eax, unsigned int *ebx, +unsigned int *ecx,unsigned int *edx) +{ +int is_feature = (*eax ==1 ); +
Quoted 8 lines
arch/i386/lguest/lguest.c line 181
Quoted 6 lines
arch/i386/lguest/lguest.c new file +6
-0+ asmvolatile ("cpuid" + :"=a" (*eax), +"=b" (*ebx), +"=c" (*ecx), +"=d" (*edx) + :"0" (*eax),"2" (*ecx));What's wrong with the standard cpuid*() macros?
Good call... we expose native_cpuid so I should use that (I can't use
cpuid: this *is* cpuid!)
Quoted 4 lines
arch/i386/lguest/lguest.c line 485
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+extern struct Xgt_desc_struct cpu_gdt_descr; +extern struct i386_pda boot_pda;No externs in .c
Yes, ugly code. Usually this stuff is done in asm, so it doesn't exist in a header. I'll fix it.
Quoted 4 lines
arch/i386/lguest/lguest.c line 487
Quoted 2 lines
arch/i386/lguest/lguest.c new file +2
-0+ + paravirt_ops.name ="lguest" ;Can you just statically initialize this and then copy over?
Sure, but then I'll hit all the things I don't want to override, and it'll be far less clear.
Quoted 3 lines
arch/i386/lguest/lguest.c line 551
Quoted 1 line
arch/i386/lguest/lguest.c new file +1
-0+ asmvolatile ("mov %0, %%gs" : :"r" (__KERNEL_PDA) :"memory" );This will be %fs soon.
I know, I had to change this back to backport from -mm tree 8( Fortunately, the breakage is *really* obvious when it happens.
Quoted 1 line
... haven't read everything else. the IO driver earlier was also not very closely looked at.
OK, I'll resend patches (in 4 parts) based on these comments (and Andrew's).
Thanks!
Rusty.
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 10 lines · Rusty Russell
Quoted 9 lines · Andi Kleen
arch/i386/lguest/Makefile line 20
Quoted 5 lines
arch/i386/lguest/Makefile new file +5
-0+# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@an .S file with .incbin is more efficient and simpler
(note it has to be an separate .S file, otherwise icecream/distcc break)
Quoted 4 lines
Quoted 1 line
It won't allow to show off any sed skills, but I guess we can live with that ;-)
Good idea, except I currently use
sizeof(hypervisor_blob): I'd have to extract the size separately and hand it in the CFLAGS 8(
hypervisor_start:
.incbin "hypervisor"hypervisor_end:
...
extern char hypervisor_start[], hypervisor_end[]; size = hypervisor_end - hypervisor_start;
Quoted 9 lines
Quoted 8 lines
arch/i386/lguest/core.c line 33
Quoted 6 lines
arch/i386/lguest/core.c new file +6
-0+static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; +struct page *hype_pages;/* Contiguous pages. */ Statics? looks funky. Why only a single
hypervisor_vma?
Quoted 3 lines
We only have one switcher: it contains an array of "struct
lguest_state"; one for each guest. (This is host code we're looking at here).
This means it is not SMP safe?
Quoted 1 line
No, the guest should not be able to evoke a printk from the host kernel.
This means nobody will know why it failed.
Quoted 11 lines
Quoted 7 lines
arch/i386/lguest/interrupts_and_traps.c
Quoted 5 lines
arch/i386/lguest/interrupts_and_traps.c new file +5
-0+else if (i <FIRST_EXTERNAL_VECTOR || i ==SYSCALL_VECTOR ) +setup_idt (lg, i, &d); +/* A virtual interrupt */ +else if (i <FIRST_EXTERNAL_VECTOR +LGUEST_IRQS ) +copy_trap (lg, &lg->interrupt[i-FIRST_EXTERNAL_VECTOR ], &d);\switch is not cool enough anymore?
It would have to be a switch then gunk at the bottom, because those last two tests don't switch-ify. IIRC I changed back from a switch because of that.
gcc has a handy extension for this:
case 0...FIRST_EXTERNAL_VECTOR-1:
case SYSCALL_VECTOR:
case FIRST_EXTERNAL_VECTOR...FIRST_EXTERNAL_VECTOR+LGUEST_IRQS:
Re: the loops; e.g. we used to have possible loop cases when a page fault does read instructions and then causes another page fault etc.etc. I haven't seen any immediate danger of this, but it might be worth double checking.
-Andi
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 16 lines · Andi Kleen
Quoted 10 lines · Rusty Russell
Quoted 9 lines · Andi Kleen
arch/i386/lguest/Makefile line 20
Quoted 5 lines
arch/i386/lguest/Makefile new file +5
-0+# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@an .S file with .incbin is more efficient and simpler
(note it has to be an separate .S file, otherwise icecream/distcc break)Quoted 4 lines
Quoted 1 line
It won't allow to show off any sed skills, but I guess we can live with that ;-)
Good idea, except I currently use
sizeof(hypervisor_blob): I'd have to extract the size separately and hand it in the CFLAGS 8(
Quoted 8 lines
hypervisor_start:
.incbin "hypervisor"hypervisor_end:...
extern char hypervisor_start[], hypervisor_end[]; size = hypervisor_end - hypervisor_start;
#define MAX_LGUEST_GUESTS \
((HYPERVISOR_SIZE-sizeof(hypervisor_blob))/sizeof(struct lguest_state))
struct lguest lguests[MAX_LGUEST_GUESTS];
I could kmalloc that array, of course, but is it worth it to get rid of one line in a Makefile?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Statics? looks funky. Why only a single
hypervisor_vma?We only have one switcher: it contains an array of "struct
lguest_state"; one for each guest. (This is host code we're looking at here).This means it is not SMP safe?
No, it's host-SMP safe. There's no guest SMP support though, which keeps things nice and simple.
Quoted 3 lines
Quoted 1 line
No, the guest should not be able to evoke a printk from the host kernel.
This means nobody will know why it failed.
No, that's why the lguest process gets the error string (and prints it out). Biggest usability improvement I made in a while.
The kernel log is the absolute worst place to report errors; iptables and module code both do that and the #1 FAQ is "What happened?"
I didn't make the same mistake this (third) time! Here's lguest when the guest crashes:
# lguest 64m bzImage ...
...
lguest: CRASH: Attempted to kill init!
# Quoted 9 lines
Quoted 3 lines
It would have to be a switch then gunk at the bottom, because those last two tests don't switch-ify. IIRC I changed back from a switch because of that.
gcc has a handy extension for this:
case 0...FIRST_EXTERNAL_VECTOR-1:
caseSYSCALL_VECTOR:
case FIRST_EXTERNAL_VECTOR...FIRST_EXTERNAL_VECTOR+LGUEST_IRQS:
Indeed, and I really wanted to use it, but it still doesn't allow overlapping ranges. The first cases are in the middle of 0 ... FIRST_EXTERNAL_VECTOR-1, and if LGUEST_IRQS is large enough, SYSCALL_VECTOR is in the middle of FIRST_EXTERNAL_VECTOR ... FIRST_EXTERNAL_VECTOR+LGUEST_IRQS 8(
Nonetheless, I switchified what I could in my update (testing now).
Quoted 4 lines
Re: the loops; e.g. we used to have possible loop cases when a page fault does read instructions and then causes another page fault etc.etc. I haven't seen any immediate danger of this, but it might be worth double checking.
OK, I'll run some tests here. There shouldn't be any danger here though....
Thanks!
Rusty.
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
In reply to Rusty RussellQuoted 8 lines · Rusty Russell
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Rusty Russell
Unfortunately, we don't have the build infrastructure for "private"
asm-offsets.hfiles, so there's a not-so-neat include inarch/i386/kernel/asm-offsets.c.Ask the kbuild people to fix that?
Quoted 7 lines
Quoted 3 lines
It indeed looks ugly.
I bet Xen et.al. could make good use of that too.
Yes. I originally had the constants #defined in the header and a whole
heap ofBUILD_BUG_ON(XYZ_offset != offsetof(xyz))in my module, which was even uglier (but at least contained in my code).
I do not quite see what you ask for.
Care to try to describe the problem a bit then I may look at it sometime.
[Heading for vacation in a few hours so no prompt reply]
Sam
Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Quoted 4 lines · Sam Ravnborg
I do not quite see what you ask for.
Care to try to describe the problem a bit then I may look at it sometime.[Heading for vacation in a few hours so no prompt reply]
I'd like my own, private "asm-offsets.h". In this case, in arch/i386/lguest/. I guess it's a matter of extracting the core of the asm-offsets.h magic and generalizing it.
Have a good break!
Rusty.
[q] kbuild for private asm-offsets (Re: [PATCH 6/10] lguest code: the little linux hypervisor.)
Quoted 4 lines
From: Rusty Russell
Newsgroups: gmane.linux.kernel,gmane.linux.kernel.virtualization
Subject: Re: [PATCH 6/10] lguest code: the little linux hypervisor.
Date: Sat, 10 Feb 2007 02:23:19 +1100
Hallo, Rusty, guys.
Quoted 12 lines · Sam Ravnborg
Quoted 4 lines
I do not quite see what you ask for.
Care to try to describe the problem a bit then I may look at it sometime.[Heading for vacation in a few hours so no prompt reply]
I'd like my own, private "
asm-offsets.h". In this case, inarch/i386/lguest/. I guess it's a matter of extracting the core of theasm-offsets.hmagic and generalizing it.Have a good break!
Rusty.
If you will have time for newbie, to explain in a few words, what is it need for (whole idea, or key detail), and, maybe, why it is generated so ... interestingly:
asm-offsets.c -> *.s -> *.h
(but this looks like interconnecting C and assembler, obviously)
I will glad to help providing solution maybe somewhat earlier (well, i'm trying to understand whole building process, if that matters).
.... And, of course, if this q isn't dumb.
Thanks.
____
Re: [q] kbuild for private asm-offsets (Re: [PATCH 6/10] lguest code: the little linux hypervisor.)
Quoted 8 lines
If you will have time for newbie, to explain in a few words, what is it need for (whole idea, or key detail), and, maybe, why it is generated so ... interestingly:
asm-offsets.c -> *.s -> *.h(but this looks like interconnecting C and assembler, obviously)
I will glad to help providing solution maybe somewhat earlier (well, i'm trying to understand whole building process, if that matters).
The problem is trying to figure out what needs to be done to get multiple asm-offsets.h.
-Andi
Re: [q] kbuild for private asm-offsets (Re: [PATCH 6/10] lguest code: the little linux hypervisor.)
In reply to Oleg VerychQuoted 6 lines
If you will have time for newbie, to explain in a few words, what is it need for (whole idea, or key detail), and, maybe, why it is generated so ... interestingly:
asm-offsets.c -> *.s -> *.h(but this looks like interconnecting C and assembler, obviously)
Correct - asm-offsets is used to transfer constants for .c to assembler, which us especially convinient for constant that differ when a stuct is changed.
Quoted 3 lines
I will glad to help providing solution maybe somewhat earlier (well, i'm trying to understand whole building process, if that matters).
Basically what is requested is to move the generic parts of asm-offsets generation from top-level Kbuild file to a generic file somewhere.
Since this is not generic Kbuild functionality but more specific to the kernel it should not be part of Kbuild generic files. include/asm-generic/asm-offsets seems to be a fair place for it.
It would contain the sed-y parts as well as cmd_offsets definition.
I would be glad if you could give that a spin.
Sam
Re: [q] kbuild for private asm-offsets (Re: [PATCH 6/10] lguest code: the little linux hypervisor.)
In reply to Oleg VerychQuoted 4 lines · Oleg Verych
Quoted 4 lines
I'd like my own, private "
asm-offsets.h". In this case, inarch/i386/lguest/. I guess it's a matter of extracting the core of theasm-offsets.hmagic and generalizing it.
Quoted 8 lines
Quoted 2 lines
Have a good break!
Rusty.If you will have time for newbie, to explain in a few words, what is it need for (whole idea, or key detail), and, maybe, why it is generated so ... interestingly:
asm-offsets.c -> *.s -> *.h(but this looks like interconnecting C and assembler, obviously)
Hi Oleg,
Always happy to explain. There's often a need to access constants in
assembler, which can only be derived from C, such as the size of a structure, or the offset of a certain member within a structure. Hardcoding the numbers in assembler is fragile leading to breakage when something changes.
So, asm-offsets.c is the solution: it uses asm() statements to emit
patterns in the assembler, with the compiler computing the actual numbers, eg:
#define DEFINE(sym, val) \
asm volatile("\n->" #sym " %0 " #val : : "i" (val))
DEFINE(SIZEOF_FOOBAR, sizeof(foobar));Becomes in asm-offsets.s:
->SIZEOF_FOOBAR $10 sizeof(foobar) #
This gets sed'd back into asm-offsets.h:
#define SIZEOF_FOOBAR 10 /* SIZEOF_FOOBAR # */
This can be included from .S files (which get passed through the pre-processor).
Hope that helps!
Rusty.
Re: [q] kbuild for private asm-offsets (Re: [PATCH 6/10] lguest code: the little linux hypervisor.)
Quoted 1 line · Rusty Russell
Always happy to explain.
Thanks!
[]
Quoted 5 lines
So,
asm-offsets.cis the solution: it usesasm()statements to emit
patterns in the assembler, with the compiler computing the actual numbers, eg:#define DEFINE(sym, val) \
(1) asm volatile("\n->" #sym " %0 " #val : : "i" (val))
Quoted 2 lines
DEFINE(SIZEOF_FOOBAR, sizeof(foobar));
(2) Becomes in asm-offsets.s:
Quoted 2 lines
->SIZEOF_FOOBAR $10 sizeof(foobar) #
(3) This gets sed'd back into asm-offsets.h:
Quoted 4 lines
#define SIZEOF_FOOBAR 10 /* SIZEOF_FOOBAR # */This can be included from .S files (which get passed through the pre-processor).
So, to make this parsing job clear once again, (1) is a pattern generator of (2) for sed (this is not assembler), which is making final (3) then. And this are actually C-for-asm offsets (asm-offsets.h is confusing to me).
On Mon, Feb 12, 2007 at 10:41:28PM +0100, Sam Ravnborg wrote:
[]
Quoted 9 lines
Quoted 2 lines
I will glad to help providing solution maybe somewhat earlier (well, i'm trying to understand whole building process, if that matters).
Basically what is requested is to move the generic parts of asm-offsets generation from top-level
Kbuildfile to a generic file somewhere.Since this is not generic
Kbuildfunctionality but more specific to the kernel it should not be part ofKbuildgeneric files.include/asm-generic/asm-offsetsseems to be a fair place for it.
Too much "generic" were used. Anyways, simply thing is, that there must be:
-- hardware file(s) for Arch;
-- "private" software file(s), for paravirt and such.
On Mon, Feb 12, 2007 at 06:24:13PM +0100, Andi Kleen wrote:
[]
Quoted 2 lines
The problem is trying to figure out what needs to be done to get multiple
asm-offsets.h.
Proposition will follow.
Kind regards.
P.S.
While it was fun to run kinds of offtopic operation systems in virtual machine back in 2000-2001. Now it isn't.
Mainly due to yet another zoo. May be this is good for quick adoption, commercial benefit, etc. Yet thankfully to Rusty, general useful infrastructure is available now. Kudos to everyone :)
____
[pp] kbuild: lguest with private asm-offsets (and some bloat)
On Tue, Feb 13, 2007 at 04:10:44AM +0100, Oleg Verych wrote:
[]
Quoted 3 lines
Proposition will follow.
[]
[patch proposition] kbuild: lguest with private asm-offsets
* added some bloat to lguest's Makefile:
- lguest doesn't rebuild, if not changed (due to FORCED implicit %o:%S),
- support of kbuild's queit/nonquiet commands,
- (hopefully) more readable and clear build process,
- private asm-offsets,
* some whitespace was killed,
* needs "asm-offsets magic demystified, generalized".
Done on top of first lkml post of lguest.
pp-by: Oleg Verych
--- arch/i386/kernel/asm-offsets.c | 23 +----------------- arch/i386/lguest/Makefile | 40 +++++++++++++++++++++++++++++--- arch/i386/lguest/asm-offsets.c | 31 +++++++++++++++++++++++++ arch/i386/lguest/hypervisor.S | 8 +++---
4 files changed, 73 insertions(+), 29 deletions(-)
arch/i386/lguest/Makefile +36 -4
@@ -1,2 +1,6 @@ +# +# i386/lguest/Makefile +# + # Guest requires the paravirt_ops replacement and the bus driver. obj-$(CONFIG_LGUEST_GUEST) += lguest.o lguest_bus.o
@@ -7,4 +11,6 @@ lg-objs := core.o hypercalls.o page_tabl segments.o io.o lguest_user.o +asm-offsets-lg := $(objtree)/include/asm/asm-offsets-lg.h + # We use top 4MB for guest traps page, then hypervisor. */ HYPE_ADDR := (0xFFC00000+4096)
@@ -13,10 +19,36 @@ HYPE_DATA_SIZE := 1024 CFLAGS += -DHYPE_ADDR="$(HYPE_ADDR)" -DHYPE_DATA_SIZE="$(HYPE_DATA_SIZE)" +LD_HYPE_DATA := $(shell printf %\#x $$(($(HYPE_ADDR)))) +LD_HYPE_TEXT := $(shell printf %\#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))) + +quiet_cmd_ld_hyber = LD [H] $@ + cmd_ld_hyber = $(LD) -static -Tdata=$(LD_HYPE_DATA) \ + -Ttext=$(LD_HYPE_TEXT) -o $@ $< +cmd_objcopy = $(OBJCOPY) -O binary $@ + +quiet_cmd_blob = BLOB $@ + cmd_blob = od -tx1 -An -v $< | sed -e 's/^ /0x/' \ + -e 's/$$/,/' -e 's/ /,0x/g' > $@ +quiet_cmd_offsets = GEN $@ + cmd_offsets = $(srctree)/scripts/mkCconstants $< $@ + $(obj)/core.o: $(obj)/hypervisor-blob.c + +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + $(call cmd,blob) + # This links the hypervisor in the right place and turns it into a C array. $(obj)/hypervisor-raw: $(obj)/hypervisor.o - @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ -$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw - @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@ + $(call if_changed,ld_hyber) + $(call cmd,objcopy) + +$(obj)/hypervisor.o: $(src)/hypervisor.S $(asm-offsets-lg) + $(call if_changed_dep,as_o_S) + +$(asm-offsets-lg): $(obj)/asm-offsets.s + $(call cmd,offsets) + +$(obj)/asm-offsets.s: $(src)/asm-offsets.c + $(call if_changed_dep,cc_s_c) -clean-files := hypervisor-blob.c hypervisor-raw +clean-files := $(asm-offsets-lg) hypervisor-blob.c hypervisor-raw
arch/i386/lguest/asm-offsets.c new file +31 -0
@@ -0,0 +1,31 @@ +/* +* lguest's "private" +*/ + +#include <linux/sched.h> +#include <asm/lguest.h> +#include "../lguest/lg.h" + +#define DEFINE (sym, val) \ + asm volatile("\n->" #sym " %0 " #val : : "i" (val)) + +#define BLANK () asm volatile("\n->" : : ) + +#define OFFSET (sym, str, mem) \ + DEFINE(sym, offsetof(struct str, mem)); + +void fell_lguest (void ) +{ +BLANK (); +OFFSET (LGUEST_DATA_irq_enabled, lguest_data, irq_enabled); +OFFSET (LGUEST_STATE_host_stackptr, lguest_state, host.stackptr); +OFFSET (LGUEST_STATE_host_pgdir, lguest_state, host.pgdir); +OFFSET (LGUEST_STATE_host_gdt, lguest_state, host.gdt); +OFFSET (LGUEST_STATE_host_idt, lguest_state, host.idt); +OFFSET (LGUEST_STATE_regs, lguest_state, regs); +OFFSET (LGUEST_STATE_gdt, lguest_state, gdt); +OFFSET (LGUEST_STATE_idt, lguest_state, idt); +OFFSET (LGUEST_STATE_gdt_table, lguest_state, gdt_table); +OFFSET (LGUEST_STATE_trapnum, lguest_state, regs.trapnum); +OFFSET (LGUEST_STATE_errcode, lguest_state, regs.errcode); +}
arch/i386/kernel/asm-offsets.c +2 -21
@@ -17,11 +17,7 @@#include <asm/elf.h> #include <asm/pda.h> -#ifdef CONFIG_LGUEST_GUEST -#include <asm/lguest.h> -#include "../lguest/lg.h" -#endif #define DEFINE (sym, val) \ - asm volatile("\n->" #sym " %0 " #val : : "i" (val)) + asm volatile("\n->" #sym " %0 " #val : : "i" (val))#define BLANK () asm volatile("\n->" : : )
@@ -104,5 +100,5 @@ void foo(void)BLANK (); -OFFSET (PDA_cpu, i386_pda, cpu_number); +OFFSET (PDA_cpu, i386_pda, cpu_number);OFFSET (PDA_pcurrent, i386_pda, pcurrent);
@@ -116,18 +112,3 @@ void foo(void)OFFSET (PARAVIRT_read_cr0, paravirt_ops, read_cr0);#endif - -#ifdef CONFIG_LGUEST_GUEST -BLANK (); -OFFSET (LGUEST_DATA_irq_enabled, lguest_data, irq_enabled); -OFFSET (LGUEST_STATE_host_stackptr, lguest_state, host.stackptr); -OFFSET (LGUEST_STATE_host_pgdir, lguest_state, host.pgdir); -OFFSET (LGUEST_STATE_host_gdt, lguest_state, host.gdt); -OFFSET (LGUEST_STATE_host_idt, lguest_state, host.idt); -OFFSET (LGUEST_STATE_regs, lguest_state, regs); -OFFSET (LGUEST_STATE_gdt, lguest_state, gdt); -OFFSET (LGUEST_STATE_idt, lguest_state, idt); -OFFSET (LGUEST_STATE_gdt_table, lguest_state, gdt_table); -OFFSET (LGUEST_STATE_trapnum, lguest_state, regs.trapnum); -OFFSET (LGUEST_STATE_errcode, lguest_state, regs.errcode); -#endif }
arch/i386/lguest/hypervisor.S +4 -4
@@ -2,5 +2,5 @@ Layout is: default_idt_entries (1k), then switch_to_guest entry point. */ #include <linux/linkage.h> -#include <asm/asm-offsets.h> +#include <asm/asm-offsets-lg.h> #include "lg.h"
@@ -104,5 +104,5 @@ switch_to_guest: popl %es; \ popl %ss - + /* Return to run_guest_once. */ return_to_host:
@@ -151,5 +151,5 @@ deliver_to_host_with_errcode: .endr .endm - + /* We intercept every interrupt, because we may need to switch back to * host. Unfortunately we can't tell them apart except by entry
@@ -158,5 +158,5 @@ deliver_to_host_with_errcode: irq_stubs: .data -default_idt_entries: +default_idt_entries: .text IRQ_STUBS 0 1 return_to_host /* First two traps */
[pp] kbuild: asm-offsets generalized
Quoted 6 lines
Quoted 3 lines
Proposition will follow.
[]
[patch proposition] kbuild: lguest with private asm-offsets
[]
Quoted 1 line
* needs "asm-offsets magic demystified, generalized".
[]
[patch proposition] kbuild: asm-offsets generalized
* scripts/mkCconstants:
- asm-offsets magic demystified, generalized,
* (hopefully) more readable sed scripts,
* top Kbuild may be updated...
* file needs chmod u+x, i don't know, how it's done in patch(1).
pp-by: Oleg Verych
--- scripts/mkCconstants | 50 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 50 insertions(+), 0 deletions(-)
scripts/mkCconstants new file +50 -0
@@ -0,0 +1,50 @@ +#!/bin/sh + +# Input file, where values of interest are stored is produced by +# `cmd_cc_s_c'. It yields calculation of constants, needed in +# assembler modules. Output is a suitable header file. +# +# $1 - input filename; +# $2 - output filename; +# $3 - header file format: "normal" (default), "mips". + +set -e + +[ -z "$1" ] || [ -z "$2" ] && exit 1 + +case $3 in + mips) + SED_SCRIPT=' +/^@@@/{ +s/^@@@//; +s/ \#.*\$//; +p; +}' + ;; + normal | *) + SED_SCRIPT=' +/^->/{ +s:^->\([^ ]*\) [\$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; +s:->::; +p; +}' + ;; +esac + +cat << "EOF" > $2 +#ifndef __ASM_OFFSETS_H__ +#define __ASM_OFFSETS_H__ + +/* + * This file was generated by scripts/mkCconstants + */ + +EOF + +sed -ne "$SED_SCRIPT" $1 >> $2 + +cat << "EOF" >> $2 + +#endif + +EOF
--
-o--=O`C info emacs : not found #oo'L O info make : not found <___=E M man gcc : not found
Re: [pp] kbuild: asm-offsets generalized
Quoted 9 lines · Oleg Verych
Quoted 6 lines
Quoted 3 lines
Proposition will follow.
[]
[patch proposition] kbuild: lguest with private asm-offsets
[]
Quoted 1 line
* needs "asm-offsets magic demystified, generalized".
[]
To make asm-offset generic I had in mind something like the following.
It uses the currect functionality, and allows for architecture override as needed - but default not used. And architecture override is kept in a architecture specific location.
Sam
commit df95bb04b04ff2f64805dfa8459099ffe469c8a5
Author: Sam Ravnborg <[email protected]> Date: Fri Feb 16 19:51:29 2007 +0100
kbuild: Make asm-offset generic
include/asm-generic/asm-offset new file +30 -0
@@ -0,0 +1,30 @@ +# +# Support generating constant useable from assembler but defined on C-level +# +# See usage in top-level Kbuild file + +# Default sed regexp - multiline due to syntax constraints +define sed-y + "/^->/{s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; s:->::; p;}" +endef + +# let architectures override the sed expression as needed +-include include/asm/asm-offset + +quiet_cmd_offsets = GEN $@ +define cmd_offsets + (set -e; \ + echo "#ifndef __ASM_OFFSETS_H__"; \ + echo "#define __ASM_OFFSETS_H__"; \ + echo "/*"; \ + echo " * DO NOT MODIFY."; \ + echo " *"; \ + echo " * This file was generated by Kbuild"; \ + echo " *"; \ + echo " */"; \ + echo ""; \ + sed -ne $(sed-y) $<; \ + echo ""; \ + echo "#endif" ) > $@ +endef +
include/asm-mips/asm-offset new file +4 -0
@@ -0,0 +1,4 @@ +# Override default sed for MIPS when generating asm-offset +sed-$(CONFIG_MIPS) := "/^@@@/{s/^@@@//; s/ \#.*\$$//; p;}" + +
Kbuild +5 -24
@@ -1,5 +1,8 @@ # # Kbuild for top-level directory of the kernel + + + # This file takes care of the following: # 1) Generate asm-offsets.h
@@ -7,36 +10,14 @@ ##### # 1) Generate asm-offsets.h # +include include/asm-generic/asm-offset + offsets-file := include/asm-$(ARCH)/asm-offsets.h always := $(offsets-file) targets := $(offsets-file) targets += arch/$(ARCH)/kernel/asm-offsets.s -# Default sed regexp - multiline due to syntax constraints -define sed-y - "/^->/{s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; s:->::; p;}" -endef -# Override default regexp for specific architectures -sed-$(CONFIG_MIPS) := "/^@@@/{s/^@@@//; s/ \#.*\$$//; p;}" - -quiet_cmd_offsets = GEN $@ -define cmd_offsets - (set -e; \ - echo "#ifndef __ASM_OFFSETS_H__"; \ - echo "#define __ASM_OFFSETS_H__"; \ - echo "/*"; \ - echo " * DO NOT MODIFY."; \ - echo " *"; \ - echo " * This file was generated by Kbuild"; \ - echo " *"; \ - echo " */"; \ - echo ""; \ - sed -ne $(sed-y) $<; \ - echo ""; \ - echo "#endif" ) > $@ -endef - # We use internal kbuild rules to avoid the "is up to date" message from make arch/$(ARCH)/kernel/asm-offsets.s: arch/$(ARCH)/kernel/asm-offsets.c FORCE $(Q)mkdir -p $(dir $@)
Re: [pp] kbuild: asm-offsets generalized
Hallo.
Quoted 6 lines · Sam Ravnborg
Quoted 5 lines · Oleg Verych
Quoted 5 lines
Quoted 3 lines
Proposition will follow.
[]
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
[patch proposition] kbuild: lguest with private asm-offsets
[]
Quoted 1 line
* needs "asm-offsets magic demystified, generalized".
[]
To make asm-offset generic I had in mind something like the following.
Then i misunderstood, what you mean, sorry.
Quoted 102 lines
It uses the currect functionality, and allows for architecture override as needed - but default not used. And architecture override is kept in a architecture specific location.
Sam
commit
df95bb04b04ff2f64805dfa8459099ffe469c8a5Author: Sam Ravnborg <[email protected]> Date: Fri Feb 16 19:51:29 2007 +0100kbuild: Make asm-offset generic
Signed-off-by Sam Ravnborg <[email protected]>include/asm-generic/asm-offset new file +30
-0@@ -0,0 +1,30 @@ +# +# Support generating constant useable from assembler but defined on C-level +# +# See usage in top-level Kbuild file + +# Default sed regexp - multiline due to syntax constraints +define sed-y + "/^->/{s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; s:->::; p;}" +endef + +# let architectures override the sed expression as needed +-include include/asm/asm-offset + +quiet_cmd_offsets = GEN $@ +define cmd_offsets + (set -e; \ + echo "#ifndef __ASM_OFFSETS_H__"; \ + echo "#define __ASM_OFFSETS_H__"; \ + echo "/*"; \ + echo " * DO NOT MODIFY."; \ + echo " *"; \ + echo " * This file was generated by Kbuild"; \ + echo " *"; \ + echo " */"; \ + echo ""; \ + sed -ne $(sed-y) $<; \ + echo ""; \ + echo "#endif" ) > $@ +endef +include/asm-mips/asm-offset new file +4
-0@@ -0,0 +1,4 @@ +# Override default sed for MIPS when generating asm-offset +sed-$(CONFIG_MIPS) := "/^@@@/{s/^@@@//; s/ \#.*\$$//; p;}" + +Kbuild +5
-17@@ -1,5 +1,8 @@ # # Kbuild for top-level directory of the kernel + + + # This file takes care of the following: # 1) Generate asm-offsets.h@@ -7,36 +10,14 @@ ##### # 1) Generate asm-offsets.h # +include include/asm-generic/asm-offset + offsets-file := include/asm-$(ARCH)/asm-offsets.h always := $(offsets-file) targets := $(offsets-file) targets += arch/$(ARCH)/kernel/asm-offsets.s -# Default sed regexp - multiline due to syntax constraints -define sed-y - "/^->/{s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; s:->::; p;}" -endef -# Override default regexp for specific architectures -sed-$(CONFIG_MIPS) := "/^@@@/{s/^@@@//; s/ \#.*\$$//; p;}" - -quiet_cmd_offsets = GEN $@ -define cmd_offsets - (set -e; \ - echo "#ifndef __ASM_OFFSETS_H__"; \ - echo "#define __ASM_OFFSETS_H__"; \ - echo "/*"; \ - echo " * DO NOT MODIFY."; \ - echo " *"; \ - echo " * This file was generated by Kbuild"; \ - echo " *"; \
Kbuild ##### · line 23
Quoted 10 lines
Kbuild +0
-7- echo " */"; \ - echo ""; \ - sed -ne $(sed-y) $<; \ - echo ""; \ - echo "#endif" ) > $@ -endef - # We use internal kbuild rules to avoid the "is up to date" message from make arch/$(ARCH)/kernel/asm-offsets.s: arch/$(ARCH)/kernel/asm-offsets.c FORCE $(Q)mkdir -p $(dir $@)
Thanks.
____
Re: [pp] kbuild: asm-offsets generalized
Quoted 1 line · Oleg Verych
Hallo.
lguest parts look good though!
Rusty.
Re: [pp] kbuild: asm-offsets generalized
Quoted 4 lines · Rusty Russell
Quoted 1 line · Oleg Verych
Hallo.
lguest parts look good though!
Thanks.
Well, then what about my way of doing generalization?
I.e. one-file shell script-let, rather many indistinguishable GNU make files throughout source tree?
Sam's approach also offers using of CONGIG* options. But this has two issues (for me, of course):
* lguest is in i386 arch, *top* Kbuild doing this for specific ARCH (include/asm/);
* lguest wants to have private for-asm constants generated, unless CONFIG is set, kbuild never will reach that directory.
Finally (my favorite). While i was told, that i'm doing more obfusticated solutions, i doubt i did now: script-let makes magic more readable, and GNU make's complications, like `$$' and various whitespace issues, do not influence.
One thing is, that
+quiet_cmd_offsets = GEN $@
+ cmd_offsets = $(srctree)/scripts/mkCconstants $< $@
+
can be moved in Kbuild.include with CONFIG* upgrade:
....
sed-$(CONFIG_MIPS) = mips
....
cmd_offsets = $(srctree)/scripts/mkCconstants $< $@ $(sed-y)
Kind regards!
--
-o--=O`C info emacs : not found #oo'L O info make : not found <___=E M man gcc : not found
Re: [pp] kbuild: asm-offsets generalized
In reply to Oleg VerychQuoted 12 lines · Oleg Verych
Quoted 6 lines
Quoted 3 lines
Proposition will follow.
[]
[patch proposition] kbuild: lguest with private asm-offsets
[]
Quoted 1 line
* needs "asm-offsets magic demystified, generalized".
[]
[patch proposition] kbuild: asm-offsets generalized
Quoted 8 lines
*
scripts/mkCconstants:
- asm-offsets magic demystified, generalized,* (hopefully) more readable sed scripts,
* top
Kbuildmay be updated...* file needs
chmod u+x, i don't know, how it's done in patch(1).
Can I ask you to provide a complete patch that replaces the current asm-offset stuff with your more readable script version.
Name the script: mkasm-offset.sh to make a direct connection to the resulting .h file name.
Thanks,
Sam
Quoted 66 lines
pp-by: Oleg Verych
--- scripts/mkCconstants | 50 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 50 insertions(+), 0 deletions(-)Index: linux-2.6.20/scripts/mkCconstants
===================================================================scripts/mkCconstants new file +50
-2@@ -0,0 +1,50 @@ +#!/bin/sh + +# Input file, where values of interest are stored is produced by +# `cmd_cc_s_c'. It yields calculation of constants, needed in +# assembler modules. Output is a suitable header file. +# +# $1 - input filename; +# $2 - output filename; +# $3 - header file format: "normal" (default), "mips". + +set -e + +[ -z "$1" ] || [ -z "$2" ] && exit 1 + +case $3 in + mips) + SED_SCRIPT=' +/^@@@/{ +s/^@@@//; +s/ \#.*\$//; +p; +}' + ;; + normal | *) + SED_SCRIPT=' +/^->/{ +s:^->\([^ ]*\) [\$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; +s:->::; +p; +}' + ;; +esac + +cat << "EOF" > $2 +#ifndef __ASM_OFFSETS_H__ +#define __ASM_OFFSETS_H__ + +/* + * This file was generated by scripts/mkCconstants + */ + +EOF + +sed -ne "$SED_SCRIPT" $1 >> $2 + +cat << "EOF" >> $2 + +#endif + +EOF -- -o--=O`C info emacs : not found #oo'L O info make : not found<___=E M man gcc : not found
Re: [pp] kbuild: asm-offsets generalized
Quoted 1 line · Sam Ravnborg
On Fri, Feb 16, 2007 at 04:59:29PM +0100, Oleg Verych wrote:
[]
Quoted 1 line
Quoted 1 line
[patch proposition] kbuild: asm-offsets generalized
[]
Quoted 3 lines
Quoted 3 lines
* (hopefully) more readable sed scripts,
* top
Kbuildmay be updated...
[]
Quoted 3 lines
Can I ask you to provide a complete patch that replaces the current asm-offset stuff with your more readable script version.
OK, unless this is 1st April joke ;)
Quoted 5 lines
Quoted 2 lines
*
scripts/mkCconstants:
- asm-offsets magic demystified, generalized,Name the script:
mkasm-offset.shto make a direct connection to the resulting .h file name.
OK, nice name.
Quoted 2 lines
Quoted 2 lines
* file needs
chmod u+x, i don't know, how it's done in patch(1).
BTW, i'll look at git-diff/git-apply for doing that, as was noted by Linus.
____
Re: [pp] kbuild: asm-offsets generalized
Quoted 21 lines · Oleg Verych
Quoted 1 line · Sam Ravnborg
On Fri, Feb 16, 2007 at 04:59:29PM +0100, Oleg Verych wrote:
[]
Quoted 1 line
Quoted 1 line
[patch proposition] kbuild: asm-offsets generalized
[]
Quoted 3 lines
Quoted 3 lines
* (hopefully) more readable sed scripts,
* top
Kbuildmay be updated...[]
Quoted 3 lines
Can I ask you to provide a complete patch that replaces the current asm-offset stuff with your more readable script version.
OK, unless this is 1st April joke ;)
Quoted 5 lines
Quoted 2 lines
*
scripts/mkCconstants:
- asm-offsets magic demystified, generalized,Name the script:
mkasm-offset.shto make a direct connection to the resulting .h file name.
Quoted 6 lines
OK, nice name.
Quoted 2 lines
Quoted 2 lines
* file needs
chmod u+x, i don't know, how it's done in patch(1).BTW, i'll look at git-diff/git-apply for doing that, as was noted by Linus.
When patch passes me I can take care - so do not worry. But tell me to do so if I should forget in the meantime.
PS. On vacation for a week from tomorrow so take your time.
Sam
PATCH 1/1 lguest: Config and headers
In reply to Rusty Russell[ Part 6 was too big, so posting in four parts ]
Unfortunately, we don't have the build infrastructure for "private" asm-offsets.h files, so there's a not-so-neat include in arch/i386/kernel/asm-offsets.c.
The four headers are:asm/lguest.h:
Things the guest needs to know (hypercall numbers, etc).asm/lguest_device.h:
Things lguest devices need to know (lguest bus registration)asm/lguest_user.h:
Things that the lguest userspace utility needs (/dev/lguest and some devices)arch/i386/lguest/lg.h:
Internal header for the lg module (which consists of 8 files).
===================================================================
arch/i386/Kconfig +21 -0
@@ -226,6 +226,27 @@ config ES7000_CLUSTERED_APIC depends on SMP && X86_ES7000 && MPENTIUMIII source "arch/i386/Kconfig.cpu" + +config LGUEST + tristate "Linux hypervisor example code" + depends on X86 && PARAVIRT && EXPERIMENTAL && !X86_PAE + select LGUEST_GUEST + select HVC_DRIVER + ---help--- + This is a very simple module which allows you to run + multiple instances of the same Linux kernel, using the + "lguest" command found in the Documentation/lguest directory. + Note that "lguest" is pronounced to rhyme with "fell quest", + not "rustyvisor". See Documentation/lguest/lguest.txt. + + If unsure, say N. If curious, say M. If masochistic, say Y. + +config LGUEST_GUEST + bool + help + The guest needs code built-in, even if the host has lguest + support as a module. The drivers are tiny, so we build them + in too. config HPET_TIMER bool "HPET Timer Support"
===================================================================
arch/i386/kernel/asm-offsets.c +19 -0
@@ -16,6 +16,10 @@#include <asm/thread_info.h> #include <asm/elf.h> #include <asm/pda.h> +#ifdef CONFIG_LGUEST_GUEST +#include <asm/lguest.h> +#include "../lguest/lg.h" +#endif #define DEFINE (sym, val) \ asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -111,4 +115,19 @@ void foo(void)OFFSET (PARAVIRT_iret, paravirt_ops, iret);OFFSET (PARAVIRT_read_cr0, paravirt_ops, read_cr0);#endif + +#ifdef CONFIG_LGUEST_GUEST +BLANK (); +OFFSET (LGUEST_DATA_irq_enabled, lguest_data, irq_enabled); +OFFSET (LGUEST_STATE_host_stackptr, lguest_state, host.stackptr); +OFFSET (LGUEST_STATE_host_pgdir, lguest_state, host.pgdir); +OFFSET (LGUEST_STATE_host_gdt, lguest_state, host.gdt); +OFFSET (LGUEST_STATE_host_idt, lguest_state, host.idt); +OFFSET (LGUEST_STATE_regs, lguest_state, regs); +OFFSET (LGUEST_STATE_gdt, lguest_state, gdt); +OFFSET (LGUEST_STATE_idt, lguest_state, idt); +OFFSET (LGUEST_STATE_gdt_table, lguest_state, gdt_table); +OFFSET (LGUEST_STATE_trapnum, lguest_state, regs.trapnum); +OFFSET (LGUEST_STATE_errcode, lguest_state, regs.errcode); +#endif }
===================================================================
include/asm-i386/lguest.h new file +86 -0
86 lines not on this page · raw message
===================================================================
include/asm-i386/lguest_device.h new file +31 -0
@@ -0,0 +1,31 @@ +#ifndef _ASM_LGUEST_DEVICE_H +#define _ASM_LGUEST_DEVICE_H +/* Everything you need to know about lguest devices. */ +#include <linux/device.h> +#include <asm/lguest.h> +#include <asm/lguest_user.h> + +struct lguest_device { +/* Unique busid, and index into lguest_page->devices[] */ +/* By convention, each device can use irq index+1 if it wants to. */ +unsigned int index; + +struct device dev; + +/* Driver can hang data off here. */ +void *private; +}; + +struct lguest_driver { +const char *name; +struct module *owner; +u16 device_type; +int (*probe)(struct lguest_device *dev); +void (*remove)(struct lguest_device *dev); + +struct device_driver drv; +}; + +extern int register_lguest_driver (struct lguest_driver *drv); +extern void unregister_lguest_driver (struct lguest_driver *drv); +#endif /* _ASM_LGUEST_DEVICE_H */
===================================================================
include/asm-i386/lguest_user.h new file +86 -0
86 lines not on this page · raw message
===================================================================
arch/i386/lguest/lg.h new file +253 -0
253 lines not on this page · raw message
PATCH 1/1 lguest: the host code (lg.ko)
This is the host module (lg.ko) which supports lguest:
arch/i386/lguest/hypervisor.S:
The actual guest <-> host switching code. This is compiled into a C array, which is mapped to 0xFFC01000 in host and guests.
arch/i386/lguest/core.c:
The core of the hypervisor, which calls into the assembler code which does this actual switch. Also contains helper routines.
arch/i386/lguest/hypercalls.c:
The entry point for the 19 hypercalls.
arch/i386/lguest/interrupts_and_traps.c:
Handling of interrupts and traps, except page faults.
arch/i386/lguest/io.c:
I/O from guest to host, and between guests.
arch/i386/lguest/lguest_user.c:
/dev/lguest interface for lguest program to launch/control guests.
arch/i386/lguest/page_tables.c:
Shadow Page table handling: generally we build up the shadow page tables by converting from guest page tables when a fault occurs.
arch/i386/lguest/segments.c:
Segmentation (GDT) handling: we have to ensure they're trimmed to avoid guest access to the switching code.
===================================================================
arch/i386/lguest/core.c new file +425 -0
@@ -0,0 +1,425 @@ +/* World's simplest hypervisor, to test paravirt_ops and show +* unbelievers that virtualization is the future. Plus, it's fun! */ +#include <linux/module.h> +#include <linux/stringify.h> +#include <linux/stddef.h> +#include <linux/io.h> +#include <linux/mm.h> +#include <linux/vmalloc.h> +#include <asm/lguest.h> +#include <asm/paravirt.h> +#include <asm/desc.h> +#include <asm/pgtable.h> +#include <asm/uaccess.h> +#include <asm/poll.h> +#include <asm/highmem.h> +#include <asm/asm-offsets.h> +#include "lg.h" + +/* This is our hypervisor, compiled from hypervisor.S. */ +static char __initdata hypervisor_blob[] = { +#include "hypervisor-blob.c" +}; + +#define MAX_LGUEST_GUESTS \ + ((HYPERVISOR_SIZE-sizeof(hypervisor_blob))/sizeof(struct lguest_state)) + +static struct vm_struct *hypervisor_vma; +static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; +struct page *hype_pages;/* Contiguous pages. */ +struct lguest lguests[MAX_LGUEST_GUESTS ]; +DECLARE_MUTEX (lguest_lock); + +/* IDT entries are at start of hypervisor. */ +const unsigned long *__lguest_default_idt_entries (void ) +{ +return (void *)HYPE_ADDR ; +} + +/* Next is switch_to_guest */ +static void *__lguest_switch_to_guest (void ) +{ +return (void *)HYPE_ADDR +HYPE_DATA_SIZE ; +} + +/* Then we use everything else to hold guest state. */ +struct lguest_state *__lguest_states (void ) +{ +return (void *)HYPE_ADDR +sizeof (hypervisor_blob); +} + +static __init int map_hypervisor (void ) +{ +unsigned int i; +int err; +struct page *pages[HYPERVISOR_PAGES ], **pagep = pages; + + hype_pages =alloc_pages (GFP_KERNEL |__GFP_ZERO, +get_order (HYPERVISOR_SIZE )); +if (!hype_pages) +return -ENOMEM ; + + hypervisor_vma =__get_vm_area (HYPERVISOR_SIZE ,VM_ALLOC , +HYPE_ADDR ,VMALLOC_END ); +if (!hypervisor_vma) { + err = -ENOMEM ; +printk ("lguest: could not map hypervisor pages high\n" ); + goto free_pages; + } + +for (i =0 ; i <HYPERVISOR_PAGES ; i++) + pages[i] = hype_pages + i; + + err =map_vm_area (hypervisor_vma,PAGE_KERNEL , &pagep); +if (err) { +printk ("lguest: map_vm_area failed: %i\n" , err); + goto free_vma; + } +memcpy (hypervisor_vma->addr, hypervisor_blob,sizeof (hypervisor_blob)); + +/* Setup LGUEST segments on all cpus */ +for_each_possible_cpu (i) { +get_cpu_gdt_table (i)[GDT_ENTRY_LGUEST_CS ] =FULL_EXEC_SEGMENT ; +get_cpu_gdt_table (i)[GDT_ENTRY_LGUEST_DS ] =FULL_SEGMENT ; + } + +/* Initialize entry point into hypervisor. */ + lguest_entry.offset = (long )__lguest_switch_to_guest (); + lguest_entry.segment =LGUEST_CS ; + +printk ("lguest: mapped hypervisor at %p\n" , hypervisor_vma->addr); +return 0 ; + +free_vma: +vunmap (hypervisor_vma->addr); +free_pages: +__free_pages (hype_pages,get_order (HYPERVISOR_SIZE )); +return err; +} + +static __exit void unmap_hypervisor(void) +{ +vunmap (hypervisor_vma->addr); +__free_pages (hype_pages,get_order (HYPERVISOR_SIZE )); +} + +/* IN/OUT insns: enough to get us past boot-time probing. */ +static int emulate_insn (struct lguest *lg) +{ +u8 insn; +unsigned int insnlen =0 , in =0 , shift =0 ; +unsigned long physaddr =guest_pa (lg, lg->state->regs.eip); + +/* This only works for addresses in linear mapping... */ +if (lg->state->regs.eip < lg->page_offset) +return 0 ; +lhread (lg, &insn, physaddr,1 ); + +/* Operand size prefix means it's actually for ax. */ +if (insn ==0x66 ) { + shift =16 ; + insnlen =1 ; +lhread (lg, &insn, physaddr + insnlen,1 ); + } + +switch (insn &0xFE ) { +case 0xE4 :/* in <next byte>,%al */ + insnlen +=2 ; + in =1 ; +break ; +case 0xEC :/* in (%dx),%al */ + insnlen +=1 ; + in =1 ; +break ; +case 0xE6 :/* out %al,<next byte> */ + insnlen +=2 ; +break ; +case 0xEE :/* out %al,(%dx) */ + insnlen +=1 ; +break ; +default : +return 0 ; + } + +if (in) { +/* Lower bit tells is whether it's a 16 or 32 bit access */ +if (insn &0x1 ) + lg->state->regs.eax =0xFFFFFFFF ; +else + lg->state->regs.eax |= (0xFFFF << shift); + } + lg->state->regs.eip += insnlen; +return 1 ; +} + +int find_free_guest (void ) +{ +unsigned int i; +for (i =0 ; i <MAX_LGUEST_GUESTS ; i++) +if (!lguests[i].state) +return i; +return -1 ; +} + +int lguest_address_ok (const struct lguest *lg,unsigned long addr) +{ +return addr /PAGE_SIZE < lg->pfn_limit; +} + +/* Just like get_user, but don't let guest access lguest binary. */ +u32 lhread_u32 (struct lguest *lg,u32 addr) +{ +u32 val =0 ; + +/* Don't let them access lguest_add */ +if (!lguest_address_ok (lg, addr) + ||get_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad read address %u" , addr); +return val; +} + +void lhwrite_u32 (struct lguest *lg,u32 addr,u32 val) +{ +if (!lguest_address_ok (lg, addr) + ||put_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad write address %u" , addr); +} + +void lhread (struct lguest *lg,void *b,u32 addr,unsigned bytes) +{ +if (addr + bytes < addr || !lguest_address_ok (lg, addr+bytes) + ||copy_from_user (b, (void __user *)addr, bytes) !=0 ) { +/* copy_from_user should do this, but as we rely on it... */ +memset (b,0 , bytes); +kill_guest (lg,"bad read address %u len %u" , addr, bytes); + } +} + +void lhwrite (struct lguest *lg,u32 addr,const void *b,unsigned bytes) +{ +if (addr + bytes < addr + || !lguest_address_ok (lg, addr+bytes) + ||copy_to_user ((void __user *)addr, b, bytes) !=0 ) +kill_guest (lg,"bad write address %u len %u" , addr, bytes); +} + +/* Saves exporting idt_table from kernel */ +static struct desc_struct *get_idt_table (void ) +{ +struct Xgt_desc_struct idt; + + asm("sidt %0" :"=m" (idt)); +return (void *)idt.address; +} + +extern asmlinkage void math_state_restore(void); + +static int usermode (struct lguest_regs *regs) +{ +return (regs->cs &SEGMENT_RPL_MASK ) ==USER_RPL ; +} + +/* Trap page resets this when it reloads gs. */ +static int new_gfp_eip (struct lguest *lg,struct lguest_regs *regs) +{ +u32 eip; +get_user (eip, &lg->lguest_data->gs_gpf_eip); +if (eip == regs->eip) +return 0 ; +put_user (regs->eip, &lg->lguest_data->gs_gpf_eip); +return 1 ; +} + +static void set_ts (unsigned int guest_ts) +{ +u32 cr0; +if (guest_ts) { + asm("movl %%cr0,%0" :"=r" (cr0)); +if (!(cr0 &8 )) + asm("movl %0,%%cr0" : :"r" (cr0|8 )); + } +} + +static void run_guest_once (struct lguest *lg) +{ +unsigned int clobber; + +/* Put eflags on stack, lcall does rest. */ + asmvolatile ("pushf; lcall *lguest_entry" + :"=a" (clobber),"=d" (clobber) + :"0" (lg->state),"1" (get_idt_table ()) + :"memory" ); +} + +int run_guest (struct lguest *lg,char *__user user) +{ +struct lguest_regs *regs = &lg->state->regs; + +while (!lg->dead) { +unsigned int cr2 =0 ;/* Damn gcc */ + +/* Hypercalls first: we might have been out to userspace */ +if (do_async_hcalls (lg)) + goto pending_dma; + +if (regs->trapnum ==LGUEST_TRAP_ENTRY ) { +/* Only do hypercall once. */ + regs->trapnum =255 ; +if (hypercall (lg, regs)) + goto pending_dma; + } + +if (signal_pending (current)) +return -EINTR ; +maybe_do_interrupt (lg); + +if (lg->dead) +break ; + +if (lg->halted) { +set_current_state (TASK_INTERRUPTIBLE ); +schedule_timeout (1 ); +continue ; + } + +/* Restore limits on TLS segments if in user mode. */ +if (usermode (regs)) { +unsigned int i; +for (i =0 ; i <ARRAY_SIZE (lg->tls_limits); i++) + lg->state->gdt_table[GDT_ENTRY_TLS_MIN +i].a + |= lg->tls_limits[i]; + } + +local_irq_disable (); +map_trap_page (lg); + +/* Host state to be restored after the guest returns. */ + asm("sidt %0" :"=m" (lg->state->host.idt)); + lg->state->host.gdt =__get_cpu_var (cpu_gdt_descr); + +/* Even if *we* don't want FPU trap, guest might... */ +set_ts (lg->ts); + +run_guest_once (lg); + +/* Save cr2 now if we page-faulted. */ +if (regs->trapnum ==14 ) + asm("movl %%cr2,%0" :"=r" (cr2)); +else if (regs->trapnum ==7 ) +math_state_restore (); +local_irq_enable (); + +switch (regs->trapnum) { +case 13 :/* We've intercepted a GPF. */ +if (regs->errcode ==0 ) { +if (emulate_insn (lg)) +continue ; + +/* FIXME: If it's reloading %gs in a loop? */ +if (usermode (regs) &&new_gfp_eip (lg,regs)) +continue ; + } + +if (reflect_trap (lg, &lg->gpf_trap,1 )) +continue ; +break ; +case 14 :/* We've intercepted a page fault. */ +if (demand_page (lg, cr2, regs->errcode &2 )) +continue ; + +/* If lguest_data is NULL, this won't hurt. */ +put_user (cr2, &lg->lguest_data->cr2); +if (reflect_trap (lg, &lg->page_trap,1 )) +continue ; +kill_guest (lg,"unhandled page fault at %#x" +" (eip=%#x, errcode=%#x)" , + cr2, regs->eip, regs->errcode); +break ; +case 7 :/* We've intercepted a Device Not Available fault. */ +/* If they don't want to know, just absorb it. */ +if (!lg->ts) +continue ; +if (reflect_trap (lg, &lg->fpu_trap,0 )) +continue ; +kill_guest (lg,"unhandled FPU fault at %#x" , + regs->eip); +break ; + case 32 ... 255: /* Real interrupt, fall thru */ +cond_resched (); +case LGUEST_TRAP_ENTRY :/* Handled at top of loop */ +continue ; +case 6 :/* Invalid opcode before they installed handler */ +check_bug_kill (lg); + } +kill_guest (lg,"unhandled trap %i at %#x (err=%i)" , + regs->trapnum, regs->eip, regs->errcode); + } +return -ENOENT ; + +pending_dma: +put_user (lg->pending_dma, (unsigned long *)user); +put_user (lg->pending_addr, (unsigned long *)user+1 ); +return sizeof (unsigned long )*2 ; +} + +#define STRUCT_LGUEST_ELEM_SIZE (elem) sizeof(((struct lguest_state *)0)->elem) + +static void adjust_pge (void *on) +{ +if (on) +write_cr4 (read_cr4 () |X86_CR4_PGE ); +else +write_cr4 (read_cr4 () & ~X86_CR4_PGE ); +} + +static int __init init (void ) +{ +int err; + +if (paravirt_enabled ()) +return -EPERM ; + + err =map_hypervisor (); +if (err) +return err; + + err =init_pagetables (hype_pages); +if (err) { +unmap_hypervisor (); +return err; + } +lguest_io_init (); + + err =lguest_device_init (); +if (err) { +free_pagetables (); +unmap_hypervisor (); +return err; + } +if (cpu_has_pge) {/* We have a broader idea of "global". */ + cpu_had_pge =1 ; +on_each_cpu (adjust_pge,0 ,0 ,1 ); +clear_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); + } +return 0 ; +} + +static void __exit fini(void) +{ +lguest_device_remove (); +free_pagetables (); +unmap_hypervisor (); +if (cpu_had_pge) { +set_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); +on_each_cpu (adjust_pge, (void *)1 ,0 ,1 ); + } +} + +module_init (init); +module_exit (fini); +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Rusty Russell <[email protected]>" );
===================================================================
arch/i386/lguest/hypercalls.c new file +199 -0
199 lines not on this page · raw message
===================================================================
arch/i386/lguest/hypervisor.S new file +170 -0
170 lines not on this page · raw message
===================================================================
arch/i386/lguest/interrupts_and_traps.c new file +221 -0
221 lines not on this page · raw message
===================================================================
arch/i386/lguest/io.c new file +413 -0
413 lines not on this page · raw message
===================================================================
arch/i386/lguest/lguest_user.c new file +242 -0
242 lines not on this page · raw message
===================================================================
arch/i386/lguest/page_tables.c new file +374 -0
374 lines not on this page · raw message
===================================================================
arch/i386/lguest/segments.c new file +171 -0
171 lines not on this page · raw message
PATCH 1/1 lguest: the guest code
This is the guest code which replaces the parts of paravirt_ops with hypercalls. It's fairly trivial. This patch also includes trivial bus driver for lguest devices.
===================================================================
arch/i386/lguest/lguest.c new file +595 -0
@@ -0,0 +1,595 @@ +/* +* Lguest specific paravirt-ops implementation +* +* Copyright (C) 2006, Rusty Russell <[email protected]> IBM Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or +* NON INFRINGEMENT. See the GNU General Public License for more +* details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +#include <linux/kernel.h> +#include <linux/start_kernel.h> +#include <linux/string.h> +#include <linux/console.h> +#include <linux/screen_info.h> +#include <linux/irq.h> +#include <linux/interrupt.h> +#include <linux/clocksource.h> +#include <asm/paravirt.h> +#include <asm/lguest.h> +#include <asm/lguest_user.h> +#include <asm/param.h> +#include <asm/page.h> +#include <asm/pgtable.h> +#include <asm/desc.h> +#include <asm/setup.h> +#include <asm/e820.h> +#include <asm/pda.h> +#include <asm/asm-offsets.h> + +extern int mce_disabled; + +struct lguest_data lguest_data; +struct lguest_device_desc *lguest_devices; +static __initdata const struct lguest_boot_info *boot =__va (0 ); + +void async_hcall (unsigned long call, +unsigned long arg1,unsigned long arg2,unsigned long arg3) +{ +/* Note: This code assumes we're uniprocessor. */ +static unsigned int next_call; +unsigned long flags; + +local_irq_save (flags); +if (lguest_data.hcall_status[next_call] !=0xFF ) { +/* Table full, so do normal hcall which will flush table. */ +hcall (call, arg1, arg2, arg3); + }else { + lguest_data.hcalls[next_call].eax = call; + lguest_data.hcalls[next_call].edx = arg1; + lguest_data.hcalls[next_call].ebx = arg2; + lguest_data.hcalls[next_call].ecx = arg3; +wmb (); + lguest_data.hcall_status[next_call] =0 ; +if (++next_call ==LHCALL_RING_SIZE ) + next_call =0 ; + } +local_irq_restore (flags); +} + +#ifdef PARAVIRT_LAZY_NONE /* Not in 2.6.20. */ +static int lazy_mode; +static void fastcall lguest_lazy_mode(int mode) +{ + lazy_mode = mode; +if (mode ==PARAVIRT_LAZY_NONE ) +hcall (LHCALL_FLUSH_ASYNC ,0 ,0 ,0 ); +} + +static void lazy_hcall (unsigned long call, +unsigned long arg1, +unsigned long arg2, +unsigned long arg3) +{ +if (lazy_mode ==PARAVIRT_LAZY_NONE ) +hcall (call, arg1, arg2, arg3); +else +async_hcall (call, arg1, arg2, arg3); +} +#else +#define lazy_hcall hcall +#endif + +static unsigned long fastcall save_fl (void ) +{ +return lguest_data.irq_enabled; +} + +static void fastcall restore_fl(unsigned long flags) +{ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled = flags; +} + +static void fastcall irq_disable(void) +{ + lguest_data.irq_enabled =0 ; +} + +static void fastcall irq_enable(void) +{ +/* Linux i386 code expects bit 9 set. */ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled =512 ; +} + +static void fastcall lguest_load_gdt(const struct Xgt_desc_struct *desc) +{ +BUG_ON ((desc->size+1 )/8 !=GDT_ENTRIES ); +hcall (LHCALL_LOAD_GDT ,__pa (desc->address),GDT_ENTRIES ,0 ); +} + +static void fastcall lguest_load_idt(const struct Xgt_desc_struct *desc) +{ +unsigned int i; +struct desc_struct *idt = (void *)desc->address; + +for (i =0 ; i < (desc->size+1 )/8 ; i++) +hcall (LHCALL_LOAD_IDT_ENTRY , i, idt[i].a, idt[i].b); +} + +static int lguest_panic (struct notifier_block *nb,unsigned long l,void *p) +{ +hcall (LHCALL_CRASH ,__pa (p),0 ,0 ); +return NOTIFY_DONE ; +} + +static struct notifier_block paniced = { + .notifier_call = lguest_panic +}; + +static cycle_t lguest_clock_read (void ) +{ +/* FIXME: This is just the native one. Account stolen time! */ +return paravirt_ops.read_tsc (); +} + +/* FIXME: Update iff tsc rate changes. */ +static struct clocksource lguest_clock = { + .name ="lguest" , + .rating =400 , + .read = lguest_clock_read, + .mask =CLOCKSOURCE_MASK (64 ), + .mult =0 ,/* to be set */ + .shift =22 , + .is_continuous =1 , +}; + +static char *lguest_memory_setup (void ) +{ +/* We do these here because lockcheck barfs if before start_kernel */ +atomic_notifier_chain_register (&panic_notifier_list, &paniced); + lguest_clock.mult = lguest_data.clock_mult; +clocksource_register (&lguest_clock); + + e820.nr_map =0 ; +add_memory_region (0 ,PFN_PHYS (boot->max_pfn),E820_RAM ); +return "LGUEST" ; +} + +static fastcall void lguest_cpuid(unsigned int *eax, unsigned int *ebx, +unsigned int *ecx,unsigned int *edx) +{ +int is_feature = (*eax ==1 ); + + asmvolatile ("cpuid" + :"=a" (*eax), +"=b" (*ebx), +"=c" (*ecx), +"=d" (*edx) + :"0" (*eax),"2" (*ecx)); + +if (is_feature) { +unsigned long *excap = (unsigned long *)ecx, + *features = (unsigned long *)edx; +/* Hypervisor needs to know when we flush kernel pages. */ +set_bit (X86_FEATURE_PGE , features); +/* We don't have any features! */ +clear_bit (X86_FEATURE_VME , features); +clear_bit (X86_FEATURE_DE , features); +clear_bit (X86_FEATURE_PSE , features); +clear_bit (X86_FEATURE_PAE , features); +clear_bit (X86_FEATURE_SEP , features); +clear_bit (X86_FEATURE_APIC , features); +clear_bit (X86_FEATURE_MTRR , features); +/* No MWAIT, either */ +clear_bit (3 , excap); + } +} + +static unsigned long current_cr3; +static void fastcall lguest_write_cr3(unsigned long cr3) +{ +hcall (LHCALL_NEW_PGTABLE , cr3,0 ,0 ); + current_cr3 = cr3; +} + +static void fastcall lguest_flush_tlb(void) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,0 ,0 ,0 ); +} + +static void fastcall lguest_flush_tlb_kernel(void) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,1 ,0 ,0 ); +} + +static void fastcall lguest_flush_tlb_single(u32 addr) +{ +/* Simply set it to zero, and it will fault back in. */ +lazy_hcall (LHCALL_SET_PTE , current_cr3, addr,0 ); +} + +/* FIXME: Eliminate all callers of this. */ +static fastcall void lguest_set_pte(pte_t *ptep, pte_t pteval) +{ + *ptep = pteval; +/* Don't bother with hypercall before initial setup. */ +if (current_cr3) +hcall (LHCALL_SET_UNKNOWN_PTE ,0 ,0 ,0 ); +} + +static fastcall void lguest_set_pte_at(struct mm_struct *mm, u32 addr, pte_t *ptep, pte_t pteval) +{ + *ptep = pteval; +lazy_hcall (LHCALL_SET_PTE ,__pa (mm->pgd), addr, pteval.pte_low); +} + +/* We only support two-level pagetables at the moment. */ +static fastcall void lguest_set_pud(pmd_t *pmdp, pmd_t pmdval) +{ + *pmdp = pmdval; +lazy_hcall (LHCALL_SET_PUD ,__pa (pmdp)&PAGE_MASK , + (__pa (pmdp)&(PAGE_SIZE -1 ))/4 ,0 ); +} + +#ifdef CONFIG_X86_LOCAL_APIC +static fastcall void lguest_apic_write(unsigned long reg, unsigned long v) +{ +} + +static fastcall void lguest_apic_write_atomic(unsigned long reg, unsigned long v) +{ +} + +static fastcall unsigned long lguest_apic_read (unsigned long reg) +{ +return 0 ; +} +#endif + +/* We move eflags word to lguest_data.irq_enabled to restore interrupt +state. For page faults, gpfs and virtual interrupts, the +hypervisor has saved eflags manually, otherwise it was delivered +directly and so eflags reflects the real machine IF state, +ie. interrupts on. Since the kernel always dies if it takes such a +trap with interrupts disabled anyway, turning interrupts back on +unconditionally here is OK. */ +asm("lguest_iret:" +" pushl %eax;" +" movl 12(%esp), %eax;" +"lguest_noirq_start:;" + " movl %eax,%ss:lguest_data+"__stringify(LGUEST_DATA_irq_enabled)";" +" popl %eax;" +" iret;" + "lguest_noirq_end:"); +extern void fastcall lguest_iret(void); +extern char lguest_noirq_start[], lguest_noirq_end[]; + +static void fastcall lguest_load_esp0(struct tss_struct *tss, +struct thread_struct *thread) +{ +lazy_hcall (LHCALL_SET_STACK , __KERNEL_DS|0x1 , thread->esp0, +THREAD_SIZE /PAGE_SIZE ); +} + +static fastcall void lguest_load_tr_desc(void) +{ +} + +static fastcall void lguest_set_ldt(const void *addr, unsigned entries) +{ +/* FIXME: Implement. */ +BUG_ON (entries); +} + +static fastcall void lguest_load_tls(struct thread_struct *t, unsigned int cpu) +{ +lazy_hcall (LHCALL_LOAD_TLS ,__pa (&t->tls_array), cpu,0 ); +} + +static fastcall void lguest_set_debugreg(int regno, unsigned long value) +{ +/* FIXME: Implement */ +} + +static unsigned int lguest_cr0; +static fastcall void lguest_clts(void) +{ +lazy_hcall (LHCALL_TS ,0 ,0 ,0 ); + lguest_cr0 &= ~8U ; +} + +static fastcall unsigned long lguest_read_cr0 (void ) +{ +return lguest_cr0; +} + +static fastcall void lguest_write_cr0(unsigned long val) +{ +hcall (LHCALL_TS , val &8 ,0 ,0 ); + lguest_cr0 = val; +} + +static fastcall unsigned long lguest_read_cr2 (void ) +{ +return lguest_data.cr2; +} + +static fastcall unsigned long lguest_read_cr3 (void ) +{ +return current_cr3; +} + +/* Used to enable/disable PGE, but we don't care. */ +static fastcall unsigned long lguest_read_cr4 (void ) +{ +return 0 ; +} + +static fastcall void lguest_write_cr4(unsigned long val) +{ +} + +/* FIXME: These should be in a header somewhere */ +extern unsigned long init_pg_tables_end; + +static void fastcall lguest_time_irq(unsigned int irq, struct irq_desc *desc) +{ +do_timer (hcall (LHCALL_TIMER_READ ,0 ,0 ,0 )); +update_process_times (user_mode_vm (get_irq_regs ())); +} + +static void disable_lguest_irq (unsigned int irq) +{ +set_bit (irq, lguest_data.interrupts); +} + +static void enable_lguest_irq (unsigned int irq) +{ +clear_bit (irq, lguest_data.interrupts); +/* FIXME: If it's pending? */ +} + +static struct irq_chip lguest_irq_controller = { + .name ="lguest" , + .mask = disable_lguest_irq, + .mask_ack = disable_lguest_irq, + .unmask = enable_lguest_irq, +}; + +static void lguest_time_init (void ) +{ +set_irq_handler (0 , lguest_time_irq); +hcall (LHCALL_TIMER_START ,HZ ,0 ,0 ); +} + +static void __init lguest_init_IRQ (void ) +{ +unsigned int i; + +for (i =0 ; i <LGUEST_IRQS ; i++) { +int vector =FIRST_EXTERNAL_VECTOR + i; +if (i >=NR_IRQS ) +break ; +if (vector !=SYSCALL_VECTOR ) { +set_intr_gate (vector, interrupt[i]); +set_irq_chip_and_handler (i, &lguest_irq_controller, + handle_level_irq); + } + } +irq_ctx_init (smp_processor_id ()); +} + +static inline void native_write_dt_entry (void *dt,int entry,u32 entry_low,u32 entry_high) +{ +u32 *lp = (u32 *)((char *)dt + entry*8 ); + lp[0 ] = entry_low; + lp[1 ] = entry_high; +} + +static fastcall void lguest_write_ldt_entry(void *dt, int entrynum, u32 low, u32 high) +{ +/* FIXME: Allow this. */ +BUG (); +} + +static fastcall void lguest_write_gdt_entry(void *dt, int entrynum, +u32 low,u32 high) +{ +native_write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_GDT ,__pa (dt),GDT_ENTRIES ,0 ); +} + +static fastcall void lguest_write_idt_entry(void *dt, int entrynum, +u32 low,u32 high) +{ +native_write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_IDT_ENTRY , entrynum, low, high); +} + +#define LGUEST_IRQ "lguest_data+"__stringify(LGUEST_DATA_irq_enabled) +#define DEF_LGUEST (name, code) \ + extern const char start_##name[], end_##name[]; \ + asm("start_" #name ": " code "; end_" #name ":") +DEF_LGUEST (cli,"movl $0," LGUEST_IRQ ); +DEF_LGUEST (sti,"movl $512," LGUEST_IRQ ); +DEF_LGUEST (popf,"movl %eax," LGUEST_IRQ ); +DEF_LGUEST (pushf,"movl " LGUEST_IRQ ",%eax" ); +DEF_LGUEST (pushf_cli,"movl " LGUEST_IRQ ",%eax; movl $0," LGUEST_IRQ ); +DEF_LGUEST (iret,".byte 0xE9,0,0,0,0" );/* jmp ... */ + +static const struct lguest_insns +{ +const char *start, *end; +} lguest_insns[] = { + [PARAVIRT_IRQ_DISABLE ] = { start_cli, end_cli }, + [PARAVIRT_IRQ_ENABLE ] = { start_sti, end_sti }, + [PARAVIRT_RESTORE_FLAGS ] = { start_popf, end_popf }, + [PARAVIRT_SAVE_FLAGS ] = { start_pushf, end_pushf }, + [PARAVIRT_SAVE_FLAGS_IRQ_DISABLE ] = { start_pushf_cli, end_pushf_cli }, + [PARAVIRT_INTERRUPT_RETURN ] = { start_iret, end_iret }, +}; +static unsigned lguest_patch (u8 type,u16 clobber,void *insns,unsigned len) +{ +unsigned int insn_len; + +/* Don't touch it if we don't have a replacement */ +if (type >=ARRAY_SIZE (lguest_insns) || !lguest_insns[type].start) +return len; + + insn_len = lguest_insns[type].end - lguest_insns[type].start; + +/* Similarly if we can't fit replacement. */ +if (len < insn_len) +return len; + +memcpy (insns, lguest_insns[type].start, insn_len); +if (type ==PARAVIRT_INTERRUPT_RETURN ) { +/* Jumps are relative. */ +u32 off = (u32 )lguest_iret - ((u32 )insns + insn_len); +memcpy (insns+1 , &off,sizeof (off)); + } +return insn_len; +} + +static void fastcall lguest_safe_halt(void) +{ +hcall (LHCALL_HALT ,0 ,0 ,0 ); +} + +static unsigned long lguest_get_wallclock (void ) +{ +return hcall (LHCALL_GET_WALLCLOCK ,0 ,0 ,0 ); +} + +static void lguest_power_off (void ) +{ +hcall (LHCALL_CRASH ,__pa ("Power down" ),0 ,0 ); +} + +static __attribute_used__ __init void lguest_init(void) +{ +extern struct Xgt_desc_struct cpu_gdt_descr; +extern struct i386_pda boot_pda; + + paravirt_ops.name ="lguest" ; + paravirt_ops.paravirt_enabled =1 ; + paravirt_ops.kernel_rpl =1 ; + + paravirt_ops.save_fl = save_fl; + paravirt_ops.restore_fl = restore_fl; + paravirt_ops.irq_disable = irq_disable; + paravirt_ops.irq_enable = irq_enable; + paravirt_ops.load_gdt = lguest_load_gdt; + paravirt_ops.memory_setup = lguest_memory_setup; + paravirt_ops.cpuid = lguest_cpuid; + paravirt_ops.write_cr3 = lguest_write_cr3; + paravirt_ops.flush_tlb_user = lguest_flush_tlb; + paravirt_ops.flush_tlb_single = lguest_flush_tlb_single; + paravirt_ops.flush_tlb_kernel = lguest_flush_tlb_kernel; + paravirt_ops.set_pte = lguest_set_pte; + paravirt_ops.set_pte_at = lguest_set_pte_at; + paravirt_ops.set_pmd = lguest_set_pud; +#ifdef CONFIG_X86_LOCAL_APIC + paravirt_ops.apic_write = lguest_apic_write; + paravirt_ops.apic_write_atomic = lguest_apic_write_atomic; + paravirt_ops.apic_read = lguest_apic_read; +#endif + paravirt_ops.load_idt = lguest_load_idt; + paravirt_ops.iret = lguest_iret; + paravirt_ops.load_esp0 = lguest_load_esp0; + paravirt_ops.load_tr_desc = lguest_load_tr_desc; + paravirt_ops.set_ldt = lguest_set_ldt; + paravirt_ops.load_tls = lguest_load_tls; + paravirt_ops.set_debugreg = lguest_set_debugreg; + paravirt_ops.clts = lguest_clts; + paravirt_ops.read_cr0 = lguest_read_cr0; + paravirt_ops.write_cr0 = lguest_write_cr0; + paravirt_ops.init_IRQ = lguest_init_IRQ; + paravirt_ops.read_cr2 = lguest_read_cr2; + paravirt_ops.read_cr3 = lguest_read_cr3; + paravirt_ops.read_cr4 = lguest_read_cr4; + paravirt_ops.write_cr4 = lguest_write_cr4; + paravirt_ops.write_ldt_entry = lguest_write_ldt_entry; + paravirt_ops.write_gdt_entry = lguest_write_gdt_entry; + paravirt_ops.write_idt_entry = lguest_write_idt_entry; + paravirt_ops.patch = lguest_patch; + paravirt_ops.safe_halt = lguest_safe_halt; + paravirt_ops.get_wallclock = lguest_get_wallclock; + paravirt_ops.time_init = lguest_time_init; +#ifdef PARAVIRT_LAZY_NONE + paravirt_ops.set_lazy_mode = lguest_lazy_mode; +#endif + +memset (lguest_data.hcall_status,0xFF ,sizeof (lguest_data.hcall_status)); + lguest_data.noirq_start = (u32 )lguest_noirq_start; + lguest_data.noirq_end = (u32 )lguest_noirq_end; +hcall (LHCALL_LGUEST_INIT ,__pa (&lguest_data),0 ,0 ); +strncpy (saved_command_line, boot->cmdline,COMMAND_LINE_SIZE ); + +/* We use top of mem for initial pagetables. */ + init_pg_tables_end =__pa (pg0); + +/* set up PDA descriptor */ +pack_descriptor ((u32 *)&cpu_gdt_table[GDT_ENTRY_PDA ].a, + (u32 *)&cpu_gdt_table[GDT_ENTRY_PDA ].b, + (unsigned )&boot_pda,sizeof (boot_pda)-1 , +0x80 |DESCTYPE_S |0x02 ,0 ); +load_gdt (&cpu_gdt_descr); + asmvolatile ("mov %0, %%gs" : :"r" (__KERNEL_PDA) :"memory" ); + +reserve_top_address (lguest_data.reserve_mem); + +cpu_detect (&new_cpu_data); +/* Need this before paging_init. */ +set_bit (X86_FEATURE_PGE , new_cpu_data.x86_capability); +/* Math is always hard! */ + new_cpu_data.hard_math =1 ; + +/* FIXME: Better way? */ +/* Suppress vgacon startup code */ +SCREEN_INFO .orig_video_isVGA =VIDEO_TYPE_VLFB ; + +add_preferred_console ("hvc" ,0 ,NULL ); + +#ifdef CONFIG_X86_MCE + mce_disabled =1 ; +#endif + +#ifdef CONFIG_ACPI + acpi_disabled =1 ; + acpi_ht =0 ; +#endif +if (boot->initrd_size) { +/* We stash this at top of memory. */ +INITRD_START = boot->max_pfn*PAGE_SIZE - boot->initrd_size; +INITRD_SIZE = boot->initrd_size; +LOADER_TYPE =0xFF ; + } + + pm_power_off = lguest_power_off; +start_kernel (); +} + +asm("lguest_maybe_init:\n" + " cmpl $"__stringify(LGUEST_MAGIC_EBP)", %ebp\n" +" jne 1f\n" + " cmpl $"__stringify(LGUEST_MAGIC_EDI)", %edi\n" +" jne 1f\n" + " cmpl $"__stringify(LGUEST_MAGIC_ESI)", %esi\n" +" je lguest_init\n" + "1: ret"); +extern void asmlinkage lguest_maybe_init(void); +paravirt_probe (lguest_maybe_init);
===================================================================
arch/i386/lguest/lguest_bus.c new file +180 -0
180 lines not on this page · raw message
PATCH 1/1 lguest: the Makefiles
Finally, we put in the Makefile, so it will build.
You can see the pain involved in creating the switcher code (hypervisor.S) ready to be copied into the top of memory.
===================================================================
arch/i386/Makefile +1 -0
@@ -108,6 +108,7 @@ drivers-$(CONFIG_PCI) += arch/i386/pci # must be linked after kernel/ drivers-$(CONFIG_OPROFILE) += arch/i386/oprofile/ drivers-$(CONFIG_PM) += arch/i386/power/ +drivers-$(CONFIG_LGUEST_GUEST) += arch/i386/lguest/ CFLAGS += $(mflags-y) AFLAGS += $(mflags-y)
===================================================================
arch/i386/lguest/Makefile new file +22 -0
@@ -0,0 +1,22 @@ +# Guest requires the paravirt_ops replacement and the bus driver. +obj-$(CONFIG_LGUEST_GUEST) += lguest.o lguest_bus.o + +# Host requires the other files, which can be a module. +obj-$(CONFIG_LGUEST) += lg.o +lg-objs := core.o hypercalls.o page_tables.o interrupts_and_traps.o \ + segments.o io.o lguest_user.o + +# We use top 4MB for guest traps page, then hypervisor. */ +HYPE_ADDR := (0xFFC00000+4096) +# The data is only 1k (256 interrupt handler pointers) +HYPE_DATA_SIZE := 1024 +CFLAGS += -DHYPE_ADDR="$(HYPE_ADDR)" -DHYPE_DATA_SIZE="$(HYPE_DATA_SIZE)" + +$(obj)/core.o: $(obj)/hypervisor-blob.c +# This links the hypervisor in the right place and turns it into a C array. +$(obj)/hypervisor-raw: $(obj)/hypervisor.o + @$(LD) -static -Tdata=`printf %#x $$(($(HYPE_ADDR)))` -Ttext=`printf %#x $$(($(HYPE_ADDR)+$(HYPE_DATA_SIZE)))` -o $@ $< && $(OBJCOPY) -O binary $@ +$(obj)/hypervisor-blob.c: $(obj)/hypervisor-raw + @od -tx1 -An -v $< | sed -e 's/^ /0x/' -e 's/$$/,/' -e 's/ /,0x/g' > $@ + +clean-files := hypervisor-blob.c hypervisor-raw
Re: [PATCH 6c/10] lguest: the guest code
In reply to Rusty Russellarch/i386/lguest/lguest.c line 574
Quoted 4 lines · Rusty Russell
arch/i386/lguest/lguest.c new file +4
-0+#ifdef CONFIG_ACPI + acpi_disabled = 1; + acpi_ht = 0; +#endif
If this is hard-coded to have ACPI disabled, why isn't it enforced at build-time?
thanks,
-Len
Re: [PATCH 6c/10] lguest: the guest code
Quoted 8 lines · Len Brown
arch/i386/lguest/lguest.c line 574
Quoted 4 lines · Rusty Russell
arch/i386/lguest/lguest.c new file +4
-0+#ifdef CONFIG_ACPI + acpi_disabled = 1; + acpi_ht = 0; +#endif If this is hard-coded to have ACPI disabled, why isn't it enforced at build-time?
This is being disabled in the guest kernel only. The host and guest kernels are expected to be the same build.
- James
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH 6c/10] lguest: the guest code
Quoted 11 lines · James Morris
Quoted 8 lines · Len Brown
arch/i386/lguest/lguest.c line 574
Quoted 4 lines · Rusty Russell
arch/i386/lguest/lguest.c new file +4
-0+#ifdef CONFIG_ACPI + acpi_disabled = 1; + acpi_ht = 0; +#endif If this is hard-coded to have ACPI disabled, why isn't it enforced at build-time?
Quoted 2 lines
This is being disabled in the guest kernel only. The host and guest kernels are expected to be the same build.
Okay, but better to use disable_acpi()
indeed, since this would be the first code not already inside CONFIG_ACPI to invoke disable_acpi(), we could define the inline as empty and you could then scratch the #ifdef too.
cheers,
-Len
[PATCH 11/10] lguest: use disable_acpi()
Quoted 8 lines · Len Brown
Quoted 2 lines · James Morris
This is being disabled in the guest kernel only. The host and guest kernels are expected to be the same build.
Okay, but better to use
disable_acpi()
indeed, since this would be the first code not already insideCONFIG_ACPIto invokedisable_acpi(), we could define the inline as empty and you could then scratch the #ifdef too.
Thanks Len!
This applies on top of that series.
==
Quoted 4 lines · Len Brown
Okay, but better to use
disable_acpi()
indeed, since this would be the first code not already insideCONFIG_ACPIto invokedisable_acpi(), we could define the inline as empty and you could then scratch the #ifdef too.
arch/i386/lguest/lguest.c +1 -4
@@ -555,10 +555,7 @@ static __attribute_used__ __init void lg mce_disabled =1 ;#endif -#ifdef CONFIG_ACPI - acpi_disabled =1 ; - acpi_ht =0 ; -#endif +disable_acpi ();if (boot->initrd_size) {/* We stash this at top of memory. */ INITRD_START = boot->max_pfn*PAGE_SIZE - boot->initrd_size;
include/asm-i386/acpi.h +1 -0
@@ -127,6 +127,7 @@ extern int acpi_irq_balance_set(char *st#define acpi_ioapic 0static inline void acpi_noirq_set (void ) { }static inline void acpi_disable_pci (void ) { } +static inline void disable_acpi (void ) { }#endif /* !CONFIG_ACPI */
Re: [PATCH 1/10] lguest: Don't rely on last-linked fallthru when no paravirt handler
In reply to Rusty Russellarch/i386/kernel/head.S 1: · line 526
Quoted 5 lines · Rusty Russell
arch/i386/kernel/head.S +5
-0+unhandled_paravirt: + /* Nothing wanted us: try to die with dignity (impossible trap). */ + movl $0x1F, %edx + pushl $0 + jmp early_fault
Please print a real message with early_printk
-Andi
Re: [PATCH 1/10] lguest: Don't rely on last-linked fallthru when no paravirt handler
Quoted 9 lines · Andi Kleen
arch/i386/kernel/head.S 1: · line 526
Quoted 5 lines · Rusty Russell
arch/i386/kernel/head.S +5
-0+unhandled_paravirt: + /* Nothing wanted us: try to die with dignity (impossible trap). */ + movl $0x1F, %edx + pushl $0 + jmp early_faultPlease print a real message with
early_printk
If we make it thought early_fault, this will do just that.
Given this is a "never happens" situation, however... if you're actually under Xen or lguest, you won't make it that far (lguest, at least, will kill you on the cr2 load in early_fault, but it doesn't matter because we won't get anywhere with early_printk anyway).
Actually, if we did BUG() here at least lguest would print something... I wonder what Xen would do...
Rusty.
Re: [PATCH 1/10] lguest: Don't rely on last-linked fallthru when no paravirt handler
Quoted 9 lines · Rusty Russell
If we make it thought
early_fault, this will do just that.Given this is a "never happens" situation, however... if you're actually under Xen or lguest, you won't make it that far (lguest, at least, will kill you on the cr2 load in
early_fault, but it doesn't matter because we won't get anywhere withearly_printkanyway).Actually, if we did
BUG()here at least lguest would print something... I wonder what Xen would do...
Xen would print a complete register dump and backtrace. There's not a lot else you can do here; you could try early_printk, but if we're in a strange virtual environment, there may be no device on which the output could appear.
J