xref: /illumos-gate/usr/src/uts/intel/io/vmm/vmm_host.c (revision 32640292)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2012 NetApp, Inc.
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
27bf21cd93STycho Nightingale  */
28bf21cd93STycho Nightingale /*
29bf21cd93STycho Nightingale  * This file and its contents are supplied under the terms of the
30bf21cd93STycho Nightingale  * Common Development and Distribution License ("CDDL"), version 1.0.
31bf21cd93STycho Nightingale  * You may only use this file in accordance with the terms of version
32bf21cd93STycho Nightingale  * 1.0 of the CDDL.
33bf21cd93STycho Nightingale  *
34bf21cd93STycho Nightingale  * A full copy of the text of the CDDL should have accompanied this
35bf21cd93STycho Nightingale  * source.  A copy of the CDDL is also available via the Internet at
36bf21cd93STycho Nightingale  * http://www.illumos.org/license/CDDL.
37bf21cd93STycho Nightingale  *
38bf21cd93STycho Nightingale  * Copyright 2013 Pluribus Networks Inc.
39bf21cd93STycho Nightingale  */
40bf21cd93STycho Nightingale 
41bf21cd93STycho Nightingale #include <sys/cdefs.h>
42bf21cd93STycho Nightingale 
43bf21cd93STycho Nightingale #include <sys/param.h>
44bf21cd93STycho Nightingale #include <sys/pcpu.h>
45bf21cd93STycho Nightingale 
46bf21cd93STycho Nightingale #include <machine/cpufunc.h>
47bf21cd93STycho Nightingale #include <machine/segments.h>
48bf21cd93STycho Nightingale #include <machine/specialreg.h>
49bf21cd93STycho Nightingale 
50bf21cd93STycho Nightingale #include "vmm_host.h"
51bf21cd93STycho Nightingale 
524c87aefeSPatrick Mooney static uint64_t vmm_host_efer, vmm_host_pat, vmm_host_cr0, vmm_host_cr4,
534c87aefeSPatrick Mooney 	vmm_host_xcr0;
544c87aefeSPatrick Mooney static struct xsave_limits vmm_xsave_limits;
55bf21cd93STycho Nightingale 
56bf21cd93STycho Nightingale void
vmm_host_state_init(void)57bf21cd93STycho Nightingale vmm_host_state_init(void)
58bf21cd93STycho Nightingale {
594c87aefeSPatrick Mooney 	unsigned int regs[4];
60bf21cd93STycho Nightingale 
61bf21cd93STycho Nightingale 	vmm_host_efer = rdmsr(MSR_EFER);
62bf21cd93STycho Nightingale 	vmm_host_pat = rdmsr(MSR_PAT);
63bf21cd93STycho Nightingale 
64bf21cd93STycho Nightingale 	/*
65bf21cd93STycho Nightingale 	 * We always want CR0.TS to be set when the processor does a VM exit.
66bf21cd93STycho Nightingale 	 *
67bf21cd93STycho Nightingale 	 * With emulation turned on unconditionally after a VM exit, we are
68bf21cd93STycho Nightingale 	 * able to trap inadvertent use of the FPU until the guest FPU state
69bf21cd93STycho Nightingale 	 * has been safely squirreled away.
70bf21cd93STycho Nightingale 	 */
71bf21cd93STycho Nightingale 	vmm_host_cr0 = rcr0() | CR0_TS;
72bf21cd93STycho Nightingale 
734c87aefeSPatrick Mooney 	/*
744c87aefeSPatrick Mooney 	 * On non-PCID or PCID but without INVPCID support machines,
754c87aefeSPatrick Mooney 	 * we flush kernel i.e. global TLB entries, by temporary
764c87aefeSPatrick Mooney 	 * clearing the CR4.PGE bit, see invltlb_glob().  If
774c87aefeSPatrick Mooney 	 * preemption occurs at the wrong time, cached vmm_host_cr4
784c87aefeSPatrick Mooney 	 * might store the value with CR4.PGE cleared.  Since FreeBSD
794c87aefeSPatrick Mooney 	 * requires support for PG_G on amd64, just set it
804c87aefeSPatrick Mooney 	 * unconditionally.
814c87aefeSPatrick Mooney 	 */
824c87aefeSPatrick Mooney 	vmm_host_cr4 = rcr4() | CR4_PGE;
834c87aefeSPatrick Mooney 
844c87aefeSPatrick Mooney 	/*
854c87aefeSPatrick Mooney 	 * Only permit a guest to use XSAVE if the host is using
864c87aefeSPatrick Mooney 	 * XSAVE.  Only permit a guest to use XSAVE features supported
874c87aefeSPatrick Mooney 	 * by the host.  This ensures that the FPU state used by the
884c87aefeSPatrick Mooney 	 * guest is always a subset of the saved guest FPU state.
894c87aefeSPatrick Mooney 	 *
904c87aefeSPatrick Mooney 	 * In addition, only permit known XSAVE features where the
914c87aefeSPatrick Mooney 	 * rules for which features depend on other features is known
924c87aefeSPatrick Mooney 	 * to properly emulate xsetbv.
934c87aefeSPatrick Mooney 	 */
944c87aefeSPatrick Mooney 	if (vmm_host_cr4 & CR4_XSAVE) {
954c87aefeSPatrick Mooney 		vmm_xsave_limits.xsave_enabled = 1;
964c87aefeSPatrick Mooney 		vmm_host_xcr0 = rxcr(0);
974c87aefeSPatrick Mooney 		vmm_xsave_limits.xcr0_allowed = vmm_host_xcr0 &
984c87aefeSPatrick Mooney 		    (XFEATURE_AVX | XFEATURE_MPX | XFEATURE_AVX512);
994c87aefeSPatrick Mooney 
1004c87aefeSPatrick Mooney 		cpuid_count(0xd, 0x0, regs);
1014c87aefeSPatrick Mooney 		vmm_xsave_limits.xsave_max_size = regs[1];
1024c87aefeSPatrick Mooney 	}
103bf21cd93STycho Nightingale }
104bf21cd93STycho Nightingale 
105bf21cd93STycho Nightingale uint64_t
vmm_get_host_pat(void)106bf21cd93STycho Nightingale vmm_get_host_pat(void)
107bf21cd93STycho Nightingale {
108bf21cd93STycho Nightingale 
109bf21cd93STycho Nightingale 	return (vmm_host_pat);
110bf21cd93STycho Nightingale }
111bf21cd93STycho Nightingale 
112bf21cd93STycho Nightingale uint64_t
vmm_get_host_efer(void)113bf21cd93STycho Nightingale vmm_get_host_efer(void)
114bf21cd93STycho Nightingale {
115bf21cd93STycho Nightingale 
116bf21cd93STycho Nightingale 	return (vmm_host_efer);
117bf21cd93STycho Nightingale }
118bf21cd93STycho Nightingale 
119bf21cd93STycho Nightingale uint64_t
vmm_get_host_cr0(void)120bf21cd93STycho Nightingale vmm_get_host_cr0(void)
121bf21cd93STycho Nightingale {
122bf21cd93STycho Nightingale 
123bf21cd93STycho Nightingale 	return (vmm_host_cr0);
124bf21cd93STycho Nightingale }
125bf21cd93STycho Nightingale 
126bf21cd93STycho Nightingale uint64_t
vmm_get_host_cr4(void)127bf21cd93STycho Nightingale vmm_get_host_cr4(void)
128bf21cd93STycho Nightingale {
129bf21cd93STycho Nightingale 
130bf21cd93STycho Nightingale 	return (vmm_host_cr4);
131bf21cd93STycho Nightingale }
132bf21cd93STycho Nightingale 
1334c87aefeSPatrick Mooney uint64_t
vmm_get_host_xcr0(void)1344c87aefeSPatrick Mooney vmm_get_host_xcr0(void)
1354c87aefeSPatrick Mooney {
1364c87aefeSPatrick Mooney 
1374c87aefeSPatrick Mooney 	return (vmm_host_xcr0);
1384c87aefeSPatrick Mooney }
1394c87aefeSPatrick Mooney 
140bf21cd93STycho Nightingale uint64_t
vmm_get_host_datasel(void)141bf21cd93STycho Nightingale vmm_get_host_datasel(void)
142bf21cd93STycho Nightingale {
143bf21cd93STycho Nightingale 	return (SEL_GDT(GDT_KDATA, SEL_KPL));
144bf21cd93STycho Nightingale }
145bf21cd93STycho Nightingale 
146bf21cd93STycho Nightingale uint64_t
vmm_get_host_codesel(void)147bf21cd93STycho Nightingale vmm_get_host_codesel(void)
148bf21cd93STycho Nightingale {
149bf21cd93STycho Nightingale 	return (SEL_GDT(GDT_KCODE, SEL_KPL));
150bf21cd93STycho Nightingale }
151bf21cd93STycho Nightingale 
152bf21cd93STycho Nightingale uint64_t
vmm_get_host_tsssel(void)153bf21cd93STycho Nightingale vmm_get_host_tsssel(void)
154bf21cd93STycho Nightingale {
155bf21cd93STycho Nightingale 	return (SEL_GDT(GDT_KTSS, SEL_KPL));
156bf21cd93STycho Nightingale }
157bf21cd93STycho Nightingale 
158bf21cd93STycho Nightingale uint64_t
vmm_get_host_fsbase(void)159bf21cd93STycho Nightingale vmm_get_host_fsbase(void)
160bf21cd93STycho Nightingale {
161bf21cd93STycho Nightingale 	return (rdmsr(MSR_FSBASE));
162bf21cd93STycho Nightingale }
163bf21cd93STycho Nightingale 
164bf21cd93STycho Nightingale uint64_t
vmm_get_host_idtrbase(void)165bf21cd93STycho Nightingale vmm_get_host_idtrbase(void)
166bf21cd93STycho Nightingale {
167bf21cd93STycho Nightingale 	desctbr_t idtr;
168bf21cd93STycho Nightingale 
169bf21cd93STycho Nightingale 	rd_idtr(&idtr);
170bf21cd93STycho Nightingale 	return (idtr.dtr_base);
171bf21cd93STycho Nightingale }
1724c87aefeSPatrick Mooney 
1734c87aefeSPatrick Mooney const struct xsave_limits *
vmm_get_xsave_limits(void)1744c87aefeSPatrick Mooney vmm_get_xsave_limits(void)
1754c87aefeSPatrick Mooney {
1764c87aefeSPatrick Mooney 
1774c87aefeSPatrick Mooney 	return (&vmm_xsave_limits);
1784c87aefeSPatrick Mooney }
179