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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2013. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
#ifndef __BIF_H__
#define __BIF_H__
extern Export* erts_format_cpu_topology_trap;
#define BIF_RETTYPE Eterm
#define BIF_P A__p
#define BIF_ALIST_0 Process* A__p, Eterm* BIF__ARGS
#define BIF_ALIST_1 Process* A__p, Eterm* BIF__ARGS
#define BIF_ALIST_2 Process* A__p, Eterm* BIF__ARGS
#define BIF_ALIST_3 Process* A__p, Eterm* BIF__ARGS
#define BIF_ARG_1 (BIF__ARGS[0])
#define BIF_ARG_2 (BIF__ARGS[1])
#define BIF_ARG_3 (BIF__ARGS[2])
#define ERTS_IS_PROC_OUT_OF_REDS(p) \
((p)->fcalls > 0 \
? 0 \
: (!ERTS_PROC_GET_SAVED_CALLS_BUF((p)) \
? (p)->fcalls == 0 \
: ((p)->fcalls == -CONTEXT_REDS)))
#define BUMP_ALL_REDS(p) do { \
if (!ERTS_PROC_GET_SAVED_CALLS_BUF((p))) \
(p)->fcalls = 0; \
else \
(p)->fcalls = -CONTEXT_REDS; \
} while(0)
#define ERTS_VBUMP_ALL_REDS(p) \
do { \
if (!ERTS_PROC_GET_SAVED_CALLS_BUF((p))) { \
if ((p)->fcalls > 0) \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds += (p)->fcalls; \
(p)->fcalls = 0; \
} \
else { \
if ((p)->fcalls > -CONTEXT_REDS) \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds \
+= ((p)->fcalls - (-CONTEXT_REDS)); \
(p)->fcalls = -CONTEXT_REDS; \
} \
} while(0)
#define BUMP_REDS(p, gc) do { \
ASSERT(p); \
ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN & erts_proc_lc_my_proc_locks(p));\
(p)->fcalls -= (gc); \
if ((p)->fcalls < 0) { \
if (!ERTS_PROC_GET_SAVED_CALLS_BUF((p))) \
(p)->fcalls = 0; \
else if ((p)->fcalls < -CONTEXT_REDS) \
(p)->fcalls = -CONTEXT_REDS; \
} \
} while(0)
#define ERTS_VBUMP_REDS(p, reds) \
do { \
if (!ERTS_PROC_GET_SAVED_CALLS_BUF((p))) { \
if ((p)->fcalls >= reds) { \
(p)->fcalls -= reds; \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds += reds; \
} \
else { \
if ((p)->fcalls > 0) \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds += (p)->fcalls;\
(p)->fcalls = 0; \
} \
} \
else { \
if ((p)->fcalls >= reds - CONTEXT_REDS) { \
(p)->fcalls -= reds; \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds += reds; \
} \
else { \
if ((p)->fcalls > -CONTEXT_REDS) \
ERTS_PROC_GET_SCHDATA((p))->virtual_reds \
+= (p)->fcalls - (-CONTEXT_REDS); \
(p)->fcalls = -CONTEXT_REDS; \
} \
} \
} while(0)
#define ERTS_BIF_REDS_LEFT(p) \
(ERTS_PROC_GET_SAVED_CALLS_BUF((p)) \
? ((p)->fcalls > -CONTEXT_REDS ? ((p)->fcalls - (-CONTEXT_REDS)) : 0)\
: ((p)->fcalls > 0 ? (p)->fcalls : 0))
#define BIF_RET2(x, gc) do { \
BUMP_REDS(BIF_P, (gc)); \
return (x); \
} while(0)
#define BIF_RET(x) return (x)
#define ERTS_BIF_PREP_RET(Ret, Val) ((Ret) = (Val))
#define BIF_ERROR(p,r) do { \
(p)->freason = r; \
return THE_NON_VALUE; \
} while(0)
#define ERTS_BIF_ERROR_TRAPPED0(Proc, Reason, Bif) \
do { \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_ERROR_TRAPPED1(Proc, Reason, Bif, A0) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_ERROR_TRAPPED2(Proc, Reason, Bif, A0, A1) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_ERROR_TRAPPED3(Proc, Reason, Bif, A0, A1, A2) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
reg[2] = (Eterm) (A2); \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_ERROR(Ret, Proc, Reason) \
do { \
(Proc)->freason = (Reason); \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_ERROR_TRAPPED0(Ret, Proc, Reason, Bif) \
do { \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_ERROR_TRAPPED1(Ret, Proc, Reason, Bif, A0) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_ERROR_TRAPPED2(Ret, Proc, Reason, Bif, A0, A1) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_ERROR_TRAPPED3(Ret, Proc, Reason, Bif, A0, A1, A2) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->freason = (Reason); \
(Proc)->current = (Bif)->code; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
reg[2] = (Eterm) (A2); \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_TRAP0(Ret, Trap, Proc) \
do { \
(Proc)->arity = 0; \
(Proc)->i = (BeamInstr*) ((Trap)->addressv[erts_active_code_ix()]); \
(Proc)->freason = TRAP; \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_TRAP1(Ret, Trap, Proc, A0) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->arity = 1; \
reg[0] = (Eterm) (A0); \
(Proc)->i = (BeamInstr*) ((Trap)->addressv[erts_active_code_ix()]); \
(Proc)->freason = TRAP; \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_TRAP2(Ret, Trap, Proc, A0, A1) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->arity = 2; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
(Proc)->i = (BeamInstr*) ((Trap)->addressv[erts_active_code_ix()]); \
(Proc)->freason = TRAP; \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_TRAP3(Ret, Trap, Proc, A0, A1, A2) \
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->arity = 3; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
reg[2] = (Eterm) (A2); \
(Proc)->i = (BeamInstr*) ((Trap)->addressv[erts_active_code_ix()]); \
(Proc)->freason = TRAP; \
(Ret) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_TRAP3_NO_RET(Trap, Proc, A0, A1, A2)\
do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((Proc))->x_reg_array; \
(Proc)->arity = 3; \
reg[0] = (Eterm) (A0); \
reg[1] = (Eterm) (A1); \
reg[2] = (Eterm) (A2); \
(Proc)->i = (BeamInstr*) ((Trap)->addressv[erts_active_code_ix()]); \
(Proc)->freason = TRAP; \
} while (0)
#define BIF_TRAP0(p, Trap_) do { \
(p)->arity = 0; \
(p)->i = (BeamInstr*) ((Trap_)->addressv[erts_active_code_ix()]); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
#define BIF_TRAP1(Trap_, p, A0) do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((p))->x_reg_array; \
(p)->arity = 1; \
reg[0] = (A0); \
(p)->i = (BeamInstr*) ((Trap_)->addressv[erts_active_code_ix()]); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
#define BIF_TRAP2(Trap_, p, A0, A1) do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((p))->x_reg_array; \
(p)->arity = 2; \
reg[0] = (A0); \
reg[1] = (A1); \
(p)->i = (BeamInstr*) ((Trap_)->addressv[erts_active_code_ix()]); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
#define BIF_TRAP3(Trap_, p, A0, A1, A2) do { \
Eterm* reg = ERTS_PROC_GET_SCHDATA((p))->x_reg_array; \
(p)->arity = 3; \
reg[0] = (A0); \
reg[1] = (A1); \
reg[2] = (A2); \
(p)->i = (BeamInstr*) ((Trap_)->addressv[erts_active_code_ix()]); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
#define BIF_TRAP_CODE_PTR_0(p, Code_) do { \
(p)->arity = 0; \
(p)->i = (BeamInstr*) (Code_); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
#define BIF_TRAP_CODE_PTR_(p, Code_) do { \
(p)-> i = (BeamInstr*) (Code_); \
(p)->freason = TRAP; \
return THE_NON_VALUE; \
} while(0)
extern Export bif_return_trap_export;
#ifdef DEBUG
#define ERTS_BIF_PREP_YIELD_RETURN_X(RET, P, VAL, DEBUG_VAL) \
do { \
ERTS_VBUMP_ALL_REDS(P); \
ERTS_BIF_PREP_TRAP2(RET, &bif_return_trap_export, (P), (VAL), \
(DEBUG_VAL)); \
} while (0)
#else
#define ERTS_BIF_PREP_YIELD_RETURN_X(RET, P, VAL, DEBUG_VAL) \
do { \
ERTS_VBUMP_ALL_REDS(P); \
ERTS_BIF_PREP_TRAP1(RET, &bif_return_trap_export, (P), (VAL)); \
} while (0)
#endif
#define ERTS_BIF_PREP_YIELD_RETURN(RET, P, VAL) \
ERTS_BIF_PREP_YIELD_RETURN_X(RET, (P), (VAL), am_undefined)
#ifdef DEBUG
#define ERTS_BIF_YIELD_RETURN_X(P, VAL, DEBUG_VAL) \
do { \
ERTS_VBUMP_ALL_REDS(P); \
BIF_TRAP2(&bif_return_trap_export, (P), (VAL), (DEBUG_VAL)); \
} while (0)
#else
#define ERTS_BIF_YIELD_RETURN_X(P, VAL, DEBUG_VAL) \
do { \
ERTS_VBUMP_ALL_REDS(P); \
BIF_TRAP1(&bif_return_trap_export, (P), (VAL)); \
} while (0)
#endif
#define ERTS_BIF_RETURN_YIELD(P) ERTS_VBUMP_ALL_REDS((P))
#define ERTS_BIF_YIELD_RETURN(P, VAL) \
ERTS_BIF_YIELD_RETURN_X((P), (VAL), am_undefined)
#define ERTS_BIF_PREP_YIELD0(RET, TRP, P) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
ERTS_BIF_PREP_TRAP0(RET, (TRP), (P)); \
} while (0)
#define ERTS_BIF_PREP_YIELD1(RET, TRP, P, A0) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
ERTS_BIF_PREP_TRAP1(RET, (TRP), (P), (A0)); \
} while (0)
#define ERTS_BIF_PREP_YIELD2(RET, TRP, P, A0, A1) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
ERTS_BIF_PREP_TRAP2(RET, (TRP), (P), (A0), (A1)); \
} while (0)
#define ERTS_BIF_PREP_YIELD3(RET, TRP, P, A0, A1, A2) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
ERTS_BIF_PREP_TRAP3(RET, (TRP), (P), (A0), (A1), (A2)); \
} while (0)
#define ERTS_BIF_YIELD0(TRP, P) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
BIF_TRAP0((TRP), (P)); \
} while (0)
#define ERTS_BIF_YIELD1(TRP, P, A0) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
BIF_TRAP1((TRP), (P), (A0)); \
} while (0)
#define ERTS_BIF_YIELD2(TRP, P, A0, A1) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
BIF_TRAP2((TRP), (P), (A0), (A1)); \
} while (0)
#define ERTS_BIF_YIELD3(TRP, P, A0, A1, A2) \
do { \
ERTS_VBUMP_ALL_REDS((P)); \
BIF_TRAP3((TRP), (P), (A0), (A1), (A2)); \
} while (0)
#define ERTS_BIF_EXITED(PROC) \
do { \
KILL_CATCHES((PROC)); \
BIF_ERROR((PROC), EXC_EXIT); \
} while (0)
#define ERTS_BIF_CHK_EXITED(PROC) \
do { \
if (ERTS_PROC_IS_EXITING((PROC))) \
ERTS_BIF_EXITED((PROC)); \
} while (0)
/*
* The ERTS_BIF_*_AWAIT_X_*_TRAP makros either exits the caller, or
* sets up a trap to erlang:await_proc_exit/3.
*
* The caller is acquired to hold the 'main' lock on C_P. No other locks
* are allowed to be held.
*/
#define ERTS_BIF_PREP_AWAIT_X_DATA_TRAP(RET, C_P, PID, DATA) \
do { \
erts_bif_prep_await_proc_exit_data_trap((C_P), (PID), (DATA)); \
(RET) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_AWAIT_X_REASON_TRAP(RET, C_P, PID) \
do { \
erts_bif_prep_await_proc_exit_reason_trap((C_P), (PID)); \
(RET) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_PREP_AWAIT_X_APPLY_TRAP(RET, C_P, PID, M, F, A, AN) \
do { \
erts_bif_prep_await_proc_exit_apply_trap((C_P), (PID), \
(M), (F), (A), (AN)); \
(RET) = THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_AWAIT_X_DATA_TRAP(C_P, PID, DATA) \
do { \
erts_bif_prep_await_proc_exit_data_trap((C_P), (PID), (DATA)); \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_AWAIT_X_REASON_TRAP(C_P, PID) \
do { \
erts_bif_prep_await_proc_exit_reason_trap((C_P), (PID)); \
return THE_NON_VALUE; \
} while (0)
#define ERTS_BIF_AWAIT_X_APPLY_TRAP(C_P, PID, M, F, A, AN) \
do { \
erts_bif_prep_await_proc_exit_apply_trap((C_P), (PID), \
(M), (F), (A), (AN)); \
return THE_NON_VALUE; \
} while (0)
void
erts_bif_prep_await_proc_exit_data_trap(Process *c_p,
Eterm pid,
Eterm data);
void
erts_bif_prep_await_proc_exit_reason_trap(Process *c_p,
Eterm pid);
void
erts_bif_prep_await_proc_exit_apply_trap(Process *c_p,
Eterm pid,
Eterm module,
Eterm function,
Eterm args[],
int nargs);
#ifndef HIPE
#define HIPE_WRAPPER_BIF_DISABLE_GC(BIF_NAME, ARITY)
#else
#include "erl_fun.h"
#include "hipe_mode_switch.h"
/*
* Hipe wrappers used by native code for BIFs that disable GC while trapping.
* Also add usage of the wrapper in ../hipe/hipe_bif_list.m4
*
* Problem:
* When native code calls a BIF that traps, hipe_mode_switch will push a
* "trap frame" on the Erlang stack in order to find its way back from beam_emu
* back to native caller when finally done. If GC is disabled and stack/heap
* is full there is no place to push the "trap frame".
*
* Solution:
* We reserve space on stack for the "trap frame" here before the BIF is called.
* If the BIF does not trap, the space is reclaimed here before returning.
* If the BIF traps, hipe_push_beam_trap_frame() will detect that a "trap frame"
* already is reserved and use it.
*/
#define HIPE_WRAPPER_BIF_DISABLE_GC(BIF_NAME, ARITY) \
BIF_RETTYPE hipe_wrapper_ ## BIF_NAME ## _ ## ARITY (Process* c_p, \
Eterm* args); \
BIF_RETTYPE hipe_wrapper_ ## BIF_NAME ## _ ## ARITY (Process* c_p, \
Eterm* args) \
{ \
BIF_RETTYPE res; \
hipe_reserve_beam_trap_frame(c_p, args, ARITY); \
res = BIF_NAME ## _ ## ARITY (c_p, args); \
if (is_value(res) || c_p->freason != TRAP) { \
hipe_unreserve_beam_trap_frame(c_p); \
} \
return res; \
}
#endif
#include "erl_bif_table.h"
#endif
|