| 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 KIO | 
|---|
| 30 | * @brief HelenOS KIO | 
|---|
| 31 | * @{ | 
|---|
| 32 | */ | 
|---|
| 33 | /** | 
|---|
| 34 | * @file | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 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 <malloc.h> | 
|---|
| 46 | #include <fibril_synch.h> | 
|---|
| 47 | #include <adt/list.h> | 
|---|
| 48 | #include <adt/prodcons.h> | 
|---|
| 49 | #include <tinput.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 | wchar_t *data; | 
|---|
| 59 | } item_t; | 
|---|
| 60 |  | 
|---|
| 61 | static prodcons_t pc; | 
|---|
| 62 |  | 
|---|
| 63 | /* Pointer to kio area */ | 
|---|
| 64 | static wchar_t *kio = (wchar_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, wchar_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(wchar_t) * length; | 
|---|
| 86 | wchar_t *buf = (wchar_t *) malloc(sz); | 
|---|
| 87 | if (data == 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 int 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 | putchar(item->data[i]); | 
|---|
| 124 |  | 
|---|
| 125 | if (log != NULL) { | 
|---|
| 126 | for (size_t i = 0; i < item->length; i++) | 
|---|
| 127 | fputc(item->data[i], log); | 
|---|
| 128 |  | 
|---|
| 129 | fflush(log); | 
|---|
| 130 | fsync(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 callid IPC call ID | 
|---|
| 146 | * @param call   IPC call structure | 
|---|
| 147 | * @param arg    Local argument | 
|---|
| 148 | * | 
|---|
| 149 | */ | 
|---|
| 150 | static void kio_notification_handler(ipc_callid_t callid, ipc_call_t *call, | 
|---|
| 151 | void *arg) | 
|---|
| 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 | * | 
|---|
| 158 | * Note: Usually the automatic masking of the kio | 
|---|
| 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 | 
|---|
| 162 | * the non-blocking architecture of kio notifications, | 
|---|
| 163 | * this possibility cannot be generally avoided. | 
|---|
| 164 | */ | 
|---|
| 165 |  | 
|---|
| 166 | fibril_mutex_lock(&mtx); | 
|---|
| 167 |  | 
|---|
| 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); | 
|---|
| 171 |  | 
|---|
| 172 | size_t offset = (kio_start + kio_len - kio_stored) % kio_length; | 
|---|
| 173 |  | 
|---|
| 174 | /* Copy data from the ring buffer */ | 
|---|
| 175 | if (offset + kio_stored >= kio_length) { | 
|---|
| 176 | size_t split = kio_length - offset; | 
|---|
| 177 |  | 
|---|
| 178 | producer(split, kio + offset); | 
|---|
| 179 | producer(kio_stored - split, kio); | 
|---|
| 180 | } else | 
|---|
| 181 | producer(kio_stored, kio + offset); | 
|---|
| 182 |  | 
|---|
| 183 | async_event_unmask(EVENT_KIO); | 
|---|
| 184 | fibril_mutex_unlock(&mtx); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | int main(int argc, char *argv[]) | 
|---|
| 188 | { | 
|---|
| 189 | size_t pages; | 
|---|
| 190 | int rc = sysinfo_get_value("kio.pages", &pages); | 
|---|
| 191 | if (rc != EOK) { | 
|---|
| 192 | fprintf(stderr, "%s: Unable to get number of kio pages\n", | 
|---|
| 193 | NAME); | 
|---|
| 194 | return rc; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | uintptr_t faddr; | 
|---|
| 198 | rc = sysinfo_get_value("kio.faddr", &faddr); | 
|---|
| 199 | if (rc != EOK) { | 
|---|
| 200 | fprintf(stderr, "%s: Unable to get kio physical address\n", | 
|---|
| 201 | NAME); | 
|---|
| 202 | return rc; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | size_t size = pages * PAGE_SIZE; | 
|---|
| 206 | kio_length = size / sizeof(wchar_t); | 
|---|
| 207 |  | 
|---|
| 208 | rc = physmem_map(faddr, pages, AS_AREA_READ | AS_AREA_CACHEABLE, | 
|---|
| 209 | (void *) &kio); | 
|---|
| 210 | if (rc != EOK) { | 
|---|
| 211 | fprintf(stderr, "%s: Unable to map kio\n", NAME); | 
|---|
| 212 | return rc; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | prodcons_initialize(&pc); | 
|---|
| 216 | rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL); | 
|---|
| 217 | if (rc != EOK) { | 
|---|
| 218 | fprintf(stderr, "%s: Unable to register kio notifications\n", | 
|---|
| 219 | NAME); | 
|---|
| 220 | return rc; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | fid_t fid = fibril_create(consumer, NULL); | 
|---|
| 224 | if (!fid) { | 
|---|
| 225 | fprintf(stderr, "%s: Unable to create consumer fibril\n", | 
|---|
| 226 | NAME); | 
|---|
| 227 | return ENOMEM; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | tinput_t *input = tinput_new(); | 
|---|
| 231 | if (!input) { | 
|---|
| 232 | fprintf(stderr, "%s: Could not create input\n", NAME); | 
|---|
| 233 | return ENOMEM; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | fibril_add_ready(fid); | 
|---|
| 237 | async_event_unmask(EVENT_KIO); | 
|---|
| 238 | kio_update(); | 
|---|
| 239 |  | 
|---|
| 240 | tinput_set_prompt(input, "kio> "); | 
|---|
| 241 |  | 
|---|
| 242 | char *str; | 
|---|
| 243 | while ((rc = tinput_read(input, &str)) == EOK) { | 
|---|
| 244 | if (str_cmp(str, "") == 0) { | 
|---|
| 245 | free(str); | 
|---|
| 246 | continue; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | kio_command(str, str_size(str)); | 
|---|
| 250 | free(str); | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | if (rc == ENOENT) | 
|---|
| 254 | rc = EOK; | 
|---|
| 255 |  | 
|---|
| 256 | return EOK; | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | /** @} | 
|---|
| 260 | */ | 
|---|