vmm_host.c revision 242275
1242275Sneel/*-
2242275Sneel * Copyright (c) 2012 NetApp, Inc.
3242275Sneel * All rights reserved.
4242275Sneel *
5242275Sneel * Redistribution and use in source and binary forms, with or without
6242275Sneel * modification, are permitted provided that the following conditions
7242275Sneel * are met:
8242275Sneel * 1. Redistributions of source code must retain the above copyright
9242275Sneel *    notice, this list of conditions and the following disclaimer.
10242275Sneel * 2. Redistributions in binary form must reproduce the above copyright
11242275Sneel *    notice, this list of conditions and the following disclaimer in the
12242275Sneel *    documentation and/or other materials provided with the distribution.
13242275Sneel *
14242275Sneel * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15242275Sneel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16242275Sneel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17242275Sneel * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18242275Sneel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19242275Sneel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20242275Sneel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21242275Sneel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22242275Sneel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23242275Sneel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24242275Sneel * SUCH DAMAGE.
25242275Sneel *
26242275Sneel * $FreeBSD: projects/bhyve/sys/amd64/vmm/vmm_host.c 242275 2012-10-29 01:51:24Z neel $
27242275Sneel */
28242275Sneel
29242275Sneel#include <sys/cdefs.h>
30242275Sneel__FBSDID("$FreeBSD: projects/bhyve/sys/amd64/vmm/vmm_host.c 242275 2012-10-29 01:51:24Z neel $");
31242275Sneel
32242275Sneel#include <sys/param.h>
33242275Sneel#include <sys/pcpu.h>
34242275Sneel
35242275Sneel#include <machine/cpufunc.h>
36242275Sneel#include <machine/segments.h>
37242275Sneel#include <machine/specialreg.h>
38242275Sneel
39242275Sneel#include "vmm_host.h"
40242275Sneel
41242275Sneelstatic uint64_t vmm_host_efer, vmm_host_pat, vmm_host_cr0, vmm_host_cr4;
42242275Sneel
43242275Sneelvoid
44242275Sneelvmm_host_state_init(void)
45242275Sneel{
46242275Sneel
47242275Sneel	vmm_host_efer = rdmsr(MSR_EFER);
48242275Sneel	vmm_host_pat = rdmsr(MSR_PAT);
49242275Sneel
50242275Sneel	/*
51242275Sneel	 * We always want CR0.TS to be set when the processor does a VM exit.
52242275Sneel	 *
53242275Sneel	 * With emulation turned on unconditionally after a VM exit, we are
54242275Sneel	 * able to trap inadvertent use of the FPU until the guest FPU state
55242275Sneel	 * has been safely squirreled away.
56242275Sneel	 */
57242275Sneel	vmm_host_cr0 = rcr0() | CR0_TS;
58242275Sneel
59242275Sneel	vmm_host_cr4 = rcr4();
60242275Sneel}
61242275Sneel
62242275Sneeluint64_t
63242275Sneelvmm_get_host_pat(void)
64242275Sneel{
65242275Sneel
66242275Sneel	return (vmm_host_pat);
67242275Sneel}
68242275Sneel
69242275Sneeluint64_t
70242275Sneelvmm_get_host_efer(void)
71242275Sneel{
72242275Sneel
73242275Sneel	return (vmm_host_efer);
74242275Sneel}
75242275Sneel
76242275Sneeluint64_t
77242275Sneelvmm_get_host_cr0(void)
78242275Sneel{
79242275Sneel
80242275Sneel	return (vmm_host_cr0);
81242275Sneel}
82242275Sneel
83242275Sneeluint64_t
84242275Sneelvmm_get_host_cr4(void)
85242275Sneel{
86242275Sneel
87242275Sneel	return (vmm_host_cr4);
88242275Sneel}
89242275Sneel
90242275Sneeluint64_t
91242275Sneelvmm_get_host_datasel(void)
92242275Sneel{
93242275Sneel
94242275Sneel	return (GSEL(GDATA_SEL, SEL_KPL));
95242275Sneel
96242275Sneel}
97242275Sneel
98242275Sneeluint64_t
99242275Sneelvmm_get_host_codesel(void)
100242275Sneel{
101242275Sneel
102242275Sneel	return (GSEL(GCODE_SEL, SEL_KPL));
103242275Sneel}
104242275Sneel
105242275Sneeluint64_t
106242275Sneelvmm_get_host_tsssel(void)
107242275Sneel{
108242275Sneel
109242275Sneel	return (GSEL(GPROC0_SEL, SEL_KPL));
110242275Sneel}
111242275Sneel
112242275Sneeluint64_t
113242275Sneelvmm_get_host_fsbase(void)
114242275Sneel{
115242275Sneel
116242275Sneel	return (0);
117242275Sneel}
118242275Sneel
119242275Sneeluint64_t
120242275Sneelvmm_get_host_idtrbase(void)
121242275Sneel{
122242275Sneel
123242275Sneel	return (r_idt.rd_base);
124242275Sneel}
125