From 7733607fb3aa72e3df47e6165156feb92ced8f5f Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:07:28 -0800 Subject: xen/pvh: Split CONFIG_XEN_PVH into CONFIG_PVH and CONFIG_XEN_PVH In order to pave the way for hypervisors other than Xen to use the PVH entry point for VMs, we need to factor the PVH entry code into Xen specific and hypervisor agnostic components. The first step in doing that, is to create a new config option for PVH entry that can be enabled independently from CONFIG_XEN. Signed-off-by: Maran Wilson Reviewed-by: Juergen Gross Acked-by: Borislav Petkov Signed-off-by: Boris Ostrovsky --- arch/x86/Kconfig | 6 ++++++ arch/x86/kernel/head_64.S | 2 +- arch/x86/xen/Kconfig | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 8689e794a43c..c2a22a74abee 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -796,6 +796,12 @@ config KVM_GUEST underlying device model, the host provides the guest with timing infrastructure such as time of day, and system time +config PVH + bool "Support for running PVH guests" + ---help--- + This option enables the PVH entry point for guest virtual machines + as specified in the x86/HVM direct boot ABI. + config KVM_DEBUG_FS bool "Enable debug information for KVM Guests in debugfs" depends on KVM_GUEST && DEBUG_FS diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 747c758f67b7..d1dbe8e4eb82 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -386,7 +386,7 @@ NEXT_PAGE(early_dynamic_pgts) .data -#if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) +#if defined(CONFIG_XEN_PV) || defined(CONFIG_PVH) NEXT_PGD_PAGE(init_top_pgt) .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC .org init_top_pgt + L4_PAGE_OFFSET*8, 0 diff --git a/arch/x86/xen/Kconfig b/arch/x86/xen/Kconfig index 1ef391aa184d..e07abefd3d26 100644 --- a/arch/x86/xen/Kconfig +++ b/arch/x86/xen/Kconfig @@ -74,6 +74,7 @@ config XEN_DEBUG_FS Enabling this option may incur a significant performance overhead. config XEN_PVH - bool "Support for running as a PVH guest" + bool "Support for running as a Xen PVH guest" depends on XEN && XEN_PVHVM && ACPI + select PVH def_bool n -- cgit v1.2.3 From fcd4747698150bc3a9596481d3eb2b3ae71d82f1 Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:07:55 -0800 Subject: xen/pvh: Move PVH entry code out of Xen specific tree Once hypervisors other than Xen start using the PVH entry point for starting VMs, we would like the option of being able to compile PVH entry capable kernels without enabling CONFIG_XEN and all the code that comes along with that. To allow that, we are moving the PVH code out of Xen and into files sitting at a higher level in the tree. This patch is not introducing any code or functional changes, just moving files from one location to another. Signed-off-by: Maran Wilson Reviewed-by: Konrad Rzeszutek Wilk Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- MAINTAINERS | 1 + arch/x86/Kbuild | 2 + arch/x86/platform/pvh/Makefile | 5 ++ arch/x86/platform/pvh/enlighten.c | 108 +++++++++++++++++++++++ arch/x86/platform/pvh/head.S | 177 ++++++++++++++++++++++++++++++++++++++ arch/x86/xen/Makefile | 4 - arch/x86/xen/enlighten_pvh.c | 108 ----------------------- arch/x86/xen/xen-pvh.S | 177 -------------------------------------- 8 files changed, 293 insertions(+), 289 deletions(-) create mode 100644 arch/x86/platform/pvh/Makefile create mode 100644 arch/x86/platform/pvh/enlighten.c create mode 100644 arch/x86/platform/pvh/head.S delete mode 100644 arch/x86/xen/enlighten_pvh.c delete mode 100644 arch/x86/xen/xen-pvh.S diff --git a/MAINTAINERS b/MAINTAINERS index 8119141a926f..fc9fe92bfa68 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16470,6 +16470,7 @@ L: xen-devel@lists.xenproject.org (moderated for non-subscribers) T: git git://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git S: Supported F: arch/x86/xen/ +F: arch/x86/platform/pvh/ F: drivers/*/xen-*front.c F: drivers/xen/ F: arch/x86/include/asm/xen/ diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 0038a2d10a7a..2089e4414300 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -7,6 +7,8 @@ obj-$(CONFIG_KVM) += kvm/ # Xen paravirtualization support obj-$(CONFIG_XEN) += xen/ +obj-$(CONFIG_XEN_PVH) += platform/pvh/ + # Hyper-V paravirtualization support obj-$(subst m,y,$(CONFIG_HYPERV)) += hyperv/ diff --git a/arch/x86/platform/pvh/Makefile b/arch/x86/platform/pvh/Makefile new file mode 100644 index 000000000000..9fd25efcd2a3 --- /dev/null +++ b/arch/x86/platform/pvh/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +OBJECT_FILES_NON_STANDARD_head.o := y + +obj-$(CONFIG_XEN_PVH) += enlighten.o +obj-$(CONFIG_XEN_PVH) += head.o diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c new file mode 100644 index 000000000000..02e3ab7ff242 --- /dev/null +++ b/arch/x86/platform/pvh/enlighten.c @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +/* + * PVH variables. + * + * xen_pvh pvh_bootparams and pvh_start_info need to live in data segment + * since they are used after startup_{32|64}, which clear .bss, are invoked. + */ +bool xen_pvh __attribute__((section(".data"))) = 0; +struct boot_params pvh_bootparams __attribute__((section(".data"))); +struct hvm_start_info pvh_start_info __attribute__((section(".data"))); + +unsigned int pvh_start_info_sz = sizeof(pvh_start_info); + +static u64 pvh_get_root_pointer(void) +{ + return pvh_start_info.rsdp_paddr; +} + +static void __init init_pvh_bootparams(void) +{ + struct xen_memory_map memmap; + int rc; + + memset(&pvh_bootparams, 0, sizeof(pvh_bootparams)); + + memmap.nr_entries = ARRAY_SIZE(pvh_bootparams.e820_table); + set_xen_guest_handle(memmap.buffer, pvh_bootparams.e820_table); + rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); + if (rc) { + xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc); + BUG(); + } + pvh_bootparams.e820_entries = memmap.nr_entries; + + if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) { + pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr = + ISA_START_ADDRESS; + pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size = + ISA_END_ADDRESS - ISA_START_ADDRESS; + pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type = + E820_TYPE_RESERVED; + pvh_bootparams.e820_entries++; + } else + xen_raw_printk("Warning: Can fit ISA range into e820\n"); + + pvh_bootparams.hdr.cmd_line_ptr = + pvh_start_info.cmdline_paddr; + + /* The first module is always ramdisk. */ + if (pvh_start_info.nr_modules) { + struct hvm_modlist_entry *modaddr = + __va(pvh_start_info.modlist_paddr); + pvh_bootparams.hdr.ramdisk_image = modaddr->paddr; + pvh_bootparams.hdr.ramdisk_size = modaddr->size; + } + + /* + * See Documentation/x86/boot.txt. + * + * Version 2.12 supports Xen entry point but we will use default x86/PC + * environment (i.e. hardware_subarch 0). + */ + pvh_bootparams.hdr.version = (2 << 8) | 12; + pvh_bootparams.hdr.type_of_loader = (9 << 4) | 0; /* Xen loader */ + + x86_init.acpi.get_root_pointer = pvh_get_root_pointer; +} + +/* + * This routine (and those that it might call) should not use + * anything that lives in .bss since that segment will be cleared later. + */ +void __init xen_prepare_pvh(void) +{ + u32 msr; + u64 pfn; + + if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) { + xen_raw_printk("Error: Unexpected magic value (0x%08x)\n", + pvh_start_info.magic); + BUG(); + } + + xen_pvh = 1; + xen_start_flags = pvh_start_info.flags; + + msr = cpuid_ebx(xen_cpuid_base() + 2); + pfn = __pa(hypercall_page); + wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32)); + + init_pvh_bootparams(); +} diff --git a/arch/x86/platform/pvh/head.S b/arch/x86/platform/pvh/head.S new file mode 100644 index 000000000000..1f8825bbaffb --- /dev/null +++ b/arch/x86/platform/pvh/head.S @@ -0,0 +1,177 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright C 2016, Oracle and/or its affiliates. All rights reserved. + */ + + .code32 + .text +#define _pa(x) ((x) - __START_KERNEL_map) + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + __HEAD + +/* + * Entry point for PVH guests. + * + * Xen ABI specifies the following register state when we come here: + * + * - `ebx`: contains the physical memory address where the loader has placed + * the boot start info structure. + * - `cr0`: bit 0 (PE) must be set. All the other writeable bits are cleared. + * - `cr4`: all bits are cleared. + * - `cs `: must be a 32-bit read/execute code segment with a base of ‘0’ + * and a limit of ‘0xFFFFFFFF’. The selector value is unspecified. + * - `ds`, `es`: must be a 32-bit read/write data segment with a base of + * ‘0’ and a limit of ‘0xFFFFFFFF’. The selector values are all + * unspecified. + * - `tr`: must be a 32-bit TSS (active) with a base of '0' and a limit + * of '0x67'. + * - `eflags`: bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared. + * Bit 8 (TF) must be cleared. Other bits are all unspecified. + * + * All other processor registers and flag bits are unspecified. The OS is in + * charge of setting up it's own stack, GDT and IDT. + */ + +#define PVH_GDT_ENTRY_CS 1 +#define PVH_GDT_ENTRY_DS 2 +#define PVH_GDT_ENTRY_CANARY 3 +#define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8) +#define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8) +#define PVH_CANARY_SEL (PVH_GDT_ENTRY_CANARY * 8) + +ENTRY(pvh_start_xen) + cld + + lgdt (_pa(gdt)) + + mov $PVH_DS_SEL,%eax + mov %eax,%ds + mov %eax,%es + mov %eax,%ss + + /* Stash hvm_start_info. */ + mov $_pa(pvh_start_info), %edi + mov %ebx, %esi + mov _pa(pvh_start_info_sz), %ecx + shr $2,%ecx + rep + movsl + + mov $_pa(early_stack_end), %esp + + /* Enable PAE mode. */ + mov %cr4, %eax + orl $X86_CR4_PAE, %eax + mov %eax, %cr4 + +#ifdef CONFIG_X86_64 + /* Enable Long mode. */ + mov $MSR_EFER, %ecx + rdmsr + btsl $_EFER_LME, %eax + wrmsr + + /* Enable pre-constructed page tables. */ + mov $_pa(init_top_pgt), %eax + mov %eax, %cr3 + mov $(X86_CR0_PG | X86_CR0_PE), %eax + mov %eax, %cr0 + + /* Jump to 64-bit mode. */ + ljmp $PVH_CS_SEL, $_pa(1f) + + /* 64-bit entry point. */ + .code64 +1: + /* Set base address in stack canary descriptor. */ + mov $MSR_GS_BASE,%ecx + mov $_pa(canary), %eax + xor %edx, %edx + wrmsr + + call xen_prepare_pvh + + /* startup_64 expects boot_params in %rsi. */ + mov $_pa(pvh_bootparams), %rsi + mov $_pa(startup_64), %rax + jmp *%rax + +#else /* CONFIG_X86_64 */ + + /* Set base address in stack canary descriptor. */ + movl $_pa(gdt_start),%eax + movl $_pa(canary),%ecx + movw %cx, (PVH_GDT_ENTRY_CANARY * 8) + 2(%eax) + shrl $16, %ecx + movb %cl, (PVH_GDT_ENTRY_CANARY * 8) + 4(%eax) + movb %ch, (PVH_GDT_ENTRY_CANARY * 8) + 7(%eax) + + mov $PVH_CANARY_SEL,%eax + mov %eax,%gs + + call mk_early_pgtbl_32 + + mov $_pa(initial_page_table), %eax + mov %eax, %cr3 + + mov %cr0, %eax + or $(X86_CR0_PG | X86_CR0_PE), %eax + mov %eax, %cr0 + + ljmp $PVH_CS_SEL, $1f +1: + call xen_prepare_pvh + mov $_pa(pvh_bootparams), %esi + + /* startup_32 doesn't expect paging and PAE to be on. */ + ljmp $PVH_CS_SEL, $_pa(2f) +2: + mov %cr0, %eax + and $~X86_CR0_PG, %eax + mov %eax, %cr0 + mov %cr4, %eax + and $~X86_CR4_PAE, %eax + mov %eax, %cr4 + + ljmp $PVH_CS_SEL, $_pa(startup_32) +#endif +END(pvh_start_xen) + + .section ".init.data","aw" + .balign 8 +gdt: + .word gdt_end - gdt_start + .long _pa(gdt_start) + .word 0 +gdt_start: + .quad 0x0000000000000000 /* NULL descriptor */ +#ifdef CONFIG_X86_64 + .quad GDT_ENTRY(0xa09a, 0, 0xfffff) /* PVH_CS_SEL */ +#else + .quad GDT_ENTRY(0xc09a, 0, 0xfffff) /* PVH_CS_SEL */ +#endif + .quad GDT_ENTRY(0xc092, 0, 0xfffff) /* PVH_DS_SEL */ + .quad GDT_ENTRY(0x4090, 0, 0x18) /* PVH_CANARY_SEL */ +gdt_end: + + .balign 16 +canary: + .fill 48, 1, 0 + +early_stack: + .fill BOOT_STACK_SIZE, 1, 0 +early_stack_end: + + ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, + _ASM_PTR (pvh_start_xen - __START_KERNEL_map)) diff --git a/arch/x86/xen/Makefile b/arch/x86/xen/Makefile index dd2550d33b38..b239922f6c6c 100644 --- a/arch/x86/xen/Makefile +++ b/arch/x86/xen/Makefile @@ -1,6 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 OBJECT_FILES_NON_STANDARD_xen-asm_$(BITS).o := y -OBJECT_FILES_NON_STANDARD_xen-pvh.o := y ifdef CONFIG_FUNCTION_TRACER # Do not profile debug and lowlevel utilities @@ -37,9 +36,6 @@ obj-$(CONFIG_XEN_PV) += multicalls.o obj-$(CONFIG_XEN_PV) += xen-asm.o obj-$(CONFIG_XEN_PV) += xen-asm_$(BITS).o -obj-$(CONFIG_XEN_PVH) += enlighten_pvh.o -obj-$(CONFIG_XEN_PVH) += xen-pvh.o - obj-$(CONFIG_EVENT_TRACING) += trace.o obj-$(CONFIG_SMP) += smp.o diff --git a/arch/x86/xen/enlighten_pvh.c b/arch/x86/xen/enlighten_pvh.c deleted file mode 100644 index 02e3ab7ff242..000000000000 --- a/arch/x86/xen/enlighten_pvh.c +++ /dev/null @@ -1,108 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -/* - * PVH variables. - * - * xen_pvh pvh_bootparams and pvh_start_info need to live in data segment - * since they are used after startup_{32|64}, which clear .bss, are invoked. - */ -bool xen_pvh __attribute__((section(".data"))) = 0; -struct boot_params pvh_bootparams __attribute__((section(".data"))); -struct hvm_start_info pvh_start_info __attribute__((section(".data"))); - -unsigned int pvh_start_info_sz = sizeof(pvh_start_info); - -static u64 pvh_get_root_pointer(void) -{ - return pvh_start_info.rsdp_paddr; -} - -static void __init init_pvh_bootparams(void) -{ - struct xen_memory_map memmap; - int rc; - - memset(&pvh_bootparams, 0, sizeof(pvh_bootparams)); - - memmap.nr_entries = ARRAY_SIZE(pvh_bootparams.e820_table); - set_xen_guest_handle(memmap.buffer, pvh_bootparams.e820_table); - rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); - if (rc) { - xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc); - BUG(); - } - pvh_bootparams.e820_entries = memmap.nr_entries; - - if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) { - pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr = - ISA_START_ADDRESS; - pvh_bootparams.e820_table[pvh_bootparams.e820_entries].size = - ISA_END_ADDRESS - ISA_START_ADDRESS; - pvh_bootparams.e820_table[pvh_bootparams.e820_entries].type = - E820_TYPE_RESERVED; - pvh_bootparams.e820_entries++; - } else - xen_raw_printk("Warning: Can fit ISA range into e820\n"); - - pvh_bootparams.hdr.cmd_line_ptr = - pvh_start_info.cmdline_paddr; - - /* The first module is always ramdisk. */ - if (pvh_start_info.nr_modules) { - struct hvm_modlist_entry *modaddr = - __va(pvh_start_info.modlist_paddr); - pvh_bootparams.hdr.ramdisk_image = modaddr->paddr; - pvh_bootparams.hdr.ramdisk_size = modaddr->size; - } - - /* - * See Documentation/x86/boot.txt. - * - * Version 2.12 supports Xen entry point but we will use default x86/PC - * environment (i.e. hardware_subarch 0). - */ - pvh_bootparams.hdr.version = (2 << 8) | 12; - pvh_bootparams.hdr.type_of_loader = (9 << 4) | 0; /* Xen loader */ - - x86_init.acpi.get_root_pointer = pvh_get_root_pointer; -} - -/* - * This routine (and those that it might call) should not use - * anything that lives in .bss since that segment will be cleared later. - */ -void __init xen_prepare_pvh(void) -{ - u32 msr; - u64 pfn; - - if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) { - xen_raw_printk("Error: Unexpected magic value (0x%08x)\n", - pvh_start_info.magic); - BUG(); - } - - xen_pvh = 1; - xen_start_flags = pvh_start_info.flags; - - msr = cpuid_ebx(xen_cpuid_base() + 2); - pfn = __pa(hypercall_page); - wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32)); - - init_pvh_bootparams(); -} diff --git a/arch/x86/xen/xen-pvh.S b/arch/x86/xen/xen-pvh.S deleted file mode 100644 index 1f8825bbaffb..000000000000 --- a/arch/x86/xen/xen-pvh.S +++ /dev/null @@ -1,177 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ - -/* - * Copyright C 2016, Oracle and/or its affiliates. All rights reserved. - */ - - .code32 - .text -#define _pa(x) ((x) - __START_KERNEL_map) - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - __HEAD - -/* - * Entry point for PVH guests. - * - * Xen ABI specifies the following register state when we come here: - * - * - `ebx`: contains the physical memory address where the loader has placed - * the boot start info structure. - * - `cr0`: bit 0 (PE) must be set. All the other writeable bits are cleared. - * - `cr4`: all bits are cleared. - * - `cs `: must be a 32-bit read/execute code segment with a base of ‘0’ - * and a limit of ‘0xFFFFFFFF’. The selector value is unspecified. - * - `ds`, `es`: must be a 32-bit read/write data segment with a base of - * ‘0’ and a limit of ‘0xFFFFFFFF’. The selector values are all - * unspecified. - * - `tr`: must be a 32-bit TSS (active) with a base of '0' and a limit - * of '0x67'. - * - `eflags`: bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared. - * Bit 8 (TF) must be cleared. Other bits are all unspecified. - * - * All other processor registers and flag bits are unspecified. The OS is in - * charge of setting up it's own stack, GDT and IDT. - */ - -#define PVH_GDT_ENTRY_CS 1 -#define PVH_GDT_ENTRY_DS 2 -#define PVH_GDT_ENTRY_CANARY 3 -#define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8) -#define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8) -#define PVH_CANARY_SEL (PVH_GDT_ENTRY_CANARY * 8) - -ENTRY(pvh_start_xen) - cld - - lgdt (_pa(gdt)) - - mov $PVH_DS_SEL,%eax - mov %eax,%ds - mov %eax,%es - mov %eax,%ss - - /* Stash hvm_start_info. */ - mov $_pa(pvh_start_info), %edi - mov %ebx, %esi - mov _pa(pvh_start_info_sz), %ecx - shr $2,%ecx - rep - movsl - - mov $_pa(early_stack_end), %esp - - /* Enable PAE mode. */ - mov %cr4, %eax - orl $X86_CR4_PAE, %eax - mov %eax, %cr4 - -#ifdef CONFIG_X86_64 - /* Enable Long mode. */ - mov $MSR_EFER, %ecx - rdmsr - btsl $_EFER_LME, %eax - wrmsr - - /* Enable pre-constructed page tables. */ - mov $_pa(init_top_pgt), %eax - mov %eax, %cr3 - mov $(X86_CR0_PG | X86_CR0_PE), %eax - mov %eax, %cr0 - - /* Jump to 64-bit mode. */ - ljmp $PVH_CS_SEL, $_pa(1f) - - /* 64-bit entry point. */ - .code64 -1: - /* Set base address in stack canary descriptor. */ - mov $MSR_GS_BASE,%ecx - mov $_pa(canary), %eax - xor %edx, %edx - wrmsr - - call xen_prepare_pvh - - /* startup_64 expects boot_params in %rsi. */ - mov $_pa(pvh_bootparams), %rsi - mov $_pa(startup_64), %rax - jmp *%rax - -#else /* CONFIG_X86_64 */ - - /* Set base address in stack canary descriptor. */ - movl $_pa(gdt_start),%eax - movl $_pa(canary),%ecx - movw %cx, (PVH_GDT_ENTRY_CANARY * 8) + 2(%eax) - shrl $16, %ecx - movb %cl, (PVH_GDT_ENTRY_CANARY * 8) + 4(%eax) - movb %ch, (PVH_GDT_ENTRY_CANARY * 8) + 7(%eax) - - mov $PVH_CANARY_SEL,%eax - mov %eax,%gs - - call mk_early_pgtbl_32 - - mov $_pa(initial_page_table), %eax - mov %eax, %cr3 - - mov %cr0, %eax - or $(X86_CR0_PG | X86_CR0_PE), %eax - mov %eax, %cr0 - - ljmp $PVH_CS_SEL, $1f -1: - call xen_prepare_pvh - mov $_pa(pvh_bootparams), %esi - - /* startup_32 doesn't expect paging and PAE to be on. */ - ljmp $PVH_CS_SEL, $_pa(2f) -2: - mov %cr0, %eax - and $~X86_CR0_PG, %eax - mov %eax, %cr0 - mov %cr4, %eax - and $~X86_CR4_PAE, %eax - mov %eax, %cr4 - - ljmp $PVH_CS_SEL, $_pa(startup_32) -#endif -END(pvh_start_xen) - - .section ".init.data","aw" - .balign 8 -gdt: - .word gdt_end - gdt_start - .long _pa(gdt_start) - .word 0 -gdt_start: - .quad 0x0000000000000000 /* NULL descriptor */ -#ifdef CONFIG_X86_64 - .quad GDT_ENTRY(0xa09a, 0, 0xfffff) /* PVH_CS_SEL */ -#else - .quad GDT_ENTRY(0xc09a, 0, 0xfffff) /* PVH_CS_SEL */ -#endif - .quad GDT_ENTRY(0xc092, 0, 0xfffff) /* PVH_DS_SEL */ - .quad GDT_ENTRY(0x4090, 0, 0x18) /* PVH_CANARY_SEL */ -gdt_end: - - .balign 16 -canary: - .fill 48, 1, 0 - -early_stack: - .fill BOOT_STACK_SIZE, 1, 0 -early_stack_end: - - ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, - _ASM_PTR (pvh_start_xen - __START_KERNEL_map)) -- cgit v1.2.3 From 4df7363e52105bf7a8589efb2959c907872644fb Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:08:44 -0800 Subject: xen/pvh: Create a new file for Xen specific PVH code We need to refactor PVH entry code so that support for other hypervisors like Qemu/KVM can be added more easily. The first step in that direction is to create a new file that will eventually hold the Xen specific routines. Signed-off-by: Maran Wilson Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- arch/x86/platform/pvh/enlighten.c | 5 ++--- arch/x86/xen/Makefile | 2 ++ arch/x86/xen/enlighten_pvh.c | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 arch/x86/xen/enlighten_pvh.c diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c index 02e3ab7ff242..491932991202 100644 --- a/arch/x86/platform/pvh/enlighten.c +++ b/arch/x86/platform/pvh/enlighten.c @@ -18,10 +18,9 @@ /* * PVH variables. * - * xen_pvh pvh_bootparams and pvh_start_info need to live in data segment - * since they are used after startup_{32|64}, which clear .bss, are invoked. + * pvh_bootparams and pvh_start_info need to live in the data segment since + * they are used after startup_{32|64}, which clear .bss, are invoked. */ -bool xen_pvh __attribute__((section(".data"))) = 0; struct boot_params pvh_bootparams __attribute__((section(".data"))); struct hvm_start_info pvh_start_info __attribute__((section(".data"))); diff --git a/arch/x86/xen/Makefile b/arch/x86/xen/Makefile index b239922f6c6c..084de77a109e 100644 --- a/arch/x86/xen/Makefile +++ b/arch/x86/xen/Makefile @@ -36,6 +36,8 @@ obj-$(CONFIG_XEN_PV) += multicalls.o obj-$(CONFIG_XEN_PV) += xen-asm.o obj-$(CONFIG_XEN_PV) += xen-asm_$(BITS).o +obj-$(CONFIG_XEN_PVH) += enlighten_pvh.o + obj-$(CONFIG_EVENT_TRACING) += trace.o obj-$(CONFIG_SMP) += smp.o diff --git a/arch/x86/xen/enlighten_pvh.c b/arch/x86/xen/enlighten_pvh.c new file mode 100644 index 000000000000..6be7bc719b38 --- /dev/null +++ b/arch/x86/xen/enlighten_pvh.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +/* + * PVH variables. + * + * The variable xen_pvh needs to live in the data segment since it is used + * after startup_{32|64} is invoked, which will clear the .bss segment. + */ +bool xen_pvh __attribute__((section(".data"))) = 0; -- cgit v1.2.3 From 8cee3974b35bfb235d4637e10de5c5b364a9057e Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:08:45 -0800 Subject: xen/pvh: Move Xen specific PVH VM initialization out of common file We need to refactor PVH entry code so that support for other hypervisors like Qemu/KVM can be added more easily. This patch moves the small block of code used for initializing Xen PVH virtual machines into the Xen specific file. This initialization is not going to be needed for Qemu/KVM guests. Moving it out of the common file is going to allow us to compile kernels in the future without CONFIG_XEN that are still capable of being booted as a Qemu/KVM guest via the PVH entry point. Signed-off-by: Maran Wilson Reviewed-by: Konrad Rzeszutek Wilk Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- arch/x86/platform/pvh/enlighten.c | 29 ++++++++++++++++++++--------- arch/x86/xen/enlighten_pvh.c | 22 +++++++++++++++++++++- include/xen/xen.h | 3 +++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c index 491932991202..637bd74ba32d 100644 --- a/arch/x86/platform/pvh/enlighten.c +++ b/arch/x86/platform/pvh/enlighten.c @@ -81,27 +81,38 @@ static void __init init_pvh_bootparams(void) x86_init.acpi.get_root_pointer = pvh_get_root_pointer; } +/* + * If we are trying to boot a Xen PVH guest, it is expected that the kernel + * will have been configured to provide the required override for this routine. + */ +void __init __weak xen_pvh_init(void) +{ + xen_raw_printk("Error: Missing xen PVH initialization\n"); + BUG(); +} + +/* + * When we add support for other hypervisors like Qemu/KVM, this routine can + * selectively invoke the appropriate initialization based on guest type. + */ +static void hypervisor_specific_init(void) +{ + xen_pvh_init(); +} + /* * This routine (and those that it might call) should not use * anything that lives in .bss since that segment will be cleared later. */ void __init xen_prepare_pvh(void) { - u32 msr; - u64 pfn; - if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) { xen_raw_printk("Error: Unexpected magic value (0x%08x)\n", pvh_start_info.magic); BUG(); } - xen_pvh = 1; - xen_start_flags = pvh_start_info.flags; - - msr = cpuid_ebx(xen_cpuid_base() + 2); - pfn = __pa(hypercall_page); - wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32)); + hypervisor_specific_init(); init_pvh_bootparams(); } diff --git a/arch/x86/xen/enlighten_pvh.c b/arch/x86/xen/enlighten_pvh.c index 6be7bc719b38..41a7d6ad74e0 100644 --- a/arch/x86/xen/enlighten_pvh.c +++ b/arch/x86/xen/enlighten_pvh.c @@ -1,5 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 -#include +#include + +#include +#include + +#include +#include +#include /* * PVH variables. @@ -8,3 +15,16 @@ * after startup_{32|64} is invoked, which will clear the .bss segment. */ bool xen_pvh __attribute__((section(".data"))) = 0; + +void __init xen_pvh_init(void) +{ + u32 msr; + u64 pfn; + + xen_pvh = 1; + xen_start_flags = pvh_start_info.flags; + + msr = cpuid_ebx(xen_cpuid_base() + 2); + pfn = __pa(hypercall_page); + wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32)); +} diff --git a/include/xen/xen.h b/include/xen/xen.h index d7a2678da77f..0e2156786ad2 100644 --- a/include/xen/xen.h +++ b/include/xen/xen.h @@ -29,6 +29,9 @@ extern bool xen_pvh; extern uint32_t xen_start_flags; +#include +extern struct hvm_start_info pvh_start_info; + #ifdef CONFIG_XEN_DOM0 #include #include -- cgit v1.2.3 From a43fb7da53007e67ad593e179f01a1925b04f586 Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:08:46 -0800 Subject: xen/pvh: Move Xen code for getting mem map via hcall out of common file We need to refactor PVH entry code so that support for other hypervisors like Qemu/KVM can be added more easily. The original design for PVH entry in Xen guests relies on being able to obtain the memory map from the hypervisor using a hypercall. When we extend the PVH entry ABI to support other hypervisors like Qemu/KVM, a new mechanism will be added that allows the guest to get the memory map without needing to use hypercalls. For Xen guests, the hypercall approach will still be supported. In preparation for adding support for other hypervisors, we can move the code that uses hypercalls into the Xen specific file. This will allow us to compile kernels in the future without CONFIG_XEN that are still capable of being booted as a Qemu/KVM guest via the PVH entry point. Signed-off-by: Maran Wilson Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- arch/x86/platform/pvh/enlighten.c | 29 ++++++++++++++--------------- arch/x86/xen/enlighten_pvh.c | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c index 637bd74ba32d..8040b3fbf545 100644 --- a/arch/x86/platform/pvh/enlighten.c +++ b/arch/x86/platform/pvh/enlighten.c @@ -8,11 +8,7 @@ #include #include -#include -#include - #include -#include #include /* @@ -31,21 +27,24 @@ static u64 pvh_get_root_pointer(void) return pvh_start_info.rsdp_paddr; } -static void __init init_pvh_bootparams(void) +/* + * Xen guests are able to obtain the memory map from the hypervisor via the + * HYPERVISOR_memory_op hypercall. + * If we are trying to boot a Xen PVH guest, it is expected that the kernel + * will have been configured to provide an override for this routine to do + * just that. + */ +void __init __weak mem_map_via_hcall(struct boot_params *ptr __maybe_unused) { - struct xen_memory_map memmap; - int rc; + xen_raw_printk("Error: Could not find memory map\n"); + BUG(); +} +static void __init init_pvh_bootparams(void) +{ memset(&pvh_bootparams, 0, sizeof(pvh_bootparams)); - memmap.nr_entries = ARRAY_SIZE(pvh_bootparams.e820_table); - set_xen_guest_handle(memmap.buffer, pvh_bootparams.e820_table); - rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); - if (rc) { - xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc); - BUG(); - } - pvh_bootparams.e820_entries = memmap.nr_entries; + mem_map_via_hcall(&pvh_bootparams); if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) { pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr = diff --git a/arch/x86/xen/enlighten_pvh.c b/arch/x86/xen/enlighten_pvh.c index 41a7d6ad74e0..35b7599d2d0b 100644 --- a/arch/x86/xen/enlighten_pvh.c +++ b/arch/x86/xen/enlighten_pvh.c @@ -1,13 +1,18 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include + #include #include +#include #include #include #include +#include + /* * PVH variables. * @@ -28,3 +33,18 @@ void __init xen_pvh_init(void) pfn = __pa(hypercall_page); wrmsr_safe(msr, (u32)pfn, (u32)(pfn >> 32)); } + +void __init mem_map_via_hcall(struct boot_params *boot_params_p) +{ + struct xen_memory_map memmap; + int rc; + + memmap.nr_entries = ARRAY_SIZE(boot_params_p->e820_table); + set_xen_guest_handle(memmap.buffer, boot_params_p->e820_table); + rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); + if (rc) { + xen_raw_printk("XENMEM_memory_map failed (%d)\n", rc); + BUG(); + } + boot_params_p->e820_entries = memmap.nr_entries; +} -- cgit v1.2.3 From d907be2b5b042536d15687ac0a86f27c8c9aebd4 Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:09:11 -0800 Subject: xen/pvh: Add memory map pointer to hvm_start_info struct The start info structure that is defined as part of the x86/HVM direct boot ABI and used for starting Xen PVH guests would be more versatile if it also included a way to pass information about the memory map to the guest. This would allow KVM guests to share the same entry point. Signed-off-by: Maran Wilson Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- include/xen/interface/hvm/start_info.h | 63 +++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/include/xen/interface/hvm/start_info.h b/include/xen/interface/hvm/start_info.h index 648415976ead..50af9ea2ff1e 100644 --- a/include/xen/interface/hvm/start_info.h +++ b/include/xen/interface/hvm/start_info.h @@ -33,7 +33,7 @@ * | magic | Contains the magic value XEN_HVM_START_MAGIC_VALUE * | | ("xEn3" with the 0x80 bit of the "E" set). * 4 +----------------+ - * | version | Version of this structure. Current version is 0. New + * | version | Version of this structure. Current version is 1. New * | | versions are guaranteed to be backwards-compatible. * 8 +----------------+ * | flags | SIF_xxx flags. @@ -48,6 +48,15 @@ * 32 +----------------+ * | rsdp_paddr | Physical address of the RSDP ACPI data structure. * 40 +----------------+ + * | memmap_paddr | Physical address of the (optional) memory map. Only + * | | present in version 1 and newer of the structure. + * 48 +----------------+ + * | memmap_entries | Number of entries in the memory map table. Zero + * | | if there is no memory map being provided. Only + * | | present in version 1 and newer of the structure. + * 52 +----------------+ + * | reserved | Version 1 and newer only. + * 56 +----------------+ * * The layout of each entry in the module structure is the following: * @@ -62,13 +71,51 @@ * | reserved | * 32 +----------------+ * + * The layout of each entry in the memory map table is as follows: + * + * 0 +----------------+ + * | addr | Base address + * 8 +----------------+ + * | size | Size of mapping in bytes + * 16 +----------------+ + * | type | Type of mapping as defined between the hypervisor + * | | and guest. See XEN_HVM_MEMMAP_TYPE_* values below. + * 20 +----------------| + * | reserved | + * 24 +----------------+ + * * The address and sizes are always a 64bit little endian unsigned integer. * * NB: Xen on x86 will always try to place all the data below the 4GiB * boundary. + * + * Version numbers of the hvm_start_info structure have evolved like this: + * + * Version 0: Initial implementation. + * + * Version 1: Added the memmap_paddr/memmap_entries fields (plus 4 bytes of + * padding) to the end of the hvm_start_info struct. These new + * fields can be used to pass a memory map to the guest. The + * memory map is optional and so guests that understand version 1 + * of the structure must check that memmap_entries is non-zero + * before trying to read the memory map. */ #define XEN_HVM_START_MAGIC_VALUE 0x336ec578 +/* + * The values used in the type field of the memory map table entries are + * defined below and match the Address Range Types as defined in the "System + * Address Map Interfaces" section of the ACPI Specification. Please refer to + * section 15 in version 6.2 of the ACPI spec: http://uefi.org/specifications + */ +#define XEN_HVM_MEMMAP_TYPE_RAM 1 +#define XEN_HVM_MEMMAP_TYPE_RESERVED 2 +#define XEN_HVM_MEMMAP_TYPE_ACPI 3 +#define XEN_HVM_MEMMAP_TYPE_NVS 4 +#define XEN_HVM_MEMMAP_TYPE_UNUSABLE 5 +#define XEN_HVM_MEMMAP_TYPE_DISABLED 6 +#define XEN_HVM_MEMMAP_TYPE_PMEM 7 + /* * C representation of the x86/HVM start info layout. * @@ -86,6 +133,13 @@ struct hvm_start_info { uint64_t cmdline_paddr; /* Physical address of the command line. */ uint64_t rsdp_paddr; /* Physical address of the RSDP ACPI data */ /* structure. */ + /* All following fields only present in version 1 and newer */ + uint64_t memmap_paddr; /* Physical address of an array of */ + /* hvm_memmap_table_entry. */ + uint32_t memmap_entries; /* Number of entries in the memmap table. */ + /* Value will be zero if there is no memory */ + /* map being provided. */ + uint32_t reserved; /* Must be zero. */ }; struct hvm_modlist_entry { @@ -95,4 +149,11 @@ struct hvm_modlist_entry { uint64_t reserved; }; +struct hvm_memmap_table_entry { + uint64_t addr; /* Base address of the memory region */ + uint64_t size; /* Size of the memory region in bytes */ + uint32_t type; /* Mapping type */ + uint32_t reserved; /* Must be zero for Version 1. */ +}; + #endif /* __XEN_PUBLIC_ARCH_X86_HVM_START_INFO_H__ */ -- cgit v1.2.3 From 716ff017a39e552f47de7667ba934d0547ba7535 Mon Sep 17 00:00:00 2001 From: Maran Wilson Date: Mon, 10 Dec 2018 11:09:35 -0800 Subject: KVM: x86: Allow Qemu/KVM to use PVH entry point For certain applications it is desirable to rapidly boot a KVM virtual machine. In cases where legacy hardware and software support within the guest is not needed, Qemu should be able to boot directly into the uncompressed Linux kernel binary without the need to run firmware. There already exists an ABI to allow this for Xen PVH guests and the ABI is supported by Linux and FreeBSD: https://xenbits.xen.org/docs/unstable/misc/pvh.html This patch enables Qemu to use that same entry point for booting KVM guests. Signed-off-by: Maran Wilson Suggested-by: Konrad Rzeszutek Wilk Suggested-by: Boris Ostrovsky Tested-by: Boris Ostrovsky Reviewed-by: Juergen Gross Signed-off-by: Boris Ostrovsky --- arch/x86/Kbuild | 2 +- arch/x86/platform/pvh/Makefile | 4 ++-- arch/x86/platform/pvh/enlighten.c | 42 +++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild index 2089e4414300..c625f57472f7 100644 --- a/arch/x86/Kbuild +++ b/arch/x86/Kbuild @@ -7,7 +7,7 @@ obj-$(CONFIG_KVM) += kvm/ # Xen paravirtualization support obj-$(CONFIG_XEN) += xen/ -obj-$(CONFIG_XEN_PVH) += platform/pvh/ +obj-$(CONFIG_PVH) += platform/pvh/ # Hyper-V paravirtualization support obj-$(subst m,y,$(CONFIG_HYPERV)) += hyperv/ diff --git a/arch/x86/platform/pvh/Makefile b/arch/x86/platform/pvh/Makefile index 9fd25efcd2a3..5dec5067c9fb 100644 --- a/arch/x86/platform/pvh/Makefile +++ b/arch/x86/platform/pvh/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 OBJECT_FILES_NON_STANDARD_head.o := y -obj-$(CONFIG_XEN_PVH) += enlighten.o -obj-$(CONFIG_XEN_PVH) += head.o +obj-$(CONFIG_PVH) += enlighten.o +obj-$(CONFIG_PVH) += head.o diff --git a/arch/x86/platform/pvh/enlighten.c b/arch/x86/platform/pvh/enlighten.c index 8040b3fbf545..62f5c7045944 100644 --- a/arch/x86/platform/pvh/enlighten.c +++ b/arch/x86/platform/pvh/enlighten.c @@ -8,6 +8,8 @@ #include #include +#include + #include #include @@ -40,11 +42,28 @@ void __init __weak mem_map_via_hcall(struct boot_params *ptr __maybe_unused) BUG(); } -static void __init init_pvh_bootparams(void) +static void __init init_pvh_bootparams(bool xen_guest) { memset(&pvh_bootparams, 0, sizeof(pvh_bootparams)); - mem_map_via_hcall(&pvh_bootparams); + if ((pvh_start_info.version > 0) && (pvh_start_info.memmap_entries)) { + struct hvm_memmap_table_entry *ep; + int i; + + ep = __va(pvh_start_info.memmap_paddr); + pvh_bootparams.e820_entries = pvh_start_info.memmap_entries; + + for (i = 0; i < pvh_bootparams.e820_entries ; i++, ep++) { + pvh_bootparams.e820_table[i].addr = ep->addr; + pvh_bootparams.e820_table[i].size = ep->size; + pvh_bootparams.e820_table[i].type = ep->type; + } + } else if (xen_guest) { + mem_map_via_hcall(&pvh_bootparams); + } else { + /* Non-xen guests are not supported by version 0 */ + BUG(); + } if (pvh_bootparams.e820_entries < E820_MAX_ENTRIES_ZEROPAGE - 1) { pvh_bootparams.e820_table[pvh_bootparams.e820_entries].addr = @@ -75,7 +94,7 @@ static void __init init_pvh_bootparams(void) * environment (i.e. hardware_subarch 0). */ pvh_bootparams.hdr.version = (2 << 8) | 12; - pvh_bootparams.hdr.type_of_loader = (9 << 4) | 0; /* Xen loader */ + pvh_bootparams.hdr.type_of_loader = ((xen_guest ? 0x9 : 0xb) << 4) | 0; x86_init.acpi.get_root_pointer = pvh_get_root_pointer; } @@ -90,13 +109,10 @@ void __init __weak xen_pvh_init(void) BUG(); } -/* - * When we add support for other hypervisors like Qemu/KVM, this routine can - * selectively invoke the appropriate initialization based on guest type. - */ -static void hypervisor_specific_init(void) +static void hypervisor_specific_init(bool xen_guest) { - xen_pvh_init(); + if (xen_guest) + xen_pvh_init(); } /* @@ -105,13 +121,17 @@ static void hypervisor_specific_init(void) */ void __init xen_prepare_pvh(void) { + + u32 msr = xen_cpuid_base(); + bool xen_guest = !!msr; + if (pvh_start_info.magic != XEN_HVM_START_MAGIC_VALUE) { xen_raw_printk("Error: Unexpected magic value (0x%08x)\n", pvh_start_info.magic); BUG(); } - hypervisor_specific_init(); + hypervisor_specific_init(xen_guest); - init_pvh_bootparams(); + init_pvh_bootparams(xen_guest); } -- cgit v1.2.3 From bf9445a33ae6ac2f0822d2f1ce1365408387d568 Mon Sep 17 00:00:00 2001 From: Andrea Righi Date: Mon, 10 Dec 2018 16:12:57 +0100 Subject: kprobes/x86/xen: blacklist non-attachable xen interrupt functions Blacklist symbols in Xen probe-prohibited areas, so that user can see these prohibited symbols in debugfs. See also: a50480cb6d61. Signed-off-by: Andrea Righi Acked-by: Masami Hiramatsu Signed-off-by: Boris Ostrovsky --- arch/x86/xen/xen-asm_64.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/xen/xen-asm_64.S b/arch/x86/xen/xen-asm_64.S index bb1c2da0381d..1e9ef0ba30a5 100644 --- a/arch/x86/xen/xen-asm_64.S +++ b/arch/x86/xen/xen-asm_64.S @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -24,6 +25,7 @@ ENTRY(xen_\name) pop %r11 jmp \name END(xen_\name) +_ASM_NOKPROBE(xen_\name) .endm xen_pv_trap divide_error -- cgit v1.2.3 From 1669907e3d1abfa3f7586e2d55dbbc117b5adba2 Mon Sep 17 00:00:00 2001 From: Ross Lagerwall Date: Fri, 14 Dec 2018 12:55:45 +0000 Subject: xen/pciback: Check dev_data before using it If pcistub_init_device fails, the release function will be called with dev_data set to NULL. Check it before using it to avoid a NULL pointer dereference. Signed-off-by: Ross Lagerwall Reviewed-by: Boris Ostrovsky Signed-off-by: Boris Ostrovsky --- drivers/xen/xen-pciback/pci_stub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/xen/xen-pciback/pci_stub.c b/drivers/xen/xen-pciback/pci_stub.c index 59661db144e5..097410a7cdb7 100644 --- a/drivers/xen/xen-pciback/pci_stub.c +++ b/drivers/xen/xen-pciback/pci_stub.c @@ -106,7 +106,8 @@ static void pcistub_device_release(struct kref *kref) * is called from "unbind" which takes a device_lock mutex. */ __pci_reset_function_locked(dev); - if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) + if (dev_data && + pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) dev_info(&dev->dev, "Could not reload PCI state\n"); else pci_restore_state(dev); -- cgit v1.2.3 From b3383974fee272ad0d444a2d083f456879447cd8 Mon Sep 17 00:00:00 2001 From: Oleksandr Andrushchenko Date: Fri, 30 Nov 2018 09:42:03 +0200 Subject: xen: Introduce shared buffer helpers for page directory... based frontends. Currently the frontends which implement similar code for sharing big buffers between frontend and backend are para-virtualized DRM and sound drivers. Both define the same way to share grant references of a data buffer with the corresponding backend with little differences. Move shared code into a helper module, so there is a single implementation of the same functionality for all. This patch introduces code which is used by sound and display frontend drivers without functional changes with the intention to remove shared code from the corresponding drivers. Signed-off-by: Oleksandr Andrushchenko Acked-by: Boris Ostrovsky Signed-off-by: Boris Ostrovsky --- drivers/xen/Kconfig | 3 + drivers/xen/Makefile | 1 + drivers/xen/xen-front-pgdir-shbuf.c | 553 ++++++++++++++++++++++++++++++++++++ include/xen/xen-front-pgdir-shbuf.h | 89 ++++++ 4 files changed, 646 insertions(+) create mode 100644 drivers/xen/xen-front-pgdir-shbuf.c create mode 100644 include/xen/xen-front-pgdir-shbuf.h diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig index 815b9e9bb975..838b66a9a0e7 100644 --- a/drivers/xen/Kconfig +++ b/drivers/xen/Kconfig @@ -340,4 +340,7 @@ config XEN_SYMS config XEN_HAVE_VPMU bool +config XEN_FRONT_PGDIR_SHBUF + tristate + endmenu diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile index 3e542f60f29f..c48927a58e10 100644 --- a/drivers/xen/Makefile +++ b/drivers/xen/Makefile @@ -44,3 +44,4 @@ xen-gntdev-y := gntdev.o xen-gntdev-$(CONFIG_XEN_GNTDEV_DMABUF) += gntdev-dmabuf.o xen-gntalloc-y := gntalloc.o xen-privcmd-y := privcmd.o privcmd-buf.o +obj-$(CONFIG_XEN_FRONT_PGDIR_SHBUF) += xen-front-pgdir-shbuf.o diff --git a/drivers/xen/xen-front-pgdir-shbuf.c b/drivers/xen/xen-front-pgdir-shbuf.c new file mode 100644 index 000000000000..48a658dc7ccf --- /dev/null +++ b/drivers/xen/xen-front-pgdir-shbuf.c @@ -0,0 +1,553 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +/* + * Xen frontend/backend page directory based shared buffer + * helper module. + * + * Copyright (C) 2018 EPAM Systems Inc. + * + * Author: Oleksandr Andrushchenko + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#ifndef GRANT_INVALID_REF +/* + * FIXME: usage of grant reference 0 as invalid grant reference: + * grant reference 0 is valid, but never exposed to a PV driver, + * because of the fact it is already in use/reserved by the PV console. + */ +#define GRANT_INVALID_REF 0 +#endif + +/** + * This structure represents the structure of a shared page + * that contains grant references to the pages of the shared + * buffer. This structure is common to many Xen para-virtualized + * protocols at include/xen/interface/io/ + */ +struct xen_page_directory { + grant_ref_t gref_dir_next_page; + grant_ref_t gref[1]; /* Variable length */ +}; + +/** + * Shared buffer ops which are differently implemented + * depending on the allocation mode, e.g. if the buffer + * is allocated by the corresponding backend or frontend. + * Some of the operations. + */ +struct xen_front_pgdir_shbuf_ops { + /* + * Calculate number of grefs required to handle this buffer, + * e.g. if grefs are required for page directory only or the buffer + * pages as well. + */ + void (*calc_num_grefs)(struct xen_front_pgdir_shbuf *buf); + + /* Fill page directory according to para-virtual display protocol. */ + void (*fill_page_dir)(struct xen_front_pgdir_shbuf *buf); + + /* Claim grant references for the pages of the buffer. */ + int (*grant_refs_for_buffer)(struct xen_front_pgdir_shbuf *buf, + grant_ref_t *priv_gref_head, int gref_idx); + + /* Map grant references of the buffer. */ + int (*map)(struct xen_front_pgdir_shbuf *buf); + + /* Unmap grant references of the buffer. */ + int (*unmap)(struct xen_front_pgdir_shbuf *buf); +}; + +/** + * Get granted reference to the very first page of the + * page directory. Usually this is passed to the backend, + * so it can find/fill the grant references to the buffer's + * pages. + * + * \param buf shared buffer which page directory is of interest. + * \return granted reference to the very first page of the + * page directory. + */ +grant_ref_t +xen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf *buf) +{ + if (!buf->grefs) + return GRANT_INVALID_REF; + + return buf->grefs[0]; +} +EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_get_dir_start); + +/** + * Map granted references of the shared buffer. + * + * Depending on the shared buffer mode of allocation + * (be_alloc flag) this can either do nothing (for buffers + * shared by the frontend itself) or map the provided granted + * references onto the backing storage (buf->pages). + * + * \param buf shared buffer which grants to be maped. + * \return zero on success or a negative number on failure. + */ +int xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf *buf) +{ + if (buf->ops && buf->ops->map) + return buf->ops->map(buf); + + /* No need to map own grant references. */ + return 0; +} +EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_map); + +/** + * Unmap granted references of the shared buffer. + * + * Depending on the shared buffer mode of allocation + * (be_alloc flag) this can either do nothing (for buffers + * shared by the frontend itself) or unmap the provided granted + * references. + * + * \param buf shared buffer which grants to be unmaped. + * \return zero on success or a negative number on failure. + */ +int xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf *buf) +{ + if (buf->ops && buf->ops->unmap) + return buf->ops->unmap(buf); + + /* No need to unmap own grant references. */ + return 0; +} +EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_unmap); + +/** + * Free all the resources of the shared buffer. + * + * \param buf shared buffer which resources to be freed. + */ +void xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf *buf) +{ + if (buf->grefs) { + int i; + + for (i = 0; i < buf->num_grefs; i++) + if (buf->grefs[i] != GRANT_INVALID_REF) + gnttab_end_foreign_access(buf->grefs[i], + 0, 0UL); + } + kfree(buf->grefs); + kfree(buf->directory); +} +EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_free); + +/* + * Number of grefs a page can hold with respect to the + * struct xen_page_directory header. + */ +#define XEN_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \ + offsetof(struct xen_page_directory, \ + gref)) / sizeof(grant_ref_t)) + +/** + * Get the number of pages the page directory consumes itself. + * + * \param buf shared buffer. + */ +static int get_num_pages_dir(struct xen_front_pgdir_shbuf *buf) +{ + return DIV_ROUND_UP(buf->num_pages, XEN_NUM_GREFS_PER_PAGE); +} + +/** + * Calculate the number of grant references needed to share the buffer + * and its pages when backend allocates the buffer. + * + * \param buf shared buffer. + */ +static void backend_calc_num_grefs(struct xen_front_pgdir_shbuf *buf) +{ + /* Only for pages the page directory consumes itself. */ + buf->num_grefs = get_num_pages_dir(buf); +} + +/** + * Calculate the number of grant references needed to share the buffer + * and its pages when frontend allocates the buffer. + * + * \param buf shared buffer. + */ +static void guest_calc_num_grefs(struct xen_front_pgdir_shbuf *buf) +{ + /* + * Number of pages the page directory consumes itself + * plus grefs for the buffer pages. + */ + buf->num_grefs = get_num_pages_dir(buf) + buf->num_pages; +} + +#define xen_page_to_vaddr(page) \ + ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page))) + +/** + * Unmap the buffer previously mapped with grant references + * provided by the backend. + * + * \param buf shared buffer. + * \return zero on success or a negative number on failure. + */ +static int backend_unmap(struct xen_front_pgdir_shbuf *buf) +{ + struct gnttab_unmap_grant_ref *unmap_ops; + int i, ret; + + if (!buf->pages || !buf->backend_map_handles || !buf->grefs) + return 0; + + unmap_ops = kcalloc(buf->num_pages, sizeof(*unmap_ops), + GFP_KERNEL); + if (!unmap_ops) + return -ENOMEM; + + for (i = 0; i < buf->num_pages; i++) { + phys_addr_t addr; + + addr = xen_page_to_vaddr(buf->pages[i]); + gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map, + buf->backend_map_handles[i]); + } + + ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages, + buf->num_pages); + + for (i = 0; i < buf->num_pages; i++) { + if (unlikely(unmap_ops[i].status != GNTST_okay)) + dev_err(&buf->xb_dev->dev, + "Failed to unmap page %d: %d\n", + i, unmap_ops[i].status); + } + + if (ret) + dev_err(&buf->xb_dev->dev, + "Failed to unmap grant references, ret %d", ret); + + kfree(unmap_ops); + kfree(buf->backend_map_handles); + buf->backend_map_handles = NULL; + return ret; +} + +/** + * Map the buffer with grant references provided by the backend. + * + * \param buf shared buffer. + * \return zero on success or a negative number on failure. + */ +static int backend_map(struct xen_front_pgdir_shbuf *buf) +{ + struct gnttab_map_grant_ref *map_ops = NULL; + unsigned char *ptr; + int ret, cur_gref, cur_dir_page, cur_page, grefs_left; + + map_ops = kcalloc(buf->num_pages, sizeof(*map_ops), GFP_KERNEL); + if (!map_ops) + return -ENOMEM; + + buf->backend_map_handles = kcalloc(buf->num_pages, + sizeof(*buf->backend_map_handles), + GFP_KERNEL); + if (!buf->backend_map_handles) { + kfree(map_ops); + return -ENOMEM; + } + + /* + * Read page directory to get grefs from the backend: for external + * buffer we only allocate buf->grefs for the page directory, + * so buf->num_grefs has number of pages in the page directory itself. + */ + ptr = buf->directory; + grefs_left = buf->num_pages; + cur_page = 0; + for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) { + struct xen_page_directory *page_dir = + (struct xen_page_directory *)ptr; + int to_copy = XEN_NUM_GREFS_PER_PAGE; + + if (to_copy > grefs_left) + to_copy = grefs_left; + + for (cur_gref = 0; cur_gref < to_copy; cur_gref++) { + phys_addr_t addr; + + addr = xen_page_to_vaddr(buf->pages[cur_page]); + gnttab_set_map_op(&map_ops[cur_page], addr, + GNTMAP_host_map, + page_dir->gref[cur_gref], + buf->xb_dev->otherend_id); + cur_page++; + } + + grefs_left -= to_copy; + ptr += PAGE_SIZE; + } + ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages); + + /* Save handles even if error, so we can unmap. */ + for (cur_page = 0; cur_page < buf->num_pages; cur_page++) { + buf->backend_map_handles[cur_page] = map_ops[cur_page].handle; + if (unlikely(map_ops[cur_page].status != GNTST_okay)) + dev_err(&buf->xb_dev->dev, + "Failed to map page %d: %d\n", + cur_page, map_ops[cur_page].status); + } + + if (ret) { + dev_err(&buf->xb_dev->dev, + "Failed to map grant references, ret %d", ret); + backend_unmap(buf); + } + + kfree(map_ops); + return ret; +} + +/** + * Fill page directory with grant references to the pages of the + * page directory itself. + * + * The grant references to the buffer pages are provided by the + * backend in this case. + * + * \param buf shared buffer. + */ +static void backend_fill_page_dir(struct xen_front_pgdir_shbuf *buf) +{ + struct xen_page_directory *page_dir; + unsigned char *ptr; + int i, num_pages_dir; + + ptr = buf->directory; + num_pages_dir = get_num_pages_dir(buf); + + /* Fill only grefs for the page directory itself. */ + for (i = 0; i < num_pages_dir - 1; i++) { + page_dir = (struct xen_page_directory *)ptr; + + page_dir->gref_dir_next_page = buf->grefs[i + 1]; + ptr += PAGE_SIZE; + } + /* Last page must say there is no more pages. */ + page_dir = (struct xen_page_directory *)ptr; + page_dir->gref_dir_next_page = GRANT_INVALID_REF; +} + +/** + * Fill page directory with grant references to the pages of the + * page directory and the buffer we share with the backend. + * + * \param buf shared buffer. + */ +static void guest_fill_page_dir(struct xen_front_pgdir_shbuf *buf) +{ + unsigned char *ptr; + int cur_gref, grefs_left, to_copy, i, num_pages_dir; + + ptr = buf->directory; + num_pages_dir = get_num_pages_dir(buf); + + /* + * While copying, skip grefs at start, they are for pages + * granted for the page directory itself. + */ + cur_gref = num_pages_dir; + grefs_left = buf->num_pages; + for (i = 0; i < num_pages_dir; i++) { + struct xen_page_directory *page_dir = + (struct xen_page_directory *)ptr; + + if (grefs_left <= XEN_NUM_GREFS_PER_PAGE) { + to_copy = grefs_left; + page_dir->gref_dir_next_page = GRANT_INVALID_REF; + } else { + to_copy = XEN_NUM_GREFS_PER_PAGE; + page_dir->gref_dir_next_page = buf->grefs[i + 1]; + } + memcpy(&page_dir->gref, &buf->grefs[cur_gref], + to_copy * sizeof(grant_ref_t)); + ptr += PAGE_SIZE; + grefs_left -= to_copy; + cur_gref += to_copy; + } +} + +/** + * Grant references to the frontend's buffer pages. + * + * These will be shared with the backend, so it can + * access the buffer's data. + * + * \param buf shared buffer. + * \return zero on success or a negative number on failure. + */ +static int guest_grant_refs_for_buffer(struct xen_front_pgdir_shbuf *buf, + grant_ref_t *priv_gref_head, + int gref_idx) +{ + int i, cur_ref, otherend_id; + + otherend_id = buf->xb_dev->otherend_id; + for (i = 0; i < buf->num_pages; i++) { + cur_ref = gnttab_claim_grant_reference(priv_gref_head); + if (cur_ref < 0) + return cur_ref; + + gnttab_grant_foreign_access_ref(cur_ref, otherend_id, + xen_page_to_gfn(buf->pages[i]), + 0); + buf->grefs[gref_idx++] = cur_ref; + } + return 0; +} + +/** + * Grant all the references needed to share the buffer. + * + * Grant references to the page directory pages and, if + * needed, also to the pages of the shared buffer data. + * + * \param buf shared buffer. + * \return zero on success or a negative number on failure. + */ +static int grant_references(struct xen_front_pgdir_shbuf *buf) +{ + grant_ref_t priv_gref_head; + int ret, i, j, cur_ref; + int otherend_id, num_pages_dir; + + ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head); + if (ret < 0) { + dev_err(&buf->xb_dev->dev, + "Cannot allocate grant references\n"); + return ret; + } + + otherend_id = buf->xb_dev->otherend_id; + j = 0; + num_pages_dir = get_num_pages_dir(buf); + for (i = 0; i < num_pages_dir; i++) { + unsigned long frame; + + cur_ref = gnttab_claim_grant_reference(&priv_gref_head); + if (cur_ref < 0) + return cur_ref; + + frame = xen_page_to_gfn(virt_to_page(buf->directory + + PAGE_SIZE * i)); + gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0); + buf->grefs[j++] = cur_ref; + } + + if (buf->ops->grant_refs_for_buffer) { + ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j); + if (ret) + return ret; + } + + gnttab_free_grant_references(priv_gref_head); + return 0; +} + +/** + * Allocate all required structures to mange shared buffer. + * + * \param buf shared buffer. + * \return zero on success or a negative number on failure. + */ +static int alloc_storage(struct xen_front_pgdir_shbuf *buf) +{ + buf->grefs = kcalloc(buf->num_grefs, sizeof(*buf->grefs), GFP_KERNEL); + if (!buf->grefs) + return -ENOMEM; + + buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL); + if (!buf->directory) + return -ENOMEM; + + return 0; +} + +/* + * For backend allocated buffers we don't need grant_refs_for_buffer + * as those grant references are allocated at backend side. + */ +static const struct xen_front_pgdir_shbuf_ops backend_ops = { + .calc_num_grefs = backend_calc_num_grefs, + .fill_page_dir = backend_fill_page_dir, + .map = backend_map, + .unmap = backend_unmap +}; + +/* + * For locally granted references we do not need to map/unmap + * the references. + */ +static const struct xen_front_pgdir_shbuf_ops local_ops = { + .calc_num_grefs = guest_calc_num_grefs, + .fill_page_dir = guest_fill_page_dir, + .grant_refs_for_buffer = guest_grant_refs_for_buffer, +}; + +/** + * Allocate a new instance of a shared buffer. + * + * \param cfg configuration to be used while allocating a new shared buffer. + * \return zero on success or a negative number on failure. + */ +int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg) +{ + struct xen_front_pgdir_shbuf *buf = cfg->pgdir; + int ret; + + if (cfg->be_alloc) + buf->ops = &backend_ops; + else + buf->ops = &local_ops; + buf->xb_dev = cfg->xb_dev; + buf->num_pages = cfg->num_pages; + buf->pages = cfg->pages; + + buf->ops->calc_num_grefs(buf); + + ret = alloc_storage(buf); + if (ret) + goto fail; + + ret = grant_references(buf); + if (ret) + goto fail; + + buf->ops->fill_page_dir(buf); + + return 0; + +fail: + xen_front_pgdir_shbuf_free(buf); + return ret; +} +EXPORT_SYMBOL_GPL(xen_front_pgdir_shbuf_alloc); + +MODULE_DESCRIPTION("Xen frontend/backend page directory based " + "shared buffer handling"); +MODULE_AUTHOR("Oleksandr Andrushchenko"); +MODULE_LICENSE("GPL"); diff --git a/include/xen/xen-front-pgdir-shbuf.h b/include/xen/xen-front-pgdir-shbuf.h new file mode 100644 index 000000000000..150ef7ec51ec --- /dev/null +++ b/include/xen/xen-front-pgdir-shbuf.h @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +/* + * Xen frontend/backend page directory based shared buffer + * helper module. + * + * Copyright (C) 2018 EPAM Systems Inc. + * + * Author: Oleksandr Andrushchenko + */ + +#ifndef __XEN_FRONT_PGDIR_SHBUF_H_ +#define __XEN_FRONT_PGDIR_SHBUF_H_ + +#include + +#include + +struct xen_front_pgdir_shbuf_ops; + +struct xen_front_pgdir_shbuf { + /* + * Number of references granted for the backend use: + * + * - for frontend allocated/imported buffers this holds the number + * of grant references for the page directory and the pages + * of the buffer + * + * - for the buffer provided by the backend this only holds the number + * of grant references for the page directory itself as grant + * references for the buffer will be provided by the backend. + */ + int num_grefs; + grant_ref_t *grefs; + /* Page directory backing storage. */ + u8 *directory; + + /* + * Number of pages for the shared buffer itself (excluding the page + * directory). + */ + int num_pages; + /* + * Backing storage of the shared buffer: these are the pages being + * shared. + */ + struct page **pages; + + struct xenbus_device *xb_dev; + + /* These are the ops used internally depending on be_alloc mode. */ + const struct xen_front_pgdir_shbuf_ops *ops; + + /* Xen map handles for the buffer allocated by the backend. */ + grant_handle_t *backend_map_handles; +}; + +struct xen_front_pgdir_shbuf_cfg { + struct xenbus_device *xb_dev; + + /* Number of pages of the buffer backing storage. */ + int num_pages; + /* Pages of the buffer to be shared. */ + struct page **pages; + + /* + * This is allocated outside because there are use-cases when + * the buffer structure is allocated as a part of a bigger one. + */ + struct xen_front_pgdir_shbuf *pgdir; + /* + * Mode of grant reference sharing: if set then backend will share + * grant references to the buffer with the frontend. + */ + int be_alloc; +}; + +int xen_front_pgdir_shbuf_alloc(struct xen_front_pgdir_shbuf_cfg *cfg); + +grant_ref_t +xen_front_pgdir_shbuf_get_dir_start(struct xen_front_pgdir_shbuf *buf); + +int xen_front_pgdir_shbuf_map(struct xen_front_pgdir_shbuf *buf); + +int xen_front_pgdir_shbuf_unmap(struct xen_front_pgdir_shbuf *buf); + +void xen_front_pgdir_shbuf_free(struct xen_front_pgdir_shbuf *buf); + +#endif /* __XEN_FRONT_PGDIR_SHBUF_H_ */ -- cgit v1.2.3 From 5641f19bdfc4193f1c2d5a829896c25dd5c0ec3d Mon Sep 17 00:00:00 2001 From: Oleksandr Andrushchenko Date: Fri, 30 Nov 2018 09:42:04 +0200 Subject: drm/xen-front: Use Xen common shared buffer implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use page directory based shared buffer implementation now available as common code for Xen frontend drivers. Remove flushing of shared buffer on page flip as this workaround needs a proper fix. Signed-off-by: Oleksandr Andrushchenko Reviewed-by: Noralf Trønnes Signed-off-by: Boris Ostrovsky --- drivers/gpu/drm/xen/Kconfig | 1 + drivers/gpu/drm/xen/Makefile | 1 - drivers/gpu/drm/xen/xen_drm_front.c | 65 ++--- drivers/gpu/drm/xen/xen_drm_front_gem.c | 1 - drivers/gpu/drm/xen/xen_drm_front_shbuf.c | 414 ------------------------------ drivers/gpu/drm/xen/xen_drm_front_shbuf.h | 64 ----- 6 files changed, 26 insertions(+), 520 deletions(-) delete mode 100644 drivers/gpu/drm/xen/xen_drm_front_shbuf.c delete mode 100644 drivers/gpu/drm/xen/xen_drm_front_shbuf.h diff --git a/drivers/gpu/drm/xen/Kconfig b/drivers/gpu/drm/xen/Kconfig index 4cca160782ab..f969d486855d 100644 --- a/drivers/gpu/drm/xen/Kconfig +++ b/drivers/gpu/drm/xen/Kconfig @@ -12,6 +12,7 @@ config DRM_XEN_FRONTEND select DRM_KMS_HELPER select VIDEOMODE_HELPERS select XEN_XENBUS_FRONTEND + select XEN_FRONT_PGDIR_SHBUF help Choose this option if you want to enable a para-virtualized frontend DRM/KMS driver for Xen guest OSes. diff --git a/drivers/gpu/drm/xen/Makefile b/drivers/gpu/drm/xen/Makefile index 712afff5ffc3..825905f67faa 100644 --- a/drivers/gpu/drm/xen/Makefile +++ b/drivers/gpu/drm/xen/Makefile @@ -4,7 +4,6 @@ drm_xen_front-objs := xen_drm_front.o \ xen_drm_front_kms.o \ xen_drm_front_conn.o \ xen_drm_front_evtchnl.o \ - xen_drm_front_shbuf.o \ xen_drm_front_cfg.o \ xen_drm_front_gem.o diff --git a/drivers/gpu/drm/xen/xen_drm_front.c b/drivers/gpu/drm/xen/xen_drm_front.c index 6b6d5ab82ec3..4d3d36fc3a5d 100644 --- a/drivers/gpu/drm/xen/xen_drm_front.c +++ b/drivers/gpu/drm/xen/xen_drm_front.c @@ -19,6 +19,7 @@ #include #include +#include #include #include "xen_drm_front.h" @@ -26,28 +27,20 @@ #include "xen_drm_front_evtchnl.h" #include "xen_drm_front_gem.h" #include "xen_drm_front_kms.h" -#include "xen_drm_front_shbuf.h" struct xen_drm_front_dbuf { struct list_head list; u64 dbuf_cookie; u64 fb_cookie; - struct xen_drm_front_shbuf *shbuf; + + struct xen_front_pgdir_shbuf shbuf; }; -static int dbuf_add_to_list(struct xen_drm_front_info *front_info, - struct xen_drm_front_shbuf *shbuf, u64 dbuf_cookie) +static void dbuf_add_to_list(struct xen_drm_front_info *front_info, + struct xen_drm_front_dbuf *dbuf, u64 dbuf_cookie) { - struct xen_drm_front_dbuf *dbuf; - - dbuf = kzalloc(sizeof(*dbuf), GFP_KERNEL); - if (!dbuf) - return -ENOMEM; - dbuf->dbuf_cookie = dbuf_cookie; - dbuf->shbuf = shbuf; list_add(&dbuf->list, &front_info->dbuf_list); - return 0; } static struct xen_drm_front_dbuf *dbuf_get(struct list_head *dbuf_list, @@ -62,15 +55,6 @@ static struct xen_drm_front_dbuf *dbuf_get(struct list_head *dbuf_list, return NULL; } -static void dbuf_flush_fb(struct list_head *dbuf_list, u64 fb_cookie) -{ - struct xen_drm_front_dbuf *buf, *q; - - list_for_each_entry_safe(buf, q, dbuf_list, list) - if (buf->fb_cookie == fb_cookie) - xen_drm_front_shbuf_flush(buf->shbuf); -} - static void dbuf_free(struct list_head *dbuf_list, u64 dbuf_cookie) { struct xen_drm_front_dbuf *buf, *q; @@ -78,8 +62,8 @@ static void dbuf_free(struct list_head *dbuf_list, u64 dbuf_cookie) list_for_each_entry_safe(buf, q, dbuf_list, list) if (buf->dbuf_cookie == dbuf_cookie) { list_del(&buf->list); - xen_drm_front_shbuf_unmap(buf->shbuf); - xen_drm_front_shbuf_free(buf->shbuf); + xen_front_pgdir_shbuf_unmap(&buf->shbuf); + xen_front_pgdir_shbuf_free(&buf->shbuf); kfree(buf); break; } @@ -91,8 +75,8 @@ static void dbuf_free_all(struct list_head *dbuf_list) list_for_each_entry_safe(buf, q, dbuf_list, list) { list_del(&buf->list); - xen_drm_front_shbuf_unmap(buf->shbuf); - xen_drm_front_shbuf_free(buf->shbuf); + xen_front_pgdir_shbuf_unmap(&buf->shbuf); + xen_front_pgdir_shbuf_free(&buf->shbuf); kfree(buf); } } @@ -171,9 +155,9 @@ int xen_drm_front_dbuf_create(struct xen_drm_front_info *front_info, u32 bpp, u64 size, struct page **pages) { struct xen_drm_front_evtchnl *evtchnl; - struct xen_drm_front_shbuf *shbuf; + struct xen_drm_front_dbuf *dbuf; struct xendispl_req *req; - struct xen_drm_front_shbuf_cfg buf_cfg; + struct xen_front_pgdir_shbuf_cfg buf_cfg; unsigned long flags; int ret; @@ -181,28 +165,29 @@ int xen_drm_front_dbuf_create(struct xen_drm_front_info *front_info, if (unlikely(!evtchnl)) return -EIO; + dbuf = kzalloc(sizeof(*dbuf), GFP_KERNEL); + if (!dbuf) + return -ENOMEM; + + dbuf_add_to_list(front_info, dbuf, dbuf_cookie); + memset(&buf_cfg, 0, sizeof(buf_cfg)); buf_cfg.xb_dev = front_info->xb_dev; + buf_cfg.num_pages = DIV_ROUND_UP(size, PAGE_SIZE); buf_cfg.pages = pages; - buf_cfg.size = size; + buf_cfg.pgdir = &dbuf->shbuf; buf_cfg.be_alloc = front_info->cfg.be_alloc; - shbuf = xen_drm_front_shbuf_alloc(&buf_cfg); - if (IS_ERR(shbuf)) - return PTR_ERR(shbuf); - - ret = dbuf_add_to_list(front_info, shbuf, dbuf_cookie); - if (ret < 0) { - xen_drm_front_shbuf_free(shbuf); - return ret; - } + ret = xen_front_pgdir_shbuf_alloc(&buf_cfg); + if (ret < 0) + goto fail_shbuf_alloc; mutex_lock(&evtchnl->u.req.req_io_lock); spin_lock_irqsave(&front_info->io_lock, flags); req = be_prepare_req(evtchnl, XENDISPL_OP_DBUF_CREATE); req->op.dbuf_create.gref_directory = - xen_drm_front_shbuf_get_dir_start(shbuf); + xen_front_pgdir_shbuf_get_dir_start(&dbuf->shbuf); req->op.dbuf_create.buffer_sz = size; req->op.dbuf_create.dbuf_cookie = dbuf_cookie; req->op.dbuf_create.width = width; @@ -221,7 +206,7 @@ int xen_drm_front_dbuf_create(struct xen_drm_front_info *front_info, if (ret < 0) goto fail; - ret = xen_drm_front_shbuf_map(shbuf); + ret = xen_front_pgdir_shbuf_map(&dbuf->shbuf); if (ret < 0) goto fail; @@ -230,6 +215,7 @@ int xen_drm_front_dbuf_create(struct xen_drm_front_info *front_info, fail: mutex_unlock(&evtchnl->u.req.req_io_lock); +fail_shbuf_alloc: dbuf_free(&front_info->dbuf_list, dbuf_cookie); return ret; } @@ -358,7 +344,6 @@ int xen_drm_front_page_flip(struct xen_drm_front_info *front_info, if (unlikely(conn_idx >= front_info->num_evt_pairs)) return -EINVAL; - dbuf_flush_fb(&front_info->dbuf_list, fb_cookie); evtchnl = &front_info->evt_pairs[conn_idx].req; mutex_lock(&evtchnl->u.req.req_io_lock); diff --git a/drivers/gpu/drm/xen/xen_drm_front_gem.c b/drivers/gpu/drm/xen/xen_drm_front_gem.c index 47ff019d3aef..28bc501af450 100644 --- a/drivers/gpu/drm/xen/xen_drm_front_gem.c +++ b/drivers/gpu/drm/xen/xen_drm_front_gem.c @@ -22,7 +22,6 @@ #include #include "xen_drm_front.h" -#include "xen_drm_front_shbuf.h" struct xen_gem_object { struct drm_gem_object base; diff --git a/drivers/gpu/drm/xen/xen_drm_front_shbuf.c b/drivers/gpu/drm/xen/xen_drm_front_shbuf.c deleted file mode 100644 index d333b67cc1a0..000000000000 --- a/drivers/gpu/drm/xen/xen_drm_front_shbuf.c +++ /dev/null @@ -1,414 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 OR MIT - -/* - * Xen para-virtual DRM device - * - * Copyright (C) 2016-2018 EPAM Systems Inc. - * - * Author: Oleksandr Andrushchenko - */ - -#include - -#if defined(CONFIG_X86) -#include -#endif -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "xen_drm_front.h" -#include "xen_drm_front_shbuf.h" - -struct xen_drm_front_shbuf_ops { - /* - * Calculate number of grefs required to handle this buffer, - * e.g. if grefs are required for page directory only or the buffer - * pages as well. - */ - void (*calc_num_grefs)(struct xen_drm_front_shbuf *buf); - /* Fill page directory according to para-virtual display protocol. */ - void (*fill_page_dir)(struct xen_drm_front_shbuf *buf); - /* Claim grant references for the pages of the buffer. */ - int (*grant_refs_for_buffer)(struct xen_drm_front_shbuf *buf, - grant_ref_t *priv_gref_head, int gref_idx); - /* Map grant references of the buffer. */ - int (*map)(struct xen_drm_front_shbuf *buf); - /* Unmap grant references of the buffer. */ - int (*unmap)(struct xen_drm_front_shbuf *buf); -}; - -grant_ref_t xen_drm_front_shbuf_get_dir_start(struct xen_drm_front_shbuf *buf) -{ - if (!buf->grefs) - return GRANT_INVALID_REF; - - return buf->grefs[0]; -} - -int xen_drm_front_shbuf_map(struct xen_drm_front_shbuf *buf) -{ - if (buf->ops->map) - return buf->ops->map(buf); - - /* no need to map own grant references */ - return 0; -} - -int xen_drm_front_shbuf_unmap(struct xen_drm_front_shbuf *buf) -{ - if (buf->ops->unmap) - return buf->ops->unmap(buf); - - /* no need to unmap own grant references */ - return 0; -} - -void xen_drm_front_shbuf_flush(struct xen_drm_front_shbuf *buf) -{ -#if defined(CONFIG_X86) - drm_clflush_pages(buf->pages, buf->num_pages); -#endif -} - -void xen_drm_front_shbuf_free(struct xen_drm_front_shbuf *buf) -{ - if (buf->grefs) { - int i; - - for (i = 0; i < buf->num_grefs; i++) - if (buf->grefs[i] != GRANT_INVALID_REF) - gnttab_end_foreign_access(buf->grefs[i], - 0, 0UL); - } - kfree(buf->grefs); - kfree(buf->directory); - kfree(buf); -} - -/* - * number of grefs a page can hold with respect to the - * struct xendispl_page_directory header - */ -#define XEN_DRM_NUM_GREFS_PER_PAGE ((PAGE_SIZE - \ - offsetof(struct xendispl_page_directory, gref)) / \ - sizeof(grant_ref_t)) - -static int get_num_pages_dir(struct xen_drm_front_shbuf *buf) -{ - /* number of pages the page directory consumes itself */ - return DIV_ROUND_UP(buf->num_pages, XEN_DRM_NUM_GREFS_PER_PAGE); -} - -static void backend_calc_num_grefs(struct xen_drm_front_shbuf *buf) -{ - /* only for pages the page directory consumes itself */ - buf->num_grefs = get_num_pages_dir(buf); -} - -static void guest_calc_num_grefs(struct xen_drm_front_shbuf *buf) -{ - /* - * number of pages the page directory consumes itself - * plus grefs for the buffer pages - */ - buf->num_grefs = get_num_pages_dir(buf) + buf->num_pages; -} - -#define xen_page_to_vaddr(page) \ - ((uintptr_t)pfn_to_kaddr(page_to_xen_pfn(page))) - -static int backend_unmap(struct xen_drm_front_shbuf *buf) -{ - struct gnttab_unmap_grant_ref *unmap_ops; - int i, ret; - - if (!buf->pages || !buf->backend_map_handles || !buf->grefs) - return 0; - - unmap_ops = kcalloc(buf->num_pages, sizeof(*unmap_ops), - GFP_KERNEL); - if (!unmap_ops) { - DRM_ERROR("Failed to get memory while unmapping\n"); - return -ENOMEM; - } - - for (i = 0; i < buf->num_pages; i++) { - phys_addr_t addr; - - addr = xen_page_to_vaddr(buf->pages[i]); - gnttab_set_unmap_op(&unmap_ops[i], addr, GNTMAP_host_map, - buf->backend_map_handles[i]); - } - - ret = gnttab_unmap_refs(unmap_ops, NULL, buf->pages, - buf->num_pages); - - for (i = 0; i < buf->num_pages; i++) { - if (unlikely(unmap_ops[i].status != GNTST_okay)) - DRM_ERROR("Failed to unmap page %d: %d\n", - i, unmap_ops[i].status); - } - - if (ret) - DRM_ERROR("Failed to unmap grant references, ret %d", ret); - - kfree(unmap_ops); - kfree(buf->backend_map_handles); - buf->backend_map_handles = NULL; - return ret; -} - -static int backend_map(struct xen_drm_front_shbuf *buf) -{ - struct gnttab_map_grant_ref *map_ops = NULL; - unsigned char *ptr; - int ret, cur_gref, cur_dir_page, cur_page, grefs_left; - - map_ops = kcalloc(buf->num_pages, sizeof(*map_ops), GFP_KERNEL); - if (!map_ops) - return -ENOMEM; - - buf->backend_map_handles = kcalloc(buf->num_pages, - sizeof(*buf->backend_map_handles), - GFP_KERNEL); - if (!buf->backend_map_handles) { - kfree(map_ops); - return -ENOMEM; - } - - /* - * read page directory to get grefs from the backend: for external - * buffer we only allocate buf->grefs for the page directory, - * so buf->num_grefs has number of pages in the page directory itself - */ - ptr = buf->directory; - grefs_left = buf->num_pages; - cur_page = 0; - for (cur_dir_page = 0; cur_dir_page < buf->num_grefs; cur_dir_page++) { - struct xendispl_page_directory *page_dir = - (struct xendispl_page_directory *)ptr; - int to_copy = XEN_DRM_NUM_GREFS_PER_PAGE; - - if (to_copy > grefs_left) - to_copy = grefs_left; - - for (cur_gref = 0; cur_gref < to_copy; cur_gref++) { - phys_addr_t addr; - - addr = xen_page_to_vaddr(buf->pages[cur_page]); - gnttab_set_map_op(&map_ops[cur_page], addr, - GNTMAP_host_map, - page_dir->gref[cur_gref], - buf->xb_dev->otherend_id); - cur_page++; - } - - grefs_left -= to_copy; - ptr += PAGE_SIZE; - } - ret = gnttab_map_refs(map_ops, NULL, buf->pages, buf->num_pages); - - /* save handles even if error, so we can unmap */ - for (cur_page = 0; cur_page < buf->num_pages; cur_page++) { - buf->backend_map_handles[cur_page] = map_ops[cur_page].handle; - if (unlikely(map_ops[cur_page].status != GNTST_okay)) - DRM_ERROR("Failed to map page %d: %d\n", - cur_page, map_ops[cur_page].status); - } - - if (ret) { - DRM_ERROR("Failed to map grant references, ret %d", ret); - backend_unmap(buf); - } - - kfree(map_ops); - return ret; -} - -static void backend_fill_page_dir(struct xen_drm_front_shbuf *buf) -{ - struct xendispl_page_directory *page_dir; - unsigned char *ptr; - int i, num_pages_dir; - - ptr = buf->directory; - num_pages_dir = get_num_pages_dir(buf); - - /* fill only grefs for the page directory itself */ - for (i = 0; i < num_pages_dir - 1; i++) { - page_dir = (struct xendispl_page_directory *)ptr; - - page_dir->gref_dir_next_page = buf->grefs[i + 1]; - ptr += PAGE_SIZE; - } - /* last page must say there is no more pages */ - page_dir = (struct xendispl_page_directory *)ptr; - page_dir->gref_dir_next_page = GRANT_INVALID_REF; -} - -static void guest_fill_page_dir(struct xen_drm_front_shbuf *buf) -{ - unsigned char *ptr; - int cur_gref, grefs_left, to_copy, i, num_pages_dir; - - ptr = buf->directory; - num_pages_dir = get_num_pages_dir(buf); - - /* - * while copying, skip grefs at start, they are for pages - * granted for the page directory itself - */ - cur_gref = num_pages_dir; - grefs_left = buf->num_pages; - for (i = 0; i < num_pages_dir; i++) { - struct xendispl_page_directory *page_dir = - (struct xendispl_page_directory *)ptr; - - if (grefs_left <= XEN_DRM_NUM_GREFS_PER_PAGE) { - to_copy = grefs_left; - page_dir->gref_dir_next_page = GRANT_INVALID_REF; - } else { - to_copy = XEN_DRM_NUM_GREFS_PER_PAGE; - page_dir->gref_dir_next_page = buf->grefs[i + 1]; - } - memcpy(&page_dir->gref, &buf->grefs[cur_gref], - to_copy * sizeof(grant_ref_t)); - ptr += PAGE_SIZE; - grefs_left -= to_copy; - cur_gref += to_copy; - } -} - -static int guest_grant_refs_for_buffer(struct xen_drm_front_shbuf *buf, - grant_ref_t *priv_gref_head, - int gref_idx) -{ - int i, cur_ref, otherend_id; - - otherend_id = buf->xb_dev->otherend_id; - for (i = 0; i < buf->num_pages; i++) { - cur_ref = gnttab_claim_grant_reference(priv_gref_head); - if (cur_ref < 0) - return cur_ref; - - gnttab_grant_foreign_access_ref(cur_ref, otherend_id, - xen_page_to_gfn(buf->pages[i]), - 0); - buf->grefs[gref_idx++] = cur_ref; - } - return 0; -} - -static int grant_references(struct xen_drm_front_shbuf *buf) -{ - grant_ref_t priv_gref_head; - int ret, i, j, cur_ref; - int otherend_id, num_pages_dir; - - ret = gnttab_alloc_grant_references(buf->num_grefs, &priv_gref_head); - if (ret < 0) { - DRM_ERROR("Cannot allocate grant references\n"); - return ret; - } - - otherend_id = buf->xb_dev->otherend_id; - j = 0; - num_pages_dir = get_num_pages_dir(buf); - for (i = 0; i < num_pages_dir; i++) { - unsigned long frame; - - cur_ref = gnttab_claim_grant_reference(&priv_gref_head); - if (cur_ref < 0) - return cur_ref; - - frame = xen_page_to_gfn(virt_to_page(buf->directory + - PAGE_SIZE * i)); - gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0); - buf->grefs[j++] = cur_ref; - } - - if (buf->ops->grant_refs_for_buffer) { - ret = buf->ops->grant_refs_for_buffer(buf, &priv_gref_head, j); - if (ret) - return ret; - } - - gnttab_free_grant_references(priv_gref_head); - return 0; -} - -static int alloc_storage(struct xen_drm_front_shbuf *buf) -{ - buf->grefs = kcalloc(buf->num_grefs, sizeof(*buf->grefs), GFP_KERNEL); - if (!buf->grefs) - return -ENOMEM; - - buf->directory = kcalloc(get_num_pages_dir(buf), PAGE_SIZE, GFP_KERNEL); - if (!buf->directory) - return -ENOMEM; - - return 0; -} - -/* - * For be allocated buffers we don't need grant_refs_for_buffer as those - * grant references are allocated at backend side - */ -static const struct xen_drm_front_shbuf_ops backend_ops = { - .calc_num_grefs = backend_calc_num_grefs, - .fill_page_dir = backend_fill_page_dir, - .map = backend_map, - .unmap = backend_unmap -}; - -/* For locally granted references we do not need to map/unmap the references */ -static const struct xen_drm_front_shbuf_ops local_ops = { - .calc_num_grefs = guest_calc_num_grefs, - .fill_page_dir = guest_fill_page_dir, - .grant_refs_for_buffer = guest_grant_refs_for_buffer, -}; - -struct xen_drm_front_shbuf * -xen_drm_front_shbuf_alloc(struct xen_drm_front_shbuf_cfg *cfg) -{ - struct xen_drm_front_shbuf *buf; - int ret; - - buf = kzalloc(sizeof(*buf), GFP_KERNEL); - if (!buf) - return ERR_PTR(-ENOMEM); - - if (cfg->be_alloc) - buf->ops = &backend_ops; - else - buf->ops = &local_ops; - - buf->xb_dev = cfg->xb_dev; - buf->num_pages = DIV_ROUND_UP(cfg->size, PAGE_SIZE); - buf->pages = cfg->pages; - - buf->ops->calc_num_grefs(buf); - - ret = alloc_storage(buf); - if (ret) - goto fail; - - ret = grant_references(buf); - if (ret) - goto fail; - - buf->ops->fill_page_dir(buf); - - return buf; - -fail: - xen_drm_front_shbuf_free(buf); - return ERR_PTR(ret); -} diff --git a/drivers/gpu/drm/xen/xen_drm_front_shbuf.h b/drivers/gpu/drm/xen/xen_drm_front_shbuf.h deleted file mode 100644 index 7545c692539e..000000000000 --- a/drivers/gpu/drm/xen/xen_drm_front_shbuf.h +++ /dev/null @@ -1,64 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 OR MIT */ - -/* - * Xen para-virtual DRM device - * - * Copyright (C) 2016-2018 EPAM Systems Inc. - * - * Author: Oleksandr Andrushchenko - */ - -#ifndef __XEN_DRM_FRONT_SHBUF_H_ -#define __XEN_DRM_FRONT_SHBUF_H_ - -#include -#include - -#include - -struct xen_drm_front_shbuf { - /* - * number of references granted for the backend use: - * - for allocated/imported dma-buf's this holds number of grant - * references for the page directory and pages of the buffer - * - for the buffer provided by the backend this holds number of - * grant references for the page directory as grant references for - * the buffer will be provided by the backend - */ - int num_grefs; - grant_ref_t *grefs; - unsigned char *directory; - - int num_pages; - struct page **pages; - - struct xenbus_device *xb_dev; - - /* these are the ops used internally depending on be_alloc mode */ - const struct xen_drm_front_shbuf_ops *ops; - - /* Xen map handles for the buffer allocated by the backend */ - grant_handle_t *backend_map_handles; -}; - -struct xen_drm_front_shbuf_cfg { - struct xenbus_device *xb_dev; - size_t size; - struct page **pages; - bool be_alloc; -}; - -struct xen_drm_front_shbuf * -xen_drm_front_shbuf_alloc(struct xen_drm_front_shbuf_cfg *cfg); - -grant_ref_t xen_drm_front_shbuf_get_dir_start(struct xen_drm_front_shbuf *buf); - -int xen_drm_front_shbuf_map(struct xen_drm_front_shbuf *buf); - -int xen_drm_front_shbuf_unmap(struct xen_drm_front_shbuf *buf); - -void xen_drm_front_shbuf_flush(struct xen_drm_front_shbuf *buf); - -void xen_drm_front_shbuf_free(struct xen_drm_front_shbuf *buf); - -#endif /* __XEN_DRM_FRONT_SHBUF_H_ */ -- cgit v1.2.3 From 58f9d806d16a38ed3bae11e2ada83393436a4956 Mon Sep 17 00:00:00 2001 From: Oleksandr Andrushchenko Date: Fri, 30 Nov 2018 09:42:05 +0200 Subject: ALSA: xen-front: Use Xen common shared buffer implementation Use page directory based shared buffer implementation now available as common code for Xen frontend drivers. Signed-off-by: Oleksandr Andrushchenko Reviewed-by: Takashi Iwai Signed-off-by: Boris Ostrovsky --- sound/xen/Kconfig | 1 + sound/xen/Makefile | 1 - sound/xen/xen_snd_front.c | 7 +- sound/xen/xen_snd_front.h | 4 +- sound/xen/xen_snd_front_alsa.c | 102 +++++++++++++++------ sound/xen/xen_snd_front_shbuf.c | 194 ---------------------------------------- sound/xen/xen_snd_front_shbuf.h | 36 -------- 7 files changed, 84 insertions(+), 261 deletions(-) delete mode 100644 sound/xen/xen_snd_front_shbuf.c delete mode 100644 sound/xen/xen_snd_front_shbuf.h diff --git a/sound/xen/Kconfig b/sound/xen/Kconfig index 4f1fceea82d2..e4d7beb4df1c 100644 --- a/sound/xen/Kconfig +++ b/sound/xen/Kconfig @@ -5,6 +5,7 @@ config SND_XEN_FRONTEND depends on XEN select SND_PCM select XEN_XENBUS_FRONTEND + select XEN_FRONT_PGDIR_SHBUF help Choose this option if you want to enable a para-virtualized frontend sound driver for Xen guest OSes. diff --git a/sound/xen/Makefile b/sound/xen/Makefile index 1e6470ecc2f2..24031775b715 100644 --- a/sound/xen/Makefile +++ b/sound/xen/Makefile @@ -3,7 +3,6 @@ snd_xen_front-objs := xen_snd_front.o \ xen_snd_front_cfg.o \ xen_snd_front_evtchnl.o \ - xen_snd_front_shbuf.o \ xen_snd_front_alsa.o obj-$(CONFIG_SND_XEN_FRONTEND) += snd_xen_front.o diff --git a/sound/xen/xen_snd_front.c b/sound/xen/xen_snd_front.c index b089b13b5160..a9e5c2cd7698 100644 --- a/sound/xen/xen_snd_front.c +++ b/sound/xen/xen_snd_front.c @@ -16,12 +16,12 @@ #include #include +#include #include #include "xen_snd_front.h" #include "xen_snd_front_alsa.h" #include "xen_snd_front_evtchnl.h" -#include "xen_snd_front_shbuf.h" static struct xensnd_req * be_stream_prepare_req(struct xen_snd_front_evtchnl *evtchnl, u8 operation) @@ -82,7 +82,7 @@ int xen_snd_front_stream_query_hw_param(struct xen_snd_front_evtchnl *evtchnl, } int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl, - struct xen_snd_front_shbuf *sh_buf, + struct xen_front_pgdir_shbuf *shbuf, u8 format, unsigned int channels, unsigned int rate, u32 buffer_sz, u32 period_sz) @@ -99,7 +99,8 @@ int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl, req->op.open.pcm_rate = rate; req->op.open.buffer_sz = buffer_sz; req->op.open.period_sz = period_sz; - req->op.open.gref_directory = xen_snd_front_shbuf_get_dir_start(sh_buf); + req->op.open.gref_directory = + xen_front_pgdir_shbuf_get_dir_start(shbuf); mutex_unlock(&evtchnl->ring_io_lock); ret = be_stream_do_io(evtchnl); diff --git a/sound/xen/xen_snd_front.h b/sound/xen/xen_snd_front.h index a2ea2463bcc5..05611f113b94 100644 --- a/sound/xen/xen_snd_front.h +++ b/sound/xen/xen_snd_front.h @@ -16,7 +16,7 @@ struct xen_snd_front_card_info; struct xen_snd_front_evtchnl; struct xen_snd_front_evtchnl_pair; -struct xen_snd_front_shbuf; +struct xen_front_pgdir_shbuf; struct xensnd_query_hw_param; struct xen_snd_front_info { @@ -35,7 +35,7 @@ int xen_snd_front_stream_query_hw_param(struct xen_snd_front_evtchnl *evtchnl, struct xensnd_query_hw_param *hw_param_resp); int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl, - struct xen_snd_front_shbuf *sh_buf, + struct xen_front_pgdir_shbuf *shbuf, u8 format, unsigned int channels, unsigned int rate, u32 buffer_sz, u32 period_sz); diff --git a/sound/xen/xen_snd_front_alsa.c b/sound/xen/xen_snd_front_alsa.c index 2cbd9679aca1..a7f413cb704d 100644 --- a/sound/xen/xen_snd_front_alsa.c +++ b/sound/xen/xen_snd_front_alsa.c @@ -15,17 +15,24 @@ #include #include +#include #include "xen_snd_front.h" #include "xen_snd_front_alsa.h" #include "xen_snd_front_cfg.h" #include "xen_snd_front_evtchnl.h" -#include "xen_snd_front_shbuf.h" struct xen_snd_front_pcm_stream_info { struct xen_snd_front_info *front_info; struct xen_snd_front_evtchnl_pair *evt_pair; - struct xen_snd_front_shbuf sh_buf; + + /* This is the shared buffer with its backing storage. */ + struct xen_front_pgdir_shbuf shbuf; + u8 *buffer; + size_t buffer_sz; + int num_pages; + struct page **pages; + int index; bool is_open; @@ -214,12 +221,20 @@ static void stream_clear(struct xen_snd_front_pcm_stream_info *stream) stream->out_frames = 0; atomic_set(&stream->hw_ptr, 0); xen_snd_front_evtchnl_pair_clear(stream->evt_pair); - xen_snd_front_shbuf_clear(&stream->sh_buf); + memset(&stream->shbuf, 0, sizeof(stream->shbuf)); + stream->buffer = NULL; + stream->buffer_sz = 0; + stream->pages = NULL; + stream->num_pages = 0; } static void stream_free(struct xen_snd_front_pcm_stream_info *stream) { - xen_snd_front_shbuf_free(&stream->sh_buf); + xen_front_pgdir_shbuf_unmap(&stream->shbuf); + xen_front_pgdir_shbuf_free(&stream->shbuf); + if (stream->buffer) + free_pages_exact(stream->buffer, stream->buffer_sz); + kfree(stream->pages); stream_clear(stream); } @@ -421,10 +436,34 @@ static int alsa_close(struct snd_pcm_substream *substream) return 0; } +static int shbuf_setup_backstore(struct xen_snd_front_pcm_stream_info *stream, + size_t buffer_sz) +{ + int i; + + stream->buffer = alloc_pages_exact(stream->buffer_sz, GFP_KERNEL); + if (!stream->buffer) + return -ENOMEM; + + stream->buffer_sz = buffer_sz; + stream->num_pages = DIV_ROUND_UP(stream->buffer_sz, PAGE_SIZE); + stream->pages = kcalloc(stream->num_pages, sizeof(struct page *), + GFP_KERNEL); + if (!stream->pages) + return -ENOMEM; + + for (i = 0; i < stream->num_pages; i++) + stream->pages[i] = virt_to_page(stream->buffer + i * PAGE_SIZE); + + return 0; +} + static int alsa_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); + struct xen_snd_front_info *front_info = stream->front_info; + struct xen_front_pgdir_shbuf_cfg buf_cfg; int ret; /* @@ -432,19 +471,32 @@ static int alsa_hw_params(struct snd_pcm_substream *substream, * so free the previously allocated shared buffer if any. */ stream_free(stream); + ret = shbuf_setup_backstore(stream, params_buffer_bytes(params)); + if (ret < 0) + goto fail; - ret = xen_snd_front_shbuf_alloc(stream->front_info->xb_dev, - &stream->sh_buf, - params_buffer_bytes(params)); - if (ret < 0) { - stream_free(stream); - dev_err(&stream->front_info->xb_dev->dev, - "Failed to allocate buffers for stream with index %d\n", - stream->index); - return ret; - } + memset(&buf_cfg, 0, sizeof(buf_cfg)); + buf_cfg.xb_dev = front_info->xb_dev; + buf_cfg.pgdir = &stream->shbuf; + buf_cfg.num_pages = stream->num_pages; + buf_cfg.pages = stream->pages; + + ret = xen_front_pgdir_shbuf_alloc(&buf_cfg); + if (ret < 0) + goto fail; + + ret = xen_front_pgdir_shbuf_map(&stream->shbuf); + if (ret < 0) + goto fail; return 0; + +fail: + stream_free(stream); + dev_err(&front_info->xb_dev->dev, + "Failed to allocate buffers for stream with index %d\n", + stream->index); + return ret; } static int alsa_hw_free(struct snd_pcm_substream *substream) @@ -476,7 +528,7 @@ static int alsa_prepare(struct snd_pcm_substream *substream) sndif_format = ret; ret = xen_snd_front_stream_prepare(&stream->evt_pair->req, - &stream->sh_buf, + &stream->shbuf, sndif_format, runtime->channels, runtime->rate, @@ -556,10 +608,10 @@ static int alsa_pb_copy_user(struct snd_pcm_substream *substream, { struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); - if (unlikely(pos + count > stream->sh_buf.buffer_sz)) + if (unlikely(pos + count > stream->buffer_sz)) return -EINVAL; - if (copy_from_user(stream->sh_buf.buffer + pos, src, count)) + if (copy_from_user(stream->buffer + pos, src, count)) return -EFAULT; return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count); @@ -571,10 +623,10 @@ static int alsa_pb_copy_kernel(struct snd_pcm_substream *substream, { struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); - if (unlikely(pos + count > stream->sh_buf.buffer_sz)) + if (unlikely(pos + count > stream->buffer_sz)) return -EINVAL; - memcpy(stream->sh_buf.buffer + pos, src, count); + memcpy(stream->buffer + pos, src, count); return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count); } @@ -586,14 +638,14 @@ static int alsa_cap_copy_user(struct snd_pcm_substream *substream, struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); int ret; - if (unlikely(pos + count > stream->sh_buf.buffer_sz)) + if (unlikely(pos + count > stream->buffer_sz)) return -EINVAL; ret = xen_snd_front_stream_read(&stream->evt_pair->req, pos, count); if (ret < 0) return ret; - return copy_to_user(dst, stream->sh_buf.buffer + pos, count) ? + return copy_to_user(dst, stream->buffer + pos, count) ? -EFAULT : 0; } @@ -604,14 +656,14 @@ static int alsa_cap_copy_kernel(struct snd_pcm_substream *substream, struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); int ret; - if (unlikely(pos + count > stream->sh_buf.buffer_sz)) + if (unlikely(pos + count > stream->buffer_sz)) return -EINVAL; ret = xen_snd_front_stream_read(&stream->evt_pair->req, pos, count); if (ret < 0) return ret; - memcpy(dst, stream->sh_buf.buffer + pos, count); + memcpy(dst, stream->buffer + pos, count); return 0; } @@ -622,10 +674,10 @@ static int alsa_pb_fill_silence(struct snd_pcm_substream *substream, { struct xen_snd_front_pcm_stream_info *stream = stream_get(substream); - if (unlikely(pos + count > stream->sh_buf.buffer_sz)) + if (unlikely(pos + count > stream->buffer_sz)) return -EINVAL; - memset(stream->sh_buf.buffer + pos, 0, count); + memset(stream->buffer + pos, 0, count); return xen_snd_front_stream_write(&stream->evt_pair->req, pos, count); } diff --git a/sound/xen/xen_snd_front_shbuf.c b/sound/xen/xen_snd_front_shbuf.c deleted file mode 100644 index 07ac176a41ba..000000000000 --- a/sound/xen/xen_snd_front_shbuf.c +++ /dev/null @@ -1,194 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 OR MIT - -/* - * Xen para-virtual sound device - * - * Copyright (C) 2016-2018 EPAM Systems Inc. - * - * Author: Oleksandr Andrushchenko - */ - -#include -#include -#include - -#include "xen_snd_front_shbuf.h" - -grant_ref_t xen_snd_front_shbuf_get_dir_start(struct xen_snd_front_shbuf *buf) -{ - if (!buf->grefs) - return GRANT_INVALID_REF; - - return buf->grefs[0]; -} - -void xen_snd_front_shbuf_clear(struct xen_snd_front_shbuf *buf) -{ - memset(buf, 0, sizeof(*buf)); -} - -void xen_snd_front_shbuf_free(struct xen_snd_front_shbuf *buf) -{ - int i; - - if (buf->grefs) { - for (i = 0; i < buf->num_grefs; i++) - if (buf->grefs[i] != GRANT_INVALID_REF) - gnttab_end_foreign_access(buf->grefs[i], - 0, 0UL); - kfree(buf->grefs); - } - kfree(buf->directory); - free_pages_exact(buf->buffer, buf->buffer_sz); - xen_snd_front_shbuf_clear(buf); -} - -/* - * number of grant references a page can hold with respect to the - * xensnd_page_directory header - */ -#define XENSND_NUM_GREFS_PER_PAGE ((XEN_PAGE_SIZE - \ - offsetof(struct xensnd_page_directory, gref)) / \ - sizeof(grant_ref_t)) - -static void fill_page_dir(struct xen_snd_front_shbuf *buf, - int num_pages_dir) -{ - struct xensnd_page_directory *page_dir; - unsigned char *ptr; - int i, cur_gref, grefs_left, to_copy; - - ptr = buf->directory; - grefs_left = buf->num_grefs - num_pages_dir; - /* - * skip grant references at the beginning, they are for pages granted - * for the page directory itself - */ - cur_gref = num_pages_dir; - for (i = 0; i < num_pages_dir; i++) { - page_dir = (struct xensnd_page_directory *)ptr; - if (grefs_left <= XENSND_NUM_GREFS_PER_PAGE) { - to_copy = grefs_left; - page_dir->gref_dir_next_page = GRANT_INVALID_REF; - } else { - to_copy = XENSND_NUM_GREFS_PER_PAGE; - page_dir->gref_dir_next_page = buf->grefs[i + 1]; - } - - memcpy(&page_dir->gref, &buf->grefs[cur_gref], - to_copy * sizeof(grant_ref_t)); - - ptr += XEN_PAGE_SIZE; - grefs_left -= to_copy; - cur_gref += to_copy; - } -} - -static int grant_references(struct xenbus_device *xb_dev, - struct xen_snd_front_shbuf *buf, - int num_pages_dir, int num_pages_buffer, - int num_grefs) -{ - grant_ref_t priv_gref_head; - unsigned long frame; - int ret, i, j, cur_ref; - int otherend_id; - - ret = gnttab_alloc_grant_references(num_grefs, &priv_gref_head); - if (ret) - return ret; - - buf->num_grefs = num_grefs; - otherend_id = xb_dev->otherend_id; - j = 0; - - for (i = 0; i < num_pages_dir; i++) { - cur_ref = gnttab_claim_grant_reference(&priv_gref_head); - if (cur_ref < 0) { - ret = cur_ref; - goto fail; - } - - frame = xen_page_to_gfn(virt_to_page(buf->directory + - XEN_PAGE_SIZE * i)); - gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0); - buf->grefs[j++] = cur_ref; - } - - for (i = 0; i < num_pages_buffer; i++) { - cur_ref = gnttab_claim_grant_reference(&priv_gref_head); - if (cur_ref < 0) { - ret = cur_ref; - goto fail; - } - - frame = xen_page_to_gfn(virt_to_page(buf->buffer + - XEN_PAGE_SIZE * i)); - gnttab_grant_foreign_access_ref(cur_ref, otherend_id, frame, 0); - buf->grefs[j++] = cur_ref; - } - - gnttab_free_grant_references(priv_gref_head); - fill_page_dir(buf, num_pages_dir); - return 0; - -fail: - gnttab_free_grant_references(priv_gref_head); - return ret; -} - -static int alloc_int_buffers(struct xen_snd_front_shbuf *buf, - int num_pages_dir, int num_pages_buffer, - int num_grefs) -{ - buf->grefs = kcalloc(num_grefs, sizeof(*buf->grefs), GFP_KERNEL); - if (!buf->grefs) - return -ENOMEM; - - buf->directory = kcalloc(num_pages_dir, XEN_PAGE_SIZE, GFP_KERNEL); - if (!buf->directory) - goto fail; - - buf->buffer_sz = num_pages_buffer * XEN_PAGE_SIZE; - buf->buffer = alloc_pages_exact(buf->buffer_sz, GFP_KERNEL); - if (!buf->buffer) - goto fail; - - return 0; - -fail: - kfree(buf->grefs); - buf->grefs = NULL; - kfree(buf->directory); - buf->directory = NULL; - return -ENOMEM; -} - -int xen_snd_front_shbuf_alloc(struct xenbus_device *xb_dev, - struct xen_snd_front_shbuf *buf, - unsigned int buffer_sz) -{ - int num_pages_buffer, num_pages_dir, num_grefs; - int ret; - - xen_snd_front_shbuf_clear(buf); - - num_pages_buffer = DIV_ROUND_UP(buffer_sz, XEN_PAGE_SIZE); - /* number of pages the page directory consumes itself */ - num_pages_dir = DIV_ROUND_UP(num_pages_buffer, - XENSND_NUM_GREFS_PER_PAGE); - num_grefs = num_pages_buffer + num_pages_dir; - - ret = alloc_int_buffers(buf, num_pages_dir, - num_pages_buffer, num_grefs); - if (ret < 0) - return ret; - - ret = grant_references(xb_dev, buf, num_pages_dir, num_pages_buffer, - num_grefs); - if (ret < 0) - return ret; - - fill_page_dir(buf, num_pages_dir); - return 0; -} diff --git a/sound/xen/xen_snd_front_shbuf.h b/sound/xen/xen_snd_front_shbuf.h deleted file mode 100644 index d28e97c47b2c..000000000000 --- a/sound/xen/xen_snd_front_shbuf.h +++ /dev/null @@ -1,36 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 OR MIT */ - -/* - * Xen para-virtual sound device - * - * Copyright (C) 2016-2018 EPAM Systems Inc. - * - * Author: Oleksandr Andrushchenko - */ - -#ifndef __XEN_SND_FRONT_SHBUF_H -#define __XEN_SND_FRONT_SHBUF_H - -#include - -#include "xen_snd_front_evtchnl.h" - -struct xen_snd_front_shbuf { - int num_grefs; - grant_ref_t *grefs; - u8 *directory; - u8 *buffer; - size_t buffer_sz; -}; - -grant_ref_t xen_snd_front_shbuf_get_dir_start(struct xen_snd_front_shbuf *buf); - -int xen_snd_front_shbuf_alloc(struct xenbus_device *xb_dev, - struct xen_snd_front_shbuf *buf, - unsigned int buffer_sz); - -void xen_snd_front_shbuf_clear(struct xen_snd_front_shbuf *buf); - -void xen_snd_front_shbuf_free(struct xen_snd_front_shbuf *buf); - -#endif /* __XEN_SND_FRONT_SHBUF_H */ -- cgit v1.2.3