2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
65 * Error log buffer for kernel printf's.
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>
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>
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>
90 #include <sys/signalvar.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>
103 /* XXX should be in a common header somewhere */
104 extern void logwakeup(struct msgbuf
*);
105 extern void oslogwakeup(void);
106 extern void oslog_streamwakeup(void);
107 static void oslog_streamwakeup_locked(void);
108 vm_offset_t kernel_firehose_addr
= 0;
110 /* log message counters for streaming mode */
111 uint32_t oslog_s_streamed_msgcount
= 0;
112 uint32_t oslog_s_dropped_msgcount
= 0;
113 extern uint32_t oslog_s_error_count
;
115 #define LOG_RDPRI (PZERO + 1)
117 #define LOG_NBIO 0x02
118 #define LOG_ASYNC 0x04
119 #define LOG_RDWAIT 0x08
121 /* All globals should be accessed under LOG_LOCK() */
123 static char amsg_bufc
[1024];
124 static struct msgbuf aslbuf
= {.msg_magic
= MSG_MAGIC
, .msg_size
= sizeof(amsg_bufc
), .msg_bufx
= 0, .msg_bufr
= 0, .msg_bufc
= amsg_bufc
};
125 struct msgbuf
*aslbufp
__attribute__((used
)) = &aslbuf
;
127 /* logsoftc only valid while log_open=1 */
129 int sc_state
; /* see above for possibilities */
130 struct selinfo sc_selp
; /* thread waiting for select */
131 int sc_pgid
; /* process/group for async I/O */
132 struct msgbuf
*sc_mbp
;
136 char smsg_bufc
[CONFIG_MSG_BSIZE
]; /* static buffer */
137 char oslog_stream_bufc
[FIREHOSE_CHUNK_SIZE
]; /* static buffer */
138 struct firehose_chunk_s oslog_boot_buf
= {
140 .fcp_next_entry_offs
= offsetof(struct firehose_chunk_s
, fc_data
),
141 .fcp_private_offs
= FIREHOSE_CHUNK_SIZE
,
142 .fcp_refcnt
= 1, // indicate that there is a writer to this chunk
143 .fcp_stream
= firehose_stream_persist
,
144 .fcp_flag_io
= 1, // for now, lets assume this is coming from the io bank
146 }; /* static buffer */
147 firehose_chunk_t firehose_boot_chunk
= &oslog_boot_buf
;
148 struct msgbuf msgbuf
= {.msg_magic
= MSG_MAGIC
, .msg_size
= sizeof(smsg_bufc
), .msg_bufx
= 0, .msg_bufr
= 0, .msg_bufc
= smsg_bufc
};
149 struct msgbuf oslog_stream_buf
= {.msg_magic
= MSG_MAGIC
, .msg_size
= 0, .msg_bufx
= 0, .msg_bufr
= 0, .msg_bufc
= NULL
};
150 struct msgbuf
*msgbufp
__attribute__((used
)) = &msgbuf
;
151 struct msgbuf
*oslog_streambufp
__attribute__((used
)) = &oslog_stream_buf
;
153 // List entries for keeping track of the streaming buffer
154 static oslog_stream_buf_entry_t oslog_stream_buf_entries
;
156 #define OSLOG_NUM_STREAM_ENTRIES 64
157 #define OSLOG_STREAM_BUF_SIZE 4096
160 int os_log_wakeup
= 0;
161 int oslog_stream_open
= 0;
162 int oslog_stream_buf_bytesavail
= 0;
163 int oslog_stream_buf_size
= OSLOG_STREAM_BUF_SIZE
;
164 int oslog_stream_num_entries
= OSLOG_NUM_STREAM_ENTRIES
;
166 uint8_t __firehose_buffer_kernel_chunk_count
= FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT
;
167 uint8_t __firehose_num_kernel_io_pages
= FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES
;
169 /* oslogsoftc only valid while oslog_open=1 */
171 int sc_state
; /* see above for possibilities */
172 struct selinfo sc_selp
; /* thread waiting for select */
173 int sc_pgid
; /* process/group for async I/O */
176 struct oslog_streamsoftc
{
177 int sc_state
; /* see above for possibilities */
178 struct selinfo sc_selp
; /* thread waiting for select */
179 int sc_pgid
; /* process/group for async I/O */
182 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_free_head
=
183 STAILQ_HEAD_INITIALIZER(oslog_stream_free_head
);
184 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_buf_head
=
185 STAILQ_HEAD_INITIALIZER(oslog_stream_buf_head
);
187 /* defined in osfmk/kern/printf.c */
188 extern void oslog_lock_init(void);
189 extern void bsd_log_lock(void);
190 extern void bsd_log_unlock(void);
192 /* defined for osfmk/kern/printf.c */
193 void bsd_log_init(void);
196 * Ideally this file would define this lock, but bsd doesn't have the definition
199 decl_lck_spin_data(extern, oslog_stream_lock
);
200 #define stream_lock() lck_spin_lock(&oslog_stream_lock)
201 #define stream_unlock() lck_spin_unlock(&oslog_stream_lock)
203 /* XXX wants a linker set so these can be static */
204 extern d_open_t logopen
;
205 extern d_close_t logclose
;
206 extern d_read_t logread
;
207 extern d_ioctl_t logioctl
;
208 extern d_select_t logselect
;
210 /* XXX wants a linker set so these can be static */
211 extern d_open_t oslogopen
;
212 extern d_close_t oslogclose
;
213 extern d_select_t oslogselect
;
214 extern d_ioctl_t oslogioctl
;
216 /* XXX wants a linker set so these can be static */
217 extern d_open_t oslog_streamopen
;
218 extern d_close_t oslog_streamclose
;
219 extern d_read_t oslog_streamread
;
220 extern d_ioctl_t oslog_streamioctl
;
221 extern d_select_t oslog_streamselect
;
223 void oslog_init(void);
224 void oslog_setsize(int size
);
225 void oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
226 uint64_t stamp
, const void *pubdata
, size_t publen
);
227 void oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
);
228 static oslog_stream_buf_entry_t
oslog_stream_find_free_buf_entry_locked(void);
229 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
);
232 * Serialize log access. Note that the log can be written at interrupt level,
233 * so any log manipulations that can be done from, or affect, another processor
234 * at interrupt level must be guarded with a spin lock.
237 #define LOG_LOCK() bsd_log_lock()
238 #define LOG_UNLOCK() bsd_log_unlock()
241 #define LOG_SETSIZE_DEBUG(x...) kprintf(x)
243 #define LOG_SETSIZE_DEBUG(x...) do { } while(0)
246 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp
,
247 void *arg1
, int arg2
, struct sysctl_req
*req
);
251 logopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
258 if (atm_get_diagnostic_config() & ATM_ENABLE_LEGACY_LOGGING
) {
259 logsoftc
.sc_mbp
= msgbufp
;
262 * Support for messagetracer (kern_asl_msg())
263 * In this mode, /dev/klog exports only ASL-formatted messages
264 * written into aslbufp via vaddlog().
266 logsoftc
.sc_mbp
= aslbufp
;
268 logsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
278 logclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
281 logsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
282 selwakeup(&logsoftc
.sc_selp
);
283 selthreadclear(&logsoftc
.sc_selp
);
291 oslogopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
298 oslogsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
306 oslogclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
309 oslogsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
310 selwakeup(&oslogsoftc
.sc_selp
);
311 selthreadclear(&oslogsoftc
.sc_selp
);
318 oslog_streamopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
320 char *oslog_stream_msg_bufc
= NULL
;
321 oslog_stream_buf_entry_t entries
= NULL
;
324 if (oslog_stream_open
) {
330 // Allocate the stream buffer
331 oslog_stream_msg_bufc
= kalloc(oslog_stream_buf_size
);
332 if (!oslog_stream_msg_bufc
) {
336 /* entries to support kernel logging in stream mode */
337 entries
= kalloc(oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
339 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
344 if (oslog_stream_open
) {
346 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
347 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
351 assert(oslog_streambufp
->msg_bufc
== NULL
);
352 oslog_streambufp
->msg_bufc
= oslog_stream_msg_bufc
;
353 oslog_streambufp
->msg_size
= oslog_stream_buf_size
;
355 oslog_stream_buf_entries
= entries
;
357 STAILQ_INIT(&oslog_stream_free_head
);
358 STAILQ_INIT(&oslog_stream_buf_head
);
360 for (int i
= 0; i
< oslog_stream_num_entries
; i
++) {
361 oslog_stream_buf_entries
[i
].type
= oslog_stream_link_type_log
;
362 oslog_stream_buf_entries
[i
].offset
= 0;
363 oslog_stream_buf_entries
[i
].size
= 0;
364 oslog_stream_buf_entries
[i
].timestamp
= 0;
365 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, &oslog_stream_buf_entries
[i
], buf_entries
);
368 /* there should be no pending entries in the stream */
369 assert(STAILQ_EMPTY(&oslog_stream_buf_head
));
370 assert(oslog_streambufp
->msg_bufx
== 0);
371 assert(oslog_streambufp
->msg_bufr
== 0);
373 oslog_streambufp
->msg_bufx
= 0;
374 oslog_streambufp
->msg_bufr
= 0;
375 oslog_streamsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
376 oslog_stream_open
= 1;
377 oslog_stream_buf_bytesavail
= oslog_stream_buf_size
;
384 oslog_streamclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
386 oslog_stream_buf_entry_t next_entry
= NULL
;
387 char *oslog_stream_msg_bufc
= NULL
;
388 oslog_stream_buf_entry_t entries
= NULL
;
392 if (oslog_stream_open
== 0) {
397 // Consume all log lines
398 while (!STAILQ_EMPTY(&oslog_stream_buf_head
)) {
399 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
400 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
402 oslog_streamwakeup_locked();
403 oslog_streamsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
404 selwakeup(&oslog_streamsoftc
.sc_selp
);
405 selthreadclear(&oslog_streamsoftc
.sc_selp
);
406 oslog_stream_open
= 0;
407 oslog_streambufp
->msg_bufr
= 0;
408 oslog_streambufp
->msg_bufx
= 0;
409 oslog_stream_msg_bufc
= oslog_streambufp
->msg_bufc
;
410 oslog_streambufp
->msg_bufc
= NULL
;
411 entries
= oslog_stream_buf_entries
;
412 oslog_stream_buf_entries
= NULL
;
413 oslog_streambufp
->msg_size
= 0;
417 // Free the stream buffer
418 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
419 // Free the list entries
420 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
427 logread(__unused dev_t dev
, struct uio
*uio
, int flag
)
431 struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
434 while (mbp
->msg_bufr
== mbp
->msg_bufx
) {
435 if (flag
& IO_NDELAY
) {
439 if (logsoftc
.sc_state
& LOG_NBIO
) {
443 logsoftc
.sc_state
|= LOG_RDWAIT
;
446 * If the wakeup is missed
447 * then wait for 5 sec and reevaluate
449 if ((error
= tsleep((caddr_t
)mbp
, LOG_RDPRI
| PCATCH
,
450 "klog", 5 * hz
)) != 0) {
451 /* if it times out; ignore */
452 if (error
!= EWOULDBLOCK
) {
458 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
460 while (uio_resid(uio
) > 0) {
463 l
= mbp
->msg_bufx
- mbp
->msg_bufr
;
465 l
= mbp
->msg_size
- mbp
->msg_bufr
;
467 l
= min(l
, uio_resid(uio
));
472 readpos
= mbp
->msg_bufr
;
474 error
= uiomove((caddr_t
)&mbp
->msg_bufc
[readpos
], l
, uio
);
479 mbp
->msg_bufr
= readpos
+ l
;
480 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
491 oslog_streamread(__unused dev_t dev
, struct uio
*uio
, int flag
)
495 static char logline
[FIREHOSE_CHUNK_SIZE
];
499 if (!oslog_stream_open
) {
504 while (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
505 assert(oslog_stream_buf_bytesavail
== oslog_stream_buf_size
);
507 if (flag
& IO_NDELAY
|| oslog_streamsoftc
.sc_state
& LOG_NBIO
) {
512 oslog_streamsoftc
.sc_state
|= LOG_RDWAIT
;
513 wait_result_t wr
= assert_wait((event_t
)oslog_streambufp
,
514 THREAD_INTERRUPTIBLE
);
515 if (wr
== THREAD_WAITING
) {
517 wr
= thread_block(THREAD_CONTINUE_NULL
);
522 case THREAD_AWAKENED
:
523 case THREAD_TIMED_OUT
:
531 if (!oslog_stream_open
) {
537 oslog_stream_buf_entry_t read_entry
= NULL
;
540 read_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
541 assert(read_entry
!= NULL
);
542 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
544 // Copy the timestamp first
545 memcpy(logline
+ logpos
, &read_entry
->timestamp
, sizeof(uint64_t));
546 logpos
+= sizeof(uint64_t);
548 switch (read_entry
->type
) {
549 /* Handle metadata messages */
550 case oslog_stream_link_type_metadata
:
552 memcpy(logline
+ logpos
,
553 (read_entry
->metadata
), read_entry
->size
);
554 logpos
+= read_entry
->size
;
558 // Free the list entry
559 kfree(read_entry
, (sizeof(struct oslog_stream_buf_entry_s
) + read_entry
->size
));
562 /* Handle log messages */
563 case oslog_stream_link_type_log
:
565 /* ensure that the correct read entry was dequeued */
566 assert(read_entry
->offset
== oslog_streambufp
->msg_bufr
);
567 rec_length
= read_entry
->size
;
569 // If the next log line is contiguous in the buffer, copy it out.
570 if (read_entry
->offset
+ rec_length
<= oslog_streambufp
->msg_size
) {
571 memcpy(logline
+ logpos
,
572 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, rec_length
);
574 oslog_streambufp
->msg_bufr
+= rec_length
;
575 if (oslog_streambufp
->msg_bufr
== oslog_streambufp
->msg_size
) {
576 oslog_streambufp
->msg_bufr
= 0;
578 logpos
+= rec_length
;
580 // Otherwise, copy until the end of the buffer, and
581 // copy the remaining bytes starting at index 0.
582 int bytes_left
= oslog_streambufp
->msg_size
- read_entry
->offset
;
583 memcpy(logline
+ logpos
,
584 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, bytes_left
);
585 logpos
+= bytes_left
;
586 rec_length
-= bytes_left
;
588 memcpy(logline
+ logpos
, (const void *)oslog_streambufp
->msg_bufc
,
590 oslog_streambufp
->msg_bufr
= rec_length
;
591 logpos
+= rec_length
;
594 oslog_stream_buf_bytesavail
+= read_entry
->size
;
595 assert(oslog_stream_buf_bytesavail
<= oslog_stream_buf_size
);
597 assert(oslog_streambufp
->msg_bufr
< oslog_streambufp
->msg_size
);
598 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, read_entry
, buf_entries
);
605 panic("Got unexpected log entry type: %hhu\n", read_entry
->type
);
609 copy_size
= min(logpos
, uio_resid(uio
));
610 if (copy_size
!= 0) {
611 error
= uiomove((caddr_t
)logline
, copy_size
, uio
);
613 os_atomic_inc(&oslog_s_streamed_msgcount
, relaxed
);
620 logselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
622 const struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
627 if (mbp
->msg_bufr
!= mbp
->msg_bufx
) {
631 selrecord(p
, &logsoftc
.sc_selp
, wql
);
639 oslogselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
648 selrecord(p
, &oslogsoftc
.sc_selp
, wql
);
656 oslog_streamselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
664 if (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
665 selrecord(p
, &oslog_streamsoftc
.sc_selp
, wql
);
677 logwakeup(struct msgbuf
*mbp
)
679 /* cf. r24974766 & r25201228*/
680 if (oslog_is_safe() == FALSE
) {
690 mbp
= logsoftc
.sc_mbp
;
692 if (mbp
!= logsoftc
.sc_mbp
) {
695 selwakeup(&logsoftc
.sc_selp
);
696 if (logsoftc
.sc_state
& LOG_ASYNC
) {
697 int pgid
= logsoftc
.sc_pgid
;
700 gsignal(-pgid
, SIGIO
);
702 proc_signal(pgid
, SIGIO
);
706 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
707 wakeup((caddr_t
)mbp
);
708 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
722 selwakeup(&oslogsoftc
.sc_selp
);
728 oslog_streamwakeup_locked(void)
730 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
731 if (!oslog_stream_open
) {
734 selwakeup(&oslog_streamsoftc
.sc_selp
);
735 if (oslog_streamsoftc
.sc_state
& LOG_RDWAIT
) {
736 wakeup((caddr_t
)oslog_streambufp
);
737 oslog_streamsoftc
.sc_state
&= ~LOG_RDWAIT
;
742 oslog_streamwakeup(void)
744 /* cf. r24974766 & r25201228*/
745 if (oslog_is_safe() == FALSE
) {
750 oslog_streamwakeup_locked();
756 logioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
759 const struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
763 /* return number of characters immediately available */
765 l
= mbp
->msg_bufx
- mbp
->msg_bufr
;
774 logsoftc
.sc_state
|= LOG_NBIO
;
776 logsoftc
.sc_state
&= ~LOG_NBIO
;
782 logsoftc
.sc_state
|= LOG_ASYNC
;
784 logsoftc
.sc_state
&= ~LOG_ASYNC
;
789 logsoftc
.sc_pgid
= *(int *)data
;
793 *(int *)data
= logsoftc
.sc_pgid
;
806 oslogioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
809 mach_vm_size_t buffer_size
= (__firehose_buffer_kernel_chunk_count
* FIREHOSE_CHUNK_SIZE
);
810 firehose_buffer_map_info_t map_info
= {0, 0};
811 firehose_buffer_t kernel_firehose_buffer
= NULL
;
812 mach_vm_address_t user_addr
= 0;
813 mach_port_t mem_entry_ptr
= MACH_PORT_NULL
;
816 /* return number of characters immediately available */
819 kernel_firehose_buffer
= (firehose_buffer_t
)kernel_firehose_addr
;
821 ret
= mach_make_memory_entry_64(kernel_map
,
823 (mach_vm_offset_t
) kernel_firehose_buffer
,
824 (MAP_MEM_VM_SHARE
| VM_PROT_READ
),
827 if (ret
== KERN_SUCCESS
) {
828 ret
= mach_vm_map_kernel(get_task_map(current_task()),
833 VM_MAP_KERNEL_FLAGS_NONE
,
843 if (ret
== KERN_SUCCESS
) {
844 map_info
.fbmi_addr
= (uint64_t) (user_addr
);
845 map_info
.fbmi_size
= buffer_size
;
846 bcopy(&map_info
, data
, sizeof(firehose_buffer_map_info_t
));
853 __firehose_merge_updates(*(firehose_push_reply_t
*)(data
));
863 oslog_streamioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
871 if (data
&& *(int *)data
) {
872 oslog_streamsoftc
.sc_state
|= LOG_NBIO
;
874 oslog_streamsoftc
.sc_state
&= ~LOG_NBIO
;
878 if (data
&& *(int *)data
) {
879 oslog_streamsoftc
.sc_state
|= LOG_ASYNC
;
881 oslog_streamsoftc
.sc_state
&= ~LOG_ASYNC
;
896 /* After this point, we must be ready to accept characters */
903 if (!PE_parse_boot_argn("firehose_chunk_count", &__firehose_buffer_kernel_chunk_count
, sizeof(__firehose_buffer_kernel_chunk_count
))) {
904 __firehose_buffer_kernel_chunk_count
= FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT
;
906 if (!PE_parse_boot_argn("firehose_io_pages", &__firehose_num_kernel_io_pages
, sizeof(__firehose_num_kernel_io_pages
))) {
907 __firehose_num_kernel_io_pages
= FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES
;
909 if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
)) {
910 printf("illegal firehose configuration %u/%u, using defaults\n", __firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
);
911 __firehose_buffer_kernel_chunk_count
= FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT
;
912 __firehose_num_kernel_io_pages
= FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES
;
914 vm_size_t size
= __firehose_buffer_kernel_chunk_count
* FIREHOSE_CHUNK_SIZE
;
918 kr
= kmem_alloc_flags(kernel_map
, &kernel_firehose_addr
,
919 size
+ (2 * PAGE_SIZE
), VM_KERN_MEMORY_LOG
,
920 KMA_GUARD_FIRST
| KMA_GUARD_LAST
);
921 if (kr
!= KERN_SUCCESS
) {
922 panic("Failed to allocate memory for firehose logging buffer");
924 kernel_firehose_addr
+= PAGE_SIZE
;
925 bzero((void *)kernel_firehose_addr
, size
);
926 /* register buffer with firehose */
927 kernel_firehose_addr
= (vm_offset_t
)__firehose_buffer_create((size_t *) &size
);
929 printf("oslog_init completed, %u chunks, %u io pages\n", __firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
);
935 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
938 * Parameters: c Character to output
942 * Notes: This functions is used for multibyte output to the log; it
943 * should be used preferrentially where possible to ensure that
944 * log entries do not end up interspersed due to preemption or
948 log_putc_locked(struct msgbuf
*mbp
, char c
)
950 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
951 if (mbp
->msg_bufx
>= mbp
->msg_size
) {
956 static oslog_stream_buf_entry_t
957 oslog_stream_find_free_buf_entry_locked(void)
960 oslog_stream_buf_entry_t buf_entry
= NULL
;
962 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
964 mbp
= oslog_streambufp
;
966 buf_entry
= STAILQ_FIRST(&oslog_stream_free_head
);
968 STAILQ_REMOVE_HEAD(&oslog_stream_free_head
, buf_entries
);
970 // If no list elements are available in the free-list,
971 // consume the next log line so we can free up its list element
972 oslog_stream_buf_entry_t prev_entry
= NULL
;
974 buf_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
975 while (buf_entry
->type
== oslog_stream_link_type_metadata
) {
976 prev_entry
= buf_entry
;
977 buf_entry
= STAILQ_NEXT(buf_entry
, buf_entries
);
980 if (prev_entry
== NULL
) {
981 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
983 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
986 mbp
->msg_bufr
+= buf_entry
->size
;
987 oslog_s_dropped_msgcount
++;
988 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
989 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
997 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
)
999 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1000 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, m_entry
, buf_entries
);
1006 oslog_streamwrite_append_bytes(const char *buffer
, int buflen
)
1010 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1012 assert(oslog_stream_buf_bytesavail
>= buflen
);
1013 oslog_stream_buf_bytesavail
-= buflen
;
1014 assert(oslog_stream_buf_bytesavail
>= 0);
1016 mbp
= oslog_streambufp
;
1017 if (mbp
->msg_bufx
+ buflen
<= mbp
->msg_size
) {
1019 * If this will fit without needing to be split across the end
1020 * of the buffer, copy it directly in one go.
1022 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, buflen
);
1024 mbp
->msg_bufx
+= buflen
;
1025 if (mbp
->msg_bufx
== mbp
->msg_size
) {
1030 * Copy up to the end of the stream buffer, and then put what remains
1033 int bytes_left
= mbp
->msg_size
- mbp
->msg_bufx
;
1034 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, bytes_left
);
1036 buflen
-= bytes_left
;
1037 buffer
+= bytes_left
;
1039 // Copy the remainder of the data from the beginning of stream
1040 memcpy((void *)mbp
->msg_bufc
, buffer
, buflen
);
1041 mbp
->msg_bufx
= buflen
;
1047 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
1048 uint64_t stamp
, const void *pubdata
, size_t publen
)
1051 oslog_stream_buf_entry_t buf_entry
= NULL
;
1052 oslog_stream_buf_entry_t next_entry
= NULL
;
1054 uint16_t ft_size
= offsetof(struct firehose_tracepoint_s
, ft_data
);
1055 int ft_length
= ft_size
+ publen
;
1057 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1059 mbp
= oslog_streambufp
;
1060 if (ft_length
> mbp
->msg_size
) {
1061 os_atomic_inc(&oslog_s_error_count
, relaxed
);
1065 // Ensure that we have a list element for this record
1066 buf_entry
= oslog_stream_find_free_buf_entry_locked();
1068 assert(buf_entry
!= NULL
);
1070 while (ft_length
> oslog_stream_buf_bytesavail
) {
1071 oslog_stream_buf_entry_t prev_entry
= NULL
;
1073 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
1074 assert(next_entry
!= NULL
);
1075 while (next_entry
->type
== oslog_stream_link_type_metadata
) {
1076 prev_entry
= next_entry
;
1077 next_entry
= STAILQ_NEXT(next_entry
, buf_entries
);
1080 if (prev_entry
== NULL
) {
1081 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
1083 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
1086 mbp
->msg_bufr
+= next_entry
->size
;
1087 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
1088 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
1091 oslog_s_dropped_msgcount
++;
1092 oslog_stream_buf_bytesavail
+= next_entry
->size
;
1093 assert(oslog_stream_buf_bytesavail
<= oslog_stream_buf_size
);
1095 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, next_entry
, buf_entries
);
1098 assert(ft_length
<= oslog_stream_buf_bytesavail
);
1100 // Write the log line and update the list entry for this record
1101 buf_entry
->offset
= mbp
->msg_bufx
;
1102 buf_entry
->size
= ft_length
;
1103 buf_entry
->timestamp
= stamp
;
1104 buf_entry
->type
= oslog_stream_link_type_log
;
1106 // Construct a tracepoint
1107 struct firehose_tracepoint_s fs
= {
1108 .ft_thread
= thread_tid(current_thread()),
1109 .ft_id
.ftid_value
= ftid
.ftid_value
,
1113 oslog_streamwrite_append_bytes((char *)&fs
, sizeof(fs
));
1114 oslog_streamwrite_append_bytes(pubdata
, publen
);
1116 assert(mbp
->msg_bufr
< mbp
->msg_size
);
1117 // Insert the element to the buffer data list
1118 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, buf_entry
, buf_entries
);
1128 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1129 * held by the caller.
1131 * Parameters: c Character to output
1135 * Notes: This function is used for single byte output to the log. It
1136 * primarily exists to maintain binary backward compatibility.
1141 int unread_count
= 0;
1143 log_putc_locked(msgbufp
, c
);
1144 unread_count
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
1147 if (unread_count
< 0) {
1148 unread_count
= 0 - unread_count
;
1150 if (c
== '\n' || unread_count
>= (msgbufp
->msg_size
/ 2)) {
1157 * it is possible to increase the kernel log buffer size by adding
1159 * to the kernel command line, and to read the current size using
1160 * sysctl kern.msgbuf
1161 * If there is no parameter on the kernel command line, the buffer is
1162 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1163 * memory is dynamically allocated. Memory management must already be up.
1166 log_setsize(int size
)
1169 int new_logsize
, new_bufr
, new_bufx
;
1171 int old_logsize
, old_bufr
, old_bufx
;
1175 if (size
> MAX_MSG_BSIZE
) {
1184 if (!(new_logdata
= (char*)kalloc(size
))) {
1185 printf("log_setsize: unable to allocate memory\n");
1188 bzero(new_logdata
, new_logsize
);
1192 old_logsize
= msgbufp
->msg_size
;
1193 old_logdata
= msgbufp
->msg_bufc
;
1194 old_bufr
= msgbufp
->msg_bufr
;
1195 old_bufx
= msgbufp
->msg_bufx
;
1197 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1198 size
, old_logdata
, old_logsize
, old_bufr
, old_bufx
);
1200 /* start "new_logsize" bytes before the write pointer */
1201 if (new_logsize
<= old_bufx
) {
1202 count
= new_logsize
;
1203 p
= old_logdata
+ old_bufx
- count
;
1206 * if new buffer is bigger, copy what we have and let the
1207 * bzero above handle the difference
1209 count
= MIN(new_logsize
, old_logsize
);
1210 p
= old_logdata
+ old_logsize
- (count
- old_bufx
);
1212 for (i
= 0; i
< count
; i
++) {
1213 if (p
>= old_logdata
+ old_logsize
) {
1218 new_logdata
[i
] = ch
;
1222 if (new_bufx
>= new_logsize
) {
1225 msgbufp
->msg_bufx
= new_bufx
;
1227 new_bufr
= old_bufx
- old_bufr
; /* how much were we trailing bufx by? */
1229 new_bufr
+= old_logsize
;
1231 new_bufr
= new_bufx
- new_bufr
; /* now relative to oldest data in new buffer */
1233 new_bufr
+= new_logsize
;
1235 msgbufp
->msg_bufr
= new_bufr
;
1237 msgbufp
->msg_size
= new_logsize
;
1238 msgbufp
->msg_bufc
= new_logdata
;
1240 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1241 size
, new_logdata
, new_logsize
, new_bufr
, new_bufx
);
1245 /* this memory is now dead - clear it so that it compresses better
1246 * in case of suspend to disk etc. */
1247 bzero(old_logdata
, old_logsize
);
1248 if (old_logdata
!= smsg_bufc
) {
1249 /* dynamic memory that must be freed */
1250 kfree(old_logdata
, old_logsize
);
1253 printf("set system log size to %d bytes\n", new_logsize
);
1259 oslog_setsize(int size
)
1262 // If the size is less than the default stream buffer
1264 if (size
<= OSLOG_STREAM_BUF_SIZE
) {
1268 scale
= (uint16_t) (size
/ OSLOG_STREAM_BUF_SIZE
);
1270 oslog_stream_buf_size
= size
;
1271 oslog_stream_num_entries
= scale
* OSLOG_NUM_STREAM_ENTRIES
;
1272 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size
, oslog_stream_num_entries
);
1275 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
,
1276 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0,
1277 sysctl_kern_msgbuf
, "I", "");
1280 sysctl_kern_msgbuf(struct sysctl_oid
*oidp __unused
,
1281 void *arg1 __unused
, int arg2 __unused
, struct sysctl_req
*req
)
1283 int old_bufsize
, bufsize
;
1287 old_bufsize
= bufsize
= msgbufp
->msg_size
;
1290 error
= sysctl_io_number(req
, bufsize
, sizeof(bufsize
), &bufsize
, NULL
);
1295 if (bufsize
!= old_bufsize
) {
1296 error
= log_setsize(bufsize
);
1304 * This should be called by /sbin/dmesg only via libproc.
1305 * It returns as much data still in the buffer as possible.
1308 log_dmesg(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1311 uint32_t localbuff_size
;
1312 int error
= 0, newl
, skip
;
1313 char *localbuff
, *p
, *copystart
, ch
;
1317 localbuff_size
= (msgbufp
->msg_size
+ 2); /* + '\n' + '\0' */
1320 /* Allocate a temporary non-circular buffer for copyout */
1321 if (!(localbuff
= (char *)kalloc(localbuff_size
))) {
1322 printf("log_dmesg: unable to allocate memory\n");
1326 /* in between here, the log could become bigger, but that's fine */
1330 * The message buffer is circular; start at the write pointer, and
1331 * make one loop up to write pointer - 1.
1333 p
= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
;
1334 for (i
= newl
= skip
= 0; p
!= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
- 1; ++p
) {
1335 if (p
>= msgbufp
->msg_bufc
+ msgbufp
->msg_size
) {
1336 p
= msgbufp
->msg_bufc
;
1339 /* Skip "\n<.*>" syslog sequences. */
1346 if (newl
&& ch
== '<') {
1353 newl
= (ch
== '\n');
1354 localbuff
[i
++] = ch
;
1355 /* The original version of this routine contained a buffer
1356 * overflow. At the time, a "small" targeted fix was desired
1357 * so the change below to check the buffer bounds was made.
1358 * TODO: rewrite this needlessly convoluted routine.
1360 if (i
== (localbuff_size
- 2)) {
1365 localbuff
[i
++] = '\n';
1369 if (buffersize
>= i
) {
1370 copystart
= localbuff
;
1373 copystart
= localbuff
+ i
- buffersize
;
1374 copysize
= buffersize
;
1379 error
= copyout(copystart
, buffer
, copysize
);
1384 kfree(localbuff
, localbuff_size
);
1388 #ifdef CONFIG_XNUPOST
1390 uint32_t find_pattern_in_buffer(char * pattern
, uint32_t len
, int expected_count
);
1393 * returns count of pattern found in systemlog buffer.
1394 * stops searching further if count reaches expected_count.
1397 find_pattern_in_buffer(char * pattern
, uint32_t len
, int expected_count
)
1399 int match_count
= 0;
1406 if (pattern
== NULL
|| len
== 0 || expected_count
== 0) {
1410 for (i
= 0; i
< msgbufp
->msg_size
; i
++) {
1412 for (j
= 0; j
< (int)len
; j
++) {
1413 pos
= (msgbufp
->msg_bufx
+ i
+ j
) % msgbufp
->msg_size
;
1414 ch
= msgbufp
->msg_bufc
[pos
];
1415 if (ch
!= pattern
[j
]) {
1420 if (no_match
== 0) {
1422 if (match_count
>= expected_count
) {