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
|
/*
* %CopyrightBegin%
*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright Ericsson AB 2016-2025. 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%
*/
#ifndef ERL_NFUNC_SCHED_H__
#define ERL_NFUNC_SCHED_H__
#include "erl_process.h"
#include "bif.h"
#include "error.h"
#include "jit/beam_asm.h"
/*
* Native function wrappers are used to schedule native functions on both
* normal and dirty schedulers.
*
* A number of values are only stored for error handling, and the fields
* following `current` can be omitted when a wrapper is statically "scheduled"
* through placement in a function stub.
*
* 'argc' is >= 0 when ErtsNativeFunc is in use, and < 0 when not.
*/
typedef struct {
struct {
ErtsCodeInfo info;
#ifdef BEAMASM
char call_bif_nif[BEAM_ASM_NFUNC_SIZE];
#else
BeamInstr call_bif_nif; /* call_bif || call_nif */
#endif
BeamInstr dfunc;
} trampoline;
struct erl_module_nif* m; /* NIF module, or NULL if BIF */
void *func; /* Indirect NIF or BIF to execute (may be unused) */
const ErtsCodeMFA *current;/* Current as set when originally called */
/* --- The following is only used on error --- */
ErtsCodePtr pc; /* Program counter */
const ErtsCodeMFA *mfa; /* MFA of original call */
int argc; /* Number of arguments in original call */
int argv_size; /* Allocated size of argv */
Eterm argv[1]; /* Saved arguments from the original call */
} ErtsNativeFunc;
ErtsNativeFunc *erts_new_proc_nfunc(Process *c_p, int argc);
void erts_destroy_nfunc(Process *p);
ErtsNativeFunc *erts_nfunc_schedule(Process *c_p, Process *dirty_shadow_proc,
const ErtsCodeMFA *mfa, ErtsCodePtr pc,
BeamInstr instr,
void *dfunc, void *ifunc,
Eterm mod, Eterm func,
int argc, const Eterm *argv);
void erts_nfunc_cleanup_nif_mod(ErtsNativeFunc *ep); /* erl_nif.c */
ERTS_GLB_INLINE ErtsNativeFunc *erts_get_proc_nfunc(Process *c_p, int extra);
ERTS_GLB_INLINE int erts_setup_nfunc_rootset(Process* proc, Eterm** objv,
Uint* nobj);
ERTS_GLB_INLINE int erts_check_nfunc_in_area(Process *p,
char *start, Uint size);
ERTS_GLB_INLINE void erts_nfunc_restore(Process *c_p, ErtsNativeFunc *ep,
Eterm result);
ERTS_GLB_INLINE void erts_nfunc_restore_error(Process* c_p,
ErtsCodePtr *pc,
Eterm *reg,
const ErtsCodeMFA **nif_mfa);
ERTS_GLB_INLINE Process *erts_proc_shadow2real(Process *c_p);
#if ERTS_GLB_INLINE_INCL_FUNC_DEF
ERTS_GLB_INLINE ErtsNativeFunc *
erts_get_proc_nfunc(Process *c_p, int argc)
{
ErtsNativeFunc *nep = ERTS_PROC_GET_NFUNC_TRAP_WRAPPER(c_p);
if (!nep || (nep->argc < 0 && nep->argv_size < argc))
return erts_new_proc_nfunc(c_p, argc);
return nep;
}
/*
* If a process has saved arguments, they need to be part of the GC
* rootset. The function below is called from setup_rootset() in
* erl_gc.c. Any exception term saved in the ErtsNativeFunc is also made
* part of the GC rootset here; it always resides in rootset[0].
*/
ERTS_GLB_INLINE int
erts_setup_nfunc_rootset(Process* proc, Eterm** objv, Uint* nobj)
{
ErtsNativeFunc* ep = (ErtsNativeFunc*) ERTS_PROC_GET_NFUNC_TRAP_WRAPPER(proc);
if (!ep || ep->argc <= 0)
return 0;
*objv = ep->argv;
*nobj = ep->argc;
return 1;
}
/*
* Check if native func wrapper points into code area...
*/
ERTS_GLB_INLINE int
erts_check_nfunc_in_area(Process *p, char *start, Uint size)
{
ErtsNativeFunc *nep = ERTS_PROC_GET_NFUNC_TRAP_WRAPPER(p);
if (!nep || nep->argc < 0)
return 0;
if (ErtsInArea(nep->pc, start, size))
return 1;
if (ErtsInArea(nep->mfa, start, size))
return 1;
if (ErtsInArea(nep->current, start, size))
return 1;
return 0;
}
ERTS_GLB_INLINE void
erts_nfunc_restore(Process *c_p, ErtsNativeFunc *ep, Eterm result)
{
ASSERT(!ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
ERTS_LC_ASSERT(!(c_p->static_flags
& ERTS_STC_FLG_SHADOW_PROC));
ERTS_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p)
& ERTS_PROC_LOCK_MAIN);
c_p->current = ep->current;
ep->argc = -1; /* Unused nif-export marker... */
}
ERTS_GLB_INLINE void
erts_nfunc_restore_error(Process* c_p, ErtsCodePtr *pc,
Eterm *reg, const ErtsCodeMFA **nif_mfa)
{
ErtsNativeFunc *nep = (ErtsNativeFunc *) ERTS_PROC_GET_NFUNC_TRAP_WRAPPER(c_p);
int ix;
ASSERT(nep);
*pc = nep->pc;
*nif_mfa = nep->mfa;
for (ix = 0; ix < nep->argc; ix++)
reg[ix] = nep->argv[ix];
erts_nfunc_restore(c_p, nep, THE_NON_VALUE);
}
ERTS_GLB_INLINE Process *
erts_proc_shadow2real(Process *c_p)
{
if (c_p->static_flags & ERTS_STC_FLG_SHADOW_PROC) {
Process *real_c_p = c_p->next;
ASSERT(ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
ASSERT(real_c_p->common.id == c_p->common.id);
return real_c_p;
}
ASSERT(!ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
return c_p;
}
#endif /* ERTS_GLB_INLINE_INCL_FUNC_DEF */
#endif /* ERL_NFUNC_SCHED_H__ */
#if defined(ERTS_WANT_NFUNC_SCHED_INTERNALS__) && !defined(ERTS_NFUNC_SCHED_INTERNALS__)
#define ERTS_NFUNC_SCHED_INTERNALS__
#define ERTS_I_BEAM_OP_TO_NFUNC(I) \
(ASSERT(BeamIsOpCode(*(const BeamInstr*)(I), op_call_bif_W) || \
BeamIsOpCode(*(const BeamInstr*)(I), op_call_nif_WWW)), \
((ErtsNativeFunc *) (((char *) (I)) - \
offsetof(ErtsNativeFunc, trampoline.call_bif_nif))))
#include "erl_message.h"
#include <stddef.h>
ERTS_GLB_INLINE void erts_flush_dirty_shadow_proc(Process *sproc);
ERTS_GLB_INLINE void erts_cache_dirty_shadow_proc(Process *sproc);
ERTS_GLB_INLINE Process *erts_make_dirty_shadow_proc(ErtsSchedulerData *esdp,
Process *c_p);
#if ERTS_GLB_INLINE_INCL_FUNC_DEF
ERTS_GLB_INLINE void
erts_flush_dirty_shadow_proc(Process *sproc)
{
Process *c_p = sproc->next;
ASSERT(sproc->common.id == c_p->common.id);
ERTS_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p)
& ERTS_PROC_LOCK_MAIN);
ASSERT(c_p->stop == sproc->stop);
ASSERT(c_p->hend == sproc->hend);
ASSERT(c_p->heap == sproc->heap);
ASSERT(c_p->abandoned_heap == sproc->abandoned_heap);
ASSERT(c_p->heap_sz == sproc->heap_sz);
ASSERT(c_p->high_water == sproc->high_water);
ASSERT(c_p->old_heap == sproc->old_heap);
ASSERT(c_p->old_htop == sproc->old_htop);
ASSERT(c_p->old_hend == sproc->old_hend);
ASSERT(c_p->htop <= sproc->htop && sproc->htop <= c_p->stop);
c_p->htop = sproc->htop;
if (!c_p->mbuf)
c_p->mbuf = sproc->mbuf;
else if (sproc->mbuf) {
ErlHeapFragment *bp;
for (bp = sproc->mbuf; bp->next; bp = bp->next)
ASSERT(!bp->off_heap.first);
bp->next = c_p->mbuf;
c_p->mbuf = sproc->mbuf;
}
c_p->mbuf_sz += sproc->mbuf_sz;
if (!c_p->off_heap.first)
c_p->off_heap.first = sproc->off_heap.first;
else if (sproc->off_heap.first) {
struct erl_off_heap_header *ohhp;
for (ohhp = sproc->off_heap.first; ohhp->next; ohhp = ohhp->next)
;
ohhp->next = c_p->off_heap.first;
c_p->off_heap.first = sproc->off_heap.first;
}
c_p->off_heap.overhead += sproc->off_heap.overhead;
ASSERT(sproc->wrt_bins == NULL);
}
ERTS_GLB_INLINE void
erts_cache_dirty_shadow_proc(Process *sproc)
{
Process *c_p = sproc->next;
ASSERT(c_p);
ASSERT(sproc->common.id == c_p->common.id);
ERTS_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p)
& ERTS_PROC_LOCK_MAIN);
sproc->htop = c_p->htop;
sproc->stop = c_p->stop;
sproc->hend = c_p->hend;
sproc->heap = c_p->heap;
sproc->abandoned_heap = c_p->abandoned_heap;
sproc->heap_sz = c_p->heap_sz;
sproc->high_water = c_p->high_water;
sproc->old_hend = c_p->old_hend;
sproc->old_htop = c_p->old_htop;
sproc->old_heap = c_p->old_heap;
sproc->mbuf = NULL;
sproc->mbuf_sz = 0;
ERTS_INIT_OFF_HEAP(&sproc->off_heap);
sproc->wrt_bins = NULL;
}
ERTS_GLB_INLINE Process *
erts_make_dirty_shadow_proc(ErtsSchedulerData *esdp, Process *c_p)
{
Process *sproc;
ASSERT(ERTS_SCHEDULER_IS_DIRTY(esdp));
sproc = esdp->dirty_shadow_process;
ASSERT(sproc);
ASSERT(sproc->static_flags & ERTS_STC_FLG_SHADOW_PROC);
ASSERT(erts_atomic32_read_nob(&sproc->state)
== (ERTS_PSFLG_ACTIVE
| ERTS_PSFLG_DIRTY_RUNNING
| ERTS_PSFLG_PROXY));
sproc->next = c_p;
sproc->common.id = c_p->common.id;
erts_cache_dirty_shadow_proc(sproc);
return sproc;
}
#endif /* ERTS_GLB_INLINE_INCL_FUNC_DEF */
#endif /* defined(ERTS_WANT_NFUNC_SCHED_INTERNALS__) && !defined(ERTS_NFUNC_SCHED_INTERNALS__) */
|