From c098c722d562986923b57aa17380ab554803d756 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 5 Mar 2019 19:24:14 +0100 Subject: [PATCH] stuff --- boot/loader/cpu/cpuid.inc | 70 +++++++++++++++++++ boot/loader/mem/management.inc | 54 +++++++++++++++ boot/loader/mem/structures.inc | 113 +++++++++++++++++++++++++++++++ boot/loader/multiboot/check.inc | 35 ++++++++++ boot/loader/multiboot/header.inc | 34 ++++++++++ 5 files changed, 306 insertions(+) create mode 100644 boot/loader/cpu/cpuid.inc create mode 100644 boot/loader/mem/management.inc create mode 100644 boot/loader/mem/structures.inc create mode 100644 boot/loader/multiboot/check.inc create mode 100644 boot/loader/multiboot/header.inc diff --git a/boot/loader/cpu/cpuid.inc b/boot/loader/cpu/cpuid.inc new file mode 100644 index 0000000..44e19e7 --- /dev/null +++ b/boot/loader/cpu/cpuid.inc @@ -0,0 +1,70 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Basic realmode CPU Detection ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K 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. ; +; ; +; OS/K 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 OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +[BITS 32] + +; ---------------------------------------------------------------------------- ; +; Checks if the CPU is compatible with 64-bits operating systems ; +; If the 21th bit of the eax register is set, then CPUID is supported ; +; We then test CPUID's result to see if long mode is supported ; +; ---------------------------------------------------------------------------- ; +Is64Bits: + ;; test if extended processor info in available + mov eax, 0x80000000 ; implicit argument for cpuid + cpuid ; get highest supported argument + cmp eax, 0x80000001 ; it needs to be at least 0x80000001 + jb .no_64 ; if it's less, the CPU is too old for long mode + + ;; use extended info to test if long mode is available + mov eax, 0x80000001 ; argument for extended processor info + cpuid ; returns various feature bits in ecx and edx + test edx, 1 << 29 ; test if the LM-bit is set in the D-register + jz .no_64 ; If it's not set, there is no long mode + ret +.no_64: + mov al, "2" ; ERROR 1 : 64bits unsupported + jmp Error + +; ---------------------------------------------------------------------------- ; +; Check if CPUID is supported by flip the bit 21 (ID bit) in the FLAGS ; +; register. If we can flip, CPUID is available. ; +; ---------------------------------------------------------------------------- ; +Check_cpuid: + pushfd ; Copy FLAGS in to EAX via stack + pop eax + mov ecx, eax ; Copy to ECX as well for comparing later on + xor eax, 1 << 21 ; Flip the ID bit + push eax ; Copy EAX to FLAGS via the stack + popfd + pushfd ; Copy FLAGS back to EAX (with the flipped bit if CPUID is supported) + pop eax + push ecx ; flipping the flipped ID bit back if it was ever flipped like flipper + popfd + cmp eax, ecx ; VERITY MOMENT + je .no_cpuid + ret +.no_cpuid: + mov al, "1" ; ERROR 1 : CPUID UNSUPPORTED + jmp Error diff --git a/boot/loader/mem/management.inc b/boot/loader/mem/management.inc new file mode 100644 index 0000000..3790a9d --- /dev/null +++ b/boot/loader/mem/management.inc @@ -0,0 +1,54 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Memory management from protected mode ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K 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. ; +; ; +; OS/K 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 OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +[BITS 32] + +; ---------------------------------------------------------------------------- ; +; Constructor for the page tables in protected mode ; +; ---------------------------------------------------------------------------- ; +Setup_paging: + ;; Map the first PML4 entry to PDP table + mov eax, PDP_table + or eax, 0b11 ; present + writable + mov [PML4_table], eax + + ;; Map the first PDP entry to PD table + mov eax, PD_table + or eax, 0b11 ; present + writable + mov [PDP_table], eax + + ;; Map each PD entry to a 'huge' 2MiB page + mov ecx, 0 ; counter variable +.map_p2_table: + ;; map ecx-th PD entry to a huge page that starts at address 2MiB*ecx + mov eax, 0x200000 + mul ecx ; start address of ecx-th page + or eax, 0b10000011 ; present + writable + huge + mov [PD_table + ecx * 8], eax + inc ecx + cmp ecx, 512 ; if counter == 512, the whole PD table is mapped + jne .map_p2_table ; else map the next entry + + ret diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc new file mode 100644 index 0000000..31e98b4 --- /dev/null +++ b/boot/loader/mem/structures.inc @@ -0,0 +1,113 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Memory Design Structures ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K 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. ; +; ; +; OS/K 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 OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +[BITS 32] + +;; GDT WITH DOC +ALIGN 4 +GDT64: + NULL_SELECTOR: ;; null selector within 64 bits + dw GDT_LENGTH ; limit of GDT + dw GDT64 ; linear address of GDT + dd 0x0 ; + + CODE_SELECTOR: ;; 32-bit code selector (ring 0) + dw 0x0000FFFF ; Segment Limit + db 0x0, 0x0, 0x0 ; Base Address + db 10011010b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- 1 when segment used. + ; | | | | | | `------ 1 when writable. + ; | | | | | `------- 1 if "conformant". Don't know what is it... spectral ? xD + ; | | | | `-------- 1 always + ; | | | `--------- 1 for segment descriptor, 0 for system descriptor + ; | | `---------- DPL !!! 0 for ring 0 + ; | `----------- DPL (2/2) + ; `------------ 1 if in physical memory, 0 if page fault + db 11001111b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- Limit 16 + ; | | | | | | `------ Limit 17 + ; | | | | | `------- Limit 18 + ; | | | | `-------- Limit 19 + ; | | | `--------- available for use + ; | | `---------- 0 always + ; | `----------- size of data. 1 for 32bits + ; `------------ 0 if limit is in Bytes, 1 if it's in pages (4ko) + db 0x0 ; Base Address + + DATA_SELECTOR: ;; flat data selector (ring 0) + dw 0x0000FFFF ; Segment Limit + db 0x0, 0x0, 0x0 ; Base Address + db 10010010b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- 1 when segment used. + ; | | | | | | `------ 1 when writable. + ; | | | | | `------- expansion direction. 1 for a LIFO + ; | | | | `-------- 1 always + ; | | | `--------- 1 for segment descriptor, 0 for system descriptor + ; | | `---------- DPL !!! 0 for ring 0 + ; | `----------- DPL (2/2) + ; `------------ 1 if in physical memory, 0 if page fault + db 10001111b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- Limit 16 + ; | | | | | | `------ Limit 17 + ; | | | | | `------- Limit 18 + ; | | | | `-------- Limit 19 + ; | | | `--------- available for use + ; | | `---------- 0 always + ; | `----------- size of data. 1 for 32bits + ; `------------ 0 if limit is in Bytes, 1 if it's in pages (4ko) + db 0x0 ; Base Address + + LONG_SELECTOR: ;; 64-bit code selector (ring 0) + dw 0x0000FFFF ; Segment Limit + db 0x0, 0x0, 0x0 ; Base Address + db 10011010b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- 1 when segment used. + ; | | | | | | `------ 1 when writable. + ; | | | | | `------- 1 if "conformant". Don't know what is it... spectral ? xD + ; | | | | `-------- 1 always + ; | | | `--------- 1 for segment descriptor, 0 for system descriptor + ; | | `---------- DPL !!! 0 for ring 0 + ; | `----------- DPL (2/2) + ; `------------ 1 if in physical memory, 0 if page fault + db 10101111b ; |7|6|5|4|3|2|1|0| + ; | | | | | | | `----- Limit 16 + ; | | | | | | `------ Limit 17 + ; | | | | | `------- Limit 18 + ; | | | | `-------- Limit 19 + ; | | | `--------- available for use + ; | | `---------- 0 always + ; | `----------- size of data. 1 for 32bits + ; `------------ 0 if limit is in Bytes, 1 if it's in pages (4ko) + db 0x0 ; Base Address + GDT_LENGTH: + +;; EMPTY PAGE TABLES (identity of the first 1GiB) +ALIGN 4 +PML4_table: + resb 4096 +PDP_table: + resb 4096 +PD_table: + resb 4096 diff --git a/boot/loader/multiboot/check.inc b/boot/loader/multiboot/check.inc new file mode 100644 index 0000000..e1b42e3 --- /dev/null +++ b/boot/loader/multiboot/check.inc @@ -0,0 +1,35 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Multiboot useful functions ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K 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. ; +; ; +; OS/K 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 OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +; ---------------------------------------------------------------------------- ; +; Multiboot check function to ensure we are launch by GRUB ; +; ---------------------------------------------------------------------------- ; +MB_check: + cmp ecx, MB_GRUB_MAGIC ; The magic number must be in ecx + jne .no_MB + ret +.no_MB: + mov al, "0" ; ERROR 0 : No multiboot + jmp Error diff --git a/boot/loader/multiboot/header.inc b/boot/loader/multiboot/header.inc new file mode 100644 index 0000000..8688c28 --- /dev/null +++ b/boot/loader/multiboot/header.inc @@ -0,0 +1,34 @@ +;=----------------------------------------------------------------------------=; +; GNU GPL OS/K ; +; ; +; Desc: Multiboot header ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K 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. ; +; ; +; OS/K 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 OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +;; MULTIBOOT HEADER +MB_AOUT_KLUDGE equ 1 << 16 ; We are not an ELF executable +MB_ALIGN equ 1 << 0 ; Ask to align loaded modules on page boundaries +MB_MEMINFO equ 1 << 1 ; Ask to provide memory map +MB_HEADER_MAGIC equ 0x1badb002 +MB_GRUB_MAGIC equ 0x2badb002 +MB_HEADER_FLAGS equ MB_AOUT_KLUDGE|MB_ALIGN|MB_MEMINFO +CHECKSUM equ -(MB_HEADER_MAGIC + MB_HEADER_FLAGS) +KERNEL_STACK equ 0x00200000 ; Stack starts at the 2mb address & grows down