lguest host code
PATCH 1/1 lguest host code
These seven patches replace [PATCH 6/10], or in the second send, patches 6a. 6b, 6c and 6d.
There are more patches because Andi hassled me about extern decls, so I had to expose some things. Tiny patches, but worth separating because you never know when such changes will break something. Compiles and runs here...
Thanks,
Rusty.
PATCH 1/1 lguest: Move mce_disabled to asm/mce.h so lguest can use it.
===================================================================
arch/i386/kernel/cpu/mcheck/mce.h +1 -1
@@ -1,4 +1,5 @@#include <linux/init.h> +#include <asm/mce.h> void amd_mcheck_init (struct cpuinfo_x86 *c);void intel_p4_mcheck_init (struct cpuinfo_x86 *c);
@@ -9,6 +10,5 @@ void winchip_mcheck_init(struct cpuinfo_/* Call the installed machine check handler for this CPU setup. */ extern fastcall void (*machine_check_vector)(struct pt_regs *, long error_code); -extern int mce_disabled;extern int nr_mce_banks;
===================================================================
include/asm-i386/mce.h +2 -0
@@ -3,3 +3,5 @@ extern void mcheck_init(struct cpuinfo_x#else #define mcheck_init (c) do {} while(0)#endif + +extern int mce_disabled;
PATCH 1/1 lguest: Rename cpu_gdt_descr and remove extern declaration from smpboot.c
When I implemented the DECLARE_PER_CPU(var) macros, I was careful that people couldn't use "var" in a non-percpu context, by prepending percpu__. I never considered that this would allow them to overload the same name for a per-cpu and a non-percpu variable.
It is only one of many horrors in the i386 boot code, but let's rename the non-perpcu cpu_gdt_descr to early_gdt_descr (not boot_gdt_descr, that's something else...)
===================================================================
arch/i386/kernel/head.S +3 -3
@@ -309,7 +309,7 @@ 2: movl %cr0,%eax call check_x87 call setup_pda - lgdt cpu_gdt_descr + lgdt early_gdt_descr lidt idt_descr ljmp $(__KERNEL_CS),$1f 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
@@ -365,7 +365,7 @@ setup_pda: movl start_pda, %eax /* slot the PDA address into the GDT */ - mov cpu_gdt_descr+2, %ecx + mov early_gdt_descr+2, %ecx mov %ax, (__KERNEL_PDA+0+2)(%ecx) /* base & 0x0000ffff */ shr $16, %eax mov %al, (__KERNEL_PDA+4+0)(%ecx) /* base & 0x00ff0000 */
@@ -588,7 +588,7 @@ idt_descr: # boot GDT descriptor (later on used by CPU#0): .word 0 # 32 bit align gdt_desc.address -ENTRY(cpu_gdt_descr) +ENTRY(early_gdt_descr) .word GDT_ENTRIES*8-1 .long cpu_gdt_table
===================================================================
arch/i386/kernel/smpboot.c +0 -1
@@ -619,7 +619,6 @@ extern struct {unsigned short ss; } stack_start;extern struct i386_pda *start_pda; -extern struct Xgt_desc_struct cpu_gdt_descr;#ifdef CONFIG_NUMA
===================================================================
include/asm-i386/desc.h +1 -1
@@ -22,7 +22,7 @@ struct Xgt_desc_struct {extern struct Xgt_desc_struct idt_descr; DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr); - +extern struct Xgt_desc_struct early_gdt_descr;static inline struct desc_struct *get_cpu_gdt_table (unsigned int cpu) {
PATCH 1/1 lguest: Remove extern declaration from mm/discontig.c, put in header.
lguest wants it too, but Andi won't let me put an extern decl in. Times, they are a'changin.
===================================================================
arch/i386/mm/discontig.c +0 -1
@@ -101,7 +101,6 @@ extern void add_one_highpage_init(structextern void add_one_highpage_init (struct page *,int ,int );extern struct e820map e820; -extern unsigned long init_pg_tables_end;extern unsigned long highend_pfn, highstart_pfn;extern unsigned long max_low_pfn;extern unsigned long totalram_pages;
===================================================================
include/asm-i386/setup.h +2 -0
@@ -77,6 +77,8 @@ void __init add_memory_region(unsigned lvoid __init add_memory_region (unsigned long long start,unsigned long long size,int type); +extern unsigned long init_pg_tables_end; +#endif /* __ASSEMBLY__ */ #endif /* __KERNEL__ */
PATCH 1/1 lguest: Config and headers
[ This is the previous 6a, with the following Andi-inspired changes:
1) use HYPERVISOR_MAP_ORDER instead of HYPERVISOR_SIZE for clarity
2) mutex instead of a semaphore ]
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 }
===================================================================
arch/i386/lguest/lg.h new file +253 -0
253 lines not on this page · raw message
===================================================================
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
PATCH 1/1 lguest: the host code (lg.ko).
[ This is patch 6b, with the following Andifications:
1) lguest_entry is now mark used: after the other changes, gcc optimized it out.
2) Use header to define math_state_restore, not manual extern.
3) try_to_freeze() inside run loop.
4) cpu_hotplug locks on init and exit code as we frob PGE on all cpus.
5) Remove the log() macro which so amused lkml.
6) switch in IDT interpreting code instead of if chaining. ]
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 +432 -0
@@ -0,0 +1,432 @@ +/* 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/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 <asm/i387.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 \ + (((1 << HYPERVISOR_MAP_ORDER) - 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 __attribute_used__; +struct page *hype_pages;/* Contiguous pages. */ +struct lguest lguests[MAX_LGUEST_GUESTS ]; +DEFINE_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,HYPERVISOR_MAP_ORDER ); +if (!hype_pages) +return -ENOMEM ; + + hypervisor_vma =__get_vm_area (1 <<HYPERVISOR_MAP_ORDER ,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,HYPERVISOR_MAP_ORDER ); +return err; +} + +static __exit void unmap_hypervisor(void) +{ +vunmap (hypervisor_vma->addr); +__free_pages (hype_pages,HYPERVISOR_MAP_ORDER ); +} + +/* 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; +} + +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); + +try_to_freeze (); + +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; + } +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_hypervisor (); +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]>" );
===================================================================
arch/i386/lguest/hypercalls.c new file +189 -0
189 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 +230 -0
230 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: Guest code
[ Changes since 6c:
1) No more extern declarations!
2) Use native_cpuid directly rather than open-coding it.
3) Add declaration of boot_pda to asm/pda.h ]
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, and one new extern declaration for boot_pda, previously frobbed only from head.S.
===================================================================
arch/i386/lguest/lguest.c new file +582 -0
@@ -0,0 +1,582 @@ +/* +* 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> +#include <asm/mce.h> + +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 ); + +native_cpuid (eax, ebx, ecx, edx); +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) +{ +} + +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) +{ + 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 (&early_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
===================================================================
include/asm-i386/pda.h +1 -0
@@ -97,4 +97,5 @@ extern struct i386_pda _proxy_pda;#define sub_pda (field,val) pda_to_op("sub",field,val)#define or_pda (field,val) pda_to_op("or",field,val) +extern struct i386_pda boot_pda;#endif /* _I386_PDA_H */
PATCH 1/1 lguest: Makefile
[ This has no changes since 6d/10. None! Andi tried to make me change it, but I defended it. This one patch is unscathed. Bwaha! ]
Finally, we put in the Makefile, so it will build.
There's a small complexity in creating the switcher code (hypervisor.S) ready to be copied into the top of memory, but it's not too bad. Really. Stop looking at me like that, Andi. It's fine as it is.
===================================================================
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 4 of 7] lguest: Config and headers
In reply to Rusty Russellarch/i386/lguest/lg.h line 7
Quoted 3 lines · Rusty Russell
arch/i386/lguest/lg.h new file +3
-0+/* 64k ought to be enough for anybody! */ +#define HYPERVISOR_MAP_ORDER 16 +#define HYPERVISOR_PAGES ((1 << HYPERVISOR_MAP_ORDER)/PAGE_SIZE)
I think it'd be better to go back to defining HYPERVISOR_SIZE then derive the map order from that via get_order(), as it should be 4 instead of 16; and this code is now both implying PAGE_SIZE while also using it for calculations.
- James
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH 4 of 7] lguest: Config and headers
Quoted 10 lines · James Morris
arch/i386/lguest/lg.h line 7
Quoted 3 lines · Rusty Russell
arch/i386/lguest/lg.h new file +3
-0+/* 64k ought to be enough for anybody! */ +#define HYPERVISOR_MAP_ORDER 16 +#define HYPERVISOR_PAGES ((1 << HYPERVISOR_MAP_ORDER)/PAGE_SIZE)I think it'd be better to go back to defining
HYPERVISOR_SIZEthen derive the map order from that viaget_order(), as it should be 4 instead of 16; and this code is now both implyingPAGE_SIZEwhile also using it for calculations.
Well it was the use of get_order() which triggered Andi's alarm bells, so I went back to deriving it. This code is correct, however.
get_order() is one of those classic functions only a kernel coder could love. Look how lovingly it has been optimized:
#define get_order(n) \
( \ __builtin_constant_p(n) ? \ ((n < (1UL << PAGE_SHIFT)) ? 0 : ilog2(n) - PAGE_SHIFT) : \ __get_order(n, PAGE_SHIFT) \
)
All that time spent, yet no consideration that it should be called "get_page_order()" or some name which hints that the divide by page size is happening. It's even documented in the comment above, so someone thought it needed explaining. Too bad they chose to explain it instead of actually clarifying it. 8(
Cheers,
Rusty.
Re: [PATCH 4 of 7] lguest: Config and headers
Quoted 2 lines · Rusty Russell
Well it was the use of
get_order()which triggered Andi's alarm bells, so I went back to deriving it. This code is correct, however.
+ hype_pages = alloc_pages(GFP_KERNEL|__GFP_ZERO, HYPERVISOR_MAP_ORDER);
+ if (!hype_pages)
+ return -ENOMEM;
This will try and allocate 2^16 pages. I guess we need a HYPERVISOR_PAGE_ORDER ?
- James
Signature · 2 lines
James Morris <[email protected]>
Re: [PATCH 4 of 7] lguest: Config and headers
Quoted 11 lines · James Morris
Quoted 2 lines · Rusty Russell
Well it was the use of
get_order()which triggered Andi's alarm bells, so I went back to deriving it. This code is correct, however.(quoted diff) +3
-0+ hype_pages = alloc_pages(GFP_KERNEL|__GFP_ZERO, HYPERVISOR_MAP_ORDER); + if (!hype_pages) + return -ENOMEM;This will try and allocate 2^16 pages. I guess we need a
HYPERVISOR_PAGE_ORDER?
Fuck a brick, you're right. Worked here tho 8) There's a moral here somewhere about futzing with perfectly working code after midnight, I'm sure.
Name: Don't allocate 2^16 pages in lg.ko
Code cleanup which avoided use of get_order() resulted in a thinko: we allocated 65536 pages instead of 65536 bytes. Thanks to James Morris for pointing this out (twice!).
arch/i386/lguest/core.c +7 -7
@@ -24,8 +24,8 @@ static char __initdata hypervisor_blob[]#include "hypervisor-blob.c" }; -#define MAX_LGUEST_GUESTS \ - (((1 << HYPERVISOR_MAP_ORDER) - sizeof(hypervisor_blob)) \ +#define MAX_LGUEST_GUESTS \ + (((PAGE_SIZE << HYPERVISOR_PAGE_ORDER) - sizeof(hypervisor_blob)) \ / sizeof(struct lguest_state))static struct vm_struct *hypervisor_vma;
@@ -62,12 +62,12 @@ static __init int map_hypervisor(void)int err;struct page *pages[HYPERVISOR_PAGES ], **pagep = pages; - hype_pages =alloc_pages (GFP_KERNEL |__GFP_ZERO,HYPERVISOR_MAP_ORDER ); + hype_pages =alloc_pages (GFP_KERNEL |__GFP_ZERO,HYPERVISOR_PAGE_ORDER );if (!hype_pages)return -ENOMEM ; - hypervisor_vma =__get_vm_area (1 <<HYPERVISOR_MAP_ORDER ,VM_ALLOC , -HYPE_ADDR ,VMALLOC_END ); + hypervisor_vma =__get_vm_area (PAGE_SIZE <<HYPERVISOR_PAGE_ORDER , +VM_ALLOC ,HYPE_ADDR ,VMALLOC_END );if (!hypervisor_vma) { err = -ENOMEM ;printk ("lguest: could not map hypervisor pages high\n" );
@@ -100,14 +100,14 @@ free_vma: free_vma:vunmap (hypervisor_vma->addr); free_pages: -__free_pages (hype_pages,HYPERVISOR_MAP_ORDER ); +__free_pages (hype_pages,HYPERVISOR_PAGE_ORDER );return err; } static __exit void unmap_hypervisor(void) {vunmap (hypervisor_vma->addr); -__free_pages (hype_pages,HYPERVISOR_MAP_ORDER ); +__free_pages (hype_pages,HYPERVISOR_PAGE_ORDER ); }/* IN/OUT insns: enough to get us past boot-time probing. */
arch/i386/lguest/lg.h +2 -2
@@ -3,8 +3,8 @@#include <asm/desc.h> /* 64k ought to be enough for anybody! */ -#define HYPERVISOR_MAP_ORDER 16 -#define HYPERVISOR_PAGES ((1 << HYPERVISOR_MAP_ORDER)/PAGE_SIZE) +#define HYPERVISOR_PAGE_ORDER (16 - PAGE_SHIFT) +#define HYPERVISOR_PAGES (1 << HYPERVISOR_PAGE_ORDER)#define GDT_ENTRY_LGUEST_CS 10#define GDT_ENTRY_LGUEST_DS 11