1 | /*
|
---|
2 | * Copyright (c) 2005 Jakub Jermar
|
---|
3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
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 |
|
---|
33 | #include <as.h>
|
---|
34 | #include <async.h>
|
---|
35 | #include <ddf/driver.h>
|
---|
36 | #include <ddf/log.h>
|
---|
37 | #include <ddi.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril.h>
|
---|
40 | #include <io/chardev.h>
|
---|
41 | #include <stdint.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <stdbool.h>
|
---|
44 | #include <sysinfo.h>
|
---|
45 |
|
---|
46 | #include "ski-con.h"
|
---|
47 |
|
---|
48 | #define SKI_GETCHAR 21
|
---|
49 | #define SKI_PUTCHAR 31
|
---|
50 |
|
---|
51 | #define POLL_INTERVAL 10000
|
---|
52 |
|
---|
53 | static errno_t ski_con_fibril(void *arg);
|
---|
54 | static int32_t ski_con_getchar(void);
|
---|
55 | static void ski_con_connection(ipc_call_t *, void *);
|
---|
56 |
|
---|
57 | static errno_t ski_con_read(chardev_srv_t *, void *, size_t, size_t *,
|
---|
58 | chardev_flags_t);
|
---|
59 | static errno_t ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
---|
60 |
|
---|
61 | static chardev_ops_t ski_con_chardev_ops = {
|
---|
62 | .read = ski_con_read,
|
---|
63 | .write = ski_con_write
|
---|
64 | };
|
---|
65 |
|
---|
66 | static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
|
---|
67 |
|
---|
68 | /** Add ski console device. */
|
---|
69 | errno_t ski_con_add(ski_con_t *con)
|
---|
70 | {
|
---|
71 | fid_t fid;
|
---|
72 | ddf_fun_t *fun = NULL;
|
---|
73 | bool bound = false;
|
---|
74 | uintptr_t faddr;
|
---|
75 | void *addr = AS_AREA_ANY;
|
---|
76 | errno_t rc;
|
---|
77 |
|
---|
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 |
|
---|
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 |
|
---|
91 | chardev_srvs_init(&con->cds);
|
---|
92 | con->cds.ops = &ski_con_chardev_ops;
|
---|
93 | con->cds.sarg = con;
|
---|
94 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
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;
|
---|
129 | }
|
---|
130 |
|
---|
131 | fibril_add_ready(fid);
|
---|
132 | return EOK;
|
---|
133 | error:
|
---|
134 | if (addr != AS_AREA_ANY)
|
---|
135 | as_area_destroy(addr);
|
---|
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 */
|
---|
145 | errno_t ski_con_remove(ski_con_t *con)
|
---|
146 | {
|
---|
147 | return ENOTSUP;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Ski console device gone */
|
---|
151 | errno_t ski_con_gone(ski_con_t *con)
|
---|
152 | {
|
---|
153 | return ENOTSUP;
|
---|
154 | }
|
---|
155 |
|
---|
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 |
|
---|
178 | /** Poll Ski for keypresses. */
|
---|
179 | static errno_t ski_con_fibril(void *arg)
|
---|
180 | {
|
---|
181 | int32_t c;
|
---|
182 | ski_con_t *con = (ski_con_t *) arg;
|
---|
183 | errno_t rc;
|
---|
184 |
|
---|
185 | while (true) {
|
---|
186 | while (!ski_con_disabled()) {
|
---|
187 | c = ski_con_getchar();
|
---|
188 | if (c == 0)
|
---|
189 | break;
|
---|
190 |
|
---|
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);
|
---|
199 | }
|
---|
200 |
|
---|
201 | fibril_usleep(POLL_INTERVAL);
|
---|
202 | }
|
---|
203 |
|
---|
204 | return 0;
|
---|
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 (
|
---|
220 | "mov r15 = %1\n"
|
---|
221 | "break 0x80000;;\n" /* modifies r8 */
|
---|
222 | "mov %0 = r8;;\n"
|
---|
223 |
|
---|
224 | : "=r" (ch)
|
---|
225 | : "i" (SKI_GETCHAR)
|
---|
226 | : "r15", "r8"
|
---|
227 | );
|
---|
228 | #else
|
---|
229 | ch = 0;
|
---|
230 | #endif
|
---|
231 | return (int32_t) ch;
|
---|
232 | }
|
---|
233 |
|
---|
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 | */
|
---|
242 | static void ski_con_putchar(ski_con_t *con, char ch)
|
---|
243 | {
|
---|
244 | if (ch == '\n')
|
---|
245 | ski_con_putchar(con, '\r');
|
---|
246 |
|
---|
247 | #ifdef UARCH_ia64
|
---|
248 | asm volatile (
|
---|
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"
|
---|
255 | );
|
---|
256 | #else
|
---|
257 | (void) ch;
|
---|
258 | #endif
|
---|
259 | }
|
---|
260 |
|
---|
261 | /** Read from Ski console device */
|
---|
262 | static errno_t ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
---|
263 | size_t *nread, chardev_flags_t flags)
|
---|
264 | {
|
---|
265 | ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
|
---|
266 | size_t p;
|
---|
267 | uint8_t *bp = (uint8_t *) buf;
|
---|
268 | errno_t rc;
|
---|
269 |
|
---|
270 | fibril_mutex_lock(&con->buf_lock);
|
---|
271 |
|
---|
272 | while ((flags & chardev_f_nonblock) == 0 &&
|
---|
273 | circ_buf_nused(&con->cbuf) == 0)
|
---|
274 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
---|
275 |
|
---|
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 | }
|
---|
283 |
|
---|
284 | fibril_mutex_unlock(&con->buf_lock);
|
---|
285 |
|
---|
286 | *nread = p;
|
---|
287 | return EOK;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Write to Ski console device */
|
---|
291 | static errno_t ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
---|
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 |
|
---|
298 | if (!ski_con_disabled()) {
|
---|
299 | for (i = 0; i < size; i++) {
|
---|
300 | ski_con_putchar(con, dp[i]);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | *nwr = size;
|
---|
305 | return EOK;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /** Character device connection handler. */
|
---|
309 | static void ski_con_connection(ipc_call_t *icall, void *arg)
|
---|
310 | {
|
---|
311 | ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
|
---|
312 | ddf_fun_get_dev((ddf_fun_t *) arg));
|
---|
313 |
|
---|
314 | chardev_conn(icall, &con->cds);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /** @}
|
---|
318 | */
|
---|