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