From b769de9274e69c1f557b726cb784765e9e157a8f Mon Sep 17 00:00:00 2001 From: ivoras Date: Wed, 17 Dec 2008 19:57:12 +0000 Subject: [PATCH] Introduce a sysctl kern.vm_guest that reflects what the kernel knows about it running under a virtual environment. This also introduces a globally accessible variable vm_guest that can be used where appropriate in the kernel to inspect this environment. To make it easier for the long run, an enum VM_GUEST is also introduced, which could possibly be factored out in a header somewhere (but the question is where - vm/vm_param.h? sys/param.h?) so it eventually becomes a part of the standard KPI. In any case, it's a start. The purpose of all this isn't to absolutely detect that the OS is running under a virtual environment (cf. "redpill") but to allow the parts of the kernel and the userland that care about this particular aspect and can do something useful depending on it to have a standardised interface. Reducing kern.hz is one example but there are other things that could be done like avoiding context switches, not using CPU instructions that are known to be slow in emulation, possibly different strategies in VM (memory) allocation, CPU scheduling, etc. It isn't clear if the JAILS/VIMAGE functionality should also be exposed by this particular mechanism (probably not since they're not "full" virtual hardware environments). Sometime in the future another sysctl and a variable could be introduced to reflect if the kernel supports any kind of virtual hosting (e.g. VMWare VMI, Xen dom0). Reviewed by: silence from src-commiters@, virtualization@, kmacy@ Approved by: gnn (mentor) Security: Obscurity doesn't help. --- sys/kern/subr_param.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/sys/kern/subr_param.c b/sys/kern/subr_param.c index 3701fdaede39..35e5c74c7c44 100644 --- a/sys/kern/subr_param.c +++ b/sys/kern/subr_param.c @@ -73,6 +73,8 @@ __FBSDID("$FreeBSD$"); #define MAXFILES (maxproc * 2) #endif +enum VM_GUEST { VM_GUEST_NO, VM_GUEST_VM, VM_GUEST_XEN }; + int hz; int tick; int maxusers; /* base tunable */ @@ -86,6 +88,7 @@ int nswbuf; int maxswzone; /* max swmeta KVA storage */ int maxbcache; /* max buffer cache KVA storage */ int maxpipekva; /* Limit on pipe KVA */ +int vm_guest; /* Running as virtual machine guest? */ u_long maxtsiz; /* max text size */ u_long dfldsiz; /* initial data size limit */ u_long maxdsiz; /* max data size */ @@ -110,6 +113,8 @@ SYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0, "max stack size"); SYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0, "amount to grow stack"); +SYSCTL_INT(_kern, OID_AUTO, vm_guest, CTLFLAG_RD, &vm_guest, 0, + "Running under a virtual machine?"); /* * These have to be allocated somewhere; allocating @@ -133,7 +138,7 @@ static const char *const vm_pnames[] = { NULL }; -static int +static enum VM_GUEST detect_virtual(void) { char *sysenv; @@ -144,7 +149,7 @@ detect_virtual(void) for (i = 0; vm_bnames[i] != NULL; i++) if (strcmp(sysenv, vm_bnames[i]) == 0) { freeenv(sysenv); - return (1); + return (VM_GUEST_VM); } freeenv(sysenv); } @@ -153,11 +158,11 @@ detect_virtual(void) for (i = 0; vm_pnames[i] != NULL; i++) if (strcmp(sysenv, vm_pnames[i]) == 0) { freeenv(sysenv); - return (1); + return (VM_GUEST_VM); } freeenv(sysenv); } - return (0); + return (VM_GUEST_NO); } /* @@ -166,11 +171,15 @@ detect_virtual(void) void init_param1(void) { - +#ifndef XEN + vm_guest = detect_virtual(); +#else + vm_guest = VM_GUEST_XEN; +#endif hz = -1; TUNABLE_INT_FETCH("kern.hz", &hz); if (hz == -1) - hz = detect_virtual() ? HZ_VM : HZ; + hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ; tick = 1000000 / hz; #ifdef VM_SWZONE_SIZE_MAX