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/chunk_private.h>
75 #include <firehose/ioctl_private.h>
76 #include <os/firehose_buffer_private.h>
78 #include <os/log_private.h>
79 #include <sys/ioctl.h>
80 #include <sys/msgbuf.h>
81 #include <sys/file_internal.h>
82 #include <sys/errno.h>
83 #include <sys/select.h>
84 #include <sys/kernel.h>
85 #include <kern/thread.h>
86 #include <kern/sched_prim.h>
87 #include <kern/simple_lock.h>
89 #include <sys/signalvar.h>
91 #include <sys/sysctl.h>
92 #include <sys/queue.h>
93 #include <kern/kalloc.h>
94 #include <pexpert/pexpert.h>
95 #include <mach/mach_port.h>
96 #include <mach/mach_vm.h>
97 #include <mach/vm_map.h>
98 #include <vm/vm_kern.h>
99 #include <kern/task.h>
100 #include <kern/locks.h>
102 /* XXX should be in a common header somewhere */
103 extern void logwakeup(void);
104 extern void oslogwakeup(void);
105 extern void oslog_streamwakeup(void);
106 static void oslog_streamwakeup_locked(void);
107 vm_offset_t kernel_firehose_addr
= 0;
109 /* log message counters for streaming mode */
110 uint32_t oslog_s_streamed_msgcount
= 0;
111 uint32_t oslog_s_dropped_msgcount
= 0;
112 extern uint32_t oslog_s_error_count
;
114 #define LOG_RDPRI (PZERO + 1)
116 #define LOG_NBIO 0x02
117 #define LOG_ASYNC 0x04
118 #define LOG_RDWAIT 0x08
120 #define MAX_UNREAD_CHARS (CONFIG_MSG_BSIZE/2)
121 /* All globals should be accessed under LOG_LOCK() */
123 /* logsoftc only valid while log_open=1 */
125 int sc_state
; /* see above for possibilities */
126 struct selinfo sc_selp
; /* thread waiting for select */
127 int sc_pgid
; /* process/group for async I/O */
130 int log_open
; /* also used in log() */
131 char smsg_bufc
[CONFIG_MSG_BSIZE
]; /* static buffer */
132 char oslog_stream_bufc
[FIREHOSE_CHUNK_SIZE
]; /* static buffer */
133 struct firehose_chunk_s oslog_boot_buf
= {
135 .fcp_next_entry_offs
= offsetof(struct firehose_chunk_s
, fc_data
),
136 .fcp_private_offs
= FIREHOSE_CHUNK_SIZE
,
137 .fcp_refcnt
= 1, // indicate that there is a writer to this chunk
138 .fcp_stream
= firehose_stream_persist
,
139 .fcp_flag_io
= 1, // for now, lets assume this is coming from the io bank
141 }; /* static buffer */
142 firehose_chunk_t firehose_boot_chunk
= &oslog_boot_buf
;
143 struct msgbuf msgbuf
= {MSG_MAGIC
,sizeof(smsg_bufc
),0,0,smsg_bufc
};
144 struct msgbuf oslog_stream_buf
= {MSG_MAGIC
,0,0,0,NULL
};
145 struct msgbuf
*msgbufp
__attribute__((used
)) = &msgbuf
;
146 struct msgbuf
*oslog_streambufp
__attribute__((used
)) = &oslog_stream_buf
;
148 // List entries for keeping track of the streaming buffer
149 static oslog_stream_buf_entry_t oslog_stream_buf_entries
;
151 #define OSLOG_NUM_STREAM_ENTRIES 64
152 #define OSLOG_STREAM_BUF_SIZE 4096
155 int os_log_wakeup
= 0;
156 int oslog_stream_open
= 0;
157 int oslog_stream_buf_size
= OSLOG_STREAM_BUF_SIZE
;
158 int oslog_stream_num_entries
= OSLOG_NUM_STREAM_ENTRIES
;
160 /* oslogsoftc only valid while oslog_open=1 */
162 int sc_state
; /* see above for possibilities */
163 struct selinfo sc_selp
; /* thread waiting for select */
164 int sc_pgid
; /* process/group for async I/O */
167 struct oslog_streamsoftc
{
168 int sc_state
; /* see above for possibilities */
169 struct selinfo sc_selp
; /* thread waiting for select */
170 int sc_pgid
; /* process/group for async I/O */
173 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_free_head
=
174 STAILQ_HEAD_INITIALIZER(oslog_stream_free_head
);
175 STAILQ_HEAD(, oslog_stream_buf_entry_s
) oslog_stream_buf_head
=
176 STAILQ_HEAD_INITIALIZER(oslog_stream_buf_head
);
178 /* defined in osfmk/kern/printf.c */
179 extern void oslog_lock_init(void);
180 extern void bsd_log_lock(void);
181 extern void bsd_log_unlock(void);
183 /* defined for osfmk/kern/printf.c */
184 void bsd_log_init(void);
187 * Ideally this file would define this lock, but bsd doesn't have the definition
190 decl_lck_spin_data(extern, oslog_stream_lock
)
192 /* XXX wants a linker set so these can be static */
193 extern d_open_t logopen
;
194 extern d_close_t logclose
;
195 extern d_read_t logread
;
196 extern d_ioctl_t logioctl
;
197 extern d_select_t logselect
;
199 /* XXX wants a linker set so these can be static */
200 extern d_open_t oslogopen
;
201 extern d_close_t oslogclose
;
202 extern d_select_t oslogselect
;
203 extern d_ioctl_t oslogioctl
;
205 /* XXX wants a linker set so these can be static */
206 extern d_open_t oslog_streamopen
;
207 extern d_close_t oslog_streamclose
;
208 extern d_read_t oslog_streamread
;
209 extern d_ioctl_t oslog_streamioctl
;
210 extern d_select_t oslog_streamselect
;
212 void oslog_init(void);
213 void oslog_setsize(int size
);
214 void oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
215 uint64_t stamp
, const void *pubdata
, size_t publen
);
216 void oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
);
217 static oslog_stream_buf_entry_t
oslog_stream_find_free_buf_entry_locked(void);
218 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
);
221 * Serialize log access. Note that the log can be written at interrupt level,
222 * so any log manipulations that can be done from, or affect, another processor
223 * at interrupt level must be guarded with a spin lock.
226 #define LOG_LOCK() bsd_log_lock()
227 #define LOG_UNLOCK() bsd_log_unlock()
230 #define LOG_SETSIZE_DEBUG(x...) kprintf(x)
232 #define LOG_SETSIZE_DEBUG(x...) do { } while(0)
235 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp
,
238 struct sysctl_req
*req
);
242 logopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
249 logsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
259 logclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
262 logsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
263 selwakeup(&logsoftc
.sc_selp
);
264 selthreadclear(&logsoftc
.sc_selp
);
272 oslogopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
279 oslogsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
287 oslogclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
290 oslogsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
291 selwakeup(&oslogsoftc
.sc_selp
);
292 selthreadclear(&oslogsoftc
.sc_selp
);
299 oslog_streamopen(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, struct proc
*p
)
301 char *oslog_stream_msg_bufc
= NULL
;
302 oslog_stream_buf_entry_t entries
= NULL
;
304 lck_spin_lock(&oslog_stream_lock
);
305 if (oslog_stream_open
) {
306 lck_spin_unlock(&oslog_stream_lock
);
309 lck_spin_unlock(&oslog_stream_lock
);
311 // Allocate the stream buffer
312 oslog_stream_msg_bufc
= kalloc(oslog_stream_buf_size
);
313 if (!oslog_stream_msg_bufc
) {
317 /* entries to support kernel logging in stream mode */
318 entries
= kalloc(oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
320 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
324 lck_spin_lock(&oslog_stream_lock
);
325 if (oslog_stream_open
) {
326 lck_spin_unlock(&oslog_stream_lock
);
327 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
328 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
332 assert(oslog_streambufp
->msg_bufc
== NULL
);
333 oslog_streambufp
->msg_bufc
= oslog_stream_msg_bufc
;
334 oslog_streambufp
->msg_size
= oslog_stream_buf_size
;
336 oslog_stream_buf_entries
= entries
;
338 STAILQ_INIT(&oslog_stream_free_head
);
339 STAILQ_INIT(&oslog_stream_buf_head
);
341 for (int i
= 0; i
< oslog_stream_num_entries
; i
++) {
342 oslog_stream_buf_entries
[i
].type
= oslog_stream_link_type_log
;
343 oslog_stream_buf_entries
[i
].offset
= 0;
344 oslog_stream_buf_entries
[i
].size
= 0;
345 oslog_stream_buf_entries
[i
].timestamp
= 0;
346 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, &oslog_stream_buf_entries
[i
], buf_entries
);
349 /* there should be no pending entries in the stream */
350 assert(STAILQ_EMPTY(&oslog_stream_buf_head
));
351 assert(oslog_streambufp
->msg_bufx
== 0);
352 assert(oslog_streambufp
->msg_bufr
== 0);
354 oslog_streambufp
->msg_bufx
= 0;
355 oslog_streambufp
->msg_bufr
= 0;
356 oslog_streamsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
357 oslog_stream_open
= 1;
358 lck_spin_unlock(&oslog_stream_lock
);
364 oslog_streamclose(__unused dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused
struct proc
*p
)
366 oslog_stream_buf_entry_t next_entry
= NULL
;
367 char *oslog_stream_msg_bufc
= NULL
;
368 oslog_stream_buf_entry_t entries
= NULL
;
370 lck_spin_lock(&oslog_stream_lock
);
372 if (oslog_stream_open
== 0) {
373 lck_spin_unlock(&oslog_stream_lock
);
377 // Consume all log lines
378 while (!STAILQ_EMPTY(&oslog_stream_buf_head
)) {
379 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
380 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
382 oslog_streamwakeup_locked();
383 oslog_streamsoftc
.sc_state
&= ~(LOG_NBIO
| LOG_ASYNC
);
384 selwakeup(&oslog_streamsoftc
.sc_selp
);
385 selthreadclear(&oslog_streamsoftc
.sc_selp
);
386 oslog_stream_open
= 0;
387 oslog_streambufp
->msg_bufr
= 0;
388 oslog_streambufp
->msg_bufx
= 0;
389 oslog_stream_msg_bufc
= oslog_streambufp
->msg_bufc
;
390 oslog_streambufp
->msg_bufc
= NULL
;
391 entries
= oslog_stream_buf_entries
;
392 oslog_stream_buf_entries
= NULL
;
393 oslog_streambufp
->msg_size
= 0;
395 lck_spin_unlock(&oslog_stream_lock
);
397 // Free the stream buffer
398 kfree(oslog_stream_msg_bufc
, oslog_stream_buf_size
);
399 // Free the list entries
400 kfree(entries
, oslog_stream_num_entries
* sizeof(struct oslog_stream_buf_entry_s
));
407 logread(__unused dev_t dev
, struct uio
*uio
, int flag
)
413 while (msgbufp
->msg_bufr
== msgbufp
->msg_bufx
) {
414 if (flag
& IO_NDELAY
) {
418 if (logsoftc
.sc_state
& LOG_NBIO
) {
422 logsoftc
.sc_state
|= LOG_RDWAIT
;
425 * If the wakeup is missed
426 * then wait for 5 sec and reevaluate
428 if ((error
= tsleep((caddr_t
)msgbufp
, LOG_RDPRI
| PCATCH
,
429 "klog", 5 * hz
)) != 0) {
430 /* if it times out; ignore */
431 if (error
!= EWOULDBLOCK
)
436 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
438 while (uio_resid(uio
) > 0) {
441 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
443 l
= msgbufp
->msg_size
- msgbufp
->msg_bufr
;
444 l
= min(l
, uio_resid(uio
));
448 readpos
= msgbufp
->msg_bufr
;
450 error
= uiomove((caddr_t
)&msgbufp
->msg_bufc
[readpos
],
455 msgbufp
->msg_bufr
= readpos
+ l
;
456 if (msgbufp
->msg_bufr
>= msgbufp
->msg_size
)
457 msgbufp
->msg_bufr
= 0;
466 oslog_streamread(__unused dev_t dev
, struct uio
*uio
, int flag
)
470 static char logline
[FIREHOSE_CHUNK_SIZE
];
472 lck_spin_lock(&oslog_stream_lock
);
474 if (!oslog_stream_open
) {
475 lck_spin_unlock(&oslog_stream_lock
);
479 while (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
480 if (flag
& IO_NDELAY
|| oslog_streamsoftc
.sc_state
& LOG_NBIO
) {
481 lck_spin_unlock(&oslog_stream_lock
);
485 oslog_streamsoftc
.sc_state
|= LOG_RDWAIT
;
486 wait_result_t wr
= assert_wait((event_t
)oslog_streambufp
,
487 THREAD_INTERRUPTIBLE
);
488 if (wr
== THREAD_WAITING
) {
489 lck_spin_unlock(&oslog_stream_lock
);
490 wr
= thread_block(THREAD_CONTINUE_NULL
);
491 lck_spin_lock(&oslog_stream_lock
);
495 case THREAD_AWAKENED
:
496 case THREAD_TIMED_OUT
:
499 lck_spin_unlock(&oslog_stream_lock
);
504 if (!oslog_stream_open
) {
505 lck_spin_unlock(&oslog_stream_lock
);
510 oslog_stream_buf_entry_t read_entry
= NULL
;
513 read_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
514 assert(read_entry
!= NULL
);
515 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
517 // Copy the timestamp first
518 memcpy(logline
+ logpos
, &read_entry
->timestamp
, sizeof(uint64_t));
519 logpos
+= sizeof(uint64_t);
521 switch (read_entry
->type
) {
522 /* Handle metadata messages */
523 case oslog_stream_link_type_metadata
:
525 memcpy(logline
+ logpos
,
526 (read_entry
->metadata
), read_entry
->size
);
527 logpos
+= read_entry
->size
;
529 lck_spin_unlock(&oslog_stream_lock
);
531 // Free the list entry
532 kfree(read_entry
, (sizeof(struct oslog_stream_buf_entry_s
) + read_entry
->size
));
535 /* Handle log messages */
536 case oslog_stream_link_type_log
:
538 /* ensure that the correct read entry was dequeued */
539 assert(read_entry
->offset
== oslog_streambufp
->msg_bufr
);
540 rec_length
= read_entry
->size
;
542 // If the next log line is contiguous in the buffer, copy it out.
543 if(read_entry
->offset
+ rec_length
<= oslog_streambufp
->msg_size
) {
544 memcpy(logline
+ logpos
,
545 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, rec_length
);
547 oslog_streambufp
->msg_bufr
+= rec_length
;
548 if (oslog_streambufp
->msg_bufr
== oslog_streambufp
->msg_size
) {
549 oslog_streambufp
->msg_bufr
= 0;
551 logpos
+= rec_length
;
553 // Otherwise, copy until the end of the buffer, and
554 // copy the remaining bytes starting at index 0.
555 int bytes_left
= oslog_streambufp
->msg_size
- read_entry
->offset
;
556 memcpy(logline
+ logpos
,
557 oslog_streambufp
->msg_bufc
+ read_entry
->offset
, bytes_left
);
558 logpos
+= bytes_left
;
559 rec_length
-= bytes_left
;
561 memcpy(logline
+ logpos
, (const void *)oslog_streambufp
->msg_bufc
,
563 oslog_streambufp
->msg_bufr
= rec_length
;
564 logpos
+= rec_length
;
566 assert(oslog_streambufp
->msg_bufr
< oslog_streambufp
->msg_size
);
567 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, read_entry
, buf_entries
);
569 lck_spin_unlock(&oslog_stream_lock
);
574 panic("Got unexpected log entry type: %hhu\n", read_entry
->type
);
578 copy_size
= min(logpos
, uio_resid(uio
));
579 if (copy_size
!= 0) {
580 error
= uiomove((caddr_t
)logline
, copy_size
, uio
);
582 (void)hw_atomic_add(&oslog_s_streamed_msgcount
, 1);
589 logselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
595 if (msgbufp
->msg_bufr
!= msgbufp
->msg_bufx
) {
599 selrecord(p
, &logsoftc
.sc_selp
, wql
);
607 oslogselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
617 selrecord(p
, &oslogsoftc
.sc_selp
, wql
);
625 oslog_streamselect(__unused dev_t dev
, int rw
, void * wql
, struct proc
*p
)
629 lck_spin_lock(&oslog_stream_lock
);
633 if (STAILQ_EMPTY(&oslog_stream_buf_head
)) {
634 selrecord(p
, &oslog_streamsoftc
.sc_selp
, wql
);
641 lck_spin_unlock(&oslog_stream_lock
);
650 /* cf. r24974766 & r25201228*/
651 if (oslog_is_safe() == FALSE
) {
660 selwakeup(&logsoftc
.sc_selp
);
661 if (logsoftc
.sc_state
& LOG_ASYNC
) {
662 pgid
= logsoftc
.sc_pgid
;
665 gsignal(-pgid
, SIGIO
);
667 proc_signal(pgid
, SIGIO
);
670 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
671 wakeup((caddr_t
)msgbufp
);
672 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
685 selwakeup(&oslogsoftc
.sc_selp
);
691 oslog_streamwakeup_locked(void)
693 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
694 if (!oslog_stream_open
) {
697 selwakeup(&oslog_streamsoftc
.sc_selp
);
698 if (oslog_streamsoftc
.sc_state
& LOG_RDWAIT
) {
699 wakeup((caddr_t
)oslog_streambufp
);
700 oslog_streamsoftc
.sc_state
&= ~LOG_RDWAIT
;
705 oslog_streamwakeup(void)
707 /* cf. r24974766 & r25201228*/
708 if (oslog_is_safe() == FALSE
) {
712 lck_spin_lock(&oslog_stream_lock
);
713 oslog_streamwakeup_locked();
714 lck_spin_unlock(&oslog_stream_lock
);
719 logioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
726 /* return number of characters immediately available */
728 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
730 l
+= msgbufp
->msg_size
;
736 logsoftc
.sc_state
|= LOG_NBIO
;
738 logsoftc
.sc_state
&= ~LOG_NBIO
;
743 logsoftc
.sc_state
|= LOG_ASYNC
;
745 logsoftc
.sc_state
&= ~LOG_ASYNC
;
749 logsoftc
.sc_pgid
= *(int *)data
;
753 *(int *)data
= logsoftc
.sc_pgid
;
766 oslogioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
769 mach_vm_size_t buffer_size
= (FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT
* FIREHOSE_CHUNK_SIZE
);
770 firehose_buffer_map_info_t map_info
= {0, 0};
771 firehose_buffer_t kernel_firehose_buffer
= NULL
;
772 mach_vm_address_t user_addr
= 0;
773 mach_port_t mem_entry_ptr
= MACH_PORT_NULL
;
777 /* return number of characters immediately available */
780 kernel_firehose_buffer
= (firehose_buffer_t
)kernel_firehose_addr
;
782 ret
= mach_make_memory_entry_64(kernel_map
,
784 (mach_vm_offset_t
) kernel_firehose_buffer
,
785 ( MAP_MEM_VM_SHARE
| VM_PROT_READ
),
788 if (ret
== KERN_SUCCESS
) {
789 ret
= mach_vm_map_kernel(get_task_map(current_task()),
803 if (ret
== KERN_SUCCESS
) {
804 map_info
.fbmi_addr
= (uint64_t) (user_addr
);
805 map_info
.fbmi_size
= buffer_size
;
806 bcopy(&map_info
, data
, sizeof(firehose_buffer_map_info_t
));
813 __firehose_merge_updates(*(firehose_push_reply_t
*)(data
));
823 oslog_streamioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
827 lck_spin_lock(&oslog_stream_lock
);
831 if (data
&& *(int *)data
)
832 oslog_streamsoftc
.sc_state
|= LOG_NBIO
;
834 oslog_streamsoftc
.sc_state
&= ~LOG_NBIO
;
837 if (data
&& *(int *)data
)
838 oslog_streamsoftc
.sc_state
|= LOG_ASYNC
;
840 oslog_streamsoftc
.sc_state
&= ~LOG_ASYNC
;
847 lck_spin_unlock(&oslog_stream_lock
);
854 /* After this point, we must be ready to accept characters */
861 vm_size_t size
= FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT
* FIREHOSE_CHUNK_SIZE
;
865 kr
= kmem_alloc_flags(kernel_map
, &kernel_firehose_addr
,
866 size
+ (2 * PAGE_SIZE
), VM_KERN_MEMORY_LOG
,
867 KMA_GUARD_FIRST
| KMA_GUARD_LAST
);
868 if (kr
!= KERN_SUCCESS
) {
869 panic("Failed to allocate memory for firehose logging buffer");
871 kernel_firehose_addr
+= PAGE_SIZE
;
872 bzero((void *)kernel_firehose_addr
, size
);
873 /* register buffer with firehose */
874 kernel_firehose_addr
= (vm_offset_t
)__firehose_buffer_create((size_t *) &size
);
876 kprintf("oslog_init completed\n");
882 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
885 * Parameters: c Character to output
889 * Notes: This functions is used for multibyte output to the log; it
890 * should be used preferrentially where possible to ensure that
891 * log entries do not end up interspersed due to preemption or
895 log_putc_locked(char c
)
900 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
901 if (mbp
->msg_bufx
>= msgbufp
->msg_size
)
905 static oslog_stream_buf_entry_t
906 oslog_stream_find_free_buf_entry_locked(void)
909 oslog_stream_buf_entry_t buf_entry
= NULL
;
911 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
913 mbp
= oslog_streambufp
;
915 buf_entry
= STAILQ_FIRST(&oslog_stream_free_head
);
917 STAILQ_REMOVE_HEAD(&oslog_stream_free_head
, buf_entries
);
920 // If no list elements are available in the free-list,
921 // consume the next log line so we can free up its list element
922 oslog_stream_buf_entry_t prev_entry
= NULL
;
924 buf_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
925 while (buf_entry
->type
== oslog_stream_link_type_metadata
) {
926 prev_entry
= buf_entry
;
927 buf_entry
= STAILQ_NEXT(buf_entry
, buf_entries
);
930 if (prev_entry
== NULL
) {
931 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
934 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
937 mbp
->msg_bufr
+= buf_entry
->size
;
938 oslog_s_dropped_msgcount
++;
939 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
940 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
948 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
)
950 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
951 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, m_entry
, buf_entries
);
956 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
)
960 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
962 mbp
= oslog_streambufp
;
963 // Check if we have enough space in the stream buffer to write the data
964 if (mbp
->msg_bufx
+ buflen
<= mbp
->msg_size
) {
965 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, buflen
);
967 mbp
->msg_bufx
+= buflen
;
968 if (mbp
->msg_bufx
== mbp
->msg_size
) {
972 // Copy part of the data until the end of the stream
973 int bytes_left
= mbp
->msg_size
- mbp
->msg_bufx
;
974 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, bytes_left
);
976 buflen
-= bytes_left
;
977 buffer
+= bytes_left
;
979 // Copy the remainder of the data from the beginning of stream
980 memcpy((void *)mbp
->msg_bufc
, buffer
, buflen
);
981 mbp
->msg_bufx
= buflen
;
988 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
989 uint64_t stamp
, const void *pubdata
, size_t publen
)
992 int available_space
= 0;
993 oslog_stream_buf_entry_t buf_entry
= NULL
;
994 oslog_stream_buf_entry_t next_entry
= NULL
;
996 uint16_t ft_size
= offsetof(struct firehose_tracepoint_s
, ft_data
);
997 int ft_length
= ft_size
+ publen
;
999 LCK_SPIN_ASSERT(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1001 mbp
= oslog_streambufp
;
1002 if (ft_length
> mbp
->msg_size
) {
1003 (void)hw_atomic_add(&oslog_s_error_count
, 1);
1007 // Ensure that we have a list element for this record
1008 buf_entry
= oslog_stream_find_free_buf_entry_locked();
1010 assert(buf_entry
!= NULL
);
1012 // Ensure that we have space in the ring buffer for the current logline
1013 if (mbp
->msg_bufr
> mbp
->msg_bufx
) {
1014 available_space
= mbp
->msg_bufr
- mbp
->msg_bufx
;
1016 available_space
= mbp
->msg_size
- mbp
->msg_bufx
+ mbp
->msg_bufr
;
1018 while(ft_length
> available_space
) {
1019 oslog_stream_buf_entry_t prev_entry
= NULL
;
1021 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
1022 assert(next_entry
!= NULL
);
1023 while (next_entry
->type
== oslog_stream_link_type_metadata
) {
1024 prev_entry
= next_entry
;
1025 next_entry
= STAILQ_NEXT(next_entry
, buf_entries
);
1028 if (prev_entry
== NULL
) {
1029 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
1032 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
1035 mbp
->msg_bufr
+= next_entry
->size
;
1036 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
1037 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
1040 oslog_s_dropped_msgcount
++;
1041 available_space
+= next_entry
->size
;
1043 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, next_entry
, buf_entries
);
1046 assert(ft_length
<= available_space
);
1048 // Write the log line and update the list entry for this record
1049 buf_entry
->offset
= mbp
->msg_bufx
;
1050 buf_entry
->size
= ft_length
;
1051 buf_entry
->timestamp
= stamp
;
1052 buf_entry
->type
= oslog_stream_link_type_log
;
1054 // Construct a tracepoint
1055 struct firehose_tracepoint_s fs
= {
1056 .ft_thread
= thread_tid(current_thread()),
1057 .ft_id
.ftid_value
= ftid
.ftid_value
,
1061 oslog_streamwrite_append_bytes((char *)&fs
, sizeof(fs
));
1062 oslog_streamwrite_append_bytes(pubdata
, publen
);
1064 assert(mbp
->msg_bufr
< mbp
->msg_size
);
1065 // Insert the element to the buffer data list
1066 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, buf_entry
, buf_entries
);
1076 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1077 * held by the caller.
1079 * Parameters: c Character to output
1083 * Notes: This function is used for syingle byte output to the log. It
1084 * primarily exists to maintain binary backward compatibility.
1089 int unread_count
= 0;
1092 unread_count
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
1095 if (unread_count
< 0)
1096 unread_count
= 0 - unread_count
;
1097 if (c
== '\n' || unread_count
>= MAX_UNREAD_CHARS
)
1103 * it is possible to increase the kernel log buffer size by adding
1105 * to the kernel command line, and to read the current size using
1106 * sysctl kern.msgbuf
1107 * If there is no parameter on the kernel command line, the buffer is
1108 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1109 * memory is dynamically allocated. Memory management must already be up.
1112 log_setsize(int size
) {
1114 int new_logsize
, new_bufr
, new_bufx
;
1116 int old_logsize
, old_bufr
, old_bufx
;
1120 if (size
> MAX_MSG_BSIZE
)
1127 if (!(new_logdata
= (char*)kalloc(size
))) {
1128 printf("log_setsize: unable to allocate memory\n");
1131 bzero(new_logdata
, new_logsize
);
1135 old_logsize
= msgbufp
->msg_size
;
1136 old_logdata
= msgbufp
->msg_bufc
;
1137 old_bufr
= msgbufp
->msg_bufr
;
1138 old_bufx
= msgbufp
->msg_bufx
;
1140 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1141 size
, old_logdata
, old_logsize
, old_bufr
, old_bufx
);
1143 /* start "new_logsize" bytes before the write pointer */
1144 if (new_logsize
<= old_bufx
) {
1145 count
= new_logsize
;
1146 p
= old_logdata
+ old_bufx
- count
;
1149 * if new buffer is bigger, copy what we have and let the
1150 * bzero above handle the difference
1152 count
= MIN(new_logsize
, old_logsize
);
1153 p
= old_logdata
+ old_logsize
- (count
- old_bufx
);
1155 for (i
= 0; i
< count
; i
++) {
1156 if (p
>= old_logdata
+ old_logsize
)
1160 new_logdata
[i
] = ch
;
1164 if (new_bufx
>= new_logsize
)
1166 msgbufp
->msg_bufx
= new_bufx
;
1168 new_bufr
= old_bufx
- old_bufr
; /* how much were we trailing bufx by? */
1170 new_bufr
+= old_logsize
;
1171 new_bufr
= new_bufx
- new_bufr
; /* now relative to oldest data in new buffer */
1173 new_bufr
+= new_logsize
;
1174 msgbufp
->msg_bufr
= new_bufr
;
1176 msgbufp
->msg_size
= new_logsize
;
1177 msgbufp
->msg_bufc
= new_logdata
;
1179 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1180 size
, new_logdata
, new_logsize
, new_bufr
, new_bufx
);
1184 /* this memory is now dead - clear it so that it compresses better
1185 in case of suspend to disk etc. */
1186 bzero(old_logdata
, old_logsize
);
1187 if (old_logdata
!= smsg_bufc
) {
1188 /* dynamic memory that must be freed */
1189 kfree(old_logdata
, old_logsize
);
1192 printf("set system log size to %d bytes\n", new_logsize
);
1197 void oslog_setsize(int size
)
1200 // If the size is less than the default stream buffer
1202 if (size
<= OSLOG_STREAM_BUF_SIZE
) {
1206 scale
= (uint16_t) (size
/ OSLOG_STREAM_BUF_SIZE
);
1208 oslog_stream_buf_size
= size
;
1209 oslog_stream_num_entries
= scale
* OSLOG_NUM_STREAM_ENTRIES
;
1210 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size
, oslog_stream_num_entries
);
1213 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0, sysctl_kern_msgbuf
, "I", "");
1215 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp __unused
,
1216 void *arg1 __unused
,
1218 struct sysctl_req
*req
)
1220 int old_bufsize
, bufsize
;
1224 old_bufsize
= bufsize
= msgbufp
->msg_size
;
1227 error
= sysctl_io_number(req
, bufsize
, sizeof(bufsize
), &bufsize
, NULL
);
1231 if (bufsize
!= old_bufsize
) {
1232 error
= log_setsize(bufsize
);
1240 * This should be called by /sbin/dmesg only via libproc.
1241 * It returns as much data still in the buffer as possible.
1244 log_dmesg(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
) {
1246 uint32_t localbuff_size
;
1247 int error
= 0, newl
, skip
;
1248 char *localbuff
, *p
, *copystart
, ch
;
1252 localbuff_size
= (msgbufp
->msg_size
+ 2); /* + '\n' + '\0' */
1255 /* Allocate a temporary non-circular buffer for copyout */
1256 if (!(localbuff
= (char *)kalloc(localbuff_size
))) {
1257 printf("log_dmesg: unable to allocate memory\n");
1261 /* in between here, the log could become bigger, but that's fine */
1265 * The message buffer is circular; start at the write pointer, and
1266 * make one loop up to write pointer - 1.
1268 p
= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
;
1269 for (i
= newl
= skip
= 0; p
!= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
- 1; ++p
) {
1270 if (p
>= msgbufp
->msg_bufc
+ msgbufp
->msg_size
)
1271 p
= msgbufp
->msg_bufc
;
1273 /* Skip "\n<.*>" syslog sequences. */
1279 if (newl
&& ch
== '<') {
1285 newl
= (ch
== '\n');
1286 localbuff
[i
++] = ch
;
1287 /* The original version of this routine contained a buffer
1288 * overflow. At the time, a "small" targeted fix was desired
1289 * so the change below to check the buffer bounds was made.
1290 * TODO: rewrite this needlessly convoluted routine.
1292 if (i
== (localbuff_size
- 2))
1296 localbuff
[i
++] = '\n';
1299 if (buffersize
>= i
) {
1300 copystart
= localbuff
;
1303 copystart
= localbuff
+ i
- buffersize
;
1304 copysize
= buffersize
;
1309 error
= copyout(copystart
, buffer
, copysize
);
1313 kfree(localbuff
, localbuff_size
);