Skip to content
Merged
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
6 changes: 4 additions & 2 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: configure
run: sudo apt install gcc build-essential grub2-common qemu-system-x86 xorriso mtools
run: |
sudo apt update
sudo apt install gcc build-essential grub2-common qemu-system-x86 xorriso mtools
- name: make
run: make
- name: run and test
run: make test
run: make clean && make && make test
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ kernel/kernel.elf: $(OBJS)
as $(ASFLAGS) -o $@ $<

serial.log: cis-os.iso
timeout 10s qemu-system-x86_64 -m 256 -cdrom $< -d guest_errors -serial file:$@ || true
timeout 10s qemu-system-x86_64 -nographic -m 256 -cdrom $< -d guest_errors -serial file:$@ --no-reboot -no-shutdown || true

test: serial.log
cat $<
ls
grep -q '\[OK\]' $< && echo "Test passed." || (echo "Test failed." && exit 1)

run: cis-os.iso
qemu-system-x86_64 -m 256 -cdrom $< -d guest_errors -serial file:serial.log
qemu-system-x86_64 -m 256 -cdrom $< -d guest_errors -serial file:serial.log --no-reboot -no-shutdown

clean:
rm -rf isodir
Expand Down
200 changes: 159 additions & 41 deletions kernel/arch/amd64/boot.s
Original file line number Diff line number Diff line change
@@ -1,65 +1,183 @@
.section .multiboot
.align 8
.code32

.extern bss_start
.extern end
.extern phys

.long 0x1BADB002
.long 0x00000003
.long -(0x1BADB002 + 0x00000003)
.set MULTIBOOT_MAGIC, 0x1BADB002
.set MULTIBOOT_FLAG_PAGE_ALIGN, 1 << 0
.set MULTIBOOT_FLAG_MEMORY_INFO, 1 << 1
.set MULTIBOOT_FLAGS, MULTIBOOT_FLAG_PAGE_ALIGN | MULTIBOOT_FLAG_MEMORY_INFO
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_MAGIC + MULTIBOOT_FLAGS)

.section .bss
.align 16
.align 4
multiboot_header:
.long MULTIBOOT_MAGIC
.long MULTIBOOT_FLAGS
.long MULTIBOOT_CHECKSUM
.long multiboot_header
.long phys
.long bss_start
.long end
.long _start

.section .stack, "aw", @nobits
stack_bottom:
.skip 1024 * 4
.skip 16384
.global stack_top
stack_top:

.section .text
.section .bootstrap
.code32
.align 4

.extern paging_main
.extern jmp_long
.type jmp_long, @function

.extern kernel_main64
.type kernel_main64, @function

.global _start
.type _start, @function
_start:
movl $0xFFFFFFFF, %esp

movl $0x80000000, %eax
cpuid
cmpl $0x80000001, %eax
jb no_long_mode
movl $0x80000001, %eax
cpuid
testl $0x20000000, %edx
jnz long_mode_supported
no_long_mode:
hlt
long_mode_supported:
# PAE
movl %cr4, %eax
orl $0x20, %eax
movl %eax, %cr4
movl %ebx, %ecx
addl $16, %ecx
mov %ecx, %esp
call _forward
_forward:
popl %ecx
subl $_forward, %ecx

mov $stack_top, %esp
addl %ecx, %esp
and $-16, %esp

pushl $0
pushl %esp
pushl $0
pushl %eax
pushl $0
pushl %ebx

jmp jmp_long

.align 4

jmp_long:
mov $paging_main, %edi
addl %ecx, %edi
pushl %ecx

mov $0x1007, %eax
add %edi, %eax
mov %eax, (%edi)

add $0x1000, %edi
mov $0x1003, %eax
add %edi, %eax
mov %eax, (%edi)

add $0x1000, %edi

mov $0x87, %ebx
mov $32, %ecx

# GDT
call gdt64_install
.set_entry:
mov %ebx, (%edi)
add $0x200000, %ebx
add $8, %edi
loop .set_entry

# IDT
call idt_install
mov $paging_main, %edi
popl %ecx
addl %ecx, %edi
pushl %ecx
mov %edi, %cr3

# AMD64
movl $0x101, %eax
movl %eax, %cr0
movl %cr0, %eax
orl $0x80000000, %eax
movl %eax, %cr0
mov %cr4, %eax
or $32, %eax
mov %eax, %cr4

ljmp $0x08, $enter_long_mode
mov $0xC0000080, %ecx
rdmsr
or $256, %eax
wrmsr

mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0

popl %ecx

lea (_lgdt+3)(%ecx), %eax
movl (%eax), %ebx
addl %ecx, %ebx
movl %ebx, (%eax)

lea (gdtr+2)(%ecx), %eax
movl (%eax), %ebx
addl %ecx, %ebx
movl %ebx, (%eax)

lea (_ljmp+1)(%ecx), %eax
movl (%eax), %ebx
addl %ecx, %ebx
movl %ebx, (%eax)

pushl $0
pushl %ecx

_lgdt:
lgdt gdtr

_ljmp:
ljmp $0x08, $amd64

.align 8
gdtr:
.word gdt_end - gdt_base
.quad gdt_base

gdt_base:
.quad 0
.word 0
.word 0
.byte 0
.byte 0x9a
.byte 0x20
.byte 0
.word 0xffff
.word 0
.byte 0
.byte 0x92
.byte 0
.byte 0
gdt_end:

.code64
enter_long_mode:
movw $0x10, %ax
.align 8
.section .bootstrap

amd64:
cli
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
finit

movq stack_top, %rsp

call kernel_main64
cli
pop %rcx
pop %rdi
pop %rsi
pop %rdx
callq kernel_main64

halt:
cli
hlt
jmp halt
31 changes: 0 additions & 31 deletions kernel/arch/amd64/gdt.c

This file was deleted.

39 changes: 0 additions & 39 deletions kernel/arch/amd64/idt.c

This file was deleted.

15 changes: 3 additions & 12 deletions kernel/kernel.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <kstdint.h>

uint64_t paging_main[3][512] __attribute__((aligned(4096))) = {0};

static inline void outb(uint16_t port, uint8_t val) {
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
}
Expand All @@ -8,21 +10,10 @@ void hal_write_byte_manual(uint8_t byte) {
outb(0x3f8, byte);
}

void puts(char *str) {
char *video_memory = (char *)0xB8000;
uint16_t *v = (uint16_t *)video_memory;
int i = 0;
while(str[i]) {
v[i] = (str[i] << 8) | 0x0F;
i++;
}
}

void kernel_main64() {
void kernel_main64(uint64_t *multiboot, uint32_t magic, void *esp, uint64_t base) {
hal_write_byte_manual('[');
hal_write_byte_manual('O');
hal_write_byte_manual('K');
hal_write_byte_manual(']');
puts("Hello, World from CIS! ");
for(;;) {}
}
Loading
Loading