chan_kern.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/tty.h>
  7. #include <linux/tty_flip.h>
  8. #include "chan.h"
  9. #include <os.h>
  10. #include <irq_kern.h>
  11. #ifdef CONFIG_NOCONFIG_CHAN
  12. static void *not_configged_init(char *str, int device,
  13. const struct chan_opts *opts)
  14. {
  15. printk(KERN_ERR "Using a channel type which is configured out of "
  16. "UML\n");
  17. return NULL;
  18. }
  19. static int not_configged_open(int input, int output, int primary, void *data,
  20. char **dev_out)
  21. {
  22. printk(KERN_ERR "Using a channel type which is configured out of "
  23. "UML\n");
  24. return -ENODEV;
  25. }
  26. static void not_configged_close(int fd, void *data)
  27. {
  28. printk(KERN_ERR "Using a channel type which is configured out of "
  29. "UML\n");
  30. }
  31. static int not_configged_read(int fd, u8 *c_out, void *data)
  32. {
  33. printk(KERN_ERR "Using a channel type which is configured out of "
  34. "UML\n");
  35. return -EIO;
  36. }
  37. static int not_configged_write(int fd, const u8 *buf, size_t len, void *data)
  38. {
  39. printk(KERN_ERR "Using a channel type which is configured out of "
  40. "UML\n");
  41. return -EIO;
  42. }
  43. static int not_configged_console_write(int fd, const char *buf, int len)
  44. {
  45. printk(KERN_ERR "Using a channel type which is configured out of "
  46. "UML\n");
  47. return -EIO;
  48. }
  49. static int not_configged_window_size(int fd, void *data, unsigned short *rows,
  50. unsigned short *cols)
  51. {
  52. printk(KERN_ERR "Using a channel type which is configured out of "
  53. "UML\n");
  54. return -ENODEV;
  55. }
  56. static void not_configged_free(void *data)
  57. {
  58. printk(KERN_ERR "Using a channel type which is configured out of "
  59. "UML\n");
  60. }
  61. static const struct chan_ops not_configged_ops = {
  62. .init = not_configged_init,
  63. .open = not_configged_open,
  64. .close = not_configged_close,
  65. .read = not_configged_read,
  66. .write = not_configged_write,
  67. .console_write = not_configged_console_write,
  68. .window_size = not_configged_window_size,
  69. .free = not_configged_free,
  70. .winch = 0,
  71. };
  72. #endif /* CONFIG_NOCONFIG_CHAN */
  73. static inline bool need_output_blocking(void)
  74. {
  75. return time_travel_mode == TT_MODE_INFCPU ||
  76. time_travel_mode == TT_MODE_EXTERNAL;
  77. }
  78. static int open_one_chan(struct chan *chan)
  79. {
  80. int fd, err;
  81. if (chan->opened)
  82. return 0;
  83. if (chan->ops->open == NULL)
  84. fd = 0;
  85. else fd = (*chan->ops->open)(chan->input, chan->output, chan->primary,
  86. chan->data, &chan->dev);
  87. if (fd < 0)
  88. return fd;
  89. err = os_set_fd_block(fd, 0);
  90. if (err)
  91. goto out_close;
  92. chan->fd_in = fd;
  93. chan->fd_out = fd;
  94. /*
  95. * In time-travel modes infinite-CPU and external we need to guarantee
  96. * that any writes to the output succeed immdiately from the point of
  97. * the VM. The best way to do this is to put the FD in blocking mode
  98. * and simply wait/retry until everything is written.
  99. * As every write is guaranteed to complete, we also do not need to
  100. * request an IRQ for the output.
  101. *
  102. * Note that input cannot happen in a time synchronized way. We permit
  103. * it, but time passes very quickly if anything waits for a read.
  104. */
  105. if (chan->output && need_output_blocking()) {
  106. err = os_dup_file(chan->fd_out);
  107. if (err < 0)
  108. goto out_close;
  109. chan->fd_out = err;
  110. err = os_set_fd_block(chan->fd_out, 1);
  111. if (err) {
  112. os_close_file(chan->fd_out);
  113. goto out_close;
  114. }
  115. }
  116. chan->opened = 1;
  117. return 0;
  118. out_close:
  119. (*chan->ops->close)(fd, chan->data);
  120. return err;
  121. }
  122. static int open_chan(struct list_head *chans)
  123. {
  124. struct list_head *ele;
  125. struct chan *chan;
  126. int ret, err = 0;
  127. list_for_each(ele, chans) {
  128. chan = list_entry(ele, struct chan, list);
  129. ret = open_one_chan(chan);
  130. if (chan->primary)
  131. err = ret;
  132. }
  133. return err;
  134. }
  135. void chan_enable_winch(struct chan *chan, struct tty_port *port)
  136. {
  137. if (chan && chan->primary && chan->ops->winch)
  138. register_winch(chan->fd_in, port);
  139. }
  140. static void line_timer_cb(struct work_struct *work)
  141. {
  142. struct line *line = container_of(work, struct line, task.work);
  143. if (!line->throttled)
  144. chan_interrupt(line, line->read_irq);
  145. }
  146. int enable_chan(struct line *line)
  147. {
  148. struct list_head *ele;
  149. struct chan *chan;
  150. int err;
  151. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  152. list_for_each(ele, &line->chan_list) {
  153. chan = list_entry(ele, struct chan, list);
  154. err = open_one_chan(chan);
  155. if (err) {
  156. if (chan->primary)
  157. goto out_close;
  158. continue;
  159. }
  160. if (chan->enabled)
  161. continue;
  162. err = line_setup_irq(chan->fd_in, chan->input,
  163. chan->output && !need_output_blocking(),
  164. line, chan);
  165. if (err)
  166. goto out_close;
  167. chan->enabled = 1;
  168. }
  169. return 0;
  170. out_close:
  171. close_chan(line);
  172. return err;
  173. }
  174. /* Items are added in IRQ context, when free_irq can't be called, and
  175. * removed in process context, when it can.
  176. * This handles interrupt sources which disappear, and which need to
  177. * be permanently disabled. This is discovered in IRQ context, but
  178. * the freeing of the IRQ must be done later.
  179. */
  180. static DEFINE_RAW_SPINLOCK(irqs_to_free_lock);
  181. static LIST_HEAD(irqs_to_free);
  182. void free_irqs(void)
  183. {
  184. struct chan *chan;
  185. LIST_HEAD(list);
  186. struct list_head *ele;
  187. unsigned long flags;
  188. raw_spin_lock_irqsave(&irqs_to_free_lock, flags);
  189. list_splice_init(&irqs_to_free, &list);
  190. raw_spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  191. list_for_each(ele, &list) {
  192. chan = list_entry(ele, struct chan, free_list);
  193. if (chan->input && chan->enabled)
  194. um_free_irq(chan->line->read_irq, chan);
  195. if (chan->output && chan->enabled &&
  196. !need_output_blocking())
  197. um_free_irq(chan->line->write_irq, chan);
  198. chan->enabled = 0;
  199. }
  200. }
  201. static void close_one_chan(struct chan *chan, int delay_free_irq)
  202. {
  203. unsigned long flags;
  204. if (!chan->opened)
  205. return;
  206. if (delay_free_irq) {
  207. raw_spin_lock_irqsave(&irqs_to_free_lock, flags);
  208. list_add(&chan->free_list, &irqs_to_free);
  209. raw_spin_unlock_irqrestore(&irqs_to_free_lock, flags);
  210. } else {
  211. if (chan->input && chan->enabled)
  212. um_free_irq(chan->line->read_irq, chan);
  213. if (chan->output && chan->enabled &&
  214. !need_output_blocking())
  215. um_free_irq(chan->line->write_irq, chan);
  216. chan->enabled = 0;
  217. }
  218. if (chan->fd_out != chan->fd_in)
  219. os_close_file(chan->fd_out);
  220. if (chan->ops->close != NULL)
  221. (*chan->ops->close)(chan->fd_in, chan->data);
  222. chan->opened = 0;
  223. chan->fd_in = -1;
  224. chan->fd_out = -1;
  225. }
  226. void close_chan(struct line *line)
  227. {
  228. struct chan *chan;
  229. /* Close in reverse order as open in case more than one of them
  230. * refers to the same device and they save and restore that device's
  231. * state. Then, the first one opened will have the original state,
  232. * so it must be the last closed.
  233. */
  234. list_for_each_entry_reverse(chan, &line->chan_list, list) {
  235. close_one_chan(chan, 0);
  236. }
  237. }
  238. void deactivate_chan(struct chan *chan, int irq)
  239. {
  240. if (chan && chan->enabled)
  241. deactivate_fd(chan->fd_in, irq);
  242. }
  243. int write_chan(struct chan *chan, const u8 *buf, size_t len, int write_irq)
  244. {
  245. int n, ret = 0;
  246. if (len == 0 || !chan || !chan->ops->write)
  247. return 0;
  248. n = chan->ops->write(chan->fd_out, buf, len, chan->data);
  249. if (chan->primary) {
  250. ret = n;
  251. }
  252. return ret;
  253. }
  254. int console_write_chan(struct chan *chan, const char *buf, int len)
  255. {
  256. int n, ret = 0;
  257. if (!chan || !chan->ops->console_write)
  258. return 0;
  259. n = chan->ops->console_write(chan->fd_out, buf, len);
  260. if (chan->primary)
  261. ret = n;
  262. return ret;
  263. }
  264. int console_open_chan(struct line *line, struct console *co)
  265. {
  266. int err;
  267. err = open_chan(&line->chan_list);
  268. if (err)
  269. return err;
  270. printk(KERN_INFO "Console initialized on /dev/%s%d\n", co->name,
  271. co->index);
  272. return 0;
  273. }
  274. int chan_window_size(struct line *line, unsigned short *rows_out,
  275. unsigned short *cols_out)
  276. {
  277. struct chan *chan;
  278. chan = line->chan_in;
  279. if (chan && chan->primary) {
  280. if (chan->ops->window_size == NULL)
  281. return 0;
  282. return chan->ops->window_size(chan->fd_in, chan->data,
  283. rows_out, cols_out);
  284. }
  285. chan = line->chan_out;
  286. if (chan && chan->primary) {
  287. if (chan->ops->window_size == NULL)
  288. return 0;
  289. return chan->ops->window_size(chan->fd_in, chan->data,
  290. rows_out, cols_out);
  291. }
  292. return 0;
  293. }
  294. static void free_one_chan(struct chan *chan)
  295. {
  296. list_del(&chan->list);
  297. close_one_chan(chan, 0);
  298. if (chan->ops->free != NULL)
  299. (*chan->ops->free)(chan->data);
  300. if (chan->primary && chan->output)
  301. ignore_sigio_fd(chan->fd_in);
  302. kfree(chan);
  303. }
  304. static void free_chan(struct list_head *chans)
  305. {
  306. struct list_head *ele, *next;
  307. struct chan *chan;
  308. list_for_each_safe(ele, next, chans) {
  309. chan = list_entry(ele, struct chan, list);
  310. free_one_chan(chan);
  311. }
  312. }
  313. static int one_chan_config_string(struct chan *chan, char *str, int size,
  314. char **error_out)
  315. {
  316. int n = 0;
  317. if (chan == NULL) {
  318. CONFIG_CHUNK(str, size, n, "none", 1);
  319. return n;
  320. }
  321. CONFIG_CHUNK(str, size, n, chan->ops->type, 0);
  322. if (chan->dev == NULL) {
  323. CONFIG_CHUNK(str, size, n, "", 1);
  324. return n;
  325. }
  326. CONFIG_CHUNK(str, size, n, ":", 0);
  327. CONFIG_CHUNK(str, size, n, chan->dev, 0);
  328. return n;
  329. }
  330. static int chan_pair_config_string(struct chan *in, struct chan *out,
  331. char *str, int size, char **error_out)
  332. {
  333. int n;
  334. n = one_chan_config_string(in, str, size, error_out);
  335. str += n;
  336. size -= n;
  337. if (in == out) {
  338. CONFIG_CHUNK(str, size, n, "", 1);
  339. return n;
  340. }
  341. CONFIG_CHUNK(str, size, n, ",", 1);
  342. n = one_chan_config_string(out, str, size, error_out);
  343. str += n;
  344. size -= n;
  345. CONFIG_CHUNK(str, size, n, "", 1);
  346. return n;
  347. }
  348. int chan_config_string(struct line *line, char *str, int size,
  349. char **error_out)
  350. {
  351. struct chan *in = line->chan_in, *out = line->chan_out;
  352. if (in && !in->primary)
  353. in = NULL;
  354. if (out && !out->primary)
  355. out = NULL;
  356. return chan_pair_config_string(in, out, str, size, error_out);
  357. }
  358. struct chan_type {
  359. char *key;
  360. const struct chan_ops *ops;
  361. };
  362. static const struct chan_type chan_table[] = {
  363. { "fd", &fd_ops },
  364. #ifdef CONFIG_NULL_CHAN
  365. { "null", &null_ops },
  366. #else
  367. { "null", &not_configged_ops },
  368. #endif
  369. #ifdef CONFIG_PORT_CHAN
  370. { "port", &port_ops },
  371. #else
  372. { "port", &not_configged_ops },
  373. #endif
  374. #ifdef CONFIG_PTY_CHAN
  375. { "pty", &pty_ops },
  376. { "pts", &pts_ops },
  377. #else
  378. { "pty", &not_configged_ops },
  379. { "pts", &not_configged_ops },
  380. #endif
  381. #ifdef CONFIG_TTY_CHAN
  382. { "tty", &tty_ops },
  383. #else
  384. { "tty", &not_configged_ops },
  385. #endif
  386. #ifdef CONFIG_XTERM_CHAN
  387. { "xterm", &xterm_ops },
  388. #else
  389. { "xterm", &not_configged_ops },
  390. #endif
  391. };
  392. static struct chan *parse_chan(struct line *line, char *str, int device,
  393. const struct chan_opts *opts, char **error_out)
  394. {
  395. const struct chan_type *entry;
  396. const struct chan_ops *ops;
  397. struct chan *chan;
  398. void *data;
  399. int i;
  400. ops = NULL;
  401. data = NULL;
  402. for(i = 0; i < ARRAY_SIZE(chan_table); i++) {
  403. entry = &chan_table[i];
  404. if (!strncmp(str, entry->key, strlen(entry->key))) {
  405. ops = entry->ops;
  406. str += strlen(entry->key);
  407. break;
  408. }
  409. }
  410. if (ops == NULL) {
  411. *error_out = "No match for configured backends";
  412. return NULL;
  413. }
  414. data = (*ops->init)(str, device, opts);
  415. if (data == NULL) {
  416. *error_out = "Configuration failed";
  417. return NULL;
  418. }
  419. chan = kmalloc_obj(*chan, GFP_ATOMIC);
  420. if (chan == NULL) {
  421. *error_out = "Memory allocation failed";
  422. return NULL;
  423. }
  424. *chan = ((struct chan) { .list = LIST_HEAD_INIT(chan->list),
  425. .free_list =
  426. LIST_HEAD_INIT(chan->free_list),
  427. .line = line,
  428. .primary = 1,
  429. .input = 0,
  430. .output = 0,
  431. .opened = 0,
  432. .enabled = 0,
  433. .fd_in = -1,
  434. .fd_out = -1,
  435. .ops = ops,
  436. .data = data });
  437. return chan;
  438. }
  439. int parse_chan_pair(char *str, struct line *line, int device,
  440. const struct chan_opts *opts, char **error_out)
  441. {
  442. struct list_head *chans = &line->chan_list;
  443. struct chan *new;
  444. char *in, *out;
  445. if (!list_empty(chans)) {
  446. line->chan_in = line->chan_out = NULL;
  447. free_chan(chans);
  448. INIT_LIST_HEAD(chans);
  449. }
  450. if (!str)
  451. return 0;
  452. out = strchr(str, ',');
  453. if (out != NULL) {
  454. in = str;
  455. *out = '\0';
  456. out++;
  457. new = parse_chan(line, in, device, opts, error_out);
  458. if (new == NULL)
  459. return -1;
  460. new->input = 1;
  461. list_add(&new->list, chans);
  462. line->chan_in = new;
  463. new = parse_chan(line, out, device, opts, error_out);
  464. if (new == NULL)
  465. return -1;
  466. list_add(&new->list, chans);
  467. new->output = 1;
  468. line->chan_out = new;
  469. }
  470. else {
  471. new = parse_chan(line, str, device, opts, error_out);
  472. if (new == NULL)
  473. return -1;
  474. list_add(&new->list, chans);
  475. new->input = 1;
  476. new->output = 1;
  477. line->chan_in = line->chan_out = new;
  478. }
  479. return 0;
  480. }
  481. void chan_interrupt(struct line *line, int irq)
  482. {
  483. struct tty_port *port = &line->port;
  484. struct chan *chan = line->chan_in;
  485. int err;
  486. u8 c;
  487. if (!chan || !chan->ops->read)
  488. goto out;
  489. do {
  490. if (!tty_buffer_request_room(port, 1)) {
  491. schedule_delayed_work(&line->task, 1);
  492. goto out;
  493. }
  494. err = chan->ops->read(chan->fd_in, &c, chan->data);
  495. if (err > 0)
  496. tty_insert_flip_char(port, c, TTY_NORMAL);
  497. } while (err > 0);
  498. if (err == -EIO) {
  499. if (chan->primary) {
  500. tty_port_tty_hangup(&line->port, false);
  501. if (line->chan_out != chan)
  502. close_one_chan(line->chan_out, 1);
  503. }
  504. close_one_chan(chan, 1);
  505. if (chan->primary)
  506. return;
  507. }
  508. out:
  509. tty_flip_buffer_push(port);
  510. }