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