1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
3 | * Copyright (c) 2006 Jakub Vana
|
---|
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.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <abi/syscall.h>
|
---|
37 | #include <stddef.h>
|
---|
38 | #include <libc.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <abi/kio.h>
|
---|
44 | #include <io/kio.h>
|
---|
45 | #include <printf_core.h>
|
---|
46 | #include <macros.h>
|
---|
47 | #include <libarch/config.h>
|
---|
48 |
|
---|
49 | #include "../private/futex.h"
|
---|
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;
|
---|
57 | } kio_buffer;
|
---|
58 |
|
---|
59 | void __kio_init(void)
|
---|
60 | {
|
---|
61 | if (futex_initialize(&kio_buffer.futex, 1) != EOK)
|
---|
62 | abort();
|
---|
63 | }
|
---|
64 |
|
---|
65 | void __kio_fini(void)
|
---|
66 | {
|
---|
67 | futex_destroy(&kio_buffer.futex);
|
---|
68 | }
|
---|
69 |
|
---|
70 | errno_t kio_write(const void *buf, size_t size, size_t *nwritten)
|
---|
71 | {
|
---|
72 | /* Using down/up instead of lock/unlock so we can print very early. */
|
---|
73 | futex_down(&kio_buffer.futex);
|
---|
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 | }
|
---|
98 |
|
---|
99 | futex_up(&kio_buffer.futex);
|
---|
100 | if (nwritten)
|
---|
101 | *nwritten = size;
|
---|
102 | return EOK;
|
---|
103 | }
|
---|
104 |
|
---|
105 | void kio_update(void)
|
---|
106 | {
|
---|
107 | (void) __SYSCALL3(SYS_KIO, KIO_UPDATE, (uintptr_t) NULL, 0);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void kio_command(const void *buf, size_t size)
|
---|
111 | {
|
---|
112 | (void) __SYSCALL3(SYS_KIO, KIO_COMMAND, (sysarg_t) buf, (sysarg_t) size);
|
---|
113 | }
|
---|
114 |
|
---|
115 | size_t kio_read(char *buf, size_t n, size_t at)
|
---|
116 | {
|
---|
117 | return __SYSCALL3(SYS_KIO_READ, (sysarg_t) buf, n, at);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Print formatted text to kio.
|
---|
121 | *
|
---|
122 | * @param fmt Format string
|
---|
123 | *
|
---|
124 | * \see For more details about format string see printf_core.
|
---|
125 | *
|
---|
126 | */
|
---|
127 | int kio_printf(const char *fmt, ...)
|
---|
128 | {
|
---|
129 | va_list args;
|
---|
130 | va_start(args, fmt);
|
---|
131 |
|
---|
132 | int ret = kio_vprintf(fmt, args);
|
---|
133 |
|
---|
134 | va_end(args);
|
---|
135 |
|
---|
136 | return ret;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static errno_t kio_vprintf_str_write(const char *str, size_t size, void *data)
|
---|
140 | {
|
---|
141 | size_t wr = 0;
|
---|
142 | return kio_write(str, size, &wr);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Print formatted text to kio.
|
---|
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 | */
|
---|
153 | int kio_vprintf(const char *fmt, va_list ap)
|
---|
154 | {
|
---|
155 | printf_spec_t ps = {
|
---|
156 | kio_vprintf_str_write,
|
---|
157 | NULL
|
---|
158 | };
|
---|
159 |
|
---|
160 | return printf_core(fmt, &ps, ap);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** @}
|
---|
164 | */
|
---|