1 /*
2 * Copyright (c) 2000-2020 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
29 #ifdef __x86_64__
30 #include <i386/mp.h>
31 #include <i386/cpu_data.h>
32 #include <i386/bit_routines.h>
33 #include <i386/machine_routines.h>
34 #include <i386/misc_protos.h>
35 #include <i386/serial_io.h>
36 #endif /* __x86_64__ */
37
38 #include <machine/machine_cpu.h>
39 #include <libkern/OSAtomic.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_map.h>
42 #include <console/video_console.h>
43 #include <console/serial_protos.h>
44 #include <kern/startup.h>
45 #include <kern/thread.h>
46 #include <kern/cpu_data.h>
47 #include <kern/sched_prim.h>
48 #include <libkern/section_keywords.h>
49
50 #if __arm64__
51 #include <machine/machine_routines.h>
52 #include <arm/cpu_data_internal.h>
53 #endif
54
55 #ifdef CONFIG_XNUPOST
56 #include <tests/xnupost.h>
57 kern_return_t console_serial_test(void);
58 kern_return_t console_serial_parallel_log_tests(void);
59 #endif
60
61 /* Structure representing the console ring buffer. */
62 static struct {
63 /* The ring buffer backing store. */
64 char *buffer;
65
66 /* The total length of the ring buffer. */
67 int len;
68
69 /**
70 * The number of characters that have been written into the buffer that need
71 * to be drained.
72 */
73 int used;
74
75 /**
76 * Number of reserved regions in the buffer. These are regions that are
77 * currently being written into by various CPUs. We use this as a way of
78 * determining when it's safe to drain the buffer.
79 */
80 int nreserved;
81
82 /* The location in the buffer thats written to next. */
83 char *write_ptr;
84
85 /* The location in the buffer that will be drained next. */
86 char *read_ptr;
87
88 /* Synchronizes the flushing of the ring buffer to hardware */
89 lck_mtx_t flush_lock;
90
91 /**
92 * Synchronizes reserving space in the ring buffer and ensures that only
93 * completed writes are flushed.
94 */
95 lck_ticket_t write_lock;
96 } console_ring;
97
98 /**
99 * We don't dedicate any buffer space to specific CPUs, but this value is used
100 * to scale the size of the console buffer by the number of CPUs.
101 *
102 * How many bytes-per-cpu to allocate in the console ring buffer. Also affects
103 * the maximum number of bytes a single console thread can drain.
104 */
105 #define CPU_CONS_BUF_SIZE 256
106
107 /* Scale the size of the console ring buffer by the number of CPUs. */
108 #define KERN_CONSOLE_RING_SIZE vm_map_round_page(CPU_CONS_BUF_SIZE * (MAX_CPUS + 1), PAGE_SIZE - 1)
109
110 #define MAX_FLUSH_SIZE_LOCK_HELD 16
111 #define MAX_TOTAL_FLUSH_SIZE (MAX(2, MAX_CPUS) * CPU_CONS_BUF_SIZE)
112
113 extern int serial_getc(void);
114 extern void serial_putc_options(char, bool);
115
116 #if DEBUG || DEVELOPMENT
117 TUNABLE(bool, allow_printf_from_interrupts_disabled_context, "nointr_consio", false);
118 #else
119 #define allow_printf_from_interrupts_disabled_context false
120 #endif
121
122 SECURITY_READ_ONLY_EARLY(struct console_ops) cons_ops[] = {
123 {
124 .putc = serial_putc_options, .getc = _serial_getc,
125 },
126 {
127 .putc = vcputc_options, .getc = _vcgetc,
128 },
129 };
130
131 SECURITY_READ_ONLY_EARLY(uint32_t) nconsops = (sizeof cons_ops / sizeof cons_ops[0]);
132
133 #if __x86_64__
134 uint32_t cons_ops_index = VC_CONS_OPS;
135 #else
136 SECURITY_READ_ONLY_LATE(uint32_t) cons_ops_index = VC_CONS_OPS;
137 #endif
138
139 LCK_GRP_DECLARE(console_lck_grp, "console");
140
141 /* If the NMI string is entered into the console, the system will enter the debugger. */
142 #define NMI_STRING_SIZE 32
143 char nmi_string[NMI_STRING_SIZE] = "afDIGHr84A84jh19Kphgp428DNPdnapq";
144 static int nmi_counter = 0;
145
146 /**
147 * This is used to prevent console output from going through the console ring
148 * buffer synchronization in cases where that could cause issues (e.g., during
149 * panics/stackshots and going down for sleep).
150 */
151 static bool console_suspended = false;
152
153 /**
154 * Enforce policies around when console I/O is allowed. Most importantly about
155 * not performing console I/O while interrupts are disabled (which can cause
156 * serious latency issues).
157 *
158 * @return True if console I/O should be allowed, false otherwise.
159 */
160 static inline bool
console_io_allowed(void)161 console_io_allowed(void)
162 {
163 if (!allow_printf_from_interrupts_disabled_context &&
164 !console_suspended &&
165 startup_phase >= STARTUP_SUB_EARLY_BOOT &&
166 !ml_get_interrupts_enabled()) {
167 #if defined(__arm64__) || DEBUG || DEVELOPMENT
168 panic("Console I/O from interrupt-disabled context");
169 #else
170 return false;
171 #endif
172 }
173
174 return true;
175 }
176
177 /**
178 * Initialize the console ring buffer and console lock. It's still possible to
179 * call console_write() before initializing the ring buffer. In that case the
180 * data will get outputted directly to the underlying serial/video console
181 * without synchronization.
182 *
183 * This function is also safe to call multiple times. Any call after the first
184 * will return early without doing anything.
185 */
186 void
console_init(void)187 console_init(void)
188 {
189 if (console_ring.len != 0) {
190 return;
191 }
192
193 kmem_alloc(kernel_map, (vm_offset_t *)&console_ring.buffer,
194 KERN_CONSOLE_RING_SIZE + ptoa(2), KMA_NOFAIL | KMA_PERMANENT |
195 KMA_KOBJECT | KMA_PERMANENT | KMA_GUARD_FIRST | KMA_GUARD_LAST |
196 KMA_ZERO | KMA_DATA, VM_KERN_MEMORY_OSFMK);
197
198 console_ring.buffer += PAGE_SIZE; /* Skip past the first guard page. */
199 console_ring.len = KERN_CONSOLE_RING_SIZE;
200 console_ring.used = 0;
201 console_ring.nreserved = 0;
202 console_ring.read_ptr = console_ring.buffer;
203 console_ring.write_ptr = console_ring.buffer;
204
205 lck_mtx_init(&console_ring.flush_lock, &console_lck_grp, LCK_ATTR_NULL);
206 lck_ticket_init(&console_ring.write_lock, &console_lck_grp);
207 }
208
209 /**
210 * Returns true when the console has already been initialized.
211 */
212 static inline bool
is_console_initialized(void)213 is_console_initialized(void)
214 {
215 return console_ring.len == KERN_CONSOLE_RING_SIZE;
216 }
217
218 /**
219 * Return the index to the currently selected console (serial/video). This is
220 * an index into the "cons_ops[]" array of function pointer structs.
221 */
222 static inline uint32_t
get_cons_ops_index(void)223 get_cons_ops_index(void)
224 {
225 uint32_t idx = cons_ops_index;
226
227 if (idx >= nconsops) {
228 panic("Bad cons_ops_index: %d", idx);
229 }
230
231 return idx;
232 }
233
234 /**
235 * Helper function for outputting a character to the underlying console
236 * (either video or serial) with the possibility of sleeping waiting for
237 * an interrupt indicating the console is ready.
238 *
239 * @note assumes console_ring.read lock is held if poll == false
240 *
241 * @param c The character to print.
242 * @param poll Whether or not this call should poll instead of going to sleep
243 * waiting for an interrupt when the hardware device isn't ready
244 */
245 static inline void
_cnputc(char c,bool poll)246 _cnputc(char c, bool poll)
247 {
248 bool in_debugger = (kernel_debugger_entry_count > 0);
249 const uint32_t idx = get_cons_ops_index();
250
251 poll = poll || in_debugger;
252
253 if (c == '\n') {
254 _cnputc('\r', poll);
255 }
256
257 cons_ops[idx].putc(c, poll);
258 }
259
260 /**
261 * Helper function for outputting characters directly to the underlying console
262 * (either video or serial).
263 *
264 * @param c The array of characters to print.
265 * @param poll Whether or not this call should poll instead of going to sleep
266 * waiting for an interrupt when the hardware device isn't ready
267 * @param size The number of characters to print to the console.
268 */
269 static inline void
_cnputs(char * c,int size,bool poll)270 _cnputs(char *c, int size, bool poll)
271 {
272 extern int disableConsoleOutput;
273
274 if (disableConsoleOutput) {
275 return;
276 }
277
278 assert(c != NULL);
279
280 while (size-- > 0) {
281 _cnputc(*c, poll);
282 c++;
283 }
284 }
285
286 /**
287 * Attempt to reserve space for a number of characters in the console ring
288 * buffer. Space in the ring buffer must be reserved before new characters can
289 * be entered.
290 *
291 * Every call to this function should be paired with a corresponding call to
292 * console_ring_unreserve_space().
293 *
294 * @note If space is successfully reserved, this will disable preemption because
295 * otherwise, console_ring_try_empty() could take arbitrarily long.
296 *
297 * @param nchars The number of characters to reserve.
298 *
299 * @return If the wanted number of characters could not be reserved, then return
300 * NULL. Otherwise, return a pointer to the beginning of the reserved
301 * space.
302 */
303 static inline char*
console_ring_reserve_space(int nchars)304 console_ring_reserve_space(int nchars)
305 {
306 char *write_ptr = NULL;
307 lck_ticket_lock(&console_ring.write_lock, &console_lck_grp);
308 if ((console_ring.len - console_ring.used) >= nchars) {
309 console_ring.used += nchars;
310 mp_disable_preemption();
311 os_atomic_inc(&console_ring.nreserved, relaxed);
312
313 /* Return out the pointer to the beginning of the just reserved space. */
314 write_ptr = console_ring.write_ptr;
315
316 /* Move the console ring's write pointer to the beginning of the next free space. */
317 const ptrdiff_t write_index = console_ring.write_ptr - console_ring.buffer;
318 console_ring.write_ptr = console_ring.buffer + ((write_index + nchars) % console_ring.len);
319 }
320 lck_ticket_unlock(&console_ring.write_lock);
321 return write_ptr;
322 }
323
324 /**
325 * Decrement the number of reserved spaces in the console ring (now that the data
326 * has been written) and re-enable preemption.
327 *
328 * Every call to this function should be paired with a corresponding call to
329 * console_ring_reserve_space().
330 */
331 static inline void
console_ring_unreserve_space(void)332 console_ring_unreserve_space(void)
333 {
334 assert(console_ring.nreserved > 0);
335
336 os_atomic_dec(&console_ring.nreserved, relaxed);
337 mp_enable_preemption();
338 }
339
340 /**
341 * Write a single character into the console ring buffer and handle moving the
342 * write pointer circularly around the buffer.
343 *
344 * @note Space to write this character must have already been reserved using
345 * console_ring_reserve_space().
346 *
347 * @param write_ptr Pointer into the reserved space in the buffer to write the
348 * character. This pointer will get moved to the next valid
349 * location to write a character so the same pointer can be
350 * passed into subsequent calls to write multiple characters.
351 * @param ch The character to insert into the ring buffer.
352 */
353 static inline void
console_ring_put(char ** write_ptr,char ch)354 console_ring_put(char **write_ptr, char ch)
355 {
356 assert(console_ring.nreserved > 0);
357 **write_ptr = ch;
358 ++(*write_ptr);
359 if ((*write_ptr - console_ring.buffer) == console_ring.len) {
360 *write_ptr = console_ring.buffer;
361 }
362 }
363
364 /**
365 * Attempt to drain the console ring buffer if no other CPUs are already doing
366 * so.
367 *
368 * @param fail_fast If true, this function returns immediately instead of
369 * sleeping if the thread fails to acquire the console flush
370 * mutex.
371 *
372 * @note This function should not be called with preemption disabled.
373 *
374 * @note To prevent one CPU from holding the console lock for too long, only
375 * MAX_FLUSH_SIZE_LOCK_HELD number of characters can be drained at a time
376 * with the lock held. The lock will be dropped between each drain of size
377 * MAX_FLUSH_SIZE_LOCK_HELD to allow another CPU to grab the lock. If
378 * another CPU grabs the lock, then the original thread can stop draining
379 * and return instead of sleeping for the lock.
380 *
381 * @note To prevent one thread from being the drain thread for too long (presumably
382 * that thread has other things it wants to do besides draining serial), the
383 * total number of characters a single call to this function can drain is
384 * restricted to MAX_TOTAL_FLUSH_SIZE.
385 */
386 static void
console_ring_try_empty(bool fail_fast)387 console_ring_try_empty(bool fail_fast)
388 {
389 char flush_buf[MAX_FLUSH_SIZE_LOCK_HELD];
390
391 int nchars_out = 0;
392 int total_chars_out = 0;
393 int size_before_wrap = 0;
394 bool in_debugger = (kernel_debugger_entry_count > 0);
395
396 if (__improbable(!console_io_allowed()) || get_preemption_level() != 0) {
397 return;
398 }
399
400 do {
401 if (__probable(!in_debugger) && fail_fast && !lck_mtx_try_lock(&console_ring.flush_lock)) {
402 return;
403 } else if (__probable(!in_debugger) && !fail_fast) {
404 lck_mtx_lock(&console_ring.flush_lock);
405 }
406
407 if (__probable(!in_debugger)) {
408 lck_ticket_lock(&console_ring.write_lock, &console_lck_grp);
409
410 /**
411 * If we've managed to grab the write lock, but there's still space
412 * reserved in the buffer, then other CPUs are actively writing into
413 * the ring, wait for them to finish.
414 */
415 while (os_atomic_load(&console_ring.nreserved, relaxed) > 0) {
416 cpu_pause();
417 }
418 }
419
420 /* Try small chunk at a time, so we allow writes from other cpus into the buffer. */
421 nchars_out = MIN(console_ring.used, (int)sizeof(flush_buf));
422
423 /* Account for data to be read before wrap around. */
424 size_before_wrap = (int)((console_ring.buffer + console_ring.len) - console_ring.read_ptr);
425 if (nchars_out > size_before_wrap) {
426 nchars_out = size_before_wrap;
427 }
428
429 /**
430 * Copy the characters to be drained into a separate flush buffer, and
431 * move the console read pointer to the next chunk of data that needs to
432 * be drained.
433 */
434 if (nchars_out > 0) {
435 memcpy(flush_buf, console_ring.read_ptr, nchars_out);
436 const ptrdiff_t read_index = console_ring.read_ptr - console_ring.buffer;
437 console_ring.read_ptr = console_ring.buffer + ((read_index + nchars_out) % console_ring.len);
438 console_ring.used -= nchars_out;
439 }
440
441 if (__probable(!in_debugger)) {
442 lck_ticket_unlock(&console_ring.write_lock);
443 }
444
445 /**
446 * Output characters to the underlying console (serial/video). We should
447 * only poll if the console is suspended.
448 */
449 if (nchars_out > 0) {
450 total_chars_out += nchars_out;
451 _cnputs(flush_buf, nchars_out, console_suspended);
452 }
453
454 if (__probable(!in_debugger)) {
455 lck_mtx_unlock(&console_ring.flush_lock);
456 }
457
458 /**
459 * Prevent this thread from sleeping on the lock again if another thread
460 * grabs it after we drop it.
461 */
462 fail_fast = true;
463
464 /*
465 * In case we end up being the console drain thread for far too long,
466 * break out. Except in panic/suspend cases where we should clear out
467 * the full buffer.
468 */
469 if (!console_suspended && (total_chars_out >= MAX_TOTAL_FLUSH_SIZE)) {
470 break;
471 }
472 } while (nchars_out > 0);
473 }
474
475 /**
476 * Notify the console subystem that all following console writes should skip
477 * synchronization and get outputted directly to the underlying console. This is
478 * important for cases like panic/stackshots and going down for sleep where
479 * assumptions about the state of the system could cause hangs or nested panics.
480 */
481 void
console_suspend()482 console_suspend()
483 {
484 console_suspended = true;
485 console_ring_try_empty(false);
486 }
487
488 /**
489 * Notify the console subsystem that it is now safe to use the console ring
490 * buffer synchronization when writing console data.
491 */
492 void
console_resume()493 console_resume()
494 {
495 console_suspended = false;
496 }
497
498 /**
499 * Write a string of characters to the underlying video or serial console in a
500 * synchronized manner. By synchronizing access to a global console buffer, this
501 * prevents the serial output from appearing interleaved to the end user when
502 * multiple CPUs are outputting to the console at the same time.
503 *
504 * @note It's safe to call this function even before the console buffer has been
505 * initialized. In that case, the data will be sent directly to the
506 * underlying console with no buffering. This is the same for when the
507 * console is suspended.
508 *
509 * @param str The string of characters to print.
510 * @param size The number of characters in `str` to print.
511 */
512 void
console_write(char * str,int size)513 console_write(char *str, int size)
514 {
515 assert(str != NULL);
516
517 char *write_ptr = NULL;
518 int chunk_size = CPU_CONS_BUF_SIZE;
519 int i = 0;
520
521 if (__improbable(console_suspended || !is_console_initialized() || pmap_in_ppl())) {
522 /*
523 * Output directly to console in the following cases:
524 * 1. If this is early in boot before the console has been initialized.
525 * 2. If we're heading into suspend.
526 * 3. If we're in the kernel debugger for a panic/stackshot. If any of
527 * the other cores happened to halt while holding any of the console
528 * locks, attempting to use the normal path will result in sadness.
529 * 4. If we're in the PPL. As we synchronize the ring buffer with a
530 * mutex and preemption is disabled in the PPL, any writes must go
531 * directly to the hardware device.
532 */
533 _cnputs(str, size, true);
534 return;
535 } else if (__improbable(!console_io_allowed())) {
536 return;
537 }
538
539 while (size > 0) {
540 /**
541 * Restrict the maximum number of characters that can be reserved at
542 * once. This helps prevent one CPU from reserving too much and starving
543 * out the other CPUs.
544 */
545 if (size < chunk_size) {
546 chunk_size = size;
547 }
548
549 /**
550 * Attempt to reserve space in the ring buffer and if that fails, then
551 * keep attempting to drain the ring buffer until there's enough space.
552 * We can't flush the serial console with preemption disabled so return
553 * early to drop the message in that case.
554 */
555 while ((write_ptr = console_ring_reserve_space(chunk_size)) == NULL) {
556 if (get_preemption_level() != 0) {
557 return;
558 }
559
560 console_ring_try_empty(false);
561 }
562
563 for (i = 0; i < chunk_size; i++) {
564 console_ring_put(&write_ptr, str[i]);
565 }
566
567 console_ring_unreserve_space();
568 str = &str[i];
569 size -= chunk_size;
570 }
571
572 /* Do good faith flush if preemption is not disabled */
573 if (get_preemption_level() == 0) {
574 console_ring_try_empty(true);
575 }
576 }
577
578 /**
579 * Output a character directly to the underlying console (either video or serial).
580 * This directly bypasses the console serial buffer (as provided by console_write())
581 * and all of the synchronization that provides.
582 *
583 * @note This function can cause serial data to get printed interleaved if being
584 * called on multiple CPUs at the same time. Only use this function if
585 * there's a specific reason why this serial data can't get synchronized
586 * through the console buffer.
587 *
588 * @param c The character to print.
589 */
590 void
console_write_unbuffered(char c)591 console_write_unbuffered(char c)
592 {
593 _cnputc(c, true);
594 }
595
596 /**
597 * Write a single character to the selected console (video or serial).
598 *
599 * @param c The character to print.
600 */
601 void
console_write_char(char c)602 console_write_char(char c)
603 {
604 console_write(&c, 1);
605 }
606
607 /**
608 * Wrapper around the platform-dependent serial input method which handles
609 * waiting for a new character and checking for the NMI string.
610 *
611 * @param wait True if this function should block until a character appears.
612 *
613 * @return The character if one was read, -1 otherwise.
614 */
615 int
_serial_getc(bool wait)616 _serial_getc(bool wait)
617 {
618 int c = -1;
619
620 do {
621 c = serial_getc();
622 } while (wait && c < 0);
623
624 /* Check for the NMI string. */
625 if (c == nmi_string[nmi_counter]) {
626 nmi_counter++;
627 if (nmi_counter == NMI_STRING_SIZE) {
628 /* We've got the NMI string, now do an NMI. */
629 Debugger("Automatic NMI");
630 nmi_counter = 0;
631 return '\n';
632 }
633 } else if (c != -1) {
634 nmi_counter = 0;
635 }
636
637 return c;
638 }
639
640 /**
641 * Typically the video console doesn't support input, but we call into the
642 * pexpert to give each platform an opportunity to provide console input through
643 * alternative methods if it so desires.
644 *
645 * Usually a platform will either not provide any input, or will grab input from
646 * the serial driver.
647 *
648 * @return The character if one was read, or -1 otherwise.
649 */
650 int
_vcgetc(__unused bool wait)651 _vcgetc(__unused bool wait)
652 {
653 char c;
654
655 if (0 == PE_stub_poll_input(0, &c)) {
656 return c;
657 } else {
658 return -1;
659 }
660 }
661
662 /**
663 * Block until a character is available from the console and return it.
664 *
665 * @return The character retrieved from the console.
666 */
667 int
console_read_char(void)668 console_read_char(void)
669 {
670 const uint32_t idx = get_cons_ops_index();
671 return cons_ops[idx].getc(true);
672 }
673
674 /**
675 * Attempt to read a character from the console, and if one isn't available,
676 * then return immediately.
677 *
678 * @return The character if one is available, -1 otherwise.
679 */
680 int
console_try_read_char(void)681 console_try_read_char(void)
682 {
683 const uint32_t idx = get_cons_ops_index();
684 return cons_ops[idx].getc(false);
685 }
686
687 #ifdef CONFIG_XNUPOST
688 static uint32_t cons_test_ops_count = 0;
689
690 /*
691 * Log to console by multiple methods - printf, unbuffered write, console_write()
692 */
693 static void
log_to_console_func(void * arg __unused,wait_result_t wres __unused)694 log_to_console_func(void * arg __unused, wait_result_t wres __unused)
695 {
696 uint64_t thread_id = current_thread()->thread_id;
697 char somedata[10] = "123456789";
698 for (int i = 0; i < 26; i++) {
699 os_atomic_inc(&cons_test_ops_count, relaxed);
700 printf(" thid: %llu printf iteration %d\n", thread_id, i);
701 console_write_unbuffered((char)('A' + i));
702 console_write_unbuffered('\n');
703 console_write((char *)somedata, sizeof(somedata));
704 delay(10);
705 }
706 printf("finished the log_to_console_func operations\n\n");
707 }
708
709 /* Test that outputting to the console can occur on multiple threads at the same time. */
710 kern_return_t
console_serial_parallel_log_tests(void)711 console_serial_parallel_log_tests(void)
712 {
713 thread_t thread;
714 kern_return_t kr;
715 cons_test_ops_count = 0;
716
717 kr = kernel_thread_start(log_to_console_func, NULL, &thread);
718 T_ASSERT_EQ_INT(kr, KERN_SUCCESS, "kernel_thread_start returned successfully");
719
720 delay(100);
721
722 log_to_console_func(NULL, 0);
723
724 /* wait until other thread has also finished */
725 while (cons_test_ops_count < 52) {
726 delay(1000);
727 }
728
729 thread_deallocate(thread);
730 T_LOG("parallel_logging tests is now complete. From this point forward we expect full lines\n");
731 return KERN_SUCCESS;
732 }
733
734 /* Basic serial test that prints serial output through various methods (printf/T_LOG). */
735 kern_return_t
console_serial_test(void)736 console_serial_test(void)
737 {
738 unsigned long i;
739 char buffer[CPU_CONS_BUF_SIZE];
740
741 T_LOG("Checking console_ring status.");
742 T_ASSERT_EQ_INT(console_ring.len, KERN_CONSOLE_RING_SIZE, "Console ring size is not correct.");
743
744 /* setup buffer to be chars */
745 for (i = 0; i < CPU_CONS_BUF_SIZE; i++) {
746 buffer[i] = (char)('0' + (i % 10));
747 }
748 buffer[CPU_CONS_BUF_SIZE - 1] = '\0';
749
750 T_LOG("Printing %d char string to serial one char at a time.", CPU_CONS_BUF_SIZE);
751 for (i = 0; i < CPU_CONS_BUF_SIZE; i++) {
752 printf("%c", buffer[i]);
753 }
754 printf("End\n");
755 T_LOG("Printing %d char string to serial as a whole", CPU_CONS_BUF_SIZE);
756 printf("%s\n", buffer);
757
758 T_LOG("Using console_write call repeatedly for 100 iterations");
759 for (i = 0; i < 100; i++) {
760 console_write(&buffer[0], 14);
761 if ((i % 6) == 0) {
762 printf("\n");
763 }
764 }
765 printf("\n");
766
767 T_LOG("Using T_LOG to print buffer %s", buffer);
768 return KERN_SUCCESS;
769 }
770 #endif
771