xref: /titanic_41/usr/src/uts/common/os/space.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 
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 from swapgeneric.c that must be resident.
106  */
107 struct vnode *rootvp;		/* vnode of the root device */
108 dev_t rootdev;			/* dev_t of the root device */
109 int root_is_svm;		/* root is a mirrored device flag */
110 
111 int netboot;
112 int obpdebug;
113 char *dhcack;	/* Used to cache ascii form of DHCPACK handed up by boot */
114 char *netdev_path;	/* Used to cache the netdev_path handed up by boot */
115 
116 /*
117  * Data from arp.c that must be resident.
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 #include <net/if_arp.h>
127 #include <netinet/in.h>
128 #include <netinet/in_var.h>
129 #include <netinet/if_ether.h>
130 
131 ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
132 
133 /*
134  * Data from timod that must be resident
135  */
136 
137 /*
138  * state transition table for TI interface
139  */
140 #include <sys/tihdr.h>
141 
142 #define	nr	127		/* not reachable */
143 
144 char ti_statetbl[TE_NOEVENTS][TS_NOSTATES] = {
145 				/* STATES */
146 	/* 0  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16 */
147 
148 	{ 1, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
149 	{nr, nr, nr,  2, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
150 	{nr, nr, nr,  4, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
151 	{nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
152 	{nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
153 	{nr,  0,  3, nr,  3,  3, nr, nr,  7, nr, nr, nr,  6,  7,  9, 10, 11},
154 	{nr, nr,  0, nr, nr,  6, nr, nr, nr, nr, nr, nr,  3, nr,  3,  3,  3},
155 	{nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr,  3, nr, nr, nr},
156 	{nr, nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr,  3, nr, nr, nr},
157 	{nr, nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr,  7, nr, nr, nr},
158 	{nr, nr, nr,  5, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
159 	{nr, nr, nr, nr, nr, nr, nr,  8, nr, nr, nr, nr, nr, nr, nr, nr, nr},
160 	{nr, nr, nr, nr, nr, nr, 12, 13, nr, 14, 15, 16, nr, nr, nr, nr, nr},
161 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
162 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
163 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 10, nr,  3, nr, nr, nr, nr, nr},
164 	{nr, nr, nr,  7, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
165 	{nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
166 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
167 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
168 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 11,  3, nr, nr, nr, nr, nr, nr},
169 	{nr, nr, nr, nr, nr, nr,  3, nr, nr,  3,  3,  3, nr, nr, nr, nr, nr},
170 	{nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr},
171 	{nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
172 	{nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
173 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
174 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
175 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
176 };
177 
178 
179 #include <sys/sad.h>
180 #include <sys/tty.h>
181 #include <sys/ptyvar.h>
182 
183 static void store_fetch_initspace();
184 
185 /*
186  * Allocate tunable structures at runtime.
187  */
188 void
189 space_init(void)
190 {
191 	sad_initspace();
192 	pty_initspace();
193 	store_fetch_initspace();
194 }
195 
196 int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
197 
198 /*
199  * Previously defined in consmsconf.c ...
200  */
201 dev_t kbddev = NODEV;
202 dev_t mousedev = NODEV;
203 dev_t stdindev = NODEV;
204 struct vnode *wsconsvp;
205 
206 dev_t fbdev = NODEV;
207 struct vnode *fbvp;
208 dev_info_t *fbdip;
209 
210 /*
211  * moved from cons.c because they must be resident in the kernel.
212  */
213 vnode_t	*rconsvp;
214 dev_t	rconsdev;
215 dev_t	uconsdev = NODEV;
216 
217 /*
218  * This flag, when set marks rconsvp in a transition state.
219  */
220 
221 int	cn_conf;
222 
223 /*
224  * Moved from sad_conf.c because of the usual in loadable modules
225  */
226 
227 #ifndef NSTRPHASH
228 #define	NSTRPHASH	128
229 #endif
230 struct autopush **strpcache;
231 int strpmask = NSTRPHASH - 1;
232 
233 /*
234  * Flag whether console fb output is using PROM/PROM emulation
235  * terminal emulator, or is using the kernel terminal emulator.
236  */
237 int	consmode = CONS_FW;
238 
239 /*
240  * The following allows systems to disable use of the kernel
241  * terminal emulator (retreat to PROM terminal emulator if there
242  * is PROM).
243  */
244 int	cons_tem_disable;
245 
246 /*
247  * Moved here from wscons.c
248  * Package the redirection-related routines into an ops vector of the form
249  * that the redirecting driver expects.
250  */
251 srvnops_t wscons_srvnops = {
252 	wcvnget,
253 	wcvnrele
254 };
255 
256 /*
257  * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
258  * keyboard/frame buffer combination, aka the workstation console.
259  */
260 
261 vnode_t *rwsconsvp;
262 dev_t	rwsconsdev;
263 
264 /*
265  * Platform console abort policy.
266  * Platforms may override the default software policy, if such hardware
267  * (e.g. keyswitches with a secure position) exists.
268  */
269 int abort_enable = KIOCABORTENABLE;
270 
271 /* from iwscons.c */
272 
273 kthread_id_t	iwscn_thread;	/* thread that is allowed to push redirm */
274 wcm_data_t	*iwscn_wcm_data; /* allocated data for redirm */
275 
276 /* from cpc.c */
277 uint_t kcpc_key;	/* TSD key for CPU performance counter context */
278 
279 /*
280  * storing and retrieving data by string key
281  *
282  * this mechanism allows a consumer to store and retrieve by name a pointer
283  * to some space maintained by the consumer.
284  * For example, a driver or module may want to have persistent data
285  * over unloading/loading cycles. The pointer is typically to some
286  * kmem_alloced space and it should not be pointing to data that will
287  * be destroyed when the module is unloaded.
288  */
289 static mod_hash_t *space_hash;
290 static char *space_hash_name = "space_hash";
291 static size_t	space_hash_nchains = 8;
292 
293 static void
294 store_fetch_initspace()
295 {
296 	space_hash = mod_hash_create_strhash(space_hash_name,
297 		space_hash_nchains, mod_hash_null_valdtor);
298 	ASSERT(space_hash);
299 }
300 
301 int
302 space_store(char *key, uintptr_t ptr)
303 {
304 	char *s;
305 	int rval;
306 	size_t l;
307 
308 	/* some sanity checks first */
309 	if (key == NULL) {
310 		return (-1);
311 	}
312 	l = (size_t)strlen(key);
313 	if (l == 0) {
314 		return (-1);
315 	}
316 
317 	/* increment for null terminator */
318 	l++;
319 
320 	/* alloc space for the string, mod_hash_insert will deallocate */
321 	s = kmem_alloc(l, KM_SLEEP);
322 	bcopy(key, s, l);
323 
324 	rval = mod_hash_insert(space_hash,
325 		(mod_hash_key_t)s, (mod_hash_val_t)ptr);
326 
327 	switch (rval) {
328 	case 0:
329 		break;
330 #ifdef DEBUG
331 	case MH_ERR_DUPLICATE:
332 		cmn_err(CE_WARN, "space_store: duplicate key %s", key);
333 		rval = -1;
334 		break;
335 	case MH_ERR_NOMEM:
336 		cmn_err(CE_WARN, "space_store: no mem for key %s", key);
337 		rval = -1;
338 		break;
339 	default:
340 		cmn_err(CE_WARN, "space_store: unspecified error for key %s",
341 		    key);
342 		rval = -1;
343 		break;
344 #else
345 	default:
346 		rval = -1;
347 		break;
348 #endif
349 	}
350 
351 	return (rval);
352 }
353 
354 uintptr_t
355 space_fetch(char *key)
356 {
357 	uintptr_t ptr = 0;
358 	mod_hash_val_t val;
359 	int rval;
360 
361 	if (key) {
362 		rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
363 		if (rval == 0) {
364 			ptr = (uintptr_t)val;
365 		}
366 	}
367 
368 	return (ptr);
369 }
370 
371 void
372 space_free(char *key)
373 {
374 	if (key) {
375 		(void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
376 	}
377 }
378 
379 /*
380  * Support for CRC32.  At present all calculations are done in simple
381  * macros, so all we need is somewhere to declare the global lookup table.
382  */
383 
384 #include <sys/crc32.h>
385 
386 const uint32_t crc32_table[256] = { CRC32_TABLE };
387 
388 
389 /*
390  * We need to fanout load from NIC which can overwhelm a single
391  * CPU. A 10Gb NIC interrupting a single CPU is a good example.
392  * Instead of fanning out to random CPUs, it a big performance
393  * win if you can fanout to the threads on the same core (niagara)
394  * that is taking interrupts.
395  *
396  * We need a better mechanism to figure out the other threads on
397  * the same core or cores on the same chip which share caches etc.
398  * but for time being, this will suffice.
399  */
400 #define	NUMBER_OF_THREADS_PER_CPU	4
401 uint_t		ip_threads_per_cpu = NUMBER_OF_THREADS_PER_CPU;
402 
403 /* Global flag to enable/disable soft ring facility */
404 boolean_t	ip_squeue_soft_ring = B_FALSE;
405