svm.h revision 249967
1226026Sdelphij/*-
2226026Sdelphij * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
3226026Sdelphij * All rights reserved.
4226026Sdelphij *
5226026Sdelphij * Redistribution and use in source and binary forms, with or without
6226026Sdelphij * modification, are permitted provided that the following conditions
7226026Sdelphij * are met:
8226026Sdelphij * 1. Redistributions of source code must retain the above copyright
9226026Sdelphij *    notice unmodified, this list of conditions, and the following
10226026Sdelphij *    disclaimer.
11226026Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
12226026Sdelphij *    notice, this list of conditions and the following disclaimer in the
13226026Sdelphij *    documentation and/or other materials provided with the distribution.
14226026Sdelphij *
15226026Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16226026Sdelphij * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17226026Sdelphij * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18226026Sdelphij * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19226026Sdelphij * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20226026Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21226026Sdelphij * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22226026Sdelphij * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23226026Sdelphij * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24226026Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25226026Sdelphij *
26226026Sdelphij * $FreeBSD: projects/bhyve_svm/sys/amd64/vmm/amd/svm.h 249967 2013-04-27 04:49:51Z neel $
27226026Sdelphij */
28226026Sdelphij
29226026Sdelphij#ifndef _SVM_H_
30226026Sdelphij#define _SVM_H_
31226026Sdelphij
32226026Sdelphij#define BIT(n)			(1ULL << n)
33226026Sdelphij#define ERR(fmt, args...)	\
34226026Sdelphij	printf("SVM ERROR:%s " fmt "\n", __func__, ##args);
35226026Sdelphij
36226026Sdelphij/*
37226026Sdelphij * Software saved machine state for guest and host.
38226026Sdelphij */
39226026Sdelphij
40226026Sdelphij/* Additional guest register state */
41226026Sdelphijstruct svm_gctx {
42226026Sdelphij	register_t	sctx_rdx;
43226026Sdelphij	register_t	sctx_rdi;
44226026Sdelphij	register_t	sctx_rsi;
45226026Sdelphij	/* Points to host context area. */
46226026Sdelphij	register_t	sctx_hostctx_base;
47226026Sdelphij};
48226026Sdelphij
49226026Sdelphij/* Additional host register state */
50226026Sdelphijstruct svm_hctx {
51226026Sdelphij	uint16_t	sctx_fs;
52226026Sdelphij	uint16_t	sctx_gs;
53226026Sdelphij
54226026Sdelphij	register_t	sctx_rsp;
55226026Sdelphij};
56226026Sdelphij
57226026Sdelphij/* Common register context area for guest and host. */
58226026Sdelphijstruct svm_regctx {
59226026Sdelphij	register_t	sctx_rbp;
60226026Sdelphij
61226026Sdelphij	register_t 	sctx_rbx;
62226026Sdelphij	register_t	sctx_rcx;
63226026Sdelphij
64226026Sdelphij	register_t	sctx_r8;
65226026Sdelphij	register_t	sctx_r9;
66226026Sdelphij	register_t	sctx_r10;
67226026Sdelphij	register_t	sctx_r11;
68226026Sdelphij	register_t	sctx_r12;
69226026Sdelphij	register_t	sctx_r13;
70226026Sdelphij	register_t	sctx_r14;
71226026Sdelphij	register_t	sctx_r15;
72226026Sdelphij
73226026Sdelphij	union {
74226026Sdelphij		struct svm_hctx h;	/* host-specific register state */
75226026Sdelphij		struct svm_gctx g;	/* guest-specific register state */
76226026Sdelphij	} e;
77226026Sdelphij};
78226026Sdelphij
79226026Sdelphijvoid svm_launch(uint64_t pa, struct svm_regctx *, struct svm_regctx *);
80226026Sdelphij
81226026Sdelphijstatic __inline void
82226026Sdelphijdisable_gintr(void)
83226026Sdelphij{
84226026Sdelphij
85226026Sdelphij        __asm __volatile("clgi" : : :);
86226026Sdelphij}
87226026Sdelphij
88226026Sdelphijstatic __inline void
89226026Sdelphijenable_gintr(void)
90226026Sdelphij{
91226026Sdelphij
92226026Sdelphij        __asm __volatile("stgi" : : :);
93226026Sdelphij}
94226026Sdelphij
95226026Sdelphijstatic __inline void
96226026Sdelphijsave_cr2(uint64_t *cr2)
97226026Sdelphij{
98226026Sdelphij
99226026Sdelphij	__asm __volatile(
100226026Sdelphij		"mov %%cr2, %%rax; movq %%rax, %0"
101226026Sdelphij		:"=m"(*cr2)
102226026Sdelphij		:
103226026Sdelphij		:"rax", "memory");
104226026Sdelphij}
105226026Sdelphij
106226026Sdelphijstatic __inline void
107226026Sdelphijload_cr2(uint64_t *cr2)
108226026Sdelphij{
109226026Sdelphij	__asm __volatile(
110226026Sdelphij		"movq %0, %%rax; movq %%rax, %%cr2"
111226026Sdelphij		:
112226026Sdelphij		:"m"(*cr2)
113226026Sdelphij		:"rax");
114226026Sdelphij}
115226026Sdelphij
116226026Sdelphij#endif /* _SVM_H_ */
117226026Sdelphij