| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /* TTY output rings. Commands write bytes; the terminal process drains and
- * renders them. A full ring blocks the writer (a terminal that is not
- * scheduled will drain shortly), so `cat`/helpers cannot silently lose output. */
- #include "tty.h"
- #include "mm.h"
- #include "netlog.h"
- #include "process.h"
- #define TTY_STRIDE (2 * PAGE_SIZE)
- struct tty {
- int used;
- unsigned int owner;
- unsigned char buf[TTY_BUF];
- unsigned int head, tail;
- };
- _Static_assert(sizeof(struct tty) <= TTY_STRIDE,
- "tty state no longer fits its two-page slot");
- /* The rings are 8 KB of state each (128 KB at TTY_MAX 16); allocate them from
- * the frame heap instead of BSS to keep the kernel's low-memory footprint
- * small. */
- static struct tty *ttys;
- static int tty_ready;
- static int tty_init(void)
- {
- unsigned int base, i;
- unsigned char *p;
- if (tty_ready)
- return 1;
- base = mm_alloc_pages(TTY_MAX * (TTY_STRIDE / PAGE_SIZE));
- if (!base)
- return 0;
- p = (unsigned char *)base;
- for (i = 0; i < TTY_MAX * TTY_STRIDE; i++)
- p[i] = 0;
- ttys = (struct tty *)base;
- tty_ready = 1;
- return 1;
- }
- static struct tty *tty_slot(unsigned int i)
- {
- return (struct tty *)((unsigned char *)ttys + i * TTY_STRIDE);
- }
- static struct tty *tty_get(int id)
- {
- if (!ttys || id <= 0 || id > TTY_MAX)
- return 0;
- return tty_slot((unsigned int)(id - 1));
- }
- int tty_valid(int id)
- {
- struct tty *t = tty_get(id);
- return t && t->used;
- }
- int tty_free_count(void)
- {
- unsigned int i, n = 0;
- if (!tty_init())
- return 0;
- for (i = 0; i < TTY_MAX; i++)
- if (!tty_slot(i)->used)
- n++;
- return (int)n;
- }
- int tty_alloc(void)
- {
- struct process *p = current_proc();
- unsigned int i;
- if (!p || !tty_init())
- return -1;
- for (i = 0; i < TTY_MAX; i++) {
- struct tty *t = tty_slot(i);
- if (!t->used) {
- t->used = 1;
- t->owner = p->pid;
- t->head = t->tail = 0;
- return (int)(i + 1);
- }
- }
- return -1;
- }
- int tty_create(void)
- {
- struct process *p = current_proc();
- int id = tty_alloc();
- if (id > 0 && p)
- p->tty = id;
- return id;
- }
- void tty_close(int id)
- {
- struct tty *t = tty_get(id);
- if (t) {
- t->used = 0;
- t->head = t->tail = 0;
- }
- }
- int tty_close_owned(int id, unsigned int pid)
- {
- struct tty *t = tty_get(id);
- if (!t || !t->used || t->owner != pid)
- return -1;
- t->used = 0;
- t->head = t->tail = 0;
- return 0;
- }
- int tty_write(int id, const char *buf, unsigned int len)
- {
- struct tty *t;
- unsigned int i = 0;
- if (!(t = tty_get(id)) || !t->used)
- return -1;
- while (i < len) {
- unsigned int next = (t->head + 1) % TTY_BUF;
- if (next == t->tail) {
- struct process *cur = current_proc();
- if (!process_is_alive(t->owner))
- return -1;
- /* The owner is the only reader: sleeping on its own
- * full ring can never make progress. Drop instead
- * (the browser rebinds itself to its helpers' tty). */
- if (cur && cur->pid == t->owner)
- return -1;
- process_sleep_ms(1);
- continue;
- }
- t->buf[t->head] = (unsigned char)buf[i++];
- t->head = next;
- /* Mirror command output (including ring-3 terminal commands) to
- * the network console, like screen_putchar does for the kernel
- * terminal. */
- netlog_char(buf[i - 1]);
- }
- return 0;
- }
- int tty_try_write(int id, const char *buf, unsigned int len)
- {
- struct tty *t;
- unsigned int i, used, free;
- if (!(t = tty_get(id)) || !t->used)
- return -1;
- used = (t->head + TTY_BUF - t->tail) % TTY_BUF;
- free = TTY_BUF - 1 - used;
- if (len > free)
- return -1; /* no partial control messages */
- for (i = 0; i < len; i++) {
- t->buf[t->head] = (unsigned char)buf[i];
- t->head = (t->head + 1) % TTY_BUF;
- netlog_char(buf[i]);
- }
- return 0;
- }
- int tty_write_str(int id, const char *s)
- {
- unsigned int n = 0;
- while (s[n])
- n++;
- return tty_write(id, s, n);
- }
- int tty_read(int id, char *buf, unsigned int max)
- {
- struct tty *t;
- unsigned int n = 0;
- if (!(t = tty_get(id)) || !t->used)
- return -1;
- while (n < max && t->tail != t->head) {
- buf[n++] = (char)t->buf[t->tail];
- t->tail = (t->tail + 1) % TTY_BUF;
- }
- return (int)n;
- }
- void tty_reap(unsigned int pid)
- {
- unsigned int i;
- if (!ttys)
- return;
- for (i = 0; i < TTY_MAX; i++) {
- struct tty *t = tty_slot(i);
- if (t->used && t->owner == pid)
- t->used = 0;
- }
- }
|