Skip to content

Commit

Permalink
hw/intc: sifive_plic: Improve robustness of the PLIC config parser
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
lbmeng authored and alistair23 committed Dec 19, 2022
1 parent 45072e4 commit 9487780
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions hw/intc/sifive_plic.c
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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++;
}
}
Expand Down

0 comments on commit 9487780

Please sign in to comment.