From owner-svn-src-head@FreeBSD.ORG Wed Dec 17 19:57:13 2008 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4A4971065670; Wed, 17 Dec 2008 19:57:13 +0000 (UTC) (envelope-from ivoras@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3384A8FC12; Wed, 17 Dec 2008 19:57:13 +0000 (UTC) (envelope-from ivoras@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id mBHJvCUk013766; Wed, 17 Dec 2008 19:57:12 GMT (envelope-from ivoras@svn.freebsd.org) Received: (from ivoras@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id mBHJvCO2013765; Wed, 17 Dec 2008 19:57:12 GMT (envelope-from ivoras@svn.freebsd.org) Message-Id: <200812171957.mBHJvCO2013765@svn.freebsd.org> From: Ivan Voras Date: Wed, 17 Dec 2008 19:57:12 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r186252 - head/sys/kern X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Dec 2008 19:57:13 -0000 Author: ivoras Date: Wed Dec 17 19:57:12 2008 New Revision: 186252 URL: http://svn.freebsd.org/changeset/base/186252 Log: 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. Modified: head/sys/kern/subr_param.c Modified: head/sys/kern/subr_param.c ============================================================================== --- head/sys/kern/subr_param.c Wed Dec 17 19:19:14 2008 (r186251) +++ head/sys/kern/subr_param.c Wed Dec 17 19:57:12 2008 (r186252) @@ -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, C "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