source: mainline/uspace/app/kio/kio.c@ b8b64a8

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b8b64a8 was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[51dbadf3]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[51dbadf3]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
[a63966d]29/** @addtogroup kio
[b2951e2]30 * @{
[d8fcfc0]31 */
[b2951e2]32/**
33 * @file
34 */
35
[51dbadf3]36#include <stdio.h>
37#include <async.h>
38#include <as.h>
[6119f24]39#include <ddi.h>
[1c03c17]40#include <errno.h>
[5a9f4d7]41#include <str_error.h>
[6fa9a99d]42#include <io/kio.h>
[6119f24]43#include <sysinfo.h>
[38d150e]44#include <stdlib.h>
[4e1a2f5]45#include <fibril_synch.h>
46#include <adt/list.h>
47#include <adt/prodcons.h>
[297cb73]48#include <tinput.h>
[a56cef9]49#include <vfs/vfs.h>
[1c03c17]50
[6fa9a99d]51#define NAME "kio"
52#define LOG_FNAME "/log/kio"
[1c03c17]53
[4e1a2f5]54/* Producer/consumer buffers */
55typedef struct {
56 link_t link;
57 size_t length;
[28a5ebd]58 char32_t *data;
[4e1a2f5]59} item_t;
60
61static prodcons_t pc;
62
[6fa9a99d]63/* Pointer to kio area */
[28a5ebd]64static char32_t *kio = (char32_t *) AS_AREA_ANY;
[6fa9a99d]65static size_t kio_length;
[51dbadf3]66
[4e1a2f5]67/* Notification mutex */
68static FIBRIL_MUTEX_INITIALIZE(mtx);
[5a9f4d7]69
[4e1a2f5]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.
[6fa9a99d]76 * @param data Pointer to the kernel kio buffer.
[4e1a2f5]77 *
78 */
[28a5ebd]79static void producer(size_t length, char32_t *data)
[51dbadf3]80{
[4e1a2f5]81 item_t *item = (item_t *) malloc(sizeof(item_t));
82 if (item == NULL)
83 return;
[a35b458]84
[28a5ebd]85 size_t sz = sizeof(char32_t) * length;
86 char32_t *buf = (char32_t *) malloc(sz);
[659ebd86]87 if (buf == NULL) {
[4e1a2f5]88 free(item);
89 return;
90 }
[a35b458]91
[4e1a2f5]92 memcpy(buf, data, sz);
[a35b458]93
[4e1a2f5]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 */
[b7fd2a0]111static errno_t consumer(void *data)
[4e1a2f5]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));
[a35b458]117
[4e1a2f5]118 while (true) {
119 link_t *link = prodcons_consume(&pc);
120 item_t *item = list_get_instance(link, item_t, link);
[a35b458]121
[4e1a2f5]122 for (size_t i = 0; i < item->length; i++)
[28a5ebd]123 putuchar(item->data[i]);
[a35b458]124
[4e1a2f5]125 if (log != NULL) {
126 for (size_t i = 0; i < item->length; i++)
[28a5ebd]127 fputuc(item->data[i], log);
[a35b458]128
[4e1a2f5]129 fflush(log);
[a56cef9]130 vfs_sync(fileno(log));
[4e1a2f5]131 }
[a35b458]132
[4e1a2f5]133 free(item->data);
134 free(item);
[5a9f4d7]135 }
[a35b458]136
[4e1a2f5]137 fclose(log);
138 return EOK;
139}
140
141/** Kernel notification handler
142 *
[6fa9a99d]143 * Receives kernel kio notifications.
[4e1a2f5]144 *
[3815efb]145 * @param call IPC call structure
146 * @param arg Local argument
[4e1a2f5]147 *
148 */
[01c3bb4]149static void kio_notification_handler(ipc_call_t *call, void *arg)
[4e1a2f5]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 *
[6fa9a99d]156 * Note: Usually the automatic masking of the kio
[4e1a2f5]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
[6fa9a99d]160 * the non-blocking architecture of kio notifications,
[4e1a2f5]161 * this possibility cannot be generally avoided.
162 */
[a35b458]163
[4e1a2f5]164 fibril_mutex_lock(&mtx);
[a35b458]165
[fafb8e5]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);
[a35b458]169
[6fa9a99d]170 size_t offset = (kio_start + kio_len - kio_stored) % kio_length;
[a35b458]171
[4e1a2f5]172 /* Copy data from the ring buffer */
[6fa9a99d]173 if (offset + kio_stored >= kio_length) {
174 size_t split = kio_length - offset;
[a35b458]175
[6fa9a99d]176 producer(split, kio + offset);
177 producer(kio_stored - split, kio);
[4e1a2f5]178 } else
[6fa9a99d]179 producer(kio_stored, kio + offset);
[a35b458]180
[8820544]181 async_event_unmask(EVENT_KIO);
[4e1a2f5]182 fibril_mutex_unlock(&mtx);
[51dbadf3]183}
184
185int main(int argc, char *argv[])
186{
[6119f24]187 size_t pages;
[b7fd2a0]188 errno_t rc = sysinfo_get_value("kio.pages", &pages);
[6119f24]189 if (rc != EOK) {
[6fa9a99d]190 fprintf(stderr, "%s: Unable to get number of kio pages\n",
[6119f24]191 NAME);
192 return rc;
193 }
[a35b458]194
[6119f24]195 uintptr_t faddr;
[6fa9a99d]196 rc = sysinfo_get_value("kio.faddr", &faddr);
[6119f24]197 if (rc != EOK) {
[6fa9a99d]198 fprintf(stderr, "%s: Unable to get kio physical address\n",
[6119f24]199 NAME);
200 return rc;
201 }
[a35b458]202
[6119f24]203 size_t size = pages * PAGE_SIZE;
[28a5ebd]204 kio_length = size / sizeof(char32_t);
[a35b458]205
[8442d10]206 rc = physmem_map(faddr, pages, AS_AREA_READ | AS_AREA_CACHEABLE,
[6fa9a99d]207 (void *) &kio);
[6119f24]208 if (rc != EOK) {
[6fa9a99d]209 fprintf(stderr, "%s: Unable to map kio\n", NAME);
[6119f24]210 return rc;
[51dbadf3]211 }
[a35b458]212
[4e1a2f5]213 prodcons_initialize(&pc);
[8820544]214 rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL);
[6119f24]215 if (rc != EOK) {
[6fa9a99d]216 fprintf(stderr, "%s: Unable to register kio notifications\n",
[6119f24]217 NAME);
218 return rc;
[05641a9e]219 }
[a35b458]220
[4e1a2f5]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 }
[a35b458]227
[297cb73]228 tinput_t *input = tinput_new();
229 if (!input) {
230 fprintf(stderr, "%s: Could not create input\n", NAME);
231 return ENOMEM;
[8820544]232 }
[297cb73]233
[4e1a2f5]234 fibril_add_ready(fid);
[8820544]235 async_event_unmask(EVENT_KIO);
[6fa9a99d]236 kio_update();
[a35b458]237
[6fa9a99d]238 tinput_set_prompt(input, "kio> ");
[297cb73]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
[6fa9a99d]247 kio_command(str, str_size(str));
[297cb73]248 free(str);
249 }
[8820544]250
[297cb73]251 if (rc == ENOENT)
[8820544]252 rc = EOK;
[297cb73]253
254 return EOK;
[51dbadf3]255}
[b2951e2]256
257/** @}
258 */
Note: See TracBrowser for help on using the repository browser.