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.
This commit is contained in:
ivoras 2008-12-17 19:57:12 +00:00
parent 074f3c5a4e
commit b769de9274

View File

@ -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