From a8626d6fdd3de57ee71c71e1fceaab9e2181e527 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Mon, 8 Mar 2021 00:50:23 +0100 Subject: [PATCH] misc: Initial Makefile rework --- Makefile | 50 ++++++++++++----------- decompressor/Makefile | 28 ++++++------- stage1/cd/bootsect.asm | 4 +- stage1/hdd/bootsect.asm | 4 +- stage1/pxe/bootsect.asm | 4 +- stage23/Makefile | 88 +++++++++++++++++++++++++---------------- stage23/sys/gdt.h | 6 +++ stage23/sys/gdt.s2.c | 5 ++- stage23/sys/smp.c | 10 ++--- 9 files changed, 115 insertions(+), 84 deletions(-) diff --git a/Makefile b/Makefile index 2a6fa394..d62e114e 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,12 @@ OBJCOPY = objcopy CFLAGS = -O2 -pipe -Wall -Wextra PREFIX = /usr/local DESTDIR = -TARGET = bios PATH := $(shell pwd)/toolchain/bin:$(PATH) -.PHONY: all clean install tinf-clean bootloader bootloader-clean distclean stage23 stage23-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test iso9660-test +.PHONY: all clean install distclean limine-bios limine-uefi limine-bios-clean limine-uefi-clean stage23-bios stage23-bios-clean stage23-uefi stage23-uefi-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test iso9660-test -all: bin/limine-install +all: limine-uefi | limine-bios bin/limine-install bin/limine-install: limine-install.c limine-hdd.o $(CC) $(CFLAGS) -std=c11 limine-hdd.o limine-install.c -o $@ @@ -17,51 +16,56 @@ bin/limine-install: limine-install.c limine-hdd.o limine-hdd.o: bin/limine-hdd.bin $(OBJCOPY) -B i8086 -I binary -O default bin/limine-hdd.bin $@ -clean: +clean: limine-bios-clean limine-uefi-clean rm -f limine-hdd.o install: all install -d $(DESTDIR)$(PREFIX)/bin - install -s limine-install $(DESTDIR)$(PREFIX)/bin/ + install -s bin/limine-install $(DESTDIR)$(PREFIX)/bin/ install -d $(DESTDIR)$(PREFIX)/share install -m 644 bin/limine.sys $(DESTDIR)$(PREFIX)/share/ install -m 644 bin/limine-cd.bin $(DESTDIR)$(PREFIX)/share/ install -m 644 bin/limine-pxe.bin $(DESTDIR)$(PREFIX)/share/ + install -m 644 bin/BOOTX64.EFI $(DESTDIR)$(PREFIX)/share/ -ifeq ($(TARGET), bios) -bootloader: | decompressor stage23 +limine-bios: stage23-bios decompressor mkdir -p bin cd stage1/hdd && nasm bootsect.asm -fbin -o ../../bin/limine-hdd.bin cd stage1/cd && nasm bootsect.asm -fbin -o ../../bin/limine-cd.bin cd stage1/pxe && nasm bootsect.asm -fbin -o ../../bin/limine-pxe.bin - cp stage23/limine.sys ./bin/ -else ifeq ($(TARGET), uefi) -bootloader: | gnu-efi stage23 + cp build/stage23-bios/limine.sys ./bin/ + +limine-uefi: | gnu-efi stage23-uefi mkdir -p bin - cp stage23/BOOTX64.EFI ./bin/ -endif + cp build/stage23-uefi/BOOTX64.EFI ./bin/ -bootloader-clean: stage23-clean decompressor-clean +limine-bios-clean: stage23-bios-clean decompressor-clean -distclean: clean bootloader-clean test-clean - rm -rf bin stivale toolchain ovmf gnu-efi +limine-uefi-clean: stage23-uefi-clean + +distclean: clean test-clean + rm -rf bin build stivale toolchain ovmf gnu-efi stivale: git clone https://github.com/stivale/stivale.git -stage23: stivale - cd tinf && rm -rf *.o *.d - $(MAKE) -C stage23 all TARGET=$(TARGET) +stage23-uefi: stivale + $(MAKE) -C stage23 all TARGET=uefi BUILDDIR="`pwd`/build/stage23-uefi" -stage23-clean: - $(MAKE) -C stage23 clean +stage23-uefi-clean: + $(MAKE) -C stage23 clean TARGET=uefi BUILDDIR="`pwd`/build/stage23-uefi" + +stage23-bios: stivale + $(MAKE) -C stage23 all TARGET=bios BUILDDIR="`pwd`/build/stage23-bios" + +stage23-bios-clean: + $(MAKE) -C stage23 clean TARGET=bios BUILDDIR="`pwd`/build/stage23-bios" decompressor: - cd tinf && rm -rf *.o *.d - $(MAKE) -C decompressor all + $(MAKE) -C decompressor all BUILDDIR="`pwd`/build/decompressor" decompressor-clean: - $(MAKE) -C decompressor clean + $(MAKE) -C decompressor clean BUILDDIR="`pwd`/build/decompressor" test-clean: $(MAKE) -C test clean diff --git a/decompressor/Makefile b/decompressor/Makefile index a2f86164..aa9b0e80 100644 --- a/decompressor/Makefile +++ b/decompressor/Makefile @@ -1,6 +1,7 @@ CC = i386-elf-gcc LD = i386-elf-gcc OBJCOPY = i386-elf-objcopy +BUILDDIR = . CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror @@ -12,11 +13,7 @@ INTERNAL_CFLAGS = \ -fomit-frame-pointer \ -Wno-address-of-packed-member \ -masm=intel \ - -mno-80387 \ - -mno-mmx \ - -mno-3dnow \ - -mno-sse \ - -mno-sse2 \ + -mgeneral-regs-only \ -MMD \ -I. @@ -35,22 +32,25 @@ INTERNAL_LDFLAGS = \ C_FILES := $(shell find -L ./ -type f -name '*.c' | sort) ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort) -OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o) -HEADER_DEPS := $(C_FILES:.c=.d) +OBJ := $(addprefix $(BUILDDIR)/, $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)) +HEADER_DEPS := $(addprefix $(BUILDDIR)/, $(C_FILES:.c=.d)) -all: decompressor.bin +all: | builddir $(BUILDDIR)/decompressor.bin -decompressor.bin: $(OBJ) - $(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o decompressor.elf - $(OBJCOPY) -O binary decompressor.elf decompressor.bin +builddir: + for i in $(OBJ); do mkdir -p `dirname $$i`; done + +$(BUILDDIR)/decompressor.bin: $(OBJ) + $(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf + $(OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@ -include $(HEADER_DEPS) -%.o: %.c +$(addprefix $(BUILDDIR)/, %.o): %.c $(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ -%.o: %.asm +$(addprefix $(BUILDDIR)/, %.o): %.asm nasm $< -f elf32 -o $@ clean: - rm -f decompressor.bin decompressor.elf $(OBJ) $(HEADER_DEPS) + rm -rf $(BUILDDIR) diff --git a/stage1/cd/bootsect.asm b/stage1/cd/bootsect.asm index 2965adb5..083d2e35 100644 --- a/stage1/cd/bootsect.asm +++ b/stage1/cd/bootsect.asm @@ -103,12 +103,12 @@ pmode: ; Align stage2 to 2K ON DISK ALIGN2K DEC_LBA_OFFSET equ ($-$$)/2048 -incbin '../../decompressor/decompressor.bin' +incbin '../../build/decompressor/decompressor.bin' ALIGN2K STAGE2_START equ $-$$ STAGE2_LBA_OFFSET equ STAGE2_START/2048 DEC_LBA_COUNT equ STAGE2_LBA_OFFSET - DEC_LBA_OFFSET -incbin '../../stage23/stage2.bin.gz' +incbin '../../build/stage23-bios/stage2.bin.gz' STAGE2_SIZE equ ($-$$) - STAGE2_START STAGE2_LBA_COUNT equ (2047 + $-$$)/2048 diff --git a/stage1/hdd/bootsect.asm b/stage1/hdd/bootsect.asm index 6277de4f..8332b180 100644 --- a/stage1/hdd/bootsect.asm +++ b/stage1/hdd/bootsect.asm @@ -126,9 +126,9 @@ dw 0xaa55 ; ********************* Stage 2 ********************* decompressor: -incbin '../../decompressor/decompressor.bin' +incbin '../../build/decompressor/decompressor.bin' align 16 stage2: -incbin '../../stage23/stage2.bin.gz' +incbin '../../build/stage23-bios/stage2.bin.gz' .size: equ $ - stage2 diff --git a/stage1/pxe/bootsect.asm b/stage1/pxe/bootsect.asm index e67aa637..a2f8e638 100644 --- a/stage1/pxe/bootsect.asm +++ b/stage1/pxe/bootsect.asm @@ -55,10 +55,10 @@ err: ; ********************* Stage 2 ********************* decompressor: -incbin '../../decompressor/decompressor.bin' +incbin '../../build/decompressor/decompressor.bin' align 16 stage2: -incbin '../../stage23/stage2.bin.gz' +incbin '../../build/stage23-bios/stage2.bin.gz' .size: equ $ - stage2 .fullsize: equ $ - decompressor diff --git a/stage23/Makefile b/stage23/Makefile index 3584646f..a9a84b46 100644 --- a/stage23/Makefile +++ b/stage23/Makefile @@ -1,4 +1,5 @@ TARGET = bios +BUILDDIR = . ifeq ($(TARGET), bios) TOOLCHAIN=i386-elf @@ -72,7 +73,7 @@ ifeq ($(TARGET), bios) -static-libgcc endif -.PHONY: all clean +.PHONY: all clean builddir C_FILES := $(shell find -L ./ -type f -name '*.c' | sort) ifeq ($(TARGET), bios) @@ -81,84 +82,101 @@ endif ifeq ($(TARGET), uefi) ASM_FILES := lib/do_32.asm endif -OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o) -HEADER_DEPS := $(C_FILES:.c=.d) +OBJ := $(addprefix $(BUILDDIR)/, $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)) +HEADER_DEPS := $(addprefix $(BUILDDIR)/, $(C_FILES:.c=.d)) ifeq ($(TARGET), bios) -all: limine_dbg.elf limine.sys stage2.bin stage2.bin.gz +all: | builddir everything +.PHONY: everything +everything: $(BUILDDIR)/limine_dbg.elf $(BUILDDIR)/limine.sys $(BUILDDIR)/stage2.bin $(BUILDDIR)/stage2.bin.gz else ifeq ($(TARGET), uefi) -all: BOOTX64.EFI +all: | builddir $(BUILDDIR)/BOOTX64.EFI endif -BOOTX64.EFI: limine_efi.elf - $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 limine_efi.elf $@ +builddir: + for i in $(OBJ); do mkdir -p `dirname $$i`; done -stage2.bin.gz: stage2.bin - gzip -n -9 < stage2.bin > stage2.bin.gz +$(BUILDDIR)/sys/smp_trampoline.bin: sys/smp_trampoline.real + nasm $< -f bin -o $@ -stage2.bin: limine.sys - dd if=limine.sys bs=$$(( 0x$$($(READELF) -S limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@ +$(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin + cd "`dirname $<`" && \ + $(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@ -limine.map.o: limine_nomap.elf - ./gensyms.sh $(OBJDUMP) limine_nomap.elf limine +$(BUILDDIR)/font.o: font.bin + cd "`dirname $<`" && \ + $(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@ -limine.sys: limine.elf +ifeq ($(TARGET), bios) + +$(BUILDDIR)/stage2.bin.gz: $(BUILDDIR)/stage2.bin + gzip -n -9 < $< > $@ + +$(BUILDDIR)/stage2.bin: $(BUILDDIR)/limine.sys + dd if=$< bs=$$(( 0x$$($(READELF) -S $(BUILDDIR)/limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@ + +$(BUILDDIR)/limine.map.o: $(BUILDDIR)/limine_nomap.elf + GENSYMS="`pwd`/gensyms.sh" && \ + cd "`dirname $<`" && \ + "$$GENSYMS" $(OBJDUMP) $< limine + +$(BUILDDIR)/limine.sys: $(BUILDDIR)/limine.elf $(OBJCOPY) -O binary $< $@ -limine_nomap.elf: $(OBJ) font.o sys/smp_trampoline.o - $(CC) $(OBJ) font.o sys/smp_trampoline.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@ - $(CC) $(OBJ) font.o sys/smp_trampoline.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Wl,--gc-sections -Tlinker_stage2only.ld -o limine_stage2only.elf || \ +$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o + $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@ + $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Wl,--gc-sections -Tlinker_stage2only.ld -o limine_stage2only.elf || \ ( echo "This error means that stage 2 was trying to use stage 3 symbols before loading stage 3" && \ false ) -limine_dbg.elf: $(OBJ) font.o sys/smp_trampoline.o - $(CC) $(OBJ) font.o sys/smp_trampoline.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@ +$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/limine.map.o + $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@ -sys/smp_trampoline.o: sys/smp_trampoline.bin - $(OBJCOPY) -B i8086 -I binary -O default sys/smp_trampoline.bin $@ +$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o + $(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@ -sys/smp_trampoline.bin: sys/smp_trampoline.real - nasm $< -f bin -o $@ +endif -font.o: font.bin - $(OBJCOPY) -B i8086 -I binary -O default font.bin $@ +ifeq ($(TARGET), uefi) -limine.elf: $(OBJ) font.o sys/smp_trampoline.o limine.map.o - $(CC) $(OBJ) font.o sys/smp_trampoline.o limine.map.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@ +$(BUILDDIR)/BOOTX64.EFI: $(BUILDDIR)/limine_efi.elf + $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $< $@ -limine_efi.elf: $(OBJ) font.o sys/smp_trampoline.o +$(BUILDDIR)/limine_efi.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(LD) -shared -Bsymbolic \ -T../gnu-efi/gnuefi/elf_x86_64_efi.lds \ ../gnu-efi/gnuefi/crt0-efi-x86_64.o \ ../gnu-efi/gnuefi/libgnuefi.a \ ../gnu-efi/lib/x86_64/efi_stub.o \ - $(OBJ) font.o sys/smp_trampoline.o -o $@ + $^ -o $@ + +endif -include $(HEADER_DEPS) -%.o: %.c +$(addprefix $(BUILDDIR)/, %.o): %.c $(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ ifeq ($(TARGET), bios) -%.s2.o: %.s2.c +$(addprefix $(BUILDDIR)/, %.s2.o): %.s2.c $(CC) $(S2CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ endif ifeq ($(TARGET), uefi) -%.32.o: %.32.c +$(addprefix $(BUILDDIR)/, %.32.o): %.32.c $(CC32) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@.32 $(OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@ rm $@.32 endif ifeq ($(TARGET), bios) -%.o: %.asm +$(addprefix $(BUILDDIR)/, %.o): %.asm nasm $< -F dwarf -g -f elf32 -o $@ endif ifeq ($(TARGET), uefi) -%.o: %.asm +$(addprefix $(BUILDDIR)/, %.o): %.asm nasm $< -F dwarf -g -f elf64 -o $@ endif clean: - rm -f limine.elf limine_efi.elf limine_dbg.elf limine_nomap.elf limine_stage2only.elf font.o limine.map.o limine.sys stage2.bin stage2.bin.gz BOOTX64.EFI sys/smp_trampoline.bin sys/smp_trampoline.o $(OBJ) $(HEADER_DEPS) + rm -rf $(BUILDDIR) diff --git a/stage23/sys/gdt.h b/stage23/sys/gdt.h index 35f36a04..2132160e 100644 --- a/stage23/sys/gdt.h +++ b/stage23/sys/gdt.h @@ -5,7 +5,13 @@ struct gdtr { uint16_t limit; +#if defined (uefi) uint64_t ptr; +#endif +#if defined (bios) + uint32_t ptr; + uint32_t pad; +#endif } __attribute__((packed)); struct gdt_desc { diff --git a/stage23/sys/gdt.s2.c b/stage23/sys/gdt.s2.c index 0db99914..f918573f 100644 --- a/stage23/sys/gdt.s2.c +++ b/stage23/sys/gdt.s2.c @@ -61,5 +61,8 @@ static struct gdt_desc gdt_descs[] = { struct gdtr gdt = { sizeof(gdt_descs) - 1, - (uintptr_t)gdt_descs + (uintptr_t)gdt_descs, +#if defined (bios) + 0 +#endif }; diff --git a/stage23/sys/smp.c b/stage23/sys/smp.c index da56568d..90f9f930 100644 --- a/stage23/sys/smp.c +++ b/stage23/sys/smp.c @@ -45,8 +45,8 @@ static void delay(uint32_t cycles) { inb(0x80); } -extern symbol _binary_sys_smp_trampoline_bin_start; -extern symbol _binary_sys_smp_trampoline_bin_end; +extern symbol _binary_smp_trampoline_bin_start; +extern symbol _binary_smp_trampoline_bin_end; struct trampoline_passed_info { uint8_t smp_tpl_booted_flag; @@ -62,15 +62,15 @@ static bool smp_start_ap(uint32_t lapic_id, struct gdtr *gdtr, struct smp_information *info_struct, bool longmode, bool lv5, uint32_t pagemap, bool x2apic) { - size_t trampoline_size = (size_t)_binary_sys_smp_trampoline_bin_end - - (size_t)_binary_sys_smp_trampoline_bin_start; + size_t trampoline_size = (size_t)_binary_smp_trampoline_bin_end + - (size_t)_binary_smp_trampoline_bin_start; // Prepare the trampoline static void *trampoline = NULL; if (trampoline == NULL) { trampoline = conv_mem_alloc_aligned(trampoline_size, 4096); - memcpy(trampoline, _binary_sys_smp_trampoline_bin_start, trampoline_size); + memcpy(trampoline, _binary_smp_trampoline_bin_start, trampoline_size); } static struct trampoline_passed_info *passed_info = NULL;