Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:10

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2024 Matheus Pecoraro
0005  * Copyright (C) 2023 Karel Gardas
0006  * Copyright (C) 2018 embedded brains GmbH & Co. KG
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0027  * POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 
0030 #include <bspopts.h>
0031 #include <gdt.h>
0032 
0033 #ifdef BSP_MULTIBOOT_SUPPORT
0034 #include <multiboot2.h>
0035 #endif
0036 
0037     .text
0038     .section    .text._start,"ax",@progbits
0039     .p2align 4,,15
0040     .globl  _start
0041     .type   _start, @function
0042 _start:
0043     .cfi_startproc
0044         movq    %rsp, %rbp
0045     /*
0046      * _ISR_Stack_size is aligned to CPU_INTERRUPT_STACK_ALIGNMENT (64 bits)
0047      * by compiler directive at /cpukit/include/rtems/confdefs/percpu.h.
0048      * No reference will occur beyond the stack region since the call
0049      * instruction will decrement the %rsp and then save the value of %rip.
0050      */
0051     movabsq $_ISR_Stack_area_begin, %rsp
0052         addq $_ISR_Stack_size, %rsp
0053 
0054 #ifndef BSP_USE_EFI_BOOT_SERVICES
0055         /* Use our own GDT instead of the one set up by UEFI */
0056         lgdt amd64_gdt_descriptor
0057 
0058         /* Load data segment registers */
0059         movw $GDT_DATA_SEG_OFFSET, %ax
0060         movw %ax, %ds
0061         movw %ax, %es
0062         movw %ax, %ss
0063         movw %ax, %fs
0064 
0065         /* Load code segment register */
0066         pushq $GDT_CODE_SEG_OFFSET
0067         pushq $after_load_gdt
0068         retfq
0069 
0070 after_load_gdt:
0071 #endif
0072 
0073 #ifndef BSP_MULTIBOOT_SUPPORT
0074         /**
0075          * The FreeBSD bootloader gives us control with the following stack:
0076          * 0(%rsp) = 32 bit return address (cannot be used)
0077          * 4(%rsp) = 32 bit modulep
0078          * 8(%rsp) = 32 bit kernend
0079          *
0080          * We will extract the necessary info set up by the FreeBSD bootloader
0081          * before dynamic memory is set up since it is stored in memory after
0082          * the kernel
0083          */
0084         movl    4(%rbp), %edi
0085         call    retrieve_info_from_freebsd_loader
0086 #endif
0087 
0088     .cfi_def_cfa_offset 16
0089     xorl    %edi, %edi
0090 #ifdef BSP_MULTIBOOT_SUPPORT
0091         mov    %eax, _multiboot2_magic
0092         mov    %rbx, _multiboot2_info_ptr
0093 #endif
0094     movabsq $boot_card, %rax
0095     call    *%rax
0096     .cfi_endproc
0097 
0098 #ifdef BSP_MULTIBOOT_SUPPORT
0099 
0100 multiboot2:
0101         ret
0102 
0103 _multiboot2_start:
0104     jmp _start
0105 
0106     .text
0107     .section    .multiboot2_header
0108     .p2align    4,,15
0109 multiboot2_header_start:
0110         .long   MULTIBOOT2_HEADER_MAGIC
0111         .long   MULTIBOOT_ARCHITECTURE_I386
0112     .long   multiboot2_header_end - multiboot2_header_start
0113         .long   -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + (multiboot2_header_end - multiboot2_header_start))
0114 efi_bootservices_start:
0115         .short  MULTIBOOT_HEADER_TAG_EFI_BS
0116         .short  0
0117         .long   efi_bootservices_end - efi_bootservices_start
0118 efi_bootservices_end:
0119 efi64_entry_start:
0120         .short  MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64
0121         .short  0
0122         .long   efi64_entry_end - efi64_entry_start
0123         .long   _start /* directly copied from resulting ELF */
0124         /* padding to 8 byte tags allignment */
0125         .long   0
0126 efi64_entry_end:
0127 info_requests_start:
0128         .short  MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST
0129         .short  0
0130         .long   info_requests_end - info_requests_start
0131         .long   MULTIBOOT_TAG_TYPE_EFI64
0132         .long   MULTIBOOT_TAG_TYPE_CMDLINE
0133 #ifdef BSP_USE_EFI_BOOT_SERVICES
0134         .long   MULTIBOOT_TAG_TYPE_EFI_BS
0135 #else
0136     .long   0
0137 #endif
0138         .long   MULTIBOOT_TAG_TYPE_ACPI_OLD
0139         .long   MULTIBOOT_TAG_TYPE_ACPI_NEW
0140         .long   0
0141 info_requests_end:
0142         /* header end*/
0143         .short  MULTIBOOT_HEADER_TAG_END
0144         .short  0
0145         .long   8
0146 multiboot2_header_end:
0147 
0148         .data
0149         .global  _multiboot2_magic
0150 _multiboot2_magic:
0151         .long   0
0152 
0153         .data
0154         .global  _multiboot2_info_ptr
0155 _multiboot2_info_ptr:
0156         .quad   0
0157 
0158 #endif