xref: /illumos-gate/usr/src/uts/common/os/space.c (revision b8ffbd31)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq  * Common Development and Distribution License (the "License").
6fea9cb91Slq  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq 
227c478bd9Sstevel@tonic-gate /*
23361ed64aSJames Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
255f10ef69SYuri Pankov  * Copyright 2016 Nexenta Systems, Inc.
26*b8ffbd31SJohn Levon  * Copyright 2020 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * The intent of this file is to contain any data that must remain
317c478bd9Sstevel@tonic-gate  * resident in the kernel.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * space_store(), space_fetch(), and space_free() have been added to
347c478bd9Sstevel@tonic-gate  * easily store and retrieve kernel resident data.
357c478bd9Sstevel@tonic-gate  * These functions are recommended rather than adding new variables to
367c478bd9Sstevel@tonic-gate  * this file.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Note that it's possible for name collisions to occur.  In order to
397c478bd9Sstevel@tonic-gate  * prevent collisions, it's recommended that the convention in
407c478bd9Sstevel@tonic-gate  * PSARC/1997/389 be used.  If a collision occurs, then space_store will
417c478bd9Sstevel@tonic-gate  * fail.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/var.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/signal.h>
497c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
507c478bd9Sstevel@tonic-gate #include <sys/buf.h>
517c478bd9Sstevel@tonic-gate #include <sys/cred.h>
527c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
557c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
567c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
577c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
587c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/strredir.h>
617c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
62fea9cb91Slq #include <sys/consdev.h>
63fea9cb91Slq #include <sys/wscons.h>
646cefaae1SJack Meng #include <sys/bootprops.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct	buf	bfreelist;	/* Head of the free list of buffers */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate sysinfo_t	sysinfo;
697c478bd9Sstevel@tonic-gate vminfo_t	vminfo;		/* VM stats protected by sysinfolock mutex */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef	lint
727c478bd9Sstevel@tonic-gate int	__lintzero;		/* Alway zero for shutting up lint */
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * The following describe the physical memory configuration.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *	physmem	 -  The amount of physical memory configured
797c478bd9Sstevel@tonic-gate  *		    in pages.  ptob(physmem) is the amount
807c478bd9Sstevel@tonic-gate  *		    of physical memory in bytes.  Defined in
817c478bd9Sstevel@tonic-gate  *		    .../os/startup.c.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  *	physmax  -  The highest numbered physical page in memory.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *	maxmem	 -  Maximum available memory, in pages.  Defined
867c478bd9Sstevel@tonic-gate  *		    in main.c.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *	physinstalled
897c478bd9Sstevel@tonic-gate  *		 -  Pages of physical memory installed;
907c478bd9Sstevel@tonic-gate  *		    includes use by PROM/boot not counted in
917c478bd9Sstevel@tonic-gate  *		    physmem.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate pfn_t	physmax;
957c478bd9Sstevel@tonic-gate pgcnt_t	physinstalled;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #include <sys/systm.h>
987c478bd9Sstevel@tonic-gate #include <sys/conf.h>
997c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1007c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1017c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
1027c478bd9Sstevel@tonic-gate 
103af4c679fSSean McEnroe /*
104af4c679fSSean McEnroe  * Data for segkmem pages that should be resident
105af4c679fSSean McEnroe  */
106af4c679fSSean McEnroe struct vnode kvps[KV_MAX];
107af4c679fSSean McEnroe 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Data from swapgeneric.c that must be resident.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate struct vnode *rootvp;		/* vnode of the root device */
1127c478bd9Sstevel@tonic-gate dev_t rootdev;			/* dev_t of the root device */
113986fd29aSsetje boolean_t root_is_ramdisk;	/* root is ramdisk */
114986fd29aSsetje uint32_t ramdisk_size;		/* (KB) currently set only for sparc netboots */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
117986fd29aSsetje  * dhcp
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate #include <sys/socket.h>
1207c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1217c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
1227c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1237c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1247c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
1257c478bd9Sstevel@tonic-gate #include <net/if.h>
126986fd29aSsetje 
127986fd29aSsetje int netboot;
128986fd29aSsetje int obpdebug;
129986fd29aSsetje char *dhcack;		/* dhcp response packet */
130986fd29aSsetje int dhcacklen;
131986fd29aSsetje char *netdev_path;	/* Used to cache the netdev_path handed up by boot */
132986fd29aSsetje char dhcifname[IFNAMSIZ];
133986fd29aSsetje 
134986fd29aSsetje /*
135986fd29aSsetje  * Data from arp.c that must be resident.
136986fd29aSsetje  */
1377c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
1387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
1397c478bd9Sstevel@tonic-gate #include <netinet/in_var.h>
1407c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #include <sys/tty.h>
1457c478bd9Sstevel@tonic-gate #include <sys/ptyvar.h>
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static void store_fetch_initspace();
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * Allocate tunable structures at runtime.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate void
space_init(void)1537c478bd9Sstevel@tonic-gate space_init(void)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	pty_initspace();
1567c478bd9Sstevel@tonic-gate 	store_fetch_initspace();
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Previously defined in consmsconf.c ...
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate dev_t kbddev = NODEV;
1657c478bd9Sstevel@tonic-gate dev_t mousedev = NODEV;
1667c478bd9Sstevel@tonic-gate dev_t stdindev = NODEV;
167dbad7380SToomas Soome dev_t diagdev = NODEV;
1687c478bd9Sstevel@tonic-gate struct vnode *wsconsvp;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate dev_t fbdev = NODEV;
1717c478bd9Sstevel@tonic-gate struct vnode *fbvp;
1727c478bd9Sstevel@tonic-gate dev_info_t *fbdip;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * moved from cons.c because they must be resident in the kernel.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate vnode_t	*rconsvp;
1787c478bd9Sstevel@tonic-gate dev_t	rconsdev;
1797c478bd9Sstevel@tonic-gate dev_t	uconsdev = NODEV;
1807c478bd9Sstevel@tonic-gate 
181361ed64aSJames Anderson /*
182361ed64aSJames Anderson  * serial virtual console vnode pointer.
183361ed64aSJames Anderson  */
184361ed64aSJames Anderson vnode_t		*vsconsvp = NULL;
185361ed64aSJames Anderson 
186fea9cb91Slq /*
187fea9cb91Slq  * Flag whether console fb output is using PROM/PROM emulation
188fea9cb91Slq  * terminal emulator, or is using the kernel terminal emulator.
189fea9cb91Slq  */
190fea9cb91Slq int	consmode = CONS_FW;
191fea9cb91Slq 
192fea9cb91Slq /*
193fea9cb91Slq  * The following allows systems to disable use of the kernel
194fea9cb91Slq  * terminal emulator (retreat to PROM terminal emulator if there
195fea9cb91Slq  * is PROM).
196fea9cb91Slq  */
197fea9cb91Slq int	cons_tem_disable;
198fea9cb91Slq 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
2017c478bd9Sstevel@tonic-gate  * keyboard/frame buffer combination, aka the workstation console.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate vnode_t *rwsconsvp;
2047c478bd9Sstevel@tonic-gate dev_t	rwsconsdev;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Platform console abort policy.
2087c478bd9Sstevel@tonic-gate  * Platforms may override the default software policy, if such hardware
2097c478bd9Sstevel@tonic-gate  * (e.g. keyswitches with a secure position) exists.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate int abort_enable = KIOCABORTENABLE;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /* from cpc.c */
2147c478bd9Sstevel@tonic-gate uint_t kcpc_key;	/* TSD key for CPU performance counter context */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * storing and retrieving data by string key
2187c478bd9Sstevel@tonic-gate  *
2197c478bd9Sstevel@tonic-gate  * this mechanism allows a consumer to store and retrieve by name a pointer
2207c478bd9Sstevel@tonic-gate  * to some space maintained by the consumer.
2217c478bd9Sstevel@tonic-gate  * For example, a driver or module may want to have persistent data
2227c478bd9Sstevel@tonic-gate  * over unloading/loading cycles. The pointer is typically to some
2237c478bd9Sstevel@tonic-gate  * kmem_alloced space and it should not be pointing to data that will
2247c478bd9Sstevel@tonic-gate  * be destroyed when the module is unloaded.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate static mod_hash_t *space_hash;
2277c478bd9Sstevel@tonic-gate static char *space_hash_name = "space_hash";
2287c478bd9Sstevel@tonic-gate static size_t	space_hash_nchains = 8;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
store_fetch_initspace()2317c478bd9Sstevel@tonic-gate store_fetch_initspace()
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	space_hash = mod_hash_create_strhash(space_hash_name,
234986fd29aSsetje 	    space_hash_nchains, mod_hash_null_valdtor);
2357c478bd9Sstevel@tonic-gate 	ASSERT(space_hash);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate int
space_store(char * key,uintptr_t ptr)2397c478bd9Sstevel@tonic-gate space_store(char *key, uintptr_t ptr)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	char *s;
2427c478bd9Sstevel@tonic-gate 	int rval;
2437c478bd9Sstevel@tonic-gate 	size_t l;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* some sanity checks first */
2467c478bd9Sstevel@tonic-gate 	if (key == NULL) {
2477c478bd9Sstevel@tonic-gate 		return (-1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	l = (size_t)strlen(key);
2507c478bd9Sstevel@tonic-gate 	if (l == 0) {
2517c478bd9Sstevel@tonic-gate 		return (-1);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/* increment for null terminator */
2557c478bd9Sstevel@tonic-gate 	l++;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* alloc space for the string, mod_hash_insert will deallocate */
2587c478bd9Sstevel@tonic-gate 	s = kmem_alloc(l, KM_SLEEP);
2597c478bd9Sstevel@tonic-gate 	bcopy(key, s, l);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	rval = mod_hash_insert(space_hash,
262986fd29aSsetje 	    (mod_hash_key_t)s, (mod_hash_val_t)ptr);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	switch (rval) {
2657c478bd9Sstevel@tonic-gate 	case 0:
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate #ifdef DEBUG
2687c478bd9Sstevel@tonic-gate 	case MH_ERR_DUPLICATE:
2697c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
2707c478bd9Sstevel@tonic-gate 		rval = -1;
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 	case MH_ERR_NOMEM:
2737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
2747c478bd9Sstevel@tonic-gate 		rval = -1;
2757c478bd9Sstevel@tonic-gate 		break;
2767c478bd9Sstevel@tonic-gate 	default:
2777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
2787c478bd9Sstevel@tonic-gate 		    key);
2797c478bd9Sstevel@tonic-gate 		rval = -1;
2807c478bd9Sstevel@tonic-gate 		break;
2817c478bd9Sstevel@tonic-gate #else
2827c478bd9Sstevel@tonic-gate 	default:
2837c478bd9Sstevel@tonic-gate 		rval = -1;
2847c478bd9Sstevel@tonic-gate 		break;
2857c478bd9Sstevel@tonic-gate #endif
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	return (rval);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate uintptr_t
space_fetch(char * key)2927c478bd9Sstevel@tonic-gate space_fetch(char *key)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	uintptr_t ptr = 0;
2957c478bd9Sstevel@tonic-gate 	mod_hash_val_t val;
2967c478bd9Sstevel@tonic-gate 	int rval;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (key) {
2997c478bd9Sstevel@tonic-gate 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
3007c478bd9Sstevel@tonic-gate 		if (rval == 0) {
3017c478bd9Sstevel@tonic-gate 			ptr = (uintptr_t)val;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (ptr);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate void
space_free(char * key)3097c478bd9Sstevel@tonic-gate space_free(char *key)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	if (key) {
3127c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * Support for CRC32.  At present all calculations are done in simple
3187c478bd9Sstevel@tonic-gate  * macros, so all we need is somewhere to declare the global lookup table.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate #include <sys/crc32.h>
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate const uint32_t crc32_table[256] = { CRC32_TABLE };
3244b46d1efSkrgopi 
3254b46d1efSkrgopi /*
326da14cebeSEric Cheng  * We need to fanout load from NIC which can overwhelm a single CPU.
327da14cebeSEric Cheng  * This becomes especially important on systems having slow CPUs
328da14cebeSEric Cheng  * (sun4v architecture). mac_soft_ring_enable is false on all
329da14cebeSEric Cheng  * systems except sun4v. On sun4v, they get enabled by default (see
330da14cebeSEric Cheng  * sun4v/os/mach_startup.c).
3314b46d1efSkrgopi  */
332da14cebeSEric Cheng boolean_t	mac_soft_ring_enable = B_FALSE;
3336cefaae1SJack Meng 
3346cefaae1SJack Meng /*
3356cefaae1SJack Meng  * Global iscsi boot prop
3366cefaae1SJack Meng  */
3376cefaae1SJack Meng ib_boot_prop_t	*iscsiboot_prop = NULL;
338