mirror of
https://github.com/hardkernel/linux.git
synced 2026-06-05 18:41:58 +09:00
ANDROID: KVM: arm64: Extend memory sharing to allow host-to-guest transitions
In preparation for handling guest stage-2 mappings at EL2, extend our memory protection mechanisms to support sharing of pages from the host to a specific guest. Signed-off-by: Will Deacon <will@kernel.org> Signed-off-by: Will Deacon <willdeacon@google.com> Bug: 233587962 Change-Id: I8e1d7cf4db70ad55a29d935f60e6335fc83490eb
This commit is contained in:
@@ -472,8 +472,12 @@ struct kvm_vcpu_arch {
|
||||
/* vcpu power state */
|
||||
struct kvm_mp_state mp_state;
|
||||
|
||||
/* 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/EL2 if it runs out */
|
||||
struct kvm_hyp_memcache pkvm_memcache;
|
||||
};
|
||||
|
||||
/* Target CPU and feature flags */
|
||||
int target;
|
||||
|
||||
@@ -57,6 +57,7 @@ extern struct host_mmu host_mmu;
|
||||
enum pkvm_component_id {
|
||||
PKVM_ID_HOST,
|
||||
PKVM_ID_HYP,
|
||||
PKVM_ID_GUEST,
|
||||
};
|
||||
|
||||
extern unsigned long hyp_nr_cpus;
|
||||
@@ -67,6 +68,7 @@ int __pkvm_host_unshare_hyp(u64 pfn);
|
||||
int __pkvm_host_reclaim_page(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 pkvm_hyp_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);
|
||||
|
||||
@@ -578,11 +578,21 @@ struct pkvm_mem_transition {
|
||||
struct {
|
||||
u64 completer_addr;
|
||||
} hyp;
|
||||
struct {
|
||||
struct pkvm_hyp_vcpu *hyp_vcpu;
|
||||
} guest;
|
||||
};
|
||||
} initiator;
|
||||
|
||||
struct {
|
||||
enum pkvm_component_id id;
|
||||
|
||||
union {
|
||||
struct {
|
||||
struct pkvm_hyp_vcpu *hyp_vcpu;
|
||||
phys_addr_t phys;
|
||||
} guest;
|
||||
};
|
||||
} completer;
|
||||
};
|
||||
|
||||
@@ -846,6 +856,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 pkvm_hyp_vcpu *vcpu, u64 addr,
|
||||
u64 size, enum pkvm_page_state state)
|
||||
{
|
||||
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
|
||||
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.hyp_vcpu,
|
||||
addr, size, PKVM_NOPAGE);
|
||||
}
|
||||
|
||||
static int guest_complete_share(u64 addr, const struct pkvm_mem_transition *tx,
|
||||
enum kvm_pgtable_prot perms)
|
||||
{
|
||||
struct pkvm_hyp_vcpu *vcpu = tx->completer.guest.hyp_vcpu;
|
||||
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
|
||||
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->vcpu.arch.pkvm_memcache);
|
||||
}
|
||||
|
||||
static int check_share(struct pkvm_mem_share *share)
|
||||
{
|
||||
const struct pkvm_mem_transition *tx = &share->tx;
|
||||
@@ -867,6 +923,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;
|
||||
}
|
||||
@@ -895,6 +954,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;
|
||||
}
|
||||
@@ -1261,6 +1323,44 @@ void hyp_unpin_shared_mem(void *from, void *to)
|
||||
host_unlock_component();
|
||||
}
|
||||
|
||||
int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu)
|
||||
{
|
||||
int ret;
|
||||
u64 host_addr = hyp_pfn_to_phys(pfn);
|
||||
u64 guest_addr = hyp_pfn_to_phys(gfn);
|
||||
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
|
||||
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 = {
|
||||
.hyp_vcpu = vcpu,
|
||||
.phys = host_addr,
|
||||
},
|
||||
},
|
||||
},
|
||||
.completer_prot = KVM_PGTABLE_PROT_RWX,
|
||||
};
|
||||
|
||||
host_lock_component();
|
||||
guest_lock_component(vm);
|
||||
|
||||
ret = do_share(&share);
|
||||
|
||||
guest_unlock_component(vm);
|
||||
host_unlock_component();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int hyp_zero_page(phys_addr_t phys)
|
||||
{
|
||||
void *addr;
|
||||
|
||||
Reference in New Issue
Block a user