xref: /illumos-gate/usr/src/uts/common/os/space.c (revision 6cdd461fe55315d9bc0ba40ea7da22cddbdcf784)
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 #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 for segkmem pages that should be resident
104  */
105 struct vnode kvps[KV_MAX];
106 
107 /*
108  * Data from swapgeneric.c that must be resident.
109  */
110 struct vnode *rootvp;		/* vnode of the root device */
111 dev_t rootdev;			/* dev_t of the root device */
112 boolean_t root_is_ramdisk;	/* root is ramdisk */
113 uint32_t ramdisk_size;		/* (KB) currently set only for sparc netboots */
114 
115 /*
116  * dhcp
117  */
118 #include <sys/socket.h>
119 #include <sys/errno.h>
120 #include <sys/sockio.h>
121 #include <sys/stream.h>
122 #include <sys/stropts.h>
123 #include <sys/dlpi.h>
124 #include <net/if.h>
125 
126 int netboot;
127 int obpdebug;
128 char *dhcack;		/* dhcp response packet */
129 int dhcacklen;
130 char *netdev_path;	/* Used to cache the netdev_path handed up by boot */
131 char dhcifname[IFNAMSIZ];
132 
133 /*
134  * Data from arp.c that must be resident.
135  */
136 #include <net/if_arp.h>
137 #include <netinet/in.h>
138 #include <netinet/in_var.h>
139 #include <netinet/if_ether.h>
140 
141 ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
142 
143 
144 /*
145  * Data from timod that must be resident
146  */
147 
148 /*
149  * state transition table for TI interface
150  */
151 #include <sys/tihdr.h>
152 
153 #define	nr	127		/* not reachable */
154 
155 char ti_statetbl[TE_NOEVENTS][TS_NOSTATES] = {
156 				/* STATES */
157 	/* 0  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16 */
158 
159 	{ 1, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
160 	{nr, nr, nr,  2, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
161 	{nr, nr, nr,  4, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
162 	{nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
163 	{nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
164 	{nr,  0,  3, nr,  3,  3, nr, nr,  7, nr, nr, nr,  6,  7,  9, 10, 11},
165 	{nr, nr,  0, nr, nr,  6, nr, nr, nr, nr, nr, nr,  3, nr,  3,  3,  3},
166 	{nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr,  3, nr, nr, nr},
167 	{nr, nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr,  3, nr, nr, nr},
168 	{nr, nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr,  7, nr, nr, nr},
169 	{nr, nr, nr,  5, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
170 	{nr, nr, nr, nr, nr, nr, nr,  8, nr, nr, nr, nr, nr, nr, nr, nr, nr},
171 	{nr, nr, nr, nr, nr, nr, 12, 13, nr, 14, 15, 16, nr, nr, nr, nr, nr},
172 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
173 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
174 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 10, nr,  3, nr, nr, nr, nr, nr},
175 	{nr, nr, nr,  7, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
176 	{nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
177 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
178 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
179 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 11,  3, nr, nr, nr, nr, nr, nr},
180 	{nr, nr, nr, nr, nr, nr,  3, nr, nr,  3,  3,  3, nr, nr, nr, nr, nr},
181 	{nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr},
182 	{nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
183 	{nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
184 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
185 	{nr, nr, nr,  3, 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 };
188 
189 
190 #include <sys/tty.h>
191 #include <sys/ptyvar.h>
192 
193 static void store_fetch_initspace();
194 
195 /*
196  * Allocate tunable structures at runtime.
197  */
198 void
199 space_init(void)
200 {
201 	pty_initspace();
202 	store_fetch_initspace();
203 }
204 
205 int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
206 
207 /*
208  * Previously defined in consmsconf.c ...
209  */
210 dev_t kbddev = NODEV;
211 dev_t mousedev = NODEV;
212 dev_t stdindev = NODEV;
213 dev_t diagdev = NODEV;
214 struct vnode *wsconsvp;
215 
216 dev_t fbdev = NODEV;
217 struct vnode *fbvp;
218 dev_info_t *fbdip;
219 
220 /*
221  * moved from cons.c because they must be resident in the kernel.
222  */
223 vnode_t	*rconsvp;
224 dev_t	rconsdev;
225 dev_t	uconsdev = NODEV;
226 
227 /*
228  * serial virtual console vnode pointer.
229  */
230 vnode_t		*vsconsvp = NULL;
231 
232 /*
233  * Flag whether console fb output is using PROM/PROM emulation
234  * terminal emulator, or is using the kernel terminal emulator.
235  */
236 int	consmode = CONS_FW;
237 
238 /*
239  * The following allows systems to disable use of the kernel
240  * terminal emulator (retreat to PROM terminal emulator if there
241  * is PROM).
242  */
243 int	cons_tem_disable;
244 
245 /*
246  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
247  * keyboard/frame buffer combination, aka the workstation console.
248  */
249 vnode_t *rwsconsvp;
250 dev_t	rwsconsdev;
251 
252 /*
253  * Platform console abort policy.
254  * Platforms may override the default software policy, if such hardware
255  * (e.g. keyswitches with a secure position) exists.
256  */
257 int abort_enable = KIOCABORTENABLE;
258 
259 /* from cpc.c */
260 uint_t kcpc_key;	/* TSD key for CPU performance counter context */
261 
262 /*
263  * storing and retrieving data by string key
264  *
265  * this mechanism allows a consumer to store and retrieve by name a pointer
266  * to some space maintained by the consumer.
267  * For example, a driver or module may want to have persistent data
268  * over unloading/loading cycles. The pointer is typically to some
269  * kmem_alloced space and it should not be pointing to data that will
270  * be destroyed when the module is unloaded.
271  */
272 static mod_hash_t *space_hash;
273 static char *space_hash_name = "space_hash";
274 static size_t	space_hash_nchains = 8;
275 
276 static void
277 store_fetch_initspace()
278 {
279 	space_hash = mod_hash_create_strhash(space_hash_name,
280 	    space_hash_nchains, mod_hash_null_valdtor);
281 	ASSERT(space_hash);
282 }
283 
284 int
285 space_store(char *key, uintptr_t ptr)
286 {
287 	char *s;
288 	int rval;
289 	size_t l;
290 
291 	/* some sanity checks first */
292 	if (key == NULL) {
293 		return (-1);
294 	}
295 	l = (size_t)strlen(key);
296 	if (l == 0) {
297 		return (-1);
298 	}
299 
300 	/* increment for null terminator */
301 	l++;
302 
303 	/* alloc space for the string, mod_hash_insert will deallocate */
304 	s = kmem_alloc(l, KM_SLEEP);
305 	bcopy(key, s, l);
306 
307 	rval = mod_hash_insert(space_hash,
308 	    (mod_hash_key_t)s, (mod_hash_val_t)ptr);
309 
310 	switch (rval) {
311 	case 0:
312 		break;
313 #ifdef DEBUG
314 	case MH_ERR_DUPLICATE:
315 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
316 		rval = -1;
317 		break;
318 	case MH_ERR_NOMEM:
319 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
320 		rval = -1;
321 		break;
322 	default:
323 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
324 		    key);
325 		rval = -1;
326 		break;
327 #else
328 	default:
329 		rval = -1;
330 		break;
331 #endif
332 	}
333 
334 	return (rval);
335 }
336 
337 uintptr_t
338 space_fetch(char *key)
339 {
340 	uintptr_t ptr = 0;
341 	mod_hash_val_t val;
342 	int rval;
343 
344 	if (key) {
345 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
346 		if (rval == 0) {
347 			ptr = (uintptr_t)val;
348 		}
349 	}
350 
351 	return (ptr);
352 }
353 
354 void
355 space_free(char *key)
356 {
357 	if (key) {
358 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
359 	}
360 }
361 
362 /*
363  * Support for CRC32.  At present all calculations are done in simple
364  * macros, so all we need is somewhere to declare the global lookup table.
365  */
366 
367 #include <sys/crc32.h>
368 
369 const uint32_t crc32_table[256] = { CRC32_TABLE };
370 
371 /*
372  * We need to fanout load from NIC which can overwhelm a single CPU.
373  * This becomes especially important on systems having slow CPUs
374  * (sun4v architecture). mac_soft_ring_enable is false on all
375  * systems except sun4v. On sun4v, they get enabled by default (see
376  * sun4v/os/mach_startup.c).
377  */
378 boolean_t	mac_soft_ring_enable = B_FALSE;
379 
380 /*
381  * Global iscsi boot prop
382  */
383 ib_boot_prop_t	*iscsiboot_prop = NULL;
384