1 // SPDX-License-Identifier: GPL-2.0-only
3 * FF-A v1.0 proxy to filter out invalid memory-sharing SMC calls issued by
4 * the host. FF-A is a slightly more palatable abbreviation of "Arm Firmware
5 * Framework for Arm A-profile", which is specified by Arm in document
8 * Copyright (C) 2022 - Google LLC
9 * Author: Andrew Walbran <qwandor@google.com>
11 * This driver hooks into the SMC trapping logic for the host and intercepts
12 * all calls falling within the FF-A range. Each call is either:
14 * - Forwarded on unmodified to the SPMD at EL3
15 * - Rejected as "unsupported"
16 * - Accompanied by a host stage-2 page-table check/update and reissued
18 * Consequently, any attempts by the host to make guest memory pages
19 * accessible to the secure world using FF-A will be detected either here
20 * (in the case that the memory is already owned by the guest) or during
21 * donation to the guest (in the case that the memory was previously shared
22 * with the secure world).
24 * To allow the rolling-back of page-table updates and FF-A calls in the
25 * event of failure, operations involving the RXTX buffers are locked for
26 * the duration and are therefore serialised.
29 #include <linux/arm-smccc.h>
30 #include <linux/arm_ffa.h>
31 #include <asm/kvm_pkvm.h>
34 #include <nvhe/mem_protect.h>
35 #include <nvhe/memory.h>
36 #include <nvhe/trap_handler.h>
37 #include <nvhe/spinlock.h>
40 * "ID value 0 must be returned at the Non-secure physical FF-A instance"
41 * We share this ID with the host.
46 * A buffer to hold the maximum descriptor size we can see from the host,
47 * which is required when the SPMD returns a fragmented FFA_MEM_RETRIEVE_RESP
48 * when resolving the handle on the reclaim path.
50 struct kvm_ffa_descriptor_buffer {
55 static struct kvm_ffa_descriptor_buffer ffa_desc_buf;
57 struct kvm_ffa_buffers {
64 * Note that we don't currently lock these buffers explicitly, instead
65 * relying on the locking of the host FFA buffers as we only have one
68 static struct kvm_ffa_buffers hyp_buffers;
69 static struct kvm_ffa_buffers host_buffers;
71 static void ffa_to_smccc_error(struct arm_smccc_res *res, u64 ffa_errno)
73 *res = (struct arm_smccc_res) {
79 static void ffa_to_smccc_res_prop(struct arm_smccc_res *res, int ret, u64 prop)
81 if (ret == FFA_RET_SUCCESS) {
82 *res = (struct arm_smccc_res) { .a0 = FFA_SUCCESS,
85 ffa_to_smccc_error(res, ret);
89 static void ffa_to_smccc_res(struct arm_smccc_res *res, int ret)
91 ffa_to_smccc_res_prop(res, ret, 0);
94 static void ffa_set_retval(struct kvm_cpu_context *ctxt,
95 struct arm_smccc_res *res)
97 cpu_reg(ctxt, 0) = res->a0;
98 cpu_reg(ctxt, 1) = res->a1;
99 cpu_reg(ctxt, 2) = res->a2;
100 cpu_reg(ctxt, 3) = res->a3;
103 static bool is_ffa_call(u64 func_id)
105 return ARM_SMCCC_IS_FAST_CALL(func_id) &&
106 ARM_SMCCC_OWNER_NUM(func_id) == ARM_SMCCC_OWNER_STANDARD &&
107 ARM_SMCCC_FUNC_NUM(func_id) >= FFA_MIN_FUNC_NUM &&
108 ARM_SMCCC_FUNC_NUM(func_id) <= FFA_MAX_FUNC_NUM;
111 static int ffa_map_hyp_buffers(u64 ffa_page_count)
113 struct arm_smccc_res res;
115 arm_smccc_1_1_smc(FFA_FN64_RXTX_MAP,
116 hyp_virt_to_phys(hyp_buffers.tx),
117 hyp_virt_to_phys(hyp_buffers.rx),
122 return res.a0 == FFA_SUCCESS ? FFA_RET_SUCCESS : res.a2;
125 static int ffa_unmap_hyp_buffers(void)
127 struct arm_smccc_res res;
129 arm_smccc_1_1_smc(FFA_RXTX_UNMAP,
134 return res.a0 == FFA_SUCCESS ? FFA_RET_SUCCESS : res.a2;
137 static void ffa_mem_frag_tx(struct arm_smccc_res *res, u32 handle_lo,
138 u32 handle_hi, u32 fraglen, u32 endpoint_id)
140 arm_smccc_1_1_smc(FFA_MEM_FRAG_TX,
141 handle_lo, handle_hi, fraglen, endpoint_id,
146 static void ffa_mem_frag_rx(struct arm_smccc_res *res, u32 handle_lo,
147 u32 handle_hi, u32 fragoff)
149 arm_smccc_1_1_smc(FFA_MEM_FRAG_RX,
150 handle_lo, handle_hi, fragoff, HOST_FFA_ID,
155 static void ffa_mem_xfer(struct arm_smccc_res *res, u64 func_id, u32 len,
158 arm_smccc_1_1_smc(func_id, len, fraglen,
163 static void ffa_mem_reclaim(struct arm_smccc_res *res, u32 handle_lo,
164 u32 handle_hi, u32 flags)
166 arm_smccc_1_1_smc(FFA_MEM_RECLAIM,
167 handle_lo, handle_hi, flags,
172 static void ffa_retrieve_req(struct arm_smccc_res *res, u32 len)
174 arm_smccc_1_1_smc(FFA_FN64_MEM_RETRIEVE_REQ,
180 static void do_ffa_rxtx_map(struct arm_smccc_res *res,
181 struct kvm_cpu_context *ctxt)
183 DECLARE_REG(phys_addr_t, tx, ctxt, 1);
184 DECLARE_REG(phys_addr_t, rx, ctxt, 2);
185 DECLARE_REG(u32, npages, ctxt, 3);
187 void *rx_virt, *tx_virt;
189 if (npages != (KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) / FFA_PAGE_SIZE) {
190 ret = FFA_RET_INVALID_PARAMETERS;
194 if (!PAGE_ALIGNED(tx) || !PAGE_ALIGNED(rx)) {
195 ret = FFA_RET_INVALID_PARAMETERS;
199 hyp_spin_lock(&host_buffers.lock);
200 if (host_buffers.tx) {
201 ret = FFA_RET_DENIED;
206 * Map our hypervisor buffers into the SPMD before mapping and
207 * pinning the host buffers in our own address space.
209 ret = ffa_map_hyp_buffers(npages);
213 ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(tx));
215 ret = FFA_RET_INVALID_PARAMETERS;
219 ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(rx));
221 ret = FFA_RET_INVALID_PARAMETERS;
225 tx_virt = hyp_phys_to_virt(tx);
226 ret = hyp_pin_shared_mem(tx_virt, tx_virt + 1);
228 ret = FFA_RET_INVALID_PARAMETERS;
232 rx_virt = hyp_phys_to_virt(rx);
233 ret = hyp_pin_shared_mem(rx_virt, rx_virt + 1);
235 ret = FFA_RET_INVALID_PARAMETERS;
239 host_buffers.tx = tx_virt;
240 host_buffers.rx = rx_virt;
243 hyp_spin_unlock(&host_buffers.lock);
245 ffa_to_smccc_res(res, ret);
249 hyp_unpin_shared_mem(tx_virt, tx_virt + 1);
251 __pkvm_host_unshare_hyp(hyp_phys_to_pfn(rx));
253 __pkvm_host_unshare_hyp(hyp_phys_to_pfn(tx));
255 ffa_unmap_hyp_buffers();
259 static void do_ffa_rxtx_unmap(struct arm_smccc_res *res,
260 struct kvm_cpu_context *ctxt)
262 DECLARE_REG(u32, id, ctxt, 1);
265 if (id != HOST_FFA_ID) {
266 ret = FFA_RET_INVALID_PARAMETERS;
270 hyp_spin_lock(&host_buffers.lock);
271 if (!host_buffers.tx) {
272 ret = FFA_RET_INVALID_PARAMETERS;
276 hyp_unpin_shared_mem(host_buffers.tx, host_buffers.tx + 1);
277 WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.tx)));
278 host_buffers.tx = NULL;
280 hyp_unpin_shared_mem(host_buffers.rx, host_buffers.rx + 1);
281 WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.rx)));
282 host_buffers.rx = NULL;
284 ffa_unmap_hyp_buffers();
287 hyp_spin_unlock(&host_buffers.lock);
289 ffa_to_smccc_res(res, ret);
292 static u32 __ffa_host_share_ranges(struct ffa_mem_region_addr_range *ranges,
297 for (i = 0; i < nranges; ++i) {
298 struct ffa_mem_region_addr_range *range = &ranges[i];
299 u64 sz = (u64)range->pg_cnt * FFA_PAGE_SIZE;
300 u64 pfn = hyp_phys_to_pfn(range->address);
302 if (!PAGE_ALIGNED(sz))
305 if (__pkvm_host_share_ffa(pfn, sz / PAGE_SIZE))
312 static u32 __ffa_host_unshare_ranges(struct ffa_mem_region_addr_range *ranges,
317 for (i = 0; i < nranges; ++i) {
318 struct ffa_mem_region_addr_range *range = &ranges[i];
319 u64 sz = (u64)range->pg_cnt * FFA_PAGE_SIZE;
320 u64 pfn = hyp_phys_to_pfn(range->address);
322 if (!PAGE_ALIGNED(sz))
325 if (__pkvm_host_unshare_ffa(pfn, sz / PAGE_SIZE))
332 static int ffa_host_share_ranges(struct ffa_mem_region_addr_range *ranges,
335 u32 nshared = __ffa_host_share_ranges(ranges, nranges);
338 if (nshared != nranges) {
339 WARN_ON(__ffa_host_unshare_ranges(ranges, nshared) != nshared);
340 ret = FFA_RET_DENIED;
346 static int ffa_host_unshare_ranges(struct ffa_mem_region_addr_range *ranges,
349 u32 nunshared = __ffa_host_unshare_ranges(ranges, nranges);
352 if (nunshared != nranges) {
353 WARN_ON(__ffa_host_share_ranges(ranges, nunshared) != nunshared);
354 ret = FFA_RET_DENIED;
360 static void do_ffa_mem_frag_tx(struct arm_smccc_res *res,
361 struct kvm_cpu_context *ctxt)
363 DECLARE_REG(u32, handle_lo, ctxt, 1);
364 DECLARE_REG(u32, handle_hi, ctxt, 2);
365 DECLARE_REG(u32, fraglen, ctxt, 3);
366 DECLARE_REG(u32, endpoint_id, ctxt, 4);
367 struct ffa_mem_region_addr_range *buf;
368 int ret = FFA_RET_INVALID_PARAMETERS;
371 if (fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)
374 if (fraglen % sizeof(*buf))
377 hyp_spin_lock(&host_buffers.lock);
378 if (!host_buffers.tx)
381 buf = hyp_buffers.tx;
382 memcpy(buf, host_buffers.tx, fraglen);
383 nr_ranges = fraglen / sizeof(*buf);
385 ret = ffa_host_share_ranges(buf, nr_ranges);
388 * We're effectively aborting the transaction, so we need
389 * to restore the global state back to what it was prior to
390 * transmission of the first fragment.
392 ffa_mem_reclaim(res, handle_lo, handle_hi, 0);
393 WARN_ON(res->a0 != FFA_SUCCESS);
397 ffa_mem_frag_tx(res, handle_lo, handle_hi, fraglen, endpoint_id);
398 if (res->a0 != FFA_SUCCESS && res->a0 != FFA_MEM_FRAG_RX)
399 WARN_ON(ffa_host_unshare_ranges(buf, nr_ranges));
402 hyp_spin_unlock(&host_buffers.lock);
405 ffa_to_smccc_res(res, ret);
408 * If for any reason this did not succeed, we're in trouble as we have
409 * now lost the content of the previous fragments and we can't rollback
410 * the host stage-2 changes. The pages previously marked as shared will
411 * remain stuck in that state forever, hence preventing the host from
412 * sharing/donating them again and may possibly lead to subsequent
413 * failures, but this will not compromise confidentiality.
418 static __always_inline void do_ffa_mem_xfer(const u64 func_id,
419 struct arm_smccc_res *res,
420 struct kvm_cpu_context *ctxt)
422 DECLARE_REG(u32, len, ctxt, 1);
423 DECLARE_REG(u32, fraglen, ctxt, 2);
424 DECLARE_REG(u64, addr_mbz, ctxt, 3);
425 DECLARE_REG(u32, npages_mbz, ctxt, 4);
426 struct ffa_mem_region_attributes *ep_mem_access;
427 struct ffa_composite_mem_region *reg;
428 struct ffa_mem_region *buf;
429 u32 offset, nr_ranges;
432 BUILD_BUG_ON(func_id != FFA_FN64_MEM_SHARE &&
433 func_id != FFA_FN64_MEM_LEND);
435 if (addr_mbz || npages_mbz || fraglen > len ||
436 fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) {
437 ret = FFA_RET_INVALID_PARAMETERS;
441 if (fraglen < sizeof(struct ffa_mem_region) +
442 sizeof(struct ffa_mem_region_attributes)) {
443 ret = FFA_RET_INVALID_PARAMETERS;
447 hyp_spin_lock(&host_buffers.lock);
448 if (!host_buffers.tx) {
449 ret = FFA_RET_INVALID_PARAMETERS;
453 buf = hyp_buffers.tx;
454 memcpy(buf, host_buffers.tx, fraglen);
456 ep_mem_access = (void *)buf +
457 ffa_mem_desc_offset(buf, 0, FFA_VERSION_1_0);
458 offset = ep_mem_access->composite_off;
459 if (!offset || buf->ep_count != 1 || buf->sender_id != HOST_FFA_ID) {
460 ret = FFA_RET_INVALID_PARAMETERS;
464 if (fraglen < offset + sizeof(struct ffa_composite_mem_region)) {
465 ret = FFA_RET_INVALID_PARAMETERS;
469 reg = (void *)buf + offset;
470 nr_ranges = ((void *)buf + fraglen) - (void *)reg->constituents;
471 if (nr_ranges % sizeof(reg->constituents[0])) {
472 ret = FFA_RET_INVALID_PARAMETERS;
476 nr_ranges /= sizeof(reg->constituents[0]);
477 ret = ffa_host_share_ranges(reg->constituents, nr_ranges);
481 ffa_mem_xfer(res, func_id, len, fraglen);
482 if (fraglen != len) {
483 if (res->a0 != FFA_MEM_FRAG_RX)
486 if (res->a3 != fraglen)
488 } else if (res->a0 != FFA_SUCCESS) {
493 hyp_spin_unlock(&host_buffers.lock);
496 ffa_to_smccc_res(res, ret);
500 WARN_ON(ffa_host_unshare_ranges(reg->constituents, nr_ranges));
504 static void do_ffa_mem_reclaim(struct arm_smccc_res *res,
505 struct kvm_cpu_context *ctxt)
507 DECLARE_REG(u32, handle_lo, ctxt, 1);
508 DECLARE_REG(u32, handle_hi, ctxt, 2);
509 DECLARE_REG(u32, flags, ctxt, 3);
510 struct ffa_mem_region_attributes *ep_mem_access;
511 struct ffa_composite_mem_region *reg;
512 u32 offset, len, fraglen, fragoff;
513 struct ffa_mem_region *buf;
517 handle = PACK_HANDLE(handle_lo, handle_hi);
519 hyp_spin_lock(&host_buffers.lock);
521 buf = hyp_buffers.tx;
522 *buf = (struct ffa_mem_region) {
523 .sender_id = HOST_FFA_ID,
527 ffa_retrieve_req(res, sizeof(*buf));
528 buf = hyp_buffers.rx;
529 if (res->a0 != FFA_MEM_RETRIEVE_RESP)
535 ep_mem_access = (void *)buf +
536 ffa_mem_desc_offset(buf, 0, FFA_VERSION_1_0);
537 offset = ep_mem_access->composite_off;
539 * We can trust the SPMD to get this right, but let's at least
540 * check that we end up with something that doesn't look _completely_
543 if (WARN_ON(offset > len ||
544 fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)) {
545 ret = FFA_RET_ABORTED;
549 if (len > ffa_desc_buf.len) {
550 ret = FFA_RET_NO_MEMORY;
554 buf = ffa_desc_buf.buf;
555 memcpy(buf, hyp_buffers.rx, fraglen);
557 for (fragoff = fraglen; fragoff < len; fragoff += fraglen) {
558 ffa_mem_frag_rx(res, handle_lo, handle_hi, fragoff);
559 if (res->a0 != FFA_MEM_FRAG_TX) {
560 ret = FFA_RET_INVALID_PARAMETERS;
565 memcpy((void *)buf + fragoff, hyp_buffers.rx, fraglen);
568 ffa_mem_reclaim(res, handle_lo, handle_hi, flags);
569 if (res->a0 != FFA_SUCCESS)
572 reg = (void *)buf + offset;
573 /* If the SPMD was happy, then we should be too. */
574 WARN_ON(ffa_host_unshare_ranges(reg->constituents,
575 reg->addr_range_cnt));
577 hyp_spin_unlock(&host_buffers.lock);
580 ffa_to_smccc_res(res, ret);
584 * Is a given FFA function supported, either by forwarding on directly
585 * or by handling at EL2?
587 static bool ffa_call_supported(u64 func_id)
590 /* Unsupported memory management calls */
591 case FFA_FN64_MEM_RETRIEVE_REQ:
592 case FFA_MEM_RETRIEVE_RESP:
593 case FFA_MEM_RELINQUISH:
594 case FFA_MEM_OP_PAUSE:
595 case FFA_MEM_OP_RESUME:
596 case FFA_MEM_FRAG_RX:
597 case FFA_FN64_MEM_DONATE:
598 /* Indirect message passing via RX/TX buffers */
602 /* 32-bit variants of 64-bit calls */
603 case FFA_MSG_SEND_DIRECT_REQ:
604 case FFA_MSG_SEND_DIRECT_RESP:
607 case FFA_MEM_RETRIEVE_REQ:
614 static bool do_ffa_features(struct arm_smccc_res *res,
615 struct kvm_cpu_context *ctxt)
617 DECLARE_REG(u32, id, ctxt, 1);
621 if (!ffa_call_supported(id)) {
622 ret = FFA_RET_NOT_SUPPORTED;
628 case FFA_FN64_MEM_SHARE:
630 case FFA_FN64_MEM_LEND:
631 ret = FFA_RET_SUCCESS;
632 prop = 0; /* No support for dynamic buffers */
639 ffa_to_smccc_res_prop(res, ret, prop);
643 bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id)
645 struct arm_smccc_res res;
648 * There's no way we can tell what a non-standard SMC call might
649 * be up to. Ideally, we would terminate these here and return
650 * an error to the host, but sadly devices make use of custom
651 * firmware calls for things like power management, debugging,
652 * RNG access and crash reporting.
654 * Given that the architecture requires us to trust EL3 anyway,
655 * we forward unrecognised calls on under the assumption that
656 * the firmware doesn't expose a mechanism to access arbitrary
657 * non-secure memory. Short of a per-device table of SMCs, this
658 * is the best we can do.
660 if (!is_ffa_call(func_id))
665 if (!do_ffa_features(&res, host_ctxt))
668 /* Memory management */
669 case FFA_FN64_RXTX_MAP:
670 do_ffa_rxtx_map(&res, host_ctxt);
673 do_ffa_rxtx_unmap(&res, host_ctxt);
676 case FFA_FN64_MEM_SHARE:
677 do_ffa_mem_xfer(FFA_FN64_MEM_SHARE, &res, host_ctxt);
679 case FFA_MEM_RECLAIM:
680 do_ffa_mem_reclaim(&res, host_ctxt);
683 case FFA_FN64_MEM_LEND:
684 do_ffa_mem_xfer(FFA_FN64_MEM_LEND, &res, host_ctxt);
686 case FFA_MEM_FRAG_TX:
687 do_ffa_mem_frag_tx(&res, host_ctxt);
691 if (ffa_call_supported(func_id))
692 return false; /* Pass through */
694 ffa_to_smccc_error(&res, FFA_RET_NOT_SUPPORTED);
696 ffa_set_retval(host_ctxt, &res);
700 int hyp_ffa_init(void *pages)
702 struct arm_smccc_res res;
706 if (kvm_host_psci_config.smccc_version < ARM_SMCCC_VERSION_1_2)
709 arm_smccc_1_1_smc(FFA_VERSION, FFA_VERSION_1_0, 0, 0, 0, 0, 0, 0, &res);
710 if (res.a0 == FFA_RET_NOT_SUPPORTED)
714 * Firmware returns the maximum supported version of the FF-A
715 * implementation. Check that the returned version is
716 * backwards-compatible with the hyp according to the rules in DEN0077A
719 * Of course, things are never simple when dealing with firmware. v1.1
720 * broke ABI with v1.0 on several structures, which is itself
721 * incompatible with the aforementioned versioning scheme. The
722 * expectation is that v1.x implementations that do not support the v1.0
723 * ABI return NOT_SUPPORTED rather than a version number, according to
724 * DEN0077A v1.1 REL0 18.6.4.
726 if (FFA_MAJOR_VERSION(res.a0) != 1)
729 arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res);
730 if (res.a0 != FFA_SUCCESS)
733 if (res.a2 != HOST_FFA_ID)
736 arm_smccc_1_1_smc(FFA_FEATURES, FFA_FN64_RXTX_MAP,
737 0, 0, 0, 0, 0, 0, &res);
738 if (res.a0 != FFA_SUCCESS)
742 case FFA_FEAT_RXTX_MIN_SZ_4K:
745 case FFA_FEAT_RXTX_MIN_SZ_16K:
746 min_rxtx_sz = SZ_16K;
748 case FFA_FEAT_RXTX_MIN_SZ_64K:
749 min_rxtx_sz = SZ_64K;
755 if (min_rxtx_sz > PAGE_SIZE)
759 pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
761 pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
763 ffa_desc_buf = (struct kvm_ffa_descriptor_buffer) {
766 (hyp_ffa_proxy_pages() - (2 * KVM_FFA_MBOX_NR_PAGES)),
769 hyp_buffers = (struct kvm_ffa_buffers) {
770 .lock = __HYP_SPIN_LOCK_UNLOCKED,
775 host_buffers = (struct kvm_ffa_buffers) {
776 .lock = __HYP_SPIN_LOCK_UNLOCKED,