Commit acd7d8ce authored by Nicholas Piggin's avatar Nicholas Piggin Committed by Michael Ellerman

powerpc/64s: Optimize hypercall/syscall entry

After bc355125 ("powerpc/64: Allow for relocation-on interrupts from
guest to host"), a getppid() system call goes from 307 cycles to 358
cycles (+17%) on POWER8. This is due significantly to the scratch SPR
used by the hypercall check.

It turns out there are a some volatile registers common to both system
call and hypercall (in particular, r12, cr0, ctr), which can be used to
avoid the SPR and some other overheads. This brings getppid to 320 cycles
(+4%).

Testing hcall entry performance by running "sc 1" in guest userspace
before this patch is 854 cycles, afterwards is 826. Also a small win
there.

POWER9 syscall is improved by about the same amount, hcall not tested.
Signed-off-by: default avatarNicholas Piggin <npiggin@gmail.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
parent 9abcc981
...@@ -821,46 +821,80 @@ EXC_VIRT(trap_0b, 0x4b00, 0x100, 0xb00) ...@@ -821,46 +821,80 @@ EXC_VIRT(trap_0b, 0x4b00, 0x100, 0xb00)
TRAMP_KVM(PACA_EXGEN, 0xb00) TRAMP_KVM(PACA_EXGEN, 0xb00)
EXC_COMMON(trap_0b_common, 0xb00, unknown_exception) EXC_COMMON(trap_0b_common, 0xb00, unknown_exception)
/*
* system call / hypercall (0xc00, 0x4c00)
*
* The system call exception is invoked with "sc 0" and does not alter HV bit.
* There is support for kernel code to invoke system calls but there are no
* in-tree users.
*
* The hypercall is invoked with "sc 1" and sets HV=1.
*
* In HPT, sc 1 always goes to 0xc00 real mode. In RADIX, sc 1 can go to
* 0x4c00 virtual mode.
*
* Call convention:
*
* syscall register convention is in Documentation/powerpc/syscall64-abi.txt
*
* For hypercalls, the register convention is as follows:
* r0 volatile
* r1-2 nonvolatile
* r3 volatile parameter and return value for status
* r4-r10 volatile input and output value
* r11 volatile hypercall number and output value
* r12 volatile
* r13-r31 nonvolatile
* LR nonvolatile
* CTR volatile
* XER volatile
* CR0-1 CR5-7 volatile
* CR2-4 nonvolatile
* Other registers nonvolatile
*
* The intersection of volatile registers that don't contain possible
* inputs is: r12, cr0, xer, ctr. We may use these as scratch regs
* upon entry without saving.
*/
#ifdef CONFIG_KVM_BOOK3S_64_HANDLER #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
/* /*
* If CONFIG_KVM_BOOK3S_64_HANDLER is set, save the PPR (on systems * There is a little bit of juggling to get syscall and hcall
* that support it) before changing to HMT_MEDIUM. That allows the KVM * working well. Save r10 in ctr to be restored in case it is a
* code to save that value into the guest state (it is the guest's PPR * hcall.
* value). Otherwise just change to HMT_MEDIUM as userspace has *
* already saved the PPR. * Userspace syscalls have already saved the PPR, hcalls must save
* it before setting HMT_MEDIUM.
*/ */
#define SYSCALL_KVMTEST \ #define SYSCALL_KVMTEST \
SET_SCRATCH0(r13); \ mr r12,r13; \
GET_PACA(r13); \ GET_PACA(r13); \
std r9,PACA_EXGEN+EX_R9(r13); \ mtctr r10; \
OPT_GET_SPR(r9, SPRN_PPR, CPU_FTR_HAS_PPR); \ KVMTEST_PR(0xc00); /* uses r10, branch to do_kvm_0xc00_system_call */ \
HMT_MEDIUM; \ HMT_MEDIUM; \
std r10,PACA_EXGEN+EX_R10(r13); \ mr r9,r12; \
OPT_SAVE_REG_TO_PACA(PACA_EXGEN+EX_PPR, r9, CPU_FTR_HAS_PPR); \
mfcr r9; \
KVMTEST_PR(0xc00); \
GET_SCRATCH0(r13)
#else #else
#define SYSCALL_KVMTEST \ #define SYSCALL_KVMTEST \
HMT_MEDIUM HMT_MEDIUM; \
mr r9,r13; \
GET_PACA(r13);
#endif #endif
#define LOAD_SYSCALL_HANDLER(reg) \ #define LOAD_SYSCALL_HANDLER(reg) \
__LOAD_HANDLER(reg, system_call_common) __LOAD_HANDLER(reg, system_call_common)
/* Syscall routine is used twice, in reloc-off and reloc-on paths */ #define SYSCALL_FASTENDIAN_TEST \
#define SYSCALL_PSERIES_1 \
BEGIN_FTR_SECTION \ BEGIN_FTR_SECTION \
cmpdi r0,0x1ebe ; \ cmpdi r0,0x1ebe ; \
beq- 1f ; \ beq- 1f ; \
END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \
mr r9,r13 ; \
GET_PACA(r13) ; \
mfspr r11,SPRN_SRR0 ; \
0:
#define SYSCALL_PSERIES_2_RFID \ /*
* After SYSCALL_KVMTEST, we reach here with PACA in r13, r13 in r9,
* and HMT_MEDIUM.
*/
#define SYSCALL_REAL \
mfspr r11,SPRN_SRR0 ; \
mfspr r12,SPRN_SRR1 ; \ mfspr r12,SPRN_SRR1 ; \
LOAD_SYSCALL_HANDLER(r10) ; \ LOAD_SYSCALL_HANDLER(r10) ; \
mtspr SPRN_SRR0,r10 ; \ mtspr SPRN_SRR0,r10 ; \
...@@ -869,11 +903,12 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \ ...@@ -869,11 +903,12 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \
rfid ; \ rfid ; \
b . ; /* prevent speculative execution */ b . ; /* prevent speculative execution */
#define SYSCALL_PSERIES_3 \ #define SYSCALL_FASTENDIAN \
/* Fast LE/BE switch system call */ \ /* Fast LE/BE switch system call */ \
1: mfspr r12,SPRN_SRR1 ; \ 1: mfspr r12,SPRN_SRR1 ; \
xori r12,r12,MSR_LE ; \ xori r12,r12,MSR_LE ; \
mtspr SPRN_SRR1,r12 ; \ mtspr SPRN_SRR1,r12 ; \
mr r13,r9 ; \
rfid ; /* return to userspace */ \ rfid ; /* return to userspace */ \
b . ; /* prevent speculative execution */ b . ; /* prevent speculative execution */
...@@ -882,16 +917,18 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \ ...@@ -882,16 +917,18 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \
* We can't branch directly so we do it via the CTR which * We can't branch directly so we do it via the CTR which
* is volatile across system calls. * is volatile across system calls.
*/ */
#define SYSCALL_PSERIES_2_DIRECT \ #define SYSCALL_VIRT \
LOAD_SYSCALL_HANDLER(r12) ; \ LOAD_SYSCALL_HANDLER(r10) ; \
mtctr r12 ; \ mtctr r10 ; \
mfspr r11,SPRN_SRR0 ; \
mfspr r12,SPRN_SRR1 ; \ mfspr r12,SPRN_SRR1 ; \
li r10,MSR_RI ; \ li r10,MSR_RI ; \
mtmsrd r10,1 ; \ mtmsrd r10,1 ; \
bctr ; bctr ;
#else #else
/* We can branch directly */ /* We can branch directly */
#define SYSCALL_PSERIES_2_DIRECT \ #define SYSCALL_VIRT \
mfspr r11,SPRN_SRR0 ; \
mfspr r12,SPRN_SRR1 ; \ mfspr r12,SPRN_SRR1 ; \
li r10,MSR_RI ; \ li r10,MSR_RI ; \
mtmsrd r10,1 ; /* Set RI (EE=0) */ \ mtmsrd r10,1 ; /* Set RI (EE=0) */ \
...@@ -899,20 +936,43 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \ ...@@ -899,20 +936,43 @@ END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE) \
#endif #endif
EXC_REAL_BEGIN(system_call, 0xc00, 0x100) EXC_REAL_BEGIN(system_call, 0xc00, 0x100)
SYSCALL_KVMTEST SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
SYSCALL_PSERIES_1 SYSCALL_FASTENDIAN_TEST
SYSCALL_PSERIES_2_RFID SYSCALL_REAL
SYSCALL_PSERIES_3 SYSCALL_FASTENDIAN
EXC_REAL_END(system_call, 0xc00, 0x100) EXC_REAL_END(system_call, 0xc00, 0x100)
EXC_VIRT_BEGIN(system_call, 0x4c00, 0x100) EXC_VIRT_BEGIN(system_call, 0x4c00, 0x100)
SYSCALL_KVMTEST SYSCALL_KVMTEST /* loads PACA into r13, and saves r13 to r9 */
SYSCALL_PSERIES_1 SYSCALL_FASTENDIAN_TEST
SYSCALL_PSERIES_2_DIRECT SYSCALL_VIRT
SYSCALL_PSERIES_3 SYSCALL_FASTENDIAN
EXC_VIRT_END(system_call, 0x4c00, 0x100) EXC_VIRT_END(system_call, 0x4c00, 0x100)
TRAMP_KVM(PACA_EXGEN, 0xc00) #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
/*
* This is a hcall, so register convention is as above, with these
* differences:
* r13 = PACA
* r12 = orig r13
* ctr = orig r10
*/
TRAMP_KVM_BEGIN(do_kvm_0xc00)
/*
* Save the PPR (on systems that support it) before changing to
* HMT_MEDIUM. That allows the KVM code to save that value into the
* guest state (it is the guest's PPR value).
*/
OPT_GET_SPR(r0, SPRN_PPR, CPU_FTR_HAS_PPR)
HMT_MEDIUM
OPT_SAVE_REG_TO_PACA(PACA_EXGEN+EX_PPR, r0, CPU_FTR_HAS_PPR)
mfctr r10
SET_SCRATCH0(r12)
std r9,PACA_EXGEN+EX_R9(r13)
mfcr r9
std r10,PACA_EXGEN+EX_R10(r13)
KVM_HANDLER(PACA_EXGEN, EXC_STD, 0xc00)
#endif
EXC_REAL(single_step, 0xd00, 0x100) EXC_REAL(single_step, 0xd00, 0x100)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment