diff options
Diffstat (limited to 'kernel/arch/x86_64')
-rw-r--r-- | kernel/arch/x86_64/asm/boot.s | 31 | ||||
-rw-r--r-- | kernel/arch/x86_64/asm/isr.s | 155 | ||||
-rw-r--r-- | kernel/arch/x86_64/asm/spinlock.s | 46 | ||||
-rw-r--r-- | kernel/arch/x86_64/asm/stack_trace.s | 40 | ||||
-rw-r--r-- | kernel/arch/x86_64/linker.ld | 21 | ||||
-rw-r--r-- | kernel/arch/x86_64/make.config | 14 |
6 files changed, 307 insertions, 0 deletions
diff --git a/kernel/arch/x86_64/asm/boot.s b/kernel/arch/x86_64/asm/boot.s new file mode 100644 index 0000000..973373b --- /dev/null +++ b/kernel/arch/x86_64/asm/boot.s @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2025 Danny Holman <dholman@gymli.org> + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see <https://www.gnu.org/licenses/>. + */ + +.section .text + +.global _start +_start: + rdseed %rax + movq %rax, (__stack_chk_guard) + + cld + call kernel_main + +1: hlt + jmp 1b diff --git a/kernel/arch/x86_64/asm/isr.s b/kernel/arch/x86_64/asm/isr.s new file mode 100644 index 0000000..a79532d --- /dev/null +++ b/kernel/arch/x86_64/asm/isr.s @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2025 Danny Holman <dholman@gymli.org> + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see <https://www.gnu.org/licenses/>. + */ + +.section .text + +.macro isr_err_stub num +.global isr_stub_\num +.type isr_stub_\num, @function +isr_stub_\num: + cli + pushq $\num + jmp isr_frame_asm +.endm +.macro isr_no_err_stub num +.global isr_stub_\num +.type isr_stub_\num, @function +isr_stub_\num: + cli + pushq $0 + pushq $\num + jmp isr_frame_asm +.endm + +.macro irq_stub num +.global irq_stub_\num +.type irq_stub_\num, @function +irq_stub_\num: + cli + pushq $0 + pushq $\num+32 + jmp isr_frame_asm +.endm + +isr_frame_asm: + pushq %rax + pushq %rbx + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 + + movq %cr0, %rax + pushq %rax + movq %cr2, %rax + pushq %rax + movq %cr3, %rax + pushq %rax + movq %cr4, %rax + pushq %rax + + movq %rsp, %rdi + cld + call isr_dispatch + + popq %rax + movq %rax, %cr4 + popq %rax + movq %rax, %cr3 + popq %rax + movq %rax, %cr2 + popq %rax + movq %rax, %cr0 + + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %r11 + popq %r10 + popq %r9 + popq %r8 + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %rbx + popq %rax + addq $16, %rsp + iretq + +isr_no_err_stub 0 +isr_no_err_stub 1 +isr_no_err_stub 2 +isr_no_err_stub 3 +isr_no_err_stub 4 +isr_no_err_stub 5 +isr_no_err_stub 6 +isr_no_err_stub 7 +isr_err_stub 8 +isr_no_err_stub 9 +isr_err_stub 10 +isr_err_stub 11 +isr_err_stub 12 +isr_err_stub 13 +isr_err_stub 14 +isr_no_err_stub 15 +isr_no_err_stub 16 +isr_err_stub 17 +isr_no_err_stub 18 +isr_no_err_stub 19 +isr_no_err_stub 20 +isr_err_stub 21 +isr_no_err_stub 22 +isr_no_err_stub 23 +isr_no_err_stub 24 +isr_no_err_stub 25 +isr_no_err_stub 26 +isr_no_err_stub 27 +isr_no_err_stub 28 +isr_err_stub 29 +isr_err_stub 30 +isr_no_err_stub 31 + +irq_stub 0 +irq_stub 1 +irq_stub 2 +irq_stub 3 +irq_stub 4 +irq_stub 5 +irq_stub 6 +irq_stub 7 +irq_stub 8 +irq_stub 9 +irq_stub 10 +irq_stub 11 +irq_stub 12 +irq_stub 13 +irq_stub 14 +irq_stub 15 + +isr_no_err_stub 128 diff --git a/kernel/arch/x86_64/asm/spinlock.s b/kernel/arch/x86_64/asm/spinlock.s new file mode 100644 index 0000000..1c69820 --- /dev/null +++ b/kernel/arch/x86_64/asm/spinlock.s @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2025 Danny Holman <dholman@gymli.org> + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see <https://www.gnu.org/licenses/>. + */ + +.section .text + +.global acquire_lock +acquire_lock: + pushq %rbp + movq %rsp, %rbp + + movq $0, %rax + lock bts %rax, (%rdi) +.loop: + pause + testq %rax, %rax + jz .done + jmp .loop +.done: + popq %rbp + ret + +.global release_lock +release_lock: + pushq %rbp + movq %rsp, %rbp + + movq $0, %rax + lock btr %rax, (%rdi) + + ret diff --git a/kernel/arch/x86_64/asm/stack_trace.s b/kernel/arch/x86_64/asm/stack_trace.s new file mode 100644 index 0000000..57d98b9 --- /dev/null +++ b/kernel/arch/x86_64/asm/stack_trace.s @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2025 Danny Holman <dholman@gymli.org> + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see <https://www.gnu.org/licenses/>. + */ + +.section .text + +.global walk_stack +walk_stack: + pushq %rbp + movq %rsp, %rbp + + xorq %rdx, %rdx +.next_frame: + cmpq %rsi, %rdx + jge .done + + movq 8(%rbp), %rax + movq %rax, (%rdi, %rdx, 8) + + incq %rdx + + movq (%rbp), %rbp + jmp .next_frame +.done: + ret diff --git a/kernel/arch/x86_64/linker.ld b/kernel/arch/x86_64/linker.ld new file mode 100644 index 0000000..460bb14 --- /dev/null +++ b/kernel/arch/x86_64/linker.ld @@ -0,0 +1,21 @@ +ENTRY(_start) + +SECTIONS { + . = 0x10000; + __kstart = .; + + .text : { + *(.text) + } + + .data : { + *(.data) + } + + .bss : { + *(.bss) + *(COMMON) + } + + __kend = .; +} diff --git a/kernel/arch/x86_64/make.config b/kernel/arch/x86_64/make.config new file mode 100644 index 0000000..2347ce2 --- /dev/null +++ b/kernel/arch/x86_64/make.config @@ -0,0 +1,14 @@ +KERNEL_ARCH_INCLUDE=-I$(ARCHDIR)/include +KERNEL_ARCH_CFLAGS=$(SYS_CFLAGS) -mno-red-zone -mno-mmx -mno-sse -mno-sse2 +KERNEL_ARCH_LDFLAGS= +KERNEL_ARCH_LIBS= + +KERNEL_ARCH_OBJS=$(ARCHDIR)/asm/boot.o \ + $(ARCHDIR)/asm/isr.o \ + $(ARCHDIR)/asm/paging.o \ + $(ARCHDIR)/asm/spinlock.o \ + $(ARCHDIR)/asm/stack_trace.o \ + $(ARCHDIR)/asm/switch_thread.o \ + $(ARCHDIR)/kernel/serial.o \ + $(ARCHDIR)/kernel/syscall.o \ + $(ARCHDIR)/mem/gdt.o \ |