Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 9 | #include "hf/cpu.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 10 | |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 11 | #include <stdalign.h> |
| 12 | |
Max Shvetsov | 9c0ebe4 | 2020-08-27 12:37:57 +0100 | [diff] [blame] | 13 | #include "hf/arch/cache.h" |
| 14 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 15 | #include "hf/api.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 16 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/dlog.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 19 | #include "vmapi/hf/call.h" |
| 20 | |
Maksims Svecovs | 134b8f9 | 2022-03-04 15:14:09 +0000 | [diff] [blame] | 21 | #include "system/sys/cdefs.h" |
| 22 | |
Andrew Scull | eed724b | 2019-09-12 09:57:19 +0100 | [diff] [blame] | 23 | /** |
| 24 | * The stacks to be used by the CPUs. |
| 25 | * |
Kathleen Capella | 4df5520 | 2022-12-01 15:39:26 -0500 | [diff] [blame] | 26 | * Defined in assembly for aarch64 in "src/arch/aarch64/stacks.S." |
| 27 | * Defined for host-based unit tests in "src/cpu_test.cc". |
Andrew Scull | eed724b | 2019-09-12 09:57:19 +0100 | [diff] [blame] | 28 | */ |
Kathleen Capella | 4df5520 | 2022-12-01 15:39:26 -0500 | [diff] [blame] | 29 | |
| 30 | extern char callstacks[MAX_CPUS][STACK_SIZE]; |
Andrew Scull | eed724b | 2019-09-12 09:57:19 +0100 | [diff] [blame] | 31 | |
| 32 | /* NOLINTNEXTLINE(misc-redundant-expression) */ |
| 33 | static_assert((STACK_SIZE % PAGE_SIZE) == 0, "Keep each stack page aligned."); |
| 34 | static_assert((PAGE_SIZE % STACK_ALIGN) == 0, |
| 35 | "Page alignment is too weak for the stack."); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 36 | |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 37 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 38 | * Internal buffer used to store FF-A messages from a VM Tx. Its usage prevents |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 39 | * TOCTOU issues while Hafnium performs actions on information that would |
| 40 | * otherwise be re-writable by the VM. |
| 41 | * |
J-Alves | a39a844 | 2024-04-26 15:41:47 +0100 | [diff] [blame] | 42 | * Each buffer is owned by a single CPU. Can be used when handling FF-A memory |
| 43 | * management ABIs, and FF-A Indirect Messaging. |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 44 | */ |
Andrew Walbran | 0ec6159 | 2019-12-17 13:29:01 +0000 | [diff] [blame] | 45 | alignas(PAGE_SIZE) static uint8_t cpu_message_buffer[MAX_CPUS][PAGE_SIZE]; |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 46 | |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 47 | uint8_t *cpu_get_buffer(struct cpu *c) |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 48 | { |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 49 | size_t cpu_indx = cpu_index(c); |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 50 | |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 51 | CHECK(cpu_indx < MAX_CPUS); |
| 52 | |
| 53 | return cpu_message_buffer[cpu_indx]; |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 56 | uint32_t cpu_get_buffer_size(struct cpu *c) |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 57 | { |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 58 | size_t cpu_indx = cpu_index(c); |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 59 | |
Mahesh Bireddy | 8ca5786 | 2020-01-07 13:43:21 +0530 | [diff] [blame] | 60 | CHECK(cpu_indx < MAX_CPUS); |
| 61 | |
| 62 | return sizeof(cpu_message_buffer[cpu_indx]); |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 65 | /* State of all supported CPUs. The stack of the first one is initialized. */ |
| 66 | struct cpu cpus[MAX_CPUS] = { |
| 67 | { |
| 68 | .is_on = 1, |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 69 | .stack_bottom = &callstacks[0][STACK_SIZE], |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 70 | }, |
| 71 | }; |
| 72 | |
Max Shvetsov | 9c0ebe4 | 2020-08-27 12:37:57 +0100 | [diff] [blame] | 73 | uint32_t cpu_count = 1; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 74 | |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 75 | void cpu_module_init(const cpu_id_t *cpu_ids, size_t count) |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 76 | { |
| 77 | uint32_t i; |
| 78 | uint32_t j; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 79 | cpu_id_t boot_cpu_id = cpus[0].id; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 80 | bool found_boot_cpu = false; |
| 81 | |
| 82 | cpu_count = count; |
| 83 | |
| 84 | /* |
| 85 | * Initialize CPUs with the IDs from the configuration passed in. The |
| 86 | * CPUs after the boot CPU are initialized in reverse order. The boot |
| 87 | * CPU is initialized when it is found or in place of the last CPU if it |
| 88 | * is not found. |
| 89 | */ |
| 90 | j = cpu_count; |
| 91 | for (i = 0; i < cpu_count; ++i) { |
| 92 | struct cpu *c; |
Madhukar Pappireddy | f658f5e | 2024-09-25 14:10:18 -0500 | [diff] [blame^] | 93 | struct timer_pending_vcpu_list *timer_list; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 94 | cpu_id_t id = cpu_ids[i]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 95 | |
| 96 | if (found_boot_cpu || id != boot_cpu_id) { |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 97 | --j; |
| 98 | c = &cpus[j]; |
| 99 | c->stack_bottom = &callstacks[j][STACK_SIZE]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 100 | } else { |
| 101 | found_boot_cpu = true; |
| 102 | c = &cpus[0]; |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 103 | CHECK(c->stack_bottom == &callstacks[0][STACK_SIZE]); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 104 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 105 | |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 106 | sl_init(&c->lock); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 107 | c->id = id; |
Madhukar Pappireddy | f658f5e | 2024-09-25 14:10:18 -0500 | [diff] [blame^] | 108 | |
| 109 | timer_list = &c->pending_timer_vcpus_list; |
| 110 | |
| 111 | /* |
| 112 | * Initialize the list of vCPUs with pending arch timer for |
| 113 | * each CPU. The root entry fields is configured such that |
| 114 | * its `prev` and `next` fields point to itself. |
| 115 | */ |
| 116 | list_init(&(timer_list->root_entry)); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 117 | } |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 118 | |
| 119 | if (!found_boot_cpu) { |
| 120 | /* Boot CPU was initialized but with wrong ID. */ |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 121 | dlog_warning("Boot CPU's ID not found in config.\n"); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 122 | cpus[0].id = boot_cpu_id; |
| 123 | } |
Max Shvetsov | 9c0ebe4 | 2020-08-27 12:37:57 +0100 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Clean the cache for the cpus array such that secondary cores |
| 127 | * hitting the entry point can read the cpus array consistently |
| 128 | * with MMU off (hence data cache off). |
| 129 | */ |
Olivier Deprez | a284617 | 2021-03-23 18:45:41 +0100 | [diff] [blame] | 130 | arch_cache_data_clean_range(va_from_ptr(cpus), sizeof(cpus)); |
Max Shvetsov | 9c0ebe4 | 2020-08-27 12:37:57 +0100 | [diff] [blame] | 131 | |
Olivier Deprez | a284617 | 2021-03-23 18:45:41 +0100 | [diff] [blame] | 132 | arch_cache_data_clean_range(va_from_ptr(&cpu_count), sizeof(cpu_count)); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | size_t cpu_index(struct cpu *c) |
| 136 | { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 137 | return c - cpus; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Olivier Deprez | 7d5e553 | 2020-09-22 15:06:58 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Return cpu with the given index. |
| 142 | */ |
| 143 | struct cpu *cpu_find_index(size_t index) |
| 144 | { |
| 145 | return (index < MAX_CPUS) ? &cpus[index] : NULL; |
| 146 | } |
| 147 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 148 | /** |
| 149 | * Turns CPU on and returns the previous state. |
| 150 | */ |
Olivier Deprez | 70f8a4a | 2022-09-26 09:17:56 +0200 | [diff] [blame] | 151 | bool cpu_on(struct cpu *c) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 152 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 153 | bool prev; |
| 154 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 155 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 156 | prev = c->is_on; |
| 157 | c->is_on = true; |
| 158 | sl_unlock(&c->lock); |
| 159 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 160 | return prev; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 163 | /** |
| 164 | * Prepares the CPU for turning itself off. |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 165 | */ |
| 166 | void cpu_off(struct cpu *c) |
| 167 | { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 168 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 169 | c->is_on = false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 170 | sl_unlock(&c->lock); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 171 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 172 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 173 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 174 | * Searches for a CPU based on its ID. |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 175 | */ |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 176 | struct cpu *cpu_find(cpu_id_t id) |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 177 | { |
| 178 | size_t i; |
| 179 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 180 | for (i = 0; i < cpu_count; i++) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 181 | if (cpus[i].id == id) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 182 | return &cpus[i]; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | return NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 187 | } |