Andrew Walbran | 13c3a0b | 2018-11-30 11:51:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | */ |
| 17 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 18 | #include <linux/hrtimer.h> |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 19 | #include <linux/atomic.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/kthread.h> |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 23 | #include <linux/mm.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/sched/task.h> |
| 26 | #include <linux/slab.h> |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 27 | #include <linux/net.h> |
| 28 | #include <net/sock.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 29 | |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 30 | #include <hf/call.h> |
| 31 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 32 | /* TODO: Reusing AF_ECONET for now as it's otherwise unused. */ |
| 33 | #define AF_HF AF_ECONET |
| 34 | #define PF_HF AF_HF |
| 35 | |
| 36 | #define MESSAGE_INT_ID 1 |
| 37 | |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 38 | #define CONFIG_HAFNIUM_MAX_VMS 16 |
| 39 | #define CONFIG_HAFNIUM_MAX_VCPUS 32 |
| 40 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 41 | struct hf_vcpu { |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 42 | struct hf_vm *vm; |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 43 | uint32_t vcpu_index; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 44 | struct task_struct *task; |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 45 | atomic_t abort_sleep; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 46 | struct hrtimer timer; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct hf_vm { |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 50 | uint32_t id; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 51 | uint32_t vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 52 | struct hf_vcpu *vcpu; |
| 53 | }; |
| 54 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 55 | struct hf_msg_hdr { |
| 56 | uint64_t src_port; |
| 57 | uint64_t dst_port; |
| 58 | }; |
| 59 | |
| 60 | struct hf_sock { |
| 61 | /* This needs to be the first field. */ |
| 62 | struct sock sk; |
| 63 | |
| 64 | /* |
| 65 | * The following fields are immutable after the socket transitions to |
| 66 | * SS_CONNECTED state. |
| 67 | */ |
| 68 | uint64_t local_port; |
| 69 | uint64_t remote_port; |
| 70 | struct hf_vm *peer_vm; |
| 71 | }; |
| 72 | |
| 73 | struct sockaddr_hf { |
| 74 | sa_family_t family; |
| 75 | uint32_t vm_id; |
| 76 | uint64_t port; |
| 77 | }; |
| 78 | |
| 79 | static struct proto hf_sock_proto = { |
| 80 | .name = "hafnium", |
| 81 | .owner = THIS_MODULE, |
| 82 | .obj_size = sizeof(struct hf_sock), |
| 83 | }; |
| 84 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 85 | static struct hf_vm *hf_vms; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 86 | static uint32_t hf_vm_count; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 87 | static struct page *hf_send_page; |
| 88 | static struct page *hf_recv_page; |
| 89 | static atomic64_t hf_next_port = ATOMIC64_INIT(0); |
| 90 | static DEFINE_SPINLOCK(hf_send_lock); |
| 91 | static DEFINE_HASHTABLE(hf_local_port_hash, 7); |
| 92 | static DEFINE_SPINLOCK(hf_local_port_hash_lock); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 93 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 94 | /** |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 95 | * Wakes up the kernel thread responsible for running the given vcpu. |
| 96 | * |
| 97 | * Returns 0 if the thread was already running, 1 otherwise. |
| 98 | */ |
| 99 | static int hf_vcpu_wake_up(struct hf_vcpu *vcpu) |
| 100 | { |
| 101 | /* Set a flag indicating that the thread should not go to sleep. */ |
| 102 | atomic_set(&vcpu->abort_sleep, 1); |
| 103 | |
| 104 | /* Set the thread to running state. */ |
| 105 | return wake_up_process(vcpu->task); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Puts the current thread to sleep. The current thread must be responsible for |
| 110 | * running the given vcpu. |
| 111 | * |
| 112 | * Going to sleep will fail if hf_vcpu_wake_up() or kthread_stop() was called on |
| 113 | * this vcpu/thread since the last time it [re]started running. |
| 114 | */ |
| 115 | static void hf_vcpu_sleep(struct hf_vcpu *vcpu) |
| 116 | { |
| 117 | int abort; |
| 118 | |
| 119 | set_current_state(TASK_INTERRUPTIBLE); |
| 120 | |
| 121 | /* Check the sleep-abort flag after making thread interruptible. */ |
| 122 | abort = atomic_read(&vcpu->abort_sleep); |
| 123 | if (!abort && !kthread_should_stop()) |
| 124 | schedule(); |
| 125 | |
| 126 | /* Set state back to running on the way out. */ |
| 127 | set_current_state(TASK_RUNNING); |
| 128 | } |
| 129 | |
| 130 | /** |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 131 | * Wakes up the thread associated with the vcpu that owns the given timer. This |
| 132 | * is called when the timer the thread is waiting on expires. |
| 133 | */ |
| 134 | static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer) |
| 135 | { |
| 136 | struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer); |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 137 | /* TODO: Inject interrupt. */ |
| 138 | hf_vcpu_wake_up(vcpu); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 139 | return HRTIMER_NORESTART; |
| 140 | } |
| 141 | |
| 142 | /** |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 143 | * Handles a message delivered to this VM by validating that it's well-formed |
| 144 | * and then queueing it for delivery to the appropriate socket. |
| 145 | */ |
| 146 | static void hf_handle_message(struct hf_vm *sender, const void *ptr, size_t len) |
| 147 | { |
| 148 | struct hf_sock *hsock; |
| 149 | const struct hf_msg_hdr *hdr = ptr; |
| 150 | struct sk_buff *skb; |
| 151 | int err; |
| 152 | |
| 153 | /* Ignore messages that are too small to hold a header. */ |
| 154 | if (len < sizeof(struct hf_msg_hdr)) |
| 155 | return; |
| 156 | |
| 157 | len -= sizeof(struct hf_msg_hdr); |
| 158 | |
| 159 | /* Go through the colliding sockets. */ |
| 160 | rcu_read_lock(); |
| 161 | hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node, |
| 162 | hdr->dst_port) { |
| 163 | if (hsock->peer_vm == sender && |
| 164 | hsock->remote_port == hdr->src_port) { |
| 165 | sock_hold(&hsock->sk); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | rcu_read_unlock(); |
| 170 | |
| 171 | /* Nothing to do if we couldn't find the target. */ |
| 172 | if (!hsock) |
| 173 | return; |
| 174 | |
Wedson Almeida Filho | 89d0e47 | 2019-01-03 19:18:39 +0000 | [diff] [blame] | 175 | /* |
| 176 | * TODO: From this point on, there are two failure paths: when we |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 177 | * create the skb below, and when we enqueue it to the socket. What |
| 178 | * should we do if they fail? Ideally we would have some form of flow |
| 179 | * control to prevent message loss, but how to do it efficiently? |
| 180 | * |
| 181 | * One option is to have a pre-allocated message that indicates to the |
| 182 | * sender that a message was dropped. This way we guarantee that the |
| 183 | * sender will be aware of loss and should back-off. |
| 184 | */ |
| 185 | /* Create the skb. */ |
| 186 | skb = alloc_skb(len, GFP_KERNEL); |
| 187 | if (!skb) |
| 188 | goto exit; |
| 189 | |
| 190 | memcpy(skb_put(skb, len), hdr + 1, len); |
| 191 | |
| 192 | /* |
| 193 | * Add the skb to the receive queue of the target socket. On success it |
| 194 | * calls sk->sk_data_ready, which is currently set to sock_def_readable, |
| 195 | * which wakes up any waiters. |
| 196 | */ |
| 197 | err = sock_queue_rcv_skb(&hsock->sk, skb); |
| 198 | if (err) |
| 199 | kfree_skb(skb); |
| 200 | |
| 201 | exit: |
| 202 | sock_put(&hsock->sk); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * This function is called when Hafnium requests that the primary VM wake up a |
| 207 | * vCPU that belongs to a secondary VM. |
| 208 | * |
| 209 | * It wakes up the thread if it's sleeping, or kicks it if it's already running. |
| 210 | * |
| 211 | * If vCPU is HF_INVALID_VCPU, it injects a MESSAGE_INT_ID interrupt into a vCPU |
| 212 | * belonging to the specified VM. |
| 213 | */ |
| 214 | static void hf_handle_wake_up_request(uint32_t vm_id, uint16_t vcpu) |
| 215 | { |
| 216 | struct hf_vm *vm; |
| 217 | |
| 218 | if (vm_id > hf_vm_count) { |
| 219 | pr_warn("Request to wake up non-existent VM id: %u\n", vm_id); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | vm = &hf_vms[vm_id - 1]; |
| 224 | if (vcpu >= vm->vcpu_count) { |
| 225 | int64_t ret; |
| 226 | |
| 227 | if (vcpu != HF_INVALID_VCPU) { |
| 228 | pr_warn("Request to wake up non-existent vCPU: %u.%u\n", |
| 229 | vm_id, vcpu); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * TODO: For now we're picking the first vcpu to interrupt, but |
| 235 | * we want to be smarter. |
| 236 | */ |
| 237 | vcpu = 0; |
Wedson Almeida Filho | e75db9d | 2019-01-09 19:32:58 +0000 | [diff] [blame] | 238 | ret = hf_interrupt_inject(vm_id, vcpu, MESSAGE_INT_ID); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 239 | if (ret != 1) { |
| 240 | /* We don't need to wake up the vcpu. */ |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) { |
| 246 | /* |
| 247 | * The task was already running (presumably on a different |
| 248 | * physical CPU); interrupt it. This gives Hafnium a chance to |
| 249 | * inject any new interrupts. |
| 250 | */ |
| 251 | kick_process(vm->vcpu[vcpu].task); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 256 | * This is the main loop of each vcpu. |
| 257 | */ |
| 258 | static int hf_vcpu_thread(void *data) |
| 259 | { |
| 260 | struct hf_vcpu *vcpu = data; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 261 | struct hf_vcpu_run_return ret; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 262 | |
| 263 | hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 264 | vcpu->timer.function = &hf_vcpu_timer_expired; |
| 265 | |
| 266 | while (!kthread_should_stop()) { |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 267 | /* |
| 268 | * We're about to run the vcpu, so we can reset the abort-sleep |
| 269 | * flag. |
| 270 | */ |
| 271 | atomic_set(&vcpu->abort_sleep, 0); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 272 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 273 | /* Call into Hafnium to run vcpu. */ |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 274 | ret = hf_vcpu_run(vcpu->vm->id, vcpu->vcpu_index); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 275 | |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 276 | switch (ret.code) { |
Andrew Scull | e05702e | 2019-01-08 14:46:46 +0000 | [diff] [blame^] | 277 | /* Preempted. */ |
| 278 | case HF_VCPU_RUN_PREEMPTED: |
| 279 | if (need_resched()) |
| 280 | schedule(); |
| 281 | break; |
| 282 | |
| 283 | /* Yield. */ |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 284 | case HF_VCPU_RUN_YIELD: |
Andrew Scull | e05702e | 2019-01-08 14:46:46 +0000 | [diff] [blame^] | 285 | if (!kthread_should_stop()) |
| 286 | schedule(); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 287 | break; |
| 288 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 289 | /* WFI. */ |
| 290 | case HF_VCPU_RUN_WAIT_FOR_INTERRUPT: |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 291 | hf_vcpu_sleep(vcpu); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 292 | break; |
| 293 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 294 | /* Wake up another vcpu. */ |
| 295 | case HF_VCPU_RUN_WAKE_UP: |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 296 | hf_handle_wake_up_request(ret.wake_up.vm_id, |
| 297 | ret.wake_up.vcpu); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 298 | break; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 299 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 300 | /* Response available. */ |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 301 | case HF_VCPU_RUN_MESSAGE: |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 302 | hf_handle_message(vcpu->vm, page_address(hf_recv_page), |
| 303 | ret.message.size); |
| 304 | hf_mailbox_clear(); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 305 | break; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 306 | |
| 307 | case HF_VCPU_RUN_SLEEP: |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 308 | hrtimer_start(&vcpu->timer, ret.sleep.ns, |
| 309 | HRTIMER_MODE_REL); |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 310 | hf_vcpu_sleep(vcpu); |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame] | 311 | hrtimer_cancel(&vcpu->timer); |
| 312 | break; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /** |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 320 | * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It |
| 321 | * relies on the fact that the first field of hf_sock is a sock. |
| 322 | */ |
| 323 | static struct hf_sock *hsock_from_sk(struct sock *sk) |
| 324 | { |
| 325 | return (struct hf_sock *)sk; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * This is called when the last reference to the outer socket is released. For |
| 330 | * example, if it's a user-space socket, when the last file descriptor pointing |
| 331 | * to this socket is closed. |
| 332 | * |
| 333 | * It begins cleaning up resources, though some can only be cleaned up after all |
| 334 | * references to the underlying socket are released, which is handled by |
| 335 | * hf_sock_destruct(). |
| 336 | */ |
| 337 | static int hf_sock_release(struct socket *sock) |
| 338 | { |
| 339 | struct sock *sk = sock->sk; |
| 340 | struct hf_sock *hsock = hsock_from_sk(sk); |
| 341 | unsigned long flags; |
| 342 | |
| 343 | if (!sk) |
| 344 | return 0; |
| 345 | |
| 346 | /* Shutdown for both send and receive. */ |
| 347 | lock_sock(sk); |
| 348 | sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN; |
| 349 | sk->sk_state_change(sk); |
| 350 | release_sock(sk); |
| 351 | |
| 352 | /* Remove from the hash table, so lookups from now on won't find it. */ |
| 353 | spin_lock_irqsave(&hf_local_port_hash_lock, flags); |
| 354 | hash_del_rcu(&hsock->sk.sk_node); |
| 355 | spin_unlock_irqrestore(&hf_local_port_hash_lock, flags); |
| 356 | |
| 357 | /* |
| 358 | * TODO: When we implement a tx queue, we need to clear it here so that |
| 359 | * sk_wmem_alloc will not prevent sk from being freed (sk_free). |
| 360 | */ |
| 361 | |
| 362 | /* |
| 363 | * 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] | 364 | * 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] | 365 | * release. |
| 366 | */ |
| 367 | synchronize_rcu(); |
| 368 | sock_put(sk); |
| 369 | sock->sk = NULL; |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * This is called when there are no more references to the socket. It frees all |
| 376 | * resources that haven't been freed during release. |
| 377 | */ |
| 378 | static void hf_sock_destruct(struct sock *sk) |
| 379 | { |
| 380 | /* |
| 381 | * Clear the receive queue now that the handler cannot add any more |
| 382 | * skbs to it. |
| 383 | */ |
| 384 | skb_queue_purge(&sk->sk_receive_queue); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Connects the Hafnium socket to the provided VM and port. After the socket is |
| 389 | * connected, it can be used to exchange datagrams with the specified peer. |
| 390 | */ |
| 391 | static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, |
| 392 | int len, int connect_flags) |
| 393 | { |
| 394 | struct sock *sk = sock->sk; |
| 395 | struct hf_sock *hsock = hsock_from_sk(sk); |
| 396 | struct hf_vm *vm; |
| 397 | struct sockaddr_hf *addr; |
| 398 | int err; |
| 399 | unsigned long flags; |
| 400 | |
| 401 | /* Basic address validation. */ |
| 402 | if (len < sizeof(struct sockaddr_hf) || saddr->sa_family != AF_HF) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | addr = (struct sockaddr_hf *)saddr; |
| 406 | if (addr->vm_id > hf_vm_count) |
| 407 | return -ENETUNREACH; |
| 408 | |
| 409 | vm = &hf_vms[addr->vm_id - 1]; |
| 410 | |
| 411 | /* |
| 412 | * TODO: Once we implement access control in Hafnium, check that the |
| 413 | * caller is allowed to contact the specified VM. Return -ECONNREFUSED |
| 414 | * if access is denied. |
| 415 | */ |
| 416 | |
| 417 | /* Take lock to make sure state doesn't change as we connect. */ |
| 418 | lock_sock(sk); |
| 419 | |
| 420 | /* Only unconnected sockets are allowed to become connected. */ |
| 421 | if (sock->state != SS_UNCONNECTED) { |
| 422 | err = -EISCONN; |
| 423 | goto exit; |
| 424 | } |
| 425 | |
| 426 | hsock->local_port = atomic64_inc_return(&hf_next_port); |
| 427 | hsock->remote_port = addr->port; |
| 428 | hsock->peer_vm = vm; |
| 429 | |
| 430 | sock->state = SS_CONNECTED; |
| 431 | |
| 432 | /* Add socket to hash table now that it's fully initialised. */ |
| 433 | spin_lock_irqsave(&hf_local_port_hash_lock, flags); |
| 434 | hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port); |
| 435 | spin_unlock_irqrestore(&hf_local_port_hash_lock, flags); |
| 436 | |
| 437 | err = 0; |
| 438 | exit: |
| 439 | release_sock(sk); |
| 440 | return err; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Sends the given skb to the appropriate VM by calling Hafnium. It will also |
| 445 | * trigger the wake up of a recipient VM. |
| 446 | * |
| 447 | * Takes ownership of the skb on success. |
| 448 | */ |
| 449 | static int hf_send_skb(struct sk_buff *skb) |
| 450 | { |
| 451 | unsigned long flags; |
| 452 | int64_t ret; |
| 453 | struct hf_sock *hsock = hsock_from_sk(skb->sk); |
| 454 | struct hf_vm *vm = hsock->peer_vm; |
| 455 | |
| 456 | /* |
| 457 | * Call Hafnium under the send lock so that we serialize the use of the |
| 458 | * global send buffer. |
| 459 | */ |
| 460 | spin_lock_irqsave(&hf_send_lock, flags); |
| 461 | memcpy(page_address(hf_send_page), skb->data, skb->len); |
Wedson Almeida Filho | dbfc903 | 2019-01-09 19:03:32 +0000 | [diff] [blame] | 462 | ret = hf_mailbox_send(vm->id, skb->len, false); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 463 | spin_unlock_irqrestore(&hf_send_lock, flags); |
| 464 | |
| 465 | if (ret < 0) |
| 466 | return -EAGAIN; |
| 467 | |
| 468 | /* Wake some vcpu up to handle the new message. */ |
| 469 | hf_handle_wake_up_request(vm->id, ret); |
| 470 | |
| 471 | kfree_skb(skb); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Determines if the given socket is in the connected state. It acquires and |
| 478 | * releases the socket lock. |
| 479 | */ |
| 480 | static bool hf_sock_is_connected(struct socket *sock) |
| 481 | { |
| 482 | bool ret; |
| 483 | |
| 484 | lock_sock(sock->sk); |
| 485 | ret = sock->state == SS_CONNECTED; |
| 486 | release_sock(sock->sk); |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Sends a message to the VM & port the socket is connected to. All variants |
| 493 | * of write/send/sendto/sendmsg eventually call this function. |
| 494 | */ |
| 495 | static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len) |
| 496 | { |
| 497 | struct sock *sk = sock->sk; |
| 498 | struct sk_buff *skb; |
| 499 | int err; |
| 500 | struct hf_msg_hdr *hdr; |
| 501 | struct hf_sock *hsock = hsock_from_sk(sk); |
| 502 | |
| 503 | /* Check length. */ |
| 504 | if (len > HF_MAILBOX_SIZE - sizeof(struct hf_msg_hdr)) |
| 505 | return -EMSGSIZE; |
| 506 | |
| 507 | /* We don't allow the destination address to be specified. */ |
| 508 | if (m->msg_namelen > 0) |
| 509 | return -EISCONN; |
| 510 | |
| 511 | /* We don't support out of band messages. */ |
| 512 | if (m->msg_flags & MSG_OOB) |
| 513 | return -EOPNOTSUPP; |
| 514 | |
| 515 | /* |
| 516 | * Ensure that the socket is connected. We don't need to hold the socket |
| 517 | * lock (acquired and released by hf_sock_is_connected) for the |
| 518 | * remainder of the function because the fields we care about are |
| 519 | * immutable once the state is SS_CONNECTED. |
| 520 | */ |
| 521 | if (!hf_sock_is_connected(sock)) |
| 522 | return -ENOTCONN; |
| 523 | |
| 524 | /* |
| 525 | * Allocate an skb for this write. If there isn't enough room in the |
| 526 | * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block |
| 527 | * (if it's a blocking call). On success, it increments sk_wmem_alloc |
| 528 | * and sets up the skb such that sk_wmem_alloc gets decremented when |
| 529 | * the skb is freed (sock_wfree gets called). |
| 530 | */ |
| 531 | skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr), |
| 532 | m->msg_flags & MSG_DONTWAIT, &err); |
| 533 | if (!skb) |
| 534 | return err; |
| 535 | |
| 536 | /* Reserve room for the header and initialise it. */ |
| 537 | skb_reserve(skb, sizeof(struct hf_msg_hdr)); |
| 538 | hdr = skb_push(skb, sizeof(struct hf_msg_hdr)); |
| 539 | hdr->src_port = hsock->local_port; |
| 540 | hdr->dst_port = hsock->remote_port; |
| 541 | |
| 542 | /* Allocate area for the contents, then copy into skb. */ |
| 543 | if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) { |
| 544 | err = -EFAULT; |
| 545 | goto err_cleanup; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * TODO: We currently do this inline, but when we have support for |
| 550 | * readiness notification from Hafnium, we must add this to a per-VM tx |
| 551 | * queue that can make progress when the VM becomes writable. This will |
| 552 | * fix send buffering and poll readiness notification. |
| 553 | */ |
| 554 | err = hf_send_skb(skb); |
| 555 | if (err) |
| 556 | goto err_cleanup; |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | err_cleanup: |
| 561 | kfree_skb(skb); |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Receives a message originated from the VM & port the socket is connected to. |
| 567 | * All variants of read/recv/recvfrom/recvmsg eventually call this function. |
| 568 | */ |
| 569 | static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len, |
| 570 | int flags) |
| 571 | { |
| 572 | struct sock *sk = sock->sk; |
| 573 | struct sk_buff *skb; |
| 574 | int err; |
| 575 | size_t copy_len; |
| 576 | |
| 577 | if (!hf_sock_is_connected(sock)) |
| 578 | return -ENOTCONN; |
| 579 | |
| 580 | /* Grab the next skb from the receive queue. */ |
| 581 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
| 582 | if (!skb) |
| 583 | return err; |
| 584 | |
| 585 | /* Make sure we don't copy more than what fits in the output buffer. */ |
| 586 | copy_len = skb->len; |
| 587 | if (copy_len > len) { |
| 588 | copy_len = len; |
| 589 | m->msg_flags |= MSG_TRUNC; |
| 590 | } |
| 591 | |
| 592 | /* Make sure we don't overflow the return value type. */ |
| 593 | if (copy_len > INT_MAX) { |
| 594 | copy_len = INT_MAX; |
| 595 | m->msg_flags |= MSG_TRUNC; |
| 596 | } |
| 597 | |
| 598 | /* Copy skb to output iterator, then free it. */ |
| 599 | err = skb_copy_datagram_msg(skb, 0, m, copy_len); |
| 600 | skb_free_datagram(sk, skb); |
| 601 | if (err) |
| 602 | return err; |
| 603 | |
| 604 | return copy_len; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * This function is called when a Hafnium socket is created. It initialises all |
| 609 | * state such that the caller will be able to connect the socket and then send |
| 610 | * and receive messages through it. |
| 611 | */ |
| 612 | static int hf_sock_create(struct net *net, struct socket *sock, int protocol, |
| 613 | int kern) |
| 614 | { |
| 615 | static const struct proto_ops ops = { |
| 616 | .family = PF_HF, |
| 617 | .owner = THIS_MODULE, |
| 618 | .release = hf_sock_release, |
| 619 | .bind = sock_no_bind, |
| 620 | .connect = hf_sock_connect, |
| 621 | .socketpair = sock_no_socketpair, |
| 622 | .accept = sock_no_accept, |
| 623 | .ioctl = sock_no_ioctl, |
| 624 | .listen = sock_no_listen, |
| 625 | .shutdown = sock_no_shutdown, |
| 626 | .setsockopt = sock_no_setsockopt, |
| 627 | .getsockopt = sock_no_getsockopt, |
| 628 | .sendmsg = hf_sock_sendmsg, |
| 629 | .recvmsg = hf_sock_recvmsg, |
| 630 | .mmap = sock_no_mmap, |
| 631 | .sendpage = sock_no_sendpage, |
| 632 | .poll = datagram_poll, |
| 633 | }; |
| 634 | struct sock *sk; |
| 635 | |
| 636 | if (sock->type != SOCK_DGRAM) |
| 637 | return -ESOCKTNOSUPPORT; |
| 638 | |
| 639 | if (protocol != 0) |
| 640 | return -EPROTONOSUPPORT; |
| 641 | |
| 642 | /* |
| 643 | * For now we only allow callers with sys admin capability to create |
| 644 | * Hafnium sockets. |
| 645 | */ |
| 646 | if (!capable(CAP_SYS_ADMIN)) |
| 647 | return -EPERM; |
| 648 | |
| 649 | /* Allocate and initialise socket. */ |
| 650 | sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern); |
| 651 | if (!sk) |
| 652 | return -ENOMEM; |
| 653 | |
| 654 | sock_init_data(sock, sk); |
| 655 | |
| 656 | sk->sk_destruct = hf_sock_destruct; |
| 657 | sock->ops = &ops; |
| 658 | sock->state = SS_UNCONNECTED; |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 664 | * Frees all resources, including threads, associated with the Hafnium driver. |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 665 | */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 666 | static void hf_free_resources(void) |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 667 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 668 | uint32_t i, j; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * First stop all worker threads. We need to do this before freeing |
| 672 | * resources because workers may reference each other, so it is only |
| 673 | * safe to free resources after they have all stopped. |
| 674 | */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 675 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 676 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 677 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 678 | for (j = 0; j < vm->vcpu_count; j++) |
| 679 | kthread_stop(vm->vcpu[j].task); |
| 680 | } |
| 681 | |
| 682 | /* Free resources. */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 683 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 684 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 685 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 686 | for (j = 0; j < vm->vcpu_count; j++) |
| 687 | put_task_struct(vm->vcpu[j].task); |
| 688 | kfree(vm->vcpu); |
| 689 | } |
| 690 | |
| 691 | kfree(hf_vms); |
| 692 | } |
| 693 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 694 | /** |
| 695 | * 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] | 696 | * virtual machine. |
| 697 | */ |
| 698 | static int __init hf_init(void) |
| 699 | { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 700 | static const struct net_proto_family proto_family = { |
| 701 | .family = PF_HF, |
| 702 | .create = hf_sock_create, |
| 703 | .owner = THIS_MODULE, |
| 704 | }; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 705 | int64_t ret; |
| 706 | uint32_t i, j; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 707 | uint32_t total_vm_count; |
| 708 | uint32_t total_vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 709 | |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 710 | /* Allocate a page for send and receive buffers. */ |
| 711 | hf_send_page = alloc_page(GFP_KERNEL); |
| 712 | if (!hf_send_page) { |
| 713 | pr_err("Unable to allocate send buffer\n"); |
| 714 | return -ENOMEM; |
| 715 | } |
| 716 | |
| 717 | hf_recv_page = alloc_page(GFP_KERNEL); |
| 718 | if (!hf_recv_page) { |
| 719 | __free_page(hf_send_page); |
| 720 | pr_err("Unable to allocate receive buffer\n"); |
| 721 | return -ENOMEM; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Configure both addresses. Once configured, we cannot free these pages |
| 726 | * because the hypervisor will use them, even if the module is |
| 727 | * unloaded. |
| 728 | */ |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 729 | ret = hf_vm_configure(page_to_phys(hf_send_page), |
| 730 | page_to_phys(hf_recv_page)); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 731 | if (ret) { |
| 732 | __free_page(hf_send_page); |
| 733 | __free_page(hf_recv_page); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 734 | /* |
| 735 | * TODO: We may want to grab this information from hypervisor |
| 736 | * and go from there. |
| 737 | */ |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 738 | pr_err("Unable to configure VM\n"); |
| 739 | return -EIO; |
| 740 | } |
| 741 | |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 742 | /* Get the number of VMs. */ |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 743 | ret = hf_vm_get_count(); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 744 | if (ret < 0) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 745 | pr_err("Unable to retrieve number of VMs: %lld\n", ret); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 746 | return -EIO; |
| 747 | } |
| 748 | |
| 749 | /* Confirm the maximum number of VMs looks sane. */ |
| 750 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1); |
| 751 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX); |
| 752 | |
| 753 | /* Validate the number of VMs. There must at least be the primary. */ |
| 754 | if (ret < 1 || ret > CONFIG_HAFNIUM_MAX_VMS) { |
| 755 | pr_err("Number of VMs is out of range: %lld\n", ret); |
| 756 | return -EDQUOT; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 759 | /* Only track the secondary VMs. */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 760 | total_vm_count = ret - 1; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 761 | hf_vms = kmalloc_array(total_vm_count, sizeof(struct hf_vm), |
| 762 | GFP_KERNEL); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 763 | if (!hf_vms) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | /* Initialize each VM. */ |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 767 | total_vcpu_count = 0; |
| 768 | for (i = 0; i < total_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 769 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 770 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 771 | /* Adjust the ID as only the secondaries are tracked. */ |
| 772 | vm->id = i + 1; |
| 773 | |
| 774 | ret = hf_vcpu_get_count(vm->id); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 775 | if (ret < 0) { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 776 | pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %lld", |
| 777 | vm->id, ret); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 778 | ret = -EIO; |
| 779 | goto fail_with_cleanup; |
| 780 | } |
| 781 | |
| 782 | /* Avoid overflowing the vcpu count. */ |
| 783 | if (ret > (U32_MAX - total_vcpu_count)) { |
| 784 | pr_err("Too many vcpus: %u\n", total_vcpu_count); |
| 785 | ret = -EDQUOT; |
| 786 | goto fail_with_cleanup; |
| 787 | } |
| 788 | |
| 789 | /* Confirm the maximum number of VCPUs looks sane. */ |
| 790 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1); |
| 791 | BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX); |
| 792 | |
| 793 | /* Enforce the limit on vcpus. */ |
| 794 | total_vcpu_count += ret; |
| 795 | if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) { |
| 796 | pr_err("Too many vcpus: %u\n", total_vcpu_count); |
| 797 | ret = -EDQUOT; |
| 798 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | vm->vcpu_count = ret; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 802 | vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu), |
| 803 | GFP_KERNEL); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 804 | if (!vm->vcpu) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 805 | pr_err("No memory for %u vcpus for vm %u", |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 806 | vm->vcpu_count, vm->id); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 807 | ret = -ENOMEM; |
| 808 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 811 | /* Update the number of initialized VMs. */ |
| 812 | hf_vm_count = i + 1; |
| 813 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 814 | /* Create a kernel thread for each vcpu. */ |
| 815 | for (j = 0; j < vm->vcpu_count; j++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 816 | struct hf_vcpu *vcpu = &vm->vcpu[j]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 817 | vcpu->task = kthread_create(hf_vcpu_thread, vcpu, |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 818 | "vcpu_thread_%u_%u", |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 819 | vm->id, j); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 820 | if (IS_ERR(vcpu->task)) { |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 821 | pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n", |
| 822 | vm->id, j, PTR_ERR(vcpu->task)); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 823 | vm->vcpu_count = j; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 824 | ret = PTR_ERR(vcpu->task); |
| 825 | goto fail_with_cleanup; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | get_task_struct(vcpu->task); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 829 | vcpu->vm = vm; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 830 | vcpu->vcpu_index = j; |
Wedson Almeida Filho | 7fe6233 | 2018-12-15 03:09:57 +0000 | [diff] [blame] | 831 | atomic_set(&vcpu->abort_sleep, 0); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 835 | /* Register protocol and socket family. */ |
| 836 | ret = proto_register(&hf_sock_proto, 0); |
| 837 | if (ret) { |
| 838 | pr_err("Unable to register protocol: %lld\n", ret); |
| 839 | goto fail_with_cleanup; |
| 840 | } |
| 841 | |
| 842 | ret = sock_register(&proto_family); |
| 843 | if (ret) { |
| 844 | pr_err("Unable to register Hafnium's socket family: %lld\n", |
| 845 | ret); |
| 846 | goto fail_unregister_proto; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Start running threads now that all is initialized. |
| 851 | * |
| 852 | * Any failures from this point on must also unregister the socket |
| 853 | * family with a call to sock_unregister(). |
| 854 | */ |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 855 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 856 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 857 | for (j = 0; j < vm->vcpu_count; j++) |
| 858 | wake_up_process(vm->vcpu[j].task); |
| 859 | } |
| 860 | |
| 861 | /* Dump vm/vcpu count info. */ |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 862 | pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 863 | for (i = 0; i < hf_vm_count; i++) { |
| 864 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 865 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 866 | pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 867 | } |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 868 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 869 | return 0; |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 870 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 871 | fail_unregister_proto: |
| 872 | proto_unregister(&hf_sock_proto); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 873 | fail_with_cleanup: |
| 874 | hf_free_resources(); |
| 875 | return ret; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 879 | * 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] | 880 | * unloading it. |
| 881 | */ |
| 882 | static void __exit hf_exit(void) |
| 883 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 884 | pr_info("Preparing to unload Hafnium\n"); |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 885 | sock_unregister(PF_HF); |
| 886 | proto_unregister(&hf_sock_proto); |
Andrew Scull | 82257c4 | 2018-10-01 10:37:48 +0100 | [diff] [blame] | 887 | hf_free_resources(); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 888 | pr_info("Hafnium ready to unload\n"); |
| 889 | } |
| 890 | |
Wedson Almeida Filho | 1ee3565 | 2018-12-24 01:36:48 +0000 | [diff] [blame] | 891 | MODULE_LICENSE("GPL v2"); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 892 | |
| 893 | module_init(hf_init); |
| 894 | module_exit(hf_exit); |