blob: 2061005c1758445113c6092206847bce18319bed [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 Walbranb331fa92019-10-03 16:48:07 +0100271static void hf_handle_message(struct hf_vm *sender, size_t len,
Andrew Walbrancafe0172019-10-07 14:14:05 +0100272 const void *message)
Andrew Sculldf6478f2019-02-19 17:52:08 +0000273{
274 struct hf_sock *hsock;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100275 const struct hf_msg_hdr *hdr = (struct hf_msg_hdr *)message;
Andrew Sculldf6478f2019-02-19 17:52:08 +0000276 struct sk_buff *skb;
277 int err;
278
279 /* Ignore messages that are too small to hold a header. */
280 if (len < sizeof(struct hf_msg_hdr))
281 return;
282
283 len -= sizeof(struct hf_msg_hdr);
284
285 /* Go through the colliding sockets. */
286 rcu_read_lock();
287 hash_for_each_possible_rcu(hf_local_port_hash, hsock, sk.sk_node,
288 hdr->dst_port) {
289 if (hsock->peer_vm == sender &&
290 hsock->remote_port == hdr->src_port) {
291 sock_hold(&hsock->sk);
292 break;
293 }
294 }
295 rcu_read_unlock();
296
297 /* Nothing to do if we couldn't find the target. */
298 if (!hsock)
299 return;
300
301 /*
302 * TODO: From this point on, there are two failure paths: when we
303 * create the skb below, and when we enqueue it to the socket. What
304 * should we do if they fail? Ideally we would have some form of flow
305 * control to prevent message loss, but how to do it efficiently?
306 *
307 * One option is to have a pre-allocated message that indicates to the
308 * sender that a message was dropped. This way we guarantee that the
309 * sender will be aware of loss and should back-off.
310 */
311 /* Create the skb. */
312 skb = alloc_skb(len, GFP_KERNEL);
313 if (!skb)
314 goto exit;
315
316 memcpy(skb_put(skb, len), hdr + 1, len);
317
318 /*
319 * Add the skb to the receive queue of the target socket. On success it
320 * calls sk->sk_data_ready, which is currently set to sock_def_readable,
321 * which wakes up any waiters.
322 */
323 err = sock_queue_rcv_skb(&hsock->sk, skb);
324 if (err)
325 kfree_skb(skb);
326
327exit:
328 sock_put(&hsock->sk);
Andrew Scull71f57362019-02-05 16:11:35 +0000329
330 if (hf_mailbox_clear() == 1)
331 hf_notify_waiters(HF_PRIMARY_VM_ID);
Andrew Sculldf6478f2019-02-19 17:52:08 +0000332}
333
334/**
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100335 * This is the main loop of each vcpu.
336 */
337static int hf_vcpu_thread(void *data)
338{
339 struct hf_vcpu *vcpu = data;
Andrew Walbran39bf7892019-11-01 14:14:47 +0000340 struct spci_value ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100341
342 hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
343 vcpu->timer.function = &hf_vcpu_timer_expired;
344
345 while (!kthread_should_stop()) {
Andrew Walbran3eeb1de2019-06-25 18:32:30 +0100346 spci_vcpu_index_t i;
Andrew Scull01f83de2019-01-23 13:41:47 +0000347
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000348 /*
349 * We're about to run the vcpu, so we can reset the abort-sleep
350 * flag.
351 */
352 atomic_set(&vcpu->abort_sleep, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100353
Andrew Scullbb7ae412018-09-28 21:07:15 +0100354 /* Call into Hafnium to run vcpu. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000355 ret = spci_run(vcpu->vm->id, vcpu->vcpu_index);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100356
Andrew Walbran39bf7892019-11-01 14:14:47 +0000357 switch (ret.func) {
Andrew Sculle05702e2019-01-08 14:46:46 +0000358 /* Preempted. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000359 case SPCI_INTERRUPT_32:
Andrew Sculle05702e2019-01-08 14:46:46 +0000360 if (need_resched())
361 schedule();
362 break;
363
364 /* Yield. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000365 case SPCI_YIELD_32:
Andrew Sculle05702e2019-01-08 14:46:46 +0000366 if (!kthread_should_stop())
367 schedule();
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100368 break;
369
Andrew Scull01778112019-01-14 15:37:53 +0000370 /* WFI. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000371 case HF_SPCI_RUN_WAIT_FOR_INTERRUPT:
372 if (ret.arg2 != SPCI_SLEEP_INDEFINITE) {
373 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000374 HRTIMER_MODE_REL);
375 }
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +0000376 hf_vcpu_sleep(vcpu);
Andrew Scull71f57362019-02-05 16:11:35 +0000377 hrtimer_cancel(&vcpu->timer);
378 break;
379
380 /* Waiting for a message. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000381 case SPCI_MSG_WAIT_32:
Andrew Scull71f57362019-02-05 16:11:35 +0000382 atomic_set(&vcpu->waiting_for_message, 1);
Andrew Walbran39bf7892019-11-01 14:14:47 +0000383 if (ret.arg2 != SPCI_SLEEP_INDEFINITE) {
384 hrtimer_start(&vcpu->timer, ret.arg2,
Andrew Scull71f57362019-02-05 16:11:35 +0000385 HRTIMER_MODE_REL);
386 }
387 hf_vcpu_sleep(vcpu);
388 hrtimer_cancel(&vcpu->timer);
389 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100390 break;
391
Andrew Scullb3a61b52018-09-17 14:30:34 +0100392 /* Wake up another vcpu. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000393 case HF_SPCI_RUN_WAKE_UP:
394 hf_handle_wake_up_request(wake_up_get_vm_id(ret),
395 wake_up_get_vcpu(ret));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100396 break;
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100397
Andrew Scullb3a61b52018-09-17 14:30:34 +0100398 /* Response available. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000399 case SPCI_MSG_SEND_32:
400 if (spci_msg_send_receiver(ret) == HF_PRIMARY_VM_ID) {
401 hf_handle_message(vcpu->vm,
402 spci_msg_send_size(ret),
Andrew Scull94704232019-04-01 12:36:37 +0100403 page_address(hf_recv_page));
Andrew Scull71f57362019-02-05 16:11:35 +0000404 } else {
Andrew Walbran39bf7892019-11-01 14:14:47 +0000405 hf_deliver_message(spci_msg_send_receiver(ret));
Andrew Scull71f57362019-02-05 16:11:35 +0000406 }
Andrew Sculldc8cab52018-10-10 18:29:39 +0100407 break;
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000408
409 /* Notify all waiters. */
Andrew Walbran39bf7892019-11-01 14:14:47 +0000410 case SPCI_RX_RELEASE_32:
Wedson Almeida Filhocd9fef92019-01-11 21:24:08 +0000411 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 Walbran39bf7892019-11-01 14:14:47 +0000415 case SPCI_ERROR_32:
416 switch (ret.arg2) {
417 case SPCI_ABORTED:
418 for (i = 0; i < vcpu->vm->vcpu_count; i++) {
419 if (i == vcpu->vcpu_index)
420 continue;
421 hf_handle_wake_up_request(vcpu->vm->id,
422 i);
423 }
424 hf_vcpu_sleep(vcpu);
425 break;
Andrew Scull01f83de2019-01-23 13:41:47 +0000426 }
Andrew Scull01f83de2019-01-23 13:41:47 +0000427 break;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100428 }
429 }
430
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100431 return 0;
432}
433
434/**
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000435 * Converts a pointer to a struct sock into a pointer to a struct hf_sock. It
436 * relies on the fact that the first field of hf_sock is a sock.
437 */
438static struct hf_sock *hsock_from_sk(struct sock *sk)
439{
440 return (struct hf_sock *)sk;
441}
442
443/**
444 * This is called when the last reference to the outer socket is released. For
445 * example, if it's a user-space socket, when the last file descriptor pointing
446 * to this socket is closed.
447 *
448 * It begins cleaning up resources, though some can only be cleaned up after all
449 * references to the underlying socket are released, which is handled by
450 * hf_sock_destruct().
451 */
452static int hf_sock_release(struct socket *sock)
453{
454 struct sock *sk = sock->sk;
455 struct hf_sock *hsock = hsock_from_sk(sk);
456 unsigned long flags;
457
458 if (!sk)
459 return 0;
460
461 /* Shutdown for both send and receive. */
462 lock_sock(sk);
463 sk->sk_shutdown |= RCV_SHUTDOWN | SEND_SHUTDOWN;
464 sk->sk_state_change(sk);
465 release_sock(sk);
466
467 /* Remove from the hash table, so lookups from now on won't find it. */
468 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
469 hash_del_rcu(&hsock->sk.sk_node);
470 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
471
472 /*
473 * TODO: When we implement a tx queue, we need to clear it here so that
474 * sk_wmem_alloc will not prevent sk from being freed (sk_free).
475 */
476
477 /*
478 * Wait for in-flight lookups to finish. We need to do this here because
Wedson Almeida Filho89d0e472019-01-03 19:18:39 +0000479 * in-flight lookups rely on the reference to the socket we're about to
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000480 * release.
481 */
482 synchronize_rcu();
483 sock_put(sk);
484 sock->sk = NULL;
485
486 return 0;
487}
488
489/**
490 * This is called when there are no more references to the socket. It frees all
491 * resources that haven't been freed during release.
492 */
493static void hf_sock_destruct(struct sock *sk)
494{
495 /*
496 * Clear the receive queue now that the handler cannot add any more
497 * skbs to it.
498 */
499 skb_queue_purge(&sk->sk_receive_queue);
500}
501
502/**
503 * Connects the Hafnium socket to the provided VM and port. After the socket is
504 * connected, it can be used to exchange datagrams with the specified peer.
505 */
Andrew Scull01778112019-01-14 15:37:53 +0000506static int hf_sock_connect(struct socket *sock, struct sockaddr *saddr, int len,
507 int connect_flags)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000508{
509 struct sock *sk = sock->sk;
510 struct hf_sock *hsock = hsock_from_sk(sk);
511 struct hf_vm *vm;
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100512 struct hf_sockaddr *addr;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000513 int err;
514 unsigned long flags;
515
516 /* Basic address validation. */
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100517 if (len < sizeof(struct hf_sockaddr) || saddr->sa_family != AF_HF)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000518 return -EINVAL;
519
Fuad Tabba3e669bc2019-08-08 16:43:55 +0100520 addr = (struct hf_sockaddr *)saddr;
Wedson Almeida Filhoec841932019-01-22 23:07:50 +0000521 vm = hf_vm_from_id(addr->vm_id);
522 if (!vm)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000523 return -ENETUNREACH;
524
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000525 /*
526 * TODO: Once we implement access control in Hafnium, check that the
527 * caller is allowed to contact the specified VM. Return -ECONNREFUSED
528 * if access is denied.
529 */
530
531 /* Take lock to make sure state doesn't change as we connect. */
532 lock_sock(sk);
533
534 /* Only unconnected sockets are allowed to become connected. */
535 if (sock->state != SS_UNCONNECTED) {
536 err = -EISCONN;
537 goto exit;
538 }
539
540 hsock->local_port = atomic64_inc_return(&hf_next_port);
541 hsock->remote_port = addr->port;
542 hsock->peer_vm = vm;
543
544 sock->state = SS_CONNECTED;
545
546 /* Add socket to hash table now that it's fully initialised. */
547 spin_lock_irqsave(&hf_local_port_hash_lock, flags);
548 hash_add_rcu(hf_local_port_hash, &sk->sk_node, hsock->local_port);
549 spin_unlock_irqrestore(&hf_local_port_hash_lock, flags);
550
551 err = 0;
552exit:
553 release_sock(sk);
554 return err;
555}
556
557/**
558 * Sends the given skb to the appropriate VM by calling Hafnium. It will also
559 * trigger the wake up of a recipient VM.
560 *
561 * Takes ownership of the skb on success.
562 */
563static int hf_send_skb(struct sk_buff *skb)
564{
565 unsigned long flags;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100566 struct spci_value ret;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000567 struct hf_sock *hsock = hsock_from_sk(skb->sk);
568 struct hf_vm *vm = hsock->peer_vm;
Andrew Walbrancafe0172019-10-07 14:14:05 +0100569 void *message = page_address(hf_send_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000570
571 /*
572 * Call Hafnium under the send lock so that we serialize the use of the
573 * global send buffer.
574 */
575 spin_lock_irqsave(&hf_send_lock, flags);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100576 memcpy(message, skb->data, skb->len);
Jose Marinho1cc6c752019-03-11 16:28:03 +0000577
Andrew Walbrancafe0172019-10-07 14:14:05 +0100578 ret = spci_msg_send(current_vm_id, vm->id, skb->len, 0);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000579 spin_unlock_irqrestore(&hf_send_lock, flags);
580
Andrew Walbrancafe0172019-10-07 14:14:05 +0100581 if (ret.func == SPCI_ERROR_32) {
Andrew Walbranb040b302019-10-10 13:50:06 +0100582 switch (ret.arg2) {
Andrew Walbrancafe0172019-10-07 14:14:05 +0100583 case SPCI_INVALID_PARAMETERS:
584 return -ENXIO;
585 case SPCI_NOT_SUPPORTED:
586 return -EIO;
587 case SPCI_DENIED:
588 case SPCI_BUSY:
589 default:
590 return -EAGAIN;
591 }
592 }
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000593
Andrew Scull71f57362019-02-05 16:11:35 +0000594 /* Ensure the VM will run to pick up the message. */
595 hf_deliver_message(vm->id);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000596
597 kfree_skb(skb);
598
599 return 0;
600}
601
602/**
603 * Determines if the given socket is in the connected state. It acquires and
604 * releases the socket lock.
605 */
606static bool hf_sock_is_connected(struct socket *sock)
607{
608 bool ret;
609
610 lock_sock(sock->sk);
611 ret = sock->state == SS_CONNECTED;
612 release_sock(sock->sk);
613
614 return ret;
615}
616
617/**
618 * Sends a message to the VM & port the socket is connected to. All variants
619 * of write/send/sendto/sendmsg eventually call this function.
620 */
621static int hf_sock_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
622{
623 struct sock *sk = sock->sk;
624 struct sk_buff *skb;
625 int err;
626 struct hf_msg_hdr *hdr;
627 struct hf_sock *hsock = hsock_from_sk(sk);
Andrew Walbrancafe0172019-10-07 14:14:05 +0100628 size_t payload_max_len = HF_MAILBOX_SIZE - sizeof(struct hf_msg_hdr);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000629
630 /* Check length. */
Andrew Scull614ed7f2019-04-01 12:12:38 +0100631 if (len > payload_max_len)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000632 return -EMSGSIZE;
633
634 /* We don't allow the destination address to be specified. */
635 if (m->msg_namelen > 0)
636 return -EISCONN;
637
638 /* We don't support out of band messages. */
639 if (m->msg_flags & MSG_OOB)
640 return -EOPNOTSUPP;
641
642 /*
643 * Ensure that the socket is connected. We don't need to hold the socket
644 * lock (acquired and released by hf_sock_is_connected) for the
645 * remainder of the function because the fields we care about are
646 * immutable once the state is SS_CONNECTED.
647 */
648 if (!hf_sock_is_connected(sock))
649 return -ENOTCONN;
650
651 /*
652 * Allocate an skb for this write. If there isn't enough room in the
653 * socket's send buffer (sk_wmem_alloc >= sk_sndbuf), this will block
654 * (if it's a blocking call). On success, it increments sk_wmem_alloc
655 * and sets up the skb such that sk_wmem_alloc gets decremented when
656 * the skb is freed (sock_wfree gets called).
657 */
658 skb = sock_alloc_send_skb(sk, len + sizeof(struct hf_msg_hdr),
659 m->msg_flags & MSG_DONTWAIT, &err);
660 if (!skb)
661 return err;
662
663 /* Reserve room for the header and initialise it. */
664 skb_reserve(skb, sizeof(struct hf_msg_hdr));
665 hdr = skb_push(skb, sizeof(struct hf_msg_hdr));
666 hdr->src_port = hsock->local_port;
667 hdr->dst_port = hsock->remote_port;
668
669 /* Allocate area for the contents, then copy into skb. */
670 if (!copy_from_iter_full(skb_put(skb, len), len, &m->msg_iter)) {
671 err = -EFAULT;
672 goto err_cleanup;
673 }
674
675 /*
676 * TODO: We currently do this inline, but when we have support for
677 * readiness notification from Hafnium, we must add this to a per-VM tx
678 * queue that can make progress when the VM becomes writable. This will
679 * fix send buffering and poll readiness notification.
680 */
681 err = hf_send_skb(skb);
682 if (err)
683 goto err_cleanup;
684
685 return 0;
686
687err_cleanup:
688 kfree_skb(skb);
689 return err;
690}
691
692/**
693 * Receives a message originated from the VM & port the socket is connected to.
694 * All variants of read/recv/recvfrom/recvmsg eventually call this function.
695 */
696static int hf_sock_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
697 int flags)
698{
699 struct sock *sk = sock->sk;
700 struct sk_buff *skb;
701 int err;
702 size_t copy_len;
703
704 if (!hf_sock_is_connected(sock))
705 return -ENOTCONN;
706
707 /* Grab the next skb from the receive queue. */
708 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
709 if (!skb)
710 return err;
711
712 /* Make sure we don't copy more than what fits in the output buffer. */
713 copy_len = skb->len;
714 if (copy_len > len) {
715 copy_len = len;
716 m->msg_flags |= MSG_TRUNC;
717 }
718
719 /* Make sure we don't overflow the return value type. */
720 if (copy_len > INT_MAX) {
721 copy_len = INT_MAX;
722 m->msg_flags |= MSG_TRUNC;
723 }
724
725 /* Copy skb to output iterator, then free it. */
726 err = skb_copy_datagram_msg(skb, 0, m, copy_len);
727 skb_free_datagram(sk, skb);
728 if (err)
729 return err;
730
731 return copy_len;
732}
733
734/**
735 * This function is called when a Hafnium socket is created. It initialises all
736 * state such that the caller will be able to connect the socket and then send
737 * and receive messages through it.
738 */
739static int hf_sock_create(struct net *net, struct socket *sock, int protocol,
Andrew Scull01778112019-01-14 15:37:53 +0000740 int kern)
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000741{
742 static const struct proto_ops ops = {
743 .family = PF_HF,
744 .owner = THIS_MODULE,
745 .release = hf_sock_release,
746 .bind = sock_no_bind,
747 .connect = hf_sock_connect,
748 .socketpair = sock_no_socketpair,
749 .accept = sock_no_accept,
750 .ioctl = sock_no_ioctl,
751 .listen = sock_no_listen,
752 .shutdown = sock_no_shutdown,
753 .setsockopt = sock_no_setsockopt,
754 .getsockopt = sock_no_getsockopt,
755 .sendmsg = hf_sock_sendmsg,
756 .recvmsg = hf_sock_recvmsg,
757 .mmap = sock_no_mmap,
758 .sendpage = sock_no_sendpage,
759 .poll = datagram_poll,
760 };
761 struct sock *sk;
762
763 if (sock->type != SOCK_DGRAM)
764 return -ESOCKTNOSUPPORT;
765
766 if (protocol != 0)
767 return -EPROTONOSUPPORT;
768
769 /*
770 * For now we only allow callers with sys admin capability to create
771 * Hafnium sockets.
772 */
773 if (!capable(CAP_SYS_ADMIN))
774 return -EPERM;
775
776 /* Allocate and initialise socket. */
777 sk = sk_alloc(net, PF_HF, GFP_KERNEL, &hf_sock_proto, kern);
778 if (!sk)
779 return -ENOMEM;
780
781 sock_init_data(sock, sk);
782
783 sk->sk_destruct = hf_sock_destruct;
784 sock->ops = &ops;
785 sock->state = SS_UNCONNECTED;
786
787 return 0;
788}
789
790/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100791 * Frees all resources, including threads, associated with the Hafnium driver.
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100792 */
Andrew Scull82257c42018-10-01 10:37:48 +0100793static void hf_free_resources(void)
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100794{
Fuad Tabba5da4b6b2019-08-05 13:56:20 +0100795 uint16_t i;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100796 spci_vcpu_index_t j;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100797
798 /*
799 * First stop all worker threads. We need to do this before freeing
800 * resources because workers may reference each other, so it is only
801 * safe to free resources after they have all stopped.
802 */
Andrew Scull82257c42018-10-01 10:37:48 +0100803 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100804 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000805
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100806 for (j = 0; j < vm->vcpu_count; j++)
807 kthread_stop(vm->vcpu[j].task);
808 }
809
810 /* Free resources. */
Andrew Scull82257c42018-10-01 10:37:48 +0100811 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +0100812 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000813
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100814 for (j = 0; j < vm->vcpu_count; j++)
815 put_task_struct(vm->vcpu[j].task);
816 kfree(vm->vcpu);
817 }
818
819 kfree(hf_vms);
820}
821
Andrew Scullbb7ae412018-09-28 21:07:15 +0100822/**
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000823 * Handles the hypervisor timer interrupt.
824 */
825static irqreturn_t hf_nop_irq_handler(int irq, void *dev)
826{
827 /*
828 * No need to do anything, the interrupt only exists to return to the
829 * primary vCPU so that the virtual timer will be restored and fire as
830 * normal.
831 */
832 return IRQ_HANDLED;
833}
834
835/**
836 * Enables the hypervisor timer interrupt on a CPU, when it starts or after the
837 * driver is first loaded.
838 */
839static int hf_starting_cpu(unsigned int cpu)
840{
841 if (hf_irq != 0) {
842 /* Enable the interrupt, and set it to be edge-triggered. */
843 enable_percpu_irq(hf_irq, IRQ_TYPE_EDGE_RISING);
844 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000845
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000846 return 0;
847}
848
849/**
850 * Disables the hypervisor timer interrupt on a CPU when it is powered down.
851 */
852static int hf_dying_cpu(unsigned int cpu)
853{
854 if (hf_irq != 0) {
855 /* Disable the interrupt while the CPU is asleep. */
856 disable_percpu_irq(hf_irq);
857 }
858
859 return 0;
860}
861
862/**
863 * Registers for the hypervisor timer interrupt.
864 */
865static int hf_int_driver_probe(struct platform_device *pdev)
866{
867 int irq;
868 int ret;
869
870 /*
871 * Register a handler for the hyperviser timer IRQ, as it is needed for
872 * Hafnium to emulate the virtual timer for Linux while a secondary vCPU
873 * is running.
874 */
875 irq = platform_get_irq(pdev, ARCH_TIMER_HYP_PPI);
876 if (irq < 0) {
877 pr_err("Error getting hypervisor timer IRQ: %d\n", irq);
878 return irq;
879 }
880 hf_irq = irq;
881
882 ret = request_percpu_irq(irq, hf_nop_irq_handler, HYPERVISOR_TIMER_NAME,
883 pdev);
884 if (ret != 0) {
885 pr_err("Error registering hypervisor timer IRQ %d: %d\n",
886 irq, ret);
887 return ret;
888 }
889 pr_info("Hafnium registered for IRQ %d\n", irq);
890 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
891 "hafnium/hypervisor_timer:starting",
892 hf_starting_cpu, hf_dying_cpu);
893 if (ret < 0) {
894 pr_err("Error enabling timer on all CPUs: %d\n", ret);
Andrew Walbran8d55e502019-02-05 11:42:08 +0000895 free_percpu_irq(irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000896 return ret;
897 }
Andrew Walbran8d55e502019-02-05 11:42:08 +0000898 hf_cpuhp_state = ret;
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000899
900 return 0;
901}
902
903/**
904 * Unregisters for the hypervisor timer interrupt.
905 */
906static int hf_int_driver_remove(struct platform_device *pdev)
907{
Andrew Walbran8d55e502019-02-05 11:42:08 +0000908 /*
909 * This will cause hf_dying_cpu to be called on each CPU, which will
910 * disable the IRQs.
911 */
912 cpuhp_remove_state(hf_cpuhp_state);
913 free_percpu_irq(hf_irq, pdev);
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +0000914
915 return 0;
916}
917
918static const struct of_device_id hf_int_driver_id[] = {
919 {.compatible = "arm,armv7-timer"},
920 {.compatible = "arm,armv8-timer"},
921 {}
922};
923
924static struct platform_driver hf_int_driver = {
925 .driver = {
926 .name = HYPERVISOR_TIMER_NAME,
927 .owner = THIS_MODULE,
928 .of_match_table = of_match_ptr(hf_int_driver_id),
929 },
930 .probe = hf_int_driver_probe,
931 .remove = hf_int_driver_remove,
932};
933
934/**
Andrew Scullbb7ae412018-09-28 21:07:15 +0100935 * Initializes the Hafnium driver by creating a thread for each vCPU of each
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100936 * virtual machine.
937 */
938static int __init hf_init(void)
939{
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000940 static const struct net_proto_family proto_family = {
941 .family = PF_HF,
942 .create = hf_sock_create,
943 .owner = THIS_MODULE,
944 };
Andrew Scullbb7ae412018-09-28 21:07:15 +0100945 int64_t ret;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100946 spci_vm_id_t i;
947 spci_vcpu_index_t j;
948 spci_vm_count_t secondary_vm_count;
Andrew Scull82257c42018-10-01 10:37:48 +0100949 uint32_t total_vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100950
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100951 /* Allocate a page for send and receive buffers. */
952 hf_send_page = alloc_page(GFP_KERNEL);
953 if (!hf_send_page) {
954 pr_err("Unable to allocate send buffer\n");
955 return -ENOMEM;
956 }
957
958 hf_recv_page = alloc_page(GFP_KERNEL);
959 if (!hf_recv_page) {
960 __free_page(hf_send_page);
961 pr_err("Unable to allocate receive buffer\n");
962 return -ENOMEM;
963 }
964
965 /*
966 * Configure both addresses. Once configured, we cannot free these pages
967 * because the hypervisor will use them, even if the module is
968 * unloaded.
969 */
Andrew Scull55704232018-08-10 17:19:54 +0100970 ret = hf_vm_configure(page_to_phys(hf_send_page),
971 page_to_phys(hf_recv_page));
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100972 if (ret) {
973 __free_page(hf_send_page);
974 __free_page(hf_recv_page);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +0000975 /*
976 * TODO: We may want to grab this information from hypervisor
977 * and go from there.
978 */
Wedson Almeida Filhof9e11922018-08-12 15:54:31 +0100979 pr_err("Unable to configure VM\n");
980 return -EIO;
981 }
982
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100983 /* Get the number of secondary VMs. */
984 secondary_vm_count = hf_vm_get_count() - 1;
Andrew Scull82257c42018-10-01 10:37:48 +0100985
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. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100991 if (secondary_vm_count > CONFIG_HAFNIUM_MAX_VMS - 1) {
Fuad Tabba8523ccd2019-07-31 15:37:29 +0100992 pr_err("Number of VMs is out of range: %d\n",
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100993 secondary_vm_count);
Andrew Scull82257c42018-10-01 10:37:48 +0100994 return -EDQUOT;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +0100995 }
996
Andrew Scullb722f952018-09-27 15:39:10 +0100997 /* Only track the secondary VMs. */
Andrew Walbran4c96d0c2019-06-25 18:32:56 +0100998 hf_vms = kmalloc_array(secondary_vm_count, sizeof(struct hf_vm),
999 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;
Andrew Walbran4c96d0c2019-06-25 18:32:56 +01001008 for (i = 0; i < secondary_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001009 struct hf_vm *vm = &hf_vms[i];
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001010 spci_vcpu_count_t vcpu_count;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001011
Andrew Scullb722f952018-09-27 15:39:10 +01001012 /* Adjust the ID as only the secondaries are tracked. */
Wedson Almeida Filhoec841932019-01-22 23:07:50 +00001013 vm->id = i + FIRST_SECONDARY_VM_ID;
Andrew Scullb722f952018-09-27 15:39:10 +01001014
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001015 vcpu_count = hf_vcpu_get_count(vm->id);
1016 if (vcpu_count < 0) {
1017 pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %d",
1018 vm->id, vcpu_count);
Andrew Scull82257c42018-10-01 10:37:48 +01001019 ret = -EIO;
1020 goto fail_with_cleanup;
1021 }
1022
1023 /* Avoid overflowing the vcpu count. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001024 if (vcpu_count > (U32_MAX - total_vcpu_count)) {
Andrew Scull82257c42018-10-01 10:37:48 +01001025 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1026 ret = -EDQUOT;
1027 goto fail_with_cleanup;
1028 }
1029
1030 /* Confirm the maximum number of VCPUs looks sane. */
1031 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS < 1);
1032 BUILD_BUG_ON(CONFIG_HAFNIUM_MAX_VCPUS > U16_MAX);
1033
1034 /* Enforce the limit on vcpus. */
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001035 total_vcpu_count += vcpu_count;
Andrew Scull82257c42018-10-01 10:37:48 +01001036 if (total_vcpu_count > CONFIG_HAFNIUM_MAX_VCPUS) {
1037 pr_err("Too many vcpus: %u\n", total_vcpu_count);
1038 ret = -EDQUOT;
1039 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001040 }
1041
Andrew Walbran3eeb1de2019-06-25 18:32:30 +01001042 vm->vcpu_count = vcpu_count;
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001043 vm->vcpu = kmalloc_array(vm->vcpu_count, sizeof(struct hf_vcpu),
1044 GFP_KERNEL);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001045 if (!vm->vcpu) {
Andrew Scull82257c42018-10-01 10:37:48 +01001046 ret = -ENOMEM;
1047 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001048 }
1049
Andrew Scull82257c42018-10-01 10:37:48 +01001050 /* Update the number of initialized VMs. */
1051 hf_vm_count = i + 1;
1052
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001053 /* Create a kernel thread for each vcpu. */
1054 for (j = 0; j < vm->vcpu_count; j++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001055 struct hf_vcpu *vcpu = &vm->vcpu[j];
Andrew Scull01778112019-01-14 15:37:53 +00001056
1057 vcpu->task =
1058 kthread_create(hf_vcpu_thread, vcpu,
1059 "vcpu_thread_%u_%u", vm->id, j);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001060 if (IS_ERR(vcpu->task)) {
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001061 pr_err("Error creating task (vm=%u,vcpu=%u): %ld\n",
1062 vm->id, j, PTR_ERR(vcpu->task));
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001063 vm->vcpu_count = j;
Andrew Scull82257c42018-10-01 10:37:48 +01001064 ret = PTR_ERR(vcpu->task);
1065 goto fail_with_cleanup;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001066 }
1067
1068 get_task_struct(vcpu->task);
Andrew Scullb722f952018-09-27 15:39:10 +01001069 vcpu->vm = vm;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001070 vcpu->vcpu_index = j;
Wedson Almeida Filho7fe62332018-12-15 03:09:57 +00001071 atomic_set(&vcpu->abort_sleep, 0);
Andrew Scullece5ef42019-05-08 15:07:25 +01001072 atomic_set(&vcpu->waiting_for_message, 0);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001073 }
1074 }
1075
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001076 /* Register protocol and socket family. */
1077 ret = proto_register(&hf_sock_proto, 0);
1078 if (ret) {
1079 pr_err("Unable to register protocol: %lld\n", ret);
1080 goto fail_with_cleanup;
1081 }
1082
1083 ret = sock_register(&proto_family);
1084 if (ret) {
1085 pr_err("Unable to register Hafnium's socket family: %lld\n",
1086 ret);
1087 goto fail_unregister_proto;
1088 }
1089
1090 /*
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001091 * Register as a driver for the timer device, so we can register a
1092 * handler for the hyperviser timer IRQ.
1093 */
1094 ret = platform_driver_register(&hf_int_driver);
1095 if (ret != 0) {
1096 pr_err("Error registering timer driver %lld\n", ret);
1097 goto fail_unregister_socket;
1098 }
1099
1100 /*
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001101 * Start running threads now that all is initialized.
1102 *
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001103 * Any failures from this point on must also unregister the driver with
1104 * platform_driver_unregister().
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001105 */
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001106 for (i = 0; i < hf_vm_count; i++) {
Andrew Scullb3a61b52018-09-17 14:30:34 +01001107 struct hf_vm *vm = &hf_vms[i];
Andrew Scull01778112019-01-14 15:37:53 +00001108
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001109 for (j = 0; j < vm->vcpu_count; j++)
1110 wake_up_process(vm->vcpu[j].task);
1111 }
1112
1113 /* Dump vm/vcpu count info. */
Andrew Scullbb7ae412018-09-28 21:07:15 +01001114 pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001115 for (i = 0; i < hf_vm_count; i++) {
1116 struct hf_vm *vm = &hf_vms[i];
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001117
Andrew Scullbb7ae412018-09-28 21:07:15 +01001118 pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count);
Andrew Scullb722f952018-09-27 15:39:10 +01001119 }
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001120
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001121 return 0;
Andrew Scull82257c42018-10-01 10:37:48 +01001122
Andrew Walbranb3ca1dc2019-01-30 17:13:44 +00001123fail_unregister_socket:
1124 sock_unregister(PF_HF);
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001125fail_unregister_proto:
1126 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001127fail_with_cleanup:
1128 hf_free_resources();
1129 return ret;
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001130}
1131
1132/**
Andrew Scullbb7ae412018-09-28 21:07:15 +01001133 * Frees up all resources used by the Hafnium driver in preparation for
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001134 * unloading it.
1135 */
1136static void __exit hf_exit(void)
1137{
Andrew Scullbb7ae412018-09-28 21:07:15 +01001138 pr_info("Preparing to unload Hafnium\n");
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001139 sock_unregister(PF_HF);
1140 proto_unregister(&hf_sock_proto);
Andrew Scull82257c42018-10-01 10:37:48 +01001141 hf_free_resources();
Andrew Walbran8d55e502019-02-05 11:42:08 +00001142 platform_driver_unregister(&hf_int_driver);
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001143 pr_info("Hafnium ready to unload\n");
1144}
1145
Wedson Almeida Filho1ee35652018-12-24 01:36:48 +00001146MODULE_LICENSE("GPL v2");
Wedson Almeida Filho2f62b422018-06-19 06:44:32 +01001147
1148module_init(hf_init);
1149module_exit(hf_exit);