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
= 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(get_task_map(current_task()),
802 if (ret
== KERN_SUCCESS
) {
803 map_info
.fbmi_addr
= (uint64_t) (user_addr
);
804 map_info
.fbmi_size
= buffer_size
;
805 bcopy(&map_info
, data
, sizeof(firehose_buffer_map_info_t
));
812 __firehose_merge_updates(*(firehose_push_reply_t
*)(data
));
822 oslog_streamioctl(__unused dev_t dev
, u_long com
, caddr_t data
, __unused
int flag
, __unused
struct proc
*p
)
826 lck_spin_lock(&oslog_stream_lock
);
830 if (data
&& *(int *)data
)
831 oslog_streamsoftc
.sc_state
|= LOG_NBIO
;
833 oslog_streamsoftc
.sc_state
&= ~LOG_NBIO
;
836 if (data
&& *(int *)data
)
837 oslog_streamsoftc
.sc_state
|= LOG_ASYNC
;
839 oslog_streamsoftc
.sc_state
&= ~LOG_ASYNC
;
846 lck_spin_unlock(&oslog_stream_lock
);
853 /* After this point, we must be ready to accept characters */
860 vm_size_t size
= FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT
* FIREHOSE_CHUNK_SIZE
;
864 kr
= kmem_alloc_flags(kernel_map
, &kernel_firehose_addr
,
865 size
+ (2 * PAGE_SIZE
), VM_KERN_MEMORY_LOG
,
866 KMA_GUARD_FIRST
| KMA_GUARD_LAST
);
867 if (kr
!= KERN_SUCCESS
) {
868 panic("Failed to allocate memory for firehose logging buffer");
870 kernel_firehose_addr
+= PAGE_SIZE
;
871 bzero(kernel_firehose_addr
, size
);
872 /* register buffer with firehose */
873 kernel_firehose_addr
= __firehose_buffer_create((size_t *) &size
);
875 kprintf("oslog_init completed\n");
881 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
884 * Parameters: c Character to output
888 * Notes: This functions is used for multibyte output to the log; it
889 * should be used preferrentially where possible to ensure that
890 * log entries do not end up interspersed due to preemption or
894 log_putc_locked(char c
)
899 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
900 if (mbp
->msg_bufx
>= msgbufp
->msg_size
)
904 static oslog_stream_buf_entry_t
905 oslog_stream_find_free_buf_entry_locked(void)
908 oslog_stream_buf_entry_t buf_entry
= NULL
;
910 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
912 mbp
= oslog_streambufp
;
914 buf_entry
= STAILQ_FIRST(&oslog_stream_free_head
);
916 STAILQ_REMOVE_HEAD(&oslog_stream_free_head
, buf_entries
);
919 // If no list elements are available in the free-list,
920 // consume the next log line so we can free up its list element
921 oslog_stream_buf_entry_t prev_entry
= NULL
;
923 buf_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
924 while (buf_entry
->type
== oslog_stream_link_type_metadata
) {
925 prev_entry
= buf_entry
;
926 buf_entry
= STAILQ_NEXT(buf_entry
, buf_entries
);
929 if (prev_entry
== NULL
) {
930 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
933 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
936 mbp
->msg_bufr
+= buf_entry
->size
;
937 oslog_s_dropped_msgcount
++;
938 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
939 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
947 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry
)
949 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
950 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, m_entry
, buf_entries
);
955 static void oslog_streamwrite_append_bytes(const char *buffer
, int buflen
)
959 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
961 mbp
= oslog_streambufp
;
962 // Check if we have enough space in the stream buffer to write the data
963 if (mbp
->msg_bufx
+ buflen
<= mbp
->msg_size
) {
964 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, buflen
);
966 mbp
->msg_bufx
+= buflen
;
967 if (mbp
->msg_bufx
== mbp
->msg_size
) {
971 // Copy part of the data until the end of the stream
972 int bytes_left
= mbp
->msg_size
- mbp
->msg_bufx
;
973 memcpy((void *)(mbp
->msg_bufc
+ mbp
->msg_bufx
), buffer
, bytes_left
);
975 buflen
-= bytes_left
;
976 buffer
+= bytes_left
;
978 // Copy the remainder of the data from the beginning of stream
979 memcpy((void *)mbp
->msg_bufc
, buffer
, buflen
);
980 mbp
->msg_bufx
= buflen
;
987 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid
,
988 uint64_t stamp
, const void *pubdata
, size_t publen
)
991 int available_space
= 0;
992 oslog_stream_buf_entry_t buf_entry
= NULL
;
993 oslog_stream_buf_entry_t next_entry
= NULL
;
995 uint16_t ft_size
= offsetof(struct firehose_tracepoint_s
, ft_data
);
996 int ft_length
= ft_size
+ publen
;
998 lck_spin_assert(&oslog_stream_lock
, LCK_ASSERT_OWNED
);
1000 mbp
= oslog_streambufp
;
1001 if (ft_length
> mbp
->msg_size
) {
1002 (void)hw_atomic_add(&oslog_s_error_count
, 1);
1006 // Ensure that we have a list element for this record
1007 buf_entry
= oslog_stream_find_free_buf_entry_locked();
1009 assert(buf_entry
!= NULL
);
1011 // Ensure that we have space in the ring buffer for the current logline
1012 if (mbp
->msg_bufr
> mbp
->msg_bufx
) {
1013 available_space
= mbp
->msg_bufr
- mbp
->msg_bufx
;
1015 available_space
= mbp
->msg_size
- mbp
->msg_bufx
+ mbp
->msg_bufr
;
1017 while(ft_length
> available_space
) {
1018 oslog_stream_buf_entry_t prev_entry
= NULL
;
1020 next_entry
= STAILQ_FIRST(&oslog_stream_buf_head
);
1021 assert(next_entry
!= NULL
);
1022 while (next_entry
->type
== oslog_stream_link_type_metadata
) {
1023 prev_entry
= next_entry
;
1024 next_entry
= STAILQ_NEXT(next_entry
, buf_entries
);
1027 if (prev_entry
== NULL
) {
1028 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head
, buf_entries
);
1031 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head
, prev_entry
, buf_entries
);
1034 mbp
->msg_bufr
+= next_entry
->size
;
1035 if (mbp
->msg_bufr
>= mbp
->msg_size
) {
1036 mbp
->msg_bufr
= (mbp
->msg_bufr
% mbp
->msg_size
);
1039 oslog_s_dropped_msgcount
++;
1040 available_space
+= next_entry
->size
;
1042 STAILQ_INSERT_TAIL(&oslog_stream_free_head
, next_entry
, buf_entries
);
1045 assert(ft_length
<= available_space
);
1047 // Write the log line and update the list entry for this record
1048 buf_entry
->offset
= mbp
->msg_bufx
;
1049 buf_entry
->size
= ft_length
;
1050 buf_entry
->timestamp
= stamp
;
1051 buf_entry
->type
= oslog_stream_link_type_log
;
1053 // Construct a tracepoint
1054 struct firehose_tracepoint_s fs
= {
1055 .ft_thread
= thread_tid(current_thread()),
1056 .ft_id
.ftid_value
= ftid
.ftid_value
,
1060 oslog_streamwrite_append_bytes((char *)&fs
, sizeof(fs
));
1061 oslog_streamwrite_append_bytes(pubdata
, publen
);
1063 assert(mbp
->msg_bufr
< mbp
->msg_size
);
1064 // Insert the element to the buffer data list
1065 STAILQ_INSERT_TAIL(&oslog_stream_buf_head
, buf_entry
, buf_entries
);
1075 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1076 * held by the caller.
1078 * Parameters: c Character to output
1082 * Notes: This function is used for syingle byte output to the log. It
1083 * primarily exists to maintain binary backward compatibility.
1088 int unread_count
= 0;
1091 unread_count
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
1094 if (unread_count
< 0)
1095 unread_count
= 0 - unread_count
;
1096 if (c
== '\n' || unread_count
>= MAX_UNREAD_CHARS
)
1102 * it is possible to increase the kernel log buffer size by adding
1104 * to the kernel command line, and to read the current size using
1105 * sysctl kern.msgbuf
1106 * If there is no parameter on the kernel command line, the buffer is
1107 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1108 * memory is dynamically allocated. Memory management must already be up.
1111 log_setsize(int size
) {
1113 int new_logsize
, new_bufr
, new_bufx
;
1115 int old_logsize
, old_bufr
, old_bufx
;
1119 if (size
> MAX_MSG_BSIZE
)
1126 if (!(new_logdata
= (char*)kalloc(size
))) {
1127 printf("log_setsize: unable to allocate memory\n");
1130 bzero(new_logdata
, new_logsize
);
1134 old_logsize
= msgbufp
->msg_size
;
1135 old_logdata
= msgbufp
->msg_bufc
;
1136 old_bufr
= msgbufp
->msg_bufr
;
1137 old_bufx
= msgbufp
->msg_bufx
;
1139 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1140 size
, old_logdata
, old_logsize
, old_bufr
, old_bufx
);
1142 /* start "new_logsize" bytes before the write pointer */
1143 if (new_logsize
<= old_bufx
) {
1144 count
= new_logsize
;
1145 p
= old_logdata
+ old_bufx
- count
;
1148 * if new buffer is bigger, copy what we have and let the
1149 * bzero above handle the difference
1151 count
= MIN(new_logsize
, old_logsize
);
1152 p
= old_logdata
+ old_logsize
- (count
- old_bufx
);
1154 for (i
= 0; i
< count
; i
++) {
1155 if (p
>= old_logdata
+ old_logsize
)
1159 new_logdata
[i
] = ch
;
1163 if (new_bufx
>= new_logsize
)
1165 msgbufp
->msg_bufx
= new_bufx
;
1167 new_bufr
= old_bufx
- old_bufr
; /* how much were we trailing bufx by? */
1169 new_bufr
+= old_logsize
;
1170 new_bufr
= new_bufx
- new_bufr
; /* now relative to oldest data in new buffer */
1172 new_bufr
+= new_logsize
;
1173 msgbufp
->msg_bufr
= new_bufr
;
1175 msgbufp
->msg_size
= new_logsize
;
1176 msgbufp
->msg_bufc
= new_logdata
;
1178 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1179 size
, new_logdata
, new_logsize
, new_bufr
, new_bufx
);
1183 /* this memory is now dead - clear it so that it compresses better
1184 in case of suspend to disk etc. */
1185 bzero(old_logdata
, old_logsize
);
1186 if (old_logdata
!= smsg_bufc
) {
1187 /* dynamic memory that must be freed */
1188 kfree(old_logdata
, old_logsize
);
1191 printf("set system log size to %d bytes\n", new_logsize
);
1196 void oslog_setsize(int size
)
1199 // If the size is less than the default stream buffer
1201 if (size
<= OSLOG_STREAM_BUF_SIZE
) {
1205 scale
= (uint16_t) (size
/ OSLOG_STREAM_BUF_SIZE
);
1207 oslog_stream_buf_size
= size
;
1208 oslog_stream_num_entries
= scale
* OSLOG_NUM_STREAM_ENTRIES
;
1209 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size
, oslog_stream_num_entries
);
1212 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, 0, sysctl_kern_msgbuf
, "I", "");
1214 static int sysctl_kern_msgbuf(struct sysctl_oid
*oidp __unused
,
1215 void *arg1 __unused
,
1217 struct sysctl_req
*req
)
1219 int old_bufsize
, bufsize
;
1223 old_bufsize
= bufsize
= msgbufp
->msg_size
;
1226 error
= sysctl_io_number(req
, bufsize
, sizeof(bufsize
), &bufsize
, NULL
);
1230 if (bufsize
!= old_bufsize
) {
1231 error
= log_setsize(bufsize
);
1239 * This should be called by /sbin/dmesg only via libproc.
1240 * It returns as much data still in the buffer as possible.
1243 log_dmesg(user_addr_t buffer
, uint32_t buffersize
, int32_t * retval
) {
1245 uint32_t localbuff_size
;
1246 int error
= 0, newl
, skip
;
1247 char *localbuff
, *p
, *copystart
, ch
;
1251 localbuff_size
= (msgbufp
->msg_size
+ 2); /* + '\n' + '\0' */
1254 /* Allocate a temporary non-circular buffer for copyout */
1255 if (!(localbuff
= (char *)kalloc(localbuff_size
))) {
1256 printf("log_dmesg: unable to allocate memory\n");
1260 /* in between here, the log could become bigger, but that's fine */
1264 * The message buffer is circular; start at the write pointer, and
1265 * make one loop up to write pointer - 1.
1267 p
= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
;
1268 for (i
= newl
= skip
= 0; p
!= msgbufp
->msg_bufc
+ msgbufp
->msg_bufx
- 1; ++p
) {
1269 if (p
>= msgbufp
->msg_bufc
+ msgbufp
->msg_size
)
1270 p
= msgbufp
->msg_bufc
;
1272 /* Skip "\n<.*>" syslog sequences. */
1278 if (newl
&& ch
== '<') {
1284 newl
= (ch
== '\n');
1285 localbuff
[i
++] = ch
;
1286 /* The original version of this routine contained a buffer
1287 * overflow. At the time, a "small" targeted fix was desired
1288 * so the change below to check the buffer bounds was made.
1289 * TODO: rewrite this needlessly convoluted routine.
1291 if (i
== (localbuff_size
- 2))
1295 localbuff
[i
++] = '\n';
1298 if (buffersize
>= i
) {
1299 copystart
= localbuff
;
1302 copystart
= localbuff
+ i
- buffersize
;
1303 copysize
= buffersize
;
1308 error
= copyout(copystart
, buffer
, copysize
);
1312 kfree(localbuff
, localbuff_size
);