blob: 44c782edae90bc18e7eb05a11d2774c61c9a115f [file] [log] [blame]
Andrew Scull01778112019-01-14 15:37:53 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Walbran13c3a0b2018-11-30 11:51:53 +00002/*
Andrew Walbran2bc0a322019-03-07 15:48:06 +00003 * Copyright 2018 The Hafnium Authors.
Andrew Walbran13c3a0b2018-11-30 11:51:53 +00004 *
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 Walbran13c3a0b2018-11-30 11:51:53 +000013 */
14
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000015#include <clocksource/arm_arch_timer.h>
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000016#include <linux/atomic.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000017#include <linux/cpuhotplug.h>
18#include <linux/hrtimer.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010019#include <linux/init.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000020#include <linux/interrupt.h>
21#include <linux/irq.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010022#include <linux/kernel.h>
23#include <linux/kthread.h>
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +010024#include <linux/mm.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010025#include <linux/module.h>
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000026#include <linux/net.h>
27#include <linux/of.h>
28#include <linux/platform_device.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010029#include <linux/sched/task.h>
30#include <linux/slab.h>
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000031#include <net/sock.h>
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010032
Andrew Scull55704232018-08-10 17:19:54 +010033#include <hf/call.h>
Jose Marinho1cc6c752019-03-11 16:28:03 +000034#include <hf/spci.h>
Andrew Scull55704232018-08-10 17:19:54 +010035
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000036/* 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 Walbranb3ca1dc2019-01-30 17:13:44 +000040#define HYPERVISOR_TIMER_NAME "el2_timer"
41
Andrew Scull82257c42018-10-01 10:37:48 +010042#define CONFIG_HAFNIUM_MAX_VMS 16
43#define CONFIG_HAFNIUM_MAX_VCPUS 32
44
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000045#define FIRST_SECONDARY_VM_ID 1
46
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010047struct hf_vcpu {
Andrew Scullb722f952018-09-27 15:39:10 +010048 struct hf_vm *vm;
Andrew Scull55704232018-08-10 17:19:54 +010049 uint32_t vcpu_index;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010050 struct task_struct *task;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +000051 atomic_t abort_sleep;
Andrew Scull71f57362019-02-05 16:11:35 +000052 atomic_t waiting_for_message;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010053 struct hrtimer timer;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010054};
55
56struct hf_vm {
Andrew Scullb722f952018-09-27 15:39:10 +010057 uint32_t id;
Andrew Scullbb7ae412018-09-28 21:07:15 +010058 uint32_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010059 struct hf_vcpu *vcpu;
60};
61
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000062struct hf_msg_hdr {
63 uint64_t src_port;
64 uint64_t dst_port;
65};
66
67struct 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
80struct sockaddr_hf {
81 sa_family_t family;
82 uint32_t vm_id;
83 uint64_t port;
84};
85
86static struct proto hf_sock_proto = {
87 .name = "hafnium",
88 .owner = THIS_MODULE,
89 .obj_size = sizeof(struct hf_sock),
90};
91
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010092static struct hf_vm *hf_vms;
Andrew Scullbb7ae412018-09-28 21:07:15 +010093static uint32_t hf_vm_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000094static struct page *hf_send_page;
95static struct page *hf_recv_page;
96static atomic64_t hf_next_port = ATOMIC64_INIT(0);
97static DEFINE_SPINLOCK(hf_send_lock);
98static DEFINE_HASHTABLE(hf_local_port_hash, 7);
99static DEFINE_SPINLOCK(hf_local_port_hash_lock);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000100static int hf_irq;
Andrew Walbran8d55e502019-02-05 11:42:08 +0000101static enum cpuhp_state hf_cpuhp_state;
Jose Marinho1cc6c752019-03-11 16:28:03 +0000102static spci_vm_id_t current_vm_id;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100103
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100104/**
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000105 * Retrieves a VM from its ID, returning NULL if the VM doesn't exist.
106 */
107static struct hf_vm *hf_vm_from_id(uint32_t vm_id)
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 Filho7fe62332018-12-15 03:09:57 +0000117 * Wakes up the kernel thread responsible for running the given vcpu.
118 *
119 * Returns 0 if the thread was already running, 1 otherwise.
120 */
121static 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 */
137static 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 Filho2f62b422018-06-19 06:44:32 +0100153 * 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 */
156static 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 Filho7fe62332018-12-15 03:09:57 +0000159 /* TODO: Inject interrupt. */
160 hf_vcpu_wake_up(vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100161 return HRTIMER_NORESTART;
162}
163
164/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000165 * 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 Filho1ee35652018-12-24 01:36:48 +0000169 */
Andrew Scull71f57362019-02-05 16:11:35 +0000170static void hf_handle_wake_up_request(uint32_t vm_id, uint16_t vcpu)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000171{
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000172 struct hf_vm *vm = hf_vm_from_id(vm_id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000173
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000174 if (!vm) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000175 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
176 return;
177 }
178
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000179 if (vcpu >= vm->vcpu_count) {
Andrew Scull71f57362019-02-05 16:11:35 +0000180 pr_warn("Request to wake up non-existent vCPU: %u.%u\n",
181 vm_id, vcpu);
182 return;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000183 }
184
185 if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) {
186 /*
187 * The task was already running (presumably on a different
188 * physical CPU); interrupt it. This gives Hafnium a chance to
189 * inject any new interrupts.
190 */
191 kick_process(vm->vcpu[vcpu].task);
192 }
193}
194
195/**
Andrew Scull71f57362019-02-05 16:11:35 +0000196 * Injects an interrupt into a vCPU of the VM and ensures the vCPU will run to
197 * handle the interrupt.
198 */
199static void hf_interrupt_vm(uint32_t vm_id, uint64_t int_id)
200{
201 struct hf_vm *vm = hf_vm_from_id(vm_id);
202 uint16_t vcpu;
203 int64_t ret;
204
205 if (!vm) {
206 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
207 return;
208 }
209
210 /*
211 * TODO: For now we're picking the first vcpu to interrupt, but
212 * we want to be smarter.
213 */
214 vcpu = 0;
215 ret = hf_interrupt_inject(vm_id, vcpu, int_id);
216
217 if (ret == -1) {
218 pr_warn("Failed to inject interrupt %lld to vCPU %d of VM %d",
219 int_id, vcpu, vm_id);
220 return;
221 }
222
223 if (ret != 1) {
224 /* We don't need to wake up the vcpu. */
225 return;
226 }
227
228 hf_handle_wake_up_request(vm_id, vcpu);
229}
230
231/**
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000232 * Notify all waiters on the given VM.
233 */
234static void hf_notify_waiters(uint32_t vm_id)
235{
Andrew Scull71f57362019-02-05 16:11:35 +0000236 int64_t waiter_vm_id;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000237
Andrew Scull71f57362019-02-05 16:11:35 +0000238 while ((waiter_vm_id = hf_mailbox_waiter_get(vm_id)) != -1) {
239 if (waiter_vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000240 /*
241 * TODO: Use this information when implementing per-vm
242 * queues.
243 */
244 } else {
Andrew Scull71f57362019-02-05 16:11:35 +0000245 hf_interrupt_vm(waiter_vm_id,
246 HF_MAILBOX_WRITABLE_INTID);
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000247 }
248 }
249}
250
251/**
Andrew Scull71f57362019-02-05 16:11:35 +0000252 * Delivers a message to a VM.
253 */
254static void hf_deliver_message(uint32_t vm_id)
255{
256 struct hf_vm *vm = hf_vm_from_id(vm_id);
257 uint32_t i;
258
259 if (!vm) {
260 pr_warn("Tried to deliver message to non-existent VM id: %u\n",
261 vm_id);
262 return;
263 }
264
265 /* Try to wake a vCPU that is waiting for a message. */
266 for (i = 0; i < vm->vcpu_count; i++) {
267 if (atomic_read(&vm->vcpu[i].waiting_for_message)) {
268 hf_handle_wake_up_request(vm->id,
269 vm->vcpu[i].vcpu_index);
270 return;
271 }
272 }
273
274 /* None were waiting for a message so interrupt one. */
275 hf_interrupt_vm(vm->id, HF_MAILBOX_READABLE_INTID);
276}
277
278/**
Andrew Sculldf6478f2019-02-19 17:52:08 +0000279 * Handles a message delivered to this VM by validating that it's well-formed
280 * and then queueing it for delivery to the appropriate socket.
281 */
282static void hf_handle_message(struct hf_vm *sender, const void *ptr, size_t len)
283{
284 struct hf_sock *hsock;
285 const struct hf_msg_hdr *hdr = ptr;
286 struct sk_buff *skb;
287 int err;
288
289 /* Ignore messages that are too small to hold a header. */
290 if (len < sizeof(struct hf_msg_hdr))
291 return;
292
293 len -= sizeof(struct hf_msg_hdr);
294
295 /* Go through the colliding sockets. */
296 rcu_read_lock();
297 hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node,
298 hdr->dst_port) {
299 if (hsock->peer_vm == sender &&
300 hsock->remote_port == hdr->src_port) {
301 sock_hold(&hsock->sk);
302 break;
303 }
304 }
305 rcu_read_unlock();
306
307 /* Nothing to do if we couldn't find the target. */
308 if (!hsock)
309 return;
310
311 /*
312 * TODO: From this point on, there are two failure paths: when we
313 * create the skb below, and when we enqueue it to the socket. What
314 * should we do if they fail? Ideally we would have some form of flow
315 * control to prevent message loss, but how to do it efficiently?
316 *
317 * One option is to have a pre-allocated message that indicates to the
318 * sender that a message was dropped. This way we guarantee that the
319 * sender will be aware of loss and should back-off.
320 */
321 /* Create the skb. */
322 skb = alloc_skb(len, GFP_KERNEL);
323 if (!skb)
324 goto exit;
325
326 memcpy(skb_put(skb, len), hdr + 1, len);
327
328 /*
329 * Add the skb to the receive queue of the target socket. On success it
330 * calls sk->sk_data_ready, which is currently set to sock_def_readable,
331 * which wakes up any waiters.
332 */
333 err = sock_queue_rcv_skb(&hsock->sk, skb);
334 if (err)
335 kfree_skb(skb);
336
337exit:
338 sock_put(&hsock->sk);
Andrew Scull71f57362019-02-05 16:11:35 +0000339
340 if (hf_mailbox_clear() == 1)
341 hf_notify_waiters(HF_PRIMARY_VM_ID);
Andrew Sculldf6478f2019-02-19 17:52:08 +0000342}
343
344/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100345 * This is the main loop of each vcpu.
346 */
347static int hf_vcpu_thread(void *data)
348{
349 struct hf_vcpu *vcpu = data;
Andrew Sculldc8cab52018-10-10 18:29:39 +0100350 struct hf_vcpu_run_return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100351
352 hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
353 vcpu->timer.function = &hf_vcpu_timer_expired;
354
355 while (!kthread_should_stop()) {
Andrew Scull01f83de2019-01-23 13:41:47 +0000356 uint32_t i;
357
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000358 /*
359 * We're about to run the vcpu, so we can reset the abort-sleep
360 * flag.
361 */
362 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100363
Andrew Scullbb7ae412018-09-28 21:07:15 +0100364 /* Call into Hafnium to run vcpu. */
Andrew Scullb722f952018-09-27 15:39:10 +0100365 ret = hf_vcpu_run(vcpu->vm->id, vcpu->vcpu_index);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100366
Andrew Sculldc8cab52018-10-10 18:29:39 +0100367 switch (ret.code) {
Andrew Sculle05702e2019-01-08 14:46:46 +0000368 /* Preempted. */
369 case HF_VCPU_RUN_PREEMPTED:
370 if (need_resched())
371 schedule();
372 break;
373
374 /* Yield. */
Andrew Scullb3a61b52018-09-17 14:30:34 +0100375 case HF_VCPU_RUN_YIELD:
Andrew Sculle05702e2019-01-08 14:46:46 +0000376 if (!kthread_should_stop())
377 schedule();
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100378 break;
379
Andrew Scull01778112019-01-14 15:37:53 +0000380 /* WFI. */
Andrew Scullb3a61b52018-09-17 14:30:34 +0100381 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
Andrew Scull71f57362019-02-05 16:11:35 +0000382 if (ret.sleep.ns != HF_SLEEP_INDEFINITE) {
383 hrtimer_start(&vcpu->timer, ret.sleep.ns,
384 HRTIMER_MODE_REL);
385 }
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000386 hf_vcpu_sleep(vcpu);
Andrew Scull71f57362019-02-05 16:11:35 +0000387 hrtimer_cancel(&vcpu->timer);
388 break;
389
390 /* Waiting for a message. */
391 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
392 atomic_set(&vcpu->waiting_for_message, 1);
393 if (ret.sleep.ns != HF_SLEEP_INDEFINITE) {
394 hrtimer_start(&vcpu->timer, ret.sleep.ns,
395 HRTIMER_MODE_REL);
396 }
397 hf_vcpu_sleep(vcpu);
398 hrtimer_cancel(&vcpu->timer);
399 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100400 break;
401
Andrew Scullb3a61b52018-09-17 14:30:34 +0100402 /* Wake up another vcpu. */
403 case HF_VCPU_RUN_WAKE_UP:
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000404 hf_handle_wake_up_request(ret.wake_up.vm_id,
Andrew Scull71f57362019-02-05 16:11:35 +0000405 ret.wake_up.vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100406 break;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100407
Andrew Scullb3a61b52018-09-17 14:30:34 +0100408 /* Response available. */
Andrew Scull0973a2e2018-10-05 11:11:24 +0100409 case HF_VCPU_RUN_MESSAGE:
Andrew Scull71f57362019-02-05 16:11:35 +0000410 if (ret.message.vm_id == HF_PRIMARY_VM_ID) {
411 hf_handle_message(vcpu->vm,
412 page_address(hf_recv_page),
413 ret.message.size);
414 } else {
415 hf_deliver_message(ret.message.vm_id);
416 }
Andrew Sculldc8cab52018-10-10 18:29:39 +0100417 break;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000418
419 /* Notify all waiters. */
420 case HF_VCPU_RUN_NOTIFY_WAITERS:
421 hf_notify_waiters(vcpu->vm->id);
422 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000423
Andrew Scull71f57362019-02-05 16:11:35 +0000424 /* Abort was triggered. */
Andrew Scull01f83de2019-01-23 13:41:47 +0000425 case HF_VCPU_RUN_ABORTED:
426 for (i = 0; i < vcpu->vm->vcpu_count; i++) {
427 if (i == vcpu->vcpu_index)
428 continue;
Andrew Scull71f57362019-02-05 16:11:35 +0000429 hf_handle_wake_up_request(vcpu->vm->id, i);
Andrew Scull01f83de2019-01-23 13:41:47 +0000430 }
431 hf_vcpu_sleep(vcpu);
432 break;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100433 }
434 }
435
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100436 return 0;
437}
438
439/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000440 * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It
441 * relies on the fact that the first field of hf_sock is a sock.
442 */
443static struct hf_sock *hsock_from_sk(struct sock *sk)
444{
445 return (struct hf_sock *)sk;
446}
447
448/**
449 * This is called when the last reference to the outer socket is released. For
450 * example, if it's a user-space socket, when the last file descriptor pointing
451 * to this socket is closed.
452 *
453 * It begins cleaning up resources, though some can only be cleaned up after all
454 * references to the underlying socket are released, which is handled by
455 * hf_sock_destruct().
456 */
457static int hf_sock_release(struct socket *sock)
458{
459 struct sock *sk = sock->sk;
460 struct hf_sock *hsock = hsock_from_sk(sk);
461 unsigned long flags;
462
463 if (!sk)
464 return 0;
465
466 /* Shutdown for both send and receive. */
467 lock_sock(sk);
468 sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN;
469 sk->sk_state_change(sk);
470 release_sock(sk);
471
472 /* Remove from the hash table, so lookups from now on won't find it. */
473 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
474 hash_del_rcu(&hsock->sk.sk_node);
475 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
476
477 /*
478 * TODO: When we implement a tx queue, we need to clear it here so that
479 * sk_wmem_alloc will not prevent sk from being freed (sk_free).
480 */
481
482 /*
483 * Wait for in-flight lookups to finish. We need to do this here because
Wedson Almeida Filho89d0e472019-01-03 19:18:39 +0000484 * in-flight lookups rely on the reference to the socket we're about to
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000485 * release.
486 */
487 synchronize_rcu();
488 sock_put(sk);
489 sock->sk = NULL;
490
491 return 0;
492}
493
494/**
495 * This is called when there are no more references to the socket. It frees all
496 * resources that haven't been freed during release.
497 */
498static void hf_sock_destruct(struct sock *sk)
499{
500 /*
501 * Clear the receive queue now that the handler cannot add any more
502 * skbs to it.
503 */
504 skb_queue_purge(&sk->sk_receive_queue);
505}
506
507/**
508 * Connects the Hafnium socket to the provided VM and port. After the socket is
509 * connected, it can be used to exchange datagrams with the specified peer.
510 */
Andrew Scull01778112019-01-14 15:37:53 +0000511static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len,
512 int connect_flags)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000513{
514 struct sock *sk = sock->sk;
515 struct hf_sock *hsock = hsock_from_sk(sk);
516 struct hf_vm *vm;
517 struct sockaddr_hf *addr;
518 int err;
519 unsigned long flags;
520
521 /* Basic address validation. */
522 if (len < sizeof(struct sockaddr_hf) || saddr->sa_family != AF_HF)
523 return -EINVAL;
524
525 addr = (struct sockaddr_hf *)saddr;
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000526 vm = hf_vm_from_id(addr->vm_id);
527 if (!vm)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000528 return -ENETUNREACH;
529
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000530 /*
531 * TODO: Once we implement access control in Hafnium, check that the
532 * caller is allowed to contact the specified VM. Return -ECONNREFUSED
533 * if access is denied.
534 */
535
536 /* Take lock to make sure state doesn't change as we connect. */
537 lock_sock(sk);
538
539 /* Only unconnected sockets are allowed to become connected. */
540 if (sock->state != SS_UNCONNECTED) {
541 err = -EISCONN;
542 goto exit;
543 }
544
545 hsock->local_port = atomic64_inc_return(&hf_next_port);
546 hsock->remote_port = addr->port;
547 hsock->peer_vm = vm;
548
549 sock->state = SS_CONNECTED;
550
551 /* Add socket to hash table now that it's fully initialised. */
552 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
553 hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port);
554 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
555
556 err = 0;
557exit:
558 release_sock(sk);
559 return err;
560}
561
562/**
563 * Sends the given skb to the appropriate VM by calling Hafnium. It will also
564 * trigger the wake up of a recipient VM.
565 *
566 * Takes ownership of the skb on success.
567 */
568static int hf_send_skb(struct sk_buff *skb)
569{
570 unsigned long flags;
571 int64_t ret;
572 struct hf_sock *hsock = hsock_from_sk(skb->sk);
573 struct hf_vm *vm = hsock->peer_vm;
Jose Marinho1cc6c752019-03-11 16:28:03 +0000574 struct spci_message *message = page_address(hf_send_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000575
576 /*
577 * Call Hafnium under the send lock so that we serialize the use of the
578 * global send buffer.
579 */
580 spin_lock_irqsave(&hf_send_lock, flags);
Jose Marinho1cc6c752019-03-11 16:28:03 +0000581 memcpy(message->payload, skb->data, skb->len);
582 spci_message_init(message, skb->len,
583 vm->id, current_vm_id);
584
585 ret = spci_msg_send(0);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000586 spin_unlock_irqrestore(&hf_send_lock, flags);
587
588 if (ret < 0)
589 return -EAGAIN;
590
Andrew Scull71f57362019-02-05 16:11:35 +0000591 /* Ensure the VM will run to pick up the message. */
592 hf_deliver_message(vm->id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000593
594 kfree_skb(skb);
595
596 return 0;
597}
598
599/**
600 * Determines if the given socket is in the connected state. It acquires and
601 * releases the socket lock.
602 */
603static bool hf_sock_is_connected(struct socket *sock)
604{
605 bool ret;
606
607 lock_sock(sock->sk);
608 ret = sock->state == SS_CONNECTED;
609 release_sock(sock->sk);
610
611 return ret;
612}
613
614/**
615 * Sends a message to the VM & port the socket is connected to. All variants
616 * of write/send/sendto/sendmsg eventually call this function.
617 */
618static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
619{
620 struct sock *sk = sock->sk;
621 struct sk_buff *skb;
622 int err;
623 struct hf_msg_hdr *hdr;
624 struct hf_sock *hsock = hsock_from_sk(sk);
Andrew Scull614ed7f2019-04-01 12:12:38 +0100625 size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct spci_message)
626 - sizeof(struct hf_msg_hdr);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000627
628 /* Check length. */
Andrew Scull614ed7f2019-04-01 12:12:38 +0100629 if (len > payload_max_len)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000630 return -EMSGSIZE;
631
632 /* We don't allow the destination address to be specified. */
633 if (m->msg_namelen > 0)
634 return -EISCONN;
635
636 /* We don't support out of band messages. */
637 if (m->msg_flags & MSG_OOB)
638 return -EOPNOTSUPP;
639
640 /*
641 * Ensure that the socket is connected. We don't need to hold the socket
642 * lock (acquired and released by hf_sock_is_connected) for the
643 * remainder of the function because the fields we care about are
644 * immutable once the state is SS_CONNECTED.
645 */
646 if (!hf_sock_is_connected(sock))
647 return -ENOTCONN;
648
649 /*
650 * Allocate an skb for this write. If there isn't enough room in the
651 * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block
652 * (if it's a blocking call). On success, it increments sk_wmem_alloc
653 * and sets up the skb such that sk_wmem_alloc gets decremented when
654 * the skb is freed (sock_wfree gets called).
655 */
656 skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr),
657 m->msg_flags & MSG_DONTWAIT, &err);
658 if (!skb)
659 return err;
660
661 /* Reserve room for the header and initialise it. */
662 skb_reserve(skb, sizeof(struct hf_msg_hdr));
663 hdr = skb_push(skb, sizeof(struct hf_msg_hdr));
664 hdr->src_port = hsock->local_port;
665 hdr->dst_port = hsock->remote_port;
666
667 /* Allocate area for the contents, then copy into skb. */
668 if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) {
669 err = -EFAULT;
670 goto err_cleanup;
671 }
672
673 /*
674 * TODO: We currently do this inline, but when we have support for
675 * readiness notification from Hafnium, we must add this to a per-VM tx
676 * queue that can make progress when the VM becomes writable. This will
677 * fix send buffering and poll readiness notification.
678 */
679 err = hf_send_skb(skb);
680 if (err)
681 goto err_cleanup;
682
683 return 0;
684
685err_cleanup:
686 kfree_skb(skb);
687 return err;
688}
689
690/**
691 * Receives a message originated from the VM & port the socket is connected to.
692 * All variants of read/recv/recvfrom/recvmsg eventually call this function.
693 */
694static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
695 int flags)
696{
697 struct sock *sk = sock->sk;
698 struct sk_buff *skb;
699 int err;
700 size_t copy_len;
701
702 if (!hf_sock_is_connected(sock))
703 return -ENOTCONN;
704
705 /* Grab the next skb from the receive queue. */
706 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
707 if (!skb)
708 return err;
709
710 /* Make sure we don't copy more than what fits in the output buffer. */
711 copy_len = skb->len;
712 if (copy_len > len) {
713 copy_len = len;
714 m->msg_flags |= MSG_TRUNC;
715 }
716
717 /* Make sure we don't overflow the return value type. */
718 if (copy_len > INT_MAX) {
719 copy_len = INT_MAX;
720 m->msg_flags |= MSG_TRUNC;
721 }
722
723 /* Copy skb to output iterator, then free it. */
724 err = skb_copy_datagram_msg(skb, 0, m, copy_len);
725 skb_free_datagram(sk, skb);
726 if (err)
727 return err;
728
729 return copy_len;
730}
731
732/**
733 * This function is called when a Hafnium socket is created. It initialises all
734 * state such that the caller will be able to connect the socket and then send
735 * and receive messages through it.
736 */
737static int hf_sock_create(struct net *net, struct socket *sock, int protocol,
Andrew Scull01778112019-01-14 15:37:53 +0000738 int kern)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000739{
740 static const struct proto_ops ops = {
741 .family = PF_HF,
742 .owner = THIS_MODULE,
743 .release = hf_sock_release,
744 .bind = sock_no_bind,
745 .connect = hf_sock_connect,
746 .socketpair = sock_no_socketpair,
747 .accept = sock_no_accept,
748 .ioctl = sock_no_ioctl,
749 .listen = sock_no_listen,
750 .shutdown = sock_no_shutdown,
751 .setsockopt = sock_no_setsockopt,
752 .getsockopt = sock_no_getsockopt,
753 .sendmsg = hf_sock_sendmsg,
754 .recvmsg = hf_sock_recvmsg,
755 .mmap = sock_no_mmap,
756 .sendpage = sock_no_sendpage,
757 .poll = datagram_poll,
758 };
759 struct sock *sk;
760
761 if (sock->type != SOCK_DGRAM)
762 return -ESOCKTNOSUPPORT;
763
764 if (protocol != 0)
765 return -EPROTONOSUPPORT;
766
767 /*
768 * For now we only allow callers with sys admin capability to create
769 * Hafnium sockets.
770 */
771 if (!capable(CAP_SYS_ADMIN))
772 return -EPERM;
773
774 /* Allocate and initialise socket. */
775 sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern);
776 if (!sk)
777 return -ENOMEM;
778
779 sock_init_data(sock, sk);
780
781 sk->sk_destruct = hf_sock_destruct;
782 sock->ops = &ops;
783 sock->state = SS_UNCONNECTED;
784
785 return 0;
786}
787
788/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100789 * Frees all resources, including threads, associated with the Hafnium driver.
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100790 */
Andrew Scull82257c42018-10-01 10:37:48 +0100791static void hf_free_resources(void)
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100792{
Andrew Scullbb7ae412018-09-28 21:07:15 +0100793 uint32_t i, j;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100794
795 /*
796 * First stop all worker threads. We need to do this before freeing
797 * resources because workers may reference each other, so it is only
798 * safe to free resources after they have all stopped.
799 */
Andrew Scull82257c42018-10-01 10:37:48 +0100800 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100801 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000802
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100803 for (j = 0; j < vm->vcpu_count; j++)
804 kthread_stop(vm->vcpu[j].task);
805 }
806
807 /* Free resources. */
Andrew Scull82257c42018-10-01 10:37:48 +0100808 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100809 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000810
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100811 for (j = 0; j < vm->vcpu_count; j++)
812 put_task_struct(vm->vcpu[j].task);
813 kfree(vm->vcpu);
814 }
815
816 kfree(hf_vms);
817}
818
Andrew Scullbb7ae412018-09-28 21:07:15 +0100819/**
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000820 * Handles the hypervisor timer interrupt.
821 */
822static irqreturn_t hf_nop_irq_handler(int irq, void *dev)
823{
824 /*
825 * No need to do anything, the interrupt only exists to return to the
826 * primary vCPU so that the virtual timer will be restored and fire as
827 * normal.
828 */
829 return IRQ_HANDLED;
830}
831
832/**
833 * Enables the hypervisor timer interrupt on a CPU, when it starts or after the
834 * driver is first loaded.
835 */
836static int hf_starting_cpu(unsigned int cpu)
837{
838 if (hf_irq != 0) {
839 /* Enable the interrupt, and set it to be edge-triggered. */
840 enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING);
841 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000842
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000843 return 0;
844}
845
846/**
847 * Disables the hypervisor timer interrupt on a CPU when it is powered down.
848 */
849static int hf_dying_cpu(unsigned int cpu)
850{
851 if (hf_irq != 0) {
852 /* Disable the interrupt while the CPU is asleep. */
853 disable_percpu_irq(hf_irq);
854 }
855
856 return 0;
857}
858
859/**
860 * Registers for the hypervisor timer interrupt.
861 */
862static int hf_int_driver_probe(struct platform_device *pdev)
863{
864 int irq;
865 int ret;
866
867 /*
868 * Register a handler for the hyperviser timer IRQ, as it is needed for
869 * Hafnium to emulate the virtual timer for Linux while a secondary vCPU
870 * is running.
871 */
872 irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI);
873 if (irq < 0) {
874 pr_err("Error getting hypervisor timer IRQ: %d\n", irq);
875 return irq;
876 }
877 hf_irq = irq;
878
879 ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME,
880 pdev);
881 if (ret != 0) {
882 pr_err("Error registering hypervisor timer IRQ %d: %d\n",
883 irq, ret);
884 return ret;
885 }
886 pr_info("Hafnium registered for IRQ %d\n", irq);
887 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
888 "hafnium/hypervisor_timer:starting",
889 hf_starting_cpu, hf_dying_cpu);
890 if (ret < 0) {
891 pr_err("Error enabling timer on all CPUs: %d\n", ret);
Andrew Walbran8d55e502019-02-05 11:42:08 +0000892 free_percpu_irq(irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000893 return ret;
894 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000895 hf_cpuhp_state = ret;
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000896
897 return 0;
898}
899
900/**
901 * Unregisters for the hypervisor timer interrupt.
902 */
903static int hf_int_driver_remove(struct platform_device *pdev)
904{
Andrew Walbran8d55e502019-02-05 11:42:08 +0000905 /*
906 * This will cause hf_dying_cpu to be called on each CPU, which will
907 * disable the IRQs.
908 */
909 cpuhp_remove_state(hf_cpuhp_state);
910 free_percpu_irq(hf_irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000911
912 return 0;
913}
914
915static const struct of_device_id hf_int_driver_id[] = {
916 {.compatible = "arm,armv7-timer"},
917 {.compatible = "arm,armv8-timer"},
918 {}
919};
920
921static struct platform_driver hf_int_driver = {
922 .driver = {
923 .name = HYPERVISOR_TIMER_NAME,
924 .owner = THIS_MODULE,
925 .of_match_table = of_match_ptr(hf_int_driver_id),
926 },
927 .probe = hf_int_driver_probe,
928 .remove = hf_int_driver_remove,
929};
930
931/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100932 * Initializes the Hafnium driver by creating a thread for each vCPU of each
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100933 * virtual machine.
934 */
935static int __init hf_init(void)
936{
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000937 static const struct net_proto_family proto_family = {
938 .family = PF_HF,
939 .create = hf_sock_create,
940 .owner = THIS_MODULE,
941 };
Andrew Scullbb7ae412018-09-28 21:07:15 +0100942 int64_t ret;
943 uint32_t i, j;
Andrew Scull82257c42018-10-01 10:37:48 +0100944 uint32_t total_vm_count;
945 uint32_t total_vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100946
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100947 /* Allocate a page for send and receive buffers. */
948 hf_send_page = alloc_page(GFP_KERNEL);
949 if (!hf_send_page) {
950 pr_err("Unable to allocate send buffer\n");
951 return -ENOMEM;
952 }
953
954 hf_recv_page = alloc_page(GFP_KERNEL);
955 if (!hf_recv_page) {
956 __free_page(hf_send_page);
957 pr_err("Unable to allocate receive buffer\n");
958 return -ENOMEM;
959 }
960
961 /*
962 * Configure both addresses. Once configured, we cannot free these pages
963 * because the hypervisor will use them, even if the module is
964 * unloaded.
965 */
Andrew Scull55704232018-08-10 17:19:54 +0100966 ret = hf_vm_configure(page_to_phys(hf_send_page),
967 page_to_phys(hf_recv_page));
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100968 if (ret) {
969 __free_page(hf_send_page);
970 __free_page(hf_recv_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000971 /*
972 * TODO: We may want to grab this information from hypervisor
973 * and go from there.
974 */
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100975 pr_err("Unable to configure VM\n");
976 return -EIO;
977 }
978
Andrew Scull82257c42018-10-01 10:37:48 +0100979 /* Get the number of VMs. */
Andrew Scull55704232018-08-10 17:19:54 +0100980 ret = hf_vm_get_count();
Andrew Scull82257c42018-10-01 10:37:48 +0100981 if (ret < 0) {
Andrew Scullbb7ae412018-09-28 21:07:15 +0100982 pr_err("Unable to retrieve number of VMs: %lld\n", ret);
Andrew Scull82257c42018-10-01 10:37:48 +0100983 return -EIO;
984 }
985
986 /* Confirm the maximum number of VMs looks sane. */
987 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1);
988 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX);
989
990 /* Validate the number of VMs. There must at least be the primary. */
991 if (ret < 1 || ret > CONFIG_HAFNIUM_MAX_VMS) {
992 pr_err("Number of VMs is out of range: %lld\n", ret);
993 return -EDQUOT;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100994 }
995
Andrew Scullb722f952018-09-27 15:39:10 +0100996 /* Only track the secondary VMs. */
Andrew Scull82257c42018-10-01 10:37:48 +0100997 total_vm_count = ret - 1;
Andrew Scull01778112019-01-14 15:37:53 +0000998 hf_vms =
999 kmalloc_array(total_vm_count, sizeof(struct hf_vm), GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001000 if (!hf_vms)
1001 return -ENOMEM;
1002
Jose Marinho1cc6c752019-03-11 16:28:03 +00001003 /* Cache the VM id for later usage. */
1004 current_vm_id = hf_vm_get_id();
1005
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001006 /* Initialize each VM. */
Andrew Scull82257c42018-10-01 10:37:48 +01001007 total_vcpu_count = 0;
1008 for (i = 0; i < total_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001009 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001010
Andrew Scullb722f952018-09-27 15:39:10 +01001011 /* Adjust the ID as only the secondaries are tracked. */
Wedson Almeida Filhoec841932019-01-22 23:07:50 +00001012 vm->id = i + FIRST_SECONDARY_VM_ID;
Andrew Scullb722f952018-09-27 15:39:10 +01001013
1014 ret = hf_vcpu_get_count(vm->id);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001015 if (ret < 0) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001016 pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %lld",
1017 vm->id, ret);
Andrew Scull82257c42018-10-01 10:37:48 +01001018 ret = -EIO;
1019 goto fail_with_cleanup;
1020 }
1021
1022 /* Avoid overflowing the vcpu count. */
1023 if (ret > (U32_MAX - total_vcpu_count)) {
1024 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1025 ret = -EDQUOT;
1026 goto fail_with_cleanup;
1027 }
1028
1029 /* Confirm the maximum number of VCPUs looks sane. */
1030 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1);
1031 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX);
1032
1033 /* Enforce the limit on vcpus. */
1034 total_vcpu_count += ret;
1035 if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) {
1036 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1037 ret = -EDQUOT;
1038 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001039 }
1040
1041 vm->vcpu_count = ret;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001042 vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu),
1043 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001044 if (!vm->vcpu) {
Andrew Scull82257c42018-10-01 10:37:48 +01001045 ret = -ENOMEM;
1046 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001047 }
1048
Andrew Scull82257c42018-10-01 10:37:48 +01001049 /* Update the number of initialized VMs. */
1050 hf_vm_count = i + 1;
1051
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001052 /* Create a kernel thread for each vcpu. */
1053 for (j = 0; j < vm->vcpu_count; j++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001054 struct hf_vcpu *vcpu = &vm->vcpu[j];
Andrew Scull01778112019-01-14 15:37:53 +00001055
1056 vcpu->task =
1057 kthread_create(hf_vcpu_thread, vcpu,
1058 "vcpu_thread_%u_%u", vm->id, j);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001059 if (IS_ERR(vcpu->task)) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001060 pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n",
1061 vm->id, j, PTR_ERR(vcpu->task));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001062 vm->vcpu_count = j;
Andrew Scull82257c42018-10-01 10:37:48 +01001063 ret = PTR_ERR(vcpu->task);
1064 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001065 }
1066
1067 get_task_struct(vcpu->task);
Andrew Scullb722f952018-09-27 15:39:10 +01001068 vcpu->vm = vm;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001069 vcpu->vcpu_index = j;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +00001070 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001071 }
1072 }
1073
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001074 /* Register protocol and socket family. */
1075 ret = proto_register(&hf_sock_proto, 0);
1076 if (ret) {
1077 pr_err("Unable to register protocol: %lld\n", ret);
1078 goto fail_with_cleanup;
1079 }
1080
1081 ret = sock_register(&proto_family);
1082 if (ret) {
1083 pr_err("Unable to register Hafnium's socket family: %lld\n",
1084 ret);
1085 goto fail_unregister_proto;
1086 }
1087
1088 /*
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001089 * Register as a driver for the timer device, so we can register a
1090 * handler for the hyperviser timer IRQ.
1091 */
1092 ret = platform_driver_register(&hf_int_driver);
1093 if (ret != 0) {
1094 pr_err("Error registering timer driver %lld\n", ret);
1095 goto fail_unregister_socket;
1096 }
1097
1098 /*
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001099 * Start running threads now that all is initialized.
1100 *
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001101 * Any failures from this point on must also unregister the driver with
1102 * platform_driver_unregister().
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001103 */
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001104 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001105 struct hf_vm *vm = &hf_vms[i];
Andrew Scull01778112019-01-14 15:37:53 +00001106
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001107 for (j = 0; j < vm->vcpu_count; j++)
1108 wake_up_process(vm->vcpu[j].task);
1109 }
1110
1111 /* Dump vm/vcpu count info. */
Andrew Scullbb7ae412018-09-28 21:07:15 +01001112 pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001113 for (i = 0; i < hf_vm_count; i++) {
1114 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001115
Andrew Scullbb7ae412018-09-28 21:07:15 +01001116 pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001117 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001118
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001119 return 0;
Andrew Scull82257c42018-10-01 10:37:48 +01001120
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001121fail_unregister_socket:
1122 sock_unregister(PF_HF);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001123fail_unregister_proto:
1124 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001125fail_with_cleanup:
1126 hf_free_resources();
1127 return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001128}
1129
1130/**
Andrew Scullbb7ae412018-09-28 21:07:15 +01001131 * Frees up all resources used by the Hafnium driver in preparation for
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001132 * unloading it.
1133 */
1134static void __exit hf_exit(void)
1135{
Andrew Scullbb7ae412018-09-28 21:07:15 +01001136 pr_info("Preparing to unload Hafnium\n");
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001137 sock_unregister(PF_HF);
1138 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001139 hf_free_resources();
Andrew Walbran8d55e502019-02-05 11:42:08 +00001140 platform_driver_unregister(&hf_int_driver);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001141 pr_info("Hafnium ready to unload\n");
1142}
1143
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001144MODULE_LICENSE("GPL v2");
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001145
1146module_init(hf_init);
1147module_exit(hf_exit);