tty.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. unsigned int i;
  54. if (!tty_init())
  55. return -1;
  56. for (i = 0; i < TTY_MAX; i++) {
  57. struct tty *t = tty_slot(i);
  58. if (!t->used) {
  59. t->used = 1;
  60. t->owner = procs[current_pid].pid;
  61. t->head = t->tail = 0;
  62. procs[current_pid].tty = (int)(i + 1);
  63. return (int)(i + 1);
  64. }
  65. }
  66. return -1;
  67. }
  68. void tty_close(int id)
  69. {
  70. struct tty *t = tty_get(id);
  71. if (t) {
  72. t->used = 0;
  73. t->head = t->tail = 0;
  74. }
  75. }
  76. int tty_write(int id, const char *buf, unsigned int len)
  77. {
  78. struct tty *t;
  79. unsigned int i = 0;
  80. if (!(t = tty_get(id)) || !t->used)
  81. return -1;
  82. while (i < len) {
  83. unsigned int next = (t->head + 1) % TTY_BUF;
  84. if (next == t->tail) {
  85. if (!process_is_alive(t->owner))
  86. return -1;
  87. process_sleep_ms(1);
  88. continue;
  89. }
  90. t->buf[t->head] = (unsigned char)buf[i++];
  91. t->head = next;
  92. }
  93. return 0;
  94. }
  95. int tty_write_str(int id, const char *s)
  96. {
  97. unsigned int n = 0;
  98. while (s[n])
  99. n++;
  100. return tty_write(id, s, n);
  101. }
  102. int tty_read(int id, char *buf, unsigned int max)
  103. {
  104. struct tty *t;
  105. unsigned int n = 0;
  106. if (!(t = tty_get(id)) || !t->used)
  107. return -1;
  108. while (n < max && t->tail != t->head) {
  109. buf[n++] = (char)t->buf[t->tail];
  110. t->tail = (t->tail + 1) % TTY_BUF;
  111. }
  112. return (int)n;
  113. }
  114. void tty_reap(unsigned int pid)
  115. {
  116. unsigned int i;
  117. if (!ttys)
  118. return;
  119. for (i = 0; i < TTY_MAX; i++) {
  120. struct tty *t = tty_slot(i);
  121. if (t->used && t->owner == pid)
  122. t->used = 0;
  123. }
  124. }