Merge commit 'ea01a15a654b9e1c7b37d958f4d1911882ed7781'
[unleashed.git] / kernel / os / space.c
blobff390defde953185c14e2bf6d25621935c4fd7ea
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2016 Nexenta Systems, Inc.
29 * The intent of this file is to contain any data that must remain
30 * resident in the kernel.
32 * space_store(), space_fetch(), and space_free() have been added to
33 * easily store and retrieve kernel resident data.
34 * These functions are recommended rather than adding new variables to
35 * this file.
37 * Note that it's possible for name collisions to occur. In order to
38 * prevent collisions, it's recommended that the convention in
39 * PSARC/1997/389 be used. If a collision occurs, then space_store will
40 * fail.
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/var.h>
46 #include <sys/proc.h>
47 #include <sys/signal.h>
48 #include <sys/utsname.h>
49 #include <sys/buf.h>
50 #include <sys/cred.h>
51 #include <sys/vfs.h>
52 #include <sys/vnode.h>
53 #include <sys/sysinfo.h>
54 #include <sys/t_lock.h>
55 #include <sys/vmem.h>
56 #include <sys/modhash.h>
57 #include <sys/cmn_err.h>
59 #include <sys/strredir.h>
60 #include <sys/kbio.h>
61 #include <sys/consdev.h>
62 #include <sys/wscons.h>
63 #include <sys/bootprops.h>
65 struct buf bfreelist; /* Head of the free list of buffers */
67 sysinfo_t sysinfo;
68 vminfo_t vminfo; /* VM stats protected by sysinfolock mutex */
72 * The following describe the physical memory configuration.
74 * physmem - The amount of physical memory configured
75 * in pages. ptob(physmem) is the amount
76 * of physical memory in bytes. Defined in
77 * .../os/startup.c.
79 * physmax - The highest numbered physical page in memory.
81 * maxmem - Maximum available memory, in pages. Defined
82 * in main.c.
84 * physinstalled
85 * - Pages of physical memory installed;
86 * includes use by PROM/boot not counted in
87 * physmem.
90 pfn_t physmax;
91 pgcnt_t physinstalled;
93 struct var v;
95 #include <sys/systm.h>
96 #include <sys/conf.h>
97 #include <sys/kmem.h>
98 #include <sys/sysmacros.h>
99 #include <sys/bootconf.h>
102 * Data for segkmem pages that should be resident
104 struct vnode kvps[KV_MAX];
107 * Data from swapgeneric.c that must be resident.
109 struct vnode *rootvp; /* vnode of the root device */
110 dev_t rootdev; /* dev_t of the root device */
111 boolean_t root_is_ramdisk; /* root is ramdisk */
112 uint32_t ramdisk_size; /* (KB) currently set only for sparc netboots */
115 * dhcp
117 #include <sys/socket.h>
118 #include <sys/errno.h>
119 #include <sys/sockio.h>
120 #include <sys/stream.h>
121 #include <sys/stropts.h>
122 #include <sys/dlpi.h>
123 #include <net/if.h>
125 int netboot;
126 int obpdebug;
127 char *dhcack; /* dhcp response packet */
128 int dhcacklen;
129 char *netdev_path; /* Used to cache the netdev_path handed up by boot */
130 char dhcifname[IFNAMSIZ];
133 * Data from arp.c that must be resident.
135 #include <net/if_arp.h>
136 #include <netinet/in.h>
137 #include <netinet/in_var.h>
138 #include <netinet/if_ether.h>
140 ether_addr_t etherbroadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
144 * Data from timod that must be resident
148 * state transition table for TI interface
150 #include <sys/tihdr.h>
152 #define nr 127 /* not reachable */
154 char ti_statetbl[TE_NOEVENTS][TS_NOSTATES] = {
155 /* STATES */
156 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
158 { 1, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
159 {nr, nr, nr, 2, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
160 {nr, nr, nr, 4, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
161 {nr, 3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
162 {nr, nr, nr, nr, 3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
163 {nr, 0, 3, nr, 3, 3, nr, nr, 7, nr, nr, nr, 6, 7, 9, 10, 11},
164 {nr, nr, 0, nr, nr, 6, nr, nr, nr, nr, nr, nr, 3, nr, 3, 3, 3},
165 {nr, nr, nr, nr, nr, nr, nr, nr, 9, nr, nr, nr, nr, 3, nr, nr, nr},
166 {nr, nr, nr, nr, nr, nr, nr, nr, 3, nr, nr, nr, nr, 3, nr, nr, nr},
167 {nr, nr, nr, nr, nr, nr, nr, nr, 7, nr, nr, nr, nr, 7, nr, nr, nr},
168 {nr, nr, nr, 5, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
169 {nr, nr, nr, nr, nr, nr, nr, 8, nr, nr, nr, nr, nr, nr, nr, nr, nr},
170 {nr, nr, nr, nr, nr, nr, 12, 13, nr, 14, 15, 16, 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, 9, nr, 11, nr, nr, nr, nr, nr},
173 {nr, nr, nr, nr, nr, nr, nr, nr, nr, 10, nr, 3, nr, nr, nr, nr, nr},
174 {nr, nr, nr, 7, nr, nr, nr, 7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
175 {nr, nr, nr, nr, nr, nr, 9, nr, nr, nr, nr, 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, 9, 10, nr, nr, nr, nr, nr, nr},
178 {nr, nr, nr, nr, nr, nr, nr, nr, nr, 11, 3, nr, nr, nr, nr, nr, nr},
179 {nr, nr, nr, nr, nr, nr, 3, nr, nr, 3, 3, 3, nr, nr, nr, nr, nr},
180 {nr, nr, nr, nr, nr, nr, nr, 3, nr, nr, nr, nr, nr, nr, nr, nr, nr},
181 {nr, nr, nr, nr, nr, nr, nr, 7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
182 {nr, nr, nr, 9, 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 {nr, nr, nr, 3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
189 #include <sys/tty.h>
190 #include <sys/ptyvar.h>
192 static void store_fetch_initspace();
195 * Allocate tunable structures at runtime.
197 void
198 space_init(void)
200 pty_initspace();
201 store_fetch_initspace();
204 int ts_dispatch_extended = -1; /* set in ts_getdptbl or set_platform_default */
207 * Previously defined in consmsconf.c ...
209 dev_t kbddev = NODEV;
210 dev_t mousedev = NODEV;
211 dev_t stdindev = NODEV;
212 struct vnode *wsconsvp;
214 dev_t fbdev = NODEV;
215 struct vnode *fbvp;
216 dev_info_t *fbdip;
219 * moved from cons.c because they must be resident in the kernel.
221 vnode_t *rconsvp;
222 dev_t rconsdev;
223 dev_t uconsdev = NODEV;
226 * serial virtual console vnode pointer.
228 vnode_t *vsconsvp = NULL;
231 * Flag whether console fb output is using PROM/PROM emulation
232 * terminal emulator, or is using the kernel terminal emulator.
234 int consmode = CONS_FW;
237 * The following allows systems to disable use of the kernel
238 * terminal emulator (retreat to PROM terminal emulator if there
239 * is PROM).
241 int cons_tem_disable;
244 * consconfig() in autoconf.c sets this; it's the vnode of the distinguished
245 * keyboard/frame buffer combination, aka the workstation console.
247 vnode_t *rwsconsvp;
248 dev_t rwsconsdev;
251 * Platform console abort policy.
252 * Platforms may override the default software policy, if such hardware
253 * (e.g. keyswitches with a secure position) exists.
255 int abort_enable = KIOCABORTENABLE;
257 /* from cpc.c */
258 uint_t kcpc_key; /* TSD key for CPU performance counter context */
261 * storing and retrieving data by string key
263 * this mechanism allows a consumer to store and retrieve by name a pointer
264 * to some space maintained by the consumer.
265 * For example, a driver or module may want to have persistent data
266 * over unloading/loading cycles. The pointer is typically to some
267 * kmem_alloced space and it should not be pointing to data that will
268 * be destroyed when the module is unloaded.
270 static mod_hash_t *space_hash;
271 static char *space_hash_name = "space_hash";
272 static size_t space_hash_nchains = 8;
274 static void
275 store_fetch_initspace()
277 space_hash = mod_hash_create_strhash(space_hash_name,
278 space_hash_nchains, mod_hash_null_valdtor);
279 ASSERT(space_hash);
283 space_store(char *key, uintptr_t ptr)
285 char *s;
286 int rval;
287 size_t l;
289 /* some sanity checks first */
290 if (key == NULL) {
291 return (-1);
293 l = (size_t)strlen(key);
294 if (l == 0) {
295 return (-1);
298 /* increment for null terminator */
299 l++;
301 /* alloc space for the string, mod_hash_insert will deallocate */
302 s = kmem_alloc(l, KM_SLEEP);
303 bcopy(key, s, l);
305 rval = mod_hash_insert(space_hash,
306 (mod_hash_key_t)s, (mod_hash_val_t)ptr);
308 switch (rval) {
309 case 0:
310 break;
311 #ifdef DEBUG
312 case MH_ERR_DUPLICATE:
313 cmn_err(CE_WARN, "space_store: duplicate key %s", key);
314 rval = -1;
315 break;
316 case MH_ERR_NOMEM:
317 cmn_err(CE_WARN, "space_store: no mem for key %s", key);
318 rval = -1;
319 break;
320 default:
321 cmn_err(CE_WARN, "space_store: unspecified error for key %s",
322 key);
323 rval = -1;
324 break;
325 #else
326 default:
327 rval = -1;
328 break;
329 #endif
332 return (rval);
335 uintptr_t
336 space_fetch(char *key)
338 uintptr_t ptr = 0;
339 mod_hash_val_t val;
340 int rval;
342 if (key) {
343 rval = mod_hash_find(space_hash, (mod_hash_key_t)key, &val);
344 if (rval == 0) {
345 ptr = (uintptr_t)val;
349 return (ptr);
352 void
353 space_free(char *key)
355 if (key) {
356 (void) mod_hash_destroy(space_hash, (mod_hash_key_t)key);
361 * Support for CRC32. At present all calculations are done in simple
362 * macros, so all we need is somewhere to declare the global lookup table.
365 #include <sys/crc32.h>
367 const uint32_t crc32_table[256] = { CRC32_TABLE };
370 * We need to fanout load from NIC which can overwhelm a single CPU.
371 * This becomes especially important on systems having slow CPUs
372 * (sun4v architecture). mac_soft_ring_enable is false on all
373 * systems except sun4v. On sun4v, they get enabled by default (see
374 * sun4v/os/mach_startup.c).
376 boolean_t mac_soft_ring_enable = B_FALSE;
379 * Global iscsi boot prop
381 ib_boot_prop_t *iscsiboot_prop = NULL;