summary refs log tree commit diff
path: root/src/rt/arch/arm/morestack.S
blob: f0ec3f4b7a5118e6c6863654eddce2c61ea2d94f (plain)
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
64
65
66
67
68
69
70
71
// Mark stack as non-executable
#if defined(__linux__) && defined(__ELF__)
.section	.note.GNU-stack, "", %progbits
#endif

.text
.code 32
.arm
.align

.global upcall_new_stack
.global upcall_del_stack
.global __morestack
.hidden __morestack

// r4 and r5 are scratch registers for __morestack due to llvm
// ARMFrameLowering::adjustForSegmentedStacks() implementation.
 .type __morestack,%function
__morestack:
	.fnstart
	// Save frame pointer and return address
	.save {r4, r5}
	.save {lr}
	.save {r6, fp, lr}
    push {r6, fp, lr}

	.movsp r6
	mov r6, sp
	.setfp fp, sp, #4
	add fp, sp, #4

    // Save argument registers of the original function
    push {r0, r1, r2, r3, lr}

    mov r0, r4         // The amount of stack needed
    add r1, fp, #20    // Address of stack arguments
    mov r2, r5         // Size of stack arguments

    // Create new stack
    bl upcall_new_stack@plt

    // Hold new stack pointer
    mov r5, r0

    // Pop the saved arguments
    pop {r0, r1, r2, r3, lr}

    // Grab the return pointer
    add r4, lr, #16    // Skip past the return
    mov sp, r5         // Swich to the new stack
    mov lr, pc
    mov pc, r4         // Call the original function

    // Switch back to rust stack
    mov sp, r6

    // Save return value
	mov r4, r0
	mov r5, r1

    // Remove the new allocated stack
    bl upcall_del_stack@plt

    // Restore return value
	mov r0, r4
	mov r1, r5

    // Return
    pop {r6, fp, lr}
    mov pc, lr
    .fnend