|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * ACRN Hypervisor Service Module (HSM) |
| 4 | + * |
| 5 | + * Copyright (C) 2020 Intel Corporation. All rights reserved. |
| 6 | + * |
| 7 | + * Authors: |
| 8 | + |
| 9 | + |
| 10 | + */ |
| 11 | + |
| 12 | +#include <linux/miscdevice.h> |
| 13 | +#include <linux/mm.h> |
| 14 | +#include <linux/module.h> |
| 15 | +#include <linux/slab.h> |
| 16 | + |
| 17 | +#include <asm/acrn.h> |
| 18 | +#include <asm/hypervisor.h> |
| 19 | + |
| 20 | +#include "acrn_drv.h" |
| 21 | + |
| 22 | +/* |
| 23 | + * When /dev/acrn_hsm is opened, a 'struct acrn_vm' object is created to |
| 24 | + * represent a VM instance and continues to be associated with the opened file |
| 25 | + * descriptor. All ioctl operations on this file descriptor will be targeted to |
| 26 | + * the VM instance. Release of this file descriptor will destroy the object. |
| 27 | + */ |
| 28 | +static int acrn_dev_open(struct inode *inode, struct file *filp) |
| 29 | +{ |
| 30 | + struct acrn_vm *vm; |
| 31 | + |
| 32 | + vm = kzalloc(sizeof(*vm), GFP_KERNEL); |
| 33 | + if (!vm) |
| 34 | + return -ENOMEM; |
| 35 | + |
| 36 | + vm->vmid = ACRN_INVALID_VMID; |
| 37 | + filp->private_data = vm; |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static int acrn_dev_release(struct inode *inode, struct file *filp) |
| 42 | +{ |
| 43 | + struct acrn_vm *vm = filp->private_data; |
| 44 | + |
| 45 | + kfree(vm); |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static const struct file_operations acrn_fops = { |
| 50 | + .owner = THIS_MODULE, |
| 51 | + .open = acrn_dev_open, |
| 52 | + .release = acrn_dev_release, |
| 53 | +}; |
| 54 | + |
| 55 | +static struct miscdevice acrn_dev = { |
| 56 | + .minor = MISC_DYNAMIC_MINOR, |
| 57 | + .name = "acrn_hsm", |
| 58 | + .fops = &acrn_fops, |
| 59 | +}; |
| 60 | + |
| 61 | +static int __init hsm_init(void) |
| 62 | +{ |
| 63 | + int ret; |
| 64 | + |
| 65 | + if (x86_hyper_type != X86_HYPER_ACRN) |
| 66 | + return -ENODEV; |
| 67 | + |
| 68 | + if (!(cpuid_eax(ACRN_CPUID_FEATURES) & ACRN_FEATURE_PRIVILEGED_VM)) |
| 69 | + return -EPERM; |
| 70 | + |
| 71 | + ret = misc_register(&acrn_dev); |
| 72 | + if (ret) |
| 73 | + pr_err("Create misc dev failed!\n"); |
| 74 | + |
| 75 | + return ret; |
| 76 | +} |
| 77 | + |
| 78 | +static void __exit hsm_exit(void) |
| 79 | +{ |
| 80 | + misc_deregister(&acrn_dev); |
| 81 | +} |
| 82 | +module_init(hsm_init); |
| 83 | +module_exit(hsm_exit); |
| 84 | + |
| 85 | +MODULE_AUTHOR("Intel Corporation"); |
| 86 | +MODULE_LICENSE("GPL"); |
| 87 | +MODULE_DESCRIPTION("ACRN Hypervisor Service Module (HSM)"); |
0 commit comments