hv: vuart: fix the data type violations

- Fix the data type violations based on MISRA-C requirements
- Add '-fsigned-char' in Makefile to let the compiler treats 'char' be
   signed, like 'signed char'.
  Otherwise, the static checker treats 'char', 'signed char' and 'unsigned
   char' as three different types.
- Fix some minor coding style issues, such as TAB issues, line over 80
   characters issues and comments style

v1 -> v2:
 * fix the violation regarding to 'fifo_getchar'

Tracked-On: #861
Signed-off-by: Shiqing Gao <shiqing.gao@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Shiqing Gao 2018-08-16 14:57:20 +08:00 committed by lijinxia
parent d82a86e648
commit 40745d90c5
4 changed files with 68 additions and 64 deletions

View File

@ -37,6 +37,7 @@ include scripts/kconfig/kconfig.mk
CFLAGS += -Wall -W CFLAGS += -Wall -W
CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -fshort-wchar -ffreestanding CFLAGS += -fshort-wchar -ffreestanding
CFLAGS += -fsigned-char
CFLAGS += -m64 -mno-mmx -mno-sse -mno-sse2 -mno-80387 -mno-fp-ret-in-387 CFLAGS += -m64 -mno-mmx -mno-sse -mno-sse2 -mno-80387 -mno-fp-ret-in-387
CFLAGS += -mno-red-zone CFLAGS += -mno-red-zone
CFLAGS += -nostdinc -nostdlib -fno-common CFLAGS += -nostdinc -nostdlib -fno-common

View File

@ -32,10 +32,10 @@
#include "uart16550.h" #include "uart16550.h"
#define COM1_BASE 0x3F8 #define COM1_BASE 0x3F8U
#define COM1_IRQ 4U #define COM1_IRQ 4U
#define RX_FIFO_SIZE 256 #define RX_FIFO_SIZE 256U
#define TX_FIFO_SIZE 65536 #define TX_FIFO_SIZE 65536U
#define vuart_lock_init(vu) spinlock_init(&((vu)->lock)) #define vuart_lock_init(vu) spinlock_init(&((vu)->lock))
#define vuart_lock(vu) spinlock_obtain(&((vu)->lock)) #define vuart_lock(vu) spinlock_obtain(&((vu)->lock))
@ -45,12 +45,12 @@
static void fifo_reset(struct fifo *fifo) static void fifo_reset(struct fifo *fifo)
{ {
fifo->rindex = 0; fifo->rindex = 0U;
fifo->windex = 0; fifo->windex = 0U;
fifo->num = 0; fifo->num = 0U;
} }
static void fifo_init(struct fifo *fifo, int sz) static void fifo_init(struct fifo *fifo, uint32_t sz)
{ {
fifo->buf = calloc(1U, sz); fifo->buf = calloc(1U, sz);
ASSERT(fifo->buf != NULL, ""); ASSERT(fifo->buf != NULL, "");
@ -62,11 +62,11 @@ static void fifo_putchar(struct fifo *fifo, char ch)
{ {
fifo->buf[fifo->windex] = ch; fifo->buf[fifo->windex] = ch;
if (fifo->num < fifo->size) { if (fifo->num < fifo->size) {
fifo->windex = (fifo->windex + 1) % fifo->size; fifo->windex = (fifo->windex + 1U) % fifo->size;
fifo->num++; fifo->num++;
} else { } else {
fifo->rindex = (fifo->rindex + 1) % fifo->size; fifo->rindex = (fifo->rindex + 1U) % fifo->size;
fifo->windex = (fifo->windex + 1) % fifo->size; fifo->windex = (fifo->windex + 1U) % fifo->size;
} }
} }
@ -74,9 +74,9 @@ static char fifo_getchar(struct fifo *fifo)
{ {
char c; char c;
if (fifo->num > 0) { if (fifo->num > 0U) {
c = fifo->buf[fifo->rindex]; c = fifo->buf[fifo->rindex];
fifo->rindex = (fifo->rindex + 1) % fifo->size; fifo->rindex = (fifo->rindex + 1U) % fifo->size;
fifo->num--; fifo->num--;
return c; return c;
} else { } else {
@ -84,7 +84,7 @@ static char fifo_getchar(struct fifo *fifo)
} }
} }
static int fifo_numchars(struct fifo *fifo) static uint32_t fifo_numchars(struct fifo *fifo)
{ {
return fifo->num; return fifo->num;
} }
@ -98,11 +98,12 @@ static int fifo_numchars(struct fifo *fifo)
*/ */
static uint8_t vuart_intr_reason(struct vuart *vu) static uint8_t vuart_intr_reason(struct vuart *vu)
{ {
if (((vu->lsr & LSR_OE) != 0) && ((vu->ier & IER_ELSI) != 0)) { if (((vu->lsr & LSR_OE) != 0U) && ((vu->ier & IER_ELSI) != 0U)) {
return IIR_RLS; return IIR_RLS;
} else if ((fifo_numchars(&vu->rxfifo) > 0) && ((vu->ier & IER_ERBFI) != 0)) { } else if ((fifo_numchars(&vu->rxfifo) > 0U) &&
((vu->ier & IER_ERBFI) != 0U)) {
return IIR_RXTOUT; return IIR_RXTOUT;
} else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0)) { } else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0U)) {
return IIR_TXRDY; return IIR_TXRDY;
} else { } else {
return IIR_NOPEND; return IIR_NOPEND;
@ -145,7 +146,7 @@ static void vuart_write(__unused struct vm_io_handler *hdlr, struct vm *vm,
/* /*
* Take care of the special case DLAB accesses first * Take care of the special case DLAB accesses first
*/ */
if ((vu->lcr & LCR_DLAB) != 0) { if ((vu->lcr & LCR_DLAB) != 0U) {
if (offset == UART16550_DLL) { if (offset == UART16550_DLL) {
vu->dll = value_u8; vu->dll = value_u8;
goto done; goto done;
@ -159,7 +160,7 @@ static void vuart_write(__unused struct vm_io_handler *hdlr, struct vm *vm,
switch (offset) { switch (offset) {
case UART16550_THR: case UART16550_THR:
fifo_putchar(&vu->txfifo, value_u8); fifo_putchar(&vu->txfifo, (char)value_u8);
vu->thre_int_pending = true; vu->thre_int_pending = true;
break; break;
case UART16550_IER: case UART16550_IER:
@ -231,7 +232,7 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
/* /*
* Take care of the special case DLAB accesses first * Take care of the special case DLAB accesses first
*/ */
if ((vu->lcr & LCR_DLAB) != 0) { if ((vu->lcr & LCR_DLAB) != 0U) {
if (offset == UART16550_DLL) { if (offset == UART16550_DLL) {
reg = vu->dll; reg = vu->dll;
goto done; goto done;
@ -245,13 +246,13 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
switch (offset) { switch (offset) {
case UART16550_RBR: case UART16550_RBR:
vu->lsr &= ~LSR_OE; vu->lsr &= ~LSR_OE;
reg = fifo_getchar(&vu->rxfifo); reg = (uint8_t)fifo_getchar(&vu->rxfifo);
break; break;
case UART16550_IER: case UART16550_IER:
reg = vu->ier; reg = vu->ier;
break; break;
case UART16550_IIR: case UART16550_IIR:
iir = ((vu->fcr & FCR_FIFOE) != 0) ? IIR_FIFO_MASK : 0; iir = ((vu->fcr & FCR_FIFOE) != 0U) ? IIR_FIFO_MASK : 0U;
intr_reason = vuart_intr_reason(vu); intr_reason = vuart_intr_reason(vu);
/* /*
* Deal with side effects of reading the IIR register * Deal with side effects of reading the IIR register
@ -272,7 +273,7 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
/* Transmitter is always ready for more data */ /* Transmitter is always ready for more data */
vu->lsr |= LSR_TEMT | LSR_THRE; vu->lsr |= LSR_TEMT | LSR_THRE;
/* Check for new receive data */ /* Check for new receive data */
if (fifo_numchars(&vu->rxfifo) > 0) { if (fifo_numchars(&vu->rxfifo) > 0U) {
vu->lsr |= LSR_DR; vu->lsr |= LSR_DR;
} else { } else {
vu->lsr &= ~LSR_DR; vu->lsr &= ~LSR_DR;
@ -283,13 +284,13 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
break; break;
case UART16550_MSR: case UART16550_MSR:
/* ignore modem I*/ /* ignore modem I*/
reg = 0; reg = 0U;
break; break;
case UART16550_SCR: case UART16550_SCR:
reg = vu->scr; reg = vu->scr;
break; break;
default: default:
reg = 0xFF; reg = 0xFFU;
break; break;
} }
done: done:
@ -315,7 +316,7 @@ static void vuart_register_io_handler(struct vm *vm)
void vuart_console_tx_chars(struct vuart *vu) void vuart_console_tx_chars(struct vuart *vu)
{ {
vuart_lock(vu); vuart_lock(vu);
while (fifo_numchars(&vu->txfifo) > 0) { while (fifo_numchars(&vu->txfifo) > 0U) {
printf("%c", fifo_getchar(&vu->txfifo)); printf("%c", fifo_getchar(&vu->txfifo));
} }
vuart_unlock(vu); vuart_unlock(vu);
@ -363,7 +364,7 @@ struct vuart *vuart_console_active(void)
void *vuart_init(struct vm *vm) void *vuart_init(struct vm *vm)
{ {
struct vuart *vu; struct vuart *vu;
uint16_t divisor; uint32_t divisor;
vu = calloc(1U, sizeof(struct vuart)); vu = calloc(1U, sizeof(struct vuart));
ASSERT(vu != NULL, ""); ASSERT(vu != NULL, "");

View File

@ -32,10 +32,10 @@
struct fifo { struct fifo {
char *buf; char *buf;
int rindex; /* index to read from */ uint32_t rindex; /* index to read from */
int windex; /* index to write to */ uint32_t windex; /* index to write to */
int num; /* number of characters in the fifo */ uint32_t num; /* number of characters in the fifo */
int size; /* size of the fifo */ uint32_t size; /* size of the fifo */
}; };
struct vuart { struct vuart {
@ -52,7 +52,7 @@ struct vuart {
struct fifo rxfifo; struct fifo rxfifo;
struct fifo txfifo; struct fifo txfifo;
int base; uint16_t base;
bool thre_int_pending; /* THRE interrupt pending */ bool thre_int_pending; /* THRE interrupt pending */
bool active; bool active;

View File

@ -366,7 +366,7 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg)
ASSERT(false); ASSERT(false);
} }
/*same memory block, no need to copy*/ /* same memory block, no need to copy */
if (d == s) { if (d == s) {
return d; return d;
} }
@ -374,28 +374,30 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg)
dest8 = (uint8_t *)d; dest8 = (uint8_t *)d;
src8 = (uint8_t *)s; src8 = (uint8_t *)s;
/*small data block*/ /* small data block */
if (slen < 8U) { if (slen < 8U) {
while (slen != 0U) { while (slen != 0U) {
*dest8 = *src8; *dest8 = *src8;
dest8++; dest8++;
src8++; src8++;
slen--; slen--;
} }
return d; return d;
} }
/*make sure 8bytes-aligned for at least one addr.*/ /* make sure 8bytes-aligned for at least one addr. */
if ((!MEM_ALIGNED_CHECK(src8, 8)) && (!MEM_ALIGNED_CHECK(dest8, 8))) { if ((!MEM_ALIGNED_CHECK(src8, 8UL)) &&
for (; (slen != 0U) && ((((uint64_t)src8) & 7UL) != 0UL); slen--) { (!MEM_ALIGNED_CHECK(dest8, 8UL))) {
*dest8 = *src8; for (; (slen != 0U) && ((((uint64_t)src8) & 7UL) != 0UL);
dest8++; slen--) {
src8++; *dest8 = *src8;
} dest8++;
} src8++;
}
}
/*copy main data blocks, with rep prefix*/ /* copy main data blocks, with rep prefix */
if (slen > 8U) { if (slen > 8U) {
uint32_t ecx; uint32_t ecx;
@ -407,13 +409,13 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg)
slen = slen % 8U; slen = slen % 8U;
} }
/*tail bytes*/ /* tail bytes */
while (slen != 0U) { while (slen != 0U) {
*dest8 = *src8; *dest8 = *src8;
dest8++; dest8++;
src8++; src8++;
slen--; slen--;
} }
return d; return d;
} }
@ -430,14 +432,14 @@ void *memset(void *base, uint8_t v, size_t n)
return NULL; return NULL;
} }
/*do the few bytes to get uint64_t alignment*/ /* do the few bytes to get uint64_t alignment */
count = n; count = n;
for (; (count != 0U) && (((uint64_t)dest_p & 7UL) != 0UL); count--) { for (; (count != 0U) && (((uint64_t)dest_p & 7UL) != 0UL); count--) {
*dest_p = v; *dest_p = v;
dest_p++; dest_p++;
} }
/*64-bit mode*/ /* 64-bit mode */
n_q = count >> 3U; n_q = count >> 3U;
asm volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb" asm volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
: "+c"(n_q), "+D"(dest_p) : "+c"(n_q), "+D"(dest_p)