hw/intc: sifive_plic: Improve robustness of the PLIC config parser

At present the PLIC config parser can only handle legal config string
like "MS,MS". However if a config string like ",MS,MS,,MS,MS,," is
given the parser won't get the correct configuration.

This commit improves the config parser to make it more robust.

Signed-off-by: Bin Meng <bmeng@tinylab.org>
Acked-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20221211030829.802437-7-bmeng@tinylab.org>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
Bin Meng 2022-12-11 11:08:20 +08:00 committed by Alistair Francis
parent 2904dc1c1e
commit 7b0f26e420

View File

@ -290,7 +290,7 @@ static void sifive_plic_reset(DeviceState *dev)
*/
static void parse_hart_config(SiFivePLICState *plic)
{
int addrid, hartid, modes;
int addrid, hartid, modes, m;
const char *p;
char c;
@ -299,11 +299,13 @@ static void parse_hart_config(SiFivePLICState *plic)
p = plic->hart_config;
while ((c = *p++)) {
if (c == ',') {
addrid += ctpop8(modes);
modes = 0;
hartid++;
if (modes) {
addrid += ctpop8(modes);
hartid++;
modes = 0;
}
} else {
int m = 1 << char_to_mode(c);
m = 1 << char_to_mode(c);
if (modes == (modes | m)) {
error_report("plic: duplicate mode '%c' in config: %s",
c, plic->hart_config);
@ -314,8 +316,9 @@ static void parse_hart_config(SiFivePLICState *plic)
}
if (modes) {
addrid += ctpop8(modes);
hartid++;
modes = 0;
}
hartid++;
plic->num_addrs = addrid;
plic->num_harts = hartid;
@ -326,11 +329,16 @@ static void parse_hart_config(SiFivePLICState *plic)
p = plic->hart_config;
while ((c = *p++)) {
if (c == ',') {
hartid++;
if (modes) {
hartid++;
modes = 0;
}
} else {
m = char_to_mode(c);
plic->addr_config[addrid].addrid = addrid;
plic->addr_config[addrid].hartid = hartid;
plic->addr_config[addrid].mode = char_to_mode(c);
plic->addr_config[addrid].mode = m;
modes |= (1 << m);
addrid++;
}
}