xref: /illumos-gate/usr/src/uts/common/os/space.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
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  * Copyright 2020 Joyent, Inc.
27  */
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 #include <sys/consdev.h>
63 #include <sys/wscons.h>
64 #include <sys/bootprops.h>
65 
66 struct	buf	bfreelist;	/* Head of the free list of buffers */
67 
68 sysinfo_t	sysinfo;
69 vminfo_t	vminfo;		/* VM stats protected by sysinfolock mutex */
70 
71 #ifdef	lint
72 int	__lintzero;		/* Alway zero for shutting up lint */
73 #endif
74 
75 /*
76  * The following describe the physical memory configuration.
77  *
78  *	physmem	 -  The amount of physical memory configured
79  *		    in pages.  ptob(physmem) is the amount
80  *		    of physical memory in bytes.  Defined in
81  *		    .../os/startup.c.
82  *
83  *	physmax  -  The highest numbered physical page in memory.
84  *
85  *	maxmem	 -  Maximum available memory, in pages.  Defined
86  *		    in main.c.
87  *
88  *	physinstalled
89  *		 -  Pages of physical memory installed;
90  *		    includes use by PROM/boot not counted in
91  *		    physmem.
92  */
93 
94 pfn_t	physmax;
95 pgcnt_t	physinstalled;
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 for segkmem pages that should be resident
105  */
106 struct vnode kvps[KV_MAX];
107 
108 /*
109  * Data from swapgeneric.c that must be resident.
110  */
111 struct vnode *rootvp;		/* vnode of the root device */
112 dev_t rootdev;			/* dev_t of the root device */
113 boolean_t root_is_ramdisk;	/* root is ramdisk */
114 uint32_t ramdisk_size;		/* (KB) currently set only for sparc netboots */
115 
116 /*
117  * dhcp
118  */
119 #include <sys/socket.h>
120 #include <sys/errno.h>
121 #include <sys/sockio.h>
122 #include <sys/stream.h>
123 #include <sys/stropts.h>
124 #include <sys/dlpi.h>
125 #include <net/if.h>
126 
127 int netboot;
128 int obpdebug;
129 char *dhcack;		/* dhcp response packet */
130 int dhcacklen;
131 char *netdev_path;	/* Used to cache the netdev_path handed up by boot */
132 char dhcifname[IFNAMSIZ];
133 
134 /*
135  * Data from arp.c that must be resident.
136  */
137 #include <net/if_arp.h>
138 #include <netinet/in.h>
139 #include <netinet/in_var.h>
140 #include <netinet/if_ether.h>
141 
142 ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
143 
144 #include <sys/tty.h>
145 #include <sys/ptyvar.h>
146 
147 static void store_fetch_initspace();
148 
149 /*
150  * Allocate tunable structures at runtime.
151  */
152 void
153 space_init(void)
154 {
155 	pty_initspace();
156 	store_fetch_initspace();
157 }
158 
159 int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
160 
161 /*
162  * Previously defined in consmsconf.c ...
163  */
164 dev_t kbddev = NODEV;
165 dev_t mousedev = NODEV;
166 dev_t stdindev = NODEV;
167 dev_t diagdev = NODEV;
168 struct vnode *wsconsvp;
169 
170 dev_t fbdev = NODEV;
171 struct vnode *fbvp;
172 dev_info_t *fbdip;
173 
174 /*
175  * moved from cons.c because they must be resident in the kernel.
176  */
177 vnode_t	*rconsvp;
178 dev_t	rconsdev;
179 dev_t	uconsdev = NODEV;
180 
181 /*
182  * serial virtual console vnode pointer.
183  */
184 vnode_t		*vsconsvp = NULL;
185 
186 /*
187  * Flag whether console fb output is using PROM/PROM emulation
188  * terminal emulator, or is using the kernel terminal emulator.
189  */
190 int	consmode = CONS_FW;
191 
192 /*
193  * The following allows systems to disable use of the kernel
194  * terminal emulator (retreat to PROM terminal emulator if there
195  * is PROM).
196  */
197 int	cons_tem_disable;
198 
199 /*
200  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
201  * keyboard/frame buffer combination, aka the workstation console.
202  */
203 vnode_t *rwsconsvp;
204 dev_t	rwsconsdev;
205 
206 /*
207  * Platform console abort policy.
208  * Platforms may override the default software policy, if such hardware
209  * (e.g. keyswitches with a secure position) exists.
210  */
211 int abort_enable = KIOCABORTENABLE;
212 
213 /* from cpc.c */
214 uint_t kcpc_key;	/* TSD key for CPU performance counter context */
215 
216 /*
217  * storing and retrieving data by string key
218  *
219  * this mechanism allows a consumer to store and retrieve by name a pointer
220  * to some space maintained by the consumer.
221  * For example, a driver or module may want to have persistent data
222  * over unloading/loading cycles. The pointer is typically to some
223  * kmem_alloced space and it should not be pointing to data that will
224  * be destroyed when the module is unloaded.
225  */
226 static mod_hash_t *space_hash;
227 static char *space_hash_name = "space_hash";
228 static size_t	space_hash_nchains = 8;
229 
230 static void
231 store_fetch_initspace()
232 {
233 	space_hash = mod_hash_create_strhash(space_hash_name,
234 	    space_hash_nchains, mod_hash_null_valdtor);
235 	ASSERT(space_hash);
236 }
237 
238 int
239 space_store(char *key, uintptr_t ptr)
240 {
241 	char *s;
242 	int rval;
243 	size_t l;
244 
245 	/* some sanity checks first */
246 	if (key == NULL) {
247 		return (-1);
248 	}
249 	l = (size_t)strlen(key);
250 	if (l == 0) {
251 		return (-1);
252 	}
253 
254 	/* increment for null terminator */
255 	l++;
256 
257 	/* alloc space for the string, mod_hash_insert will deallocate */
258 	s = kmem_alloc(l, KM_SLEEP);
259 	bcopy(key, s, l);
260 
261 	rval = mod_hash_insert(space_hash,
262 	    (mod_hash_key_t)s, (mod_hash_val_t)ptr);
263 
264 	switch (rval) {
265 	case 0:
266 		break;
267 #ifdef DEBUG
268 	case MH_ERR_DUPLICATE:
269 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
270 		rval = -1;
271 		break;
272 	case MH_ERR_NOMEM:
273 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
274 		rval = -1;
275 		break;
276 	default:
277 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
278 		    key);
279 		rval = -1;
280 		break;
281 #else
282 	default:
283 		rval = -1;
284 		break;
285 #endif
286 	}
287 
288 	return (rval);
289 }
290 
291 uintptr_t
292 space_fetch(char *key)
293 {
294 	uintptr_t ptr = 0;
295 	mod_hash_val_t val;
296 	int rval;
297 
298 	if (key) {
299 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
300 		if (rval == 0) {
301 			ptr = (uintptr_t)val;
302 		}
303 	}
304 
305 	return (ptr);
306 }
307 
308 void
309 space_free(char *key)
310 {
311 	if (key) {
312 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
313 	}
314 }
315 
316 /*
317  * Support for CRC32.  At present all calculations are done in simple
318  * macros, so all we need is somewhere to declare the global lookup table.
319  */
320 
321 #include <sys/crc32.h>
322 
323 const uint32_t crc32_table[256] = { CRC32_TABLE };
324 
325 /*
326  * We need to fanout load from NIC which can overwhelm a single CPU.
327  * This becomes especially important on systems having slow CPUs
328  * (sun4v architecture). mac_soft_ring_enable is false on all
329  * systems except sun4v. On sun4v, they get enabled by default (see
330  * sun4v/os/mach_startup.c).
331  */
332 boolean_t	mac_soft_ring_enable = B_FALSE;
333 
334 /*
335  * Global iscsi boot prop
336  */
337 ib_boot_prop_t	*iscsiboot_prop = NULL;
338