diff --git a/so3/so3/devices/console.c b/so3/so3/devices/console.c index 88e76f9e5..1277c7b40 100644 --- a/so3/so3/devices/console.c +++ b/so3/so3/devices/console.c @@ -17,8 +17,12 @@ */ #include +#include #include +#define CTRL_C 3 +#define CTRL_D 4 + /* Default termios flags that will be used by the console. */ static termios_t termios = { .c_iflag = INLCR, @@ -77,19 +81,33 @@ static int console_getc(int fd, void *buffer, int count) c = console_get_next_c(); /* ETX (Ctrl-C, signalled by the serial IRQ while reading): - * discard whatever was typed and return an empty line, so the - * reader (e.g. the shell) reprints its prompt. The IRQ has - * already echoed "^C\n". */ - if (c == 3) { + * discard whatever was typed. A reader sent SIGINT is + * interrupted; the top shell gets an empty line and reprints + * its prompt. The IRQ has already echoed "^C\n". */ + if (c == CTRL_C) { + if (serial_intr_signalled) { + serial_intr_signalled = false; + return -EINTR; + } + total = 0; c_buf[total++] = '\n'; break; } + /* EOF: the line typed so far without a newline, 0 on an empty line. */ + if (c == CTRL_D) + break; + icanon_handle_char(fd, c, c_buf, &total); } while (c != '\n' && total < count); } else { *c_buf = console_get_next_c(); + if ((*c_buf == CTRL_C) && serial_intr_signalled) { + serial_intr_signalled = false; + return -EINTR; + } + total = 1; } diff --git a/so3/so3/devices/serial.c b/so3/so3/devices/serial.c index 707b8262b..fa46a5ac5 100644 --- a/so3/so3/devices/serial.c +++ b/so3/so3/devices/serial.c @@ -33,6 +33,7 @@ serial_ops_t serial_ops; mutex_t read_lock; tcb_t *tcb_owner; volatile bool serial_intr = false; +volatile bool serial_intr_signalled = false; /* Outputs an ASCII string to console */ int serial_putc(char c) @@ -117,6 +118,35 @@ void send_qemu_halt(void) #endif +/* + * Ctrl-C typed on the console, called from the UART interrupt. A console read + * in progress is cancelled, and SIGINT goes to its reader as well as to the + * foreground process the shell waits for. The root process (the top shell) + * only has its line cancelled: it must not die. + */ + +void serial_ctrl_c(void) +{ +#ifdef CONFIG_IPC_SIGNAL + /* fg_pcb, not current(): in IRQ context, current() is whatever thread was running. */ + pcb_t *target = fg_pcb ? fg_pcb : current()->pcb; + bool reading = mutex_is_locked(&read_lock); + pcb_t *reader = reading ? tcb_owner->pcb : NULL; + + if (reading) { + /* The read fails with -EINTR if its process does not ignore the signal. */ + serial_intr_signalled = reader && reader->parent && (reader->sa[SIGINT].sa_handler != SIG_IGN); + serial_intr = true; + } + + if (reader && reader->parent && (reader != target)) + sys_do_kill(reader->pid, SIGINT); + + if (target && (!reading || target->parent)) + sys_do_kill(target->pid, SIGINT); +#endif +} + /* * Release the lock if a process/thread is finishing during a read. */ @@ -125,8 +155,13 @@ void serial_cleanup(void) /* Check if the lock is owned by a thread which is currently terminated (via ctrl/c) or * killed. In this case only, and if the thread is locking the serial device, the lock must be released. */ - if ((current() == tcb_owner) && mutex_is_locked(&read_lock)) + if ((current() == tcb_owner) && mutex_is_locked(&read_lock)) { + /* A Ctrl-C which killed the reader is not for the next one. */ + serial_intr = false; + serial_intr_signalled = false; + mutex_unlock(&read_lock); + } } /* diff --git a/so3/so3/devices/serial/bcm283x_mu.c b/so3/so3/devices/serial/bcm283x_mu.c index 44a9b91af..dbbc99bd0 100644 --- a/so3/so3/devices/serial/bcm283x_mu.c +++ b/so3/so3/devices/serial/bcm283x_mu.c @@ -125,9 +125,7 @@ static char bcm283x_mu_get_byte(bool polling) * interrupt, so we loop on LSR rather than decoding IIR — that also sidesteps * the mini UART's muddled IIR documentation. * - * Ctrl-C handling mirrors pl011_int(): if a thread holds read_lock it is - * blocked reading the console (e.g. the shell), so we only cancel its line - * instead of killing it; otherwise a foreground app is running and gets SIGINT. + * Ctrl-C is handled by serial_ctrl_c(), as on the PL011. */ static irq_return_t bcm283x_mu_int(int irq, void *dummy) { @@ -143,21 +141,8 @@ static irq_return_t bcm283x_mu_int(int irq, void *dummy) bcm283x_mu_put_byte('C'); bcm283x_mu_put_byte('\n'); -#ifdef CONFIG_IPC_SIGNAL - if (mutex_is_locked(&read_lock)) { - serial_intr = true; - } else { - /* Use fg_pcb (the shell's foreground job), NOT - * current(): in IRQ context current() is just the - * thread running when the key arrived (usually the - * idle thread, since the foreground app is typically - * asleep). Fall back to current() if no foreground is - * tracked yet. */ - pcb_t *target = fg_pcb ? fg_pcb : current()->pcb; - if (target != NULL) - sys_do_kill(target->pid, SIGINT); - } -#endif + serial_ctrl_c(); + /* Already echoed above; keep it out of the ring. */ continue; } diff --git a/so3/so3/devices/serial/pl011.c b/so3/so3/devices/serial/pl011.c index 0b8207905..873f7205b 100644 --- a/so3/so3/devices/serial/pl011.c +++ b/so3/so3/devices/serial/pl011.c @@ -125,26 +125,7 @@ static irq_return_t pl011_int(int irq, void *dummy) pl011_put_byte('\n'); prod--; /* Already sent out to the serial interface */ -#ifdef CONFIG_IPC_SIGNAL - if (mutex_is_locked(&read_lock)) { - /* A thread (e.g. the shell) is blocked reading the - * console: cancel its current line so it returns an - * empty line and reprints its prompt, rather than - * killing it. */ - serial_intr = true; - } else { - /* No console read in progress -> a foreground app is - * running. Deliver SIGINT to it. Use fg_pcb (the - * shell's foreground job), NOT current(): in IRQ - * context current() is just the thread running when - * the key arrived (usually the idle thread, since the - * foreground app is typically asleep). Fall back to - * current() if no foreground is tracked yet. */ - pcb_t *target = fg_pcb ? fg_pcb : current()->pcb; - if (target != NULL) - sys_do_kill(target->pid, SIGINT); - } -#endif + serial_ctrl_c(); } prod = (prod + 1) % SERIAL_BUFFER_SIZE; diff --git a/so3/so3/devices/serial/uart_pio.c b/so3/so3/devices/serial/uart_pio.c index 9345ef237..ba3915de7 100644 --- a/so3/so3/devices/serial/uart_pio.c +++ b/so3/so3/devices/serial/uart_pio.c @@ -114,16 +114,8 @@ static void uart_rx_char(char c) uart_put_byte('C'); uart_put_byte('\n'); -#ifdef CONFIG_IPC_SIGNAL - if (mutex_is_locked(&read_lock)) { - serial_intr = true; - } else { - pcb_t *target = fg_pcb ? fg_pcb : current()->pcb; - - if (target != NULL) - sys_do_kill(target->pid, SIGINT); - } -#endif + serial_ctrl_c(); + return; } diff --git a/so3/so3/include/device/serial.h b/so3/so3/include/device/serial.h index 3102b422d..c2990c44a 100644 --- a/so3/so3/include/device/serial.h +++ b/so3/so3/include/device/serial.h @@ -49,6 +49,7 @@ extern serial_ops_t serial_ops; * cancel the current line (the blocked reader returns an empty line). See * the serial IRQ (pl011.c) and console_getc (console.c). */ extern volatile bool serial_intr; +extern volatile bool serial_intr_signalled; struct winsize { unsigned short ws_row; /* rows, in characters */ @@ -68,6 +69,7 @@ int ll_serial_write(char *str, int len); void serial_init(void); void serial_cleanup(void); +void serial_ctrl_c(void); void enable_uart_irq(void); void disable_uart_irq(void);