lguest
Subsystems (5)
cover lguest
As promised to Andrew, and with much thanks to Andi Kleen for feedback, this is the new series of lguest patches.
Main change is the move to drivers/lguest (for future non-i386 expansion), but lots of cleanups driven by Andi's feedback and the documentation effort (which made me examine every line of code).
It's not perfect, but it's definitely useful.
Cheers,
Rusty.
List of changes from the previous version in -mm:
- Updated to 2.6.21-rc6-mm1
- Moved to drivers/lguest
- Don't bother fflush()ing in verbose(): we always use a \n now anyway.
- Remove part of lguest.txt which documents the code: moving them to the code, is in progress.
- Move assembler parts of lguest.c out to lguest_asm.S.
- Use ktimer_get_real_ts() rather than do_gettimeofday. It's the new black!
- Use explicit types (w/ typechecking) for shadow pagetable code.
- Use unsigned long for addresses rather than u32.
- Comment on wmb().
- Use mask-in, not mask-out on CPUID for more future proofiness.
- Implement noop wbinvd (used when cpuid says there's no clflush, ie. pre Pentium 4). (Discovered while playing with CPUID).
- Rename many internal shadow pagetable functions & vars for clarity.
- Remove cr3 member from "struct lguest": easy to calculate when needed.
- Add comment on lack of interface stability.
- Move lguest_devices array declaration from lguest.h to lguest_bus.h (it's only useful for devices).
- Use read_cr0() and write_cr0() rather than open-coding them.
- Use KERN_INFO on printk() for lguest initialization
- Remove unneeded __attribute_used__ in core.c
- Print out message when failing to run lguest under lguest/Xen etc.
- Use a wmb() for dma transfer code, not mb().
- Change hash table size from 64 to 61.
- Reorder can cleanup of example launcher code to make it suck less.
- s/hypervisor/switcher/ for more clarity.
- lguest_address_ok() now takes length to check.
- Rename GUEST_DPL to GUEST_PL: it's not just for descriptors.
- Wake other guest when it's out of buffers, don't just yield.
- Rename "addr" to "key" as I/O argument.
- Rename "interrupts" in "struct lguest_data" to "blocked_interrupts".
- Don't set regs fields to 0: allocated with get_zeroed_page() anyway.
- Various minor cleanups.
PATCH 1/8 lguest: the guest code
This is the code and headers required to make an i386 kernel an lguest guest.
--- drivers/lguest/lguest.c | 509 +++++++++++++++++++++++++++++++++++++++++++ drivers/lguest/lguest_asm.S | 59 ++++ drivers/lguest/lguest_bus.c | 148 ++++++++++++ include/linux/lguest.h | 85 +++++++ include/linux/lguest_bus.h | 33 ++ 5 files changed, 834 insertions(+)
===================================================================
drivers/lguest/lguest.c new file +509 -0
@@ -0,0 +1,509 @@ +/* +* 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/lguest.h> +#include <linux/lguest_launcher.h> +#include <asm/paravirt.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/mce.h> + +/* Declarations for definitions in lguest_guest.S */ +extern char lguest_noirq_start[], lguest_noirq_end[]; +extern const char lgstart_cli[], lgend_cli[]; +extern const char lgstart_sti[], lgend_sti[]; +extern const char lgstart_popf[], lgend_popf[]; +extern const char lgstart_pushf[], lgend_pushf[]; +extern const char lgstart_iret[], lgend_iret[]; +extern asmlinkage void lguest_maybe_init(void); +extern void lguest_iret (void ); + +struct lguest_data lguest_data = { + .hcall_status = { [0 ...LHCALL_RING_SIZE -1 ] =0xFF }, + .noirq_start = (u32 )lguest_noirq_start, + .noirq_end = (u32 )lguest_noirq_end, + .blocked_interrupts = {1 },/* Block timer interrupts */ +}; +struct lguest_device_desc *lguest_devices; +static __initdata const struct lguest_boot_info *boot =__va (0 ); + +static enum paravirt_lazy_mode lazy_mode; +static void lguest_lazy_mode (enum paravirt_lazy_mode 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); +} + +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; +/* Make sure host sees arguments before "valid" flag. */ +wmb (); + lguest_data.hcall_status[next_call] =0 ; +if (++next_call ==LHCALL_RING_SIZE ) + next_call =0 ; + } +local_irq_restore (flags); +} + +static unsigned long save_fl (void ) +{ +return lguest_data.irq_enabled; +} + +static void restore_fl (unsigned long flags) +{ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled = flags; +} + +static void irq_disable (void ) +{ + lguest_data.irq_enabled =0 ; +} + +static void irq_enable (void ) +{ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled =X86_EFLAGS_IF ; +} + +static void lguest_write_idt_entry (struct desc_struct *dt, +int entrynum,u32 low,u32 high) +{ +write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_IDT_ENTRY , entrynum, low, high); +} + +static void 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 void 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 lguest_write_gdt_entry (struct desc_struct *dt, +int entrynum,u32 low,u32 high) +{ +write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_GDT ,__pa (dt),GDT_ENTRIES ,0 ); +} + +static void lguest_load_tls (struct thread_struct *t,unsigned int cpu) +{ +lazy_hcall (LHCALL_LOAD_TLS ,__pa (&t->tls_array), cpu,0 ); +} + +static void lguest_set_ldt (const void *addr,unsigned entries) +{ +} + +static void lguest_load_tr_desc (void ) +{ +} + +static void lguest_cpuid (unsigned int *eax,unsigned int *ebx, +unsigned int *ecx,unsigned int *edx) +{ +int function = *eax; + +native_cpuid (eax, ebx, ecx, edx); +switch (function) { +case 1 :/* Basic feature request. */ +/* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */ + *ecx &=0x00002201 ; +/* Similarly: SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, FPU. */ + *edx &=0x07808101 ; +/* Host wants to know when we flush kernel pages: set PGE. */ + *edx |=0x00002000 ; +break ; +case 0x80000000 : +/* Futureproof this a little: if they ask how much extended +* processor information, limit it to known fields. */ +if (*eax >0x80000008 ) + *eax =0x80000008 ; +break ; + } +} + +static unsigned long current_cr0, current_cr3; +static void lguest_write_cr0 (unsigned long val) +{ +hcall (LHCALL_TS , val &8 ,0 ,0 ); + current_cr0 = val; +} + +static unsigned long lguest_read_cr0 (void ) +{ +return current_cr0; +} + +static void lguest_clts (void ) +{ +lazy_hcall (LHCALL_TS ,0 ,0 ,0 ); + current_cr0 &= ~8U ; +} + +static unsigned long lguest_read_cr2 (void ) +{ +return lguest_data.cr2; +} + +static void lguest_write_cr3 (unsigned long cr3) +{ +hcall (LHCALL_NEW_PGTABLE , cr3,0 ,0 ); + current_cr3 = cr3; +} + +static unsigned long lguest_read_cr3 (void ) +{ +return current_cr3; +} + +/* Used to enable/disable PGE, but we don't care. */ +static unsigned long lguest_read_cr4 (void ) +{ +return 0 ; +} + +static void lguest_write_cr4 (unsigned long val) +{ +} + +static void lguest_set_pte_at (struct mm_struct *mm,unsigned long 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 void lguest_set_pmd (pmd_t *pmdp,pmd_t pmdval) +{ + *pmdp = pmdval; +lazy_hcall (LHCALL_SET_PMD ,__pa (pmdp)&PAGE_MASK , + (__pa (pmdp)&(PAGE_SIZE -1 ))/4 ,0 ); +} + +/* FIXME: Eliminate all callers of this. */ +static 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_FLUSH_TLB ,1 ,0 ,0 ); +} + +static void lguest_flush_tlb_single (unsigned long addr) +{ +/* Simply set it to zero, and it will fault back in. */ +lazy_hcall (LHCALL_SET_PTE , current_cr3, addr,0 ); +} + +static void lguest_flush_tlb_user (void ) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,0 ,0 ,0 ); +} + +static void lguest_flush_tlb_kernel (void ) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,1 ,0 ,0 ); +} + +static void disable_lguest_irq (unsigned int irq) +{ +set_bit (irq, lguest_data.blocked_interrupts); +} + +static void enable_lguest_irq (unsigned int irq) +{ +clear_bit (irq, lguest_data.blocked_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 __init lguest_init_IRQ (void ) +{ +unsigned int i; + +for (i =0 ; i <LGUEST_IRQS ; i++) { +int vector =FIRST_EXTERNAL_VECTOR + i; +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 unsigned long lguest_get_wallclock (void ) +{ +return hcall (LHCALL_GET_WALLCLOCK ,0 ,0 ,0 ); +} + +static void 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 lguest_time_init (void ) +{ +set_irq_handler (0 , lguest_time_irq); +hcall (LHCALL_TIMER_READ ,0 ,0 ,0 ); +enable_lguest_irq (0 ); +} + +static void 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 void lguest_set_debugreg (int regno,unsigned long value) +{ +/* FIXME: Implement */ +} + +static void lguest_wbinvd (void ) +{ +} + +#ifdef CONFIG_X86_LOCAL_APIC +static void lguest_apic_write (unsigned long reg,unsigned long v) +{ +} + +static unsigned long lguest_apic_read (unsigned long reg) +{ +return 0 ; +} +#endif + +static void lguest_safe_halt (void ) +{ +hcall (LHCALL_HALT ,0 ,0 ,0 ); +} + +static void lguest_power_off (void ) +{ +hcall (LHCALL_CRASH ,__pa ("Power down" ),0 ,0 ); +} + +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 __init char *lguest_memory_setup (void ) +{ +/* We do this here because lockcheck barfs if before start_kernel */ +atomic_notifier_chain_register (&panic_notifier_list, &paniced); + + e820.nr_map =0 ; +add_memory_region (0 ,PFN_PHYS (boot->max_pfn),E820_RAM ); +return "LGUEST" ; +} + +static const struct lguest_insns +{ +const char *start, *end; +} lguest_insns[] = { + [PARAVIRT_PATCH (irq_disable)] = { lgstart_cli, lgend_cli }, + [PARAVIRT_PATCH (irq_enable)] = { lgstart_sti, lgend_sti }, + [PARAVIRT_PATCH (restore_fl)] = { lgstart_popf, lgend_popf }, + [PARAVIRT_PATCH (save_fl)] = { lgstart_pushf, lgend_pushf }, + [PARAVIRT_PATCH (iret)] = { lgstart_iret, lgend_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_PATCH (iret)) { +/* Jumps are relative. */ +u32 off = (u32 )lguest_iret - ((u32 )insns + insn_len); +memcpy (insns+1 , &off,sizeof (off)); + } +return insn_len; +} + +/* From head.S */ +extern void setup_pda (void ); +extern struct Xgt_desc_struct early_gdt_descr; + +__init void lguest_init (void ) +{ + 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_user; + 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_pmd; +#ifdef CONFIG_X86_LOCAL_APIC + paravirt_ops.apic_write = lguest_apic_write; + paravirt_ops.apic_write_atomic = lguest_apic_write; + 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_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; + paravirt_ops.set_lazy_mode = lguest_lazy_mode; + paravirt_ops.wbinvd = lguest_wbinvd; + +hcall (LHCALL_LGUEST_INIT ,__pa (&lguest_data),0 ,0 ); +strncpy (boot_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 */ +setup_pda (); +load_gdt (&early_gdt_descr); + asmvolatile ("mov %0, %%fs" : :"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 ; + +#ifdef CONFIG_X86_MCE + mce_disabled =1 ; +#endif + +#ifdef CONFIG_ACPI + acpi_disabled =1 ; + acpi_ht =0 ; +#endif + +add_preferred_console ("hvc" ,0 ,NULL ); + +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 (); +} +paravirt_probe (lguest_maybe_init);
===================================================================
drivers/lguest/lguest_asm.S new file +59 -0
59 lines not on this page · raw message
===================================================================
drivers/lguest/lguest_bus.c new file +148 -0
148 lines not on this page · raw message
===================================================================
include/linux/lguest.h new file +85 -0
85 lines not on this page · raw message
===================================================================
include/linux/lguest_bus.h new file +33 -0
@@ -0,0 +1,33 @@ +#ifndef _ASM_LGUEST_DEVICE_H +#define _ASM_LGUEST_DEVICE_H +/* Everything you need to know about lguest devices. */ +#include <linux/device.h> +#include <linux/lguest.h> +#include <linux/lguest_launcher.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); + +extern struct lguest_device_desc *lguest_devices;/* Just past max_pfn */ +#endif /* _ASM_LGUEST_DEVICE_H */
PATCH 1/8 lguest: the guest code
In reply to Rusty RussellThis is the code and headers required to make an i386 kernel an lguest guest.
--- drivers/lguest/lguest.c | 509 +++++++++++++++++++++++++++++++++++++++++++ drivers/lguest/lguest_asm.S | 59 ++++ drivers/lguest/lguest_bus.c | 148 ++++++++++++ include/linux/lguest.h | 85 +++++++ include/linux/lguest_bus.h | 33 ++ 5 files changed, 834 insertions(+)
===================================================================
drivers/lguest/lguest.c new file +509 -0
@@ -0,0 +1,509 @@ +/* +* 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/lguest.h> +#include <linux/lguest_launcher.h> +#include <asm/paravirt.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/mce.h> + +/* Declarations for definitions in lguest_guest.S */ +extern char lguest_noirq_start[], lguest_noirq_end[]; +extern const char lgstart_cli[], lgend_cli[]; +extern const char lgstart_sti[], lgend_sti[]; +extern const char lgstart_popf[], lgend_popf[]; +extern const char lgstart_pushf[], lgend_pushf[]; +extern const char lgstart_iret[], lgend_iret[]; +extern asmlinkage void lguest_maybe_init(void); +extern void lguest_iret (void ); + +struct lguest_data lguest_data = { + .hcall_status = { [0 ...LHCALL_RING_SIZE -1 ] =0xFF }, + .noirq_start = (u32 )lguest_noirq_start, + .noirq_end = (u32 )lguest_noirq_end, + .blocked_interrupts = {1 },/* Block timer interrupts */ +}; +struct lguest_device_desc *lguest_devices; +static __initdata const struct lguest_boot_info *boot =__va (0 ); + +static enum paravirt_lazy_mode lazy_mode; +static void lguest_lazy_mode (enum paravirt_lazy_mode 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); +} + +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; +/* Make sure host sees arguments before "valid" flag. */ +wmb (); + lguest_data.hcall_status[next_call] =0 ; +if (++next_call ==LHCALL_RING_SIZE ) + next_call =0 ; + } +local_irq_restore (flags); +} + +static unsigned long save_fl (void ) +{ +return lguest_data.irq_enabled; +} + +static void restore_fl (unsigned long flags) +{ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled = flags; +} + +static void irq_disable (void ) +{ + lguest_data.irq_enabled =0 ; +} + +static void irq_enable (void ) +{ +/* FIXME: Check if interrupt pending... */ + lguest_data.irq_enabled =X86_EFLAGS_IF ; +} + +static void lguest_write_idt_entry (struct desc_struct *dt, +int entrynum,u32 low,u32 high) +{ +write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_IDT_ENTRY , entrynum, low, high); +} + +static void 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 void 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 lguest_write_gdt_entry (struct desc_struct *dt, +int entrynum,u32 low,u32 high) +{ +write_dt_entry (dt, entrynum, low, high); +hcall (LHCALL_LOAD_GDT ,__pa (dt),GDT_ENTRIES ,0 ); +} + +static void lguest_load_tls (struct thread_struct *t,unsigned int cpu) +{ +lazy_hcall (LHCALL_LOAD_TLS ,__pa (&t->tls_array), cpu,0 ); +} + +static void lguest_set_ldt (const void *addr,unsigned entries) +{ +} + +static void lguest_load_tr_desc (void ) +{ +} + +static void lguest_cpuid (unsigned int *eax,unsigned int *ebx, +unsigned int *ecx,unsigned int *edx) +{ +int function = *eax; + +native_cpuid (eax, ebx, ecx, edx); +switch (function) { +case 1 :/* Basic feature request. */ +/* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */ + *ecx &=0x00002201 ; +/* Similarly: SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, FPU. */ + *edx &=0x07808101 ; +/* Host wants to know when we flush kernel pages: set PGE. */ + *edx |=0x00002000 ; +break ; +case 0x80000000 : +/* Futureproof this a little: if they ask how much extended +* processor information, limit it to known fields. */ +if (*eax >0x80000008 ) + *eax =0x80000008 ; +break ; + } +} + +static unsigned long current_cr0, current_cr3; +static void lguest_write_cr0 (unsigned long val) +{ +hcall (LHCALL_TS , val &8 ,0 ,0 ); + current_cr0 = val; +} + +static unsigned long lguest_read_cr0 (void ) +{ +return current_cr0; +} + +static void lguest_clts (void ) +{ +lazy_hcall (LHCALL_TS ,0 ,0 ,0 ); + current_cr0 &= ~8U ; +} + +static unsigned long lguest_read_cr2 (void ) +{ +return lguest_data.cr2; +} + +static void lguest_write_cr3 (unsigned long cr3) +{ +hcall (LHCALL_NEW_PGTABLE , cr3,0 ,0 ); + current_cr3 = cr3; +} + +static unsigned long lguest_read_cr3 (void ) +{ +return current_cr3; +} + +/* Used to enable/disable PGE, but we don't care. */ +static unsigned long lguest_read_cr4 (void ) +{ +return 0 ; +} + +static void lguest_write_cr4 (unsigned long val) +{ +} + +static void lguest_set_pte_at (struct mm_struct *mm,unsigned long 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 void lguest_set_pmd (pmd_t *pmdp,pmd_t pmdval) +{ + *pmdp = pmdval; +lazy_hcall (LHCALL_SET_PMD ,__pa (pmdp)&PAGE_MASK , + (__pa (pmdp)&(PAGE_SIZE -1 ))/4 ,0 ); +} + +/* FIXME: Eliminate all callers of this. */ +static 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_FLUSH_TLB ,1 ,0 ,0 ); +} + +static void lguest_flush_tlb_single (unsigned long addr) +{ +/* Simply set it to zero, and it will fault back in. */ +lazy_hcall (LHCALL_SET_PTE , current_cr3, addr,0 ); +} + +static void lguest_flush_tlb_user (void ) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,0 ,0 ,0 ); +} + +static void lguest_flush_tlb_kernel (void ) +{ +lazy_hcall (LHCALL_FLUSH_TLB ,1 ,0 ,0 ); +} + +static void disable_lguest_irq (unsigned int irq) +{ +set_bit (irq, lguest_data.blocked_interrupts); +} + +static void enable_lguest_irq (unsigned int irq) +{ +clear_bit (irq, lguest_data.blocked_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 __init lguest_init_IRQ (void ) +{ +unsigned int i; + +for (i =0 ; i <LGUEST_IRQS ; i++) { +int vector =FIRST_EXTERNAL_VECTOR + i; +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 unsigned long lguest_get_wallclock (void ) +{ +return hcall (LHCALL_GET_WALLCLOCK ,0 ,0 ,0 ); +} + +static void 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 lguest_time_init (void ) +{ +set_irq_handler (0 , lguest_time_irq); +hcall (LHCALL_TIMER_READ ,0 ,0 ,0 ); +enable_lguest_irq (0 ); +} + +static void 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 void lguest_set_debugreg (int regno,unsigned long value) +{ +/* FIXME: Implement */ +} + +static void lguest_wbinvd (void ) +{ +} + +#ifdef CONFIG_X86_LOCAL_APIC +static void lguest_apic_write (unsigned long reg,unsigned long v) +{ +} + +static unsigned long lguest_apic_read (unsigned long reg) +{ +return 0 ; +} +#endif + +static void lguest_safe_halt (void ) +{ +hcall (LHCALL_HALT ,0 ,0 ,0 ); +} + +static void lguest_power_off (void ) +{ +hcall (LHCALL_CRASH ,__pa ("Power down" ),0 ,0 ); +} + +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 __init char *lguest_memory_setup (void ) +{ +/* We do this here because lockcheck barfs if before start_kernel */ +atomic_notifier_chain_register (&panic_notifier_list, &paniced); + + e820.nr_map =0 ; +add_memory_region (0 ,PFN_PHYS (boot->max_pfn),E820_RAM ); +return "LGUEST" ; +} + +static const struct lguest_insns +{ +const char *start, *end; +} lguest_insns[] = { + [PARAVIRT_PATCH (irq_disable)] = { lgstart_cli, lgend_cli }, + [PARAVIRT_PATCH (irq_enable)] = { lgstart_sti, lgend_sti }, + [PARAVIRT_PATCH (restore_fl)] = { lgstart_popf, lgend_popf }, + [PARAVIRT_PATCH (save_fl)] = { lgstart_pushf, lgend_pushf }, + [PARAVIRT_PATCH (iret)] = { lgstart_iret, lgend_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_PATCH (iret)) { +/* Jumps are relative. */ +u32 off = (u32 )lguest_iret - ((u32 )insns + insn_len); +memcpy (insns+1 , &off,sizeof (off)); + } +return insn_len; +} + +/* From head.S */ +extern void setup_pda (void ); +extern struct Xgt_desc_struct early_gdt_descr; + +__init void lguest_init (void ) +{ + 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_user; + 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_pmd; +#ifdef CONFIG_X86_LOCAL_APIC + paravirt_ops.apic_write = lguest_apic_write; + paravirt_ops.apic_write_atomic = lguest_apic_write; + 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_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; + paravirt_ops.set_lazy_mode = lguest_lazy_mode; + paravirt_ops.wbinvd = lguest_wbinvd; + +hcall (LHCALL_LGUEST_INIT ,__pa (&lguest_data),0 ,0 ); +strncpy (boot_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 */ +setup_pda (); +load_gdt (&early_gdt_descr); + asmvolatile ("mov %0, %%fs" : :"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 ; + +#ifdef CONFIG_X86_MCE + mce_disabled =1 ; +#endif + +#ifdef CONFIG_ACPI + acpi_disabled =1 ; + acpi_ht =0 ; +#endif + +add_preferred_console ("hvc" ,0 ,NULL ); + +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 (); +} +paravirt_probe (lguest_maybe_init);
===================================================================
drivers/lguest/lguest_asm.S new file +59 -0
59 lines not on this page · raw message
===================================================================
drivers/lguest/lguest_bus.c new file +148 -0
148 lines not on this page · raw message
===================================================================
include/linux/lguest.h new file +85 -0
85 lines not on this page · raw message
===================================================================
include/linux/lguest_bus.h new file +33 -0
@@ -0,0 +1,33 @@ +#ifndef _ASM_LGUEST_DEVICE_H +#define _ASM_LGUEST_DEVICE_H +/* Everything you need to know about lguest devices. */ +#include <linux/device.h> +#include <linux/lguest.h> +#include <linux/lguest_launcher.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); + +extern struct lguest_device_desc *lguest_devices;/* Just past max_pfn */ +#endif /* _ASM_LGUEST_DEVICE_H */
PATCH 2/8 lguest: the host code
This is the code for the "lg.ko" module, which allows lguest guests to be launched.
--- drivers/lguest/core.c | 444 +++++++++++++++++++++++++++++++++ drivers/lguest/hypercalls.c | 172 ++++++++++++ drivers/lguest/interrupts_and_traps.c | 258 +++++++++++++++++++ drivers/lguest/io.c | 412 ++++++++++++++++++++++++++++++ drivers/lguest/lg.h | 278 ++++++++++++++++++++ drivers/lguest/lguest_user.c | 192 ++++++++++++++ drivers/lguest/page_tables.c | 407 ++++++++++++++++++++++++++++++ drivers/lguest/segments.c | 115 ++++++++ drivers/lguest/switcher.S | 159 +++++++++++ include/linux/lguest_launcher.h | 80 +++++ 10 files changed, 2517 insertions(+)
===================================================================
drivers/lguest/core.c new file +444 -0
@@ -0,0 +1,444 @@ +/* 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 <linux/cpu.h> +#include <linux/freezer.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 <asm/i387.h> +#include "lg.h" + +/* Found in switcher.S */ +extern char start_switcher_text[], end_switcher_text[], switch_to_guest[]; +extern unsigned long default_idt_entries[]; + +/* Every guest maps the core switcher code. */ +#define SHARED_SWITCHER_PAGES \ + DIV_ROUND_UP(end_switcher_text - start_switcher_text, PAGE_SIZE) +/* Pages for switcher itself, then two pages per cpu */ +#define TOTAL_SWITCHER_PAGES (SHARED_SWITCHER_PAGES + 2 * NR_CPUS) + +/* We map at -4M for ease of mapping into the guest (one PTE page). */ +#define SWITCHER_ADDR 0xFFC00000 + +static struct vm_struct *switcher_vma; +static struct page **switcher_page; + +static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; + +/* This One Big lock protects all inter-guest data structures. */ +DEFINE_MUTEX (lguest_lock); +static DEFINE_PER_CPU(struct lguest *, last_guest); + +/* FIXME: Make dynamic. */ +#define MAX_LGUEST_GUESTS 16 +struct lguest lguests[MAX_LGUEST_GUESTS ]; + +/* Offset from where switcher.S was compiled to where we've copied it */ +static unsigned long switcher_offset (void ) +{ +return SWITCHER_ADDR - (unsigned long )start_switcher_text; +} + +/* This cpu's struct lguest_pages. */ +static struct lguest_pages *lguest_pages (unsigned int cpu) +{ +return &(((struct lguest_pages *) + (SWITCHER_ADDR +SHARED_SWITCHER_PAGES *PAGE_SIZE ))[cpu]); +} + +static __init int map_switcher (void ) +{ +int i, err; +struct page **pagep; + + switcher_page =kmalloc (sizeof (switcher_page[0 ])*TOTAL_SWITCHER_PAGES , +GFP_KERNEL ); +if (!switcher_page) { + err = -ENOMEM ; + goto out; + } + +for (i =0 ; i <TOTAL_SWITCHER_PAGES ; i++) { +unsigned long addr =get_zeroed_page (GFP_KERNEL ); +if (!addr) { + err = -ENOMEM ; + goto free_some_pages; + } + switcher_page[i] =virt_to_page (addr); + } + + switcher_vma =__get_vm_area (TOTAL_SWITCHER_PAGES *PAGE_SIZE , +VM_ALLOC ,SWITCHER_ADDR ,VMALLOC_END ); +if (!switcher_vma) { + err = -ENOMEM ; +printk ("lguest: could not map switcher pages high\n" ); + goto free_pages; + } + + pagep = switcher_page; + err =map_vm_area (switcher_vma,PAGE_KERNEL , &pagep); +if (err) { +printk ("lguest: map_vm_area failed: %i\n" , err); + goto free_vma; + } +memcpy (switcher_vma->addr, start_switcher_text, + end_switcher_text - start_switcher_text); + +/* Fix up IDT entries to point into copied text. */ +for (i =0 ; i <IDT_ENTRIES ; i++) + default_idt_entries[i] +=switcher_offset (); + +for_each_possible_cpu (i) { +struct lguest_pages *pages =lguest_pages (i); +struct lguest_ro_state *state = &pages->state; + +/* These fields are static: rest done in copy_in_guest_info */ + state->host_gdt_desc.size =GDT_SIZE -1 ; + state->host_gdt_desc.address = (long )get_cpu_gdt_table (i); +store_idt (&state->host_idt_desc); + state->guest_idt_desc.size =sizeof (state->guest_idt)-1 ; + state->guest_idt_desc.address = (long )&state->guest_idt; + state->guest_gdt_desc.size =sizeof (state->guest_gdt)-1 ; + state->guest_gdt_desc.address = (long )&state->guest_gdt; + state->guest_tss.esp0 = (long )(&pages->regs +1 ); + state->guest_tss.ss0 =LGUEST_DS ; +/* No I/O for you! */ + state->guest_tss.io_bitmap_base =sizeof (state->guest_tss); +setup_default_gdt_entries (state); +setup_default_idt_entries (state, default_idt_entries); + +/* Setup LGUEST segments on all cpus */ +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 switcher. */ + lguest_entry.offset = (long )switch_to_guest +switcher_offset (); + lguest_entry.segment =LGUEST_CS ; + +printk (KERN_INFO "lguest: mapped switcher at %p\n" , + switcher_vma->addr); +return 0 ; + +free_vma: +vunmap (switcher_vma->addr); +free_pages: + i =TOTAL_SWITCHER_PAGES ; +free_some_pages: +for (--i; i >=0 ; i--) +__free_pages (switcher_page[i],0 ); +kfree (switcher_page); +out: +return err; +} + +static void unmap_switcher (void ) +{ +unsigned int i; + +vunmap (switcher_vma->addr); +for (i =0 ; i <TOTAL_SWITCHER_PAGES ; i++) +__free_pages (switcher_page[i],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->regs->eip); + +/* This only works for addresses in linear mapping... */ +if (lg->regs->eip < lg->page_offset) +return 0 ; +lgread (lg, &insn, physaddr,1 ); + +/* Operand size prefix means it's actually for ax. */ +if (insn ==0x66 ) { + shift =16 ; + insnlen =1 ; +lgread (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->regs->eax =0xFFFFFFFF ; +else + lg->regs->eax |= (0xFFFF << shift); + } + lg->regs->eip += insnlen; +return 1 ; +} + +int lguest_address_ok (const struct lguest *lg, +unsigned long addr,unsigned long len) +{ +return (addr+len) /PAGE_SIZE < lg->pfn_limit && (addr+len >= addr); +} + +/* Just like get_user, but don't let guest access lguest binary. */ +u32 lgread_u32 (struct lguest *lg,u32 addr) +{ +u32 val =0 ; + +/* Don't let them access lguest binary */ +if (!lguest_address_ok (lg, addr,sizeof (val)) + ||get_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad read address %u" , addr); +return val; +} + +void lgwrite_u32 (struct lguest *lg,u32 addr,u32 val) +{ +if (!lguest_address_ok (lg, addr,sizeof (val)) + ||put_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad write address %u" , addr); +} + +void lgread (struct lguest *lg,void *b,u32 addr,unsigned bytes) +{ +if (!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 lgwrite (struct lguest *lg,u32 addr,const void *b,unsigned bytes) +{ +if (!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); +} + +static void set_ts (void ) +{ +u32 cr0; + + cr0 =read_cr0 (); +if (!(cr0 &8 )) +write_cr0 (cr0|8 ); +} + +static void copy_in_guest_info (struct lguest *lg,struct lguest_pages *pages) +{ +if (__get_cpu_var (last_guest) != lg || lg->last_pages != pages) { +__get_cpu_var (last_guest) = lg; + lg->last_pages = pages; + lg->changed =CHANGED_ALL ; + } + +/* These are pretty cheap, so we do them unconditionally. */ + pages->state.host_cr3 =__pa (current->mm->pgd); +map_switcher_in_guest (lg, pages); + pages->state.guest_tss.esp1 = lg->esp1; + pages->state.guest_tss.ss1 = lg->ss1; + +/* Copy direct trap entries. */ +if (lg->changed &CHANGED_IDT ) +copy_traps (lg, pages->state.guest_idt, default_idt_entries); + +/* Copy all GDT entries but the TSS. */ +if (lg->changed &CHANGED_GDT ) +copy_gdt (lg, pages->state.guest_gdt); + + lg->changed =0 ; +} + +static void run_guest_once (struct lguest *lg,struct lguest_pages *pages) +{ +unsigned int clobber; + +copy_in_guest_info (lg, pages); + +/* Put eflags on stack, lcall does rest: suitable for iret return. */ + asmvolatile ("pushf; lcall *lguest_entry" + :"=a" (clobber),"=b" (clobber) + :"0" (pages),"1" (__pa (lg->pgdirs[lg->pgdidx].pgdir)) + :"memory" ,"%edx" ,"%ecx" ,"%edi" ,"%esi" ); +} + +int run_guest (struct lguest *lg,char *__user user) +{ +while (!lg->dead) { +unsigned int cr2 =0 ;/* Damn gcc */ + +/* Hypercalls first: we might have been out to userspace */ +do_hypercalls (lg); +if (lg->dma_is_pending) { +put_user (lg->pending_dma, (unsigned long *)user); +put_user (lg->pending_key, (unsigned long *)user+1 ); +return sizeof (unsigned long )*2 ; + } + +if (signal_pending (current)) +return -EINTR ; +maybe_do_interrupt (lg); + +try_to_freeze (); + +if (lg->dead) +break ; + +if (lg->halted) { +set_current_state (TASK_INTERRUPTIBLE ); +schedule_timeout (1 ); +continue ; + } + +local_irq_disable (); + +/* Even if *we* don't want FPU trap, guest might... */ +if (lg->ts) +set_ts (); + +run_guest_once (lg,lguest_pages (raw_smp_processor_id ())); + +/* Save cr2 now if we page-faulted. */ +if (lg->regs->trapnum ==14 ) + cr2 =read_cr2 (); +else if (lg->regs->trapnum ==7 ) +math_state_restore (); +local_irq_enable (); + +switch (lg->regs->trapnum) { +case 13 :/* We've intercepted a GPF. */ +if (lg->regs->errcode ==0 ) { +if (emulate_insn (lg)) +continue ; + } +break ; +case 14 :/* We've intercepted a page fault. */ +if (demand_page (lg, cr2, lg->regs->errcode &2 )) +continue ; + +/* If lguest_data is NULL, this won't hurt. */ +put_user (cr2, &lg->lguest_data->cr2); +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 ; +break ; + case 32 ... 255: /* Real interrupt, fall thru */ +cond_resched (); +case LGUEST_TRAP_ENTRY :/* Handled at top of loop */ +continue ; + } + +if (deliver_trap (lg, lg->regs->trapnum)) +continue ; + +kill_guest (lg,"unhandled trap %i at %#x (%#x)" , + lg->regs->trapnum, lg->regs->eip, + lg->regs->trapnum ==14 ? cr2 : lg->regs->errcode); + } +return -ENOENT ; +} + +int find_free_guest (void ) +{ +unsigned int i; +for (i =0 ; i <MAX_LGUEST_GUESTS ; i++) +if (!lguests[i].tsk) +return i; +return -1 ; +} + +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 ()) { +printk ("lguest is afraid of %s\n" , paravirt_ops.name); +return -EPERM ; + } + + err =map_switcher (); +if (err) +return err; + + err =init_pagetables (switcher_page,SHARED_SWITCHER_PAGES ); +if (err) { +unmap_switcher (); +return err; + } +lguest_io_init (); + + err =lguest_device_init (); +if (err) { +free_pagetables (); +unmap_switcher (); +return err; + } +lock_cpu_hotplug (); +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); + } +unlock_cpu_hotplug (); +return 0 ; +} + +static void __exit fini(void) +{ +lguest_device_remove (); +free_pagetables (); +unmap_switcher (); +lock_cpu_hotplug (); +if (cpu_had_pge) { +set_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); +on_each_cpu (adjust_pge, (void *)1 ,0 ,1 ); + } +unlock_cpu_hotplug (); +} + +module_init (init); +module_exit (fini); +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Rusty Russell <[email protected]>" );
===================================================================
drivers/lguest/hypercalls.c new file +172 -0
172 lines not on this page · raw message
===================================================================
drivers/lguest/interrupts_and_traps.c new file +258 -0
258 lines not on this page · raw message
===================================================================
drivers/lguest/io.c new file +412 -0
412 lines not on this page · raw message
===================================================================
drivers/lguest/lg.h new file +278 -0
278 lines not on this page · raw message
===================================================================
drivers/lguest/lguest_user.c new file +192 -0
192 lines not on this page · raw message
===================================================================
drivers/lguest/page_tables.c new file +407 -0
407 lines not on this page · raw message
===================================================================
drivers/lguest/segments.c new file +115 -0
115 lines not on this page · raw message
===================================================================
drivers/lguest/switcher.S new file +159 -0
159 lines not on this page · raw message
===================================================================
include/linux/lguest_launcher.h new file +80 -0
80 lines not on this page · raw message
PATCH 3/8 lguest: the asm offsets
In reply to Rusty RussellThis is the structure offsets required by lg.ko's switcher.S.
Unfortunately we don't have infrastructure for private asm-offsets creation.
--- arch/i386/kernel/asm-offsets.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
===================================================================
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 <linux/lguest.h> +#include "../../../drivers/lguest/lg.h" +#endif #define DEFINE (sym, val) \ asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -114,4 +118,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_PAGES_host_gdt_desc, lguest_pages, state.host_gdt_desc); +OFFSET (LGUEST_PAGES_host_idt_desc, lguest_pages, state.host_idt_desc); +OFFSET (LGUEST_PAGES_host_cr3, lguest_pages, state.host_cr3); +OFFSET (LGUEST_PAGES_host_sp, lguest_pages, state.host_sp); +OFFSET (LGUEST_PAGES_guest_gdt_desc, lguest_pages,state.guest_gdt_desc); +OFFSET (LGUEST_PAGES_guest_idt_desc, lguest_pages,state.guest_idt_desc); +OFFSET (LGUEST_PAGES_guest_gdt, lguest_pages, state.guest_gdt); +OFFSET (LGUEST_PAGES_regs_trapnum, lguest_pages, regs.trapnum); +OFFSET (LGUEST_PAGES_regs_errcode, lguest_pages, regs.errcode); +OFFSET (LGUEST_PAGES_regs, lguest_pages, regs); +#endif }
PATCH 2/8 lguest: the host code
In reply to Rusty RussellThis is the code for the "lg.ko" module, which allows lguest guests to be launched.
--- drivers/lguest/core.c | 444 +++++++++++++++++++++++++++++++++ drivers/lguest/hypercalls.c | 172 ++++++++++++ drivers/lguest/interrupts_and_traps.c | 258 +++++++++++++++++++ drivers/lguest/io.c | 412 ++++++++++++++++++++++++++++++ drivers/lguest/lg.h | 278 ++++++++++++++++++++ drivers/lguest/lguest_user.c | 192 ++++++++++++++ drivers/lguest/page_tables.c | 407 ++++++++++++++++++++++++++++++ drivers/lguest/segments.c | 115 ++++++++ drivers/lguest/switcher.S | 159 +++++++++++ include/linux/lguest_launcher.h | 80 +++++ 10 files changed, 2517 insertions(+)
===================================================================
drivers/lguest/core.c new file +444 -0
@@ -0,0 +1,444 @@ +/* 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 <linux/cpu.h> +#include <linux/freezer.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 <asm/i387.h> +#include "lg.h" + +/* Found in switcher.S */ +extern char start_switcher_text[], end_switcher_text[], switch_to_guest[]; +extern unsigned long default_idt_entries[]; + +/* Every guest maps the core switcher code. */ +#define SHARED_SWITCHER_PAGES \ + DIV_ROUND_UP(end_switcher_text - start_switcher_text, PAGE_SIZE) +/* Pages for switcher itself, then two pages per cpu */ +#define TOTAL_SWITCHER_PAGES (SHARED_SWITCHER_PAGES + 2 * NR_CPUS) + +/* We map at -4M for ease of mapping into the guest (one PTE page). */ +#define SWITCHER_ADDR 0xFFC00000 + +static struct vm_struct *switcher_vma; +static struct page **switcher_page; + +static int cpu_had_pge; +static struct { +unsigned long offset; +unsigned short segment; +} lguest_entry; + +/* This One Big lock protects all inter-guest data structures. */ +DEFINE_MUTEX (lguest_lock); +static DEFINE_PER_CPU(struct lguest *, last_guest); + +/* FIXME: Make dynamic. */ +#define MAX_LGUEST_GUESTS 16 +struct lguest lguests[MAX_LGUEST_GUESTS ]; + +/* Offset from where switcher.S was compiled to where we've copied it */ +static unsigned long switcher_offset (void ) +{ +return SWITCHER_ADDR - (unsigned long )start_switcher_text; +} + +/* This cpu's struct lguest_pages. */ +static struct lguest_pages *lguest_pages (unsigned int cpu) +{ +return &(((struct lguest_pages *) + (SWITCHER_ADDR +SHARED_SWITCHER_PAGES *PAGE_SIZE ))[cpu]); +} + +static __init int map_switcher (void ) +{ +int i, err; +struct page **pagep; + + switcher_page =kmalloc (sizeof (switcher_page[0 ])*TOTAL_SWITCHER_PAGES , +GFP_KERNEL ); +if (!switcher_page) { + err = -ENOMEM ; + goto out; + } + +for (i =0 ; i <TOTAL_SWITCHER_PAGES ; i++) { +unsigned long addr =get_zeroed_page (GFP_KERNEL ); +if (!addr) { + err = -ENOMEM ; + goto free_some_pages; + } + switcher_page[i] =virt_to_page (addr); + } + + switcher_vma =__get_vm_area (TOTAL_SWITCHER_PAGES *PAGE_SIZE , +VM_ALLOC ,SWITCHER_ADDR ,VMALLOC_END ); +if (!switcher_vma) { + err = -ENOMEM ; +printk ("lguest: could not map switcher pages high\n" ); + goto free_pages; + } + + pagep = switcher_page; + err =map_vm_area (switcher_vma,PAGE_KERNEL , &pagep); +if (err) { +printk ("lguest: map_vm_area failed: %i\n" , err); + goto free_vma; + } +memcpy (switcher_vma->addr, start_switcher_text, + end_switcher_text - start_switcher_text); + +/* Fix up IDT entries to point into copied text. */ +for (i =0 ; i <IDT_ENTRIES ; i++) + default_idt_entries[i] +=switcher_offset (); + +for_each_possible_cpu (i) { +struct lguest_pages *pages =lguest_pages (i); +struct lguest_ro_state *state = &pages->state; + +/* These fields are static: rest done in copy_in_guest_info */ + state->host_gdt_desc.size =GDT_SIZE -1 ; + state->host_gdt_desc.address = (long )get_cpu_gdt_table (i); +store_idt (&state->host_idt_desc); + state->guest_idt_desc.size =sizeof (state->guest_idt)-1 ; + state->guest_idt_desc.address = (long )&state->guest_idt; + state->guest_gdt_desc.size =sizeof (state->guest_gdt)-1 ; + state->guest_gdt_desc.address = (long )&state->guest_gdt; + state->guest_tss.esp0 = (long )(&pages->regs +1 ); + state->guest_tss.ss0 =LGUEST_DS ; +/* No I/O for you! */ + state->guest_tss.io_bitmap_base =sizeof (state->guest_tss); +setup_default_gdt_entries (state); +setup_default_idt_entries (state, default_idt_entries); + +/* Setup LGUEST segments on all cpus */ +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 switcher. */ + lguest_entry.offset = (long )switch_to_guest +switcher_offset (); + lguest_entry.segment =LGUEST_CS ; + +printk (KERN_INFO "lguest: mapped switcher at %p\n" , + switcher_vma->addr); +return 0 ; + +free_vma: +vunmap (switcher_vma->addr); +free_pages: + i =TOTAL_SWITCHER_PAGES ; +free_some_pages: +for (--i; i >=0 ; i--) +__free_pages (switcher_page[i],0 ); +kfree (switcher_page); +out: +return err; +} + +static void unmap_switcher (void ) +{ +unsigned int i; + +vunmap (switcher_vma->addr); +for (i =0 ; i <TOTAL_SWITCHER_PAGES ; i++) +__free_pages (switcher_page[i],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->regs->eip); + +/* This only works for addresses in linear mapping... */ +if (lg->regs->eip < lg->page_offset) +return 0 ; +lgread (lg, &insn, physaddr,1 ); + +/* Operand size prefix means it's actually for ax. */ +if (insn ==0x66 ) { + shift =16 ; + insnlen =1 ; +lgread (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->regs->eax =0xFFFFFFFF ; +else + lg->regs->eax |= (0xFFFF << shift); + } + lg->regs->eip += insnlen; +return 1 ; +} + +int lguest_address_ok (const struct lguest *lg, +unsigned long addr,unsigned long len) +{ +return (addr+len) /PAGE_SIZE < lg->pfn_limit && (addr+len >= addr); +} + +/* Just like get_user, but don't let guest access lguest binary. */ +u32 lgread_u32 (struct lguest *lg,u32 addr) +{ +u32 val =0 ; + +/* Don't let them access lguest binary */ +if (!lguest_address_ok (lg, addr,sizeof (val)) + ||get_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad read address %u" , addr); +return val; +} + +void lgwrite_u32 (struct lguest *lg,u32 addr,u32 val) +{ +if (!lguest_address_ok (lg, addr,sizeof (val)) + ||put_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad write address %u" , addr); +} + +void lgread (struct lguest *lg,void *b,u32 addr,unsigned bytes) +{ +if (!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 lgwrite (struct lguest *lg,u32 addr,const void *b,unsigned bytes) +{ +if (!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); +} + +static void set_ts (void ) +{ +u32 cr0; + + cr0 =read_cr0 (); +if (!(cr0 &8 )) +write_cr0 (cr0|8 ); +} + +static void copy_in_guest_info (struct lguest *lg,struct lguest_pages *pages) +{ +if (__get_cpu_var (last_guest) != lg || lg->last_pages != pages) { +__get_cpu_var (last_guest) = lg; + lg->last_pages = pages; + lg->changed =CHANGED_ALL ; + } + +/* These are pretty cheap, so we do them unconditionally. */ + pages->state.host_cr3 =__pa (current->mm->pgd); +map_switcher_in_guest (lg, pages); + pages->state.guest_tss.esp1 = lg->esp1; + pages->state.guest_tss.ss1 = lg->ss1; + +/* Copy direct trap entries. */ +if (lg->changed &CHANGED_IDT ) +copy_traps (lg, pages->state.guest_idt, default_idt_entries); + +/* Copy all GDT entries but the TSS. */ +if (lg->changed &CHANGED_GDT ) +copy_gdt (lg, pages->state.guest_gdt); + + lg->changed =0 ; +} + +static void run_guest_once (struct lguest *lg,struct lguest_pages *pages) +{ +unsigned int clobber; + +copy_in_guest_info (lg, pages); + +/* Put eflags on stack, lcall does rest: suitable for iret return. */ + asmvolatile ("pushf; lcall *lguest_entry" + :"=a" (clobber),"=b" (clobber) + :"0" (pages),"1" (__pa (lg->pgdirs[lg->pgdidx].pgdir)) + :"memory" ,"%edx" ,"%ecx" ,"%edi" ,"%esi" ); +} + +int run_guest (struct lguest *lg,char *__user user) +{ +while (!lg->dead) { +unsigned int cr2 =0 ;/* Damn gcc */ + +/* Hypercalls first: we might have been out to userspace */ +do_hypercalls (lg); +if (lg->dma_is_pending) { +put_user (lg->pending_dma, (unsigned long *)user); +put_user (lg->pending_key, (unsigned long *)user+1 ); +return sizeof (unsigned long )*2 ; + } + +if (signal_pending (current)) +return -EINTR ; +maybe_do_interrupt (lg); + +try_to_freeze (); + +if (lg->dead) +break ; + +if (lg->halted) { +set_current_state (TASK_INTERRUPTIBLE ); +schedule_timeout (1 ); +continue ; + } + +local_irq_disable (); + +/* Even if *we* don't want FPU trap, guest might... */ +if (lg->ts) +set_ts (); + +run_guest_once (lg,lguest_pages (raw_smp_processor_id ())); + +/* Save cr2 now if we page-faulted. */ +if (lg->regs->trapnum ==14 ) + cr2 =read_cr2 (); +else if (lg->regs->trapnum ==7 ) +math_state_restore (); +local_irq_enable (); + +switch (lg->regs->trapnum) { +case 13 :/* We've intercepted a GPF. */ +if (lg->regs->errcode ==0 ) { +if (emulate_insn (lg)) +continue ; + } +break ; +case 14 :/* We've intercepted a page fault. */ +if (demand_page (lg, cr2, lg->regs->errcode &2 )) +continue ; + +/* If lguest_data is NULL, this won't hurt. */ +put_user (cr2, &lg->lguest_data->cr2); +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 ; +break ; + case 32 ... 255: /* Real interrupt, fall thru */ +cond_resched (); +case LGUEST_TRAP_ENTRY :/* Handled at top of loop */ +continue ; + } + +if (deliver_trap (lg, lg->regs->trapnum)) +continue ; + +kill_guest (lg,"unhandled trap %i at %#x (%#x)" , + lg->regs->trapnum, lg->regs->eip, + lg->regs->trapnum ==14 ? cr2 : lg->regs->errcode); + } +return -ENOENT ; +} + +int find_free_guest (void ) +{ +unsigned int i; +for (i =0 ; i <MAX_LGUEST_GUESTS ; i++) +if (!lguests[i].tsk) +return i; +return -1 ; +} + +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 ()) { +printk ("lguest is afraid of %s\n" , paravirt_ops.name); +return -EPERM ; + } + + err =map_switcher (); +if (err) +return err; + + err =init_pagetables (switcher_page,SHARED_SWITCHER_PAGES ); +if (err) { +unmap_switcher (); +return err; + } +lguest_io_init (); + + err =lguest_device_init (); +if (err) { +free_pagetables (); +unmap_switcher (); +return err; + } +lock_cpu_hotplug (); +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); + } +unlock_cpu_hotplug (); +return 0 ; +} + +static void __exit fini(void) +{ +lguest_device_remove (); +free_pagetables (); +unmap_switcher (); +lock_cpu_hotplug (); +if (cpu_had_pge) { +set_bit (X86_FEATURE_PGE , boot_cpu_data.x86_capability); +on_each_cpu (adjust_pge, (void *)1 ,0 ,1 ); + } +unlock_cpu_hotplug (); +} + +module_init (init); +module_exit (fini); +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Rusty Russell <[email protected]>" );
===================================================================
drivers/lguest/hypercalls.c new file +172 -0
172 lines not on this page · raw message
===================================================================
drivers/lguest/interrupts_and_traps.c new file +258 -0
258 lines not on this page · raw message
===================================================================
drivers/lguest/io.c new file +412 -0
412 lines not on this page · raw message
===================================================================
drivers/lguest/lg.h new file +278 -0
278 lines not on this page · raw message
===================================================================
drivers/lguest/lguest_user.c new file +192 -0
192 lines not on this page · raw message
===================================================================
drivers/lguest/page_tables.c new file +407 -0
407 lines not on this page · raw message
===================================================================
drivers/lguest/segments.c new file +115 -0
115 lines not on this page · raw message
===================================================================
drivers/lguest/switcher.S new file +159 -0
159 lines not on this page · raw message
===================================================================
include/linux/lguest_launcher.h new file +80 -0
80 lines not on this page · raw message
PATCH 3/8 lguest: the asm offsets
This is the structure offsets required by lg.ko's switcher.S.
Unfortunately we don't have infrastructure for private asm-offsets creation.
--- arch/i386/kernel/asm-offsets.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
===================================================================
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 <linux/lguest.h> +#include "../../../drivers/lguest/lg.h" +#endif #define DEFINE (sym, val) \ asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -114,4 +118,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_PAGES_host_gdt_desc, lguest_pages, state.host_gdt_desc); +OFFSET (LGUEST_PAGES_host_idt_desc, lguest_pages, state.host_idt_desc); +OFFSET (LGUEST_PAGES_host_cr3, lguest_pages, state.host_cr3); +OFFSET (LGUEST_PAGES_host_sp, lguest_pages, state.host_sp); +OFFSET (LGUEST_PAGES_guest_gdt_desc, lguest_pages,state.guest_gdt_desc); +OFFSET (LGUEST_PAGES_guest_idt_desc, lguest_pages,state.guest_idt_desc); +OFFSET (LGUEST_PAGES_guest_gdt, lguest_pages, state.guest_gdt); +OFFSET (LGUEST_PAGES_regs_trapnum, lguest_pages, regs.trapnum); +OFFSET (LGUEST_PAGES_regs_errcode, lguest_pages, regs.errcode); +OFFSET (LGUEST_PAGES_regs, lguest_pages, regs); +#endif }
PATCH 4/8 lguest: the Makefile and Kconfig
This is the Kconfig and Makefile to allow lguest to actually be compiled.
--- drivers/Kconfig | 2 ++ drivers/Makefile | 1 + drivers/lguest/Kconfig | 20 ++++++++++++++++++++ drivers/lguest/Makefile | 7 +++++++ 4 files changed, 30 insertions(+)
===================================================================
drivers/Kconfig +2 -0
@@ -87,4 +87,6 @@ source "drivers/kvm/Kconfig" source "drivers/kvm/Kconfig" source "drivers/uio/Kconfig" + +source "drivers/lguest/Kconfig" endmenu
===================================================================
drivers/Makefile +1 -0
@@ -86,3 +86,4 @@ obj-$(CONFIG_HID) += hid/ obj-$(CONFIG_HID) += hid/ obj-$(CONFIG_PPC_PS3) += ps3/ obj-$(CONFIG_SSB) += ssb/ +obj-$(CONFIG_LGUEST_GUEST) += lguest/
===================================================================
drivers/lguest/Kconfig new file +20 -0
@@ -0,0 +1,20 @@ +config LGUEST + tristate "Linux hypervisor example code" + depends on X86 && PARAVIRT && NET && 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.
===================================================================
drivers/lguest/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# Guest requires the paravirt_ops replacement and the bus driver. +obj-$(CONFIG_LGUEST_GUEST) += lguest.o lguest_asm.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 switcher.o
PATCH 5/8 lguest: the console driver
A simple console driver for lguest.
--- drivers/char/Makefile | 1 drivers/char/hvc_lguest.c | 99 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+)
===================================================================
drivers/char/Makefile +1 -0
@@ -44,6 +44,7 @@ obj-$(CONFIG_SX) += sx.o generic_serial obj-$(CONFIG_SX) += sx.o generic_serial.o obj-$(CONFIG_RIO) += rio/ generic_serial.o obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o +obj-$(CONFIG_LGUEST_GUEST) += hvc_lguest.o obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o obj-$(CONFIG_HVC_RTAS) += hvc_rtas.o obj-$(CONFIG_HVC_BEAT) += hvc_beat.o
===================================================================
drivers/char/hvc_lguest.c new file +99 -0
99 lines not on this page · raw message
Re: [PATCH 5/8] lguest: the net driver
A simple net driver for lguest.
--- drivers/net/Makefile | 1 drivers/net/lguest_net.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 356 insertions(+)
===================================================================
drivers/net/Makefile +1 -0
@@ -220,3 +220,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 +355 -0
355 lines not on this page · raw message
PATCH 6/8 lguest: the net driver
In reply to Rusty RussellLguest net driver
A simple net driver for lguest.
--- drivers/net/Makefile | 1 drivers/net/lguest_net.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 356 insertions(+)
===================================================================
drivers/net/Makefile +1 -0
@@ -220,3 +220,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 +355 -0
355 lines not on this page · raw message
PATCH 7/8] lguest: the block driver
In reply to Rusty RussellLguest block driver
A simple block driver for lguest.
--- drivers/block/Makefile | 1 drivers/block/lguest_blk.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 272 insertions(+)
===================================================================
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 +271 -0
271 lines not on this page · raw message
PATCH 6/8 lguest: the net driver
In reply to Rusty RussellLguest net driver
A simple net driver for lguest.
--- drivers/net/Makefile | 1 drivers/net/lguest_net.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 356 insertions(+)
===================================================================
drivers/net/Makefile +1 -0
@@ -220,3 +220,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 +355 -0
355 lines not on this page · raw message
PATCH 7/8] lguest: the block driver
Lguest block driver
A simple block driver for lguest.
--- drivers/block/Makefile | 1 drivers/block/lguest_blk.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 272 insertions(+)
===================================================================
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 +271 -0
271 lines not on this page · raw message
PATCH 8/8 lguest: the documentation, example launcher
A brief document describing how to use lguest. Because lguest doesn't have an ABI we also include an example launcher in the Documentation directory.
--- Documentation/lguest/Makefile | 20 Documentation/lguest/lguest.c | 982 +++++++++++++++++++++++++++++++++++++++ Documentation/lguest/lguest.txt | 125 ++++ 3 files changed, 1127 insertions(+)
===================================================================
Documentation/lguest/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# This creates the demonstration utility "lguest" which runs a Linux guest. + +# We rely on CONFIG_PAGE_OFFSET to know where to put lguest binary. +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.lds 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 +982 -0
982 lines not on this page · raw message
===================================================================
Documentation/lguest/lguest.txt new file +125 -0
125 lines not on this page · raw message
PATCH 8/8 lguest: the documentation, example launcher
In reply to Rusty RussellA brief document describing how to use lguest. Because lguest doesn't have an ABI we also include an example launcher in the Documentation directory.
--- Documentation/lguest/Makefile | 20 Documentation/lguest/lguest.c | 982 +++++++++++++++++++++++++++++++++++++++ Documentation/lguest/lguest.txt | 125 ++++ 3 files changed, 1127 insertions(+)
===================================================================
Documentation/lguest/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# This creates the demonstration utility "lguest" which runs a Linux guest. + +# We rely on CONFIG_PAGE_OFFSET to know where to put lguest binary. +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.lds 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 +982 -0
982 lines not on this page · raw message
===================================================================
Documentation/lguest/lguest.txt new file +125 -0
125 lines not on this page · raw message
Re: PATCH 7/8] lguest: the block driver
In reply to Rusty RussellHi Rusty,
drivers/block/lguest_blk.c line 56
Quoted 9 lines · Rusty Russell
drivers/block/lguest_blk.c new file +9
-0+/* Jens gave me this nice helper to end all chunks of a request. */ +static void end_entire_request (struct request *req,int uptodate) +{ +if (end_that_request_first (req, uptodate, req->hard_nr_sectors)) +BUG (); +add_disk_randomness (req->rq_disk); +blkdev_dequeue_request (req); +end_that_request_last (req, uptodate); +}
Perhaps we should move this to generic code (i.e. block/ll_rw_blk.c)?
Re: PATCH 7/8] lguest: the block driver
In reply to Rusty RussellHi Rusty,
drivers/block/lguest_blk.c line 56
Quoted 9 lines · Rusty Russell
drivers/block/lguest_blk.c new file +9
-0+/* Jens gave me this nice helper to end all chunks of a request. */ +static void end_entire_request (struct request *req,int uptodate) +{ +if (end_that_request_first (req, uptodate, req->hard_nr_sectors)) +BUG (); +add_disk_randomness (req->rq_disk); +blkdev_dequeue_request (req); +end_that_request_last (req, uptodate); +}
Perhaps we should move this to generic code (i.e. block/ll_rw_blk.c)?
Re: PATCH 7/8] lguest: the block driver
Quoted 9 lines · Rusty Russell
Quoted 9 lines
drivers/block/lguest_blk.c new file +9
-0+/* Jens gave me this nice helper to end all chunks of a request. */ +static void end_entire_request (struct request *req,int uptodate) +{ +if (end_that_request_first (req, uptodate, req->hard_nr_sectors)) +BUG (); +add_disk_randomness (req->rq_disk); +blkdev_dequeue_request (req); +end_that_request_last (req, uptodate); +}
Quoted 1 line · Pekka Enberg
Perhaps we should move this to generic code (i.e.
block/ll_rw_blk.c)?
Uhm, I am bit confused now. Why don't you just use end_request() here?
Re: PATCH 7/8] lguest: the block driver
In reply to Pekka EnbergQuoted 9 lines · Rusty Russell
Quoted 9 lines
drivers/block/lguest_blk.c new file +9
-0+/* Jens gave me this nice helper to end all chunks of a request. */ +static void end_entire_request (struct request *req,int uptodate) +{ +if (end_that_request_first (req, uptodate, req->hard_nr_sectors)) +BUG (); +add_disk_randomness (req->rq_disk); +blkdev_dequeue_request (req); +end_that_request_last (req, uptodate); +}
Quoted 1 line · Pekka Enberg
Perhaps we should move this to generic code (i.e.
block/ll_rw_blk.c)?
Uhm, I am bit confused now. Why don't you just use end_request() here?
Re: PATCH 7/8] lguest: the block driver
Quoted 12 lines · Pekka Enberg
Quoted 9 lines · Rusty Russell
drivers/block/lguest_blk.c line 56
Quoted 9 lines
drivers/block/lguest_blk.c new file +9
-0+/* Jens gave me this nice helper to end all chunks of a request. */ +static void end_entire_request (struct request *req,int uptodate) +{ +if (end_that_request_first (req, uptodate, req->hard_nr_sectors)) +BUG (); +add_disk_randomness (req->rq_disk); +blkdev_dequeue_request (req); +end_that_request_last (req, uptodate); +}On 4/10/07, Pekka Enberg <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
Perhaps we should move this to generic code (i.e.
block/ll_rw_blk.c)?
Yeah, Jens said to put it in here and he'd hoist it later.
Quoted 1 line
Uhm, I am bit confused now. Why don't you just use
end_request()here?
What a question! end_request() doesn't end a request! What a crazy idea!
As far as I can tell, every name in the block layer is actually some variant of "fuck off, this is too complicated for you to understand".
Hope that clarifies!
Rusty.
Re: PATCH 7/8] lguest: the block driver
Quoted 2 lines · Rusty Russell
What a question!
end_request()doesn't end a request! What a crazy idea!
Aah, indeed, end_request() uses req->hard_cur_sectors while end_entire_request() uses req->hard_nr_sectors which I missed.
Quoted 1 line · Rusty Russell
Hope that clarifies!
It does. Thanks Rusty :)
Pekka
Re: PATCH 7/8] lguest: the block driver
In reply to Rusty RussellQuoted 2 lines · Rusty Russell
What a question!
end_request()doesn't end a request! What a crazy idea!
Aah, indeed, end_request() uses req->hard_cur_sectors while end_entire_request() uses req->hard_nr_sectors which I missed.
Quoted 1 line · Rusty Russell
Hope that clarifies!
It does. Thanks Rusty :)
Pekka