Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <debug.h> |
| 9 | #include <mm_svc.h> |
| 10 | #include <secure_partition.h> |
| 11 | #include <sp_helpers.h> |
| 12 | #include <spm_svc.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * Handle a fast secure service request, i.e. one made through an MM_COMMUNICATE |
| 18 | * call. |
| 19 | * |
| 20 | * cc |
| 21 | * Calling convention. If MM_COMMUNICATE has been invoked using the SMC32 |
| 22 | * calling convention, this argument must be 32, else 64. |
| 23 | * |
| 24 | * sps |
| 25 | * Communication buffer attached to the secure partition service request. |
| 26 | */ |
| 27 | static int32_t cactus_handle_fast_request(int cc, |
| 28 | secure_partition_request_info_t *sps) |
| 29 | { |
| 30 | assert(cc == 32 || cc == 64); |
| 31 | |
| 32 | /* No SMC32 is supported at the moment. Just ignore them. */ |
| 33 | if (cc == 32) { |
| 34 | INFO("Ignoring MM_COMMUNICATE_AARCH32 call\n"); |
| 35 | return SPM_SUCCESS; |
| 36 | } |
| 37 | |
| 38 | /* See secure_partition.h for possible ID values. */ |
| 39 | switch (sps->id) { |
| 40 | case SPS_TIMER_SLEEP: { |
| 41 | if (sps->data_size != 1) { |
| 42 | ERROR("Invalid payload size for SPM_SPS_TIMER_SLEEP request (%llu)\n", |
| 43 | sps->data_size); |
| 44 | return SPM_INVALID_PARAMETER; |
| 45 | } |
Antonio Nino Diaz | 81d507e | 2019-03-21 13:49:23 +0000 | [diff] [blame] | 46 | uint32_t duration_sec = sps->data[0]; |
| 47 | sp_sleep(duration_sec * 1000); |
Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Write back to the communication buffer to acknowledge the |
| 51 | * request has been successfully handled. |
| 52 | */ |
| 53 | uint32_t response = CACTUS_FAST_REQUEST_SUCCESS; |
| 54 | memcpy(sps->data, &response, sizeof(response)); |
| 55 | return SPM_SUCCESS; |
| 56 | } |
| 57 | |
| 58 | case SPS_CHECK_ALIVE: |
| 59 | return SPM_SUCCESS; |
| 60 | |
| 61 | default: |
| 62 | INFO("Unsupported MM_COMMUNICATE_AARCH64 call with service ID 0x%x, ignoring it\n", |
| 63 | sps->id); |
| 64 | return SPM_INVALID_PARAMETER; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | __dead2 void secure_services_loop(void) |
| 69 | { |
| 70 | int32_t event_status_code; |
| 71 | svc_args svc_values = { 0 }; |
| 72 | |
| 73 | /* |
| 74 | * The first time this loop is executed corresponds to when Cactus has |
| 75 | * finished initialising its run time environment and is ready to handle |
| 76 | * secure service requests. |
| 77 | */ |
| 78 | NOTICE("Cactus: Signal end of init to SPM\n"); |
| 79 | event_status_code = SPM_SUCCESS; |
| 80 | |
| 81 | while (1) { |
| 82 | svc_values.fid = SP_EVENT_COMPLETE_AARCH64; |
| 83 | svc_values.arg1 = event_status_code; |
| 84 | int32_t event_id = sp_svc(&svc_values); |
| 85 | |
| 86 | switch (event_id) { |
| 87 | case MM_COMMUNICATE_AARCH64: |
| 88 | { |
| 89 | uint64_t ctx_addr = svc_values.arg1; |
| 90 | uint32_t ctx_size = svc_values.arg2; |
| 91 | uint64_t cookie = svc_values.arg3; |
| 92 | |
J-Alves | 66ee632 | 2024-05-07 14:44:56 +0100 | [diff] [blame] | 93 | (void) cookie; |
| 94 | (void) ctx_size; |
| 95 | |
| 96 | VERBOSE("Cactus: Received MM_COMMUNICATE_AARCH64 call\n"); |
| 97 | VERBOSE("Cactus: Context address: 0x%llx\n", ctx_addr); |
| 98 | VERBOSE("Cactus: Context size : %u\n", ctx_size); |
| 99 | VERBOSE("Cactus: Cookie : 0x%llx\n", cookie); |
Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 100 | |
| 101 | if (ctx_addr == 0) { |
| 102 | ERROR("Context address is invalid\n"); |
| 103 | event_status_code = SPM_INVALID_PARAMETER; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | secure_partition_request_info_t *sps = (void *)(uintptr_t) ctx_addr; |
J-Alves | 66ee632 | 2024-05-07 14:44:56 +0100 | [diff] [blame] | 108 | INFO("Received fast secure service request with ID #%u\n", |
Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 109 | sps->id); |
| 110 | event_status_code = cactus_handle_fast_request(64, sps); |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | case MM_COMMUNICATE_AARCH32: |
| 115 | { |
| 116 | uint32_t ctx_addr = svc_values.arg1; |
| 117 | uint32_t ctx_size = svc_values.arg2; |
| 118 | uint32_t cookie = svc_values.arg3; |
| 119 | |
J-Alves | 66ee632 | 2024-05-07 14:44:56 +0100 | [diff] [blame] | 120 | (void) cookie; |
| 121 | (void) ctx_size; |
| 122 | |
| 123 | VERBOSE("Cactus: Received MM_COMMUNICATE_AARCH32 call\n"); |
| 124 | VERBOSE("Cactus: Context address: 0x%x\n", ctx_addr); |
| 125 | VERBOSE("Cactus: Context size : %u\n", ctx_size); |
| 126 | VERBOSE("Cactus: Cookie : 0x%x\n", cookie); |
Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 127 | |
| 128 | if (ctx_addr == 0) { |
| 129 | ERROR("Context address is invalid\n"); |
| 130 | event_status_code = SPM_INVALID_PARAMETER; |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | secure_partition_request_info_t *sps = (void *)(uintptr_t) ctx_addr; |
J-Alves | 66ee632 | 2024-05-07 14:44:56 +0100 | [diff] [blame] | 135 | INFO("Received fast secure service request with ID #%u\n", |
| 136 | sps->id); |
Antonio Nino Diaz | f2218e7 | 2019-03-19 10:59:11 +0000 | [diff] [blame] | 137 | event_status_code = cactus_handle_fast_request(32, sps); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | default: |
| 142 | NOTICE("Unhandled Service ID 0x%x\n", event_id); |
| 143 | event_status_code = SPM_NOT_SUPPORTED; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |