Skip to content

Commit 40745d9

Browse files
shiqingglijinxia
authored andcommitted
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>
1 parent d82a86e commit 40745d9

File tree

4 files changed

+68
-64
lines changed

4 files changed

+68
-64
lines changed

hypervisor/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ include scripts/kconfig/kconfig.mk
3737
CFLAGS += -Wall -W
3838
CFLAGS += -ffunction-sections -fdata-sections
3939
CFLAGS += -fshort-wchar -ffreestanding
40+
CFLAGS += -fsigned-char
4041
CFLAGS += -m64 -mno-mmx -mno-sse -mno-sse2 -mno-80387 -mno-fp-ret-in-387
4142
CFLAGS += -mno-red-zone
4243
CFLAGS += -nostdinc -nostdlib -fno-common

hypervisor/debug/vuart.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
#include "uart16550.h"
3434

35-
#define COM1_BASE 0x3F8
36-
#define COM1_IRQ 4U
37-
#define RX_FIFO_SIZE 256
38-
#define TX_FIFO_SIZE 65536
35+
#define COM1_BASE 0x3F8U
36+
#define COM1_IRQ 4U
37+
#define RX_FIFO_SIZE 256U
38+
#define TX_FIFO_SIZE 65536U
3939

4040
#define vuart_lock_init(vu) spinlock_init(&((vu)->lock))
4141
#define vuart_lock(vu) spinlock_obtain(&((vu)->lock))
@@ -45,12 +45,12 @@
4545

4646
static void fifo_reset(struct fifo *fifo)
4747
{
48-
fifo->rindex = 0;
49-
fifo->windex = 0;
50-
fifo->num = 0;
48+
fifo->rindex = 0U;
49+
fifo->windex = 0U;
50+
fifo->num = 0U;
5151
}
5252

53-
static void fifo_init(struct fifo *fifo, int sz)
53+
static void fifo_init(struct fifo *fifo, uint32_t sz)
5454
{
5555
fifo->buf = calloc(1U, sz);
5656
ASSERT(fifo->buf != NULL, "");
@@ -62,29 +62,29 @@ static void fifo_putchar(struct fifo *fifo, char ch)
6262
{
6363
fifo->buf[fifo->windex] = ch;
6464
if (fifo->num < fifo->size) {
65-
fifo->windex = (fifo->windex + 1) % fifo->size;
65+
fifo->windex = (fifo->windex + 1U) % fifo->size;
6666
fifo->num++;
6767
} else {
68-
fifo->rindex = (fifo->rindex + 1) % fifo->size;
69-
fifo->windex = (fifo->windex + 1) % fifo->size;
68+
fifo->rindex = (fifo->rindex + 1U) % fifo->size;
69+
fifo->windex = (fifo->windex + 1U) % fifo->size;
7070
}
7171
}
7272

7373
static char fifo_getchar(struct fifo *fifo)
7474
{
7575
char c;
7676

77-
if (fifo->num > 0) {
77+
if (fifo->num > 0U) {
7878
c = fifo->buf[fifo->rindex];
79-
fifo->rindex = (fifo->rindex + 1) % fifo->size;
79+
fifo->rindex = (fifo->rindex + 1U) % fifo->size;
8080
fifo->num--;
8181
return c;
8282
} else {
8383
return -1;
8484
}
8585
}
8686

87-
static int fifo_numchars(struct fifo *fifo)
87+
static uint32_t fifo_numchars(struct fifo *fifo)
8888
{
8989
return fifo->num;
9090
}
@@ -98,11 +98,12 @@ static int fifo_numchars(struct fifo *fifo)
9898
*/
9999
static uint8_t vuart_intr_reason(struct vuart *vu)
100100
{
101-
if (((vu->lsr & LSR_OE) != 0) && ((vu->ier & IER_ELSI) != 0)) {
101+
if (((vu->lsr & LSR_OE) != 0U) && ((vu->ier & IER_ELSI) != 0U)) {
102102
return IIR_RLS;
103-
} else if ((fifo_numchars(&vu->rxfifo) > 0) && ((vu->ier & IER_ERBFI) != 0)) {
103+
} else if ((fifo_numchars(&vu->rxfifo) > 0U) &&
104+
((vu->ier & IER_ERBFI) != 0U)) {
104105
return IIR_RXTOUT;
105-
} else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0)) {
106+
} else if (vu->thre_int_pending && ((vu->ier & IER_ETBEI) != 0U)) {
106107
return IIR_TXRDY;
107108
} else {
108109
return IIR_NOPEND;
@@ -145,7 +146,7 @@ static void vuart_write(__unused struct vm_io_handler *hdlr, struct vm *vm,
145146
/*
146147
* Take care of the special case DLAB accesses first
147148
*/
148-
if ((vu->lcr & LCR_DLAB) != 0) {
149+
if ((vu->lcr & LCR_DLAB) != 0U) {
149150
if (offset == UART16550_DLL) {
150151
vu->dll = value_u8;
151152
goto done;
@@ -159,7 +160,7 @@ static void vuart_write(__unused struct vm_io_handler *hdlr, struct vm *vm,
159160

160161
switch (offset) {
161162
case UART16550_THR:
162-
fifo_putchar(&vu->txfifo, value_u8);
163+
fifo_putchar(&vu->txfifo, (char)value_u8);
163164
vu->thre_int_pending = true;
164165
break;
165166
case UART16550_IER:
@@ -231,7 +232,7 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
231232
/*
232233
* Take care of the special case DLAB accesses first
233234
*/
234-
if ((vu->lcr & LCR_DLAB) != 0) {
235+
if ((vu->lcr & LCR_DLAB) != 0U) {
235236
if (offset == UART16550_DLL) {
236237
reg = vu->dll;
237238
goto done;
@@ -245,13 +246,13 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
245246
switch (offset) {
246247
case UART16550_RBR:
247248
vu->lsr &= ~LSR_OE;
248-
reg = fifo_getchar(&vu->rxfifo);
249+
reg = (uint8_t)fifo_getchar(&vu->rxfifo);
249250
break;
250251
case UART16550_IER:
251252
reg = vu->ier;
252253
break;
253254
case UART16550_IIR:
254-
iir = ((vu->fcr & FCR_FIFOE) != 0) ? IIR_FIFO_MASK : 0;
255+
iir = ((vu->fcr & FCR_FIFOE) != 0U) ? IIR_FIFO_MASK : 0U;
255256
intr_reason = vuart_intr_reason(vu);
256257
/*
257258
* 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,
272273
/* Transmitter is always ready for more data */
273274
vu->lsr |= LSR_TEMT | LSR_THRE;
274275
/* Check for new receive data */
275-
if (fifo_numchars(&vu->rxfifo) > 0) {
276+
if (fifo_numchars(&vu->rxfifo) > 0U) {
276277
vu->lsr |= LSR_DR;
277278
} else {
278279
vu->lsr &= ~LSR_DR;
@@ -283,13 +284,13 @@ static uint32_t vuart_read(__unused struct vm_io_handler *hdlr, struct vm *vm,
283284
break;
284285
case UART16550_MSR:
285286
/* ignore modem I*/
286-
reg = 0;
287+
reg = 0U;
287288
break;
288289
case UART16550_SCR:
289290
reg = vu->scr;
290291
break;
291292
default:
292-
reg = 0xFF;
293+
reg = 0xFFU;
293294
break;
294295
}
295296
done:
@@ -315,7 +316,7 @@ static void vuart_register_io_handler(struct vm *vm)
315316
void vuart_console_tx_chars(struct vuart *vu)
316317
{
317318
vuart_lock(vu);
318-
while (fifo_numchars(&vu->txfifo) > 0) {
319+
while (fifo_numchars(&vu->txfifo) > 0U) {
319320
printf("%c", fifo_getchar(&vu->txfifo));
320321
}
321322
vuart_unlock(vu);
@@ -363,7 +364,7 @@ struct vuart *vuart_console_active(void)
363364
void *vuart_init(struct vm *vm)
364365
{
365366
struct vuart *vu;
366-
uint16_t divisor;
367+
uint32_t divisor;
367368

368369
vu = calloc(1U, sizeof(struct vuart));
369370
ASSERT(vu != NULL, "");

hypervisor/include/debug/vuart.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
struct fifo {
3434
char *buf;
35-
int rindex; /* index to read from */
36-
int windex; /* index to write to */
37-
int num; /* number of characters in the fifo */
38-
int size; /* size of the fifo */
35+
uint32_t rindex; /* index to read from */
36+
uint32_t windex; /* index to write to */
37+
uint32_t num; /* number of characters in the fifo */
38+
uint32_t size; /* size of the fifo */
3939
};
4040

4141
struct vuart {
@@ -52,7 +52,7 @@ struct vuart {
5252

5353
struct fifo rxfifo;
5454
struct fifo txfifo;
55-
int base;
55+
uint16_t base;
5656

5757
bool thre_int_pending; /* THRE interrupt pending */
5858
bool active;

hypervisor/lib/memory.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -366,36 +366,38 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg)
366366
ASSERT(false);
367367
}
368368

369-
/*same memory block, no need to copy*/
369+
/* same memory block, no need to copy */
370370
if (d == s) {
371371
return d;
372372
}
373373

374374
dest8 = (uint8_t *)d;
375375
src8 = (uint8_t *)s;
376376

377-
/*small data block*/
378-
if (slen < 8U) {
379-
while (slen != 0U) {
380-
*dest8 = *src8;
381-
dest8++;
382-
src8++;
383-
slen--;
384-
}
377+
/* small data block */
378+
if (slen < 8U) {
379+
while (slen != 0U) {
380+
*dest8 = *src8;
381+
dest8++;
382+
src8++;
383+
slen--;
384+
}
385385

386386
return d;
387387
}
388388

389-
/*make sure 8bytes-aligned for at least one addr.*/
390-
if ((!MEM_ALIGNED_CHECK(src8, 8)) && (!MEM_ALIGNED_CHECK(dest8, 8))) {
391-
for (; (slen != 0U) && ((((uint64_t)src8) & 7UL) != 0UL); slen--) {
392-
*dest8 = *src8;
393-
dest8++;
394-
src8++;
395-
}
396-
}
389+
/* make sure 8bytes-aligned for at least one addr. */
390+
if ((!MEM_ALIGNED_CHECK(src8, 8UL)) &&
391+
(!MEM_ALIGNED_CHECK(dest8, 8UL))) {
392+
for (; (slen != 0U) && ((((uint64_t)src8) & 7UL) != 0UL);
393+
slen--) {
394+
*dest8 = *src8;
395+
dest8++;
396+
src8++;
397+
}
398+
}
397399

398-
/*copy main data blocks, with rep prefix*/
400+
/* copy main data blocks, with rep prefix */
399401
if (slen > 8U) {
400402
uint32_t ecx;
401403

@@ -407,13 +409,13 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen_arg)
407409
slen = slen % 8U;
408410
}
409411

410-
/*tail bytes*/
411-
while (slen != 0U) {
412-
*dest8 = *src8;
413-
dest8++;
414-
src8++;
415-
slen--;
416-
}
412+
/* tail bytes */
413+
while (slen != 0U) {
414+
*dest8 = *src8;
415+
dest8++;
416+
src8++;
417+
slen--;
418+
}
417419

418420
return d;
419421
}
@@ -430,14 +432,14 @@ void *memset(void *base, uint8_t v, size_t n)
430432
return NULL;
431433
}
432434

433-
/*do the few bytes to get uint64_t alignment*/
434-
count = n;
435-
for (; (count != 0U) && (((uint64_t)dest_p & 7UL) != 0UL); count--) {
436-
*dest_p = v;
437-
dest_p++;
438-
}
435+
/* do the few bytes to get uint64_t alignment */
436+
count = n;
437+
for (; (count != 0U) && (((uint64_t)dest_p & 7UL) != 0UL); count--) {
438+
*dest_p = v;
439+
dest_p++;
440+
}
439441

440-
/*64-bit mode*/
442+
/* 64-bit mode */
441443
n_q = count >> 3U;
442444
asm volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
443445
: "+c"(n_q), "+D"(dest_p)

0 commit comments

Comments
 (0)