[kernel]: start 实现进行架构区分
startup.S 完全实现 除 el1_up 外
irq 暂未实现完全
This commit is contained in:
@@ -7,4 +7,6 @@
|
|||||||
|
|
||||||
#define MAGNITUDE_PRINT_BUFFER_SIZE 512
|
#define MAGNITUDE_PRINT_BUFFER_SIZE 512
|
||||||
|
|
||||||
|
#define MAGNITUDE_STACK_BOOT_SIZE 4096
|
||||||
|
|
||||||
#endif //MAGNITUDE_H
|
#endif //MAGNITUDE_H
|
||||||
|
|||||||
14
magnitude/kernel/include/magnitude/mm/stack.h
Normal file
14
magnitude/kernel/include/magnitude/mm/stack.h
Normal 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
|
||||||
@@ -8,8 +8,8 @@
|
|||||||
#include <magnitude/io/ringbuf.h>
|
#include <magnitude/io/ringbuf.h>
|
||||||
#include <magnitude/irq/irq.h>
|
#include <magnitude/irq/irq.h>
|
||||||
|
|
||||||
#include <console/bsp-uart.h>
|
#include <serial/bspuart.h>
|
||||||
#include <irq/bsp-gic.h>
|
#include <irq/bspgic.h>
|
||||||
|
|
||||||
#include <bsp.h>
|
#include <bsp.h>
|
||||||
|
|
||||||
@@ -44,9 +44,9 @@ void magnitude_serial_up ( void ) {
|
|||||||
// 使能串口
|
// 使能串口
|
||||||
bsp_uart_up();
|
bsp_uart_up();
|
||||||
// 安装 UART IRQ handler
|
// 安装 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 ) {
|
ssize_t magnitude_putc ( char c ) {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
#include <magnitude/irq/irq.h>
|
#include <magnitude/irq/irq.h>
|
||||||
#include <magnitude/irq/isr.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>
|
#include <bsp.h>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include <magnitude/irq/isr.h>
|
#include <magnitude/irq/isr.h>
|
||||||
#include <arch-register.h>
|
#include <archregister.h>
|
||||||
|
|
||||||
void magnitude_isr_enable ( void ) {
|
void magnitude_isr_enable ( void ) {
|
||||||
arch_irq_enable();
|
arch_irq_enable();
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#ifndef INCLUDED_tlsf
|
#ifndef INCLUDED_tlsf
|
||||||
#define INCLUDED_tlsf
|
#define INCLUDED_tlsf
|
||||||
|
|
||||||
#include <../../../include/magnitude/magnitude_assert.h>
|
#include <magnitude_assert.h>
|
||||||
#include <magnitude_types.h>
|
#include <magnitude_types.h>
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
|
|||||||
19
magnitude/kernel/mm/magstackalloc.c
Normal file
19
magnitude/kernel/mm/magstackalloc.c
Normal 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;
|
||||||
|
}
|
||||||
@@ -3,20 +3,29 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include <start/arch-start.h>
|
#include <start/arch-start.h>
|
||||||
#include <start/bsp-startup.h>
|
#include <start/bspstart.h>
|
||||||
#include <mmu/arch-mmu.h>
|
#include <mmu/arch-mmu.h>
|
||||||
|
|
||||||
#include <magnitude/kernel.h>
|
#include <magnitude/kernel.h>
|
||||||
#include <magnitude/mm/pmm.h>
|
#include <magnitude/mm/pmm.h>
|
||||||
|
|
||||||
|
#include <magnitude/io/io.h>
|
||||||
|
|
||||||
__attribute__((section(".text.boot")))
|
__attribute__((section(".text.boot")))
|
||||||
void magnitude_start(void) {
|
void magnitude_start(void) {
|
||||||
// 注册页表分配基本算法实现
|
// 注册页表分配基本算法实现
|
||||||
// 以供接下来MMU的物理内存申请
|
// 以供接下来MMU的物理内存申请
|
||||||
// arch_mmu_physics_callback.physics_alloc = magnitude_pmm_alloc;
|
// arch_mmu_physics_callback.physics_alloc = magnitude_pmm_alloc;
|
||||||
// arch_mmu_physics_callback.physics_free = magnitude_pmm_free;
|
// arch_mmu_physics_callback.physics_free = magnitude_pmm_free;
|
||||||
_arch_start();
|
arch_boot_entry(0);
|
||||||
_bsp_startup();
|
bsp_boot_entry();
|
||||||
|
|
||||||
|
magnitude_serial_up();
|
||||||
|
|
||||||
|
magnitude_putc('M');
|
||||||
|
magnitude_putc('a');
|
||||||
|
magnitude_putc('g');
|
||||||
|
magnitude_putc('\n');
|
||||||
|
|
||||||
__asm__ __volatile__("wfi");
|
__asm__ __volatile__("wfi");
|
||||||
}
|
}
|
||||||
147
magnitude/startup/aarch64/startup.S
Normal file
147
magnitude/startup/aarch64/startup.S
Normal 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
|
||||||
@@ -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
|
|
||||||
Reference in New Issue
Block a user