line.c 16 KB

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