From 99ed01e3a92f3075ea221a0fcbba6d2c140d4120 Mon Sep 17 00:00:00 2001 From: Axel Heider Date: Fri, 12 Apr 2024 17:13:34 +0200 Subject: [PATCH 1/4] libsel4allocman: simplify code Signed-off-by: Axel Heider --- libsel4allocman/src/bootstrap.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/libsel4allocman/src/bootstrap.c b/libsel4allocman/src/bootstrap.c index e78fed9e2..2d16611e1 100644 --- a/libsel4allocman/src/bootstrap.c +++ b/libsel4allocman/src/bootstrap.c @@ -1119,15 +1119,14 @@ int allocman_add_simple_untypeds_with_regions(allocman_t *alloc, simple_t *simpl uintptr_t paddr; bool device; cspacepath_t path = allocman_cspace_make_path(alloc, simple_get_nth_untyped(simple, i, &size_bits, &paddr, &device)); - int dev_type = device ? ALLOCMAN_UT_DEV : ALLOCMAN_UT_KERNEL; - // If it is regular UT memory, then we add cap and move on. - if (dev_type == ALLOCMAN_UT_KERNEL) { - error = allocman_utspace_add_uts(alloc, 1, &path, &size_bits, &paddr, dev_type); - ZF_LOGF_IF(error, "Could not add kernel untyped."); - } else { - // Otherwise we are Device untyped. + if (device) { + // Separates device RAM memory into separate untyped caps error = handle_device_untyped_cap(state, paddr, size_bits, &path, alloc); - ZF_LOGF_IF(error, "bootstrap_arch_handle_device_untyped_cap failed."); + ZF_LOGF_IF(error, "handle_device_untyped_cap failed (%d).", error); + } else { + // for regular UT memory we add cap + error = allocman_utspace_add_uts(alloc, 1, &path, &size_bits, &paddr, ALLOCMAN_UT_KERNEL); + ZF_LOGF_IF(error, "Could not add kernel untyped (%d).", error); } } if (state) { From 55b4963e3d495cd94e3fdbbd8b3ffd33b5627ec3 Mon Sep 17 00:00:00 2001 From: Axel Heider Date: Fri, 12 Apr 2024 17:20:29 +0200 Subject: [PATCH 2/4] libsel4allocman: add error check The signature allows returning an error, so this must be checked. Signed-off-by: Axel Heider --- libsel4allocman/src/bootstrap.c | 15 +++++++++++++-- libsel4simple/src/simple.c | 7 ++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libsel4allocman/src/bootstrap.c b/libsel4allocman/src/bootstrap.c index 2d16611e1..d4dbc4655 100644 --- a/libsel4allocman/src/bootstrap.c +++ b/libsel4allocman/src/bootstrap.c @@ -233,7 +233,14 @@ static int bootstrap_add_untypeds_from_simple(bootstrap_info_t *bs, simple_t *si if (!bs->have_boot_cspace || (bs->uts && !bs->uts_in_current_cspace)) { return 1; } - for (i = 0; i < simple_get_untyped_count(simple); i++) { + + int cnt = simple_get_untyped_count(simple); + if (cnt < 0) { + ZF_LOGW("Could not get untyped count (%d)", cnt); + return 1; + } + + for (i = 0; i < cnt; i++) { size_t size_bits; uintptr_t paddr; bool device; @@ -1112,7 +1119,11 @@ int allocman_add_simple_untypeds_with_regions(allocman_t *alloc, simple_t *simpl ZF_LOGF_IF(error, "bootstrap_prepare_handle_device_untyped_cap Failed"); size_t i; - size_t total_untyped = simple_get_untyped_count(simple); + int total_untyped = simple_get_untyped_count(simple); + if (total_untyped < 0) { + ZF_LOGW("Could not get untyped count (%d)", total_untyped); + return 0; /* don't report an error, just do nothing */ + } for(i = 0; i < total_untyped; i++) { size_t size_bits; diff --git a/libsel4simple/src/simple.c b/libsel4simple/src/simple.c index d280e6e4d..34f087205 100644 --- a/libsel4simple/src/simple.c +++ b/libsel4simple/src/simple.c @@ -9,8 +9,13 @@ bool simple_is_untyped_cap(simple_t *simple, seL4_CPtr pos) { int i; + int cnt = simple_get_untyped_count(simple); + if (cnt < 0) { + ZF_LOGW("Could not get untyped count (%d)", cnt); + return false; + } - for (i = 0; i < simple_get_untyped_count(simple); i++) { + for (i = 0; i < cnt; i++) { seL4_CPtr ut_pos = simple_get_nth_untyped(simple, i, NULL, NULL, NULL); if (ut_pos == pos) { return true; From 96cb46f1bb4e091e110648cc78457c4e95581ac6 Mon Sep 17 00:00:00 2001 From: Axel Heider Date: Fri, 3 Nov 2023 16:15:35 +0100 Subject: [PATCH 3/4] libsel4allocman: inline loop variable Signed-off-by: Axel Heider --- libsel4allocman/src/bootstrap.c | 6 ++---- libsel4simple/src/simple.c | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/libsel4allocman/src/bootstrap.c b/libsel4allocman/src/bootstrap.c index d4dbc4655..81bcf5472 100644 --- a/libsel4allocman/src/bootstrap.c +++ b/libsel4allocman/src/bootstrap.c @@ -227,7 +227,6 @@ int bootstrap_add_untypeds_from_bootinfo(bootstrap_info_t *bs, seL4_BootInfo *bi static int bootstrap_add_untypeds_from_simple(bootstrap_info_t *bs, simple_t *simple) { int error; - int i; /* if we do not have a boot cspace, or we have added some uts that aren't in the * current space then just bail */ if (!bs->have_boot_cspace || (bs->uts && !bs->uts_in_current_cspace)) { @@ -240,7 +239,7 @@ static int bootstrap_add_untypeds_from_simple(bootstrap_info_t *bs, simple_t *si return 1; } - for (i = 0; i < cnt; i++) { + for (int i = 0; i < cnt; i++) { size_t size_bits; uintptr_t paddr; bool device; @@ -1118,14 +1117,13 @@ int allocman_add_simple_untypeds_with_regions(allocman_t *alloc, simple_t *simpl int error = prepare_handle_device_untyped_cap(alloc, simple, &state, num_regions, region_list); ZF_LOGF_IF(error, "bootstrap_prepare_handle_device_untyped_cap Failed"); - size_t i; int total_untyped = simple_get_untyped_count(simple); if (total_untyped < 0) { ZF_LOGW("Could not get untyped count (%d)", total_untyped); return 0; /* don't report an error, just do nothing */ } - for(i = 0; i < total_untyped; i++) { + for(int i = 0; i < total_untyped; i++) { size_t size_bits; uintptr_t paddr; bool device; diff --git a/libsel4simple/src/simple.c b/libsel4simple/src/simple.c index 34f087205..886db7338 100644 --- a/libsel4simple/src/simple.c +++ b/libsel4simple/src/simple.c @@ -8,14 +8,13 @@ bool simple_is_untyped_cap(simple_t *simple, seL4_CPtr pos) { - int i; int cnt = simple_get_untyped_count(simple); if (cnt < 0) { ZF_LOGW("Could not get untyped count (%d)", cnt); return false; } - for (i = 0; i < cnt; i++) { + for (int i = 0; i < cnt; i++) { seL4_CPtr ut_pos = simple_get_nth_untyped(simple, i, NULL, NULL, NULL); if (ut_pos == pos) { return true; From 070911a2412bf641bb2933c1d2ae9c31754029c5 Mon Sep 17 00:00:00 2001 From: Axel Heider Date: Fri, 12 Apr 2024 17:13:46 +0200 Subject: [PATCH 4/4] libsel4allocman: cleanup Signed-off-by: Axel Heider --- libsel4allocman/src/bootstrap.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libsel4allocman/src/bootstrap.c b/libsel4allocman/src/bootstrap.c index 81bcf5472..bde6a2067 100644 --- a/libsel4allocman/src/bootstrap.c +++ b/libsel4allocman/src/bootstrap.c @@ -1011,10 +1011,7 @@ static int handle_device_untyped_cap(add_untypeds_state_t *state, uintptr_t padd bool cap_tainted = false; int error; uintptr_t ut_end = paddr + BIT(size_bits); - int num_regions = 0; - if (state != NULL) { - num_regions = state->num_regions; - } + int num_regions = state ? state->num_regions : 0; for (int i = 0; i < num_regions; i++) { pmem_region_t *region = &state->regions[i]; uint64_t region_end = region->base_addr + region->length;