misc: Forbid variable shadowing

This commit is contained in:
mintsuki 2021-07-06 05:17:18 +02:00
parent 04d713a45b
commit 4a87babe4a
8 changed files with 78 additions and 73 deletions

View File

@ -59,6 +59,7 @@ INTERNAL_CFLAGS := \
-fno-lto \ -fno-lto \
-fno-pic \ -fno-pic \
-Wno-address-of-packed-member \ -Wno-address-of-packed-member \
-Wshadow \
-mno-80387 \ -mno-80387 \
-mno-mmx \ -mno-mmx \
-mno-3dnow \ -mno-3dnow \

View File

@ -206,8 +206,8 @@ static bool fat32_filename_to_8_3(char *dest, const char *src) {
int i = 0, j = 0; int i = 0, j = 0;
bool ext = false; bool ext = false;
for (size_t i = 0; i < 8+3; i++) for (size_t k = 0; k < 8+3; k++)
dest[i] = ' '; dest[k] = ' ';
while (src[i]) { while (src[i]) {
if (src[i] == '.') { if (src[i] == '.') {

View File

@ -19,7 +19,7 @@ extern symbol bss_end;
#endif #endif
static bool allocations_disallowed = true; static bool allocations_disallowed = true;
static void sanitise_entries(bool align_entries); static void sanitise_entries(struct e820_entry_t *, size_t *, bool);
void *conv_mem_alloc(size_t count) { void *conv_mem_alloc(size_t count) {
static uintptr_t base = 4096; static uintptr_t base = 4096;
@ -39,7 +39,7 @@ void *conv_mem_alloc(size_t count) {
memset(ret, 0, count); memset(ret, 0, count);
base += count; base += count;
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
return ret; return ret;
} }
@ -101,22 +101,24 @@ static bool align_entry(uint64_t *base, uint64_t *length) {
return true; return true;
} }
static void sanitise_entries(bool align_entries) { static void sanitise_entries(struct e820_entry_t *m, size_t *_count, bool align_entries) {
size_t count = *_count;
for (size_t i = 0; i < memmap_entries; i++) { for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != MEMMAP_USABLE) if (m[i].type != MEMMAP_USABLE)
continue; continue;
// Check if the entry overlaps other entries // Check if the entry overlaps other entries
for (size_t j = 0; j < memmap_entries; j++) { for (size_t j = 0; j < count; j++) {
if (j == i) if (j == i)
continue; continue;
uint64_t base = memmap[i].base; uint64_t base = m[i].base;
uint64_t length = memmap[i].length; uint64_t length = m[i].length;
uint64_t top = base + length; uint64_t top = base + length;
uint64_t res_base = memmap[j].base; uint64_t res_base = m[j].base;
uint64_t res_length = memmap[j].length; uint64_t res_length = m[j].length;
uint64_t res_top = res_base + res_length; uint64_t res_top = res_base + res_length;
// TODO actually handle splitting off usable chunks // TODO actually handle splitting off usable chunks
@ -133,58 +135,60 @@ static void sanitise_entries(bool align_entries) {
base = res_top; base = res_top;
} }
memmap[i].base = base; m[i].base = base;
memmap[i].length = top - base; m[i].length = top - base;
} }
if (!memmap[i].length if (!m[i].length
|| (align_entries && !align_entry(&memmap[i].base, &memmap[i].length))) { || (align_entries && !align_entry(&m[i].base, &m[i].length))) {
// Eradicate from memmap // Eradicate from memmap
for (size_t j = i; j < memmap_entries - 1; j++) { for (size_t j = i; j < count - 1; j++) {
memmap[j] = memmap[j+1]; m[j] = m[j+1];
} }
memmap_entries--; count--;
i--; i--;
} }
} }
// Sort the entries // Sort the entries
for (size_t p = 0; p < memmap_entries - 1; p++) { for (size_t p = 0; p < count - 1; p++) {
uint64_t min = memmap[p].base; uint64_t min = m[p].base;
size_t min_index = p; size_t min_index = p;
for (size_t i = p; i < memmap_entries; i++) { for (size_t i = p; i < count; i++) {
if (memmap[i].base < min) { if (m[i].base < min) {
min = memmap[i].base; min = m[i].base;
min_index = i; min_index = i;
} }
} }
struct e820_entry_t min_e = memmap[min_index]; struct e820_entry_t min_e = m[min_index];
memmap[min_index] = memmap[p]; m[min_index] = m[p];
memmap[p] = min_e; m[p] = min_e;
} }
// Merge contiguous bootloader-reclaimable and usable entries // Merge contiguous bootloader-reclaimable and usable entries
for (size_t i = 0; i < memmap_entries - 1; i++) { for (size_t i = 0; i < count - 1; i++) {
if (memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE if (m[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE
&& memmap[i].type != MEMMAP_USABLE) && m[i].type != MEMMAP_USABLE)
continue; continue;
if (memmap[i+1].type == memmap[i].type if (m[i+1].type == m[i].type
&& memmap[i+1].base == memmap[i].base + memmap[i].length) { && m[i+1].base == m[i].base + m[i].length) {
memmap[i].length += memmap[i+1].length; m[i].length += m[i+1].length;
// Eradicate from memmap // Eradicate from memmap
for (size_t j = i+1; j < memmap_entries - 1; j++) { for (size_t j = i+1; j < count - 1; j++) {
memmap[j] = memmap[j+1]; m[j] = m[j+1];
} }
memmap_entries--; count--;
i--; i--;
} }
} }
*_count = count;
} }
struct e820_entry_t *get_memmap(size_t *entries) { struct e820_entry_t *get_memmap(size_t *entries) {
sanitise_entries(true); sanitise_entries(memmap, &memmap_entries, true);
*entries = memmap_entries; *entries = memmap_entries;
@ -234,7 +238,7 @@ void init_memmap(void) {
memmap_alloc_range(4096, memmap_alloc_range(4096,
ALIGN_UP((uintptr_t)bss_end, 4096) - 4096, MEMMAP_BOOTLOADER_RECLAIMABLE, true, true, false, false); ALIGN_UP((uintptr_t)bss_end, 4096) - 4096, MEMMAP_BOOTLOADER_RECLAIMABLE, true, true, false, false);
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
allocations_disallowed = false; allocations_disallowed = false;
} }
@ -304,7 +308,7 @@ void init_memmap(void) {
memmap_entries++; memmap_entries++;
} }
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
allocations_disallowed = false; allocations_disallowed = false;
@ -334,7 +338,7 @@ void pmm_reclaim_uefi_mem(void) {
memmap[i].type = MEMMAP_USABLE; memmap[i].type = MEMMAP_USABLE;
} }
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
} }
void pmm_release_uefi_mem(void) { void pmm_release_uefi_mem(void) {
@ -447,7 +451,7 @@ void *ext_mem_alloc_type(size_t count, uint32_t type) {
// Zero out allocated space // Zero out allocated space
memset(ret, 0, count); memset(ret, 0, count);
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
return ret; return ret;
} }
@ -501,7 +505,7 @@ bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free
target->length = entry_top - top; target->length = entry_top - top;
} }
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
return true; return true;
} }
@ -520,7 +524,7 @@ bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free
target->base = base; target->base = base;
target->length = length; target->length = length;
sanitise_entries(false); sanitise_entries(memmap, &memmap_entries, false);
return true; return true;
} }

View File

@ -555,15 +555,15 @@ void linux_load(char *config, char *cmdline) {
struct boot_e820_entry *e820_table = boot_params->e820_table; struct boot_e820_entry *e820_table = boot_params->e820_table;
size_t memmap_entries; size_t mmap_entries;
struct e820_entry_t *memmap = get_raw_memmap(&memmap_entries); struct e820_entry_t *mmap = get_raw_memmap(&mmap_entries);
boot_params->e820_entries = memmap_entries; boot_params->e820_entries = mmap_entries;
for (size_t i = 0; i < memmap_entries; i++) { for (size_t i = 0; i < mmap_entries; i++) {
e820_table[i].addr = memmap[i].base; e820_table[i].addr = mmap[i].base;
e820_table[i].size = memmap[i].length; e820_table[i].size = mmap[i].length;
e820_table[i].type = memmap[i].type; e820_table[i].type = mmap[i].type;
} }
/////////////////////////////////////// ///////////////////////////////////////

View File

@ -137,7 +137,7 @@ void multiboot1_load(char *config, char *cmdline) {
if (!uri_open(&f, module_path)) if (!uri_open(&f, module_path))
panic("multiboot1: Failed to open module with path `%s`. Is the path correct?", module_path); panic("multiboot1: Failed to open module with path `%s`. Is the path correct?", module_path);
char *cmdline = config_get_value(config, i, "MODULE_STRING"); char *module_cmdline = config_get_value(config, i, "MODULE_STRING");
void *module_addr = (void *)(uintptr_t)ALIGN_UP(kernel_top, 4096); void *module_addr = (void *)(uintptr_t)ALIGN_UP(kernel_top, 4096);
memmap_alloc_range((uintptr_t)module_addr, f.size, MEMMAP_KERNEL_AND_MODULES, memmap_alloc_range((uintptr_t)module_addr, f.size, MEMMAP_KERNEL_AND_MODULES,
@ -147,13 +147,13 @@ void multiboot1_load(char *config, char *cmdline) {
m->begin = (uint32_t)(size_t)module_addr; m->begin = (uint32_t)(size_t)module_addr;
m->end = m->begin + f.size; m->end = m->begin + f.size;
m->cmdline = (uint32_t)(size_t)cmdline; m->cmdline = (uint32_t)(size_t)module_cmdline;
m->pad = 0; m->pad = 0;
if (verbose) { if (verbose) {
print("multiboot1: Requested module %u:\n", i); print("multiboot1: Requested module %u:\n", i);
print(" Path: %s\n", module_path); print(" Path: %s\n", module_path);
print(" String: \"%s\"\n", cmdline ?: ""); print(" String: \"%s\"\n", module_cmdline ?: "");
print(" Begin: %x\n", m->begin); print(" Begin: %x\n", m->begin);
print(" End: %x\n", m->end); print(" End: %x\n", m->end);
} }

View File

@ -286,11 +286,11 @@ void stivale_load(char *config, char *cmdline) {
// Reserve 32K at 0x70000 // Reserve 32K at 0x70000
memmap_alloc_range(0x70000, 0x8000, MEMMAP_USABLE, true, true, false, false); memmap_alloc_range(0x70000, 0x8000, MEMMAP_USABLE, true, true, false, false);
size_t memmap_entries; size_t mmap_entries;
struct e820_entry_t *memmap = get_memmap(&memmap_entries); struct e820_entry_t *mmap = get_memmap(&mmap_entries);
stivale_struct.memory_map_entries = (uint64_t)memmap_entries; stivale_struct.memory_map_entries = (uint64_t)mmap_entries;
stivale_struct.memory_map_addr = REPORTED_ADDR((uint64_t)(size_t)memmap); stivale_struct.memory_map_addr = REPORTED_ADDR((uint64_t)(size_t)mmap);
stivale_spinup(bits, want_5lv, &pagemap, stivale_spinup(bits, want_5lv, &pagemap,
entry_point, REPORTED_ADDR((uint64_t)(uintptr_t)&stivale_struct), entry_point, REPORTED_ADDR((uint64_t)(uintptr_t)&stivale_struct),
@ -341,8 +341,8 @@ pagemap_t stivale_build_pagemap(bool level5pg, bool unmap_null) {
uint64_t aligned_top = ALIGN_UP(top, 0x200000); uint64_t aligned_top = ALIGN_UP(top, 0x200000);
uint64_t aligned_length = aligned_top - aligned_base; uint64_t aligned_length = aligned_top - aligned_base;
for (uint64_t i = 0; i < aligned_length; i += 0x200000) { for (uint64_t j = 0; j < aligned_length; j += 0x200000) {
uint64_t page = aligned_base + i; uint64_t page = aligned_base + j;
map_page(pagemap, page, page, 0x03, true); map_page(pagemap, page, page, 0x03, true);
map_page(pagemap, higher_half_base + page, page, 0x03, true); map_page(pagemap, higher_half_base + page, page, 0x03, true);
} }
@ -363,7 +363,7 @@ __attribute__((noreturn)) void stivale_spinup_32(
__attribute__((noreturn)) void stivale_spinup( __attribute__((noreturn)) void stivale_spinup(
int bits, bool level5pg, pagemap_t *pagemap, int bits, bool level5pg, pagemap_t *pagemap,
uint64_t entry_point, uint64_t stivale_struct, uint64_t stack) { uint64_t entry_point, uint64_t _stivale_struct, uint64_t stack) {
#if defined (bios) #if defined (bios)
if (bits == 64) { if (bits == 64) {
// If we're going 64, we might as well call this BIOS interrupt // If we're going 64, we might as well call this BIOS interrupt
@ -382,6 +382,6 @@ __attribute__((noreturn)) void stivale_spinup(
common_spinup(stivale_spinup_32, 9, common_spinup(stivale_spinup_32, 9,
bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level, bits, level5pg, (uint32_t)(uintptr_t)pagemap->top_level,
(uint32_t)entry_point, (uint32_t)(entry_point >> 32), (uint32_t)entry_point, (uint32_t)(entry_point >> 32),
(uint32_t)stivale_struct, (uint32_t)(stivale_struct >> 32), (uint32_t)_stivale_struct, (uint32_t)(_stivale_struct >> 32),
(uint32_t)stack, (uint32_t)(stack >> 32)); (uint32_t)stack, (uint32_t)(stack >> 32));
} }

View File

@ -505,14 +505,14 @@ skip_modeset:;
// Reserve 32K at 0x70000 // Reserve 32K at 0x70000
memmap_alloc_range(0x70000, 0x8000, MEMMAP_USABLE, true, true, false, false); memmap_alloc_range(0x70000, 0x8000, MEMMAP_USABLE, true, true, false, false);
size_t memmap_entries; size_t mmap_entries;
struct e820_entry_t *memmap = get_memmap(&memmap_entries); struct e820_entry_t *mmap = get_memmap(&mmap_entries);
tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID; tag->tag.identifier = STIVALE2_STRUCT_TAG_MEMMAP_ID;
tag->entries = (uint64_t)memmap_entries; tag->entries = (uint64_t)mmap_entries;
memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap), memcpy((void*)tag + sizeof(struct stivale2_struct_tag_memmap),
memmap, sizeof(struct e820_entry_t) * memmap_entries); mmap, sizeof(struct e820_entry_t) * mmap_entries);
append_tag(&stivale2_struct, (struct stivale2_tag *)tag); append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
} }

View File

@ -182,10 +182,10 @@ struct smp_information *init_smp(size_t header_hack_size,
if (!x2apic) if (!x2apic)
continue; continue;
struct madt_x2apic *x2apic = (void *)madt_ptr; struct madt_x2apic *x2lapic = (void *)madt_ptr;
// Check if we can actually try to start the AP // Check if we can actually try to start the AP
if ((x2apic->flags & 1) ^ ((x2apic->flags >> 1) & 1)) if ((x2lapic->flags & 1) ^ ((x2lapic->flags >> 1) & 1))
max_cpus++; max_cpus++;
continue; continue;
@ -242,27 +242,27 @@ struct smp_information *init_smp(size_t header_hack_size,
if (!x2apic) if (!x2apic)
continue; continue;
struct madt_x2apic *x2apic = (void *)madt_ptr; struct madt_x2apic *x2lapic = (void *)madt_ptr;
// Check if we can actually try to start the AP // Check if we can actually try to start the AP
if (!((x2apic->flags & 1) ^ ((x2apic->flags >> 1) & 1))) if (!((x2lapic->flags & 1) ^ ((x2lapic->flags >> 1) & 1)))
continue; continue;
struct smp_information *info_struct = &ret[*cpu_count]; struct smp_information *info_struct = &ret[*cpu_count];
info_struct->acpi_processor_uid = x2apic->acpi_processor_uid; info_struct->acpi_processor_uid = x2lapic->acpi_processor_uid;
info_struct->lapic_id = x2apic->x2apic_id; info_struct->lapic_id = x2lapic->x2apic_id;
// Do not try to restart the BSP // Do not try to restart the BSP
if (x2apic->x2apic_id == bsp_x2apic_id) { if (x2lapic->x2apic_id == bsp_x2apic_id) {
(*cpu_count)++; (*cpu_count)++;
continue; continue;
} }
printv("smp: [x2APIC] Found candidate AP for bring-up. LAPIC ID: %u\n", x2apic->x2apic_id); printv("smp: [x2APIC] Found candidate AP for bring-up. LAPIC ID: %u\n", x2lapic->x2apic_id);
// Try to start the AP // Try to start the AP
if (!smp_start_ap(x2apic->x2apic_id, &gdtr, info_struct, if (!smp_start_ap(x2lapic->x2apic_id, &gdtr, info_struct,
longmode, lv5, (uintptr_t)pagemap.top_level, longmode, lv5, (uintptr_t)pagemap.top_level,
true)) { true)) {
print("smp: FAILED to bring-up AP\n"); print("smp: FAILED to bring-up AP\n");