mirror of
https://github.com/hardkernel/linux.git
synced 2026-06-07 11:26:02 +09:00
ANDROID: KVM: arm64: Add __pkvm_host_share_guest hypercall
Add a new hypercall for the nVHE protected mode allowing the host to share one of its pages with a guest. Signed-off-by: Quentin Perret <qperret@google.com> Bug: 209580772 Change-Id: Ibfb86932e5da58c6b88448b49be6c1f994dbbd70 Signed-off-by: Will Deacon <willdeacon@google.com>
This commit is contained in:
committed by
Will Deacon
parent
35c5a8c85d
commit
2570e98d66
@@ -64,6 +64,7 @@ enum __kvm_host_smccc_func {
|
||||
/* Hypercalls available after pKVM finalisation */
|
||||
__KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp,
|
||||
__KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp,
|
||||
__KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest,
|
||||
__KVM_HOST_SMCCC_FUNC___kvm_adjust_pc,
|
||||
__KVM_HOST_SMCCC_FUNC___kvm_vcpu_run,
|
||||
__KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
|
||||
|
||||
@@ -452,8 +452,12 @@ struct kvm_vcpu_arch {
|
||||
/* Don't run the guest (internal implementation need) */
|
||||
bool pause;
|
||||
|
||||
/* Cache some mmu pages needed inside spinlock regions */
|
||||
struct kvm_mmu_memory_cache mmu_page_cache;
|
||||
union {
|
||||
/* Cache some mmu pages needed inside spinlock regions */
|
||||
struct kvm_mmu_memory_cache mmu_page_cache;
|
||||
/* Pages to be donated to pkvm/EL2e if it runs out */
|
||||
struct kvm_hyp_memcache pkvm_memcache;
|
||||
};
|
||||
|
||||
/* Target CPU and feature flags */
|
||||
int target;
|
||||
|
||||
@@ -60,6 +60,7 @@ int __pkvm_host_share_hyp(u64 pfn);
|
||||
int __pkvm_host_unshare_hyp(u64 pfn);
|
||||
int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
|
||||
int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
|
||||
int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct kvm_vcpu *vcpu);
|
||||
|
||||
bool addr_is_memory(phys_addr_t phys);
|
||||
int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
|
||||
|
||||
@@ -579,6 +579,33 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)
|
||||
cpu_reg(host_ctxt, 1) = ret;
|
||||
}
|
||||
|
||||
static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt)
|
||||
{
|
||||
DECLARE_REG(u64, pfn, host_ctxt, 1);
|
||||
DECLARE_REG(u64, gfn, host_ctxt, 2);
|
||||
DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 3);
|
||||
struct pkvm_loaded_state *state;
|
||||
int ret = -EINVAL;
|
||||
u64 nr_pages;
|
||||
|
||||
if (!is_protected_kvm_enabled())
|
||||
goto out;
|
||||
|
||||
vcpu = kern_hyp_va(vcpu);
|
||||
state = this_cpu_ptr(&loaded_state);
|
||||
if (!state->vcpu || !state->is_shadow)
|
||||
goto out;
|
||||
|
||||
/* Topup shadow memcache with the host's */
|
||||
nr_pages = VTCR_EL2_LVLS(state->vcpu->arch.pkvm.shadow_vm->arch.vtcr) - 1;
|
||||
ret = refill_memcache(&state->vcpu->arch.pkvm_memcache, nr_pages,
|
||||
&vcpu->arch.pkvm_memcache);
|
||||
if (!ret)
|
||||
ret = __pkvm_host_share_guest(pfn, gfn, state->vcpu);
|
||||
out:
|
||||
cpu_reg(host_ctxt, 1) = ret;
|
||||
}
|
||||
|
||||
static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt)
|
||||
{
|
||||
DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
|
||||
@@ -818,6 +845,7 @@ static const hcall_t host_hcall[] = {
|
||||
|
||||
HANDLE_FUNC(__pkvm_host_share_hyp),
|
||||
HANDLE_FUNC(__pkvm_host_unshare_hyp),
|
||||
HANDLE_FUNC(__pkvm_host_share_guest),
|
||||
HANDLE_FUNC(__kvm_adjust_pc),
|
||||
HANDLE_FUNC(__kvm_vcpu_run),
|
||||
HANDLE_FUNC(__kvm_flush_vm_context),
|
||||
|
||||
@@ -66,6 +66,16 @@ static void hyp_unlock_component(void)
|
||||
hyp_spin_unlock(&pkvm_pgd_lock);
|
||||
}
|
||||
|
||||
static void guest_lock_component(struct kvm_vcpu *vcpu)
|
||||
{
|
||||
__guest_lock(vcpu->arch.pkvm.shadow_vm);
|
||||
}
|
||||
|
||||
static void guest_unlock_component(struct kvm_vcpu *vcpu)
|
||||
{
|
||||
__guest_unlock(vcpu->arch.pkvm.shadow_vm);
|
||||
}
|
||||
|
||||
static void *host_s2_zalloc_pages_exact(size_t size)
|
||||
{
|
||||
void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
|
||||
@@ -625,6 +635,7 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
|
||||
enum pkvm_component_id {
|
||||
PKVM_ID_HOST,
|
||||
PKVM_ID_HYP,
|
||||
PKVM_ID_GUEST,
|
||||
};
|
||||
|
||||
struct pkvm_mem_transition {
|
||||
@@ -648,6 +659,13 @@ struct pkvm_mem_transition {
|
||||
|
||||
struct {
|
||||
enum pkvm_component_id id;
|
||||
|
||||
union {
|
||||
struct {
|
||||
struct kvm_vcpu *vcpu;
|
||||
phys_addr_t phys;
|
||||
} guest;
|
||||
};
|
||||
} completer;
|
||||
};
|
||||
|
||||
@@ -918,6 +936,52 @@ static int hyp_complete_donation(u64 addr,
|
||||
return pkvm_create_mappings_locked(start, end, prot);
|
||||
}
|
||||
|
||||
static enum pkvm_page_state guest_get_page_state(kvm_pte_t pte)
|
||||
{
|
||||
if (!kvm_pte_valid(pte))
|
||||
return PKVM_NOPAGE;
|
||||
|
||||
return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte));
|
||||
}
|
||||
|
||||
static int __guest_check_page_state_range(struct kvm_vcpu *vcpu, u64 addr,
|
||||
u64 size, enum pkvm_page_state state)
|
||||
{
|
||||
struct kvm_shadow_vm *vm = vcpu->arch.pkvm.shadow_vm;
|
||||
struct check_walk_data d = {
|
||||
.desired = state,
|
||||
.get_page_state = guest_get_page_state,
|
||||
};
|
||||
|
||||
hyp_assert_lock_held(&vm->lock);
|
||||
return check_page_state_range(&vm->pgt, addr, size, &d);
|
||||
}
|
||||
|
||||
static int guest_ack_share(u64 addr, const struct pkvm_mem_transition *tx,
|
||||
enum kvm_pgtable_prot perms)
|
||||
{
|
||||
u64 size = tx->nr_pages * PAGE_SIZE;
|
||||
|
||||
if (perms != KVM_PGTABLE_PROT_RWX)
|
||||
return -EPERM;
|
||||
|
||||
return __guest_check_page_state_range(tx->completer.guest.vcpu, addr,
|
||||
size, PKVM_NOPAGE);
|
||||
}
|
||||
|
||||
static int guest_complete_share(u64 addr, const struct pkvm_mem_transition *tx,
|
||||
enum kvm_pgtable_prot perms)
|
||||
{
|
||||
struct kvm_vcpu *vcpu = tx->completer.guest.vcpu;
|
||||
struct kvm_shadow_vm *vm = vcpu->arch.pkvm.shadow_vm;
|
||||
u64 size = tx->nr_pages * PAGE_SIZE;
|
||||
enum kvm_pgtable_prot prot;
|
||||
|
||||
prot = pkvm_mkstate(perms, PKVM_PAGE_SHARED_BORROWED);
|
||||
return kvm_pgtable_stage2_map(&vm->pgt, addr, size, tx->completer.guest.phys,
|
||||
prot, &vcpu->arch.pkvm_memcache);
|
||||
}
|
||||
|
||||
static int check_share(struct pkvm_mem_share *share)
|
||||
{
|
||||
const struct pkvm_mem_transition *tx = &share->tx;
|
||||
@@ -939,6 +1003,9 @@ static int check_share(struct pkvm_mem_share *share)
|
||||
case PKVM_ID_HYP:
|
||||
ret = hyp_ack_share(completer_addr, tx, share->completer_prot);
|
||||
break;
|
||||
case PKVM_ID_GUEST:
|
||||
ret = guest_ack_share(completer_addr, tx, share->completer_prot);
|
||||
break;
|
||||
default:
|
||||
ret = -EINVAL;
|
||||
}
|
||||
@@ -967,6 +1034,9 @@ static int __do_share(struct pkvm_mem_share *share)
|
||||
case PKVM_ID_HYP:
|
||||
ret = hyp_complete_share(completer_addr, tx, share->completer_prot);
|
||||
break;
|
||||
case PKVM_ID_GUEST:
|
||||
ret = guest_complete_share(completer_addr, tx, share->completer_prot);
|
||||
break;
|
||||
default:
|
||||
ret = -EINVAL;
|
||||
}
|
||||
@@ -1332,3 +1402,40 @@ void hyp_unpin_shared_mem(void *from, void *to)
|
||||
hyp_unlock_component();
|
||||
host_unlock_component();
|
||||
}
|
||||
|
||||
int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct kvm_vcpu *vcpu)
|
||||
{
|
||||
int ret;
|
||||
u64 host_addr = hyp_pfn_to_phys(pfn);
|
||||
u64 guest_addr = hyp_pfn_to_phys(gfn);
|
||||
struct pkvm_mem_share share = {
|
||||
.tx = {
|
||||
.nr_pages = 1,
|
||||
.initiator = {
|
||||
.id = PKVM_ID_HOST,
|
||||
.addr = host_addr,
|
||||
.host = {
|
||||
.completer_addr = guest_addr,
|
||||
},
|
||||
},
|
||||
.completer = {
|
||||
.id = PKVM_ID_GUEST,
|
||||
.guest = {
|
||||
.vcpu = vcpu,
|
||||
.phys = host_addr,
|
||||
},
|
||||
},
|
||||
},
|
||||
.completer_prot = KVM_PGTABLE_PROT_RWX,
|
||||
};
|
||||
|
||||
host_lock_component();
|
||||
guest_lock_component(vcpu);
|
||||
|
||||
ret = do_share(&share);
|
||||
|
||||
guest_unlock_component(vcpu);
|
||||
host_unlock_component();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user