Compare commits

...

4 Commits

Author SHA1 Message Date
nub31
b6caeb4a74 64 bit mode 2025-08-23 19:33:24 +02:00
nub31
655bd7b174 ... 2025-08-23 19:23:49 +02:00
nub31
9cb2959a77 ... 2025-08-23 19:21:41 +02:00
nub31
624c90ce88 ... 2025-08-23 19:20:42 +02:00
8 changed files with 168 additions and 65 deletions

View File

@@ -5,8 +5,8 @@
- `make` - `make`
- `grub` - `grub`
- `mtools` - `mtools`
- `i386-elf-gcc` - `x86_64-elf-gcc`
- `i386-elf-ld` - `x86_64-elf-ld`
## Building ## Building

View File

@@ -1,4 +1,3 @@
menuentry "nub-os" { menuentry "nub-os" {
multiboot /boot/kernel.bin multiboot2 /boot/kernel.bin
boot
} }

View File

@@ -1,7 +1,7 @@
CC = i386-elf-gcc CC = x86_64-elf-gcc
LD = i386-elf-ld LD = x86_64-elf-ld
CFLAGS = -ffreestanding -m32 CFLAGS = -ffreestanding -m64
all: .build/nub-os.iso all: .build/nub-os.iso
@@ -17,17 +17,14 @@ build-dir:
cp .build/kernel.bin .build/iso/boot/ cp .build/kernel.bin .build/iso/boot/
grub-mkrescue -o .build/nub-os.iso .build/iso/ grub-mkrescue -o .build/nub-os.iso .build/iso/
.build/kernel.bin: build-dir .build/entry.o .build/kernel.o .build/mem.o .build/print.o .build/kernel.bin: build-dir .build/boot.o .build/kernel.o .build/print.o
$(LD) -Ttext 0x100000 -o .build/kernel.bin .build/entry.o .build/kernel.o .build/mem.o .build/print.o $(LD) -Ttext 0x100000 -o .build/kernel.bin .build/boot.o .build/kernel.o .build/print.o
.build/kernel.o: build-dir src/kernel.c .build/kernel.o: build-dir src/kernel.c
$(CC) $(CFLAGS) -c -o .build/kernel.o src/kernel.c $(CC) $(CFLAGS) -c -o .build/kernel.o src/kernel.c
.build/mem.o: build-dir src/mem.c
$(CC) $(CFLAGS) -c -o .build/mem.o src/mem.c
.build/print.o: build-dir src/print.c .build/print.o: build-dir src/print.c
$(CC) $(CFLAGS) -c -o .build/print.o src/print.c $(CC) $(CFLAGS) -c -o .build/print.o src/print.c
.build/entry.o: build-dir src/entry.asm .build/boot.o: build-dir src/boot.asm
nasm -f elf -o .build/entry.o src/entry.asm nasm -f elf64 -o .build/boot.o src/boot.asm

157
src/boot.asm Normal file
View File

@@ -0,0 +1,157 @@
%define MAGIC 0xe85250d6
%define ARCH 0
%define LEN (header_end - header_start)
%define CHECKSUM -(MAGIC + ARCH + LEN)
header_start:
align 4
dd MAGIC
dd ARCH
dd LEN
dd CHECKSUM
dw 0
dd 8
header_end:
section .bss
align 4096
page_table_l4:
resb 4096
page_table_l3:
resb 4096
page_table_l2:
resb 4096
align 16
resb 16384
stack_top:
extern kernel_main
section .text
bits 32
global _start
_start:
mov esp, stack_top
call check_cpuid
call check_long_mode
call setup_page_tables
call enable_paging
lgdt [gdt64.pointer]
jmp gdt64.kernel_code:long_mode_start
check_cpuid:
pushfd
pop eax
mov ecx, eax
xor eax, 1 << 21
push eax
popfd
pushfd
pop eax
push ecx
popfd
xor eax, ecx
jz .no_cpuid
ret
.no_cpuid:
mov al, 'I'
jmp error
check_long_mode:
mov eax, 0x80000000
cpuid
cmp eax, 0x80000001
jb .no_long_mode
mov eax, 0x80000001
cpuid
test edx, 1 << 29
jz .no_long_mode
ret
.no_long_mode:
mov al, 'L'
jmp error
setup_page_tables:
mov edi, page_table_l4
mov ecx, 4096 * 3 / 4
xor eax, eax
rep stosd
mov eax, page_table_l3
or eax, 0b11
mov [page_table_l4], eax
mov eax, page_table_l2
or eax, 0b11
mov [page_table_l3], eax
mov ecx, 0
.map_p2_table:
mov eax, 0x200000
mul ecx
or eax, 0b10000011
mov [page_table_l2 + ecx * 8], eax
inc ecx
cmp ecx, 512
jne .map_p2_table
ret
enable_paging:
mov eax, page_table_l4
mov cr3, eax
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
mov ecx, 0xC0000080
rdmsr
or eax, 1 << 8
wrmsr
mov eax, cr0
or eax, 1 << 31
mov cr0, eax
ret
bits 64
long_mode_start:
mov ax, gdt64.kernel_data
mov ss, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov rsp, stack_top
call kernel_main
cli
hang:
hlt
jmp hang
error:
mov dword [0xb8000], 0x4f524f45
mov dword [0xb8004], 0x4f3a4f52
mov dword [0xb8008], 0x4f204f20
mov byte [0xb800a], al
hlt
section .rodata
gdt64:
dq 0
.kernel_code: equ $ - gdt64
dq (1<<44) | (1<<47) | (1<<41) | (1<<43) | (1<<53)
.kernel_data: equ $ - gdt64
dq (1<<44) | (1<<47) | (1<<41)
.pointer:
dw $ - gdt64 - 1
dq gdt64

View File

@@ -1,21 +0,0 @@
extern kernel_init
section .multiboot
align 4
dd 0x1BADB002 ; multiboot magic number
dd 0x0 ; flags
dd -(0x1BADB002+0x0) ; checksum
section .text
global _start
_start:
cli
mov esp, stack_top
call kernel_init
.hang:
hlt
jmp .hang
section .bss
resb 8192
stack_top:

View File

@@ -1,6 +1,6 @@
#include "print.h" #include "print.h"
void kernel_init(void) void kernel_main(void)
{ {
print("Starting nub-os\n"); print("Starting nub-os\n");
} }

View File

@@ -1,26 +0,0 @@
#include "mem.h"
void* memcpy(void* dest, const void* src, long n)
{
char* d = dest;
const char* s = src;
for (long i = 0; i < n; i++)
d[i] = s[i];
return dest;
}
void* memset(void* dest, int val, long n)
{
char* d = dest;
for (long i = 0; i < n; i++)
d[i] = val;
return dest;
}
long strlen(const char* str)
{
long len = 0;
while (str[len])
len++;
return len;
}

View File

@@ -1,3 +0,0 @@
void* memcpy(void* dest, const void* src, long n);
void* memset(void* dest, int val, long n);
long strlen(const char* str);