]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
xnu-3789.51.2.tar.gz
[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 = 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(get_task_map(current_task()),
790 &user_addr,
791 buffer_size,
792 0, /* mask */
793 VM_FLAGS_ANYWHERE,
794 mem_entry_ptr,
795 0, /* offset */
796 FALSE, /* copy */
797 VM_PROT_READ,
798 VM_PROT_READ,
799 VM_INHERIT_SHARE);
800 }
801
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));
806 }
807 break;
808 case LOGFLUSHED:
809 LOG_LOCK();
810 os_log_wakeup = 0;
811 LOG_UNLOCK();
812 __firehose_merge_updates(*(firehose_push_reply_t *)(data));
813 break;
814 default:
815 return (-1);
816 }
817 return (0);
818 }
819
820 /*ARGSUSED*/
821 int
822 oslog_streamioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
823 {
824 int err = 0;
825
826 lck_spin_lock(&oslog_stream_lock);
827
828 switch (com) {
829 case FIONBIO:
830 if (data && *(int *)data)
831 oslog_streamsoftc.sc_state |= LOG_NBIO;
832 else
833 oslog_streamsoftc.sc_state &= ~LOG_NBIO;
834 break;
835 case FIOASYNC:
836 if (data && *(int *)data)
837 oslog_streamsoftc.sc_state |= LOG_ASYNC;
838 else
839 oslog_streamsoftc.sc_state &= ~LOG_ASYNC;
840 break;
841 default:
842 err = -1;
843 break;
844 }
845
846 lck_spin_unlock(&oslog_stream_lock);
847 return err;
848 }
849
850 void
851 bsd_log_init(void)
852 {
853 /* After this point, we must be ready to accept characters */
854 }
855
856 void
857 oslog_init(void)
858 {
859 kern_return_t kr;
860 vm_size_t size = FIREHOSE_BUFFER_KERNEL_CHUNK_COUNT * FIREHOSE_CHUNK_SIZE;
861
862 oslog_lock_init();
863
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");
869 }
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);
874
875 kprintf("oslog_init completed\n");
876 }
877
878 /*
879 * log_putc_locked
880 *
881 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
882 * by the caller.
883 *
884 * Parameters: c Character to output
885 *
886 * Returns: (void)
887 *
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
891 * SMP reentrancy.
892 */
893 void
894 log_putc_locked(char c)
895 {
896 struct msgbuf *mbp;
897
898 mbp = msgbufp;
899 mbp->msg_bufc[mbp->msg_bufx++] = c;
900 if (mbp->msg_bufx >= msgbufp->msg_size)
901 mbp->msg_bufx = 0;
902 }
903
904 static oslog_stream_buf_entry_t
905 oslog_stream_find_free_buf_entry_locked(void)
906 {
907 struct msgbuf *mbp;
908 oslog_stream_buf_entry_t buf_entry = NULL;
909
910 lck_spin_assert(&oslog_stream_lock, LCK_ASSERT_OWNED);
911
912 mbp = oslog_streambufp;
913
914 buf_entry = STAILQ_FIRST(&oslog_stream_free_head);
915 if (buf_entry) {
916 STAILQ_REMOVE_HEAD(&oslog_stream_free_head, buf_entries);
917 }
918 else {
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;
922
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);
927 }
928
929 if (prev_entry == NULL) {
930 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
931 }
932 else {
933 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
934 }
935
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);
940 }
941 }
942
943 return buf_entry;
944 }
945
946 void
947 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry)
948 {
949 lck_spin_assert(&oslog_stream_lock, LCK_ASSERT_OWNED);
950 STAILQ_INSERT_TAIL(&oslog_stream_buf_head, m_entry, buf_entries);
951
952 return;
953 }
954
955 static void oslog_streamwrite_append_bytes(const char *buffer, int buflen)
956 {
957 struct msgbuf *mbp;
958
959 lck_spin_assert(&oslog_stream_lock, LCK_ASSERT_OWNED);
960
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);
965
966 mbp->msg_bufx += buflen;
967 if (mbp->msg_bufx == mbp->msg_size) {
968 mbp->msg_bufx = 0;
969 }
970 } else {
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);
974
975 buflen -= bytes_left;
976 buffer += bytes_left;
977
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;
981 }
982 return;
983 }
984
985
986 void
987 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid,
988 uint64_t stamp, const void *pubdata, size_t publen)
989 {
990 struct msgbuf *mbp;
991 int available_space = 0;
992 oslog_stream_buf_entry_t buf_entry = NULL;
993 oslog_stream_buf_entry_t next_entry = NULL;
994
995 uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data);
996 int ft_length = ft_size + publen;
997
998 lck_spin_assert(&oslog_stream_lock, LCK_ASSERT_OWNED);
999
1000 mbp = oslog_streambufp;
1001 if (ft_length > mbp->msg_size) {
1002 (void)hw_atomic_add(&oslog_s_error_count, 1);
1003 return;
1004 }
1005
1006 // Ensure that we have a list element for this record
1007 buf_entry = oslog_stream_find_free_buf_entry_locked();
1008
1009 assert(buf_entry != NULL);
1010
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;
1014 } else {
1015 available_space = mbp->msg_size - mbp->msg_bufx + mbp->msg_bufr;
1016 }
1017 while(ft_length > available_space) {
1018 oslog_stream_buf_entry_t prev_entry = NULL;
1019
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);
1025 }
1026
1027 if (prev_entry == NULL) {
1028 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
1029 }
1030 else {
1031 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
1032 }
1033
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);
1037 }
1038
1039 oslog_s_dropped_msgcount++;
1040 available_space += next_entry->size;
1041
1042 STAILQ_INSERT_TAIL(&oslog_stream_free_head, next_entry, buf_entries);
1043 }
1044
1045 assert(ft_length <= available_space);
1046
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;
1052
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,
1057 .ft_length = publen
1058 };
1059
1060 oslog_streamwrite_append_bytes((char *)&fs, sizeof(fs));
1061 oslog_streamwrite_append_bytes(pubdata, publen);
1062
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);
1066
1067 return;
1068 }
1069
1070
1071
1072 /*
1073 * log_putc
1074 *
1075 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1076 * held by the caller.
1077 *
1078 * Parameters: c Character to output
1079 *
1080 * Returns: (void)
1081 *
1082 * Notes: This function is used for syingle byte output to the log. It
1083 * primarily exists to maintain binary backward compatibility.
1084 */
1085 void
1086 log_putc(char c)
1087 {
1088 int unread_count = 0;
1089 LOG_LOCK();
1090 log_putc_locked(c);
1091 unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr;
1092 LOG_UNLOCK();
1093
1094 if (unread_count < 0)
1095 unread_count = 0 - unread_count;
1096 if (c == '\n' || unread_count >= MAX_UNREAD_CHARS)
1097 logwakeup();
1098 }
1099
1100
1101 /*
1102 * it is possible to increase the kernel log buffer size by adding
1103 * msgbuf=n
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.
1109 */
1110 int
1111 log_setsize(int size) {
1112 char *new_logdata;
1113 int new_logsize, new_bufr, new_bufx;
1114 char *old_logdata;
1115 int old_logsize, old_bufr, old_bufx;
1116 int i, count;
1117 char *p, ch;
1118
1119 if (size > MAX_MSG_BSIZE)
1120 return (EINVAL);
1121
1122 if (size <= 0)
1123 return (EINVAL);
1124
1125 new_logsize = size;
1126 if (!(new_logdata = (char*)kalloc(size))) {
1127 printf("log_setsize: unable to allocate memory\n");
1128 return (ENOMEM);
1129 }
1130 bzero(new_logdata, new_logsize);
1131
1132 LOG_LOCK();
1133
1134 old_logsize = msgbufp->msg_size;
1135 old_logdata = msgbufp->msg_bufc;
1136 old_bufr = msgbufp->msg_bufr;
1137 old_bufx = msgbufp->msg_bufx;
1138
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);
1141
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;
1146 } else {
1147 /*
1148 * if new buffer is bigger, copy what we have and let the
1149 * bzero above handle the difference
1150 */
1151 count = MIN(new_logsize, old_logsize);
1152 p = old_logdata + old_logsize - (count - old_bufx);
1153 }
1154 for (i = 0; i < count; i++) {
1155 if (p >= old_logdata + old_logsize)
1156 p = old_logdata;
1157
1158 ch = *p++;
1159 new_logdata[i] = ch;
1160 }
1161
1162 new_bufx = i;
1163 if (new_bufx >= new_logsize)
1164 new_bufx = 0;
1165 msgbufp->msg_bufx = new_bufx;
1166
1167 new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
1168 if (new_bufr < 0)
1169 new_bufr += old_logsize;
1170 new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
1171 if (new_bufr < 0)
1172 new_bufr += new_logsize;
1173 msgbufp->msg_bufr = new_bufr;
1174
1175 msgbufp->msg_size = new_logsize;
1176 msgbufp->msg_bufc = new_logdata;
1177
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);
1180
1181 LOG_UNLOCK();
1182
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);
1189 }
1190
1191 printf("set system log size to %d bytes\n", new_logsize);
1192
1193 return 0;
1194 }
1195
1196 void oslog_setsize(int size)
1197 {
1198 uint16_t scale = 0;
1199 // If the size is less than the default stream buffer
1200 // do nothing
1201 if (size <= OSLOG_STREAM_BUF_SIZE) {
1202 return;
1203 }
1204
1205 scale = (uint16_t) (size / OSLOG_STREAM_BUF_SIZE);
1206
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);
1210 }
1211
1212 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_kern_msgbuf, "I", "");
1213
1214 static int sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
1215 void *arg1 __unused,
1216 int arg2 __unused,
1217 struct sysctl_req *req)
1218 {
1219 int old_bufsize, bufsize;
1220 int error;
1221
1222 LOG_LOCK();
1223 old_bufsize = bufsize = msgbufp->msg_size;
1224 LOG_UNLOCK();
1225
1226 error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
1227 if (error)
1228 return (error);
1229
1230 if (bufsize != old_bufsize) {
1231 error = log_setsize(bufsize);
1232 }
1233
1234 return (error);
1235 }
1236
1237
1238 /*
1239 * This should be called by /sbin/dmesg only via libproc.
1240 * It returns as much data still in the buffer as possible.
1241 */
1242 int
1243 log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval) {
1244 uint32_t i;
1245 uint32_t localbuff_size;
1246 int error = 0, newl, skip;
1247 char *localbuff, *p, *copystart, ch;
1248 size_t copysize;
1249
1250 LOG_LOCK();
1251 localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
1252 LOG_UNLOCK();
1253
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");
1257 return (ENOMEM);
1258 }
1259
1260 /* in between here, the log could become bigger, but that's fine */
1261 LOG_LOCK();
1262
1263 /*
1264 * The message buffer is circular; start at the write pointer, and
1265 * make one loop up to write pointer - 1.
1266 */
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;
1271 ch = *p;
1272 /* Skip "\n<.*>" syslog sequences. */
1273 if (skip) {
1274 if (ch == '>')
1275 newl = skip = 0;
1276 continue;
1277 }
1278 if (newl && ch == '<') {
1279 skip = 1;
1280 continue;
1281 }
1282 if (ch == '\0')
1283 continue;
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.
1290 */
1291 if (i == (localbuff_size - 2))
1292 break;
1293 }
1294 if (!newl)
1295 localbuff[i++] = '\n';
1296 localbuff[i++] = 0;
1297
1298 if (buffersize >= i) {
1299 copystart = localbuff;
1300 copysize = i;
1301 } else {
1302 copystart = localbuff + i - buffersize;
1303 copysize = buffersize;
1304 }
1305
1306 LOG_UNLOCK();
1307
1308 error = copyout(copystart, buffer, copysize);
1309 if (!error)
1310 *retval = copysize;
1311
1312 kfree(localbuff, localbuff_size);
1313 return (error);
1314 }
1315