Skip to content

Commit ab02e52

Browse files
ref: Replace hardcoded booleans with named macros
This commit updates mainly memory and bitmap operations to use named macros like MEM_DONT_FREE_PAGES, MEM_ALIGN_PPAGES, and BITMAP_DONT_SET instead of raw boolean values. Signed-off-by: David Cerdeira <[email protected]>
1 parent a1daae7 commit ab02e52

File tree

14 files changed

+46
-30
lines changed

14 files changed

+46
-30
lines changed

src/arch/armv8/armv8-a/smmuv2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ ssize_t smmu_alloc_ctxbnk(void)
182182
{
183183
spin_lock(&smmu.ctx_lock);
184184
/* Find a free context bank. */
185-
ssize_t nth = bitmap_find_nth(smmu.ctxbank_bitmap, smmu.ctx_num, 1, 0, false);
185+
ssize_t nth = bitmap_find_nth(smmu.ctxbank_bitmap, smmu.ctx_num, 1, 0, BITMAP_DONT_SET);
186186
if (nth >= 0) {
187187
bitmap_set(smmu.ctxbank_bitmap, (size_t)nth);
188188
}
@@ -247,7 +247,7 @@ ssize_t smmu_alloc_sme(void)
247247
{
248248
spin_lock(&smmu.sme_lock);
249249
/* Find a free sme. */
250-
ssize_t nth = bitmap_find_nth(smmu.sme_bitmap, smmu.sme_num, 1, 0, false);
250+
ssize_t nth = bitmap_find_nth(smmu.sme_bitmap, smmu.sme_num, 1, 0, BITMAP_DONT_SET);
251251
if (nth >= 0) {
252252
bitmap_set(smmu.sme_bitmap, (size_t)nth);
253253
}

src/arch/armv8/armv8-r/vmm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void vmm_arch_profile_init()
2929

3030
timer_freq = timer_ctl->CNTDIF0;
3131

32-
mem_unmap(&cpu()->as, (vaddr_t)timer_ctl, sizeof(struct generic_timer_cntctrl), false);
32+
mem_unmap(&cpu()->as, (vaddr_t)timer_ctl, sizeof(struct generic_timer_cntctrl), MEM_DONT_FREE_PAGES);
3333
}
3434

3535
cpu_sync_barrier(&cpu_glb_sync);

src/arch/armv8/inc/arch/vgic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ struct vgic_priv {
7171
struct vgic_int interrupts[GIC_CPU_PRIV];
7272
};
7373

74+
#define VGIC_GICR_ACCESS true
75+
#define VGIC_NO_GICR_ACCESS false
76+
7477
void vgic_init(struct vm* vm, const struct vgic_dscrp* vgic_dscrp);
7578
void vgic_cpu_init(struct vcpu* vcpu);
7679
void vgic_set_hw(struct vm* vm, irqid_t id);

src/arch/armv8/vgic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ bool vgicd_emul_handler(struct emul_access* acc)
968968

969969
if (vgic_check_reg_alignment(acc, handler_info)) {
970970
spin_lock(&cpu()->vcpu->vm->arch.vgicd.lock);
971-
handler_info->reg_access(acc, handler_info, false, cpu()->vcpu->id);
971+
handler_info->reg_access(acc, handler_info, VGIC_NO_GICR_ACCESS, cpu()->vcpu->id);
972972
spin_unlock(&cpu()->vcpu->vm->arch.vgicd.lock);
973973
return true;
974974
} else {

src/arch/armv8/vgicv2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void vgic_init(struct vm* vm, const struct vgic_dscrp* vgic_dscrp)
154154
(vaddr_t)platform.arch.gic.gicv_addr, n);
155155

156156
size_t vgic_int_size = vm->arch.vgicd.int_num * sizeof(struct vgic_int);
157-
vm->arch.vgicd.interrupts = mem_alloc_page(NUM_PAGES(vgic_int_size), SEC_HYP_VM, false);
157+
vm->arch.vgicd.interrupts = mem_alloc_page(NUM_PAGES(vgic_int_size), SEC_HYP_VM, MEM_DONT_ALIGN_PPAGES);
158158
if (vm->arch.vgicd.interrupts == NULL) {
159159
ERROR("failed to alloc vgic");
160160
}

src/arch/armv8/vgicv3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static bool vgicr_emul_handler(struct emul_access* acc)
276276
struct vcpu* vcpu =
277277
vgicr_id == cpu()->vcpu->id ? cpu()->vcpu : vm_get_vcpu(cpu()->vcpu->vm, vgicr_id);
278278
spin_lock(&vcpu->arch.vgic_priv.vgicr.lock);
279-
handler_info->reg_access(acc, handler_info, true, vgicr_id);
279+
handler_info->reg_access(acc, handler_info, VGIC_GICR_ACCESS, vgicr_id);
280280
spin_unlock(&vcpu->arch.vgic_priv.vgicr.lock);
281281
return true;
282282
} else {
@@ -330,7 +330,7 @@ void vgic_init(struct vm* vm, const struct vgic_dscrp* vgic_dscrp)
330330
vm->arch.vgicd.IIDR = gicd->IIDR;
331331

332332
size_t vgic_int_size = vm->arch.vgicd.int_num * sizeof(struct vgic_int);
333-
vm->arch.vgicd.interrupts = mem_alloc_page(NUM_PAGES(vgic_int_size), SEC_HYP_VM, false);
333+
vm->arch.vgicd.interrupts = mem_alloc_page(NUM_PAGES(vgic_int_size), SEC_HYP_VM, MEM_DONT_ALIGN_PPAGES);
334334
if (vm->arch.vgicd.interrupts == NULL) {
335335
ERROR("failed to alloc vgic");
336336
}

src/core/inc/mem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ struct shmem {
4949
spinlock_t lock;
5050
};
5151

52+
#define MEM_ALIGN_PPAGES true
53+
#define MEM_DONT_ALIGN_PPAGES false
54+
55+
#define MEM_FREE_PAGES true
56+
#define MEM_DONT_FREE_PAGES false
57+
5258
static inline struct ppages mem_ppages_get(paddr_t base, size_t num_pages)
5359
{
5460
return (struct ppages){ .colors = 0, .base = base, .num_pages = num_pages };

src/core/mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool pp_alloc(struct page_pool* pool, size_t num_pages, bool aligned, struct ppa
4444
*/
4545
for (size_t i = 0; i < 2 && !ok; i++) {
4646
while (pool->free != 0) {
47-
ssize_t bit = bitmap_find_consec(pool->bitmap, pool->size, curr, num_pages, false);
47+
ssize_t bit = bitmap_find_consec(pool->bitmap, pool->size, curr, num_pages, BITMAP_DONT_SET);
4848

4949
if (bit < 0) {
5050
/**
@@ -215,7 +215,7 @@ static void pp_init(struct page_pool* pool, paddr_t base, size_t size)
215215
return;
216216
}
217217

218-
pages = mem_alloc_ppages(cpu()->as.colors, bitmap_size, false);
218+
pages = mem_alloc_ppages(cpu()->as.colors, bitmap_size, MEM_DONT_ALIGN_PPAGES);
219219
if (pages.num_pages != bitmap_size) {
220220
return;
221221
}

src/core/mmu/mem.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <tlb.h>
1717
#include <config.h>
1818

19+
1920
extern uint8_t _image_start, _image_load_end, _image_end, _dmem_phys_beg, _dmem_beg,
2021
_cpu_private_beg, _cpu_private_end, _vm_beg, _vm_end, _vm_image_start, _vm_image_end;
2122

@@ -189,7 +190,7 @@ static inline pte_t* mem_alloc_pt(struct addr_space* as, pte_t* parent, size_t l
189190
{
190191
/* Must have lock on as and va section to call */
191192
size_t ptsize = NUM_PAGES(pt_size(&as->pt, lvl + 1));
192-
struct ppages ppage = mem_alloc_ppages(as->colors, ptsize, ptsize > 1 ? true : false);
193+
struct ppages ppage = mem_alloc_ppages(as->colors, ptsize, ptsize > 1 ? MEM_ALIGN_PPAGES : MEM_DONT_ALIGN_PPAGES);
193194
if (ppage.num_pages == 0) {
194195
return NULL;
195196
}
@@ -547,7 +548,7 @@ static bool mem_map(struct addr_space* as, vaddr_t va, struct ppages* ppages, si
547548
while ((entry < nentries) && (count < num_pages) &&
548549
(num_pages - count >= lvlsz / PAGE_SIZE)) {
549550
if (ppages == NULL) {
550-
struct ppages temp = mem_alloc_ppages(as->colors, lvlsz / PAGE_SIZE, true);
551+
struct ppages temp = mem_alloc_ppages(as->colors, lvlsz / PAGE_SIZE, MEM_ALIGN_PPAGES);
551552
if (temp.num_pages < lvlsz / PAGE_SIZE) {
552553
if (lvl == (as->pt.dscr->lvls - 1)) {
553554
// TODO: free previously allocated pages
@@ -671,8 +672,8 @@ bool mem_map_reclr(struct addr_space* as, vaddr_t va, struct ppages* ppages, siz
671672
.colors = ~as->colors };
672673
mem_free_ppages(&unused_pages);
673674

674-
mem_unmap(&cpu()->as, reclrd_va_base, reclrd_num, false);
675-
mem_unmap(&cpu()->as, phys_va_base, num_pages, false);
675+
mem_unmap(&cpu()->as, reclrd_va_base, reclrd_num, MEM_DONT_FREE_PAGES);
676+
mem_unmap(&cpu()->as, phys_va_base, num_pages, MEM_DONT_FREE_PAGES);
676677

677678
return true;
678679
}
@@ -712,7 +713,7 @@ vaddr_t mem_map_cpy(struct addr_space* ass, struct addr_space* asd, vaddr_t vas,
712713

713714
static void* copy_space(void* base, const size_t size, struct ppages* pages)
714715
{
715-
*pages = mem_alloc_ppages(cpu()->as.colors, NUM_PAGES(size), false);
716+
*pages = mem_alloc_ppages(cpu()->as.colors, NUM_PAGES(size), MEM_DONT_ALIGN_PPAGES);
716717
vaddr_t va = mem_alloc_vpage(&cpu()->as, SEC_HYP_PRIVATE, INVALID_VA, NUM_PAGES(size));
717718
mem_map(&cpu()->as, va, pages, NUM_PAGES(size), PTE_HYP_FLAGS);
718719
memcpy((void*)va, base, size);
@@ -873,14 +874,14 @@ void mem_color_hypervisor(const paddr_t load_addr, struct mem_region* root_regio
873874
va = mem_alloc_vpage(&cpu()->as, SEC_HYP_GLOBAL, INVALID_VA, p_image.num_pages);
874875
mem_map(&cpu()->as, va, &p_image, p_image.num_pages, PTE_HYP_FLAGS);
875876
memset((void*)va, 0, p_image.num_pages * PAGE_SIZE);
876-
mem_unmap(&cpu()->as, va, p_image.num_pages, true);
877+
mem_unmap(&cpu()->as, va, p_image.num_pages, MEM_FREE_PAGES);
877878

878879
p_image = mem_ppages_get(load_addr + image_load_size + vm_image_size,
879880
NUM_PAGES(image_noload_size));
880881
va = mem_alloc_vpage(&cpu()->as, SEC_HYP_GLOBAL, INVALID_VA, p_image.num_pages);
881882
mem_map(&cpu()->as, va, &p_image, p_image.num_pages, PTE_HYP_FLAGS);
882883
memset((void*)va, 0, p_image.num_pages * PAGE_SIZE);
883-
mem_unmap(&cpu()->as, va, p_image.num_pages, true);
884+
mem_unmap(&cpu()->as, va, p_image.num_pages, MEM_FREE_PAGES);
884885

885886
p_bitmap = mem_ppages_get(load_addr + image_size + vm_image_size +
886887
(cpu_boot_size * platform.cpu_num),
@@ -889,15 +890,15 @@ void mem_color_hypervisor(const paddr_t load_addr, struct mem_region* root_regio
889890
va = mem_alloc_vpage(&cpu()->as, SEC_HYP_GLOBAL, INVALID_VA, p_bitmap.num_pages);
890891
mem_map(&cpu()->as, va, &p_bitmap, p_bitmap.num_pages, PTE_HYP_FLAGS);
891892
memset((void*)va, 0, p_bitmap.num_pages * PAGE_SIZE);
892-
mem_unmap(&cpu()->as, va, p_bitmap.num_pages, true);
893+
mem_unmap(&cpu()->as, va, p_bitmap.num_pages, MEM_FREE_PAGES);
893894
}
894895

895896
p_cpu = mem_ppages_get(load_addr + image_size + vm_image_size + (cpu_boot_size * cpu()->id),
896897
cpu_boot_size / PAGE_SIZE);
897898
va = mem_alloc_vpage(&cpu()->as, SEC_HYP_PRIVATE, INVALID_VA, p_cpu.num_pages);
898899
mem_map(&cpu()->as, va, &p_cpu, p_cpu.num_pages, PTE_HYP_FLAGS);
899900
memset((void*)va, 0, p_cpu.num_pages * PAGE_SIZE);
900-
mem_unmap(&cpu()->as, va, p_cpu.num_pages, false);
901+
mem_unmap(&cpu()->as, va, p_cpu.num_pages, MEM_DONT_FREE_PAGES);
901902
}
902903

903904
void as_init(struct addr_space* as, enum AS_TYPE type, asid_t id, pte_t* root_pt, colormap_t colors)

src/core/mpu/mem.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <objpool.h>
1313
#include <config.h>
1414

15+
#define MEM_BROADCAST true
16+
#define MEM_DONT_BROADCAST false
17+
1518
struct shared_region {
1619
enum AS_TYPE as_type;
1720
asid_t asid;
@@ -309,7 +312,7 @@ static bool mem_vmpu_remove_region(struct addr_space* as, mpid_t mpid, bool broa
309312
static void mem_handle_broadcast_insert(struct addr_space* as, struct mp_region* mpr)
310313
{
311314
if (as->type == AS_HYP) {
312-
mem_map(&cpu()->as, mpr, false);
315+
mem_map(&cpu()->as, mpr, MEM_DONT_BROADCAST);
313316
} else {
314317
mpu_map(as_priv(as), mpr);
315318
}
@@ -318,7 +321,7 @@ static void mem_handle_broadcast_insert(struct addr_space* as, struct mp_region*
318321
static void mem_handle_broadcast_remove(struct addr_space* as, struct mp_region* mpr)
319322
{
320323
if (as->type == AS_HYP) {
321-
mem_unmap_range(&cpu()->as, mpr->base, mpr->size, false);
324+
mem_unmap_range(&cpu()->as, mpr->base, mpr->size, MEM_DONT_BROADCAST);
322325
} else {
323326
mpu_unmap(as_priv(as), mpr);
324327
}
@@ -435,21 +438,21 @@ bool mem_unmap_range(struct addr_space* as, vaddr_t vaddr, size_t size, bool bro
435438
size_t top_size = limit >= r_limit ? 0 : r_limit - limit;
436439
size_t bottom_size = vaddr <= r_base ? 0 : vaddr - r_base;
437440

438-
mem_vmpu_remove_region(as, mpid, true);
441+
mem_vmpu_remove_region(as, mpid, MEM_BROADCAST);
439442

440443
if (top_size > 0) {
441444
struct mp_region top = reg;
442445
top.base = limit;
443446
top.size = top_size;
444447
mpid_t top_mpid = mem_vmpu_allocate_entry(as);
445-
mem_vmpu_insert_region(as, top_mpid, &top, true);
448+
mem_vmpu_insert_region(as, top_mpid, &top, MEM_BROADCAST);
446449
}
447450

448451
if (bottom_size > 0) {
449452
struct mp_region bottom = reg;
450453
bottom.size = bottom_size;
451454
mpid_t bottom_mpid = mem_vmpu_allocate_entry(as);
452-
mem_vmpu_insert_region(as, bottom_mpid, &bottom, true);
455+
mem_vmpu_insert_region(as, bottom_mpid, &bottom, MEM_BROADCAST);
453456
}
454457

455458
size_t overlap_size = reg.size - top_size - bottom_size;
@@ -463,7 +466,7 @@ bool mem_unmap_range(struct addr_space* as, vaddr_t vaddr, size_t size, bool bro
463466

464467
void mem_unmap(struct addr_space* as, vaddr_t at, size_t num_pages, bool free_ppages)
465468
{
466-
if (mem_unmap_range(as, at, num_pages * PAGE_SIZE, true) && free_ppages) {
469+
if (mem_unmap_range(as, at, num_pages * PAGE_SIZE, MEM_BROADCAST) && free_ppages) {
467470
struct ppages ppages = mem_ppages_get(at, num_pages);
468471
mem_free_ppages(&ppages);
469472
}
@@ -490,7 +493,7 @@ vaddr_t mem_map_cpy(struct addr_space* ass, struct addr_space* asd, vaddr_t vas,
490493
mpr = mpe->region;
491494
spin_unlock(&ass->lock);
492495

493-
if (mem_map(asd, &mpr, true)) {
496+
if (mem_map(asd, &mpr, MEM_BROADCAST)) {
494497
va_res = vas;
495498
} else {
496499
INFO("failed mem map on mem map cpy");
@@ -542,7 +545,7 @@ vaddr_t mem_alloc_map(struct addr_space* as, as_sec_t section, struct ppages* pp
542545
.mem_flags = flags,
543546
};
544547

545-
mem_map(as, &mpr, true);
548+
mem_map(as, &mpr, MEM_BROADCAST);
546549

547550
return at;
548551
}

0 commit comments

Comments
 (0)