xref: /illumos-gate/usr/src/uts/common/sys/systm.h (revision 92ae099e204069d2fec11f099863387b5317d849)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved	*/
24 
25 /*
26  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  * Copyright 2016 Nexenta Systems, Inc.
29  * Copyright 2025 Oxide Computer Company
30  */
31 
32 #ifndef _SYS_SYSTM_H
33 #define	_SYS_SYSTM_H
34 
35 #if defined(_STANDALONE)
36 #include <sys/cdefs.h>
37 #include <string.h>
38 #else
39 #include <sys/types.h>
40 #include <sys/t_lock.h>
41 #include <sys/proc.h>
42 #include <sys/dditypes.h>
43 #endif
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 #if !defined(_STANDALONE)
50 /*
51  * The pc_t is the type of the kernel's program counter.  In general, a
52  * pc_t is a uintptr_t -- except for a sparcv9 kernel, in which case all
53  * instruction text is below 4G, and a pc_t is thus a uint32_t.
54  */
55 #ifdef __sparcv9
56 typedef uint32_t pc_t;
57 #else
58 typedef uintptr_t pc_t;
59 #endif
60 
61 /*
62  * Random set of variables used by more than one routine.
63  */
64 
65 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
66 #include <sys/inttypes.h>
67 #include <sys/types32.h>
68 #include <sys/varargs.h>
69 #include <sys/uadmin.h>
70 
71 extern int hz;			/* system clock rate */
72 extern struct vnode *rootdir;	/* pointer to vnode of root directory */
73 extern struct vnode *devicesdir;	/* pointer to /devices vnode */
74 extern int interrupts_unleashed;	/* set after the spl0() in main() */
75 
76 extern char runin;		/* scheduling flag */
77 extern char runout;		/* scheduling flag */
78 extern char wake_sched;		/* causes clock to wake swapper on next tick */
79 extern char wake_sched_sec;	/* causes clock to wake swapper after a sec */
80 
81 extern pgcnt_t	maxmem;		/* max available memory (pages) */
82 extern pgcnt_t	physmem;	/* physical memory (pages) on this CPU */
83 extern pfn_t	physmax;	/* highest numbered physical page present */
84 extern pgcnt_t	physinstalled;	/* physical pages including PROM/boot use */
85 
86 extern caddr_t	s_text;		/* start of kernel text segment */
87 extern caddr_t	e_text;		/* end of kernel text segment */
88 extern caddr_t	s_data;		/* start of kernel text segment */
89 extern caddr_t	e_data;		/* end of kernel text segment */
90 
91 extern pgcnt_t	availrmem;	/* Available resident (not swapable)	*/
92 				/* memory in pages.			*/
93 extern pgcnt_t	availrmem_initial;	/* initial value of availrmem	*/
94 extern pgcnt_t	segspt_minfree;	/* low water mark for availrmem in seg_spt */
95 extern pgcnt_t	freemem;	/* Current free memory.			*/
96 
97 extern dev_t	rootdev;	/* device of the root */
98 extern struct vnode *rootvp;	/* vnode of root device */
99 extern boolean_t root_is_ramdisk;	/* root is boot_archive ramdisk */
100 extern uint32_t  ramdisk_size;		/* (KB) set only for sparc netboots */
101 extern char *volatile panicstr;	/* panic string pointer */
102 extern va_list  panicargs;	/* panic arguments */
103 extern volatile int quiesce_active;	/* quiesce(9E) is in progress */
104 
105 extern int	rstchown;	/* 1 ==> restrictive chown(2) semantics */
106 extern int	klustsize;
107 
108 extern int	abort_enable;	/* Platform input-device abort policy */
109 
110 extern int	audit_active;	/* Solaris Auditing module state */
111 
112 extern int	avenrun[];	/* array of load averages */
113 
114 extern char *isa_list;		/* For sysinfo's isalist option */
115 
116 extern int noexec_user_stack;		/* patchable via /etc/system */
117 extern int noexec_user_stack_log;	/* patchable via /etc/system */
118 
119 /*
120  * Use NFS client operations in the global zone only.  Under contract with
121  * admin/install; do not change without coordinating with that consolidation.
122  */
123 extern int nfs_global_client_only;
124 
125 extern void report_stack_exec(proc_t *, caddr_t);
126 
127 extern void startup(void);
128 extern void clkstart(void);
129 extern void post_startup(void);
130 extern void kern_setup1(void);
131 extern void ka_init(void);
132 extern void nodename_set(void);
133 
134 /*
135  * for tod fault detection
136  */
137 enum tod_fault_type {
138 	TOD_REVERSED = 0,
139 	TOD_STALLED,
140 	TOD_JUMPED,
141 	TOD_RATECHANGED,
142 	TOD_RDONLY,
143 	TOD_NOFAULT
144 };
145 
146 #define	TOD_GET_FAILED		0x1	/* TOD could not be read */
147 #define	TOD_SET_DONE		0x2	/* TOD has been modified */
148 #define	TOD_CPR_RESUME_DONE	0x4	/* CPR resume has occurred */
149 #define	TOD_DR_RESUME_DONE	0x8	/* DR resume has occurred */
150 
151 extern time_t tod_validate(time_t);
152 extern void tod_status_set(int);
153 extern void tod_status_clear(int);
154 extern void plat_tod_fault(enum tod_fault_type);
155 
156 #ifndef _LP64
157 #ifndef min
158 int min(int, int);
159 #endif
160 
161 #ifndef max
162 int max(int, int);
163 #endif
164 
165 uint_t umin(uint_t, uint_t);
166 uint_t umax(uint_t, uint_t);
167 #endif /* !_LP64 */
168 int grow(caddr_t);
169 int grow_internal(caddr_t, uint_t);
170 int brk_internal(caddr_t, uint_t);
171 typedef uint64_t callout_id_t;
172 timeout_id_t timeout(void (*)(void *), void *, clock_t);
173 timeout_id_t realtime_timeout(void (*)(void *), void *, clock_t);
174 clock_t untimeout(timeout_id_t);
175 /*
176  * The last argument to timeout_generic() is flags. See callo.h for the
177  * flags definitions.
178  */
179 callout_id_t timeout_generic(int, void (*)(void *), void *, hrtime_t, hrtime_t,
180     int);
181 callout_id_t timeout_default(void (*)(void *), void *, clock_t);
182 callout_id_t realtime_timeout_default(void (*)(void *), void *, clock_t);
183 /*
184  * The last argument to untimeout_generic() is flags. See callout.c for the
185  * use.
186  */
187 hrtime_t untimeout_generic(callout_id_t, int);
188 clock_t untimeout_default(callout_id_t, int);
189 void delay(clock_t);
190 int delay_sig(clock_t);
191 void delay_random(clock_t);
192 int nodev();
193 int nulldev();
194 major_t getudev(void);
195 int cmpldev(dev32_t *, dev_t);
196 dev_t expldev(dev32_t);
197 int bcmp(const void *, const void *, size_t) __PURE;
198 int stoi(char **);
199 void numtos(ulong_t, char *);
200 char *kmem_asprintf(const char *fmt, ...);
201 int strident_valid(const char *);
202 void strident_canon(char *, size_t);
203 int getsubopt(char **optionsp, char * const *tokens, char **valuep);
204 char *append_subopt(const char *, size_t, char *, const char *);
205 #ifndef	_FAKE_KERNEL
206 /* conflicts with libc definition */
207 int ffs(uintmax_t);
208 #endif
209 int copyin(const void *, void *, size_t);
210 void copyin_noerr(const void *, void *, size_t);
211 int xcopyin(const void *, void *, size_t);
212 int xcopyin_nta(const void *, void *, size_t, int);
213 int copyout(const void *, void *, size_t);
214 void copyout_noerr(const void *, void *, size_t);
215 int xcopyout(const void *, void *, size_t);
216 int xcopyout_nta(const void *, void *, size_t, int);
217 int copyinstr(const char *, char *, size_t, size_t *);
218 int copyinstr_noerr(const char *, char *, size_t, size_t *);
219 int copyoutstr(const char *, char *, size_t, size_t *);
220 int copyoutstr_noerr(const char *, char *, size_t, size_t *);
221 int copystr(const char *, char *, size_t, size_t *);
222 void ucopy(const void *, void *, size_t);
223 void ucopystr(const char *, char *, size_t, size_t *);
224 void pgcopy(const void *, void *, size_t);
225 void ovbcopy(const void *, void *, size_t);
226 void uzero(void *, size_t);
227 int kcopy(const void *, void *, size_t);
228 int kcopy_nta(const void *, void *, size_t, int);
229 int kzero(void *, size_t);
230 
231 int fuword8(const void *, uint8_t *);
232 int fuword16(const void *, uint16_t *);
233 int fuword32(const void *, uint32_t *);
234 int fulword(const void *, ulong_t *);
235 void fuword8_noerr(const void *, uint8_t *);
236 void fuword16_noerr(const void *, uint16_t *);
237 void fuword32_noerr(const void *, uint32_t *);
238 void fulword_noerr(const void *, ulong_t *);
239 
240 #ifdef _LP64
241 int fuword64(const void *, uint64_t *);
242 void fuword64_noerr(const void *, uint64_t *);
243 #endif
244 
245 int subyte(void *, uint8_t);
246 int suword8(void *, uint8_t);
247 int suword16(void *, uint16_t);
248 int suword32(void *, uint32_t);
249 int sulword(void *, ulong_t);
250 void subyte_noerr(void *, uint8_t);
251 void suword8_noerr(void *, uint8_t);
252 void suword16_noerr(void *, uint16_t);
253 void suword32_noerr(void *, uint32_t);
254 void sulword_noerr(void *, ulong_t);
255 
256 #ifdef _LP64
257 int suword64(void *, uint64_t);
258 void suword64_noerr(void *, uint64_t);
259 #endif
260 
261 #if !defined(_BOOT) && !defined(_FAKE_KERNEL)
262 /* conflicts with libc definition */
263 int setjmp(label_t *) __RETURNS_TWICE;
264 extern void longjmp(label_t *)
265 	__NORETURN;
266 #pragma unknown_control_flow(setjmp)
267 #endif
268 
269 void prefetch_read_once(void *);
270 void prefetch_write_once(void *);
271 void prefetch_read_many(void *);
272 void prefetch_write_many(void *);
273 caddr_t caller(void);
274 caddr_t callee(void);
275 int getpcstack(pc_t *, int);
276 int on_fault(label_t *) __RETURNS_TWICE;
277 void no_fault(void);
278 void halt(char *);
279 int scanc(size_t, uchar_t *, uchar_t *, uchar_t);
280 int movtuc(size_t, uchar_t *, uchar_t *, uchar_t *);
281 int splr(int);
282 int splhigh(void);
283 int splhi(void);
284 int splzs(void);
285 int spl0(void);
286 int spl6(void);
287 int spl7(void);
288 int spl8(void);
289 void splx(int);
290 void set_base_spl(void);
291 int __ipltospl(int);
292 int spl_xcall(void);
293 
294 void softcall_init(void);
295 void softcall(void (*)(void *), void *);
296 void softint(void);
297 
298 extern void sync_icache(caddr_t, uint_t);
299 extern void sync_data_memory(caddr_t, size_t);
300 extern void hot_patch_kernel_text(caddr_t, uint32_t, uint_t);
301 
302 void _insque(caddr_t, caddr_t);
303 void _remque(caddr_t);
304 
305 /* casts to keep lint happy */
306 #define	insque(q, p)	_insque((caddr_t)q, (caddr_t)p)
307 #define	remque(q)	_remque((caddr_t)q)
308 
309 #pragma unknown_control_flow(on_fault)
310 
311 struct timeval;
312 extern void	uniqtime(struct timeval *);
313 struct timeval32;
314 extern void	uniqtime32(struct timeval32 *);
315 
316 uint_t page_num_pagesizes(void);
317 size_t page_get_pagesize(uint_t n);
318 
319 extern int maxusers;
320 extern int pidmax;
321 
322 extern void param_preset(void);
323 extern void param_calc(int);
324 extern void param_init(void);
325 extern void param_check(void);
326 
327 /*
328  * It is easy to run afoul of integer promotion bugs when writing checks for
329  * potential overflow.  These helpers are provided to make the right thing easy.
330  *
331  * They return a "bool-y" int: non-zero for if the addition overflows
332  */
333 static inline int
sum_overflows_u16(uint16_t a,uint16_t b)334 sum_overflows_u16(uint16_t a, uint16_t b)
335 {
336 	return (((uint32_t)a + (uint32_t)b) > UINT16_MAX);
337 }
338 
339 static inline int
sum_overflows_hrtime(hrtime_t a,hrtime_t b)340 sum_overflows_hrtime(hrtime_t a, hrtime_t b)
341 {
342 	return (((unsigned long long)a + (unsigned long long)b) <
343 	    (unsigned long long)a);
344 }
345 
346 static inline int
sum_overflows_off(off_t a,off_t b)347 sum_overflows_off(off_t a, off_t b)
348 {
349 	return (((unsigned long long)a + (unsigned long long)b) <
350 	    (unsigned long long)a);
351 }
352 
353 #endif /* _KERNEL */
354 
355 /*
356  * Structure of the system-entry table.
357  *
358  *	Changes to struct sysent should maintain binary compatibility with
359  *	loadable system calls, although the interface is currently private.
360  *
361  *	This means it should only be expanded on the end, and flag values
362  *	should not be reused.
363  *
364  *	It is desirable to keep the size of this struct a power of 2 for quick
365  *	indexing.
366  */
367 struct sysent {
368 	char		sy_narg;	/* total number of arguments */
369 #ifdef _LP64
370 	unsigned short	sy_flags;	/* various flags as defined below */
371 #else
372 	unsigned char	sy_flags;	/* various flags as defined below */
373 #endif
374 	int		(*sy_call)();	/* argp, rvalp-style handler */
375 	krwlock_t	*sy_lock;	/* lock for loadable system calls */
376 	int64_t		(*sy_callc)();	/* C-style call hander or wrapper */
377 };
378 
379 extern struct sysent	sysent[];
380 #ifdef _SYSCALL32_IMPL
381 extern struct sysent	sysent32[];
382 #endif
383 
384 extern struct sysent	nosys_ent;	/* entry for invalid system call */
385 
386 #define	NSYSCALL	256		/* number of system calls */
387 
388 #define	LOADABLE_SYSCALL(s)	(s->sy_flags & SE_LOADABLE)
389 #define	LOADED_SYSCALL(s)	(s->sy_flags & SE_LOADED)
390 
391 /*
392  * sy_flags values
393  *	Values 1, 2, and 4 were used previously for SETJUMP, ASYNC, and IOSYS.
394  */
395 #define	SE_32RVAL1	0x0		/* handler returns int32_t in rval1 */
396 #define	SE_32RVAL2	0x1		/* handler returns int32_t in rval2 */
397 #define	SE_64RVAL	0x2		/* handler returns int64_t in rvals */
398 #define	SE_RVAL_MASK	0x3		/* mask of rval_t bits */
399 
400 #define	SE_LOADABLE	0x08		/* syscall is loadable */
401 #define	SE_LOADED	0x10		/* syscall is completely loaded */
402 #define	SE_NOUNLOAD	0x20		/* syscall never needs unload */
403 #define	SE_ARGC		0x40		/* syscall takes C-style args */
404 
405 /*
406  * Structure of the return-value parameter passed by reference to
407  * system entries.
408  */
409 union rval {
410 	struct	{
411 		int	r_v1;
412 		int	r_v2;
413 	} r_v;
414 	off_t	r_off;
415 	offset_t r_offset;
416 	time_t	r_time;
417 	int64_t	r_vals;
418 };
419 #define	r_val1	r_v.r_v1
420 #define	r_val2	r_v.r_v2
421 
422 typedef union rval rval_t;
423 
424 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
425 
426 extern void reset_syscall_args(void);
427 extern int save_syscall_args(void);
428 extern uint_t get_syscall_args(klwp_t *lwp, long *argp, int *nargsp);
429 #ifdef _SYSCALL32_IMPL
430 extern uint_t get_syscall32_args(klwp_t *lwp, int *argp, int *nargp);
431 #endif
432 
433 extern uint_t set_errno(uint_t error);
434 #pragma rarely_called(set_errno)
435 
436 extern int64_t syscall_ap(void);
437 extern int64_t loadable_syscall(long, long, long, long, long, long, long, long);
438 extern int64_t nosys(void);
439 extern int nosys32(void);
440 
441 extern void swtch(void);
442 
443 extern uint_t	kcpc_key;	/* TSD key for performance counter context */
444 
445 /*
446  * initname holds the path to init and is used as a point of rendezvous
447  * between krtld (which processes the boot arguments) and the kernel.
448  */
449 #define	INITNAME_SZ	32
450 extern char initname[INITNAME_SZ];
451 
452 /*
453  * initargs holds the arguments to init (such as -v, -s, -r, -m verbose) and
454  * is a point of rendezvous between krtld (which processes the boot arguments)
455  * and the kernel.
456  */
457 extern char initargs[BOOTARGS_MAX];
458 
459 extern int exec_init(const char *, const char *);
460 extern int start_init_common(void);
461 
462 #endif	/* _KERNEL */
463 
464 #if defined(_KERNEL) || defined(_FAKE_KERNEL) || defined(_BOOT)
465 
466 size_t strlcat(char *, const char *, size_t);
467 size_t strlen(const char *) __PURE;
468 char *strcat(char *, const char *);
469 char *strncat(char *, const char *, size_t);
470 char *strcpy(char *, const char *);
471 char *strncpy(char *, const char *, size_t);
472 
473 extern size_t strlcpy(char *, const char *, size_t);
474 extern size_t strspn(const char *, const char *);
475 extern size_t strcspn(const char *, const char *);
476 extern char *strdup(const char *);
477 extern void strfree(char *);
478 
479 /* Need to be consistent with <string.h> C++ definitions */
480 #if __cplusplus >= 199711L
481 extern const char *strchr(const char *, int);
482 #ifndef _STRCHR_INLINE
483 #define	_STRCHR_INLINE
484 extern "C++" {
strchr(char * __s,int __c)485 	inline char *strchr(char *__s, int __c) {
486 		return (char *)strchr((const char *)__s, __c);
487 	}
488 }
489 #endif /* _STRCHR_INLINE */
490 extern const char *strrchr(const char *, int);
491 #ifndef	_STRRCHR_INLINE
492 #define	_STRRCHR_INLINE
493 extern "C++" {
strrchr(char * __s,int __c)494 	inline char *strrchr(char *__s, int __c) {
495 		return (char *)strrchr((const char *)__s, __c);
496 	}
497 }
498 #endif	/* _STRRCHR_INLINE */
499 extern const char *strstr(const char *, const char *);
500 #ifndef	_STRSTR_INLINE
501 #define	_STRSTR_INLINE
502 extern "C++" {
strstr(char * __s1,const char * __s2)503 	inline char *strstr(char *__s1, const char *__s2) {
504 		return (char *)strstr((const char *)__s1, __s2);
505 	}
506 }
507 #endif  /* _STRSTR_INLINE */
508 #else	/* __cplusplus >= 199711L */
509 char *strchr(const char *, int);
510 char *strrchr(const char *, int);
511 char *strstr(const char *, const char *);
512 #endif	/* __cplusplus >= 199711L */
513 char *strnrchr(const char *, int, size_t);
514 int strcmp(const char *, const char *) __PURE;
515 int strncmp(const char *, const char *, size_t) __PURE;
516 int strcasecmp(const char *, const char *) __PURE;
517 int strncasecmp(const char *, const char *, size_t) __PURE;
518 /* Need to be consistent with <string.h> C++ definitions */
519 #if __cplusplus >= 199711L
520 extern const char *strpbrk(const char *, const char *);
521 #ifndef _STRPBRK_INLINE
522 #define	_STRPBRK_INLINE
523 extern "C++" {
strpbrk(char * __s1,const char * __s2)524 	inline char *strpbrk(char *__s1, const char *__s2) {
525 		return (char *)strpbrk((const char *)__s1, __s2);
526 	}
527 }
528 #endif /* _STRPBRK_INLINE */
529 #else /* __cplusplus >= 199711L */
530 char *strpbrk(const char *, const char *);
531 #endif /* __cplusplus >= 199711L */
532 void bcopy(const void *, void *, size_t);
533 void bzero(void *, size_t);
534 
535 extern void *memset(void *, int, size_t);
536 extern void *memcpy(void *, const void *, size_t);
537 extern void *memmove(void *, const void *, size_t);
538 extern int memcmp(const void *, const void *, size_t);
539 
540 #ifdef __lint
541 extern	int	__lintzero;	/* for spoofing lint */
542 #else	/* __lint */
543 #define	__lintzero 0
544 #endif	/* __lint */
545 #endif /* _KERNEL || _BOOT */
546 #endif /* !_STANDALONE */
547 
548 #ifdef	__cplusplus
549 }
550 #endif
551 
552 #endif	/* _SYS_SYSTM_H */
553