aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/erl_nfunc_sched.c
blob: 1bebc1eda4284354f0a14ec066deb3315e5dd79a (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
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
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2016. 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%
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#define ERTS_WANT_NFUNC_SCHED_INTERNALS__

#include "global.h"
#include "erl_process.h"
#include "bif.h"
#include "erl_nfunc_sched.h"
#include "erl_trace.h"

NifExport *
erts_new_proc_nif_export(Process *c_p, int argc)
{
    size_t size;
    int i;
    NifExport *nep, *old_nep;

    size = sizeof(NifExport) + (argc-1)*sizeof(Eterm);
    nep = erts_alloc(ERTS_ALC_T_NIF_TRAP_EXPORT, size);

    for (i = 0; i < ERTS_NUM_CODE_IX; i++)
	nep->exp.addressv[i] = &nep->exp.beam[0];

    nep->argc = -1; /* unused marker */
    nep->argv_size = argc;
    nep->trace = NULL;
    old_nep = ERTS_PROC_SET_NIF_TRAP_EXPORT(c_p, nep);
    if (old_nep) {
	ASSERT(!nep->trace);
	erts_free(ERTS_ALC_T_NIF_TRAP_EXPORT, old_nep);
    }
    return nep;
}

void
erts_destroy_nif_export(Process *p)
{
    NifExport *nep = ERTS_PROC_SET_NIF_TRAP_EXPORT(p, NULL);
    if (nep) {
	if (nep->m)
	    erts_nif_export_cleanup_nif_mod(nep);
	erts_free(ERTS_ALC_T_NIF_TRAP_EXPORT, nep);
    }
}

void
erts_nif_export_save_trace(Process *c_p, NifExport *nep, int applying,
			   Export* ep, BeamInstr *cp, Uint32 flags,
			   Uint32 flags_meta, BeamInstr* I,
			   ErtsTracer meta_tracer)
{
    NifExportTrace *netp;
    ASSERT(nep && nep->argc >= 0);
    ASSERT(!nep->trace);
    netp = erts_alloc(ERTS_ALC_T_NIF_EXP_TRACE,
		      sizeof(NifExportTrace));
    netp->applying = applying;
    netp->ep = ep;
    netp->cp = cp;
    netp->flags = flags;
    netp->flags_meta = flags_meta;
    netp->I = I;
    netp->meta_tracer = NIL;
    erts_tracer_update(&netp->meta_tracer, meta_tracer);
    nep->trace = netp;
}

void
erts_nif_export_restore_trace(Process *c_p, Eterm result, NifExport *nep)
{
    NifExportTrace *netp = nep->trace;
    nep->trace = NULL;
    erts_bif_trace_epilogue(c_p, result, netp->applying, netp->ep,
			    netp->cp, netp->flags, netp->flags_meta,
			    netp->I, netp->meta_tracer);
    erts_tracer_update(&netp->meta_tracer, NIL);
    erts_free(ERTS_ALC_T_NIF_EXP_TRACE, netp);
}

NifExport *
erts_nif_export_schedule(Process *c_p, Process *dirty_shadow_proc,
			 ErtsCodeMFA *mfa, BeamInstr *pc,
			 BeamInstr instr,
			 void *dfunc, void *ifunc,
			 Eterm mod, Eterm func,
			 int argc, const Eterm *argv)
{
    Process *used_proc;
    ErtsSchedulerData *esdp;
    Eterm* reg;
    NifExport* nep;
    int i;

    ERTS_SMP_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p)
		       & ERTS_PROC_LOCK_MAIN);

    if (dirty_shadow_proc) {
	esdp = erts_get_scheduler_data();
	ASSERT(esdp && ERTS_SCHEDULER_IS_DIRTY(esdp));

	used_proc = dirty_shadow_proc;
    }
    else {
	esdp = erts_proc_sched_data(c_p);
	ASSERT(esdp && !ERTS_SCHEDULER_IS_DIRTY(esdp));

	used_proc = c_p;
	ERTS_VBUMP_ALL_REDS(c_p);
    }

    reg = esdp->x_reg_array;

    if (mfa)
	nep = erts_get_proc_nif_export(c_p, (int) mfa->arity);
    else {
	/* If no mfa, this is not the first schedule... */
	nep = ERTS_PROC_GET_NIF_TRAP_EXPORT(c_p);
	ASSERT(nep && nep->argc >= 0);
    }

    if (nep->argc < 0) {
	/*
	 * First schedule; save things that might
	 * need to be restored...
	 */
	for (i = 0; i < (int) mfa->arity; i++)
	    nep->argv[i] = reg[i];
	nep->pc = pc;
	nep->cp = c_p->cp;
	nep->mfa = mfa;
	nep->current = c_p->current;
	ASSERT(argc >= 0);
	nep->argc = (int) mfa->arity;
	nep->m = NULL;

	ASSERT(!erts_check_nif_export_in_area(c_p,
					      (char *) nep,
					      (sizeof(NifExport)
					       + (sizeof(Eterm)
						  *(nep->argc-1)))));
    }
    /* Copy new arguments into register array if necessary... */
    if (reg != argv) {
	for (i = 0; i < argc; i++)
	    reg[i] = argv[i];
    }
    ASSERT(is_atom(mod) && is_atom(func));
    nep->exp.info.mfa.module = mod;
    nep->exp.info.mfa.function = func;
    nep->exp.info.mfa.arity = (Uint) argc;
    nep->exp.beam[0] = (BeamInstr) instr; /* call_nif || apply_bif */
    nep->exp.beam[1] = (BeamInstr) dfunc;
    nep->func = ifunc;
    used_proc->arity = argc;
    used_proc->freason = TRAP;
    used_proc->i = (BeamInstr*) nep->exp.addressv[0];
    return nep;
}