diff --git a/usr.sbin/bhyve/uart_backend.h b/usr.sbin/bhyve/uart_backend.h --- a/usr.sbin/bhyve/uart_backend.h +++ b/usr.sbin/bhyve/uart_backend.h @@ -51,5 +51,6 @@ struct uart_softc *uart_init(void); int uart_tty_open(struct uart_softc *sc, const char *path, void (*drain)(int, enum ev_type, void *), void *arg); - +void uart_softc_lock(struct uart_softc *sc); +void uart_softc_unlock(struct uart_softc *sc); #endif /* _UART_BACKEND_H_ */ diff --git a/usr.sbin/bhyve/uart_backend.c b/usr.sbin/bhyve/uart_backend.c --- a/usr.sbin/bhyve/uart_backend.c +++ b/usr.sbin/bhyve/uart_backend.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,7 @@ struct ttyfd tty; struct fifo rxfifo; struct mevent *mev; + pthread_mutex_t mtx; }; static bool uart_stdio; /* stdio in use for i/o */ @@ -325,7 +327,13 @@ struct uart_softc * uart_init(void) { - return (calloc(1, sizeof(struct uart_softc))); + struct uart_softc *sc = calloc(1, sizeof(struct uart_softc)); + if (sc == NULL) + return (NULL); + + pthread_mutex_init(&sc->mtx, NULL); + + return (sc); } int @@ -346,3 +354,15 @@ return (retval); } + +void +uart_softc_lock(struct uart_softc *sc) +{ + pthread_mutex_lock(&sc->mtx); +} + +void +uart_softc_unlock(struct uart_softc *sc) +{ + pthread_mutex_unlock(&sc->mtx); +} diff --git a/usr.sbin/bhyve/uart_emul.c b/usr.sbin/bhyve/uart_emul.c --- a/usr.sbin/bhyve/uart_emul.c +++ b/usr.sbin/bhyve/uart_emul.c @@ -83,7 +83,6 @@ struct uart_ns16550_softc { struct uart_softc *backend; - pthread_mutex_t mtx; /* protects all softc elements */ uint8_t data; /* Data register (R/W) */ uint8_t ier; /* Interrupt enable register (R/W) */ uint8_t lcr; /* Line control register (R/W) */ @@ -204,14 +203,14 @@ * to take out the softc lock to protect against concurrent * access from a vCPU i/o exit */ - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); loopback = (sc->mcr & MCR_LOOPBACK) != 0; uart_rxfifo_drain(sc->backend, loopback); if (!loopback) uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } void @@ -220,7 +219,7 @@ int fifosz; uint8_t msr; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); /* * Take care of the special case DLAB accesses first @@ -329,7 +328,7 @@ done: uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } uint8_t @@ -337,7 +336,7 @@ { uint8_t iir, intr_reason, reg; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); /* * Take care of the special case DLAB accesses first @@ -414,7 +413,7 @@ done: uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); return (reg); } @@ -446,8 +445,6 @@ sc->intr_deassert = intr_deassert; sc->backend = uart_init(); - pthread_mutex_init(&sc->mtx, NULL); - uart_reset(sc); return (sc); diff --git a/usr.sbin/bhyve/uart_pl011.c b/usr.sbin/bhyve/uart_pl011.c --- a/usr.sbin/bhyve/uart_pl011.c +++ b/usr.sbin/bhyve/uart_pl011.c @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -106,7 +105,6 @@ struct uart_pl011_softc { struct uart_softc *backend; - pthread_mutex_t mtx; /* protects all softc elements */ uint16_t irq_state; @@ -184,7 +182,7 @@ * to take out the softc lock to protect against concurrent * access from a vCPU i/o exit */ - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); old_size = uart_rxfifo_numchars(sc->backend); @@ -202,7 +200,7 @@ if (!loopback) uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } void @@ -210,7 +208,7 @@ { bool loopback; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); switch (offset) { case UARTDR: loopback = (sc->cr & UARTCR_LBE) != 0; @@ -266,7 +264,7 @@ break; } uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } uint32_t @@ -276,7 +274,7 @@ int fifo_sz; reg = 0; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); switch (offset) { case UARTDR: reg = uart_rxfifo_getchar(sc->backend); @@ -362,7 +360,7 @@ break; } uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); return (reg); } @@ -380,8 +378,6 @@ sc->intr_deassert = intr_deassert; sc->backend = uart_init(); - pthread_mutex_init(&sc->mtx, NULL); - uart_reset(sc); return (sc);