tty.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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; allocate them from the frame heap instead of
  18. * BSS to keep the kernel's low-memory footprint small. */
  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_free_count(void)
  53. {
  54. unsigned int i, n = 0;
  55. if (!tty_init())
  56. return 0;
  57. for (i = 0; i < TTY_MAX; i++)
  58. if (!tty_slot(i)->used)
  59. n++;
  60. return (int)n;
  61. }
  62. int tty_create(void)
  63. {
  64. struct process *p = current_proc();
  65. unsigned int i;
  66. if (!p || !tty_init())
  67. return -1;
  68. for (i = 0; i < TTY_MAX; i++) {
  69. struct tty *t = tty_slot(i);
  70. if (!t->used) {
  71. t->used = 1;
  72. t->owner = p->pid;
  73. t->head = t->tail = 0;
  74. p->tty = (int)(i + 1);
  75. return (int)(i + 1);
  76. }
  77. }
  78. return -1;
  79. }
  80. void tty_close(int id)
  81. {
  82. struct tty *t = tty_get(id);
  83. if (t) {
  84. t->used = 0;
  85. t->head = t->tail = 0;
  86. }
  87. }
  88. int tty_write(int id, const char *buf, unsigned int len)
  89. {
  90. struct tty *t;
  91. unsigned int i = 0;
  92. if (!(t = tty_get(id)) || !t->used)
  93. return -1;
  94. while (i < len) {
  95. unsigned int next = (t->head + 1) % TTY_BUF;
  96. if (next == t->tail) {
  97. if (!process_is_alive(t->owner))
  98. return -1;
  99. process_sleep_ms(1);
  100. continue;
  101. }
  102. t->buf[t->head] = (unsigned char)buf[i++];
  103. t->head = next;
  104. /* Mirror command output (including ring-3 terminal commands) to
  105. * the network console, like screen_putchar does for the kernel
  106. * terminal. */
  107. netlog_char(buf[i - 1]);
  108. }
  109. return 0;
  110. }
  111. int tty_write_str(int id, const char *s)
  112. {
  113. unsigned int n = 0;
  114. while (s[n])
  115. n++;
  116. return tty_write(id, s, n);
  117. }
  118. int tty_read(int id, char *buf, unsigned int max)
  119. {
  120. struct tty *t;
  121. unsigned int n = 0;
  122. if (!(t = tty_get(id)) || !t->used)
  123. return -1;
  124. while (n < max && t->tail != t->head) {
  125. buf[n++] = (char)t->buf[t->tail];
  126. t->tail = (t->tail + 1) % TTY_BUF;
  127. }
  128. return (int)n;
  129. }
  130. void tty_reap(unsigned int pid)
  131. {
  132. unsigned int i;
  133. if (!ttys)
  134. return;
  135. for (i = 0; i < TTY_MAX; i++) {
  136. struct tty *t = tty_slot(i);
  137. if (t->used && t->owner == pid)
  138. t->used = 0;
  139. }
  140. }