[b861b58] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[2595dab] | 3 | * Copyright (c) 2006 Jakub Vana
|
---|
[b861b58] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[4e2cf8b] | 28 | */
|
---|
[b861b58] | 29 |
|
---|
[fadd381] | 30 | /** @addtogroup libc
|
---|
[b2951e2] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[d5b37b6] | 36 | #include <abi/syscall.h>
|
---|
[582a0b8] | 37 | #include <stddef.h>
|
---|
[2595dab] | 38 | #include <libc.h>
|
---|
[19f857a] | 39 | #include <str.h>
|
---|
[8d2dd7f2] | 40 | #include <stdint.h>
|
---|
[45c8eea] | 41 | #include <stdlib.h>
|
---|
[d8de5d3] | 42 | #include <errno.h>
|
---|
[6fa9a99d] | 43 | #include <abi/kio.h>
|
---|
| 44 | #include <io/kio.h>
|
---|
[694ca3d6] | 45 | #include <printf_core.h>
|
---|
[5e904dd] | 46 | #include <macros.h>
|
---|
| 47 | #include <libarch/config.h>
|
---|
[f787c8e] | 48 |
|
---|
| 49 | #include "../private/futex.h"
|
---|
[5e904dd] | 50 |
|
---|
| 51 | #define KIO_BUFFER_SIZE PAGE_SIZE
|
---|
| 52 |
|
---|
| 53 | static struct {
|
---|
| 54 | futex_t futex;
|
---|
| 55 | char data[KIO_BUFFER_SIZE];
|
---|
| 56 | size_t used;
|
---|
[45c8eea] | 57 | } kio_buffer;
|
---|
| 58 |
|
---|
| 59 | void __kio_init(void)
|
---|
| 60 | {
|
---|
| 61 | if (futex_initialize(&kio_buffer.futex, 1) != EOK)
|
---|
| 62 | abort();
|
---|
| 63 | }
|
---|
[350514c] | 64 |
|
---|
[25f6bddb] | 65 | void __kio_fini(void)
|
---|
| 66 | {
|
---|
| 67 | futex_destroy(&kio_buffer.futex);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[b7fd2a0] | 70 | errno_t kio_write(const void *buf, size_t size, size_t *nwritten)
|
---|
[2595dab] | 71 | {
|
---|
[40abf56a] | 72 | /* Using down/up instead of lock/unlock so we can print very early. */
|
---|
| 73 | futex_down(&kio_buffer.futex);
|
---|
[5e904dd] | 74 |
|
---|
| 75 | const char *s = buf;
|
---|
| 76 | while (true) {
|
---|
| 77 | const char *endl = memchr(s, '\n', size);
|
---|
| 78 | if (endl) {
|
---|
| 79 | size_t used = kio_buffer.used;
|
---|
| 80 |
|
---|
| 81 | size_t sz = min(KIO_BUFFER_SIZE - used, (size_t) (endl - s));
|
---|
| 82 | memcpy(&kio_buffer.data[used], s, sz);
|
---|
| 83 |
|
---|
| 84 | __SYSCALL3(SYS_KIO, KIO_WRITE,
|
---|
| 85 | (sysarg_t) &kio_buffer.data[0], used + sz);
|
---|
| 86 |
|
---|
| 87 | kio_buffer.used = 0;
|
---|
| 88 | size -= endl + 1 - s;
|
---|
| 89 | s = endl + 1;
|
---|
| 90 | } else {
|
---|
| 91 | size_t used = kio_buffer.used;
|
---|
| 92 | size_t sz = min(KIO_BUFFER_SIZE - used, size);
|
---|
| 93 | memcpy(&kio_buffer.data[used], s, sz);
|
---|
| 94 | kio_buffer.used += sz;
|
---|
| 95 | break;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[a35b458] | 98 |
|
---|
[40abf56a] | 99 | futex_up(&kio_buffer.futex);
|
---|
[5e904dd] | 100 | if (nwritten)
|
---|
[6afc9d7] | 101 | *nwritten = size;
|
---|
[5e904dd] | 102 | return EOK;
|
---|
[2595dab] | 103 | }
|
---|
[b861b58] | 104 |
|
---|
[6fa9a99d] | 105 | void kio_update(void)
|
---|
[2595dab] | 106 | {
|
---|
[6fa9a99d] | 107 | (void) __SYSCALL3(SYS_KIO, KIO_UPDATE, (uintptr_t) NULL, 0);
|
---|
[2595dab] | 108 | }
|
---|
[b2951e2] | 109 |
|
---|
[6fa9a99d] | 110 | void kio_command(const void *buf, size_t size)
|
---|
[ec85df0] | 111 | {
|
---|
[6fa9a99d] | 112 | (void) __SYSCALL3(SYS_KIO, KIO_COMMAND, (sysarg_t) buf, (sysarg_t) size);
|
---|
[ec85df0] | 113 | }
|
---|
| 114 |
|
---|
[690ad20] | 115 | size_t kio_read(char *buf, size_t n, size_t at)
|
---|
[d5b37b6] | 116 | {
|
---|
| 117 | return __SYSCALL3(SYS_KIO_READ, (sysarg_t) buf, n, at);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[6fa9a99d] | 120 | /** Print formatted text to kio.
|
---|
[d8de5d3] | 121 | *
|
---|
| 122 | * @param fmt Format string
|
---|
| 123 | *
|
---|
| 124 | * \see For more details about format string see printf_core.
|
---|
| 125 | *
|
---|
| 126 | */
|
---|
[6fa9a99d] | 127 | int kio_printf(const char *fmt, ...)
|
---|
[d8de5d3] | 128 | {
|
---|
| 129 | va_list args;
|
---|
| 130 | va_start(args, fmt);
|
---|
[a35b458] | 131 |
|
---|
[6fa9a99d] | 132 | int ret = kio_vprintf(fmt, args);
|
---|
[a35b458] | 133 |
|
---|
[d8de5d3] | 134 | va_end(args);
|
---|
[a35b458] | 135 |
|
---|
[d8de5d3] | 136 | return ret;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[163e34c] | 139 | static errno_t kio_vprintf_str_write(const char *str, size_t size, void *data)
|
---|
[d8de5d3] | 140 | {
|
---|
[163e34c] | 141 | size_t wr = 0;
|
---|
| 142 | return kio_write(str, size, &wr);
|
---|
[d8de5d3] | 143 | }
|
---|
| 144 |
|
---|
[6fa9a99d] | 145 | /** Print formatted text to kio.
|
---|
[d8de5d3] | 146 | *
|
---|
| 147 | * @param fmt Format string
|
---|
| 148 | * @param ap Format parameters
|
---|
| 149 | *
|
---|
| 150 | * \see For more details about format string see printf_core.
|
---|
| 151 | *
|
---|
| 152 | */
|
---|
[6fa9a99d] | 153 | int kio_vprintf(const char *fmt, va_list ap)
|
---|
[d8de5d3] | 154 | {
|
---|
| 155 | printf_spec_t ps = {
|
---|
[6fa9a99d] | 156 | kio_vprintf_str_write,
|
---|
[d8de5d3] | 157 | NULL
|
---|
| 158 | };
|
---|
[a35b458] | 159 |
|
---|
[d8de5d3] | 160 | return printf_core(fmt, &ps, ap);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[fadd381] | 163 | /** @}
|
---|
[b2951e2] | 164 | */
|
---|