Skip to content

Commit 5c5167c

Browse files
committed
feat: realize paging, add page, frame allocator and change kfree, kmalloc, krealloc
1 parent ffe676b commit 5c5167c

File tree

15 files changed

+597
-53
lines changed

15 files changed

+597
-53
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit ffe676bbcd462e3e7d7c8bd82ca1683361ba3bf6
2+
Author: Alexeev Bronislav <[email protected]>
3+
Date: Sun Aug 24 04:06:51 2025 +0700
4+
5+
docs: add paging docs
6+
17
commit 14696be1af1db0eb9fd795677785047285ee7277
28
Author: Alexeev Bronislav <[email protected]>
39
Date: Sun Aug 24 03:27:10 2025 +0700

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ LDFLAGS = -Ttext 0x1000 --oformat binary
1717

1818
KERNEL_ENTRY = $(BIN_DIR)/bootloader/kernel_entry.o
1919
INTERRUPT_OBJ = $(BIN_DIR)/kernel/cpu/interrupt.o
20+
PAGING_OBJ = $(BIN_DIR)/bootloader/paging.o
2021

2122
C_SOURCES = $(shell find $(SRC_DIR) -name '*.c')
2223
C_OBJS = $(C_SOURCES:$(SRC_DIR)/%.c=$(BIN_DIR)/%.o)
2324

24-
OBJS = $(KERNEL_ENTRY) $(INTERRUPT_OBJ) $(C_OBJS)
25+
OBJS = $(KERNEL_ENTRY) $(INTERRUPT_OBJ) $(PAGING_OBJ) $(C_OBJS)
2526

2627
RED=\033[0;31m
2728
GREEN=\033[0;32m
@@ -77,7 +78,7 @@ debug: $(DISKIMG_DIR)/$(DISKIMG_NAME)
7778
@qemu-system-i386 -fda $< -boot a -s -S
7879

7980
clean:
80-
@printf "$(RED)[RM] Clean bin %-50s$(RESET)\n" "$(BIN_DIR)"
81+
@printf "$(RED)[RM] Clean $(BIN_DIR) and $(DISKIMG_DIR)$(RESET)\n"
8182
@rm -rf $(BIN_DIR)/* $(DISKIMG_DIR)/*
8283

8384
.PHONY: all diskimg run run_bin debug clean clean_all

src/bootloader/paging.asm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[bits 32]
2+
[global load_page_dir]
3+
[global enable_paging]
4+
5+
load_page_dir:
6+
mov eax, [esp+4] ; аргумент из стека
7+
mov cr3, eax ; и суем в CR3
8+
ret
9+
10+
enable_paging:
11+
mov eax, cr0
12+
or eax, 0x80000000
13+
mov cr0, eax
14+
ret

src/kernel/cpu/isr.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "../kklibc/stdlib.h"
66
#include "timer.h"
77
#include "../drivers/lowlevel_io.h"
8+
#include "../kklibc/stdio.h"
9+
#include "../kklibc/paging/paging.h"
810

911
isr_t interrupt_handlers[256];
1012

@@ -116,14 +118,47 @@ char *exception_messages[] = {
116118
"Reserved"
117119
};
118120

121+
void page_fault_handler(registers_t r) {
122+
u32 fault_addr;
123+
asm volatile("mov %%cr2, %0" : "=r" (fault_addr));
124+
125+
int present = !(r.err_code & 0x1); // Bit 0: 0 if not present
126+
int rw = r.err_code & 0x2; // Bit 1: 1 if write
127+
int us = r.err_code & 0x4; // Bit 2: 1 if user mode
128+
int reserved = r.err_code & 0x8; // Bit 3: 1 if reserved bit overwritten
129+
130+
kprintf("Page fault at %x: %s %s in %s mode\n",
131+
fault_addr,
132+
present ? "protection fault" : "not present",
133+
rw ? "write" : "read",
134+
us ? "user" : "kernel");
135+
136+
debug_page_fault(fault_addr);
137+
138+
if (us) {
139+
kprintf("Killing process.\n");
140+
asm volatile("hlt");
141+
return;
142+
}
143+
144+
kprintf("Kernel page fault! Halting.\n");
145+
for(;;);
146+
}
147+
119148
void isr_handler(registers_t r) {
149+
if (r.int_no == 14) {
150+
page_fault_handler(r);
151+
}
152+
120153
kprint("received interrupt: ");
121154
char s[3];
122155
int_to_ascii(r.int_no, s);
156+
123157
kprint(s);
124158
kprint("\n");
125159
kprint(exception_messages[r.int_no]);
126160
kprint("\n");
161+
asm volatile("hlt");
127162
}
128163

129164
void register_interrupt_handler(u8 n, isr_t handler) {
@@ -148,6 +183,7 @@ void irq_install() {
148183
asm volatile("sti");
149184
/* IRQ0: таймер */
150185
init_timer(50);
186+
151187
/* IRQ1: клавиатура */
152188
init_keyboard();
153189
}

src/kernel/cpu/isr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ typedef struct {
8181
void isr_install();
8282
void isr_handler(registers_t r);
8383
void irq_install();
84-
8584
typedef void (*isr_t)(registers_t);
8685
void register_interrupt_handler(u8 n, isr_t handler);
8786

src/kernel/kernel/kernel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ void kmain() {
2424
irq_install();
2525
kprint("IRQ Installed\n");
2626

27+
init_paging();
28+
2729
heap_init();
2830

2931
// Приглашение
30-
kprint("Success loaded! Welcome to Kintsugi OS\n");
31-
kprint("Copyright (C) alexeev-prog\nRepository: https://github.com/alexeev-prog/KintsugiOS\n");
32+
kprint("\nKintsugi OS (C) 2025\nRepository: https://github.com/alexeev-prog/KintsugiOS\n");
3233

3334
// Уведомление о старте оболочки командной строки
3435
kprint("\nKeramika Shell v0.1.0 "
35-
"Type END to halt the CPU\n"
3636
"Type HELP to view commands\n\n!#> ");
3737
}
3838

src/kernel/kklibc/kklibc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
#include "stdlib.h"
88
#include "function.h"
99
#include "math.h"
10+
#include "paging/paging.h"
1011

1112
#endif

0 commit comments

Comments
 (0)