Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions kernel/include/kernel/acpi/rsdp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef ACPI_RSDP_H
#define ACPI_RSDP_H

#include <stdint.h>

typedef struct __attribute__ ((packed)) {
char signature[8];
uint8_t checksum;
char oemid[6];
uint8_t revision;
uint32_t rsdt_address;
} RSDP_t;

typedef struct __attribute__ ((packed)) {
char signature[8];
uint8_t checksum;
char oemid[6];
uint8_t revision;
uint32_t rsdt_address;
uint32_t length;
uint64_t xsdt_address;
uint8_t extended_checksum;
uint8_t reserved[3];
} XSDP_t;

void* init_rsdp (uintptr_t rsdp_base_ptr, uint64_t hhdm_offset);

bool is_init (void);
bool is_rsdp (void);
bool is_xsdp (void);

uint8_t get_rsdp_revision ();
RSDP_t* get_rsdp ();
XSDP_t* get_xsdp ();

#endif
13 changes: 13 additions & 0 deletions kernel/include/kernel/acpi/rsdt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ACPI_RSDT_H
#define ACPI_RSDT_H

#include <kernel/acpi/sdt_header.h>

typedef struct __attribute__ ((packed)) {
SDT_header_t header;
uint32_t other_sdt_ptrs[];
} RSDT_t;

void init_rsdt (RSDP_t* rsdp_base_ptr);

#endif
18 changes: 18 additions & 0 deletions kernel/include/kernel/acpi/sdt_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ACPI_SDT_HEADER_H
#define ACPI_SDT_HEADER_H

#include <stdint.h>

typedef struct __attribute__ ((packed)) {
char signature[4];
uint32_t length;
uint8_t revision;
uint8_t checksum;
char oemid[6];
char oemTableId[8];
uint32_t oemRevision;
uint32_t creatorId;
uint32_t creatorRevision;
} SDT_header_t;

#endif
6 changes: 4 additions & 2 deletions kernel/include/kernel/memmgt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <stddef.h>
#include <stdint.h>

#define PAGE_SIZE 4096ull
#define PAGE_SIZE 4096ull
#define ALIGN_PAGE_DOWN(x) ((x) & ~(PAGE_SIZE - 1))

typedef struct {
uint64_t present : 1; // Page present in memory
Expand Down Expand Up @@ -91,7 +92,8 @@ uint64_t read_cr3 (void);
void write_cr3 (uint64_t new_value);

vaddr_t get_vaddr_t_from_ptr (void* ptr);
void* get_vaddr_from_frame (uint64_t phys_address);
void* get_vaddr_from_frame (uint64_t phys_frame);
void* get_vaddr_from_phys_addr (uint64_t phys_address);
void* vaddr_t_to_ptr (vaddr_t* virtual_addr);

void* alloc_vpages (size_t req_count, bool user);
Expand Down
60 changes: 60 additions & 0 deletions kernel/src/kernel/acpi/rsdp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <kclib/string.h>
#include <kernel/acpi/rsdp.h>
#include <kernel/memmgt.h>

static bool is_init_flag = false;

static RSDP_t* rsdp_ptr = nullptr;
static XSDP_t* xsdp_ptr = nullptr;
static uint8_t rsdp_ver = 0;

static void* init_and_return (void* arg) {
is_init_flag = true;
return arg;
}

inline bool is_init (void) { return is_init_flag; }
inline bool is_rsdp (void) { return is_init () ? (rsdp_ptr != nullptr) : false; }
inline bool is_xsdp (void) { return is_init () ? (xsdp_ptr != nullptr) : false; }

uint8_t get_rsdp_revision (void) { return rsdp_ver; }
RSDP_t* get_rsdp (void) { return rsdp_ptr; }
XSDP_t* get_xsdp (void) { return xsdp_ptr; }

void* init_rsdp (uintptr_t rsdp_base_ptr, uint64_t hhdm_offset) {
if (rsdp_base_ptr == 0) return nullptr;

// check if address is physical ; if it is, we will probably have to allocate memory
if (rsdp_base_ptr < hhdm_offset) {
paddr_t rsdp_base_frame = (paddr_t)ALIGN_PAGE_DOWN (rsdp_base_ptr);
vaddr_t rsdp_vaddr_start =
get_vaddr_t_from_ptr ((void*)ALIGN_PAGE_DOWN (rsdp_base_ptr + hhdm_offset));
vaddr_t rsdp_vaddr_end = get_vaddr_t_from_ptr (
(void*)ALIGN_PAGE_DOWN (rsdp_base_ptr + hhdm_offset + sizeof (XSDP_t)));

alloc_all_vpages_in_range (rsdp_vaddr_start, rsdp_vaddr_end, rsdp_base_frame);
rsdp_base_ptr += hhdm_offset;
}

RSDP_t* tmp_rsdp_ptr = (RSDP_t*)rsdp_base_ptr;
XSDP_t* tmp_xsdp_ptr = (XSDP_t*)rsdp_base_ptr;

// validate the RSDP & set revision
uint8_t rsdp_checksum = 0;
for (uint64_t i = 0; i < sizeof (RSDP_t); i++)
rsdp_checksum += ((uint8_t*)tmp_rsdp_ptr)[i];
if (rsdp_checksum != 0) return nullptr;
if (kmemcmp (tmp_rsdp_ptr->signature, "RSD PTR ", 8) != 0) return nullptr;
rsdp_ver = tmp_rsdp_ptr->revision;

// if we are on rev 1, set rsdp_ptr and leave
if (rsdp_ver == 0) return init_and_return (rsdp_ptr = tmp_rsdp_ptr);

// validate the XSDP
uint8_t xsdp_checksum = 0;
for (uint64_t j = 0; j < sizeof (XSDP_t); j++)
xsdp_checksum += ((uint8_t*)tmp_xsdp_ptr)[j];
if (xsdp_checksum != 0) return nullptr;

return init_and_return (xsdp_ptr = tmp_xsdp_ptr);
}
11 changes: 11 additions & 0 deletions kernel/src/kernel/entry.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <kclib/stdio.h>
#include <kclib/string.h>
#include <kernel/acpi/rsdp.h>
#include <kernel/console.h>
#include <kernel/elf.h>
#include <kernel/fs/chardev.h>
Expand Down Expand Up @@ -30,9 +31,11 @@ extern volatile struct limine_boot_time_request boottime_req;
extern volatile struct limine_memmap_request memmap_req;
extern volatile struct limine_module_request mod_req;
extern volatile struct limine_hhdm_request hhdm_req;
extern volatile struct limine_rsdp_request rsdp_req;

struct limine_framebuffer* framebuffer = nullptr;
struct limine_file* initramfs = nullptr;
uintptr_t rsdp = 0;
uint64_t hhdm_base = 0;

// Declaration of kernel entry points
Expand Down Expand Up @@ -104,6 +107,7 @@ static void print_info (void) {
__attribute__ ((noreturn)) void _start_stage2 (void) {
init_graphics (framebuffer);
init_console (framebuffer->width, framebuffer->height, 40, 40, 1, 1, 2);
rsdp = (uintptr_t)init_rsdp (rsdp, hhdm_base);

for (int i = 0; i < 3; i++) // open stdin, stdout and stderr
do_syscall (SYSCALL_SYS_OPEN, (uint64_t)"/dev/tty1", 0, 0);
Expand All @@ -118,6 +122,9 @@ __attribute__ ((noreturn)) void _start_stage2 (void) {

kprintf ("[Stage 2] Trying to load the ELF.\n");

kserial_printf ("RSDP Address: %llx\n", rsdp);
kserial_printf ("ACPI Revision: %u\n", get_rsdp_revision ());

uint64_t fork_result = do_syscall (SYSCALL_SYS_FORK, 0, 0, 0);

if (fork_result == 0) {
Expand Down Expand Up @@ -158,10 +165,14 @@ static void get_limine_requests (void) {
// REQUIRED: modules (for initramfs)
if (mod_req.response == nullptr || mod_req.response->module_count < 1) hcf ();

// REQUIRED: RSDP for ACPI
if (rsdp_req.response == nullptr) hcf ();

// set the values received from limine
framebuffer = framebuffer_request.response->framebuffers[0];
hhdm_base = hhdm_req.response->offset;
initramfs = mod_req.response->modules[0];
rsdp = (uintptr_t)rsdp_req.response->address;
}

/*!
Expand Down
4 changes: 4 additions & 0 deletions kernel/src/kernel/limine_requests.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ __attribute__ ((used,
section (".limine_requests"))) volatile struct limine_module_request mod_req = {
.id = LIMINE_MODULE_REQUEST, .revision = 0};

__attribute__ ((used,
section (".limine_requests"))) volatile struct limine_rsdp_request rsdp_req = {
.id = LIMINE_RSDP_REQUEST, .revision = 0};

__attribute__ ((used,
section (".limine_requests_start"))) static volatile LIMINE_REQUESTS_START_MARKER;

Expand Down
9 changes: 9 additions & 0 deletions kernel/src/kernel/memmgt.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ inline void* get_vaddr_from_frame (uint64_t phys_frame) {
return (void*)((phys_frame << 12) + hhdm_offset);
}

/*!
* Access a physical pointer as vaddr pointer with HHDM mapping
* @param phys_address the physical pointer
* @return pointer to virtual memory using HHDM mapping
*/
void* get_vaddr_from_phys_addr (uint64_t phys_address) {
return (void*)(phys_address + hhdm_offset);
}

/*!
* Set a bit in the memory bitmap
* @param page_idx page index
Expand Down
2 changes: 1 addition & 1 deletion user/hello/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ clean:

.PHONY: format
format:
find -type f \( -name '*.c' -o -name '*.h' \) ! -name 'limine.h' -exec clang-format -i {} +
find . -type f \( -name '*.c' -o -name '*.h' \) ! -name 'limine.h' -exec clang-format -i {} +
Loading