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_FLAGS_ANYWHERE,
490 VM_MAP_KERNEL_FLAGS_NONE,
491 VM_KERN_MEMORY_NONE,
492 mem_entry_ptr,
493 0, /* offset */
494 FALSE, /* copy */
495 VM_PROT_READ,
496 VM_PROT_READ,
497 VM_INHERIT_SHARE);
498 }
499
500 if (ret == KERN_SUCCESS) {
501 map_info.fbmi_addr = (uint64_t) (user_addr);
502 map_info.fbmi_size = buffer_size;
503 bcopy(&map_info, data, sizeof(firehose_buffer_map_info_t));
504 }
505 break;
506 case LOGFLUSHED:
507 has_more = __firehose_merge_updates(*(firehose_push_reply_t *)(data));
508 bsd_log_lock_safe();
509 os_log_wakeup = has_more;
510 if (os_log_wakeup) {
511 selwakeup(&oslogsoftc.sc_selp);
512 }
513 bsd_log_unlock();
514 break;
515 default:
516 return -1;
517 }
518 return 0;
519 }
520
521 __startup_func
522 static void
oslog_init_firehose(void)523 oslog_init_firehose(void)
524 {
525 if (os_log_disabled()) {
526 printf("Firehose disabled: Logging disabled by ATM\n");
527 return;
528 }
529
530 if (!PE_parse_boot_argn("firehose_chunk_count", &__firehose_buffer_kernel_chunk_count, sizeof(__firehose_buffer_kernel_chunk_count))) {
531 __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
532 }
533 if (!PE_parse_boot_argn("firehose_io_pages", &__firehose_num_kernel_io_pages, sizeof(__firehose_num_kernel_io_pages))) {
534 __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
535 }
536 if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages)) {
537 printf("illegal firehose configuration %u/%u, using defaults\n", __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
538 __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
539 __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
540 }
541 vm_size_t size = __firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE;
542
543 kmem_alloc(kernel_map, &kernel_firehose_addr, size + ptoa(2),
544 KMA_NOFAIL | KMA_PERMANENT | KMA_GUARD_FIRST | KMA_GUARD_LAST |
545 KMA_DATA | KMA_ZERO, VM_KERN_MEMORY_LOG);
546
547 kernel_firehose_addr += PAGE_SIZE;
548 /* register buffer with firehose */
549 kernel_firehose_addr = (vm_offset_t)__firehose_buffer_create((size_t *) &size);
550
551 printf("Firehose configured: %u chunks, %u io pages\n",
552 __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
553 }
554 STARTUP(OSLOG, STARTUP_RANK_SECOND, oslog_init_firehose);
555
556 /*
557 * log_putc_locked
558 *
559 * Decription: Output a character to the log; assumes the bsd_log_lock() or
560 * bsd_log_lock_safe() is held by the caller.
561 *
562 * Parameters: c Character to output
563 *
564 * Returns: (void)
565 *
566 * Notes: This functions is used for multibyte output to the log; it
567 * should be used preferrentially where possible to ensure that
568 * log entries do not end up interspersed due to preemption or
569 * SMP reentrancy.
570 */
571 void
log_putc_locked(struct msgbuf * mbp,char c)572 log_putc_locked(struct msgbuf *mbp, char c)
573 {
574 mbp->msg_bufc[mbp->msg_bufx++] = c;
575 if (mbp->msg_bufx >= mbp->msg_size) {
576 mbp->msg_bufx = 0;
577 }
578 }
579
580 /*
581 * log_putc
582 *
583 * Decription: Output a character to the log; assumes the bsd_log_lock() or
584 * bsd_log_lock_safe() is NOT held by the caller.
585 *
586 * Parameters: c Character to output
587 *
588 * Returns: (void)
589 *
590 * Notes: This function is used for single byte output to the log. It
591 * primarily exists to maintain binary backward compatibility.
592 */
593 void
log_putc(char c)594 log_putc(char c)
595 {
596 if (!bsd_log_lock(oslog_is_safe())) {
597 os_atomic_inc(&oslog_msgbuf_dropped_charcount, relaxed);
598 return;
599 }
600
601 log_putc_locked(msgbufp, c);
602 int unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr;
603
604 bsd_log_unlock();
605
606 if (unread_count < 0) {
607 unread_count = 0 - unread_count;
608 }
609 if (c == '\n' || unread_count >= (msgbufp->msg_size / 2)) {
610 logwakeup(msgbufp);
611 }
612 }
613
614 /*
615 * it is possible to increase the kernel log buffer size by adding
616 * msgbuf=n
617 * to the kernel command line, and to read the current size using
618 * sysctl kern.msgbuf
619 * If there is no parameter on the kernel command line, the buffer is
620 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
621 * memory is dynamically allocated. Memory management must already be up.
622 */
623 static int
log_setsize(size_t size)624 log_setsize(size_t size)
625 {
626 int i, count;
627 char *p;
628
629 if (size == 0 || size > MAX_MSG_BSIZE) {
630 return EINVAL;
631 }
632
633 int new_logsize = (int)size;
634 char *new_logdata = kalloc_data(size, Z_WAITOK | Z_ZERO);
635 if (!new_logdata) {
636 printf("Cannot resize system message buffer: Not enough memory\n");
637 return ENOMEM;
638 }
639
640 bsd_log_lock_safe();
641
642 char *old_logdata = msgbufp->msg_bufc;
643 int old_logsize = msgbufp->msg_size;
644 int old_bufr = msgbufp->msg_bufr;
645 int old_bufx = msgbufp->msg_bufx;
646
647 /* start "new_logsize" bytes before the write pointer */
648 if (new_logsize <= old_bufx) {
649 count = new_logsize;
650 p = old_logdata + old_bufx - count;
651 } else {
652 /*
653 * if new buffer is bigger, copy what we have and let the
654 * bzero above handle the difference
655 */
656 count = MIN(new_logsize, old_logsize);
657 p = old_logdata + old_logsize - (count - old_bufx);
658 }
659 for (i = 0; i < count; i++) {
660 if (p >= old_logdata + old_logsize) {
661 p = old_logdata;
662 }
663 new_logdata[i] = *p++;
664 }
665
666 int new_bufx = i;
667 if (new_bufx >= new_logsize) {
668 new_bufx = 0;
669 }
670 msgbufp->msg_bufx = new_bufx;
671
672 int new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
673 if (new_bufr < 0) {
674 new_bufr += old_logsize;
675 }
676 new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
677 if (new_bufr < 0) {
678 new_bufr += new_logsize;
679 }
680 msgbufp->msg_bufr = new_bufr;
681
682 msgbufp->msg_size = new_logsize;
683 msgbufp->msg_bufc = new_logdata;
684
685 bsd_log_unlock();
686
687 /*
688 * This memory is now dead - clear it so that it compresses better
689 * in case of suspend to disk etc.
690 */
691 bzero(old_logdata, old_logsize);
692 if (old_logdata != smsg_bufc) {
693 /* dynamic memory that must be freed */
694 kfree_data(old_logdata, old_logsize);
695 }
696
697 printf("System message buffer configured: %lu bytes\n", size);
698
699 return 0;
700 }
701
702 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
703 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
704 sysctl_kern_msgbuf, "I", "");
705
706 static int
sysctl_kern_msgbuf(struct sysctl_oid * oidp __unused,void * arg1 __unused,int arg2 __unused,struct sysctl_req * req)707 sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
708 void *arg1 __unused, int arg2 __unused, struct sysctl_req *req)
709 {
710 int old_bufsize, bufsize;
711 int error;
712
713 bsd_log_lock_safe();
714 old_bufsize = bufsize = msgbufp->msg_size;
715 bsd_log_unlock();
716
717 error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
718 if (error) {
719 return error;
720 }
721
722 if (bufsize < 0) {
723 return EINVAL;
724 }
725
726 if (bufsize != old_bufsize) {
727 error = log_setsize(bufsize);
728 }
729
730 return error;
731 }
732
733 /*
734 * This should be called by /sbin/dmesg only via libproc.
735 * It returns as much data still in the buffer as possible.
736 */
737 int
log_dmesg(user_addr_t buffer,uint32_t buffersize,int32_t * retval)738 log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t *retval)
739 {
740 uint32_t i;
741 uint32_t localbuff_size;
742 int error = 0, newl, skip;
743 char *localbuff, *p, *copystart, ch;
744 size_t copysize;
745
746 bsd_log_lock_safe();
747 localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
748 bsd_log_unlock();
749
750 /* Allocate a temporary non-circular buffer for copyout */
751 localbuff = kalloc_data(localbuff_size, Z_WAITOK);
752 if (!localbuff) {
753 printf("log_dmesg: unable to allocate memory\n");
754 return ENOMEM;
755 }
756
757 /* in between here, the log could become bigger, but that's fine */
758 bsd_log_lock_safe();
759
760 /*
761 * The message buffer is circular; start at the write pointer, and
762 * make one loop up to write pointer - 1.
763 */
764 p = msgbufp->msg_bufc + msgbufp->msg_bufx;
765 for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) {
766 if (p >= msgbufp->msg_bufc + msgbufp->msg_size) {
767 p = msgbufp->msg_bufc;
768 }
769 ch = *p;
770 /* Skip "\n<.*>" syslog sequences. */
771 if (skip) {
772 if (ch == '>') {
773 newl = skip = 0;
774 }
775 continue;
776 }
777 if (newl && ch == '<') {
778 skip = 1;
779 continue;
780 }
781 if (ch == '\0') {
782 continue;
783 }
784 newl = (ch == '\n');
785 localbuff[i++] = ch;
786 /* The original version of this routine contained a buffer
787 * overflow. At the time, a "small" targeted fix was desired
788 * so the change below to check the buffer bounds was made.
789 * TODO: rewrite this needlessly convoluted routine.
790 */
791 if (i == (localbuff_size - 2)) {
792 break;
793 }
794 }
795 if (!newl) {
796 localbuff[i++] = '\n';
797 }
798 localbuff[i++] = 0;
799
800 if (buffersize >= i) {
801 copystart = localbuff;
802 copysize = i;
803 } else {
804 copystart = localbuff + i - buffersize;
805 copysize = buffersize;
806 }
807
808 bsd_log_unlock();
809
810 error = copyout(copystart, buffer, copysize);
811 if (!error) {
812 *retval = (int32_t)copysize;
813 }
814
815 kfree_data(localbuff, localbuff_size);
816 return error;
817 }
818
819 #ifdef CONFIG_XNUPOST
820
821 size_t find_pattern_in_buffer(const char *, size_t, size_t);
822
823 /*
824 * returns count of pattern found in systemlog buffer.
825 * stops searching further if count reaches expected_count.
826 */
827 size_t
find_pattern_in_buffer(const char * pattern,size_t len,size_t expected_count)828 find_pattern_in_buffer(const char *pattern, size_t len, size_t expected_count)
829 {
830 if (pattern == NULL || len == 0 || expected_count == 0) {
831 return 0;
832 }
833
834 size_t msg_bufx = msgbufp->msg_bufx;
835 size_t msg_size = msgbufp->msg_size;
836 size_t match_count = 0;
837
838 for (size_t i = 0; i < msg_size; i++) {
839 boolean_t match = TRUE;
840 for (size_t j = 0; j < len; j++) {
841 size_t pos = (msg_bufx + i + j) % msg_size;
842 if (msgbufp->msg_bufc[pos] != pattern[j]) {
843 match = FALSE;
844 break;
845 }
846 }
847 if (match && ++match_count >= expected_count) {
848 break;
849 }
850 }
851
852 return match_count;
853 }
854
855 __startup_func
856 static void
oslog_init_msgbuf(void)857 oslog_init_msgbuf(void)
858 {
859 size_t msgbuf_size = 0;
860
861 if (PE_parse_boot_argn("msgbuf", &msgbuf_size, sizeof(msgbuf_size))) {
862 (void) log_setsize(msgbuf_size);
863 }
864 }
865 STARTUP(OSLOG, STARTUP_RANK_SECOND, oslog_init_msgbuf);
866
867 #endif
868