xref: /titanic_51/usr/src/uts/common/os/space.c (revision fc3af78a71855c71878866a294572d00e6720533)
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 2008 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  * Flag whether console fb output is using PROM/PROM emulation
225  * terminal emulator, or is using the kernel terminal emulator.
226  */
227 int	consmode = CONS_FW;
228 
229 /*
230  * The following allows systems to disable use of the kernel
231  * terminal emulator (retreat to PROM terminal emulator if there
232  * is PROM).
233  */
234 int	cons_tem_disable;
235 
236 /*
237  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
238  * keyboard/frame buffer combination, aka the workstation console.
239  */
240 vnode_t *rwsconsvp;
241 dev_t	rwsconsdev;
242 
243 /*
244  * Platform console abort policy.
245  * Platforms may override the default software policy, if such hardware
246  * (e.g. keyswitches with a secure position) exists.
247  */
248 int abort_enable = KIOCABORTENABLE;
249 
250 /* from cpc.c */
251 uint_t kcpc_key;	/* TSD key for CPU performance counter context */
252 
253 /*
254  * storing and retrieving data by string key
255  *
256  * this mechanism allows a consumer to store and retrieve by name a pointer
257  * to some space maintained by the consumer.
258  * For example, a driver or module may want to have persistent data
259  * over unloading/loading cycles. The pointer is typically to some
260  * kmem_alloced space and it should not be pointing to data that will
261  * be destroyed when the module is unloaded.
262  */
263 static mod_hash_t *space_hash;
264 static char *space_hash_name = "space_hash";
265 static size_t	space_hash_nchains = 8;
266 
267 static void
268 store_fetch_initspace()
269 {
270 	space_hash = mod_hash_create_strhash(space_hash_name,
271 	    space_hash_nchains, mod_hash_null_valdtor);
272 	ASSERT(space_hash);
273 }
274 
275 int
276 space_store(char *key, uintptr_t ptr)
277 {
278 	char *s;
279 	int rval;
280 	size_t l;
281 
282 	/* some sanity checks first */
283 	if (key == NULL) {
284 		return (-1);
285 	}
286 	l = (size_t)strlen(key);
287 	if (l == 0) {
288 		return (-1);
289 	}
290 
291 	/* increment for null terminator */
292 	l++;
293 
294 	/* alloc space for the string, mod_hash_insert will deallocate */
295 	s = kmem_alloc(l, KM_SLEEP);
296 	bcopy(key, s, l);
297 
298 	rval = mod_hash_insert(space_hash,
299 	    (mod_hash_key_t)s, (mod_hash_val_t)ptr);
300 
301 	switch (rval) {
302 	case 0:
303 		break;
304 #ifdef DEBUG
305 	case MH_ERR_DUPLICATE:
306 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
307 		rval = -1;
308 		break;
309 	case MH_ERR_NOMEM:
310 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
311 		rval = -1;
312 		break;
313 	default:
314 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
315 		    key);
316 		rval = -1;
317 		break;
318 #else
319 	default:
320 		rval = -1;
321 		break;
322 #endif
323 	}
324 
325 	return (rval);
326 }
327 
328 uintptr_t
329 space_fetch(char *key)
330 {
331 	uintptr_t ptr = 0;
332 	mod_hash_val_t val;
333 	int rval;
334 
335 	if (key) {
336 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
337 		if (rval == 0) {
338 			ptr = (uintptr_t)val;
339 		}
340 	}
341 
342 	return (ptr);
343 }
344 
345 void
346 space_free(char *key)
347 {
348 	if (key) {
349 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
350 	}
351 }
352 
353 /*
354  * Support for CRC32.  At present all calculations are done in simple
355  * macros, so all we need is somewhere to declare the global lookup table.
356  */
357 
358 #include <sys/crc32.h>
359 
360 const uint32_t crc32_table[256] = { CRC32_TABLE };
361 
362 
363 /*
364  * We need to fanout load from NIC which can overwhelm a single
365  * CPU. A 10Gb NIC interrupting a single CPU is a good example.
366  * Instead of fanning out to random CPUs, it a big performance
367  * win if you can fanout to the threads on the same core (niagara)
368  * that is taking interrupts.
369  *
370  * We need a better mechanism to figure out the other threads on
371  * the same core or cores on the same chip which share caches etc.
372  * but for time being, this will suffice.
373  */
374 #define	NUMBER_OF_THREADS_PER_CPU	4
375 uint_t		ip_threads_per_cpu = NUMBER_OF_THREADS_PER_CPU;
376 
377 /* Global flag to enable/disable soft ring facility */
378 boolean_t	ip_squeue_soft_ring = B_FALSE;
379 
380 /*
381  * Global iscsi boot prop
382  */
383 ib_boot_prop_t	*iscsiboot_prop = NULL;
384