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