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