[kernel]: start 实现进行架构区分

startup.S 完全实现 除 el1_up 外
           irq 暂未实现完全
This commit is contained in:
2025-08-30 13:34:57 +08:00
parent da76f666ce
commit 3518d3ea3c
12 changed files with 202 additions and 34 deletions

View File

@@ -7,4 +7,6 @@
#define MAGNITUDE_PRINT_BUFFER_SIZE 512
#define MAGNITUDE_STACK_BOOT_SIZE 4096
#endif //MAGNITUDE_H

View File

@@ -0,0 +1,14 @@
//
// Created by dongl on 25-8-28.
//
#ifndef STACK_H
#define STACK_H
#include <magnitude_types.h>
extern uint64_t mag_stack_top;
extern uint64_t mag_stack_bottom;
extern uint64_t mag_stack_count;
#endif //STACK_H

View File

@@ -8,8 +8,8 @@
#include <magnitude/io/ringbuf.h>
#include <magnitude/irq/irq.h>
#include <console/bsp-uart.h>
#include <irq/bsp-gic.h>
#include <serial/bspuart.h>
#include <irq/bspgic.h>
#include <bsp.h>
@@ -44,9 +44,9 @@ void magnitude_serial_up ( void ) {
// 使能串口
bsp_uart_up();
// 安装 UART IRQ handler
magnitude_irq_install_handler(UART_IRQ_NUM, uart_isr_handler, &mag_print_tx);
magnitude_irq_install_handler(IRQ_UART, uart_isr_handler, &mag_print_tx);
// 启用中断号
bsp_gic_enable_irq(UART_IRQ_NUM);
bsp_gic_enable_irq(IRQ_UART);
}
ssize_t magnitude_putc ( char c ) {

View File

@@ -5,9 +5,9 @@
#include <magnitude/irq/irq.h>
#include <magnitude/irq/isr.h>
#include <irq/bsp-gic.h>
#include <irq/bspgic.h>
#include <arch-register.h>
#include <archregister.h>
#include <bsp.h>

View File

@@ -3,7 +3,7 @@
//
#include <magnitude/irq/isr.h>
#include <arch-register.h>
#include <archregister.h>
void magnitude_isr_enable ( void ) {
arch_irq_enable();

View File

@@ -7,7 +7,7 @@
#ifndef INCLUDED_tlsf
#define INCLUDED_tlsf
#include <../../../include/magnitude/magnitude_assert.h>
#include <magnitude_assert.h>
#include <magnitude_types.h>
#if defined(__cplusplus)

View File

@@ -0,0 +1,19 @@
//
// Created by dongl on 25-8-28.
//
#include <magnitude/mm/stack.h>
extern uint64_t __stack_top;
extern uint64_t __stack_bottom;
extern uint64_t __stack_count;
uint64_t mag_stack_top = (uint64_t) &__stack_top;
uint64_t mag_stack_bottom = (uint64_t) &__stack_bottom;
uint64_t mag_stack_count = (uint64_t) &__stack_count;
void t() {
(void)mag_stack_top;
(void)mag_stack_bottom;
(void)mag_stack_count;
}

View File

@@ -3,20 +3,29 @@
//
#include <start/arch-start.h>
#include <start/bsp-startup.h>
#include <start/bspstart.h>
#include <mmu/arch-mmu.h>
#include <magnitude/kernel.h>
#include <magnitude/mm/pmm.h>
#include <magnitude/io/io.h>
__attribute__((section(".text.boot")))
void magnitude_start(void) {
// 注册页表分配基本算法实现
// 以供接下来MMU的物理内存申请
// arch_mmu_physics_callback.physics_alloc = magnitude_pmm_alloc;
// arch_mmu_physics_callback.physics_free = magnitude_pmm_free;
_arch_start();
_bsp_startup();
arch_boot_entry(0);
bsp_boot_entry();
magnitude_serial_up();
magnitude_putc('M');
magnitude_putc('a');
magnitude_putc('g');
magnitude_putc('\n');
__asm__ __volatile__("wfi");
}

View File

@@ -0,0 +1,147 @@
/*
| | | 1 | 0 |
| -- | --- | ------------------------ | --------------- |
| 0 | NS | | |
| 1 | IRQ | IRQ EL3 | IRQ EL1/EL2 |
| 2 | FIQ | FIQ EL3 | FIQ EL1/EL2 |
| 3 | EA | Abort EL3 | EL1/EL2 |
| 4 | SMD | SMC EL3 UNDEF | SMC EL3 |
| 5 | HCE | HVC EL1/EL2 UNDEF | HVC EL2 |
| 10 | RW | AArch64 | AArch32 |
*/
#include <magnitude/magnitude.h>
.section ".text.boot", "ax"
.align 4
.global _start
.extern __stack_top
.extern __stack_bottom
.extern mag_stack_count
.extern magnitude_start
.extern bsp_vector_table_el1
.weak bsp_start_hook_0
.weak bsp_start_hook_1
_start:
mov x5, x1 /* machine type number or ~0 for DT boot */
mov x6, x2 /* physical address of ATAGs or DTB */
//
mov x0, XZR
msr SCTLR_EL1, x0
// ID
mrs x0, MPIDR_EL1
and x0, x0, #0xff // ID
cmp x0, #0
b.ne secondary_halt // 0
// EL
mrs x0, CurrentEL // EL
lsr x0, x0, #2 // EL 0=EL0(), 1=EL1, 2=EL2, 3=EL3
cmp x0, #1 // EL1
beq el1_up // EL1
cmp x0, #2 // EL2
beq el2_up // EL2
//
.align 4
el3_up:
// 使
ldr x0, =__stack_top // mag_stack_top /
ldr x1, =mag_stack_count // mag_stack_count /
ldr x2, [x1] // mag_stack_count
// EL3 SP
mov x3, #MAGNITUDE_STACK_BOOT_SIZE //
mul x3, x3, x2 //
sub x0, x0, x3 //
msr spsel, #1 // SP_ELx
mov sp, x0 // EL3
// EL2 SP
add x2, x2, #1 //
mov x3, #MAGNITUDE_STACK_BOOT_SIZE //
mul x3, x3, x2 //
sub x0, x0, x3 //
msr sp_el2, x0 // EL2
// EL1 SP
add x2, x2, #1 //
mov x3, #MAGNITUDE_STACK_BOOT_SIZE //
mul x3, x3, x2 //
sub x0, x0, x3 //
msr sp_el1, x0 // EL1
// 使
add x2, x2, #1 // __stack_count 使 + 1
str x2, [x1] // __stack_count 使
// HCR_EL2 SCTLR_EL2
msr HCR_EL2, XZR
msr SCTLR_EL2, XZR
// scr_el3
mrs x0, scr_el3 //
orr x0, x0, #(1 << 10) // 64
orr x0, x0, #(1 << 0) //
msr scr_el3, x0 // SCR_EL3
// EL3 hook
bl bsp_start_hook_0
//
mov x0, #0b01001 // EL2
msr spsr_el3, x0 //
// EL1
ldr x0, =bsp_vector_table_el1
msr vbar_el1, x0
msr vbar_el2, x0
msr vbar_el3, x0
//
adr x0, el2_up
msr elr_el3, x0
//
eret
//
.align 4
el2_up:
// hcr_el2
mrs x0, HCR_EL2
orr x0, x0, #(1<<31) // 64
// Disable ID traps
// EL1 便便 Cache
bic x0, x0, #(1<<15) // EL1 TLB EL2
bic x0, x0, #(1<<16) // EL1 便
bic x0, x0, #(1<<17) // EL1 使 DC ZVA
bic x0, x0, #(1<<18) // EL1 Cache/
msr HCR_EL2, x0
// EL1
mov x0, #0b00101
msr spsr_el2, x0
//
adr x0, el1_up
msr ELR_EL2, x0
eret
//
.align 4
el1_up:
bl bsp_start_hook_0
//
b magnitude_start
.align 4
secondary_halt:
1: wfe
b 1b

View File

@@ -1,23 +0,0 @@
.section ".text.boot", "ax"
.align 7
.global _start
.extern __stack_top
.extern __stack_bottom
.extern __stack_count
.extern _arch_start
.extern _bsp_startup
.extern magnitude_start
_start:
// boot stack
ldr x3, =__stack_top // x3 =
ldr x4, =__stack_count // x4 = __stack_count
ldr x5, [x4] // x5 = __stack_count
add x5, x5, #1 // x5 = __stack_count + 1
str x5, [x4] // __stack_count
mov sp, x3 //
bl magnitude_start
ret