aboutsummaryrefslogtreecommitdiff
path: root/lib/arch/riscv64/yield.s
blob: 985cb52b3a9a6ecc2a92f1f11ab3c83d039ed527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2021 Johannes Stoelp

    .macro push reg
        addi sp, sp, -8
        sd \reg, 0(sp)
    .endm

    .macro pop reg
        ld \reg, 0(sp)
        addi sp, sp, 8
    .endm

    .section .text, "ax", @progbits

    # extern "C" void yield(const void* new_stack, void** old_stack);
    #                                   ^^^^^^^^^         ^^^^^^^^^
    #                                   a0 (x10)          a1 (x11)
    .global yield
    .type   yield, @function
yield:
    .cfi_startproc
    # save return address
    push ra

    # push callee saved registers
    push x8
    push x9
    push x18
    push x19
    push x20
    push x21
    push x22
    push x23
    push x24
    push x25
    push x26
    push x27

    # arg0: a0 holds new stack
    # arg1: a1 holds addr to location current stack must be saved
    sd sp, 0(a1)  # save current stack ptr
    mv sp, a0     # switch to new stack ptr

    # pop callee saved registers
    pop x27
    pop x26
    pop x25
    pop x24
    pop x23
    pop x22
    pop x21
    pop x20
    pop x19
    pop x18
    pop x9
    pop x8

    # restore return address
    pop ra

    ret
    .cfi_endproc
    .size yield, .-yield