Patchset to protect guest ROM areas from R/W access.
PATCH 1/1 Patchset to protect guest ROM areas from R/W access.
This is a set of patches to protect guest ROM areas by making them actually be read-only.
xen_handle_p2m_type_ro.patch:
This makes writes to pages with p2m_ram_ro stop falling through to the
device model, and instead writes get logged but discarded.
xen_hvmop_set_mem_type.patch:
This adds a new command, "set_mem_type" to the hvmop hypercall which
allows marking ram page ranges as ro, rw, or mmio_dm.
ioemu_xen_platform.patch
This adds functionality to the xen platform device to enable a guest to
set/unset the RO state of ROM. A guest can enable
or disable RW access to the ROM range ( 0xc0000-0xfffff ) by writing to
the lowermost io port of the xen platform device. This
port now provides access to a flags register, which currently only has
one flag bit, namely bit 0, to set/clear write access to the ROM range.
This patch should apply to both the old ioemu tree or the new
upstream-merged git tree.
xen_firmware_make_ROM_ro.patch
This adds changes to hvmloader and rombios to make use of the aboveplatform_device functionality to write-protect ROM areas. This is somewhat ugly, but unfortunately the virtual bios has grown to rely on being able to self-modify. At a later stage, I'm going to try to remove any self-modifying bits on the rombios and move any such post-load modifications into hvmloader, so that hvmloader can handle the ROM locking, which would be cleaner.
xen/arch/x86/hvm/hvm.c +13 -0
@@ -1492,6 +1492,18 @@return HVMCOPY_bad_gfn_to_mfn;ASSERT (mfn_valid (mfn)); +if (unlikely ((flags & HVMCOPY_to_guest) && (p2mt == p2m_ram_ro)) ) + { +static unsigned long lastpage =0 ; +if ( gfn != lastpage ) + { + lastpage = gfn; +gdprintk (XENLOG_DEBUG ,"guest attempted write to read-only memory page. " +"page=%#lx, mfn=%#lx\n" , (lastpage <<PAGE_SHIFT ), mfn); + } + goto nocopy; + } + p = (char *)map_domain_page (mfn) + (addr & ~PAGE_MASK );if ( flags & HVMCOPY_to_guest )
@@ -1506,6 +1518,7 @@unmap_domain_page (p); +nocopy: addr += count; buf += count; todo -= count;
xen/arch/x86/mm/shadow/multi.c +20 -4
@@ -3042,6 +3042,8 @@int fast_emul =0 ;#endif + emul_ctxt.no_write =0 ; +SHADOW_PRINTK ("d:v=%u:%u va=%#lx err=%u, rip=%lx\n" , v->domain->domain_id, v->vcpu_id, va, regs->error_code, regs->rip);
@@ -3344,14 +3346,25 @@ } } -/* Need to hand off device-model MMIO and writes to read-only -* memory to the device model */ -if ( p2mt == p2m_mmio_dm - || (p2mt == p2m_ram_ro && ft == ft_demand_write) ) +/* Need to hand off device-model MMIO to the device model */ +if ( p2mt == p2m_mmio_dm ) { gpa =guest_walk_to_gpa (&gw); goto mmio; } +/* Log attempts to write to read-only memory */ +else if (p2mt == p2m_ram_ro && ft == ft_demand_write) + { +static unsigned long lastpage =0 ; +if ( (va &PAGE_MASK ) != lastpage ) + { + lastpage = (va &PAGE_MASK ); +gdprintk (XENLOG_DEBUG ,"guest attempted write to read-only memory page. " +"va page=%#lx, mfn=%#lx\n" , ( va &PAGE_MASK ),mfn_x (gmfn)); + } + emul_ctxt.no_write =1 ; + goto emulate; + }/* In HVM guests, we force CR0.WP always to be set, so that the * pagetables are always write-protected. If the guest thinks
@@ -4776,6 +4789,9 @@ {void *addr; +if ( sh_ctxt->no_write ) +return X86EMUL_OKAY ; +/* Unaligned writes are only acceptable on HVM */ if ( (vaddr & (bytes -1 )) && !is_hvm_vcpu (v) )return X86EMUL_UNHANDLEABLE ;
xen/arch/x86/mm/shadow/private.h +3 -0
@@ -751,6 +751,9 @@/* MFNs being written to in write/cmpxchg callbacks */ mfn_t mfn1, mfn2; +/* Discard writes */ +unsigned int no_write; +#if (SHADOW_OPTIMIZATIONS &SHOPT_SKIP_VERIFY )/* Special case for avoiding having to verify writes: remember * whether the old value had its low bit (_PAGE_PRESENT) clear. */
tools/libxc/xc_misc.c +30 -0
@@ -295,6 +295,36 @@return rc; } +int xc_hvm_set_mem_type ( +int xc_handle,domid_t dom,hvmmem_type_t mem_type,uint64_t first_pfn,uint64_t nr) +{ +DECLARE_HYPERCALL ; +struct xen_hvm_set_mem_type arg; +int rc; + + hypercall.op = __HYPERVISOR_hvm_op; + hypercall.arg[0 ] = HVMOP_set_mem_type; + hypercall.arg[1 ] = (unsigned long )&arg; + + arg.domid = dom; + arg.hvmmem_type = mem_type; + arg.first_pfn = first_pfn; + arg.nr = nr; + +if ( (rc =lock_pages (&arg,sizeof (arg))) !=0 ) + { +PERROR ("Could not lock memory" ); +return rc; + } + + rc =do_xen_hypercall (xc_handle, &hypercall); + +unlock_pages (&arg,sizeof (arg)); + +return rc; +} + +void *xc_map_foreign_pages (int xc_handle,uint32_t dom,int prot,const xen_pfn_t *arr,int num) {
tools/libxc/xenctrl.h +9 -0
@@ -27,6 +27,7 @@#include <xen/event_channel.h> #include <xen/sched.h> #include <xen/memory.h> +#include <xen/hvm/params.h> #include <xen/xsm/acm.h> #include <xen/xsm/acm_ops.h> #include <xen/xsm/flask_op.h>
@@ -942,6 +943,14 @@int xc_hvm_modified_memory (int xc_handle,domid_t dom,uint64_t first_pfn,uint64_t nr); +/* +* Set a range of memory to a specific type. +* Allowed types are HVMMEM_ram_rw, HVMMEM_ram_ro, HVMMEM_mmio_dm +*/ +int xc_hvm_set_mem_type ( +int xc_handle,domid_t dom,hvmmem_type_t memtype,uint64_t first_pfn,uint64_t nr); + + typedef enum { XC_ERROR_NONE = 0, XC_INTERNAL_ERROR = 1,
xen/arch/x86/hvm/hvm.c +59 -0
65 lines not on this page · raw message
xen/include/public/hvm/hvm_op.h +21 -0
@@ -105,6 +105,27 @@typedef struct xen_hvm_modified_memory xen_hvm_modified_memory_t ;DEFINE_XEN_GUEST_HANDLE (xen_hvm_modified_memory_t); +#define HVMOP_set_mem_type 8 +typedef enum { + HVMMEM_ram_rw,/* Normal read/write guest RAM */ + HVMMEM_ram_ro,/* Read-only; writes go to the device model */ + HVMMEM_mmio_dm,/* Reads and write go to the device model */ +}hvmmem_type_t ; +/* Notify that a section of ram is to be treated as ROM. */ +struct xen_hvm_set_mem_type { +/* Domain to be updated. */ +domid_t domid; +/* Memory type */ +hvmmem_type_t hvmmem_type; +/* First pfn. */ +uint64_aligned_t first_pfn; +/* Number of pages. */ +uint64_aligned_t nr; +}; +typedef struct xen_hvm_set_mem_type xen_hvm_set_mem_type_t ; +DEFINE_XEN_GUEST_HANDLE (xen_hvm_set_mem_type_t); + +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ #endif /* __XEN_PUBLIC_HVM_HVM_OP_H__ */
hw/xen_platform.c +77 -13
130 lines not on this page · raw message
tools/firmware/hvmloader/config.h +3 -0
@@ -31,4 +31,7 @@#define ROMBIOS_PHYSICAL_ADDRESS 0x000F0000#define SCRATCH_PHYSICAL_ADDRESS 0x00010000 +/* Xen Platform Device */ +#define PFFLAG_ROM_LOCK 1/* Sets whether ROM memory area is RW or RO */ +#endif /* __HVMLOADER_CONFIG_H__ */
tools/firmware/hvmloader/hvmloader.c +42 -0
82 lines not on this page · raw message
tools/firmware/rombios/rombios.c +29 -0
@@ -275,6 +275,26 @@ mov ?1*4+2, ax MEND +MACRO ROM_WRITE_ENABLE + push ds + mov ax,cs + mov ds,ax + mov ax,#0 + mov dx,platform_device_base_ioport + out dx,al + pop ds +MEND + +MACRO ROM_WRITE_DISABLE + push ds + mov ax,cs + mov ds,ax + mov ax,#1 + mov dx,platform_device_base_ioport + out dx,al + pop ds +MEND + ASM_END typedef unsigned char Bit8u;
@@ -10155,6 +10176,10 @@ call tcpa_post_part2#endif +#ifdef HVMASSIST +ROM_WRITE_DISABLE +#endif + ;; Start the boot sequence. See the comments in int19_relocated ;; for why we use INT 18h instead of INT 19h here. int #0x18
@@ -10955,6 +10980,11 @@ dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;; 896 bytes .align 16 +db 0x5F, 0x5F, 0x5F, 0x48, 0x56, 0x4D, 0x50, 0x44, 0x49, 0x4F ;; ___HVMPDIO +platform_device_base_ioport: +dw 0xDEAD; + +.align 16 smbios_entry_point: db 0,0,0,0,0,0,0,0 ; 8 bytes db 0,0,0,0,0,0,0,0 ; 16 bytes
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
New approach to the firmware part, as per Keir's suggestions.
hvmloader/acpi/build.c +2 -6
@@ -70,12 +70,7 @@static int construct_bios_info_table (uint8_t *buf) { -struct bios_info { -uint8_t com1_present:1 ; -uint8_t com2_present:1 ; -uint8_t hpet_present:1 ; -uint32_t pci_min, pci_len; - } *bios_info = (struct bios_info *)buf; +struct bios_info *bios_info = (struct bios_info *)buf;memset (bios_info,0 ,sizeof (*bios_info));
@@ -86,6 +81,7 @@ bios_info->pci_min =PCI_MEMBASE ; bios_info->pci_len =PCI_MEMSIZE ; + bios_info->xen_pfiob =0xdead ;return align16 (sizeof (*bios_info)); }
hvmloader/config.h +3 -0
@@ -31,4 +31,7 @@#define ROMBIOS_PHYSICAL_ADDRESS 0x000F0000#define SCRATCH_PHYSICAL_ADDRESS 0x00010000 +/* Xen Platform Device */ +#define PFFLAG_ROM_LOCK 1/* Sets whether ROM memory area is RW or RO */ +#endif /* __HVMLOADER_CONFIG_H__ */
hvmloader/hvmloader.c +14 -0
@@ -105,6 +105,8 @@static enum { VGA_none, VGA_std, VGA_cirrus } virtual_vga = VGA_none; +static uint16_t xen_platform_io_base; +static void init_hypercalls (void ) {
@@ -149,6 +151,7 @@uint32_t base, devfn, bar_reg, bar_data, bar_sz, cmd;uint16_t class, vendor_id, device_id;unsigned int bar, pin, link, isa_irq; +uint32_t xen_pf_devfn =0xff ;/* Resources assignable to PCI devices via BARs. */ struct resource {
@@ -213,6 +216,10 @@pci_writew (devfn,0x42 ,0x8000 );/* enable IDE1 */ }break ; +case 0xff80 : +if ( (vendor_id ==0x5853 ) && (device_id ==0x0001 ) ) + xen_pf_devfn = devfn; +break ; }/* Map the I/O memory and port resources. */
@@ -284,6 +291,10 @@ } base = (resource->base + bar_sz -1 ) & ~(bar_sz -1 ); + +if ( devfn == xen_pf_devfn && resource == &io_resource ) + xen_platform_io_base = (uint16_t ) base; + bar_data |= base; base += bar_sz;
@@ -527,6 +538,9 @@ROMBIOS_PHYSICAL_ADDRESS , ROMBIOS_PHYSICAL_ADDRESS + rombios_sz - 1); +/* Tell rombios the platform device base io port so it can write-protect ROM once its done self-modifying */ + ((struct bios_info *)ACPI_PHYSICAL_ADDRESS )->xen_pfiob = xen_platform_io_base; +printf ("Invoking ROMBIOS ...\n" );return 0 ; }
hvmloader/util.h +8 -0
@@ -137,6 +137,14 @@/* Prepare the 32bit BIOS */ void highbios_setup (void ); +struct bios_info { +uint8_t com1_present:1 ; +uint8_t com2_present:1 ; +uint8_t hpet_present:1 ; +uint32_t pci_min, pci_len; +uint16_t xen_pfiob; +}; +#define isdigit (c) ((c) >= '0' && (c) <= '9')#endif /* __HVMLOADER_UTIL_H__ */
rombios/rombios.c +29 -0
@@ -275,6 +275,31 @@ mov ?1*4+2, ax MEND +/* bios_info is the first structure at ACPI_PHYSICAL_ADDRESS(0xEA000) +and the offset of .xen_pfiob is 0x0C */ + +#define ACPI_PFIOB_OFFSET 0xA00C + +MACRO ROM_WRITE_ENABLE + push ds + mov ax,#((ACPI_PHYSICAL_ADDRESS >> 4) & $F000) + mov ds,ax + mov ax,#0 + mov dx,ACPI_PFIOB_OFFSET + out dx,al + pop ds +MEND + +MACRO ROM_WRITE_DISABLE + push ds + mov ax,#((ACPI_PHYSICAL_ADDRESS >> 4) & $F000) + mov ds,ax + mov ax,#1 + mov dx,ACPI_PFIOB_OFFSET + out dx,al + pop ds +MEND + ASM_END typedef unsigned char Bit8u;
@@ -10155,6 +10180,10 @@ call tcpa_post_part2#endif +#ifdef HVMASSIST +ROM_WRITE_DISABLE +#endif + ;; Start the boot sequence. See the comments in int19_relocated ;; for why we use INT 18h instead of INT 19h here. int #0x18
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel