tty.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* TTY output rings. Commands write bytes; the terminal process drains and
  2. * renders them. A full ring blocks the writer (a terminal that is not
  3. * scheduled will drain shortly), so `cat`/helpers cannot silently lose output. */
  4. #include "tty.h"
  5. #include "mm.h"
  6. #include "netlog.h"
  7. #include "process.h"
  8. #define TTY_STRIDE (2 * PAGE_SIZE)
  9. struct tty {
  10. int used;
  11. unsigned int owner;
  12. unsigned char buf[TTY_BUF];
  13. unsigned int head, tail;
  14. };
  15. _Static_assert(sizeof(struct tty) <= TTY_STRIDE,
  16. "tty state no longer fits its two-page slot");
  17. /* The rings are 16 KB of state; keep them out of BSS so the kernel image
  18. * stays below the run area. */
  19. static struct tty *ttys;
  20. static int tty_ready;
  21. static int tty_init(void)
  22. {
  23. unsigned int base, i;
  24. unsigned char *p;
  25. if (tty_ready)
  26. return 1;
  27. base = mm_alloc_pages(TTY_MAX * (TTY_STRIDE / PAGE_SIZE));
  28. if (!base)
  29. return 0;
  30. p = (unsigned char *)base;
  31. for (i = 0; i < TTY_MAX * TTY_STRIDE; i++)
  32. p[i] = 0;
  33. ttys = (struct tty *)base;
  34. tty_ready = 1;
  35. return 1;
  36. }
  37. static struct tty *tty_slot(unsigned int i)
  38. {
  39. return (struct tty *)((unsigned char *)ttys + i * TTY_STRIDE);
  40. }
  41. static struct tty *tty_get(int id)
  42. {
  43. if (!ttys || id <= 0 || id > TTY_MAX)
  44. return 0;
  45. return tty_slot((unsigned int)(id - 1));
  46. }
  47. int tty_valid(int id)
  48. {
  49. struct tty *t = tty_get(id);
  50. return t && t->used;
  51. }
  52. int tty_create(void)
  53. {
  54. struct process *p = current_proc();
  55. unsigned int i;
  56. if (!p || !tty_init())
  57. return -1;
  58. for (i = 0; i < TTY_MAX; i++) {
  59. struct tty *t = tty_slot(i);
  60. if (!t->used) {
  61. t->used = 1;
  62. t->owner = p->pid;
  63. t->head = t->tail = 0;
  64. p->tty = (int)(i + 1);
  65. return (int)(i + 1);
  66. }
  67. }
  68. return -1;
  69. }
  70. void tty_close(int id)
  71. {
  72. struct tty *t = tty_get(id);
  73. if (t) {
  74. t->used = 0;
  75. t->head = t->tail = 0;
  76. }
  77. }
  78. int tty_write(int id, const char *buf, unsigned int len)
  79. {
  80. struct tty *t;
  81. unsigned int i = 0;
  82. if (!(t = tty_get(id)) || !t->used)
  83. return -1;
  84. while (i < len) {
  85. unsigned int next = (t->head + 1) % TTY_BUF;
  86. if (next == t->tail) {
  87. if (!process_is_alive(t->owner))
  88. return -1;
  89. process_sleep_ms(1);
  90. continue;
  91. }
  92. t->buf[t->head] = (unsigned char)buf[i++];
  93. t->head = next;
  94. /* Mirror command output (including ring-3 terminal commands) to
  95. * the network console, like screen_putchar does for the kernel
  96. * terminal. */
  97. netlog_char(buf[i - 1]);
  98. }
  99. return 0;
  100. }
  101. int tty_write_str(int id, const char *s)
  102. {
  103. unsigned int n = 0;
  104. while (s[n])
  105. n++;
  106. return tty_write(id, s, n);
  107. }
  108. int tty_read(int id, char *buf, unsigned int max)
  109. {
  110. struct tty *t;
  111. unsigned int n = 0;
  112. if (!(t = tty_get(id)) || !t->used)
  113. return -1;
  114. while (n < max && t->tail != t->head) {
  115. buf[n++] = (char)t->buf[t->tail];
  116. t->tail = (t->tail + 1) % TTY_BUF;
  117. }
  118. return (int)n;
  119. }
  120. void tty_reap(unsigned int pid)
  121. {
  122. unsigned int i;
  123. if (!ttys)
  124. return;
  125. for (i = 0; i < TTY_MAX; i++) {
  126. struct tty *t = tty_slot(i);
  127. if (t->used && t->owner == pid)
  128. t->used = 0;
  129. }
  130. }