From d8a8efe3e8d90ec83069d1c934ff319626e87a2d Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 27 Feb 2025 10:13:35 +0000 Subject: add descriptor_table access_byte --- arch/x86_64/src/boot/boot.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 2197dce2..dbea42a2 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -197,10 +197,10 @@ _start: call enable_paging call enable_sse - cli // Clears the interrupt flag during the GDT setup + cli /* Clears the interrupt flag during the GDT setup */ lgdt (global_descriptor_table_pointer) jmp $global_descriptor_table_code,$_transition_to_long_mode - // The interrupt flag is set in cpp after setting up the GDT + /* The interrupt flag is set in cpp after setting up the GDT */ call halt -- cgit v1.2.3 From 2e4cbd473ff3bb7ac7371af39becf830b4fb753b Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 13 Mar 2025 14:05:45 +0000 Subject: IN_PROGRESS implement gdt initialization --- arch/x86_64/src/boot/boot.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index dbea42a2..39bfe337 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -199,7 +199,7 @@ _start: cli /* Clears the interrupt flag during the GDT setup */ lgdt (global_descriptor_table_pointer) - jmp $global_descriptor_table_code,$_transition_to_long_mode + jmp $global_descriptor_table_code, $_transition_to_long_mode /* The interrupt flag is set in cpp after setting up the GDT */ call halt -- cgit v1.2.3 From b6ee8bec7ed23fd0c544f67f735e96b2bfe67682 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 20 Mar 2025 15:30:18 +0000 Subject: begin implementation of IDT --- arch/x86_64/src/boot/boot.s | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 39bfe337..4fb23e54 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -354,7 +354,20 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 +.global segment_register_reload_pointer +segment_register_reload_pointer: + xor %rax, %rax + mov %rax, %ss + mov %rax, %ds + mov %rax, %es + mov %rax, %fs + mov %rax, %gs + + ret + _transition_to_long_mode: + call segment_register_reload_pointer + xor %rax, %rax mov %rax, %ss mov %rax, %ds -- cgit v1.2.3 From a6c5f6a273d0c5c4161f600fca6d4fe49858c23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 27 Mar 2025 09:40:32 +0000 Subject: Attempt to fix crash in far jump. WIP does not return from call to assembler method --- arch/x86_64/src/boot/boot.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 4fb23e54..35a61217 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -366,7 +366,7 @@ segment_register_reload_pointer: ret _transition_to_long_mode: - call segment_register_reload_pointer + //call segment_register_reload_pointer xor %rax, %rax mov %rax, %ss -- cgit v1.2.3 From 9ddfcd02413a93718e8cde53f9ba5a96a5b29b8f Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 27 Mar 2025 14:02:05 +0000 Subject: update long jump handling --- arch/x86_64/src/boot/boot.s | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 35a61217..f6c79785 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -354,8 +354,11 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -.global segment_register_reload_pointer -segment_register_reload_pointer: +.global reload_segment_register_trampoline +reload_segment_register_trampoline: + jmp 0x08, $_reload_cs + +_reload_cs: xor %rax, %rax mov %rax, %ss mov %rax, %ds @@ -366,14 +369,7 @@ segment_register_reload_pointer: ret _transition_to_long_mode: - //call segment_register_reload_pointer - - xor %rax, %rax - mov %rax, %ss - mov %rax, %ds - mov %rax, %es - mov %rax, %fs - mov %rax, %gs + call _reload_cs movl $0xb8000, (vga_buffer_pointer) -- cgit v1.2.3 From e0eae9b9e905a1842b333823bfdb7c253cda8d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 28 Mar 2025 09:59:09 +0000 Subject: Revert "update long jump handling" This reverts commit 9ddfcd02413a93718e8cde53f9ba5a96a5b29b8f. --- arch/x86_64/src/boot/boot.s | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index f6c79785..35a61217 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -354,11 +354,8 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -.global reload_segment_register_trampoline -reload_segment_register_trampoline: - jmp 0x08, $_reload_cs - -_reload_cs: +.global segment_register_reload_pointer +segment_register_reload_pointer: xor %rax, %rax mov %rax, %ss mov %rax, %ds @@ -369,7 +366,14 @@ _reload_cs: ret _transition_to_long_mode: - call _reload_cs + //call segment_register_reload_pointer + + xor %rax, %rax + mov %rax, %ss + mov %rax, %ds + mov %rax, %es + mov %rax, %fs + mov %rax, %gs movl $0xb8000, (vga_buffer_pointer) -- cgit v1.2.3 From 437c3554f9a86b6347d97f5e2a82543c1e068b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 28 Mar 2025 10:52:25 +0000 Subject: Attempt to fix ljmp. Might not be possible in Long mode --- arch/x86_64/src/boot/boot.s | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 35a61217..bf150a32 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -354,8 +354,8 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -.global segment_register_reload_pointer -segment_register_reload_pointer: +.global reload_segment_register +reload_segment_register: xor %rax, %rax mov %rax, %ss mov %rax, %ds @@ -366,7 +366,7 @@ segment_register_reload_pointer: ret _transition_to_long_mode: - //call segment_register_reload_pointer + //call reload_segment_register xor %rax, %rax mov %rax, %ss -- cgit v1.2.3 From fbd1ebe4f7c5985554fdca7c7fc05de15d47dd3a Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 28 Mar 2025 18:35:28 +0100 Subject: gdt: fix reload of GDT The core problems were/are the following: - The flags of the segments were not entirely correct. Please recheck them against the spec! - The GDT pointer did not contain the address of the first (null) GTD entry, but the address of the stl::vector containing the GDT entries. - The far pointer must consist of: - the address to jump to - the byte index into the GDT for the desired segement descriptor to be loaded into CS. - The type of the "dummy" function we jump to was wrong (it's a function, we should declare it as such). - We cannot enable interrupts right now, since we die with a triple fault. This is caused by some initia fault which seems to lead to a general protection fault, which then triple faults since we cannot find the IDT. Some FIXMEs have been added to the code. Please look at them carefully and compare things against the specs. --- arch/x86_64/src/boot/boot.s | 3 +++ 1 file changed, 3 insertions(+) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index bf150a32..108dbe5b 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -356,6 +356,9 @@ prepare_page_maps: .global reload_segment_register reload_segment_register: + // FIXME: maybe we should set the actually correct values here. We'd need to communicate them down from C++. + // Alternatively, we could probably implement this as a [[gnu::naked]] function in C++, to have easier access to + // arguments and symbols. Maybe later. xor %rax, %rax mov %rax, %ss mov %rax, %ds -- cgit v1.2.3 From aba154ad01fc0e1e1274f2582b1493e78daa2559 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 29 Mar 2025 14:47:04 +0000 Subject: fix gdt segments, improve idt and trial&error for triple fault --- arch/x86_64/src/boot/boot.s | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 108dbe5b..38a8af40 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -197,10 +197,8 @@ _start: call enable_paging call enable_sse - cli /* Clears the interrupt flag during the GDT setup */ lgdt (global_descriptor_table_pointer) jmp $global_descriptor_table_code, $_transition_to_long_mode - /* The interrupt flag is set in cpp after setting up the GDT */ call halt @@ -369,14 +367,7 @@ reload_segment_register: ret _transition_to_long_mode: - //call reload_segment_register - - xor %rax, %rax - mov %rax, %ss - mov %rax, %ds - mov %rax, %es - mov %rax, %fs - mov %rax, %gs + call reload_segment_register movl $0xb8000, (vga_buffer_pointer) -- cgit v1.2.3 From a8852f91967a7e55e62e30f5cc07d076092b8b78 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sat, 5 Apr 2025 15:27:20 +0000 Subject: add wip context switch to user mode --- arch/x86_64/src/boot/boot.s | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 38a8af40..79320456 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,11 +352,7 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -.global reload_segment_register -reload_segment_register: - // FIXME: maybe we should set the actually correct values here. We'd need to communicate them down from C++. - // Alternatively, we could probably implement this as a [[gnu::naked]] function in C++, to have easier access to - // arguments and symbols. Maybe later. +_transition_to_long_mode: xor %rax, %rax mov %rax, %ss mov %rax, %ds @@ -364,11 +360,6 @@ reload_segment_register: mov %rax, %fs mov %rax, %gs - ret - -_transition_to_long_mode: - call reload_segment_register - movl $0xb8000, (vga_buffer_pointer) call _init -- cgit v1.2.3 From 350aedae0e50749f9821ac7dc6b8316cf35f24bb Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 6 Apr 2025 07:47:27 +0000 Subject: wip context switch in asm --- arch/x86_64/src/boot/boot.s | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 79320456..85ae1a11 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,6 +352,35 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 +.global context_switch +context_switch: + // ring 3 data with bottom 2 bits set for ring 3 + mov $((4 * 16) | 3), %rax + mov %rax, %ds + mov %rax, %es + mov %rax, %fs + mov %rax, %gs + // SS is handled by iret https://wiki.osdev.org/Getting_to_Ring_3 + + // set up the stack frame iret expects + mov %rsp, %rax + // user data selector + push $((4 * 16) | 3) + // current exp + push %rax + // push eflags + pushf + // push code selector (ring 3 code with bottom 2 bits set for ring 3) + push $((3 * 16) | 3) + // instruction address to return to + push test_function + + iret + +test_function: + cli + ret + _transition_to_long_mode: xor %rax, %rax mov %rax, %ss -- cgit v1.2.3 From 295f1bc9a29267b72504fffb582a08c2467b1a7f Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 10 Apr 2025 09:32:20 +0000 Subject: fix context_switch function --- arch/x86_64/src/boot/boot.s | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 85ae1a11..139fd1aa 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,6 +352,10 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 +test_function: + cli + ret + .global context_switch context_switch: // ring 3 data with bottom 2 bits set for ring 3 @@ -366,20 +370,17 @@ context_switch: mov %rsp, %rax // user data selector push $((4 * 16) | 3) - // current exp + // current rsp push %rax // push eflags pushf // push code selector (ring 3 code with bottom 2 bits set for ring 3) push $((3 * 16) | 3) - // instruction address to return to - push test_function - - iret + // push instruction address to return to + lea [test_function], %rax + push %rax -test_function: - cli - ret + iretq _transition_to_long_mode: xor %rax, %rax -- cgit v1.2.3 From 62d7fa83e831e84ea851d97b5c957146880ad69a Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 10 Apr 2025 10:28:46 +0000 Subject: move context_switch function into cpp code --- arch/x86_64/src/boot/boot.s | 30 ------------------------------ 1 file changed, 30 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 139fd1aa..79320456 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,36 +352,6 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -test_function: - cli - ret - -.global context_switch -context_switch: - // ring 3 data with bottom 2 bits set for ring 3 - mov $((4 * 16) | 3), %rax - mov %rax, %ds - mov %rax, %es - mov %rax, %fs - mov %rax, %gs - // SS is handled by iret https://wiki.osdev.org/Getting_to_Ring_3 - - // set up the stack frame iret expects - mov %rsp, %rax - // user data selector - push $((4 * 16) | 3) - // current rsp - push %rax - // push eflags - pushf - // push code selector (ring 3 code with bottom 2 bits set for ring 3) - push $((3 * 16) | 3) - // push instruction address to return to - lea [test_function], %rax - push %rax - - iretq - _transition_to_long_mode: xor %rax, %rax mov %rax, %ss -- cgit v1.2.3 From c865eff02ae1978b4f665432d853374d1ffacecf Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 27 Apr 2025 10:21:29 +0000 Subject: create trampoline for syscall --- arch/x86_64/src/boot/boot.s | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 79320456..24b38cae 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,6 +352,31 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 +syscall_target: + iretq + +.global syscall_trampoline +syscall_trampoline: + /* Write target function pointer in IA32_LSTAR MSR */ + mov $0xC0000082, %ecx /* IA32_LSTAR MSR */ + lea [syscall_target], %rax + lea [syscall_target], %rdx + shr $32, %rdx + wrmsr + + /* Write ... in IA32_LSTAR MSR */ + mov $0xC0000084, %ecx /* IA32_FMASK MSR */ + mov $0x0, %rax /* ... lower 32 bits */ + mov $0x0, %rdx /* ... upper 32 bits */ + wrmsr + + /* Write Segment selector in IA32_STAR MSR */ + mov $0xC0000081, %ecx /* IA32_STAR MSR */ + mov $0x10, %rax /* SS lower 32 bits */ + mov $0x0, %rdx /* SS upper 32 bits */ + wrmsr + + _transition_to_long_mode: xor %rax, %rax mov %rax, %ss -- cgit v1.2.3 From 7261c64bb236a313ed8846a9c9dbded6890a9e98 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Sun, 27 Apr 2025 11:20:02 +0000 Subject: wip implement syscall in cpp --- arch/x86_64/src/boot/boot.s | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'arch/x86_64/src/boot') diff --git a/arch/x86_64/src/boot/boot.s b/arch/x86_64/src/boot/boot.s index 24b38cae..79320456 100644 --- a/arch/x86_64/src/boot/boot.s +++ b/arch/x86_64/src/boot/boot.s @@ -352,31 +352,6 @@ prepare_page_maps: .section .boot_text, "ax", @progbits .code64 -syscall_target: - iretq - -.global syscall_trampoline -syscall_trampoline: - /* Write target function pointer in IA32_LSTAR MSR */ - mov $0xC0000082, %ecx /* IA32_LSTAR MSR */ - lea [syscall_target], %rax - lea [syscall_target], %rdx - shr $32, %rdx - wrmsr - - /* Write ... in IA32_LSTAR MSR */ - mov $0xC0000084, %ecx /* IA32_FMASK MSR */ - mov $0x0, %rax /* ... lower 32 bits */ - mov $0x0, %rdx /* ... upper 32 bits */ - wrmsr - - /* Write Segment selector in IA32_STAR MSR */ - mov $0xC0000081, %ecx /* IA32_STAR MSR */ - mov $0x10, %rax /* SS lower 32 bits */ - mov $0x0, %rdx /* SS upper 32 bits */ - wrmsr - - _transition_to_long_mode: xor %rax, %rax mov %rax, %ss -- cgit v1.2.3