Skip to content

Commit

Permalink
Run clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
ramidzkh committed Jan 17, 2025
1 parent a214e7f commit ed8ddc0
Show file tree
Hide file tree
Showing 27 changed files with 208 additions and 198 deletions.
58 changes: 31 additions & 27 deletions kernel/src/arch/x86_64/acpi/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "../memory/paging.h"

#define BIOS_MEMORY_BEGIN 0x0E0000
#define BIOS_MEMORY_END 0xFFFFF
#define BIOS_MEMORY_END 0xFFFFF

#define RSDP_SIGNATURE "RSD PTR "

Expand All @@ -26,12 +26,12 @@ static struct rsdp *rsdp = NULL;

bool rsdp_find(void) {
// Refer to 5.2.5.1 and 5.2.5.3 from ACPI Specification
char *addr = (char *)BIOS_MEMORY_BEGIN;
char *addr = (char *) BIOS_MEMORY_BEGIN;
for (;
addr < (char *)BIOS_MEMORY_END && !cmp_signature(addr, RSDP_SIGNATURE);
addr < (char *) BIOS_MEMORY_END && !cmp_signature(addr, RSDP_SIGNATURE);
addr += 16);
rsdp = (struct rsdp *)addr;
return addr < (char *)BIOS_MEMORY_END;
rsdp = (struct rsdp *) addr;
return addr < (char *) BIOS_MEMORY_END;
}

void rsdp_print_signature(void) {
Expand All @@ -44,7 +44,7 @@ void rsdp_print_signature(void) {
// No need to map rsdp I think cause its in bios (already mapped)
bool rsdp_verify(void) {
// Refer to 5.2.5.3 from ACPI Specification
uint8_t *addr = (int8_t *)rsdp;
uint8_t *addr = (int8_t *) rsdp;
// ! IMPORTANT sum needs to be unsigned 8 bits
uint8_t sum = 0;
for (size_t i = 0; i < 20; i++) {
Expand All @@ -53,31 +53,33 @@ bool rsdp_verify(void) {
return !sum;
}

uint8_t rsdp_get_revision(void) { return rsdp->revision; }
uint8_t rsdp_get_revision(void) {
return rsdp->revision;
}

static struct rsdt *rsdt_get(void) {
return (struct rsdt *)(uint64_t)rsdp->rsdt_address;
return (struct rsdt *) (uint64_t) rsdp->rsdt_address;
}

bool rsdt_map(void) {
uint64_t address = (uint64_t)rsdp->rsdt_address;
uint64_t address = (uint64_t) rsdp->rsdt_address;
map_page(KERNEL_MAPPING_ADDRESS | address, address, PAGE_PRESENT);
return verify_mapping(KERNEL_MAPPING_ADDRESS | address);
}

bool rsdt_verify(void) {
struct rsdt *rsdt =
(struct rsdt *)(KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);
(struct rsdt *) (KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);
uint8_t sum = 0;
for (size_t i = 0; i < rsdt->length; i++) {
sum += ((uint8_t *)rsdt)[i];
sum += ((uint8_t *) rsdt)[i];
}
return !sum;
}

static size_t rsdt_get_entry_count(void) {
struct rsdt *rsdt =
(struct rsdt *)(KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);
(struct rsdt *) (KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);
return (rsdt->length - sizeof(struct rsdt)) / sizeof(uint32_t);
}

Expand All @@ -88,33 +90,33 @@ static struct madt *madt = NULL;
bool madt_find(void) {
size_t length = rsdt_get_entry_count();
struct rsdt *rsdt =
(struct rsdt *)(KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);
(struct rsdt *) (KERNEL_MAPPING_ADDRESS | rsdp->rsdt_address);

for (size_t i = 0; i < length; i++) {
map_page(KERNEL_MAPPING_ADDRESS | (uint64_t)rsdt->entry[i],
(uint64_t)rsdt->entry[i], PAGE_PRESENT);
map_page(KERNEL_MAPPING_ADDRESS | (uint64_t) rsdt->entry[i],
(uint64_t) rsdt->entry[i], PAGE_PRESENT);

struct description_header *description_header =
(struct description_header *)(KERNEL_MAPPING_ADDRESS |
(uint64_t)rsdt->entry[i]);
(struct description_header *) (KERNEL_MAPPING_ADDRESS |
(uint64_t) rsdt->entry[i]);
if (cmp_signature(description_header->signature, MADT_SIGNATURE)) {
madt = (struct madt *)(uint64_t)rsdt->entry[i];
madt = (struct madt *) (uint64_t) rsdt->entry[i];
return true;
}
}
return false;
}

bool madt_map(void) {
uint64_t address = (uint64_t)madt;
uint64_t address = (uint64_t) madt;
map_page(KERNEL_MAPPING_ADDRESS | address, address, PAGE_PRESENT);
madt = (struct madt *)(KERNEL_MAPPING_ADDRESS | address);
madt = (struct madt *) (KERNEL_MAPPING_ADDRESS | address);
return verify_mapping(KERNEL_MAPPING_ADDRESS | address);
}

bool madt_verify(void) {
uint8_t sum = 0;
uint8_t *addr = (uint8_t *)madt;
uint8_t *addr = (uint8_t *) madt;
for (size_t i = 0; i < madt->length; i++) {
sum += addr[i];
}
Expand All @@ -131,24 +133,26 @@ static size_t max_madt_entries = 0;
size_t ioapic_count_entries(void) {
// all entries are after madt
struct madt_entry *madt_entry =
(struct madt_entry *)((uint8_t *)madt + sizeof(struct madt));
(struct madt_entry *) ((uint8_t *) madt + sizeof(struct madt));
// madt->length is length of bytes to just add madt with length to get end
// address

uint8_t *end = (uint8_t *)madt + madt->length;
uint8_t *end = (uint8_t *) madt + madt->length;
max_madt_entries =
(end - (uint8_t *)madt_entry) / sizeof(struct madt_entry);
(end - (uint8_t *) madt_entry) / sizeof(struct madt_entry);
if (madt_entries) free(madt_entries);
madt_entries = kmalloc(sizeof(*madt_entries) * max_madt_entries);

// they vary in different types tho

for (size_t i = 0; (uint8_t *)madt_entry < end; i++) {
for (size_t i = 0; (uint8_t *) madt_entry < end; i++) {
madt_entries[i] = madt_entry;
madt_entry =
(struct madt_entry *)((uint8_t *)madt_entry + madt_entry->length);
(struct madt_entry *) ((uint8_t *) madt_entry + madt_entry->length);
}
return max_madt_entries;
}

struct madt_entry **get_madt_entries(void) { return madt_entries; }
struct madt_entry **get_madt_entries(void) {
return madt_entries;
}
4 changes: 2 additions & 2 deletions kernel/src/arch/x86_64/acpi/acpi.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>

// Refer to 5.2.5.3 of ACPI Spec
struct rsdp {
Expand Down Expand Up @@ -49,7 +49,7 @@ struct madt {
} __attribute__((packed));

#define MADT_ENTRY \
uint8_t type; \
uint8_t type; \
uint8_t length;

// generalised struct
Expand Down
8 changes: 4 additions & 4 deletions kernel/src/arch/x86_64/apic/ioapic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
#include <stdint.h>

uint32_t ioapic_read_reg(void *ioapicaddr, uint32_t reg) {
uint32_t volatile *ioapic = (uint32_t volatile *)ioapicaddr;
uint32_t volatile *ioapic = (uint32_t volatile *) ioapicaddr;
ioapic[0] = reg;

// window of ioapic
return ioapic[4];
}

void ioapic_write_reg(void *ioapicaddr, uint32_t reg, uint32_t value) {
uint32_t volatile *ioapic = (uint32_t volatile *)ioapicaddr;
ioapic[0] = reg;
ioapic[4] = value;
uint32_t volatile *ioapic = (uint32_t volatile *) ioapicaddr;
ioapic[0] = reg;
ioapic[4] = value;
}

bool ioapic_map(void) {
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/arch/x86_64/apic/ioapic.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <stdint.h>
#include "../memory/paging.h"
#include <stdint.h>

#define IOAPICBASE 0xFEC00000
#define IOAPICBASE 0xFEC00000
#define IOAPIC_VIRTUAL_ADDRESS (KERNEL_MAPPING_ADDRESS | IOAPICBASE)

// Reads register from ioapic
Expand Down
29 changes: 14 additions & 15 deletions kernel/src/arch/x86_64/apic/lapic.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "../memory/paging.h"
#include "../serial/serial.h"

#define IA32_APIC_BASE_MSR 0x1B
#define IA32_APIC_BASE_MSR_BSP 0x100
#define IA32_APIC_BASE_MSR 0x1B
#define IA32_APIC_BASE_MSR_BSP 0x100
#define IA32_APIC_BASE_MSR_ENABLE 0x800

bool apic_is_supported(void) {
Expand Down Expand Up @@ -42,17 +42,17 @@ static uintptr_t cpu_get_apic_base(void) {
#define BASE_LAPIC 0xFEE00000

void write_reg(void *lapicbase, uint32_t reg, uint32_t value) {
uint32_t volatile *lapic = (uint32_t volatile *)lapicbase;
uint32_t volatile *lapic = (uint32_t volatile *) lapicbase;
lapic[reg] = value;
}

uint32_t read_reg(void *lapicbase, uint32_t reg) {
uint32_t volatile *lapic = (uint32_t volatile *)lapicbase;
uint32_t volatile *lapic = (uint32_t volatile *) lapicbase;
return lapic[reg];
}

#define PIC1 0x20 /* IO base address for master PIC */
#define PIC2 0xA0 /* IO base address for slave PIC */
#define PIC1 0x20 /* IO base address for master PIC */
#define PIC2 0xA0 /* IO base address for slave PIC */
#define PIC1_DATA (PIC1 + 1)
#define PIC2_DATA (PIC2 + 1)

Expand All @@ -62,7 +62,6 @@ bool map_apic(void) {
map_page(LAPIC_VIRTUAL_ADDRESS, BASE_LAPIC,
PAGE_PRESENT | PAGE_WRITE | PAGE_CACHE_DISABLE);
return verify_mapping(LAPIC_VIRTUAL_ADDRESS);

}

void enable_apic(void) {
Expand All @@ -72,27 +71,27 @@ void enable_apic(void) {

cpu_set_apic_base(cpu_get_apic_base());
// ! triple faults here
write_reg((void *)LAPIC_VIRTUAL_ADDRESS, 0xF0,
read_reg((void *)LAPIC_VIRTUAL_ADDRESS, 0xF0) | 0x100);
write_reg((void *) LAPIC_VIRTUAL_ADDRESS, 0xF0,
read_reg((void *) LAPIC_VIRTUAL_ADDRESS, 0xF0) | 0x100);
}

void send_apic_eoi(void) {
*(uint32_t volatile *)(LAPIC_VIRTUAL_ADDRESS + 0xB0) = 0;
*(uint32_t volatile *) (LAPIC_VIRTUAL_ADDRESS + 0xB0) = 0;
}

// Refer to Intel IA-32 Volume 3 12.5.4 APIC Timer
void init_apic_timer(uint32_t initial_count, uint8_t vector) {
// be divisble by 1 (every 10ms) (Figure 12-10)
*(uint32_t volatile *)(LAPIC_VIRTUAL_ADDRESS + 0x3E0) = 1011;
*(uint32_t volatile *) (LAPIC_VIRTUAL_ADDRESS + 0x3E0) = 1011;
// set initial count (Figure 12-11)
*(uint32_t volatile *)(LAPIC_VIRTUAL_ADDRESS + 0x380) = initial_count;
*(uint32_t volatile *) (LAPIC_VIRTUAL_ADDRESS + 0x380) = initial_count;
// Refer to 12.5.1 (Timer mode and vector in Figure 12-8)
// 0 = one shot, 1 = periodic, 2 = TSC-deadline
#define APIC_TIMER_MODE 0
*(uint32_t volatile *)(LAPIC_VIRTUAL_ADDRESS + 0x320) =
vector | (APIC_TIMER_MODE << 17);
*(uint32_t volatile *) (LAPIC_VIRTUAL_ADDRESS + 0x320) =
vector | (APIC_TIMER_MODE << 17);
}

uint32_t get_apic_timer_current(void) {
return *(uint32_t volatile *)(LAPIC_VIRTUAL_ADDRESS + 0x390);
return *(uint32_t volatile *) (LAPIC_VIRTUAL_ADDRESS + 0x390);
}
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/apic/lapic.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>

// Checks whether apic is supported
bool apic_is_supported(void);
Expand Down
Loading

0 comments on commit ed8ddc0

Please sign in to comment.