blob: 3533c999194a3db8d835d570bd7f4d86bb494bbb [file] [log] [blame]
Alexander Graf29eb61b2009-10-30 05:47:07 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
23#include <asm/page.h>
24#include <asm/asm-offsets.h>
25#include <asm/exception-64s.h>
26
Alexander Grafb79fcdf2010-04-16 00:11:47 +020027#if defined(CONFIG_PPC_BOOK3S_64)
Alexander Grafb79fcdf2010-04-16 00:11:47 +020028#define FUNC(name) GLUE(.,name)
Paul Mackerrasa2d56022013-09-20 14:52:43 +100029#define GET_SHADOW_VCPU(reg) addi reg, r13, PACA_SVCPU
30
Alexander Grafb79fcdf2010-04-16 00:11:47 +020031#elif defined(CONFIG_PPC_BOOK3S_32)
Alexander Grafb79fcdf2010-04-16 00:11:47 +020032#define FUNC(name) name
Paul Mackerrasa2d56022013-09-20 14:52:43 +100033#define GET_SHADOW_VCPU(reg) lwz reg, (THREAD + THREAD_KVM_SVCPU)(r2)
34
Alexander Grafb79fcdf2010-04-16 00:11:47 +020035#endif /* CONFIG_PPC_BOOK3S_XX */
36
Alexander Graf97c4cfb2010-01-04 22:19:25 +010037#define VCPU_LOAD_NVGPRS(vcpu) \
Michael Neulingc75df6f2012-06-25 13:33:10 +000038 PPC_LL r14, VCPU_GPR(R14)(vcpu); \
39 PPC_LL r15, VCPU_GPR(R15)(vcpu); \
40 PPC_LL r16, VCPU_GPR(R16)(vcpu); \
41 PPC_LL r17, VCPU_GPR(R17)(vcpu); \
42 PPC_LL r18, VCPU_GPR(R18)(vcpu); \
43 PPC_LL r19, VCPU_GPR(R19)(vcpu); \
44 PPC_LL r20, VCPU_GPR(R20)(vcpu); \
45 PPC_LL r21, VCPU_GPR(R21)(vcpu); \
46 PPC_LL r22, VCPU_GPR(R22)(vcpu); \
47 PPC_LL r23, VCPU_GPR(R23)(vcpu); \
48 PPC_LL r24, VCPU_GPR(R24)(vcpu); \
49 PPC_LL r25, VCPU_GPR(R25)(vcpu); \
50 PPC_LL r26, VCPU_GPR(R26)(vcpu); \
51 PPC_LL r27, VCPU_GPR(R27)(vcpu); \
52 PPC_LL r28, VCPU_GPR(R28)(vcpu); \
53 PPC_LL r29, VCPU_GPR(R29)(vcpu); \
54 PPC_LL r30, VCPU_GPR(R30)(vcpu); \
55 PPC_LL r31, VCPU_GPR(R31)(vcpu); \
Alexander Graf97c4cfb2010-01-04 22:19:25 +010056
Alexander Graf29eb61b2009-10-30 05:47:07 +000057/*****************************************************************************
58 * *
59 * Guest entry / exit code that is in kernel module memory (highmem) *
60 * *
61 ****************************************************************************/
62
63/* Registers:
64 * r3: kvm_run pointer
65 * r4: vcpu pointer
66 */
Paul Mackerrasdf6909e52011-06-29 00:19:50 +000067_GLOBAL(__kvmppc_vcpu_run)
Alexander Graf29eb61b2009-10-30 05:47:07 +000068
69kvm_start_entry:
70 /* Write correct stack frame */
Alexander Grafb79fcdf2010-04-16 00:11:47 +020071 mflr r0
72 PPC_STL r0,PPC_LR_STKOFF(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000073
74 /* Save host state to the stack */
Alexander Grafb79fcdf2010-04-16 00:11:47 +020075 PPC_STLU r1, -SWITCH_FRAME_SIZE(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000076
77 /* Save r3 (kvm_run) and r4 (vcpu) */
78 SAVE_2GPRS(3, r1)
79
80 /* Save non-volatile registers (r14 - r31) */
81 SAVE_NVGPRS(r1)
82
Alexander Grafe1f8acf2012-03-05 16:00:28 +010083 /* Save CR */
84 mfcr r14
85 stw r14, _CCR(r1)
86
Alexander Graf29eb61b2009-10-30 05:47:07 +000087 /* Save LR */
Alexander Grafb79fcdf2010-04-16 00:11:47 +020088 PPC_STL r0, _LINK(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000089
Alexander Graf97c4cfb2010-01-04 22:19:25 +010090 /* Load non-volatile guest state from the vcpu */
91 VCPU_LOAD_NVGPRS(r4)
92
Alexander Graf29eb61b2009-10-30 05:47:07 +000093kvm_start_lightweight:
Paul Mackerrasa2d56022013-09-20 14:52:43 +100094 /* Copy registers into shadow vcpu so we can access them in real mode */
95 GET_SHADOW_VCPU(r3)
96 bl FUNC(kvmppc_copy_to_svcpu)
97 nop
98 REST_GPR(4, r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +000099
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200100#ifdef CONFIG_PPC_BOOK3S_64
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000101 /* Get the dcbz32 flag */
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200102 PPC_LL r3, VCPU_HFLAGS(r4)
Paul Mackerras02143942011-07-23 17:41:44 +1000103 rldicl r3, r3, 0, 63 /* r3 &= 1 */
104 stb r3, HSTATE_RESTORE_HID5(r13)
Paul Mackerrasc8ae0ac2013-07-11 21:49:43 +1000105
106 /* Load up guest SPRG3 value, since it's user readable */
107 ld r3, VCPU_SHARED(r4)
108 ld r3, VCPU_SHARED_SPRG3(r3)
109 mtspr SPRN_SPRG3, r3
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200110#endif /* CONFIG_PPC_BOOK3S_64 */
111
Paul Mackerras02143942011-07-23 17:41:44 +1000112 PPC_LL r4, VCPU_SHADOW_MSR(r4) /* get shadow_msr */
Alexander Graf7e57cba2010-01-08 02:58:03 +0100113
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200114 /* Jump to segment patching handler and into our guest */
Paul Mackerras02143942011-07-23 17:41:44 +1000115 bl FUNC(kvmppc_entry_trampoline)
116 nop
Alexander Graf29eb61b2009-10-30 05:47:07 +0000117
118/*
119 * This is the handler in module memory. It gets jumped at from the
120 * lowmem trampoline code, so it's basically the guest exit code.
121 *
122 */
123
Alexander Graf29eb61b2009-10-30 05:47:07 +0000124 /*
125 * Register usage at this point:
126 *
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200127 * R1 = host R1
128 * R2 = host R2
129 * R12 = exit handler id
130 * R13 = PACA
131 * SVCPU.* = guest *
Alexander Graf3d3319b2013-11-29 02:32:31 +0100132 * MSR.EE = 1
Alexander Graf29eb61b2009-10-30 05:47:07 +0000133 *
134 */
135
Alexander Grafd825a042013-11-29 02:24:18 +0100136 PPC_LL r3, GPR4(r1) /* vcpu pointer */
137
138 /*
139 * kvmppc_copy_from_svcpu can clobber volatile registers, save
140 * the exit handler id to the vcpu and restore it from there later.
141 */
142 stw r12, VCPU_TRAP(r3)
143
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000144 /* Transfer reg values from shadow vcpu back to vcpu struct */
145 /* On 64-bit, interrupts are still off at this point */
Alexander Grafd825a042013-11-29 02:24:18 +0100146
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000147 GET_SHADOW_VCPU(r4)
148 bl FUNC(kvmppc_copy_from_svcpu)
149 nop
Alexander Graf29eb61b2009-10-30 05:47:07 +0000150
Paul Mackerrasc8ae0ac2013-07-11 21:49:43 +1000151#ifdef CONFIG_PPC_BOOK3S_64
152 /*
153 * Reload kernel SPRG3 value.
154 * No need to save guest value as usermode can't modify SPRG3.
155 */
Scott Wood9d378df2014-03-10 17:29:38 -0500156 ld r3, PACA_SPRG_VDSO(r13)
157 mtspr SPRN_SPRG_VDSO_WRITE, r3
Paul Mackerrasc8ae0ac2013-07-11 21:49:43 +1000158#endif /* CONFIG_PPC_BOOK3S_64 */
159
Paul Mackerrasa2d56022013-09-20 14:52:43 +1000160 /* R7 = vcpu */
161 PPC_LL r7, GPR4(r1)
162
Michael Neulingc75df6f2012-06-25 13:33:10 +0000163 PPC_STL r14, VCPU_GPR(R14)(r7)
164 PPC_STL r15, VCPU_GPR(R15)(r7)
165 PPC_STL r16, VCPU_GPR(R16)(r7)
166 PPC_STL r17, VCPU_GPR(R17)(r7)
167 PPC_STL r18, VCPU_GPR(R18)(r7)
168 PPC_STL r19, VCPU_GPR(R19)(r7)
169 PPC_STL r20, VCPU_GPR(R20)(r7)
170 PPC_STL r21, VCPU_GPR(R21)(r7)
171 PPC_STL r22, VCPU_GPR(R22)(r7)
172 PPC_STL r23, VCPU_GPR(R23)(r7)
173 PPC_STL r24, VCPU_GPR(R24)(r7)
174 PPC_STL r25, VCPU_GPR(R25)(r7)
175 PPC_STL r26, VCPU_GPR(R26)(r7)
176 PPC_STL r27, VCPU_GPR(R27)(r7)
177 PPC_STL r28, VCPU_GPR(R28)(r7)
178 PPC_STL r29, VCPU_GPR(R29)(r7)
179 PPC_STL r30, VCPU_GPR(R30)(r7)
180 PPC_STL r31, VCPU_GPR(R31)(r7)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000181
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100182 /* Pass the exit number as 3rd argument to kvmppc_handle_exit */
Alexander Grafd825a042013-11-29 02:24:18 +0100183 lwz r5, VCPU_TRAP(r7)
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100184
Alexander Graf29eb61b2009-10-30 05:47:07 +0000185 /* Restore r3 (kvm_run) and r4 (vcpu) */
186 REST_2GPRS(3, r1)
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530187 bl FUNC(kvmppc_handle_exit_pr)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000188
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100189 /* If RESUME_GUEST, get back in the loop */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000190 cmpwi r3, RESUME_GUEST
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100191 beq kvm_loop_lightweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000192
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100193 cmpwi r3, RESUME_GUEST_NV
194 beq kvm_loop_heavyweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000195
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100196kvm_exit_loop:
Alexander Graf29eb61b2009-10-30 05:47:07 +0000197
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200198 PPC_LL r4, _LINK(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000199 mtlr r4
200
Alexander Grafe1f8acf2012-03-05 16:00:28 +0100201 lwz r14, _CCR(r1)
202 mtcr r14
203
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100204 /* Restore non-volatile host registers (r14 - r31) */
205 REST_NVGPRS(r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000206
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100207 addi r1, r1, SWITCH_FRAME_SIZE
208 blr
209
210kvm_loop_heavyweight:
211
Alexander Grafb79fcdf2010-04-16 00:11:47 +0200212 PPC_LL r4, _LINK(r1)
213 PPC_STL r4, (PPC_LR_STKOFF + SWITCH_FRAME_SIZE)(r1)
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100214
215 /* Load vcpu and cpu_run */
Alexander Graf29eb61b2009-10-30 05:47:07 +0000216 REST_2GPRS(3, r1)
217
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100218 /* Load non-volatile guest state from the vcpu */
219 VCPU_LOAD_NVGPRS(r4)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000220
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100221 /* Jump back into the beginning of this function */
222 b kvm_start_lightweight
Alexander Graf29eb61b2009-10-30 05:47:07 +0000223
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100224kvm_loop_lightweight:
Alexander Graf29eb61b2009-10-30 05:47:07 +0000225
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100226 /* We'll need the vcpu pointer */
227 REST_GPR(4, r1)
Alexander Graf29eb61b2009-10-30 05:47:07 +0000228
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100229 /* Jump back into the beginning of this function */
230 b kvm_start_lightweight