1b819cea2SGordon Ross /*
2b819cea2SGordon Ross  * CDDL HEADER START
3b819cea2SGordon Ross  *
4b819cea2SGordon Ross  * The contents of this file are subject to the terms of the
5b819cea2SGordon Ross  * Common Development and Distribution License (the "License").
6b819cea2SGordon Ross  * You may not use this file except in compliance with the License.
7b819cea2SGordon Ross  *
8b819cea2SGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b819cea2SGordon Ross  * or http://www.opensolaris.org/os/licensing.
10b819cea2SGordon Ross  * See the License for the specific language governing permissions
11b819cea2SGordon Ross  * and limitations under the License.
12b819cea2SGordon Ross  *
13b819cea2SGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14b819cea2SGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b819cea2SGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16b819cea2SGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17b819cea2SGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18b819cea2SGordon Ross  *
19b819cea2SGordon Ross  * CDDL HEADER END
20b819cea2SGordon Ross  */
21b819cea2SGordon Ross /*
22b819cea2SGordon Ross  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23b819cea2SGordon Ross  * Copyright (c) 2012 by Delphix. All rights reserved.
24*8329232eSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
26a923e7f4SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
27b819cea2SGordon Ross  */
28b819cea2SGordon Ross 
29b819cea2SGordon Ross #include <sys/param.h>
30b819cea2SGordon Ross #include <sys/types.h>
31b819cea2SGordon Ross #include <sys/varargs.h>
32b819cea2SGordon Ross #include <sys/systm.h>
33b819cea2SGordon Ross #include <sys/cmn_err.h>
34b819cea2SGordon Ross #include <sys/log.h>
35b819cea2SGordon Ross 
36b819cea2SGordon Ross #include <fakekernel.h>
37b819cea2SGordon Ross 
38b819cea2SGordon Ross void	abort(void) __NORETURN;
39*8329232eSGordon Ross void	debug_enter(char *);
40b819cea2SGordon Ross 
41b819cea2SGordon Ross char *volatile panicstr;
42b819cea2SGordon Ross va_list  panicargs;
43b819cea2SGordon Ross char panicbuf[512];
44b819cea2SGordon Ross 
45b819cea2SGordon Ross volatile int aok;
46b819cea2SGordon Ross 
47b819cea2SGordon Ross static const int
48b819cea2SGordon Ross ce_flags[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
49b819cea2SGordon Ross static const char
50b819cea2SGordon Ross ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
51b819cea2SGordon Ross static const char
52b819cea2SGordon Ross ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
53b819cea2SGordon Ross 
54b819cea2SGordon Ross 
55b819cea2SGordon Ross /*
56b819cea2SGordon Ross  * This function is just a stub, exported NODIRECT so that
57b819cea2SGordon Ross  * comsumers like fksmbd can provide their own.
58b819cea2SGordon Ross  * (One that actually prints the messages.)
59b819cea2SGordon Ross  *
60b819cea2SGordon Ross  * It's used by fakekernel_cprintf() below.
61b819cea2SGordon Ross  * The flags are SL_... from strlog.h
62b819cea2SGordon Ross  */
63b819cea2SGordon Ross /* ARGSUSED */
64b819cea2SGordon Ross void
65b819cea2SGordon Ross fakekernel_putlog(char *msg, size_t len, int flags)
66b819cea2SGordon Ross {
67b819cea2SGordon Ross }
68b819cea2SGordon Ross 
69b819cea2SGordon Ross /*
70b819cea2SGordon Ross  * fakekernel_cprintf() corresponds to os/printf.c:cprintf()
71b819cea2SGordon Ross  * This formats the message and calls fakekernel_putlog().
72b819cea2SGordon Ross  * It's exported NODIRECT to allow replacment.
73b819cea2SGordon Ross  * The flags are SL_... from strlog.h
74b819cea2SGordon Ross  */
75b819cea2SGordon Ross void
76b819cea2SGordon Ross fakekernel_cprintf(const char *fmt, va_list adx, int flags,
77f06dce2cSAndrew Stormont     const char *prefix, const char *suffix)
78b819cea2SGordon Ross {
79b819cea2SGordon Ross 	size_t bufsize = LOG_MSGSIZE;
80b819cea2SGordon Ross 	char buf[LOG_MSGSIZE];
81b819cea2SGordon Ross 	char *bufp = buf;
82b819cea2SGordon Ross 	char *msgp, *bufend;
83b819cea2SGordon Ross 	size_t len;
84b819cea2SGordon Ross 
85b819cea2SGordon Ross 	if (strchr("^!?", fmt[0]) != NULL) {
86b819cea2SGordon Ross 		if (fmt[0] == '^')
87b819cea2SGordon Ross 			flags |= SL_CONSONLY;
88b819cea2SGordon Ross 		else if (fmt[0] == '!')
89b819cea2SGordon Ross 			flags |= SL_LOGONLY;
90b819cea2SGordon Ross 		fmt++;
91b819cea2SGordon Ross 	}
92b819cea2SGordon Ross 
93b819cea2SGordon Ross 	bufend = bufp + bufsize;
94b819cea2SGordon Ross 	msgp = bufp;
95b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, "[fake_kernel] ");
96b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, prefix);
97b819cea2SGordon Ross 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
98b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, suffix);
99b819cea2SGordon Ross 	len = msgp - bufp;
100b819cea2SGordon Ross 
101b819cea2SGordon Ross 	fakekernel_putlog(bufp, len, flags);
102b819cea2SGordon Ross }
103b819cea2SGordon Ross 
104*8329232eSGordon Ross /* ARGSUSED */
105*8329232eSGordon Ross void
106*8329232eSGordon Ross vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
107*8329232eSGordon Ross {
108*8329232eSGordon Ross 	fakekernel_cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "");
109*8329232eSGordon Ross }
110*8329232eSGordon Ross 
111*8329232eSGordon Ross /*PRINTFLIKE2*/
112*8329232eSGordon Ross void
113*8329232eSGordon Ross zprintf(zoneid_t zoneid, const char *fmt, ...)
114*8329232eSGordon Ross {
115*8329232eSGordon Ross 	va_list adx;
116*8329232eSGordon Ross 
117*8329232eSGordon Ross 	va_start(adx, fmt);
118*8329232eSGordon Ross 	vzprintf(zoneid, fmt, adx);
119*8329232eSGordon Ross 	va_end(adx);
120*8329232eSGordon Ross }
121*8329232eSGordon Ross 
122b819cea2SGordon Ross /*
123b819cea2SGordon Ross  * "User-level crash dump", if you will.
124b819cea2SGordon Ross  */
125b819cea2SGordon Ross void
126b819cea2SGordon Ross vpanic(const char *fmt, va_list adx)
127b819cea2SGordon Ross {
128b819cea2SGordon Ross 	va_list tmpargs;
129b819cea2SGordon Ross 
130b819cea2SGordon Ross 	panicstr = (char *)fmt;
131b819cea2SGordon Ross 	va_copy(panicargs, adx);
132b819cea2SGordon Ross 
133b819cea2SGordon Ross 	va_copy(tmpargs, adx);
134b819cea2SGordon Ross 	fakekernel_cprintf(fmt, tmpargs, SL_FATAL, "fatal: ", "\n");
135b819cea2SGordon Ross 
136b819cea2SGordon Ross 	/* Call libc`assfail() so that mdb ::status works */
137b819cea2SGordon Ross 	(void) vsnprintf(panicbuf, sizeof (panicbuf), fmt, adx);
138*8329232eSGordon Ross 	debug_enter(panicbuf);
139a923e7f4SJohn Levon 	(void) assfail(panicbuf, "(panic)", 0);
140b819cea2SGordon Ross 
141b819cea2SGordon Ross 	abort();	/* avoid "noreturn" warnings */
142b819cea2SGordon Ross }
143b819cea2SGordon Ross 
144b819cea2SGordon Ross void
145b819cea2SGordon Ross panic(const char *fmt, ...)
146b819cea2SGordon Ross {
147b819cea2SGordon Ross 	va_list adx;
148b819cea2SGordon Ross 
149b819cea2SGordon Ross 	va_start(adx, fmt);
150b819cea2SGordon Ross 	vpanic(fmt, adx);
151b819cea2SGordon Ross 	va_end(adx);
152b819cea2SGordon Ross }
153b819cea2SGordon Ross 
154f06dce2cSAndrew Stormont void
155f06dce2cSAndrew Stormont fm_panic(const char *fmt, ...)
156f06dce2cSAndrew Stormont {
157f06dce2cSAndrew Stormont 	va_list adx;
158f06dce2cSAndrew Stormont 
159f06dce2cSAndrew Stormont 	va_start(adx, fmt);
160f06dce2cSAndrew Stormont 	vpanic(fmt, adx);
161f06dce2cSAndrew Stormont 	va_end(adx);
162f06dce2cSAndrew Stormont }
163f06dce2cSAndrew Stormont 
164b819cea2SGordon Ross void
165b819cea2SGordon Ross vcmn_err(int ce, const char *fmt, va_list adx)
166b819cea2SGordon Ross {
167b819cea2SGordon Ross 
168b819cea2SGordon Ross 	if (ce == CE_PANIC)
169b819cea2SGordon Ross 		vpanic(fmt, adx);
170b819cea2SGordon Ross 	if (ce >= CE_IGNORE)
171b819cea2SGordon Ross 		return;
172b819cea2SGordon Ross 
173b819cea2SGordon Ross 	fakekernel_cprintf(fmt, adx, ce_flags[ce] | SL_CONSOLE,
174b819cea2SGordon Ross 	    ce_prefix[ce], ce_suffix[ce]);
175b819cea2SGordon Ross }
176b819cea2SGordon Ross 
177b819cea2SGordon Ross /*PRINTFLIKE2*/
178b819cea2SGordon Ross void
179b819cea2SGordon Ross cmn_err(int ce, const char *fmt, ...)
180b819cea2SGordon Ross {
181b819cea2SGordon Ross 	va_list adx;
182b819cea2SGordon Ross 
183b819cea2SGordon Ross 	va_start(adx, fmt);
184b819cea2SGordon Ross 	vcmn_err(ce, fmt, adx);
185b819cea2SGordon Ross 	va_end(adx);
186b819cea2SGordon Ross }
187*8329232eSGordon Ross 
188*8329232eSGordon Ross /* ARGSUSED */
189*8329232eSGordon Ross void
190*8329232eSGordon Ross debug_enter(char *str)
191*8329232eSGordon Ross {
192*8329232eSGordon Ross 	/* Just a place for a break point. */
193*8329232eSGordon Ross }
194