19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1980, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 3470b012caSJeffrey Hsu * @(#)param.c 8.3 (Berkeley) 8/20/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 404bd49128SPeter Wemm #include "opt_param.h" 41ee342e1bSPeter Wemm #include "opt_maxusers.h" 4250c73f36SGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44ee342e1bSPeter Wemm #include <sys/systm.h> 45ee342e1bSPeter Wemm #include <sys/kernel.h> 467c45a9c4SAlfred Perlstein #include <sys/sysctl.h> 47df8bae1dSRodney W. Grimes 487419d1e2SDag-Erling Smørgrav #include <vm/vm_param.h> 49cbc89bfbSPaul Saab 50df8bae1dSRodney W. Grimes /* 51df8bae1dSRodney W. Grimes * System parameter formulae. 52df8bae1dSRodney W. Grimes */ 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #ifndef HZ 55ea35b592SMarius Strobl # if defined(__amd64__) || defined(__i386__) || defined(__ia64__) || defined(__sparc64__) 560c7d0f96SPoul-Henning Kamp # define HZ 1000 578f650450SPeter Wemm # else 58df8bae1dSRodney W. Grimes # define HZ 100 59df8bae1dSRodney W. Grimes # endif 607f03c419SMaxim Sobolev # ifndef HZ_VM 617f03c419SMaxim Sobolev # define HZ_VM 10 627f03c419SMaxim Sobolev # endif 637f03c419SMaxim Sobolev #else 647f03c419SMaxim Sobolev # ifndef HZ_VM 657f03c419SMaxim Sobolev # define HZ_VM HZ 667f03c419SMaxim Sobolev # endif 678f650450SPeter Wemm #endif 68ee342e1bSPeter Wemm #define NPROC (20 + 16 * maxusers) 69ee342e1bSPeter Wemm #ifndef NBUF 70ee342e1bSPeter Wemm #define NBUF 0 71ee342e1bSPeter Wemm #endif 725a00f364SDag-Erling Smørgrav #ifndef MAXFILES 73ee342e1bSPeter Wemm #define MAXFILES (maxproc * 2) 745a00f364SDag-Erling Smørgrav #endif 75dd0b2081SDavid Greenman 7698a44311SIvan Voras /* Values of enum VM_GUEST members are used as indices in 7798a44311SIvan Voras * vm_guest_sysctl_names */ 7898a44311SIvan Voras enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN }; 793dc30911SIvan Voras 803610a226SIvan Voras static int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS); 813610a226SIvan Voras 82ee342e1bSPeter Wemm int hz; 83ee342e1bSPeter Wemm int tick; 84ee342e1bSPeter Wemm int maxusers; /* base tunable */ 85ee342e1bSPeter Wemm int maxproc; /* maximum # of processes */ 86ee342e1bSPeter Wemm int maxprocperuid; /* max # of procs per user */ 87ee342e1bSPeter Wemm int maxfiles; /* sys. wide open files limit */ 88ee342e1bSPeter Wemm int maxfilesperproc; /* per-proc open files limit */ 89ee342e1bSPeter Wemm int ncallout; /* maximum # of timer events */ 90ee342e1bSPeter Wemm int nbuf; 91ee342e1bSPeter Wemm int nswbuf; 925bd65606SJohn Baldwin long maxswzone; /* max swmeta KVA storage */ 935bd65606SJohn Baldwin long maxbcache; /* max buffer cache KVA storage */ 945bd65606SJohn Baldwin u_long maxpipekva; /* Limit on pipe KVA */ 953dc30911SIvan Voras int vm_guest; /* Running as virtual machine guest? */ 967419d1e2SDag-Erling Smørgrav u_long maxtsiz; /* max text size */ 977419d1e2SDag-Erling Smørgrav u_long dfldsiz; /* initial data size limit */ 987419d1e2SDag-Erling Smørgrav u_long maxdsiz; /* max data size */ 997419d1e2SDag-Erling Smørgrav u_long dflssiz; /* initial stack size limit */ 1007419d1e2SDag-Erling Smørgrav u_long maxssiz; /* max stack size */ 1017419d1e2SDag-Erling Smørgrav u_long sgrowsiz; /* amount to grow stack */ 102df8bae1dSRodney W. Grimes 103b109dd74SPawel Jakub Dawidek SYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0, "ticks/second"); 104b109dd74SPawel Jakub Dawidek SYSCTL_INT(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0, 105b109dd74SPawel Jakub Dawidek "max swmeta KVA storage"); 106b109dd74SPawel Jakub Dawidek SYSCTL_INT(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0, 107b109dd74SPawel Jakub Dawidek "max buffer cache KVA storage"); 108b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0, 109b109dd74SPawel Jakub Dawidek "max text size"); 110b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0, 111b109dd74SPawel Jakub Dawidek "initial data size limit"); 112b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0, 113b109dd74SPawel Jakub Dawidek "max data size"); 114b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0, 115b109dd74SPawel Jakub Dawidek "initial stack size limit"); 116b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0, 117b109dd74SPawel Jakub Dawidek "max stack size"); 118b109dd74SPawel Jakub Dawidek SYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0, 119b109dd74SPawel Jakub Dawidek "amount to grow stack"); 1203610a226SIvan Voras SYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING, 1213610a226SIvan Voras NULL, 0, sysctl_kern_vm_guest, "A", 1223610a226SIvan Voras "Virtual machine detected? (none|generic|xen)"); 1237c45a9c4SAlfred Perlstein 124df8bae1dSRodney W. Grimes /* 125df8bae1dSRodney W. Grimes * These have to be allocated somewhere; allocating 126df8bae1dSRodney W. Grimes * them here forces loader errors if this file is omitted 127df8bae1dSRodney W. Grimes * (if they've been externed everywhere else; hah!). 128df8bae1dSRodney W. Grimes */ 12928f8db14SBruce Evans struct buf *swbuf; 1309722d88fSJason Evans 13134820bbfSBjoern A. Zeeb static const char *const vm_guest_sysctl_names[] = { 13234820bbfSBjoern A. Zeeb "none", 13334820bbfSBjoern A. Zeeb "generic", 13434820bbfSBjoern A. Zeeb "xen", 13534820bbfSBjoern A. Zeeb NULL 13634820bbfSBjoern A. Zeeb }; 13734820bbfSBjoern A. Zeeb 13834820bbfSBjoern A. Zeeb #ifndef XEN 1399bd2cbe4SJung-uk Kim static const char *const vm_bnames[] = { 1409bd2cbe4SJung-uk Kim "QEMU", /* QEMU */ 1419bd2cbe4SJung-uk Kim "Plex86", /* Plex86 */ 1429bd2cbe4SJung-uk Kim "Bochs", /* Bochs */ 1439bd2cbe4SJung-uk Kim NULL 1449bd2cbe4SJung-uk Kim }; 1459bd2cbe4SJung-uk Kim 1464da059f3SMaxim Sobolev static const char *const vm_pnames[] = { 1477f03c419SMaxim Sobolev "VMware Virtual Platform", /* VMWare VM */ 1487f03c419SMaxim Sobolev "Virtual Machine", /* Microsoft VirtualPC */ 1497f03c419SMaxim Sobolev "VirtualBox", /* Sun xVM VirtualBox */ 1507f03c419SMaxim Sobolev "Parallels Virtual Platform", /* Parallels VM */ 1517f03c419SMaxim Sobolev NULL 1527f03c419SMaxim Sobolev }; 1537f03c419SMaxim Sobolev 1543610a226SIvan Voras 1553610a226SIvan Voras /* 1563610a226SIvan Voras * Detect known Virtual Machine hosts by inspecting the emulated BIOS. 1573610a226SIvan Voras */ 1583dc30911SIvan Voras static enum VM_GUEST 1597f03c419SMaxim Sobolev detect_virtual(void) 1607f03c419SMaxim Sobolev { 1617f03c419SMaxim Sobolev char *sysenv; 1627f03c419SMaxim Sobolev int i; 1637f03c419SMaxim Sobolev 1649bd2cbe4SJung-uk Kim sysenv = getenv("smbios.bios.vendor"); 1659bd2cbe4SJung-uk Kim if (sysenv != NULL) { 1669bd2cbe4SJung-uk Kim for (i = 0; vm_bnames[i] != NULL; i++) 1679bd2cbe4SJung-uk Kim if (strcmp(sysenv, vm_bnames[i]) == 0) { 1689bd2cbe4SJung-uk Kim freeenv(sysenv); 1693dc30911SIvan Voras return (VM_GUEST_VM); 1709bd2cbe4SJung-uk Kim } 1719bd2cbe4SJung-uk Kim freeenv(sysenv); 1729bd2cbe4SJung-uk Kim } 1737f03c419SMaxim Sobolev sysenv = getenv("smbios.system.product"); 1747f03c419SMaxim Sobolev if (sysenv != NULL) { 1759bd2cbe4SJung-uk Kim for (i = 0; vm_pnames[i] != NULL; i++) 1769bd2cbe4SJung-uk Kim if (strcmp(sysenv, vm_pnames[i]) == 0) { 1779bd2cbe4SJung-uk Kim freeenv(sysenv); 1783dc30911SIvan Voras return (VM_GUEST_VM); 1797f03c419SMaxim Sobolev } 1809bd2cbe4SJung-uk Kim freeenv(sysenv); 1817f03c419SMaxim Sobolev } 1823dc30911SIvan Voras return (VM_GUEST_NO); 1837f03c419SMaxim Sobolev } 18434820bbfSBjoern A. Zeeb #endif 1857f03c419SMaxim Sobolev 1869722d88fSJason Evans /* 18766a11b9fSMatthew Dillon * Boot time overrides that are not scaled against main memory 188ee342e1bSPeter Wemm */ 189ee342e1bSPeter Wemm void 19066a11b9fSMatthew Dillon init_param1(void) 191ee342e1bSPeter Wemm { 1923dc30911SIvan Voras #ifndef XEN 1933dc30911SIvan Voras vm_guest = detect_virtual(); 1943dc30911SIvan Voras #else 1953dc30911SIvan Voras vm_guest = VM_GUEST_XEN; 1963dc30911SIvan Voras #endif 1977f03c419SMaxim Sobolev hz = -1; 198ee342e1bSPeter Wemm TUNABLE_INT_FETCH("kern.hz", &hz); 1999bd2cbe4SJung-uk Kim if (hz == -1) 2003dc30911SIvan Voras hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ; 201ee342e1bSPeter Wemm tick = 1000000 / hz; 202ee342e1bSPeter Wemm 203e1616f3aSMatthew Dillon #ifdef VM_SWZONE_SIZE_MAX 2042f9e4e80SMatthew Dillon maxswzone = VM_SWZONE_SIZE_MAX; 205e1616f3aSMatthew Dillon #endif 2065bd65606SJohn Baldwin TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone); 207e1616f3aSMatthew Dillon #ifdef VM_BCACHE_SIZE_MAX 2082f9e4e80SMatthew Dillon maxbcache = VM_BCACHE_SIZE_MAX; 209e1616f3aSMatthew Dillon #endif 2105bd65606SJohn Baldwin TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache); 211cbc89bfbSPaul Saab 212cbc89bfbSPaul Saab maxtsiz = MAXTSIZ; 2137419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz); 214cbc89bfbSPaul Saab dfldsiz = DFLDSIZ; 2157419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz); 216cbc89bfbSPaul Saab maxdsiz = MAXDSIZ; 2177419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz); 218cbc89bfbSPaul Saab dflssiz = DFLSSIZ; 2197419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz); 220cbc89bfbSPaul Saab maxssiz = MAXSSIZ; 2217419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz); 222cbc89bfbSPaul Saab sgrowsiz = SGROWSIZ; 2237419d1e2SDag-Erling Smørgrav TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz); 224ee342e1bSPeter Wemm } 22566a11b9fSMatthew Dillon 22666a11b9fSMatthew Dillon /* 22766a11b9fSMatthew Dillon * Boot time overrides that are scaled against main memory 22866a11b9fSMatthew Dillon */ 22966a11b9fSMatthew Dillon void 230447b3772SPeter Wemm init_param2(long physpages) 23166a11b9fSMatthew Dillon { 23266a11b9fSMatthew Dillon 23366a11b9fSMatthew Dillon /* Base parameters */ 2340b94a0e9SMatthew Dillon maxusers = MAXUSERS; 2350b94a0e9SMatthew Dillon TUNABLE_INT_FETCH("kern.maxusers", &maxusers); 2360b94a0e9SMatthew Dillon if (maxusers == 0) { 2374fbd563eSMatthew Dillon maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE); 23866a11b9fSMatthew Dillon if (maxusers < 32) 23966a11b9fSMatthew Dillon maxusers = 32; 2404fbd563eSMatthew Dillon if (maxusers > 384) 2414fbd563eSMatthew Dillon maxusers = 384; 24266a11b9fSMatthew Dillon } 24366a11b9fSMatthew Dillon 24466a11b9fSMatthew Dillon /* 24566a11b9fSMatthew Dillon * The following can be overridden after boot via sysctl. Note: 24666a11b9fSMatthew Dillon * unless overriden, these macros are ultimately based on maxusers. 24766a11b9fSMatthew Dillon */ 24866a11b9fSMatthew Dillon maxproc = NPROC; 24966a11b9fSMatthew Dillon TUNABLE_INT_FETCH("kern.maxproc", &maxproc); 25077a7d074SMike Silbersack /* 25177a7d074SMike Silbersack * Limit maxproc so that kmap entries cannot be exhausted by 25277a7d074SMike Silbersack * processes. 25377a7d074SMike Silbersack */ 25477a7d074SMike Silbersack if (maxproc > (physpages / 12)) 25577a7d074SMike Silbersack maxproc = physpages / 12; 25666a11b9fSMatthew Dillon maxfiles = MAXFILES; 25766a11b9fSMatthew Dillon TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles); 258ebacce5eSMike Silbersack maxprocperuid = (maxproc * 9) / 10; 259ebacce5eSMike Silbersack maxfilesperproc = (maxfiles * 9) / 10; 26066a11b9fSMatthew Dillon 26166a11b9fSMatthew Dillon /* 262347194c1SMike Silbersack * Cannot be changed after boot. 263347194c1SMike Silbersack */ 264347194c1SMike Silbersack nbuf = NBUF; 265347194c1SMike Silbersack TUNABLE_INT_FETCH("kern.nbuf", &nbuf); 266347194c1SMike Silbersack 267347194c1SMike Silbersack ncallout = 16 + maxproc + maxfiles; 268347194c1SMike Silbersack TUNABLE_INT_FETCH("kern.ncallout", &ncallout); 269347194c1SMike Silbersack } 270347194c1SMike Silbersack 271347194c1SMike Silbersack /* 2726819e13eSAlan Cox * Boot time overrides that are scaled against the kmem map 273347194c1SMike Silbersack */ 274347194c1SMike Silbersack void 275347194c1SMike Silbersack init_param3(long kmempages) 276347194c1SMike Silbersack { 2771dc10fceSAlan Cox 278347194c1SMike Silbersack /* 2796819e13eSAlan Cox * The default for maxpipekva is max(5% of the kmem map, 512KB). 2801dc10fceSAlan Cox * See sys_pipe.c for more details. 281289016f2SMike Silbersack */ 282cebde069SMike Silbersack maxpipekva = (kmempages / 20) * PAGE_SIZE; 283289016f2SMike Silbersack if (maxpipekva < 512 * 1024) 284289016f2SMike Silbersack maxpipekva = 512 * 1024; 2855bd65606SJohn Baldwin TUNABLE_ULONG_FETCH("kern.ipc.maxpipekva", &maxpipekva); 28666a11b9fSMatthew Dillon } 2873610a226SIvan Voras 2883610a226SIvan Voras /* 2893610a226SIvan Voras * Sysctl stringiying handler for kern.vm_guest. 2903610a226SIvan Voras */ 2913610a226SIvan Voras static int 2923610a226SIvan Voras sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS) 2933610a226SIvan Voras { 2943610a226SIvan Voras return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest], 2953610a226SIvan Voras strlen(vm_guest_sysctl_names[vm_guest]))); 2963610a226SIvan Voras } 297