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