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
) {
335 /* Zeroing to avoid copying uninitialized struct padding to userspace. */
336 bzero(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
338 /* entries to support kernel logging in stream mode */
339 size_t entries_size
= oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
);
340 entries
= kalloc(entries_size
);
342 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
345 /* Zeroing to avoid copying uninitialized struct padding to userspace. */
346 bzero(entries
, entries_size
);
349 if (oslog_stream_open
) {
351 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
352 kfree(entries
, entries_size
);
356 assert(oslog_streambufp
->msg_bufc
== NULL
);
357 oslog_streambufp
->msg_bufc
= oslog_stream_msg_bufc
;
358 oslog_streambufp
->msg_size
= oslog_stream_buf_size
;
360 oslog_stream_buf_entries
= entries
;
362 STAILQ_INIT(&oslog_stream_free_head
);
363 STAILQ_INIT(&oslog_stream_buf_head
);
365 for (int i
= 0; i
< oslog_stream_num_entries
; i
++) {
366 oslog_stream_buf_entries
[i
].type
= oslog_stream_link_type_log
;
367 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, &oslog_stream_buf_entries
[i
], buf_entries
);
370 /* there should be no pending entries in the stream */
371 assert(STAILQ_EMPTY(&oslog_stream_buf_head
));
372 assert(oslog_streambufp
->msg_bufx
== 0);
373 assert(oslog_streambufp
->msg_bufr
== 0);
375 oslog_streambufp
->msg_bufx
= 0;
376 oslog_streambufp
->msg_bufr
= 0;
377 oslog_streamsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
378 oslog_stream_open
= 1;
379 oslog_stream_buf_bytesavail
= oslog_stream_buf_size
;
386 oslog_streamclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
388 oslog_stream_buf_entry_t next_entry
= NULL
;
389 char *oslog_stream_msg_bufc
= NULL
;
390 oslog_stream_buf_entry_t entries
= NULL
;
394 if (oslog_stream_open
== 0) {
399 // Consume all log lines
400 while (!STAILQ_EMPTY(&oslog_stream_buf_head
)) {
401 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
402 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
404 oslog_streamwakeup_locked();
405 oslog_streamsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
406 selwakeup(&oslog_streamsoftc
.sc_selp
);
407 selthreadclear(&oslog_streamsoftc
.sc_selp
);
408 oslog_stream_open
= 0;
409 oslog_streambufp
->msg_bufr
= 0;
410 oslog_streambufp
->msg_bufx
= 0;
411 oslog_stream_msg_bufc
= oslog_streambufp
->msg_bufc
;
412 oslog_streambufp
->msg_bufc
= NULL
;
413 entries
= oslog_stream_buf_entries
;
414 oslog_stream_buf_entries
= NULL
;
415 oslog_streambufp
->msg_size
= 0;
419 // Free the stream buffer
420 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
421 // Free the list entries
422 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
429 logread(__unused dev_t dev
, struct uio
*uio
, int flag
)
433 struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
436 while (mbp
->msg_bufr
== mbp
->msg_bufx
) {
437 if (flag
& IO_NDELAY
) {
441 if (logsoftc
.sc_state
& LOG_NBIO
) {
445 logsoftc
.sc_state
|= LOG_RDWAIT
;
448 * If the wakeup is missed
449 * then wait for 5 sec and reevaluate
451 if ((error
= tsleep((caddr_t
)mbp
, LOG_RDPRI
| PCATCH
,
452 "klog", 5 * hz
)) != 0) {
453 /* if it times out; ignore */
454 if (error
!= EWOULDBLOCK
) {
460 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
462 while (uio_resid(uio
) > 0) {
465 l
= mbp
->msg_bufx
- mbp
->msg_bufr
;
467 l
= mbp
->msg_size
- mbp
->msg_bufr
;
469 l
= min(l
, uio_resid(uio
));
474 readpos
= mbp
->msg_bufr
;
476 error
= uiomove((caddr_t
)&mbp
->msg_bufc
[readpos
], l
, uio
);
481 mbp
->msg_bufr
= readpos
+ l
;
482 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
493 oslog_streamread(__unused dev_t dev
, struct uio
*uio
, int flag
)
497 static char logline
[FIREHOSE_CHUNK_SIZE
];
501 if (!oslog_stream_open
) {
506 while (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
507 assert(oslog_stream_buf_bytesavail
== oslog_stream_buf_size
);
509 if (flag
& IO_NDELAY
|| oslog_streamsoftc
.sc_state
& LOG_NBIO
) {
514 oslog_streamsoftc
.sc_state
|= LOG_RDWAIT
;
515 wait_result_t wr
= assert_wait((event_t
)oslog_streambufp
,
516 THREAD_INTERRUPTIBLE
);
517 if (wr
== THREAD_WAITING
) {
519 wr
= thread_block(THREAD_CONTINUE_NULL
);
524 case THREAD_AWAKENED
:
525 case THREAD_TIMED_OUT
:
533 if (!oslog_stream_open
) {
539 oslog_stream_buf_entry_t read_entry
= NULL
;
542 read_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
543 assert(read_entry
!= NULL
);
544 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
546 // Copy the timestamp first
547 memcpy(logline
+ logpos
, &read_entry
->timestamp
, sizeof(uint64_t));
548 logpos
+= sizeof(uint64_t);
550 switch (read_entry
->type
) {
551 /* Handle metadata messages */
552 case oslog_stream_link_type_metadata
:
554 memcpy(logline
+ logpos
,
555 (read_entry
->metadata
), read_entry
->size
);
556 logpos
+= read_entry
->size
;
560 // Free the list entry
561 kfree(read_entry
, (sizeof(struct oslog_stream_buf_entry_s
) + read_entry
->size
));
564 /* Handle log messages */
565 case oslog_stream_link_type_log
:
567 /* ensure that the correct read entry was dequeued */
568 assert(read_entry
->offset
== oslog_streambufp
->msg_bufr
);
569 rec_length
= read_entry
->size
;
571 // If the next log line is contiguous in the buffer, copy it out.
572 if (read_entry
->offset
+ rec_length
<= oslog_streambufp
->msg_size
) {
573 memcpy(logline
+ logpos
,
574 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, rec_length
);
576 oslog_streambufp
->msg_bufr
+= rec_length
;
577 if (oslog_streambufp
->msg_bufr
== oslog_streambufp
->msg_size
) {
578 oslog_streambufp
->msg_bufr
= 0;
580 logpos
+= rec_length
;
582 // Otherwise, copy until the end of the buffer, and
583 // copy the remaining bytes starting at index 0.
584 int bytes_left
= oslog_streambufp
->msg_size
- read_entry
->offset
;
585 memcpy(logline
+ logpos
,
586 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, bytes_left
);
587 logpos
+= bytes_left
;
588 rec_length
-= bytes_left
;
590 memcpy(logline
+ logpos
, (const void *)oslog_streambufp
->msg_bufc
,
592 oslog_streambufp
->msg_bufr
= rec_length
;
593 logpos
+= rec_length
;
596 oslog_stream_buf_bytesavail
+= read_entry
->size
;
597 assert(oslog_stream_buf_bytesavail
<= oslog_stream_buf_size
);
599 assert(oslog_streambufp
->msg_bufr
< oslog_streambufp
->msg_size
);
600 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, read_entry
, buf_entries
);
607 panic("Got unexpected log entry type: %hhu\n", read_entry
->type
);
611 copy_size
= min(logpos
, uio_resid(uio
));
612 if (copy_size
!= 0) {
613 error
= uiomove((caddr_t
)logline
, copy_size
, uio
);
615 os_atomic_inc(&oslog_s_streamed_msgcount
, relaxed
);
622 logselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
624 const struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
629 if (mbp
->msg_bufr
!= mbp
->msg_bufx
) {
633 selrecord(p
, &logsoftc
.sc_selp
, wql
);
641 oslogselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
650 selrecord(p
, &oslogsoftc
.sc_selp
, wql
);
658 oslog_streamselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
666 if (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
667 selrecord(p
, &oslog_streamsoftc
.sc_selp
, wql
);
679 logwakeup(struct msgbuf
*mbp
)
681 /* cf. r24974766 & r25201228*/
682 if (oslog_is_safe() == FALSE
) {
692 mbp
= logsoftc
.sc_mbp
;
694 if (mbp
!= logsoftc
.sc_mbp
) {
697 selwakeup(&logsoftc
.sc_selp
);
698 if (logsoftc
.sc_state
& LOG_ASYNC
) {
699 int pgid
= logsoftc
.sc_pgid
;
702 gsignal(-pgid
, SIGIO
);
704 proc_signal(pgid
, SIGIO
);
708 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
709 wakeup((caddr_t
)mbp
);
710 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
724 selwakeup(&oslogsoftc
.sc_selp
);
730 oslog_streamwakeup_locked(void)
732 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
733 if (!oslog_stream_open
) {
736 selwakeup(&oslog_streamsoftc
.sc_selp
);
737 if (oslog_streamsoftc
.sc_state
& LOG_RDWAIT
) {
738 wakeup((caddr_t
)oslog_streambufp
);
739 oslog_streamsoftc
.sc_state
&= ~LOG_RDWAIT
;
744 oslog_streamwakeup(void)
746 /* cf. r24974766 & r25201228*/
747 if (oslog_is_safe() == FALSE
) {
752 oslog_streamwakeup_locked();
758 logioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
761 const struct msgbuf
*mbp
= logsoftc
.sc_mbp
;
765 /* return number of characters immediately available */
767 l
= mbp
->msg_bufx
- mbp
->msg_bufr
;
776 logsoftc
.sc_state
|= LOG_NBIO
;
778 logsoftc
.sc_state
&= ~LOG_NBIO
;
784 logsoftc
.sc_state
|= LOG_ASYNC
;
786 logsoftc
.sc_state
&= ~LOG_ASYNC
;
791 logsoftc
.sc_pgid
= *(int *)data
;
795 *(int *)data
= logsoftc
.sc_pgid
;
808 oslogioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
811 mach_vm_size_t buffer_size
= (__firehose_buffer_kernel_chunk_count
* FIREHOSE_CHUNK_SIZE
);
812 firehose_buffer_map_info_t map_info
= {0, 0};
813 firehose_buffer_t kernel_firehose_buffer
= NULL
;
814 mach_vm_address_t user_addr
= 0;
815 mach_port_t mem_entry_ptr
= MACH_PORT_NULL
;
818 /* return number of characters immediately available */
821 kernel_firehose_buffer
= (firehose_buffer_t
)kernel_firehose_addr
;
823 ret
= mach_make_memory_entry_64(kernel_map
,
825 (mach_vm_offset_t
) kernel_firehose_buffer
,
826 (MAP_MEM_VM_SHARE
| VM_PROT_READ
),
829 if (ret
== KERN_SUCCESS
) {
830 ret
= mach_vm_map_kernel(get_task_map(current_task()),
835 VM_MAP_KERNEL_FLAGS_NONE
,
845 if (ret
== KERN_SUCCESS
) {
846 map_info
.fbmi_addr
= (uint64_t) (user_addr
);
847 map_info
.fbmi_size
= buffer_size
;
848 bcopy(&map_info
, data
, sizeof(firehose_buffer_map_info_t
));
855 __firehose_merge_updates(*(firehose_push_reply_t
*)(data
));
865 oslog_streamioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
873 if (data
&& *(int *)data
) {
874 oslog_streamsoftc
.sc_state
|= LOG_NBIO
;
876 oslog_streamsoftc
.sc_state
&= ~LOG_NBIO
;
880 if (data
&& *(int *)data
) {
881 oslog_streamsoftc
.sc_state
|= LOG_ASYNC
;
883 oslog_streamsoftc
.sc_state
&= ~LOG_ASYNC
;
898 /* After this point, we must be ready to accept characters */
905 if (!PE_parse_boot_argn("firehose_chunk_count", &__firehose_buffer_kernel_chunk_count
, sizeof(__firehose_buffer_kernel_chunk_count
))) {
906 __firehose_buffer_kernel_chunk_count
= FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT
;
908 if (!PE_parse_boot_argn("firehose_io_pages", &__firehose_num_kernel_io_pages
, sizeof(__firehose_num_kernel_io_pages
))) {
909 __firehose_num_kernel_io_pages
= FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES
;
911 if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
)) {
912 printf("illegal firehose configuration %u/%u, using defaults\n", __firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
);
913 __firehose_buffer_kernel_chunk_count
= FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT
;
914 __firehose_num_kernel_io_pages
= FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES
;
916 vm_size_t size
= __firehose_buffer_kernel_chunk_count
* FIREHOSE_CHUNK_SIZE
;
920 kr
= kmem_alloc_flags(kernel_map
, &kernel_firehose_addr
,
921 size
+ (2 * PAGE_SIZE
), VM_KERN_MEMORY_LOG
,
922 KMA_GUARD_FIRST
| KMA_GUARD_LAST
);
923 if (kr
!= KERN_SUCCESS
) {
924 panic("Failed to allocate memory for firehose logging buffer");
926 kernel_firehose_addr
+= PAGE_SIZE
;
927 bzero((void *)kernel_firehose_addr
, size
);
928 /* register buffer with firehose */
929 kernel_firehose_addr
= (vm_offset_t
)__firehose_buffer_create((size_t *) &size
);
931 printf("oslog_init completed, %u chunks, %u io pages\n", __firehose_buffer_kernel_chunk_count
, __firehose_num_kernel_io_pages
);
937 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
940 * Parameters: c Character to output
944 * Notes: This functions is used for multibyte output to the log; it
945 * should be used preferrentially where possible to ensure that
946 * log entries do not end up interspersed due to preemption or
950 log_putc_locked(struct msgbuf
*mbp
, char c
)
952 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
953 if (mbp
->msg_bufx
>= mbp
->msg_size
) {
958 static oslog_stream_buf_entry_t
959 oslog_stream_find_free_buf_entry_locked(void)
962 oslog_stream_buf_entry_t buf_entry
= NULL
;
964 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
966 mbp
= oslog_streambufp
;
968 buf_entry
= STAILQ_FIRST(&oslog_stream_free_head
);
970 STAILQ_REMOVE_HEAD(&oslog_stream_free_head
, buf_entries
);
972 // If no list elements are available in the free-list,
973 // consume the next log line so we can free up its list element
974 oslog_stream_buf_entry_t prev_entry
= NULL
;
976 buf_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
977 while (buf_entry
->type
== oslog_stream_link_type_metadata
) {
978 prev_entry
= buf_entry
;
979 buf_entry
= STAILQ_NEXT(buf_entry
, buf_entries
);
982 if (prev_entry
== NULL
) {
983 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
985 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
988 mbp
->msg_bufr
+= buf_entry
->size
;
989 oslog_s_dropped_msgcount
++;
990 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
991 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
999 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
)
1001 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1002 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, m_entry
, buf_entries
);
1008 oslog_streamwrite_append_bytes(const char *buffer
, int buflen
)
1012 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1014 assert(oslog_stream_buf_bytesavail
>= buflen
);
1015 oslog_stream_buf_bytesavail
-= buflen
;
1016 assert(oslog_stream_buf_bytesavail
>= 0);
1018 mbp
= oslog_streambufp
;
1019 if (mbp
->msg_bufx
+ buflen
<= mbp
->msg_size
) {
1021 * If this will fit without needing to be split across the end
1022 * of the buffer, copy it directly in one go.
1024 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, buflen
);
1026 mbp
->msg_bufx
+= buflen
;
1027 if (mbp
->msg_bufx
== mbp
->msg_size
) {
1032 * Copy up to the end of the stream buffer, and then put what remains
1035 int bytes_left
= mbp
->msg_size
- mbp
->msg_bufx
;
1036 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, bytes_left
);
1038 buflen
-= bytes_left
;
1039 buffer
+= bytes_left
;
1041 // Copy the remainder of the data from the beginning of stream
1042 memcpy((void *)mbp
->msg_bufc
, buffer
, buflen
);
1043 mbp
->msg_bufx
= buflen
;
1049 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
1050 uint64_t stamp
, const void *pubdata
, size_t publen
)
1053 oslog_stream_buf_entry_t buf_entry
= NULL
;
1054 oslog_stream_buf_entry_t next_entry
= NULL
;
1056 uint16_t ft_size
= offsetof(struct firehose_tracepoint_s
, ft_data
);
1057 int ft_length
= ft_size
+ publen
;
1059 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1061 mbp
= oslog_streambufp
;
1062 if (ft_length
> mbp
->msg_size
) {
1063 os_atomic_inc(&oslog_s_error_count
, relaxed
);
1067 // Ensure that we have a list element for this record
1068 buf_entry
= oslog_stream_find_free_buf_entry_locked();
1070 assert(buf_entry
!= NULL
);
1072 while (ft_length
> oslog_stream_buf_bytesavail
) {
1073 oslog_stream_buf_entry_t prev_entry
= NULL
;
1075 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
1076 assert(next_entry
!= NULL
);
1077 while (next_entry
->type
== oslog_stream_link_type_metadata
) {
1078 prev_entry
= next_entry
;
1079 next_entry
= STAILQ_NEXT(next_entry
, buf_entries
);
1082 if (prev_entry
== NULL
) {
1083 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
1085 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
1088 mbp
->msg_bufr
+= next_entry
->size
;
1089 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
1090 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
1093 oslog_s_dropped_msgcount
++;
1094 oslog_stream_buf_bytesavail
+= next_entry
->size
;
1095 assert(oslog_stream_buf_bytesavail
<= oslog_stream_buf_size
);
1097 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, next_entry
, buf_entries
);
1100 assert(ft_length
<= oslog_stream_buf_bytesavail
);
1102 // Write the log line and update the list entry for this record
1103 buf_entry
->offset
= mbp
->msg_bufx
;
1104 buf_entry
->size
= ft_length
;
1105 buf_entry
->timestamp
= stamp
;
1106 buf_entry
->type
= oslog_stream_link_type_log
;
1108 // Construct a tracepoint
1109 struct firehose_tracepoint_s fs
= {
1110 .ft_thread
= thread_tid(current_thread()),
1111 .ft_id
.ftid_value
= ftid
.ftid_value
,
1115 oslog_streamwrite_append_bytes((char *)&fs
, sizeof(fs
));
1116 oslog_streamwrite_append_bytes(pubdata
, publen
);
1118 assert(mbp
->msg_bufr
< mbp
->msg_size
);
1119 // Insert the element to the buffer data list
1120 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, buf_entry
, buf_entries
);
1130 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1131 * held by the caller.
1133 * Parameters: c Character to output
1137 * Notes: This function is used for single byte output to the log. It
1138 * primarily exists to maintain binary backward compatibility.
1143 int unread_count
= 0;
1145 log_putc_locked(msgbufp
, c
);
1146 unread_count
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
1149 if (unread_count
< 0) {
1150 unread_count
= 0 - unread_count
;
1152 if (c
== '\n' || unread_count
>= (msgbufp
->msg_size
/ 2)) {
1159 * it is possible to increase the kernel log buffer size by adding
1161 * to the kernel command line, and to read the current size using
1162 * sysctl kern.msgbuf
1163 * If there is no parameter on the kernel command line, the buffer is
1164 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1165 * memory is dynamically allocated. Memory management must already be up.
1168 log_setsize(int size
)
1171 int new_logsize
, new_bufr
, new_bufx
;
1173 int old_logsize
, old_bufr
, old_bufx
;
1177 if (size
> MAX_MSG_BSIZE
) {
1186 if (!(new_logdata
= (char*)kalloc(size
))) {
1187 printf("log_setsize: unable to allocate memory\n");
1190 bzero(new_logdata
, new_logsize
);
1194 old_logsize
= msgbufp
->msg_size
;
1195 old_logdata
= msgbufp
->msg_bufc
;
1196 old_bufr
= msgbufp
->msg_bufr
;
1197 old_bufx
= msgbufp
->msg_bufx
;
1199 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1200 size
, old_logdata
, old_logsize
, old_bufr
, old_bufx
);
1202 /* start "new_logsize" bytes before the write pointer */
1203 if (new_logsize
<= old_bufx
) {
1204 count
= new_logsize
;
1205 p
= old_logdata
+ old_bufx
- count
;
1208 * if new buffer is bigger, copy what we have and let the
1209 * bzero above handle the difference
1211 count
= MIN(new_logsize
, old_logsize
);
1212 p
= old_logdata
+ old_logsize
- (count
- old_bufx
);
1214 for (i
= 0; i
< count
; i
++) {
1215 if (p
>= old_logdata
+ old_logsize
) {
1220 new_logdata
[i
] = ch
;
1224 if (new_bufx
>= new_logsize
) {
1227 msgbufp
->msg_bufx
= new_bufx
;
1229 new_bufr
= old_bufx
- old_bufr
; /* how much were we trailing bufx by? */
1231 new_bufr
+= old_logsize
;
1233 new_bufr
= new_bufx
- new_bufr
; /* now relative to oldest data in new buffer */
1235 new_bufr
+= new_logsize
;
1237 msgbufp
->msg_bufr
= new_bufr
;
1239 msgbufp
->msg_size
= new_logsize
;
1240 msgbufp
->msg_bufc
= new_logdata
;
1242 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1243 size
, new_logdata
, new_logsize
, new_bufr
, new_bufx
);
1247 /* this memory is now dead - clear it so that it compresses better
1248 * in case of suspend to disk etc. */
1249 bzero(old_logdata
, old_logsize
);
1250 if (old_logdata
!= smsg_bufc
) {
1251 /* dynamic memory that must be freed */
1252 kfree(old_logdata
, old_logsize
);
1255 printf("set system log size to %d bytes\n", new_logsize
);
1261 oslog_setsize(int size
)
1264 // If the size is less than the default stream buffer
1266 if (size
<= OSLOG_STREAM_BUF_SIZE
) {
1270 scale
= (uint16_t) (size
/ OSLOG_STREAM_BUF_SIZE
);
1272 oslog_stream_buf_size
= size
;
1273 oslog_stream_num_entries
= scale
* OSLOG_NUM_STREAM_ENTRIES
;
1274 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size
, oslog_stream_num_entries
);
1277 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
,
1278 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0,
1279 sysctl_kern_msgbuf
, "I", "");
1282 sysctl_kern_msgbuf(struct sysctl_oid
*oidp __unused
,
1283 void *arg1 __unused
, int arg2 __unused
, struct sysctl_req
*req
)
1285 int old_bufsize
, bufsize
;
1289 old_bufsize
= bufsize
= msgbufp
->msg_size
;
1292 error
= sysctl_io_number(req
, bufsize
, sizeof(bufsize
), &bufsize
, NULL
);
1297 if (bufsize
!= old_bufsize
) {
1298 error
= log_setsize(bufsize
);
1306 * This should be called by /sbin/dmesg only via libproc.
1307 * It returns as much data still in the buffer as possible.
1310 log_dmesg(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
)
1313 uint32_t localbuff_size
;
1314 int error
= 0, newl
, skip
;
1315 char *localbuff
, *p
, *copystart
, ch
;
1319 localbuff_size
= (msgbufp
->msg_size
+ 2); /* + '\n' + '\0' */
1322 /* Allocate a temporary non-circular buffer for copyout */
1323 if (!(localbuff
= (char *)kalloc(localbuff_size
))) {
1324 printf("log_dmesg: unable to allocate memory\n");
1328 /* in between here, the log could become bigger, but that's fine */
1332 * The message buffer is circular; start at the write pointer, and
1333 * make one loop up to write pointer - 1.
1335 p
= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
;
1336 for (i
= newl
= skip
= 0; p
!= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
- 1; ++p
) {
1337 if (p
>= msgbufp
->msg_bufc
+ msgbufp
->msg_size
) {
1338 p
= msgbufp
->msg_bufc
;
1341 /* Skip "\n<.*>" syslog sequences. */
1348 if (newl
&& ch
== '<') {
1355 newl
= (ch
== '\n');
1356 localbuff
[i
++] = ch
;
1357 /* The original version of this routine contained a buffer
1358 * overflow. At the time, a "small" targeted fix was desired
1359 * so the change below to check the buffer bounds was made.
1360 * TODO: rewrite this needlessly convoluted routine.
1362 if (i
== (localbuff_size
- 2)) {
1367 localbuff
[i
++] = '\n';
1371 if (buffersize
>= i
) {
1372 copystart
= localbuff
;
1375 copystart
= localbuff
+ i
- buffersize
;
1376 copysize
= buffersize
;
1381 error
= copyout(copystart
, buffer
, copysize
);
1386 kfree(localbuff
, localbuff_size
);
1390 #ifdef CONFIG_XNUPOST
1392 uint32_t find_pattern_in_buffer(char * pattern
, uint32_t len
, int expected_count
);
1395 * returns count of pattern found in systemlog buffer.
1396 * stops searching further if count reaches expected_count.
1399 find_pattern_in_buffer(char * pattern
, uint32_t len
, int expected_count
)
1401 int match_count
= 0;
1408 if (pattern
== NULL
|| len
== 0 || expected_count
== 0) {
1412 for (i
= 0; i
< msgbufp
->msg_size
; i
++) {
1414 for (j
= 0; j
< (int)len
; j
++) {
1415 pos
= (msgbufp
->msg_bufx
+ i
+ j
) % msgbufp
->msg_size
;
1416 ch
= msgbufp
->msg_bufc
[pos
];
1417 if (ch
!= pattern
[j
]) {
1422 if (no_match
== 0) {
1424 if (match_count
>= expected_count
) {