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