Skip to content

Commit f9112ea

Browse files
willdeaconoupton
authored andcommitted
KVM: arm64: Add FF-A helpers to share/unshare memory with secure world
Extend pKVM's memory protection code so that we can update the host's stage-2 page-table to track pages shared with secure world by the host using FF-A and prevent those pages from being mapped into a guest. Co-developed-by: Andrew Walbran <qwandor@google.com> Signed-off-by: Andrew Walbran <qwandor@google.com> Signed-off-by: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20230523101828.7328-6-will@kernel.org Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 9d0c6a9 commit f9112ea

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

arch/arm64/kvm/hyp/include/nvhe/mem_protect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern struct host_mmu host_mmu;
5757
enum pkvm_component_id {
5858
PKVM_ID_HOST,
5959
PKVM_ID_HYP,
60+
PKVM_ID_FFA,
6061
};
6162

6263
extern unsigned long hyp_nr_cpus;
@@ -66,6 +67,8 @@ int __pkvm_host_share_hyp(u64 pfn);
6667
int __pkvm_host_unshare_hyp(u64 pfn);
6768
int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
6869
int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
70+
int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
71+
int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
6972

7073
bool addr_is_memory(phys_addr_t phys);
7174
int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);

arch/arm64/kvm/hyp/nvhe/mem_protect.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,13 @@ static int check_share(struct pkvm_mem_share *share)
842842
case PKVM_ID_HYP:
843843
ret = hyp_ack_share(completer_addr, tx, share->completer_prot);
844844
break;
845+
case PKVM_ID_FFA:
846+
/*
847+
* We only check the host; the secure side will check the other
848+
* end when we forward the FFA call.
849+
*/
850+
ret = 0;
851+
break;
845852
default:
846853
ret = -EINVAL;
847854
}
@@ -870,6 +877,13 @@ static int __do_share(struct pkvm_mem_share *share)
870877
case PKVM_ID_HYP:
871878
ret = hyp_complete_share(completer_addr, tx, share->completer_prot);
872879
break;
880+
case PKVM_ID_FFA:
881+
/*
882+
* We're not responsible for any secure page-tables, so there's
883+
* nothing to do here.
884+
*/
885+
ret = 0;
886+
break;
873887
default:
874888
ret = -EINVAL;
875889
}
@@ -918,6 +932,10 @@ static int check_unshare(struct pkvm_mem_share *share)
918932
case PKVM_ID_HYP:
919933
ret = hyp_ack_unshare(completer_addr, tx);
920934
break;
935+
case PKVM_ID_FFA:
936+
/* See check_share() */
937+
ret = 0;
938+
break;
921939
default:
922940
ret = -EINVAL;
923941
}
@@ -946,6 +964,10 @@ static int __do_unshare(struct pkvm_mem_share *share)
946964
case PKVM_ID_HYP:
947965
ret = hyp_complete_unshare(completer_addr, tx);
948966
break;
967+
case PKVM_ID_FFA:
968+
/* See __do_share() */
969+
ret = 0;
970+
break;
949971
default:
950972
ret = -EINVAL;
951973
}
@@ -1235,3 +1257,49 @@ void hyp_unpin_shared_mem(void *from, void *to)
12351257
hyp_unlock_component();
12361258
host_unlock_component();
12371259
}
1260+
1261+
int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)
1262+
{
1263+
int ret;
1264+
struct pkvm_mem_share share = {
1265+
.tx = {
1266+
.nr_pages = nr_pages,
1267+
.initiator = {
1268+
.id = PKVM_ID_HOST,
1269+
.addr = hyp_pfn_to_phys(pfn),
1270+
},
1271+
.completer = {
1272+
.id = PKVM_ID_FFA,
1273+
},
1274+
},
1275+
};
1276+
1277+
host_lock_component();
1278+
ret = do_share(&share);
1279+
host_unlock_component();
1280+
1281+
return ret;
1282+
}
1283+
1284+
int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)
1285+
{
1286+
int ret;
1287+
struct pkvm_mem_share share = {
1288+
.tx = {
1289+
.nr_pages = nr_pages,
1290+
.initiator = {
1291+
.id = PKVM_ID_HOST,
1292+
.addr = hyp_pfn_to_phys(pfn),
1293+
},
1294+
.completer = {
1295+
.id = PKVM_ID_FFA,
1296+
},
1297+
},
1298+
};
1299+
1300+
host_lock_component();
1301+
ret = do_unshare(&share);
1302+
host_unlock_component();
1303+
1304+
return ret;
1305+
}

0 commit comments

Comments
 (0)