1/*-
2 * Copyright (c) 1980, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)param.c	8.3 (Berkeley) 8/20/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD$");
39
40#include "opt_param.h"
41#include "opt_msgbuf.h"
42#include "opt_maxusers.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/limits.h>
48#include <sys/msgbuf.h>
49#include <sys/sysctl.h>
50#include <sys/proc.h>
51
52#include <vm/vm.h>
53#include <vm/vm_param.h>
54#include <vm/pmap.h>
55
56/*
57 * System parameter formulae.
58 */
59
60#ifndef HZ
61#  if defined(__mips__) || defined(__arm__)
62#    define	HZ 100
63#  else
64#    define	HZ 1000
65#  endif
66#  ifndef HZ_VM
67#    define	HZ_VM 100
68#  endif
69#else
70#  ifndef HZ_VM
71#    define	HZ_VM HZ
72#  endif
73#endif
74#define	NPROC (20 + 16 * maxusers)
75#ifndef NBUF
76#define NBUF 0
77#endif
78#ifndef MAXFILES
79#define	MAXFILES (maxproc * 2)
80#endif
81
82static int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
83
84int	hz;				/* system clock's frequency */
85int	tick;				/* usec per tick (1000000 / hz) */
86struct bintime tick_bt;			/* bintime per tick (1s / hz) */
87sbintime_t tick_sbt;
88int	maxusers;			/* base tunable */
89int	maxproc;			/* maximum # of processes */
90int	maxprocperuid;			/* max # of procs per user */
91int	maxfiles;			/* sys. wide open files limit */
92int	maxfilesperproc;		/* per-proc open files limit */
93int	msgbufsize;			/* size of kernel message buffer */
94int	nbuf;
95int	bio_transient_maxcnt;
96int	ngroups_max;			/* max # groups per process */
97int	nswbuf;
98pid_t	pid_max = PID_MAX;
99long	maxswzone;			/* max swmeta KVA storage */
100long	maxbcache;			/* max buffer cache KVA storage */
101long	maxpipekva;			/* Limit on pipe KVA */
102int 	vm_guest;			/* Running as virtual machine guest? */
103u_long	maxtsiz;			/* max text size */
104u_long	dfldsiz;			/* initial data size limit */
105u_long	maxdsiz;			/* max data size */
106u_long	dflssiz;			/* initial stack size limit */
107u_long	maxssiz;			/* max stack size */
108u_long	sgrowsiz;			/* amount to grow stack */
109
110SYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0,
111    "Number of clock ticks per second");
112SYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0,
113    "Number of buffers in the buffer cache");
114SYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0,
115    "Number of swap buffers");
116SYSCTL_INT(_kern, OID_AUTO, msgbufsize, CTLFLAG_RDTUN, &msgbufsize, 0,
117    "Size of the kernel message buffer");
118SYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
119    "Maximum memory for swap metadata");
120SYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
121    "Maximum value of vfs.maxbufspace");
122SYSCTL_INT(_kern, OID_AUTO, bio_transient_maxcnt, CTLFLAG_RDTUN,
123    &bio_transient_maxcnt, 0,
124    "Maximum number of transient BIOs mappings");
125SYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxtsiz, 0,
126    "Maximum text size");
127SYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RW | CTLFLAG_TUN, &dfldsiz, 0,
128    "Initial data size limit");
129SYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RW | CTLFLAG_TUN, &maxdsiz, 0,
130    "Maximum data size");
131SYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RW | CTLFLAG_TUN, &dflssiz, 0,
132    "Initial stack size limit");
133SYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RW | CTLFLAG_TUN, &maxssiz, 0,
134    "Maximum stack size");
135SYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RW | CTLFLAG_TUN, &sgrowsiz, 0,
136    "Amount to grow stack on a stack fault");
137SYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
138    NULL, 0, sysctl_kern_vm_guest, "A",
139    "Virtual machine guest detected? (none|generic|xen)");
140
141/*
142 * These have to be allocated somewhere; allocating
143 * them here forces loader errors if this file is omitted
144 * (if they've been externed everywhere else; hah!).
145 */
146struct	buf *swbuf;
147
148/*
149 * The elements of this array are ordered based upon the values of the
150 * corresponding enum VM_GUEST members.
151 */
152static const char *const vm_guest_sysctl_names[] = {
153	"none",
154	"generic",
155	"xen",
156	"hv",
157	NULL
158};
159CTASSERT(nitems(vm_guest_sysctl_names) - 1 == VM_LAST);
160
161#ifndef XEN
162static const char *const vm_bnames[] = {
163	"QEMU",				/* QEMU */
164	"Plex86",			/* Plex86 */
165	"Bochs",			/* Bochs */
166	"Xen",				/* Xen */
167	"BHYVE",			/* bhyve */
168	"Seabios",			/* KVM */
169	NULL
170};
171
172static const char *const vm_pnames[] = {
173	"VMware Virtual Platform",	/* VMWare VM */
174	"Virtual Machine",		/* Microsoft VirtualPC */
175	"VirtualBox",			/* Sun xVM VirtualBox */
176	"Parallels Virtual Platform",	/* Parallels VM */
177	"KVM",				/* KVM */
178	NULL
179};
180
181
182/*
183 * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
184 */
185static enum VM_GUEST
186detect_virtual(void)
187{
188	char *sysenv;
189	int i;
190
191	sysenv = getenv("smbios.bios.vendor");
192	if (sysenv != NULL) {
193		for (i = 0; vm_bnames[i] != NULL; i++)
194			if (strcmp(sysenv, vm_bnames[i]) == 0) {
195				freeenv(sysenv);
196				return (VM_GUEST_VM);
197			}
198		freeenv(sysenv);
199	}
200	sysenv = getenv("smbios.system.product");
201	if (sysenv != NULL) {
202		for (i = 0; vm_pnames[i] != NULL; i++)
203			if (strcmp(sysenv, vm_pnames[i]) == 0) {
204				freeenv(sysenv);
205				return (VM_GUEST_VM);
206			}
207		freeenv(sysenv);
208	}
209	return (VM_GUEST_NO);
210}
211#endif
212
213/*
214 * Boot time overrides that are not scaled against main memory
215 */
216void
217init_param1(void)
218{
219#ifndef XEN
220	vm_guest = detect_virtual();
221#else
222	vm_guest = VM_GUEST_XEN;
223#endif
224	hz = -1;
225	TUNABLE_INT_FETCH("kern.hz", &hz);
226	if (hz == -1)
227		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
228	tick = 1000000 / hz;
229	tick_sbt = SBT_1S / hz;
230	tick_bt = sbttobt(tick_sbt);
231
232#ifdef VM_SWZONE_SIZE_MAX
233	maxswzone = VM_SWZONE_SIZE_MAX;
234#endif
235	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
236#ifdef VM_BCACHE_SIZE_MAX
237	maxbcache = VM_BCACHE_SIZE_MAX;
238#endif
239	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
240	msgbufsize = MSGBUF_SIZE;
241	TUNABLE_INT_FETCH("kern.msgbufsize", &msgbufsize);
242
243	maxtsiz = MAXTSIZ;
244	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
245	dfldsiz = DFLDSIZ;
246	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
247	maxdsiz = MAXDSIZ;
248	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
249	dflssiz = DFLSSIZ;
250	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
251	maxssiz = MAXSSIZ;
252	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
253	sgrowsiz = SGROWSIZ;
254	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
255
256	/*
257	 * Let the administrator set {NGROUPS_MAX}, but disallow values
258	 * less than NGROUPS_MAX which would violate POSIX.1-2008 or
259	 * greater than INT_MAX-1 which would result in overflow.
260	 */
261	ngroups_max = NGROUPS_MAX;
262	TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max);
263	if (ngroups_max < NGROUPS_MAX)
264		ngroups_max = NGROUPS_MAX;
265
266	/*
267	 * Only allow to lower the maximal pid.
268	 * Prevent setting up a non-bootable system if pid_max is too low.
269	 */
270	TUNABLE_INT_FETCH("kern.pid_max", &pid_max);
271	if (pid_max > PID_MAX)
272		pid_max = PID_MAX;
273	else if (pid_max < 300)
274		pid_max = 300;
275
276	TUNABLE_INT_FETCH("vfs.unmapped_buf_allowed", &unmapped_buf_allowed);
277}
278
279/*
280 * Boot time overrides that are scaled against main memory
281 */
282void
283init_param2(long physpages)
284{
285
286	/* Base parameters */
287	maxusers = MAXUSERS;
288	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
289	if (maxusers == 0) {
290		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
291		if (maxusers < 32)
292			maxusers = 32;
293#ifdef VM_MAX_AUTOTUNE_MAXUSERS
294                if (maxusers > VM_MAX_AUTOTUNE_MAXUSERS)
295                        maxusers = VM_MAX_AUTOTUNE_MAXUSERS;
296#endif
297                /*
298                 * Scales down the function in which maxusers grows once
299                 * we hit 384.
300                 */
301                if (maxusers > 384)
302                        maxusers = 384 + ((maxusers - 384) / 8);
303        }
304
305	/*
306	 * The following can be overridden after boot via sysctl.  Note:
307	 * unless overriden, these macros are ultimately based on maxusers.
308	 * Limit maxproc so that kmap entries cannot be exhausted by
309	 * processes.
310	 */
311	maxproc = NPROC;
312	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
313	if (maxproc > (physpages / 12))
314		maxproc = physpages / 12;
315	maxprocperuid = (maxproc * 9) / 10;
316
317	/*
318	 * The default limit for maxfiles is 1/12 of the number of
319	 * physical page but not less than 16 times maxusers.
320	 * At most it can be 1/6 the number of physical pages.
321	 */
322	maxfiles = imax(MAXFILES, physpages / 8);
323	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
324	if (maxfiles > (physpages / 4))
325		maxfiles = physpages / 4;
326	maxfilesperproc = (maxfiles / 10) * 9;
327
328	/*
329	 * Cannot be changed after boot.
330	 */
331	nbuf = NBUF;
332	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
333	TUNABLE_INT_FETCH("kern.bio_transient_maxcnt", &bio_transient_maxcnt);
334
335	/*
336	 * The default for maxpipekva is min(1/64 of the kernel address space,
337	 * max(1/64 of main memory, 512KB)).  See sys_pipe.c for more details.
338	 */
339	maxpipekva = (physpages / 64) * PAGE_SIZE;
340	TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva);
341	if (maxpipekva < 512 * 1024)
342		maxpipekva = 512 * 1024;
343	if (maxpipekva > (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 64)
344		maxpipekva = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
345		    64;
346}
347
348/*
349 * Sysctl stringifying handler for kern.vm_guest.
350 */
351static int
352sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
353{
354	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
355	    strlen(vm_guest_sysctl_names[vm_guest])));
356}
357