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 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/cpu.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 19 | #include <stdalign.h> |
| 20 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 21 | #include "hf/api.h" |
Andrew Scull | 877ae4b | 2019-07-02 12:52:33 +0100 | [diff] [blame] | 22 | #include "hf/check.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 23 | #include "hf/dlog.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 24 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 25 | #include "vmapi/hf/call.h" |
| 26 | |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 27 | #define STACK_SIZE PAGE_SIZE |
| 28 | |
Andrew Scull | eed724b | 2019-09-12 09:57:19 +0100 | [diff] [blame] | 29 | /** |
| 30 | * The stacks to be used by the CPUs. |
| 31 | * |
| 32 | * Align to page boundaries to ensure that cache lines are not shared between a |
| 33 | * CPU's stack and data that can be accessed from other CPUs. If this did |
| 34 | * happen, there may be coherency problems when the stack is being used before |
| 35 | * caching is enabled. |
| 36 | */ |
| 37 | alignas(PAGE_SIZE) static char callstacks[MAX_CPUS][STACK_SIZE]; |
| 38 | |
| 39 | /* NOLINTNEXTLINE(misc-redundant-expression) */ |
| 40 | static_assert((STACK_SIZE % PAGE_SIZE) == 0, "Keep each stack page aligned."); |
| 41 | static_assert((PAGE_SIZE % STACK_ALIGN) == 0, |
| 42 | "Page alignment is too weak for the stack."); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 43 | |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 44 | /** |
| 45 | * Internal buffer used to store SPCI messages from a VM Tx. Its usage prevents |
| 46 | * TOCTOU issues while Hafnium performs actions on information that would |
| 47 | * otherwise be re-writable by the VM. |
| 48 | * |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 49 | * Each buffer is owned by a single CPU. The buffer can only be used for |
Jose Marinho | 20713fa | 2019-08-07 15:42:07 +0100 | [diff] [blame] | 50 | * spci_msg_send. The information stored in the buffer is only valid during the |
| 51 | * spci_msg_send request is performed. |
| 52 | */ |
Andrew Walbran | 0ec6159 | 2019-12-17 13:29:01 +0000 | [diff] [blame] | 53 | 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] | 54 | |
| 55 | uint8_t *cpu_get_buffer(cpu_id_t cpu_id) |
| 56 | { |
| 57 | CHECK(cpu_id < MAX_CPUS); |
| 58 | |
| 59 | return cpu_message_buffer[cpu_id]; |
| 60 | } |
| 61 | |
| 62 | uint32_t cpu_get_buffer_size(cpu_id_t cpu_id) |
| 63 | { |
| 64 | CHECK(cpu_id < MAX_CPUS); |
| 65 | |
| 66 | return sizeof(cpu_message_buffer[cpu_id]); |
| 67 | } |
| 68 | |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 69 | /* State of all supported CPUs. The stack of the first one is initialized. */ |
| 70 | struct cpu cpus[MAX_CPUS] = { |
| 71 | { |
| 72 | .is_on = 1, |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 73 | .stack_bottom = &callstacks[0][STACK_SIZE], |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 74 | }, |
| 75 | }; |
| 76 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 77 | static uint32_t cpu_count = 1; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 78 | |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | uint32_t i; |
| 82 | uint32_t j; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 83 | cpu_id_t boot_cpu_id = cpus[0].id; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 84 | bool found_boot_cpu = false; |
| 85 | |
| 86 | cpu_count = count; |
| 87 | |
| 88 | /* |
| 89 | * Initialize CPUs with the IDs from the configuration passed in. The |
| 90 | * CPUs after the boot CPU are initialized in reverse order. The boot |
| 91 | * CPU is initialized when it is found or in place of the last CPU if it |
| 92 | * is not found. |
| 93 | */ |
| 94 | j = cpu_count; |
| 95 | for (i = 0; i < cpu_count; ++i) { |
| 96 | struct cpu *c; |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 97 | cpu_id_t id = cpu_ids[i]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 98 | |
| 99 | if (found_boot_cpu || id != boot_cpu_id) { |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 100 | --j; |
| 101 | c = &cpus[j]; |
| 102 | c->stack_bottom = &callstacks[j][STACK_SIZE]; |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 103 | } else { |
| 104 | found_boot_cpu = true; |
| 105 | c = &cpus[0]; |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 106 | CHECK(c->stack_bottom == &callstacks[0][STACK_SIZE]); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 107 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 108 | |
Andrew Scull | 4897398 | 2019-08-16 17:40:28 +0100 | [diff] [blame] | 109 | sl_init(&c->lock); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 110 | c->id = id; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 111 | } |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 112 | |
| 113 | if (!found_boot_cpu) { |
| 114 | /* Boot CPU was initialized but with wrong ID. */ |
David Brazdil | 7d85345 | 2019-09-12 10:07:36 +0100 | [diff] [blame] | 115 | dlog("Boot CPU's ID not found in config.\n"); |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 116 | cpus[0].id = boot_cpu_id; |
| 117 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | size_t cpu_index(struct cpu *c) |
| 121 | { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 122 | return c - cpus; |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 125 | /** |
| 126 | * Turns CPU on and returns the previous state. |
| 127 | */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 128 | bool cpu_on(struct cpu *c, ipaddr_t entry, uintreg_t arg) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 129 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 130 | bool prev; |
| 131 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 132 | sl_lock(&c->lock); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 133 | prev = c->is_on; |
| 134 | c->is_on = true; |
| 135 | sl_unlock(&c->lock); |
| 136 | |
| 137 | if (!prev) { |
Andrew Walbran | 42347a9 | 2019-05-09 13:59:03 +0100 | [diff] [blame] | 138 | struct vm *vm = vm_find(HF_PRIMARY_VM_ID); |
Andrew Walbran | e1310df | 2019-04-29 17:28:28 +0100 | [diff] [blame] | 139 | struct vcpu *vcpu = vm_get_vcpu(vm, cpu_index(c)); |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 140 | struct vcpu_locked vcpu_locked; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 141 | |
Andrew Walbran | b58f899 | 2019-04-15 12:29:31 +0100 | [diff] [blame] | 142 | vcpu_locked = vcpu_lock(vcpu); |
| 143 | vcpu_on(vcpu_locked, entry, arg); |
| 144 | vcpu_unlock(&vcpu_locked); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 145 | } |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 146 | |
| 147 | return prev; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 150 | /** |
| 151 | * Prepares the CPU for turning itself off. |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 152 | */ |
| 153 | void cpu_off(struct cpu *c) |
| 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 | c->is_on = false; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 157 | sl_unlock(&c->lock); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 158 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 159 | |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 160 | /** |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 161 | * Searches for a CPU based on its ID. |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 162 | */ |
Andrew Walbran | 4d3fa28 | 2019-06-26 13:31:15 +0100 | [diff] [blame] | 163 | struct cpu *cpu_find(cpu_id_t id) |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 164 | { |
| 165 | size_t i; |
| 166 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 167 | for (i = 0; i < cpu_count; i++) { |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 168 | if (cpus[i].id == id) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 169 | return &cpus[i]; |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | return NULL; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 174 | } |