-
Notifications
You must be signed in to change notification settings - Fork 2
/
ultmigration.s
198 lines (170 loc) · 4.39 KB
/
ultmigration.s
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright © 2017, Mathias Gottschlag
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
.global ult_pool_thread_entry
/* rdi = current thread_pool_info */
ult_pool_thread_entry:
/* push callee-saved registers to the stack */
sub $64, %rsp
rdfsbase %rax
mov %rax, 56(%rsp)
rdgsbase %rax
mov %rax, 48(%rsp)
mov %rbp, 40(%rsp)
mov %rbx, 32(%rsp)
mov %r12, 24(%rsp)
mov %r13, 16(%rsp)
mov %r14, 8(%rsp)
mov %r15, 0(%rsp)
/* save stack pointer to the thread struct so that we can restart from
* here when a user level thread returns control */
mov %rsp, 64(%rdi)
push %rdi
call ult_set_pool_thread_affinity@PLT
pop %rdi
pick_next_thread:
/* pick the next thread to execute */
push %rdi
call ult_pick_next_thread@PLT
pop %rdi
cmp $0xffffffffffffffff, %rax
je stop_pool_thread
/* valid next thread, switch to it */
/* register the kernel level thread in the ULT struct */
mov %rdi, (%rax)
/* restore the thread state */
/* load the stack pointer */
mov 8(%rax), %rsp
/* restore callee-saved registers */
restore_thread:
mov 56(%rsp), %rax
wrfsbase %rax
mov 48(%rsp), %rax
wrgsbase %rax
mov 40(%rsp), %rbp
mov 32(%rsp), %rbx
mov 24(%rsp), %r12
mov 16(%rsp), %r13
mov 8(%rsp), %r14
mov 0(%rsp), %r15
add $64, %rsp
/* return to the caller */
ret
stop_pool_thread:
/* restore callee-saved registers */
mov 56(%rsp), %rax
wrfsbase %rax
mov 48(%rsp), %rax
wrgsbase %rax
mov 40(%rsp), %rbp
mov 32(%rsp), %rbx
mov 24(%rsp), %r12
mov 16(%rsp), %r13
mov 8(%rsp), %r14
mov 0(%rsp), %r15
add $64, %rsp
/* return to the caller */
ret
.global ult_migrate_asm
ult_migrate_asm:
/* push callee-saved registers to the stack */
sub $64, %rsp
rdfsbase %rax
mov %rax, 56(%rsp)
rdgsbase %rax
mov %rax, 48(%rsp)
mov %rbp, 40(%rsp)
mov %rbx, 32(%rsp)
mov %r12, 24(%rsp)
mov %r13, 16(%rsp)
mov %r14, 8(%rsp)
mov %r15, 0(%rsp)
/* save stack pointer in user-level thread struct */
mov %rsp, 8(%rdi)
/* switch stack (required if signals interrupt this thread) */
mov (%rdi), %rdx /* current kernel-level thread */
mov 64(%rdx), %rsp
/* insert thread into destination ready list */
mov $7, %rcx
mov $0, %rax
1:
add $1, %rcx
and $7, %rcx
lock cmpxchg %rdi, 24(%rsi, %rcx, 8) /* wakes up the destination */
jnz 1b
/* let this kernel-level thread wait for the next ULT */
mov %rdx, %rdi
jmp pick_next_thread
.global ult_register_asm
ult_register_asm:
/* push callee-saved registers to the stack */
sub $64, %rsp
rdfsbase %rax
mov %rax, 56(%rsp)
rdgsbase %rax
mov %rax, 48(%rsp)
mov %rbp, 40(%rsp)
mov %rbx, 32(%rsp)
mov %r12, 24(%rsp)
mov %r13, 16(%rsp)
mov %r14, 8(%rsp)
mov %r15, 0(%rsp)
/* save stack pointer in user-level thread struct */
mov %rsp, 8(%rdi)
/* switch stack */
mov %rdi, %rax
add $0x1010, %rax
mov %rax, %rsp
/* align stack */
and $0xfffffffffffffff0, %rsp
sub $8, %rsp
/* insert thread into destination ready list */
mov $7, %rcx
mov $0, %rax
1:
add $1, %rcx
and $7, %rcx
lock cmpxchg %rdi, 24(%rsi, %rcx, 8) /* wakes up the destination */
jnz 1b
/* wait for the ULT to finish */
push %rdi
call ult_wait_for_unregister@PLT
pop %rdi
/* restore the thread */
mov 8(%rdi), %rsp
jmp restore_thread
.global ult_unregister_asm
ult_unregister_asm:
/* push callee-saved registers to the stack */
sub $64, %rsp
rdfsbase %rax
mov %rax, 56(%rsp)
rdgsbase %rax
mov %rax, 48(%rsp)
mov %rbp, 40(%rsp)
mov %rbx, 32(%rsp)
mov %r12, 24(%rsp)
mov %r13, 16(%rsp)
mov %r14, 8(%rsp)
mov %r15, 0(%rsp)
/* save stack pointer in user-level thread struct */
mov %rsp, 8(%rdi)
/* switch stack (required if signals interrupt this thread) */
mov (%rdi), %rdx /* current kernel-level thread */
mov 64(%rdx), %rsp
push %rdx
call ult_signal_unregister@PLT
pop %rdi
jmp pick_next_thread