Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions so3/so3/devices/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
*/

#include <termios.h>
#include <errno.h>
#include <device/serial.h>

#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,
Expand Down Expand Up @@ -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;
}

Expand Down
37 changes: 36 additions & 1 deletion so3/so3/devices/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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);
}
}

/*
Expand Down
21 changes: 3 additions & 18 deletions so3/so3/devices/serial/bcm283x_mu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
21 changes: 1 addition & 20 deletions so3/so3/devices/serial/pl011.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 2 additions & 10 deletions so3/so3/devices/serial/uart_pio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions so3/so3/include/device/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
Loading