subr_param.c revision 202143
1139804Simp/*-
21541Srgrimes * Copyright (c) 1980, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
3414526Shsu *	@(#)param.c	8.3 (Berkeley) 8/20/94
351541Srgrimes */
361541Srgrimes
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_param.c 202143 2010-01-12 07:49:34Z brooks $");
39116182Sobrien
4014328Speter#include "opt_param.h"
4180418Speter#include "opt_maxusers.h"
4213226Swollman
43202143Sbrooks#include <sys/limits.h>
441541Srgrimes#include <sys/param.h>
4580418Speter#include <sys/systm.h>
4680418Speter#include <sys/kernel.h>
47172696Salfred#include <sys/sysctl.h>
481541Srgrimes
49137393Sdes#include <vm/vm_param.h>
5084783Sps
511541Srgrimes/*
521541Srgrimes * System parameter formulae.
531541Srgrimes */
541541Srgrimes
551541Srgrimes#ifndef HZ
56145154Smarius#  if defined(__amd64__) || defined(__i386__) || defined(__ia64__) || defined(__sparc64__)
57137307Sphk#    define	HZ 1000
58137307Sphk#  else
59137307Sphk#    define	HZ 100
60138214Sbms#  endif
61184323Ssobomax#  ifndef HZ_VM
62195430Ssilby#    define	HZ_VM 100
63184323Ssobomax#  endif
64184323Ssobomax#else
65184323Ssobomax#  ifndef HZ_VM
66184323Ssobomax#    define	HZ_VM HZ
67184323Ssobomax#  endif
681541Srgrimes#endif
6980418Speter#define	NPROC (20 + 16 * maxusers)
7080418Speter#ifndef NBUF
7180418Speter#define NBUF 0
7280418Speter#endif
7345515Sdes#ifndef MAXFILES
7480418Speter#define	MAXFILES (maxproc * 2)
7545515Sdes#endif
768747Sdg
77186619Sivoras/* Values of enum VM_GUEST members are used as indices in
78186619Sivoras * vm_guest_sysctl_names */
79186619Sivorasenum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN };
80186252Sivoras
81186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
82186286Sivoras
8380418Speterint	hz;
8480418Speterint	tick;
8580418Speterint	maxusers;			/* base tunable */
8680418Speterint	maxproc;			/* maximum # of processes */
8780418Speterint	maxprocperuid;			/* max # of procs per user */
8880418Speterint	maxfiles;			/* sys. wide open files limit */
8980418Speterint	maxfilesperproc;		/* per-proc open files limit */
9080418Speterint	ncallout;			/* maximum # of timer events */
9180418Speterint	nbuf;
92202143Sbrooksint	ngroups_max;			/* max # groups per process */
9380418Speterint	nswbuf;
94189595Sjhblong	maxswzone;			/* max swmeta KVA storage */
95189595Sjhblong	maxbcache;			/* max buffer cache KVA storage */
96189649Sjhblong	maxpipekva;			/* Limit on pipe KVA */
97186286Sivorasint 	vm_guest;			/* Running as virtual machine guest? */
98137393Sdesu_long	maxtsiz;			/* max text size */
99137393Sdesu_long	dfldsiz;			/* initial data size limit */
100137393Sdesu_long	maxdsiz;			/* max data size */
101137393Sdesu_long	dflssiz;			/* initial stack size limit */
102137393Sdesu_long	maxssiz;			/* max stack size */
103137393Sdesu_long	sgrowsiz;			/* amount to grow stack */
1041541Srgrimes
105190331SjhbSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0,
106190331Sjhb    "Number of clock ticks per second");
107189744SjhbSYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN, &ncallout, 0,
108189744Sjhb    "Number of pre-allocated timer events");
109189744SjhbSYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0,
110190331Sjhb    "Number of buffers in the buffer cache");
111189744SjhbSYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0,
112189744Sjhb    "Number of swap buffers");
113189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
114190331Sjhb    "Maximum memory for swap metadata");
115189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
116190331Sjhb    "Maximum value of vfs.maxbufspace");
117178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0,
118190331Sjhb    "Maximum text size");
119178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0,
120190331Sjhb    "Initial data size limit");
121178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0,
122190331Sjhb    "Maximum data size");
123178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0,
124190331Sjhb    "Initial stack size limit");
125178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0,
126190331Sjhb    "Maximum stack size");
127178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0,
128190331Sjhb    "Amount to grow stack on a stack fault");
129186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
130186286Sivoras    NULL, 0, sysctl_kern_vm_guest, "A",
131186286Sivoras    "Virtual machine detected? (none|generic|xen)");
132172696Salfred
1331541Srgrimes/*
1341541Srgrimes * These have to be allocated somewhere; allocating
1351541Srgrimes * them here forces loader errors if this file is omitted
1361541Srgrimes * (if they've been externed everywhere else; hah!).
1371541Srgrimes */
1389759Sbdestruct	buf *swbuf;
13967046Sjasone
140186522Sbzstatic const char *const vm_guest_sysctl_names[] = {
141186522Sbz	"none",
142186522Sbz	"generic",
143186522Sbz	"xen",
144186522Sbz	NULL
145186522Sbz};
146186522Sbz
147186522Sbz#ifndef XEN
148185772Sjkimstatic const char *const vm_bnames[] = {
149185772Sjkim	"QEMU",				/* QEMU */
150185772Sjkim	"Plex86",			/* Plex86 */
151185772Sjkim	"Bochs",			/* Bochs */
152185772Sjkim	NULL
153185772Sjkim};
154185772Sjkim
155184326Ssobomaxstatic const char *const vm_pnames[] = {
156184323Ssobomax	"VMware Virtual Platform",	/* VMWare VM */
157184323Ssobomax	"Virtual Machine",		/* Microsoft VirtualPC */
158184323Ssobomax	"VirtualBox",			/* Sun xVM VirtualBox */
159184323Ssobomax	"Parallels Virtual Platform",	/* Parallels VM */
160184323Ssobomax	NULL
161184323Ssobomax};
162184323Ssobomax
163186286Sivoras
164186286Sivoras/*
165186286Sivoras * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
166186286Sivoras */
167186252Sivorasstatic enum VM_GUEST
168184323Ssobomaxdetect_virtual(void)
169184323Ssobomax{
170184323Ssobomax	char *sysenv;
171184323Ssobomax	int i;
172184323Ssobomax
173185772Sjkim	sysenv = getenv("smbios.bios.vendor");
174185772Sjkim	if (sysenv != NULL) {
175185772Sjkim		for (i = 0; vm_bnames[i] != NULL; i++)
176185772Sjkim			if (strcmp(sysenv, vm_bnames[i]) == 0) {
177185772Sjkim				freeenv(sysenv);
178186252Sivoras				return (VM_GUEST_VM);
179185772Sjkim			}
180185772Sjkim		freeenv(sysenv);
181185772Sjkim	}
182184323Ssobomax	sysenv = getenv("smbios.system.product");
183184323Ssobomax	if (sysenv != NULL) {
184185772Sjkim		for (i = 0; vm_pnames[i] != NULL; i++)
185185772Sjkim			if (strcmp(sysenv, vm_pnames[i]) == 0) {
186185772Sjkim				freeenv(sysenv);
187186252Sivoras				return (VM_GUEST_VM);
188185772Sjkim			}
189185772Sjkim		freeenv(sysenv);
190184323Ssobomax	}
191186252Sivoras	return (VM_GUEST_NO);
192184323Ssobomax}
193186522Sbz#endif
194184323Ssobomax
19567046Sjasone/*
19687546Sdillon * Boot time overrides that are not scaled against main memory
19780418Speter */
19880418Spetervoid
19987546Sdilloninit_param1(void)
20080418Speter{
201186252Sivoras#ifndef XEN
202186252Sivoras	vm_guest = detect_virtual();
203186252Sivoras#else
204186252Sivoras	vm_guest = VM_GUEST_XEN;
205186252Sivoras#endif
206184323Ssobomax	hz = -1;
20780418Speter	TUNABLE_INT_FETCH("kern.hz", &hz);
208185772Sjkim	if (hz == -1)
209186252Sivoras		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
21080418Speter	tick = 1000000 / hz;
21180418Speter
21281986Sdillon#ifdef VM_SWZONE_SIZE_MAX
21381933Sdillon	maxswzone = VM_SWZONE_SIZE_MAX;
21481986Sdillon#endif
215189595Sjhb	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
21681986Sdillon#ifdef VM_BCACHE_SIZE_MAX
21781933Sdillon	maxbcache = VM_BCACHE_SIZE_MAX;
21881986Sdillon#endif
219189595Sjhb	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
22084783Sps
22184783Sps	maxtsiz = MAXTSIZ;
222137393Sdes	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
22384783Sps	dfldsiz = DFLDSIZ;
224137393Sdes	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
22584783Sps	maxdsiz = MAXDSIZ;
226137393Sdes	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
22784783Sps	dflssiz = DFLSSIZ;
228137393Sdes	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
22984783Sps	maxssiz = MAXSSIZ;
230137393Sdes	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
23184783Sps	sgrowsiz = SGROWSIZ;
232137393Sdes	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
233202143Sbrooks
234202143Sbrooks	/*
235202143Sbrooks	 * Let the administrator set {NGROUPS_MAX}, but disallow values
236202143Sbrooks	 * less than NGROUPS_MAX which would violate POSIX.1-2008 or
237202143Sbrooks	 * greater than INT_MAX-1 which would result in overflow.
238202143Sbrooks	 */
239202143Sbrooks	ngroups_max = NGROUPS_MAX;
240202143Sbrooks	TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max);
241202143Sbrooks	if (ngroups_max < NGROUPS_MAX)
242202143Sbrooks		ngroups_max = NGROUPS_MAX;
243202143Sbrooks	if (ngroups_max > INT_MAX - 1)
244202143Sbrooks		ngroups_max = INT_MAX - 1;
24580418Speter}
24687546Sdillon
24787546Sdillon/*
24887546Sdillon * Boot time overrides that are scaled against main memory
24987546Sdillon */
25087546Sdillonvoid
251102600Speterinit_param2(long physpages)
25287546Sdillon{
25387546Sdillon
25487546Sdillon	/* Base parameters */
25590274Sdillon	maxusers = MAXUSERS;
25690274Sdillon	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
25790274Sdillon	if (maxusers == 0) {
25889769Sdillon		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
25987546Sdillon		if (maxusers < 32)
26087546Sdillon			maxusers = 32;
26189769Sdillon		if (maxusers > 384)
26289769Sdillon			maxusers = 384;
26387546Sdillon	}
26487546Sdillon
26587546Sdillon	/*
26687546Sdillon	 * The following can be overridden after boot via sysctl.  Note:
26787546Sdillon	 * unless overriden, these macros are ultimately based on maxusers.
26887546Sdillon	 */
26987546Sdillon	maxproc = NPROC;
27087546Sdillon	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
27191780Ssilby	/*
27291780Ssilby	 * Limit maxproc so that kmap entries cannot be exhausted by
27391780Ssilby	 * processes.
27491780Ssilby	 */
27591780Ssilby	if (maxproc > (physpages / 12))
27691780Ssilby		maxproc = physpages / 12;
27787546Sdillon	maxfiles = MAXFILES;
27887546Sdillon	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
27987817Ssilby	maxprocperuid = (maxproc * 9) / 10;
28087817Ssilby	maxfilesperproc = (maxfiles * 9) / 10;
281117391Ssilby
282117391Ssilby	/*
283117391Ssilby	 * Cannot be changed after boot.
284117391Ssilby	 */
285117391Ssilby	nbuf = NBUF;
286117391Ssilby	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
28787546Sdillon
288117391Ssilby	ncallout = 16 + maxproc + maxfiles;
289117391Ssilby	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
290117391Ssilby}
291117391Ssilby
292117391Ssilby/*
293180262Salc * Boot time overrides that are scaled against the kmem map
294117391Ssilby */
295117391Ssilbyvoid
296117391Ssilbyinit_param3(long kmempages)
297117391Ssilby{
298127612Salc
29987546Sdillon	/*
300180262Salc	 * The default for maxpipekva is max(5% of the kmem map, 512KB).
301127612Salc	 * See sys_pipe.c for more details.
302117325Ssilby	 */
303118764Ssilby	maxpipekva = (kmempages / 20) * PAGE_SIZE;
304117325Ssilby	if (maxpipekva < 512 * 1024)
305117325Ssilby		maxpipekva = 512 * 1024;
306189649Sjhb	TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva);
30787546Sdillon}
308186286Sivoras
309186286Sivoras/*
310186286Sivoras * Sysctl stringiying handler for kern.vm_guest.
311186286Sivoras */
312186286Sivorasstatic int
313186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
314186286Sivoras{
315186286Sivoras	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
316186286Sivoras	    strlen(vm_guest_sysctl_names[vm_guest])));
317186286Sivoras}
318