feature: kalloc_frame and demo in the main
This commit is contained in:
parent
8fd17276b2
commit
f559e71433
@ -24,5 +24,6 @@ SECTIONS
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
end_kernel = .;
|
||||
}
|
||||
}
|
||||
|
@ -2,28 +2,12 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define NOT_PRESENT (1 << 0)
|
||||
#define PRESENT (1 << 0)
|
||||
#define RW (1 << 1)
|
||||
#define SUPERVISOR (0 << 2)
|
||||
#define INIT_FLAGS (NOT_PRESENT | RW | SUPERVISOR)
|
||||
|
||||
struct page_directory_entry {
|
||||
uint32_t present : 1;
|
||||
uint32_t rw : 1;
|
||||
uint32_t user : 1;
|
||||
uint32_t page_size : 1;
|
||||
uint32_t unused : 8;
|
||||
uint32_t frame : 20;
|
||||
};
|
||||
|
||||
struct page_table_entry {
|
||||
uint32_t present : 1;
|
||||
uint32_t rw : 1;
|
||||
uint32_t user : 1;
|
||||
uint32_t accessed : 1;
|
||||
uint32_t dirty : 1;
|
||||
uint32_t unused : 7;
|
||||
uint32_t frame : 20;
|
||||
};
|
||||
#define ACCESSED (1 << 4)
|
||||
#define INIT_FLAGS (PRESENT | RW | SUPERVISOR)
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
void init_memory(void);
|
||||
uintptr_t kalloc_frame(uint32_t nb_frames);
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include "gdt.h"
|
||||
#include "kprintf.h"
|
||||
#include "memory.h"
|
||||
#include "shell.h"
|
||||
#include "string.h"
|
||||
#include "terminal.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
@ -23,5 +25,8 @@ void kernel_main(void)
|
||||
terminal_initialize();
|
||||
init_gdt();
|
||||
init_memory();
|
||||
char *str = (char *)kalloc_frame(1);
|
||||
memcpy(str, "Hello world!\n", 15);
|
||||
kprintf(KERN_INFO, "%s", str);
|
||||
shell_init();
|
||||
}
|
||||
|
44
src/memory/frame.c
Normal file
44
src/memory/frame.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "kprintf.h"
|
||||
#include "memory.h"
|
||||
|
||||
#define MAX_FRAMES 1048319
|
||||
#define CEIL(x, y) (((x) + (y) - 1) / (y))
|
||||
#define GET_FRAME(i) (frame_table[i / 8] & (1 << (i % 8)))
|
||||
#define SET_FRAME(i, used) \
|
||||
do { \
|
||||
if (used) \
|
||||
frame_table[i / 8] |= (1 << (i % 8)); \
|
||||
else \
|
||||
frame_table[i / 8] &= ~(1 << (i % 8)); \
|
||||
} while (0)
|
||||
|
||||
extern uint32_t end_kernel;
|
||||
|
||||
static uint8_t frame_table[CEIL(MAX_FRAMES, 8)];
|
||||
|
||||
uintptr_t kalloc_frame(uint32_t nb_frames)
|
||||
{
|
||||
if (nb_frames > MAX_FRAMES)
|
||||
kprintf(KERN_CRIT, "Not enough frames (max: %d)\n", MAX_FRAMES);
|
||||
size_t i = 0;
|
||||
while (i < MAX_FRAMES) {
|
||||
size_t free_frames = 1;
|
||||
while (!GET_FRAME(i + free_frames) && free_frames < nb_frames)
|
||||
free_frames++;
|
||||
if (free_frames == nb_frames)
|
||||
goto end;
|
||||
i += free_frames;
|
||||
}
|
||||
end:
|
||||
if (i != MAX_FRAMES) {
|
||||
for (size_t j = 0; j < nb_frames; j++)
|
||||
SET_FRAME(j + i, 1);
|
||||
return (uintptr_t)&end_kernel + i * PAGE_SIZE;
|
||||
}
|
||||
kprintf(KERN_WARNING, "Not enough frames available\n", MAX_FRAMES);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user