[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 |
|
---|
[6fa9a99d] | 29 | /** @addtogroup kio KIO
|
---|
| 30 | * @brief HelenOS KIO
|
---|
[b2951e2] | 31 | * @{
|
---|
[d8fcfc0] | 32 | */
|
---|
[b2951e2] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[51dbadf3] | 37 | #include <stdio.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <as.h>
|
---|
[6119f24] | 40 | #include <ddi.h>
|
---|
[05641a9e] | 41 | #include <event.h>
|
---|
[1c03c17] | 42 | #include <errno.h>
|
---|
[5a9f4d7] | 43 | #include <str_error.h>
|
---|
[6fa9a99d] | 44 | #include <io/kio.h>
|
---|
[6119f24] | 45 | #include <sysinfo.h>
|
---|
[4e1a2f5] | 46 | #include <malloc.h>
|
---|
| 47 | #include <fibril_synch.h>
|
---|
| 48 | #include <adt/list.h>
|
---|
| 49 | #include <adt/prodcons.h>
|
---|
[297cb73] | 50 | #include <tinput.h>
|
---|
[1c03c17] | 51 |
|
---|
[6fa9a99d] | 52 | #define NAME "kio"
|
---|
| 53 | #define LOG_FNAME "/log/kio"
|
---|
[1c03c17] | 54 |
|
---|
[4e1a2f5] | 55 | /* Producer/consumer buffers */
|
---|
| 56 | typedef struct {
|
---|
| 57 | link_t link;
|
---|
| 58 | size_t length;
|
---|
| 59 | wchar_t *data;
|
---|
| 60 | } item_t;
|
---|
| 61 |
|
---|
| 62 | static prodcons_t pc;
|
---|
| 63 |
|
---|
[6fa9a99d] | 64 | /* Pointer to kio area */
|
---|
[bf9cb2f] | 65 | static wchar_t *kio = (wchar_t *) AS_AREA_ANY;
|
---|
[6fa9a99d] | 66 | static size_t kio_length;
|
---|
[51dbadf3] | 67 |
|
---|
[4e1a2f5] | 68 | /* Notification mutex */
|
---|
| 69 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
[5a9f4d7] | 70 |
|
---|
[4e1a2f5] | 71 | /** Klog producer
|
---|
| 72 | *
|
---|
| 73 | * Copies the contents of a character buffer to local
|
---|
| 74 | * producer/consumer queue.
|
---|
| 75 | *
|
---|
| 76 | * @param length Number of characters to copy.
|
---|
[6fa9a99d] | 77 | * @param data Pointer to the kernel kio buffer.
|
---|
[4e1a2f5] | 78 | *
|
---|
| 79 | */
|
---|
| 80 | static void producer(size_t length, wchar_t *data)
|
---|
[51dbadf3] | 81 | {
|
---|
[4e1a2f5] | 82 | item_t *item = (item_t *) malloc(sizeof(item_t));
|
---|
| 83 | if (item == NULL)
|
---|
| 84 | return;
|
---|
| 85 |
|
---|
| 86 | size_t sz = sizeof(wchar_t) * length;
|
---|
| 87 | wchar_t *buf = (wchar_t *) malloc(sz);
|
---|
| 88 | if (data == NULL) {
|
---|
| 89 | free(item);
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | memcpy(buf, data, sz);
|
---|
| 94 |
|
---|
| 95 | link_initialize(&item->link);
|
---|
| 96 | item->length = length;
|
---|
| 97 | item->data = buf;
|
---|
| 98 | prodcons_produce(&pc, &item->link);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Klog consumer
|
---|
| 102 | *
|
---|
| 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.
|
---|
| 106 | *
|
---|
| 107 | * @param data Unused.
|
---|
| 108 | *
|
---|
| 109 | * @return Always EOK (unreachable).
|
---|
| 110 | *
|
---|
| 111 | */
|
---|
| 112 | static int consumer(void *data)
|
---|
| 113 | {
|
---|
| 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));
|
---|
[3636964] | 118 |
|
---|
[4e1a2f5] | 119 | while (true) {
|
---|
| 120 | link_t *link = prodcons_consume(&pc);
|
---|
| 121 | item_t *item = list_get_instance(link, item_t, link);
|
---|
| 122 |
|
---|
| 123 | for (size_t i = 0; i < item->length; i++)
|
---|
| 124 | putchar(item->data[i]);
|
---|
[5a9f4d7] | 125 |
|
---|
[4e1a2f5] | 126 | if (log != NULL) {
|
---|
| 127 | for (size_t i = 0; i < item->length; i++)
|
---|
| 128 | fputc(item->data[i], log);
|
---|
| 129 |
|
---|
| 130 | fflush(log);
|
---|
| 131 | fsync(fileno(log));
|
---|
| 132 | }
|
---|
[5a9f4d7] | 133 |
|
---|
[4e1a2f5] | 134 | free(item->data);
|
---|
| 135 | free(item);
|
---|
[5a9f4d7] | 136 | }
|
---|
| 137 |
|
---|
[4e1a2f5] | 138 | fclose(log);
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** Kernel notification handler
|
---|
| 143 | *
|
---|
[6fa9a99d] | 144 | * Receives kernel kio notifications.
|
---|
[4e1a2f5] | 145 | *
|
---|
[3815efb] | 146 | * @param callid IPC call ID
|
---|
| 147 | * @param call IPC call structure
|
---|
| 148 | * @param arg Local argument
|
---|
[4e1a2f5] | 149 | *
|
---|
| 150 | */
|
---|
| 151 | static void notification_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 152 | {
|
---|
| 153 | /*
|
---|
| 154 | * Make sure we process only a single notification
|
---|
| 155 | * at any time to limit the chance of the consumer
|
---|
| 156 | * starving.
|
---|
| 157 | *
|
---|
[6fa9a99d] | 158 | * Note: Usually the automatic masking of the kio
|
---|
[4e1a2f5] | 159 | * notifications on the kernel side does the trick
|
---|
| 160 | * of limiting the chance of accidentally copying
|
---|
| 161 | * the same data multiple times. However, due to
|
---|
[6fa9a99d] | 162 | * the non-blocking architecture of kio notifications,
|
---|
[4e1a2f5] | 163 | * this possibility cannot be generally avoided.
|
---|
| 164 | */
|
---|
| 165 |
|
---|
| 166 | fibril_mutex_lock(&mtx);
|
---|
| 167 |
|
---|
[6fa9a99d] | 168 | size_t kio_start = (size_t) IPC_GET_ARG1(*call);
|
---|
| 169 | size_t kio_len = (size_t) IPC_GET_ARG2(*call);
|
---|
| 170 | size_t kio_stored = (size_t) IPC_GET_ARG3(*call);
|
---|
[4e1a2f5] | 171 |
|
---|
[6fa9a99d] | 172 | size_t offset = (kio_start + kio_len - kio_stored) % kio_length;
|
---|
[4e1a2f5] | 173 |
|
---|
| 174 | /* Copy data from the ring buffer */
|
---|
[6fa9a99d] | 175 | if (offset + kio_stored >= kio_length) {
|
---|
| 176 | size_t split = kio_length - offset;
|
---|
[4e1a2f5] | 177 |
|
---|
[6fa9a99d] | 178 | producer(split, kio + offset);
|
---|
| 179 | producer(kio_stored - split, kio);
|
---|
[4e1a2f5] | 180 | } else
|
---|
[6fa9a99d] | 181 | producer(kio_stored, kio + offset);
|
---|
[f9061b4] | 182 |
|
---|
[6fa9a99d] | 183 | event_unmask(EVENT_KIO);
|
---|
[4e1a2f5] | 184 | fibril_mutex_unlock(&mtx);
|
---|
[51dbadf3] | 185 | }
|
---|
| 186 |
|
---|
| 187 | int main(int argc, char *argv[])
|
---|
| 188 | {
|
---|
[6119f24] | 189 | size_t pages;
|
---|
[6fa9a99d] | 190 | int rc = sysinfo_get_value("kio.pages", &pages);
|
---|
[6119f24] | 191 | if (rc != EOK) {
|
---|
[6fa9a99d] | 192 | fprintf(stderr, "%s: Unable to get number of kio pages\n",
|
---|
[6119f24] | 193 | NAME);
|
---|
| 194 | return rc;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | uintptr_t faddr;
|
---|
[6fa9a99d] | 198 | rc = sysinfo_get_value("kio.faddr", &faddr);
|
---|
[6119f24] | 199 | if (rc != EOK) {
|
---|
[6fa9a99d] | 200 | fprintf(stderr, "%s: Unable to get kio physical address\n",
|
---|
[6119f24] | 201 | NAME);
|
---|
| 202 | return rc;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | size_t size = pages * PAGE_SIZE;
|
---|
[6fa9a99d] | 206 | kio_length = size / sizeof(wchar_t);
|
---|
[6119f24] | 207 |
|
---|
[8442d10] | 208 | rc = physmem_map(faddr, pages, AS_AREA_READ | AS_AREA_CACHEABLE,
|
---|
[6fa9a99d] | 209 | (void *) &kio);
|
---|
[6119f24] | 210 | if (rc != EOK) {
|
---|
[6fa9a99d] | 211 | fprintf(stderr, "%s: Unable to map kio\n", NAME);
|
---|
[6119f24] | 212 | return rc;
|
---|
[51dbadf3] | 213 | }
|
---|
[3ad953c] | 214 |
|
---|
[4e1a2f5] | 215 | prodcons_initialize(&pc);
|
---|
| 216 | async_set_interrupt_received(notification_received);
|
---|
[6fa9a99d] | 217 | rc = event_subscribe(EVENT_KIO, 0);
|
---|
[6119f24] | 218 | if (rc != EOK) {
|
---|
[6fa9a99d] | 219 | fprintf(stderr, "%s: Unable to register kio notifications\n",
|
---|
[6119f24] | 220 | NAME);
|
---|
| 221 | return rc;
|
---|
[05641a9e] | 222 | }
|
---|
[1c03c17] | 223 |
|
---|
[4e1a2f5] | 224 | fid_t fid = fibril_create(consumer, NULL);
|
---|
| 225 | if (!fid) {
|
---|
| 226 | fprintf(stderr, "%s: Unable to create consumer fibril\n",
|
---|
| 227 | NAME);
|
---|
| 228 | return ENOMEM;
|
---|
| 229 | }
|
---|
[5a9f4d7] | 230 |
|
---|
[297cb73] | 231 | tinput_t *input = tinput_new();
|
---|
| 232 | if (!input) {
|
---|
| 233 | fprintf(stderr, "%s: Could not create input\n", NAME);
|
---|
| 234 | return ENOMEM;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[4e1a2f5] | 237 | fibril_add_ready(fid);
|
---|
[6fa9a99d] | 238 | event_unmask(EVENT_KIO);
|
---|
| 239 | kio_update();
|
---|
[4e1a2f5] | 240 |
|
---|
[6fa9a99d] | 241 | tinput_set_prompt(input, "kio> ");
|
---|
[297cb73] | 242 |
|
---|
| 243 | char *str;
|
---|
| 244 | while ((rc = tinput_read(input, &str)) == EOK) {
|
---|
| 245 | if (str_cmp(str, "") == 0) {
|
---|
| 246 | free(str);
|
---|
| 247 | continue;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[6fa9a99d] | 250 | kio_command(str, str_size(str));
|
---|
[297cb73] | 251 | free(str);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | if (rc == ENOENT)
|
---|
| 255 | rc = EOK;
|
---|
| 256 |
|
---|
| 257 | return EOK;
|
---|
[51dbadf3] | 258 | }
|
---|
[b2951e2] | 259 |
|
---|
| 260 | /** @}
|
---|
| 261 | */
|
---|