Leo Yan | 2ae197a | 2024-05-16 15:59:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2025, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | |
| 9 | #include <bl31/sync_handle.h> |
| 10 | #include <context.h> |
| 11 | #include <plat/common/plat_trng.h> |
| 12 | |
| 13 | #define XZR_REG_NUM 31 |
| 14 | |
| 15 | |
| 16 | int plat_handle_rng_trap(uint64_t esr_el3, cpu_context_t *ctx) |
| 17 | { |
| 18 | uint64_t entropy; |
| 19 | |
| 20 | /* extract the target register number from the exception syndrome */ |
| 21 | unsigned int rt = get_sysreg_iss_rt(esr_el3); |
| 22 | |
| 23 | /* ignore XZR accesses and writes to the register */ |
| 24 | assert(rt != XZR_REG_NUM && !is_sysreg_iss_write(esr_el3)); |
| 25 | |
| 26 | if (!plat_get_entropy(&entropy)) { |
| 27 | ERROR("Failed to get entropy\n"); |
| 28 | panic(); |
| 29 | } |
| 30 | |
| 31 | /* Emulate RNDR and RNDRRS */ |
| 32 | gp_regs_t *gpregs = get_gpregs_ctx(ctx); |
| 33 | |
| 34 | write_ctx_reg(gpregs, rt, entropy); |
| 35 | |
| 36 | /* |
| 37 | * We successfully handled the trap, continue with the next |
| 38 | * instruction. |
| 39 | */ |
| 40 | return TRAP_RET_CONTINUE; |
| 41 | } |