some test things

This commit is contained in:
2025-05-10 15:48:06 +03:00
parent 7e97987e95
commit b836a37264
5 changed files with 379 additions and 84 deletions
+101 -75
View File
@@ -1,96 +1,122 @@
bits 32
section .multiboot
align 4
dd 0x1BADB002
dd 0x00
dd - (0x1BADB002+0x00)
%define CR0_MP 0x00000002 /* bit 01 -> enable monitor coprocessor */
%define CR0_NE 0x00000020 /* bit 05 -> enable native x87 FPU mode */
%define CR0_WP 0x00010000 /* bit 16 -> enable write protect (for CoW) */
%define CR0_AM 0x00040000 /* bit 18 -> enable alignment checking */
%define CR0_PG 0x80000000 /* bit 31 -> enable paging */
section .data
%define PAGE_OFFSET 0xC0000000
%define GDT_BASE (0xFFFFFFFF - (PAGE_OFFSET - 1))
section .setup
align 4
multiboot_info:
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
tmp_gdtr:
dw ((3 * 8) - 1)
tmp_gdta:
dd tmp_gdt
global tss
global tss_end
align 4
tss:
dd 0 ; 0-3
dd stack_top ; 4-7
dd DATA_SEG ; 8-11
dd 0 ; 12-15
dd 0 ; 16-19
dd 0 ; 20-23
dd 0 ; 24-27
dd 0 ; 28-31
dd 0 ; 32-35
dd 0 ; 36-39
dd 0 ; 40-43
dd 0 ; 44-47
dd 0 ; 48-51
dd 0 ; 52-55
dd 0 ; 56-59
dd 0 ; 60-63
dd 0 ; 64-67
dd 0 ; 68-71
dd DATA_SEG ; 72-75
dd CODE_SEG ; 76-79
dd DATA_SEG ; 80-83
dd DATA_SEG ; 84-87
dd DATA_SEG ; 88-91
dd DATA_SEG ; 92-95
dd 0 ; 96-99
dw 0 ; 100-101
dw 104 ; 102-103
tss_end:
tmp_gdt:
; null entry
dw 0x0000
dw 0x0000
dw 0x0000
dw 0x0000
section .bss
stack_bottom:
resb 16384
stack_top:
; /* KERNEL CODE */
dw 0xFFFF ; /* segment limit 15-00 */
dw 0x0000 ; /* base address 15-00 */
db 0x00 ; /* base address 23-16 */
db 0x9A ; /* P=1 DPL=00 S=1 TYPE=1010 (exec/read) */
db 0xCF ; /* G=1 DB=1 0=0 AVL=0 SEGLIM=1111 */
db GDT_BASE >> 24 ; /* base address 31-24 */
; /* KERNEL DATA */
dw 0xFFFF ; /* segment limit 15-00 */
dw 0x0000 ; /* base address 15-00 */
db 0x00 ; /* base address 23-16 */
db 0x92 ; /* P=1 DPL=00 S=1 TYPE=0010 (read/write) */
db 0xCF ; /* G=1 DB=1 0=0 AVL=0 SEGLIM=1111 */
db GDT_BASE >> 24 ; /* base address 31-24 */
section .gdt
%include "src/gdt.asm"
align 4
multiboot_header:
dd 0x1BADB002
dd 0x00
dd - (0x1BADB002 + 0x00)
section .text
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
dd 0
; section .text
global start:function (start.end - start)
extern kmain ; this function is gonna be located in our c code(kernel.c)
extern get_last_boot_addr
extern gdt_init
extern bss_init
extern setup_tmp_pgdir
align 4
start:
mov esp, stack_top
; mov esp, stack_top
cli
push ebx ; Сохраняем оригинальный ebx (указатель Multiboot)
; push ebx ; Сохраняем оригинальный ebx (указатель Multiboot)
lgdt [gdt]
;lgdt [gdt]
lgdt [tmp_gdtr]
; Заполняем адрес TSS в дескрипторе GDT
mov eax, tss ; Получаем 32-битный адрес tss
mov ebx, gdt_tss ; Адрес дескриптора TSS в GDT
mov [ebx + 2], ax ; Нижние 16 бит адреса TSS
shr eax, 16 ; Сдвигаем для доступа к старшим битам
mov [ebx + 4], al ; Средние 8 бит адреса TSS
mov [ebx + 7], ah ; Верхние 8 бит адреса TSS
mov cx, 0x10 ; data segment
mov ds, cx
mov es, cx
mov fs, cx
mov gs, cx
mov ss, cx
jmp 0x8:.huita ; 0x8 - code segment
mov ax, DATA_SEG
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp CODE_SEG:.huita
.text
align 4
.huita:
mov ax, TSS_SEG
ltr ax
pop ebx ; Восстанавливаем оригинальный ebx
push ebx ; Передаем его в kmain
call kmain
hlt
mov esp, PAGE_OFFSET + 0x10000 ; default stack address
push 0
popf ; reset EFLAGS
push ebx ; save Multiboot info structure
push eax ; save Multiboot magic value
call setup_tmp_pgdir
mov cr3, eax
mov eax, cr0
and eax, 0x00000011 ; disable all, preserve ET & PE (GRUB)
or eax, 0x80000000 ; CR0_PG ; enable PG
or eax, 0x40000 ; CR0_AM ; enable AM
or eax, 0x10000 ; CR0_WP ; enable WP
or eax, 0x20 ; CR0_NE ; enable NE
or eax, 0x2 ; CR0_MP ; enable MP
mov cr0, eax
call bss_init ; initialize BSS segment
call gdt_init ; setup and load definitive GDT
call get_last_boot_addr
pop ecx ; restore Multiboot magic value
pop ebx ; restore Multiboot info structure
push eax ; save last boot address
push ebx ; save Multiboot info structure
push ecx ; save Multiboot magic value
pop ebx ; Восстанавливаем оригинальный ebx
push ebx ; Передаем его в kmain
call kmain
hlt
.end: