xref: /xnu-8796.141.3/bsd/kern/subr_log.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1 /*
2  * Copyright (c) 2000-2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30  * Copyright (c) 1982, 1986, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. All advertising materials mentioning features or use of this software
42  *    must display the following acknowledgement:
43  *	This product includes software developed by the University of
44  *	California, Berkeley and its contributors.
45  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)subr_log.c	8.3 (Berkeley) 2/14/95
62  */
63 
64 /*
65  * Error log buffer for kernel printf's.
66  */
67 
68 #include <machine/atomic.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/proc_internal.h>
72 #include <sys/vnode.h>
73 #include <stdbool.h>
74 #include <firehose/tracepoint_private.h>
75 #include <firehose/chunk_private.h>
76 #include <firehose/ioctl_private.h>
77 #include <os/firehose_buffer_private.h>
78 
79 #include <os/log_private.h>
80 #include <sys/ioctl.h>
81 #include <sys/msgbuf.h>
82 #include <sys/file_internal.h>
83 #include <sys/errno.h>
84 #include <sys/select.h>
85 #include <sys/kernel.h>
86 #include <kern/thread.h>
87 #include <kern/sched_prim.h>
88 #include <kern/simple_lock.h>
89 #include <sys/lock.h>
90 #include <sys/signalvar.h>
91 #include <sys/conf.h>
92 #include <sys/sysctl.h>
93 #include <sys/queue.h>
94 #include <kern/kalloc.h>
95 #include <pexpert/pexpert.h>
96 #include <mach/mach_port.h>
97 #include <mach/mach_vm.h>
98 #include <mach/vm_map.h>
99 #include <vm/vm_kern.h>
100 #include <kern/task.h>
101 #include <kern/locks.h>
102 
103 extern void logwakeup(struct msgbuf *);
104 extern void oslogwakeup(void);
105 extern bool os_log_disabled(void);
106 
107 SECURITY_READ_ONLY_LATE(vm_offset_t) kernel_firehose_addr = 0;
108 SECURITY_READ_ONLY_LATE(uint8_t) __firehose_buffer_kernel_chunk_count =
109     FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
110 SECURITY_READ_ONLY_LATE(uint8_t) __firehose_num_kernel_io_pages =
111     FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
112 
113 uint32_t oslog_msgbuf_dropped_charcount = 0;
114 
115 #define LOG_RDPRI       (PZERO + 1)
116 #define LOG_NBIO        0x02
117 #define LOG_ASYNC       0x04
118 #define LOG_RDWAIT      0x08
119 
120 /* All globals should be accessed under bsd_log_lock() or bsd_log_lock_safe() */
121 
122 static char amsg_bufc[1024];
123 static struct msgbuf aslbuf = {.msg_magic = MSG_MAGIC, .msg_size = sizeof(amsg_bufc), .msg_bufx = 0, .msg_bufr = 0, .msg_bufc = amsg_bufc};
124 struct msgbuf *aslbufp __attribute__((used)) = &aslbuf;
125 
126 /* logsoftc only valid while log_open=1 */
127 struct logsoftc {
128 	int     sc_state;               /* see above for possibilities */
129 	struct  selinfo sc_selp;        /* thread waiting for select */
130 	int     sc_pgid;                /* process/group for async I/O */
131 	struct msgbuf *sc_mbp;
132 } logsoftc;
133 
134 static int log_open;
135 char smsg_bufc[CONFIG_MSG_BSIZE]; /* static buffer */
136 struct firehose_chunk_s oslog_boot_buf = {
137 	.fc_pos = {
138 		.fcp_next_entry_offs = offsetof(struct firehose_chunk_s, fc_data),
139 		.fcp_private_offs = FIREHOSE_CHUNK_SIZE,
140 		.fcp_refcnt = 1, // indicate that there is a writer to this chunk
141 		.fcp_stream = firehose_stream_persist,
142 		.fcp_flag_io = 1, // for now, lets assume this is coming from the io bank
143 	},
144 }; /* static buffer */
145 firehose_chunk_t firehose_boot_chunk = &oslog_boot_buf;
146 struct msgbuf msgbuf = {.msg_magic  = MSG_MAGIC, .msg_size = sizeof(smsg_bufc), .msg_bufx = 0, .msg_bufr = 0, .msg_bufc = smsg_bufc};
147 struct msgbuf *msgbufp __attribute__((used)) = &msgbuf;
148 
149 int     oslog_open = 0;
150 bool    os_log_wakeup = false;
151 
152 /* oslogsoftc only valid while oslog_open=1 */
153 struct oslogsoftc {
154 	int     sc_state;               /* see above for possibilities */
155 	struct  selinfo sc_selp;        /* thread waiting for select */
156 	int     sc_pgid;                /* process/group for async I/O */
157 } oslogsoftc;
158 
159 /* defined in osfmk/kern/printf.c  */
160 extern bool bsd_log_lock(bool);
161 extern void bsd_log_lock_safe(void);
162 extern void bsd_log_unlock(void);
163 
164 /* XXX wants a linker set so these can be static */
165 extern d_open_t         logopen;
166 extern d_close_t        logclose;
167 extern d_read_t         logread;
168 extern d_ioctl_t        logioctl;
169 extern d_select_t       logselect;
170 
171 /* XXX wants a linker set so these can be static */
172 extern d_open_t         oslogopen;
173 extern d_close_t        oslogclose;
174 extern d_select_t       oslogselect;
175 extern d_ioctl_t        oslogioctl;
176 
177 /*
178  * Serialize log access.  Note that the log can be written at interrupt level,
179  * so any log manipulations that can be done from, or affect, another processor
180  * at interrupt level must be guarded with a spin lock.
181  */
182 
183 static int sysctl_kern_msgbuf(struct sysctl_oid *oidp,
184     void *arg1, int arg2, struct sysctl_req *req);
185 
186 /*ARGSUSED*/
187 int
logopen(__unused dev_t dev,__unused int flags,__unused int mode,struct proc * p)188 logopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
189 {
190 	bsd_log_lock_safe();
191 	if (log_open) {
192 		bsd_log_unlock();
193 		return EBUSY;
194 	}
195 	if (atm_get_diagnostic_config() & ATM_ENABLE_LEGACY_LOGGING) {
196 		logsoftc.sc_mbp = msgbufp;
197 	} else {
198 		/*
199 		 * Support for messagetracer (kern_asl_msg())
200 		 * In this mode, /dev/klog exports only ASL-formatted messages
201 		 * written into aslbufp via vaddlog().
202 		 */
203 		logsoftc.sc_mbp = aslbufp;
204 	}
205 	logsoftc.sc_pgid = proc_getpid(p);            /* signal process only */
206 	log_open = 1;
207 
208 	bsd_log_unlock();
209 
210 	return 0;
211 }
212 
213 /*ARGSUSED*/
214 int
logclose(__unused dev_t dev,__unused int flag,__unused int devtype,__unused struct proc * p)215 logclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
216 {
217 	bsd_log_lock_safe();
218 	logsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
219 	selthreadclear(&logsoftc.sc_selp);
220 	log_open = 0;
221 	bsd_log_unlock();
222 	return 0;
223 }
224 
225 
226 int
oslogopen(__unused dev_t dev,__unused int flags,__unused int mode,struct proc * p)227 oslogopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
228 {
229 	bsd_log_lock_safe();
230 	if (oslog_open) {
231 		bsd_log_unlock();
232 		return EBUSY;
233 	}
234 	oslogsoftc.sc_pgid = proc_getpid(p);          /* signal process only */
235 	oslog_open = 1;
236 
237 	bsd_log_unlock();
238 	return 0;
239 }
240 
241 int
oslogclose(__unused dev_t dev,__unused int flag,__unused int devtype,__unused struct proc * p)242 oslogclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
243 {
244 	bsd_log_lock_safe();
245 	oslogsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
246 	selthreadclear(&oslogsoftc.sc_selp);
247 	oslog_open = 0;
248 	bsd_log_unlock();
249 	return 0;
250 }
251 
252 /*ARGSUSED*/
253 int
logread(__unused dev_t dev,struct uio * uio,int flag)254 logread(__unused dev_t dev, struct uio *uio, int flag)
255 {
256 	int error = 0;
257 	struct msgbuf *mbp = logsoftc.sc_mbp;
258 	ssize_t resid;
259 
260 	bsd_log_lock_safe();
261 	while (mbp->msg_bufr == mbp->msg_bufx) {
262 		if (flag & IO_NDELAY) {
263 			error = EWOULDBLOCK;
264 			goto out;
265 		}
266 		if (logsoftc.sc_state & LOG_NBIO) {
267 			error = EWOULDBLOCK;
268 			goto out;
269 		}
270 		logsoftc.sc_state |= LOG_RDWAIT;
271 		bsd_log_unlock();
272 		/*
273 		 * If the wakeup is missed
274 		 * then wait for 5 sec and reevaluate
275 		 */
276 		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
277 		    "klog", 5 * hz)) != 0) {
278 			/* if it times out; ignore */
279 			if (error != EWOULDBLOCK) {
280 				return error;
281 			}
282 		}
283 		bsd_log_lock_safe();
284 	}
285 	logsoftc.sc_state &= ~LOG_RDWAIT;
286 
287 	while ((resid = uio_resid(uio)) > 0) {
288 		size_t l;
289 
290 		if (mbp->msg_bufx >= mbp->msg_bufr) {
291 			l = mbp->msg_bufx - mbp->msg_bufr;
292 		} else {
293 			l = mbp->msg_size - mbp->msg_bufr;
294 		}
295 		if ((l = MIN(l, (size_t)resid)) == 0) {
296 			break;
297 		}
298 
299 		const size_t readpos = mbp->msg_bufr;
300 
301 		bsd_log_unlock();
302 		error = uiomove((caddr_t)&mbp->msg_bufc[readpos], (int)l, uio);
303 		bsd_log_lock_safe();
304 		if (error) {
305 			break;
306 		}
307 
308 		mbp->msg_bufr = (int)(readpos + l);
309 		if (mbp->msg_bufr >= mbp->msg_size) {
310 			mbp->msg_bufr = 0;
311 		}
312 	}
313 out:
314 	bsd_log_unlock();
315 	return error;
316 }
317 
318 /*ARGSUSED*/
319 int
logselect(__unused dev_t dev,int rw,void * wql,struct proc * p)320 logselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
321 {
322 	const struct msgbuf *mbp = logsoftc.sc_mbp;
323 
324 	switch (rw) {
325 	case FREAD:
326 		bsd_log_lock_safe();
327 		if (mbp->msg_bufr != mbp->msg_bufx) {
328 			bsd_log_unlock();
329 			return 1;
330 		}
331 		selrecord(p, &logsoftc.sc_selp, wql);
332 		bsd_log_unlock();
333 		break;
334 	}
335 	return 0;
336 }
337 
338 int
oslogselect(__unused dev_t dev,int rw,void * wql,struct proc * p)339 oslogselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
340 {
341 	switch (rw) {
342 	case FREAD:
343 		bsd_log_lock_safe();
344 		if (os_log_wakeup) {
345 			bsd_log_unlock();
346 			return 1;
347 		}
348 		selrecord(p, &oslogsoftc.sc_selp, wql);
349 		bsd_log_unlock();
350 		break;
351 	}
352 	return 0;
353 }
354 
355 void
logwakeup(struct msgbuf * mbp)356 logwakeup(struct msgbuf *mbp)
357 {
358 	/* cf. r24974766 & r25201228*/
359 	if (oslog_is_safe() == FALSE) {
360 		return;
361 	}
362 
363 	bsd_log_lock_safe();
364 	if (!log_open) {
365 		bsd_log_unlock();
366 		return;
367 	}
368 	if (NULL == mbp) {
369 		mbp = logsoftc.sc_mbp;
370 	}
371 	if (mbp != logsoftc.sc_mbp) {
372 		goto out;
373 	}
374 	selwakeup(&logsoftc.sc_selp);
375 	if (logsoftc.sc_state & LOG_ASYNC) {
376 		int pgid = logsoftc.sc_pgid;
377 		bsd_log_unlock();
378 		if (pgid < 0) {
379 			gsignal(-pgid, SIGIO);
380 		} else {
381 			proc_signal(pgid, SIGIO);
382 		}
383 		bsd_log_lock_safe();
384 	}
385 	if (logsoftc.sc_state & LOG_RDWAIT) {
386 		wakeup((caddr_t)mbp);
387 		logsoftc.sc_state &= ~LOG_RDWAIT;
388 	}
389 out:
390 	bsd_log_unlock();
391 }
392 
393 void
oslogwakeup(void)394 oslogwakeup(void)
395 {
396 	if (!oslog_is_safe()) {
397 		return;
398 	}
399 
400 	bsd_log_lock_safe();
401 	if (!oslog_open) {
402 		bsd_log_unlock();
403 		return;
404 	}
405 	selwakeup(&oslogsoftc.sc_selp);
406 	os_log_wakeup = true;
407 	bsd_log_unlock();
408 }
409 
410 /*ARGSUSED*/
411 int
logioctl(__unused dev_t dev,u_long com,caddr_t data,__unused int flag,__unused struct proc * p)412 logioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
413 {
414 	int l;
415 	const struct msgbuf *mbp = logsoftc.sc_mbp;
416 
417 	bsd_log_lock_safe();
418 	switch (com) {
419 	/* return number of characters immediately available */
420 	case FIONREAD:
421 		l = mbp->msg_bufx - mbp->msg_bufr;
422 		if (l < 0) {
423 			l += mbp->msg_size;
424 		}
425 		*(off_t *)data = l;
426 		break;
427 
428 	case FIONBIO:
429 		if (*(int *)data) {
430 			logsoftc.sc_state |= LOG_NBIO;
431 		} else {
432 			logsoftc.sc_state &= ~LOG_NBIO;
433 		}
434 		break;
435 
436 	case FIOASYNC:
437 		if (*(int *)data) {
438 			logsoftc.sc_state |= LOG_ASYNC;
439 		} else {
440 			logsoftc.sc_state &= ~LOG_ASYNC;
441 		}
442 		break;
443 
444 	case TIOCSPGRP:
445 		logsoftc.sc_pgid = *(int *)data;
446 		break;
447 
448 	case TIOCGPGRP:
449 		*(int *)data = logsoftc.sc_pgid;
450 		break;
451 
452 	default:
453 		bsd_log_unlock();
454 		return -1;
455 	}
456 	bsd_log_unlock();
457 	return 0;
458 }
459 
460 /*ARGSUSED*/
461 int
oslogioctl(__unused dev_t dev,u_long com,caddr_t data,__unused int flag,__unused struct proc * p)462 oslogioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
463 {
464 	int ret = 0;
465 	mach_vm_size_t buffer_size = (__firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE);
466 	firehose_buffer_map_info_t map_info = {0, 0};
467 	firehose_buffer_t kernel_firehose_buffer = NULL;
468 	mach_vm_address_t user_addr = 0;
469 	mach_port_t mem_entry_ptr = MACH_PORT_NULL;
470 	bool has_more;
471 
472 	switch (com) {
473 	/* return number of characters immediately available */
474 
475 	case LOGBUFFERMAP:
476 		kernel_firehose_buffer = (firehose_buffer_t)kernel_firehose_addr;
477 
478 		ret = mach_make_memory_entry_64(kernel_map,
479 		    &buffer_size,
480 		    (mach_vm_offset_t) kernel_firehose_buffer,
481 		    (MAP_MEM_VM_SHARE | VM_PROT_READ),
482 		    &mem_entry_ptr,
483 		    MACH_PORT_NULL);
484 		if (ret == KERN_SUCCESS) {
485 			ret = mach_vm_map_kernel(get_task_map(current_task()),
486 			    &user_addr,
487 			    buffer_size,
488 			    0,               /*  mask */
489 			    VM_MAP_KERNEL_FLAGS_ANYWHERE(),
490 			    mem_entry_ptr,
491 			    0,               /* offset */
492 			    FALSE,               /* copy */
493 			    VM_PROT_READ,
494 			    VM_PROT_READ,
495 			    VM_INHERIT_SHARE);
496 		}
497 
498 		if (ret == KERN_SUCCESS) {
499 			map_info.fbmi_addr = (uint64_t) (user_addr);
500 			map_info.fbmi_size = buffer_size;
501 			bcopy(&map_info, data, sizeof(firehose_buffer_map_info_t));
502 		}
503 		break;
504 	case LOGFLUSHED:
505 		has_more = __firehose_merge_updates(*(firehose_push_reply_t *)(data));
506 		bsd_log_lock_safe();
507 		os_log_wakeup = has_more;
508 		if (os_log_wakeup) {
509 			selwakeup(&oslogsoftc.sc_selp);
510 		}
511 		bsd_log_unlock();
512 		break;
513 	default:
514 		return -1;
515 	}
516 	return 0;
517 }
518 
519 __startup_func
520 static void
oslog_init_firehose(void)521 oslog_init_firehose(void)
522 {
523 	if (os_log_disabled()) {
524 		printf("Firehose disabled: Logging disabled by ATM\n");
525 		return;
526 	}
527 
528 	if (!PE_parse_boot_argn("firehose_chunk_count", &__firehose_buffer_kernel_chunk_count, sizeof(__firehose_buffer_kernel_chunk_count))) {
529 		__firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
530 	}
531 	if (!PE_parse_boot_argn("firehose_io_pages", &__firehose_num_kernel_io_pages, sizeof(__firehose_num_kernel_io_pages))) {
532 		__firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
533 	}
534 	if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages)) {
535 		printf("illegal firehose configuration %u/%u, using defaults\n", __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
536 		__firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
537 		__firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
538 	}
539 	vm_size_t size = __firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE;
540 
541 	kmem_alloc(kernel_map, &kernel_firehose_addr, size + ptoa(2),
542 	    KMA_NOFAIL | KMA_PERMANENT | KMA_GUARD_FIRST | KMA_GUARD_LAST |
543 	    KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_LOG);
544 
545 	kernel_firehose_addr += PAGE_SIZE;
546 	/* register buffer with firehose */
547 	kernel_firehose_addr = (vm_offset_t)__firehose_buffer_create((size_t *) &size);
548 
549 	printf("Firehose configured: %u chunks, %u io pages\n",
550 	    __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
551 }
552 STARTUP(OSLOG, STARTUP_RANK_SECOND, oslog_init_firehose);
553 
554 /*
555  * log_putc_locked
556  *
557  * Decription:	Output a character to the log; assumes the bsd_log_lock() or
558  *              bsd_log_lock_safe() is held by the caller.
559  *
560  * Parameters:	c				Character to output
561  *
562  * Returns:	(void)
563  *
564  * Notes:	This functions is used for multibyte output to the log; it
565  *		should be used preferrentially where possible to ensure that
566  *		log entries do not end up interspersed due to preemption or
567  *		SMP reentrancy.
568  */
569 void
log_putc_locked(struct msgbuf * mbp,char c)570 log_putc_locked(struct msgbuf *mbp, char c)
571 {
572 	mbp->msg_bufc[mbp->msg_bufx++] = c;
573 	if (mbp->msg_bufx >= mbp->msg_size) {
574 		mbp->msg_bufx = 0;
575 	}
576 }
577 
578 /*
579  * log_putc
580  *
581  * Decription:	Output a character to the log; assumes the bsd_log_lock() or
582  *              bsd_log_lock_safe() is NOT held by the caller.
583  *
584  * Parameters:	c				Character to output
585  *
586  * Returns:	(void)
587  *
588  * Notes:	This function is used for single byte output to the log.  It
589  *		primarily exists to maintain binary backward compatibility.
590  */
591 void
log_putc(char c)592 log_putc(char c)
593 {
594 	if (!bsd_log_lock(oslog_is_safe())) {
595 		os_atomic_inc(&oslog_msgbuf_dropped_charcount, relaxed);
596 		return;
597 	}
598 
599 	log_putc_locked(msgbufp, c);
600 	int unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr;
601 
602 	bsd_log_unlock();
603 
604 	if (unread_count < 0) {
605 		unread_count = 0 - unread_count;
606 	}
607 	if (c == '\n' || unread_count >= (msgbufp->msg_size / 2)) {
608 		logwakeup(msgbufp);
609 	}
610 }
611 
612 /*
613  * it is possible to increase the kernel log buffer size by adding
614  *   msgbuf=n
615  * to the kernel command line, and to read the current size using
616  *   sysctl kern.msgbuf
617  * If there is no parameter on the kernel command line, the buffer is
618  * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
619  * memory is dynamically allocated. Memory management must already be up.
620  */
621 static int
log_setsize(size_t size)622 log_setsize(size_t size)
623 {
624 	int i, count;
625 	char *p;
626 
627 	if (size == 0 || size > MAX_MSG_BSIZE) {
628 		return EINVAL;
629 	}
630 
631 	int new_logsize = (int)size;
632 	char *new_logdata = kalloc_data(size, Z_WAITOK | Z_ZERO);
633 	if (!new_logdata) {
634 		printf("Cannot resize system message buffer: Not enough memory\n");
635 		return ENOMEM;
636 	}
637 
638 	bsd_log_lock_safe();
639 
640 	char *old_logdata = msgbufp->msg_bufc;
641 	int old_logsize = msgbufp->msg_size;
642 	int old_bufr = msgbufp->msg_bufr;
643 	int old_bufx = msgbufp->msg_bufx;
644 
645 	/* start "new_logsize" bytes before the write pointer */
646 	if (new_logsize <= old_bufx) {
647 		count = new_logsize;
648 		p = old_logdata + old_bufx - count;
649 	} else {
650 		/*
651 		 * if new buffer is bigger, copy what we have and let the
652 		 * bzero above handle the difference
653 		 */
654 		count = MIN(new_logsize, old_logsize);
655 		p = old_logdata + old_logsize - (count - old_bufx);
656 	}
657 	for (i = 0; i < count; i++) {
658 		if (p >= old_logdata + old_logsize) {
659 			p = old_logdata;
660 		}
661 		new_logdata[i] = *p++;
662 	}
663 
664 	int new_bufx = i;
665 	if (new_bufx >= new_logsize) {
666 		new_bufx = 0;
667 	}
668 	msgbufp->msg_bufx = new_bufx;
669 
670 	int new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
671 	if (new_bufr < 0) {
672 		new_bufr += old_logsize;
673 	}
674 	new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
675 	if (new_bufr < 0) {
676 		new_bufr += new_logsize;
677 	}
678 	msgbufp->msg_bufr = new_bufr;
679 
680 	msgbufp->msg_size = new_logsize;
681 	msgbufp->msg_bufc = new_logdata;
682 
683 	bsd_log_unlock();
684 
685 	/*
686 	 * This memory is now dead - clear it so that it compresses better
687 	 * in case of suspend to disk etc.
688 	 */
689 	bzero(old_logdata, old_logsize);
690 	if (old_logdata != smsg_bufc) {
691 		/* dynamic memory that must be freed */
692 		kfree_data(old_logdata, old_logsize);
693 	}
694 
695 	printf("System message buffer configured: %lu bytes\n", size);
696 
697 	return 0;
698 }
699 
700 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
701     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
702     sysctl_kern_msgbuf, "I", "");
703 
704 static int
sysctl_kern_msgbuf(struct sysctl_oid * oidp __unused,void * arg1 __unused,int arg2 __unused,struct sysctl_req * req)705 sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
706     void *arg1 __unused, int arg2 __unused, struct sysctl_req *req)
707 {
708 	int old_bufsize, bufsize;
709 	int error;
710 
711 	bsd_log_lock_safe();
712 	old_bufsize = bufsize = msgbufp->msg_size;
713 	bsd_log_unlock();
714 
715 	error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
716 	if (error) {
717 		return error;
718 	}
719 
720 	if (bufsize < 0) {
721 		return EINVAL;
722 	}
723 
724 	if (bufsize != old_bufsize) {
725 		error = log_setsize(bufsize);
726 	}
727 
728 	return error;
729 }
730 
731 /*
732  * This should be called by /sbin/dmesg only via libproc.
733  * It returns as much data still in the buffer as possible.
734  */
735 int
log_dmesg(user_addr_t buffer,uint32_t buffersize,int32_t * retval)736 log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t *retval)
737 {
738 	uint32_t i;
739 	uint32_t localbuff_size;
740 	int error = 0, newl, skip;
741 	char *localbuff, *p, *copystart, ch;
742 	size_t copysize;
743 
744 	bsd_log_lock_safe();
745 	localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
746 	bsd_log_unlock();
747 
748 	/* Allocate a temporary non-circular buffer for copyout */
749 	localbuff = kalloc_data(localbuff_size, Z_WAITOK);
750 	if (!localbuff) {
751 		printf("log_dmesg: unable to allocate memory\n");
752 		return ENOMEM;
753 	}
754 
755 	/* in between here, the log could become bigger, but that's fine */
756 	bsd_log_lock_safe();
757 
758 	/*
759 	 * The message buffer is circular; start at the write pointer, and
760 	 * make one loop up to write pointer - 1.
761 	 */
762 	p = msgbufp->msg_bufc + msgbufp->msg_bufx;
763 	for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) {
764 		if (p >= msgbufp->msg_bufc + msgbufp->msg_size) {
765 			p = msgbufp->msg_bufc;
766 		}
767 		ch = *p;
768 		/* Skip "\n<.*>" syslog sequences. */
769 		if (skip) {
770 			if (ch == '>') {
771 				newl = skip = 0;
772 			}
773 			continue;
774 		}
775 		if (newl && ch == '<') {
776 			skip = 1;
777 			continue;
778 		}
779 		if (ch == '\0') {
780 			continue;
781 		}
782 		newl = (ch == '\n');
783 		localbuff[i++] = ch;
784 		/* The original version of this routine contained a buffer
785 		 * overflow. At the time, a "small" targeted fix was desired
786 		 * so the change below to check the buffer bounds was made.
787 		 * TODO: rewrite this needlessly convoluted routine.
788 		 */
789 		if (i == (localbuff_size - 2)) {
790 			break;
791 		}
792 	}
793 	if (!newl) {
794 		localbuff[i++] = '\n';
795 	}
796 	localbuff[i++] = 0;
797 
798 	if (buffersize >= i) {
799 		copystart = localbuff;
800 		copysize = i;
801 	} else {
802 		copystart = localbuff + i - buffersize;
803 		copysize = buffersize;
804 	}
805 
806 	bsd_log_unlock();
807 
808 	error = copyout(copystart, buffer, copysize);
809 	if (!error) {
810 		*retval = (int32_t)copysize;
811 	}
812 
813 	kfree_data(localbuff, localbuff_size);
814 	return error;
815 }
816 
817 #ifdef CONFIG_XNUPOST
818 
819 size_t find_pattern_in_buffer(const char *, size_t, size_t);
820 
821 /*
822  * returns count of pattern found in systemlog buffer.
823  * stops searching further if count reaches expected_count.
824  */
825 size_t
find_pattern_in_buffer(const char * pattern,size_t len,size_t expected_count)826 find_pattern_in_buffer(const char *pattern, size_t len, size_t expected_count)
827 {
828 	if (pattern == NULL || len == 0 || expected_count == 0) {
829 		return 0;
830 	}
831 
832 	size_t msg_bufx = msgbufp->msg_bufx;
833 	size_t msg_size = msgbufp->msg_size;
834 	size_t match_count = 0;
835 
836 	for (size_t i = 0; i < msg_size; i++) {
837 		boolean_t match = TRUE;
838 		for (size_t j = 0; j < len; j++) {
839 			size_t pos = (msg_bufx + i + j) % msg_size;
840 			if (msgbufp->msg_bufc[pos] != pattern[j]) {
841 				match = FALSE;
842 				break;
843 			}
844 		}
845 		if (match && ++match_count >= expected_count) {
846 			break;
847 		}
848 	}
849 
850 	return match_count;
851 }
852 
853 __startup_func
854 static void
oslog_init_msgbuf(void)855 oslog_init_msgbuf(void)
856 {
857 	size_t msgbuf_size = 0;
858 
859 	if (PE_parse_boot_argn("msgbuf", &msgbuf_size, sizeof(msgbuf_size))) {
860 		(void) log_setsize(msgbuf_size);
861 	}
862 }
863 STARTUP(OSLOG, STARTUP_RANK_SECOND, oslog_init_msgbuf);
864 
865 #endif
866