tty.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "process.h"
  7. #define TTY_STRIDE (2 * PAGE_SIZE)
  8. struct tty {
  9. int used;
  10. unsigned int owner;
  11. unsigned char buf[TTY_BUF];
  12. unsigned int head, tail;
  13. };
  14. _Static_assert(sizeof(struct tty) <= TTY_STRIDE,
  15. "tty state no longer fits its two-page slot");
  16. /* The rings are 16 KB of state; keep them out of BSS so the kernel image
  17. * stays below the run area. */
  18. static struct tty *ttys;
  19. static int tty_ready;
  20. static int tty_init(void)
  21. {
  22. unsigned int base, i;
  23. unsigned char *p;
  24. if (tty_ready)
  25. return 1;
  26. base = mm_alloc_pages(TTY_MAX * (TTY_STRIDE / PAGE_SIZE));
  27. if (!base)
  28. return 0;
  29. p = (unsigned char *)base;
  30. for (i = 0; i < TTY_MAX * TTY_STRIDE; i++)
  31. p[i] = 0;
  32. ttys = (struct tty *)base;
  33. tty_ready = 1;
  34. return 1;
  35. }
  36. static struct tty *tty_slot(unsigned int i)
  37. {
  38. return (struct tty *)((unsigned char *)ttys + i * TTY_STRIDE);
  39. }
  40. static struct tty *tty_get(int id)
  41. {
  42. if (!ttys || id <= 0 || id > TTY_MAX)
  43. return 0;
  44. return tty_slot((unsigned int)(id - 1));
  45. }
  46. int tty_valid(int id)
  47. {
  48. struct tty *t = tty_get(id);
  49. return t && t->used;
  50. }
  51. int tty_create(void)
  52. {
  53. struct process *p = current_proc();
  54. unsigned int i;
  55. if (!p || !tty_init())
  56. return -1;
  57. for (i = 0; i < TTY_MAX; i++) {
  58. struct tty *t = tty_slot(i);
  59. if (!t->used) {
  60. t->used = 1;
  61. t->owner = p->pid;
  62. t->head = t->tail = 0;
  63. p->tty = (int)(i + 1);
  64. return (int)(i + 1);
  65. }
  66. }
  67. return -1;
  68. }
  69. void tty_close(int id)
  70. {
  71. struct tty *t = tty_get(id);
  72. if (t) {
  73. t->used = 0;
  74. t->head = t->tail = 0;
  75. }
  76. }
  77. int tty_write(int id, const char *buf, unsigned int len)
  78. {
  79. struct tty *t;
  80. unsigned int i = 0;
  81. if (!(t = tty_get(id)) || !t->used)
  82. return -1;
  83. while (i < len) {
  84. unsigned int next = (t->head + 1) % TTY_BUF;
  85. if (next == t->tail) {
  86. if (!process_is_alive(t->owner))
  87. return -1;
  88. process_sleep_ms(1);
  89. continue;
  90. }
  91. t->buf[t->head] = (unsigned char)buf[i++];
  92. t->head = next;
  93. }
  94. return 0;
  95. }
  96. int tty_write_str(int id, const char *s)
  97. {
  98. unsigned int n = 0;
  99. while (s[n])
  100. n++;
  101. return tty_write(id, s, n);
  102. }
  103. int tty_read(int id, char *buf, unsigned int max)
  104. {
  105. struct tty *t;
  106. unsigned int n = 0;
  107. if (!(t = tty_get(id)) || !t->used)
  108. return -1;
  109. while (n < max && t->tail != t->head) {
  110. buf[n++] = (char)t->buf[t->tail];
  111. t->tail = (t->tail + 1) % TTY_BUF;
  112. }
  113. return (int)n;
  114. }
  115. void tty_reap(unsigned int pid)
  116. {
  117. unsigned int i;
  118. if (!ttys)
  119. return;
  120. for (i = 0; i < TTY_MAX; i++) {
  121. struct tty *t = tty_slot(i);
  122. if (t->used && t->owner == pid)
  123. t->used = 0;
  124. }
  125. }