5 void factor_vm::dispatch_non_resumable_signal(cell* sp, cell* pc,
9 /* Fault came from the VM or foreign code. We don't try to fix the
10 call stack from *sp and instead use the last saved "good value"
11 which we get from ctx->callstack_top. Then launch the handler
12 without going through the resumable subprimitive. */
13 signal_resumable = false;
15 cell frame_top = ctx->callstack_top;
16 cell seg_start = ctx->callstack_seg->start;
18 if (frame_top < seg_start) {
19 /* The saved callstack pointer is outside the callstack
20 segment. That means that we need to carefully cut off one frame
21 first which hopefully should put the pointer within the
22 callstack's bounds. */
23 code_block *block = code->code_block_for_address(*pc);
24 cell frame_size = block->stack_frame_size_for_address(*pc);
25 frame_top += frame_size;
28 /* Cut the callstack down to the shallowest Factor stack
29 frame that leaves room for the signal handler to do its thing,
30 and launch the handler without going through the resumable
32 FACTOR_ASSERT(seg_start <= frame_top);
33 while (frame_top < ctx->callstack_bottom && frame_top < limit) {
34 frame_top = code->frame_predecessor(frame_top);
36 ctx->callstack_top = frame_top;
41 void factor_vm::dispatch_resumable_signal(cell* sp, cell* pc, cell handler) {
43 signal_resumable = true;
45 /* Fault came from Factor, and we've got a good callstack. Route the
46 signal handler through the resumable signal handler
49 cell offset = *sp % 16;
51 signal_handler_addr = handler;
53 /* True stack frames are always 16-byte aligned. Leaf procedures
54 that don't create a stack frame will be out of alignment by
55 sizeof(cell) bytes. */
56 /* On architectures with a link register we would have to check for
57 leafness by matching the PC to a word. We should also use
58 FRAME_RETURN_ADDRESS instead of assuming the stack pointer is the
59 right place to put the resume address. */
64 index = SIGNAL_HANDLER_WORD;
65 } else if (offset == 16 - sizeof(cell)) {
66 /* Make a fake frame for the leaf procedure */
67 FACTOR_ASSERT(code->code_block_for_address(*pc) != NULL);
68 delta = LEAF_FRAME_SIZE;
69 index = LEAF_SIGNAL_HANDLER_WORD;
73 cell new_sp = *sp - delta;
76 tagged<word> handler_word = tagged<word>(special_objects[index]);
77 *pc = (cell)handler_word->entry_point;
80 void factor_vm::dispatch_signal_handler(cell* sp, cell* pc, cell handler) {
82 bool in_code_seg = code->seg->in_segment_p(*pc);
83 cell cs_limit = ctx->callstack_seg->start + stack_reserved;
84 bool resumable_p = in_code_seg && *sp >= cs_limit;
86 dispatch_resumable_signal(sp, pc, handler);
88 dispatch_non_resumable_signal(sp, pc, handler, cs_limit);
91 /* Poking with the stack pointer, which the above code does, means
92 that pointers to stack-allocated objects will become
93 corrupted. Therefore the root vectors needs to be cleared because
94 their pointers to stack variables are now garbage. */