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_composite_mem_region *reg;
427 struct ffa_mem_region *buf;
428 u32 offset, nr_ranges;
431 BUILD_BUG_ON(func_id != FFA_FN64_MEM_SHARE &&
432 func_id != FFA_FN64_MEM_LEND);
434 if (addr_mbz || npages_mbz || fraglen > len ||
435 fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) {
436 ret = FFA_RET_INVALID_PARAMETERS;
440 if (fraglen < sizeof(struct ffa_mem_region) +
441 sizeof(struct ffa_mem_region_attributes)) {
442 ret = FFA_RET_INVALID_PARAMETERS;
446 hyp_spin_lock(&host_buffers.lock);
447 if (!host_buffers.tx) {
448 ret = FFA_RET_INVALID_PARAMETERS;
452 buf = hyp_buffers.tx;
453 memcpy(buf, host_buffers.tx, fraglen);
455 offset = buf->ep_mem_access[0].composite_off;
456 if (!offset || buf->ep_count != 1 || buf->sender_id != HOST_FFA_ID) {
457 ret = FFA_RET_INVALID_PARAMETERS;
461 if (fraglen < offset + sizeof(struct ffa_composite_mem_region)) {
462 ret = FFA_RET_INVALID_PARAMETERS;
466 reg = (void *)buf + offset;
467 nr_ranges = ((void *)buf + fraglen) - (void *)reg->constituents;
468 if (nr_ranges % sizeof(reg->constituents[0])) {
469 ret = FFA_RET_INVALID_PARAMETERS;
473 nr_ranges /= sizeof(reg->constituents[0]);
474 ret = ffa_host_share_ranges(reg->constituents, nr_ranges);
478 ffa_mem_xfer(res, func_id, len, fraglen);
479 if (fraglen != len) {
480 if (res->a0 != FFA_MEM_FRAG_RX)
483 if (res->a3 != fraglen)
485 } else if (res->a0 != FFA_SUCCESS) {
490 hyp_spin_unlock(&host_buffers.lock);
493 ffa_to_smccc_res(res, ret);
497 WARN_ON(ffa_host_unshare_ranges(reg->constituents, nr_ranges));
501 static void do_ffa_mem_reclaim(struct arm_smccc_res *res,
502 struct kvm_cpu_context *ctxt)
504 DECLARE_REG(u32, handle_lo, ctxt, 1);
505 DECLARE_REG(u32, handle_hi, ctxt, 2);
506 DECLARE_REG(u32, flags, ctxt, 3);
507 struct ffa_composite_mem_region *reg;
508 u32 offset, len, fraglen, fragoff;
509 struct ffa_mem_region *buf;
513 handle = PACK_HANDLE(handle_lo, handle_hi);
515 hyp_spin_lock(&host_buffers.lock);
517 buf = hyp_buffers.tx;
518 *buf = (struct ffa_mem_region) {
519 .sender_id = HOST_FFA_ID,
523 ffa_retrieve_req(res, sizeof(*buf));
524 buf = hyp_buffers.rx;
525 if (res->a0 != FFA_MEM_RETRIEVE_RESP)
531 offset = buf->ep_mem_access[0].composite_off;
533 * We can trust the SPMD to get this right, but let's at least
534 * check that we end up with something that doesn't look _completely_
537 if (WARN_ON(offset > len ||
538 fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)) {
539 ret = FFA_RET_ABORTED;
543 if (len > ffa_desc_buf.len) {
544 ret = FFA_RET_NO_MEMORY;
548 buf = ffa_desc_buf.buf;
549 memcpy(buf, hyp_buffers.rx, fraglen);
551 for (fragoff = fraglen; fragoff < len; fragoff += fraglen) {
552 ffa_mem_frag_rx(res, handle_lo, handle_hi, fragoff);
553 if (res->a0 != FFA_MEM_FRAG_TX) {
554 ret = FFA_RET_INVALID_PARAMETERS;
559 memcpy((void *)buf + fragoff, hyp_buffers.rx, fraglen);
562 ffa_mem_reclaim(res, handle_lo, handle_hi, flags);
563 if (res->a0 != FFA_SUCCESS)
566 reg = (void *)buf + offset;
567 /* If the SPMD was happy, then we should be too. */
568 WARN_ON(ffa_host_unshare_ranges(reg->constituents,
569 reg->addr_range_cnt));
571 hyp_spin_unlock(&host_buffers.lock);
574 ffa_to_smccc_res(res, ret);
578 * Is a given FFA function supported, either by forwarding on directly
579 * or by handling at EL2?
581 static bool ffa_call_supported(u64 func_id)
584 /* Unsupported memory management calls */
585 case FFA_FN64_MEM_RETRIEVE_REQ:
586 case FFA_MEM_RETRIEVE_RESP:
587 case FFA_MEM_RELINQUISH:
588 case FFA_MEM_OP_PAUSE:
589 case FFA_MEM_OP_RESUME:
590 case FFA_MEM_FRAG_RX:
591 case FFA_FN64_MEM_DONATE:
592 /* Indirect message passing via RX/TX buffers */
596 /* 32-bit variants of 64-bit calls */
597 case FFA_MSG_SEND_DIRECT_REQ:
598 case FFA_MSG_SEND_DIRECT_RESP:
601 case FFA_MEM_RETRIEVE_REQ:
608 static bool do_ffa_features(struct arm_smccc_res *res,
609 struct kvm_cpu_context *ctxt)
611 DECLARE_REG(u32, id, ctxt, 1);
615 if (!ffa_call_supported(id)) {
616 ret = FFA_RET_NOT_SUPPORTED;
622 case FFA_FN64_MEM_SHARE:
624 case FFA_FN64_MEM_LEND:
625 ret = FFA_RET_SUCCESS;
626 prop = 0; /* No support for dynamic buffers */
633 ffa_to_smccc_res_prop(res, ret, prop);
637 bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id)
639 struct arm_smccc_res res;
642 * There's no way we can tell what a non-standard SMC call might
643 * be up to. Ideally, we would terminate these here and return
644 * an error to the host, but sadly devices make use of custom
645 * firmware calls for things like power management, debugging,
646 * RNG access and crash reporting.
648 * Given that the architecture requires us to trust EL3 anyway,
649 * we forward unrecognised calls on under the assumption that
650 * the firmware doesn't expose a mechanism to access arbitrary
651 * non-secure memory. Short of a per-device table of SMCs, this
652 * is the best we can do.
654 if (!is_ffa_call(func_id))
659 if (!do_ffa_features(&res, host_ctxt))
662 /* Memory management */
663 case FFA_FN64_RXTX_MAP:
664 do_ffa_rxtx_map(&res, host_ctxt);
667 do_ffa_rxtx_unmap(&res, host_ctxt);
670 case FFA_FN64_MEM_SHARE:
671 do_ffa_mem_xfer(FFA_FN64_MEM_SHARE, &res, host_ctxt);
673 case FFA_MEM_RECLAIM:
674 do_ffa_mem_reclaim(&res, host_ctxt);
677 case FFA_FN64_MEM_LEND:
678 do_ffa_mem_xfer(FFA_FN64_MEM_LEND, &res, host_ctxt);
680 case FFA_MEM_FRAG_TX:
681 do_ffa_mem_frag_tx(&res, host_ctxt);
685 if (ffa_call_supported(func_id))
686 return false; /* Pass through */
688 ffa_to_smccc_error(&res, FFA_RET_NOT_SUPPORTED);
690 ffa_set_retval(host_ctxt, &res);
694 int hyp_ffa_init(void *pages)
696 struct arm_smccc_res res;
700 if (kvm_host_psci_config.smccc_version < ARM_SMCCC_VERSION_1_2)
703 arm_smccc_1_1_smc(FFA_VERSION, FFA_VERSION_1_0, 0, 0, 0, 0, 0, 0, &res);
704 if (res.a0 == FFA_RET_NOT_SUPPORTED)
708 * Firmware returns the maximum supported version of the FF-A
709 * implementation. Check that the returned version is
710 * backwards-compatible with the hyp according to the rules in DEN0077A
713 * Of course, things are never simple when dealing with firmware. v1.1
714 * broke ABI with v1.0 on several structures, which is itself
715 * incompatible with the aforementioned versioning scheme. The
716 * expectation is that v1.x implementations that do not support the v1.0
717 * ABI return NOT_SUPPORTED rather than a version number, according to
718 * DEN0077A v1.1 REL0 18.6.4.
720 if (FFA_MAJOR_VERSION(res.a0) != 1)
723 arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res);
724 if (res.a0 != FFA_SUCCESS)
727 if (res.a2 != HOST_FFA_ID)
730 arm_smccc_1_1_smc(FFA_FEATURES, FFA_FN64_RXTX_MAP,
731 0, 0, 0, 0, 0, 0, &res);
732 if (res.a0 != FFA_SUCCESS)
736 case FFA_FEAT_RXTX_MIN_SZ_4K:
739 case FFA_FEAT_RXTX_MIN_SZ_16K:
740 min_rxtx_sz = SZ_16K;
742 case FFA_FEAT_RXTX_MIN_SZ_64K:
743 min_rxtx_sz = SZ_64K;
749 if (min_rxtx_sz > PAGE_SIZE)
753 pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
755 pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
757 ffa_desc_buf = (struct kvm_ffa_descriptor_buffer) {
760 (hyp_ffa_proxy_pages() - (2 * KVM_FFA_MBOX_NR_PAGES)),
763 hyp_buffers = (struct kvm_ffa_buffers) {
764 .lock = __HYP_SPIN_LOCK_UNLOCKED,
769 host_buffers = (struct kvm_ffa_buffers) {
770 .lock = __HYP_SPIN_LOCK_UNLOCKED,