Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Walbran | 13c3a0b | 2018-11-30 11:51:53 +0000 | [diff] [blame] | 2 | /* |
Andrew Walbran | 2bc0a32 | 2019-03-07 15:48:06 +0000 | [diff] [blame] | 3 | * Copyright 2018 The Hafnium Authors. |
Andrew Walbran | 13c3a0b | 2018-11-30 11:51:53 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Andrew Walbran | 13c3a0b | 2018-11-30 11:51:53 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 15 | #include <clocksource/arm_arch_timer.h> |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 17 | #include <linux/cpuhotplug.h> |
| 18 | #include <linux/hrtimer.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 19 | #include <linux/init.h> |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/kthread.h> |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 24 | #include <linux/mm.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 25 | #include <linux/module.h> |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 26 | #include <linux/net.h> |
| 27 | #include <linux/of.h> |
| 28 | #include <linux/platform_device.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 29 | #include <linux/sched/task.h> |
| 30 | #include <linux/slab.h> |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 31 | #include <net/sock.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 32 | |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 33 | #include <hf/call.h> |
Jose Marinho | 1cc6c75 | 2019-03-11 16:28:03 +0000 | [diff] [blame] | 34 | #include <hf/spci.h> |
Fuad Tabba | 3e669bc | 2019-08-08 16:43:55 +0100 | [diff] [blame] | 35 | #include <hf/transport.h> |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 36 | |
Fuad Tabba | 3e669bc | 2019-08-08 16:43:55 +0100 | [diff] [blame] | 37 | #include "uapi/hf/socket.h" |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 38 | |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 39 | #define HYPERVISOR_TIMER_NAME "el2_timer" |
| 40 | |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 41 | #define CONFIG_HAFNIUM_MAX_VMS 16 |
| 42 | #define CONFIG_HAFNIUM_MAX_VCPUS 32 |
| 43 | |
Fuad Tabba | 5da4b6b | 2019-08-05 13:56:20 +0100 | [diff] [blame] | 44 | #define FIRST_SECONDARY_VM_ID (HF_VM_ID_OFFSET + 1) |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 45 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 46 | struct hf_vcpu { |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 47 | struct hf_vm *vm; |
Andrew Walbran | ddc3394 | 2019-06-25 18:32:02 +0100 | [diff] [blame] | 48 | spci_vcpu_index_t vcpu_index; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 49 | struct task_struct *task; |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 50 | atomic_t abort_sleep; |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 51 | atomic_t waiting_for_message; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 52 | struct hrtimer timer; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct hf_vm { |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 56 | spci_vm_id_t id; |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 57 | spci_vcpu_count_t vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 58 | struct hf_vcpu *vcpu; |
| 59 | }; |
| 60 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 61 | struct hf_sock { |
| 62 | /* This needs to be the first field. */ |
| 63 | struct sock sk; |
| 64 | |
| 65 | /* |
| 66 | * The following fields are immutable after the socket transitions to |
| 67 | * SS_CONNECTED state. |
| 68 | */ |
| 69 | uint64_t local_port; |
| 70 | uint64_t remote_port; |
| 71 | struct hf_vm *peer_vm; |
| 72 | }; |
| 73 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 74 | static struct proto hf_sock_proto = { |
| 75 | .name = "hafnium", |
| 76 | .owner = THIS_MODULE, |
| 77 | .obj_size = sizeof(struct hf_sock), |
| 78 | }; |
| 79 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 80 | static struct hf_vm *hf_vms; |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 81 | static spci_vm_count_t hf_vm_count; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 82 | static struct page *hf_send_page; |
| 83 | static struct page *hf_recv_page; |
| 84 | static atomic64_t hf_next_port = ATOMIC64_INIT(0); |
| 85 | static DEFINE_SPINLOCK(hf_send_lock); |
| 86 | static DEFINE_HASHTABLE(hf_local_port_hash, 7); |
| 87 | static DEFINE_SPINLOCK(hf_local_port_hash_lock); |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 88 | static int hf_irq; |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 89 | static enum cpuhp_state hf_cpuhp_state; |
Jose Marinho | 1cc6c75 | 2019-03-11 16:28:03 +0000 | [diff] [blame] | 90 | static spci_vm_id_t current_vm_id; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 91 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 92 | /** |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 93 | * Retrieves a VM from its ID, returning NULL if the VM doesn't exist. |
| 94 | */ |
Andrew Walbran | 01390ae | 2019-06-19 11:32:46 +0100 | [diff] [blame] | 95 | static struct hf_vm *hf_vm_from_id(spci_vm_id_t vm_id) |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 96 | { |
| 97 | if (vm_id < FIRST_SECONDARY_VM_ID || |
| 98 | vm_id >= FIRST_SECONDARY_VM_ID + hf_vm_count) |
| 99 | return NULL; |
| 100 | |
| 101 | return &hf_vms[vm_id - FIRST_SECONDARY_VM_ID]; |
| 102 | } |
| 103 | |
| 104 | /** |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 105 | * Wakes up the kernel thread responsible for running the given vcpu. |
| 106 | * |
| 107 | * Returns 0 if the thread was already running, 1 otherwise. |
| 108 | */ |
| 109 | static int hf_vcpu_wake_up(struct hf_vcpu *vcpu) |
| 110 | { |
| 111 | /* Set a flag indicating that the thread should not go to sleep. */ |
| 112 | atomic_set(&vcpu->abort_sleep, 1); |
| 113 | |
| 114 | /* Set the thread to running state. */ |
| 115 | return wake_up_process(vcpu->task); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Puts the current thread to sleep. The current thread must be responsible for |
| 120 | * running the given vcpu. |
| 121 | * |
| 122 | * Going to sleep will fail if hf_vcpu_wake_up() or kthread_stop() was called on |
| 123 | * this vcpu/thread since the last time it [re]started running. |
| 124 | */ |
| 125 | static void hf_vcpu_sleep(struct hf_vcpu *vcpu) |
| 126 | { |
| 127 | int abort; |
| 128 | |
| 129 | set_current_state(TASK_INTERRUPTIBLE); |
| 130 | |
| 131 | /* Check the sleep-abort flag after making thread interruptible. */ |
| 132 | abort = atomic_read(&vcpu->abort_sleep); |
| 133 | if (!abort && !kthread_should_stop()) |
| 134 | schedule(); |
| 135 | |
| 136 | /* Set state back to running on the way out. */ |
| 137 | set_current_state(TASK_RUNNING); |
| 138 | } |
| 139 | |
| 140 | /** |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 141 | * Wakes up the thread associated with the vcpu that owns the given timer. This |
| 142 | * is called when the timer the thread is waiting on expires. |
| 143 | */ |
| 144 | static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer) |
| 145 | { |
| 146 | struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer); |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 147 | /* TODO: Inject interrupt. */ |
| 148 | hf_vcpu_wake_up(vcpu); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 149 | return HRTIMER_NORESTART; |
| 150 | } |
| 151 | |
| 152 | /** |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 153 | * This function is called when Hafnium requests that the primary VM wake up a |
| 154 | * vCPU that belongs to a secondary VM. |
| 155 | * |
| 156 | * It wakes up the thread if it's sleeping, or kicks it if it's already running. |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 157 | */ |
Andrew Walbran | ddc3394 | 2019-06-25 18:32:02 +0100 | [diff] [blame] | 158 | static void hf_handle_wake_up_request(spci_vm_id_t vm_id, |
| 159 | spci_vcpu_index_t vcpu) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 160 | { |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 161 | struct hf_vm *vm = hf_vm_from_id(vm_id); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 162 | |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 163 | if (!vm) { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 164 | pr_warn("Request to wake up non-existent VM id: %u\n", vm_id); |
| 165 | return; |
| 166 | } |
| 167 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 168 | if (vcpu >= vm->vcpu_count) { |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 169 | pr_warn("Request to wake up non-existent vCPU: %u.%u\n", |
| 170 | vm_id, vcpu); |
| 171 | return; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) { |
| 175 | /* |
| 176 | * The task was already running (presumably on a different |
| 177 | * physical CPU); interrupt it. This gives Hafnium a chance to |
| 178 | * inject any new interrupts. |
| 179 | */ |
| 180 | kick_process(vm->vcpu[vcpu].task); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 185 | * Injects an interrupt into a vCPU of the VM and ensures the vCPU will run to |
| 186 | * handle the interrupt. |
| 187 | */ |
Andrew Walbran | 01390ae | 2019-06-19 11:32:46 +0100 | [diff] [blame] | 188 | static void hf_interrupt_vm(spci_vm_id_t vm_id, uint64_t int_id) |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 189 | { |
| 190 | struct hf_vm *vm = hf_vm_from_id(vm_id); |
Andrew Walbran | ddc3394 | 2019-06-25 18:32:02 +0100 | [diff] [blame] | 191 | spci_vcpu_index_t vcpu; |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 192 | int64_t ret; |
| 193 | |
| 194 | if (!vm) { |
| 195 | pr_warn("Request to wake up non-existent VM id: %u\n", vm_id); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * TODO: For now we're picking the first vcpu to interrupt, but |
| 201 | * we want to be smarter. |
| 202 | */ |
| 203 | vcpu = 0; |
| 204 | ret = hf_interrupt_inject(vm_id, vcpu, int_id); |
| 205 | |
| 206 | if (ret == -1) { |
| 207 | pr_warn("Failed to inject interrupt %lld to vCPU %d of VM %d", |
| 208 | int_id, vcpu, vm_id); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (ret != 1) { |
| 213 | /* We don't need to wake up the vcpu. */ |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | hf_handle_wake_up_request(vm_id, vcpu); |
| 218 | } |
| 219 | |
| 220 | /** |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 221 | * Notify all waiters on the given VM. |
| 222 | */ |
Andrew Walbran | 01390ae | 2019-06-19 11:32:46 +0100 | [diff] [blame] | 223 | static void hf_notify_waiters(spci_vm_id_t vm_id) |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 224 | { |
Andrew Walbran | 01390ae | 2019-06-19 11:32:46 +0100 | [diff] [blame] | 225 | spci_vm_id_t waiter_vm_id; |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 226 | |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 227 | while ((waiter_vm_id = hf_mailbox_waiter_get(vm_id)) != -1) { |
| 228 | if (waiter_vm_id == HF_PRIMARY_VM_ID) { |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 229 | /* |
| 230 | * TODO: Use this information when implementing per-vm |
| 231 | * queues. |
| 232 | */ |
| 233 | } else { |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 234 | hf_interrupt_vm(waiter_vm_id, |
| 235 | HF_MAILBOX_WRITABLE_INTID); |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 241 | * Delivers a message to a VM. |
| 242 | */ |
Andrew Walbran | 01390ae | 2019-06-19 11:32:46 +0100 | [diff] [blame] | 243 | static void hf_deliver_message(spci_vm_id_t vm_id) |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 244 | { |
| 245 | struct hf_vm *vm = hf_vm_from_id(vm_id); |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 246 | spci_vcpu_index_t i; |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 247 | |
| 248 | if (!vm) { |
| 249 | pr_warn("Tried to deliver message to non-existent VM id: %u\n", |
| 250 | vm_id); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | /* Try to wake a vCPU that is waiting for a message. */ |
| 255 | for (i = 0; i < vm->vcpu_count; i++) { |
| 256 | if (atomic_read(&vm->vcpu[i].waiting_for_message)) { |
| 257 | hf_handle_wake_up_request(vm->id, |
| 258 | vm->vcpu[i].vcpu_index); |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* None were waiting for a message so interrupt one. */ |
| 264 | hf_interrupt_vm(vm->id, HF_MAILBOX_READABLE_INTID); |
| 265 | } |
| 266 | |
| 267 | /** |
Andrew Scull | df6478f | 2019-02-19 17:52:08 +0000 | [diff] [blame] | 268 | * Handles a message delivered to this VM by validating that it's well-formed |
| 269 | * and then queueing it for delivery to the appropriate socket. |
| 270 | */ |
Andrew Walbran | b331fa9 | 2019-10-03 16:48:07 +0100 | [diff] [blame] | 271 | static void hf_handle_message(struct hf_vm *sender, size_t len, |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 272 | const void *message) |
Andrew Scull | df6478f | 2019-02-19 17:52:08 +0000 | [diff] [blame] | 273 | { |
| 274 | struct hf_sock *hsock; |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 275 | const struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)message; |
Andrew Scull | df6478f | 2019-02-19 17:52:08 +0000 | [diff] [blame] | 276 | struct sk_buff *skb; |
| 277 | int err; |
| 278 | |
| 279 | /* Ignore messages that are too small to hold a header. */ |
| 280 | if (len < sizeof(struct hf_msg_hdr)) |
| 281 | return; |
| 282 | |
| 283 | len -= sizeof(struct hf_msg_hdr); |
| 284 | |
| 285 | /* Go through the colliding sockets. */ |
| 286 | rcu_read_lock(); |
| 287 | hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node, |
| 288 | hdr->dst_port) { |
| 289 | if (hsock->peer_vm == sender && |
| 290 | hsock->remote_port == hdr->src_port) { |
| 291 | sock_hold(&hsock->sk); |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | rcu_read_unlock(); |
| 296 | |
| 297 | /* Nothing to do if we couldn't find the target. */ |
| 298 | if (!hsock) |
| 299 | return; |
| 300 | |
| 301 | /* |
| 302 | * TODO: From this point on, there are two failure paths: when we |
| 303 | * create the skb below, and when we enqueue it to the socket. What |
| 304 | * should we do if they fail? Ideally we would have some form of flow |
| 305 | * control to prevent message loss, but how to do it efficiently? |
| 306 | * |
| 307 | * One option is to have a pre-allocated message that indicates to the |
| 308 | * sender that a message was dropped. This way we guarantee that the |
| 309 | * sender will be aware of loss and should back-off. |
| 310 | */ |
| 311 | /* Create the skb. */ |
| 312 | skb = alloc_skb(len, GFP_KERNEL); |
| 313 | if (!skb) |
| 314 | goto exit; |
| 315 | |
| 316 | memcpy(skb_put(skb, len), hdr + 1, len); |
| 317 | |
| 318 | /* |
| 319 | * Add the skb to the receive queue of the target socket. On success it |
| 320 | * calls sk->sk_data_ready, which is currently set to sock_def_readable, |
| 321 | * which wakes up any waiters. |
| 322 | */ |
| 323 | err = sock_queue_rcv_skb(&hsock->sk, skb); |
| 324 | if (err) |
| 325 | kfree_skb(skb); |
| 326 | |
| 327 | exit: |
| 328 | sock_put(&hsock->sk); |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 329 | |
| 330 | if (hf_mailbox_clear() == 1) |
| 331 | hf_notify_waiters(HF_PRIMARY_VM_ID); |
Andrew Scull | df6478f | 2019-02-19 17:52:08 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 335 | * This is the main loop of each vcpu. |
| 336 | */ |
| 337 | static int hf_vcpu_thread(void *data) |
| 338 | { |
| 339 | struct hf_vcpu *vcpu = data; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 340 | struct hf_vcpu_run_return ret; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 341 | |
| 342 | hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 343 | vcpu->timer.function = &hf_vcpu_timer_expired; |
| 344 | |
| 345 | while (!kthread_should_stop()) { |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 346 | spci_vcpu_index_t i; |
Andrew Scull | 01f83de | 2019-01-23 13:41:47 +0000 | [diff] [blame] | 347 | |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 348 | /* |
| 349 | * We're about to run the vcpu, so we can reset the abort-sleep |
| 350 | * flag. |
| 351 | */ |
| 352 | atomic_set(&vcpu->abort_sleep, 0); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 353 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 354 | /* Call into Hafnium to run vcpu. */ |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 355 | ret = hf_vcpu_run(vcpu->vm->id, vcpu->vcpu_index); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 356 | |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 357 | switch (ret.code) { |
Andrew Scull | e05702e | 2019-01-08 14:46:46 +0000 | [diff] [blame] | 358 | /* Preempted. */ |
| 359 | case HF_VCPU_RUN_PREEMPTED: |
| 360 | if (need_resched()) |
| 361 | schedule(); |
| 362 | break; |
| 363 | |
| 364 | /* Yield. */ |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 365 | case HF_VCPU_RUN_YIELD: |
Andrew Scull | e05702e | 2019-01-08 14:46:46 +0000 | [diff] [blame] | 366 | if (!kthread_should_stop()) |
| 367 | schedule(); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 368 | break; |
| 369 | |
Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 370 | /* WFI. */ |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 371 | case HF_VCPU_RUN_WAIT_FOR_INTERRUPT: |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 372 | if (ret.sleep.ns != HF_SLEEP_INDEFINITE) { |
| 373 | hrtimer_start(&vcpu->timer, ret.sleep.ns, |
| 374 | HRTIMER_MODE_REL); |
| 375 | } |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 376 | hf_vcpu_sleep(vcpu); |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 377 | hrtimer_cancel(&vcpu->timer); |
| 378 | break; |
| 379 | |
| 380 | /* Waiting for a message. */ |
| 381 | case HF_VCPU_RUN_WAIT_FOR_MESSAGE: |
| 382 | atomic_set(&vcpu->waiting_for_message, 1); |
| 383 | if (ret.sleep.ns != HF_SLEEP_INDEFINITE) { |
| 384 | hrtimer_start(&vcpu->timer, ret.sleep.ns, |
| 385 | HRTIMER_MODE_REL); |
| 386 | } |
| 387 | hf_vcpu_sleep(vcpu); |
| 388 | hrtimer_cancel(&vcpu->timer); |
| 389 | atomic_set(&vcpu->waiting_for_message, 0); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 390 | break; |
| 391 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 392 | /* Wake up another vcpu. */ |
| 393 | case HF_VCPU_RUN_WAKE_UP: |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 394 | hf_handle_wake_up_request(ret.wake_up.vm_id, |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 395 | ret.wake_up.vcpu); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 396 | break; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 397 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 398 | /* Response available. */ |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 399 | case HF_VCPU_RUN_MESSAGE: |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 400 | if (ret.message.vm_id == HF_PRIMARY_VM_ID) { |
Andrew Walbran | b331fa9 | 2019-10-03 16:48:07 +0100 | [diff] [blame] | 401 | hf_handle_message(vcpu->vm, ret.message.size, |
Andrew Scull | 9470423 | 2019-04-01 12:36:37 +0100 | [diff] [blame] | 402 | page_address(hf_recv_page)); |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 403 | } else { |
| 404 | hf_deliver_message(ret.message.vm_id); |
| 405 | } |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 406 | break; |
Wedson Almeida Filho | cd9fef9 | 2019-01-11 21:24:08 +0000 | [diff] [blame] | 407 | |
| 408 | /* Notify all waiters. */ |
| 409 | case HF_VCPU_RUN_NOTIFY_WAITERS: |
| 410 | hf_notify_waiters(vcpu->vm->id); |
| 411 | break; |
Andrew Scull | 01f83de | 2019-01-23 13:41:47 +0000 | [diff] [blame] | 412 | |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 413 | /* Abort was triggered. */ |
Andrew Scull | 01f83de | 2019-01-23 13:41:47 +0000 | [diff] [blame] | 414 | case HF_VCPU_RUN_ABORTED: |
| 415 | for (i = 0; i < vcpu->vm->vcpu_count; i++) { |
| 416 | if (i == vcpu->vcpu_index) |
| 417 | continue; |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 418 | hf_handle_wake_up_request(vcpu->vm->id, i); |
Andrew Scull | 01f83de | 2019-01-23 13:41:47 +0000 | [diff] [blame] | 419 | } |
| 420 | hf_vcpu_sleep(vcpu); |
| 421 | break; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /** |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 429 | * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It |
| 430 | * relies on the fact that the first field of hf_sock is a sock. |
| 431 | */ |
| 432 | static struct hf_sock *hsock_from_sk(struct sock *sk) |
| 433 | { |
| 434 | return (struct hf_sock *)sk; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * This is called when the last reference to the outer socket is released. For |
| 439 | * example, if it's a user-space socket, when the last file descriptor pointing |
| 440 | * to this socket is closed. |
| 441 | * |
| 442 | * It begins cleaning up resources, though some can only be cleaned up after all |
| 443 | * references to the underlying socket are released, which is handled by |
| 444 | * hf_sock_destruct(). |
| 445 | */ |
| 446 | static int hf_sock_release(struct socket *sock) |
| 447 | { |
| 448 | struct sock *sk = sock->sk; |
| 449 | struct hf_sock *hsock = hsock_from_sk(sk); |
| 450 | unsigned long flags; |
| 451 | |
| 452 | if (!sk) |
| 453 | return 0; |
| 454 | |
| 455 | /* Shutdown for both send and receive. */ |
| 456 | lock_sock(sk); |
| 457 | sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN; |
| 458 | sk->sk_state_change(sk); |
| 459 | release_sock(sk); |
| 460 | |
| 461 | /* Remove from the hash table, so lookups from now on won't find it. */ |
| 462 | spin_lock_irqsave(&hf_local_port_hash_lock, flags); |
| 463 | hash_del_rcu(&hsock->sk.sk_node); |
| 464 | spin_unlock_irqrestore(&hf_local_port_hash_lock, flags); |
| 465 | |
| 466 | /* |
| 467 | * TODO: When we implement a tx queue, we need to clear it here so that |
| 468 | * sk_wmem_alloc will not prevent sk from being freed (sk_free). |
| 469 | */ |
| 470 | |
| 471 | /* |
| 472 | * Wait for in-flight lookups to finish. We need to do this here because |
Wedson Almeida Filho | 89d0e47 | 2019-01-03 19:18:39 +0000 | [diff] [blame] | 473 | * in-flight lookups rely on the reference to the socket we're about to |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 474 | * release. |
| 475 | */ |
| 476 | synchronize_rcu(); |
| 477 | sock_put(sk); |
| 478 | sock->sk = NULL; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * This is called when there are no more references to the socket. It frees all |
| 485 | * resources that haven't been freed during release. |
| 486 | */ |
| 487 | static void hf_sock_destruct(struct sock *sk) |
| 488 | { |
| 489 | /* |
| 490 | * Clear the receive queue now that the handler cannot add any more |
| 491 | * skbs to it. |
| 492 | */ |
| 493 | skb_queue_purge(&sk->sk_receive_queue); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Connects the Hafnium socket to the provided VM and port. After the socket is |
| 498 | * connected, it can be used to exchange datagrams with the specified peer. |
| 499 | */ |
Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 500 | static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len, |
| 501 | int connect_flags) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 502 | { |
| 503 | struct sock *sk = sock->sk; |
| 504 | struct hf_sock *hsock = hsock_from_sk(sk); |
| 505 | struct hf_vm *vm; |
Fuad Tabba | 3e669bc | 2019-08-08 16:43:55 +0100 | [diff] [blame] | 506 | struct hf_sockaddr *addr; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 507 | int err; |
| 508 | unsigned long flags; |
| 509 | |
| 510 | /* Basic address validation. */ |
Fuad Tabba | 3e669bc | 2019-08-08 16:43:55 +0100 | [diff] [blame] | 511 | if (len < sizeof(struct hf_sockaddr) || saddr->sa_family != AF_HF) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | |
Fuad Tabba | 3e669bc | 2019-08-08 16:43:55 +0100 | [diff] [blame] | 514 | addr = (struct hf_sockaddr *)saddr; |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 515 | vm = hf_vm_from_id(addr->vm_id); |
| 516 | if (!vm) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 517 | return -ENETUNREACH; |
| 518 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 519 | /* |
| 520 | * TODO: Once we implement access control in Hafnium, check that the |
| 521 | * caller is allowed to contact the specified VM. Return -ECONNREFUSED |
| 522 | * if access is denied. |
| 523 | */ |
| 524 | |
| 525 | /* Take lock to make sure state doesn't change as we connect. */ |
| 526 | lock_sock(sk); |
| 527 | |
| 528 | /* Only unconnected sockets are allowed to become connected. */ |
| 529 | if (sock->state != SS_UNCONNECTED) { |
| 530 | err = -EISCONN; |
| 531 | goto exit; |
| 532 | } |
| 533 | |
| 534 | hsock->local_port = atomic64_inc_return(&hf_next_port); |
| 535 | hsock->remote_port = addr->port; |
| 536 | hsock->peer_vm = vm; |
| 537 | |
| 538 | sock->state = SS_CONNECTED; |
| 539 | |
| 540 | /* Add socket to hash table now that it's fully initialised. */ |
| 541 | spin_lock_irqsave(&hf_local_port_hash_lock, flags); |
| 542 | hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port); |
| 543 | spin_unlock_irqrestore(&hf_local_port_hash_lock, flags); |
| 544 | |
| 545 | err = 0; |
| 546 | exit: |
| 547 | release_sock(sk); |
| 548 | return err; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Sends the given skb to the appropriate VM by calling Hafnium. It will also |
| 553 | * trigger the wake up of a recipient VM. |
| 554 | * |
| 555 | * Takes ownership of the skb on success. |
| 556 | */ |
| 557 | static int hf_send_skb(struct sk_buff *skb) |
| 558 | { |
| 559 | unsigned long flags; |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 560 | struct spci_value ret; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 561 | struct hf_sock *hsock = hsock_from_sk(skb->sk); |
| 562 | struct hf_vm *vm = hsock->peer_vm; |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 563 | void *message = page_address(hf_send_page); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Call Hafnium under the send lock so that we serialize the use of the |
| 567 | * global send buffer. |
| 568 | */ |
| 569 | spin_lock_irqsave(&hf_send_lock, flags); |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 570 | memcpy(message, skb->data, skb->len); |
Jose Marinho | 1cc6c75 | 2019-03-11 16:28:03 +0000 | [diff] [blame] | 571 | |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 572 | ret = spci_msg_send(current_vm_id, vm->id, skb->len, 0); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 573 | spin_unlock_irqrestore(&hf_send_lock, flags); |
| 574 | |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 575 | if (ret.func == SPCI_ERROR_32) { |
Andrew Walbran | b040b30 | 2019-10-10 13:50:06 +0100 | [diff] [blame^] | 576 | switch (ret.arg2) { |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 577 | case SPCI_INVALID_PARAMETERS: |
| 578 | return -ENXIO; |
| 579 | case SPCI_NOT_SUPPORTED: |
| 580 | return -EIO; |
| 581 | case SPCI_DENIED: |
| 582 | case SPCI_BUSY: |
| 583 | default: |
| 584 | return -EAGAIN; |
| 585 | } |
| 586 | } |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 587 | |
Andrew Scull | 71f5736 | 2019-02-05 16:11:35 +0000 | [diff] [blame] | 588 | /* Ensure the VM will run to pick up the message. */ |
| 589 | hf_deliver_message(vm->id); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 590 | |
| 591 | kfree_skb(skb); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Determines if the given socket is in the connected state. It acquires and |
| 598 | * releases the socket lock. |
| 599 | */ |
| 600 | static bool hf_sock_is_connected(struct socket *sock) |
| 601 | { |
| 602 | bool ret; |
| 603 | |
| 604 | lock_sock(sock->sk); |
| 605 | ret = sock->state == SS_CONNECTED; |
| 606 | release_sock(sock->sk); |
| 607 | |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Sends a message to the VM & port the socket is connected to. All variants |
| 613 | * of write/send/sendto/sendmsg eventually call this function. |
| 614 | */ |
| 615 | static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len) |
| 616 | { |
| 617 | struct sock *sk = sock->sk; |
| 618 | struct sk_buff *skb; |
| 619 | int err; |
| 620 | struct hf_msg_hdr *hdr; |
| 621 | struct hf_sock *hsock = hsock_from_sk(sk); |
Andrew Walbran | cafe017 | 2019-10-07 14:14:05 +0100 | [diff] [blame] | 622 | size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct hf_msg_hdr); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 623 | |
| 624 | /* Check length. */ |
Andrew Scull | 614ed7f | 2019-04-01 12:12:38 +0100 | [diff] [blame] | 625 | if (len > payload_max_len) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 626 | return -EMSGSIZE; |
| 627 | |
| 628 | /* We don't allow the destination address to be specified. */ |
| 629 | if (m->msg_namelen > 0) |
| 630 | return -EISCONN; |
| 631 | |
| 632 | /* We don't support out of band messages. */ |
| 633 | if (m->msg_flags & MSG_OOB) |
| 634 | return -EOPNOTSUPP; |
| 635 | |
| 636 | /* |
| 637 | * Ensure that the socket is connected. We don't need to hold the socket |
| 638 | * lock (acquired and released by hf_sock_is_connected) for the |
| 639 | * remainder of the function because the fields we care about are |
| 640 | * immutable once the state is SS_CONNECTED. |
| 641 | */ |
| 642 | if (!hf_sock_is_connected(sock)) |
| 643 | return -ENOTCONN; |
| 644 | |
| 645 | /* |
| 646 | * Allocate an skb for this write. If there isn't enough room in the |
| 647 | * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block |
| 648 | * (if it's a blocking call). On success, it increments sk_wmem_alloc |
| 649 | * and sets up the skb such that sk_wmem_alloc gets decremented when |
| 650 | * the skb is freed (sock_wfree gets called). |
| 651 | */ |
| 652 | skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr), |
| 653 | m->msg_flags & MSG_DONTWAIT, &err); |
| 654 | if (!skb) |
| 655 | return err; |
| 656 | |
| 657 | /* Reserve room for the header and initialise it. */ |
| 658 | skb_reserve(skb, sizeof(struct hf_msg_hdr)); |
| 659 | hdr = skb_push(skb, sizeof(struct hf_msg_hdr)); |
| 660 | hdr->src_port = hsock->local_port; |
| 661 | hdr->dst_port = hsock->remote_port; |
| 662 | |
| 663 | /* Allocate area for the contents, then copy into skb. */ |
| 664 | if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) { |
| 665 | err = -EFAULT; |
| 666 | goto err_cleanup; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * TODO: We currently do this inline, but when we have support for |
| 671 | * readiness notification from Hafnium, we must add this to a per-VM tx |
| 672 | * queue that can make progress when the VM becomes writable. This will |
| 673 | * fix send buffering and poll readiness notification. |
| 674 | */ |
| 675 | err = hf_send_skb(skb); |
| 676 | if (err) |
| 677 | goto err_cleanup; |
| 678 | |
| 679 | return 0; |
| 680 | |
| 681 | err_cleanup: |
| 682 | kfree_skb(skb); |
| 683 | return err; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Receives a message originated from the VM & port the socket is connected to. |
| 688 | * All variants of read/recv/recvfrom/recvmsg eventually call this function. |
| 689 | */ |
| 690 | static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len, |
| 691 | int flags) |
| 692 | { |
| 693 | struct sock *sk = sock->sk; |
| 694 | struct sk_buff *skb; |
| 695 | int err; |
| 696 | size_t copy_len; |
| 697 | |
| 698 | if (!hf_sock_is_connected(sock)) |
| 699 | return -ENOTCONN; |
| 700 | |
| 701 | /* Grab the next skb from the receive queue. */ |
| 702 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
| 703 | if (!skb) |
| 704 | return err; |
| 705 | |
| 706 | /* Make sure we don't copy more than what fits in the output buffer. */ |
| 707 | copy_len = skb->len; |
| 708 | if (copy_len > len) { |
| 709 | copy_len = len; |
| 710 | m->msg_flags |= MSG_TRUNC; |
| 711 | } |
| 712 | |
| 713 | /* Make sure we don't overflow the return value type. */ |
| 714 | if (copy_len > INT_MAX) { |
| 715 | copy_len = INT_MAX; |
| 716 | m->msg_flags |= MSG_TRUNC; |
| 717 | } |
| 718 | |
| 719 | /* Copy skb to output iterator, then free it. */ |
| 720 | err = skb_copy_datagram_msg(skb, 0, m, copy_len); |
| 721 | skb_free_datagram(sk, skb); |
| 722 | if (err) |
| 723 | return err; |
| 724 | |
| 725 | return copy_len; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * This function is called when a Hafnium socket is created. It initialises all |
| 730 | * state such that the caller will be able to connect the socket and then send |
| 731 | * and receive messages through it. |
| 732 | */ |
| 733 | static int hf_sock_create(struct net *net, struct socket *sock, int protocol, |
Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 734 | int kern) |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 735 | { |
| 736 | static const struct proto_ops ops = { |
| 737 | .family = PF_HF, |
| 738 | .owner = THIS_MODULE, |
| 739 | .release = hf_sock_release, |
| 740 | .bind = sock_no_bind, |
| 741 | .connect = hf_sock_connect, |
| 742 | .socketpair = sock_no_socketpair, |
| 743 | .accept = sock_no_accept, |
| 744 | .ioctl = sock_no_ioctl, |
| 745 | .listen = sock_no_listen, |
| 746 | .shutdown = sock_no_shutdown, |
| 747 | .setsockopt = sock_no_setsockopt, |
| 748 | .getsockopt = sock_no_getsockopt, |
| 749 | .sendmsg = hf_sock_sendmsg, |
| 750 | .recvmsg = hf_sock_recvmsg, |
| 751 | .mmap = sock_no_mmap, |
| 752 | .sendpage = sock_no_sendpage, |
| 753 | .poll = datagram_poll, |
| 754 | }; |
| 755 | struct sock *sk; |
| 756 | |
| 757 | if (sock->type != SOCK_DGRAM) |
| 758 | return -ESOCKTNOSUPPORT; |
| 759 | |
| 760 | if (protocol != 0) |
| 761 | return -EPROTONOSUPPORT; |
| 762 | |
| 763 | /* |
| 764 | * For now we only allow callers with sys admin capability to create |
| 765 | * Hafnium sockets. |
| 766 | */ |
| 767 | if (!capable(CAP_SYS_ADMIN)) |
| 768 | return -EPERM; |
| 769 | |
| 770 | /* Allocate and initialise socket. */ |
| 771 | sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern); |
| 772 | if (!sk) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | sock_init_data(sock, sk); |
| 776 | |
| 777 | sk->sk_destruct = hf_sock_destruct; |
| 778 | sock->ops = &ops; |
| 779 | sock->state = SS_UNCONNECTED; |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 785 | * Frees all resources, including threads, associated with the Hafnium driver. |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 786 | */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 787 | static void hf_free_resources(void) |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 788 | { |
Fuad Tabba | 5da4b6b | 2019-08-05 13:56:20 +0100 | [diff] [blame] | 789 | uint16_t i; |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 790 | spci_vcpu_index_t j; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * First stop all worker threads. We need to do this before freeing |
| 794 | * resources because workers may reference each other, so it is only |
| 795 | * safe to free resources after they have all stopped. |
| 796 | */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 797 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 798 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 799 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 800 | for (j = 0; j < vm->vcpu_count; j++) |
| 801 | kthread_stop(vm->vcpu[j].task); |
| 802 | } |
| 803 | |
| 804 | /* Free resources. */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 805 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 806 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 807 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 808 | for (j = 0; j < vm->vcpu_count; j++) |
| 809 | put_task_struct(vm->vcpu[j].task); |
| 810 | kfree(vm->vcpu); |
| 811 | } |
| 812 | |
| 813 | kfree(hf_vms); |
| 814 | } |
| 815 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 816 | /** |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 817 | * Handles the hypervisor timer interrupt. |
| 818 | */ |
| 819 | static irqreturn_t hf_nop_irq_handler(int irq, void *dev) |
| 820 | { |
| 821 | /* |
| 822 | * No need to do anything, the interrupt only exists to return to the |
| 823 | * primary vCPU so that the virtual timer will be restored and fire as |
| 824 | * normal. |
| 825 | */ |
| 826 | return IRQ_HANDLED; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Enables the hypervisor timer interrupt on a CPU, when it starts or after the |
| 831 | * driver is first loaded. |
| 832 | */ |
| 833 | static int hf_starting_cpu(unsigned int cpu) |
| 834 | { |
| 835 | if (hf_irq != 0) { |
| 836 | /* Enable the interrupt, and set it to be edge-triggered. */ |
| 837 | enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING); |
| 838 | } |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 839 | |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Disables the hypervisor timer interrupt on a CPU when it is powered down. |
| 845 | */ |
| 846 | static int hf_dying_cpu(unsigned int cpu) |
| 847 | { |
| 848 | if (hf_irq != 0) { |
| 849 | /* Disable the interrupt while the CPU is asleep. */ |
| 850 | disable_percpu_irq(hf_irq); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Registers for the hypervisor timer interrupt. |
| 858 | */ |
| 859 | static int hf_int_driver_probe(struct platform_device *pdev) |
| 860 | { |
| 861 | int irq; |
| 862 | int ret; |
| 863 | |
| 864 | /* |
| 865 | * Register a handler for the hyperviser timer IRQ, as it is needed for |
| 866 | * Hafnium to emulate the virtual timer for Linux while a secondary vCPU |
| 867 | * is running. |
| 868 | */ |
| 869 | irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI); |
| 870 | if (irq < 0) { |
| 871 | pr_err("Error getting hypervisor timer IRQ: %d\n", irq); |
| 872 | return irq; |
| 873 | } |
| 874 | hf_irq = irq; |
| 875 | |
| 876 | ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME, |
| 877 | pdev); |
| 878 | if (ret != 0) { |
| 879 | pr_err("Error registering hypervisor timer IRQ %d: %d\n", |
| 880 | irq, ret); |
| 881 | return ret; |
| 882 | } |
| 883 | pr_info("Hafnium registered for IRQ %d\n", irq); |
| 884 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, |
| 885 | "hafnium/hypervisor_timer:starting", |
| 886 | hf_starting_cpu, hf_dying_cpu); |
| 887 | if (ret < 0) { |
| 888 | pr_err("Error enabling timer on all CPUs: %d\n", ret); |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 889 | free_percpu_irq(irq, pdev); |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 890 | return ret; |
| 891 | } |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 892 | hf_cpuhp_state = ret; |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Unregisters for the hypervisor timer interrupt. |
| 899 | */ |
| 900 | static int hf_int_driver_remove(struct platform_device *pdev) |
| 901 | { |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 902 | /* |
| 903 | * This will cause hf_dying_cpu to be called on each CPU, which will |
| 904 | * disable the IRQs. |
| 905 | */ |
| 906 | cpuhp_remove_state(hf_cpuhp_state); |
| 907 | free_percpu_irq(hf_irq, pdev); |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static const struct of_device_id hf_int_driver_id[] = { |
| 913 | {.compatible = "arm,armv7-timer"}, |
| 914 | {.compatible = "arm,armv8-timer"}, |
| 915 | {} |
| 916 | }; |
| 917 | |
| 918 | static struct platform_driver hf_int_driver = { |
| 919 | .driver = { |
| 920 | .name = HYPERVISOR_TIMER_NAME, |
| 921 | .owner = THIS_MODULE, |
| 922 | .of_match_table = of_match_ptr(hf_int_driver_id), |
| 923 | }, |
| 924 | .probe = hf_int_driver_probe, |
| 925 | .remove = hf_int_driver_remove, |
| 926 | }; |
| 927 | |
| 928 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 929 | * Initializes the Hafnium driver by creating a thread for each vCPU of each |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 930 | * virtual machine. |
| 931 | */ |
| 932 | static int __init hf_init(void) |
| 933 | { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 934 | static const struct net_proto_family proto_family = { |
| 935 | .family = PF_HF, |
| 936 | .create = hf_sock_create, |
| 937 | .owner = THIS_MODULE, |
| 938 | }; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 939 | int64_t ret; |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 940 | spci_vm_id_t i; |
| 941 | spci_vcpu_index_t j; |
| 942 | spci_vm_count_t secondary_vm_count; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 943 | uint32_t total_vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 944 | |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 945 | /* Allocate a page for send and receive buffers. */ |
| 946 | hf_send_page = alloc_page(GFP_KERNEL); |
| 947 | if (!hf_send_page) { |
| 948 | pr_err("Unable to allocate send buffer\n"); |
| 949 | return -ENOMEM; |
| 950 | } |
| 951 | |
| 952 | hf_recv_page = alloc_page(GFP_KERNEL); |
| 953 | if (!hf_recv_page) { |
| 954 | __free_page(hf_send_page); |
| 955 | pr_err("Unable to allocate receive buffer\n"); |
| 956 | return -ENOMEM; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * Configure both addresses. Once configured, we cannot free these pages |
| 961 | * because the hypervisor will use them, even if the module is |
| 962 | * unloaded. |
| 963 | */ |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 964 | ret = hf_vm_configure(page_to_phys(hf_send_page), |
| 965 | page_to_phys(hf_recv_page)); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 966 | if (ret) { |
| 967 | __free_page(hf_send_page); |
| 968 | __free_page(hf_recv_page); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 969 | /* |
| 970 | * TODO: We may want to grab this information from hypervisor |
| 971 | * and go from there. |
| 972 | */ |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 973 | pr_err("Unable to configure VM\n"); |
| 974 | return -EIO; |
| 975 | } |
| 976 | |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 977 | /* Get the number of secondary VMs. */ |
| 978 | secondary_vm_count = hf_vm_get_count() - 1; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 979 | |
| 980 | /* Confirm the maximum number of VMs looks sane. */ |
| 981 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1); |
| 982 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX); |
| 983 | |
| 984 | /* Validate the number of VMs. There must at least be the primary. */ |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 985 | if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) { |
Fuad Tabba | 8523ccd | 2019-07-31 15:37:29 +0100 | [diff] [blame] | 986 | pr_err("Number of VMs is out of range: %d\n", |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 987 | secondary_vm_count); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 988 | return -EDQUOT; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 991 | /* Only track the secondary VMs. */ |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 992 | hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm), |
| 993 | GFP_KERNEL); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 994 | if (!hf_vms) |
| 995 | return -ENOMEM; |
| 996 | |
Jose Marinho | 1cc6c75 | 2019-03-11 16:28:03 +0000 | [diff] [blame] | 997 | /* Cache the VM id for later usage. */ |
| 998 | current_vm_id = hf_vm_get_id(); |
| 999 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1000 | /* Initialize each VM. */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1001 | total_vcpu_count = 0; |
Andrew Walbran | 4c96d0c | 2019-06-25 18:32:56 +0100 | [diff] [blame] | 1002 | for (i = 0; i < secondary_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 1003 | struct hf_vm *vm = &hf_vms[i]; |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 1004 | spci_vcpu_count_t vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1005 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 1006 | /* Adjust the ID as only the secondaries are tracked. */ |
Wedson Almeida Filho | ec84193 | 2019-01-22 23:07:50 +0000 | [diff] [blame] | 1007 | vm->id = i + FIRST_SECONDARY_VM_ID; |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 1008 | |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 1009 | vcpu_count = hf_vcpu_get_count(vm->id); |
| 1010 | if (vcpu_count < 0) { |
| 1011 | pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %d", |
| 1012 | vm->id, vcpu_count); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1013 | ret = -EIO; |
| 1014 | goto fail_with_cleanup; |
| 1015 | } |
| 1016 | |
| 1017 | /* Avoid overflowing the vcpu count. */ |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 1018 | if (vcpu_count > (U32_MAX - total_vcpu_count)) { |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1019 | pr_err("Too many vcpus: %u\n", total_vcpu_count); |
| 1020 | ret = -EDQUOT; |
| 1021 | goto fail_with_cleanup; |
| 1022 | } |
| 1023 | |
| 1024 | /* Confirm the maximum number of VCPUs looks sane. */ |
| 1025 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1); |
| 1026 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX); |
| 1027 | |
| 1028 | /* Enforce the limit on vcpus. */ |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 1029 | total_vcpu_count += vcpu_count; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1030 | if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) { |
| 1031 | pr_err("Too many vcpus: %u\n", total_vcpu_count); |
| 1032 | ret = -EDQUOT; |
| 1033 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Andrew Walbran | 3eeb1de | 2019-06-25 18:32:30 +0100 | [diff] [blame] | 1036 | vm->vcpu_count = vcpu_count; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1037 | vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu), |
| 1038 | GFP_KERNEL); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1039 | if (!vm->vcpu) { |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1040 | ret = -ENOMEM; |
| 1041 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1044 | /* Update the number of initialized VMs. */ |
| 1045 | hf_vm_count = i + 1; |
| 1046 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1047 | /* Create a kernel thread for each vcpu. */ |
| 1048 | for (j = 0; j < vm->vcpu_count; j++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 1049 | struct hf_vcpu *vcpu = &vm->vcpu[j]; |
Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 1050 | |
| 1051 | vcpu->task = |
| 1052 | kthread_create(hf_vcpu_thread, vcpu, |
| 1053 | "vcpu_thread_%u_%u", vm->id, j); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1054 | if (IS_ERR(vcpu->task)) { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1055 | pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n", |
| 1056 | vm->id, j, PTR_ERR(vcpu->task)); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1057 | vm->vcpu_count = j; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1058 | ret = PTR_ERR(vcpu->task); |
| 1059 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | get_task_struct(vcpu->task); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 1063 | vcpu->vm = vm; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1064 | vcpu->vcpu_index = j; |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 1065 | atomic_set(&vcpu->abort_sleep, 0); |
Andrew Scull | ece5ef4 | 2019-05-08 15:07:25 +0100 | [diff] [blame] | 1066 | atomic_set(&vcpu->waiting_for_message, 0); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1070 | /* Register protocol and socket family. */ |
| 1071 | ret = proto_register(&hf_sock_proto, 0); |
| 1072 | if (ret) { |
| 1073 | pr_err("Unable to register protocol: %lld\n", ret); |
| 1074 | goto fail_with_cleanup; |
| 1075 | } |
| 1076 | |
| 1077 | ret = sock_register(&proto_family); |
| 1078 | if (ret) { |
| 1079 | pr_err("Unable to register Hafnium's socket family: %lld\n", |
| 1080 | ret); |
| 1081 | goto fail_unregister_proto; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 1085 | * Register as a driver for the timer device, so we can register a |
| 1086 | * handler for the hyperviser timer IRQ. |
| 1087 | */ |
| 1088 | ret = platform_driver_register(&hf_int_driver); |
| 1089 | if (ret != 0) { |
| 1090 | pr_err("Error registering timer driver %lld\n", ret); |
| 1091 | goto fail_unregister_socket; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1095 | * Start running threads now that all is initialized. |
| 1096 | * |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 1097 | * Any failures from this point on must also unregister the driver with |
| 1098 | * platform_driver_unregister(). |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1099 | */ |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1100 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 1101 | struct hf_vm *vm = &hf_vms[i]; |
Andrew Scull | 0177811 | 2019-01-14 15:37:53 +0000 | [diff] [blame] | 1102 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1103 | for (j = 0; j < vm->vcpu_count; j++) |
| 1104 | wake_up_process(vm->vcpu[j].task); |
| 1105 | } |
| 1106 | |
| 1107 | /* Dump vm/vcpu count info. */ |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 1108 | pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 1109 | for (i = 0; i < hf_vm_count; i++) { |
| 1110 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1111 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 1112 | pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 1113 | } |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1114 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1115 | return 0; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1116 | |
Andrew Walbran | b3ca1dc | 2019-01-30 17:13:44 +0000 | [diff] [blame] | 1117 | fail_unregister_socket: |
| 1118 | sock_unregister(PF_HF); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1119 | fail_unregister_proto: |
| 1120 | proto_unregister(&hf_sock_proto); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1121 | fail_with_cleanup: |
| 1122 | hf_free_resources(); |
| 1123 | return ret; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 1127 | * Frees up all resources used by the Hafnium driver in preparation for |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1128 | * unloading it. |
| 1129 | */ |
| 1130 | static void __exit hf_exit(void) |
| 1131 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 1132 | pr_info("Preparing to unload Hafnium\n"); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1133 | sock_unregister(PF_HF); |
| 1134 | proto_unregister(&hf_sock_proto); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 1135 | hf_free_resources(); |
Andrew Walbran | 8d55e50 | 2019-02-05 11:42:08 +0000 | [diff] [blame] | 1136 | platform_driver_unregister(&hf_int_driver); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1137 | pr_info("Hafnium ready to unload\n"); |
| 1138 | } |
| 1139 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 1140 | MODULE_LICENSE("GPL v2"); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1141 | |
| 1142 | module_init(hf_init); |
| 1143 | module_exit(hf_exit); |