Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 1 | #include <linux/hrtimer.h> |
| 2 | #include <linux/init.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/kthread.h> |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 5 | #include <linux/mm.h> |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/sched/task.h> |
| 8 | #include <linux/slab.h> |
| 9 | |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 10 | #include <hf/call.h> |
| 11 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 12 | struct hf_vcpu { |
| 13 | spinlock_t lock; |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 14 | struct hf_vm *vm; |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 15 | uint32_t vcpu_index; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 16 | struct task_struct *task; |
| 17 | struct hrtimer timer; |
| 18 | bool pending_irq; |
| 19 | }; |
| 20 | |
| 21 | struct hf_vm { |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 22 | uint32_t id; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 23 | uint32_t vcpu_count; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 24 | struct hf_vcpu *vcpu; |
| 25 | }; |
| 26 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 27 | static struct hf_vm *hf_vms; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 28 | static uint32_t hf_vm_count; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 29 | static struct page *hf_send_page = NULL; |
| 30 | static struct page *hf_recv_page = NULL; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 31 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 32 | /** |
| 33 | * Wakes up the thread associated with the vcpu that owns the given timer. This |
| 34 | * is called when the timer the thread is waiting on expires. |
| 35 | */ |
| 36 | static enum hrtimer_restart hf_vcpu_timer_expired(struct hrtimer *timer) |
| 37 | { |
| 38 | struct hf_vcpu *vcpu = container_of(timer, struct hf_vcpu, timer); |
| 39 | wake_up_process(vcpu->task); |
| 40 | return HRTIMER_NORESTART; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * This is the main loop of each vcpu. |
| 45 | */ |
| 46 | static int hf_vcpu_thread(void *data) |
| 47 | { |
| 48 | struct hf_vcpu *vcpu = data; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 49 | struct hf_vcpu_run_return ret; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 50 | |
| 51 | hrtimer_init(&vcpu->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 52 | vcpu->timer.function = &hf_vcpu_timer_expired; |
| 53 | |
| 54 | while (!kthread_should_stop()) { |
| 55 | unsigned long flags; |
| 56 | size_t irqs; |
| 57 | |
| 58 | set_current_state(TASK_RUNNING); |
| 59 | |
| 60 | /* Determine if we must interrupt the vcpu. */ |
| 61 | spin_lock_irqsave(&vcpu->lock, flags); |
| 62 | irqs = vcpu->pending_irq ? 1 : 0; |
| 63 | vcpu->pending_irq = false; |
| 64 | spin_unlock_irqrestore(&vcpu->lock, flags); |
| 65 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 66 | /* Call into Hafnium to run vcpu. */ |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 67 | ret = hf_vcpu_run(vcpu->vm->id, vcpu->vcpu_index); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 68 | |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 69 | switch (ret.code) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 70 | /* Yield (forcibly or voluntarily). */ |
| 71 | case HF_VCPU_RUN_YIELD: |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 72 | break; |
| 73 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 74 | /* WFI. */ |
| 75 | case HF_VCPU_RUN_WAIT_FOR_INTERRUPT: |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 76 | set_current_state(TASK_INTERRUPTIBLE); |
| 77 | if (kthread_should_stop()) |
| 78 | break; |
| 79 | schedule(); |
| 80 | break; |
| 81 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 82 | /* Wake up another vcpu. */ |
| 83 | case HF_VCPU_RUN_WAKE_UP: |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 84 | { |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 85 | struct hf_vm *vm; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 86 | if (ret.wake_up.vm_id > hf_vm_count) |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 87 | break; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 88 | vm = &hf_vms[ret.wake_up.vm_id - 1]; |
| 89 | if (ret.wake_up.vcpu < vm->vcpu_count) { |
| 90 | wake_up_process(vm->vcpu[ret.wake_up.vcpu].task); |
| 91 | } else if (ret.wake_up.vcpu == HF_INVALID_VCPU) { |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 92 | /* TODO: pick one to interrupt. */ |
| 93 | pr_warning("No vcpu to wake."); |
| 94 | } |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 95 | } |
| 96 | break; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 97 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 98 | /* Response available. */ |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 99 | case HF_VCPU_RUN_MESSAGE: |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 100 | { |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 101 | uint32_t i; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 102 | const char *buf = page_address(hf_recv_page); |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 103 | pr_info("Received response from vm %u (%u bytes): ", |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 104 | vcpu->vm->id, ret.message.size); |
| 105 | for (i = 0; i < ret.message.size; i++) |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 106 | printk(KERN_CONT "%c", buf[i]); |
| 107 | printk(KERN_CONT "\n"); |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 108 | hf_mailbox_clear(); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 109 | } |
| 110 | break; |
Andrew Scull | dc8cab5 | 2018-10-10 18:29:39 +0100 | [diff] [blame^] | 111 | |
| 112 | case HF_VCPU_RUN_SLEEP: |
| 113 | set_current_state(TASK_INTERRUPTIBLE); |
| 114 | if (kthread_should_stop()) |
| 115 | break; |
| 116 | hrtimer_start(&vcpu->timer, ret.sleep.ns, HRTIMER_MODE_REL); |
| 117 | schedule(); |
| 118 | hrtimer_cancel(&vcpu->timer); |
| 119 | break; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | set_current_state(TASK_RUNNING); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 129 | * Frees all resources, including threads, associated with the Hafnium driver. |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 130 | */ |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 131 | static void hf_free_resources(uint32_t vm_count) |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 132 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 133 | uint32_t i, j; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * First stop all worker threads. We need to do this before freeing |
| 137 | * resources because workers may reference each other, so it is only |
| 138 | * safe to free resources after they have all stopped. |
| 139 | */ |
| 140 | for (i = 0; i < vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 141 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 142 | for (j = 0; j < vm->vcpu_count; j++) |
| 143 | kthread_stop(vm->vcpu[j].task); |
| 144 | } |
| 145 | |
| 146 | /* Free resources. */ |
| 147 | for (i = 0; i < vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 148 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 149 | for (j = 0; j < vm->vcpu_count; j++) |
| 150 | put_task_struct(vm->vcpu[j].task); |
| 151 | kfree(vm->vcpu); |
| 152 | } |
| 153 | |
| 154 | kfree(hf_vms); |
| 155 | } |
| 156 | |
| 157 | static ssize_t hf_interrupt_store(struct kobject *kobj, |
| 158 | struct kobj_attribute *attr, const char *buf, |
| 159 | size_t count) |
| 160 | { |
| 161 | struct hf_vcpu *vcpu; |
| 162 | unsigned long flags; |
| 163 | struct task_struct *task; |
| 164 | |
| 165 | /* TODO: Parse input to determine which vcpu to interrupt. */ |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 166 | /* TODO: Check bounds. */ |
| 167 | |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 168 | vcpu = &hf_vms[0].vcpu[0]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 169 | |
| 170 | spin_lock_irqsave(&vcpu->lock, flags); |
| 171 | vcpu->pending_irq = true; |
| 172 | /* TODO: Do we need to increment the task's ref count here? */ |
| 173 | task = vcpu->task; |
| 174 | spin_unlock_irqrestore(&vcpu->lock, flags); |
| 175 | |
| 176 | /* Wake up the task. If it's already running, kick it out. */ |
| 177 | /* TODO: There's a race here: the kick may happen right before we go |
| 178 | * to the hypervisor. */ |
| 179 | if (wake_up_process(task) == 0) |
| 180 | kick_process(task); |
| 181 | |
| 182 | return count; |
| 183 | } |
| 184 | |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 185 | static ssize_t hf_send_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 186 | const char *buf, size_t count) |
| 187 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 188 | int64_t ret; |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 189 | struct hf_vm *vm; |
| 190 | |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 191 | count = min_t(size_t, count, HF_MAILBOX_SIZE); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 192 | |
| 193 | /* Copy data to send buffer. */ |
| 194 | memcpy(page_address(hf_send_page), buf, count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 195 | |
| 196 | vm = &hf_vms[0]; |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 197 | ret = hf_mailbox_send(vm->id, count); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return -EAGAIN; |
| 200 | |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 201 | if (ret == HF_INVALID_VCPU) { |
| 202 | /* |
| 203 | * TODO: We need to interrupt some vcpu because none are waiting |
| 204 | * for data. |
| 205 | */ |
| 206 | pr_warning("No vcpu to receive message."); |
| 207 | return -ENOSYS; |
| 208 | } |
| 209 | |
| 210 | if (ret >= vm->vcpu_count) |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | |
Andrew Scull | 0973a2e | 2018-10-05 11:11:24 +0100 | [diff] [blame] | 213 | /* Wake up the vcpu that is going to process the data. */ |
| 214 | /* TODO: There's a race where thread may get wake up before it |
| 215 | * goes to sleep. Fix this. */ |
| 216 | wake_up_process(vm->vcpu[ret].task); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 217 | |
| 218 | return count; |
| 219 | } |
| 220 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 221 | static struct kobject *hf_sysfs_obj = NULL; |
| 222 | static struct kobj_attribute interrupt_attr = |
| 223 | __ATTR(interrupt, 0200, NULL, hf_interrupt_store); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 224 | static struct kobj_attribute send_attr = |
| 225 | __ATTR(send, 0200, NULL, hf_send_store); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 226 | |
| 227 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 228 | * Initializes the Hafnium driver's sysfs interface. |
| 229 | */ |
| 230 | static void __init hf_init_sysfs(void) |
| 231 | { |
| 232 | int ret; |
| 233 | |
| 234 | /* Create the sysfs interface to interrupt vcpus. */ |
| 235 | hf_sysfs_obj = kobject_create_and_add("hafnium", kernel_kobj); |
| 236 | if (!hf_sysfs_obj) { |
| 237 | pr_err("Unable to create sysfs object"); |
| 238 | } else { |
| 239 | ret = sysfs_create_file(hf_sysfs_obj, &interrupt_attr.attr); |
| 240 | if (ret) |
| 241 | pr_err("Unable to create 'interrupt' sysfs file"); |
| 242 | |
| 243 | ret = sysfs_create_file(hf_sysfs_obj, &send_attr.attr); |
| 244 | if (ret) |
| 245 | pr_err("Unable to create 'send' sysfs file"); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Initializes the Hafnium driver by creating a thread for each vCPU of each |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 251 | * virtual machine. |
| 252 | */ |
| 253 | static int __init hf_init(void) |
| 254 | { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 255 | int64_t ret; |
| 256 | uint32_t i, j; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 257 | |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 258 | /* Allocate a page for send and receive buffers. */ |
| 259 | hf_send_page = alloc_page(GFP_KERNEL); |
| 260 | if (!hf_send_page) { |
| 261 | pr_err("Unable to allocate send buffer\n"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
| 265 | hf_recv_page = alloc_page(GFP_KERNEL); |
| 266 | if (!hf_recv_page) { |
| 267 | __free_page(hf_send_page); |
| 268 | pr_err("Unable to allocate receive buffer\n"); |
| 269 | return -ENOMEM; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Configure both addresses. Once configured, we cannot free these pages |
| 274 | * because the hypervisor will use them, even if the module is |
| 275 | * unloaded. |
| 276 | */ |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 277 | ret = hf_vm_configure(page_to_phys(hf_send_page), |
| 278 | page_to_phys(hf_recv_page)); |
Wedson Almeida Filho | f9e1192 | 2018-08-12 15:54:31 +0100 | [diff] [blame] | 279 | if (ret) { |
| 280 | __free_page(hf_send_page); |
| 281 | __free_page(hf_recv_page); |
| 282 | /* TODO: We may want to grab this information from hypervisor |
| 283 | * and go from there. */ |
| 284 | pr_err("Unable to configure VM\n"); |
| 285 | return -EIO; |
| 286 | } |
| 287 | |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 288 | /* Get the number of VMs and allocate storage for them. */ |
Andrew Scull | 5570423 | 2018-08-10 17:19:54 +0100 | [diff] [blame] | 289 | ret = hf_vm_get_count(); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 290 | if (ret < 1) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 291 | pr_err("Unable to retrieve number of VMs: %lld\n", ret); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 295 | /* Only track the secondary VMs. */ |
| 296 | hf_vm_count = ret - 1; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 297 | hf_vms = kmalloc(sizeof(struct hf_vm) * hf_vm_count, GFP_KERNEL); |
| 298 | if (!hf_vms) |
| 299 | return -ENOMEM; |
| 300 | |
| 301 | /* Initialize each VM. */ |
| 302 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 303 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 304 | |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 305 | /* Adjust the ID as only the secondaries are tracked. */ |
| 306 | vm->id = i + 1; |
| 307 | |
| 308 | ret = hf_vcpu_get_count(vm->id); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 309 | if (ret < 0) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 310 | pr_err("HF_VCPU_GET_COUNT failed for vm=%u: %lld", |
| 311 | vm->id, ret); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 312 | hf_free_resources(i); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | vm->vcpu_count = ret; |
| 317 | vm->vcpu = kmalloc(sizeof(struct hf_vcpu) * vm->vcpu_count, |
| 318 | GFP_KERNEL); |
| 319 | if (!vm->vcpu) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 320 | pr_err("No memory for %u vcpus for vm %u", |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 321 | vm->vcpu_count, vm->id); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 322 | hf_free_resources(i); |
| 323 | return -ENOMEM; |
| 324 | } |
| 325 | |
| 326 | /* Create a kernel thread for each vcpu. */ |
| 327 | for (j = 0; j < vm->vcpu_count; j++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 328 | struct hf_vcpu *vcpu = &vm->vcpu[j]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 329 | vcpu->task = kthread_create(hf_vcpu_thread, vcpu, |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 330 | "vcpu_thread_%u_%u", |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 331 | vm->id, j); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 332 | if (IS_ERR(vcpu->task)) { |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 333 | pr_err("Error creating task (vm=%u,vcpu=%u)" |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 334 | ": %ld\n", vm->id, j, PTR_ERR(vcpu->task)); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 335 | vm->vcpu_count = j; |
| 336 | hf_free_resources(i + 1); |
| 337 | return PTR_ERR(vcpu->task); |
| 338 | } |
| 339 | |
| 340 | get_task_struct(vcpu->task); |
| 341 | spin_lock_init(&vcpu->lock); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 342 | vcpu->vm = vm; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 343 | vcpu->vcpu_index = j; |
| 344 | vcpu->pending_irq = false; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* Start running threads now that all is initialized. */ |
| 349 | for (i = 0; i < hf_vm_count; i++) { |
Andrew Scull | b3a61b5 | 2018-09-17 14:30:34 +0100 | [diff] [blame] | 350 | struct hf_vm *vm = &hf_vms[i]; |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 351 | for (j = 0; j < vm->vcpu_count; j++) |
| 352 | wake_up_process(vm->vcpu[j].task); |
| 353 | } |
| 354 | |
| 355 | /* Dump vm/vcpu count info. */ |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 356 | pr_info("Hafnium successfully loaded with %u VMs:\n", hf_vm_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 357 | for (i = 0; i < hf_vm_count; i++) { |
| 358 | struct hf_vm *vm = &hf_vms[i]; |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 359 | pr_info("\tVM %u: %u vCPUS\n", vm->id, vm->vcpu_count); |
Andrew Scull | b722f95 | 2018-09-27 15:39:10 +0100 | [diff] [blame] | 360 | } |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 361 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 362 | hf_init_sysfs(); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /** |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 368 | * Frees up all resources used by the Hafnium driver in preparation for |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 369 | * unloading it. |
| 370 | */ |
| 371 | static void __exit hf_exit(void) |
| 372 | { |
| 373 | if (hf_sysfs_obj) |
| 374 | kobject_put(hf_sysfs_obj); |
| 375 | |
Andrew Scull | bb7ae41 | 2018-09-28 21:07:15 +0100 | [diff] [blame] | 376 | pr_info("Preparing to unload Hafnium\n"); |
Wedson Almeida Filho | 2f62b42 | 2018-06-19 06:44:32 +0100 | [diff] [blame] | 377 | hf_free_resources(hf_vm_count); |
| 378 | pr_info("Hafnium ready to unload\n"); |
| 379 | } |
| 380 | |
| 381 | MODULE_LICENSE("GPL"); |
| 382 | |
| 383 | module_init(hf_init); |
| 384 | module_exit(hf_exit); |