]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
5a748b5b53e750b5515227e16e6b2b7ebe4159e5
[apple/xnu.git] / bsd / kern / subr_log.c
1 /*
2 * Copyright (c) 2000-2016 Apple, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
62 */
63
64 /*
65 * Error log buffer for kernel printf's.
66 */
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/proc_internal.h>
71 #include <sys/vnode.h>
72 #include <stdbool.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>
77
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>
88 #include <sys/lock.h>
89 #include <sys/signalvar.h>
90 #include <sys/conf.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>
101
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;
108
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;
113
114 #define LOG_RDPRI (PZERO + 1)
115
116 #define LOG_NBIO 0x02
117 #define LOG_ASYNC 0x04
118 #define LOG_RDWAIT 0x08
119
120 #define MAX_UNREAD_CHARS (CONFIG_MSG_BSIZE/2)
121 /* All globals should be accessed under LOG_LOCK() */
122
123 /* logsoftc only valid while log_open=1 */
124 struct logsoftc {
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 */
128 } logsoftc;
129
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 = {
134 .fc_pos = {
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
140 },
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;
147
148 // List entries for keeping track of the streaming buffer
149 static oslog_stream_buf_entry_t oslog_stream_buf_entries;
150
151 #define OSLOG_NUM_STREAM_ENTRIES 64
152 #define OSLOG_STREAM_BUF_SIZE 4096
153
154 int oslog_open = 0;
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;
159
160 /* oslogsoftc only valid while oslog_open=1 */
161 struct oslogsoftc {
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 */
165 } oslogsoftc;
166
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 */
171 }oslog_streamsoftc;
172
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);
177
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);
182
183 /* defined for osfmk/kern/printf.c */
184 void bsd_log_init(void);
185
186 /*
187 * Ideally this file would define this lock, but bsd doesn't have the definition
188 * for lock groups.
189 */
190 decl_lck_spin_data(extern, oslog_stream_lock)
191
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;
198
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;
204
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;
211
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);
219
220 /*
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.
224 */
225
226 #define LOG_LOCK() bsd_log_lock()
227 #define LOG_UNLOCK() bsd_log_unlock()
228
229 #if DEBUG
230 #define LOG_SETSIZE_DEBUG(x...) kprintf(x)
231 #else
232 #define LOG_SETSIZE_DEBUG(x...) do { } while(0)
233 #endif
234
235 static int sysctl_kern_msgbuf(struct sysctl_oid *oidp,
236 void *arg1,
237 int arg2,
238 struct sysctl_req *req);
239
240 /*ARGSUSED*/
241 int
242 logopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
243 {
244 LOG_LOCK();
245 if (log_open) {
246 LOG_UNLOCK();
247 return (EBUSY);
248 }
249 logsoftc.sc_pgid = p->p_pid; /* signal process only */
250 log_open = 1;
251
252 LOG_UNLOCK();
253
254 return (0);
255 }
256
257 /*ARGSUSED*/
258 int
259 logclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
260 {
261 LOG_LOCK();
262 logsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
263 selwakeup(&logsoftc.sc_selp);
264 selthreadclear(&logsoftc.sc_selp);
265 log_open = 0;
266 LOG_UNLOCK();
267 return (0);
268 }
269
270
271 int
272 oslogopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
273 {
274 LOG_LOCK();
275 if (oslog_open) {
276 LOG_UNLOCK();
277 return(EBUSY);
278 }
279 oslogsoftc.sc_pgid = p->p_pid; /* signal process only */
280 oslog_open = 1;
281
282 LOG_UNLOCK();
283 return (0);
284 }
285
286 int
287 oslogclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
288 {
289 LOG_LOCK();
290 oslogsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
291 selwakeup(&oslogsoftc.sc_selp);
292 selthreadclear(&oslogsoftc.sc_selp);
293 oslog_open = 0;
294 LOG_UNLOCK();
295 return (0);
296 }
297
298 int
299 oslog_streamopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
300 {
301 char *oslog_stream_msg_bufc = NULL;
302 oslog_stream_buf_entry_t entries = NULL;
303
304 lck_spin_lock(&oslog_stream_lock);
305 if (oslog_stream_open) {
306 lck_spin_unlock(&oslog_stream_lock);
307 return EBUSY;
308 }
309 lck_spin_unlock(&oslog_stream_lock);
310
311 // Allocate the stream buffer
312 oslog_stream_msg_bufc = kalloc(oslog_stream_buf_size);
313 if (!oslog_stream_msg_bufc) {
314 return ENOMEM;
315 }
316
317 /* entries to support kernel logging in stream mode */
318 entries = kalloc(oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s));
319 if (!entries) {
320 kfree(oslog_stream_msg_bufc, oslog_stream_buf_size);
321 return ENOMEM;
322 }
323
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));
329 return EBUSY;
330 }
331
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;
335
336 oslog_stream_buf_entries = entries;
337
338 STAILQ_INIT(&oslog_stream_free_head);
339 STAILQ_INIT(&oslog_stream_buf_head);
340
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);
347 }
348
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);
353
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);
359
360 return 0;
361 }
362
363 int
364 oslog_streamclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
365 {
366 oslog_stream_buf_entry_t next_entry = NULL;
367 char *oslog_stream_msg_bufc = NULL;
368 oslog_stream_buf_entry_t entries = NULL;
369
370 lck_spin_lock(&oslog_stream_lock);
371
372 if (oslog_stream_open == 0) {
373 lck_spin_unlock(&oslog_stream_lock);
374 return EBADF;
375 }
376
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);
381 }
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;
394
395 lck_spin_unlock(&oslog_stream_lock);
396
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));
401
402 return 0;
403 }
404
405 /*ARGSUSED*/
406 int
407 logread(__unused dev_t dev, struct uio *uio, int flag)
408 {
409 int l;
410 int error = 0;
411
412 LOG_LOCK();
413 while (msgbufp->msg_bufr == msgbufp->msg_bufx) {
414 if (flag & IO_NDELAY) {
415 error = EWOULDBLOCK;
416 goto out;
417 }
418 if (logsoftc.sc_state & LOG_NBIO) {
419 error = EWOULDBLOCK;
420 goto out;
421 }
422 logsoftc.sc_state |= LOG_RDWAIT;
423 LOG_UNLOCK();
424 /*
425 * If the wakeup is missed
426 * then wait for 5 sec and reevaluate
427 */
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)
432 return (error);
433 }
434 LOG_LOCK();
435 }
436 logsoftc.sc_state &= ~LOG_RDWAIT;
437
438 while (uio_resid(uio) > 0) {
439 int readpos;
440
441 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
442 if (l < 0)
443 l = msgbufp->msg_size - msgbufp->msg_bufr;
444 l = min(l, uio_resid(uio));
445 if (l == 0)
446 break;
447
448 readpos = msgbufp->msg_bufr;
449 LOG_UNLOCK();
450 error = uiomove((caddr_t)&msgbufp->msg_bufc[readpos],
451 l, uio);
452 LOG_LOCK();
453 if (error)
454 break;
455 msgbufp->msg_bufr = readpos + l;
456 if (msgbufp->msg_bufr >= msgbufp->msg_size)
457 msgbufp->msg_bufr = 0;
458 }
459 out:
460 LOG_UNLOCK();
461 return (error);
462 }
463
464 /*ARGSUSED*/
465 int
466 oslog_streamread(__unused dev_t dev, struct uio *uio, int flag)
467 {
468 int error = 0;
469 int copy_size = 0;
470 static char logline[FIREHOSE_CHUNK_SIZE];
471
472 lck_spin_lock(&oslog_stream_lock);
473
474 if (!oslog_stream_open) {
475 lck_spin_unlock(&oslog_stream_lock);
476 return EBADF;
477 }
478
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);
482 return EWOULDBLOCK;
483 }
484
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);
492 }
493
494 switch (wr) {
495 case THREAD_AWAKENED:
496 case THREAD_TIMED_OUT:
497 break;
498 default:
499 lck_spin_unlock(&oslog_stream_lock);
500 return EINTR;
501 }
502 }
503
504 if (!oslog_stream_open) {
505 lck_spin_unlock(&oslog_stream_lock);
506 return EBADF;
507 }
508
509 int logpos = 0;
510 oslog_stream_buf_entry_t read_entry = NULL;
511 uint16_t rec_length;
512
513 read_entry = STAILQ_FIRST(&oslog_stream_buf_head);
514 assert(read_entry != NULL);
515 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
516
517 // Copy the timestamp first
518 memcpy(logline + logpos, &read_entry->timestamp, sizeof(uint64_t));
519 logpos += sizeof(uint64_t);
520
521 switch (read_entry->type) {
522 /* Handle metadata messages */
523 case oslog_stream_link_type_metadata:
524 {
525 memcpy(logline + logpos,
526 (read_entry->metadata), read_entry->size);
527 logpos += read_entry->size;
528
529 lck_spin_unlock(&oslog_stream_lock);
530
531 // Free the list entry
532 kfree(read_entry, (sizeof(struct oslog_stream_buf_entry_s) + read_entry->size));
533 break;
534 }
535 /* Handle log messages */
536 case oslog_stream_link_type_log:
537 {
538 /* ensure that the correct read entry was dequeued */
539 assert(read_entry->offset == oslog_streambufp->msg_bufr);
540 rec_length = read_entry->size;
541
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);
546
547 oslog_streambufp->msg_bufr += rec_length;
548 if (oslog_streambufp->msg_bufr == oslog_streambufp->msg_size) {
549 oslog_streambufp->msg_bufr = 0;
550 }
551 logpos += rec_length;
552 } else {
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;
560
561 memcpy(logline + logpos, (const void *)oslog_streambufp->msg_bufc,
562 rec_length);
563 oslog_streambufp->msg_bufr = rec_length;
564 logpos += rec_length;
565 }
566 assert(oslog_streambufp->msg_bufr < oslog_streambufp->msg_size);
567 STAILQ_INSERT_TAIL(&oslog_stream_free_head, read_entry, buf_entries);
568
569 lck_spin_unlock(&oslog_stream_lock);
570 break;
571 }
572 default:
573 {
574 panic("Got unexpected log entry type: %hhu\n", read_entry->type);
575 }
576 }
577
578 copy_size = min(logpos, uio_resid(uio));
579 if (copy_size != 0) {
580 error = uiomove((caddr_t)logline, copy_size, uio);
581 }
582 (void)hw_atomic_add(&oslog_s_streamed_msgcount, 1);
583
584 return error;
585 }
586
587 /*ARGSUSED*/
588 int
589 logselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
590 {
591 switch (rw) {
592
593 case FREAD:
594 LOG_LOCK();
595 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
596 LOG_UNLOCK();
597 return (1);
598 }
599 selrecord(p, &logsoftc.sc_selp, wql);
600 LOG_UNLOCK();
601 break;
602 }
603 return (0);
604 }
605
606 int
607 oslogselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
608 {
609 switch (rw) {
610
611 case FREAD:
612 LOG_LOCK();
613 if (os_log_wakeup) {
614 LOG_UNLOCK();
615 return (1);
616 }
617 selrecord(p, &oslogsoftc.sc_selp, wql);
618 LOG_UNLOCK();
619 break;
620 }
621 return (0);
622 }
623
624 int
625 oslog_streamselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
626 {
627 int ret = 0;
628
629 lck_spin_lock(&oslog_stream_lock);
630
631 switch (rw) {
632 case FREAD:
633 if (STAILQ_EMPTY(&oslog_stream_buf_head)) {
634 selrecord(p, &oslog_streamsoftc.sc_selp, wql);
635 } else {
636 ret = 1;
637 }
638 break;
639 }
640
641 lck_spin_unlock(&oslog_stream_lock);
642 return ret;
643 }
644
645 void
646 logwakeup(void)
647 {
648 int pgid;
649
650 /* cf. r24974766 & r25201228*/
651 if (oslog_is_safe() == FALSE) {
652 return;
653 }
654
655 LOG_LOCK();
656 if (!log_open) {
657 LOG_UNLOCK();
658 return;
659 }
660 selwakeup(&logsoftc.sc_selp);
661 if (logsoftc.sc_state & LOG_ASYNC) {
662 pgid = logsoftc.sc_pgid;
663 LOG_UNLOCK();
664 if (pgid < 0)
665 gsignal(-pgid, SIGIO);
666 else
667 proc_signal(pgid, SIGIO);
668 LOG_LOCK();
669 }
670 if (logsoftc.sc_state & LOG_RDWAIT) {
671 wakeup((caddr_t)msgbufp);
672 logsoftc.sc_state &= ~LOG_RDWAIT;
673 }
674 LOG_UNLOCK();
675 }
676
677 void
678 oslogwakeup(void)
679 {
680 LOG_LOCK();
681 if (!oslog_open) {
682 LOG_UNLOCK();
683 return;
684 }
685 selwakeup(&oslogsoftc.sc_selp);
686 os_log_wakeup = 1;
687 LOG_UNLOCK();
688 }
689
690 static void
691 oslog_streamwakeup_locked(void)
692 {
693 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
694 if (!oslog_stream_open) {
695 return;
696 }
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;
701 }
702 }
703
704 void
705 oslog_streamwakeup(void)
706 {
707 /* cf. r24974766 & r25201228*/
708 if (oslog_is_safe() == FALSE) {
709 return;
710 }
711
712 lck_spin_lock(&oslog_stream_lock);
713 oslog_streamwakeup_locked();
714 lck_spin_unlock(&oslog_stream_lock);
715 }
716
717 /*ARGSUSED*/
718 int
719 logioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
720 {
721 int l;
722
723 LOG_LOCK();
724 switch (com) {
725
726 /* return number of characters immediately available */
727 case FIONREAD:
728 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
729 if (l < 0)
730 l += msgbufp->msg_size;
731 *(off_t *)data = l;
732 break;
733
734 case FIONBIO:
735 if (*(int *)data)
736 logsoftc.sc_state |= LOG_NBIO;
737 else
738 logsoftc.sc_state &= ~LOG_NBIO;
739 break;
740
741 case FIOASYNC:
742 if (*(int *)data)
743 logsoftc.sc_state |= LOG_ASYNC;
744 else
745 logsoftc.sc_state &= ~LOG_ASYNC;
746 break;
747
748 case TIOCSPGRP:
749 logsoftc.sc_pgid = *(int *)data;
750 break;
751
752 case TIOCGPGRP:
753 *(int *)data = logsoftc.sc_pgid;
754 break;
755
756 default:
757 LOG_UNLOCK();
758 return (-1);
759 }
760 LOG_UNLOCK();
761 return (0);
762 }
763
764 /*ARGSUSED*/
765 int
766 oslogioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
767 {
768 int ret = 0;
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;
774
775 switch (com) {
776
777 /* return number of characters immediately available */
778
779 case LOGBUFFERMAP:
780 kernel_firehose_buffer = (firehose_buffer_t)kernel_firehose_addr;
781
782 ret = mach_make_memory_entry_64(kernel_map,
783 &buffer_size,
784 (mach_vm_offset_t) kernel_firehose_buffer,
785 ( MAP_MEM_VM_SHARE | VM_PROT_READ ),
786 &mem_entry_ptr,
787 MACH_PORT_NULL);
788 if (ret == KERN_SUCCESS) {
789 ret = mach_vm_map_kernel(get_task_map(current_task()),
790 &user_addr,
791 buffer_size,
792 0, /* mask */
793 VM_FLAGS_ANYWHERE,
794 VM_KERN_MEMORY_NONE,
795 mem_entry_ptr,
796 0, /* offset */
797 FALSE, /* copy */
798 VM_PROT_READ,
799 VM_PROT_READ,
800 VM_INHERIT_SHARE);
801 }
802
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));
807 }
808 break;
809 case LOGFLUSHED:
810 LOG_LOCK();
811 os_log_wakeup = 0;
812 LOG_UNLOCK();
813 __firehose_merge_updates(*(firehose_push_reply_t *)(data));
814 break;
815 default:
816 return (-1);
817 }
818 return (0);
819 }
820
821 /*ARGSUSED*/
822 int
823 oslog_streamioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
824 {
825 int err = 0;
826
827 lck_spin_lock(&oslog_stream_lock);
828
829 switch (com) {
830 case FIONBIO:
831 if (data && *(int *)data)
832 oslog_streamsoftc.sc_state |= LOG_NBIO;
833 else
834 oslog_streamsoftc.sc_state &= ~LOG_NBIO;
835 break;
836 case FIOASYNC:
837 if (data && *(int *)data)
838 oslog_streamsoftc.sc_state |= LOG_ASYNC;
839 else
840 oslog_streamsoftc.sc_state &= ~LOG_ASYNC;
841 break;
842 default:
843 err = -1;
844 break;
845 }
846
847 lck_spin_unlock(&oslog_stream_lock);
848 return err;
849 }
850
851 void
852 bsd_log_init(void)
853 {
854 /* After this point, we must be ready to accept characters */
855 }
856
857 void
858 oslog_init(void)
859 {
860 kern_return_t kr;
861 vm_size_t size = FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT * FIREHOSE_CHUNK_SIZE;
862
863 oslog_lock_init();
864
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");
870 }
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);
875
876 kprintf("oslog_init completed\n");
877 }
878
879 /*
880 * log_putc_locked
881 *
882 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
883 * by the caller.
884 *
885 * Parameters: c Character to output
886 *
887 * Returns: (void)
888 *
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
892 * SMP reentrancy.
893 */
894 void
895 log_putc_locked(char c)
896 {
897 struct msgbuf *mbp;
898
899 mbp = msgbufp;
900 mbp->msg_bufc[mbp->msg_bufx++] = c;
901 if (mbp->msg_bufx >= msgbufp->msg_size)
902 mbp->msg_bufx = 0;
903 }
904
905 static oslog_stream_buf_entry_t
906 oslog_stream_find_free_buf_entry_locked(void)
907 {
908 struct msgbuf *mbp;
909 oslog_stream_buf_entry_t buf_entry = NULL;
910
911 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
912
913 mbp = oslog_streambufp;
914
915 buf_entry = STAILQ_FIRST(&oslog_stream_free_head);
916 if (buf_entry) {
917 STAILQ_REMOVE_HEAD(&oslog_stream_free_head, buf_entries);
918 }
919 else {
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;
923
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);
928 }
929
930 if (prev_entry == NULL) {
931 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
932 }
933 else {
934 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
935 }
936
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);
941 }
942 }
943
944 return buf_entry;
945 }
946
947 void
948 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry)
949 {
950 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
951 STAILQ_INSERT_TAIL(&oslog_stream_buf_head, m_entry, buf_entries);
952
953 return;
954 }
955
956 static void oslog_streamwrite_append_bytes(const char *buffer, int buflen)
957 {
958 struct msgbuf *mbp;
959
960 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
961
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);
966
967 mbp->msg_bufx += buflen;
968 if (mbp->msg_bufx == mbp->msg_size) {
969 mbp->msg_bufx = 0;
970 }
971 } else {
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);
975
976 buflen -= bytes_left;
977 buffer += bytes_left;
978
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;
982 }
983 return;
984 }
985
986
987 void
988 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid,
989 uint64_t stamp, const void *pubdata, size_t publen)
990 {
991 struct msgbuf *mbp;
992 int available_space = 0;
993 oslog_stream_buf_entry_t buf_entry = NULL;
994 oslog_stream_buf_entry_t next_entry = NULL;
995
996 uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data);
997 int ft_length = ft_size + publen;
998
999 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
1000
1001 mbp = oslog_streambufp;
1002 if (ft_length > mbp->msg_size) {
1003 (void)hw_atomic_add(&oslog_s_error_count, 1);
1004 return;
1005 }
1006
1007 // Ensure that we have a list element for this record
1008 buf_entry = oslog_stream_find_free_buf_entry_locked();
1009
1010 assert(buf_entry != NULL);
1011
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;
1015 } else {
1016 available_space = mbp->msg_size - mbp->msg_bufx + mbp->msg_bufr;
1017 }
1018 while(ft_length > available_space) {
1019 oslog_stream_buf_entry_t prev_entry = NULL;
1020
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);
1026 }
1027
1028 if (prev_entry == NULL) {
1029 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
1030 }
1031 else {
1032 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
1033 }
1034
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);
1038 }
1039
1040 oslog_s_dropped_msgcount++;
1041 available_space += next_entry->size;
1042
1043 STAILQ_INSERT_TAIL(&oslog_stream_free_head, next_entry, buf_entries);
1044 }
1045
1046 assert(ft_length <= available_space);
1047
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;
1053
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,
1058 .ft_length = publen
1059 };
1060
1061 oslog_streamwrite_append_bytes((char *)&fs, sizeof(fs));
1062 oslog_streamwrite_append_bytes(pubdata, publen);
1063
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);
1067
1068 return;
1069 }
1070
1071
1072
1073 /*
1074 * log_putc
1075 *
1076 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1077 * held by the caller.
1078 *
1079 * Parameters: c Character to output
1080 *
1081 * Returns: (void)
1082 *
1083 * Notes: This function is used for syingle byte output to the log. It
1084 * primarily exists to maintain binary backward compatibility.
1085 */
1086 void
1087 log_putc(char c)
1088 {
1089 int unread_count = 0;
1090 LOG_LOCK();
1091 log_putc_locked(c);
1092 unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr;
1093 LOG_UNLOCK();
1094
1095 if (unread_count < 0)
1096 unread_count = 0 - unread_count;
1097 if (c == '\n' || unread_count >= MAX_UNREAD_CHARS)
1098 logwakeup();
1099 }
1100
1101
1102 /*
1103 * it is possible to increase the kernel log buffer size by adding
1104 * msgbuf=n
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.
1110 */
1111 int
1112 log_setsize(int size) {
1113 char *new_logdata;
1114 int new_logsize, new_bufr, new_bufx;
1115 char *old_logdata;
1116 int old_logsize, old_bufr, old_bufx;
1117 int i, count;
1118 char *p, ch;
1119
1120 if (size > MAX_MSG_BSIZE)
1121 return (EINVAL);
1122
1123 if (size <= 0)
1124 return (EINVAL);
1125
1126 new_logsize = size;
1127 if (!(new_logdata = (char*)kalloc(size))) {
1128 printf("log_setsize: unable to allocate memory\n");
1129 return (ENOMEM);
1130 }
1131 bzero(new_logdata, new_logsize);
1132
1133 LOG_LOCK();
1134
1135 old_logsize = msgbufp->msg_size;
1136 old_logdata = msgbufp->msg_bufc;
1137 old_bufr = msgbufp->msg_bufr;
1138 old_bufx = msgbufp->msg_bufx;
1139
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);
1142
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;
1147 } else {
1148 /*
1149 * if new buffer is bigger, copy what we have and let the
1150 * bzero above handle the difference
1151 */
1152 count = MIN(new_logsize, old_logsize);
1153 p = old_logdata + old_logsize - (count - old_bufx);
1154 }
1155 for (i = 0; i < count; i++) {
1156 if (p >= old_logdata + old_logsize)
1157 p = old_logdata;
1158
1159 ch = *p++;
1160 new_logdata[i] = ch;
1161 }
1162
1163 new_bufx = i;
1164 if (new_bufx >= new_logsize)
1165 new_bufx = 0;
1166 msgbufp->msg_bufx = new_bufx;
1167
1168 new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
1169 if (new_bufr < 0)
1170 new_bufr += old_logsize;
1171 new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
1172 if (new_bufr < 0)
1173 new_bufr += new_logsize;
1174 msgbufp->msg_bufr = new_bufr;
1175
1176 msgbufp->msg_size = new_logsize;
1177 msgbufp->msg_bufc = new_logdata;
1178
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);
1181
1182 LOG_UNLOCK();
1183
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);
1190 }
1191
1192 printf("set system log size to %d bytes\n", new_logsize);
1193
1194 return 0;
1195 }
1196
1197 void oslog_setsize(int size)
1198 {
1199 uint16_t scale = 0;
1200 // If the size is less than the default stream buffer
1201 // do nothing
1202 if (size <= OSLOG_STREAM_BUF_SIZE) {
1203 return;
1204 }
1205
1206 scale = (uint16_t) (size / OSLOG_STREAM_BUF_SIZE);
1207
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);
1211 }
1212
1213 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_kern_msgbuf, "I", "");
1214
1215 static int sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
1216 void *arg1 __unused,
1217 int arg2 __unused,
1218 struct sysctl_req *req)
1219 {
1220 int old_bufsize, bufsize;
1221 int error;
1222
1223 LOG_LOCK();
1224 old_bufsize = bufsize = msgbufp->msg_size;
1225 LOG_UNLOCK();
1226
1227 error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
1228 if (error)
1229 return (error);
1230
1231 if (bufsize != old_bufsize) {
1232 error = log_setsize(bufsize);
1233 }
1234
1235 return (error);
1236 }
1237
1238
1239 /*
1240 * This should be called by /sbin/dmesg only via libproc.
1241 * It returns as much data still in the buffer as possible.
1242 */
1243 int
1244 log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval) {
1245 uint32_t i;
1246 uint32_t localbuff_size;
1247 int error = 0, newl, skip;
1248 char *localbuff, *p, *copystart, ch;
1249 size_t copysize;
1250
1251 LOG_LOCK();
1252 localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
1253 LOG_UNLOCK();
1254
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");
1258 return (ENOMEM);
1259 }
1260
1261 /* in between here, the log could become bigger, but that's fine */
1262 LOG_LOCK();
1263
1264 /*
1265 * The message buffer is circular; start at the write pointer, and
1266 * make one loop up to write pointer - 1.
1267 */
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;
1272 ch = *p;
1273 /* Skip "\n<.*>" syslog sequences. */
1274 if (skip) {
1275 if (ch == '>')
1276 newl = skip = 0;
1277 continue;
1278 }
1279 if (newl && ch == '<') {
1280 skip = 1;
1281 continue;
1282 }
1283 if (ch == '\0')
1284 continue;
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.
1291 */
1292 if (i == (localbuff_size - 2))
1293 break;
1294 }
1295 if (!newl)
1296 localbuff[i++] = '\n';
1297 localbuff[i++] = 0;
1298
1299 if (buffersize >= i) {
1300 copystart = localbuff;
1301 copysize = i;
1302 } else {
1303 copystart = localbuff + i - buffersize;
1304 copysize = buffersize;
1305 }
1306
1307 LOG_UNLOCK();
1308
1309 error = copyout(copystart, buffer, copysize);
1310 if (!error)
1311 *retval = copysize;
1312
1313 kfree(localbuff, localbuff_size);
1314 return (error);
1315 }
1316