xref: /freebsd/sys/sys/exterrvar.h (revision 9521b0b91ea31b7f31b3800772ac6502c822ae56)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2025 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software were developed by Konstantin Belousov <kib@FreeBSD.org>
8  * under sponsorship from the FreeBSD Foundation.
9  */
10 
11 #ifndef _SYS_EXTERRVAR_H_
12 #define	_SYS_EXTERRVAR_H_
13 
14 #include <sys/_exterr.h>
15 #include <sys/_uexterror.h>
16 #include <sys/exterr_cat.h>
17 
18 #define	UEXTERROR_MAXLEN	256
19 
20 #define	UEXTERROR_VER		0x10010002
21 
22 #define	EXTERRCTL_ENABLE	1
23 #define	EXTERRCTL_DISABLE	2
24 #define	EXTERRCTL_UD		3
25 
26 #define	EXTERRCTLF_FORCE	0x00000001
27 
28 #ifdef _KERNEL
29 
30 #ifndef EXTERR_CATEGORY
31 #error "Specify error category before including sys/exterrvar.h"
32 #endif
33 
34 #ifdef	EXTERR_STRINGS
35 #define	SET_ERROR_MSG(mmsg)	(mmsg)
36 #else
37 #define	SET_ERROR_MSG(mmsg)	NULL
38 #endif
39 
40 #define	_SET_ERROR2_KE(kep, eerror, mmsg, pp1, pp2)	({		\
41 	(kep)->error = (eerror);					\
42 	(kep)->cat = EXTERR_CATEGORY;					\
43 	(kep)->msg = SET_ERROR_MSG(mmsg);				\
44 	(kep)->p1 = (pp1);						\
45 	(kep)->p2 = (pp2);						\
46 	(kep)->src_line = __LINE__;					\
47 	(kep)->error;					       		\
48 })
49 #define	_SET_ERROR0_KE(kep, eerror, mmsg)				\
50 	_SET_ERROR2_KE(kep, eerror, mmsg, 0, 0)
51 #define	_SET_ERROR1_KE(kep, eerror, mmsg, pp1)				\
52 	_SET_ERROR2_KE(kep, eerror, mmsg, pp1, 0)
53 
54 #define	_EXTERROR_MACRO_KE(kep, eerror, mmsg, _1, _2, NAME, ...)	\
55 	NAME
56 #define	EXTERROR_KE(...)						\
57 	_EXTERROR_MACRO_KE(__VA_ARGS__, _SET_ERROR2_KE, _SET_ERROR1_KE,	\
58 	    _SET_ERROR0_KE)(__VA_ARGS__)
59 
60 #define	_SET_ERROR2(eerror, mmsg, pp1, pp2)				\
61 	exterr_set(eerror, EXTERR_CATEGORY, SET_ERROR_MSG(mmsg),	\
62 	    (uintptr_t)(pp1), (uintptr_t)(pp2), __LINE__)
63 #define	_SET_ERROR0(eerror, mmsg)	_SET_ERROR2(eerror, mmsg, 0, 0)
64 #define	_SET_ERROR1(eerror, mmsg, pp1)	_SET_ERROR2(eerror, mmsg, pp1, 0)
65 
66 #define	_EXTERROR_MACRO(eerror, mmsg, _1, _2, NAME, ...)		\
67 	NAME
68 #define	EXTERROR(...)							\
69 	_EXTERROR_MACRO(__VA_ARGS__, _SET_ERROR2, _SET_ERROR1,		\
70 	    _SET_ERROR0)(__VA_ARGS__)
71 
72 void exterr_clear(struct kexterr *ke);
73 void exterr_db_print(struct kexterr *ke);
74 int exterr_set_from(const struct kexterr *ke);
75 int exterr_set(int eerror, int category, const char *mmsg, uintptr_t pp1,
76     uintptr_t pp2, int line);
77 int exterr_to_ue(struct thread *td, struct uexterror *ue);
78 void ktrexterr(struct thread *td);
79 
80 #else	/* _KERNEL */
81 
82 #include <sys/types.h>
83 
84 __BEGIN_DECLS
85 int exterrctl(u_int op, u_int flags, void *ptr);
86 __END_DECLS
87 
88 #endif	/* _KERNEL */
89 
90 #endif
91