Commit 49dd2c49 authored by Hollis Blanchard's avatar Hollis Blanchard Committed by Avi Kivity

KVM: powerpc: Map guest userspace with TID=0 mappings

When we use TID=N userspace mappings, we must ensure that kernel mappings have
been destroyed when entering userspace. Using TID=1/TID=0 for kernel/user
mappings and running userspace with PID=0 means that userspace can't access the
kernel mappings, but the kernel can directly access userspace.

The net is that we don't need to flush the TLB on privilege switches, but we do
on guest context switches (which are far more infrequent). Guest boot time
performance improvement: about 30%.
Signed-off-by: default avatarHollis Blanchard <hollisb@us.ibm.com>
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent 83aae4a8
...@@ -129,7 +129,11 @@ struct kvm_vcpu_arch { ...@@ -129,7 +129,11 @@ struct kvm_vcpu_arch {
u32 ivor[16]; u32 ivor[16];
u32 ivpr; u32 ivpr;
u32 pir; u32 pir;
u32 shadow_pid;
u32 pid; u32 pid;
u32 swap_pid;
u32 pvr; u32 pvr;
u32 ccr0; u32 ccr0;
u32 ccr1; u32 ccr1;
......
...@@ -64,6 +64,7 @@ extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, ...@@ -64,6 +64,7 @@ extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn,
extern void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr, extern void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
gva_t eend, u32 asid); gva_t eend, u32 asid);
extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode); extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
/* XXX Book E specific */ /* XXX Book E specific */
extern void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i); extern void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i);
...@@ -95,4 +96,12 @@ static inline void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr) ...@@ -95,4 +96,12 @@ static inline void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
kvm_vcpu_block(vcpu); kvm_vcpu_block(vcpu);
} }
static inline void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid)
{
if (vcpu->arch.pid != new_pid) {
vcpu->arch.pid = new_pid;
vcpu->arch.swap_pid = 1;
}
}
#endif /* __POWERPC_KVM_PPC_H__ */ #endif /* __POWERPC_KVM_PPC_H__ */
...@@ -369,7 +369,7 @@ int main(void) ...@@ -369,7 +369,7 @@ int main(void)
DEFINE(VCPU_SPRG5, offsetof(struct kvm_vcpu, arch.sprg5)); DEFINE(VCPU_SPRG5, offsetof(struct kvm_vcpu, arch.sprg5));
DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6)); DEFINE(VCPU_SPRG6, offsetof(struct kvm_vcpu, arch.sprg6));
DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7)); DEFINE(VCPU_SPRG7, offsetof(struct kvm_vcpu, arch.sprg7));
DEFINE(VCPU_PID, offsetof(struct kvm_vcpu, arch.pid)); DEFINE(VCPU_SHADOW_PID, offsetof(struct kvm_vcpu, arch.shadow_pid));
DEFINE(VCPU_LAST_INST, offsetof(struct kvm_vcpu, arch.last_inst)); DEFINE(VCPU_LAST_INST, offsetof(struct kvm_vcpu, arch.last_inst));
DEFINE(VCPU_FAULT_DEAR, offsetof(struct kvm_vcpu, arch.fault_dear)); DEFINE(VCPU_FAULT_DEAR, offsetof(struct kvm_vcpu, arch.fault_dear));
......
...@@ -170,7 +170,7 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid, ...@@ -170,7 +170,7 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
/* XXX what about AS? */ /* XXX what about AS? */
stlbe->tid = asid & 0xff; stlbe->tid = !(asid & 0xff);
/* Force TS=1 for all guest mappings. */ /* Force TS=1 for all guest mappings. */
/* For now we hardcode 4KB mappings, but it will be important to /* For now we hardcode 4KB mappings, but it will be important to
...@@ -190,7 +190,7 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid, ...@@ -190,7 +190,7 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid,
void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr, void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
gva_t eend, u32 asid) gva_t eend, u32 asid)
{ {
unsigned int pid = asid & 0xff; unsigned int pid = !(asid & 0xff);
int i; int i;
/* XXX Replace loop with fancy data structures. */ /* XXX Replace loop with fancy data structures. */
...@@ -222,17 +222,20 @@ void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr, ...@@ -222,17 +222,20 @@ void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr,
up_write(&current->mm->mmap_sem); up_write(&current->mm->mmap_sem);
} }
/* Invalidate all mappings, so that when they fault back in they will get the /* Invalidate all mappings on the privilege switch after PID has been changed.
* proper permission bits. */ * The guest always runs with PID=1, so we must clear the entire TLB when
* switching address spaces. */
void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode) void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
{ {
int i; int i;
if (vcpu->arch.swap_pid) {
/* XXX Replace loop with fancy data structures. */ /* XXX Replace loop with fancy data structures. */
down_write(&current->mm->mmap_sem); down_write(&current->mm->mmap_sem);
for (i = 0; i <= tlb_44x_hwater; i++) { for (i = 0; i <= tlb_44x_hwater; i++) {
struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i]; struct tlbe *stlbe = &vcpu->arch.shadow_tlb[i];
/* Future optimization: clear only userspace mappings. */
kvmppc_44x_shadow_release(vcpu, i); kvmppc_44x_shadow_release(vcpu, i);
stlbe->word0 = 0; stlbe->word0 = 0;
kvmppc_tlbe_set_modified(vcpu, i); kvmppc_tlbe_set_modified(vcpu, i);
...@@ -241,4 +244,8 @@ void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode) ...@@ -241,4 +244,8 @@ void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode)
stlbe->word2, handler); stlbe->word2, handler);
} }
up_write(&current->mm->mmap_sem); up_write(&current->mm->mmap_sem);
vcpu->arch.swap_pid = 0;
}
vcpu->arch.shadow_pid = !usermode;
} }
...@@ -486,6 +486,8 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) ...@@ -486,6 +486,8 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
vcpu->arch.msr = 0; vcpu->arch.msr = 0;
vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */ vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
vcpu->arch.shadow_pid = 1;
/* Eye-catching number so we know if the guest takes an interrupt /* Eye-catching number so we know if the guest takes an interrupt
* before it's programmed its own IVPR. */ * before it's programmed its own IVPR. */
vcpu->arch.ivpr = 0x55550000; vcpu->arch.ivpr = 0x55550000;
......
...@@ -332,7 +332,7 @@ lightweight_exit: ...@@ -332,7 +332,7 @@ lightweight_exit:
mfspr r3, SPRN_PID mfspr r3, SPRN_PID
stw r3, VCPU_HOST_PID(r4) stw r3, VCPU_HOST_PID(r4)
lwz r3, VCPU_PID(r4) lwz r3, VCPU_SHADOW_PID(r4)
mtspr SPRN_PID, r3 mtspr SPRN_PID, r3
/* Prevent all asynchronous TLB updates. */ /* Prevent all asynchronous TLB updates. */
......
...@@ -508,7 +508,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) ...@@ -508,7 +508,7 @@ int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
case SPRN_MMUCR: case SPRN_MMUCR:
vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break; vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break;
case SPRN_PID: case SPRN_PID:
vcpu->arch.pid = vcpu->arch.gpr[rs]; break; kvmppc_set_pid(vcpu, vcpu->arch.gpr[rs]); break;
case SPRN_CCR0: case SPRN_CCR0:
vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break; vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break;
case SPRN_CCR1: case SPRN_CCR1:
......
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