From 7a0c9a710c66954c63a118bafbcb61bce33ff5a7 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Sun, 28 Aug 2022 09:30:17 +0200 Subject: [PATCH] Fix KStart32-related issues and improvements to build script --- build/build-iso.sh | 72 +++++++++++++++++------------------------ src/Kernel/KStart32.ZC | 20 +++++++----- zealbooter/trampoline.S | 20 ++++++++++-- zealbooter/zealbooter.c | 12 +++---- 4 files changed, 64 insertions(+), 60 deletions(-) diff --git a/build/build-iso.sh b/build/build-iso.sh index 31021aa4..a7f9d433 100755 --- a/build/build-iso.sh +++ b/build/build-iso.sh @@ -36,11 +36,10 @@ umount_tempdisk() { [ ! -d $TMPMOUNT ] && mkdir -p $TMPMOUNT [ ! -d $TMPISODIR ] && mkdir -p $TMPISODIR -set -e +set -e echo "Building ZealBooter..." ( cd ../zealbooter && make clean all ) - set +e echo "Making temp vdisk, running auto-install ..." @@ -74,18 +73,42 @@ rm ../src/Home/MakeHome.ZC 2> /dev/null rm ../src/Boot/Kernel.ZXE 2> /dev/null mount_tempdisk sudo cp -r ../src/* $TMPMOUNT +umount_tempdisk + +echo "Building Distro ISO ..." +qemu-system-x86_64 -machine q35,accel=kvm -drive format=raw,file=$TMPDISK -m 1G -rtc base=localtime -smp 4 -device isa-debug-exit if [ ! -d "limine" ]; then git clone https://github.com/limine-bootloader/limine.git --branch=v3.0-branch-binary --depth=1 make -C limine fi -sudo mkdir -p $TMPMOUNT/EFI/BOOT -sudo cp limine/BOOTX64.EFI $TMPMOUNT/EFI/BOOT/BOOTX64.EFI -sudo cp limine/limine.sys $TMPMOUNT/ -sudo cp ../zealbooter/zealbooter.elf $TMPMOUNT/Boot/ZealBooter.ELF +mount_tempdisk +echo "Extracting MyDistro ISO from vdisk ..." +cp $TMPMOUNT/Tmp/MyDistro.ISO.C ./ZealOS-MyDistro.iso +sudo rm $TMPMOUNT/Tmp/MyDistro.ISO.C 2> /dev/null +echo "Setting up temp ISO directory contents for use with limine xorriso command ..." +sudo cp -rf $TMPMOUNT/* $TMPISODIR +sudo mkdir -p $TMPISODIR/EFI/BOOT +sudo cp limine/BOOTX64.EFI $TMPISODIR/EFI/BOOT/BOOTX64.EFI +sudo cp limine/limine-cd-efi.bin $TMPISODIR/Boot/ +sudo cp limine/limine-cd.bin $TMPISODIR/Boot/ +sudo cp limine/limine.sys $TMPISODIR/ +sudo cp ../zealbooter/zealbooter.elf $TMPISODIR/Boot/ZealBooter.ELF +echo "Copying DVDKernel.ZXE over ISO Boot/Kernel.ZXE ..." +sudo mv $TMPMOUNT/Tmp/DVDKernel.ZXE $TMPISODIR/Boot/Kernel.ZXE umount_tempdisk +sudo ls $TMPISODIR -al + +xorriso -joliet "on" -rockridge "on" -as mkisofs -b Boot/limine-cd.bin \ + -no-emul-boot -boot-load-size 4 -boot-info-table \ + --efi-boot Boot/limine-cd-efi.bin \ + -efi-boot-part --efi-boot-image --protective-msdos-label \ + $TMPISODIR -o ZealOS-UEFI-limine-dev.iso + +./limine/limine-deploy ZealOS-UEFI-limine-dev.iso + if [ ! -d "ovmf" ]; then echo "Downloading OVMF..." mkdir ovmf @@ -95,38 +118,6 @@ if [ ! -d "ovmf" ]; then cd .. fi -./limine/limine-deploy $TMPDISK - -echo "Building Distro ISO ..." -qemu-system-x86_64 -machine q35,accel=kvm -drive format=raw,file=$TMPDISK -m 1G -rtc base=localtime -bios ovmf/OVMF.fd -smp 4 -device isa-debug-exit - -mount_tempdisk -echo "Extracting MyDistro ISO from vdisk ..." -cp $TMPMOUNT/Tmp/MyDistro.ISO.C ./ZealOS-MyDistro.iso -sudo rm $TMPMOUNT/Tmp/MyDistro.ISO.C 2> /dev/null -echo "Setting up temp ISO directory contents for use with limine xorriso command ..." -sudo cp -rf $TMPMOUNT/* $TMPISODIR -sudo cp limine/limine-cd-efi.bin $TMPISODIR/Boot/ -sudo cp limine/limine-cd.bin $TMPISODIR/Boot/ -sudo cp limine/limine.sys $TMPISODIR/ -sudo cp $TMPMOUNT/limine.cfg $TMPISODIR/limine.cfg -sudo rm -rf $TMPISODIR/EFI -sudo cp -rf ../zealbooter/zealbooter.elf $TMPISODIR/Boot/ZealBooter.ELF -echo "Copying DVDKernel.ZXE over ISO Boot/Kernel.ZXE ..." -sudo mv $TMPMOUNT/Tmp/DVDKernel.ZXE $TMPISODIR/Boot/Kernel.ZXE -umount_tempdisk - -sudo ls $TMPISODIR -al -mv $TMPDISK ./ZealOS-UEFI-limine-dev.raw - -xorriso -joliet "on" -rockridge "on" -as mkisofs -b Boot/limine-cd.bin \ - -no-emul-boot -boot-load-size 4 -boot-info-table \ - --efi-boot Boot/limine-cd-efi.bin \ - -efi-boot-part --efi-boot-image --protective-msdos-label \ - $TMPISODIR -o ZealOS-UEFI-limine-dev.iso - -./limine/limine-deploy ZealOS-UEFI-limine-dev.iso - echo "Testing limine-zealbooter-xorriso isohybrid boot in UEFI mode ..." qemu-system-x86_64 -machine q35,accel=kvm -m 1G -rtc base=localtime -bios ovmf/OVMF.fd -smp 4 -cdrom ZealOS-UEFI-limine-dev.iso echo "Testing limine-zealbooter-xorriso isohybrid boot in BIOS mode ..." @@ -138,11 +129,6 @@ qemu-system-x86_64 -machine q35,accel=kvm -m 1G -rtc base=localtime -smp 4 -cdro rm ./ZealOS-2*.iso 2> /dev/null # comment this line if you want lingering old Distro ISOs mv ./ZealOS-MyDistro.iso ./ZealOS-$(date +%Y-%m-%d-%H_%M_%S).iso -echo "Testing temp vdisk in UEFI mode ..." -qemu-system-x86_64 -machine q35,accel=kvm -drive format=raw,file=ZealOS-UEFI-limine-dev.raw -m 1G -rtc base=localtime -bios ovmf/OVMF.fd -smp 4 -echo "Testing temp vdisk in BIOS mode ..." -qemu-system-x86_64 -machine q35,accel=kvm -drive format=raw,file=ZealOS-UEFI-limine-dev.raw -m 1G -rtc base=localtime -smp 4 - echo "Deleting temp folder ..." sudo rm -rf $TMPDIR sudo rm -rf $TMPISODIR diff --git a/src/Kernel/KStart32.ZC b/src/Kernel/KStart32.ZC index 987cc75c..5893d605 100755 --- a/src/Kernel/KStart32.ZC +++ b/src/Kernel/KStart32.ZC @@ -77,10 +77,6 @@ SYS_SEMAS:: DU8 SEMA_SEMAS_NUM * DEFAULT_CACHE_LINE_WIDTH DUP(0); //************************************ ALIGN 16, OC_NOP - // Signature to find CORE0_32BIT_INIT - DU64 0xaa23c08ed10bd4d7; - DU64 0xf6ceba7d4b74179a; - CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$. PUSH U32 RFLAGG_START POPFD @@ -89,8 +85,9 @@ CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$. MOV AX, CGDT.boot_ds //LOAD DS SELECTOR MOV DS, AX - //MOV ESI, U32 [SYS_BOOT_PATCH_TABLE_BASE] - //MOV EDI, U32 [MEM_BOOT_BASE] + BTS U32 [SYS_RUN_LEVEL], RLf_32BIT + MOV ESI, U32 [SYS_BOOT_PATCH_TABLE_BASE] + MOV EDI, U32 [MEM_BOOT_BASE] MOV AX, CGDT.ds MOV DS, AX @@ -98,8 +95,16 @@ CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$. MOV FS, AX MOV GS, AX MOV SS, AX - //MOV ESP, BOOT_RAM_LIMIT //Tmp Stack + MOV ESP, BOOT_RAM_LIMIT //Tmp Stack + JMP @@02 + + ALIGN 16, OC_NOP + // Signature to find CORE0_32BIT_INIT + DU64 0xaa23c08ed10bd4d7; + DU64 0xf6ceba7d4b74179a; + +@@02: //Patch abs addresses MOV ECX, U32 CPatchTableAbsAddr.abs_address_count[ESI] LEA ESI, U32 CPatchTableAbsAddr.abs_address[ESI] @@ -113,7 +118,6 @@ CORE0_32BIT_INIT:: //Entry point for $LK,"BootRAM",A="MN:BootRAM"$. DU16 CGDT.cs32; @@10: BTS U32 [SYS_RUN_LEVEL], RLf_PATCHED - BTS U32 [SYS_RUN_LEVEL], RLf_32BIT CALL SYS_FIND_PCIBIOS_SERVICE_DIR CALL SYS_FIND_PCI_SERVICES diff --git a/zealbooter/trampoline.S b/zealbooter/trampoline.S index 9e04fbb3..62398b05 100644 --- a/zealbooter/trampoline.S +++ b/zealbooter/trampoline.S @@ -2,8 +2,14 @@ .global trampoline trampoline: + // Set new stack + mov %rdx, %rsp + + // Load GDTR lgdt (%rcx) + // Far jump to 32-bit compatibility mode + // AKA (set CS to CGDT.cs32) pushq $0x30 addq $(1f - trampoline), %rax pushq %rax @@ -12,6 +18,7 @@ trampoline: .code32 1: + // Set all data segments to CGDT.ds mov $0x10, %eax mov %eax, %ds mov %eax, %es @@ -19,19 +26,26 @@ trampoline: mov %eax, %gs mov %eax, %ss + // Disable paging mov %cr0, %eax btr $31, %eax mov %eax, %cr0 + // Disable LME and friends mov $0xc0000080, %ecx xor %eax, %eax xor %edx, %edx wrmsr - mov %ebx, %eax - mov $2, %ebx + // Set CR0 to (PE | NE) AKA SYS_START_CR0 + mov $0x11, %eax + mov %eax, %cr0 - jmp *%eax + // Set flags to 0x02 + pushl $0x02 + popfl + + jmp *%ebx .global trampoline_end trampoline_end: diff --git a/zealbooter/zealbooter.c b/zealbooter/zealbooter.c index 74cc8907..bec0d050 100644 --- a/zealbooter/zealbooter.c +++ b/zealbooter/zealbooter.c @@ -116,8 +116,9 @@ struct CKernel { #define BOOT_SRC_RAM 2 #define BOOT_SRC_HDD 3 #define BOOT_SRC_DVD 4 -#define RLF_16BIT 0b01 -#define RLF_VESA 0b10 +#define RLF_16BIT 0b001 +#define RLF_VESA 0b010 +#define RLF_32BIT 0b100 extern symbol trampoline, trampoline_end; @@ -210,7 +211,7 @@ void _start(void) { CKernel->boot_patch_table_base -= (uintptr_t)kernel->address; CKernel->boot_patch_table_base += final_address; - CKernel->sys_run_level = RLF_VESA | RLF_16BIT; + CKernel->sys_run_level = RLF_VESA | RLF_16BIT | RLF_32BIT; CKernel->boot_base = (uintptr_t)&CKernel->jmp - (uintptr_t)kernel->address; CKernel->boot_base += final_address; @@ -269,12 +270,11 @@ void _start(void) { memcpy((void *)final_address, CKernel, kernel->size); asm volatile ( - "mov %5, %%rsp;" "jmp *%0" : : "a"(trampoline_phys), "b"(CORE0_32BIT_INIT), - "c"(sys_gdt_ptr), "S"(CKernel->boot_patch_table_base), - "D"(CKernel->boot_base), "r"(boot_stack) + "c"(sys_gdt_ptr), "d"(boot_stack), + "S"(CKernel->boot_patch_table_base), "D"(CKernel->boot_base) : "memory"); __builtin_unreachable();