Skip to content

Instantly share code, notes, and snippets.

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

What would you like to do?
/* Move section to .init that is allocatable and executable. */
.section .init, "ax"
.align 2
.global _start
.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
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
.section .text
.align 4
.global 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
addi sp, sp, -28 # Allocate stack frame
csrr a0, mcause # write mcause to first function argument
csrr a1, mtval # write mtval (exception specific information) to second function argument
jal trap_handler
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.