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(struct msgbuf *); |
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 | /* All globals should be accessed under LOG_LOCK() */ |
121 | |
122 | static char amsg_bufc[1024]; |
123 | static struct msgbuf aslbuf = {MSG_MAGIC, sizeof (amsg_bufc), 0, 0, amsg_bufc}; |
124 | struct msgbuf *aslbufp __attribute__((used)) = &aslbuf; |
125 | |
126 | /* logsoftc only valid while log_open=1 */ |
127 | struct logsoftc { |
128 | int sc_state; /* see above for possibilities */ |
129 | struct selinfo sc_selp; /* thread waiting for select */ |
130 | int sc_pgid; /* process/group for async I/O */ |
131 | struct msgbuf *sc_mbp; |
132 | } logsoftc; |
133 | |
134 | static int log_open; |
135 | char smsg_bufc[CONFIG_MSG_BSIZE]; /* static buffer */ |
136 | char oslog_stream_bufc[FIREHOSE_CHUNK_SIZE]; /* static buffer */ |
137 | struct firehose_chunk_s oslog_boot_buf = { |
138 | .fc_pos = { |
139 | .fcp_next_entry_offs = offsetof(struct firehose_chunk_s, fc_data), |
140 | .fcp_private_offs = FIREHOSE_CHUNK_SIZE, |
141 | .fcp_refcnt = 1, // indicate that there is a writer to this chunk |
142 | .fcp_stream = firehose_stream_persist, |
143 | .fcp_flag_io = 1, // for now, lets assume this is coming from the io bank |
144 | }, |
145 | }; /* static buffer */ |
146 | firehose_chunk_t firehose_boot_chunk = &oslog_boot_buf; |
147 | struct msgbuf msgbuf = {MSG_MAGIC, sizeof(smsg_bufc), 0, 0, smsg_bufc}; |
148 | struct msgbuf oslog_stream_buf = {MSG_MAGIC, 0, 0, 0, NULL}; |
149 | struct msgbuf *msgbufp __attribute__((used)) = &msgbuf; |
150 | struct msgbuf *oslog_streambufp __attribute__((used)) = &oslog_stream_buf; |
151 | |
152 | // List entries for keeping track of the streaming buffer |
153 | static oslog_stream_buf_entry_t oslog_stream_buf_entries; |
154 | |
155 | #define OSLOG_NUM_STREAM_ENTRIES 64 |
156 | #define OSLOG_STREAM_BUF_SIZE 4096 |
157 | |
158 | int oslog_open = 0; |
159 | int os_log_wakeup = 0; |
160 | int oslog_stream_open = 0; |
161 | int oslog_stream_buf_size = OSLOG_STREAM_BUF_SIZE; |
162 | int oslog_stream_num_entries = OSLOG_NUM_STREAM_ENTRIES; |
163 | |
164 | uint8_t __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT; |
165 | uint8_t __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES; |
166 | |
167 | /* oslogsoftc only valid while oslog_open=1 */ |
168 | struct oslogsoftc { |
169 | int sc_state; /* see above for possibilities */ |
170 | struct selinfo sc_selp; /* thread waiting for select */ |
171 | int sc_pgid; /* process/group for async I/O */ |
172 | } oslogsoftc; |
173 | |
174 | struct oslog_streamsoftc { |
175 | int sc_state; /* see above for possibilities */ |
176 | struct selinfo sc_selp; /* thread waiting for select */ |
177 | int sc_pgid; /* process/group for async I/O */ |
178 | } oslog_streamsoftc; |
179 | |
180 | STAILQ_HEAD(, oslog_stream_buf_entry_s) oslog_stream_free_head = |
181 | STAILQ_HEAD_INITIALIZER(oslog_stream_free_head); |
182 | STAILQ_HEAD(, oslog_stream_buf_entry_s) oslog_stream_buf_head = |
183 | STAILQ_HEAD_INITIALIZER(oslog_stream_buf_head); |
184 | |
185 | /* defined in osfmk/kern/printf.c */ |
186 | extern void oslog_lock_init(void); |
187 | extern void bsd_log_lock(void); |
188 | extern void bsd_log_unlock(void); |
189 | |
190 | /* defined for osfmk/kern/printf.c */ |
191 | void bsd_log_init(void); |
192 | |
193 | /* |
194 | * Ideally this file would define this lock, but bsd doesn't have the definition |
195 | * for lock groups. |
196 | */ |
197 | decl_lck_spin_data(extern, oslog_stream_lock) |
198 | |
199 | /* XXX wants a linker set so these can be static */ |
200 | extern d_open_t logopen; |
201 | extern d_close_t logclose; |
202 | extern d_read_t logread; |
203 | extern d_ioctl_t logioctl; |
204 | extern d_select_t logselect; |
205 | |
206 | /* XXX wants a linker set so these can be static */ |
207 | extern d_open_t oslogopen; |
208 | extern d_close_t oslogclose; |
209 | extern d_select_t oslogselect; |
210 | extern d_ioctl_t oslogioctl; |
211 | |
212 | /* XXX wants a linker set so these can be static */ |
213 | extern d_open_t oslog_streamopen; |
214 | extern d_close_t oslog_streamclose; |
215 | extern d_read_t oslog_streamread; |
216 | extern d_ioctl_t oslog_streamioctl; |
217 | extern d_select_t oslog_streamselect; |
218 | |
219 | void oslog_init(void); |
220 | void oslog_setsize(int size); |
221 | void oslog_streamwrite_locked(firehose_tracepoint_id_u ftid, |
222 | uint64_t stamp, const void *pubdata, size_t publen); |
223 | void oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry); |
224 | static oslog_stream_buf_entry_t oslog_stream_find_free_buf_entry_locked(void); |
225 | static void oslog_streamwrite_append_bytes(const char *buffer, int buflen); |
226 | |
227 | /* |
228 | * Serialize log access. Note that the log can be written at interrupt level, |
229 | * so any log manipulations that can be done from, or affect, another processor |
230 | * at interrupt level must be guarded with a spin lock. |
231 | */ |
232 | |
233 | #define LOG_LOCK() bsd_log_lock() |
234 | #define LOG_UNLOCK() bsd_log_unlock() |
235 | |
236 | #if DEBUG |
237 | #define LOG_SETSIZE_DEBUG(x...) kprintf(x) |
238 | #else |
239 | #define LOG_SETSIZE_DEBUG(x...) do { } while(0) |
240 | #endif |
241 | |
242 | static int sysctl_kern_msgbuf(struct sysctl_oid *oidp, |
243 | void *arg1, int arg2, struct sysctl_req *req); |
244 | |
245 | /*ARGSUSED*/ |
246 | int |
247 | logopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p) |
248 | { |
249 | LOG_LOCK(); |
250 | if (log_open) { |
251 | LOG_UNLOCK(); |
252 | return (EBUSY); |
253 | } |
254 | if (atm_get_diagnostic_config() & ATM_ENABLE_LEGACY_LOGGING) { |
255 | logsoftc.sc_mbp = msgbufp; |
256 | } else { |
257 | /* |
258 | * Support for messagetracer (kern_asl_msg()) |
259 | * In this mode, /dev/klog exports only ASL-formatted messages |
260 | * written into aslbufp via vaddlog(). |
261 | */ |
262 | logsoftc.sc_mbp = aslbufp; |
263 | } |
264 | logsoftc.sc_pgid = p->p_pid; /* signal process only */ |
265 | log_open = 1; |
266 | |
267 | LOG_UNLOCK(); |
268 | |
269 | return (0); |
270 | } |
271 | |
272 | /*ARGSUSED*/ |
273 | int |
274 | logclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p) |
275 | { |
276 | LOG_LOCK(); |
277 | logsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC); |
278 | selwakeup(&logsoftc.sc_selp); |
279 | selthreadclear(&logsoftc.sc_selp); |
280 | log_open = 0; |
281 | LOG_UNLOCK(); |
282 | return (0); |
283 | } |
284 | |
285 | |
286 | int |
287 | oslogopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p) |
288 | { |
289 | LOG_LOCK(); |
290 | if (oslog_open) { |
291 | LOG_UNLOCK(); |
292 | return(EBUSY); |
293 | } |
294 | oslogsoftc.sc_pgid = p->p_pid; /* signal process only */ |
295 | oslog_open = 1; |
296 | |
297 | LOG_UNLOCK(); |
298 | return (0); |
299 | } |
300 | |
301 | int |
302 | oslogclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p) |
303 | { |
304 | LOG_LOCK(); |
305 | oslogsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC); |
306 | selwakeup(&oslogsoftc.sc_selp); |
307 | selthreadclear(&oslogsoftc.sc_selp); |
308 | oslog_open = 0; |
309 | LOG_UNLOCK(); |
310 | return (0); |
311 | } |
312 | |
313 | int |
314 | oslog_streamopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p) |
315 | { |
316 | char *oslog_stream_msg_bufc = NULL; |
317 | oslog_stream_buf_entry_t entries = NULL; |
318 | |
319 | lck_spin_lock(&oslog_stream_lock); |
320 | if (oslog_stream_open) { |
321 | lck_spin_unlock(&oslog_stream_lock); |
322 | return EBUSY; |
323 | } |
324 | lck_spin_unlock(&oslog_stream_lock); |
325 | |
326 | // Allocate the stream buffer |
327 | oslog_stream_msg_bufc = kalloc(oslog_stream_buf_size); |
328 | if (!oslog_stream_msg_bufc) { |
329 | return ENOMEM; |
330 | } |
331 | |
332 | /* entries to support kernel logging in stream mode */ |
333 | entries = kalloc(oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s)); |
334 | if (!entries) { |
335 | kfree(oslog_stream_msg_bufc, oslog_stream_buf_size); |
336 | return ENOMEM; |
337 | } |
338 | |
339 | lck_spin_lock(&oslog_stream_lock); |
340 | if (oslog_stream_open) { |
341 | lck_spin_unlock(&oslog_stream_lock); |
342 | kfree(oslog_stream_msg_bufc, oslog_stream_buf_size); |
343 | kfree(entries, oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s)); |
344 | return EBUSY; |
345 | } |
346 | |
347 | assert(oslog_streambufp->msg_bufc == NULL); |
348 | oslog_streambufp->msg_bufc = oslog_stream_msg_bufc; |
349 | oslog_streambufp->msg_size = oslog_stream_buf_size; |
350 | |
351 | oslog_stream_buf_entries = entries; |
352 | |
353 | STAILQ_INIT(&oslog_stream_free_head); |
354 | STAILQ_INIT(&oslog_stream_buf_head); |
355 | |
356 | for (int i = 0; i < oslog_stream_num_entries; i++) { |
357 | oslog_stream_buf_entries[i].type = oslog_stream_link_type_log; |
358 | oslog_stream_buf_entries[i].offset = 0; |
359 | oslog_stream_buf_entries[i].size = 0; |
360 | oslog_stream_buf_entries[i].timestamp = 0; |
361 | STAILQ_INSERT_TAIL(&oslog_stream_free_head, &oslog_stream_buf_entries[i], buf_entries); |
362 | } |
363 | |
364 | /* there should be no pending entries in the stream */ |
365 | assert(STAILQ_EMPTY(&oslog_stream_buf_head)); |
366 | assert(oslog_streambufp->msg_bufx == 0); |
367 | assert(oslog_streambufp->msg_bufr == 0); |
368 | |
369 | oslog_streambufp->msg_bufx = 0; |
370 | oslog_streambufp->msg_bufr = 0; |
371 | oslog_streamsoftc.sc_pgid = p->p_pid; /* signal process only */ |
372 | oslog_stream_open = 1; |
373 | lck_spin_unlock(&oslog_stream_lock); |
374 | |
375 | return 0; |
376 | } |
377 | |
378 | int |
379 | oslog_streamclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p) |
380 | { |
381 | oslog_stream_buf_entry_t next_entry = NULL; |
382 | char *oslog_stream_msg_bufc = NULL; |
383 | oslog_stream_buf_entry_t entries = NULL; |
384 | |
385 | lck_spin_lock(&oslog_stream_lock); |
386 | |
387 | if (oslog_stream_open == 0) { |
388 | lck_spin_unlock(&oslog_stream_lock); |
389 | return EBADF; |
390 | } |
391 | |
392 | // Consume all log lines |
393 | while (!STAILQ_EMPTY(&oslog_stream_buf_head)) { |
394 | next_entry = STAILQ_FIRST(&oslog_stream_buf_head); |
395 | STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries); |
396 | } |
397 | oslog_streamwakeup_locked(); |
398 | oslog_streamsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC); |
399 | selwakeup(&oslog_streamsoftc.sc_selp); |
400 | selthreadclear(&oslog_streamsoftc.sc_selp); |
401 | oslog_stream_open = 0; |
402 | oslog_streambufp->msg_bufr = 0; |
403 | oslog_streambufp->msg_bufx = 0; |
404 | oslog_stream_msg_bufc = oslog_streambufp->msg_bufc; |
405 | oslog_streambufp->msg_bufc = NULL; |
406 | entries = oslog_stream_buf_entries; |
407 | oslog_stream_buf_entries = NULL; |
408 | oslog_streambufp->msg_size = 0; |
409 | |
410 | lck_spin_unlock(&oslog_stream_lock); |
411 | |
412 | // Free the stream buffer |
413 | kfree(oslog_stream_msg_bufc, oslog_stream_buf_size); |
414 | // Free the list entries |
415 | kfree(entries, oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s)); |
416 | |
417 | return 0; |
418 | } |
419 | |
420 | /*ARGSUSED*/ |
421 | int |
422 | logread(__unused dev_t dev, struct uio *uio, int flag) |
423 | { |
424 | int l; |
425 | int error = 0; |
426 | struct msgbuf *mbp = logsoftc.sc_mbp; |
427 | |
428 | LOG_LOCK(); |
429 | while (mbp->msg_bufr == mbp->msg_bufx) { |
430 | if (flag & IO_NDELAY) { |
431 | error = EWOULDBLOCK; |
432 | goto out; |
433 | } |
434 | if (logsoftc.sc_state & LOG_NBIO) { |
435 | error = EWOULDBLOCK; |
436 | goto out; |
437 | } |
438 | logsoftc.sc_state |= LOG_RDWAIT; |
439 | LOG_UNLOCK(); |
440 | /* |
441 | * If the wakeup is missed |
442 | * then wait for 5 sec and reevaluate |
443 | */ |
444 | if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, |
445 | "klog" , 5 * hz)) != 0) { |
446 | /* if it times out; ignore */ |
447 | if (error != EWOULDBLOCK) |
448 | return (error); |
449 | } |
450 | LOG_LOCK(); |
451 | } |
452 | logsoftc.sc_state &= ~LOG_RDWAIT; |
453 | |
454 | while (uio_resid(uio) > 0) { |
455 | int readpos; |
456 | |
457 | l = mbp->msg_bufx - mbp->msg_bufr; |
458 | if (l < 0) |
459 | l = mbp->msg_size - mbp->msg_bufr; |
460 | l = min(l, uio_resid(uio)); |
461 | if (l == 0) |
462 | break; |
463 | |
464 | readpos = mbp->msg_bufr; |
465 | LOG_UNLOCK(); |
466 | error = uiomove((caddr_t)&mbp->msg_bufc[readpos], l, uio); |
467 | LOG_LOCK(); |
468 | if (error) |
469 | break; |
470 | mbp->msg_bufr = readpos + l; |
471 | if (mbp->msg_bufr >= mbp->msg_size) |
472 | mbp->msg_bufr = 0; |
473 | } |
474 | out: |
475 | LOG_UNLOCK(); |
476 | return (error); |
477 | } |
478 | |
479 | /*ARGSUSED*/ |
480 | int |
481 | oslog_streamread(__unused dev_t dev, struct uio *uio, int flag) |
482 | { |
483 | int error = 0; |
484 | int copy_size = 0; |
485 | static char logline[FIREHOSE_CHUNK_SIZE]; |
486 | |
487 | lck_spin_lock(&oslog_stream_lock); |
488 | |
489 | if (!oslog_stream_open) { |
490 | lck_spin_unlock(&oslog_stream_lock); |
491 | return EBADF; |
492 | } |
493 | |
494 | while (STAILQ_EMPTY(&oslog_stream_buf_head)) { |
495 | if (flag & IO_NDELAY || oslog_streamsoftc.sc_state & LOG_NBIO) { |
496 | lck_spin_unlock(&oslog_stream_lock); |
497 | return EWOULDBLOCK; |
498 | } |
499 | |
500 | oslog_streamsoftc.sc_state |= LOG_RDWAIT; |
501 | wait_result_t wr = assert_wait((event_t)oslog_streambufp, |
502 | THREAD_INTERRUPTIBLE); |
503 | if (wr == THREAD_WAITING) { |
504 | lck_spin_unlock(&oslog_stream_lock); |
505 | wr = thread_block(THREAD_CONTINUE_NULL); |
506 | lck_spin_lock(&oslog_stream_lock); |
507 | } |
508 | |
509 | switch (wr) { |
510 | case THREAD_AWAKENED: |
511 | case THREAD_TIMED_OUT: |
512 | break; |
513 | default: |
514 | lck_spin_unlock(&oslog_stream_lock); |
515 | return EINTR; |
516 | } |
517 | } |
518 | |
519 | if (!oslog_stream_open) { |
520 | lck_spin_unlock(&oslog_stream_lock); |
521 | return EBADF; |
522 | } |
523 | |
524 | int logpos = 0; |
525 | oslog_stream_buf_entry_t read_entry = NULL; |
526 | uint16_t rec_length; |
527 | |
528 | read_entry = STAILQ_FIRST(&oslog_stream_buf_head); |
529 | assert(read_entry != NULL); |
530 | STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries); |
531 | |
532 | // Copy the timestamp first |
533 | memcpy(logline + logpos, &read_entry->timestamp, sizeof(uint64_t)); |
534 | logpos += sizeof(uint64_t); |
535 | |
536 | switch (read_entry->type) { |
537 | /* Handle metadata messages */ |
538 | case oslog_stream_link_type_metadata: |
539 | { |
540 | memcpy(logline + logpos, |
541 | (read_entry->metadata), read_entry->size); |
542 | logpos += read_entry->size; |
543 | |
544 | lck_spin_unlock(&oslog_stream_lock); |
545 | |
546 | // Free the list entry |
547 | kfree(read_entry, (sizeof(struct oslog_stream_buf_entry_s) + read_entry->size)); |
548 | break; |
549 | } |
550 | /* Handle log messages */ |
551 | case oslog_stream_link_type_log: |
552 | { |
553 | /* ensure that the correct read entry was dequeued */ |
554 | assert(read_entry->offset == oslog_streambufp->msg_bufr); |
555 | rec_length = read_entry->size; |
556 | |
557 | // If the next log line is contiguous in the buffer, copy it out. |
558 | if(read_entry->offset + rec_length <= oslog_streambufp->msg_size) { |
559 | memcpy(logline + logpos, |
560 | oslog_streambufp->msg_bufc + read_entry->offset, rec_length); |
561 | |
562 | oslog_streambufp->msg_bufr += rec_length; |
563 | if (oslog_streambufp->msg_bufr == oslog_streambufp->msg_size) { |
564 | oslog_streambufp->msg_bufr = 0; |
565 | } |
566 | logpos += rec_length; |
567 | } else { |
568 | // Otherwise, copy until the end of the buffer, and |
569 | // copy the remaining bytes starting at index 0. |
570 | int bytes_left = oslog_streambufp->msg_size - read_entry->offset; |
571 | memcpy(logline + logpos, |
572 | oslog_streambufp->msg_bufc + read_entry->offset, bytes_left); |
573 | logpos += bytes_left; |
574 | rec_length -= bytes_left; |
575 | |
576 | memcpy(logline + logpos, (const void *)oslog_streambufp->msg_bufc, |
577 | rec_length); |
578 | oslog_streambufp->msg_bufr = rec_length; |
579 | logpos += rec_length; |
580 | } |
581 | assert(oslog_streambufp->msg_bufr < oslog_streambufp->msg_size); |
582 | STAILQ_INSERT_TAIL(&oslog_stream_free_head, read_entry, buf_entries); |
583 | |
584 | lck_spin_unlock(&oslog_stream_lock); |
585 | break; |
586 | } |
587 | default: |
588 | { |
589 | panic("Got unexpected log entry type: %hhu\n" , read_entry->type); |
590 | } |
591 | } |
592 | |
593 | copy_size = min(logpos, uio_resid(uio)); |
594 | if (copy_size != 0) { |
595 | error = uiomove((caddr_t)logline, copy_size, uio); |
596 | } |
597 | (void)hw_atomic_add(&oslog_s_streamed_msgcount, 1); |
598 | |
599 | return error; |
600 | } |
601 | |
602 | /*ARGSUSED*/ |
603 | int |
604 | logselect(__unused dev_t dev, int rw, void * wql, struct proc *p) |
605 | { |
606 | const struct msgbuf *mbp = logsoftc.sc_mbp; |
607 | |
608 | switch (rw) { |
609 | |
610 | case FREAD: |
611 | LOG_LOCK(); |
612 | if (mbp->msg_bufr != mbp->msg_bufx) { |
613 | LOG_UNLOCK(); |
614 | return (1); |
615 | } |
616 | selrecord(p, &logsoftc.sc_selp, wql); |
617 | LOG_UNLOCK(); |
618 | break; |
619 | } |
620 | return (0); |
621 | } |
622 | |
623 | int |
624 | oslogselect(__unused dev_t dev, int rw, void * wql, struct proc *p) |
625 | { |
626 | switch (rw) { |
627 | |
628 | case FREAD: |
629 | LOG_LOCK(); |
630 | if (os_log_wakeup) { |
631 | LOG_UNLOCK(); |
632 | return (1); |
633 | } |
634 | selrecord(p, &oslogsoftc.sc_selp, wql); |
635 | LOG_UNLOCK(); |
636 | break; |
637 | } |
638 | return (0); |
639 | } |
640 | |
641 | int |
642 | oslog_streamselect(__unused dev_t dev, int rw, void * wql, struct proc *p) |
643 | { |
644 | int ret = 0; |
645 | |
646 | lck_spin_lock(&oslog_stream_lock); |
647 | |
648 | switch (rw) { |
649 | case FREAD: |
650 | if (STAILQ_EMPTY(&oslog_stream_buf_head)) { |
651 | selrecord(p, &oslog_streamsoftc.sc_selp, wql); |
652 | } else { |
653 | ret = 1; |
654 | } |
655 | break; |
656 | } |
657 | |
658 | lck_spin_unlock(&oslog_stream_lock); |
659 | return ret; |
660 | } |
661 | |
662 | void |
663 | logwakeup(struct msgbuf *mbp) |
664 | { |
665 | /* cf. r24974766 & r25201228*/ |
666 | if (oslog_is_safe() == FALSE) { |
667 | return; |
668 | } |
669 | |
670 | LOG_LOCK(); |
671 | if (!log_open) { |
672 | LOG_UNLOCK(); |
673 | return; |
674 | } |
675 | if (NULL == mbp) |
676 | mbp = logsoftc.sc_mbp; |
677 | if (mbp != logsoftc.sc_mbp) |
678 | goto out; |
679 | selwakeup(&logsoftc.sc_selp); |
680 | if (logsoftc.sc_state & LOG_ASYNC) { |
681 | int pgid = logsoftc.sc_pgid; |
682 | LOG_UNLOCK(); |
683 | if (pgid < 0) |
684 | gsignal(-pgid, SIGIO); |
685 | else |
686 | proc_signal(pgid, SIGIO); |
687 | LOG_LOCK(); |
688 | } |
689 | if (logsoftc.sc_state & LOG_RDWAIT) { |
690 | wakeup((caddr_t)mbp); |
691 | logsoftc.sc_state &= ~LOG_RDWAIT; |
692 | } |
693 | out: |
694 | LOG_UNLOCK(); |
695 | } |
696 | |
697 | void |
698 | oslogwakeup(void) |
699 | { |
700 | LOG_LOCK(); |
701 | if (!oslog_open) { |
702 | LOG_UNLOCK(); |
703 | return; |
704 | } |
705 | selwakeup(&oslogsoftc.sc_selp); |
706 | os_log_wakeup = 1; |
707 | LOG_UNLOCK(); |
708 | } |
709 | |
710 | static void |
711 | oslog_streamwakeup_locked(void) |
712 | { |
713 | LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED); |
714 | if (!oslog_stream_open) { |
715 | return; |
716 | } |
717 | selwakeup(&oslog_streamsoftc.sc_selp); |
718 | if (oslog_streamsoftc.sc_state & LOG_RDWAIT) { |
719 | wakeup((caddr_t)oslog_streambufp); |
720 | oslog_streamsoftc.sc_state &= ~LOG_RDWAIT; |
721 | } |
722 | } |
723 | |
724 | void |
725 | oslog_streamwakeup(void) |
726 | { |
727 | /* cf. r24974766 & r25201228*/ |
728 | if (oslog_is_safe() == FALSE) { |
729 | return; |
730 | } |
731 | |
732 | lck_spin_lock(&oslog_stream_lock); |
733 | oslog_streamwakeup_locked(); |
734 | lck_spin_unlock(&oslog_stream_lock); |
735 | } |
736 | |
737 | /*ARGSUSED*/ |
738 | int |
739 | logioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p) |
740 | { |
741 | int l; |
742 | const struct msgbuf *mbp = logsoftc.sc_mbp; |
743 | |
744 | LOG_LOCK(); |
745 | switch (com) { |
746 | |
747 | /* return number of characters immediately available */ |
748 | case FIONREAD: |
749 | l = mbp->msg_bufx - mbp->msg_bufr; |
750 | if (l < 0) |
751 | l += mbp->msg_size; |
752 | *(off_t *)data = l; |
753 | break; |
754 | |
755 | case FIONBIO: |
756 | if (*(int *)data) |
757 | logsoftc.sc_state |= LOG_NBIO; |
758 | else |
759 | logsoftc.sc_state &= ~LOG_NBIO; |
760 | break; |
761 | |
762 | case FIOASYNC: |
763 | if (*(int *)data) |
764 | logsoftc.sc_state |= LOG_ASYNC; |
765 | else |
766 | logsoftc.sc_state &= ~LOG_ASYNC; |
767 | break; |
768 | |
769 | case TIOCSPGRP: |
770 | logsoftc.sc_pgid = *(int *)data; |
771 | break; |
772 | |
773 | case TIOCGPGRP: |
774 | *(int *)data = logsoftc.sc_pgid; |
775 | break; |
776 | |
777 | default: |
778 | LOG_UNLOCK(); |
779 | return (-1); |
780 | } |
781 | LOG_UNLOCK(); |
782 | return (0); |
783 | } |
784 | |
785 | /*ARGSUSED*/ |
786 | int |
787 | oslogioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p) |
788 | { |
789 | int ret = 0; |
790 | mach_vm_size_t buffer_size = (__firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE); |
791 | firehose_buffer_map_info_t map_info = {0, 0}; |
792 | firehose_buffer_t kernel_firehose_buffer = NULL; |
793 | mach_vm_address_t user_addr = 0; |
794 | mach_port_t mem_entry_ptr = MACH_PORT_NULL; |
795 | |
796 | switch (com) { |
797 | |
798 | /* return number of characters immediately available */ |
799 | |
800 | case LOGBUFFERMAP: |
801 | kernel_firehose_buffer = (firehose_buffer_t)kernel_firehose_addr; |
802 | |
803 | ret = mach_make_memory_entry_64(kernel_map, |
804 | &buffer_size, |
805 | (mach_vm_offset_t) kernel_firehose_buffer, |
806 | ( MAP_MEM_VM_SHARE | VM_PROT_READ ), |
807 | &mem_entry_ptr, |
808 | MACH_PORT_NULL); |
809 | if (ret == KERN_SUCCESS) { |
810 | ret = mach_vm_map_kernel(get_task_map(current_task()), |
811 | &user_addr, |
812 | buffer_size, |
813 | 0, /* mask */ |
814 | VM_FLAGS_ANYWHERE, |
815 | VM_MAP_KERNEL_FLAGS_NONE, |
816 | VM_KERN_MEMORY_NONE, |
817 | mem_entry_ptr, |
818 | 0, /* offset */ |
819 | FALSE, /* copy */ |
820 | VM_PROT_READ, |
821 | VM_PROT_READ, |
822 | VM_INHERIT_SHARE); |
823 | } |
824 | |
825 | if (ret == KERN_SUCCESS) { |
826 | map_info.fbmi_addr = (uint64_t) (user_addr); |
827 | map_info.fbmi_size = buffer_size; |
828 | bcopy(&map_info, data, sizeof(firehose_buffer_map_info_t)); |
829 | } |
830 | break; |
831 | case LOGFLUSHED: |
832 | LOG_LOCK(); |
833 | os_log_wakeup = 0; |
834 | LOG_UNLOCK(); |
835 | __firehose_merge_updates(*(firehose_push_reply_t *)(data)); |
836 | break; |
837 | default: |
838 | return (-1); |
839 | } |
840 | return (0); |
841 | } |
842 | |
843 | /*ARGSUSED*/ |
844 | int |
845 | oslog_streamioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p) |
846 | { |
847 | int err = 0; |
848 | |
849 | lck_spin_lock(&oslog_stream_lock); |
850 | |
851 | switch (com) { |
852 | case FIONBIO: |
853 | if (data && *(int *)data) |
854 | oslog_streamsoftc.sc_state |= LOG_NBIO; |
855 | else |
856 | oslog_streamsoftc.sc_state &= ~LOG_NBIO; |
857 | break; |
858 | case FIOASYNC: |
859 | if (data && *(int *)data) |
860 | oslog_streamsoftc.sc_state |= LOG_ASYNC; |
861 | else |
862 | oslog_streamsoftc.sc_state &= ~LOG_ASYNC; |
863 | break; |
864 | default: |
865 | err = -1; |
866 | break; |
867 | } |
868 | |
869 | lck_spin_unlock(&oslog_stream_lock); |
870 | return err; |
871 | } |
872 | |
873 | void |
874 | bsd_log_init(void) |
875 | { |
876 | /* After this point, we must be ready to accept characters */ |
877 | } |
878 | |
879 | void |
880 | oslog_init(void) |
881 | { |
882 | kern_return_t kr; |
883 | if (!PE_parse_boot_argn("firehose_chunk_count" , &__firehose_buffer_kernel_chunk_count, sizeof(__firehose_buffer_kernel_chunk_count))) { |
884 | __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT; |
885 | } |
886 | if (!PE_parse_boot_argn("firehose_io_pages" , &__firehose_num_kernel_io_pages, sizeof(__firehose_num_kernel_io_pages))) { |
887 | __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES; |
888 | } |
889 | if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages)) { |
890 | printf("illegal firehose configuration %u/%u, using defaults\n" , __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages); |
891 | __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT; |
892 | __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES; |
893 | } |
894 | vm_size_t size = __firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE; |
895 | |
896 | oslog_lock_init(); |
897 | |
898 | kr = kmem_alloc_flags(kernel_map, &kernel_firehose_addr, |
899 | size + (2 * PAGE_SIZE), VM_KERN_MEMORY_LOG, |
900 | KMA_GUARD_FIRST | KMA_GUARD_LAST); |
901 | if (kr != KERN_SUCCESS) { |
902 | panic("Failed to allocate memory for firehose logging buffer" ); |
903 | } |
904 | kernel_firehose_addr += PAGE_SIZE; |
905 | bzero((void *)kernel_firehose_addr, size); |
906 | /* register buffer with firehose */ |
907 | kernel_firehose_addr = (vm_offset_t)__firehose_buffer_create((size_t *) &size); |
908 | |
909 | printf("oslog_init completed, %u chunks, %u io pages\n" , __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages); |
910 | } |
911 | |
912 | /* |
913 | * log_putc_locked |
914 | * |
915 | * Decription: Output a character to the log; assumes the LOG_LOCK() is held |
916 | * by the caller. |
917 | * |
918 | * Parameters: c Character to output |
919 | * |
920 | * Returns: (void) |
921 | * |
922 | * Notes: This functions is used for multibyte output to the log; it |
923 | * should be used preferrentially where possible to ensure that |
924 | * log entries do not end up interspersed due to preemption or |
925 | * SMP reentrancy. |
926 | */ |
927 | void |
928 | log_putc_locked(struct msgbuf *mbp, char c) |
929 | { |
930 | mbp->msg_bufc[mbp->msg_bufx++] = c; |
931 | if (mbp->msg_bufx >= mbp->msg_size) |
932 | mbp->msg_bufx = 0; |
933 | } |
934 | |
935 | static oslog_stream_buf_entry_t |
936 | oslog_stream_find_free_buf_entry_locked(void) |
937 | { |
938 | struct msgbuf *mbp; |
939 | oslog_stream_buf_entry_t buf_entry = NULL; |
940 | |
941 | LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED); |
942 | |
943 | mbp = oslog_streambufp; |
944 | |
945 | buf_entry = STAILQ_FIRST(&oslog_stream_free_head); |
946 | if (buf_entry) { |
947 | STAILQ_REMOVE_HEAD(&oslog_stream_free_head, buf_entries); |
948 | } |
949 | else { |
950 | // If no list elements are available in the free-list, |
951 | // consume the next log line so we can free up its list element |
952 | oslog_stream_buf_entry_t prev_entry = NULL; |
953 | |
954 | buf_entry = STAILQ_FIRST(&oslog_stream_buf_head); |
955 | while (buf_entry->type == oslog_stream_link_type_metadata) { |
956 | prev_entry = buf_entry; |
957 | buf_entry = STAILQ_NEXT(buf_entry, buf_entries); |
958 | } |
959 | |
960 | if (prev_entry == NULL) { |
961 | STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries); |
962 | } |
963 | else { |
964 | STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries); |
965 | } |
966 | |
967 | mbp->msg_bufr += buf_entry->size; |
968 | oslog_s_dropped_msgcount++; |
969 | if (mbp->msg_bufr >= mbp->msg_size) { |
970 | mbp->msg_bufr = (mbp->msg_bufr % mbp->msg_size); |
971 | } |
972 | } |
973 | |
974 | return buf_entry; |
975 | } |
976 | |
977 | void |
978 | oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry) |
979 | { |
980 | LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED); |
981 | STAILQ_INSERT_TAIL(&oslog_stream_buf_head, m_entry, buf_entries); |
982 | |
983 | return; |
984 | } |
985 | |
986 | static void |
987 | oslog_streamwrite_append_bytes(const char *buffer, int buflen) |
988 | { |
989 | struct msgbuf *mbp; |
990 | |
991 | LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED); |
992 | |
993 | mbp = oslog_streambufp; |
994 | // Check if we have enough space in the stream buffer to write the data |
995 | if (mbp->msg_bufx + buflen <= mbp->msg_size) { |
996 | memcpy((void *)(mbp->msg_bufc + mbp->msg_bufx), buffer, buflen); |
997 | |
998 | mbp->msg_bufx += buflen; |
999 | if (mbp->msg_bufx == mbp->msg_size) { |
1000 | mbp->msg_bufx = 0; |
1001 | } |
1002 | } else { |
1003 | // Copy part of the data until the end of the stream |
1004 | int bytes_left = mbp->msg_size - mbp->msg_bufx; |
1005 | memcpy((void *)(mbp->msg_bufc + mbp->msg_bufx), buffer, bytes_left); |
1006 | |
1007 | buflen -= bytes_left; |
1008 | buffer += bytes_left; |
1009 | |
1010 | // Copy the remainder of the data from the beginning of stream |
1011 | memcpy((void *)mbp->msg_bufc, buffer, buflen); |
1012 | mbp->msg_bufx = buflen; |
1013 | } |
1014 | return; |
1015 | } |
1016 | |
1017 | |
1018 | void |
1019 | oslog_streamwrite_locked(firehose_tracepoint_id_u ftid, |
1020 | uint64_t stamp, const void *pubdata, size_t publen) |
1021 | { |
1022 | struct msgbuf *mbp; |
1023 | int available_space = 0; |
1024 | oslog_stream_buf_entry_t buf_entry = NULL; |
1025 | oslog_stream_buf_entry_t next_entry = NULL; |
1026 | |
1027 | uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data); |
1028 | int ft_length = ft_size + publen; |
1029 | |
1030 | LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED); |
1031 | |
1032 | mbp = oslog_streambufp; |
1033 | if (ft_length > mbp->msg_size) { |
1034 | (void)hw_atomic_add(&oslog_s_error_count, 1); |
1035 | return; |
1036 | } |
1037 | |
1038 | // Ensure that we have a list element for this record |
1039 | buf_entry = oslog_stream_find_free_buf_entry_locked(); |
1040 | |
1041 | assert(buf_entry != NULL); |
1042 | |
1043 | // Ensure that we have space in the ring buffer for the current logline |
1044 | if (mbp->msg_bufr > mbp->msg_bufx) { |
1045 | available_space = mbp->msg_bufr - mbp->msg_bufx; |
1046 | } else { |
1047 | available_space = mbp->msg_size - mbp->msg_bufx + mbp->msg_bufr; |
1048 | } |
1049 | while(ft_length > available_space) { |
1050 | oslog_stream_buf_entry_t prev_entry = NULL; |
1051 | |
1052 | next_entry = STAILQ_FIRST(&oslog_stream_buf_head); |
1053 | assert(next_entry != NULL); |
1054 | while (next_entry->type == oslog_stream_link_type_metadata) { |
1055 | prev_entry = next_entry; |
1056 | next_entry = STAILQ_NEXT(next_entry, buf_entries); |
1057 | } |
1058 | |
1059 | if (prev_entry == NULL) { |
1060 | STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries); |
1061 | } |
1062 | else { |
1063 | STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries); |
1064 | } |
1065 | |
1066 | mbp->msg_bufr += next_entry->size; |
1067 | if (mbp->msg_bufr >= mbp->msg_size) { |
1068 | mbp->msg_bufr = (mbp->msg_bufr % mbp->msg_size); |
1069 | } |
1070 | |
1071 | oslog_s_dropped_msgcount++; |
1072 | available_space += next_entry->size; |
1073 | |
1074 | STAILQ_INSERT_TAIL(&oslog_stream_free_head, next_entry, buf_entries); |
1075 | } |
1076 | |
1077 | assert(ft_length <= available_space); |
1078 | |
1079 | // Write the log line and update the list entry for this record |
1080 | buf_entry->offset = mbp->msg_bufx; |
1081 | buf_entry->size = ft_length; |
1082 | buf_entry->timestamp = stamp; |
1083 | buf_entry->type = oslog_stream_link_type_log; |
1084 | |
1085 | // Construct a tracepoint |
1086 | struct firehose_tracepoint_s fs = { |
1087 | .ft_thread = thread_tid(current_thread()), |
1088 | .ft_id.ftid_value = ftid.ftid_value, |
1089 | .ft_length = publen |
1090 | }; |
1091 | |
1092 | oslog_streamwrite_append_bytes((char *)&fs, sizeof(fs)); |
1093 | oslog_streamwrite_append_bytes(pubdata, publen); |
1094 | |
1095 | assert(mbp->msg_bufr < mbp->msg_size); |
1096 | // Insert the element to the buffer data list |
1097 | STAILQ_INSERT_TAIL(&oslog_stream_buf_head, buf_entry, buf_entries); |
1098 | |
1099 | return; |
1100 | } |
1101 | |
1102 | |
1103 | |
1104 | /* |
1105 | * log_putc |
1106 | * |
1107 | * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT |
1108 | * held by the caller. |
1109 | * |
1110 | * Parameters: c Character to output |
1111 | * |
1112 | * Returns: (void) |
1113 | * |
1114 | * Notes: This function is used for single byte output to the log. It |
1115 | * primarily exists to maintain binary backward compatibility. |
1116 | */ |
1117 | void |
1118 | log_putc(char c) |
1119 | { |
1120 | int unread_count = 0; |
1121 | LOG_LOCK(); |
1122 | log_putc_locked(msgbufp, c); |
1123 | unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr; |
1124 | LOG_UNLOCK(); |
1125 | |
1126 | if (unread_count < 0) |
1127 | unread_count = 0 - unread_count; |
1128 | if (c == '\n' || unread_count >= (msgbufp->msg_size / 2)) |
1129 | logwakeup(msgbufp); |
1130 | } |
1131 | |
1132 | |
1133 | /* |
1134 | * it is possible to increase the kernel log buffer size by adding |
1135 | * msgbuf=n |
1136 | * to the kernel command line, and to read the current size using |
1137 | * sysctl kern.msgbuf |
1138 | * If there is no parameter on the kernel command line, the buffer is |
1139 | * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise |
1140 | * memory is dynamically allocated. Memory management must already be up. |
1141 | */ |
1142 | int |
1143 | log_setsize(int size) |
1144 | { |
1145 | char *new_logdata; |
1146 | int new_logsize, new_bufr, new_bufx; |
1147 | char *old_logdata; |
1148 | int old_logsize, old_bufr, old_bufx; |
1149 | int i, count; |
1150 | char *p, ch; |
1151 | |
1152 | if (size > MAX_MSG_BSIZE) |
1153 | return (EINVAL); |
1154 | |
1155 | if (size <= 0) |
1156 | return (EINVAL); |
1157 | |
1158 | new_logsize = size; |
1159 | if (!(new_logdata = (char*)kalloc(size))) { |
1160 | printf("log_setsize: unable to allocate memory\n" ); |
1161 | return (ENOMEM); |
1162 | } |
1163 | bzero(new_logdata, new_logsize); |
1164 | |
1165 | LOG_LOCK(); |
1166 | |
1167 | old_logsize = msgbufp->msg_size; |
1168 | old_logdata = msgbufp->msg_bufc; |
1169 | old_bufr = msgbufp->msg_bufr; |
1170 | old_bufx = msgbufp->msg_bufx; |
1171 | |
1172 | LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n" , |
1173 | size, old_logdata, old_logsize, old_bufr, old_bufx); |
1174 | |
1175 | /* start "new_logsize" bytes before the write pointer */ |
1176 | if (new_logsize <= old_bufx) { |
1177 | count = new_logsize; |
1178 | p = old_logdata + old_bufx - count; |
1179 | } else { |
1180 | /* |
1181 | * if new buffer is bigger, copy what we have and let the |
1182 | * bzero above handle the difference |
1183 | */ |
1184 | count = MIN(new_logsize, old_logsize); |
1185 | p = old_logdata + old_logsize - (count - old_bufx); |
1186 | } |
1187 | for (i = 0; i < count; i++) { |
1188 | if (p >= old_logdata + old_logsize) |
1189 | p = old_logdata; |
1190 | |
1191 | ch = *p++; |
1192 | new_logdata[i] = ch; |
1193 | } |
1194 | |
1195 | new_bufx = i; |
1196 | if (new_bufx >= new_logsize) |
1197 | new_bufx = 0; |
1198 | msgbufp->msg_bufx = new_bufx; |
1199 | |
1200 | new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */ |
1201 | if (new_bufr < 0) |
1202 | new_bufr += old_logsize; |
1203 | new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */ |
1204 | if (new_bufr < 0) |
1205 | new_bufr += new_logsize; |
1206 | msgbufp->msg_bufr = new_bufr; |
1207 | |
1208 | msgbufp->msg_size = new_logsize; |
1209 | msgbufp->msg_bufc = new_logdata; |
1210 | |
1211 | LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n" , |
1212 | size, new_logdata, new_logsize, new_bufr, new_bufx); |
1213 | |
1214 | LOG_UNLOCK(); |
1215 | |
1216 | /* this memory is now dead - clear it so that it compresses better |
1217 | in case of suspend to disk etc. */ |
1218 | bzero(old_logdata, old_logsize); |
1219 | if (old_logdata != smsg_bufc) { |
1220 | /* dynamic memory that must be freed */ |
1221 | kfree(old_logdata, old_logsize); |
1222 | } |
1223 | |
1224 | printf("set system log size to %d bytes\n" , new_logsize); |
1225 | |
1226 | return 0; |
1227 | } |
1228 | |
1229 | void oslog_setsize(int size) |
1230 | { |
1231 | uint16_t scale = 0; |
1232 | // If the size is less than the default stream buffer |
1233 | // do nothing |
1234 | if (size <= OSLOG_STREAM_BUF_SIZE) { |
1235 | return; |
1236 | } |
1237 | |
1238 | scale = (uint16_t) (size / OSLOG_STREAM_BUF_SIZE); |
1239 | |
1240 | oslog_stream_buf_size = size; |
1241 | oslog_stream_num_entries = scale * OSLOG_NUM_STREAM_ENTRIES; |
1242 | printf("oslog_setsize: new buffer size = %d, new num entries= %d\n" , oslog_stream_buf_size, oslog_stream_num_entries); |
1243 | } |
1244 | |
1245 | SYSCTL_PROC(_kern, OID_AUTO, msgbuf, |
1246 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, |
1247 | sysctl_kern_msgbuf, "I" , "" ); |
1248 | |
1249 | static int |
1250 | sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused, |
1251 | void *arg1 __unused, int arg2 __unused, struct sysctl_req *req) |
1252 | { |
1253 | int old_bufsize, bufsize; |
1254 | int error; |
1255 | |
1256 | LOG_LOCK(); |
1257 | old_bufsize = bufsize = msgbufp->msg_size; |
1258 | LOG_UNLOCK(); |
1259 | |
1260 | error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL); |
1261 | if (error) |
1262 | return (error); |
1263 | |
1264 | if (bufsize != old_bufsize) { |
1265 | error = log_setsize(bufsize); |
1266 | } |
1267 | |
1268 | return (error); |
1269 | } |
1270 | |
1271 | |
1272 | /* |
1273 | * This should be called by /sbin/dmesg only via libproc. |
1274 | * It returns as much data still in the buffer as possible. |
1275 | */ |
1276 | int |
1277 | log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval) |
1278 | { |
1279 | uint32_t i; |
1280 | uint32_t localbuff_size; |
1281 | int error = 0, newl, skip; |
1282 | char *localbuff, *p, *copystart, ch; |
1283 | size_t copysize; |
1284 | |
1285 | LOG_LOCK(); |
1286 | localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */ |
1287 | LOG_UNLOCK(); |
1288 | |
1289 | /* Allocate a temporary non-circular buffer for copyout */ |
1290 | if (!(localbuff = (char *)kalloc(localbuff_size))) { |
1291 | printf("log_dmesg: unable to allocate memory\n" ); |
1292 | return (ENOMEM); |
1293 | } |
1294 | |
1295 | /* in between here, the log could become bigger, but that's fine */ |
1296 | LOG_LOCK(); |
1297 | |
1298 | /* |
1299 | * The message buffer is circular; start at the write pointer, and |
1300 | * make one loop up to write pointer - 1. |
1301 | */ |
1302 | p = msgbufp->msg_bufc + msgbufp->msg_bufx; |
1303 | for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) { |
1304 | if (p >= msgbufp->msg_bufc + msgbufp->msg_size) |
1305 | p = msgbufp->msg_bufc; |
1306 | ch = *p; |
1307 | /* Skip "\n<.*>" syslog sequences. */ |
1308 | if (skip) { |
1309 | if (ch == '>') |
1310 | newl = skip = 0; |
1311 | continue; |
1312 | } |
1313 | if (newl && ch == '<') { |
1314 | skip = 1; |
1315 | continue; |
1316 | } |
1317 | if (ch == '\0') |
1318 | continue; |
1319 | newl = (ch == '\n'); |
1320 | localbuff[i++] = ch; |
1321 | /* The original version of this routine contained a buffer |
1322 | * overflow. At the time, a "small" targeted fix was desired |
1323 | * so the change below to check the buffer bounds was made. |
1324 | * TODO: rewrite this needlessly convoluted routine. |
1325 | */ |
1326 | if (i == (localbuff_size - 2)) |
1327 | break; |
1328 | } |
1329 | if (!newl) |
1330 | localbuff[i++] = '\n'; |
1331 | localbuff[i++] = 0; |
1332 | |
1333 | if (buffersize >= i) { |
1334 | copystart = localbuff; |
1335 | copysize = i; |
1336 | } else { |
1337 | copystart = localbuff + i - buffersize; |
1338 | copysize = buffersize; |
1339 | } |
1340 | |
1341 | LOG_UNLOCK(); |
1342 | |
1343 | error = copyout(copystart, buffer, copysize); |
1344 | if (!error) |
1345 | *retval = copysize; |
1346 | |
1347 | kfree(localbuff, localbuff_size); |
1348 | return (error); |
1349 | } |
1350 | |
1351 | #ifdef CONFIG_XNUPOST |
1352 | |
1353 | uint32_t find_pattern_in_buffer(char * pattern, uint32_t len, int expected_count); |
1354 | |
1355 | /* |
1356 | * returns count of pattern found in systemlog buffer. |
1357 | * stops searching further if count reaches expected_count. |
1358 | */ |
1359 | uint32_t |
1360 | find_pattern_in_buffer(char * pattern, uint32_t len, int expected_count) |
1361 | { |
1362 | int match_count = 0; |
1363 | int i = 0; |
1364 | int j = 0; |
1365 | int no_match = 0; |
1366 | int pos = 0; |
1367 | char ch = 0; |
1368 | |
1369 | if (pattern == NULL || len == 0 || expected_count == 0) { |
1370 | return 0; |
1371 | } |
1372 | |
1373 | for (i = 0; i < msgbufp->msg_size; i++) { |
1374 | no_match = 0; |
1375 | for (j = 0; j < (int)len; j++) { |
1376 | pos = (msgbufp->msg_bufx + i + j) % msgbufp->msg_size; |
1377 | ch = msgbufp->msg_bufc[pos]; |
1378 | if (ch != pattern[j]) { |
1379 | no_match = 1; |
1380 | break; |
1381 | } |
1382 | } |
1383 | if (no_match == 0) { |
1384 | match_count++; |
1385 | if (match_count >= expected_count) { |
1386 | break; |
1387 | } |
1388 | } |
1389 | } |
1390 | return match_count; |
1391 | } |
1392 | |
1393 | #endif |
1394 | |