xref: /illumos-gate/usr/src/uts/common/os/space.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * The intent of this file is to contain any data that must remain
31  * resident in the kernel.
32  *
33  * space_store(), space_fetch(), and space_free() have been added to
34  * easily store and retrieve kernel resident data.
35  * These functions are recommended rather than adding new variables to
36  * this file.
37  *
38  * Note that it's possible for name collisions to occur.  In order to
39  * prevent collisions, it's recommended that the convention in
40  * PSARC/1997/389 be used.  If a collision occurs, then space_store will
41  * fail.
42  */
43 
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/var.h>
47 #include <sys/proc.h>
48 #include <sys/signal.h>
49 #include <sys/utsname.h>
50 #include <sys/buf.h>
51 #include <sys/cred.h>
52 #include <sys/vfs.h>
53 #include <sys/vnode.h>
54 #include <sys/sysinfo.h>
55 #include <sys/t_lock.h>
56 #include <sys/vmem.h>
57 #include <sys/modhash.h>
58 #include <sys/cmn_err.h>
59 
60 #include <sys/strredir.h>
61 #include <sys/kbio.h>
62 
63 struct	buf	bfreelist;	/* Head of the free list of buffers */
64 
65 sysinfo_t	sysinfo;
66 vminfo_t	vminfo;		/* VM stats protected by sysinfolock mutex */
67 
68 #ifdef	lint
69 int	__lintzero;		/* Alway zero for shutting up lint */
70 #endif
71 
72 /*
73  * The following describe the physical memory configuration.
74  *
75  *	physmem	 -  The amount of physical memory configured
76  *		    in pages.  ptob(physmem) is the amount
77  *		    of physical memory in bytes.  Defined in
78  *		    .../os/startup.c.
79  *
80  *	physmax  -  The highest numbered physical page in memory.
81  *
82  *	maxmem	 -  Maximum available memory, in pages.  Defined
83  *		    in main.c.
84  *
85  *	physinstalled
86  *		 -  Pages of physical memory installed;
87  *		    includes use by PROM/boot not counted in
88  *		    physmem.
89  */
90 
91 pfn_t	physmax;
92 pgcnt_t	physinstalled;
93 
94 struct var v;
95 
96 #include <sys/systm.h>
97 #include <sys/conf.h>
98 #include <sys/kmem.h>
99 #include <sys/sysmacros.h>
100 #include <sys/bootconf.h>
101 
102 /*
103  * Data from swapgeneric.c that must be resident.
104  */
105 struct vnode *rootvp;		/* vnode of the root device */
106 dev_t rootdev;			/* dev_t of the root device */
107 int root_is_svm;		/* root is a mirrored device flag */
108 
109 int netboot;
110 int obpdebug;
111 char *dhcack;	/* Used to cache ascii form of DHCPACK handed up by boot */
112 char *netdev_path;	/* Used to cache the netdev_path handed up by boot */
113 
114 /*
115  * Data from arp.c that must be resident.
116  */
117 #include <sys/socket.h>
118 #include <sys/errno.h>
119 #include <sys/sockio.h>
120 #include <sys/stream.h>
121 #include <sys/stropts.h>
122 #include <sys/dlpi.h>
123 #include <net/if.h>
124 #include <net/if_arp.h>
125 #include <netinet/in.h>
126 #include <netinet/in_var.h>
127 #include <netinet/if_ether.h>
128 
129 ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
130 
131 /*
132  * Data from timod that must be resident
133  */
134 
135 /*
136  * state transition table for TI interface
137  */
138 #include <sys/tihdr.h>
139 
140 #define	nr	127		/* not reachable */
141 
142 char ti_statetbl[TE_NOEVENTS][TS_NOSTATES] = {
143 				/* STATES */
144 	/* 0  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16 */
145 
146 	{ 1, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
147 	{nr, nr, nr,  2, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
148 	{nr, nr, nr,  4, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
149 	{nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
150 	{nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
151 	{nr,  0,  3, nr,  3,  3, nr, nr,  7, nr, nr, nr,  6,  7,  9, 10, 11},
152 	{nr, nr,  0, nr, nr,  6, nr, nr, nr, nr, nr, nr,  3, nr,  3,  3,  3},
153 	{nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr,  3, nr, nr, nr},
154 	{nr, nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr,  3, nr, nr, nr},
155 	{nr, nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr,  7, nr, nr, nr},
156 	{nr, nr, nr,  5, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
157 	{nr, nr, nr, nr, nr, nr, nr,  8, nr, nr, nr, nr, nr, nr, nr, nr, nr},
158 	{nr, nr, nr, nr, nr, nr, 12, 13, nr, 14, 15, 16, nr, nr, nr, nr, nr},
159 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
160 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
161 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 10, nr,  3, nr, nr, nr, nr, nr},
162 	{nr, nr, nr,  7, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
163 	{nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
164 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
165 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
166 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 11,  3, nr, nr, nr, nr, nr, nr},
167 	{nr, nr, nr, nr, nr, nr,  3, nr, nr,  3,  3,  3, nr, nr, nr, nr, nr},
168 	{nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr},
169 	{nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
170 	{nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
171 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
172 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
173 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
174 };
175 
176 
177 #include <sys/sad.h>
178 #include <sys/tty.h>
179 #include <sys/ptyvar.h>
180 
181 static void store_fetch_initspace();
182 
183 /*
184  * Allocate tunable structures at runtime.
185  */
186 void
187 space_init(void)
188 {
189 	sad_initspace();
190 	pty_initspace();
191 	store_fetch_initspace();
192 }
193 
194 int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
195 
196 /*
197  * Previously defined in consmsconf.c ...
198  */
199 dev_t kbddev = NODEV;
200 dev_t mousedev = NODEV;
201 dev_t stdindev = NODEV;
202 struct vnode *wsconsvp;
203 
204 dev_t fbdev = NODEV;
205 struct vnode *fbvp;
206 dev_info_t *fbdip;
207 
208 /*
209  * moved from cons.c because they must be resident in the kernel.
210  */
211 vnode_t	*rconsvp;
212 dev_t	rconsdev;
213 dev_t	uconsdev = NODEV;
214 
215 /*
216  * This flag, when set marks rconsvp in a transition state.
217  */
218 
219 int	cn_conf;
220 
221 /*
222  * Moved from sad_conf.c because of the usual in loadable modules
223  */
224 
225 #ifndef NSTRPHASH
226 #define	NSTRPHASH	128
227 #endif
228 struct autopush **strpcache;
229 int strpmask = NSTRPHASH - 1;
230 
231 /*
232  * Moved here from wscons.c
233  * Package the redirection-related routines into an ops vector of the form
234  * that the redirecting driver expects.
235  */
236 extern int wcvnget();
237 extern void wcvnrele();
238 srvnops_t	wscons_srvnops = {
239 	wcvnget,
240 	wcvnrele
241 };
242 
243 /*
244  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
245  * keyboard/frame buffer combination, aka the workstation console.
246  */
247 
248 vnode_t *rwsconsvp;
249 dev_t	rwsconsdev;
250 
251 /*
252  * Platform console abort policy.
253  * Platforms may override the default software policy, if such hardware
254  * (e.g. keyswitches with a secure position) exists.
255  */
256 int abort_enable = KIOCABORTENABLE;
257 
258 /* from iwscons.c */
259 
260 kthread_id_t	iwscn_thread;	/* thread that is allowed to push redirm */
261 wcm_data_t	*iwscn_wcm_data; /* allocated data for redirm */
262 
263 /* from cpc.c */
264 uint_t kcpc_key;	/* TSD key for CPU performance counter context */
265 
266 /*
267  * storing and retrieving data by string key
268  *
269  * this mechanism allows a consumer to store and retrieve by name a pointer
270  * to some space maintained by the consumer.
271  * For example, a driver or module may want to have persistent data
272  * over unloading/loading cycles. The pointer is typically to some
273  * kmem_alloced space and it should not be pointing to data that will
274  * be destroyed when the module is unloaded.
275  */
276 static mod_hash_t *space_hash;
277 static char *space_hash_name = "space_hash";
278 static size_t	space_hash_nchains = 8;
279 
280 static void
281 store_fetch_initspace()
282 {
283 	space_hash = mod_hash_create_strhash(space_hash_name,
284 		space_hash_nchains, mod_hash_null_valdtor);
285 	ASSERT(space_hash);
286 }
287 
288 int
289 space_store(char *key, uintptr_t ptr)
290 {
291 	char *s;
292 	int rval;
293 	size_t l;
294 
295 	/* some sanity checks first */
296 	if (key == NULL) {
297 		return (-1);
298 	}
299 	l = (size_t)strlen(key);
300 	if (l == 0) {
301 		return (-1);
302 	}
303 
304 	/* increment for null terminator */
305 	l++;
306 
307 	/* alloc space for the string, mod_hash_insert will deallocate */
308 	s = kmem_alloc(l, KM_SLEEP);
309 	bcopy(key, s, l);
310 
311 	rval = mod_hash_insert(space_hash,
312 		(mod_hash_key_t)s, (mod_hash_val_t)ptr);
313 
314 	switch (rval) {
315 	case 0:
316 		break;
317 #ifdef DEBUG
318 	case MH_ERR_DUPLICATE:
319 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
320 		rval = -1;
321 		break;
322 	case MH_ERR_NOMEM:
323 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
324 		rval = -1;
325 		break;
326 	default:
327 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
328 		    key);
329 		rval = -1;
330 		break;
331 #else
332 	default:
333 		rval = -1;
334 		break;
335 #endif
336 	}
337 
338 	return (rval);
339 }
340 
341 uintptr_t
342 space_fetch(char *key)
343 {
344 	uintptr_t ptr = 0;
345 	mod_hash_val_t val;
346 	int rval;
347 
348 	if (key) {
349 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
350 		if (rval == 0) {
351 			ptr = (uintptr_t)val;
352 		}
353 	}
354 
355 	return (ptr);
356 }
357 
358 void
359 space_free(char *key)
360 {
361 	if (key) {
362 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
363 	}
364 }
365 
366 /*
367  * Support for CRC32.  At present all calculations are done in simple
368  * macros, so all we need is somewhere to declare the global lookup table.
369  */
370 
371 #include <sys/crc32.h>
372 
373 const uint32_t crc32_table[256] = { CRC32_TABLE };
374