iPXE
hvm.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <ipxe/malloc.h>
00030 #include <ipxe/pci.h>
00031 #include <ipxe/cpuid.h>
00032 #include <ipxe/msr.h>
00033 #include <ipxe/xen.h>
00034 #include <ipxe/xenver.h>
00035 #include <ipxe/xenmem.h>
00036 #include <ipxe/xenstore.h>
00037 #include <ipxe/xenbus.h>
00038 #include <ipxe/xengrant.h>
00039 #include "hvm.h"
00040 
00041 /** @file
00042  *
00043  * Xen HVM driver
00044  *
00045  */
00046 
00047 /**
00048  * Get CPUID base
00049  *
00050  * @v hvm               HVM device
00051  * @ret rc              Return status code
00052  */
00053 static int hvm_cpuid_base ( struct hvm_device *hvm ) {
00054         struct {
00055                 uint32_t ebx;
00056                 uint32_t ecx;
00057                 uint32_t edx;
00058         } __attribute__ (( packed )) signature;
00059         uint32_t base;
00060         uint32_t version;
00061         uint32_t discard_eax;
00062         uint32_t discard_ebx;
00063         uint32_t discard_ecx;
00064         uint32_t discard_edx;
00065 
00066         /* Scan for magic signature */
00067         for ( base = HVM_CPUID_MIN ; base <= HVM_CPUID_MAX ;
00068               base += HVM_CPUID_STEP ) {
00069                 cpuid ( base, 0, &discard_eax, &signature.ebx, &signature.ecx,
00070                         &signature.edx );
00071                 if ( memcmp ( &signature, HVM_CPUID_MAGIC,
00072                               sizeof ( signature ) ) == 0 ) {
00073                         hvm->cpuid_base = base;
00074                         cpuid ( ( base + HVM_CPUID_VERSION ), 0, &version,
00075                                 &discard_ebx, &discard_ecx, &discard_edx );
00076                         DBGC2 ( hvm, "HVM using CPUID base %#08x (v%d.%d)\n",
00077                                 base, ( version >> 16 ), ( version & 0xffff ) );
00078                         return 0;
00079                 }
00080         }
00081 
00082         DBGC ( hvm, "HVM could not find hypervisor\n" );
00083         return -ENODEV;
00084 }
00085 
00086 /**
00087  * Map hypercall page(s)
00088  *
00089  * @v hvm               HVM device
00090  * @ret rc              Return status code
00091  */
00092 static int hvm_map_hypercall ( struct hvm_device *hvm ) {
00093         uint32_t pages;
00094         uint32_t msr;
00095         uint32_t discard_ecx;
00096         uint32_t discard_edx;
00097         physaddr_t hypercall_phys;
00098         uint32_t version;
00099         static xen_extraversion_t extraversion;
00100         int xenrc;
00101         int rc;
00102 
00103         /* Get number of hypercall pages and MSR to use */
00104         cpuid ( ( hvm->cpuid_base + HVM_CPUID_PAGES ), 0, &pages, &msr,
00105                 &discard_ecx, &discard_edx );
00106 
00107         /* Allocate pages */
00108         hvm->hypercall_len = ( pages * PAGE_SIZE );
00109         hvm->xen.hypercall = malloc_dma ( hvm->hypercall_len, PAGE_SIZE );
00110         if ( ! hvm->xen.hypercall ) {
00111                 DBGC ( hvm, "HVM could not allocate %d hypercall page(s)\n",
00112                        pages );
00113                 return -ENOMEM;
00114         }
00115         hypercall_phys = virt_to_phys ( hvm->xen.hypercall );
00116         DBGC2 ( hvm, "HVM hypercall page(s) at [%#08lx,%#08lx) via MSR %#08x\n",
00117                 hypercall_phys, ( hypercall_phys + hvm->hypercall_len ), msr );
00118 
00119         /* Write to MSR */
00120         wrmsr ( msr, hypercall_phys );
00121 
00122         /* Check that hypercall mechanism is working */
00123         version = xenver_version ( &hvm->xen );
00124         if ( ( xenrc = xenver_extraversion ( &hvm->xen, &extraversion ) ) != 0){
00125                 rc = -EXEN ( xenrc );
00126                 DBGC ( hvm, "HVM could not get extraversion: %s\n",
00127                        strerror ( rc ) );
00128                 return rc;
00129         }
00130         DBGC2 ( hvm, "HVM found Xen version %d.%d%s\n",
00131                 ( version >> 16 ), ( version & 0xffff ) , extraversion );
00132 
00133         return 0;
00134 }
00135 
00136 /**
00137  * Unmap hypercall page(s)
00138  *
00139  * @v hvm               HVM device
00140  */
00141 static void hvm_unmap_hypercall ( struct hvm_device *hvm ) {
00142 
00143         /* Free pages */
00144         free_dma ( hvm->xen.hypercall, hvm->hypercall_len );
00145 }
00146 
00147 /**
00148  * Allocate and map MMIO space
00149  *
00150  * @v hvm               HVM device
00151  * @v space             Source mapping space
00152  * @v len               Length (must be a multiple of PAGE_SIZE)
00153  * @ret mmio            MMIO space address, or NULL on error
00154  */
00155 static void * hvm_ioremap ( struct hvm_device *hvm, unsigned int space,
00156                             size_t len ) {
00157         struct xen_add_to_physmap add;
00158         struct xen_remove_from_physmap remove;
00159         unsigned int pages = ( len / PAGE_SIZE );
00160         physaddr_t mmio_phys;
00161         unsigned int i;
00162         void *mmio;
00163         int xenrc;
00164         int rc;
00165 
00166         /* Sanity check */
00167         assert ( ( len % PAGE_SIZE ) == 0 );
00168 
00169         /* Check for available space */
00170         if ( ( hvm->mmio_offset + len ) > hvm->mmio_len ) {
00171                 DBGC ( hvm, "HVM could not allocate %zd bytes of MMIO space "
00172                        "(%zd of %zd remaining)\n", len,
00173                        ( hvm->mmio_len - hvm->mmio_offset ), hvm->mmio_len );
00174                 goto err_no_space;
00175         }
00176 
00177         /* Map this space */
00178         mmio = ioremap ( ( hvm->mmio + hvm->mmio_offset ), len );
00179         if ( ! mmio ) {
00180                 DBGC ( hvm, "HVM could not map MMIO space [%08lx,%08lx)\n",
00181                        ( hvm->mmio + hvm->mmio_offset ),
00182                        ( hvm->mmio + hvm->mmio_offset + len ) );
00183                 goto err_ioremap;
00184         }
00185         mmio_phys = virt_to_phys ( mmio );
00186 
00187         /* Add to physical address space */
00188         for ( i = 0 ; i < pages ; i++ ) {
00189                 add.domid = DOMID_SELF;
00190                 add.idx = i;
00191                 add.space = space;
00192                 add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
00193                 if ( ( xenrc = xenmem_add_to_physmap ( &hvm->xen, &add ) ) !=0){
00194                         rc = -EXEN ( xenrc );
00195                         DBGC ( hvm, "HVM could not add space %d idx %d at "
00196                                "[%08lx,%08lx): %s\n", space, i,
00197                                ( mmio_phys + ( i * PAGE_SIZE ) ),
00198                                ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
00199                                strerror ( rc ) );
00200                         goto err_add_to_physmap;
00201                 }
00202         }
00203 
00204         /* Update offset */
00205         hvm->mmio_offset += len;
00206 
00207         return mmio;
00208 
00209         i = pages;
00210  err_add_to_physmap:
00211         for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
00212                 remove.domid = DOMID_SELF;
00213                 add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
00214                 xenmem_remove_from_physmap ( &hvm->xen, &remove );
00215         }
00216         iounmap ( mmio );
00217  err_ioremap:
00218  err_no_space:
00219         return NULL;
00220 }
00221 
00222 /**
00223  * Unmap MMIO space
00224  *
00225  * @v hvm               HVM device
00226  * @v mmio              MMIO space address
00227  * @v len               Length (must be a multiple of PAGE_SIZE)
00228  */
00229 static void hvm_iounmap ( struct hvm_device *hvm, void *mmio, size_t len ) {
00230         struct xen_remove_from_physmap remove;
00231         physaddr_t mmio_phys = virt_to_phys ( mmio );
00232         unsigned int pages = ( len / PAGE_SIZE );
00233         unsigned int i;
00234         int xenrc;
00235         int rc;
00236 
00237         /* Unmap this space */
00238         iounmap ( mmio );
00239 
00240         /* Remove from physical address space */
00241         for ( i = 0 ; i < pages ; i++ ) {
00242                 remove.domid = DOMID_SELF;
00243                 remove.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
00244                 if ( ( xenrc = xenmem_remove_from_physmap ( &hvm->xen,
00245                                                             &remove ) ) != 0 ) {
00246                         rc = -EXEN ( xenrc );
00247                         DBGC ( hvm, "HVM could not remove space [%08lx,%08lx): "
00248                                "%s\n", ( mmio_phys + ( i * PAGE_SIZE ) ),
00249                                ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
00250                                strerror ( rc ) );
00251                         /* Nothing we can do about this */
00252                 }
00253         }
00254 }
00255 
00256 /**
00257  * Map shared info page
00258  *
00259  * @v hvm               HVM device
00260  * @ret rc              Return status code
00261  */
00262 static int hvm_map_shared_info ( struct hvm_device *hvm ) {
00263         physaddr_t shared_info_phys;
00264         int rc;
00265 
00266         /* Map shared info page */
00267         hvm->xen.shared = hvm_ioremap ( hvm, XENMAPSPACE_shared_info,
00268                                         PAGE_SIZE );
00269         if ( ! hvm->xen.shared ) {
00270                 rc = -ENOMEM;
00271                 goto err_alloc;
00272         }
00273         shared_info_phys = virt_to_phys ( hvm->xen.shared );
00274         DBGC2 ( hvm, "HVM shared info page at [%#08lx,%#08lx)\n",
00275                 shared_info_phys, ( shared_info_phys + PAGE_SIZE ) );
00276 
00277         /* Sanity check */
00278         DBGC2 ( hvm, "HVM wallclock time is %d\n",
00279                 readl ( &hvm->xen.shared->wc_sec ) );
00280 
00281         return 0;
00282 
00283         hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
00284  err_alloc:
00285         return rc;
00286 }
00287 
00288 /**
00289  * Unmap shared info page
00290  *
00291  * @v hvm               HVM device
00292  */
00293 static void hvm_unmap_shared_info ( struct hvm_device *hvm ) {
00294 
00295         /* Unmap shared info page */
00296         hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
00297 }
00298 
00299 /**
00300  * Map grant table
00301  *
00302  * @v hvm               HVM device
00303  * @ret rc              Return status code
00304  */
00305 static int hvm_map_grant ( struct hvm_device *hvm ) {
00306         physaddr_t grant_phys;
00307         int rc;
00308 
00309         /* Initialise grant table */
00310         if ( ( rc = xengrant_init ( &hvm->xen ) ) != 0 ) {
00311                 DBGC ( hvm, "HVM could not initialise grant table: %s\n",
00312                        strerror ( rc ) );
00313                 return rc;
00314         }
00315 
00316         /* Map grant table */
00317         hvm->xen.grant.table = hvm_ioremap ( hvm, XENMAPSPACE_grant_table,
00318                                              hvm->xen.grant.len );
00319         if ( ! hvm->xen.grant.table )
00320                 return -ENODEV;
00321 
00322         grant_phys = virt_to_phys ( hvm->xen.grant.table );
00323         DBGC2 ( hvm, "HVM mapped grant table at [%08lx,%08lx)\n",
00324                 grant_phys, ( grant_phys + hvm->xen.grant.len ) );
00325         return 0;
00326 }
00327 
00328 /**
00329  * Unmap grant table
00330  *
00331  * @v hvm               HVM device
00332  */
00333 static void hvm_unmap_grant ( struct hvm_device *hvm ) {
00334 
00335         /* Unmap grant table */
00336         hvm_iounmap ( hvm, hvm->xen.grant.table, hvm->xen.grant.len );
00337 }
00338 
00339 /**
00340  * Map XenStore
00341  *
00342  * @v hvm               HVM device
00343  * @ret rc              Return status code
00344  */
00345 static int hvm_map_xenstore ( struct hvm_device *hvm ) {
00346         uint64_t xenstore_evtchn;
00347         uint64_t xenstore_pfn;
00348         physaddr_t xenstore_phys;
00349         char *name;
00350         int xenrc;
00351         int rc;
00352 
00353         /* Get XenStore event channel */
00354         if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_EVTCHN,
00355                                            &xenstore_evtchn ) ) != 0 ) {
00356                 rc = -EXEN ( xenrc );
00357                 DBGC ( hvm, "HVM could not get XenStore event channel: %s\n",
00358                        strerror ( rc ) );
00359                 return rc;
00360         }
00361         hvm->xen.store.port = xenstore_evtchn;
00362 
00363         /* Get XenStore PFN */
00364         if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_PFN,
00365                                            &xenstore_pfn ) ) != 0 ) {
00366                 rc = -EXEN ( xenrc );
00367                 DBGC ( hvm, "HVM could not get XenStore PFN: %s\n",
00368                        strerror ( rc ) );
00369                 return rc;
00370         }
00371         xenstore_phys = ( xenstore_pfn * PAGE_SIZE );
00372 
00373         /* Map XenStore */
00374         hvm->xen.store.intf = ioremap ( xenstore_phys, PAGE_SIZE );
00375         if ( ! hvm->xen.store.intf ) {
00376                 DBGC ( hvm, "HVM could not map XenStore at [%08lx,%08lx)\n",
00377                        xenstore_phys, ( xenstore_phys + PAGE_SIZE ) );
00378                 return -ENODEV;
00379         }
00380         DBGC2 ( hvm, "HVM mapped XenStore at [%08lx,%08lx) with event port "
00381                 "%d\n", xenstore_phys, ( xenstore_phys + PAGE_SIZE ),
00382                 hvm->xen.store.port );
00383 
00384         /* Check that XenStore is working */
00385         if ( ( rc = xenstore_read ( &hvm->xen, &name, "name", NULL ) ) != 0 ) {
00386                 DBGC ( hvm, "HVM could not read domain name: %s\n",
00387                        strerror ( rc ) );
00388                 return rc;
00389         }
00390         DBGC2 ( hvm, "HVM running in domain \"%s\"\n", name );
00391         free ( name );
00392 
00393         return 0;
00394 }
00395 
00396 /**
00397  * Unmap XenStore
00398  *
00399  * @v hvm               HVM device
00400  */
00401 static void hvm_unmap_xenstore ( struct hvm_device *hvm ) {
00402 
00403         /* Unmap XenStore */
00404         iounmap ( hvm->xen.store.intf );
00405 }
00406 
00407 /**
00408  * Probe PCI device
00409  *
00410  * @v pci               PCI device
00411  * @ret rc              Return status code
00412  */
00413 static int hvm_probe ( struct pci_device *pci ) {
00414         struct hvm_device *hvm;
00415         int rc;
00416 
00417         /* Allocate and initialise structure */
00418         hvm = zalloc ( sizeof ( *hvm ) );
00419         if ( ! hvm ) {
00420                 rc = -ENOMEM;
00421                 goto err_alloc;
00422         }
00423         hvm->mmio = pci_bar_start ( pci, HVM_MMIO_BAR );
00424         hvm->mmio_len = pci_bar_size ( pci, HVM_MMIO_BAR );
00425         DBGC2 ( hvm, "HVM has MMIO space [%08lx,%08lx)\n",
00426                 hvm->mmio, ( hvm->mmio + hvm->mmio_len ) );
00427 
00428         /* Fix up PCI device */
00429         adjust_pci_device ( pci );
00430 
00431         /* Attach to hypervisor */
00432         if ( ( rc = hvm_cpuid_base ( hvm ) ) != 0 )
00433                 goto err_cpuid_base;
00434         if ( ( rc = hvm_map_hypercall ( hvm ) ) != 0 )
00435                 goto err_map_hypercall;
00436         if ( ( rc = hvm_map_shared_info ( hvm ) ) != 0 )
00437                 goto err_map_shared_info;
00438         if ( ( rc = hvm_map_grant ( hvm ) ) != 0 )
00439                 goto err_map_grant;
00440         if ( ( rc = hvm_map_xenstore ( hvm ) ) != 0 )
00441                 goto err_map_xenstore;
00442 
00443         /* Probe Xen devices */
00444         if ( ( rc = xenbus_probe ( &hvm->xen, &pci->dev ) ) != 0 ) {
00445                 DBGC ( hvm, "HVM could not probe Xen bus: %s\n",
00446                        strerror ( rc ) );
00447                 goto err_xenbus_probe;
00448         }
00449 
00450         pci_set_drvdata ( pci, hvm );
00451         return 0;
00452 
00453         xenbus_remove ( &hvm->xen, &pci->dev );
00454  err_xenbus_probe:
00455         hvm_unmap_xenstore ( hvm );
00456  err_map_xenstore:
00457         hvm_unmap_grant ( hvm );
00458  err_map_grant:
00459         hvm_unmap_shared_info ( hvm );
00460  err_map_shared_info:
00461         hvm_unmap_hypercall ( hvm );
00462  err_map_hypercall:
00463  err_cpuid_base:
00464         free ( hvm );
00465  err_alloc:
00466         return rc;
00467 }
00468 
00469 /**
00470  * Remove PCI device
00471  *
00472  * @v pci               PCI device
00473  */
00474 static void hvm_remove ( struct pci_device *pci ) {
00475         struct hvm_device *hvm = pci_get_drvdata ( pci );
00476 
00477         xenbus_remove ( &hvm->xen, &pci->dev );
00478         hvm_unmap_xenstore ( hvm );
00479         hvm_unmap_grant ( hvm );
00480         hvm_unmap_shared_info ( hvm );
00481         hvm_unmap_hypercall ( hvm );
00482         free ( hvm );
00483 }
00484 
00485 /** PCI device IDs */
00486 static struct pci_device_id hvm_ids[] = {
00487         PCI_ROM ( 0x5853, 0x0001, "hvm", "hvm", 0 ),
00488         PCI_ROM ( 0x5853, 0x0002, "hvm2", "hvm2", 0 ),
00489 };
00490 
00491 /** PCI driver */
00492 struct pci_driver hvm_driver __pci_driver = {
00493         .ids = hvm_ids,
00494         .id_count = ( sizeof ( hvm_ids ) / sizeof ( hvm_ids[0] ) ),
00495         .probe = hvm_probe,
00496         .remove = hvm_remove,
00497 };
00498 
00499 /* Drag in objects via hvm_driver */
00500 REQUIRING_SYMBOL ( hvm_driver );
00501 
00502 /* Drag in netfront driver */
00503 REQUIRE_OBJECT ( netfront );