diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/arch/riscv/rv64gc/include/asm/asm-offsets.h b/Ubiquitous/XiZi_AIoT/hardkernel/arch/riscv/rv64gc/include/asm/asm-offsets.h new file mode 100644 index 000000000..58e0bf035 --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/hardkernel/arch/riscv/rv64gc/include/asm/asm-offsets.h @@ -0,0 +1,156 @@ +#ifndef __ASM_OFFSETS_H__ +#define __ASM_OFFSETS_H__ +/* + * DO NOT MODIFY. + * + * This file was generated by Kbuild + */ + +#define TASK_THREAD_RA 2232 /* offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_SP 2240 /* offsetof(struct task_struct, thread.sp) */ +#define TASK_THREAD_S0 2248 /* offsetof(struct task_struct, thread.s[0]) */ +#define TASK_THREAD_S1 2256 /* offsetof(struct task_struct, thread.s[1]) */ +#define TASK_THREAD_S2 2264 /* offsetof(struct task_struct, thread.s[2]) */ +#define TASK_THREAD_S3 2272 /* offsetof(struct task_struct, thread.s[3]) */ +#define TASK_THREAD_S4 2280 /* offsetof(struct task_struct, thread.s[4]) */ +#define TASK_THREAD_S5 2288 /* offsetof(struct task_struct, thread.s[5]) */ +#define TASK_THREAD_S6 2296 /* offsetof(struct task_struct, thread.s[6]) */ +#define TASK_THREAD_S7 2304 /* offsetof(struct task_struct, thread.s[7]) */ +#define TASK_THREAD_S8 2312 /* offsetof(struct task_struct, thread.s[8]) */ +#define TASK_THREAD_S9 2320 /* offsetof(struct task_struct, thread.s[9]) */ +#define TASK_THREAD_S10 2328 /* offsetof(struct task_struct, thread.s[10]) */ +#define TASK_THREAD_S11 2336 /* offsetof(struct task_struct, thread.s[11]) */ +#define TASK_TI_FLAGS 0 /* offsetof(struct task_struct, thread_info.flags) */ +#define TASK_TI_PREEMPT_COUNT 8 /* offsetof(struct task_struct, thread_info.preempt_count) */ +#define TASK_TI_KERNEL_SP 16 /* offsetof(struct task_struct, thread_info.kernel_sp) */ +#define TASK_TI_USER_SP 24 /* offsetof(struct task_struct, thread_info.user_sp) */ +#define TASK_TI_CPU 32 /* offsetof(struct task_struct, thread_info.cpu) */ +#define TASK_THREAD_F0 2344 /* offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F1 2352 /* offsetof(struct task_struct, thread.fstate.f[1]) */ +#define TASK_THREAD_F2 2360 /* offsetof(struct task_struct, thread.fstate.f[2]) */ +#define TASK_THREAD_F3 2368 /* offsetof(struct task_struct, thread.fstate.f[3]) */ +#define TASK_THREAD_F4 2376 /* offsetof(struct task_struct, thread.fstate.f[4]) */ +#define TASK_THREAD_F5 2384 /* offsetof(struct task_struct, thread.fstate.f[5]) */ +#define TASK_THREAD_F6 2392 /* offsetof(struct task_struct, thread.fstate.f[6]) */ +#define TASK_THREAD_F7 2400 /* offsetof(struct task_struct, thread.fstate.f[7]) */ +#define TASK_THREAD_F8 2408 /* offsetof(struct task_struct, thread.fstate.f[8]) */ +#define TASK_THREAD_F9 2416 /* offsetof(struct task_struct, thread.fstate.f[9]) */ +#define TASK_THREAD_F10 2424 /* offsetof(struct task_struct, thread.fstate.f[10]) */ +#define TASK_THREAD_F11 2432 /* offsetof(struct task_struct, thread.fstate.f[11]) */ +#define TASK_THREAD_F12 2440 /* offsetof(struct task_struct, thread.fstate.f[12]) */ +#define TASK_THREAD_F13 2448 /* offsetof(struct task_struct, thread.fstate.f[13]) */ +#define TASK_THREAD_F14 2456 /* offsetof(struct task_struct, thread.fstate.f[14]) */ +#define TASK_THREAD_F15 2464 /* offsetof(struct task_struct, thread.fstate.f[15]) */ +#define TASK_THREAD_F16 2472 /* offsetof(struct task_struct, thread.fstate.f[16]) */ +#define TASK_THREAD_F17 2480 /* offsetof(struct task_struct, thread.fstate.f[17]) */ +#define TASK_THREAD_F18 2488 /* offsetof(struct task_struct, thread.fstate.f[18]) */ +#define TASK_THREAD_F19 2496 /* offsetof(struct task_struct, thread.fstate.f[19]) */ +#define TASK_THREAD_F20 2504 /* offsetof(struct task_struct, thread.fstate.f[20]) */ +#define TASK_THREAD_F21 2512 /* offsetof(struct task_struct, thread.fstate.f[21]) */ +#define TASK_THREAD_F22 2520 /* offsetof(struct task_struct, thread.fstate.f[22]) */ +#define TASK_THREAD_F23 2528 /* offsetof(struct task_struct, thread.fstate.f[23]) */ +#define TASK_THREAD_F24 2536 /* offsetof(struct task_struct, thread.fstate.f[24]) */ +#define TASK_THREAD_F25 2544 /* offsetof(struct task_struct, thread.fstate.f[25]) */ +#define TASK_THREAD_F26 2552 /* offsetof(struct task_struct, thread.fstate.f[26]) */ +#define TASK_THREAD_F27 2560 /* offsetof(struct task_struct, thread.fstate.f[27]) */ +#define TASK_THREAD_F28 2568 /* offsetof(struct task_struct, thread.fstate.f[28]) */ +#define TASK_THREAD_F29 2576 /* offsetof(struct task_struct, thread.fstate.f[29]) */ +#define TASK_THREAD_F30 2584 /* offsetof(struct task_struct, thread.fstate.f[30]) */ +#define TASK_THREAD_F31 2592 /* offsetof(struct task_struct, thread.fstate.f[31]) */ +#define TASK_THREAD_FCSR 2600 /* offsetof(struct task_struct, thread.fstate.fcsr) */ +#define TSK_STACK_CANARY 1080 /* offsetof(struct task_struct, stack_canary) */ +#define PT_SIZE 288 /* sizeof(struct pt_regs) */ +#define PT_EPC 0 /* offsetof(struct pt_regs, epc) */ +#define PT_RA 8 /* offsetof(struct pt_regs, ra) */ +#define PT_FP 64 /* offsetof(struct pt_regs, s0) */ +#define PT_S0 64 /* offsetof(struct pt_regs, s0) */ +#define PT_S1 72 /* offsetof(struct pt_regs, s1) */ +#define PT_S2 144 /* offsetof(struct pt_regs, s2) */ +#define PT_S3 152 /* offsetof(struct pt_regs, s3) */ +#define PT_S4 160 /* offsetof(struct pt_regs, s4) */ +#define PT_S5 168 /* offsetof(struct pt_regs, s5) */ +#define PT_S6 176 /* offsetof(struct pt_regs, s6) */ +#define PT_S7 184 /* offsetof(struct pt_regs, s7) */ +#define PT_S8 192 /* offsetof(struct pt_regs, s8) */ +#define PT_S9 200 /* offsetof(struct pt_regs, s9) */ +#define PT_S10 208 /* offsetof(struct pt_regs, s10) */ +#define PT_S11 216 /* offsetof(struct pt_regs, s11) */ +#define PT_SP 16 /* offsetof(struct pt_regs, sp) */ +#define PT_TP 32 /* offsetof(struct pt_regs, tp) */ +#define PT_A0 80 /* offsetof(struct pt_regs, a0) */ +#define PT_A1 88 /* offsetof(struct pt_regs, a1) */ +#define PT_A2 96 /* offsetof(struct pt_regs, a2) */ +#define PT_A3 104 /* offsetof(struct pt_regs, a3) */ +#define PT_A4 112 /* offsetof(struct pt_regs, a4) */ +#define PT_A5 120 /* offsetof(struct pt_regs, a5) */ +#define PT_A6 128 /* offsetof(struct pt_regs, a6) */ +#define PT_A7 136 /* offsetof(struct pt_regs, a7) */ +#define PT_T0 40 /* offsetof(struct pt_regs, t0) */ +#define PT_T1 48 /* offsetof(struct pt_regs, t1) */ +#define PT_T2 56 /* offsetof(struct pt_regs, t2) */ +#define PT_T3 224 /* offsetof(struct pt_regs, t3) */ +#define PT_T4 232 /* offsetof(struct pt_regs, t4) */ +#define PT_T5 240 /* offsetof(struct pt_regs, t5) */ +#define PT_T6 248 /* offsetof(struct pt_regs, t6) */ +#define PT_GP 24 /* offsetof(struct pt_regs, gp) */ +#define PT_ORIG_A0 280 /* offsetof(struct pt_regs, orig_a0) */ +#define PT_STATUS 256 /* offsetof(struct pt_regs, status) */ +#define PT_BADADDR 264 /* offsetof(struct pt_regs, badaddr) */ +#define PT_CAUSE 272 /* offsetof(struct pt_regs, cause) */ +#define SUSPEND_CONTEXT_REGS 0 /* offsetof(struct suspend_context, regs) */ +#define HIBERN_PBE_ADDR 0 /* offsetof(struct pbe, address) */ +#define HIBERN_PBE_ORIG 8 /* offsetof(struct pbe, orig_address) */ +#define HIBERN_PBE_NEXT 16 /* offsetof(struct pbe, next) */ +#define TASK_THREAD_RA_RA 0 /* offsetof(struct task_struct, thread.ra) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_SP_RA 8 /* offsetof(struct task_struct, thread.sp) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S0_RA 16 /* offsetof(struct task_struct, thread.s[0]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S1_RA 24 /* offsetof(struct task_struct, thread.s[1]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S2_RA 32 /* offsetof(struct task_struct, thread.s[2]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S3_RA 40 /* offsetof(struct task_struct, thread.s[3]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S4_RA 48 /* offsetof(struct task_struct, thread.s[4]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S5_RA 56 /* offsetof(struct task_struct, thread.s[5]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S6_RA 64 /* offsetof(struct task_struct, thread.s[6]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S7_RA 72 /* offsetof(struct task_struct, thread.s[7]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S8_RA 80 /* offsetof(struct task_struct, thread.s[8]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S9_RA 88 /* offsetof(struct task_struct, thread.s[9]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S10_RA 96 /* offsetof(struct task_struct, thread.s[10]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_S11_RA 104 /* offsetof(struct task_struct, thread.s[11]) - offsetof(struct task_struct, thread.ra) */ +#define TASK_THREAD_F0_F0 0 /* offsetof(struct task_struct, thread.fstate.f[0]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F1_F0 8 /* offsetof(struct task_struct, thread.fstate.f[1]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F2_F0 16 /* offsetof(struct task_struct, thread.fstate.f[2]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F3_F0 24 /* offsetof(struct task_struct, thread.fstate.f[3]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F4_F0 32 /* offsetof(struct task_struct, thread.fstate.f[4]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F5_F0 40 /* offsetof(struct task_struct, thread.fstate.f[5]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F6_F0 48 /* offsetof(struct task_struct, thread.fstate.f[6]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F7_F0 56 /* offsetof(struct task_struct, thread.fstate.f[7]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F8_F0 64 /* offsetof(struct task_struct, thread.fstate.f[8]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F9_F0 72 /* offsetof(struct task_struct, thread.fstate.f[9]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F10_F0 80 /* offsetof(struct task_struct, thread.fstate.f[10]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F11_F0 88 /* offsetof(struct task_struct, thread.fstate.f[11]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F12_F0 96 /* offsetof(struct task_struct, thread.fstate.f[12]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F13_F0 104 /* offsetof(struct task_struct, thread.fstate.f[13]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F14_F0 112 /* offsetof(struct task_struct, thread.fstate.f[14]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F15_F0 120 /* offsetof(struct task_struct, thread.fstate.f[15]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F16_F0 128 /* offsetof(struct task_struct, thread.fstate.f[16]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F17_F0 136 /* offsetof(struct task_struct, thread.fstate.f[17]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F18_F0 144 /* offsetof(struct task_struct, thread.fstate.f[18]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F19_F0 152 /* offsetof(struct task_struct, thread.fstate.f[19]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F20_F0 160 /* offsetof(struct task_struct, thread.fstate.f[20]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F21_F0 168 /* offsetof(struct task_struct, thread.fstate.f[21]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F22_F0 176 /* offsetof(struct task_struct, thread.fstate.f[22]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F23_F0 184 /* offsetof(struct task_struct, thread.fstate.f[23]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F24_F0 192 /* offsetof(struct task_struct, thread.fstate.f[24]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F25_F0 200 /* offsetof(struct task_struct, thread.fstate.f[25]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F26_F0 208 /* offsetof(struct task_struct, thread.fstate.f[26]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F27_F0 216 /* offsetof(struct task_struct, thread.fstate.f[27]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F28_F0 224 /* offsetof(struct task_struct, thread.fstate.f[28]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F29_F0 232 /* offsetof(struct task_struct, thread.fstate.f[29]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F30_F0 240 /* offsetof(struct task_struct, thread.fstate.f[30]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_F31_F0 248 /* offsetof(struct task_struct, thread.fstate.f[31]) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define TASK_THREAD_FCSR_F0 256 /* offsetof(struct task_struct, thread.fstate.fcsr) - offsetof(struct task_struct, thread.fstate.f[0]) */ +#define PT_SIZE_ON_STACK 288 /* ALIGN(sizeof(struct pt_regs), STACK_ALIGN) */ +#define KERNEL_MAP_VIRT_ADDR 0 /* offsetof(struct kernel_mapping, virt_addr) */ +#define SBI_HART_BOOT_TASK_PTR_OFFSET 0 /* offsetof(struct sbi_hart_boot_data, task_ptr) */ +#define SBI_HART_BOOT_STACK_PTR_OFFSET 8 /* offsetof(struct sbi_hart_boot_data, stack_ptr) */ + +#endif diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/Makefile b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/Makefile index 6fd2fb6d5..aed82542a 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/Makefile +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/Makefile @@ -1,4 +1,4 @@ -SRC_FILES := trampoline.S $(BOARD)/trap_common.c $(BOARD)/trap.c error_debug.c hard_spinlock.S +SRC_FILES := entry.S trampoline.S $(BOARD)/trap_common.c $(BOARD)/trap.c error_debug.c hard_spinlock.S ifeq ($(BOARD), jh7110) SRC_DIR := gicv3 diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/entry.S b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/entry.S new file mode 100644 index 000000000..177610739 --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/entry.S @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2020 AIIT XUOS Lab + * XiUOS is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +/** + * @file entry.S + * @brief trap in and out code + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2024-12-10 + */ + +/************************************************* +File name: entry.S +Description: trap in and out code +Others: +History: +1. Date: 2024-12-10 +Author: AIIT XUOS Lab +Modification: +1. first version +*************************************************/ + +#include "asm/asm-offsets.h" + +.macro kernel_entry + addi sp, sp, -(PT_SIZE) + + sd x1, PT_RA(sp) + sd x3, PT_GP(sp) + sd x5, PT_T0(sp) + sd x6, PT_T1(sp) + sd x7, PT_T2(sp) + sd x8, PT_S0(sp) + sd x9, PT_S1(sp) + sd x10, PT_A0(sp) + sd x11, PT_A1(sp) + sd x12, PT_A2(sp) + sd x13, PT_A3(sp) + sd x14, PT_A4(sp) + sd x15, PT_A5(sp) + sd x16, PT_A6(sp) + sd x17, PT_A7(sp) + sd x18, PT_S2(sp) + sd x19, PT_S3(sp) + sd x20, PT_S4(sp) + sd x21, PT_S5(sp) + sd x22, PT_S6(sp) + sd x23, PT_S7(sp) + sd x24, PT_S8(sp) + sd x25, PT_S9(sp) + sd x26, PT_S10(sp) + sd x27, PT_S11(sp) + sd x28, PT_T3(sp) + sd x29, PT_T4(sp) + sd x30, PT_T5(sp) + sd x31, PT_T6(sp) + + csrr s2, sepc + sd s2, PT_EPC(sp) + csrr s3, sbadaddr + sd s3, PT_BADADDR(sp) + csrr s4, scause + sd s4, PT_CAUSE(sp) + csrr s5, sscratch + sd s5, PT_TP(sp) + + addi s0, sp, PT_SIZE + sd sp, PT_SP(sp) +.endm + + +.macro kernel_exit + ld a0, PT_STATUS(sp) + csrw sstatus, a0 + ld a2, PT_EPC(sp) + csrw sepc, a2 + + ld x1, PT_RA(sp) + ld x3, PT_GP(sp) + ld x5, PT_T0(sp) + ld x6, PT_T1(sp) + ld x7, PT_T2(sp) + ld x8, PT_S0(sp) + ld x9, PT_S1(sp) + ld x10, PT_A0(sp) + ld x11, PT_A1(sp) + ld x12, PT_A2(sp) + ld x13, PT_A3(sp) + ld x14, PT_A4(sp) + ld x15, PT_A5(sp) + ld x16, PT_A6(sp) + ld x17, PT_A7(sp) + ld x18, PT_S2(sp) + ld x19, PT_S3(sp) + ld x20, PT_S4(sp) + ld x21, PT_S5(sp) + ld x22, PT_S6(sp) + ld x23, PT_S7(sp) + ld x24, PT_S8(sp) + ld x25, PT_S9(sp) + ld x26, PT_S10(sp) + ld x27, PT_S11(sp) + ld x28, PT_T3(sp) + ld x29, PT_T4(sp) + ld x30, PT_T5(sp) + ld x31, PT_T6(sp) + + ld x2, PT_SP(sp) +.endm + + + +.align 4 + .global do_exception_vector +do_exception_vector: + kernel_entry + la ra, ret_from_exception + mv a0, sp /* pt_regs */ + mv a1, s4 + tail do_exception + +ret_from_exception: +restore_all: + kernel_exit + sret + +.global trigger_fault +trigger_fault: + li a0, 0x70000000 + ld a0, (a0) + ret diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/jh7110/trap.c b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/jh7110/trap.c index f28c7a6a3..5361f8f2a 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/jh7110/trap.c +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/jh7110/trap.c @@ -38,6 +38,10 @@ Modification: #include "mmu.h" +#include "asm/csr.h" +#include "ptrace.h" + + extern void dabort_handler(struct trapframe* r); extern void iabort_handler(struct trapframe* r); @@ -118,4 +122,114 @@ void syscall_arch_handler(struct trapframe* tf) panic("dabort end should never be reashed.\n"); } } -} \ No newline at end of file +} + + + +static void do_trap_error(struct pt_regs *regs, const char *str) +{ + printk("Oops: %s\n", str); + printk("sstatus: 0x%016lx, sbadaddr: 0x%016lx, scause: 0x%016lx\n", + regs->status, regs->badaddr, regs->cause); + panic("Fatal exception\n"); +} + +#define DO_ERROR_INFO(name) \ +static int name(struct pt_regs *regs, const char *str) \ +{ \ + do_trap_error(regs, str); \ + return 0; \ +} + +DO_ERROR_INFO(do_trap_unknown); +DO_ERROR_INFO(do_trap_insn_misaligned); +DO_ERROR_INFO(do_trap_insn_fault); +DO_ERROR_INFO(do_trap_insn_illegal); +DO_ERROR_INFO(do_trap_load_misaligned); +DO_ERROR_INFO(do_trap_load_fault); +DO_ERROR_INFO(do_trap_store_misaligned); +DO_ERROR_INFO(do_trap_store_fault); +DO_ERROR_INFO(do_trap_ecall_u); +DO_ERROR_INFO(do_trap_ecall_s); +DO_ERROR_INFO(do_trap_break); +DO_ERROR_INFO(do_page_fault); + +struct fault_info { + int (*fn)(struct pt_regs *regs, const char *name); + const char *name; +}; + +static const struct fault_info fault_inf[] = { + {do_trap_insn_misaligned, "Instruction address misaligned"}, + {do_trap_insn_fault, "Instruction access fault"}, + {do_trap_insn_illegal, "Illegal instruction"}, + {do_trap_break, "Breakpoint"}, + {do_trap_load_misaligned, "Load address misaligned"}, + {do_trap_load_fault, "Load access fault"}, + {do_trap_store_misaligned, "Store/AMO address misaligned"}, + {do_trap_store_fault, "Store/AMO access fault"}, + {do_trap_ecall_u, "Environment call from U-mode"}, + {do_trap_ecall_s, "Environment call from S-mode"}, + {do_trap_unknown, "unknown 10"}, + {do_trap_unknown, "unknown 11"}, + {do_page_fault, "Instruction page fault"}, + {do_page_fault, "Load page fault"}, + {do_trap_unknown, "unknown 14"}, + {do_page_fault, "Store/AMO page fault"}, +}; + + +/* +void delegate_traps(void) +{ + unsigned long interrupts = MIP_SSIP | MIP_STIP | MIP_SEIP; + + unsigned long exceptions = (1UL << CAUSE_MISALIGNED_FETCH) | + (1UL << CAUSE_FETCH_PAGE_FAULT) | + (1UL << CAUSE_BREAKPOINT) | + (1UL << CAUSE_LOAD_PAGE_FAULT) | + (1UL << CAUSE_STORE_PAGE_FAULT) | + (1UL << CAUSE_USER_ECALL) | + (1UL << CAUSE_LOAD_ACCESS_FAULT) | + (1UL << CAUSE_STORE_ACCESS_FAULT); + + csr_write(mideleg, interrupts); + csr_write(medeleg, exceptions); +} +*/ + + +struct fault_info * ec_to_fault_info(unsigned long scause) +{ + struct fault_info *inf; + if (scause >= (sizeof(fault_inf)/sizeof(fault_inf[0]))) { + printk("The cause is out of range Exception Code, scause=0x%lx\n", scause); + panic("Fatal exception\n"); + } + inf = &fault_inf[scause]; + return inf; +} +extern void do_exception_vector(void); +void trap_init(void) +{ + csr_write(stvec, do_exception_vector); + //printk("stvec=0x%lx, do_exception_vector=0x%lx\n", csr_read(stvec), (unsigned long)do_exception_vector); + csr_write(sie, 0); +} + +void do_exception(struct pt_regs *regs, unsigned long scause) +{ + const struct fault_info *inf; + + printk("%s, scause: 0x%lx\n", __func__, scause); + + if (scause & CAUSE_IRQ_FLAG) { + // TODO: 处理中断 + } + else { + inf = ec_to_fault_info(scause); + if (!inf->fn(regs, inf->name)) { + return; + } + } +} diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/ptrace.h b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/ptrace.h new file mode 100644 index 000000000..0427b750e --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/hardkernel/intr/riscv/rv64gc/ptrace.h @@ -0,0 +1,114 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2012 Regents of the University of California + */ + +#ifndef _ASM_RISCV_PTRACE_H +#define _ASM_RISCV_PTRACE_H + +//#include +//#include +//#include + + +struct pt_regs { + unsigned long epc; + unsigned long ra; + unsigned long sp; + unsigned long gp; + unsigned long tp; + unsigned long t0; + unsigned long t1; + unsigned long t2; + unsigned long s0; + unsigned long s1; + unsigned long a0; + unsigned long a1; + unsigned long a2; + unsigned long a3; + unsigned long a4; + unsigned long a5; + unsigned long a6; + unsigned long a7; + unsigned long s2; + unsigned long s3; + unsigned long s4; + unsigned long s5; + unsigned long s6; + unsigned long s7; + unsigned long s8; + unsigned long s9; + unsigned long s10; + unsigned long s11; + unsigned long t3; + unsigned long t4; + unsigned long t5; + unsigned long t6; + /* Supervisor/Machine CSRs */ + unsigned long status; + unsigned long badaddr; + unsigned long cause; + /* a0 value before the syscall */ + unsigned long orig_a0; +}; + +#define REG_FMT "%016lx" + +#define user_mode(regs) (((regs)->status & SR_PP) == 0) + +//#define MAX_REG_OFFSET offsetof(struct pt_regs, orig_a0) + +/* Helpers for working with the instruction pointer */ +static inline unsigned long instruction_pointer(struct pt_regs *regs) +{ + return regs->epc; +} +static inline void instruction_pointer_set(struct pt_regs *regs, + unsigned long val) +{ + regs->epc = val; +} + +#define profile_pc(regs) instruction_pointer(regs) + +/* Helpers for working with the user stack pointer */ +static inline unsigned long user_stack_pointer(struct pt_regs *regs) +{ + return regs->sp; +} +static inline void user_stack_pointer_set(struct pt_regs *regs, + unsigned long val) +{ + regs->sp = val; +} + +/* Valid only for Kernel mode traps. */ +static inline unsigned long kernel_stack_pointer(struct pt_regs *regs) +{ + return regs->sp; +} + +/* Helpers for working with the frame pointer */ +static inline unsigned long frame_pointer(struct pt_regs *regs) +{ + return regs->s0; +} +static inline void frame_pointer_set(struct pt_regs *regs, + unsigned long val) +{ + regs->s0 = val; +} + +static inline unsigned long regs_return_value(struct pt_regs *regs) +{ + return regs->a0; +} + +static inline void regs_set_return_value(struct pt_regs *regs, + unsigned long val) +{ + regs->a0 = val; +} + + +#endif /* _ASM_RISCV_PTRACE_H */ diff --git a/Ubiquitous/XiZi_AIoT/hardkernel/uart/printf.h b/Ubiquitous/XiZi_AIoT/hardkernel/uart/printf.h index 93e207e93..ed0bd0d3b 100644 --- a/Ubiquitous/XiZi_AIoT/hardkernel/uart/printf.h +++ b/Ubiquitous/XiZi_AIoT/hardkernel/uart/printf.h @@ -51,6 +51,7 @@ extern "C" { #define printf printf_ int printf_(const char* format, ...); int printf_early(const char* format, ...); +#define printk printf_ /** * Tiny sprintf implementation @@ -100,4 +101,5 @@ int fctprintf(void (*out)(char character, void* arg), void* arg, const char* for } #endif -#endif // _PRINTF_H_ \ No newline at end of file +#endif // _PRINTF_H_ +