HV: vuart: MISRA clean in vuart.c

1. remove multiple return
2. remove goto
3. fix potential NULL pointer dereferencing
4. rename struct fifo to struct vuart_fifo to avoid naming conflicts.

Tracked-On: #2987
Signed-off-by: Conghui Chen <conghui.chen@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Conghui Chen 2019-04-29 14:39:57 +08:00 committed by ACRN System Integration
parent c61db6ffa0
commit a4b5e39fab
2 changed files with 151 additions and 159 deletions

View File

@ -43,14 +43,14 @@ static uint16_t vuart_com_base = CONFIG_COM_BASE;
#define vuart_lock(vu) spinlock_obtain(&((vu)->lock)) #define vuart_lock(vu) spinlock_obtain(&((vu)->lock))
#define vuart_unlock(vu) spinlock_release(&((vu)->lock)) #define vuart_unlock(vu) spinlock_release(&((vu)->lock))
static inline void fifo_reset(struct fifo *fifo) static inline void fifo_reset(struct vuart_fifo *fifo)
{ {
fifo->rindex = 0U; fifo->rindex = 0U;
fifo->windex = 0U; fifo->windex = 0U;
fifo->num = 0U; fifo->num = 0U;
} }
static inline void fifo_putchar(struct fifo *fifo, char ch) static inline void fifo_putchar(struct vuart_fifo *fifo, char ch)
{ {
fifo->buf[fifo->windex] = ch; fifo->buf[fifo->windex] = ch;
if (fifo->num < fifo->size) { if (fifo->num < fifo->size) {
@ -62,21 +62,19 @@ static inline void fifo_putchar(struct fifo *fifo, char ch)
} }
} }
static inline char fifo_getchar(struct fifo *fifo) static inline char fifo_getchar(struct vuart_fifo *fifo)
{ {
char c; char c = -1;
if (fifo->num > 0U) { if (fifo->num > 0U) {
c = fifo->buf[fifo->rindex]; c = fifo->buf[fifo->rindex];
fifo->rindex = (fifo->rindex + 1U) % fifo->size; fifo->rindex = (fifo->rindex + 1U) % fifo->size;
fifo->num--; fifo->num--;
return c;
} else {
return -1;
} }
return c;
} }
static inline uint32_t fifo_numchars(const struct fifo *fifo) static inline uint32_t fifo_numchars(const struct vuart_fifo *fifo)
{ {
return fifo->num; return fifo->num;
} }
@ -117,15 +115,16 @@ static inline void vuart_fifo_init(struct acrn_vuart *vu)
*/ */
static uint8_t vuart_intr_reason(const struct acrn_vuart *vu) static uint8_t vuart_intr_reason(const struct acrn_vuart *vu)
{ {
uint8_t ret = IIR_NOPEND;
if (((vu->lsr & LSR_OE) != 0U) && ((vu->ier & IER_ELSI) != 0U)) { if (((vu->lsr & LSR_OE) != 0U) && ((vu->ier & IER_ELSI) != 0U)) {
return IIR_RLS; ret = IIR_RLS;
} else if ((fifo_numchars(&vu->rxfifo) > 0U) && ((vu->ier & IER_ERBFI) != 0U)) { } else if ((fifo_numchars(&vu->rxfifo) > 0U) && ((vu->ier & IER_ERBFI) != 0U)) {
return IIR_RXTOUT; ret = IIR_RXTOUT;
} else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0U)) { } else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0U)) {
return IIR_TXRDY; ret = IIR_TXRDY;
} else {
return IIR_NOPEND;
} }
return ret;
} }
struct acrn_vuart *find_vuart_by_port(struct acrn_vm *vm, uint16_t offset) struct acrn_vuart *find_vuart_by_port(struct acrn_vm *vm, uint16_t offset)
@ -194,29 +193,21 @@ static bool vuart_write(struct acrn_vm *vm, uint16_t offset_arg,
uint8_t value_u8 = (uint8_t)value; uint8_t value_u8 = (uint8_t)value;
struct acrn_vuart *target_vu = NULL; struct acrn_vuart *target_vu = NULL;
if (vu) {
offset -= vu->port_base; offset -= vu->port_base;
target_vu = vu->target_vu; target_vu = vu->target_vu;
if ((offset == UART16550_THR) && target_vu) { if ((offset == UART16550_THR) && target_vu) {
vuart_write_to_target(target_vu, value_u8); vuart_write_to_target(target_vu, value_u8);
return true; } else {
}
vuart_lock(vu); vuart_lock(vu);
/* /*
* Take care of the special case DLAB accesses first * Take care of the special case DLAB accesses first
*/ */
if ((vu->lcr & LCR_DLAB) != 0U) { if ((vu->lcr & LCR_DLAB) != 0U && offset == UART16550_DLL) {
if (offset == UART16550_DLL) {
vu->dll = value_u8; vu->dll = value_u8;
goto done; } else if ((vu->lcr & LCR_DLAB) != 0U && offset == UART16550_DLM) {
}
if (offset == UART16550_DLM) {
vu->dlh = value_u8; vu->dlh = value_u8;
goto done; } else {
}
}
switch (offset) { switch (offset) {
case UART16550_THR: case UART16550_THR:
fifo_putchar(&vu->txfifo, (char)value_u8); fifo_putchar(&vu->txfifo, (char)value_u8);
@ -272,11 +263,12 @@ static bool vuart_write(struct acrn_vm *vm, uint16_t offset_arg,
*/ */
break; break;
} }
}
done:
vuart_toggle_intr(vu); vuart_toggle_intr(vu);
vuart_unlock(vu); vuart_unlock(vu);
}
}
return true; return true;
} }
@ -288,6 +280,7 @@ static bool vuart_read(struct acrn_vm *vm, struct acrn_vcpu *vcpu, uint16_t offs
struct acrn_vuart *vu = find_vuart_by_port(vm, offset); struct acrn_vuart *vu = find_vuart_by_port(vm, offset);
struct pio_request *pio_req = &vcpu->req.reqs.pio; struct pio_request *pio_req = &vcpu->req.reqs.pio;
if (vu) {
offset -= vu->port_base; offset -= vu->port_base;
vuart_lock(vu); vuart_lock(vu);
/* /*
@ -296,14 +289,12 @@ static bool vuart_read(struct acrn_vm *vm, struct acrn_vcpu *vcpu, uint16_t offs
if ((vu->lcr & LCR_DLAB) != 0U) { if ((vu->lcr & LCR_DLAB) != 0U) {
if (offset == UART16550_DLL) { if (offset == UART16550_DLL) {
reg = vu->dll; reg = vu->dll;
goto done; } else if (offset == UART16550_DLM) {
}
if (offset == UART16550_DLM) {
reg = vu->dlh; reg = vu->dlh;
goto done; } else {
} reg = 0U;
} }
} else {
switch (offset) { switch (offset) {
case UART16550_RBR: case UART16550_RBR:
vu->lsr &= ~LSR_OE; vu->lsr &= ~LSR_OE;
@ -354,10 +345,11 @@ static bool vuart_read(struct acrn_vm *vm, struct acrn_vcpu *vcpu, uint16_t offs
reg = 0xFFU; reg = 0xFFU;
break; break;
} }
done: }
vuart_toggle_intr(vu); vuart_toggle_intr(vu);
pio_req->value = (uint32_t)reg; pio_req->value = (uint32_t)reg;
vuart_unlock(vu); vuart_unlock(vu);
}
return true; return true;
} }

View File

@ -48,7 +48,7 @@
#define COM3_IRQ 6U #define COM3_IRQ 6U
#define COM4_IRQ 7U #define COM4_IRQ 7U
struct fifo { struct vuart_fifo {
char *buf; char *buf;
uint32_t rindex; /* index to read from */ uint32_t rindex; /* index to read from */
uint32_t windex; /* index to write to */ uint32_t windex; /* index to write to */