gdb - Local mods (compile)
[dragonfly.git] / sys / kern / subr_param.c
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  * 3. 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  * $FreeBSD: src/sys/kern/subr_param.c,v 1.42.2.10 2002/03/09 21:05:47 silby Exp $
36  */
37
38 #include "opt_param.h"
39 #include "opt_maxusers.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
46 #include <vm/pmap.h>
47 #include <machine/vmparam.h>
48
49 /*
50  * System parameter formulae.
51  */
52
53 #ifndef HZ
54 #define HZ 100
55 #endif
56 #define NPROC (20 + 16 * maxusers)
57 #ifndef NBUF
58 #define NBUF 0
59 #endif
60 #ifndef MAXFILES
61 #define MAXFILES (maxproc * 16)
62 #endif
63 #ifndef MAXPOSIXLOCKSPERUID
64 #define MAXPOSIXLOCKSPERUID (maxusers * 64) /* Should be a safe value */
65 #endif
66
67 static int sysctl_kern_vmm_guest(SYSCTL_HANDLER_ARGS);
68
69 int     hz;
70 int     stathz;
71 int     profhz;
72 int     ustick;                         /* tick interval in microseconds */
73 int     nstick;                         /* tick interval in nanoseconds */
74 int     maxusers;                       /* base tunable */
75 int     maxproc;                        /* maximum # of processes */
76 int     maxprocperuid;                  /* max # of procs per user */
77 int     maxfiles;                       /* system wide open files limit */
78 int     maxfilesrootres;                /* descriptors reserved for root use */
79 int     minfilesperproc;                /* per-proc min open files (safety) */
80 int     maxfilesperproc;                /* per-proc open files limit */
81 int     maxfilesperuser;                /* per-user open files limit */
82 int     maxposixlocksperuid;            /* max # POSIX locks per uid */
83 int     ncallout;                       /* maximum # of timer events */
84 int     mbuf_wait = 32;                 /* mbuf sleep time in ticks */
85 long    nbuf;
86 long    nswbuf;
87 long    maxswzone;                      /* max swmeta KVA storage */
88 long    maxbcache;                      /* max buffer cache KVA storage */
89 enum vmm_guest_type vmm_guest = VMM_GUEST_NONE; /* Running as VM guest? */
90 u_quad_t        maxtsiz;                        /* max text size */
91 u_quad_t        dfldsiz;                        /* initial data size limit */
92 u_quad_t        maxdsiz;                        /* max data size */
93 u_quad_t        dflssiz;                        /* initial stack size limit */
94 u_quad_t        maxssiz;                        /* max stack size */
95 u_quad_t        sgrowsiz;                       /* amount to grow stack */
96
97 SYSCTL_PROC(_kern, OID_AUTO, vmm_guest, CTLFLAG_RD | CTLTYPE_STRING,
98     NULL, 0, sysctl_kern_vmm_guest, "A",
99     "Virtual machine guest type");
100
101 /*
102  * These have to be allocated somewhere; allocating
103  * them here forces loader errors if this file is omitted
104  * (if they've been externed everywhere else; hah!).
105  */
106 struct  buf *swbuf;
107
108 struct vmm_bname {
109         const char *str;
110         enum vmm_guest_type type;
111 };
112
113 static struct vmm_bname vmm_bnames[] = {
114         { "QEMU",       VMM_GUEST_QEMU },       /* QEMU */
115         { "Plex86",     VMM_GUEST_PLEX86 },     /* Plex86 */
116         { "Bochs",      VMM_GUEST_BOCHS },      /* Bochs */
117         { "Xen",        VMM_GUEST_XEN },        /* Xen */
118         { "BHYVE",      VMM_GUEST_BHYVE },      /* bhyve */
119         { "Seabios",    VMM_GUEST_KVM},         /* KVM */
120         { NULL, 0 }
121 };
122
123 static struct vmm_bname vmm_pnames[] = {
124         { "VMware Virtual Platform",    VMM_GUEST_VMWARE },     /* VMWare VM */
125         { "Virtual Machine",            VMM_GUEST_VPC },        /* M$ VirtualPC */
126         { "VirtualBox",                 VMM_GUEST_VBOX },       /* Sun VirtualBox */
127         { "Parallels Virtual Platform", VMM_GUEST_PARALLELS },  /* Parallels VM */
128         { "KVM",                        VMM_GUEST_KVM },        /* KVM */
129         { NULL, 0 }
130 };
131
132 static const char *const vmm_guest_sysctl_names[] = {
133         "none",
134         "qemu",
135         "plex86",
136         "bochs",
137         "xen",
138         "bhyve",
139         "kvm",
140         "vmware",
141         "vpc",
142         "vbox",
143         "parallels",
144         "vkernel",
145         "unknown",
146         NULL
147 };
148 CTASSERT(NELEM(vmm_guest_sysctl_names) - 1 == VMM_GUEST_LAST);
149
150 /*
151  * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
152  */
153 enum vmm_guest_type
154 detect_virtual(void)
155 {
156         char *sysenv;
157         int i;
158
159         sysenv = kgetenv("smbios.bios.vendor");
160         if (sysenv != NULL) {
161                 for (i = 0; vmm_bnames[i].str != NULL; i++)
162                         if (strcmp(sysenv, vmm_bnames[i].str) == 0) {
163                                 kfreeenv(sysenv);
164                                 return (vmm_bnames[i].type);
165                         }
166                 kfreeenv(sysenv);
167         }
168         sysenv = kgetenv("smbios.system.product");
169         if (sysenv != NULL) {
170                 for (i = 0; vmm_pnames[i].str != NULL; i++)
171                         if (strcmp(sysenv, vmm_pnames[i].str) == 0) {
172                                 kfreeenv(sysenv);
173                                 return (vmm_pnames[i].type);
174                         }
175                 kfreeenv(sysenv);
176         }
177         return (VMM_GUEST_NONE);
178 }
179
180 /*
181  * Boot time overrides that are not scaled against main memory
182  */
183 void
184 init_param1(void)
185 {
186         hz = HZ;
187         TUNABLE_INT_FETCH("kern.hz", &hz);
188         stathz = hz * 128 / 100;
189         profhz = stathz;
190         ustick = 1000000 / hz;
191         nstick = 1000000000 / hz;
192         /* can adjust 30ms in 60s */
193         ntp_default_tick_delta = howmany(30000000, 60 * hz);
194
195 #ifdef VM_SWZONE_SIZE_MAX
196         maxswzone = VM_SWZONE_SIZE_MAX;
197 #endif
198         TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
199 #ifdef VM_BCACHE_SIZE_MAX
200         maxbcache = VM_BCACHE_SIZE_MAX;
201 #endif
202         TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
203         maxtsiz = MAXTSIZ;
204         TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz);
205         dfldsiz = DFLDSIZ;
206         TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz);
207         maxdsiz = MAXDSIZ;
208         TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz);
209         dflssiz = DFLSSIZ;
210         TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz);
211         maxssiz = MAXSSIZ;
212         TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz);
213         sgrowsiz = SGROWSIZ;
214         TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz);
215 }
216
217 /*
218  * Boot time overrides that are scaled against main memory
219  */
220 void
221 init_param2(int physpages)
222 {
223         size_t limsize;
224
225         /*
226          * Calculate manually becaus the VM page queues / system is not set up yet
227          */
228         limsize = (size_t)physpages * PAGE_SIZE;
229         if (limsize > KvaSize)
230                 limsize = KvaSize;
231         limsize /= 1024 * 1024;         /* smaller of KVM or physmem in MB */
232
233         /* Base parameters */
234         maxusers = MAXUSERS;
235         TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
236         if (maxusers == 0) {
237                 maxusers = limsize / 8;         /* ~384 per 3G */
238                 if (maxusers < 32)
239                         maxusers = 32;
240                 /* no upper limit */
241         }
242
243         /*
244          * The following can be overridden after boot via sysctl.  Note:
245          * unless overriden, these macros are ultimately based on maxusers.
246          *
247          * Limit maxproc so that kmap entries cannot be exhausted by
248          * processes.
249          */
250         maxproc = NPROC;
251         TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
252         if (maxproc < 32)
253                 maxproc = 32;
254         if (maxproc > limsize * 21)
255                 maxproc = limsize * 21;
256
257         /*
258          * Maximum number of open files
259          */
260         maxfiles = MAXFILES;
261         TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
262         if (maxfiles < 128)
263                 maxfiles = 128;
264
265         /*
266          * Limit file descriptors so no single user can exhaust the
267          * system.
268          *
269          * WARNING: Do not set minfilesperproc too high or the user
270          *          can exhaust the system with a combination of fork()
271          *          and open().  Actual worst case is:
272          *
273          *          (minfilesperproc * maxprocperuid) + maxfilesperuser
274          */
275         maxprocperuid = maxproc / 4;
276         if (maxprocperuid < 128)
277                 maxprocperuid = maxproc / 2;
278         minfilesperproc = 8;
279         maxfilesperproc = maxfiles / 4;
280         maxfilesperuser = maxfilesperproc * 2;
281         maxfilesrootres = maxfiles / 20;
282
283         /*
284          * Severe hack to try to prevent pipe() descriptors from
285          * blowing away kernel memory.
286          */
287         if (KvaSize <= (vm_offset_t)(1536LL * 1024 * 1024) &&
288             maxfilesperuser > 20000) {
289                 maxfilesperuser = 20000;
290         }
291
292         maxposixlocksperuid = MAXPOSIXLOCKSPERUID;
293         TUNABLE_INT_FETCH("kern.maxposixlocksperuid", &maxposixlocksperuid);
294
295         /*
296          * Unless overriden, NBUF is typically 0 (auto-sized later).
297          */
298         nbuf = NBUF;
299         TUNABLE_LONG_FETCH("kern.nbuf", &nbuf);
300
301         ncallout = 16 + maxproc + maxfiles;
302         TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
303 }
304
305 /*
306  * Sysctl stringifying handler for kern.vmm_guest.
307  */
308 static int
309 sysctl_kern_vmm_guest(SYSCTL_HANDLER_ARGS)
310 {
311         return (SYSCTL_OUT(req, vmm_guest_sysctl_names[vmm_guest], 
312             strlen(vmm_guest_sysctl_names[vmm_guest])));
313 }