subr_param.c revision 239582
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: stable/9/sys/kern/subr_param.c 239582 2012-08-22 19:53:33Z kib $");
39116182Sobrien
4014328Speter#include "opt_param.h"
41217688Spluknet#include "opt_msgbuf.h"
4280418Speter#include "opt_maxusers.h"
4313226Swollman
441541Srgrimes#include <sys/param.h>
4580418Speter#include <sys/systm.h>
4680418Speter#include <sys/kernel.h>
47239582Skib#include <sys/limits.h>
48239582Skib#include <sys/msgbuf.h>
49172696Salfred#include <sys/sysctl.h>
50239582Skib#include <sys/proc.h>
511541Srgrimes
52219920Salc#include <vm/vm.h>
53137393Sdes#include <vm/vm_param.h>
54219920Salc#include <vm/pmap.h>
5584783Sps
561541Srgrimes/*
571541Srgrimes * System parameter formulae.
581541Srgrimes */
591541Srgrimes
601541Srgrimes#ifndef HZ
61209492Snwhitehorn#  if defined(__mips__) || defined(__arm__)
62209492Snwhitehorn#    define	HZ 100
63209492Snwhitehorn#  else
64137307Sphk#    define	HZ 1000
65138214Sbms#  endif
66184323Ssobomax#  ifndef HZ_VM
67195430Ssilby#    define	HZ_VM 100
68184323Ssobomax#  endif
69184323Ssobomax#else
70184323Ssobomax#  ifndef HZ_VM
71184323Ssobomax#    define	HZ_VM HZ
72184323Ssobomax#  endif
731541Srgrimes#endif
7480418Speter#define	NPROC (20 + 16 * maxusers)
7580418Speter#ifndef NBUF
7680418Speter#define NBUF 0
7780418Speter#endif
7845515Sdes#ifndef MAXFILES
7980418Speter#define	MAXFILES (maxproc * 2)
8045515Sdes#endif
818747Sdg
82186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
83186286Sivoras
8480418Speterint	hz;
8580418Speterint	tick;
8680418Speterint	maxusers;			/* base tunable */
8780418Speterint	maxproc;			/* maximum # of processes */
8880418Speterint	maxprocperuid;			/* max # of procs per user */
8980418Speterint	maxfiles;			/* sys. wide open files limit */
9080418Speterint	maxfilesperproc;		/* per-proc open files limit */
91217688Spluknetint	msgbufsize;			/* size of kernel message buffer */
9280418Speterint	ncallout;			/* maximum # of timer events */
9380418Speterint	nbuf;
94202143Sbrooksint	ngroups_max;			/* max # groups per process */
9580418Speterint	nswbuf;
96239582Skibpid_t	pid_max = PID_MAX;
97189595Sjhblong	maxswzone;			/* max swmeta KVA storage */
98189595Sjhblong	maxbcache;			/* max buffer cache KVA storage */
99189649Sjhblong	maxpipekva;			/* Limit on pipe KVA */
100186286Sivorasint 	vm_guest;			/* Running as virtual machine guest? */
101137393Sdesu_long	maxtsiz;			/* max text size */
102137393Sdesu_long	dfldsiz;			/* initial data size limit */
103137393Sdesu_long	maxdsiz;			/* max data size */
104137393Sdesu_long	dflssiz;			/* initial stack size limit */
105137393Sdesu_long	maxssiz;			/* max stack size */
106137393Sdesu_long	sgrowsiz;			/* amount to grow stack */
1071541Srgrimes
108190331SjhbSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0,
109190331Sjhb    "Number of clock ticks per second");
110189744SjhbSYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN, &ncallout, 0,
111189744Sjhb    "Number of pre-allocated timer events");
112189744SjhbSYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0,
113190331Sjhb    "Number of buffers in the buffer cache");
114189744SjhbSYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0,
115189744Sjhb    "Number of swap buffers");
116217688SpluknetSYSCTL_INT(_kern, OID_AUTO, msgbufsize, CTLFLAG_RDTUN, &msgbufsize, 0,
117217688Spluknet    "Size of the kernel message buffer");
118189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
119190331Sjhb    "Maximum memory for swap metadata");
120189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
121190331Sjhb    "Maximum value of vfs.maxbufspace");
122178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0,
123190331Sjhb    "Maximum text size");
124178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0,
125190331Sjhb    "Initial data size limit");
126178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0,
127190331Sjhb    "Maximum data size");
128178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0,
129190331Sjhb    "Initial stack size limit");
130178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0,
131190331Sjhb    "Maximum stack size");
132178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0,
133190331Sjhb    "Amount to grow stack on a stack fault");
134186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
135186286Sivoras    NULL, 0, sysctl_kern_vm_guest, "A",
136204611Sivoras    "Virtual machine guest detected? (none|generic|xen)");
137172696Salfred
1381541Srgrimes/*
1391541Srgrimes * These have to be allocated somewhere; allocating
1401541Srgrimes * them here forces loader errors if this file is omitted
1411541Srgrimes * (if they've been externed everywhere else; hah!).
1421541Srgrimes */
1439759Sbdestruct	buf *swbuf;
14467046Sjasone
145204420Salc/*
146204420Salc * The elements of this array are ordered based upon the values of the
147204420Salc * corresponding enum VM_GUEST members.
148204420Salc */
149186522Sbzstatic const char *const vm_guest_sysctl_names[] = {
150186522Sbz	"none",
151186522Sbz	"generic",
152186522Sbz	"xen",
153186522Sbz	NULL
154186522Sbz};
155186522Sbz
156186522Sbz#ifndef XEN
157185772Sjkimstatic const char *const vm_bnames[] = {
158185772Sjkim	"QEMU",				/* QEMU */
159185772Sjkim	"Plex86",			/* Plex86 */
160185772Sjkim	"Bochs",			/* Bochs */
161210935Scsjp	"Xen",				/* Xen */
162185772Sjkim	NULL
163185772Sjkim};
164185772Sjkim
165184326Ssobomaxstatic const char *const vm_pnames[] = {
166184323Ssobomax	"VMware Virtual Platform",	/* VMWare VM */
167184323Ssobomax	"Virtual Machine",		/* Microsoft VirtualPC */
168184323Ssobomax	"VirtualBox",			/* Sun xVM VirtualBox */
169184323Ssobomax	"Parallels Virtual Platform",	/* Parallels VM */
170184323Ssobomax	NULL
171184323Ssobomax};
172184323Ssobomax
173186286Sivoras
174186286Sivoras/*
175186286Sivoras * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
176186286Sivoras */
177186252Sivorasstatic enum VM_GUEST
178184323Ssobomaxdetect_virtual(void)
179184323Ssobomax{
180184323Ssobomax	char *sysenv;
181184323Ssobomax	int i;
182184323Ssobomax
183185772Sjkim	sysenv = getenv("smbios.bios.vendor");
184185772Sjkim	if (sysenv != NULL) {
185185772Sjkim		for (i = 0; vm_bnames[i] != NULL; i++)
186185772Sjkim			if (strcmp(sysenv, vm_bnames[i]) == 0) {
187185772Sjkim				freeenv(sysenv);
188186252Sivoras				return (VM_GUEST_VM);
189185772Sjkim			}
190185772Sjkim		freeenv(sysenv);
191185772Sjkim	}
192184323Ssobomax	sysenv = getenv("smbios.system.product");
193184323Ssobomax	if (sysenv != NULL) {
194185772Sjkim		for (i = 0; vm_pnames[i] != NULL; i++)
195185772Sjkim			if (strcmp(sysenv, vm_pnames[i]) == 0) {
196185772Sjkim				freeenv(sysenv);
197186252Sivoras				return (VM_GUEST_VM);
198185772Sjkim			}
199185772Sjkim		freeenv(sysenv);
200184323Ssobomax	}
201186252Sivoras	return (VM_GUEST_NO);
202184323Ssobomax}
203186522Sbz#endif
204184323Ssobomax
20567046Sjasone/*
20687546Sdillon * Boot time overrides that are not scaled against main memory
20780418Speter */
20880418Spetervoid
20987546Sdilloninit_param1(void)
21080418Speter{
211186252Sivoras#ifndef XEN
212186252Sivoras	vm_guest = detect_virtual();
213186252Sivoras#else
214186252Sivoras	vm_guest = VM_GUEST_XEN;
215186252Sivoras#endif
216184323Ssobomax	hz = -1;
21780418Speter	TUNABLE_INT_FETCH("kern.hz", &hz);
218185772Sjkim	if (hz == -1)
219186252Sivoras		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
22080418Speter	tick = 1000000 / hz;
22180418Speter
22281986Sdillon#ifdef VM_SWZONE_SIZE_MAX
22381933Sdillon	maxswzone = VM_SWZONE_SIZE_MAX;
22481986Sdillon#endif
225189595Sjhb	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
22681986Sdillon#ifdef VM_BCACHE_SIZE_MAX
22781933Sdillon	maxbcache = VM_BCACHE_SIZE_MAX;
22881986Sdillon#endif
229189595Sjhb	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
230217688Spluknet	msgbufsize = MSGBUF_SIZE;
231217688Spluknet	TUNABLE_INT_FETCH("kern.msgbufsize", &msgbufsize);
23284783Sps
23384783Sps	maxtsiz = MAXTSIZ;
234137393Sdes	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
23584783Sps	dfldsiz = DFLDSIZ;
236137393Sdes	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
23784783Sps	maxdsiz = MAXDSIZ;
238137393Sdes	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
23984783Sps	dflssiz = DFLSSIZ;
240137393Sdes	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
24184783Sps	maxssiz = MAXSSIZ;
242137393Sdes	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
24384783Sps	sgrowsiz = SGROWSIZ;
244137393Sdes	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
245202143Sbrooks
246202143Sbrooks	/*
247202143Sbrooks	 * Let the administrator set {NGROUPS_MAX}, but disallow values
248202143Sbrooks	 * less than NGROUPS_MAX which would violate POSIX.1-2008 or
249202143Sbrooks	 * greater than INT_MAX-1 which would result in overflow.
250202143Sbrooks	 */
251202143Sbrooks	ngroups_max = NGROUPS_MAX;
252202143Sbrooks	TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max);
253202143Sbrooks	if (ngroups_max < NGROUPS_MAX)
254202143Sbrooks		ngroups_max = NGROUPS_MAX;
255239582Skib
256239582Skib	/*
257239582Skib	 * Only allow to lower the maximal pid.
258239582Skib	 * Prevent setting up a non-bootable system if pid_max is too low.
259239582Skib	 */
260239582Skib	TUNABLE_INT_FETCH("kern.pid_max", &pid_max);
261239582Skib	if (pid_max > PID_MAX)
262239582Skib		pid_max = PID_MAX;
263239582Skib	else if (pid_max < 300)
264239582Skib		pid_max = 300;
26580418Speter}
26687546Sdillon
26787546Sdillon/*
26887546Sdillon * Boot time overrides that are scaled against main memory
26987546Sdillon */
27087546Sdillonvoid
271102600Speterinit_param2(long physpages)
27287546Sdillon{
27387546Sdillon
27487546Sdillon	/* Base parameters */
27590274Sdillon	maxusers = MAXUSERS;
27690274Sdillon	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
27790274Sdillon	if (maxusers == 0) {
27889769Sdillon		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
27987546Sdillon		if (maxusers < 32)
28087546Sdillon			maxusers = 32;
28189769Sdillon		if (maxusers > 384)
28289769Sdillon			maxusers = 384;
28387546Sdillon	}
28487546Sdillon
28587546Sdillon	/*
28687546Sdillon	 * The following can be overridden after boot via sysctl.  Note:
28787546Sdillon	 * unless overriden, these macros are ultimately based on maxusers.
28887546Sdillon	 */
28987546Sdillon	maxproc = NPROC;
29087546Sdillon	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
29191780Ssilby	/*
29291780Ssilby	 * Limit maxproc so that kmap entries cannot be exhausted by
29391780Ssilby	 * processes.
29491780Ssilby	 */
29591780Ssilby	if (maxproc > (physpages / 12))
29691780Ssilby		maxproc = physpages / 12;
29787546Sdillon	maxfiles = MAXFILES;
29887546Sdillon	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
29987817Ssilby	maxprocperuid = (maxproc * 9) / 10;
30087817Ssilby	maxfilesperproc = (maxfiles * 9) / 10;
301117391Ssilby
302117391Ssilby	/*
303117391Ssilby	 * Cannot be changed after boot.
304117391Ssilby	 */
305117391Ssilby	nbuf = NBUF;
306117391Ssilby	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
30787546Sdillon
308117391Ssilby	ncallout = 16 + maxproc + maxfiles;
309117391Ssilby	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
310117391Ssilby
31187546Sdillon	/*
312219920Salc	 * The default for maxpipekva is min(1/64 of the kernel address space,
313219920Salc	 * max(1/64 of main memory, 512KB)).  See sys_pipe.c for more details.
314117325Ssilby	 */
315219920Salc	maxpipekva = (physpages / 64) * PAGE_SIZE;
316117325Ssilby	if (maxpipekva < 512 * 1024)
317117325Ssilby		maxpipekva = 512 * 1024;
318219920Salc	if (maxpipekva > (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 64)
319219920Salc		maxpipekva = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
320219920Salc		    64;
321189649Sjhb	TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva);
32287546Sdillon}
323186286Sivoras
324186286Sivoras/*
325186286Sivoras * Sysctl stringiying handler for kern.vm_guest.
326186286Sivoras */
327186286Sivorasstatic int
328186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
329186286Sivoras{
330186286Sivoras	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
331186286Sivoras	    strlen(vm_guest_sysctl_names[vm_guest])));
332186286Sivoras}
333