1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 
  25 #include <sys/types.h>
  26 #include <sys/archsystm.h>
  27 #include <sys/machsystm.h>
  28 #include <sys/sunndi.h>
  29 #include <sys/sunddi.h>
  30 #include <sys/ddi_subrdefs.h>
  31 #include <sys/xpv_support.h>
  32 #include <sys/xen_errno.h>
  33 #include <sys/hypervisor.h>
  34 #include <sys/gnttab.h>
  35 #include <sys/xenbus_comms.h>
  36 #include <sys/xenbus_impl.h>
  37 #include <sys/sysmacros.h>
  38 #include <sys/x86_archext.h>
  39 #include <sys/mman.h>
  40 #include <sys/stat.h>
  41 #include <sys/conf.h>
  42 #include <sys/devops.h>
  43 #include <sys/pc_mmu.h>
  44 #include <sys/cmn_err.h>
  45 #include <sys/cpr.h>
  46 #include <sys/ddi.h>
  47 #include <vm/seg_kmem.h>
  48 #include <vm/as.h>
  49 #include <vm/hat_pte.h>
  50 #include <vm/hat_i86.h>
  51 
  52 static int xen_hvm_inited;
  53 
  54 /*
  55  * This structure is ordinarily constructed by Xen. In the HVM world, we
  56  * manually fill in the few fields the PV drivers need.
  57  */
  58 static start_info_t __xen_info;
  59 start_info_t *xen_info = NULL;
  60 
  61 static int xen_bits = -1;
  62 static int xen_major = -1, xen_minor = -1;
  63 
  64 /*
  65  * Feature bits; more bits will be added, like direct I/O, etc.
  66  */
  67 #define XEN_HVM_HYPERCALLS      0x0001
  68 #define XEN_HVM_TLBFLUSH        0x0002
  69 static uint64_t xen_hvm_features;
  70 
  71 /* Metadata page shared between domain and Xen */
  72 shared_info_t *HYPERVISOR_shared_info = NULL;
  73 pfn_t xen_shared_info_frame;
  74 
  75 /* Page containing code to issue hypercalls.  */
  76 extern caddr_t hypercall_page;
  77 extern caddr_t hypercall_shared_info_page;
  78 
  79 static int
  80 hvm_get_param(int param_id, uint64_t *val)
  81 {
  82         struct xen_hvm_param xhp;
  83 
  84         xhp.domid = DOMID_SELF;
  85         xhp.index = param_id;
  86         if ((HYPERVISOR_hvm_op(HVMOP_get_param, &xhp) < 0))
  87                 return (-1);
  88         *val = xhp.value;
  89         return (0);
  90 }
  91 
  92 void
  93 xen_hvm_init(void)
  94 {
  95         struct cpuid_regs cp;
  96         uint32_t xen_signature[4], base;
  97         char *xen_str;
  98         struct xen_add_to_physmap xatp;
  99         xen_capabilities_info_t caps;
 100         pfn_t pfn;
 101         uint64_t msrval, val;
 102         extern int apix_enable;
 103 
 104         if (xen_hvm_inited != 0)
 105                 return;
 106 
 107         xen_hvm_inited = 1;
 108 
 109         /*
 110          * Xen's pseudo-cpuid function returns a string representing
 111          * the Xen signature in %ebx, %ecx, and %edx.
 112          * Loop over the base values, since it may be different if
 113          * the hypervisor has hyper-v emulation switched on.
 114          *
 115          * %eax contains the maximum supported cpuid function.
 116          */
 117         for (base = 0x40000000; base < 0x40010000; base += 0x100) {
 118                 cp.cp_eax = base;
 119                 (void) __cpuid_insn(&cp);
 120                 xen_signature[0] = cp.cp_ebx;
 121                 xen_signature[1] = cp.cp_ecx;
 122                 xen_signature[2] = cp.cp_edx;
 123                 xen_signature[3] = 0;
 124                 xen_str = (char *)xen_signature;
 125                 if (strcmp("XenVMMXenVMM", xen_str)  == 0 &&
 126                     cp.cp_eax >= (base + 2))
 127                         break;
 128         }
 129         if (base >= 0x40010000)
 130                 return;
 131 
 132         /*
 133          * cpuid function at base + 1 returns the Xen version in %eax.  The
 134          * top 16 bits are the major version, the bottom 16 are the minor
 135          * version.
 136          */
 137         cp.cp_eax = base + 1;
 138         (void) __cpuid_insn(&cp);
 139         xen_major = cp.cp_eax >> 16;
 140         xen_minor = cp.cp_eax & 0xffff;
 141 
 142         /*
 143          * Below version 3.1 we can't do anything special as a HVM domain;
 144          * the PV drivers don't work, many hypercalls are not available,
 145          * etc.
 146          */
 147         if (xen_major < 3 || (xen_major == 3 && xen_minor < 1))
 148                 return;
 149 
 150         /*
 151          * cpuid function at base + 2 returns information about the
 152          * hypercall page.  %eax nominally contains the number of pages
 153          * with hypercall code, but according to the Xen guys, "I'll
 154          * guarantee that remains one forever more, so you can just
 155          * allocate a single page and get quite upset if you ever see CPUID
 156          * return more than one page."  %ebx contains an MSR we use to ask
 157          * Xen to remap each page at a specific pfn.
 158          */
 159         cp.cp_eax = base + 2;
 160         (void) __cpuid_insn(&cp);
 161 
 162         /*
 163          * Let Xen know where we want the hypercall page mapped.  We
 164          * already have a page allocated in the .text section to simplify
 165          * the wrapper code.
 166          */
 167         pfn = va_to_pfn(&hypercall_page);
 168         msrval = mmu_ptob(pfn);
 169         wrmsr(cp.cp_ebx, msrval);
 170 
 171         /* Fill in the xen_info data */
 172         xen_info = &__xen_info;
 173         (void) sprintf(xen_info->magic, "xen-%d.%d", xen_major, xen_minor);
 174 
 175         if (hvm_get_param(HVM_PARAM_STORE_PFN, &val) < 0)
 176                 return;
 177         /*
 178          * The first hypercall worked, so mark hypercalls as working.
 179          */
 180         xen_hvm_features |= XEN_HVM_HYPERCALLS;
 181 
 182         xen_info->store_mfn = (mfn_t)val;
 183         if (hvm_get_param(HVM_PARAM_STORE_EVTCHN, &val) < 0)
 184                 return;
 185         xen_info->store_evtchn = (mfn_t)val;
 186 
 187         /* Figure out whether the hypervisor is 32-bit or 64-bit.  */
 188         if ((HYPERVISOR_xen_version(XENVER_capabilities, &caps) == 0)) {
 189                 ((char *)(caps))[sizeof (caps) - 1] = '\0';
 190                 if (strstr(caps, "x86_64") != NULL)
 191                         xen_bits = 64;
 192                 else if (strstr(caps, "x86_32") != NULL)
 193                         xen_bits = 32;
 194         }
 195 
 196         if (xen_bits < 0)
 197                 return;
 198 #ifdef __amd64
 199         ASSERT(xen_bits == 64);
 200 #endif
 201 
 202         /*
 203          * Allocate space for the shared_info page and tell Xen where it
 204          * is.
 205          */
 206         xen_shared_info_frame = va_to_pfn(&hypercall_shared_info_page);
 207         xatp.domid = DOMID_SELF;
 208         xatp.idx = 0;
 209         xatp.space = XENMAPSPACE_shared_info;
 210         xatp.gpfn = xen_shared_info_frame;
 211         if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp) != 0)
 212                 return;
 213 
 214         HYPERVISOR_shared_info = (void *)&hypercall_shared_info_page;
 215 
 216         /*
 217          * A working HVM tlb flush hypercall was introduced in Xen 3.3.
 218          */
 219         if (xen_major > 3 || (xen_major == 3 && xen_minor >= 3))
 220                 xen_hvm_features |= XEN_HVM_TLBFLUSH;
 221 
 222         /* FIXME Disable apix for the time being */
 223         apix_enable = 0;
 224 }
 225 
 226 /*
 227  * Returns:
 228  *          -1 if a feature is not available
 229  *          1 if a boolean feature is available
 230  *          > 0 if numeric feature is available
 231  */
 232 int
 233 xpv_feature(int which)
 234 {
 235         switch (which) {
 236         case XPVF_BITS:
 237                 return (xen_bits);
 238         case XPVF_VERSION_MAJOR:
 239                 return (xen_major);
 240         case XPVF_VERSION_MINOR:
 241                 return (xen_minor);
 242         case XPVF_HYPERCALLS:
 243                 if (xen_hvm_features & XEN_HVM_HYPERCALLS)
 244                         return (1);
 245                 break;
 246         case XPVF_SHARED_INFO:
 247                 if (HYPERVISOR_shared_info != NULL)
 248                         return (1);
 249                 break;
 250         case XPVF_TLB_FLUSH:
 251                 if (xen_hvm_features & XEN_HVM_TLBFLUSH)
 252                         return (1);
 253                 break;
 254         default:
 255                 break;
 256         }
 257 
 258         return (-1);
 259 }