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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2020-2020. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#include "beam_asm.hpp"
extern "C"
{
#include "bif.h"
#include "code_ix.h"
#include "erl_proc_sig_queue.h"
#ifdef USE_VM_PROBES
# include "dtrace-wrapper.h"
#endif
}
#ifdef ERTS_SUPPORT_OLD_RECV_MARK_INSTRS
static void recv_mark(Process *p) {
/* inlined here... */
erts_msgq_recv_marker_insert_bind(p, erts_old_recv_marker_id);
}
static void recv_mark_set(Process *p) {
/* inlined here... */
erts_msgq_recv_marker_set_save(p, erts_old_recv_marker_id);
}
void BeamModuleAssembler::emit_i_recv_mark() {
/*
* OLD INSTRUCTION: This instruction is to be removed
* in OTP 26.
*
* Save the current end of message queue
*/
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<1>(recv_mark);
emit_leave_runtime();
}
void BeamModuleAssembler::emit_i_recv_set() {
/*
* OLD INSTRUCTION: This instruction is to be removed
* in OTP 26.
*
* If previously saved recv mark, set save pointer to it
*/
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<1>(recv_mark_set);
emit_leave_runtime();
}
#endif /* ERTS_SUPPORT_OLD_RECV_MARK_INSTRS */
void BeamModuleAssembler::emit_recv_marker_reserve(const ArgVal &Dst) {
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<1>(erts_msgq_recv_marker_insert);
emit_leave_runtime();
mov_arg(Dst, ARG1);
}
void BeamModuleAssembler::emit_recv_marker_bind(const ArgVal &Marker,
const ArgVal &Reference) {
mov_arg(ARG2, Marker);
mov_arg(ARG3, Reference);
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<3>(erts_msgq_recv_marker_bind);
emit_leave_runtime();
}
void BeamModuleAssembler::emit_recv_marker_clear(const ArgVal &Reference) {
mov_arg(ARG2, Reference);
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<2>(erts_msgq_recv_marker_clear);
emit_leave_runtime();
}
void BeamModuleAssembler::emit_recv_marker_use(const ArgVal &Reference) {
mov_arg(ARG2, Reference);
emit_enter_runtime();
a.mov(ARG1, c_p);
runtime_call<2>(erts_msgq_recv_marker_set_save);
emit_leave_runtime();
}
#ifdef ERTS_ENABLE_LOCK_CHECK
int erts_lc_proc_sig_receive_helper(Process *c_p,
int fcalls,
int neg_o_reds,
ErtsMessage **msgpp,
int *get_outp) {
int res;
/*
* erts_proc_sig_receive_helper() may temporarliy release
* its own main lock...
*/
ERTS_UNREQ_PROC_MAIN_LOCK(c_p);
res = erts_proc_sig_receive_helper(c_p,
fcalls,
neg_o_reds,
msgpp,
get_outp);
ERTS_REQ_PROC_MAIN_LOCK(c_p);
return res;
}
#endif
void BeamGlobalAssembler::emit_i_loop_rec_shared() {
Label restart = a.newLabel(), peek_message = a.newLabel(),
schedule_out = a.newLabel(), check_is_distributed = a.newLabel(),
done = a.newLabel();
arm::Mem await_addr = TMP_MEM1q, message_ptr = TMP_MEM2q,
get_out = TMP_MEM3q;
arm::Mem flags = arm::Mem(c_p, offsetof(Process, flags));
a.mov(XREG1, a64::x30);
a.ldr(TMP1.w(), flags);
a.orr(TMP1, TMP1, imm(F_DELAY_GC));
a.str(TMP1.w(), flags);
a.str(ARG1, arm::Mem(c_p, offsetof(Process, i)));
a.str(ARG2, await_addr);
a.bind(restart);
{
a.tst(FCALLS, FCALLS);
a.cond_le().b(schedule_out);
/* !! FALL THROUGH !! */
}
comment("Peek next message");
a.bind(peek_message);
{
a.ldr(TMP1, arm::Mem(c_p, offsetof(Process, sig_qs.save)));
a.ldr(ARG1, arm::Mem(TMP1));
a.cbnz(ARG1, check_is_distributed);
comment("Inner queue empty, fetch more from outer/middle queues");
emit_enter_runtime<Update::eReductions | Update::eStack |
Update::eHeap>(0);
a.str(ZERO, message_ptr);
a.mov(ARG1, c_p);
a.mov(ARG2, FCALLS);
mov_imm(ARG3, 0);
lea(ARG4, message_ptr);
lea(ARG5, get_out);
#ifdef ERTS_ENABLE_LOCK_CHECK
runtime_call<5>(erts_lc_proc_sig_receive_helper);
#else
runtime_call<5>(erts_proc_sig_receive_helper);
#endif
/* erts_proc_sig_receive_helper merely inspects FCALLS, so we don't
* need to update it here.
*
* Also note that another process may have loaded new code and sent us
* a message to notify us about it, so we must update the active code
* index. */
emit_leave_runtime<Update::eStack | Update::eHeap | Update::eCodeIndex>(
0);
a.sub(FCALLS, FCALLS, ARG1);
/* Need to spill message_ptr to ARG1 as check_is_distributed uses it. */
a.ldr(ARG1, message_ptr);
a.cbnz(ARG1, check_is_distributed);
/* Did we receive a signal or run out of reds? */
a.ldr(TMP1.w(), get_out);
a.cbnz(TMP1, schedule_out);
/* The queue is empty and we're not yielding or exiting, so we'll jump
* to our wait/timeout instruction.
*
* Note that the message queue lock is still held in this case. */
a.ldr(TMP1.w(), flags);
a.and_(TMP1, TMP1, imm(~F_DELAY_GC));
a.str(TMP1.w(), flags);
a.ldr(TMP1, await_addr);
a.br(TMP1);
}
a.bind(schedule_out);
{
/* We either ran out of reductions or received an exit signal; schedule
* ourselves out. The yield address (`c_p->i`) was set on ingress. */
a.ldr(TMP1.w(), flags);
a.and_(TMP1, TMP1, imm(~F_DELAY_GC));
a.str(TMP1.w(), flags);
a.str(ZERO, arm::Mem(c_p, offsetof(Process, arity)));
a.str(ZERO, arm::Mem(c_p, offsetof(Process, current)));
a.b(labels[do_schedule]);
}
/*
* ARG1 now contains the pointer to a message.
*/
comment("Check if message is distributed");
a.bind(check_is_distributed);
{
a.ldr(TMP1, arm::Mem(ARG1, offsetof(ErtsSignal, common.tag)));
emit_branch_if_value(TMP1, done);
sub(FCALLS, FCALLS, 10);
emit_enter_runtime(0);
a.mov(ARG2, ARG1);
a.mov(ARG1, c_p);
runtime_call<2>(beam_jit_decode_dist);
emit_leave_runtime(0);
a.cbz(ARG1, restart);
/* !! FALL THROUGH !! */
}
a.bind(done);
{
a.ldr(XREG0, arm::Mem(ARG1, offsetof(ErtsMessage, m[0])));
a.ret(XREG1);
}
}
void BeamModuleAssembler::emit_i_loop_rec(const ArgVal &Wait) {
Label entry = a.newLabel();
a.bind(entry);
a.adr(ARG1, entry);
a.ldr(ARG2, embed_constant(Wait, disp32K));
fragment_call(ga->get_i_loop_rec_shared());
}
void BeamModuleAssembler::emit_remove_message() {
/* HTOP and E are passed explicitly and only read from, so we don't need to
* swap them out. */
a.mov(ARG3, HTOP);
a.mov(ARG4, E);
emit_enter_runtime();
a.mov(ARG1, c_p);
a.mov(ARG2, FCALLS);
a.mov(ARG5, active_code_ix);
runtime_call<5>(beam_jit_remove_message);
a.mov(FCALLS, ARG1);
emit_leave_runtime();
}
void BeamModuleAssembler::emit_loop_rec_end(const ArgVal &Dest) {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
runtime_call<1>(erts_msgq_set_save_next);
emit_leave_runtime(0);
a.sub(FCALLS, FCALLS, imm(1));
a.b(resolve_beam_label(Dest, disp128MB));
}
void BeamModuleAssembler::emit_wait_unlocked(const ArgVal &Dest) {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
a.ldr(ARG2, embed_constant(Dest, disp32K));
runtime_call<2>(beam_jit_wait_unlocked);
emit_leave_runtime(0);
a.b(resolve_fragment(ga->get_do_schedule(), disp128MB));
}
void BeamModuleAssembler::emit_wait_locked(const ArgVal &Dest) {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
a.ldr(ARG2, embed_constant(Dest, disp32K));
runtime_call<2>(beam_jit_wait_locked);
emit_leave_runtime(0);
a.b(resolve_fragment(ga->get_do_schedule(), disp128MB));
}
void BeamModuleAssembler::emit_wait_timeout_unlocked(const ArgVal &Src,
const ArgVal &Dest) {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
runtime_call<1>(beam_jit_take_receive_lock);
emit_leave_runtime(0);
emit_wait_timeout_locked(Src, Dest);
}
void BeamModuleAssembler::emit_wait_timeout_locked(const ArgVal &Src,
const ArgVal &Dest) {
Label wait = a.newLabel(), next = a.newLabel();
mov_arg(ARG2, Src);
emit_enter_runtime(0);
a.mov(ARG1, c_p);
a.adr(ARG3, next);
runtime_call<3>(beam_jit_wait_timeout);
emit_leave_runtime(0);
ERTS_CT_ASSERT(RET_next < RET_wait && RET_wait < RET_badarg);
a.cmp(ARG1, imm(RET_wait));
a.cond_eq().b(wait);
a.cond_lt().b(next);
emit_raise_exception(currLabel, (ErtsCodeMFA *)nullptr);
a.bind(wait);
emit_wait_locked(Dest);
a.bind(next);
}
void BeamModuleAssembler::emit_timeout_locked() {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
runtime_call<1>(beam_jit_timeout_locked);
emit_leave_runtime(0);
}
void BeamModuleAssembler::emit_timeout() {
emit_enter_runtime(0);
a.mov(ARG1, c_p);
runtime_call<1>(beam_jit_timeout);
emit_leave_runtime(0);
}
|