1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
6 #include <linux/linkage.h>
8 #include <asm/asmmacro.h>
9 #include <asm/loongarch.h>
10 #include <asm/regdef.h>
11 #include <asm/unwind_hints.h>
13 #define HGPR_OFFSET(x) (PT_R0 + 8*x)
14 #define GGPR_OFFSET(x) (KVM_ARCH_GGPR + 8*x)
16 .macro kvm_save_host_gpr base
17 .irp n,1,2,3,22,23,24,25,26,27,28,29,30,31
18 st.d $r\n, \base, HGPR_OFFSET(\n)
22 .macro kvm_restore_host_gpr base
23 .irp n,1,2,3,22,23,24,25,26,27,28,29,30,31
24 ld.d $r\n, \base, HGPR_OFFSET(\n)
29 * Save and restore all GPRs except base register,
30 * and default value of base register is a2.
32 .macro kvm_save_guest_gprs base
33 .irp n,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
34 st.d $r\n, \base, GGPR_OFFSET(\n)
38 .macro kvm_restore_guest_gprs base
39 .irp n,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
40 ld.d $r\n, \base, GGPR_OFFSET(\n)
45 * Prepare switch to guest, save host regs and restore guest regs.
46 * a2: kvm_vcpu_arch, don't touch it until 'ertn'
47 * t0, t1: temp register
49 .macro kvm_switch_to_guest
50 /* Set host ECFG.VS=0, all exceptions share one exception entry */
51 csrrd t0, LOONGARCH_CSR_ECFG
52 bstrins.w t0, zero, CSR_ECFG_VS_SHIFT_END, CSR_ECFG_VS_SHIFT
53 csrwr t0, LOONGARCH_CSR_ECFG
55 /* Load up the new EENTRY */
56 ld.d t0, a2, KVM_ARCH_GEENTRY
57 csrwr t0, LOONGARCH_CSR_EENTRY
60 ld.d t0, a2, KVM_ARCH_GPC
61 csrwr t0, LOONGARCH_CSR_ERA
64 csrrd t0, LOONGARCH_CSR_PGDL
65 st.d t0, a2, KVM_ARCH_HPGD
68 ld.d t1, a2, KVM_VCPU_KVM - KVM_VCPU_ARCH
73 csrwr t0, LOONGARCH_CSR_PGDL
76 csrrd t1, LOONGARCH_CSR_GSTAT
77 bstrpick.w t1, t1, CSR_GSTAT_GID_SHIFT_END, CSR_GSTAT_GID_SHIFT
78 csrrd t0, LOONGARCH_CSR_GTLBC
79 bstrins.w t0, t1, CSR_GTLBC_TGID_SHIFT_END, CSR_GTLBC_TGID_SHIFT
80 csrwr t0, LOONGARCH_CSR_GTLBC
83 * Enable intr in root mode with future ertn so that host interrupt
84 * can be responsed during VM runs
85 * Guest CRMD comes from separate GCSR_CRMD register
87 ori t0, zero, CSR_PRMD_PIE
88 csrxchg t0, t0, LOONGARCH_CSR_PRMD
90 /* Set PVM bit to setup ertn to guest context */
91 ori t0, zero, CSR_GSTAT_PVM
92 csrxchg t0, t0, LOONGARCH_CSR_GSTAT
95 kvm_restore_guest_gprs a2
96 /* Load KVM_ARCH register */
97 ld.d a2, a2, (KVM_ARCH_GGPR + 8 * REG_A2)
99 ertn /* Switch to guest: GSTAT.PGM = 1, ERRCTL.ISERR = 0, TLBRPRMD.ISTLBR = 0 */
103 * Exception entry for general exception from guest mode
105 * - kernel privilege in root mode
106 * - page mode keep unchanged from previous PRMD in root mode
107 * - Fixme: tlb exception cannot happen since registers relative with TLB
108 * - is still in guest mode, such as pgd table/vmid registers etc,
109 * - will fix with hw page walk enabled in future
110 * load kvm_vcpu from reserved CSR KVM_VCPU_KS, and save a2 to KVM_TEMP_KS
113 .cfi_sections .debug_frame
114 SYM_CODE_START(kvm_exc_entry)
115 UNWIND_HINT_UNDEFINED
116 csrwr a2, KVM_TEMP_KS
117 csrrd a2, KVM_VCPU_KS
118 addi.d a2, a2, KVM_VCPU_ARCH
120 /* After save GPRs, free to use any GPR */
121 kvm_save_guest_gprs a2
123 csrrd t0, KVM_TEMP_KS
124 st.d t0, a2, (KVM_ARCH_GGPR + 8 * REG_A2)
126 /* A2 is kvm_vcpu_arch, A1 is free to use */
127 csrrd s1, KVM_VCPU_KS
128 ld.d s0, s1, KVM_VCPU_RUN
130 csrrd t0, LOONGARCH_CSR_ESTAT
131 st.d t0, a2, KVM_ARCH_HESTAT
132 csrrd t0, LOONGARCH_CSR_ERA
133 st.d t0, a2, KVM_ARCH_GPC
134 csrrd t0, LOONGARCH_CSR_BADV
135 st.d t0, a2, KVM_ARCH_HBADV
136 csrrd t0, LOONGARCH_CSR_BADI
137 st.d t0, a2, KVM_ARCH_HBADI
139 /* Restore host ECFG.VS */
140 csrrd t0, LOONGARCH_CSR_ECFG
141 ld.d t1, a2, KVM_ARCH_HECFG
143 csrwr t0, LOONGARCH_CSR_ECFG
145 /* Restore host EENTRY */
146 ld.d t0, a2, KVM_ARCH_HEENTRY
147 csrwr t0, LOONGARCH_CSR_EENTRY
149 /* Restore host pgd table */
150 ld.d t0, a2, KVM_ARCH_HPGD
151 csrwr t0, LOONGARCH_CSR_PGDL
154 * Disable PGM bit to enter root mode by default with next ertn
156 ori t0, zero, CSR_GSTAT_PVM
157 csrxchg zero, t0, LOONGARCH_CSR_GSTAT
160 * Clear GTLBC.TGID field
161 * 0: for root tlb update in future tlb instr
162 * others: for guest tlb update like gpa to hpa in future tlb instr
164 csrrd t0, LOONGARCH_CSR_GTLBC
165 bstrins.w t0, zero, CSR_GTLBC_TGID_SHIFT_END, CSR_GTLBC_TGID_SHIFT
166 csrwr t0, LOONGARCH_CSR_GTLBC
167 ld.d tp, a2, KVM_ARCH_HTP
168 ld.d sp, a2, KVM_ARCH_HSP
169 /* restore per cpu register */
170 ld.d u0, a2, KVM_ARCH_HPERCPU
171 addi.d sp, sp, -PT_SIZE
173 /* Prepare handle exception */
176 ld.d t8, a2, KVM_ARCH_HANDLE_EXIT
180 addi.d a2, a2, KVM_VCPU_ARCH
182 /* Resume host when ret <= 0 */
187 * Save per cpu register again, maybe switched to another cpu
189 st.d u0, a2, KVM_ARCH_HPERCPU
191 /* Save kvm_vcpu to kscratch */
192 csrwr s1, KVM_VCPU_KS
196 ld.d a2, a2, KVM_ARCH_HSP
197 addi.d a2, a2, -PT_SIZE
198 kvm_restore_host_gpr a2
201 SYM_INNER_LABEL(kvm_exc_entry_end, SYM_L_LOCAL)
202 SYM_CODE_END(kvm_exc_entry)
205 * int kvm_enter_guest(struct kvm_run *run, struct kvm_vcpu *vcpu)
211 SYM_FUNC_START(kvm_enter_guest)
212 /* Allocate space in stack bottom */
213 addi.d a2, sp, -PT_SIZE
217 addi.d a2, a1, KVM_VCPU_ARCH
218 st.d sp, a2, KVM_ARCH_HSP
219 st.d tp, a2, KVM_ARCH_HTP
220 /* Save per cpu register */
221 st.d u0, a2, KVM_ARCH_HPERCPU
223 /* Save kvm_vcpu to kscratch */
224 csrwr a1, KVM_VCPU_KS
226 SYM_INNER_LABEL(kvm_enter_guest_end, SYM_L_LOCAL)
227 SYM_FUNC_END(kvm_enter_guest)
229 SYM_FUNC_START(kvm_save_fpu)
231 fpu_save_double a0 t1
234 SYM_FUNC_END(kvm_save_fpu)
236 SYM_FUNC_START(kvm_restore_fpu)
237 fpu_restore_double a0 t1
238 fpu_restore_csr a0 t1 t2
239 fpu_restore_cc a0 t1 t2
241 SYM_FUNC_END(kvm_restore_fpu)
243 #ifdef CONFIG_CPU_HAS_LSX
244 SYM_FUNC_START(kvm_save_lsx)
249 SYM_FUNC_END(kvm_save_lsx)
251 SYM_FUNC_START(kvm_restore_lsx)
252 lsx_restore_data a0 t1
253 fpu_restore_cc a0 t1 t2
254 fpu_restore_csr a0 t1 t2
256 SYM_FUNC_END(kvm_restore_lsx)
259 #ifdef CONFIG_CPU_HAS_LASX
260 SYM_FUNC_START(kvm_save_lasx)
265 SYM_FUNC_END(kvm_save_lasx)
267 SYM_FUNC_START(kvm_restore_lasx)
268 lasx_restore_data a0 t1
269 fpu_restore_cc a0 t1 t2
270 fpu_restore_csr a0 t1 t2
272 SYM_FUNC_END(kvm_restore_lasx)
275 SYM_DATA(kvm_exception_size, .quad kvm_exc_entry_end - kvm_exc_entry)
276 SYM_DATA(kvm_enter_guest_size, .quad kvm_enter_guest_end - kvm_enter_guest)
278 #ifdef CONFIG_CPU_HAS_LBT
279 STACK_FRAME_NON_STANDARD kvm_restore_fpu
280 #ifdef CONFIG_CPU_HAS_LSX
281 STACK_FRAME_NON_STANDARD kvm_restore_lsx
283 #ifdef CONFIG_CPU_HAS_LASX
284 STACK_FRAME_NON_STANDARD kvm_restore_lasx