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