Skip to contentSkip to conversation

Should we check arguments for function `from_bcd` and `to_bcd`?

Qemu-devel Su Hang 2 participants 3 messages Mar 22, 2018

lore
Share & tools
lore

Su Hang

author
#0 lore raw

When I was reading 'qemu/hw/timer/m48t59.c'(Line:328) and run with make check-qtest-ppc,
I found when write an invalid value 0xc to address 0x1FFF, from_bcd return 12 instead of raising an exception(or error).

"""(qemu/hw/timer/m48t59.c)

    case 0x1FFF:
    case 0x07FF:
        /* year */
    tmp = from_bcd(val);
    if (tmp >= 0 && tmp <= 99) {

"""

"""(qemu/include/qemu/bcd.h)
/* Convert a byte between binary and BCD. */
static inline uint8_t to_bcd(uint8_t val)

{
    return ((val / 10) << 4) | (val % 10);
}

static inline uint8_t from_bcd(uint8_t val)

{
    return ((val >> 4) * 10) + (val & 0x0f);
}

"""

Su Hang

#1 lore raw
Quoted 4 lines · Su Hang

When I was reading 'qemu/hw/timer/m48t59.c'(Line:328) and run with make check-qtest-ppc,
I found when write an invalid value 0xc to address 0x1FFF, from_bcd return 12 instead of raising an exception(or error).

Each device probably has a different behavior when a wrong value is written to a register that expects valid BCD. Therefore, if you want to model that, you have to fix it in hw/timer/m48t59.c, not in from_bcd and to_bcd.

However, note that anything that the guest does should never cause an assertion.

Thanks,

Paolo

Quoted 24 lines

"""(qemu/hw/timer/m48t59.c)

    case 0x1FFF:
    case 0x07FF:
        /* year */
    tmp = from_bcd(val);
    if (tmp >= 0 && tmp <= 99) {

"""

"""(qemu/include/qemu/bcd.h)
/* Convert a byte between binary and BCD. */
static inline uint8_t to_bcd(uint8_t val)

{
    return ((val / 10) << 4) | (val % 10);
}

static inline uint8_t from_bcd(uint8_t val)

{
    return ((val >> 4) * 10) + (val & 0x0f);
}

"""

Su Hang

author
#2 lore raw

Thanks for your reply. :-)

Su Hang

Quoted 43 lines · Paolo Bonzini
Quoted 4 lines · Su Hang

When I was reading 'qemu/hw/timer/m48t59.c'(Line:328) and run with make check-qtest-ppc,
I found when write an invalid value 0xc to address 0x1FFF, from_bcd return 12 instead of raising an exception(or error).

Each device probably has a different behavior when a wrong value is written to a register that expects valid BCD. Therefore, if you want to model that, you have to fix it in hw/timer/m48t59.c, not in from_bcd and to_bcd.

However, note that anything that the guest does should never cause an assertion.

Thanks,

Paolo

Quoted 24 lines

"""(qemu/hw/timer/m48t59.c)

    case 0x1FFF:
    case 0x07FF:
        /* year */
    tmp = from_bcd(val);
    if (tmp >= 0 && tmp <= 99) {

"""

"""(qemu/include/qemu/bcd.h)
/* Convert a byte between binary and BCD. */
static inline uint8_t to_bcd(uint8_t val)

{
    return ((val / 10) << 4) | (val % 10);
}

static inline uint8_t from_bcd(uint8_t val)

{
    return ((val >> 4) * 10) + (val & 0x0f);
}

"""

Su Hang