Skip to content

Commit af12376

Browse files
Ard Biesheuvelwilldeacon
authored andcommitted
arm64: kvm: stop treating register x18 as caller save
In preparation of reserving x18, stop treating it as caller save in the KVM guest entry/exit code. Currently, the code assumes there is no need to preserve it for the host, given that it would have been assumed clobbered anyway by the function call to __guest_enter(). Instead, preserve its value and restore it upon return. Link: https://patchwork.kernel.org/patch/9836891/ Signed-off-by: Ard Biesheuvel <[email protected]> [Sami: updated commit message, switched from x18 to x29 for the guest context] Signed-off-by: Sami Tolvanen <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Reviewed-by: Mark Rutland <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 7f153cc commit af12376

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

arch/arm64/kvm/hyp/entry.S

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
.text
2323
.pushsection .hyp.text, "ax"
2424

25+
/*
26+
* We treat x18 as callee-saved as the host may use it as a platform
27+
* register (e.g. for shadow call stack).
28+
*/
2529
.macro save_callee_saved_regs ctxt
30+
str x18, [\ctxt, #CPU_XREG_OFFSET(18)]
2631
stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
2732
stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
2833
stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
@@ -32,6 +37,8 @@
3237
.endm
3338

3439
.macro restore_callee_saved_regs ctxt
40+
// We require \ctxt is not x18-x28
41+
ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)]
3542
ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
3643
ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
3744
ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
@@ -48,7 +55,7 @@ ENTRY(__guest_enter)
4855
// x0: vcpu
4956
// x1: host context
5057
// x2-x17: clobbered by macros
51-
// x18: guest context
58+
// x29: guest context
5259

5360
// Store the host regs
5461
save_callee_saved_regs x1
@@ -67,31 +74,28 @@ alternative_else_nop_endif
6774
ret
6875

6976
1:
70-
add x18, x0, #VCPU_CONTEXT
77+
add x29, x0, #VCPU_CONTEXT
7178

7279
// Macro ptrauth_switch_to_guest format:
7380
// ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
7481
// The below macro to restore guest keys is not implemented in C code
7582
// as it may cause Pointer Authentication key signing mismatch errors
7683
// when this feature is enabled for kernel code.
77-
ptrauth_switch_to_guest x18, x0, x1, x2
84+
ptrauth_switch_to_guest x29, x0, x1, x2
7885

7986
// Restore guest regs x0-x17
80-
ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)]
81-
ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)]
82-
ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)]
83-
ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)]
84-
ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)]
85-
ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)]
86-
ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)]
87-
ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)]
88-
ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)]
89-
90-
// Restore guest regs x19-x29, lr
91-
restore_callee_saved_regs x18
92-
93-
// Restore guest reg x18
94-
ldr x18, [x18, #CPU_XREG_OFFSET(18)]
87+
ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)]
88+
ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)]
89+
ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)]
90+
ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)]
91+
ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)]
92+
ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)]
93+
ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)]
94+
ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)]
95+
ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)]
96+
97+
// Restore guest regs x18-x29, lr
98+
restore_callee_saved_regs x29
9599

96100
// Do not touch any register after this!
97101
eret
@@ -114,7 +118,7 @@ ENTRY(__guest_exit)
114118
// Retrieve the guest regs x0-x1 from the stack
115119
ldp x2, x3, [sp], #16 // x0, x1
116120

117-
// Store the guest regs x0-x1 and x4-x18
121+
// Store the guest regs x0-x1 and x4-x17
118122
stp x2, x3, [x1, #CPU_XREG_OFFSET(0)]
119123
stp x4, x5, [x1, #CPU_XREG_OFFSET(4)]
120124
stp x6, x7, [x1, #CPU_XREG_OFFSET(6)]
@@ -123,9 +127,8 @@ ENTRY(__guest_exit)
123127
stp x12, x13, [x1, #CPU_XREG_OFFSET(12)]
124128
stp x14, x15, [x1, #CPU_XREG_OFFSET(14)]
125129
stp x16, x17, [x1, #CPU_XREG_OFFSET(16)]
126-
str x18, [x1, #CPU_XREG_OFFSET(18)]
127130

128-
// Store the guest regs x19-x29, lr
131+
// Store the guest regs x18-x29, lr
129132
save_callee_saved_regs x1
130133

131134
get_host_ctxt x2, x3

0 commit comments

Comments
 (0)