Temporarily restore assert.h and errno.h in libposix.
[helenos.git] / uspace / app / kio / kio.c
blobf6c5070bfc910869f572c8f7186deedf9919fc1b
1 /*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup kio KIO
30 * @brief HelenOS KIO
31 * @{
33 /**
34 * @file
37 #include <stdio.h>
38 #include <async.h>
39 #include <as.h>
40 #include <ddi.h>
41 #include <errno.h>
42 #include <str_error.h>
43 #include <io/kio.h>
44 #include <sysinfo.h>
45 #include <stdlib.h>
46 #include <fibril_synch.h>
47 #include <adt/list.h>
48 #include <adt/prodcons.h>
49 #include <tinput.h>
50 #include <vfs/vfs.h>
52 #define NAME "kio"
53 #define LOG_FNAME "/log/kio"
55 /* Producer/consumer buffers */
56 typedef struct {
57 link_t link;
58 size_t length;
59 wchar_t *data;
60 } item_t;
62 static prodcons_t pc;
64 /* Pointer to kio area */
65 static wchar_t *kio = (wchar_t *) AS_AREA_ANY;
66 static size_t kio_length;
68 /* Notification mutex */
69 static FIBRIL_MUTEX_INITIALIZE(mtx);
71 /** Klog producer
73 * Copies the contents of a character buffer to local
74 * producer/consumer queue.
76 * @param length Number of characters to copy.
77 * @param data Pointer to the kernel kio buffer.
80 static void producer(size_t length, wchar_t *data)
82 item_t *item = (item_t *) malloc(sizeof(item_t));
83 if (item == NULL)
84 return;
86 size_t sz = sizeof(wchar_t) * length;
87 wchar_t *buf = (wchar_t *) malloc(sz);
88 if (buf == NULL) {
89 free(item);
90 return;
93 memcpy(buf, data, sz);
95 link_initialize(&item->link);
96 item->length = length;
97 item->data = buf;
98 prodcons_produce(&pc, &item->link);
101 /** Klog consumer
103 * Waits in an infinite loop for the character data created by
104 * the producer and outputs them to stdout and optionally into
105 * a file.
107 * @param data Unused.
109 * @return Always EOK (unreachable).
112 static errno_t consumer(void *data)
114 FILE *log = fopen(LOG_FNAME, "a");
115 if (log == NULL)
116 printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
117 str_error(errno));
119 while (true) {
120 link_t *link = prodcons_consume(&pc);
121 item_t *item = list_get_instance(link, item_t, link);
123 for (size_t i = 0; i < item->length; i++)
124 putchar(item->data[i]);
126 if (log != NULL) {
127 for (size_t i = 0; i < item->length; i++)
128 fputc(item->data[i], log);
130 fflush(log);
131 vfs_sync(fileno(log));
134 free(item->data);
135 free(item);
138 fclose(log);
139 return EOK;
142 /** Kernel notification handler
144 * Receives kernel kio notifications.
146 * @param call IPC call structure
147 * @param arg Local argument
150 static void kio_notification_handler(ipc_call_t *call, void *arg)
153 * Make sure we process only a single notification
154 * at any time to limit the chance of the consumer
155 * starving.
157 * Note: Usually the automatic masking of the kio
158 * notifications on the kernel side does the trick
159 * of limiting the chance of accidentally copying
160 * the same data multiple times. However, due to
161 * the non-blocking architecture of kio notifications,
162 * this possibility cannot be generally avoided.
165 fibril_mutex_lock(&mtx);
167 size_t kio_start = (size_t) IPC_GET_ARG1(*call);
168 size_t kio_len = (size_t) IPC_GET_ARG2(*call);
169 size_t kio_stored = (size_t) IPC_GET_ARG3(*call);
171 size_t offset = (kio_start + kio_len - kio_stored) % kio_length;
173 /* Copy data from the ring buffer */
174 if (offset + kio_stored >= kio_length) {
175 size_t split = kio_length - offset;
177 producer(split, kio + offset);
178 producer(kio_stored - split, kio);
179 } else
180 producer(kio_stored, kio + offset);
182 async_event_unmask(EVENT_KIO);
183 fibril_mutex_unlock(&mtx);
186 int main(int argc, char *argv[])
188 size_t pages;
189 errno_t rc = sysinfo_get_value("kio.pages", &pages);
190 if (rc != EOK) {
191 fprintf(stderr, "%s: Unable to get number of kio pages\n",
192 NAME);
193 return rc;
196 uintptr_t faddr;
197 rc = sysinfo_get_value("kio.faddr", &faddr);
198 if (rc != EOK) {
199 fprintf(stderr, "%s: Unable to get kio physical address\n",
200 NAME);
201 return rc;
204 size_t size = pages * PAGE_SIZE;
205 kio_length = size / sizeof(wchar_t);
207 rc = physmem_map(faddr, pages, AS_AREA_READ | AS_AREA_CACHEABLE,
208 (void *) &kio);
209 if (rc != EOK) {
210 fprintf(stderr, "%s: Unable to map kio\n", NAME);
211 return rc;
214 prodcons_initialize(&pc);
215 rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL);
216 if (rc != EOK) {
217 fprintf(stderr, "%s: Unable to register kio notifications\n",
218 NAME);
219 return rc;
222 fid_t fid = fibril_create(consumer, NULL);
223 if (!fid) {
224 fprintf(stderr, "%s: Unable to create consumer fibril\n",
225 NAME);
226 return ENOMEM;
229 tinput_t *input = tinput_new();
230 if (!input) {
231 fprintf(stderr, "%s: Could not create input\n", NAME);
232 return ENOMEM;
235 fibril_add_ready(fid);
236 async_event_unmask(EVENT_KIO);
237 kio_update();
239 tinput_set_prompt(input, "kio> ");
241 char *str;
242 while ((rc = tinput_read(input, &str)) == EOK) {
243 if (str_cmp(str, "") == 0) {
244 free(str);
245 continue;
248 kio_command(str, str_size(str));
249 free(str);
252 if (rc == ENOENT)
253 rc = EOK;
255 return EOK;
258 /** @}