xref: /linux/arch/x86/kvm/svm/vmenter.S (revision f94909ceb1ed4bfdb2ada72f93236305e6d6951f)
1199cd1d7SUros Bizjak/* SPDX-License-Identifier: GPL-2.0 */
2199cd1d7SUros Bizjak#include <linux/linkage.h>
3199cd1d7SUros Bizjak#include <asm/asm.h>
4199cd1d7SUros Bizjak#include <asm/bitsperlong.h>
5199cd1d7SUros Bizjak#include <asm/kvm_vcpu_regs.h>
6f14eec0aSPaolo Bonzini#include <asm/nospec-branch.h>
7199cd1d7SUros Bizjak
8199cd1d7SUros Bizjak#define WORD_SIZE (BITS_PER_LONG / 8)
9199cd1d7SUros Bizjak
10199cd1d7SUros Bizjak/* Intentionally omit RAX as it's context switched by hardware */
11199cd1d7SUros Bizjak#define VCPU_RCX	__VCPU_REGS_RCX * WORD_SIZE
12199cd1d7SUros Bizjak#define VCPU_RDX	__VCPU_REGS_RDX * WORD_SIZE
13199cd1d7SUros Bizjak#define VCPU_RBX	__VCPU_REGS_RBX * WORD_SIZE
14199cd1d7SUros Bizjak/* Intentionally omit RSP as it's context switched by hardware */
15199cd1d7SUros Bizjak#define VCPU_RBP	__VCPU_REGS_RBP * WORD_SIZE
16199cd1d7SUros Bizjak#define VCPU_RSI	__VCPU_REGS_RSI * WORD_SIZE
17199cd1d7SUros Bizjak#define VCPU_RDI	__VCPU_REGS_RDI * WORD_SIZE
18199cd1d7SUros Bizjak
19199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
20199cd1d7SUros Bizjak#define VCPU_R8		__VCPU_REGS_R8  * WORD_SIZE
21199cd1d7SUros Bizjak#define VCPU_R9		__VCPU_REGS_R9  * WORD_SIZE
22199cd1d7SUros Bizjak#define VCPU_R10	__VCPU_REGS_R10 * WORD_SIZE
23199cd1d7SUros Bizjak#define VCPU_R11	__VCPU_REGS_R11 * WORD_SIZE
24199cd1d7SUros Bizjak#define VCPU_R12	__VCPU_REGS_R12 * WORD_SIZE
25199cd1d7SUros Bizjak#define VCPU_R13	__VCPU_REGS_R13 * WORD_SIZE
26199cd1d7SUros Bizjak#define VCPU_R14	__VCPU_REGS_R14 * WORD_SIZE
27199cd1d7SUros Bizjak#define VCPU_R15	__VCPU_REGS_R15 * WORD_SIZE
28199cd1d7SUros Bizjak#endif
29199cd1d7SUros Bizjak
30135961e0SThomas Gleixner.section .noinstr.text, "ax"
31199cd1d7SUros Bizjak
32199cd1d7SUros Bizjak/**
33199cd1d7SUros Bizjak * __svm_vcpu_run - Run a vCPU via a transition to SVM guest mode
34199cd1d7SUros Bizjak * @vmcb_pa:	unsigned long
35199cd1d7SUros Bizjak * @regs:	unsigned long * (to guest registers)
36199cd1d7SUros Bizjak */
37199cd1d7SUros BizjakSYM_FUNC_START(__svm_vcpu_run)
38199cd1d7SUros Bizjak	push %_ASM_BP
39199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
40199cd1d7SUros Bizjak	push %r15
41199cd1d7SUros Bizjak	push %r14
42199cd1d7SUros Bizjak	push %r13
43199cd1d7SUros Bizjak	push %r12
44199cd1d7SUros Bizjak#else
45199cd1d7SUros Bizjak	push %edi
46199cd1d7SUros Bizjak	push %esi
47199cd1d7SUros Bizjak#endif
48199cd1d7SUros Bizjak	push %_ASM_BX
49199cd1d7SUros Bizjak
50199cd1d7SUros Bizjak	/* Save @regs. */
51199cd1d7SUros Bizjak	push %_ASM_ARG2
52199cd1d7SUros Bizjak
53199cd1d7SUros Bizjak	/* Save @vmcb. */
54199cd1d7SUros Bizjak	push %_ASM_ARG1
55199cd1d7SUros Bizjak
56199cd1d7SUros Bizjak	/* Move @regs to RAX. */
57199cd1d7SUros Bizjak	mov %_ASM_ARG2, %_ASM_AX
58199cd1d7SUros Bizjak
59199cd1d7SUros Bizjak	/* Load guest registers. */
60199cd1d7SUros Bizjak	mov VCPU_RCX(%_ASM_AX), %_ASM_CX
61199cd1d7SUros Bizjak	mov VCPU_RDX(%_ASM_AX), %_ASM_DX
62199cd1d7SUros Bizjak	mov VCPU_RBX(%_ASM_AX), %_ASM_BX
63199cd1d7SUros Bizjak	mov VCPU_RBP(%_ASM_AX), %_ASM_BP
64199cd1d7SUros Bizjak	mov VCPU_RSI(%_ASM_AX), %_ASM_SI
65199cd1d7SUros Bizjak	mov VCPU_RDI(%_ASM_AX), %_ASM_DI
66199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
67199cd1d7SUros Bizjak	mov VCPU_R8 (%_ASM_AX),  %r8
68199cd1d7SUros Bizjak	mov VCPU_R9 (%_ASM_AX),  %r9
69199cd1d7SUros Bizjak	mov VCPU_R10(%_ASM_AX), %r10
70199cd1d7SUros Bizjak	mov VCPU_R11(%_ASM_AX), %r11
71199cd1d7SUros Bizjak	mov VCPU_R12(%_ASM_AX), %r12
72199cd1d7SUros Bizjak	mov VCPU_R13(%_ASM_AX), %r13
73199cd1d7SUros Bizjak	mov VCPU_R14(%_ASM_AX), %r14
74199cd1d7SUros Bizjak	mov VCPU_R15(%_ASM_AX), %r15
75199cd1d7SUros Bizjak#endif
76199cd1d7SUros Bizjak
77199cd1d7SUros Bizjak	/* "POP" @vmcb to RAX. */
78199cd1d7SUros Bizjak	pop %_ASM_AX
79199cd1d7SUros Bizjak
80199cd1d7SUros Bizjak	/* Enter guest mode */
81f14eec0aSPaolo Bonzini	sti
82199cd1d7SUros Bizjak
837531b47cSUros Bizjak1:	vmrun %_ASM_AX
84199cd1d7SUros Bizjak
857531b47cSUros Bizjak2:	cli
86f14eec0aSPaolo Bonzini
87f14eec0aSPaolo Bonzini#ifdef CONFIG_RETPOLINE
88f14eec0aSPaolo Bonzini	/* IMPORTANT: Stuff the RSB immediately after VM-Exit, before RET! */
89f14eec0aSPaolo Bonzini	FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
90f14eec0aSPaolo Bonzini#endif
91f14eec0aSPaolo Bonzini
92199cd1d7SUros Bizjak	/* "POP" @regs to RAX. */
93199cd1d7SUros Bizjak	pop %_ASM_AX
94199cd1d7SUros Bizjak
95199cd1d7SUros Bizjak	/* Save all guest registers.  */
96199cd1d7SUros Bizjak	mov %_ASM_CX,   VCPU_RCX(%_ASM_AX)
97199cd1d7SUros Bizjak	mov %_ASM_DX,   VCPU_RDX(%_ASM_AX)
98199cd1d7SUros Bizjak	mov %_ASM_BX,   VCPU_RBX(%_ASM_AX)
99199cd1d7SUros Bizjak	mov %_ASM_BP,   VCPU_RBP(%_ASM_AX)
100199cd1d7SUros Bizjak	mov %_ASM_SI,   VCPU_RSI(%_ASM_AX)
101199cd1d7SUros Bizjak	mov %_ASM_DI,   VCPU_RDI(%_ASM_AX)
102199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
103199cd1d7SUros Bizjak	mov %r8,  VCPU_R8 (%_ASM_AX)
104199cd1d7SUros Bizjak	mov %r9,  VCPU_R9 (%_ASM_AX)
105199cd1d7SUros Bizjak	mov %r10, VCPU_R10(%_ASM_AX)
106199cd1d7SUros Bizjak	mov %r11, VCPU_R11(%_ASM_AX)
107199cd1d7SUros Bizjak	mov %r12, VCPU_R12(%_ASM_AX)
108199cd1d7SUros Bizjak	mov %r13, VCPU_R13(%_ASM_AX)
109199cd1d7SUros Bizjak	mov %r14, VCPU_R14(%_ASM_AX)
110199cd1d7SUros Bizjak	mov %r15, VCPU_R15(%_ASM_AX)
111199cd1d7SUros Bizjak#endif
112199cd1d7SUros Bizjak
113199cd1d7SUros Bizjak	/*
114199cd1d7SUros Bizjak	 * Clear all general purpose registers except RSP and RAX to prevent
115199cd1d7SUros Bizjak	 * speculative use of the guest's values, even those that are reloaded
116199cd1d7SUros Bizjak	 * via the stack.  In theory, an L1 cache miss when restoring registers
117199cd1d7SUros Bizjak	 * could lead to speculative execution with the guest's values.
118199cd1d7SUros Bizjak	 * Zeroing XORs are dirt cheap, i.e. the extra paranoia is essentially
119199cd1d7SUros Bizjak	 * free.  RSP and RAX are exempt as they are restored by hardware
120199cd1d7SUros Bizjak	 * during VM-Exit.
121199cd1d7SUros Bizjak	 */
122199cd1d7SUros Bizjak	xor %ecx, %ecx
123199cd1d7SUros Bizjak	xor %edx, %edx
124199cd1d7SUros Bizjak	xor %ebx, %ebx
125199cd1d7SUros Bizjak	xor %ebp, %ebp
126199cd1d7SUros Bizjak	xor %esi, %esi
127199cd1d7SUros Bizjak	xor %edi, %edi
128199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
129199cd1d7SUros Bizjak	xor %r8d,  %r8d
130199cd1d7SUros Bizjak	xor %r9d,  %r9d
131199cd1d7SUros Bizjak	xor %r10d, %r10d
132199cd1d7SUros Bizjak	xor %r11d, %r11d
133199cd1d7SUros Bizjak	xor %r12d, %r12d
134199cd1d7SUros Bizjak	xor %r13d, %r13d
135199cd1d7SUros Bizjak	xor %r14d, %r14d
136199cd1d7SUros Bizjak	xor %r15d, %r15d
137199cd1d7SUros Bizjak#endif
138199cd1d7SUros Bizjak
139199cd1d7SUros Bizjak	pop %_ASM_BX
140199cd1d7SUros Bizjak
141199cd1d7SUros Bizjak#ifdef CONFIG_X86_64
142199cd1d7SUros Bizjak	pop %r12
143199cd1d7SUros Bizjak	pop %r13
144199cd1d7SUros Bizjak	pop %r14
145199cd1d7SUros Bizjak	pop %r15
146199cd1d7SUros Bizjak#else
147199cd1d7SUros Bizjak	pop %esi
148199cd1d7SUros Bizjak	pop %edi
149199cd1d7SUros Bizjak#endif
150199cd1d7SUros Bizjak	pop %_ASM_BP
151*f94909ceSPeter Zijlstra	RET
1527531b47cSUros Bizjak
1537531b47cSUros Bizjak3:	cmpb $0, kvm_rebooting
1547531b47cSUros Bizjak	jne 2b
1557531b47cSUros Bizjak	ud2
1567531b47cSUros Bizjak
1577531b47cSUros Bizjak	_ASM_EXTABLE(1b, 3b)
1587531b47cSUros Bizjak
159199cd1d7SUros BizjakSYM_FUNC_END(__svm_vcpu_run)
16016809ecdSTom Lendacky
16116809ecdSTom Lendacky/**
16216809ecdSTom Lendacky * __svm_sev_es_vcpu_run - Run a SEV-ES vCPU via a transition to SVM guest mode
16316809ecdSTom Lendacky * @vmcb_pa:	unsigned long
16416809ecdSTom Lendacky */
16516809ecdSTom LendackySYM_FUNC_START(__svm_sev_es_vcpu_run)
16616809ecdSTom Lendacky	push %_ASM_BP
16716809ecdSTom Lendacky#ifdef CONFIG_X86_64
16816809ecdSTom Lendacky	push %r15
16916809ecdSTom Lendacky	push %r14
17016809ecdSTom Lendacky	push %r13
17116809ecdSTom Lendacky	push %r12
17216809ecdSTom Lendacky#else
17316809ecdSTom Lendacky	push %edi
17416809ecdSTom Lendacky	push %esi
17516809ecdSTom Lendacky#endif
17616809ecdSTom Lendacky	push %_ASM_BX
17716809ecdSTom Lendacky
1787531b47cSUros Bizjak	/* Move @vmcb to RAX. */
17916809ecdSTom Lendacky	mov %_ASM_ARG1, %_ASM_AX
1807531b47cSUros Bizjak
1817531b47cSUros Bizjak	/* Enter guest mode */
18216809ecdSTom Lendacky	sti
18316809ecdSTom Lendacky
18416809ecdSTom Lendacky1:	vmrun %_ASM_AX
18516809ecdSTom Lendacky
1867531b47cSUros Bizjak2:	cli
18716809ecdSTom Lendacky
18816809ecdSTom Lendacky#ifdef CONFIG_RETPOLINE
18916809ecdSTom Lendacky	/* IMPORTANT: Stuff the RSB immediately after VM-Exit, before RET! */
19016809ecdSTom Lendacky	FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
19116809ecdSTom Lendacky#endif
19216809ecdSTom Lendacky
19316809ecdSTom Lendacky	pop %_ASM_BX
19416809ecdSTom Lendacky
19516809ecdSTom Lendacky#ifdef CONFIG_X86_64
19616809ecdSTom Lendacky	pop %r12
19716809ecdSTom Lendacky	pop %r13
19816809ecdSTom Lendacky	pop %r14
19916809ecdSTom Lendacky	pop %r15
20016809ecdSTom Lendacky#else
20116809ecdSTom Lendacky	pop %esi
20216809ecdSTom Lendacky	pop %edi
20316809ecdSTom Lendacky#endif
20416809ecdSTom Lendacky	pop %_ASM_BP
205*f94909ceSPeter Zijlstra	RET
2067531b47cSUros Bizjak
2077531b47cSUros Bizjak3:	cmpb $0, kvm_rebooting
2087531b47cSUros Bizjak	jne 2b
2097531b47cSUros Bizjak	ud2
2107531b47cSUros Bizjak
2117531b47cSUros Bizjak	_ASM_EXTABLE(1b, 3b)
2127531b47cSUros Bizjak
21316809ecdSTom LendackySYM_FUNC_END(__svm_sev_es_vcpu_run)
214