Skip to content

Commit cd10a70

Browse files
authored
Async exceptions implementation for arm64 (#2450)
* Async exns for arm64: runtime5 * Async exns for arm64: runtime4 Based on: commit b24a074 Author: Anmol Sahoo <[email protected]> Date: Wed Jul 26 00:31:49 2023 -0400 Fixed async exceptions on arm64 The following tests - tests/lib-systhreads/eintr.ml and tests/async-exns/async_exns_1 were failing. This patch loads the trap ptr into Caml_state->async_exception_pointer in arm64.S which fixes the test cases.
1 parent d15c624 commit cd10a70

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

ocaml/runtime/arm64.S

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ G(name):
203203
#define Cstack_sp(reg) [reg, #8]
204204
#define Cstack_sp_offset 8
205205
#define Cstack_prev(reg) [reg, #16]
206+
#define Cstack_async_exn_handler(reg) [reg, #24]
206207

207208
/* struct stack_handler */
208209
#define Handler_value(reg) [reg]
@@ -352,7 +353,7 @@ FUNCTION(caml_call_realloc_stack)
352353
ldp x29, x30, [sp], 16
353354
add sp, sp, 16 /* pop argument */
354355
ADDRGLOBAL(x0, caml_exn_Stack_overflow)
355-
b G(caml_raise_exn)
356+
b G(caml_raise_async)
356357
CFI_ENDPROC
357358
END_FUNCTION(caml_call_realloc_stack)
358359

@@ -543,7 +544,9 @@ L(jump_to_caml):
543544
ldr ALLOC_PTR, Caml_state(young_ptr)
544545
/* Build (16-byte aligned) struct c_stack_link on the C stack */
545546
ldr x8, Caml_state(c_stack)
546-
stp x8, xzr, [sp, -16]! /* C_stack_prev, pad */
547+
ldr x9, Caml_state(async_exn_handler)
548+
stp x8, x9, [sp, -16]!
549+
/* C_stack_prev, C_stack_async_exn_handler */
547550
CFI_ADJUST(16)
548551
stp xzr, xzr, [sp, -16]! /* C_stack_stack, C_stack_sp */
549552
CFI_ADJUST(16)
@@ -563,6 +566,10 @@ L(jump_to_caml):
563566
adr x10, L(trap_handler)
564567
stp x9, x10, [x8, -16]!
565568
mov TRAP_PTR, x8
569+
/* Note that the async exception handler chain always goes through
570+
Caml_state, unlike the normal exception handler chain, which goes
571+
through the TRAP_PTR register. */
572+
str x8, Caml_state(async_exn_handler)
566573
/* Switch stacks and call the OCaml code */
567574
mov sp, x8
568575
#ifdef ASM_CFI_SUPPORTED
@@ -598,6 +605,9 @@ L(return_result):
598605
ldr x9, Caml_state(c_stack)
599606
mov sp, x9
600607
CFI_RESTORE_STATE
608+
/* Restore saved async exception pointer */
609+
ldr x8, Cstack_async_exn_handler(sp)
610+
str x8, Caml_state(async_exn_handler)
601611
/* Pop the struct c_stack_link */
602612
ldr x8, Cstack_prev(sp)
603613
add sp, sp, 32

ocaml/runtime4/arm64.S

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ FUNCTION(caml_start_program)
401401
/* Address of OCaml code to call is in TMP2 */
402402
/* Arguments to the OCaml code are in x0...x7 */
403403

404-
/* CR mshinwell: asynchronous exceptions are not yet implemented here */
405-
406404
L(jump_to_caml):
407405
/* Set up stack frame and save callee-save registers */
408406
CFI_OFFSET(29, -160)
@@ -425,15 +423,19 @@ L(jump_to_caml):
425423
ldr x8, Caml_state(bottom_of_stack)
426424
ldr x9, Caml_state(last_return_address)
427425
ldr x10, Caml_state(gc_regs)
426+
ldr x11, Caml_state(async_exception_pointer)
428427
stp x8, x9, [sp, -32]! /* 16-byte alignment */
429428
CFI_ADJUST(32)
430429
str x10, [sp, 16]
430+
str x11, [sp, 24]
431431
/* Setup a trap frame to catch exceptions escaping the OCaml code */
432432
ldr x8, Caml_state(exn_handler)
433433
adr x9, L(trap_handler)
434434
stp x8, x9, [sp, -16]!
435435
CFI_ADJUST(16)
436436
add TRAP_PTR, sp, #0
437+
/* Store the async exception pointer */
438+
str TRAP_PTR, Caml_state(async_exception_pointer)
437439
/* Reload allocation pointer */
438440
ldr ALLOC_PTR, Caml_state(young_ptr)
439441
/* Call the OCaml code */
@@ -445,12 +447,14 @@ L(caml_retaddr):
445447
str x8, Caml_state(exn_handler)
446448
/* Pop the callback link, restoring the global variables */
447449
L(return_result):
450+
ldr x11, [sp, 24]
448451
ldr x10, [sp, 16]
449452
ldp x8, x9, [sp], 32
450453
CFI_ADJUST(-32)
451454
str x8, Caml_state(bottom_of_stack)
452455
str x9, Caml_state(last_return_address)
453456
str x10, Caml_state(gc_regs)
457+
str x11, Caml_state(async_exception_pointer)
454458
/* Update allocation pointer */
455459
str ALLOC_PTR, Caml_state(young_ptr)
456460
/* Reload callee-save registers and return address */
@@ -556,9 +560,10 @@ FUNCTION(caml_raise_exception)
556560
FUNCTION(caml_stack_overflow)
557561
/* Load the exception bucket */
558562
ADDRGLOBAL(x0, caml_exn_Stack_overflow)
559-
/* Cut stack at current trap handler */
560-
mov sp, TRAP_PTR
561-
/* Pop previous handler and jump to it */
563+
/* Cut stack at current async exn trap handler */
564+
ldr TMP, Caml_state(async_exception_pointer)
565+
mov sp, TMP
566+
/* Pop handler and jump to it */
562567
ldr TMP, [sp, 8]
563568
ldr TRAP_PTR, [sp], 16
564569
br TMP

0 commit comments

Comments
 (0)