-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
#include "lib/multiboot2.hpp"
.set GDT.NULL, . - GDT
.quad 0
.set GDT.KERNEL_DATA, . - GDT
- .short 0xFFFF # Limit[0:16]
- .short 0x0000 # Base[0:16]
- .byte 0x00 # Base[16:24]
- .byte 0b10010010 # Access: present | writeable | ring0
- .byte 0b11001111 # Flags: page_gran | d_b, Limit[16:20]
- .byte 0x00 # Base[24:32]
+ .short 0xFFFF // Limit[0:16]
+ .short 0x0000 // Base[0:16]
+ .byte 0x00 // Base[16:24]
+ .byte 0b10010010 // Access: present | writeable | ring0
+ .byte 0b11001111 // Flags: page_gran | d_b, Limit[16:20]
+ .byte 0x00 // Base[24:32]
.set GDT.KERNEL_CODE, . - GDT
- .short 0xFFFF # Limit[0:16]
- .short 0x0000 # Base[0:16]
- .byte 0x00 # Base[16:24]
- .byte 0b10011010 # Access: present | readable | ring0
- .byte 0b10101111 # Flags: page_gran | long_mode, Limit[16:20]
- .byte 0x00 # Base[24:32]
+ .short 0xFFFF // Limit[0:16]
+ .short 0x0000 // Base[0:16]
+ .byte 0x00 // Base[16:24]
+ .byte 0b10011010 // Access: present | readable | ring0
+ .byte 0b10101111 // Flags: page_gran | long_mode, Limit[16:20]
+ .byte 0x00 // Base[24:32]
.set GDT.USER_DATA, . - GDT
- .short 0xFFFF # Limit[0:16]
- .short 0x0000 # Base[0:16]
- .byte 0x00 # Base[16:24]
- .byte 0b11110010 # Access: present | writeable | ring3
- .byte 0b11001111 # Flags: page_gran | d_b, Limit[16:20]
- .byte 0x00 # Base[24:32]
+ .short 0xFFFF // Limit[0:16]
+ .short 0x0000 // Base[0:16]
+ .byte 0x00 // Base[16:24]
+ .byte 0b11110010 // Access: present | writeable | ring3
+ .byte 0b11001111 // Flags: page_gran | d_b, Limit[16:20]
+ .byte 0x00 // Base[24:32]
.set GDT.USER_CODE, . - GDT
- .short 0xFFFF # Limit[0:16]
- .short 0x0000 # Base[0:16]
- .byte 0x00 # Base[16:24]
- .byte 0b11111010 # Access: present | readable | ring3
- .byte 0b10101111 # Flags: page_gran | long_mode, Limit[16:20]
- .byte 0x00 # Base[24:32]
+ .short 0xFFFF // Limit[0:16]
+ .short 0x0000 // Base[0:16]
+ .byte 0x00 // Base[16:24]
+ .byte 0b11111010 // Access: present | readable | ring3
+ .byte 0b10101111 // Flags: page_gran | long_mode, Limit[16:20]
+ .byte 0x00 // Base[24:32]
.globl GDT.TSS
.set GDT.TSS, . - GDT
- .short (TSS_SIZE - 1) & 0xFFFF # Limit[0:16]
- .short 0x0000 # Base[0:16] # TSS & 0xFFFF
- .byte 0x00 # Base[16:24] # (TSS >> 16) & 0xFF
- .byte 0b10001001 # Access
- .byte (0b0000 << 4) | (((TSS_SIZE - 1) >> 16) & 0xF) # Flags, Limit[16:20]
- .byte 0x00 # Base[24:32] # (TSS >> 24) & 0xFF
- .int 0 # Base[32:63] # (TSS >> 32) & 0xFFFFFFFF
- .int 0 # reserved
+ .short (TSS_SIZE - 1) & 0xFFFF // Limit[0:16]
+ .short 0x0000 // Base[0:16] // TSS & 0xFFFF
+ .byte 0x00 // Base[16:24] // (TSS >> 16) & 0xFF
+ .byte 0b10001001 // Access
+ .byte (0b0000 << 4) | (((TSS_SIZE - 1) >> 16) & 0xF) // Flags, Limit[16:20]
+ .byte 0x00 // Base[24:32] // (TSS >> 24) & 0xFF
+ .int 0 // Base[32:63] // (TSS >> 32) & 0xFFFFFFFF
+ .int 0 // reserved
GDT.PTR:
.short . - GDT - 1
.quad GDT - KERNEL_VMA
PML4T: .quad PDPT_low - KERNEL_VMA + 3
.skip 0x1000 - 16
.quad PDPT_high - KERNEL_VMA + 3
-PDPT_low: .quad 0x83 # 1GiB page | R/W | P
+PDPT_low: .quad 0x83 // 1GiB page | R/W | P
.skip 0x1000 - 8
PDPT_high: .skip 0x1000 - 16
- .quad 0x183 # G | 1GiB page | R/W | P
+ .quad 0x183 // G | 1GiB page | R/W | P
.quad 0x183 + 1024 * 1024 * 1024
.section .bss
phys_mem_map: .skip 0x1000 * 128 - 8
.align 16
stack_bottom:
-.skip 4096 * 4 # 16KiB
+.skip 4096 * 4 // 16KiB
.globl stack_top
stack_top:
interrupt_stack_bottom:
-.skip 4096 * 4 # 16KiB
+.skip 4096 * 4 // 16KiB
.globl interrupt_stack_top
interrupt_stack_top:
push %ecx
popfl
xor %ecx, %eax
- jz halt # No cpuid.
+ jz halt // No cpuid.
ret
check_long_mode_support:
mov $0x80000000, %eax
cpuid
cmp $0x80000001, %eax
- jb halt # No ability to check for long mode.
+ jb halt // No ability to check for long mode.
mov $0x80000001, %eax
cpuid
- # Long mode:
+ // Long mode:
test $1 << 29, %edx
- jz halt # No long mode.
- # 1GiB pages:
+ jz halt // No long mode.
+ // 1GiB pages:
test $1 << 26, %edx
- jz halt # No 1GiB pages.
+ jz halt // No 1GiB pages.
ret
.globl _start
cli
mov $stack_top - KERNEL_VMA, %esp
- # Save multiboot info:
- push $0 # /
- push %eax # \ magic
- push $0 # /
- push %ebx # \ info structure
+ // Save multiboot info:
+ push $0 // /
+ push %eax // \ magic
+ push $0 // /
+ push %ebx // \ info structure
call check_cpuid_support
call check_long_mode_support
- # Disable 32-bit paging:
+ // Disable 32-bit paging:
mov %cr0, %eax
and $~(1 << 31), %eax
mov %eax, %cr0
- # Setup 64-bit paging:
+ // Setup 64-bit paging:
mov $PML4T - KERNEL_VMA, %eax
- or $0x10, %eax # NO CACHE
+ or $0x10, %eax // NO CACHE
mov %eax, %cr3
- # Enable PAE paging, and global pages:
+ // Enable PAE paging, and global pages:
mov %cr4, %eax
- or $1 << 5, %eax # PAE
- or $1 << 7, %eax # PGE
+ or $1 << 5, %eax // PAE
+ or $1 << 7, %eax // PGE
mov %eax, %cr4
- # Set long mode bit (and NXE):
+ // Set long mode bit (and NXE):
mov $0xC0000080, %ecx
rdmsr
or $1 << 8, %eax
or $1 << 11, %eax
wrmsr
- # Enable paging:
+ // Enable paging:
mov %cr0, %eax
or $1 << 31, %eax
mov %eax, %cr0
- # Jump to 64 bits:
+ // Jump to 64 bits:
lgdt GDT.PTR - KERNEL_VMA
jmp $GDT.KERNEL_CODE, $.trampoline
mov %esp, %esp
movq $GDT, GDT.PTR + 2
- lgdt GDT.PTR # Reload GDT in higher half.
+ lgdt GDT.PTR // Reload GDT in higher half.
pushq $GDT.KERNEL_CODE
movabsq $.trampoline2, %rax
pushq %rax
- lretq # Jump to new GDT.
+ lretq // Jump to new GDT.
.trampoline2:
- mov $GDT.KERNEL_DATA, %ax # Set the A-register to the data descriptor.
- mov %ax, %ds # Set the data segment to the A-register.
- mov %ax, %es # Set the extra segment to the A-register.
- mov %ax, %fs # Set the F-segment to the A-register.
- mov %ax, %gs # Set the G-segment to the A-register.
- mov %ax, %ss # Set the stack segment to the A-register.
+ mov $GDT.KERNEL_DATA, %ax // Set the A-register to the data descriptor.
+ mov %ax, %ds // Set the data segment to the A-register.
+ mov %ax, %es // Set the extra segment to the A-register.
+ mov %ax, %fs // Set the F-segment to the A-register.
+ mov %ax, %gs // Set the G-segment to the A-register.
+ mov %ax, %ss // Set the stack segment to the A-register.
mov $.higher_half, %rax
jmp *%rax
.section .text
.higher_half:
mov %esp, %esp
- pop %r14 # Multiboot info address.
- pop %r15 # Multiboot magic.
+ pop %r14 // Multiboot info address.
+ pop %r15 // Multiboot magic.
mov $stack_top, %rsp
- # map physical memory in kernel:
+ // map physical memory in kernel:
mov $phys_mem_map - KERNEL_VMA + 3, %rbx
mov $PML4T + 256 * 8, %rdi
mov $128, %rcx
add $8, %rdi
loop 1b
- mov $0x183, %rbx # G | R/W | P
+ mov $0x183, %rbx // G | R/W | P
mov $phys_mem_map, %rdi
mov $128 * 512, %rcx
1: mov %rbx, (%rdi)
add $8, %rdi
loop 1b
- # Setup new page tables:
+ // Setup new page tables:
mov $PML4T - KERNEL_VMA, %rax
- or $0x10, %eax # NO CACHE
+ or $0x10, %eax // NO CACHE
mov %rax, %cr3
- # Setup TSS address in GDT
+ // Setup TSS address in GDT
movabs $TSS, %rax
shr $0, %rax
and $0xFFFF, %rax
mov %r14, %rsi
mov %r15, %rdi
- call kmain # With the two arguments popped earlier.
+ call kmain // With the two arguments popped earlier.
- # kmain should never terminate, but, oh well.
+ // kmain should never terminate, but, oh well.
call _fini
cli
-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
#define PUSH_REGS \
pushq %rax; \
-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
.section .init
.global _init
-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
.section .text
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
- # iretq handles cs and ss
+ // iretq handles cs and ss
pushq $(8 * 3) | 3
pushq %rsi
.globl syscall_64bit_handler
syscall_64bit_handler:
- # Save all registers:
+ // Save all registers:
.irp reg,rax,rbx,rcx,rdx,rsp,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15
mov %\reg, save_reg_tmp_\reg
.endr
- # Setup stack:
+ // Setup stack:
mov $stack_top, %rsp
- # Get process data location:
+ // Get process data location:
mov current_pid, %rdi
call get_process
- # Really save all regs:
+ // Really save all regs:
.irp reg,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
- mov save_regs_tmp + \reg * 8, %rbx # Get %r\reg
- mov %rbx, 8 + \reg * 8(%rax) # And store it.
+ mov save_regs_tmp + \reg * 8, %rbx // Get %r\reg
+ mov %rbx, 8 + \reg * 8(%rax) // And store it.
.endr
- mov save_reg_tmp_rcx, %rbx # Save the rip also.
+ mov save_reg_tmp_rcx, %rbx // Save the rip also.
mov %rbx, 0x88(%rax)
- # Get ready for syscall:
+ // Get ready for syscall:
mov save_reg_tmp_rdi, %rdi
mov save_reg_tmp_rsi, %rsi
mov save_reg_tmp_rdx, %rdx
- mov save_reg_tmp_r10, %rcx # Syscall ABI -> C ABI
+ mov save_reg_tmp_r10, %rcx // Syscall ABI -> C ABI
mov save_reg_tmp_r8, %r8
mov save_reg_tmp_r9, %r9
mov save_reg_tmp_rax, %rax
jae incorrect_syscall
callq *syscalls_call_table(, %rax, 8)
syscall_end:
- mov %rax, %rbp # Save the return
- # Get process data location:
+ mov %rax, %rbp // Save the return
+ // Get process data location:
mov current_pid, %rdi
call get_process
- # Restore all regs:
+ // Restore all regs:
.irp reg,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
- mov 8 + \reg * 8(%rax), %rbx # Get %r\reg
- mov %rbx, save_regs_tmp + \reg * 8 # And restore it.
+ mov 8 + \reg * 8(%rax), %rbx // Get %r\reg
+ mov %rbx, save_regs_tmp + \reg * 8 // And restore it.
.endr
- mov %rbp, save_reg_tmp_rax # Restore the return register as well.
- mov 0x88(%rax), %rbx # Save the rip in place of rcx, because this is sysret.
+ mov %rbp, save_reg_tmp_rax // Restore the return register as well.
+ mov 0x88(%rax), %rbx // Save the rip in place of rcx, because this is sysret.
mov %rbx, save_reg_tmp_rcx
- # Really restore all regs:
+ // Really restore all regs:
.irp reg,rax,rbx,rcx,rdx,rsp,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15
mov save_reg_tmp_\reg, %\reg
.endr
- # Current process registers have now all been restored.
+ // Current process registers have now all been restored.
sysretq
incorrect_syscall:
call syscall_rax_error_handler
.quad syscall_write
.quad syscall_close
syscalls_call_table_end:
-.set syscall_n, (syscalls_call_table_end - syscalls_call_table) >> 3 # Because / 8 *doesn't work* !
+.set syscall_n, (syscalls_call_table_end - syscalls_call_table) >> 3 // Because / 8 *doesn't work* !
.globl syscall_n
.section .data
-# Copyright 2023 Amélia COUTARD.
-#
-# This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free Software Foundation,
-# either version 3 of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that 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 this program. If
-# not, see <https://www.gnu.org/licenses/>.
+// Copyright 2023 Amélia COUTARD.
+//
+// This file from the program "voyage au centre des fichiers" is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free Software Foundation,
+// either version 3 of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that 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 this program. If
+// not, see <https://www.gnu.org/licenses/>.
.section .text