iPXE
Functions | Variables
hvm.c File Reference

Xen HVM driver. More...

#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/malloc.h>
#include <ipxe/pci.h>
#include <ipxe/cpuid.h>
#include <ipxe/msr.h>
#include <ipxe/xen.h>
#include <ipxe/xenver.h>
#include <ipxe/xenmem.h>
#include <ipxe/xenstore.h>
#include <ipxe/xenbus.h>
#include <ipxe/xengrant.h>
#include "hvm.h"

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int hvm_cpuid_base (struct hvm_device *hvm)
 Get CPUID base.
static int hvm_map_hypercall (struct hvm_device *hvm)
 Map hypercall page(s)
static void hvm_unmap_hypercall (struct hvm_device *hvm)
 Unmap hypercall page(s)
static void * hvm_ioremap (struct hvm_device *hvm, unsigned int space, size_t len)
 Allocate and map MMIO space.
static void hvm_iounmap (struct hvm_device *hvm, void *mmio, size_t len)
 Unmap MMIO space.
static int hvm_map_shared_info (struct hvm_device *hvm)
 Map shared info page.
static void hvm_unmap_shared_info (struct hvm_device *hvm)
 Unmap shared info page.
static int hvm_map_grant (struct hvm_device *hvm)
 Map grant table.
static void hvm_unmap_grant (struct hvm_device *hvm)
 Unmap grant table.
static int hvm_map_xenstore (struct hvm_device *hvm)
 Map XenStore.
static void hvm_unmap_xenstore (struct hvm_device *hvm)
 Unmap XenStore.
static int hvm_probe (struct pci_device *pci)
 Probe PCI device.
static void hvm_remove (struct pci_device *pci)
 Remove PCI device.
 REQUIRING_SYMBOL (hvm_driver)
 REQUIRE_OBJECT (netfront)

Variables

static struct pci_device_id hvm_ids []
 PCI device IDs.
struct pci_driver hvm_driver __pci_driver
 PCI driver.

Detailed Description

Xen HVM driver.

Definition in file hvm.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int hvm_cpuid_base ( struct hvm_device hvm) [static]

Get CPUID base.

Parameters:
hvmHVM device
Return values:
rcReturn status code

Definition at line 53 of file hvm.c.

References __attribute__, base, hvm_device::cpuid_base, DBGC, DBGC2, ebx, ecx, edx, ENODEV, HVM_CPUID_MAGIC, HVM_CPUID_MAX, HVM_CPUID_MIN, HVM_CPUID_STEP, HVM_CPUID_VERSION, memcmp(), signature, and version.

Referenced by hvm_probe().

                                                     {
        struct {
                uint32_t ebx;
                uint32_t ecx;
                uint32_t edx;
        } __attribute__ (( packed )) signature;
        uint32_t base;
        uint32_t version;
        uint32_t discard_eax;
        uint32_t discard_ebx;
        uint32_t discard_ecx;
        uint32_t discard_edx;

        /* Scan for magic signature */
        for ( base = HVM_CPUID_MIN ; base <= HVM_CPUID_MAX ;
              base += HVM_CPUID_STEP ) {
                cpuid ( base, 0, &discard_eax, &signature.ebx, &signature.ecx,
                        &signature.edx );
                if ( memcmp ( &signature, HVM_CPUID_MAGIC,
                              sizeof ( signature ) ) == 0 ) {
                        hvm->cpuid_base = base;
                        cpuid ( ( base + HVM_CPUID_VERSION ), 0, &version,
                                &discard_ebx, &discard_ecx, &discard_edx );
                        DBGC2 ( hvm, "HVM using CPUID base %#08x (v%d.%d)\n",
                                base, ( version >> 16 ), ( version & 0xffff ) );
                        return 0;
                }
        }

        DBGC ( hvm, "HVM could not find hypervisor\n" );
        return -ENODEV;
}
static int hvm_map_hypercall ( struct hvm_device hvm) [static]

Map hypercall page(s)

Parameters:
hvmHVM device
Return values:
rcReturn status code

Definition at line 92 of file hvm.c.

References hvm_device::cpuid_base, DBGC, DBGC2, ENOMEM, EXEN, HVM_CPUID_PAGES, xen_hypervisor::hypercall, hvm_device::hypercall_len, malloc_dma(), PAGE_SIZE, rc, strerror(), version, virt_to_phys(), and hvm_device::xen.

Referenced by hvm_probe().

                                                        {
        uint32_t pages;
        uint32_t msr;
        uint32_t discard_ecx;
        uint32_t discard_edx;
        physaddr_t hypercall_phys;
        uint32_t version;
        static xen_extraversion_t extraversion;
        int xenrc;
        int rc;

        /* Get number of hypercall pages and MSR to use */
        cpuid ( ( hvm->cpuid_base + HVM_CPUID_PAGES ), 0, &pages, &msr,
                &discard_ecx, &discard_edx );

        /* Allocate pages */
        hvm->hypercall_len = ( pages * PAGE_SIZE );
        hvm->xen.hypercall = malloc_dma ( hvm->hypercall_len, PAGE_SIZE );
        if ( ! hvm->xen.hypercall ) {
                DBGC ( hvm, "HVM could not allocate %d hypercall page(s)\n",
                       pages );
                return -ENOMEM;
        }
        hypercall_phys = virt_to_phys ( hvm->xen.hypercall );
        DBGC2 ( hvm, "HVM hypercall page(s) at [%#08lx,%#08lx) via MSR %#08x\n",
                hypercall_phys, ( hypercall_phys + hvm->hypercall_len ), msr );

        /* Write to MSR */
        wrmsr ( msr, hypercall_phys );

        /* Check that hypercall mechanism is working */
        version = xenver_version ( &hvm->xen );
        if ( ( xenrc = xenver_extraversion ( &hvm->xen, &extraversion ) ) != 0){
                rc = -EXEN ( xenrc );
                DBGC ( hvm, "HVM could not get extraversion: %s\n",
                       strerror ( rc ) );
                return rc;
        }
        DBGC2 ( hvm, "HVM found Xen version %d.%d%s\n",
                ( version >> 16 ), ( version & 0xffff ) , extraversion );

        return 0;
}
static void hvm_unmap_hypercall ( struct hvm_device hvm) [static]

Unmap hypercall page(s)

Parameters:
hvmHVM device

Definition at line 141 of file hvm.c.

References free_dma(), xen_hypervisor::hypercall, hvm_device::hypercall_len, and hvm_device::xen.

Referenced by hvm_probe(), and hvm_remove().

                                                           {

        /* Free pages */
        free_dma ( hvm->xen.hypercall, hvm->hypercall_len );
}
static void* hvm_ioremap ( struct hvm_device hvm,
unsigned int  space,
size_t  len 
) [static]

Allocate and map MMIO space.

Parameters:
hvmHVM device
spaceSource mapping space
lenLength (must be a multiple of PAGE_SIZE)
Return values:
mmioMMIO space address, or NULL on error

Definition at line 155 of file hvm.c.

References assert, DBGC, xen_add_to_physmap::domid, DOMID_SELF, EXEN, xen_add_to_physmap::gpfn, xen_add_to_physmap::idx, ioremap(), iounmap(), len, hvm_device::mmio, hvm_device::mmio_len, hvm_device::mmio_offset, NULL, PAGE_SIZE, rc, xen_add_to_physmap::space, strerror(), virt_to_phys(), and hvm_device::xen.

Referenced by hvm_map_grant(), and hvm_map_shared_info().

                                         {
        struct xen_add_to_physmap add;
        struct xen_remove_from_physmap remove;
        unsigned int pages = ( len / PAGE_SIZE );
        physaddr_t mmio_phys;
        unsigned int i;
        void *mmio;
        int xenrc;
        int rc;

        /* Sanity check */
        assert ( ( len % PAGE_SIZE ) == 0 );

        /* Check for available space */
        if ( ( hvm->mmio_offset + len ) > hvm->mmio_len ) {
                DBGC ( hvm, "HVM could not allocate %zd bytes of MMIO space "
                       "(%zd of %zd remaining)\n", len,
                       ( hvm->mmio_len - hvm->mmio_offset ), hvm->mmio_len );
                goto err_no_space;
        }

        /* Map this space */
        mmio = ioremap ( ( hvm->mmio + hvm->mmio_offset ), len );
        if ( ! mmio ) {
                DBGC ( hvm, "HVM could not map MMIO space [%08lx,%08lx)\n",
                       ( hvm->mmio + hvm->mmio_offset ),
                       ( hvm->mmio + hvm->mmio_offset + len ) );
                goto err_ioremap;
        }
        mmio_phys = virt_to_phys ( mmio );

        /* Add to physical address space */
        for ( i = 0 ; i < pages ; i++ ) {
                add.domid = DOMID_SELF;
                add.idx = i;
                add.space = space;
                add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
                if ( ( xenrc = xenmem_add_to_physmap ( &hvm->xen, &add ) ) !=0){
                        rc = -EXEN ( xenrc );
                        DBGC ( hvm, "HVM could not add space %d idx %d at "
                               "[%08lx,%08lx): %s\n", space, i,
                               ( mmio_phys + ( i * PAGE_SIZE ) ),
                               ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
                               strerror ( rc ) );
                        goto err_add_to_physmap;
                }
        }

        /* Update offset */
        hvm->mmio_offset += len;

        return mmio;

        i = pages;
 err_add_to_physmap:
        for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
                remove.domid = DOMID_SELF;
                add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
                xenmem_remove_from_physmap ( &hvm->xen, &remove );
        }
        iounmap ( mmio );
 err_ioremap:
 err_no_space:
        return NULL;
}
static void hvm_iounmap ( struct hvm_device hvm,
void *  mmio,
size_t  len 
) [static]

Unmap MMIO space.

Parameters:
hvmHVM device
mmioMMIO space address
lenLength (must be a multiple of PAGE_SIZE)

Definition at line 229 of file hvm.c.

References DBGC, DOMID_SELF, EXEN, iounmap(), PAGE_SIZE, rc, strerror(), virt_to_phys(), and hvm_device::xen.

Referenced by hvm_map_shared_info(), hvm_unmap_grant(), and hvm_unmap_shared_info().

                                                                           {
        struct xen_remove_from_physmap remove;
        physaddr_t mmio_phys = virt_to_phys ( mmio );
        unsigned int pages = ( len / PAGE_SIZE );
        unsigned int i;
        int xenrc;
        int rc;

        /* Unmap this space */
        iounmap ( mmio );

        /* Remove from physical address space */
        for ( i = 0 ; i < pages ; i++ ) {
                remove.domid = DOMID_SELF;
                remove.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
                if ( ( xenrc = xenmem_remove_from_physmap ( &hvm->xen,
                                                            &remove ) ) != 0 ) {
                        rc = -EXEN ( xenrc );
                        DBGC ( hvm, "HVM could not remove space [%08lx,%08lx): "
                               "%s\n", ( mmio_phys + ( i * PAGE_SIZE ) ),
                               ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
                               strerror ( rc ) );
                        /* Nothing we can do about this */
                }
        }
}
static int hvm_map_shared_info ( struct hvm_device hvm) [static]

Map shared info page.

Parameters:
hvmHVM device
Return values:
rcReturn status code

Definition at line 262 of file hvm.c.

References DBGC2, ENOMEM, hvm_ioremap(), hvm_iounmap(), PAGE_SIZE, rc, readl(), xen_hypervisor::shared, virt_to_phys(), shared_info::wc_sec, hvm_device::xen, and XENMAPSPACE_shared_info.

Referenced by hvm_probe().

                                                          {
        physaddr_t shared_info_phys;
        int rc;

        /* Map shared info page */
        hvm->xen.shared = hvm_ioremap ( hvm, XENMAPSPACE_shared_info,
                                        PAGE_SIZE );
        if ( ! hvm->xen.shared ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        shared_info_phys = virt_to_phys ( hvm->xen.shared );
        DBGC2 ( hvm, "HVM shared info page at [%#08lx,%#08lx)\n",
                shared_info_phys, ( shared_info_phys + PAGE_SIZE ) );

        /* Sanity check */
        DBGC2 ( hvm, "HVM wallclock time is %d\n",
                readl ( &hvm->xen.shared->wc_sec ) );

        return 0;

        hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
 err_alloc:
        return rc;
}
static void hvm_unmap_shared_info ( struct hvm_device hvm) [static]

Unmap shared info page.

Parameters:
hvmHVM device

Definition at line 293 of file hvm.c.

References hvm_iounmap(), PAGE_SIZE, xen_hypervisor::shared, and hvm_device::xen.

Referenced by hvm_probe(), and hvm_remove().

                                                             {

        /* Unmap shared info page */
        hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
}
static int hvm_map_grant ( struct hvm_device hvm) [static]

Map grant table.

Parameters:
hvmHVM device
Return values:
rcReturn status code

Definition at line 305 of file hvm.c.

References DBGC, DBGC2, ENODEV, xen_hypervisor::grant, hvm_ioremap(), xen_grant::len, rc, strerror(), xen_grant::table, virt_to_phys(), hvm_device::xen, xengrant_init(), and XENMAPSPACE_grant_table.

Referenced by hvm_probe().

                                                    {
        physaddr_t grant_phys;
        int rc;

        /* Initialise grant table */
        if ( ( rc = xengrant_init ( &hvm->xen ) ) != 0 ) {
                DBGC ( hvm, "HVM could not initialise grant table: %s\n",
                       strerror ( rc ) );
                return rc;
        }

        /* Map grant table */
        hvm->xen.grant.table = hvm_ioremap ( hvm, XENMAPSPACE_grant_table,
                                             hvm->xen.grant.len );
        if ( ! hvm->xen.grant.table )
                return -ENODEV;

        grant_phys = virt_to_phys ( hvm->xen.grant.table );
        DBGC2 ( hvm, "HVM mapped grant table at [%08lx,%08lx)\n",
                grant_phys, ( grant_phys + hvm->xen.grant.len ) );
        return 0;
}
static void hvm_unmap_grant ( struct hvm_device hvm) [static]

Unmap grant table.

Parameters:
hvmHVM device

Definition at line 333 of file hvm.c.

References xen_hypervisor::grant, hvm_iounmap(), xen_grant::len, xen_grant::table, and hvm_device::xen.

Referenced by hvm_probe(), and hvm_remove().

                                                       {

        /* Unmap grant table */
        hvm_iounmap ( hvm, hvm->xen.grant.table, hvm->xen.grant.len );
}
static int hvm_map_xenstore ( struct hvm_device hvm) [static]

Map XenStore.

Parameters:
hvmHVM device
Return values:
rcReturn status code

Definition at line 345 of file hvm.c.

References DBGC, DBGC2, ENODEV, EXEN, free, HVM_PARAM_STORE_EVTCHN, HVM_PARAM_STORE_PFN, xen_store::intf, ioremap(), name, NULL, PAGE_SIZE, xen_store::port, rc, xen_hypervisor::store, strerror(), hvm_device::xen, xen_hvm_get_param(), and xenstore_read().

Referenced by hvm_probe().

                                                       {
        uint64_t xenstore_evtchn;
        uint64_t xenstore_pfn;
        physaddr_t xenstore_phys;
        char *name;
        int xenrc;
        int rc;

        /* Get XenStore event channel */
        if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_EVTCHN,
                                           &xenstore_evtchn ) ) != 0 ) {
                rc = -EXEN ( xenrc );
                DBGC ( hvm, "HVM could not get XenStore event channel: %s\n",
                       strerror ( rc ) );
                return rc;
        }
        hvm->xen.store.port = xenstore_evtchn;

        /* Get XenStore PFN */
        if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_PFN,
                                           &xenstore_pfn ) ) != 0 ) {
                rc = -EXEN ( xenrc );
                DBGC ( hvm, "HVM could not get XenStore PFN: %s\n",
                       strerror ( rc ) );
                return rc;
        }
        xenstore_phys = ( xenstore_pfn * PAGE_SIZE );

        /* Map XenStore */
        hvm->xen.store.intf = ioremap ( xenstore_phys, PAGE_SIZE );
        if ( ! hvm->xen.store.intf ) {
                DBGC ( hvm, "HVM could not map XenStore at [%08lx,%08lx)\n",
                       xenstore_phys, ( xenstore_phys + PAGE_SIZE ) );
                return -ENODEV;
        }
        DBGC2 ( hvm, "HVM mapped XenStore at [%08lx,%08lx) with event port "
                "%d\n", xenstore_phys, ( xenstore_phys + PAGE_SIZE ),
                hvm->xen.store.port );

        /* Check that XenStore is working */
        if ( ( rc = xenstore_read ( &hvm->xen, &name, "name", NULL ) ) != 0 ) {
                DBGC ( hvm, "HVM could not read domain name: %s\n",
                       strerror ( rc ) );
                return rc;
        }
        DBGC2 ( hvm, "HVM running in domain \"%s\"\n", name );
        free ( name );

        return 0;
}
static void hvm_unmap_xenstore ( struct hvm_device hvm) [static]

Unmap XenStore.

Parameters:
hvmHVM device

Definition at line 401 of file hvm.c.

References xen_store::intf, iounmap(), xen_hypervisor::store, and hvm_device::xen.

Referenced by hvm_probe(), and hvm_remove().

                                                          {

        /* Unmap XenStore */
        iounmap ( hvm->xen.store.intf );
}
static int hvm_probe ( struct pci_device pci) [static]

Probe PCI device.

Parameters:
pciPCI device
Return values:
rcReturn status code

Definition at line 413 of file hvm.c.

References adjust_pci_device(), DBGC, DBGC2, pci_device::dev, ENOMEM, free, hvm_cpuid_base(), hvm_map_grant(), hvm_map_hypercall(), hvm_map_shared_info(), hvm_map_xenstore(), HVM_MMIO_BAR, hvm_unmap_grant(), hvm_unmap_hypercall(), hvm_unmap_shared_info(), hvm_unmap_xenstore(), hvm_device::mmio, hvm_device::mmio_len, pci_bar_size(), pci_bar_start(), pci_set_drvdata(), rc, strerror(), hvm_device::xen, xenbus_probe(), xenbus_remove(), and zalloc().

                                                {
        struct hvm_device *hvm;
        int rc;

        /* Allocate and initialise structure */
        hvm = zalloc ( sizeof ( *hvm ) );
        if ( ! hvm ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        hvm->mmio = pci_bar_start ( pci, HVM_MMIO_BAR );
        hvm->mmio_len = pci_bar_size ( pci, HVM_MMIO_BAR );
        DBGC2 ( hvm, "HVM has MMIO space [%08lx,%08lx)\n",
                hvm->mmio, ( hvm->mmio + hvm->mmio_len ) );

        /* Fix up PCI device */
        adjust_pci_device ( pci );

        /* Attach to hypervisor */
        if ( ( rc = hvm_cpuid_base ( hvm ) ) != 0 )
                goto err_cpuid_base;
        if ( ( rc = hvm_map_hypercall ( hvm ) ) != 0 )
                goto err_map_hypercall;
        if ( ( rc = hvm_map_shared_info ( hvm ) ) != 0 )
                goto err_map_shared_info;
        if ( ( rc = hvm_map_grant ( hvm ) ) != 0 )
                goto err_map_grant;
        if ( ( rc = hvm_map_xenstore ( hvm ) ) != 0 )
                goto err_map_xenstore;

        /* Probe Xen devices */
        if ( ( rc = xenbus_probe ( &hvm->xen, &pci->dev ) ) != 0 ) {
                DBGC ( hvm, "HVM could not probe Xen bus: %s\n",
                       strerror ( rc ) );
                goto err_xenbus_probe;
        }

        pci_set_drvdata ( pci, hvm );
        return 0;

        xenbus_remove ( &hvm->xen, &pci->dev );
 err_xenbus_probe:
        hvm_unmap_xenstore ( hvm );
 err_map_xenstore:
        hvm_unmap_grant ( hvm );
 err_map_grant:
        hvm_unmap_shared_info ( hvm );
 err_map_shared_info:
        hvm_unmap_hypercall ( hvm );
 err_map_hypercall:
 err_cpuid_base:
        free ( hvm );
 err_alloc:
        return rc;
}
static void hvm_remove ( struct pci_device pci) [static]

Remove PCI device.

Parameters:
pciPCI device

Definition at line 474 of file hvm.c.

References pci_device::dev, free, hvm_unmap_grant(), hvm_unmap_hypercall(), hvm_unmap_shared_info(), hvm_unmap_xenstore(), pci_get_drvdata(), hvm_device::xen, and xenbus_remove().

                                                  {
        struct hvm_device *hvm = pci_get_drvdata ( pci );

        xenbus_remove ( &hvm->xen, &pci->dev );
        hvm_unmap_xenstore ( hvm );
        hvm_unmap_grant ( hvm );
        hvm_unmap_shared_info ( hvm );
        hvm_unmap_hypercall ( hvm );
        free ( hvm );
}
REQUIRING_SYMBOL ( hvm_driver  )
REQUIRE_OBJECT ( netfront  )

Variable Documentation

struct pci_device_id hvm_ids[] [static]
Initial value:
 {
        PCI_ROM ( 0x5853, 0x0001, "hvm", "hvm", 0 ),
        PCI_ROM ( 0x5853, 0x0002, "hvm2", "hvm2", 0 ),
}

PCI device IDs.

Definition at line 486 of file hvm.c.

struct pci_driver hvm_driver __pci_driver
Initial value:
 {
        .ids = hvm_ids,
        .id_count = ( sizeof ( hvm_ids ) / sizeof ( hvm_ids[0] ) ),
        .probe = hvm_probe,
        .remove = hvm_remove,
}

PCI driver.

Definition at line 492 of file hvm.c.