2 * Copyright (c) 2000-2016 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 <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/proc_internal.h>
71 #include <sys/vnode.h>
73 #include <firehose/tracepoint_private.h>
74 #include <firehose/ioctl_private.h>
75 #include <os/firehose_buffer_private.h>
77 #include <os/log_private.h>
78 #include <sys/ioctl.h>
79 #include <sys/msgbuf.h>
80 #include <sys/file_internal.h>
81 #include <sys/errno.h>
82 #include <sys/select.h>
83 #include <sys/kernel.h>
84 #include <kern/thread.h>
85 #include <kern/sched_prim.h>
86 #include <kern/simple_lock.h>
88 #include <sys/signalvar.h>
90 #include <sys/sysctl.h>
91 #include <sys/queue.h>
92 #include <kern/kalloc.h>
93 #include <pexpert/pexpert.h>
94 #include <mach/mach_port.h>
95 #include <mach/mach_vm.h>
96 #include <mach/vm_map.h>
97 #include <vm/vm_kern.h>
98 #include <kern/task.h>
99 #include <kern/locks.h>
101 /* XXX should be in a common header somewhere */
102 extern void logwakeup(void);
103 extern void oslogwakeup(void);
104 extern void oslog_streamwakeup(void);
105 static void oslog_streamwakeup_locked(void);
106 vm_offset_t kernel_firehose_addr
= 0;
108 /* log message counters for streaming mode */
109 uint32_t oslog_s_streamed_msgcount
= 0;
110 uint32_t oslog_s_dropped_msgcount
= 0;
111 extern uint32_t oslog_s_error_count
;
113 #define LOG_RDPRI (PZERO + 1)
115 #define LOG_NBIO 0x02
116 #define LOG_ASYNC 0x04
117 #define LOG_RDWAIT 0x08
119 #define MAX_UNREAD_CHARS (CONFIG_MSG_BSIZE/2)
120 /* All globals should be accessed under LOG_LOCK() */
122 /* logsoftc only valid while log_open=1 */
124 int sc_state
; /* see above for possibilities */
125 struct selinfo sc_selp
; /* thread waiting for select */
126 int sc_pgid
; /* process/group for async I/O */
129 int log_open
; /* also used in log() */
130 char smsg_bufc
[CONFIG_MSG_BSIZE
]; /* static buffer */
131 char oslog_stream_bufc
[FIREHOSE_BUFFER_CHUNK_SIZE
]; /* static buffer */
132 struct firehose_buffer_chunk_s
__attribute__((aligned(8))) oslog_boot_buf
= {
134 .fbc_next_entry_offs
= offsetof(struct firehose_buffer_chunk_s
, fbc_data
),
135 .fbc_private_offs
= FIREHOSE_BUFFER_CHUNK_SIZE
,
136 .fbc_refcnt
= 1, // indicate that there is a writer to this chunk
137 .fbc_stream
= firehose_stream_persist
,
138 .fbc_flag_io
= 1, // for now, lets assume this is coming from the io bank
140 }; /* static buffer */
141 firehose_buffer_chunk_t firehose_boot_chunk
= &oslog_boot_buf
;
142 struct msgbuf msgbuf
= {MSG_MAGIC
,sizeof(smsg_bufc
),0,0,smsg_bufc
};
143 struct msgbuf oslog_stream_buf
= {MSG_MAGIC
,0,0,0,NULL
};
144 struct msgbuf
*msgbufp
__attribute__((used
)) = &msgbuf
;
145 struct msgbuf
*oslog_streambufp
__attribute__((used
)) = &oslog_stream_buf
;
147 // List entries for keeping track of the streaming buffer
148 static oslog_stream_buf_entry_t oslog_stream_buf_entries
;
150 #define OSLOG_NUM_STREAM_ENTRIES 64
151 #define OSLOG_STREAM_BUF_SIZE 4096
154 int os_log_wakeup
= 0;
155 int oslog_stream_open
= 0;
156 int oslog_stream_buf_size
= OSLOG_STREAM_BUF_SIZE
;
157 int oslog_stream_num_entries
= OSLOG_NUM_STREAM_ENTRIES
;
159 /* oslogsoftc only valid while oslog_open=1 */
161 int sc_state
; /* see above for possibilities */
162 struct selinfo sc_selp
; /* thread waiting for select */
163 int sc_pgid
; /* process/group for async I/O */
166 struct oslog_streamsoftc
{
167 int sc_state
; /* see above for possibilities */
168 struct selinfo sc_selp
; /* thread waiting for select */
169 int sc_pgid
; /* process/group for async I/O */
172 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_free_head
=
173 STAILQ_HEAD_INITIALIZER(oslog_stream_free_head
);
174 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_buf_head
=
175 STAILQ_HEAD_INITIALIZER(oslog_stream_buf_head
);
177 /* defined in osfmk/kern/printf.c */
178 extern void oslog_lock_init(void);
179 extern void bsd_log_lock(void);
180 extern void bsd_log_unlock(void);
182 /* defined for osfmk/kern/printf.c */
183 void bsd_log_init(void);
186 * Ideally this file would define this lock, but bsd doesn't have the definition
189 decl_lck_spin_data(extern, oslog_stream_lock
)
191 /* XXX wants a linker set so these can be static */
192 extern d_open_t logopen
;
193 extern d_close_t logclose
;
194 extern d_read_t logread
;
195 extern d_ioctl_t logioctl
;
196 extern d_select_t logselect
;
198 /* XXX wants a linker set so these can be static */
199 extern d_open_t oslogopen
;
200 extern d_close_t oslogclose
;
201 extern d_select_t oslogselect
;
202 extern d_ioctl_t oslogioctl
;
204 /* XXX wants a linker set so these can be static */
205 extern d_open_t oslog_streamopen
;
206 extern d_close_t oslog_streamclose
;
207 extern d_read_t oslog_streamread
;
208 extern d_ioctl_t oslog_streamioctl
;
209 extern d_select_t oslog_streamselect
;
211 void oslog_init(void);
212 void oslog_setsize(int size
);
213 void oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
214 uint64_t stamp
, const void *pubdata
, size_t publen
);
215 void oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
);
216 static oslog_stream_buf_entry_t
oslog_stream_find_free_buf_entry_locked(void);
217 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
);
220 * Serialize log access. Note that the log can be written at interrupt level,
221 * so any log manipulations that can be done from, or affect, another processor
222 * at interrupt level must be guarded with a spin lock.
225 #define LOG_LOCK() bsd_log_lock()
226 #define LOG_UNLOCK() bsd_log_unlock()
229 #define LOG_SETSIZE_DEBUG(x...) kprintf(x)
231 #define LOG_SETSIZE_DEBUG(x...) do { } while(0)
234 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp
,
237 struct sysctl_req
*req
);
241 logopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
248 logsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
258 logclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
261 logsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
262 selwakeup(&logsoftc
.sc_selp
);
263 selthreadclear(&logsoftc
.sc_selp
);
271 oslogopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
278 oslogsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
286 oslogclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
289 oslogsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
290 selwakeup(&oslogsoftc
.sc_selp
);
291 selthreadclear(&oslogsoftc
.sc_selp
);
298 oslog_streamopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
300 char *oslog_stream_msg_bufc
= NULL
;
301 oslog_stream_buf_entry_t entries
= NULL
;
303 lck_spin_lock(&oslog_stream_lock
);
304 if (oslog_stream_open
) {
305 lck_spin_unlock(&oslog_stream_lock
);
308 lck_spin_unlock(&oslog_stream_lock
);
310 // Allocate the stream buffer
311 oslog_stream_msg_bufc
= kalloc(oslog_stream_buf_size
);
312 if (!oslog_stream_msg_bufc
) {
316 /* entries to support kernel logging in stream mode */
317 entries
= kalloc(oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
319 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
323 lck_spin_lock(&oslog_stream_lock
);
324 if (oslog_stream_open
) {
325 lck_spin_unlock(&oslog_stream_lock
);
326 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
327 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
331 assert(oslog_streambufp
->msg_bufc
== NULL
);
332 oslog_streambufp
->msg_bufc
= oslog_stream_msg_bufc
;
333 oslog_streambufp
->msg_size
= oslog_stream_buf_size
;
335 oslog_stream_buf_entries
= entries
;
337 STAILQ_INIT(&oslog_stream_free_head
);
338 STAILQ_INIT(&oslog_stream_buf_head
);
340 for (int i
= 0; i
< oslog_stream_num_entries
; i
++) {
341 oslog_stream_buf_entries
[i
].type
= oslog_stream_link_type_log
;
342 oslog_stream_buf_entries
[i
].offset
= 0;
343 oslog_stream_buf_entries
[i
].size
= 0;
344 oslog_stream_buf_entries
[i
].timestamp
= 0;
345 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, &oslog_stream_buf_entries
[i
], buf_entries
);
348 /* there should be no pending entries in the stream */
349 assert(STAILQ_EMPTY(&oslog_stream_buf_head
));
350 assert(oslog_streambufp
->msg_bufx
== 0);
351 assert(oslog_streambufp
->msg_bufr
== 0);
353 oslog_streambufp
->msg_bufx
= 0;
354 oslog_streambufp
->msg_bufr
= 0;
355 oslog_streamsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
356 oslog_stream_open
= 1;
357 lck_spin_unlock(&oslog_stream_lock
);
363 oslog_streamclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
365 oslog_stream_buf_entry_t next_entry
= NULL
;
366 char *oslog_stream_msg_bufc
= NULL
;
367 oslog_stream_buf_entry_t entries
= NULL
;
369 lck_spin_lock(&oslog_stream_lock
);
371 if (oslog_stream_open
== 0) {
372 lck_spin_unlock(&oslog_stream_lock
);
376 // Consume all log lines
377 while (!STAILQ_EMPTY(&oslog_stream_buf_head
)) {
378 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
379 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
381 oslog_streamwakeup_locked();
382 oslog_streamsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
383 selwakeup(&oslog_streamsoftc
.sc_selp
);
384 selthreadclear(&oslog_streamsoftc
.sc_selp
);
385 oslog_stream_open
= 0;
386 oslog_streambufp
->msg_bufr
= 0;
387 oslog_streambufp
->msg_bufx
= 0;
388 oslog_stream_msg_bufc
= oslog_streambufp
->msg_bufc
;
389 oslog_streambufp
->msg_bufc
= NULL
;
390 entries
= oslog_stream_buf_entries
;
391 oslog_stream_buf_entries
= NULL
;
392 oslog_streambufp
->msg_size
= 0;
394 lck_spin_unlock(&oslog_stream_lock
);
396 // Free the stream buffer
397 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
398 // Free the list entries
399 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
406 logread(__unused dev_t dev
, struct uio
*uio
, int flag
)
412 while (msgbufp
->msg_bufr
== msgbufp
->msg_bufx
) {
413 if (flag
& IO_NDELAY
) {
417 if (logsoftc
.sc_state
& LOG_NBIO
) {
421 logsoftc
.sc_state
|= LOG_RDWAIT
;
424 * If the wakeup is missed
425 * then wait for 5 sec and reevaluate
427 if ((error
= tsleep((caddr_t
)msgbufp
, LOG_RDPRI
| PCATCH
,
428 "klog", 5 * hz
)) != 0) {
429 /* if it times out; ignore */
430 if (error
!= EWOULDBLOCK
)
435 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
437 while (uio_resid(uio
) > 0) {
440 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
442 l
= msgbufp
->msg_size
- msgbufp
->msg_bufr
;
443 l
= min(l
, uio_resid(uio
));
447 readpos
= msgbufp
->msg_bufr
;
449 error
= uiomove((caddr_t
)&msgbufp
->msg_bufc
[readpos
],
454 msgbufp
->msg_bufr
= readpos
+ l
;
455 if (msgbufp
->msg_bufr
>= msgbufp
->msg_size
)
456 msgbufp
->msg_bufr
= 0;
465 oslog_streamread(__unused dev_t dev
, struct uio
*uio
, int flag
)
469 static char logline
[FIREHOSE_BUFFER_CHUNK_SIZE
];
471 lck_spin_lock(&oslog_stream_lock
);
473 if (!oslog_stream_open
) {
474 lck_spin_unlock(&oslog_stream_lock
);
478 while (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
479 if (flag
& IO_NDELAY
|| oslog_streamsoftc
.sc_state
& LOG_NBIO
) {
480 lck_spin_unlock(&oslog_stream_lock
);
484 oslog_streamsoftc
.sc_state
|= LOG_RDWAIT
;
485 wait_result_t wr
= assert_wait((event_t
)oslog_streambufp
,
486 THREAD_INTERRUPTIBLE
);
487 if (wr
== THREAD_WAITING
) {
488 lck_spin_unlock(&oslog_stream_lock
);
489 wr
= thread_block(THREAD_CONTINUE_NULL
);
490 lck_spin_lock(&oslog_stream_lock
);
494 case THREAD_AWAKENED
:
495 case THREAD_TIMED_OUT
:
498 lck_spin_unlock(&oslog_stream_lock
);
503 if (!oslog_stream_open
) {
504 lck_spin_unlock(&oslog_stream_lock
);
509 oslog_stream_buf_entry_t read_entry
= NULL
;
512 read_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
513 assert(read_entry
!= NULL
);
514 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
516 // Copy the timestamp first
517 memcpy(logline
+ logpos
, &read_entry
->timestamp
, sizeof(uint64_t));
518 logpos
+= sizeof(uint64_t);
520 switch (read_entry
->type
) {
521 /* Handle metadata messages */
522 case oslog_stream_link_type_metadata
:
524 memcpy(logline
+ logpos
,
525 (read_entry
->metadata
), read_entry
->size
);
526 logpos
+= read_entry
->size
;
528 lck_spin_unlock(&oslog_stream_lock
);
530 // Free the list entry
531 kfree(read_entry
, (sizeof(struct oslog_stream_buf_entry_s
) + read_entry
->size
));
534 /* Handle log messages */
535 case oslog_stream_link_type_log
:
537 /* ensure that the correct read entry was dequeued */
538 assert(read_entry
->offset
== oslog_streambufp
->msg_bufr
);
539 rec_length
= read_entry
->size
;
541 // If the next log line is contiguous in the buffer, copy it out.
542 if(read_entry
->offset
+ rec_length
<= oslog_streambufp
->msg_size
) {
543 memcpy(logline
+ logpos
,
544 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, rec_length
);
546 oslog_streambufp
->msg_bufr
+= rec_length
;
547 if (oslog_streambufp
->msg_bufr
== oslog_streambufp
->msg_size
) {
548 oslog_streambufp
->msg_bufr
= 0;
550 logpos
+= rec_length
;
552 // Otherwise, copy until the end of the buffer, and
553 // copy the remaining bytes starting at index 0.
554 int bytes_left
= oslog_streambufp
->msg_size
- read_entry
->offset
;
555 memcpy(logline
+ logpos
,
556 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, bytes_left
);
557 logpos
+= bytes_left
;
558 rec_length
-= bytes_left
;
560 memcpy(logline
+ logpos
, (const void *)oslog_streambufp
->msg_bufc
,
562 oslog_streambufp
->msg_bufr
= rec_length
;
563 logpos
+= rec_length
;
565 assert(oslog_streambufp
->msg_bufr
< oslog_streambufp
->msg_size
);
566 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, read_entry
, buf_entries
);
568 lck_spin_unlock(&oslog_stream_lock
);
573 panic("Got unexpected log entry type: %hhu\n", read_entry
->type
);
577 copy_size
= min(logpos
, uio_resid(uio
));
578 if (copy_size
!= 0) {
579 error
= uiomove((caddr_t
)logline
, copy_size
, uio
);
581 (void)hw_atomic_add(&oslog_s_streamed_msgcount
, 1);
588 logselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
594 if (msgbufp
->msg_bufr
!= msgbufp
->msg_bufx
) {
598 selrecord(p
, &logsoftc
.sc_selp
, wql
);
606 oslogselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
616 selrecord(p
, &oslogsoftc
.sc_selp
, wql
);
624 oslog_streamselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
628 lck_spin_lock(&oslog_stream_lock
);
632 if (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
633 selrecord(p
, &oslog_streamsoftc
.sc_selp
, wql
);
640 lck_spin_unlock(&oslog_stream_lock
);
649 /* cf. r24974766 & r25201228*/
650 if (oslog_is_safe() == FALSE
) {
659 selwakeup(&logsoftc
.sc_selp
);
660 if (logsoftc
.sc_state
& LOG_ASYNC
) {
661 pgid
= logsoftc
.sc_pgid
;
664 gsignal(-pgid
, SIGIO
);
666 proc_signal(pgid
, SIGIO
);
669 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
670 wakeup((caddr_t
)msgbufp
);
671 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
684 selwakeup(&oslogsoftc
.sc_selp
);
690 oslog_streamwakeup_locked(void)
692 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
693 if (!oslog_stream_open
) {
696 selwakeup(&oslog_streamsoftc
.sc_selp
);
697 if (oslog_streamsoftc
.sc_state
& LOG_RDWAIT
) {
698 wakeup((caddr_t
)oslog_streambufp
);
699 oslog_streamsoftc
.sc_state
&= ~LOG_RDWAIT
;
704 oslog_streamwakeup(void)
706 /* cf. r24974766 & r25201228*/
707 if (oslog_is_safe() == FALSE
) {
711 lck_spin_lock(&oslog_stream_lock
);
712 oslog_streamwakeup_locked();
713 lck_spin_unlock(&oslog_stream_lock
);
718 logioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
725 /* return number of characters immediately available */
727 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
729 l
+= msgbufp
->msg_size
;
735 logsoftc
.sc_state
|= LOG_NBIO
;
737 logsoftc
.sc_state
&= ~LOG_NBIO
;
742 logsoftc
.sc_state
|= LOG_ASYNC
;
744 logsoftc
.sc_state
&= ~LOG_ASYNC
;
748 logsoftc
.sc_pgid
= *(int *)data
;
752 *(int *)data
= logsoftc
.sc_pgid
;
765 oslogioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
768 mach_vm_size_t buffer_size
= (FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT
* FIREHOSE_BUFFER_CHUNK_SIZE
);
769 firehose_buffer_map_info_t map_info
= {0, 0};
770 firehose_buffer_t kernel_firehose_buffer
= NULL
;
771 mach_vm_address_t user_addr
= 0;
772 mach_port_t mem_entry_ptr
= MACH_PORT_NULL
;
776 /* return number of characters immediately available */
779 kernel_firehose_buffer
= kernel_firehose_addr
;
781 ret
= mach_make_memory_entry_64(kernel_map
,
783 (mach_vm_offset_t
) kernel_firehose_buffer
,
784 ( MAP_MEM_VM_SHARE
| VM_PROT_READ
),
787 if (ret
== KERN_SUCCESS
) {
788 ret
= mach_vm_map(get_task_map(current_task()),
801 if (ret
== KERN_SUCCESS
) {
802 map_info
.fbmi_addr
= (uint64_t) (user_addr
);
803 map_info
.fbmi_size
= buffer_size
;
804 bcopy(&map_info
, data
, sizeof(firehose_buffer_map_info_t
));
811 __firehose_merge_updates(*(firehose_push_reply_t
*)(data
));
821 oslog_streamioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
825 lck_spin_lock(&oslog_stream_lock
);
829 if (data
&& *(int *)data
)
830 oslog_streamsoftc
.sc_state
|= LOG_NBIO
;
832 oslog_streamsoftc
.sc_state
&= ~LOG_NBIO
;
835 if (data
&& *(int *)data
)
836 oslog_streamsoftc
.sc_state
|= LOG_ASYNC
;
838 oslog_streamsoftc
.sc_state
&= ~LOG_ASYNC
;
845 lck_spin_unlock(&oslog_stream_lock
);
852 /* After this point, we must be ready to accept characters */
859 vm_size_t size
= FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT
* FIREHOSE_BUFFER_CHUNK_SIZE
;
863 kr
= kmem_alloc_flags(kernel_map
, &kernel_firehose_addr
,
864 size
+ (2 * PAGE_SIZE
), VM_KERN_MEMORY_LOG
,
865 KMA_GUARD_FIRST
| KMA_GUARD_LAST
);
866 if (kr
!= KERN_SUCCESS
) {
867 panic("Failed to allocate memory for firehose logging buffer");
869 kernel_firehose_addr
+= PAGE_SIZE
;
870 bzero(kernel_firehose_addr
, size
);
871 /* register buffer with firehose */
872 kernel_firehose_addr
= __firehose_buffer_create((size_t *) &size
);
874 kprintf("oslog_init completed\n");
880 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
883 * Parameters: c Character to output
887 * Notes: This functions is used for multibyte output to the log; it
888 * should be used preferrentially where possible to ensure that
889 * log entries do not end up interspersed due to preemption or
893 log_putc_locked(char c
)
898 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
899 if (mbp
->msg_bufx
>= msgbufp
->msg_size
)
903 static oslog_stream_buf_entry_t
904 oslog_stream_find_free_buf_entry_locked(void)
907 oslog_stream_buf_entry_t buf_entry
= NULL
;
909 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
911 mbp
= oslog_streambufp
;
913 buf_entry
= STAILQ_FIRST(&oslog_stream_free_head
);
915 STAILQ_REMOVE_HEAD(&oslog_stream_free_head
, buf_entries
);
918 // If no list elements are available in the free-list,
919 // consume the next log line so we can free up its list element
920 oslog_stream_buf_entry_t prev_entry
= NULL
;
922 buf_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
923 while (buf_entry
->type
== oslog_stream_link_type_metadata
) {
924 prev_entry
= buf_entry
;
925 buf_entry
= STAILQ_NEXT(buf_entry
, buf_entries
);
928 if (prev_entry
== NULL
) {
929 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
932 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
935 mbp
->msg_bufr
+= buf_entry
->size
;
936 oslog_s_dropped_msgcount
++;
937 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
938 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
946 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
)
948 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
949 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, m_entry
, buf_entries
);
954 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
)
958 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
960 mbp
= oslog_streambufp
;
961 // Check if we have enough space in the stream buffer to write the data
962 if (mbp
->msg_bufx
+ buflen
<= mbp
->msg_size
) {
963 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, buflen
);
965 mbp
->msg_bufx
+= buflen
;
966 if (mbp
->msg_bufx
== mbp
->msg_size
) {
970 // Copy part of the data until the end of the stream
971 int bytes_left
= mbp
->msg_size
- mbp
->msg_bufx
;
972 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, bytes_left
);
974 buflen
-= bytes_left
;
975 buffer
+= bytes_left
;
977 // Copy the remainder of the data from the beginning of stream
978 memcpy((void *)mbp
->msg_bufc
, buffer
, buflen
);
979 mbp
->msg_bufx
= buflen
;
986 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
987 uint64_t stamp
, const void *pubdata
, size_t publen
)
990 int available_space
= 0;
991 oslog_stream_buf_entry_t buf_entry
= NULL
;
992 oslog_stream_buf_entry_t next_entry
= NULL
;
994 uint16_t ft_size
= offsetof(struct firehose_tracepoint_s
, ft_data
);
995 int ft_length
= ft_size
+ publen
;
997 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
999 mbp
= oslog_streambufp
;
1000 if (ft_length
> mbp
->msg_size
) {
1001 (void)hw_atomic_add(&oslog_s_error_count
, 1);
1005 // Ensure that we have a list element for this record
1006 buf_entry
= oslog_stream_find_free_buf_entry_locked();
1008 assert(buf_entry
!= NULL
);
1010 // Ensure that we have space in the ring buffer for the current logline
1011 if (mbp
->msg_bufr
> mbp
->msg_bufx
) {
1012 available_space
= mbp
->msg_bufr
- mbp
->msg_bufx
;
1014 available_space
= mbp
->msg_size
- mbp
->msg_bufx
+ mbp
->msg_bufr
;
1016 while(ft_length
> available_space
) {
1017 oslog_stream_buf_entry_t prev_entry
= NULL
;
1019 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
1020 assert(next_entry
!= NULL
);
1021 while (next_entry
->type
== oslog_stream_link_type_metadata
) {
1022 prev_entry
= next_entry
;
1023 next_entry
= STAILQ_NEXT(next_entry
, buf_entries
);
1026 if (prev_entry
== NULL
) {
1027 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
1030 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
1033 mbp
->msg_bufr
+= next_entry
->size
;
1034 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
1035 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
1038 oslog_s_dropped_msgcount
++;
1039 available_space
+= next_entry
->size
;
1041 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, next_entry
, buf_entries
);
1044 assert(ft_length
<= available_space
);
1046 // Write the log line and update the list entry for this record
1047 buf_entry
->offset
= mbp
->msg_bufx
;
1048 buf_entry
->size
= ft_length
;
1049 buf_entry
->timestamp
= stamp
;
1050 buf_entry
->type
= oslog_stream_link_type_log
;
1052 // Construct a tracepoint
1053 struct firehose_tracepoint_s fs
= {
1054 .ft_thread
= thread_tid(current_thread()),
1055 .ft_id
.ftid_value
= ftid
.ftid_value
,
1059 oslog_streamwrite_append_bytes((char *)&fs
, sizeof(fs
));
1060 oslog_streamwrite_append_bytes(pubdata
, publen
);
1062 assert(mbp
->msg_bufr
< mbp
->msg_size
);
1063 // Insert the element to the buffer data list
1064 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, buf_entry
, buf_entries
);
1074 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1075 * held by the caller.
1077 * Parameters: c Character to output
1081 * Notes: This function is used for syingle byte output to the log. It
1082 * primarily exists to maintain binary backward compatibility.
1087 int unread_count
= 0;
1090 unread_count
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
1093 if (unread_count
< 0)
1094 unread_count
= 0 - unread_count
;
1095 if (c
== '\n' || unread_count
>= MAX_UNREAD_CHARS
)
1101 * it is possible to increase the kernel log buffer size by adding
1103 * to the kernel command line, and to read the current size using
1104 * sysctl kern.msgbuf
1105 * If there is no parameter on the kernel command line, the buffer is
1106 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1107 * memory is dynamically allocated. Memory management must already be up.
1110 log_setsize(int size
) {
1112 int new_logsize
, new_bufr
, new_bufx
;
1114 int old_logsize
, old_bufr
, old_bufx
;
1118 if (size
> MAX_MSG_BSIZE
)
1125 if (!(new_logdata
= (char*)kalloc(size
))) {
1126 printf("log_setsize: unable to allocate memory\n");
1129 bzero(new_logdata
, new_logsize
);
1133 old_logsize
= msgbufp
->msg_size
;
1134 old_logdata
= msgbufp
->msg_bufc
;
1135 old_bufr
= msgbufp
->msg_bufr
;
1136 old_bufx
= msgbufp
->msg_bufx
;
1138 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1139 size
, old_logdata
, old_logsize
, old_bufr
, old_bufx
);
1141 /* start "new_logsize" bytes before the write pointer */
1142 if (new_logsize
<= old_bufx
) {
1143 count
= new_logsize
;
1144 p
= old_logdata
+ old_bufx
- count
;
1147 * if new buffer is bigger, copy what we have and let the
1148 * bzero above handle the difference
1150 count
= MIN(new_logsize
, old_logsize
);
1151 p
= old_logdata
+ old_logsize
- (count
- old_bufx
);
1153 for (i
= 0; i
< count
; i
++) {
1154 if (p
>= old_logdata
+ old_logsize
)
1158 new_logdata
[i
] = ch
;
1162 if (new_bufx
>= new_logsize
)
1164 msgbufp
->msg_bufx
= new_bufx
;
1166 new_bufr
= old_bufx
- old_bufr
; /* how much were we trailing bufx by? */
1168 new_bufr
+= old_logsize
;
1169 new_bufr
= new_bufx
- new_bufr
; /* now relative to oldest data in new buffer */
1171 new_bufr
+= new_logsize
;
1172 msgbufp
->msg_bufr
= new_bufr
;
1174 msgbufp
->msg_size
= new_logsize
;
1175 msgbufp
->msg_bufc
= new_logdata
;
1177 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1178 size
, new_logdata
, new_logsize
, new_bufr
, new_bufx
);
1182 /* this memory is now dead - clear it so that it compresses better
1183 in case of suspend to disk etc. */
1184 bzero(old_logdata
, old_logsize
);
1185 if (old_logdata
!= smsg_bufc
) {
1186 /* dynamic memory that must be freed */
1187 kfree(old_logdata
, old_logsize
);
1190 printf("set system log size to %d bytes\n", new_logsize
);
1195 void oslog_setsize(int size
)
1198 // If the size is less than the default stream buffer
1200 if (size
<= OSLOG_STREAM_BUF_SIZE
) {
1204 scale
= (uint16_t) (size
/ OSLOG_STREAM_BUF_SIZE
);
1206 oslog_stream_buf_size
= size
;
1207 oslog_stream_num_entries
= scale
* OSLOG_NUM_STREAM_ENTRIES
;
1208 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size
, oslog_stream_num_entries
);
1211 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0, sysctl_kern_msgbuf
, "I", "");
1213 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp __unused
,
1214 void *arg1 __unused
,
1216 struct sysctl_req
*req
)
1218 int old_bufsize
, bufsize
;
1222 old_bufsize
= bufsize
= msgbufp
->msg_size
;
1225 error
= sysctl_io_number(req
, bufsize
, sizeof(bufsize
), &bufsize
, NULL
);
1229 if (bufsize
!= old_bufsize
) {
1230 error
= log_setsize(bufsize
);
1238 * This should be called by /sbin/dmesg only via libproc.
1239 * It returns as much data still in the buffer as possible.
1242 log_dmesg(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
) {
1244 uint32_t localbuff_size
;
1245 int error
= 0, newl
, skip
;
1246 char *localbuff
, *p
, *copystart
, ch
;
1250 localbuff_size
= (msgbufp
->msg_size
+ 2); /* + '\n' + '\0' */
1253 /* Allocate a temporary non-circular buffer for copyout */
1254 if (!(localbuff
= (char *)kalloc(localbuff_size
))) {
1255 printf("log_dmesg: unable to allocate memory\n");
1259 /* in between here, the log could become bigger, but that's fine */
1263 * The message buffer is circular; start at the write pointer, and
1264 * make one loop up to write pointer - 1.
1266 p
= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
;
1267 for (i
= newl
= skip
= 0; p
!= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
- 1; ++p
) {
1268 if (p
>= msgbufp
->msg_bufc
+ msgbufp
->msg_size
)
1269 p
= msgbufp
->msg_bufc
;
1271 /* Skip "\n<.*>" syslog sequences. */
1277 if (newl
&& ch
== '<') {
1283 newl
= (ch
== '\n');
1284 localbuff
[i
++] = ch
;
1285 /* The original version of this routine contained a buffer
1286 * overflow. At the time, a "small" targeted fix was desired
1287 * so the change below to check the buffer bounds was made.
1288 * TODO: rewrite this needlessly convoluted routine.
1290 if (i
== (localbuff_size
- 2))
1294 localbuff
[i
++] = '\n';
1297 if (buffersize
>= i
) {
1298 copystart
= localbuff
;
1301 copystart
= localbuff
+ i
- buffersize
;
1302 copysize
= buffersize
;
1307 error
= copyout(copystart
, buffer
, copysize
);
1311 kfree(localbuff
, localbuff_size
);