summaryrefslogtreecommitdiff
path: root/erts/emulator/beam/jit/x86/instr_trace.cpp
blob: 02f76300f0f73df4cd990727372c0f7febe80517 (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
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
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2020-2021. 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 "beam_common.h"
#include "erl_bif_table.h"
#include "beam_bp.h"
};

/* This function is jumped to from the export entry of a function.
 *
 * RET = export entry */
void BeamGlobalAssembler::emit_generic_bp_global() {
    emit_enter_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    a.lea(ARG2, x86::qword_ptr(RET, offsetof(Export, info)));
    load_x_reg_array(ARG3);
    runtime_call<3>(erts_generic_breakpoint);

    emit_leave_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    a.jmp(RET);
}

/* This function is called from the module header, which is in turn called from
 * the prologue of the traced function. As such, the real return address is at
 * RSP+8.
 *
 * See beam_asm.h about more details */
void BeamGlobalAssembler::emit_generic_bp_local() {
    emit_assert_erlang_stack();

#ifdef NATIVE_ERLANG_STACK
    /* Since we've entered here on the Erlang stack, we need to stash our return
     * addresses in case `erts_generic_breakpoint` pushes any trace frames. */
    a.pop(TMP_MEM2q);
    a.pop(ARG2);
#else
    a.mov(ARG2, x86::qword_ptr(x86::rsp, 8));
#endif

    a.mov(TMP_MEM1q, ARG2);

    /* Our actual return address is valid (and word-aligned), but it points just
     * after the trampoline word so we'll need to skip that to find our
     * ErtsCodeInfo. */
    a.sub(ARG2, imm(sizeof(UWord) + sizeof(ErtsCodeInfo)));

#ifdef DEBUG
    {
        Label next = a.newLabel();

        /* Crash if our return address isn't word-aligned. */
        a.test(ARG2, imm(sizeof(UWord) - 1));
        a.je(next);

        a.hlt();

        a.bind(next);
    }
#endif

    emit_enter_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    /* ARG2 is already set above */
    load_x_reg_array(ARG3);
    runtime_call<3>(erts_generic_breakpoint);

    emit_leave_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

#ifdef NATIVE_ERLANG_STACK
    a.push(TMP_MEM1q);
    a.push(TMP_MEM2q);
#endif

    a.cmp(RET, imm(BeamOpCodeAddr(op_i_debug_breakpoint)));
    a.je(labels[debug_bp]);

    a.ret();
}

/* This function is called from the module header which is called from the
 * prologue of the function to trace. See beam_asm.h about more details
 *
 * The only place that we can come to here is from generic_bp_local */
void BeamGlobalAssembler::emit_debug_bp() {
    Label error = a.newLabel();

    emit_assert_erlang_stack();

    emit_enter_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    /* Read and adjust the return address we saved in generic_bp_local. */
    a.mov(ARG2, TMP_MEM1q);
    a.sub(ARG2, imm(sizeof(UWord)));

    a.mov(ARG1, c_p);
    a.lea(ARG2, x86::qword_ptr(ARG2, -(int)sizeof(ErtsCodeMFA)));
    load_x_reg_array(ARG3);
    a.mov(ARG4, imm(am_breakpoint));
    runtime_call<4>(call_error_handler);

    emit_leave_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    /* Skip two frames so we can make a direct jump to the error handler. This
     * makes it so that if we are to do a call_nif_early, we skip that and call
     * the error handler's code instead, mirroring the way the interpreter
     * works. */
    emit_discard_cp();
    emit_discard_cp();

    a.test(RET, RET);
    a.je(error);

    a.jmp(emit_setup_export_call(RET));

    a.bind(error);
    {
        a.mov(ARG2, TMP_MEM1q);
        a.jmp(labels[handle_error_shared]);
    }
}

static void return_trace(Process *c_p,
                         ErtsCodeMFA *mfa,
                         Eterm val,
                         ErtsTracer *tracer) {
    ERTS_UNREQ_PROC_MAIN_LOCK(c_p);
    erts_trace_return(c_p, mfa, val, tracer);
    ERTS_REQ_PROC_MAIN_LOCK(c_p);
}

void BeamModuleAssembler::emit_return_trace() {
    a.mov(ARG2, getYRef(0));
    a.mov(ARG3, getXRef(0));
    a.lea(ARG4, getYRef(1));

    emit_enter_runtime<Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    runtime_call<4>(return_trace);

    emit_leave_runtime<Update::eStack | Update::eHeap>();

    emit_deallocate(ArgVal(ArgVal::u, 2));
    emit_return();
}

void BeamModuleAssembler::emit_i_return_time_trace() {
    /* Pass prev_info if present (is a CP), otherwise null. */
    a.mov(ARG2, getYRef(0));
    mov_imm(ARG3, 0);

    a.test(ARG2, imm(_CPMASK));
    a.lea(ARG2, x86::qword_ptr(ARG2, -(Sint)sizeof(ErtsCodeInfo)));
    a.cmovnz(ARG2, ARG3);

    emit_enter_runtime<Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    runtime_call<2>(erts_trace_time_return);

    emit_leave_runtime<Update::eStack | Update::eHeap>();

    emit_deallocate(ArgVal(ArgVal::u, 1));
    emit_return();
}

static void i_return_to_trace(Process *c_p) {
    if (IS_TRACED_FL(c_p, F_TRACE_RETURN_TO)) {
        Uint *cpp = (Uint *)c_p->stop;
        while (is_not_CP(*cpp)) {
            cpp++;
        }
        for (;;) {
            ErtsCodePtr w = cp_val(*cpp);
            if (BeamIsReturnTrace(w)) {
                do
                    ++cpp;
                while (is_not_CP(*cpp));
                cpp += 2;
            } else if (BeamIsReturnToTrace(w)) {
                do
                    ++cpp;
                while (is_not_CP(*cpp));
            } else {
                break;
            }
        }
        ERTS_UNREQ_PROC_MAIN_LOCK(c_p);
        erts_trace_return_to(c_p, cp_val(*cpp));
        ERTS_REQ_PROC_MAIN_LOCK(c_p);
    }
}

void BeamModuleAssembler::emit_i_return_to_trace() {
    emit_enter_runtime<Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    runtime_call<1>(i_return_to_trace);

    emit_leave_runtime<Update::eStack | Update::eHeap>();

    /* Remove the zero-sized stack frame. (Will actually do nothing if
     * the native stack is used.) */
    emit_deallocate(ArgVal(ArgVal::u, 0));

    emit_return();
}

void BeamModuleAssembler::emit_i_hibernate() {
    Label error = a.newLabel();

    emit_enter_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    a.mov(ARG1, c_p);
    load_x_reg_array(ARG2);
    runtime_call<2>(erts_hibernate);

    emit_leave_runtime<Update::eReductions | Update::eStack | Update::eHeap>();

    a.test(RET, RET);
    a.je(error);

    a.mov(ARG1, x86::qword_ptr(c_p, offsetof(Process, flags)));
    a.and_(ARG1, imm(~F_HIBERNATE_SCHED));
    a.mov(x86::qword_ptr(c_p, offsetof(Process, flags)), ARG1);
    abs_jmp(ga->get_do_schedule());

    a.bind(error);
    emit_handle_error(&BIF_TRAP_EXPORT(BIF_hibernate_3)->info.mfa);
}