Skip to content

Instantly share code, notes, and snippets.

@benmezger benmezger/crt0.S
Created Jan 12, 2020

Embed
What would you like to do?
/* Move section to .init that is allocatable and executable. */
.section .init, "ax"
.align 2
.global _start
_start:
.cfi_startproc
.cfi_undefined ra
.option push
.option norelax
la gp, __global_pointer$
.option pop
la sp, __stack_top
add s0, sp, zero
j setup_mtrap
jal zero, main
.cfi_endproc
.end
.macro SAVE_CONTEXT
sw ra, 24(sp) # save return address to the stack
sw t0, 20(sp) # save temporary
sw t1, 16(sp) # save temporary
sw t2, 12(sp) # save temporary
sw t3, 8(sp) # save temporary
sw a0, 4(sp) # save first argument to function
.endm
.section .text
.align 4
.global setup_mtrap
setup_mtrap:
# Setup machine trap handler address
la t0, _trap_entry # load address of mtrap_handler
# Set DIRECT_MODE access to
slli t0, t0, 1 # shift address left by one ; LSB will be 0 (DIRECT_MODE)
csrw mtvec, t0 # write handler and address mode
# enable machine-mode interrupt (mstatus.MIE = 1)
li t0, 8
csrrs zero, mstatus, t0
_trap_entry:
addi sp, sp, -28 # Allocate stack frame
SAVE_CONTEXT
csrr a0, mcause # write mcause to first function argument
csrr a1, mtval # write mtval (exception specific information) to second function argument
jal trap_handler
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.