ANDROID: drivers: virt: Initialization halla module We are adding a GKI module for the Halla Hypervisor. This module interfaces with Halla in Crosvm, and the Halla hypervisor must run at EL2. The related features include VM operation, such as virtual timer and debug capabilities. Bug: 414538168 Bug: 464945327 Change-Id: I777f4ea64e6f189b185e3865b54a9aebc6dd76d0 Signed-off-by: Kim Mankyum <mankyum.kim@samsung.com>
diff --git a/arch/arm64/configs/gki_defconfig b/arch/arm64/configs/gki_defconfig index a568e47..1014f4c 100644 --- a/arch/arm64/configs/gki_defconfig +++ b/arch/arm64/configs/gki_defconfig
@@ -611,6 +611,7 @@ CONFIG_GUNYAH_IRQFD=y CONFIG_GUNYAH_IOEVENTFD=y CONFIG_GUNYAH_QCOM_TRUSTED_VM=y +CONFIG_EXYNOS_HVM=m CONFIG_VIRTIO_PCI=m CONFIG_VIRTIO_BALLOON=m CONFIG_VHOST_VSOCK=y
diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig index 6692963..6d0032d 100644 --- a/drivers/virt/Kconfig +++ b/drivers/virt/Kconfig
@@ -53,4 +53,6 @@ source "drivers/virt/gunyah/Kconfig" +source "drivers/virt/halla/Kconfig" + endif
diff --git a/drivers/virt/Makefile b/drivers/virt/Makefile index ef6a3835..febd4e6 100644 --- a/drivers/virt/Makefile +++ b/drivers/virt/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_ACRN_HSM) += acrn/ obj-y += coco/ obj-y += gunyah/ +obj-$(CONFIG_EXYNOS_HVM) += halla/
diff --git a/drivers/virt/halla/Kconfig b/drivers/virt/halla/Kconfig new file mode 100644 index 0000000..b42685b --- /dev/null +++ b/drivers/virt/halla/Kconfig
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Exynos HVM solutions +# + +config EXYNOS_HVM + tristate "Exynos Halla VM Management driver" + depends on ARM64 && EVENTFD + help + Exynos Halla VM Management(HVM) module. + + This module supports the Guest VM to operate on the Halla hypervisor. + In addition, it interacts with the crosvm to manage and operate the + Guest VM. + + Select M if you want it be built as a module (exynos-hvm.ko). + + If unsure, say N.
diff --git a/drivers/virt/halla/Makefile b/drivers/virt/halla/Makefile new file mode 100644 index 0000000..5ff1ad2 --- /dev/null +++ b/drivers/virt/halla/Makefile
@@ -0,0 +1,6 @@ +obj-$(CONFIG_EXYNOS_HVM) += exynos-hvm.o + +exynos-hvm-y := hvm_main.o hvm_vm.o \ + hvm_vcpu.o hvm_irqfd.o \ + hvm_ioeventfd.o hvm_mmu.o \ + hvm_vtimer.o hvm_debug.o
diff --git a/drivers/virt/halla/exynos-hvc.h b/drivers/virt/halla/exynos-hvc.h new file mode 100644 index 0000000..ba97577 --- /dev/null +++ b/drivers/virt/halla/exynos-hvc.h
@@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (c) 2025 Samsung Electronics. + * + * A header for Hypervisor Call(HVC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#ifndef __EXYNOS_HVC_H__ +#define __EXYNOS_HVC_H__ + +#include <linux/arm-smccc.h> + +/* HVC FID */ +#define HVC_FID_BASE (0xC6000000) + +#define HVC_FID_HVM_CREATE_VM (HVC_FID_BASE + 0x400) +#define HVC_FID_HVM_DESTROY_VM (HVC_FID_BASE + 0x401) +#define HVC_FID_HVM_PRE_DESTROY_VM (HVC_FID_BASE + 0x402) +#define HVC_FID_HVM_CREATE_VCPU (HVC_FID_BASE + 0x410) +#define HVC_FID_HVM_DESTROY_VCPU (HVC_FID_BASE + 0x411) +#define HVC_FID_HVM_SET_MEMREGION (HVC_FID_BASE + 0x412) +#define HVC_FID_HVM_VCPU_RUN (HVC_FID_BASE + 0x413) +#define HVC_FID_HVM_GET_ONE_REG (HVC_FID_BASE + 0x414) +#define HVC_FID_HVM_SET_ONE_REG (HVC_FID_BASE + 0x415) +#define HVC_FID_HVM_INIT_STAGE2_MMU (HVC_FID_BASE + 0x416) +#define HVC_FID_HVM_REGISTER_VM_PGTABLE (HVC_FID_BASE + 0x417) +#define HVC_FID_HVM_NEED_VM_PGTABLE (HVC_FID_BASE + 0x418) +#define HVC_FID_HVM_RECLAIM_GUEST_MEMORY (HVC_FID_BASE + 0x419) +#define HVC_FID_HVM_RECLAIM_GUEST_S2_PGTABLE (HVC_FID_BASE + 0x41A) +#define HVC_FID_HVM_IRQ_LINE (HVC_FID_BASE + 0x420) +#define HVC_FID_HVM_CREATE_DEVICE (HVC_FID_BASE + 0x421) +#define HVC_FID_HVM_PROBE (HVC_FID_BASE + 0x430) +#define HVC_FID_HVM_ENABLE_CAP (HVC_FID_BASE + 0x431) +#define HVC_FID_HVM_NOTIFY_EXIT (HVC_FID_BASE + 0x432) +#define HVC_FID_HVM_MEMREGION_PURPOSE (HVC_FID_BASE + 0x433) +#define HVC_FID_HVM_SET_DTB_CONFIG (HVC_FID_BASE + 0x434) +#define HVC_FID_HVM_MAP_GUEST (HVC_FID_BASE + 0x440) +#define HVC_FID_HVM_MAP_GUEST_BLOCK (HVC_FID_BASE + 0x441) +#define HVC_FID_HVM_DEBUG (HVC_FID_BASE + 0x442) +#define HVC_FID_HYPERVISOR_GET_VM_IDLE_TIME (HVC_FID_BASE + 0x443) + +/* HVC ERROR */ +#define HVC_OK (0x0) +#define HVC_UNK (-1) + +#ifndef __ASSEMBLY__ +#include <linux/types.h> + +/* HVC_FID_GET_HALLA_INFO */ +enum halla_info_type { + HALLA_INFO_TYPE_VERSION = 0, + HALLA_INFO_TYPE_HALLA_BASE, + HALLA_INFO_TYPE_HALLA_SIZE, + HALLA_INFO_TYPE_RAM_LOG_BASE, + HALLA_INFO_TYPE_RAM_LOG_SIZE, + HALLA_INFO_TYPE_VM_S2_PTDUMP_BASE = 8, + HALLA_INFO_TYPE_VM_S2_PTDUMP_SIZE +}; + +static inline unsigned long exynos_hvc(unsigned long hvc_fid, + unsigned long arg1, + unsigned long arg2, + unsigned long arg3, + unsigned long arg4) +{ + struct arm_smccc_res res; + + arm_smccc_hvc(hvc_fid, arg1, arg2, arg3, arg4, 0, 0, 0, &res); + + return res.a0; +} + +#endif /* __ASSEMBLY__ */ + +#endif /* __EXYNOS_HVC_H__ */
diff --git a/drivers/virt/halla/hvm_debug.c b/drivers/virt/halla/hvm_debug.c new file mode 100644 index 0000000..d3b4f2f --- /dev/null +++ b/drivers/virt/halla/hvm_debug.c
@@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/debugfs.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +static ssize_t translate_addr_ops_get_pa(struct file *file, char __user *buf, + size_t len, loff_t *offset) +{ + char tmp[64] = {0, }; + struct hvm *vm = file->private_data; + u64 ipa, pa; + u64 ret; + + if (*offset == 0) { + // get translated pa + ipa = vm->vm_debug.ipa_requested; + + ret = exynos_hvc(HVC_FID_HVM_DEBUG, HVM_DEBUG_TRANSLATE_IPA2PA, + vm->vm_id, ipa, 0); + + if (ret == -ENOMEM) { + snprintf(tmp, sizeof(tmp), + "Invalid address 0x%llx: Outside of vm memory\n", ipa); + goto exit; + } else if (ret == -EFAULT) { + snprintf(tmp, sizeof(tmp), + "Invalid address 0x%llx: Not mapped\n", ipa); + goto exit; + } + + pa = ret; + + // Is ipa shared with host? + ret = exynos_hvc(HVC_FID_HVM_DEBUG, HVM_DEBUG_IS_SHARED_PAGE, + vm->vm_id, ipa, 0); + + if (ret == -EFAULT) { + snprintf(tmp, sizeof(tmp), + "Invalid address 0X%llx: Not mapped\n", ipa); + goto exit; + } + + snprintf(tmp, sizeof(tmp), "0x%llx -> 0x%llx: %s\n", + ipa, pa, (ret ? "Shared w/ host" : "Protected")); + +exit: + if (copy_to_user(buf, tmp, sizeof(tmp))) + return -EFAULT; + + *offset += sizeof(tmp); + + return sizeof(tmp); + } + + return 0; +} + +static ssize_t translate_addr_ops_set_ipa(struct file *file, const char __user *buf, + size_t len, loff_t *offset) +{ + char tmp[64] = {0, }; + struct hvm *vm = file->private_data; + ssize_t ret = -EFAULT; + + if (*offset == 0) { + // set ipa to be translated + if (copy_from_user(tmp, buf, sizeof(tmp))) + return -EFAULT; + + ret = kstrtoull(tmp, 0, &vm->vm_debug.ipa_requested); + if (ret) + return ret; + + return len; + } + + return 0; +} + +static const struct file_operations translate_addr_ops = { + .open = simple_open, + .read = translate_addr_ops_get_pa, + .write = translate_addr_ops_set_ipa, +}; + +int hvm_drv_debug_init(void) +{ + debugfs_create_dir(HALLA_KERNEL_DEV_NAME, NULL); + + return 0; +} + +int hvm_create_vm_debugfs(struct hvm *hvm) +{ + struct dentry *dent, *parent; + char dname[16]; + + snprintf(dname, sizeof(dname), "VM-%d", hvm->vm_id); + + parent = debugfs_lookup(HALLA_KERNEL_DEV_NAME, NULL); + + dent = debugfs_create_dir(dname, parent); + + debugfs_create_file("stage2_ipa_to_pa", 0644, dent, hvm, &translate_addr_ops); + + dput(parent); + + return 0; +} + +int hvm_destroy_vm_debugfs(struct hvm *hvm) +{ + struct dentry *parent; + char dname[16]; + + snprintf(dname, sizeof(dname), "VM-%d", hvm->vm_id); + + parent = debugfs_lookup(HALLA_KERNEL_DEV_NAME, NULL); + + debugfs_lookup_and_remove(dname, parent); + + dput(parent); + + return 0; +} + +void hvm_drv_debug_exit(void) +{ + debugfs_lookup_and_remove(HALLA_KERNEL_DEV_NAME, NULL); +}
diff --git a/drivers/virt/halla/hvm_drv.h b/drivers/virt/halla/hvm_drv.h new file mode 100644 index 0000000..a07bbd0 --- /dev/null +++ b/drivers/virt/halla/hvm_drv.h
@@ -0,0 +1,481 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#ifndef __HVM_DRV_H__ +#define __HVM_DRV_H__ + +#include <linux/eventfd.h> +#include <linux/list.h> +#include <linux/mm.h> +#include <linux/mutex.h> +#include <linux/srcu.h> +#include <linux/rbtree.h> +#include <linux/clocksource.h> +#include <linux/const.h> +#include <linux/ioctl.h> +#include <linux/types.h> + + +/* UAPI data structure between kernel drvier and user space */ +/* HVM ioctls */ +#define HVM_IOC_MAGIC 0x4C /* halla */ + +/* ioctls for /dev/hvm fds */ +#define HVM_CREATE_VM _IO(HVM_IOC_MAGIC, 0x01) +#define HVM_CHECK_EXTENSION _IO(HVM_IOC_MAGIC, 0x02) + +/* ioctls for VM fds */ +#define HVM_CREATE_VCPU _IO(HVM_IOC_MAGIC, 0x20) +#define HVM_SET_USER_MEMORY_REGION _IOW(HVM_IOC_MAGIC, 0x41, \ + struct hvm_userspace_memory_region) + +#define HVM_IRQFD _IOW(HVM_IOC_MAGIC, 0x61, \ + struct hvm_irqfd) +#define HVM_IRQ_LINE _IOW(HVM_IOC_MAGIC, 0x62, \ + struct hvm_irq_level) + +#define HVM_CREATE_DEVICE _IOWR(HVM_IOC_MAGIC, 0x80, \ + struct hvm_create_device) + +#define HVM_IOEVENTFD _IOW(HVM_IOC_MAGIC, 0xa0, \ + struct hvm_ioeventfd) + +#define HVM_ENABLE_CAP _IOW(HVM_IOC_MAGIC, 0x03, \ + struct hvm_enable_cap) +#define HVM_SET_DTB_CONFIG _IOW(HVM_IOC_MAGIC, 0x04, \ + struct hvm_dtb_config) + +/* ioctls for vcpu fds */ +#define HVM_VCPU_RUN _IO(HVM_IOC_MAGIC, 0x21) + +#define HVM_GET_ONE_REG _IOW(HVM_IOC_MAGIC, 0x28, \ + struct hvm_one_reg) +#define HVM_SET_ONE_REG _IOW(HVM_IOC_MAGIC, 0x29, \ + struct hvm_one_reg) + +/* Capability for HVM_CHECK_EXTENSION */ +#define HVM_CAP_VM_GPA_SIZE 0x475041 // 'GPA' +#define HVM_CAP_PROTECTED_VM 0x50564d // 'PVM' + +/* sub-commands put in args[0] for HVM_CAP_PROTECTED_VM */ +#define HVM_CAP_PVM_SET_PVMFW_GPA 0 +#define HVM_CAP_PVM_GET_PVMFW_SIZE 1 +/* HVM_CAP_PVM_SET_PROTECTED_VM only sets protected but not load pvmfw */ +#define HVM_CAP_PVM_SET_PROTECTED_VM 2 + +/* + * Reg size = BIT((reg.id & HVM_REG_SIZE_MASK) >> HVM_REG_SIZE_SHIFT) bytes + */ +#define HVM_REG_SIZE_SHIFT 52 +#define HVM_REG_SIZE_MASK GENMASK_ULL(55, 52) + +/* bit field for HVM_IRQ_LINE */ +#define HVM_IRQ_VCPU_MASK 0xff +#define HVM_IRQ_LINE_TYPE GENMASK(27, 24) +#define HVM_IRQ_LINE_VCPU GENMASK(23, 16) +#define HVM_IRQ_LINE_VCPU2 GENMASK(31, 28) +#define HVM_IRQ_LINE_NUM GENMASK(15, 0) + +/* Flags for irqfd */ +#define HVM_IRQFD_FLAG_DEASSIGN BIT(0) +#define HVM_IRQFD_FLAG_RESAMPLE BIT(1) + +/* Flags for ioeventfd */ +#define HVM_IOEVENTFD_FLAG_DATAMATCH BIT(0) +#define HVM_IOEVENTFD_FLAG_PIO BIT(1) +#define HVM_IOEVENTFD_FLAG_DEASSIGN BIT(2) +#define HVM_IOEVENTFD_VALID_FLAG_MASK GENMASK(2, 0) + +/* Timer PPI */ +#define HVM_IRQ_PPI_START_NUM (16) + +/* Halla dev name */ +#define HALLA_KERNEL_DEV_NAME "halla" + +/* VM exit reason */ +enum { + HVM_EXIT_HALLA = 0x484c0000, + HVM_EXIT_MMIO = 0x484c0001, + HVM_EXIT_IRQ = 0x484c0002, + HVM_EXIT_EXCEPTION = 0x484c0003, + HVM_EXIT_INTERNAL_ERROR = 0x484c0004, + HVM_EXIT_SYSTEM_EVENT = 0x484c0005, + HVM_EXIT_SHUTDOWN = 0x484c0006, + HVM_EXIT_IDLE = 0x484c0007, + HVM_EXIT_SGI = 0x484c0008, +}; + +/* exception definitions of HVM_EXIT_EXCEPTION */ +enum { + HVM_EXCEPTION_UNKNOWN = 0x0, + HVM_EXCEPTION_PAGE_FAULT = 0x1, +}; + +/** + * struct hvm_userspace_memory_region: For HVM_SET_USER_MEMORY_REGION. + */ +struct hvm_userspace_memory_region { + __u32 slot; + __u32 flags; + __u64 guest_phys_addr; + __u64 memory_size; + __u64 userspace_addr; +}; + +/** + * struct hvm_irq_level: For HVM_IRQ_LINE + */ +struct hvm_irq_level { + __u32 irq; + __u32 level; +}; + +/** + * struct hvm_create_device: For HVM_CREATE_DEVICE. + */ +struct hvm_create_device { + __u32 dev_type; + __u32 id; + __u64 flags; + __u64 dev_addr; + __u64 dev_reg_size; + __u64 attr_addr; + __u64 attr_size; +}; + +/** + * struct hvm_irqfd: For HVM_IRQFD + */ +struct hvm_irqfd { + __u32 fd; + __u32 gsi; + __u32 flags; + __u32 resamplefd; + __u8 pad[16]; +}; + +/** + * struct hvm_ioeventfd: For HVM_IOEVENTFD + */ +struct hvm_ioeventfd { + __u64 datamatch; + /* private: legal pio/mmio address */ + __u64 addr; + /* private: 1, 2, 4, or 8 bytes; or 0 to ignore length */ + __u32 len; + __s32 fd; + __u32 flags; + __u8 pad[36]; +}; + +/** + * struct hvm_enable_cap: For HVM_ENABLE_CAP + */ +struct hvm_enable_cap { + __u64 cap; + __u64 args[5]; +}; + +/** + * struct hvm_dtb_config: For HVM_SET_DTB_CONFIG + */ +struct hvm_dtb_config { + __u64 dtb_addr; + __u64 dtb_size; +}; + +/** + * Structures for guest exit reason + * This is used in hvm_vcpu_run structure's union + */ +/* HVM_EXIT_MMIO */ +struct mmio { + /* From FAR_EL2 */ + /* The address guest tries to access */ + __u64 phys_addr; + /* The value to be written (is_write is 1) or + * be filled by user for reads (is_write is 0) + */ + __u8 data[8]; + /* From ESR_EL2 as */ + /* The size of written data. + * Only the first `size` bytes of `data` are handled + */ + __u64 size; + /* From ESR_EL2 */ + /* The register number where the data is stored */ + __u32 reg_nr; + /* From ESR_EL2 */ + /* 1 for VM to perform a write or 0 for VM to perform a read */ + __u8 is_write; +}; + +/* HVM_EXIT_EXCEPTION */ +struct exception { + /* Which exception vector */ + __u32 exception; + /* Exception error codes */ + __u32 error_code; + /* Fault GPA (guest physical address or IPA in ARM) */ + __u64 fault_gpa; + /* Future-proof reservation and reset to zero in hypervisor. + * Fill up to the union size, 256 bytes. + */ + __u64 reserved[30]; +}; + +/* System Event types */ +enum { + SYSTEM_EVENT_SHUTDOWN = 1, + SYSTEM_EVENT_RESET, + SYSTEM_EVENT_CRASH, +}; + +/* HVM_EXIT_SYSTEM_EVENT */ +struct system_event { + /* System event type. + * Ex. HVM_SYSTEM_EVENT_SHUTDOWN or HVM_SYSTEM_EVENT_RESET...etc. + */ + __u32 type; + /* The number of elements used in data[] */ + __u32 ndata; + /* Keep the detailed information about HVM_EXIT_SYSTEM_EVENT */ + __u64 data[16]; +}; + +/** + * struct hvm_vcpu_run: For HVM_VCPU_RUN + * This struct is shared between Crosvm, kernel and Halla + */ +struct hvm_vcpu_run { + /* to userspace */ + __u32 exit_reason; + __u8 immediate_exit; + __u8 pad[3]; + /* union structure of collection of guest exit reason */ + union { + struct mmio mmio; + struct exception exception; + struct system_event system_event; + char padding[256]; + }; +}; + +/** + * struct hvm_one_reg: For HVM_GET/SET_ONE_REG + */ +struct hvm_one_reg { + __u64 id; + __u64 addr; +}; + + +/* Data structure for kernel driver and Halla */ +#define GIC_V3_NR_LRS 16 + +#define HVM_VCPU_MMAP_SIZE PAGE_SIZE +#define INVALID_VM_ID 0xffff + +/* + * The following data structures are for data transferring between driver and + * hypervisor, and they're aligned with hypervisor definitions + */ +#define HVM_MAX_VCPUS 10 +#define HVM_MAX_MEM_REGION 10 + +#define HVM_VCPU_RUN_MAP_SIZE (PAGE_SIZE * 2) + +#define HVM_MAX_VM_PGTABLE_SIZE (64) + +#define VM_S2_PTDUMP_DSS_NAME "log_vm_s2pt" + +/* structure for ioevent */ +struct hvm_ioevent { + struct list_head list; + u64 addr; + u32 len; + struct eventfd_ctx *evt_ctx; + u64 datamatch; + bool wildcard; +}; + +/* structures for memory region */ +struct mem_region_addr_range { + u64 address; + u32 pg_cnt; + u32 reserved; +}; + +struct hvm_memory_region_ranges { + u32 slot; + u32 constituent_cnt; + u64 total_pages; + u64 gpa; + struct mem_region_addr_range constituents[]; +}; + +/* structure for ppage */ +struct hvm_pinned_page { + struct rb_node node; + struct page *page; + u64 ipa; +}; + +/* vm debug types */ +enum { + HVM_DEBUG_TRANSLATE_IPA2PA, + HVM_DEBUG_IS_SHARED_PAGE, +}; + +/* structure for vm debug */ +struct hvm_vm_debug { + u64 ipa_requested; + u64 pa_translated; +}; + +/* + * A reasonable and large enough limit for the maximum number of pages a + * guest can use. + */ +#define HVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) + +#define HVM_RECLAIM_MIN_SIZE (0x10000) + +/* structure for VM's memory slot */ +struct hvm_memslot { + u64 base_gfn; + unsigned long npages; + unsigned long userspace_addr; + struct vm_area_struct *vma; + u32 flags; + u32 slot_id; +}; + +/* structure for VM's virt timer */ +struct hvm_vtimer { + u64 cntp_ctl_el0; + u64 cntp_cval_el0; + u64 cntp_tval_el0; +}; + +/* structure for management of vcpus */ +struct hvm_vcpu { + struct hvm *hvm; + u32 vcpuid; + /* lock of vcpu*/ + struct mutex lock; + struct hvm_vcpu_run *run; + struct rcuwait wait; + + struct hvm_vtimer vtimer; + struct hrtimer idle_hrtimer; + atomic_t idle_state; // >=1 need to wakeup +}; + +/** + * structure for management of VMs + */ +struct hvm { + struct hvm_vcpu *vcpus[HVM_MAX_VCPUS]; + struct mm_struct *mm; + struct hvm_memslot memslot[HVM_MAX_MEM_REGION]; + struct mutex lock; + + struct { + spinlock_t lock; + struct list_head items; + struct list_head resampler_list; + struct mutex resampler_lock; + } irqfds; + + struct list_head ioevents; + struct mutex ioevent_lock; + + struct list_head vm_list; + u16 vm_id; + + struct srcu_struct irq_srcu; + + struct rb_root pinned_pages; + struct mutex mem_lock; + + unsigned long *s2table_buf; + + /* Each pointer points to PAGE_SIZE * 2 of memory */ + unsigned long *vm_pgtable[HVM_MAX_VM_PGTABLE_SIZE]; + unsigned int pgtable_num; + + struct hvm_vm_debug vm_debug; + + struct { + u32 mult; + u32 shift; + } clock_scale_factor; + u32 nr_vcpus; +}; + +void hvm_vtimer_init_regs(struct hvm_vcpu *vcpu); +void hvm_restore_vtimer(struct hvm_vcpu *vcpu); +void hvm_save_vtimer(struct hvm_vcpu *vcpu); +u32 __percpu *hvm_get_running_vcpus(void); +u32 hvm_get_guest_vtimer_irq(void); +int hvm_vtimer_init(struct device *dev); +void hvm_vtimer_exit(void); + +int hvm_dev_ioctl_create_vm(unsigned long vm_type); + +void hvm_destroy_all_vms(void); + +void hvm_destroy_vcpus(struct hvm *hvm); + +int hvm_check_extension(struct hvm *hvm, + u64 cap, + void __user *argp); + +void *hvm_alloc_pages(size_t size); +void hvm_free_pages(void *virt, size_t size); +int hvm_init_vm_stage2_mmu(struct hvm *hvm); + +int hvm_vm_ioctl_create_vcpu(struct hvm *hvm, u32 cpuid); + +int hvm_handle_page_fault(struct hvm_vcpu *vcpu); + +void hvm_notify_acked_irq(struct hvm *hvm, + unsigned int gsi); +int hvm_irqfd(struct hvm *hvm, + struct hvm_irqfd *args); +int hvm_drv_irqfd_init(void); +void hvm_drv_irqfd_exit(void); +int hvm_vm_irqfd_init(struct hvm *hvm); +void hvm_vm_irqfd_release(struct hvm *hvm); + +int hvm_init_ioeventfd(struct hvm *hvm); +int hvm_ioeventfd(struct hvm *hvm, + struct hvm_ioeventfd *args); +bool hvm_ioevent_write(struct hvm_vcpu *vcpu, + u64 addr, + int len, + const void *val); +int hvm_inject_irq(struct hvm *hvm, + unsigned int vcpu_idx, + u32 irq, + bool level); + +void eventfd_ctx_do_read(struct eventfd_ctx *ctx, + u64 *cnt); +struct vm_area_struct *vma_lookup(struct mm_struct *mm, + unsigned long addr); +void add_wait_queue_priority(struct wait_queue_head *wq_head, + struct wait_queue_entry *wq_entry); +void hvm_vcpu_wakeup(struct hvm_vcpu *vcpu); + +/* hvm debug */ +int hvm_drv_debug_init(void); +void hvm_drv_debug_exit(void); +int hvm_create_vm_debugfs(struct hvm *hvm); +int hvm_destroy_vm_debugfs(struct hvm *hvm); + +#endif /* __HVM_DRV_H__ */
diff --git a/drivers/virt/halla/hvm_ioeventfd.c b/drivers/virt/halla/hvm_ioeventfd.c new file mode 100644 index 0000000..ecfbf05 --- /dev/null +++ b/drivers/virt/halla/hvm_ioeventfd.c
@@ -0,0 +1,277 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/eventfd.h> +#include <linux/file.h> +#include <linux/syscalls.h> +#include <linux/wait.h> +#include <linux/poll.h> +#include <linux/module.h> +#include <linux/slab.h> + +#include "hvm_drv.h" + +/** + * ioeventfd_check_collision() - Check collison assumes hvm->ioevent_lock held. + * @: Pointer to hvm. + * @p: Pointer to hvm_ioevent. + * + * Return: + * * true - collison found + * * false - no collison + */ +static bool ioeventfd_check_collision(struct hvm *hvm, struct hvm_ioevent *p) +{ + struct hvm_ioevent *_p; + + lockdep_assert_held(&hvm->ioevent_lock); + + list_for_each_entry(_p, &hvm->ioevents, list) { + if (_p->addr == p->addr && + (!_p->len || !p->len || + (_p->len == p->len && + (_p->wildcard || p->wildcard || + _p->datamatch == p->datamatch)))) + return true; + if (p->addr >= _p->addr && p->addr < _p->addr + _p->len) + return true; + } + + return false; +} + +static void hvm_ioevent_release(struct hvm *hvm, struct hvm_ioevent *p) +{ + lockdep_assert_held(&hvm->ioevent_lock); + + eventfd_ctx_put(p->evt_ctx); + list_del(&p->list); + kfree(p); +} + +static bool hvm_ioevent_in_range(struct hvm_ioevent *p, u64 addr, + int len, const void *val) +{ + u64 _val; + + if (addr != p->addr) + /* address must be precise for a hit */ + return false; + + if (!p->len) + /* length = 0 means only look at the address, so always a hit */ + return true; + + if (len != p->len) + /* address-range must be precise for a hit */ + return false; + + if (p->wildcard) + /* all else equal, wildcard is always a hit */ + return true; + + /* otherwise, we have to actually compare the data */ + + switch (len) { + case 1: + _val = *(u8 *)val; + break; + case 2: + _val = *(u16 *)val; + break; + case 4: + _val = *(u32 *)val; + break; + case 8: + _val = *(u64 *)val; + break; + default: + return false; + } + + return _val == p->datamatch; +} + +static int hvm_deassign_ioeventfd(struct hvm *hvm, + struct hvm_ioeventfd *args) +{ + struct hvm_ioevent *p, *tmp; + struct eventfd_ctx *evt_ctx; + int ret = -ENOENT; + bool wildcard; + + evt_ctx = eventfd_ctx_fdget(args->fd); + if (IS_ERR(evt_ctx)) + return PTR_ERR(evt_ctx); + + wildcard = !(args->flags & HVM_IOEVENTFD_FLAG_DATAMATCH); + + mutex_lock(&hvm->ioevent_lock); + list_for_each_entry_safe(p, tmp, &hvm->ioevents, list) { + if (p->evt_ctx != evt_ctx || + p->addr != args->addr || + p->len != args->len || + p->wildcard != wildcard) + continue; + + if (!p->wildcard && p->datamatch != args->datamatch) + continue; + + hvm_ioevent_release(hvm, p); + ret = 0; + break; + } + + mutex_unlock(&hvm->ioevent_lock); + + /* got in the front of this function */ + eventfd_ctx_put(evt_ctx); + + return ret; +} + +static int hvm_assign_ioeventfd(struct hvm *hvm, + struct hvm_ioeventfd *args) +{ + struct eventfd_ctx *evt_ctx; + struct hvm_ioevent *evt; + int ret; + + evt_ctx = eventfd_ctx_fdget(args->fd); + if (IS_ERR(evt_ctx)) + return PTR_ERR(evt_ctx); + + evt = kmalloc(sizeof(*evt), GFP_KERNEL); + if (!evt) + return -ENOMEM; + *evt = (struct hvm_ioevent) { + .addr = args->addr, + .len = args->len, + .evt_ctx = evt_ctx, + }; + if (args->flags & HVM_IOEVENTFD_FLAG_DATAMATCH) { + evt->datamatch = args->datamatch; + evt->wildcard = false; + } else { + evt->wildcard = true; + } + + mutex_lock(&hvm->ioevent_lock); + if (ioeventfd_check_collision(hvm, evt)) { + ret = -EEXIST; + mutex_unlock(&hvm->ioevent_lock); + goto err_free; + } + + list_add_tail(&evt->list, &hvm->ioevents); + mutex_unlock(&hvm->ioevent_lock); + + return 0; + +err_free: + kfree(evt); + eventfd_ctx_put(evt_ctx); + return ret; +} + +/** + * hvm_ioeventfd_check_valid() - Check user arguments is valid. + * @args: Pointer to hvm_ioeventfd. + * + * Return: + * * true if user arguments are valid. + * * false if user arguments are invalid. + */ +static bool hvm_ioeventfd_check_valid(struct hvm_ioeventfd *args) +{ + /* must be natural-word sized, or 0 to ignore length */ + switch (args->len) { + case 0: + case 1: + case 2: + case 4: + case 8: + break; + default: + return false; + } + + /* check for range overflow */ + if (args->addr + args->len < args->addr) + return false; + + /* check for extra flags that we don't understand */ + if (args->flags & ~HVM_IOEVENTFD_VALID_FLAG_MASK) + return false; + + /* ioeventfd with no length can't be combined with DATAMATCH */ + if (!args->len && (args->flags & HVM_IOEVENTFD_FLAG_DATAMATCH)) + return false; + + /* does not support pio bus ioeventfd */ + if (args->flags & HVM_IOEVENTFD_FLAG_PIO) + return false; + + return true; +} + +/** + * hvm_ioeventfd() - Register ioevent to ioevent list. + * @: Pointer to hvm. + * @args: Pointer to hvm_ioeventfd. + * + * Return: + * * 0 - Success. + * * Negative - Failure. + */ +int hvm_ioeventfd(struct hvm *hvm, struct hvm_ioeventfd *args) +{ + if (hvm_ioeventfd_check_valid(args) == false) + return -EINVAL; + + if (args->flags & HVM_IOEVENTFD_FLAG_DEASSIGN) + return hvm_deassign_ioeventfd(hvm, args); + + return hvm_assign_ioeventfd(hvm, args); +} + +/** + * hvm_ioevent_write() - Travers this vm's registered ioeventfd to see if + * need notifying it. + * @vcpu: Pointer to vcpu. + * @addr: mmio address. + * @len: mmio size. + * @val: Pointer to void. + * + * Return: + * * true if this io is already sent to ioeventfd's listener. + * * false if we cannot find any ioeventfd registering this mmio write. + */ +bool hvm_ioevent_write(struct hvm_vcpu *vcpu, u64 addr, int len, + const void *val) +{ + struct hvm_ioevent *e; + + mutex_lock(&vcpu->hvm->ioevent_lock); + list_for_each_entry(e, &vcpu->hvm->ioevents, list) { + if (hvm_ioevent_in_range(e, addr, len, val)) { + eventfd_signal(e->evt_ctx); + mutex_unlock(&vcpu->hvm->ioevent_lock); + return true; + } + } + + mutex_unlock(&vcpu->hvm->ioevent_lock); + return false; +} + +int hvm_init_ioeventfd(struct hvm *hvm) +{ + INIT_LIST_HEAD(&hvm->ioevents); + mutex_init(&hvm->ioevent_lock); + + return 0; +}
diff --git a/drivers/virt/halla/hvm_irqfd.c b/drivers/virt/halla/hvm_irqfd.c new file mode 100644 index 0000000..099cc39 --- /dev/null +++ b/drivers/virt/halla/hvm_irqfd.c
@@ -0,0 +1,368 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/eventfd.h> +#include <linux/syscalls.h> + +#include "hvm_drv.h" + +struct hvm_irq_ack_notifier { + struct hlist_node link; + u32 gsi; + void (*irq_acked)(struct hvm_irq_ack_notifier *ian); +}; + +/** + * struct hvm_kernel_irqfd: hvm kernel irqfd descriptor. + * @hvm: Pointer to struct hvm. + * @wait: Wait queue entry. + * @gsi: Used for level IRQ fast-path. + * @eventfd: Used for setup/shutdown. + * @list: struct list_head. + * @pt: struct poll_table_struct. + * @shutdown: struct work_struct. + */ +struct hvm_kernel_irqfd { + struct hvm *hvm; + wait_queue_entry_t wait; + + int gsi; + + struct eventfd_ctx *eventfd; + struct list_head list; + poll_table pt; + struct work_struct shutdown; +}; + +static struct workqueue_struct *irqfd_cleanup_wq; + +/** + * irqfd_set_irq(): irqfd to inject virtual interrupt. + * @hvm: Pointer to hvm. + * @irq: This is spi interrupt number (starts from 0 instead of 32). + * @level: irq triggered level. + */ +static void irqfd_set_irq(struct hvm *hvm, u32 irq, int level) +{ + if (level) + hvm_inject_irq(hvm, 0, irq, level); +} + +/** + * irqfd_shutdown() - Race-free decouple logic (ordering is critical). + * @work: Pointer to work_struct. + */ +static void irqfd_shutdown(struct work_struct *work) +{ + struct hvm_kernel_irqfd *irqfd = + container_of(work, struct hvm_kernel_irqfd, shutdown); + struct hvm *hvm = irqfd->hvm; + u64 cnt; + + /* Make sure irqfd has been initialized in assign path. */ + synchronize_srcu(&hvm->irq_srcu); + + /* + * Synchronize with the wait-queue and unhook ourselves to prevent + * further events. + */ + eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt); + + /* + * It is now safe to release the object's resources + */ + eventfd_ctx_put(irqfd->eventfd); + kfree(irqfd); +} + +/** + * irqfd_is_active() + * @irqfd: Pointer to hvm_kernel_irqfd. + * + * Return: + * * true - irqfd is active. + */ +static bool irqfd_is_active(struct hvm_kernel_irqfd *irqfd) +{ + struct hvm *hvm = irqfd->hvm; + + lockdep_assert_held(&hvm->irqfds.lock); + + return list_empty(&irqfd->list) ? false : true; +} + +/** + * irqfd_deactivate() - Mark the irqfd as inactive and schedule it for removal. + * + * @irqfd: Pointer to hvm_kernel_irqfd. + */ +static void irqfd_deactivate(struct hvm_kernel_irqfd *irqfd) +{ + struct hvm *hvm = irqfd->hvm; + + if (!irqfd_is_active(irqfd)) + return; + + lockdep_assert_held(&hvm->irqfds.lock); + + list_del_init(&irqfd->list); + + queue_work(irqfd_cleanup_wq, &irqfd->shutdown); +} + +/** + * irqfd_wakeup() - Callback of irqfd wait queue, would be woken by writing to + * irqfd to do virtual interrupt injection. + * @wait: Pointer to wait_queue_entry_t. + * @mode: Unused. + * @sync: Unused. + * @key: Get flags about Epoll events. + * + * Return: + * * 0 - Success + */ +static int irqfd_wakeup(wait_queue_entry_t *wait, u32 mode, int sync, + void *key) +{ + struct hvm_kernel_irqfd *irqfd = + container_of(wait, struct hvm_kernel_irqfd, wait); + __poll_t flags = key_to_poll(key); + struct hvm *hvm = irqfd->hvm; + + if (flags & EPOLLIN) { + u64 cnt; + + eventfd_ctx_do_read(irqfd->eventfd, &cnt); + /* hvm's irq injection is not blocked, don't need workq */ + irqfd_set_irq(hvm, irqfd->gsi, 1); + } + + if (flags & EPOLLHUP) { + /* The eventfd is closing, detach from HVM */ + unsigned long iflags; + + spin_lock_irqsave(&hvm->irqfds.lock, iflags); + + /* + * Do more check if someone deactivated the irqfd before + * we could acquire the irqfds.lock. + */ + if (irqfd_is_active(irqfd)) + irqfd_deactivate(irqfd); + + spin_unlock_irqrestore(&hvm->irqfds.lock, iflags); + } + + return 0; +} + +static void irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, + poll_table *pt) +{ + struct hvm_kernel_irqfd *irqfd = + container_of(pt, struct hvm_kernel_irqfd, pt); + add_wait_queue_priority(wqh, &irqfd->wait); +} + +static int hvm_irqfd_assign(struct hvm *hvm, struct hvm_irqfd *args) +{ + struct hvm_kernel_irqfd *irqfd, *tmp; + struct fd f; + struct eventfd_ctx *eventfd = NULL; + int ret; + int idx; + + irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL_ACCOUNT); + if (!irqfd) + return -ENOMEM; + + irqfd->hvm = hvm; + irqfd->gsi = args->gsi; + + INIT_LIST_HEAD(&irqfd->list); + INIT_WORK(&irqfd->shutdown, irqfd_shutdown); + + f = fdget(args->fd); + if (!fd_file(f)) { + ret = -EBADF; + goto out; + } + + eventfd = eventfd_ctx_fileget(fd_file(f)); + if (IS_ERR(eventfd)) { + ret = PTR_ERR(eventfd); + goto fail; + } + + irqfd->eventfd = eventfd; + + /* + * Install our own custom wake-up handling so we are notified via + * a callback whenever someone signals the underlying eventfd + */ + init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); + init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); + + spin_lock_irq(&hvm->irqfds.lock); + + ret = 0; + list_for_each_entry(tmp, &hvm->irqfds.items, list) { + if (irqfd->eventfd != tmp->eventfd) + continue; + /* This fd is used for another irq already. */ + ret = -EBUSY; + spin_unlock_irq(&hvm->irqfds.lock); + goto fail; + } + + idx = srcu_read_lock(&hvm->irq_srcu); + + list_add_tail(&irqfd->list, &hvm->irqfds.items); + + spin_unlock_irq(&hvm->irqfds.lock); + + vfs_poll(fd_file(f), &irqfd->pt); + + srcu_read_unlock(&hvm->irq_srcu, idx); + + /* + * do not drop the file until the irqfd is fully initialized, otherwise + * we might race against the EPOLLHUP + */ + fdput(f); + return 0; + +fail: + if (eventfd && !IS_ERR(eventfd)) + eventfd_ctx_put(eventfd); + + fdput(f); + +out: + kfree(irqfd); + return ret; +} + +/** + * hvm_irqfd_deassign() - Shutdown any irqfd's that match fd+gsi. + * @hvm: Pointer to hvm. + * @args: Pointer to hvm_irqfd. + * + * Return: + * * 0 - Success. + * * Negative value - Failure. + */ +static int hvm_irqfd_deassign(struct hvm *hvm, struct hvm_irqfd *args) +{ + struct hvm_kernel_irqfd *irqfd, *tmp; + struct eventfd_ctx *eventfd; + + eventfd = eventfd_ctx_fdget(args->fd); + if (IS_ERR(eventfd)) + return PTR_ERR(eventfd); + + spin_lock_irq(&hvm->irqfds.lock); + + list_for_each_entry_safe(irqfd, tmp, &hvm->irqfds.items, list) { + if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) + irqfd_deactivate(irqfd); + } + + spin_unlock_irq(&hvm->irqfds.lock); + eventfd_ctx_put(eventfd); + + /* + * Block until we know all outstanding shutdown jobs have completed + * so that we guarantee there will not be any more interrupts on this + * gsi once this deassign function returns. + */ + flush_workqueue(irqfd_cleanup_wq); + + return 0; +} + +int hvm_irqfd(struct hvm *hvm, struct hvm_irqfd *args) +{ + for (int i = 0; i < ARRAY_SIZE(args->pad); i++) { + if (args->pad[i]) + return -EINVAL; + } + + if (args->flags & + ~(HVM_IRQFD_FLAG_DEASSIGN | HVM_IRQFD_FLAG_RESAMPLE)) + return -EINVAL; + + if (args->flags & HVM_IRQFD_FLAG_DEASSIGN) + return hvm_irqfd_deassign(hvm, args); + + return hvm_irqfd_assign(hvm, args); +} + +/** + * hvm_vm_irqfd_init() - Initialize irqfd data structure per VM + * + * @hvm: Pointer to struct hvm. + * + * Return: + * * 0 - Success. + * * Negative - Failure. + */ +int hvm_vm_irqfd_init(struct hvm *hvm) +{ + spin_lock_init(&hvm->irqfds.lock); + INIT_LIST_HEAD(&hvm->irqfds.items); + if (init_srcu_struct(&hvm->irq_srcu)) + return -EINVAL; + + return 0; +} + +/** + * hvm_vm_irqfd_release() - This function is called as the hvm VM fd is being + * released. Shutdown all irqfds that still remain open. + * @hvm: Pointer to hvm. + */ +void hvm_vm_irqfd_release(struct hvm *hvm) +{ + struct hvm_kernel_irqfd *irqfd, *tmp; + + spin_lock_irq(&hvm->irqfds.lock); + + list_for_each_entry_safe(irqfd, tmp, &hvm->irqfds.items, list) + irqfd_deactivate(irqfd); + + spin_unlock_irq(&hvm->irqfds.lock); + + /* + * Block until we know all outstanding shutdown jobs have completed. + */ + flush_workqueue(irqfd_cleanup_wq); +} + +/** + * hvm_drv_irqfd_init() - Erase flushing work items when a VM exits. + * + * Return: + * * 0 - Success. + * * Negative - Failure. + * + * Create a host-wide workqueue for issuing deferred shutdown requests + * aggregated from all vm* instances. We need our own isolated + * queue to ease flushing work items when a VM exits. + */ +int hvm_drv_irqfd_init(void) +{ + irqfd_cleanup_wq = alloc_workqueue("hvm-irqfd-cleanup", 0, 0); + if (!irqfd_cleanup_wq) + return -ENOMEM; + + return 0; +} + +void hvm_drv_irqfd_exit(void) +{ + destroy_workqueue(irqfd_cleanup_wq); +}
diff --git a/drivers/virt/halla/hvm_main.c b/drivers/virt/halla/hvm_main.c new file mode 100644 index 0000000..b2d3cdb --- /dev/null +++ b/drivers/virt/halla/hvm_main.c
@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/device.h> +#include <linux/file.h> +#include <linux/kdev_t.h> +#include <linux/miscdevice.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_reserved_mem.h> +#include <linux/platform_device.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +static long hvm_dev_ioctl(struct file *filp, unsigned int cmd, + unsigned long args) +{ + void __user *argp = (void __user *)args; + + switch (cmd) { + case HVM_CREATE_VM: + return hvm_dev_ioctl_create_vm(args); + case HVM_CHECK_EXTENSION: { + u64 cap; + + if (copy_from_user(&cap, argp, sizeof(__u64))) + return -EFAULT; + + return hvm_check_extension(NULL, cap, argp); + } + default: + break; + } + + return -ENOTTY; +} + +static const struct file_operations hvm_dev_ops = { + .unlocked_ioctl = hvm_dev_ioctl, +}; + +static struct miscdevice hvm_dev = { + .name = HALLA_KERNEL_DEV_NAME, + .fops = &hvm_dev_ops, +}; + +static struct reserved_mem *hvm_get_vm_pgt_rmem(struct device *dev) +{ + struct device_node *np; + struct reserved_mem *rmem; + + np = of_parse_phandle(dev->of_node, "memory-region", 0); + if (!np) { + dev_err(dev, "Failed to acquire memory region phandle\n"); + return NULL; + } + + rmem = of_reserved_mem_lookup(np); + if (!rmem) { + dev_err(dev, "Failed to acquire memory region\n"); + return NULL; + } + + dev_info(dev, "Reserved memory for VM pgtable: addr=%#llx, size=%#llx\n", + rmem->base, rmem->size); + + return rmem; +} + +static int drv_init(struct device *dev) +{ + struct reserved_mem *rmem; + unsigned long ret; + + rmem = hvm_get_vm_pgt_rmem(dev); + if (!rmem) + return -ENOENT; + + ret = exynos_hvc(HVC_FID_HVM_PROBE, rmem->base, rmem->size, + hvm_get_guest_vtimer_irq(), 0); + if (ret) { + dev_err(dev, "HVC for HVM probe failure (%#lx)\n", ret); + return -ENXIO; + } + + return 0; +} + +static int hvm_drv_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + int ret; + + ret = misc_register(&hvm_dev); + if (ret) + return ret; + + ret = hvm_drv_irqfd_init(); + if (ret) + goto err_irqfd_init; + + ret = hvm_vtimer_init(dev); + if (ret) + goto err_vtimer_init; + + ret = hvm_drv_debug_init(); + if (ret) + goto err_debug_init; + + ret = drv_init(dev); + if (ret) + goto err_drv_init; + + return 0; + +err_drv_init: + hvm_drv_debug_exit(); +err_debug_init: + hvm_vtimer_exit(); +err_vtimer_init: + hvm_drv_irqfd_exit(); +err_irqfd_init: + misc_deregister(&hvm_dev); + + return ret; +} + +static void hvm_drv_remove(struct platform_device *pdev) +{ + hvm_drv_irqfd_exit(); + hvm_destroy_all_vms(); + misc_deregister(&hvm_dev); +} + +static const struct of_device_id hvm_of_match[] = { + { .compatible = "samsung,exynos-hvm" }, + { }, +}; + +static struct platform_driver hvm_driver = { + .probe = hvm_drv_probe, + .remove = hvm_drv_remove, + .driver = { + .name = "exynos-hvm", + .owner = THIS_MODULE, + .of_match_table = hvm_of_match, + }, +}; + +module_platform_driver(hvm_driver); + +MODULE_DEVICE_TABLE(of, hvm_of_match); +MODULE_DESCRIPTION("Halla VM management"); +MODULE_AUTHOR("Samsung LSI"); +MODULE_LICENSE("GPL");
diff --git a/drivers/virt/halla/hvm_mmu.c b/drivers/virt/halla/hvm_mmu.c new file mode 100644 index 0000000..017a3a7 --- /dev/null +++ b/drivers/virt/halla/hvm_mmu.c
@@ -0,0 +1,278 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/gfp.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +static int hvm_gfn_to_hva_memslot(struct hvm_memslot *memslot, + u64 gfn, + u64 *hva_memslot) +{ + u64 offset; + + if (gfn < memslot->base_gfn) + return -EINVAL; + + offset = gfn - memslot->base_gfn; + *hva_memslot = memslot->userspace_addr + offset * PAGE_SIZE; + return 0; +} + +static int cmp_ppages(struct rb_node *node, const struct rb_node *parent) +{ + struct hvm_pinned_page *a = container_of(node, + struct hvm_pinned_page, + node); + struct hvm_pinned_page *b = container_of(parent, + struct hvm_pinned_page, + node); + + if (a->ipa < b->ipa) + return -1; + if (a->ipa > b->ipa) + return 1; + return 0; +} + +/* Invoker of this function is responsible for locking */ +static int hvm_insert_ppage(struct hvm *vm, struct hvm_pinned_page *ppage) +{ + if (rb_find_add(&ppage->node, &vm->pinned_pages, cmp_ppages)) + return -EEXIST; + return 0; +} + +static int pin_one_page(struct hvm *vm, unsigned long hva, u64 gpa, + struct page **out_page) +{ + u32 flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE; + struct hvm_pinned_page *ppage = NULL; + struct mm_struct *mm = current->mm; + struct page *page = NULL; + int ret; + + ppage = kmalloc(sizeof(*ppage), GFP_KERNEL_ACCOUNT); + if (!ppage) + return -ENOMEM; + + mmap_read_lock(mm); + ret = pin_user_pages(hva, 1, flags, &page); + mmap_read_unlock(mm); + + if (ret != 1 || !page) { + kfree(ppage); + return -EFAULT; + } + + ppage->page = page; + ppage->ipa = gpa; + + mutex_lock(&vm->mem_lock); + ret = hvm_insert_ppage(vm, ppage); + + /** + * The return of -EEXIST from hvm_insert_ppage is considered an + * expected behavior in this context. + * This situation arises when two or more VCPUs are concurrently + * engaged in demand paging handling. The initial VCPU has already + * allocated and pinned a page, while the subsequent VCPU attempts + * to pin the same page again. As a result, we prompt the unpinning + * and release of the allocated structure, followed by a return -EAGAIN. + */ + if (ret == -EEXIST) { + kfree(ppage); + unpin_user_pages(&page, 1); + mutex_unlock(&vm->mem_lock); + return -EAGAIN; + } + mutex_unlock(&vm->mem_lock); + *out_page = page; + + return ret; +} + +void *hvm_alloc_pages(size_t size) +{ + return alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); +} + +void hvm_free_pages(void *virt, size_t size) +{ + free_pages_exact(virt, size); +} + +static int hvm_register_vm_pgtable(struct hvm *hvm) +{ + unsigned long *buf; + unsigned long buf_pa; + size_t size = PAGE_SIZE * 2; + unsigned long ret; + + buf = hvm_alloc_pages(size); + if (buf == NULL) + return -ENOMEM; + + buf_pa = virt_to_phys(buf); + + ret = exynos_hvc(HVC_FID_HVM_REGISTER_VM_PGTABLE, + hvm->vm_id, buf_pa, size, 0); + if (ret) { + hvm_free_pages(buf, size); + return -EINVAL; + } + + hvm->vm_pgtable[hvm->pgtable_num++] = buf; + + return 0; +} + +int hvm_init_vm_stage2_mmu(struct hvm *hvm) +{ + unsigned long *buf; + unsigned long buf_pa; + unsigned long ret; + u16 vm_id = hvm->vm_id; + + buf = hvm_alloc_pages(PAGE_SIZE); + if (buf == NULL) + return -ENOMEM; + + buf_pa = virt_to_phys(buf); + + ret = exynos_hvc(HVC_FID_HVM_INIT_STAGE2_MMU, + vm_id, buf_pa, PAGE_SIZE, 0); + if (ret) { + hvm_free_pages(buf, PAGE_SIZE); + return -EINVAL; + } + + hvm->s2table_buf = buf; + hvm->pgtable_num = 0; + + return hvm_register_vm_pgtable(hvm); +} + +static int hvm_vm_allocate_guest_page(struct hvm *vm, struct hvm_memslot *slot, + u64 gfn, u64 *pfn) +{ + struct page *page = NULL; + unsigned long hva; + int ret; + + if (hvm_gfn_to_hva_memslot(slot, gfn, (u64 *)&hva) != 0) + return -EINVAL; + + ret = pin_one_page(vm, hva, PFN_PHYS(gfn), &page); + if (ret != 0) + return ret; + + if (page == NULL) + return -EFAULT; + /** + * As `pin_user_pages` already gets the page struct, we don't need to + * call other APIs to reduce function call overhead. + */ + *pfn = page_to_pfn(page); + + return 0; +} + +static int hvm_alloc_vm_pgtable(struct hvm *hvm) +{ + if (!exynos_hvc(HVC_FID_HVM_NEED_VM_PGTABLE, + hvm->vm_id, 0, 0, 0)) + return 0; + + return hvm_register_vm_pgtable(hvm); +} + +static int hvm_map_guest(struct hvm *hvm, int memslot_id, + u64 pfn, u64 gfn, u64 nr_pages) +{ + unsigned long ret; + u64 ids; + + ret = hvm_alloc_vm_pgtable(hvm); + if (ret) + return (int)ret; + + ids = (u32)memslot_id; + ids = (ids << 32) | hvm->vm_id; + + ret = exynos_hvc(HVC_FID_HVM_MAP_GUEST, ids, pfn, gfn, nr_pages); + + return (int)ret; +} + +static int handle_demand_page(struct hvm *vm, int memslot_id, u64 gfn) +{ + int ret; + u64 pfn; + + ret = hvm_vm_allocate_guest_page(vm, &vm->memslot[memslot_id], gfn, &pfn); + if (unlikely(ret)) { + if (ret == -EAGAIN) + return 0; + else + return -EFAULT; + } + + ret = hvm_map_guest(vm, memslot_id, pfn, gfn, 1); + if (unlikely(ret)) + return -EFAULT; + + return ret; +} + +/** + * hvm_find_memslot() - Find memslot containing this @gpa + * @vm: Pointer to struct hvm + * @gfn: Guest frame number + * + * Return: + * * >=0 - Index of memslot + * * -EFAULT - Not found + */ +static int hvm_find_memslot(struct hvm *vm, u64 gfn) +{ + int i; + + for (i = 0; i < HVM_MAX_MEM_REGION; i++) { + if (vm->memslot[i].npages == 0) + continue; + + if (gfn >= vm->memslot[i].base_gfn && + gfn < vm->memslot[i].base_gfn + vm->memslot[i].npages) + return i; + } + + return -EFAULT; +} + +/** + * hvm_handle_page_fault() - Handle guest page fault, find corresponding page + * for the faulting gpa + * @vcpu: Pointer to struct hvm_vcpu_run of the faulting vcpu + * + * Return: + * * 0 - Success to handle guest page fault + * * -EFAULT - Failed to map phys addr to guest's GPA + */ +int hvm_handle_page_fault(struct hvm_vcpu *vcpu) +{ + struct hvm *vm = vcpu->hvm; + int memslot_id; + u64 gfn; + + gfn = PHYS_PFN(vcpu->run->exception.fault_gpa); + memslot_id = hvm_find_memslot(vm, gfn); + if (unlikely(memslot_id < 0)) + return -EFAULT; + + return handle_demand_page(vm, memslot_id, gfn); +}
diff --git a/drivers/virt/halla/hvm_vcpu.c b/drivers/virt/halla/hvm_vcpu.c new file mode 100644 index 0000000..aee7a52 --- /dev/null +++ b/drivers/virt/halla/hvm_vcpu.c
@@ -0,0 +1,436 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/anon_inodes.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/mm.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/preempt.h> +#include <linux/percpu.h> +#include <linux/rcuwait.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +/* maximum size needed for holding an integer */ +#define ITOA_MAX_LEN 12 + +static long hvm_vcpu_update_one_reg(struct hvm_vcpu *vcpu, + void __user *argp, + bool is_write) +{ + struct hvm_one_reg reg; + void __user *reg_addr; + u64 data = 0; + u64 reg_size; + long ret; + + if (copy_from_user(®, argp, sizeof(reg))) + return -EFAULT; + + reg_addr = u64_to_user_ptr(reg.addr); + reg_size = (reg.id & HVM_REG_SIZE_MASK) >> HVM_REG_SIZE_SHIFT; + reg_size = BIT(reg_size); + + if (reg_size != 1 && reg_size != 2 && reg_size != 4 && reg_size != 8) + return -EINVAL; + + if (is_write) { + /* Halla hypervisor would filter out invalid vcpu register access */ + if (copy_from_user(&data, reg_addr, reg_size)) + return -EFAULT; + + ret = exynos_hvc(HVC_FID_HVM_SET_ONE_REG, + vcpu->hvm->vm_id, vcpu->vcpuid, + reg.id, data); + } else { + /* Read is not supported yet */ + return -EOPNOTSUPP; + } + + if (ret) + return ret; + + return 0; +} + +/** + * hvm_vcpu_handle_mmio() - Handle mmio in kernel space. + * @vcpu: Pointer to vcpu. + * + * Return: + * * true - This mmio exit has been processed. + * * false - This mmio exit has not been processed, require userspace. + */ +static bool hvm_vcpu_handle_mmio(struct hvm_vcpu *vcpu) +{ + u64 addr; + u32 len; + const void *val_ptr; + + /* So far, we don't have in-kernel mmio read handler */ + if (!vcpu->run->mmio.is_write) + return false; + addr = vcpu->run->mmio.phys_addr; + len = vcpu->run->mmio.size; + val_ptr = &vcpu->run->mmio.data; + + return hvm_ioevent_write(vcpu, addr, len, val_ptr); +} + +/** + * hvm_handle_guest_exception() - Handle guest exception + * @vcpu: Pointer to struct hvm_vcpu_run in userspace + * Return: + * * true - This exception has been processed, no need to back to VMM. + * * false - This exception has not been processed, require userspace. + */ +static bool hvm_handle_guest_exception(struct hvm_vcpu *vcpu) +{ + int ret; + + for (int i = 0; i < ARRAY_SIZE(vcpu->run->exception.reserved); i++) { + if (vcpu->run->exception.reserved[i]) + return false; + } + + switch (vcpu->run->exception.exception) { + case HVM_EXCEPTION_PAGE_FAULT: + ret = hvm_handle_page_fault(vcpu); + break; + case HVM_EXCEPTION_UNKNOWN: + fallthrough; + default: + ret = -EFAULT; + } + + if (!ret) + return true; + else + return false; +} + +static void hvm_vcpu_load(struct hvm_vcpu *vcpu) +{ + u32 tuple = ((u32)vcpu->hvm->vm_id << 16 | (u16)vcpu->vcpuid); + + __this_cpu_write(*hvm_get_running_vcpus(), tuple); + + hvm_restore_vtimer(vcpu); +} + +static void hvm_vcpu_put(struct hvm_vcpu *vcpu) +{ + hvm_save_vtimer(vcpu); + + __this_cpu_write(*hvm_get_running_vcpus(), (u32)(~0)); +} + +void hvm_vcpu_wakeup(struct hvm_vcpu *vcpu) +{ + atomic_inc(&vcpu->idle_state); + rcuwait_wake_up(&vcpu->wait); +} + +static enum hrtimer_restart hvm_idle_hrtimer_expire(struct hrtimer *hrt) +{ + struct hvm_vcpu *vcpu; + + vcpu = container_of(hrt, struct hvm_vcpu, idle_hrtimer); + + hvm_vcpu_wakeup(vcpu); + + return HRTIMER_NORESTART; +} + +static void hvm_idle_hrtimer_init(struct hvm_vcpu *vcpu) +{ + hrtimer_init(&vcpu->idle_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD); + vcpu->idle_hrtimer.function = hvm_idle_hrtimer_expire; + atomic_set(&vcpu->idle_state, 0); +} + +static void hvm_idle_hrtimer_set(struct hvm_vcpu *vcpu, u64 ns) +{ + hrtimer_start(&vcpu->idle_hrtimer, ktime_add_ns(ktime_get(), ns), HRTIMER_MODE_ABS_HARD); +} + +static void hvm_idle_hrtimer_release(struct hvm_vcpu *vcpu) +{ + hrtimer_cancel(&vcpu->idle_hrtimer); +} + +static void vcpu_block_wait(struct hvm_vcpu *vcpu) +{ + struct rcuwait *wait = &vcpu->wait; + + rcuwait_wait_event(wait, + atomic_read(&vcpu->idle_state), + TASK_INTERRUPTIBLE); + atomic_set(&vcpu->idle_state, 0); +} + +static int hvm_handle_guest_idle(struct hvm_vcpu *vcpu) +{ + int ret = 0; + u64 ns; + u64 next_cval; + u64 cur_val; + + ns = exynos_hvc(HVC_FID_HYPERVISOR_GET_VM_IDLE_TIME, + ((vcpu->hvm->vm_id << 16) | vcpu->vcpuid), + 0, 0, 0); + next_cval = vcpu->vtimer.cntp_cval_el0; + cur_val = read_sysreg(cntpct_el0); + + if (next_cval > cur_val) { + ns = clocksource_cyc2ns(next_cval - cur_val, + vcpu->hvm->clock_scale_factor.mult, + vcpu->hvm->clock_scale_factor.shift); + } + + hvm_idle_hrtimer_set(vcpu, ns); + vcpu_block_wait(vcpu); + hvm_idle_hrtimer_release(vcpu); + + return ret; +} + +static void hvm_handle_guest_gsi(struct hvm_vcpu *vcpu) +{ + struct hvm *hvm = vcpu->hvm; + + for (int i = 0; i < hvm->nr_vcpus; i++) { + if (hvm->vcpus[i]) + hvm_vcpu_wakeup(hvm->vcpus[i]); + } +} + +static bool handle_guest_exit(struct hvm_vcpu *vcpu, u64 exit_reason) +{ + switch (exit_reason) { + case HVM_EXIT_MMIO: + if (!hvm_vcpu_handle_mmio(vcpu)) + return true; + break; + /** + * it's Halla's responsibility to fill corresponding data + * structure + */ + case HVM_EXIT_EXCEPTION: + if (!hvm_handle_guest_exception(vcpu)) + return true; + break; + case HVM_EXIT_HALLA: + fallthrough; + case HVM_EXIT_IRQ: + break; + case HVM_EXIT_IDLE: + hvm_handle_guest_idle(vcpu); + break; + case HVM_EXIT_SGI: + hvm_handle_guest_gsi(vcpu); + break; + default: + return true; + } + + return false; +} + +/** + * hvm_vcpu_run() - Handle vcpu run ioctl, entry point to guest and exit + * point from guest + * @vcpu: Pointer to struct hvm_vcpu + * @argp: Pointer to struct hvm_vcpu_run in userspace + * + * Return: + * * 0 - Success. + * * Negative - Failure. + */ +static long hvm_vcpu_run(struct hvm_vcpu *vcpu, void __user *argp) +{ + bool need_userspace = false; + u64 exit_reason; + + if (copy_from_user(vcpu->run, argp, sizeof(struct hvm_vcpu_run))) + return -EFAULT; + + for (int i = 0; i < ARRAY_SIZE(vcpu->run->pad); i++) { + if (vcpu->run->pad[i]) + return -EINVAL; + } + + if (vcpu->run->immediate_exit == 1) + return -EINTR; + + while (!need_userspace && !signal_pending(current)) { + preempt_disable(); + hvm_vcpu_load(vcpu); + + exit_reason = exynos_hvc(HVC_FID_HVM_VCPU_RUN, + vcpu->hvm->vm_id, vcpu->vcpuid, 0, 0); + + /** + * Need to give guarantee for processing VM timer + * interrupt handler. + */ + isb(); + + hvm_vcpu_put(vcpu); + preempt_enable(); + + need_userspace = handle_guest_exit(vcpu, exit_reason); + } + + if (copy_to_user(argp, vcpu->run, sizeof(struct hvm_vcpu_run))) + return -EFAULT; + + if (signal_pending(current)) { + exynos_hvc(HVC_FID_HVM_NOTIFY_EXIT, + vcpu->hvm->vm_id, 0, 0, 0); + return -ERESTARTSYS; + } + + return 0; +} + +static long hvm_vcpu_ioctl(struct file *filp, unsigned int ioctl, + unsigned long arg) +{ + int ret = -EOPNOTSUPP; + void __user *argp = (void __user *)arg; + struct hvm_vcpu *vcpu = filp->private_data; + + switch (ioctl) { + case HVM_VCPU_RUN: + ret = hvm_vcpu_run(vcpu, argp); + break; + case HVM_SET_ONE_REG: + /* is_write */ + ret = hvm_vcpu_update_one_reg(vcpu, argp, true); + break; + default: + break; + } + + return ret; +} + +static const struct file_operations hvm_vcpu_fops = { + .unlocked_ioctl = hvm_vcpu_ioctl, +}; + +/* caller must hold the vm lock */ +static void hvm_destroy_vcpu(struct hvm_vcpu *vcpu) +{ + if (!vcpu) + return; + + /* Destroy vcpu in Halla */ + exynos_hvc(HVC_FID_HVM_DESTROY_VCPU, + ((vcpu->hvm->vm_id << 16) | vcpu->vcpuid), + 0, 0, 0); + + /* + * Clear the shared memory region. This is done to prevent any sensitive + * data from the guest or hypervisor from lingering in the memory pages + * after the vCPU is destroyed, improving security and preventing + * potential information leaks. + */ + memset(vcpu->run, 0, HVM_VCPU_RUN_MAP_SIZE); + free_pages_exact(vcpu->run, HVM_VCPU_RUN_MAP_SIZE); + kfree(vcpu); +} + +/** + * hvm_destroy_vcpus() - Destroy all vcpus, caller has to hold the vm lock + * + * @hvm: vm struct that owns the vcpus + */ +void hvm_destroy_vcpus(struct hvm *hvm) +{ + int i; + + for (i = 0; i < hvm->nr_vcpus; i++) { + hvm_destroy_vcpu(hvm->vcpus[i]); + hvm->vcpus[i] = NULL; + } + hvm->nr_vcpus = 0; +} + +/* create_vcpu_fd() - Allocates an inode for the vcpu. */ +static int create_vcpu_fd(struct hvm_vcpu *vcpu) +{ + /* sizeof("hvm-vcpu:") + max(strlen(itoa(vcpuid))) + null */ + char name[10 + ITOA_MAX_LEN + 1]; + + snprintf(name, sizeof(name), "hvm-vcpu:%d", vcpu->vcpuid); + return anon_inode_getfd(name, &hvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC); +} + +/** + * hvm_vm_ioctl_create_vcpu() - for HVM_CREATE_VCPU + * @hvm: Pointer to struct hvm + * @cpuid: equals arg + * + * Return: Fd of vcpu, negative errno if error occurs + */ +int hvm_vm_ioctl_create_vcpu(struct hvm *hvm, u32 cpuid) +{ + struct hvm_vcpu *vcpu; + int ret; + + if (cpuid > HVM_MAX_VCPUS) + return -EINVAL; + + vcpu = kzalloc(sizeof(*vcpu), GFP_KERNEL); + if (!vcpu) + return -ENOMEM; + + /** + * Allocate 2 pages for data sharing between driver and halla hypervisor + * + * |- page 0 -|- page 1 -| + * |hvm_vcpu_run|......|not_used_yet|.......| + * + */ + vcpu->run = alloc_pages_exact(HVM_VCPU_RUN_MAP_SIZE, + GFP_KERNEL_ACCOUNT | __GFP_ZERO); + if (!vcpu->run) { + ret = -ENOMEM; + goto free_vcpu; + } + vcpu->vcpuid = cpuid; + vcpu->hvm = hvm; + mutex_init(&vcpu->lock); + + ret = (int)exynos_hvc(HVC_FID_HVM_CREATE_VCPU, + hvm->vm_id, vcpu->vcpuid, + (u64)virt_to_phys(vcpu->run), 0); + if (ret < 0) + goto free_vcpu_run; + + ret = create_vcpu_fd(vcpu); + if (ret < 0) + goto free_vcpu_run; + hvm->vcpus[cpuid] = vcpu; + hvm->nr_vcpus += 1; + + hvm_vtimer_init_regs(vcpu); + + hvm_idle_hrtimer_init(vcpu); + + return ret; + +free_vcpu_run: + free_pages_exact(vcpu->run, HVM_VCPU_RUN_MAP_SIZE); +free_vcpu: + kfree(vcpu); + return ret; +}
diff --git a/drivers/virt/halla/hvm_vm.c b/drivers/virt/halla/hvm_vm.c new file mode 100644 index 0000000..0c87265 --- /dev/null +++ b/drivers/virt/halla/hvm_vm.c
@@ -0,0 +1,631 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/anon_inodes.h> +#include <linux/file.h> +#include <linux/kdev_t.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/debugfs.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +static DEFINE_MUTEX(hvm_list_lock); +static LIST_HEAD(hvm_list); + +/** + * register_memslot_addr_range() - Register memory region to Halla + * @hvm: Pointer to struct hvm + * @memslot: Pointer to struct hvm_memslot + * + * Return: 0 for success, negative number for error + */ +static int +register_memslot_addr_range(struct hvm *hvm, struct hvm_memslot *memslot) +{ + struct hvm_memory_region_ranges *region; + u32 buf_size = PAGE_SIZE * 2; + u64 gfn; + unsigned long ret; + + region = alloc_pages_exact(buf_size, GFP_KERNEL); + if (!region) + return -ENOMEM; + + region->slot = memslot->slot_id; + region->total_pages = memslot->npages; + gfn = memslot->base_gfn; + region->gpa = PFN_PHYS(gfn); + + ret = exynos_hvc(HVC_FID_HVM_SET_MEMREGION, + hvm->vm_id, buf_size, + virt_to_phys(region), 0); + if (ret) { + free_pages_exact(region, buf_size); + return -EFAULT; + } + + free_pages_exact(region, buf_size); + + return 0; +} + +/** + * memregion_validity_check() - Preliminary check for memory region + * @hvm: Pointer to struct hvm. + * @mem: Input memory region from user. + * + * Return: true for check passed, false for invalid input. + */ +static bool +memregion_validity_check(struct hvm *hvm, + struct hvm_userspace_memory_region *mem) +{ + if (mem->slot >= HVM_MAX_MEM_REGION) + return false; + + if (!PAGE_ALIGNED(mem->guest_phys_addr) || + !PAGE_ALIGNED(mem->memory_size)) + return false; + + if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) + return false; + + if ((mem->memory_size >> PAGE_SHIFT) > HVM_MEM_MAX_NR_PAGES) + return false; + + return true; +} + +/** + * hvm_vm_ioctl_set_memory_region() - Set memory region of guest + * @hvm: Pointer to struct hvm. + * @mem: Input memory region from user. + * + * Return: 0 for success, negative number for error + * + * -EXIO - The memslot is out-of-range + * -EFAULT - Cannot find corresponding vma + * -EINVAL - Region size and VMA size mismatch + */ +static int +hvm_vm_ioctl_set_memory_region(struct hvm *hvm, + struct hvm_userspace_memory_region *mem) +{ + int ret; + struct vm_area_struct *vma; + struct hvm_memslot *memslot; + unsigned long size; + + if (memregion_validity_check(hvm, mem) != true) + return -EINVAL; + + memslot = &hvm->memslot[mem->slot]; + + vma = vma_lookup(hvm->mm, mem->userspace_addr); + if (!vma) + return -EFAULT; + + size = vma->vm_end - vma->vm_start; + if (size != mem->memory_size) + return -EINVAL; + + memslot->base_gfn = __phys_to_pfn(mem->guest_phys_addr); + memslot->npages = size >> PAGE_SHIFT; + memslot->userspace_addr = mem->userspace_addr; + memslot->vma = vma; + memslot->flags = mem->flags; + memslot->slot_id = mem->slot; + + ret = (int)exynos_hvc(HVC_FID_HVM_MEMREGION_PURPOSE, + hvm->vm_id, mem->guest_phys_addr, + mem->memory_size, mem->flags); + if (ret) + return -EFAULT; + + return register_memslot_addr_range(hvm, memslot); +} + +/** + * hvm_inject_irq() - Inject virtual interrupt to a VM + * @hvm: Pointer to struct hvm + * @vcpu_idx: vcpu index, only valid if PPI + * @irq: *SPI* irq number (excluding offset value `32`) + * @level: 1 if true else 0 + * + * Return: + * * 0 - Success. + * * Negative - Failure. + */ +int hvm_inject_irq(struct hvm *hvm, unsigned int vcpu_idx, + u32 irq, bool level) +{ + unsigned long ret; + u64 ids; + struct hvm_vcpu *vcpu = hvm->vcpus[vcpu_idx]; + + ids = (u32)hvm->vm_id; + ids = (ids << 16) | vcpu_idx; + + ret = exynos_hvc(HVC_FID_HVM_IRQ_LINE, + ids, irq, level, 0); + + hvm_vcpu_wakeup(vcpu); + + return (int)ret; +} + +static int hvm_vm_ioctl_irq_line(struct hvm *hvm, + struct hvm_irq_level *irq_level) +{ + u32 irq = irq_level->irq; + u32 vcpu_idx, vcpu2_idx, irq_num; + bool level = irq_level->level; + + vcpu_idx = FIELD_GET(HVM_IRQ_LINE_VCPU, irq); + vcpu2_idx = FIELD_GET(HVM_IRQ_LINE_VCPU2, irq) * (HVM_IRQ_VCPU_MASK + 1); + irq_num = FIELD_GET(HVM_IRQ_LINE_NUM, irq); + + return hvm_inject_irq(hvm, vcpu_idx + vcpu2_idx, irq_num, + level); +} + +static int hvm_vm_ioctl_create_device(struct hvm *hvm, void __user *argp) +{ + struct hvm_create_device *hvm_dev; + void *dev_data = NULL; + int ret; + + hvm_dev = alloc_pages_exact(PAGE_SIZE, GFP_KERNEL); + if (!hvm_dev) + return -ENOMEM; + + if (copy_from_user(hvm_dev, argp, sizeof(struct hvm_create_device))) { + ret = -EFAULT; + goto err_free_dev; + } + + if (hvm_dev->attr_addr != 0 && hvm_dev->attr_size != 0) { + size_t attr_size = hvm_dev->attr_size; + void __user *attr_addr = u64_to_user_ptr(hvm_dev->attr_addr); + + /* Size of device specific data should not be over a page. */ + if (attr_size > PAGE_SIZE) { + ret = -EINVAL; + goto err_free_dev; + } + + dev_data = alloc_pages_exact(attr_size, GFP_KERNEL); + if (!dev_data) { + ret = -ENOMEM; + goto err_free_dev; + } + + if (copy_from_user(dev_data, attr_addr, attr_size)) { + ret = -EFAULT; + goto err_free_dev_data; + } + hvm_dev->attr_addr = virt_to_phys(dev_data); + } + + ret = (int)exynos_hvc(HVC_FID_HVM_CREATE_DEVICE, + hvm->vm_id, virt_to_phys(hvm_dev), 0, 0); +err_free_dev_data: + if (dev_data) + free_pages_exact(dev_data, 0); +err_free_dev: + free_pages_exact(hvm_dev, 0); + return ret; +} + +static int hvm_vm_enable_cap(struct hvm *hvm, + struct hvm_enable_cap *cap, + struct arm_smccc_res *res) +{ + arm_smccc_hvc(HVC_FID_HVM_ENABLE_CAP, + hvm->vm_id, cap->cap, cap->args[0], cap->args[1], 0, 0, 0, res); + + return res->a0; +} + +static int hvm_vm_ioctl_enable_cap(struct hvm *hvm, + struct hvm_enable_cap *cap, + void __user *argp) +{ + struct arm_smccc_res res = {0}; + int ret; + + /* Currently, only pvm-related capabilities exist */ + if (cap->cap != HVM_CAP_PROTECTED_VM) + return -EINVAL; + + switch (cap->args[0]) { + case HVM_CAP_PVM_SET_PVMFW_GPA: + fallthrough; + case HVM_CAP_PVM_SET_PROTECTED_VM: + ret = hvm_vm_enable_cap(hvm, cap, &res); + return ret; + case HVM_CAP_PVM_GET_PVMFW_SIZE: { + ret = hvm_vm_enable_cap(hvm, cap, &res); + if (ret) + return -EINVAL; + cap->args[1] = res.a1; + if (copy_to_user(argp, cap, sizeof(*cap))) + return -EFAULT; + + return 0; + } + default: + break; + } + + return -EINVAL; +} + +static int set_dtb_config(struct hvm *hvm, struct hvm_dtb_config *cfg) +{ + unsigned long ret; + + ret = exynos_hvc(HVC_FID_HVM_SET_DTB_CONFIG, + hvm->vm_id, cfg->dtb_addr, cfg->dtb_size, 0); + + return (int)ret; +} + +static unsigned long hvm_pre_destory_vm(struct hvm *hvm) +{ + return exynos_hvc(HVC_FID_HVM_PRE_DESTROY_VM, + hvm->vm_id, 0, 0, 0); +} + +static unsigned long +__hvm_reclaim_guest_memory(u16 vmid, u64 gfn, unsigned long nr_pages) +{ + phys_addr_t ipa = PFN_PHYS(gfn); + unsigned long left_pages = nr_pages; + unsigned long min_pages = HVM_RECLAIM_MIN_SIZE >> PAGE_SHIFT; + unsigned long order = min_pages; + unsigned long ret = 0; + + while (left_pages > 0) { + if (order > left_pages) + order = left_pages; + + ret = exynos_hvc(HVC_FID_HVM_RECLAIM_GUEST_MEMORY, + vmid, ipa, order, 0); + if (ret) + break; + + left_pages -= order; + ipa += (order << PAGE_SHIFT); + } + + return ret; +} + +static unsigned long hvm_memslot_reclaim_guest_memory(struct hvm *hvm) +{ + struct hvm_memslot *slot; + unsigned long ret = 0; + + for (unsigned int i = 0; i < HVM_MAX_MEM_REGION; i++) { + slot = &hvm->memslot[i]; + + if (slot->npages == 0) + continue; + + ret = __hvm_reclaim_guest_memory(hvm->vm_id, + slot->base_gfn, + slot->npages); + if (ret) + break; + } + return ret; +} + +static unsigned long hvm_reclaim_guest_memory(struct hvm *hvm) +{ + return hvm_memslot_reclaim_guest_memory(hvm); +} + +static void hvm_reclaim_guest_stage2_pgtable(struct hvm *hvm) +{ + unsigned long ret; + + ret = exynos_hvc(HVC_FID_HVM_RECLAIM_GUEST_S2_PGTABLE, + hvm->vm_id, 0, 0, 0); + if (ret) + return; + + hvm_free_pages(hvm->s2table_buf, PAGE_SIZE); + + for (unsigned int i = 0; i < hvm->pgtable_num; i++) + hvm_free_pages(hvm->vm_pgtable[i], PAGE_SIZE * 2); +} + +/* Invoker of this function is responsible for locking */ +static void hvm_destroy_all_ppage(struct hvm *hvm) +{ + struct hvm_pinned_page *ppage; + struct rb_node *node; + + node = rb_first(&hvm->pinned_pages); + while (node) { + ppage = rb_entry(node, struct hvm_pinned_page, node); + unpin_user_pages_dirty_lock(&ppage->page, 1, true); + node = rb_next(node); + rb_erase(&ppage->node, &hvm->pinned_pages); + kfree(ppage); + } +} + +static void hvm_destroy_vm(struct hvm *hvm) +{ + unsigned long ret = 0; + + mutex_lock(&hvm->lock); + + hvm_pre_destory_vm(hvm); + + hvm_vm_irqfd_release(hvm); + + ret = hvm_reclaim_guest_memory(hvm); + hvm_reclaim_guest_stage2_pgtable(hvm); + + hvm_destroy_vcpus(hvm); + + exynos_hvc(HVC_FID_HVM_DESTROY_VM, + hvm->vm_id, 0, 0, 0); + + hvm_destroy_vm_debugfs(hvm); + + mutex_lock(&hvm_list_lock); + list_del(&hvm->vm_list); + mutex_unlock(&hvm_list_lock); + + mutex_unlock(&hvm->lock); + + /** + * Have to leave the page pinned as we don't know whether its + * save to access + */ + if (!ret) { + /** + * No need to lock here becauese it's single-threaded + * execution + */ + hvm_destroy_all_ppage(hvm); + } + + kfree(hvm); +} + +void hvm_destroy_all_vms(void) +{ + struct hvm *hvm, *tmp; + + mutex_lock(&hvm_list_lock); + if (list_empty(&hvm_list)) + goto out; + + list_for_each_entry_safe(hvm, tmp, &hvm_list, vm_list) + hvm_destroy_vm(hvm); + +out: + mutex_unlock(&hvm_list_lock); +} + +int hvm_check_extension(struct hvm *hvm, u64 cap, void __user *argp) +{ + switch (cap) { + case HVM_CAP_PROTECTED_VM: { + u64 success = 1; + + if (copy_to_user(argp, &success, sizeof(__u64))) + return -EFAULT; + + return 0; + } + case HVM_CAP_VM_GPA_SIZE: { + u64 value = CONFIG_ARM64_PA_BITS; + + if (copy_to_user(argp, &value, sizeof(__u64))) + return -EFAULT; + + return 0; + } + default: + break; + } + + return -EOPNOTSUPP; +} + +/* hvm_vm_ioctl() - Ioctl handler of VM FD */ +static long hvm_vm_ioctl(struct file *filp, unsigned int ioctl, + unsigned long arg) +{ + long ret; + void __user *argp = (void __user *)arg; + struct hvm *hvm = filp->private_data; + + switch (ioctl) { + case HVM_CHECK_EXTENSION: { + u64 cap; + + if (copy_from_user(&cap, argp, sizeof(__u64))) + return -EFAULT; + + ret = hvm_check_extension(hvm, cap, argp); + break; + } + case HVM_CREATE_VCPU: { + ret = hvm_vm_ioctl_create_vcpu(hvm, arg); + break; + } + case HVM_SET_USER_MEMORY_REGION: { + struct hvm_userspace_memory_region userspace_mem; + + if (copy_from_user(&userspace_mem, argp, sizeof(userspace_mem))) + return -EFAULT; + + ret = hvm_vm_ioctl_set_memory_region(hvm, &userspace_mem); + break; + } + case HVM_IRQ_LINE: { + struct hvm_irq_level irq_level; + + if (copy_from_user(&irq_level, argp, sizeof(irq_level))) { + ret = -EFAULT; + goto out; + } + ret = hvm_vm_ioctl_irq_line(hvm, &irq_level); + break; + } + case HVM_CREATE_DEVICE: { + ret = hvm_vm_ioctl_create_device(hvm, argp); + break; + } + case HVM_IRQFD: { + struct hvm_irqfd data; + + if (copy_from_user(&data, argp, sizeof(data))) { + ret = -EFAULT; + goto out; + } + ret = hvm_irqfd(hvm, &data); + break; + } + case HVM_IOEVENTFD: { + struct hvm_ioeventfd data; + + if (copy_from_user(&data, argp, sizeof(data))) { + ret = -EFAULT; + goto out; + } + ret = hvm_ioeventfd(hvm, &data); + break; + } + case HVM_ENABLE_CAP: { + struct hvm_enable_cap cap; + + if (copy_from_user(&cap, argp, sizeof(cap))) { + ret = -EFAULT; + goto out; + } + ret = hvm_vm_ioctl_enable_cap(hvm, &cap, argp); + break; + } + case HVM_SET_DTB_CONFIG: { + struct hvm_dtb_config cfg; + + if (copy_from_user(&cfg, argp, sizeof(cfg))) { + ret = -EFAULT; + goto out; + } + ret = set_dtb_config(hvm, &cfg); + break; + } + default: + ret = -ENOTTY; + } +out: + return ret; +} + +static int hvm_vm_release(struct inode *inode, struct file *filp) +{ + struct hvm *hvm = filp->private_data; + + hvm_destroy_vm(hvm); + + return 0; +} + +static const struct file_operations hvm_vm_fops = { + .release = hvm_vm_release, + .unlocked_ioctl = hvm_vm_ioctl, +}; + +static int hvm_arch_drv_init(struct hvm *hvm) +{ + clocks_calc_mult_shift(&hvm->clock_scale_factor.mult, + &hvm->clock_scale_factor.shift, + arch_timer_get_cntfrq(), + NSEC_PER_SEC, + 0); + + return 0; +} + +/** + * hvm_dev_ioctl_create_vm - Create vm fd + * @vm_type: VM type. Only supports Linux VM now. + * + * Return: fd of vm, negative if error + */ +int hvm_dev_ioctl_create_vm(unsigned long vm_type) +{ + struct hvm *hvm; + int ret; + + hvm = kzalloc(sizeof(struct hvm), GFP_KERNEL); + if (!hvm) + return -ENOMEM; + + ret = exynos_hvc(HVC_FID_HVM_CREATE_VM, vm_type, 0, 0, 0); + if (ret == 0) { + kfree(hvm); + return -ENOMEM; + } + + hvm->vm_id = ret; + hvm->mm = current->mm; + mutex_init(&hvm->lock); + mutex_init(&hvm->mem_lock); + hvm->pinned_pages = RB_ROOT; + + ret = hvm_vm_irqfd_init(hvm); + if (ret) { + kfree(hvm); + return ret; + } + + ret = hvm_init_ioeventfd(hvm); + if (ret) { + kfree(hvm); + return ret; + } + + ret = hvm_init_vm_stage2_mmu(hvm); + if (ret) { + kfree(hvm); + return ret; + } + + ret = hvm_create_vm_debugfs(hvm); + if (ret) { + kfree(hvm); + return ret; + } + + ret = hvm_arch_drv_init(hvm); + if (ret) { + kfree(hvm); + return ret; + } + + mutex_lock(&hvm_list_lock); + list_add(&hvm->vm_list, &hvm_list); + mutex_unlock(&hvm_list_lock); + + return anon_inode_getfd("hvm-vm", &hvm_vm_fops, hvm, + O_RDWR | O_CLOEXEC); +}
diff --git a/drivers/virt/halla/hvm_vtimer.c b/drivers/virt/halla/hvm_vtimer.c new file mode 100644 index 0000000..1e50a8d --- /dev/null +++ b/drivers/virt/halla/hvm_vtimer.c
@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. + * http://www.samsung.com/ + */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/irq.h> +#include <linux/smp.h> +#include <linux/percpu.h> +#include <linux/cpuhotplug.h> +#include <clocksource/arm_arch_timer.h> +#include <asm/sysreg.h> +#include <linux/of.h> +#include <linux/of_irq.h> +#include <linux/platform_device.h> + +#include "exynos-hvc.h" +#include "hvm_drv.h" + +static u32 host_vtimer_irq; +static u32 guest_vtimer_irq; +static u32 host_vtimer_irq_flags; + +// hvm_running_vcpu(u32) : vmid(bit 31~16), vcpuid(bit 15~0) +static u32 __percpu *hvm_running_vcpu; + +void hvm_vtimer_init_regs(struct hvm_vcpu *vcpu) +{ + vcpu->vtimer.cntp_ctl_el0 = (u64)0; + vcpu->vtimer.cntp_cval_el0 = (u64)0; +} + +void hvm_restore_vtimer(struct hvm_vcpu *vcpu) +{ + unsigned long flags; + + local_irq_save(flags); + + write_sysreg(vcpu->vtimer.cntp_cval_el0, cntp_cval_el0); + write_sysreg(vcpu->vtimer.cntp_ctl_el0, cntp_ctl_el0); + isb(); + + local_irq_restore(flags); +} + +void hvm_save_vtimer(struct hvm_vcpu *vcpu) +{ + unsigned long flags; + + local_irq_save(flags); + + vcpu->vtimer.cntp_ctl_el0 = read_sysreg(cntp_ctl_el0); + vcpu->vtimer.cntp_cval_el0 = read_sysreg(cntp_cval_el0); + + write_sysreg(0, cntp_ctl_el0); + isb(); + + local_irq_restore(flags); +} + +u32 __percpu *hvm_get_running_vcpus(void) +{ + return (u32 __percpu *)hvm_running_vcpu; +} + +u32 hvm_get_guest_vtimer_irq(void) +{ + return guest_vtimer_irq; +} + +static irqreturn_t hvm_arch_timer_handler(int irq, void *dev_id) +{ + unsigned long ret; + u32 tuple = *(u32 *)dev_id; + u32 timer_ppi = guest_vtimer_irq - HVM_IRQ_PPI_START_NUM; + + if (tuple == (u32)(~0) || (read_sysreg(cntp_ctl_el0) & (u64)(2))) + return IRQ_NONE; + + // mask timer + write_sysreg(read_sysreg(cntp_ctl_el0) | (u64)(2), cntp_ctl_el0); + isb(); + + // call IRQ_LINE HVC + ret = exynos_hvc(HVC_FID_HVM_IRQ_LINE, tuple, + ((u32)(1) << 31) | timer_ppi, true, 0); + + return IRQ_HANDLED; +} + +static int hvm_vtimer_starting_cpu(u32 cpu) +{ + enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); + + return 0; +} + +static int hvm_vtimer_dying_cpu(u32 cpu) +{ + disable_percpu_irq(host_vtimer_irq); + + return 0; +} + +static void hvm_irq_fixup_flags(u32 virq, u32 *flags) +{ + *flags = irq_get_trigger_type(virq); + if (*flags != IRQF_TRIGGER_HIGH && *flags != IRQF_TRIGGER_LOW) + *flags = IRQF_TRIGGER_LOW; +} + +int hvm_vtimer_init(struct device *dev) +{ + int err; + // mapping INTID 27 to Linux irq (host_vtimer_irq) + struct device_node *vtimer_node = of_find_node_by_path("/timer"); + struct irq_data *irqd; + + // 2: INTID 30(PPI 14) index in timer dt + host_vtimer_irq = (u32)of_irq_get(vtimer_node, ARCH_TIMER_PHYS_NONSECURE_PPI); + hvm_irq_fixup_flags(host_vtimer_irq, &host_vtimer_irq_flags); + + irqd = irq_get_irq_data(host_vtimer_irq); + if (!irqd) { + dev_err(dev, "Fail to get vtimer irq data (%d)\n", + host_vtimer_irq); + return IRQ_NONE; + } + + guest_vtimer_irq = irqd_to_hwirq(irqd); + + hvm_running_vcpu = alloc_percpu(u32); + if (!hvm_running_vcpu) { + dev_err(dev, "memory allocation failed for running_vcpu\n"); + return -ENOMEM; + } + + // register EL1 virtual timer irq + err = request_percpu_irq(host_vtimer_irq, hvm_arch_timer_handler, + "hvm guest vtimer", hvm_get_running_vcpus()); + if (err) { + dev_err(dev, "can't request vtimer interrupt %d (%d)\n", + host_vtimer_irq, err); + return err; + } + + err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "exynos/arch_timer:starting", + hvm_vtimer_starting_cpu, hvm_vtimer_dying_cpu); + if (err < 0) + dev_err(dev, "can't setup cpuhp state (%d)\n", err); + + return 0; +} + +void hvm_vtimer_exit(void) +{ + free_percpu_irq(host_vtimer_irq, hvm_get_running_vcpus()); + + free_percpu(hvm_running_vcpu); +}
diff --git a/modules.bzl b/modules.bzl index febc4bd..277c77e 100644 --- a/modules.bzl +++ b/modules.bzl
@@ -102,6 +102,7 @@ "drivers/char/hw_random/cctrng.ko", "drivers/misc/open-dice.ko", "drivers/ptp/ptp_kvm.ko", + "drivers/virt/halla/exynos-hvm.ko", ] _X86_GKI_MODULES_LIST = [