[6d15572] | 1 | /*
|
---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
---|
[392f0e7] | 3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[6d15572] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @file Ski console driver.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf3a905c] | 33 | #include <as.h>
|
---|
[39026d7c] | 34 | #include <async.h>
|
---|
[6d15572] | 35 | #include <ddf/driver.h>
|
---|
| 36 | #include <ddf/log.h>
|
---|
[cf3a905c] | 37 | #include <ddi.h>
|
---|
[6d15572] | 38 | #include <errno.h>
|
---|
[7a6065c] | 39 | #include <fibril.h>
|
---|
| 40 | #include <io/chardev.h>
|
---|
[6d15572] | 41 | #include <stdint.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <stdbool.h>
|
---|
[392f0e7] | 44 | #include <sysinfo.h>
|
---|
[6d15572] | 45 |
|
---|
| 46 | #include "ski-con.h"
|
---|
| 47 |
|
---|
| 48 | #define SKI_GETCHAR 21
|
---|
[9940ce0] | 49 | #define SKI_PUTCHAR 31
|
---|
[6d15572] | 50 |
|
---|
| 51 | #define POLL_INTERVAL 10000
|
---|
| 52 |
|
---|
[b7fd2a0] | 53 | static errno_t ski_con_fibril(void *arg);
|
---|
[6d15572] | 54 | static int32_t ski_con_getchar(void);
|
---|
[984a9ba] | 55 | static void ski_con_connection(ipc_call_t *, void *);
|
---|
[6d15572] | 56 |
|
---|
[f2d88f3] | 57 | static errno_t ski_con_read(chardev_srv_t *, void *, size_t, size_t *,
|
---|
| 58 | chardev_flags_t);
|
---|
[b7fd2a0] | 59 | static errno_t ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
---|
[7a6065c] | 60 |
|
---|
| 61 | static chardev_ops_t ski_con_chardev_ops = {
|
---|
| 62 | .read = ski_con_read,
|
---|
| 63 | .write = ski_con_write
|
---|
| 64 | };
|
---|
| 65 |
|
---|
[9940ce0] | 66 | static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
|
---|
| 67 |
|
---|
[676e833] | 68 | /** Add ski console device. */
|
---|
[b7fd2a0] | 69 | errno_t ski_con_add(ski_con_t *con)
|
---|
[6d15572] | 70 | {
|
---|
[7a6065c] | 71 | fid_t fid;
|
---|
[6d15572] | 72 | ddf_fun_t *fun = NULL;
|
---|
| 73 | bool bound = false;
|
---|
[cf3a905c] | 74 | uintptr_t faddr;
|
---|
| 75 | void *addr = AS_AREA_ANY;
|
---|
[b7fd2a0] | 76 | errno_t rc;
|
---|
[6d15572] | 77 |
|
---|
[7a6065c] | 78 | circ_buf_init(&con->cbuf, con->buf, ski_con_buf_size, 1);
|
---|
| 79 | fibril_mutex_initialize(&con->buf_lock);
|
---|
| 80 | fibril_condvar_initialize(&con->buf_cv);
|
---|
| 81 |
|
---|
[6d15572] | 82 | fun = ddf_fun_create(con->dev, fun_exposed, "a");
|
---|
| 83 | if (fun == NULL) {
|
---|
| 84 | ddf_msg(LVL_ERROR, "Error creating function 'a'.");
|
---|
| 85 | rc = ENOMEM;
|
---|
| 86 | goto error;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | ddf_fun_set_conn_handler(fun, ski_con_connection);
|
---|
| 90 |
|
---|
[7a6065c] | 91 | chardev_srvs_init(&con->cds);
|
---|
| 92 | con->cds.ops = &ski_con_chardev_ops;
|
---|
| 93 | con->cds.sarg = con;
|
---|
| 94 |
|
---|
[cf3a905c] | 95 | rc = sysinfo_get_value("ski.paddr", &faddr);
|
---|
| 96 | if (rc != EOK)
|
---|
| 97 | faddr = 0; /* No kernel driver to arbitrate with */
|
---|
| 98 |
|
---|
| 99 | if (faddr != 0) {
|
---|
| 100 | addr = AS_AREA_ANY;
|
---|
| 101 | rc = physmem_map(faddr, 1, AS_AREA_READ | AS_AREA_CACHEABLE,
|
---|
| 102 | &addr);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | ddf_msg(LVL_ERROR, "Cannot map kernel driver arbitration area.");
|
---|
| 105 | goto error;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[6d15572] | 109 | rc = ddf_fun_bind(fun);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | ddf_msg(LVL_ERROR, "Error binding function 'a'.");
|
---|
| 112 | goto error;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | bound = true;
|
---|
| 116 |
|
---|
[4f87a85a] | 117 | rc = ddf_fun_add_to_category(fun, "console");
|
---|
| 118 | if (rc != EOK) {
|
---|
| 119 | ddf_msg(LVL_ERROR, "Error adding function 'a' to category "
|
---|
| 120 | "'console'.");
|
---|
| 121 | goto error;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[7a6065c] | 124 | fid = fibril_create(ski_con_fibril, con);
|
---|
| 125 | if (fid == 0) {
|
---|
| 126 | ddf_msg(LVL_ERROR, "Error creating fibril.");
|
---|
| 127 | rc = ENOMEM;
|
---|
| 128 | goto error;
|
---|
[6d15572] | 129 | }
|
---|
| 130 |
|
---|
[7a6065c] | 131 | fibril_add_ready(fid);
|
---|
[6d15572] | 132 | return EOK;
|
---|
| 133 | error:
|
---|
[cf3a905c] | 134 | if (addr != AS_AREA_ANY)
|
---|
| 135 | as_area_destroy(addr);
|
---|
[6d15572] | 136 | if (bound)
|
---|
| 137 | ddf_fun_unbind(fun);
|
---|
| 138 | if (fun != NULL)
|
---|
| 139 | ddf_fun_destroy(fun);
|
---|
| 140 |
|
---|
| 141 | return rc;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Remove ski console device */
|
---|
[b7fd2a0] | 145 | errno_t ski_con_remove(ski_con_t *con)
|
---|
[6d15572] | 146 | {
|
---|
| 147 | return ENOTSUP;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Ski console device gone */
|
---|
[b7fd2a0] | 151 | errno_t ski_con_gone(ski_con_t *con)
|
---|
[6d15572] | 152 | {
|
---|
| 153 | return ENOTSUP;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[392f0e7] | 156 | /** Detect if SKI console is in use by the kernel.
|
---|
| 157 | *
|
---|
| 158 | * This is needed since the kernel has no way of fencing off the user-space
|
---|
| 159 | * driver.
|
---|
| 160 | *
|
---|
| 161 | * @return @c true if in use by the kernel.
|
---|
| 162 | */
|
---|
| 163 | static bool ski_con_disabled(void)
|
---|
| 164 | {
|
---|
| 165 | sysarg_t kconsole;
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * XXX Ideally we should get information from our kernel counterpart
|
---|
| 169 | * driver. But there needs to be a mechanism for the kernel console
|
---|
| 170 | * to inform the kernel driver.
|
---|
| 171 | */
|
---|
| 172 | if (sysinfo_get_value("kconsole", &kconsole) != EOK)
|
---|
| 173 | return false;
|
---|
| 174 |
|
---|
| 175 | return kconsole != false;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[7a6065c] | 178 | /** Poll Ski for keypresses. */
|
---|
[b7fd2a0] | 179 | static errno_t ski_con_fibril(void *arg)
|
---|
[6d15572] | 180 | {
|
---|
| 181 | int32_t c;
|
---|
| 182 | ski_con_t *con = (ski_con_t *) arg;
|
---|
[b7fd2a0] | 183 | errno_t rc;
|
---|
[6d15572] | 184 |
|
---|
[76d0981d] | 185 | while (true) {
|
---|
[392f0e7] | 186 | while (!ski_con_disabled()) {
|
---|
[6d15572] | 187 | c = ski_con_getchar();
|
---|
| 188 | if (c == 0)
|
---|
| 189 | break;
|
---|
| 190 |
|
---|
[7a6065c] | 191 | fibril_mutex_lock(&con->buf_lock);
|
---|
| 192 |
|
---|
| 193 | rc = circ_buf_push(&con->cbuf, &c);
|
---|
| 194 | if (rc != EOK)
|
---|
| 195 | ddf_msg(LVL_ERROR, "Buffer overrun");
|
---|
| 196 |
|
---|
| 197 | fibril_mutex_unlock(&con->buf_lock);
|
---|
| 198 | fibril_condvar_broadcast(&con->buf_cv);
|
---|
[6d15572] | 199 | }
|
---|
| 200 |
|
---|
[5f97ef44] | 201 | fibril_usleep(POLL_INTERVAL);
|
---|
[6d15572] | 202 | }
|
---|
[7a6065c] | 203 |
|
---|
| 204 | return 0;
|
---|
[6d15572] | 205 | }
|
---|
| 206 |
|
---|
| 207 | /** Ask Ski if a key was pressed.
|
---|
| 208 | *
|
---|
| 209 | * Use SSC (Simulator System Call) to get character from the debug console.
|
---|
| 210 | * This call is non-blocking.
|
---|
| 211 | *
|
---|
| 212 | * @return ASCII code of pressed key or 0 if no key pressed.
|
---|
| 213 | */
|
---|
| 214 | static int32_t ski_con_getchar(void)
|
---|
| 215 | {
|
---|
| 216 | uint64_t ch;
|
---|
| 217 |
|
---|
| 218 | #ifdef UARCH_ia64
|
---|
| 219 | asm volatile (
|
---|
[1433ecda] | 220 | "mov r15 = %1\n"
|
---|
| 221 | "break 0x80000;;\n" /* modifies r8 */
|
---|
| 222 | "mov %0 = r8;;\n"
|
---|
[6d15572] | 223 |
|
---|
[1433ecda] | 224 | : "=r" (ch)
|
---|
| 225 | : "i" (SKI_GETCHAR)
|
---|
| 226 | : "r15", "r8"
|
---|
[6d15572] | 227 | );
|
---|
| 228 | #else
|
---|
| 229 | ch = 0;
|
---|
| 230 | #endif
|
---|
| 231 | return (int32_t) ch;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[9940ce0] | 234 | /** Display character on ski debug console
|
---|
| 235 | *
|
---|
| 236 | * Use SSC (Simulator System Call) to
|
---|
| 237 | * display character on debug console.
|
---|
| 238 | *
|
---|
| 239 | * @param c Character to be printed.
|
---|
| 240 | *
|
---|
| 241 | */
|
---|
[6d15572] | 242 | static void ski_con_putchar(ski_con_t *con, char ch)
|
---|
| 243 | {
|
---|
[5f4c41b2] | 244 | if (ch == '\n')
|
---|
| 245 | ski_con_putchar(con, '\r');
|
---|
| 246 |
|
---|
[9940ce0] | 247 | #ifdef UARCH_ia64
|
---|
| 248 | asm volatile (
|
---|
[1433ecda] | 249 | "mov r15 = %0\n"
|
---|
| 250 | "mov r32 = %1\n" /* r32 is in0 */
|
---|
| 251 | "break 0x80000\n" /* modifies r8 */
|
---|
| 252 | :
|
---|
| 253 | : "i" (SKI_PUTCHAR), "r" (ch)
|
---|
| 254 | : "r15", "in0", "r8"
|
---|
[9940ce0] | 255 | );
|
---|
| 256 | #else
|
---|
| 257 | (void) ch;
|
---|
| 258 | #endif
|
---|
[6d15572] | 259 | }
|
---|
| 260 |
|
---|
[7a6065c] | 261 | /** Read from Ski console device */
|
---|
[b7fd2a0] | 262 | static errno_t ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
---|
[f2d88f3] | 263 | size_t *nread, chardev_flags_t flags)
|
---|
[6d15572] | 264 | {
|
---|
[7a6065c] | 265 | ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
|
---|
| 266 | size_t p;
|
---|
| 267 | uint8_t *bp = (uint8_t *) buf;
|
---|
[b7fd2a0] | 268 | errno_t rc;
|
---|
[6d15572] | 269 |
|
---|
[7a6065c] | 270 | fibril_mutex_lock(&con->buf_lock);
|
---|
[6d15572] | 271 |
|
---|
[f2d88f3] | 272 | while ((flags & chardev_f_nonblock) == 0 &&
|
---|
| 273 | circ_buf_nused(&con->cbuf) == 0)
|
---|
[7a6065c] | 274 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
---|
[6d15572] | 275 |
|
---|
[7a6065c] | 276 | p = 0;
|
---|
| 277 | while (p < size) {
|
---|
| 278 | rc = circ_buf_pop(&con->cbuf, &bp[p]);
|
---|
| 279 | if (rc != EOK)
|
---|
| 280 | break;
|
---|
| 281 | ++p;
|
---|
| 282 | }
|
---|
[6d15572] | 283 |
|
---|
[7a6065c] | 284 | fibril_mutex_unlock(&con->buf_lock);
|
---|
[6d15572] | 285 |
|
---|
[7a6065c] | 286 | *nread = p;
|
---|
| 287 | return EOK;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /** Write to Ski console device */
|
---|
[b7fd2a0] | 291 | static errno_t ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
---|
[7a6065c] | 292 | size_t *nwr)
|
---|
| 293 | {
|
---|
| 294 | ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
|
---|
| 295 | size_t i;
|
---|
| 296 | uint8_t *dp = (uint8_t *) data;
|
---|
| 297 |
|
---|
[2519349] | 298 | if (!ski_con_disabled()) {
|
---|
| 299 | for (i = 0; i < size; i++) {
|
---|
[392f0e7] | 300 | ski_con_putchar(con, dp[i]);
|
---|
[2519349] | 301 | }
|
---|
[392f0e7] | 302 | }
|
---|
[7a6065c] | 303 |
|
---|
| 304 | *nwr = size;
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /** Character device connection handler. */
|
---|
[984a9ba] | 309 | static void ski_con_connection(ipc_call_t *icall, void *arg)
|
---|
[7a6065c] | 310 | {
|
---|
| 311 | ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
|
---|
| 312 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
---|
| 313 |
|
---|
[984a9ba] | 314 | chardev_conn(icall, &con->cds);
|
---|
[6d15572] | 315 | }
|
---|
| 316 |
|
---|
| 317 | /** @}
|
---|
| 318 | */
|
---|