1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 */
5
6 #include <linux/irqreturn.h>
7 #include <linux/kd.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10
11 #include "chan.h"
12 #include <irq_kern.h>
13 #include <irq_user.h>
14 #include <kern_util.h>
15 #include <os.h>
16
17 #define LINE_BUFSIZE 4096
18
line_interrupt(int irq,void * data)19 static irqreturn_t line_interrupt(int irq, void *data)
20 {
21 struct chan *chan = data;
22 struct line *line = chan->line;
23
24 if (line)
25 chan_interrupt(line, irq);
26
27 return IRQ_HANDLED;
28 }
29
30 /*
31 * Returns the free space inside the ring buffer of this line.
32 *
33 * Should be called while holding line->lock (this does not modify data).
34 */
write_room(struct line * line)35 static int write_room(struct line *line)
36 {
37 int n;
38
39 if (line->buffer == NULL)
40 return LINE_BUFSIZE - 1;
41
42 /* This is for the case where the buffer is wrapped! */
43 n = line->head - line->tail;
44
45 if (n <= 0)
46 n += LINE_BUFSIZE; /* The other case */
47 return n - 1;
48 }
49
line_write_room(struct tty_struct * tty)50 int line_write_room(struct tty_struct *tty)
51 {
52 struct line *line = tty->driver_data;
53 unsigned long flags;
54 int room;
55
56 spin_lock_irqsave(&line->lock, flags);
57 room = write_room(line);
58 spin_unlock_irqrestore(&line->lock, flags);
59
60 return room;
61 }
62
line_chars_in_buffer(struct tty_struct * tty)63 int line_chars_in_buffer(struct tty_struct *tty)
64 {
65 struct line *line = tty->driver_data;
66 unsigned long flags;
67 int ret;
68
69 spin_lock_irqsave(&line->lock, flags);
70 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
71 ret = LINE_BUFSIZE - (write_room(line) + 1);
72 spin_unlock_irqrestore(&line->lock, flags);
73
74 return ret;
75 }
76
77 /*
78 * This copies the content of buf into the circular buffer associated with
79 * this line.
80 * The return value is the number of characters actually copied, i.e. the ones
81 * for which there was space: this function is not supposed to ever flush out
82 * the circular buffer.
83 *
84 * Must be called while holding line->lock!
85 */
buffer_data(struct line * line,const char * buf,int len)86 static int buffer_data(struct line *line, const char *buf, int len)
87 {
88 int end, room;
89
90 if (line->buffer == NULL) {
91 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
92 if (line->buffer == NULL) {
93 printk(KERN_ERR "buffer_data - atomic allocation "
94 "failed\n");
95 return 0;
96 }
97 line->head = line->buffer;
98 line->tail = line->buffer;
99 }
100
101 room = write_room(line);
102 len = (len > room) ? room : len;
103
104 end = line->buffer + LINE_BUFSIZE - line->tail;
105
106 if (len < end) {
107 memcpy(line->tail, buf, len);
108 line->tail += len;
109 }
110 else {
111 /* The circular buffer is wrapping */
112 memcpy(line->tail, buf, end);
113 buf += end;
114 memcpy(line->buffer, buf, len - end);
115 line->tail = line->buffer + len - end;
116 }
117
118 return len;
119 }
120
121 /*
122 * Flushes the ring buffer to the output channels. That is, write_chan is
123 * called, passing it line->head as buffer, and an appropriate count.
124 *
125 * On exit, returns 1 when the buffer is empty,
126 * 0 when the buffer is not empty on exit,
127 * and -errno when an error occurred.
128 *
129 * Must be called while holding line->lock!*/
flush_buffer(struct line * line)130 static int flush_buffer(struct line *line)
131 {
132 int n, count;
133
134 if ((line->buffer == NULL) || (line->head == line->tail))
135 return 1;
136
137 if (line->tail < line->head) {
138 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
139 count = line->buffer + LINE_BUFSIZE - line->head;
140
141 n = write_chan(line->chan_out, line->head, count,
142 line->driver->write_irq);
143 if (n < 0)
144 return n;
145 if (n == count) {
146 /*
147 * We have flushed from ->head to buffer end, now we
148 * must flush only from the beginning to ->tail.
149 */
150 line->head = line->buffer;
151 } else {
152 line->head += n;
153 return 0;
154 }
155 }
156
157 count = line->tail - line->head;
158 n = write_chan(line->chan_out, line->head, count,
159 line->driver->write_irq);
160
161 if (n < 0)
162 return n;
163
164 line->head += n;
165 return line->head == line->tail;
166 }
167
line_flush_buffer(struct tty_struct * tty)168 void line_flush_buffer(struct tty_struct *tty)
169 {
170 struct line *line = tty->driver_data;
171 unsigned long flags;
172
173 spin_lock_irqsave(&line->lock, flags);
174 flush_buffer(line);
175 spin_unlock_irqrestore(&line->lock, flags);
176 }
177
178 /*
179 * We map both ->flush_chars and ->put_char (which go in pair) onto
180 * ->flush_buffer and ->write. Hope it's not that bad.
181 */
line_flush_chars(struct tty_struct * tty)182 void line_flush_chars(struct tty_struct *tty)
183 {
184 line_flush_buffer(tty);
185 }
186
line_write(struct tty_struct * tty,const unsigned char * buf,int len)187 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
188 {
189 struct line *line = tty->driver_data;
190 unsigned long flags;
191 int n, ret = 0;
192
193 spin_lock_irqsave(&line->lock, flags);
194 if (line->head != line->tail)
195 ret = buffer_data(line, buf, len);
196 else {
197 n = write_chan(line->chan_out, buf, len,
198 line->driver->write_irq);
199 if (n < 0) {
200 ret = n;
201 goto out_up;
202 }
203
204 len -= n;
205 ret += n;
206 if (len > 0)
207 ret += buffer_data(line, buf + n, len);
208 }
209 out_up:
210 spin_unlock_irqrestore(&line->lock, flags);
211 return ret;
212 }
213
line_set_termios(struct tty_struct * tty,struct ktermios * old)214 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
215 {
216 /* nothing */
217 }
218
line_throttle(struct tty_struct * tty)219 void line_throttle(struct tty_struct *tty)
220 {
221 struct line *line = tty->driver_data;
222
223 deactivate_chan(line->chan_in, line->driver->read_irq);
224 line->throttled = 1;
225 }
226
line_unthrottle(struct tty_struct * tty)227 void line_unthrottle(struct tty_struct *tty)
228 {
229 struct line *line = tty->driver_data;
230
231 line->throttled = 0;
232 chan_interrupt(line, line->driver->read_irq);
233 }
234
line_write_interrupt(int irq,void * data)235 static irqreturn_t line_write_interrupt(int irq, void *data)
236 {
237 struct chan *chan = data;
238 struct line *line = chan->line;
239 int err;
240
241 /*
242 * Interrupts are disabled here because genirq keep irqs disabled when
243 * calling the action handler.
244 */
245
246 spin_lock(&line->lock);
247 err = flush_buffer(line);
248 if (err == 0) {
249 spin_unlock(&line->lock);
250 return IRQ_NONE;
251 } else if ((err < 0) && (err != -EAGAIN)) {
252 line->head = line->buffer;
253 line->tail = line->buffer;
254 }
255 spin_unlock(&line->lock);
256
257 tty_port_tty_wakeup(&line->port);
258
259 return IRQ_HANDLED;
260 }
261
line_setup_irq(int fd,int input,int output,struct line * line,void * data)262 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
263 {
264 const struct line_driver *driver = line->driver;
265 int err;
266
267 if (input) {
268 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
269 line_interrupt, IRQF_SHARED,
270 driver->read_irq_name, data);
271 if (err < 0)
272 return err;
273 }
274
275 if (output) {
276 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
277 line_write_interrupt, IRQF_SHARED,
278 driver->write_irq_name, data);
279 if (err < 0)
280 return err;
281 }
282
283 return 0;
284 }
285
line_activate(struct tty_port * port,struct tty_struct * tty)286 static int line_activate(struct tty_port *port, struct tty_struct *tty)
287 {
288 int ret;
289 struct line *line = tty->driver_data;
290
291 ret = enable_chan(line);
292 if (ret)
293 return ret;
294
295 if (!line->sigio) {
296 chan_enable_winch(line->chan_out, port);
297 line->sigio = 1;
298 }
299
300 chan_window_size(line, &tty->winsize.ws_row,
301 &tty->winsize.ws_col);
302
303 return 0;
304 }
305
306 static void unregister_winch(struct tty_struct *tty);
307
line_destruct(struct tty_port * port)308 static void line_destruct(struct tty_port *port)
309 {
310 struct tty_struct *tty = tty_port_tty_get(port);
311 struct line *line = tty->driver_data;
312
313 if (line->sigio) {
314 unregister_winch(tty);
315 line->sigio = 0;
316 }
317 }
318
319 static const struct tty_port_operations line_port_ops = {
320 .activate = line_activate,
321 .destruct = line_destruct,
322 };
323
line_open(struct tty_struct * tty,struct file * filp)324 int line_open(struct tty_struct *tty, struct file *filp)
325 {
326 struct line *line = tty->driver_data;
327
328 return tty_port_open(&line->port, tty, filp);
329 }
330
line_install(struct tty_driver * driver,struct tty_struct * tty,struct line * line)331 int line_install(struct tty_driver *driver, struct tty_struct *tty,
332 struct line *line)
333 {
334 int ret;
335
336 ret = tty_standard_install(driver, tty);
337 if (ret)
338 return ret;
339
340 tty->driver_data = line;
341
342 return 0;
343 }
344
line_close(struct tty_struct * tty,struct file * filp)345 void line_close(struct tty_struct *tty, struct file * filp)
346 {
347 struct line *line = tty->driver_data;
348
349 tty_port_close(&line->port, tty, filp);
350 }
351
line_hangup(struct tty_struct * tty)352 void line_hangup(struct tty_struct *tty)
353 {
354 struct line *line = tty->driver_data;
355
356 tty_port_hangup(&line->port);
357 }
358
close_lines(struct line * lines,int nlines)359 void close_lines(struct line *lines, int nlines)
360 {
361 int i;
362
363 for(i = 0; i < nlines; i++)
364 close_chan(&lines[i]);
365 }
366
setup_one_line(struct line * lines,int n,char * init,const struct chan_opts * opts,char ** error_out)367 int setup_one_line(struct line *lines, int n, char *init,
368 const struct chan_opts *opts, char **error_out)
369 {
370 struct line *line = &lines[n];
371 struct tty_driver *driver = line->driver->driver;
372 int err = -EINVAL;
373
374 if (line->port.count) {
375 *error_out = "Device is already open";
376 goto out;
377 }
378
379 if (!strcmp(init, "none")) {
380 if (line->valid) {
381 line->valid = 0;
382 kfree(line->init_str);
383 tty_unregister_device(driver, n);
384 parse_chan_pair(NULL, line, n, opts, error_out);
385 err = 0;
386 }
387 } else {
388 char *new = kstrdup(init, GFP_KERNEL);
389 if (!new) {
390 *error_out = "Failed to allocate memory";
391 return -ENOMEM;
392 }
393 if (line->valid) {
394 tty_unregister_device(driver, n);
395 kfree(line->init_str);
396 }
397 line->init_str = new;
398 line->valid = 1;
399 err = parse_chan_pair(new, line, n, opts, error_out);
400 if (!err) {
401 struct device *d = tty_port_register_device(&line->port,
402 driver, n, NULL);
403 if (IS_ERR(d)) {
404 *error_out = "Failed to register device";
405 err = PTR_ERR(d);
406 parse_chan_pair(NULL, line, n, opts, error_out);
407 }
408 }
409 if (err) {
410 line->init_str = NULL;
411 line->valid = 0;
412 kfree(new);
413 }
414 }
415 out:
416 return err;
417 }
418
419 /*
420 * Common setup code for both startup command line and mconsole initialization.
421 * @lines contains the array (of size @num) to modify;
422 * @init is the setup string;
423 * @error_out is an error string in the case of failure;
424 */
425
line_setup(char ** conf,unsigned int num,char ** def,char * init,char * name)426 int line_setup(char **conf, unsigned int num, char **def,
427 char *init, char *name)
428 {
429 char *error;
430
431 if (*init == '=') {
432 /*
433 * We said con=/ssl= instead of con#=, so we are configuring all
434 * consoles at once.
435 */
436 *def = init + 1;
437 } else {
438 char *end;
439 unsigned n = simple_strtoul(init, &end, 0);
440
441 if (*end != '=') {
442 error = "Couldn't parse device number";
443 goto out;
444 }
445 if (n >= num) {
446 error = "Device number out of range";
447 goto out;
448 }
449 conf[n] = end + 1;
450 }
451 return 0;
452
453 out:
454 printk(KERN_ERR "Failed to set up %s with "
455 "configuration string \"%s\" : %s\n", name, init, error);
456 return -EINVAL;
457 }
458
line_config(struct line * lines,unsigned int num,char * str,const struct chan_opts * opts,char ** error_out)459 int line_config(struct line *lines, unsigned int num, char *str,
460 const struct chan_opts *opts, char **error_out)
461 {
462 char *end;
463 int n;
464
465 if (*str == '=') {
466 *error_out = "Can't configure all devices from mconsole";
467 return -EINVAL;
468 }
469
470 n = simple_strtoul(str, &end, 0);
471 if (*end++ != '=') {
472 *error_out = "Couldn't parse device number";
473 return -EINVAL;
474 }
475 if (n >= num) {
476 *error_out = "Device number out of range";
477 return -EINVAL;
478 }
479
480 return setup_one_line(lines, n, end, opts, error_out);
481 }
482
line_get_config(char * name,struct line * lines,unsigned int num,char * str,int size,char ** error_out)483 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
484 int size, char **error_out)
485 {
486 struct line *line;
487 char *end;
488 int dev, n = 0;
489
490 dev = simple_strtoul(name, &end, 0);
491 if ((*end != '\0') || (end == name)) {
492 *error_out = "line_get_config failed to parse device number";
493 return 0;
494 }
495
496 if ((dev < 0) || (dev >= num)) {
497 *error_out = "device number out of range";
498 return 0;
499 }
500
501 line = &lines[dev];
502
503 if (!line->valid)
504 CONFIG_CHUNK(str, size, n, "none", 1);
505 else {
506 struct tty_struct *tty = tty_port_tty_get(&line->port);
507 if (tty == NULL) {
508 CONFIG_CHUNK(str, size, n, line->init_str, 1);
509 } else {
510 n = chan_config_string(line, str, size, error_out);
511 tty_kref_put(tty);
512 }
513 }
514
515 return n;
516 }
517
line_id(char ** str,int * start_out,int * end_out)518 int line_id(char **str, int *start_out, int *end_out)
519 {
520 char *end;
521 int n;
522
523 n = simple_strtoul(*str, &end, 0);
524 if ((*end != '\0') || (end == *str))
525 return -1;
526
527 *str = end;
528 *start_out = n;
529 *end_out = n;
530 return n;
531 }
532
line_remove(struct line * lines,unsigned int num,int n,char ** error_out)533 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
534 {
535 if (n >= num) {
536 *error_out = "Device number out of range";
537 return -EINVAL;
538 }
539 return setup_one_line(lines, n, "none", NULL, error_out);
540 }
541
register_lines(struct line_driver * line_driver,const struct tty_operations * ops,struct line * lines,int nlines)542 int register_lines(struct line_driver *line_driver,
543 const struct tty_operations *ops,
544 struct line *lines, int nlines)
545 {
546 struct tty_driver *driver = alloc_tty_driver(nlines);
547 int err;
548 int i;
549
550 if (!driver)
551 return -ENOMEM;
552
553 driver->driver_name = line_driver->name;
554 driver->name = line_driver->device_name;
555 driver->major = line_driver->major;
556 driver->minor_start = line_driver->minor_start;
557 driver->type = line_driver->type;
558 driver->subtype = line_driver->subtype;
559 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
560 driver->init_termios = tty_std_termios;
561
562 for (i = 0; i < nlines; i++) {
563 tty_port_init(&lines[i].port);
564 lines[i].port.ops = &line_port_ops;
565 spin_lock_init(&lines[i].lock);
566 lines[i].driver = line_driver;
567 INIT_LIST_HEAD(&lines[i].chan_list);
568 }
569 tty_set_operations(driver, ops);
570
571 err = tty_register_driver(driver);
572 if (err) {
573 printk(KERN_ERR "register_lines : can't register %s driver\n",
574 line_driver->name);
575 put_tty_driver(driver);
576 for (i = 0; i < nlines; i++)
577 tty_port_destroy(&lines[i].port);
578 return err;
579 }
580
581 line_driver->driver = driver;
582 mconsole_register_dev(&line_driver->mc);
583 return 0;
584 }
585
586 static DEFINE_SPINLOCK(winch_handler_lock);
587 static LIST_HEAD(winch_handlers);
588
589 struct winch {
590 struct list_head list;
591 int fd;
592 int tty_fd;
593 int pid;
594 struct tty_port *port;
595 unsigned long stack;
596 struct work_struct work;
597 };
598
__free_winch(struct work_struct * work)599 static void __free_winch(struct work_struct *work)
600 {
601 struct winch *winch = container_of(work, struct winch, work);
602 um_free_irq(WINCH_IRQ, winch);
603
604 if (winch->pid != -1)
605 os_kill_process(winch->pid, 1);
606 if (winch->stack != 0)
607 free_stack(winch->stack, 0);
608 kfree(winch);
609 }
610
free_winch(struct winch * winch)611 static void free_winch(struct winch *winch)
612 {
613 int fd = winch->fd;
614 winch->fd = -1;
615 if (fd != -1)
616 os_close_file(fd);
617 __free_winch(&winch->work);
618 }
619
winch_interrupt(int irq,void * data)620 static irqreturn_t winch_interrupt(int irq, void *data)
621 {
622 struct winch *winch = data;
623 struct tty_struct *tty;
624 struct line *line;
625 int fd = winch->fd;
626 int err;
627 char c;
628 struct pid *pgrp;
629
630 if (fd != -1) {
631 err = generic_read(fd, &c, NULL);
632 if (err < 0) {
633 if (err != -EAGAIN) {
634 winch->fd = -1;
635 list_del(&winch->list);
636 os_close_file(fd);
637 printk(KERN_ERR "winch_interrupt : "
638 "read failed, errno = %d\n", -err);
639 printk(KERN_ERR "fd %d is losing SIGWINCH "
640 "support\n", winch->tty_fd);
641 INIT_WORK(&winch->work, __free_winch);
642 schedule_work(&winch->work);
643 return IRQ_HANDLED;
644 }
645 goto out;
646 }
647 }
648 tty = tty_port_tty_get(winch->port);
649 if (tty != NULL) {
650 line = tty->driver_data;
651 if (line != NULL) {
652 chan_window_size(line, &tty->winsize.ws_row,
653 &tty->winsize.ws_col);
654 pgrp = tty_get_pgrp(tty);
655 if (pgrp)
656 kill_pgrp(pgrp, SIGWINCH, 1);
657 put_pid(pgrp);
658 }
659 tty_kref_put(tty);
660 }
661 out:
662 return IRQ_HANDLED;
663 }
664
register_winch_irq(int fd,int tty_fd,int pid,struct tty_port * port,unsigned long stack)665 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port,
666 unsigned long stack)
667 {
668 struct winch *winch;
669
670 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
671 if (winch == NULL) {
672 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
673 goto cleanup;
674 }
675
676 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
677 .fd = fd,
678 .tty_fd = tty_fd,
679 .pid = pid,
680 .port = port,
681 .stack = stack });
682
683 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
684 IRQF_SHARED, "winch", winch) < 0) {
685 printk(KERN_ERR "register_winch_irq - failed to register "
686 "IRQ\n");
687 goto out_free;
688 }
689
690 spin_lock(&winch_handler_lock);
691 list_add(&winch->list, &winch_handlers);
692 spin_unlock(&winch_handler_lock);
693
694 return;
695
696 out_free:
697 kfree(winch);
698 cleanup:
699 os_kill_process(pid, 1);
700 os_close_file(fd);
701 if (stack != 0)
702 free_stack(stack, 0);
703 }
704
unregister_winch(struct tty_struct * tty)705 static void unregister_winch(struct tty_struct *tty)
706 {
707 struct list_head *ele, *next;
708 struct winch *winch;
709 struct tty_struct *wtty;
710
711 spin_lock(&winch_handler_lock);
712
713 list_for_each_safe(ele, next, &winch_handlers) {
714 winch = list_entry(ele, struct winch, list);
715 wtty = tty_port_tty_get(winch->port);
716 if (wtty == tty) {
717 list_del(&winch->list);
718 spin_unlock(&winch_handler_lock);
719 free_winch(winch);
720 break;
721 }
722 tty_kref_put(wtty);
723 }
724 spin_unlock(&winch_handler_lock);
725 }
726
winch_cleanup(void)727 static void winch_cleanup(void)
728 {
729 struct winch *winch;
730
731 spin_lock(&winch_handler_lock);
732 while ((winch = list_first_entry_or_null(&winch_handlers,
733 struct winch, list))) {
734 list_del(&winch->list);
735 spin_unlock(&winch_handler_lock);
736
737 free_winch(winch);
738
739 spin_lock(&winch_handler_lock);
740 }
741
742 spin_unlock(&winch_handler_lock);
743 }
744 __uml_exitcall(winch_cleanup);
745
add_xterm_umid(char * base)746 char *add_xterm_umid(char *base)
747 {
748 char *umid, *title;
749 int len;
750
751 umid = get_umid();
752 if (*umid == '\0')
753 return base;
754
755 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
756 title = kmalloc(len, GFP_KERNEL);
757 if (title == NULL) {
758 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
759 return base;
760 }
761
762 snprintf(title, len, "%s (%s)", base, umid);
763 return title;
764 }
765