xref: /onnv-gate/usr/src/uts/i86pc/os/xpv_platform.c (revision 12090:2986679fe0b5)
110175SStuart.Maybee@Sun.COM /*
210175SStuart.Maybee@Sun.COM  * CDDL HEADER START
310175SStuart.Maybee@Sun.COM  *
410175SStuart.Maybee@Sun.COM  * The contents of this file are subject to the terms of the
510175SStuart.Maybee@Sun.COM  * Common Development and Distribution License (the "License").
610175SStuart.Maybee@Sun.COM  * You may not use this file except in compliance with the License.
710175SStuart.Maybee@Sun.COM  *
810175SStuart.Maybee@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910175SStuart.Maybee@Sun.COM  * or http://www.opensolaris.org/os/licensing.
1010175SStuart.Maybee@Sun.COM  * See the License for the specific language governing permissions
1110175SStuart.Maybee@Sun.COM  * and limitations under the License.
1210175SStuart.Maybee@Sun.COM  *
1310175SStuart.Maybee@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
1410175SStuart.Maybee@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510175SStuart.Maybee@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
1610175SStuart.Maybee@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
1710175SStuart.Maybee@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
1810175SStuart.Maybee@Sun.COM  *
1910175SStuart.Maybee@Sun.COM  * CDDL HEADER END
2010175SStuart.Maybee@Sun.COM  */
2110175SStuart.Maybee@Sun.COM /*
22*12090SFrank.Vanderlinden@Sun.COM  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2310175SStuart.Maybee@Sun.COM  */
2410175SStuart.Maybee@Sun.COM 
2510175SStuart.Maybee@Sun.COM #include <sys/types.h>
2610175SStuart.Maybee@Sun.COM #include <sys/archsystm.h>
2710175SStuart.Maybee@Sun.COM #include <sys/machsystm.h>
2810175SStuart.Maybee@Sun.COM #include <sys/sunndi.h>
2910175SStuart.Maybee@Sun.COM #include <sys/sunddi.h>
3010175SStuart.Maybee@Sun.COM #include <sys/ddi_subrdefs.h>
3110175SStuart.Maybee@Sun.COM #include <sys/xpv_support.h>
3210175SStuart.Maybee@Sun.COM #include <sys/xen_errno.h>
3310175SStuart.Maybee@Sun.COM #include <sys/hypervisor.h>
3410175SStuart.Maybee@Sun.COM #include <sys/gnttab.h>
3510175SStuart.Maybee@Sun.COM #include <sys/xenbus_comms.h>
3610175SStuart.Maybee@Sun.COM #include <sys/xenbus_impl.h>
3710175SStuart.Maybee@Sun.COM #include <sys/sysmacros.h>
3810175SStuart.Maybee@Sun.COM #include <sys/x86_archext.h>
3910175SStuart.Maybee@Sun.COM #include <sys/mman.h>
4010175SStuart.Maybee@Sun.COM #include <sys/stat.h>
4110175SStuart.Maybee@Sun.COM #include <sys/conf.h>
4210175SStuart.Maybee@Sun.COM #include <sys/devops.h>
4310175SStuart.Maybee@Sun.COM #include <sys/pc_mmu.h>
4410175SStuart.Maybee@Sun.COM #include <sys/cmn_err.h>
4510175SStuart.Maybee@Sun.COM #include <sys/cpr.h>
4610175SStuart.Maybee@Sun.COM #include <sys/ddi.h>
4710175SStuart.Maybee@Sun.COM #include <vm/seg_kmem.h>
4810175SStuart.Maybee@Sun.COM #include <vm/as.h>
4910175SStuart.Maybee@Sun.COM #include <vm/hat_pte.h>
5010175SStuart.Maybee@Sun.COM #include <vm/hat_i86.h>
5110175SStuart.Maybee@Sun.COM 
5210175SStuart.Maybee@Sun.COM static int xen_hvm_inited;
5310175SStuart.Maybee@Sun.COM 
5410175SStuart.Maybee@Sun.COM /*
5510175SStuart.Maybee@Sun.COM  * This structure is ordinarily constructed by Xen. In the HVM world, we
5610175SStuart.Maybee@Sun.COM  * manually fill in the few fields the PV drivers need.
5710175SStuart.Maybee@Sun.COM  */
5810175SStuart.Maybee@Sun.COM static start_info_t __xen_info;
5910175SStuart.Maybee@Sun.COM start_info_t *xen_info = NULL;
6010175SStuart.Maybee@Sun.COM 
6110175SStuart.Maybee@Sun.COM static int xen_bits = -1;
6210175SStuart.Maybee@Sun.COM static int xen_major = -1, xen_minor = -1;
6310175SStuart.Maybee@Sun.COM 
6410175SStuart.Maybee@Sun.COM /*
6510175SStuart.Maybee@Sun.COM  * Feature bits; more bits will be added, like direct I/O, etc.
6610175SStuart.Maybee@Sun.COM  */
6710175SStuart.Maybee@Sun.COM #define	XEN_HVM_HYPERCALLS	0x0001
6810175SStuart.Maybee@Sun.COM #define	XEN_HVM_TLBFLUSH	0x0002
6910175SStuart.Maybee@Sun.COM static uint64_t xen_hvm_features;
7010175SStuart.Maybee@Sun.COM 
7110175SStuart.Maybee@Sun.COM /* Metadata page shared between domain and Xen */
7210175SStuart.Maybee@Sun.COM shared_info_t *HYPERVISOR_shared_info = NULL;
7310175SStuart.Maybee@Sun.COM pfn_t xen_shared_info_frame;
7410175SStuart.Maybee@Sun.COM 
7510175SStuart.Maybee@Sun.COM /* Page containing code to issue hypercalls.  */
7610175SStuart.Maybee@Sun.COM extern caddr_t hypercall_page;
7710175SStuart.Maybee@Sun.COM extern caddr_t hypercall_shared_info_page;
7810175SStuart.Maybee@Sun.COM 
7910175SStuart.Maybee@Sun.COM static int
hvm_get_param(int param_id,uint64_t * val)8010175SStuart.Maybee@Sun.COM hvm_get_param(int param_id, uint64_t *val)
8110175SStuart.Maybee@Sun.COM {
8210175SStuart.Maybee@Sun.COM 	struct xen_hvm_param xhp;
8310175SStuart.Maybee@Sun.COM 
8410175SStuart.Maybee@Sun.COM 	xhp.domid = DOMID_SELF;
8510175SStuart.Maybee@Sun.COM 	xhp.index = param_id;
8610175SStuart.Maybee@Sun.COM 	if ((HYPERVISOR_hvm_op(HVMOP_get_param, &xhp) < 0))
8710175SStuart.Maybee@Sun.COM 		return (-1);
8810175SStuart.Maybee@Sun.COM 	*val = xhp.value;
8910175SStuart.Maybee@Sun.COM 	return (0);
9010175SStuart.Maybee@Sun.COM }
9110175SStuart.Maybee@Sun.COM 
9210175SStuart.Maybee@Sun.COM void
xen_hvm_init(void)9310175SStuart.Maybee@Sun.COM xen_hvm_init(void)
9410175SStuart.Maybee@Sun.COM {
9510175SStuart.Maybee@Sun.COM 	struct cpuid_regs cp;
96*12090SFrank.Vanderlinden@Sun.COM 	uint32_t xen_signature[4], base;
9710175SStuart.Maybee@Sun.COM 	char *xen_str;
9810175SStuart.Maybee@Sun.COM 	struct xen_add_to_physmap xatp;
9910175SStuart.Maybee@Sun.COM 	xen_capabilities_info_t caps;
10010175SStuart.Maybee@Sun.COM 	pfn_t pfn;
10110175SStuart.Maybee@Sun.COM 	uint64_t msrval, val;
10210175SStuart.Maybee@Sun.COM 
10310175SStuart.Maybee@Sun.COM 	if (xen_hvm_inited != 0)
10410175SStuart.Maybee@Sun.COM 		return;
10510175SStuart.Maybee@Sun.COM 
10610175SStuart.Maybee@Sun.COM 	xen_hvm_inited = 1;
10710175SStuart.Maybee@Sun.COM 
10810175SStuart.Maybee@Sun.COM 	/*
109*12090SFrank.Vanderlinden@Sun.COM 	 * Xen's pseudo-cpuid function returns a string representing
110*12090SFrank.Vanderlinden@Sun.COM 	 * the Xen signature in %ebx, %ecx, and %edx.
111*12090SFrank.Vanderlinden@Sun.COM 	 * Loop over the base values, since it may be different if
112*12090SFrank.Vanderlinden@Sun.COM 	 * the hypervisor has hyper-v emulation switched on.
113*12090SFrank.Vanderlinden@Sun.COM 	 *
11410175SStuart.Maybee@Sun.COM 	 * %eax contains the maximum supported cpuid function.
11510175SStuart.Maybee@Sun.COM 	 */
116*12090SFrank.Vanderlinden@Sun.COM 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
117*12090SFrank.Vanderlinden@Sun.COM 		cp.cp_eax = base;
118*12090SFrank.Vanderlinden@Sun.COM 		(void) __cpuid_insn(&cp);
119*12090SFrank.Vanderlinden@Sun.COM 		xen_signature[0] = cp.cp_ebx;
120*12090SFrank.Vanderlinden@Sun.COM 		xen_signature[1] = cp.cp_ecx;
121*12090SFrank.Vanderlinden@Sun.COM 		xen_signature[2] = cp.cp_edx;
122*12090SFrank.Vanderlinden@Sun.COM 		xen_signature[3] = 0;
123*12090SFrank.Vanderlinden@Sun.COM 		xen_str = (char *)xen_signature;
124*12090SFrank.Vanderlinden@Sun.COM 		if (strcmp("XenVMMXenVMM", xen_str)  == 0 &&
125*12090SFrank.Vanderlinden@Sun.COM 		    cp.cp_eax >= (base + 2))
126*12090SFrank.Vanderlinden@Sun.COM 			break;
127*12090SFrank.Vanderlinden@Sun.COM 	}
128*12090SFrank.Vanderlinden@Sun.COM 	if (base >= 0x40010000)
12910175SStuart.Maybee@Sun.COM 		return;
13010175SStuart.Maybee@Sun.COM 
13110175SStuart.Maybee@Sun.COM 	/*
132*12090SFrank.Vanderlinden@Sun.COM 	 * cpuid function at base + 1 returns the Xen version in %eax.  The
13310175SStuart.Maybee@Sun.COM 	 * top 16 bits are the major version, the bottom 16 are the minor
13410175SStuart.Maybee@Sun.COM 	 * version.
13510175SStuart.Maybee@Sun.COM 	 */
136*12090SFrank.Vanderlinden@Sun.COM 	cp.cp_eax = base + 1;
13710175SStuart.Maybee@Sun.COM 	(void) __cpuid_insn(&cp);
13810175SStuart.Maybee@Sun.COM 	xen_major = cp.cp_eax >> 16;
13910175SStuart.Maybee@Sun.COM 	xen_minor = cp.cp_eax & 0xffff;
14010175SStuart.Maybee@Sun.COM 
14110175SStuart.Maybee@Sun.COM 	/*
14210175SStuart.Maybee@Sun.COM 	 * Below version 3.1 we can't do anything special as a HVM domain;
14310175SStuart.Maybee@Sun.COM 	 * the PV drivers don't work, many hypercalls are not available,
14410175SStuart.Maybee@Sun.COM 	 * etc.
14510175SStuart.Maybee@Sun.COM 	 */
14610175SStuart.Maybee@Sun.COM 	if (xen_major < 3 || (xen_major == 3 && xen_minor < 1))
14710175SStuart.Maybee@Sun.COM 		return;
14810175SStuart.Maybee@Sun.COM 
14910175SStuart.Maybee@Sun.COM 	/*
150*12090SFrank.Vanderlinden@Sun.COM 	 * cpuid function at base + 2 returns information about the
15110175SStuart.Maybee@Sun.COM 	 * hypercall page.  %eax nominally contains the number of pages
15210175SStuart.Maybee@Sun.COM 	 * with hypercall code, but according to the Xen guys, "I'll
15310175SStuart.Maybee@Sun.COM 	 * guarantee that remains one forever more, so you can just
15410175SStuart.Maybee@Sun.COM 	 * allocate a single page and get quite upset if you ever see CPUID
15510175SStuart.Maybee@Sun.COM 	 * return more than one page."  %ebx contains an MSR we use to ask
15610175SStuart.Maybee@Sun.COM 	 * Xen to remap each page at a specific pfn.
15710175SStuart.Maybee@Sun.COM 	 */
158*12090SFrank.Vanderlinden@Sun.COM 	cp.cp_eax = base + 2;
15910175SStuart.Maybee@Sun.COM 	(void) __cpuid_insn(&cp);
16010175SStuart.Maybee@Sun.COM 
16110175SStuart.Maybee@Sun.COM 	/*
16210175SStuart.Maybee@Sun.COM 	 * Let Xen know where we want the hypercall page mapped.  We
16310175SStuart.Maybee@Sun.COM 	 * already have a page allocated in the .text section to simplify
16410175SStuart.Maybee@Sun.COM 	 * the wrapper code.
16510175SStuart.Maybee@Sun.COM 	 */
16610175SStuart.Maybee@Sun.COM 	pfn = va_to_pfn(&hypercall_page);
16710175SStuart.Maybee@Sun.COM 	msrval = mmu_ptob(pfn);
16810175SStuart.Maybee@Sun.COM 	wrmsr(cp.cp_ebx, msrval);
16910175SStuart.Maybee@Sun.COM 
17010175SStuart.Maybee@Sun.COM 	/* Fill in the xen_info data */
17110175SStuart.Maybee@Sun.COM 	xen_info = &__xen_info;
17210175SStuart.Maybee@Sun.COM 	(void) sprintf(xen_info->magic, "xen-%d.%d", xen_major, xen_minor);
17310175SStuart.Maybee@Sun.COM 
17410175SStuart.Maybee@Sun.COM 	if (hvm_get_param(HVM_PARAM_STORE_PFN, &val) < 0)
17510175SStuart.Maybee@Sun.COM 		return;
17610175SStuart.Maybee@Sun.COM 	/*
17710175SStuart.Maybee@Sun.COM 	 * The first hypercall worked, so mark hypercalls as working.
17810175SStuart.Maybee@Sun.COM 	 */
17910175SStuart.Maybee@Sun.COM 	xen_hvm_features |= XEN_HVM_HYPERCALLS;
18010175SStuart.Maybee@Sun.COM 
18110175SStuart.Maybee@Sun.COM 	xen_info->store_mfn = (mfn_t)val;
18210175SStuart.Maybee@Sun.COM 	if (hvm_get_param(HVM_PARAM_STORE_EVTCHN, &val) < 0)
18310175SStuart.Maybee@Sun.COM 		return;
18410175SStuart.Maybee@Sun.COM 	xen_info->store_evtchn = (mfn_t)val;
18510175SStuart.Maybee@Sun.COM 
18610175SStuart.Maybee@Sun.COM 	/* Figure out whether the hypervisor is 32-bit or 64-bit.  */
18710175SStuart.Maybee@Sun.COM 	if ((HYPERVISOR_xen_version(XENVER_capabilities, &caps) == 0)) {
18810175SStuart.Maybee@Sun.COM 		((char *)(caps))[sizeof (caps) - 1] = '\0';
18910175SStuart.Maybee@Sun.COM 		if (strstr(caps, "x86_64") != NULL)
19010175SStuart.Maybee@Sun.COM 			xen_bits = 64;
19110175SStuart.Maybee@Sun.COM 		else if (strstr(caps, "x86_32") != NULL)
19210175SStuart.Maybee@Sun.COM 			xen_bits = 32;
19310175SStuart.Maybee@Sun.COM 	}
19410175SStuart.Maybee@Sun.COM 
19510175SStuart.Maybee@Sun.COM 	if (xen_bits < 0)
19610175SStuart.Maybee@Sun.COM 		return;
19710175SStuart.Maybee@Sun.COM #ifdef __amd64
19810175SStuart.Maybee@Sun.COM 	ASSERT(xen_bits == 64);
19910175SStuart.Maybee@Sun.COM #endif
20010175SStuart.Maybee@Sun.COM 
20110175SStuart.Maybee@Sun.COM 	/*
20210175SStuart.Maybee@Sun.COM 	 * Allocate space for the shared_info page and tell Xen where it
20310175SStuart.Maybee@Sun.COM 	 * is.
20410175SStuart.Maybee@Sun.COM 	 */
20510175SStuart.Maybee@Sun.COM 	xen_shared_info_frame = va_to_pfn(&hypercall_shared_info_page);
20610175SStuart.Maybee@Sun.COM 	xatp.domid = DOMID_SELF;
20710175SStuart.Maybee@Sun.COM 	xatp.idx = 0;
20810175SStuart.Maybee@Sun.COM 	xatp.space = XENMAPSPACE_shared_info;
20910175SStuart.Maybee@Sun.COM 	xatp.gpfn = xen_shared_info_frame;
21010175SStuart.Maybee@Sun.COM 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp) != 0)
21110175SStuart.Maybee@Sun.COM 		return;
21210175SStuart.Maybee@Sun.COM 
21310175SStuart.Maybee@Sun.COM 	HYPERVISOR_shared_info = (void *)&hypercall_shared_info_page;
21410175SStuart.Maybee@Sun.COM 
21510175SStuart.Maybee@Sun.COM 	/*
21610175SStuart.Maybee@Sun.COM 	 * A working HVM tlb flush hypercall was introduced in Xen 3.3.
21710175SStuart.Maybee@Sun.COM 	 */
21810175SStuart.Maybee@Sun.COM 	if (xen_major > 3 || (xen_major == 3 && xen_minor >= 3))
21910175SStuart.Maybee@Sun.COM 		xen_hvm_features |= XEN_HVM_TLBFLUSH;
22010175SStuart.Maybee@Sun.COM }
22110175SStuart.Maybee@Sun.COM 
22210175SStuart.Maybee@Sun.COM /*
22310175SStuart.Maybee@Sun.COM  * Returns:
22410175SStuart.Maybee@Sun.COM  *          -1 if a feature is not available
22510175SStuart.Maybee@Sun.COM  *          1 if a boolean feature is available
22610175SStuart.Maybee@Sun.COM  *          > 0 if numeric feature is available
22710175SStuart.Maybee@Sun.COM  */
22810175SStuart.Maybee@Sun.COM int
xpv_feature(int which)22910175SStuart.Maybee@Sun.COM xpv_feature(int which)
23010175SStuart.Maybee@Sun.COM {
23110175SStuart.Maybee@Sun.COM 	switch (which) {
23210175SStuart.Maybee@Sun.COM 	case XPVF_BITS:
23310175SStuart.Maybee@Sun.COM 		return (xen_bits);
23410175SStuart.Maybee@Sun.COM 	case XPVF_VERSION_MAJOR:
23510175SStuart.Maybee@Sun.COM 		return (xen_major);
23610175SStuart.Maybee@Sun.COM 	case XPVF_VERSION_MINOR:
23710175SStuart.Maybee@Sun.COM 		return (xen_minor);
23810175SStuart.Maybee@Sun.COM 	case XPVF_HYPERCALLS:
23910175SStuart.Maybee@Sun.COM 		if (xen_hvm_features & XEN_HVM_HYPERCALLS)
24010175SStuart.Maybee@Sun.COM 			return (1);
24110175SStuart.Maybee@Sun.COM 		break;
24210175SStuart.Maybee@Sun.COM 	case XPVF_SHARED_INFO:
24310175SStuart.Maybee@Sun.COM 		if (HYPERVISOR_shared_info != NULL)
24410175SStuart.Maybee@Sun.COM 			return (1);
24510175SStuart.Maybee@Sun.COM 		break;
24610175SStuart.Maybee@Sun.COM 	case XPVF_TLB_FLUSH:
24710175SStuart.Maybee@Sun.COM 		if (xen_hvm_features & XEN_HVM_TLBFLUSH)
24810175SStuart.Maybee@Sun.COM 			return (1);
24910175SStuart.Maybee@Sun.COM 		break;
25010175SStuart.Maybee@Sun.COM 	default:
25110175SStuart.Maybee@Sun.COM 		break;
25210175SStuart.Maybee@Sun.COM 	}
25310175SStuart.Maybee@Sun.COM 
25410175SStuart.Maybee@Sun.COM 	return (-1);
25510175SStuart.Maybee@Sun.COM }
256