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 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 *
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2016 Joyent, Inc.
28 */
29
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/inline.h>
34 #include <sys/varargs.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/cmn_err.h>
38 #include <sys/syslog.h>
39 #include <sys/log.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/session.h>
43 #include <sys/stream.h>
44 #include <sys/kmem.h>
45 #include <sys/kobj.h>
46 #include <sys/atomic.h>
47 #include <sys/console.h>
48 #include <sys/cpuvar.h>
49 #include <sys/modctl.h>
50 #include <sys/reboot.h>
51 #include <sys/debug.h>
52 #include <sys/panic.h>
53 #include <sys/spl.h>
54 #include <sys/zone.h>
55 #include <sys/sunddi.h>
56
57 /*
58 * In some debugging situations it's useful to log all messages to panicbuf,
59 * which is persistent across reboots (on most platforms). The range
60 * panicbuf[panicbuf_log..PANICBUFSIZE-1] may be used for this purpose.
61 * By default, panicbuf_log == PANICBUFSIZE and no messages are logged.
62 * To enable panicbuf logging, set panicbuf_log to a small value, say 1K;
63 * this will reserve 1K for panic information and 7K for message logging.
64 */
65 uint32_t panicbuf_log = PANICBUFSIZE;
66 uint32_t panicbuf_index = PANICBUFSIZE;
67
68 int aask, aok;
69 static int ce_to_sl[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
70 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
71 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
72
73 static void
cprintf(const char * fmt,va_list adx,int sl,const char * prefix,const char * suffix,void * site,int mid,int sid,int level,zoneid_t zoneid,dev_info_t * dip)74 cprintf(const char *fmt, va_list adx, int sl, const char *prefix,
75 const char *suffix, void *site, int mid, int sid, int level,
76 zoneid_t zoneid, dev_info_t *dip)
77 {
78 uint32_t msgid;
79 size_t bufsize = LOG_MSGSIZE;
80 char buf[LOG_MSGSIZE];
81 char *bufp = buf;
82 char *body, *msgp, *bufend;
83 mblk_t *mp;
84 int s, on_intr;
85 size_t len;
86
87 s = splhi();
88 on_intr = CPU_ON_INTR(CPU) ||
89 (interrupts_unleashed && (spltoipl(s) > LOCK_LEVEL));
90 splx(s);
91
92 ASSERT(zoneid == GLOBAL_ZONEID || !on_intr);
93
94 STRLOG_MAKE_MSGID(fmt, msgid);
95
96 if (strchr("^!?", fmt[0]) != NULL) {
97 if (fmt[0] == '^')
98 sl |= SL_CONSONLY;
99 else if (fmt[0] == '!' ||
100 (prefix[0] == '\0' && !(boothowto & RB_VERBOSE)))
101 sl = (sl & ~(SL_USER | SL_NOTE)) | SL_LOGONLY;
102 fmt++;
103 }
104
105 if ((sl & SL_USER) && (MUTEX_HELD(&pidlock) || on_intr)) {
106 zoneid = getzoneid();
107 sl = sl & ~(SL_USER | SL_LOGONLY) | SL_CONSOLE;
108 }
109
110 retry:
111 bufend = bufp + bufsize;
112 msgp = bufp;
113 body = msgp += snprintf(msgp, bufend - msgp,
114 "%s: [ID %u FACILITY_AND_PRIORITY] ",
115 mod_containing_pc(site), msgid);
116 msgp += snprintf(msgp, bufend - msgp, prefix);
117 if (dip != NULL)
118 msgp += snprintf(msgp, bufend - msgp, "%s%d: ",
119 ddi_driver_name(dip), ddi_get_instance(dip));
120 msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
121 msgp += snprintf(msgp, bufend - msgp, suffix);
122 len = strlen(body);
123
124 if (((sl & SL_CONSONLY) && panicstr) ||
125 (zoneid == GLOBAL_ZONEID && log_global.lz_active == 0)) {
126 console_printf("%s", body);
127 goto out;
128 }
129
130 if (msgp - bufp >= bufsize && !on_intr) {
131 ASSERT(bufp == buf);
132 bufsize = msgp - bufp + 1;
133 bufp = kmem_alloc(bufsize, KM_NOSLEEP);
134 if (bufp != NULL)
135 goto retry;
136 bufsize = LOG_MSGSIZE;
137 bufp = buf;
138 }
139
140 mp = log_makemsg(mid, sid, level, sl, LOG_KERN, bufp,
141 MIN(bufsize, msgp - bufp + 1), on_intr);
142 if (mp == NULL) {
143 if ((sl & (SL_CONSOLE | SL_LOGONLY)) == SL_CONSOLE && !on_intr)
144 console_printf("%s", body);
145 goto out;
146 }
147
148 if (sl & SL_USER) {
149 ssize_t resid;
150 sess_t *sp;
151
152 if ((sp = tty_hold()) != NULL) {
153 if (sp->s_vp != NULL)
154 (void) vn_rdwr(UIO_WRITE, sp->s_vp, body,
155 len, 0LL, UIO_SYSSPACE, FAPPEND,
156 (rlim64_t)LOG_HIWAT, kcred, &resid);
157 tty_rele(sp);
158 }
159 }
160
161 if (on_intr && !panicstr) {
162 (void) putq(log_intrq, mp);
163 softcall((void (*)(void *))log_flushq, log_intrq);
164 } else {
165 log_sendmsg(mp, zoneid);
166 }
167 out:
168 if (panicbuf_log + len < PANICBUFSIZE) {
169 uint32_t old, new;
170 do {
171 old = panicbuf_index;
172 new = old + len;
173 if (new >= PANICBUFSIZE)
174 new = panicbuf_log + len;
175 } while (atomic_cas_32(&panicbuf_index, old, new) != old);
176 bcopy(body, &panicbuf[new - len], len);
177 }
178 if (bufp != buf)
179 kmem_free(bufp, bufsize);
180 }
181
182 void
vzprintf(zoneid_t zoneid,const char * fmt,va_list adx)183 vzprintf(zoneid_t zoneid, const char *fmt, va_list adx)
184 {
185 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
186 zoneid, NULL);
187 }
188
189 void
vprintf(const char * fmt,va_list adx)190 vprintf(const char *fmt, va_list adx)
191 {
192 vzprintf(GLOBAL_ZONEID, fmt, adx);
193 }
194
195 /*PRINTFLIKE1*/
196 void
printf(const char * fmt,...)197 printf(const char *fmt, ...)
198 {
199 va_list adx;
200
201 va_start(adx, fmt);
202 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
203 GLOBAL_ZONEID, NULL);
204 va_end(adx);
205 }
206
207 /*PRINTFLIKE2*/
208 void
zprintf(zoneid_t zoneid,const char * fmt,...)209 zprintf(zoneid_t zoneid, const char *fmt, ...)
210 {
211 va_list adx;
212
213 va_start(adx, fmt);
214 cprintf(fmt, adx, SL_CONSOLE | SL_NOTE, "", "", caller(), 0, 0, 0,
215 zoneid, NULL);
216 va_end(adx);
217 }
218
219 void
vuprintf(const char * fmt,va_list adx)220 vuprintf(const char *fmt, va_list adx)
221 {
222 va_list adxcp;
223 va_copy(adxcp, adx);
224
225 /* Message the user tty, if any, and the global zone syslog */
226 cprintf(fmt, adx, SL_CONSOLE | SL_LOGONLY | SL_USER | SL_NOTE,
227 "", "", caller(), 0, 0, 0, GLOBAL_ZONEID, NULL);
228
229 /* Now message the local zone syslog */
230 if (!INGLOBALZONE(curproc))
231 cprintf(fmt, adxcp, SL_CONSOLE | SL_LOGONLY | SL_NOTE,
232 "", "", caller(), 0, 0, 0, getzoneid(), NULL);
233
234 va_end(adxcp);
235 }
236
237 /*PRINTFLIKE1*/
238 void
uprintf(const char * fmt,...)239 uprintf(const char *fmt, ...)
240 {
241 va_list adx;
242
243 va_start(adx, fmt);
244
245 vuprintf(fmt, adx);
246
247 va_end(adx);
248 }
249
250 static void
vzdcmn_err(zoneid_t zoneid,void * site,int ce,const char * fmt,va_list adx,dev_info_t * dip)251 vzdcmn_err(zoneid_t zoneid, void *site, int ce, const char *fmt, va_list adx,
252 dev_info_t *dip)
253 {
254 if (ce == CE_PANIC) {
255 panic_dip = dip;
256 vpanic(fmt, adx);
257 }
258
259 if ((uint_t)ce < CE_IGNORE) {
260 cprintf(fmt, adx, ce_to_sl[ce] | SL_CONSOLE,
261 ce_prefix[ce], ce_suffix[ce], site, 0, 0, 0,
262 zoneid, dip);
263 }
264 }
265
266 void
vzcmn_err(zoneid_t zoneid,int ce,const char * fmt,va_list adx)267 vzcmn_err(zoneid_t zoneid, int ce, const char *fmt, va_list adx)
268 {
269 vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
270 }
271
272 void
vcmn_err(int ce,const char * fmt,va_list adx)273 vcmn_err(int ce, const char *fmt, va_list adx)
274 {
275 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
276 }
277
278 /*PRINTFLIKE3*/
279 void
vdev_err(dev_info_t * dip,int ce,const char * fmt,va_list adx)280 vdev_err(dev_info_t *dip, int ce, const char *fmt, va_list adx)
281 {
282 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
283 }
284
285 /*PRINTFLIKE2*/
286 void
cmn_err(int ce,const char * fmt,...)287 cmn_err(int ce, const char *fmt, ...)
288 {
289 va_list adx;
290
291 va_start(adx, fmt);
292 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, NULL);
293 va_end(adx);
294 }
295
296 /*PRINTFLIKE3*/
297 void
zcmn_err(zoneid_t zoneid,int ce,const char * fmt,...)298 zcmn_err(zoneid_t zoneid, int ce, const char *fmt, ...)
299 {
300 va_list adx;
301
302 va_start(adx, fmt);
303 vzdcmn_err(zoneid, caller(), ce, fmt, adx, NULL);
304 va_end(adx);
305 }
306
307 /*PRINTFLIKE3*/
308 void
dev_err(dev_info_t * dip,int ce,char * fmt,...)309 dev_err(dev_info_t *dip, int ce, char *fmt, ...)
310 {
311 va_list adx;
312
313 va_start(adx, fmt);
314 vzdcmn_err(GLOBAL_ZONEID, caller(), ce, fmt, adx, dip);
315 va_end(adx);
316 }
317
318 void
assfail(const char * a,const char * f,int l)319 assfail(const char *a, const char *f, int l)
320 {
321 if (aask) {
322 printf("ASSERTION CAUGHT: %s, file: %s, line: %d", a, f, l);
323 debug_enter(NULL);
324 }
325
326 if (!aok && !panicstr)
327 panic("assertion failed: %s, file: %s, line: %d", a, f, l);
328 }
329
330 void
assfail3(const char * a,uintmax_t lv,const char * op,uintmax_t rv,const char * f,int l)331 assfail3(const char *a, uintmax_t lv, const char *op, uintmax_t rv,
332 const char *f, int l)
333 {
334 if (aask) {
335 printf("ASSERTION CAUGHT: %s (0x%llx %s 0x%llx), file: %s, "
336 "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
337 f, l);
338 debug_enter(NULL);
339 }
340
341 if (!aok && !panicstr)
342 panic("assertion failed: %s (0x%llx %s 0x%llx), file: %s, "
343 "line: %d", a, (u_longlong_t)lv, op, (u_longlong_t)rv,
344 f, l);
345 }
346
347 int
strlog(short mid,short sid,char level,ushort_t sl,char * fmt,...)348 strlog(short mid, short sid, char level, ushort_t sl, char *fmt, ...)
349 {
350 if (sl & log_global.lz_active) {
351 va_list adx;
352 va_start(adx, fmt);
353 cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
354 GLOBAL_ZONEID, NULL);
355 va_end(adx);
356 }
357 return (1);
358 }
359
360 int
vstrlog(short mid,short sid,char level,ushort_t sl,char * fmt,va_list adx)361 vstrlog(short mid, short sid, char level, ushort_t sl, char *fmt, va_list adx)
362 {
363 if (sl & log_global.lz_active)
364 cprintf(fmt, adx, sl, "", "", caller(), mid, sid, level,
365 GLOBAL_ZONEID, NULL);
366 return (1);
367 }
368