1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kio
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <as.h>
|
---|
39 | #include <ddi.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <io/kio.h>
|
---|
43 | #include <sysinfo.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <fibril_synch.h>
|
---|
46 | #include <adt/list.h>
|
---|
47 | #include <adt/prodcons.h>
|
---|
48 | #include <tinput.h>
|
---|
49 | #include <vfs/vfs.h>
|
---|
50 |
|
---|
51 | #define NAME "kio"
|
---|
52 | #define LOG_FNAME "/log/kio"
|
---|
53 |
|
---|
54 | /* Producer/consumer buffers */
|
---|
55 | typedef struct {
|
---|
56 | link_t link;
|
---|
57 | size_t length;
|
---|
58 | char32_t *data;
|
---|
59 | } item_t;
|
---|
60 |
|
---|
61 | static prodcons_t pc;
|
---|
62 |
|
---|
63 | /* Pointer to kio area */
|
---|
64 | static char32_t *kio = (char32_t *) AS_AREA_ANY;
|
---|
65 | static size_t kio_length;
|
---|
66 |
|
---|
67 | /* Notification mutex */
|
---|
68 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
69 |
|
---|
70 | /** Klog producer
|
---|
71 | *
|
---|
72 | * Copies the contents of a character buffer to local
|
---|
73 | * producer/consumer queue.
|
---|
74 | *
|
---|
75 | * @param length Number of characters to copy.
|
---|
76 | * @param data Pointer to the kernel kio buffer.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | static void producer(size_t length, char32_t *data)
|
---|
80 | {
|
---|
81 | item_t *item = (item_t *) malloc(sizeof(item_t));
|
---|
82 | if (item == NULL)
|
---|
83 | return;
|
---|
84 |
|
---|
85 | size_t sz = sizeof(char32_t) * length;
|
---|
86 | char32_t *buf = (char32_t *) malloc(sz);
|
---|
87 | if (buf == NULL) {
|
---|
88 | free(item);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | memcpy(buf, data, sz);
|
---|
93 |
|
---|
94 | link_initialize(&item->link);
|
---|
95 | item->length = length;
|
---|
96 | item->data = buf;
|
---|
97 | prodcons_produce(&pc, &item->link);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Klog consumer
|
---|
101 | *
|
---|
102 | * Waits in an infinite loop for the character data created by
|
---|
103 | * the producer and outputs them to stdout and optionally into
|
---|
104 | * a file.
|
---|
105 | *
|
---|
106 | * @param data Unused.
|
---|
107 | *
|
---|
108 | * @return Always EOK (unreachable).
|
---|
109 | *
|
---|
110 | */
|
---|
111 | static errno_t consumer(void *data)
|
---|
112 | {
|
---|
113 | FILE *log = fopen(LOG_FNAME, "a");
|
---|
114 | if (log == NULL)
|
---|
115 | printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
|
---|
116 | str_error(errno));
|
---|
117 |
|
---|
118 | while (true) {
|
---|
119 | link_t *link = prodcons_consume(&pc);
|
---|
120 | item_t *item = list_get_instance(link, item_t, link);
|
---|
121 |
|
---|
122 | for (size_t i = 0; i < item->length; i++)
|
---|
123 | putuchar(item->data[i]);
|
---|
124 |
|
---|
125 | if (log != NULL) {
|
---|
126 | for (size_t i = 0; i < item->length; i++)
|
---|
127 | fputuc(item->data[i], log);
|
---|
128 |
|
---|
129 | fflush(log);
|
---|
130 | vfs_sync(fileno(log));
|
---|
131 | }
|
---|
132 |
|
---|
133 | free(item->data);
|
---|
134 | free(item);
|
---|
135 | }
|
---|
136 |
|
---|
137 | fclose(log);
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Kernel notification handler
|
---|
142 | *
|
---|
143 | * Receives kernel kio notifications.
|
---|
144 | *
|
---|
145 | * @param call IPC call structure
|
---|
146 | * @param arg Local argument
|
---|
147 | *
|
---|
148 | */
|
---|
149 | static void kio_notification_handler(ipc_call_t *call, void *arg)
|
---|
150 | {
|
---|
151 | /*
|
---|
152 | * Make sure we process only a single notification
|
---|
153 | * at any time to limit the chance of the consumer
|
---|
154 | * starving.
|
---|
155 | *
|
---|
156 | * Note: Usually the automatic masking of the kio
|
---|
157 | * notifications on the kernel side does the trick
|
---|
158 | * of limiting the chance of accidentally copying
|
---|
159 | * the same data multiple times. However, due to
|
---|
160 | * the non-blocking architecture of kio notifications,
|
---|
161 | * this possibility cannot be generally avoided.
|
---|
162 | */
|
---|
163 |
|
---|
164 | fibril_mutex_lock(&mtx);
|
---|
165 |
|
---|
166 | size_t kio_start = (size_t) ipc_get_arg1(call);
|
---|
167 | size_t kio_len = (size_t) ipc_get_arg2(call);
|
---|
168 | size_t kio_stored = (size_t) ipc_get_arg3(call);
|
---|
169 |
|
---|
170 | size_t offset = (kio_start + kio_len - kio_stored) % kio_length;
|
---|
171 |
|
---|
172 | /* Copy data from the ring buffer */
|
---|
173 | if (offset + kio_stored >= kio_length) {
|
---|
174 | size_t split = kio_length - offset;
|
---|
175 |
|
---|
176 | producer(split, kio + offset);
|
---|
177 | producer(kio_stored - split, kio);
|
---|
178 | } else
|
---|
179 | producer(kio_stored, kio + offset);
|
---|
180 |
|
---|
181 | async_event_unmask(EVENT_KIO);
|
---|
182 | fibril_mutex_unlock(&mtx);
|
---|
183 | }
|
---|
184 |
|
---|
185 | int main(int argc, char *argv[])
|
---|
186 | {
|
---|
187 | size_t pages;
|
---|
188 | errno_t rc = sysinfo_get_value("kio.pages", &pages);
|
---|
189 | if (rc != EOK) {
|
---|
190 | fprintf(stderr, "%s: Unable to get number of kio pages\n",
|
---|
191 | NAME);
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 | uintptr_t faddr;
|
---|
196 | rc = sysinfo_get_value("kio.faddr", &faddr);
|
---|
197 | if (rc != EOK) {
|
---|
198 | fprintf(stderr, "%s: Unable to get kio physical address\n",
|
---|
199 | NAME);
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 |
|
---|
203 | size_t size = pages * PAGE_SIZE;
|
---|
204 | kio_length = size / sizeof(char32_t);
|
---|
205 |
|
---|
206 | rc = physmem_map(faddr, pages, AS_AREA_READ | AS_AREA_CACHEABLE,
|
---|
207 | (void *) &kio);
|
---|
208 | if (rc != EOK) {
|
---|
209 | fprintf(stderr, "%s: Unable to map kio\n", NAME);
|
---|
210 | return rc;
|
---|
211 | }
|
---|
212 |
|
---|
213 | prodcons_initialize(&pc);
|
---|
214 | rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL);
|
---|
215 | if (rc != EOK) {
|
---|
216 | fprintf(stderr, "%s: Unable to register kio notifications\n",
|
---|
217 | NAME);
|
---|
218 | return rc;
|
---|
219 | }
|
---|
220 |
|
---|
221 | fid_t fid = fibril_create(consumer, NULL);
|
---|
222 | if (!fid) {
|
---|
223 | fprintf(stderr, "%s: Unable to create consumer fibril\n",
|
---|
224 | NAME);
|
---|
225 | return ENOMEM;
|
---|
226 | }
|
---|
227 |
|
---|
228 | tinput_t *input = tinput_new();
|
---|
229 | if (!input) {
|
---|
230 | fprintf(stderr, "%s: Could not create input\n", NAME);
|
---|
231 | return ENOMEM;
|
---|
232 | }
|
---|
233 |
|
---|
234 | fibril_add_ready(fid);
|
---|
235 | async_event_unmask(EVENT_KIO);
|
---|
236 | kio_update();
|
---|
237 |
|
---|
238 | tinput_set_prompt(input, "kio> ");
|
---|
239 |
|
---|
240 | char *str;
|
---|
241 | while ((rc = tinput_read(input, &str)) == EOK) {
|
---|
242 | if (str_cmp(str, "") == 0) {
|
---|
243 | free(str);
|
---|
244 | continue;
|
---|
245 | }
|
---|
246 |
|
---|
247 | kio_command(str, str_size(str));
|
---|
248 | free(str);
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (rc == ENOENT)
|
---|
252 | rc = EOK;
|
---|
253 |
|
---|
254 | return EOK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** @}
|
---|
258 | */
|
---|