kern: fix integer underflow in exec_shell_imgact.
[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  * $DragonFly: src/sys/kern/subr_param.c,v 1.7 2005/06/26 22:03:22 dillon Exp $
37  */
38
39 #include "opt_param.h"
40 #include "opt_maxusers.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.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 int     hz;
68 int     stathz;
69 int     profhz;
70 int     ustick;                         /* tick interval in microseconds */
71 int     nstick;                         /* tick interval in nanoseconds */
72 int     maxusers;                       /* base tunable */
73 int     maxproc;                        /* maximum # of processes */
74 int     maxprocperuid;                  /* max # of procs per user */
75 int     maxfiles;                       /* system wide open files limit */
76 int     maxfilesrootres;                /* descriptors reserved for root use */
77 int     minfilesperproc;                /* per-proc min open files (safety) */
78 int     maxfilesperproc;                /* per-proc open files limit */
79 int     maxfilesperuser;                /* per-user open files limit */
80 int     maxposixlocksperuid;            /* max # POSIX locks per uid */
81 int     ncallout;                       /* maximum # of timer events */
82 int     mbuf_wait = 32;                 /* mbuf sleep time in ticks */
83 long    nbuf;
84 long    nswbuf;
85 long    maxswzone;                      /* max swmeta KVA storage */
86 long    maxbcache;                      /* max buffer cache KVA storage */
87 int     vmm_guest = 0;                  /* Running as virtual machine guest? */
88 u_quad_t        maxtsiz;                        /* max text size */
89 u_quad_t        dfldsiz;                        /* initial data size limit */
90 u_quad_t        maxdsiz;                        /* max data size */
91 u_quad_t        dflssiz;                        /* initial stack size limit */
92 u_quad_t        maxssiz;                        /* max stack size */
93 u_quad_t        sgrowsiz;                       /* amount to grow stack */
94
95 /*
96  * These have to be allocated somewhere; allocating
97  * them here forces loader errors if this file is omitted
98  * (if they've been externed everywhere else; hah!).
99  */
100 struct  buf *swbuf;
101
102 /*
103  * Boot time overrides that are not scaled against main memory
104  */
105 void
106 init_param1(void)
107 {
108         hz = HZ;
109         TUNABLE_INT_FETCH("kern.hz", &hz);
110         stathz = hz * 128 / 100;
111         profhz = stathz;
112         ustick = 1000000 / hz;
113         nstick = 1000000000 / hz;
114         /* can adjust 30ms in 60s */
115         ntp_default_tick_delta = howmany(30000000, 60 * hz);
116
117 #ifdef VM_SWZONE_SIZE_MAX
118         maxswzone = VM_SWZONE_SIZE_MAX;
119 #endif
120         TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
121 #ifdef VM_BCACHE_SIZE_MAX
122         maxbcache = VM_BCACHE_SIZE_MAX;
123 #endif
124         TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
125         maxtsiz = MAXTSIZ;
126         TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz);
127         dfldsiz = DFLDSIZ;
128         TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz);
129         maxdsiz = MAXDSIZ;
130         TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz);
131         dflssiz = DFLSSIZ;
132         TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz);
133         maxssiz = MAXSSIZ;
134         TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz);
135         sgrowsiz = SGROWSIZ;
136         TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz);
137 }
138
139 /*
140  * Boot time overrides that are scaled against main memory
141  */
142 void
143 init_param2(int physpages)
144 {
145         size_t limsize;
146
147         /*
148          * Calculate manually becaus the VM page queues / system is not set up yet
149          */
150         limsize = (size_t)physpages * PAGE_SIZE;
151         if (limsize > KvaSize)
152                 limsize = KvaSize;
153         limsize /= 1024 * 1024;         /* smaller of KVM or physmem in MB */
154
155         /* Base parameters */
156         maxusers = MAXUSERS;
157         TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
158         if (maxusers == 0) {
159                 maxusers = limsize / 8;         /* ~384 per 3G */
160                 if (maxusers < 32)
161                         maxusers = 32;
162                 /* no upper limit */
163         }
164
165         /*
166          * The following can be overridden after boot via sysctl.  Note:
167          * unless overriden, these macros are ultimately based on maxusers.
168          *
169          * Limit maxproc so that kmap entries cannot be exhausted by
170          * processes.
171          */
172         maxproc = NPROC;
173         TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
174         if (maxproc < 32)
175                 maxproc = 32;
176         if (maxproc > limsize * 21)
177                 maxproc = limsize * 21;
178
179         /*
180          * Maximum number of open files
181          */
182         maxfiles = MAXFILES;
183         TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
184         if (maxfiles < 128)
185                 maxfiles = 128;
186
187         /*
188          * Limit file descriptors so no single user can exhaust the
189          * system.
190          *
191          * WARNING: Do not set minfilesperproc too high or the user
192          *          can exhaust the system with a combination of fork()
193          *          and open().  Actual worst case is:
194          *
195          *          (minfilesperproc * maxprocperuid) + maxfilesperuser
196          */
197         maxprocperuid = maxproc / 4;
198         if (maxprocperuid < 128)
199                 maxprocperuid = maxproc / 2;
200         minfilesperproc = 8;
201         maxfilesperproc = maxfiles / 4;
202         maxfilesperuser = maxfilesperproc * 2;
203         maxfilesrootres = maxfiles / 20;
204
205         /*
206          * Severe hack to try to prevent pipe() descriptors from
207          * blowing away kernel memory.
208          */
209         if (KvaSize <= (vm_offset_t)(1536LL * 1024 * 1024) &&
210             maxfilesperuser > 20000) {
211                 maxfilesperuser = 20000;
212         }
213
214         maxposixlocksperuid = MAXPOSIXLOCKSPERUID;
215         TUNABLE_INT_FETCH("kern.maxposixlocksperuid", &maxposixlocksperuid);
216
217         /*
218          * Unless overriden, NBUF is typically 0 (auto-sized later).
219          */
220         nbuf = NBUF;
221         TUNABLE_LONG_FETCH("kern.nbuf", &nbuf);
222
223         ncallout = 16 + maxproc + maxfiles;
224         TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
225 }
226