blob: 20a67a1b7f68f8effbb5682e34aec3d404830d9d [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>
Fuad Tabba3e669bc2019-08-08 16:43:55 +010035#include <hf/transport.h>
Andrew Scull55704232018-08-10 17:19:54 +010036
Fuad Tabba3e669bc2019-08-08 16:43:55 +010037#include "uapi/hf/socket.h"
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000038
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000039#define HYPERVISOR_TIMER_NAME "el2_timer"
40
Andrew Scull82257c42018-10-01 10:37:48 +010041#define CONFIG_HAFNIUM_MAX_VMS 16
42#define CONFIG_HAFNIUM_MAX_VCPUS 32
43
Fuad Tabba5da4b6b2019-08-05 13:56:20 +010044#define FIRST_SECONDARY_VM_ID (HF_VM_ID_OFFSET + 1)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000045
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010046struct hf_vcpu {
Andrew Scullb722f952018-09-27 15:39:10 +010047 struct hf_vm *vm;
Andrew Walbranddc33942019-06-25 18:32:02 +010048 spci_vcpu_index_t vcpu_index;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010049 struct task_struct *task;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +000050 atomic_t abort_sleep;
Andrew Scull71f57362019-02-05 16:11:35 +000051 atomic_t waiting_for_message;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010052 struct hrtimer timer;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010053};
54
55struct hf_vm {
Andrew Walbran4c96d0c2019-06-25 18:32:56 +010056 spci_vm_id_t id;
Andrew Walbran3eeb1de2019-06-25 18:32:30 +010057 spci_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010058 struct hf_vcpu *vcpu;
59};
60
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000061struct hf_sock {
62 /* This needs to be the first field. */
63 struct sock sk;
64
65 /*
66 * The following fields are immutable after the socket transitions to
67 * SS_CONNECTED state.
68 */
69 uint64_t local_port;
70 uint64_t remote_port;
71 struct hf_vm *peer_vm;
72};
73
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000074static struct proto hf_sock_proto = {
75 .name = "hafnium",
76 .owner = THIS_MODULE,
77 .obj_size = sizeof(struct hf_sock),
78};
79
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010080static struct hf_vm *hf_vms;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +010081static spci_vm_count_t hf_vm_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +000082static struct page *hf_send_page;
83static struct page *hf_recv_page;
84static atomic64_t hf_next_port = ATOMIC64_INIT(0);
85static DEFINE_SPINLOCK(hf_send_lock);
86static DEFINE_HASHTABLE(hf_local_port_hash, 7);
87static DEFINE_SPINLOCK(hf_local_port_hash_lock);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +000088static int hf_irq;
Andrew Walbran8d55e502019-02-05 11:42:08 +000089static enum cpuhp_state hf_cpuhp_state;
Jose Marinho1cc6c752019-03-11 16:28:03 +000090static spci_vm_id_t current_vm_id;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010091
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +010092/**
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000093 * Retrieves a VM from its ID, returning NULL if the VM doesn't exist.
94 */
Andrew Walbran01390ae2019-06-19 11:32:46 +010095static struct hf_vm *hf_vm_from_id(spci_vm_id_t vm_id)
Wedson Almeida Filhoec841932019-01-22 23:07:50 +000096{
97 if (vm_id < FIRST_SECONDARY_VM_ID ||
98 vm_id >= FIRST_SECONDARY_VM_ID + hf_vm_count)
99 return NULL;
100
101 return &hf_vms[vm_id - FIRST_SECONDARY_VM_ID];
102}
103
104/**
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000105 * Wakes up the kernel thread responsible for running the given vcpu.
106 *
107 * Returns 0 if the thread was already running, 1 otherwise.
108 */
109static int hf_vcpu_wake_up(struct hf_vcpu *vcpu)
110{
111 /* Set a flag indicating that the thread should not go to sleep. */
112 atomic_set(&vcpu->abort_sleep, 1);
113
114 /* Set the thread to running state. */
115 return wake_up_process(vcpu->task);
116}
117
118/**
119 * Puts the current thread to sleep. The current thread must be responsible for
120 * running the given vcpu.
121 *
122 * Going to sleep will fail if hf_vcpu_wake_up() or kthread_stop() was called on
123 * this vcpu/thread since the last time it [re]started running.
124 */
125static void hf_vcpu_sleep(struct hf_vcpu *vcpu)
126{
127 int abort;
128
129 set_current_state(TASK_INTERRUPTIBLE);
130
131 /* Check the sleep-abort flag after making thread interruptible. */
132 abort = atomic_read(&vcpu->abort_sleep);
133 if (!abort && !kthread_should_stop())
134 schedule();
135
136 /* Set state back to running on the way out. */
137 set_current_state(TASK_RUNNING);
138}
139
140/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100141 * Wakes up the thread associated with the vcpu that owns the given timer. This
142 * is called when the timer the thread is waiting on expires.
143 */
144static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer)
145{
146 struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer);
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000147 /* TODO: Inject interrupt. */
148 hf_vcpu_wake_up(vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100149 return HRTIMER_NORESTART;
150}
151
152/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000153 * This function is called when Hafnium requests that the primary VM wake up a
154 * vCPU that belongs to a secondary VM.
155 *
156 * It wakes up the thread if it's sleeping, or kicks it if it's already running.
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000157 */
Andrew Walbranddc33942019-06-25 18:32:02 +0100158static void hf_handle_wake_up_request(spci_vm_id_t vm_id,
159 spci_vcpu_index_t vcpu)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000160{
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000161 struct hf_vm *vm = hf_vm_from_id(vm_id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000162
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000163 if (!vm) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000164 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
165 return;
166 }
167
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000168 if (vcpu >= vm->vcpu_count) {
Andrew Scull71f57362019-02-05 16:11:35 +0000169 pr_warn("Request to wake up non-existent vCPU: %u.%u\n",
170 vm_id, vcpu);
171 return;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000172 }
173
174 if (hf_vcpu_wake_up(&vm->vcpu[vcpu]) == 0) {
175 /*
176 * The task was already running (presumably on a different
177 * physical CPU); interrupt it. This gives Hafnium a chance to
178 * inject any new interrupts.
179 */
180 kick_process(vm->vcpu[vcpu].task);
181 }
182}
183
184/**
Andrew Scull71f57362019-02-05 16:11:35 +0000185 * Injects an interrupt into a vCPU of the VM and ensures the vCPU will run to
186 * handle the interrupt.
187 */
Andrew Walbran01390ae2019-06-19 11:32:46 +0100188static void hf_interrupt_vm(spci_vm_id_t vm_id, uint64_t int_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000189{
190 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbranddc33942019-06-25 18:32:02 +0100191 spci_vcpu_index_t vcpu;
Andrew Scull71f57362019-02-05 16:11:35 +0000192 int64_t ret;
193
194 if (!vm) {
195 pr_warn("Request to wake up non-existent VM id: %u\n", vm_id);
196 return;
197 }
198
199 /*
200 * TODO: For now we're picking the first vcpu to interrupt, but
201 * we want to be smarter.
202 */
203 vcpu = 0;
204 ret = hf_interrupt_inject(vm_id, vcpu, int_id);
205
206 if (ret == -1) {
207 pr_warn("Failed to inject interrupt %lld to vCPU %d of VM %d",
208 int_id, vcpu, vm_id);
209 return;
210 }
211
212 if (ret != 1) {
213 /* We don't need to wake up the vcpu. */
214 return;
215 }
216
217 hf_handle_wake_up_request(vm_id, vcpu);
218}
219
220/**
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000221 * Notify all waiters on the given VM.
222 */
Andrew Walbran01390ae2019-06-19 11:32:46 +0100223static void hf_notify_waiters(spci_vm_id_t vm_id)
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000224{
Andrew Walbran01390ae2019-06-19 11:32:46 +0100225 spci_vm_id_t waiter_vm_id;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000226
Andrew Scull71f57362019-02-05 16:11:35 +0000227 while ((waiter_vm_id = hf_mailbox_waiter_get(vm_id)) != -1) {
228 if (waiter_vm_id == HF_PRIMARY_VM_ID) {
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000229 /*
230 * TODO: Use this information when implementing per-vm
231 * queues.
232 */
233 } else {
Andrew Scull71f57362019-02-05 16:11:35 +0000234 hf_interrupt_vm(waiter_vm_id,
235 HF_MAILBOX_WRITABLE_INTID);
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000236 }
237 }
238}
239
240/**
Andrew Scull71f57362019-02-05 16:11:35 +0000241 * Delivers a message to a VM.
242 */
Andrew Walbran01390ae2019-06-19 11:32:46 +0100243static void hf_deliver_message(spci_vm_id_t vm_id)
Andrew Scull71f57362019-02-05 16:11:35 +0000244{
245 struct hf_vm *vm = hf_vm_from_id(vm_id);
Andrew Walbran3eeb1de2019-06-25 18:32:30 +0100246 spci_vcpu_index_t i;
Andrew Scull71f57362019-02-05 16:11:35 +0000247
248 if (!vm) {
249 pr_warn("Tried to deliver message to non-existent VM id: %u\n",
250 vm_id);
251 return;
252 }
253
254 /* Try to wake a vCPU that is waiting for a message. */
255 for (i = 0; i < vm->vcpu_count; i++) {
256 if (atomic_read(&vm->vcpu[i].waiting_for_message)) {
257 hf_handle_wake_up_request(vm->id,
258 vm->vcpu[i].vcpu_index);
259 return;
260 }
261 }
262
263 /* None were waiting for a message so interrupt one. */
264 hf_interrupt_vm(vm->id, HF_MAILBOX_READABLE_INTID);
265}
266
267/**
Andrew Sculldf6478f2019-02-19 17:52:08 +0000268 * Handles a message delivered to this VM by validating that it's well-formed
269 * and then queueing it for delivery to the appropriate socket.
270 */
Andrew Scull94704232019-04-01 12:36:37 +0100271static void hf_handle_message(struct hf_vm *sender,
272 const struct spci_message *message)
Andrew Sculldf6478f2019-02-19 17:52:08 +0000273{
274 struct hf_sock *hsock;
Andrew Scull94704232019-04-01 12:36:37 +0100275 const struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)message->payload;
276 size_t len = message->length;
Andrew Sculldf6478f2019-02-19 17:52:08 +0000277 struct sk_buff *skb;
278 int err;
279
280 /* Ignore messages that are too small to hold a header. */
281 if (len < sizeof(struct hf_msg_hdr))
282 return;
283
284 len -= sizeof(struct hf_msg_hdr);
285
286 /* Go through the colliding sockets. */
287 rcu_read_lock();
288 hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node,
289 hdr->dst_port) {
290 if (hsock->peer_vm == sender &&
291 hsock->remote_port == hdr->src_port) {
292 sock_hold(&hsock->sk);
293 break;
294 }
295 }
296 rcu_read_unlock();
297
298 /* Nothing to do if we couldn't find the target. */
299 if (!hsock)
300 return;
301
302 /*
303 * TODO: From this point on, there are two failure paths: when we
304 * create the skb below, and when we enqueue it to the socket. What
305 * should we do if they fail? Ideally we would have some form of flow
306 * control to prevent message loss, but how to do it efficiently?
307 *
308 * One option is to have a pre-allocated message that indicates to the
309 * sender that a message was dropped. This way we guarantee that the
310 * sender will be aware of loss and should back-off.
311 */
312 /* Create the skb. */
313 skb = alloc_skb(len, GFP_KERNEL);
314 if (!skb)
315 goto exit;
316
317 memcpy(skb_put(skb, len), hdr + 1, len);
318
319 /*
320 * Add the skb to the receive queue of the target socket. On success it
321 * calls sk->sk_data_ready, which is currently set to sock_def_readable,
322 * which wakes up any waiters.
323 */
324 err = sock_queue_rcv_skb(&hsock->sk, skb);
325 if (err)
326 kfree_skb(skb);
327
328exit:
329 sock_put(&hsock->sk);
Andrew Scull71f57362019-02-05 16:11:35 +0000330
331 if (hf_mailbox_clear() == 1)
332 hf_notify_waiters(HF_PRIMARY_VM_ID);
Andrew Sculldf6478f2019-02-19 17:52:08 +0000333}
334
335/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100336 * This is the main loop of each vcpu.
337 */
338static int hf_vcpu_thread(void *data)
339{
340 struct hf_vcpu *vcpu = data;
Andrew Sculldc8cab52018-10-10 18:29:39 +0100341 struct hf_vcpu_run_return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100342
343 hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
344 vcpu->timer.function = &hf_vcpu_timer_expired;
345
346 while (!kthread_should_stop()) {
Andrew Walbran3eeb1de2019-06-25 18:32:30 +0100347 spci_vcpu_index_t i;
Andrew Scull01f83de2019-01-23 13:41:47 +0000348
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000349 /*
350 * We're about to run the vcpu, so we can reset the abort-sleep
351 * flag.
352 */
353 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100354
Andrew Scullbb7ae412018-09-28 21:07:15 +0100355 /* Call into Hafnium to run vcpu. */
Andrew Scullb722f952018-09-27 15:39:10 +0100356 ret = hf_vcpu_run(vcpu->vm->id, vcpu->vcpu_index);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100357
Andrew Sculldc8cab52018-10-10 18:29:39 +0100358 switch (ret.code) {
Andrew Sculle05702e2019-01-08 14:46:46 +0000359 /* Preempted. */
360 case HF_VCPU_RUN_PREEMPTED:
361 if (need_resched())
362 schedule();
363 break;
364
365 /* Yield. */
Andrew Scullb3a61b52018-09-17 14:30:34 +0100366 case HF_VCPU_RUN_YIELD:
Andrew Sculle05702e2019-01-08 14:46:46 +0000367 if (!kthread_should_stop())
368 schedule();
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100369 break;
370
Andrew Scull01778112019-01-14 15:37:53 +0000371 /* WFI. */
Andrew Scullb3a61b52018-09-17 14:30:34 +0100372 case HF_VCPU_RUN_WAIT_FOR_INTERRUPT:
Andrew Scull71f57362019-02-05 16:11:35 +0000373 if (ret.sleep.ns != HF_SLEEP_INDEFINITE) {
374 hrtimer_start(&vcpu->timer, ret.sleep.ns,
375 HRTIMER_MODE_REL);
376 }
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000377 hf_vcpu_sleep(vcpu);
Andrew Scull71f57362019-02-05 16:11:35 +0000378 hrtimer_cancel(&vcpu->timer);
379 break;
380
381 /* Waiting for a message. */
382 case HF_VCPU_RUN_WAIT_FOR_MESSAGE:
383 atomic_set(&vcpu->waiting_for_message, 1);
384 if (ret.sleep.ns != HF_SLEEP_INDEFINITE) {
385 hrtimer_start(&vcpu->timer, ret.sleep.ns,
386 HRTIMER_MODE_REL);
387 }
388 hf_vcpu_sleep(vcpu);
389 hrtimer_cancel(&vcpu->timer);
390 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100391 break;
392
Andrew Scullb3a61b52018-09-17 14:30:34 +0100393 /* Wake up another vcpu. */
394 case HF_VCPU_RUN_WAKE_UP:
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000395 hf_handle_wake_up_request(ret.wake_up.vm_id,
Andrew Scull71f57362019-02-05 16:11:35 +0000396 ret.wake_up.vcpu);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100397 break;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100398
Andrew Scullb3a61b52018-09-17 14:30:34 +0100399 /* Response available. */
Andrew Scull0973a2e2018-10-05 11:11:24 +0100400 case HF_VCPU_RUN_MESSAGE:
Andrew Scull71f57362019-02-05 16:11:35 +0000401 if (ret.message.vm_id == HF_PRIMARY_VM_ID) {
402 hf_handle_message(vcpu->vm,
Andrew Scull94704232019-04-01 12:36:37 +0100403 page_address(hf_recv_page));
Andrew Scull71f57362019-02-05 16:11:35 +0000404 } else {
405 hf_deliver_message(ret.message.vm_id);
406 }
Andrew Sculldc8cab52018-10-10 18:29:39 +0100407 break;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000408
409 /* Notify all waiters. */
410 case HF_VCPU_RUN_NOTIFY_WAITERS:
411 hf_notify_waiters(vcpu->vm->id);
412 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000413
Andrew Scull71f57362019-02-05 16:11:35 +0000414 /* Abort was triggered. */
Andrew Scull01f83de2019-01-23 13:41:47 +0000415 case HF_VCPU_RUN_ABORTED:
416 for (i = 0; i < vcpu->vm->vcpu_count; i++) {
417 if (i == vcpu->vcpu_index)
418 continue;
Andrew Scull71f57362019-02-05 16:11:35 +0000419 hf_handle_wake_up_request(vcpu->vm->id, i);
Andrew Scull01f83de2019-01-23 13:41:47 +0000420 }
421 hf_vcpu_sleep(vcpu);
422 break;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100423 }
424 }
425
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100426 return 0;
427}
428
429/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000430 * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It
431 * relies on the fact that the first field of hf_sock is a sock.
432 */
433static struct hf_sock *hsock_from_sk(struct sock *sk)
434{
435 return (struct hf_sock *)sk;
436}
437
438/**
439 * This is called when the last reference to the outer socket is released. For
440 * example, if it's a user-space socket, when the last file descriptor pointing
441 * to this socket is closed.
442 *
443 * It begins cleaning up resources, though some can only be cleaned up after all
444 * references to the underlying socket are released, which is handled by
445 * hf_sock_destruct().
446 */
447static int hf_sock_release(struct socket *sock)
448{
449 struct sock *sk = sock->sk;
450 struct hf_sock *hsock = hsock_from_sk(sk);
451 unsigned long flags;
452
453 if (!sk)
454 return 0;
455
456 /* Shutdown for both send and receive. */
457 lock_sock(sk);
458 sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN;
459 sk->sk_state_change(sk);
460 release_sock(sk);
461
462 /* Remove from the hash table, so lookups from now on won't find it. */
463 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
464 hash_del_rcu(&hsock->sk.sk_node);
465 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
466
467 /*
468 * TODO: When we implement a tx queue, we need to clear it here so that
469 * sk_wmem_alloc will not prevent sk from being freed (sk_free).
470 */
471
472 /*
473 * Wait for in-flight lookups to finish. We need to do this here because
Wedson Almeida Filho89d0e472019-01-03 19:18:39 +0000474 * in-flight lookups rely on the reference to the socket we're about to
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000475 * release.
476 */
477 synchronize_rcu();
478 sock_put(sk);
479 sock->sk = NULL;
480
481 return 0;
482}
483
484/**
485 * This is called when there are no more references to the socket. It frees all
486 * resources that haven't been freed during release.
487 */
488static void hf_sock_destruct(struct sock *sk)
489{
490 /*
491 * Clear the receive queue now that the handler cannot add any more
492 * skbs to it.
493 */
494 skb_queue_purge(&sk->sk_receive_queue);
495}
496
497/**
498 * Connects the Hafnium socket to the provided VM and port. After the socket is
499 * connected, it can be used to exchange datagrams with the specified peer.
500 */
Andrew Scull01778112019-01-14 15:37:53 +0000501static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len,
502 int connect_flags)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000503{
504 struct sock *sk = sock->sk;
505 struct hf_sock *hsock = hsock_from_sk(sk);
506 struct hf_vm *vm;
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100507 struct hf_sockaddr *addr;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000508 int err;
509 unsigned long flags;
510
511 /* Basic address validation. */
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100512 if (len < sizeof(struct hf_sockaddr) || saddr->sa_family != AF_HF)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000513 return -EINVAL;
514
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100515 addr = (struct hf_sockaddr *)saddr;
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000516 vm = hf_vm_from_id(addr->vm_id);
517 if (!vm)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000518 return -ENETUNREACH;
519
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000520 /*
521 * TODO: Once we implement access control in Hafnium, check that the
522 * caller is allowed to contact the specified VM. Return -ECONNREFUSED
523 * if access is denied.
524 */
525
526 /* Take lock to make sure state doesn't change as we connect. */
527 lock_sock(sk);
528
529 /* Only unconnected sockets are allowed to become connected. */
530 if (sock->state != SS_UNCONNECTED) {
531 err = -EISCONN;
532 goto exit;
533 }
534
535 hsock->local_port = atomic64_inc_return(&hf_next_port);
536 hsock->remote_port = addr->port;
537 hsock->peer_vm = vm;
538
539 sock->state = SS_CONNECTED;
540
541 /* Add socket to hash table now that it's fully initialised. */
542 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
543 hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port);
544 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
545
546 err = 0;
547exit:
548 release_sock(sk);
549 return err;
550}
551
552/**
553 * Sends the given skb to the appropriate VM by calling Hafnium. It will also
554 * trigger the wake up of a recipient VM.
555 *
556 * Takes ownership of the skb on success.
557 */
558static int hf_send_skb(struct sk_buff *skb)
559{
560 unsigned long flags;
561 int64_t ret;
562 struct hf_sock *hsock = hsock_from_sk(skb->sk);
563 struct hf_vm *vm = hsock->peer_vm;
Jose Marinho1cc6c752019-03-11 16:28:03 +0000564 struct spci_message *message = page_address(hf_send_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000565
566 /*
567 * Call Hafnium under the send lock so that we serialize the use of the
568 * global send buffer.
569 */
570 spin_lock_irqsave(&hf_send_lock, flags);
Jose Marinho1cc6c752019-03-11 16:28:03 +0000571 memcpy(message->payload, skb->data, skb->len);
572 spci_message_init(message, skb->len,
573 vm->id, current_vm_id);
574
575 ret = spci_msg_send(0);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000576 spin_unlock_irqrestore(&hf_send_lock, flags);
577
578 if (ret < 0)
579 return -EAGAIN;
580
Andrew Scull71f57362019-02-05 16:11:35 +0000581 /* Ensure the VM will run to pick up the message. */
582 hf_deliver_message(vm->id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000583
584 kfree_skb(skb);
585
586 return 0;
587}
588
589/**
590 * Determines if the given socket is in the connected state. It acquires and
591 * releases the socket lock.
592 */
593static bool hf_sock_is_connected(struct socket *sock)
594{
595 bool ret;
596
597 lock_sock(sock->sk);
598 ret = sock->state == SS_CONNECTED;
599 release_sock(sock->sk);
600
601 return ret;
602}
603
604/**
605 * Sends a message to the VM & port the socket is connected to. All variants
606 * of write/send/sendto/sendmsg eventually call this function.
607 */
608static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
609{
610 struct sock *sk = sock->sk;
611 struct sk_buff *skb;
612 int err;
613 struct hf_msg_hdr *hdr;
614 struct hf_sock *hsock = hsock_from_sk(sk);
Andrew Scull614ed7f2019-04-01 12:12:38 +0100615 size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct spci_message)
616 - sizeof(struct hf_msg_hdr);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000617
618 /* Check length. */
Andrew Scull614ed7f2019-04-01 12:12:38 +0100619 if (len > payload_max_len)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000620 return -EMSGSIZE;
621
622 /* We don't allow the destination address to be specified. */
623 if (m->msg_namelen > 0)
624 return -EISCONN;
625
626 /* We don't support out of band messages. */
627 if (m->msg_flags & MSG_OOB)
628 return -EOPNOTSUPP;
629
630 /*
631 * Ensure that the socket is connected. We don't need to hold the socket
632 * lock (acquired and released by hf_sock_is_connected) for the
633 * remainder of the function because the fields we care about are
634 * immutable once the state is SS_CONNECTED.
635 */
636 if (!hf_sock_is_connected(sock))
637 return -ENOTCONN;
638
639 /*
640 * Allocate an skb for this write. If there isn't enough room in the
641 * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block
642 * (if it's a blocking call). On success, it increments sk_wmem_alloc
643 * and sets up the skb such that sk_wmem_alloc gets decremented when
644 * the skb is freed (sock_wfree gets called).
645 */
646 skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr),
647 m->msg_flags & MSG_DONTWAIT, &err);
648 if (!skb)
649 return err;
650
651 /* Reserve room for the header and initialise it. */
652 skb_reserve(skb, sizeof(struct hf_msg_hdr));
653 hdr = skb_push(skb, sizeof(struct hf_msg_hdr));
654 hdr->src_port = hsock->local_port;
655 hdr->dst_port = hsock->remote_port;
656
657 /* Allocate area for the contents, then copy into skb. */
658 if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) {
659 err = -EFAULT;
660 goto err_cleanup;
661 }
662
663 /*
664 * TODO: We currently do this inline, but when we have support for
665 * readiness notification from Hafnium, we must add this to a per-VM tx
666 * queue that can make progress when the VM becomes writable. This will
667 * fix send buffering and poll readiness notification.
668 */
669 err = hf_send_skb(skb);
670 if (err)
671 goto err_cleanup;
672
673 return 0;
674
675err_cleanup:
676 kfree_skb(skb);
677 return err;
678}
679
680/**
681 * Receives a message originated from the VM & port the socket is connected to.
682 * All variants of read/recv/recvfrom/recvmsg eventually call this function.
683 */
684static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
685 int flags)
686{
687 struct sock *sk = sock->sk;
688 struct sk_buff *skb;
689 int err;
690 size_t copy_len;
691
692 if (!hf_sock_is_connected(sock))
693 return -ENOTCONN;
694
695 /* Grab the next skb from the receive queue. */
696 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
697 if (!skb)
698 return err;
699
700 /* Make sure we don't copy more than what fits in the output buffer. */
701 copy_len = skb->len;
702 if (copy_len > len) {
703 copy_len = len;
704 m->msg_flags |= MSG_TRUNC;
705 }
706
707 /* Make sure we don't overflow the return value type. */
708 if (copy_len > INT_MAX) {
709 copy_len = INT_MAX;
710 m->msg_flags |= MSG_TRUNC;
711 }
712
713 /* Copy skb to output iterator, then free it. */
714 err = skb_copy_datagram_msg(skb, 0, m, copy_len);
715 skb_free_datagram(sk, skb);
716 if (err)
717 return err;
718
719 return copy_len;
720}
721
722/**
723 * This function is called when a Hafnium socket is created. It initialises all
724 * state such that the caller will be able to connect the socket and then send
725 * and receive messages through it.
726 */
727static int hf_sock_create(struct net *net, struct socket *sock, int protocol,
Andrew Scull01778112019-01-14 15:37:53 +0000728 int kern)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000729{
730 static const struct proto_ops ops = {
731 .family = PF_HF,
732 .owner = THIS_MODULE,
733 .release = hf_sock_release,
734 .bind = sock_no_bind,
735 .connect = hf_sock_connect,
736 .socketpair = sock_no_socketpair,
737 .accept = sock_no_accept,
738 .ioctl = sock_no_ioctl,
739 .listen = sock_no_listen,
740 .shutdown = sock_no_shutdown,
741 .setsockopt = sock_no_setsockopt,
742 .getsockopt = sock_no_getsockopt,
743 .sendmsg = hf_sock_sendmsg,
744 .recvmsg = hf_sock_recvmsg,
745 .mmap = sock_no_mmap,
746 .sendpage = sock_no_sendpage,
747 .poll = datagram_poll,
748 };
749 struct sock *sk;
750
751 if (sock->type != SOCK_DGRAM)
752 return -ESOCKTNOSUPPORT;
753
754 if (protocol != 0)
755 return -EPROTONOSUPPORT;
756
757 /*
758 * For now we only allow callers with sys admin capability to create
759 * Hafnium sockets.
760 */
761 if (!capable(CAP_SYS_ADMIN))
762 return -EPERM;
763
764 /* Allocate and initialise socket. */
765 sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern);
766 if (!sk)
767 return -ENOMEM;
768
769 sock_init_data(sock, sk);
770
771 sk->sk_destruct = hf_sock_destruct;
772 sock->ops = &ops;
773 sock->state = SS_UNCONNECTED;
774
775 return 0;
776}
777
778/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100779 * Frees all resources, including threads, associated with the Hafnium driver.
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100780 */
Andrew Scull82257c42018-10-01 10:37:48 +0100781static void hf_free_resources(void)
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100782{
Fuad Tabba5da4b6b2019-08-05 13:56:20 +0100783 uint16_t i;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100784 spci_vcpu_index_t j;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100785
786 /*
787 * First stop all worker threads. We need to do this before freeing
788 * resources because workers may reference each other, so it is only
789 * safe to free resources after they have all stopped.
790 */
Andrew Scull82257c42018-10-01 10:37:48 +0100791 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100792 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000793
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100794 for (j = 0; j < vm->vcpu_count; j++)
795 kthread_stop(vm->vcpu[j].task);
796 }
797
798 /* Free resources. */
Andrew Scull82257c42018-10-01 10:37:48 +0100799 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100800 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000801
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100802 for (j = 0; j < vm->vcpu_count; j++)
803 put_task_struct(vm->vcpu[j].task);
804 kfree(vm->vcpu);
805 }
806
807 kfree(hf_vms);
808}
809
Andrew Scullbb7ae412018-09-28 21:07:15 +0100810/**
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000811 * Handles the hypervisor timer interrupt.
812 */
813static irqreturn_t hf_nop_irq_handler(int irq, void *dev)
814{
815 /*
816 * No need to do anything, the interrupt only exists to return to the
817 * primary vCPU so that the virtual timer will be restored and fire as
818 * normal.
819 */
820 return IRQ_HANDLED;
821}
822
823/**
824 * Enables the hypervisor timer interrupt on a CPU, when it starts or after the
825 * driver is first loaded.
826 */
827static int hf_starting_cpu(unsigned int cpu)
828{
829 if (hf_irq != 0) {
830 /* Enable the interrupt, and set it to be edge-triggered. */
831 enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING);
832 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000833
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000834 return 0;
835}
836
837/**
838 * Disables the hypervisor timer interrupt on a CPU when it is powered down.
839 */
840static int hf_dying_cpu(unsigned int cpu)
841{
842 if (hf_irq != 0) {
843 /* Disable the interrupt while the CPU is asleep. */
844 disable_percpu_irq(hf_irq);
845 }
846
847 return 0;
848}
849
850/**
851 * Registers for the hypervisor timer interrupt.
852 */
853static int hf_int_driver_probe(struct platform_device *pdev)
854{
855 int irq;
856 int ret;
857
858 /*
859 * Register a handler for the hyperviser timer IRQ, as it is needed for
860 * Hafnium to emulate the virtual timer for Linux while a secondary vCPU
861 * is running.
862 */
863 irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI);
864 if (irq < 0) {
865 pr_err("Error getting hypervisor timer IRQ: %d\n", irq);
866 return irq;
867 }
868 hf_irq = irq;
869
870 ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME,
871 pdev);
872 if (ret != 0) {
873 pr_err("Error registering hypervisor timer IRQ %d: %d\n",
874 irq, ret);
875 return ret;
876 }
877 pr_info("Hafnium registered for IRQ %d\n", irq);
878 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
879 "hafnium/hypervisor_timer:starting",
880 hf_starting_cpu, hf_dying_cpu);
881 if (ret < 0) {
882 pr_err("Error enabling timer on all CPUs: %d\n", ret);
Andrew Walbran8d55e502019-02-05 11:42:08 +0000883 free_percpu_irq(irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000884 return ret;
885 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000886 hf_cpuhp_state = ret;
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000887
888 return 0;
889}
890
891/**
892 * Unregisters for the hypervisor timer interrupt.
893 */
894static int hf_int_driver_remove(struct platform_device *pdev)
895{
Andrew Walbran8d55e502019-02-05 11:42:08 +0000896 /*
897 * This will cause hf_dying_cpu to be called on each CPU, which will
898 * disable the IRQs.
899 */
900 cpuhp_remove_state(hf_cpuhp_state);
901 free_percpu_irq(hf_irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000902
903 return 0;
904}
905
906static const struct of_device_id hf_int_driver_id[] = {
907 {.compatible = "arm,armv7-timer"},
908 {.compatible = "arm,armv8-timer"},
909 {}
910};
911
912static struct platform_driver hf_int_driver = {
913 .driver = {
914 .name = HYPERVISOR_TIMER_NAME,
915 .owner = THIS_MODULE,
916 .of_match_table = of_match_ptr(hf_int_driver_id),
917 },
918 .probe = hf_int_driver_probe,
919 .remove = hf_int_driver_remove,
920};
921
922/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100923 * Initializes the Hafnium driver by creating a thread for each vCPU of each
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100924 * virtual machine.
925 */
926static int __init hf_init(void)
927{
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000928 static const struct net_proto_family proto_family = {
929 .family = PF_HF,
930 .create = hf_sock_create,
931 .owner = THIS_MODULE,
932 };
Andrew Scullbb7ae412018-09-28 21:07:15 +0100933 int64_t ret;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100934 spci_vm_id_t i;
935 spci_vcpu_index_t j;
936 spci_vm_count_t secondary_vm_count;
Andrew Scull82257c42018-10-01 10:37:48 +0100937 uint32_t total_vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100938
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100939 /* Allocate a page for send and receive buffers. */
940 hf_send_page = alloc_page(GFP_KERNEL);
941 if (!hf_send_page) {
942 pr_err("Unable to allocate send buffer\n");
943 return -ENOMEM;
944 }
945
946 hf_recv_page = alloc_page(GFP_KERNEL);
947 if (!hf_recv_page) {
948 __free_page(hf_send_page);
949 pr_err("Unable to allocate receive buffer\n");
950 return -ENOMEM;
951 }
952
953 /*
954 * Configure both addresses. Once configured, we cannot free these pages
955 * because the hypervisor will use them, even if the module is
956 * unloaded.
957 */
Andrew Scull55704232018-08-10 17:19:54 +0100958 ret = hf_vm_configure(page_to_phys(hf_send_page),
959 page_to_phys(hf_recv_page));
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100960 if (ret) {
961 __free_page(hf_send_page);
962 __free_page(hf_recv_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000963 /*
964 * TODO: We may want to grab this information from hypervisor
965 * and go from there.
966 */
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100967 pr_err("Unable to configure VM\n");
968 return -EIO;
969 }
970
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100971 /* Get the number of secondary VMs. */
972 secondary_vm_count = hf_vm_get_count() - 1;
Andrew Scull82257c42018-10-01 10:37:48 +0100973
974 /* Confirm the maximum number of VMs looks sane. */
975 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS < 1);
976 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VMS > U16_MAX);
977
978 /* Validate the number of VMs. There must at least be the primary. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100979 if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) {
Fuad Tabba8523ccd2019-07-31 15:37:29 +0100980 pr_err("Number of VMs is out of range: %d\n",
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100981 secondary_vm_count);
Andrew Scull82257c42018-10-01 10:37:48 +0100982 return -EDQUOT;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100983 }
984
Andrew Scullb722f952018-09-27 15:39:10 +0100985 /* Only track the secondary VMs. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100986 hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm),
987 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100988 if (!hf_vms)
989 return -ENOMEM;
990
Jose Marinho1cc6c752019-03-11 16:28:03 +0000991 /* Cache the VM id for later usage. */
992 current_vm_id = hf_vm_get_id();
993
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100994 /* Initialize each VM. */
Andrew Scull82257c42018-10-01 10:37:48 +0100995 total_vcpu_count = 0;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100996 for (i = 0; i < secondary_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100997 struct hf_vm *vm = &hf_vms[i];
Andrew Walbran3eeb1de2019-06-25 18:32:30 +0100998 spci_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100999
Andrew Scullb722f952018-09-27 15:39:10 +01001000 /* Adjust the ID as only the secondaries are tracked. */
Wedson Almeida Filhoec841932019-01-22 23:07:50 +00001001 vm->id = i + FIRST_SECONDARY_VM_ID;
Andrew Scullb722f952018-09-27 15:39:10 +01001002
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001003 vcpu_count = hf_vcpu_get_count(vm->id);
1004 if (vcpu_count < 0) {
1005 pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %d",
1006 vm->id, vcpu_count);
Andrew Scull82257c42018-10-01 10:37:48 +01001007 ret = -EIO;
1008 goto fail_with_cleanup;
1009 }
1010
1011 /* Avoid overflowing the vcpu count. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001012 if (vcpu_count > (U32_MAX - total_vcpu_count)) {
Andrew Scull82257c42018-10-01 10:37:48 +01001013 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1014 ret = -EDQUOT;
1015 goto fail_with_cleanup;
1016 }
1017
1018 /* Confirm the maximum number of VCPUs looks sane. */
1019 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1);
1020 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX);
1021
1022 /* Enforce the limit on vcpus. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001023 total_vcpu_count += vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001024 if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) {
1025 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1026 ret = -EDQUOT;
1027 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001028 }
1029
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001030 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001031 vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu),
1032 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001033 if (!vm->vcpu) {
Andrew Scull82257c42018-10-01 10:37:48 +01001034 ret = -ENOMEM;
1035 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001036 }
1037
Andrew Scull82257c42018-10-01 10:37:48 +01001038 /* Update the number of initialized VMs. */
1039 hf_vm_count = i + 1;
1040
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001041 /* Create a kernel thread for each vcpu. */
1042 for (j = 0; j < vm->vcpu_count; j++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001043 struct hf_vcpu *vcpu = &vm->vcpu[j];
Andrew Scull01778112019-01-14 15:37:53 +00001044
1045 vcpu->task =
1046 kthread_create(hf_vcpu_thread, vcpu,
1047 "vcpu_thread_%u_%u", vm->id, j);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001048 if (IS_ERR(vcpu->task)) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001049 pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n",
1050 vm->id, j, PTR_ERR(vcpu->task));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001051 vm->vcpu_count = j;
Andrew Scull82257c42018-10-01 10:37:48 +01001052 ret = PTR_ERR(vcpu->task);
1053 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001054 }
1055
1056 get_task_struct(vcpu->task);
Andrew Scullb722f952018-09-27 15:39:10 +01001057 vcpu->vm = vm;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001058 vcpu->vcpu_index = j;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +00001059 atomic_set(&vcpu->abort_sleep, 0);
Andrew Scullece5ef42019-05-08 15:07:25 +01001060 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001061 }
1062 }
1063
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001064 /* Register protocol and socket family. */
1065 ret = proto_register(&hf_sock_proto, 0);
1066 if (ret) {
1067 pr_err("Unable to register protocol: %lld\n", ret);
1068 goto fail_with_cleanup;
1069 }
1070
1071 ret = sock_register(&proto_family);
1072 if (ret) {
1073 pr_err("Unable to register Hafnium's socket family: %lld\n",
1074 ret);
1075 goto fail_unregister_proto;
1076 }
1077
1078 /*
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001079 * Register as a driver for the timer device, so we can register a
1080 * handler for the hyperviser timer IRQ.
1081 */
1082 ret = platform_driver_register(&hf_int_driver);
1083 if (ret != 0) {
1084 pr_err("Error registering timer driver %lld\n", ret);
1085 goto fail_unregister_socket;
1086 }
1087
1088 /*
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001089 * Start running threads now that all is initialized.
1090 *
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001091 * Any failures from this point on must also unregister the driver with
1092 * platform_driver_unregister().
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001093 */
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001094 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001095 struct hf_vm *vm = &hf_vms[i];
Andrew Scull01778112019-01-14 15:37:53 +00001096
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001097 for (j = 0; j < vm->vcpu_count; j++)
1098 wake_up_process(vm->vcpu[j].task);
1099 }
1100
1101 /* Dump vm/vcpu count info. */
Andrew Scullbb7ae412018-09-28 21:07:15 +01001102 pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001103 for (i = 0; i < hf_vm_count; i++) {
1104 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001105
Andrew Scullbb7ae412018-09-28 21:07:15 +01001106 pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001107 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001108
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001109 return 0;
Andrew Scull82257c42018-10-01 10:37:48 +01001110
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001111fail_unregister_socket:
1112 sock_unregister(PF_HF);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001113fail_unregister_proto:
1114 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001115fail_with_cleanup:
1116 hf_free_resources();
1117 return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001118}
1119
1120/**
Andrew Scullbb7ae412018-09-28 21:07:15 +01001121 * Frees up all resources used by the Hafnium driver in preparation for
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001122 * unloading it.
1123 */
1124static void __exit hf_exit(void)
1125{
Andrew Scullbb7ae412018-09-28 21:07:15 +01001126 pr_info("Preparing to unload Hafnium\n");
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001127 sock_unregister(PF_HF);
1128 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001129 hf_free_resources();
Andrew Walbran8d55e502019-02-05 11:42:08 +00001130 platform_driver_unregister(&hf_int_driver);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001131 pr_info("Hafnium ready to unload\n");
1132}
1133
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001134MODULE_LICENSE("GPL v2");
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001135
1136module_init(hf_init);
1137module_exit(hf_exit);