Module Name: src Committed By: martin Date: Sun Aug 2 08:49:08 UTC 2020
Modified Files: src/sys/dev/nvmm [netbsd-9]: files.nvmm nvmm.c nvmm_internal.h src/sys/dev/nvmm/x86 [netbsd-9]: nvmm_x86_svm.c nvmm_x86_vmx.c src/sys/modules/nvmm [netbsd-9]: nvmm.ioconf Log Message: Pull up following revision(s) (requested by maxv in ticket #1032): sys/dev/nvmm/x86/nvmm_x86_vmx.c: revision 1.60 (patch) sys/dev/nvmm/x86/nvmm_x86_vmx.c: revision 1.61 (patch) sys/dev/nvmm/nvmm.c: revision 1.30 sys/dev/nvmm/nvmm.c: revision 1.31 sys/dev/nvmm/nvmm.c: revision 1.32 sys/dev/nvmm/nvmm_internal.h: revision 1.15 sys/dev/nvmm/nvmm_internal.h: revision 1.16 sys/dev/nvmm/files.nvmm: revision 1.3 sys/dev/nvmm/x86/nvmm_x86_svm.c: revision 1.62 (patch) sys/dev/nvmm/x86/nvmm_x86_svm.c: revision 1.63 (patch) sys/dev/nvmm/x86/nvmm_x86_vmx.c: revision 1.59 (patch) sys/modules/nvmm/nvmm.ioconf: revision 1.2 Gather the conditions to return from the VCPU loops in nvmm_return_needed(), and use it in nvmm_do_vcpu_run() as well. This fixes two undesired behaviors: - When a VM initializes, the many nested page faults that need processing could cause the calling thread to occupy the CPU too much if we're unlucky and are only getting repeated nested page faults thousands of times in a row. - When the emulator calls nvmm_vcpu_run() and immediately sends a signal to stop the VCPU, it's better to check signals earlier and leave right away, rather than doing a round of VCPU run that could increase the time spent by the emulator waiting for the return. style Register NVMM as an actual pseudo-device. Without PMF handler, to explicitly disallow ACPI suspend if NVMM is running. Should fix PR/55406. Print the backend name when attaching. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.2.6.1 src/sys/dev/nvmm/files.nvmm cvs rdiff -u -r1.22.2.4 -r1.22.2.5 src/sys/dev/nvmm/nvmm.c cvs rdiff -u -r1.12.2.2 -r1.12.2.3 src/sys/dev/nvmm/nvmm_internal.h cvs rdiff -u -r1.46.4.5 -r1.46.4.6 src/sys/dev/nvmm/x86/nvmm_x86_svm.c cvs rdiff -u -r1.36.2.7 -r1.36.2.8 src/sys/dev/nvmm/x86/nvmm_x86_vmx.c cvs rdiff -u -r1.1 -r1.1.8.1 src/sys/modules/nvmm/nvmm.ioconf Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/nvmm/files.nvmm diff -u src/sys/dev/nvmm/files.nvmm:1.2 src/sys/dev/nvmm/files.nvmm:1.2.6.1 --- src/sys/dev/nvmm/files.nvmm:1.2 Thu Mar 28 19:00:40 2019 +++ src/sys/dev/nvmm/files.nvmm Sun Aug 2 08:49:08 2020 @@ -1,6 +1,6 @@ -# $NetBSD: files.nvmm,v 1.2 2019/03/28 19:00:40 maxv Exp $ +# $NetBSD: files.nvmm,v 1.2.6.1 2020/08/02 08:49:08 martin Exp $ -defpseudo nvmm +defpseudodev nvmm file dev/nvmm/nvmm.c nvmm Index: src/sys/dev/nvmm/nvmm.c diff -u src/sys/dev/nvmm/nvmm.c:1.22.2.4 src/sys/dev/nvmm/nvmm.c:1.22.2.5 --- src/sys/dev/nvmm/nvmm.c:1.22.2.4 Thu May 21 10:52:58 2020 +++ src/sys/dev/nvmm/nvmm.c Sun Aug 2 08:49:08 2020 @@ -1,7 +1,7 @@ -/* $NetBSD: nvmm.c,v 1.22.2.4 2020/05/21 10:52:58 martin Exp $ */ +/* $NetBSD: nvmm.c,v 1.22.2.5 2020/08/02 08:49:08 martin Exp $ */ /* - * Copyright (c) 2018-2019 The NetBSD Foundation, Inc. + * Copyright (c) 2018-2020 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.22.2.4 2020/05/21 10:52:58 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.22.2.5 2020/08/02 08:49:08 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -44,7 +44,7 @@ __KERNEL_RCSID(0, "$NetBSD: nvmm.c,v 1.2 #include <sys/mman.h> #include <sys/file.h> #include <sys/filedesc.h> -#include <sys/kauth.h> +#include <sys/device.h> #include <uvm/uvm.h> #include <uvm/uvm_page.h> @@ -570,11 +570,19 @@ nvmm_do_vcpu_run(struct nvmm_machine *ma int ret; while (1) { + /* Got a signal? Or pending resched? Leave. */ + if (__predict_false(nvmm_return_needed())) { + exit->reason = NVMM_VCPU_EXIT_NONE; + return 0; + } + + /* Run the VCPU. */ ret = (*nvmm_impl->vcpu_run)(mach, vcpu, exit); if (__predict_false(ret != 0)) { return ret; } + /* Process nested page faults. */ if (__predict_true(exit->reason != NVMM_VCPU_EXIT_MEMORY)) { break; } @@ -952,22 +960,27 @@ nvmm_ctl(struct nvmm_owner *owner, struc /* -------------------------------------------------------------------------- */ +static const struct nvmm_impl * +nvmm_ident(void) +{ + size_t i; + + for (i = 0; i < __arraycount(nvmm_impl_list); i++) { + if ((*nvmm_impl_list[i]->ident)()) + return nvmm_impl_list[i]; + } + + return NULL; +} + static int nvmm_init(void) { size_t i, n; - for (i = 0; i < __arraycount(nvmm_impl_list); i++) { - if (!(*nvmm_impl_list[i]->ident)()) { - continue; - } - nvmm_impl = nvmm_impl_list[i]; - break; - } - if (nvmm_impl == NULL) { - printf("NVMM: CPU not supported\n"); + nvmm_impl = nvmm_ident(); + if (nvmm_impl == NULL) return ENOTSUP; - } for (i = 0; i < NVMM_MAX_MACHINES; i++) { machines[i].machid = i; @@ -1161,6 +1174,54 @@ nvmm_ioctl(file_t *fp, u_long cmd, void /* -------------------------------------------------------------------------- */ +static int nvmm_match(device_t, cfdata_t, void *); +static void nvmm_attach(device_t, device_t, void *); +static int nvmm_detach(device_t, int); + +extern struct cfdriver nvmm_cd; + +CFATTACH_DECL_NEW(nvmm, 0, nvmm_match, nvmm_attach, nvmm_detach, NULL); + +static struct cfdata nvmm_cfdata[] = { + { + .cf_name = "nvmm", + .cf_atname = "nvmm", + .cf_unit = 0, + .cf_fstate = FSTATE_STAR, + .cf_loc = NULL, + .cf_flags = 0, + .cf_pspec = NULL, + }, + { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL } +}; + +static int +nvmm_match(device_t self, cfdata_t cfdata, void *arg) +{ + return 1; +} + +static void +nvmm_attach(device_t parent, device_t self, void *aux) +{ + int error; + + error = nvmm_init(); + if (error) + panic("%s: impossible", __func__); + aprint_normal_dev(self, "attached, using backend %s\n", + nvmm_impl->name); +} + +static int +nvmm_detach(device_t self, int flags) +{ + if (nmachines > 0) + return EBUSY; + nvmm_fini(); + return 0; +} + void nvmmattach(int nunits) { @@ -1169,51 +1230,83 @@ nvmmattach(int nunits) MODULE(MODULE_CLASS_MISC, nvmm, NULL); +#if defined(_MODULE) +CFDRIVER_DECL(nvmm, DV_VIRTUAL, NULL); +#endif + static int nvmm_modcmd(modcmd_t cmd, void *arg) { +#if defined(_MODULE) + devmajor_t bmajor = NODEVMAJOR; + devmajor_t cmajor = 345; +#endif int error; switch (cmd) { case MODULE_CMD_INIT: - error = nvmm_init(); + if (nvmm_ident() == NULL) { + aprint_error("%s: cpu not supported\n", + nvmm_cd.cd_name); + return ENOTSUP; + } +#if defined(_MODULE) + error = config_cfdriver_attach(&nvmm_cd); if (error) return error; +#endif + error = config_cfattach_attach(nvmm_cd.cd_name, &nvmm_ca); + if (error) { + config_cfdriver_detach(&nvmm_cd); + aprint_error("%s: config_cfattach_attach failed\n", + nvmm_cd.cd_name); + return error; + } + + error = config_cfdata_attach(nvmm_cfdata, 1); + if (error) { + config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca); + config_cfdriver_detach(&nvmm_cd); + aprint_error("%s: unable to register cfdata\n", + nvmm_cd.cd_name); + return error; + } + + if (config_attach_pseudo(nvmm_cfdata) == NULL) { + aprint_error("%s: config_attach_pseudo failed\n", + nvmm_cd.cd_name); + config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca); + config_cfdriver_detach(&nvmm_cd); + return ENXIO; + } #if defined(_MODULE) - { - devmajor_t bmajor = NODEVMAJOR; - devmajor_t cmajor = 345; - - /* mknod /dev/nvmm c 345 0 */ - error = devsw_attach("nvmm", NULL, &bmajor, - &nvmm_cdevsw, &cmajor); - if (error) { - nvmm_fini(); - return error; - } + /* mknod /dev/nvmm c 345 0 */ + error = devsw_attach(nvmm_cd.cd_name, NULL, &bmajor, + &nvmm_cdevsw, &cmajor); + if (error) { + aprint_error("%s: unable to register devsw\n", + nvmm_cd.cd_name); + config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca); + config_cfdriver_detach(&nvmm_cd); + return error; } #endif return 0; - case MODULE_CMD_FINI: - if (nmachines > 0) { - return EBUSY; - } + error = config_cfdata_detach(nvmm_cfdata); + if (error) + return error; + error = config_cfattach_detach(nvmm_cd.cd_name, &nvmm_ca); + if (error) + return error; #if defined(_MODULE) - { - error = devsw_detach(NULL, &nvmm_cdevsw); - if (error) { - return error; - } - } + config_cfdriver_detach(&nvmm_cd); + devsw_detach(NULL, &nvmm_cdevsw); #endif - nvmm_fini(); return 0; - case MODULE_CMD_AUTOUNLOAD: return EBUSY; - default: return ENOTTY; } Index: src/sys/dev/nvmm/nvmm_internal.h diff -u src/sys/dev/nvmm/nvmm_internal.h:1.12.2.2 src/sys/dev/nvmm/nvmm_internal.h:1.12.2.3 --- src/sys/dev/nvmm/nvmm_internal.h:1.12.2.2 Wed May 13 12:21:56 2020 +++ src/sys/dev/nvmm/nvmm_internal.h Sun Aug 2 08:49:08 2020 @@ -1,7 +1,7 @@ -/* $NetBSD: nvmm_internal.h,v 1.12.2.2 2020/05/13 12:21:56 martin Exp $ */ +/* $NetBSD: nvmm_internal.h,v 1.12.2.3 2020/08/02 08:49:08 martin Exp $ */ /* - * Copyright (c) 2018-2019 The NetBSD Foundation, Inc. + * Copyright (c) 2018-2020 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -91,6 +91,7 @@ struct nvmm_machine { }; struct nvmm_impl { + const char *name; bool (*ident)(void); void (*init)(void); void (*fini)(void); @@ -121,4 +122,16 @@ struct nvmm_impl { extern const struct nvmm_impl nvmm_x86_svm; extern const struct nvmm_impl nvmm_x86_vmx; +static inline bool +nvmm_return_needed(void) +{ + if (preempt_needed()) { + return true; + } + if (curlwp->l_flag & LW_USERRET) { + return true; + } + return false; +} + #endif /* _NVMM_INTERNAL_H_ */ Index: src/sys/dev/nvmm/x86/nvmm_x86_svm.c diff -u src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.46.4.5 src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.46.4.6 --- src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.46.4.5 Thu May 21 10:52:58 2020 +++ src/sys/dev/nvmm/x86/nvmm_x86_svm.c Sun Aug 2 08:49:08 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: nvmm_x86_svm.c,v 1.46.4.5 2020/05/21 10:52:58 martin Exp $ */ +/* $NetBSD: nvmm_x86_svm.c,v 1.46.4.6 2020/08/02 08:49:08 martin Exp $ */ /* * Copyright (c) 2018-2019 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.46.4.5 2020/05/21 10:52:58 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.46.4.6 2020/08/02 08:49:08 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -1466,13 +1466,7 @@ svm_vcpu_run(struct nvmm_machine *mach, } /* If no reason to return to userland, keep rolling. */ - if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) { - break; - } - if (curcpu()->ci_data.cpu_softints != 0) { - break; - } - if (curlwp->l_flag & LW_USERRET) { + if (nvmm_return_needed()) { break; } if (exit->reason != NVMM_VCPU_EXIT_NONE) { @@ -2442,6 +2436,7 @@ svm_capability(struct nvmm_capability *c } const struct nvmm_impl nvmm_x86_svm = { + .name = "x86-svm", .ident = svm_ident, .init = svm_init, .fini = svm_fini, Index: src/sys/dev/nvmm/x86/nvmm_x86_vmx.c diff -u src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.36.2.7 src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.36.2.8 --- src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.36.2.7 Thu May 21 10:52:58 2020 +++ src/sys/dev/nvmm/x86/nvmm_x86_vmx.c Sun Aug 2 08:49:08 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: nvmm_x86_vmx.c,v 1.36.2.7 2020/05/21 10:52:58 martin Exp $ */ +/* $NetBSD: nvmm_x86_vmx.c,v 1.36.2.8 2020/08/02 08:49:08 martin Exp $ */ /* * Copyright (c) 2018-2019 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.36.2.7 2020/05/21 10:52:58 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.36.2.8 2020/08/02 08:49:08 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -487,7 +487,7 @@ vmx_vmclear(paddr_t *pa) #define VMCS_HOST_IA32_SYSENTER_ESP 0x00006C10 #define VMCS_HOST_IA32_SYSENTER_EIP 0x00006C12 #define VMCS_HOST_RSP 0x00006C14 -#define VMCS_HOST_RIP 0x00006c16 +#define VMCS_HOST_RIP 0x00006C16 /* VMX basic exit reasons. */ #define VMCS_EXITCODE_EXC_NMI 0 @@ -2186,13 +2186,7 @@ vmx_vcpu_run(struct nvmm_machine *mach, } /* If no reason to return to userland, keep rolling. */ - if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) { - break; - } - if (curcpu()->ci_data.cpu_softints != 0) { - break; - } - if (curlwp->l_flag & LW_USERRET) { + if (nvmm_return_needed()) { break; } if (exit->reason != NVMM_VCPU_EXIT_NONE) { @@ -3402,6 +3396,7 @@ vmx_capability(struct nvmm_capability *c } const struct nvmm_impl nvmm_x86_vmx = { + .name = "x86-vmx", .ident = vmx_ident, .init = vmx_init, .fini = vmx_fini, Index: src/sys/modules/nvmm/nvmm.ioconf diff -u src/sys/modules/nvmm/nvmm.ioconf:1.1 src/sys/modules/nvmm/nvmm.ioconf:1.1.8.1 --- src/sys/modules/nvmm/nvmm.ioconf:1.1 Wed Nov 7 07:43:08 2018 +++ src/sys/modules/nvmm/nvmm.ioconf Sun Aug 2 08:49:08 2020 @@ -1,7 +1,8 @@ -# $NetBSD: nvmm.ioconf,v 1.1 2018/11/07 07:43:08 maxv Exp $ +# $NetBSD: nvmm.ioconf,v 1.1.8.1 2020/08/02 08:49:08 martin Exp $ ioconf nvmm include "conf/files" +include "dev/nvmm/files.nvmm" pseudo-device nvmm