smp/trampoline: Use hand written pos independent code instead of objcopying

This commit is contained in:
mintsuki 2022-07-28 09:35:59 +02:00
parent 9bb7c7e301
commit 6f3f16f702
2 changed files with 36 additions and 41 deletions

View File

@ -175,15 +175,6 @@ else ifeq ($(TARGET), uefi-ia32)
all: $(call MKESCAPE,$(BUILDDIR))/BOOTIA32.EFI all: $(call MKESCAPE,$(BUILDDIR))/BOOTIA32.EFI
endif endif
$(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.bin: sys/smp_trampoline.real
mkdir -p "$$(dirname '$(call SHESCAPE,$@)')"
nasm $< -f bin -o '$(call SHESCAPE,$@)'
$(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o: $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.bin
mkdir -p "$$(dirname '$(call SHESCAPE,$@)')"
cd '$(call SHESCAPE,$(BUILDDIR))/sys' && \
$(CROSS_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) smp_trampoline.bin '$(call SHESCAPE,$@)'
$(call MKESCAPE,$(BUILDDIR))/font.o: font.bin $(call MKESCAPE,$(BUILDDIR))/font.o: font.bin
mkdir -p "$$(dirname '$(call SHESCAPE,$@)')" mkdir -p "$$(dirname '$(call SHESCAPE,$@)')"
$(CROSS_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) font.bin '$(call SHESCAPE,$@)' $(CROSS_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) font.bin '$(call SHESCAPE,$@)'
@ -235,7 +226,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker_nomap.ld: linker_bios.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_bios.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_bios.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/stage2.map.o $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/stage2.map.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(CROSS_LD) '$(call OBJESCAPE,$^)' $(CROSS_LDFLAGS) $(INTERNAL_LDFLAGS) -T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' -o '$(call SHESCAPE,$@)' $(CROSS_LD) '$(call OBJESCAPE,$^)' $(CROSS_LDFLAGS) $(INTERNAL_LDFLAGS) -T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' -o '$(call SHESCAPE,$@)'
$(CROSS_OBJCOPY) -O binary --only-section=.note.gnu.build-id '$(call SHESCAPE,$@)' '$(call SHESCAPE,$(BUILDDIR))/build-id.s2.bin' $(CROSS_OBJCOPY) -O binary --only-section=.note.gnu.build-id '$(call SHESCAPE,$@)' '$(call SHESCAPE,$(BUILDDIR))/build-id.s2.bin'
@ -250,7 +241,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker.ld: linker_bios.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef linker_bios.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(CROSS_CC) -x c -E -P -undef linker_bios.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(call MKESCAPE,$(BUILDDIR))/limine.elf: $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/stage2.map.o $(call MKESCAPE,$(BUILDDIR))/full.map.o $(call MKESCAPE,$(BUILDDIR))/limine.elf: $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/stage2.map.o $(call MKESCAPE,$(BUILDDIR))/full.map.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(CROSS_LD) '$(call OBJESCAPE,$^)' $(CROSS_LDFLAGS) $(INTERNAL_LDFLAGS) -T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' -o '$(call SHESCAPE,$@)' $(CROSS_LD) '$(call OBJESCAPE,$^)' $(CROSS_LDFLAGS) $(INTERNAL_LDFLAGS) -T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' -o '$(call SHESCAPE,$@)'
$(CROSS_OBJCOPY) -O binary --only-section=.note.gnu.build-id '$(call SHESCAPE,$@)' '$(call SHESCAPE,$(BUILDDIR))/build-id.s2.bin' $(CROSS_OBJCOPY) -O binary --only-section=.note.gnu.build-id '$(call SHESCAPE,$@)' '$(call SHESCAPE,$(BUILDDIR))/build-id.s2.bin'
@ -288,7 +279,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker_nomap.ld: linker_uefi_x86_64.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_uefi_x86_64.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_uefi_x86_64.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-x86_64.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_x86_64.o $(OBJ) ../libgcc-binaries/libgcc-x86_64-no-red-zone.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-x86_64.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_x86_64.o $(OBJ) ../libgcc-binaries/libgcc-x86_64-no-red-zone.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(CROSS_LD) \ $(CROSS_LD) \
-T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' \ -T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' \
@ -298,7 +289,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker.ld: linker_uefi_x86_64.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef linker_uefi_x86_64.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(CROSS_CC) -x c -E -P -undef linker_uefi_x86_64.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(call MKESCAPE,$(BUILDDIR))/limine.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-x86_64.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_x86_64.o $(OBJ) ../libgcc-binaries/libgcc-x86_64-no-red-zone.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/full.map.o $(call MKESCAPE,$(BUILDDIR))/limine.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-x86_64.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_x86_64.o $(OBJ) ../libgcc-binaries/libgcc-x86_64-no-red-zone.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/full.map.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(CROSS_LD) \ $(CROSS_LD) \
-T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' \ -T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' \
@ -327,7 +318,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker_nomap.ld: linker_uefi_ia32.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_uefi_ia32.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(CROSS_CC) -x c -E -P -undef -DLINKER_NOMAP linker_uefi_ia32.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-ia32.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_ia32.o $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-ia32.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_ia32.o $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld'
$(CROSS_LD) \ $(CROSS_LD) \
-T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' \ -T'$(call SHESCAPE,$(BUILDDIR))/linker_nomap.ld' \
@ -337,7 +328,7 @@ $(call MKESCAPE,$(BUILDDIR))/linker.ld: linker_uefi_ia32.ld.in
mkdir -p '$(call SHESCAPE,$(BUILDDIR))' mkdir -p '$(call SHESCAPE,$(BUILDDIR))'
$(CROSS_CC) -x c -E -P -undef linker_uefi_ia32.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(CROSS_CC) -x c -E -P -undef linker_uefi_ia32.ld.in -o '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(call MKESCAPE,$(BUILDDIR))/limine.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-ia32.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_ia32.o $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.o $(call MKESCAPE,$(BUILDDIR))/full.map.o $(call MKESCAPE,$(BUILDDIR))/limine.elf: $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/crt0-efi-ia32.o $(call MKESCAPE,$(BUILDDIR))/limine-efi/gnuefi/reloc_ia32.o $(OBJ) ../libgcc-binaries/libgcc-i686.a $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o $(call MKESCAPE,$(BUILDDIR))/font.o $(call MKESCAPE,$(BUILDDIR))/full.map.o
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld' $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/linker.ld'
$(CROSS_LD) \ $(CROSS_LD) \
-T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' \ -T'$(call SHESCAPE,$(BUILDDIR))/linker.ld' \

View File

@ -1,22 +1,24 @@
org 0
bits 16 bits 16
smp_trampoline:
section .rodata
global _binary_smp_trampoline_bin_start
_binary_smp_trampoline_bin_start:
cli cli
cld cld
mov ebx, cs mov ebx, cs
shl ebx, 4 shl ebx, 4
o32 lidt [cs:invalid_idt] o32 lidt [cs:(invalid_idt - _binary_smp_trampoline_bin_start)]
o32 lgdt [cs:passed_info.gdtr] o32 lgdt [cs:(passed_info.gdtr - _binary_smp_trampoline_bin_start)]
lea eax, [ebx + .mode32] lea eax, [ebx + (.mode32 - _binary_smp_trampoline_bin_start)]
mov [cs:.farjmp_off], eax mov [cs:(.farjmp_off - _binary_smp_trampoline_bin_start)], eax
mov eax, 0x00000011 mov eax, 0x00000011
mov cr0, eax mov cr0, eax
o32 jmp far [cs:.farjmp] o32 jmp far [cs:(.farjmp - _binary_smp_trampoline_bin_start)]
.farjmp: .farjmp:
.farjmp_off: dd 0 .farjmp_off: dd 0
@ -37,7 +39,7 @@ smp_trampoline:
xor eax, eax xor eax, eax
mov cr4, eax mov cr4, eax
test dword [ebx + passed_info.target_mode], (1 << 2) test dword [ebx + (passed_info.target_mode - _binary_smp_trampoline_bin_start)], (1 << 2)
jz .nox2apic jz .nox2apic
mov ecx, 0x1b mov ecx, 0x1b
@ -47,9 +49,9 @@ smp_trampoline:
wrmsr wrmsr
.nox2apic: .nox2apic:
lea esp, [ebx + temp_stack.top] lea esp, [ebx + (temp_stack.top - _binary_smp_trampoline_bin_start)]
test dword [ebx + passed_info.target_mode], (1 << 0) test dword [ebx + (passed_info.target_mode - _binary_smp_trampoline_bin_start)], (1 << 0)
jz parking32 jz parking32
mov eax, cr4 mov eax, cr4
@ -61,7 +63,7 @@ smp_trampoline:
xor edx, edx xor edx, edx
wrmsr wrmsr
test dword [ebx + passed_info.target_mode], (1 << 1) test dword [ebx + (passed_info.target_mode - _binary_smp_trampoline_bin_start)], (1 << 1)
jz .no5lv jz .no5lv
mov eax, cr4 mov eax, cr4
@ -69,15 +71,14 @@ smp_trampoline:
mov cr4, eax mov cr4, eax
.no5lv: .no5lv:
mov eax, dword [ebx + passed_info.pagemap] mov eax, dword [ebx + (passed_info.pagemap - _binary_smp_trampoline_bin_start)]
mov cr3, eax mov cr3, eax
mov eax, cr0 mov eax, cr0
bts eax, 31 bts eax, 31
mov cr0, eax mov cr0, eax
mov eax, .mode64 lea eax, [ebx + (.mode64 - _binary_smp_trampoline_bin_start)]
add eax, ebx
push 0x28 push 0x28
push eax push eax
retf retf
@ -92,7 +93,7 @@ smp_trampoline:
mov ss, ax mov ss, ax
mov ebx, ebx mov ebx, ebx
test dword [rbx + passed_info.target_mode], (1 << 3) test dword [rbx + (passed_info.target_mode - _binary_smp_trampoline_bin_start)], (1 << 3)
jz .nonx jz .nonx
mov ecx, 0xc0000080 mov ecx, 0xc0000080
@ -101,7 +102,7 @@ smp_trampoline:
wrmsr wrmsr
.nonx: .nonx:
test dword [rbx + passed_info.target_mode], (1 << 4) test dword [rbx + (passed_info.target_mode - _binary_smp_trampoline_bin_start)], (1 << 4)
jz .nowp jz .nowp
mov rax, cr0 mov rax, cr0
@ -109,19 +110,19 @@ smp_trampoline:
mov cr0, rax mov cr0, rax
.nowp: .nowp:
mov rax, qword [rbx + passed_info.hhdm] mov rax, qword [rbx + (passed_info.hhdm - _binary_smp_trampoline_bin_start)]
add qword [rbx + passed_info.gdtr + 2], rax add qword [rbx + (passed_info.gdtr - _binary_smp_trampoline_bin_start) + 2], rax
lgdt [rbx + passed_info.gdtr] lgdt [rbx + (passed_info.gdtr - _binary_smp_trampoline_bin_start)]
lea rax, [rax + rbx + parking64] lea rax, [rax + rbx + (parking64 - _binary_smp_trampoline_bin_start)]
jmp rax jmp rax
bits 32 bits 32
parking32: parking32:
mov edi, dword [ebx + passed_info.smp_info_struct] mov edi, dword [ebx + (passed_info.smp_info_struct - _binary_smp_trampoline_bin_start)]
mov eax, 1 mov eax, 1
lock xchg dword [ebx + passed_info.booted_flag], eax lock xchg dword [ebx + (passed_info.booted_flag - _binary_smp_trampoline_bin_start)], eax
xor eax, eax xor eax, eax
.loop: .loop:
@ -148,10 +149,10 @@ parking32:
bits 64 bits 64
parking64: parking64:
mov ebx, ebx mov ebx, ebx
mov edi, dword [rbx + passed_info.smp_info_struct] mov edi, dword [rbx + (passed_info.smp_info_struct - _binary_smp_trampoline_bin_start)]
add rdi, qword [rbx + passed_info.hhdm] add rdi, qword [rbx + (passed_info.hhdm - _binary_smp_trampoline_bin_start)]
mov eax, 1 mov eax, 1
lock xchg dword [rbx + passed_info.booted_flag], eax lock xchg dword [rbx + (passed_info.booted_flag - _binary_smp_trampoline_bin_start)], eax
xor eax, eax xor eax, eax
.loop: .loop:
@ -200,3 +201,6 @@ passed_info:
dq 0 dq 0
.hhdm: .hhdm:
dq 0 dq 0
global _binary_smp_trampoline_bin_end
_binary_smp_trampoline_bin_end: