Free host: a stop leaves guest code by a jump back to the dispatcher (__builtin_longjmp), not by throwing StopGuest; frames keep their stop context words only for the call-log host

A 1P match stops guest code ~585 times a frame (match1p pinned: 9,355,622 stops in 16,000 frames; most are jumps
through registers, dbsTraverse's table, which lose their frames as the reference's do). Each throw unwound every C++
frame down to the dispatcher through the SEH tables: 10% of the game thread's time in the profile (~1.7 ms a frame).
Nothing between a stop and the dispatcher holds anything to destroy on the free host's paths (generated code, the
runtime's entries, the free host's out-call paths; a port's overrides never see a stop, it is deferred there), so the
free host sets Dispatcher::jumpStops. The call-log host keeps the exception (its stops have strings in scope).
Frame::last (332 context words a dispatch) is only read by the call-log host: Dispatcher::keepStopWords, off on the
free host.

Free 1P (16,000 frames) and story (12,000) records byte-identical to main's; boot and match1p pinned MATCH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Thomas MartinandClaude Opus 5.5 committed 2026-11-04 23:15:14 +01:00
1 parent 37ba878992
commit 7f0a6eeb04
3 files changed
+58 -12

No files matched your search

+40 -12
View File
@@ -149,6 +149,8 @@ namespace bozo::ee
{
ctx->pc = pc;
++stops;
if (stopJump_)
__builtin_longjmp(stopJump_, 1);
throw StopGuest{};
}
@@ -618,6 +620,27 @@ namespace bozo::ee
host_.fault(*this, ctx, std::format("no code at 0x{:x}", pc));
}
// Its own function, not inlined: after the jump back nothing of runFrame's locals is relied on (only what was set
// before the __builtin_setjmp and not changed since: this and ctx).
__attribute__((noinline)) bool Dispatcher::enterCatchingStops(R5900Context *ctx)
{
void *buf[5];
if (__builtin_setjmp(buf))
{
stopJump_ = nullptr;
return false;
}
stopJump_ = buf;
struct Clear // an exception passing through (a host ending the run) leaves no jump target behind
{
void **&p;
~Clear() { p = nullptr; }
} clear{stopJump_};
updateLimit(ctx);
enter(ctx);
return ctx->pc == 0;
}
void Dispatcher::runFrame(Thread &t, Frame &f)
{
current_ = &t;
@@ -629,17 +652,21 @@ namespace bozo::ee
inGuest_ = true;
lastHandlerEnded_ = false;
bool ended = false;
try
{
updateLimit(ctx);
// Guest code runs until it stops or the bottom function returns: then into a function whose frame is gone
// (the dispatcher goes on there, at ctx->pc, as after any stop), or to 0 (the thread or handler ends).
enter(ctx);
ended = ctx->pc == 0;
}
catch (const StopGuest &)
{
}
if (jumpStops)
ended = enterCatchingStops(ctx);
else
try
{
updateLimit(ctx);
// Guest code runs until it stops or the bottom function returns: then into a function whose frame is
// gone (the dispatcher goes on there, at ctx->pc, as after any stop), or to 0 (the thread or handler
// ends).
enter(ctx);
ended = ctx->pc == 0;
}
catch (const StopGuest &)
{
}
inGuest_ = false;
ctx->noStop = 0; // a stop or a return to the dispatcher ends every inner call (their return stack is gone)
ctx->inPlace = 0;
@@ -648,7 +675,8 @@ namespace bozo::ee
checkpoints_ = ctx->checkpoints;
f.status = rt_.cop0Status;
f.ran = true;
toWords(f.ctx, f.status, f.last);
if (keepStopWords)
toWords(f.ctx, f.status, f.last);
lastThread_ = t.id;
lastDepth_ = t.depth();
current_ = nullptr;
+4
View File
@@ -27,6 +27,10 @@ namespace bozo::host
std::string &error)
{
d_ = &d;
// Stops jump back to the dispatcher (nothing on the way holds anything to destroy: dispatcher.h), and no frame
// keeps the context words only the call-log host compares.
d.jumpStops = true;
d.keepStopWords = false;
d.kernel().setObserver(this);
d.runtime().port = this;
libs_.attach(d);
+14
View File
@@ -203,6 +203,16 @@ namespace bozo::ee
// Pinned mode (kernel.md section 4): asynchronous events are taken where the host says (the reference's
// points), not when the EE clock reaches them; everything else is decided as in free mode.
bool pinned = false;
// How a stop leaves guest code. Off: by throwing StopGuest, which unwinds every C++ frame between the stop and
// the dispatcher (each one's destructors run). On: by a jump straight back to the dispatcher (__builtin_longjmp),
// no frame unwound: a host may set it only when nothing between holds anything to destroy (the generated code,
// the runtime's entries and its own out-call paths; a port's overrides never see a stop, it is deferred there:
// kernel.md section 3b). The free host does: an exception's unwinding cost ~20 us a stop (the 1P match's
// jumps through registers, dbsTraverse's table, stop a few hundred times a second).
bool jumpStops = false;
// Whether each frame keeps the context it stopped with (Frame::last, the CTX record's words: calllog.md 4.4).
// Only the call-log host reads them.
bool keepStopWords = true;
// Statistics.
uint64_t dispatches = 0, stops = 0, handlersRun = 0, jumps = 0;
@@ -214,6 +224,9 @@ namespace bozo::ee
bool kernelCall(KernelCall call, R5900Context *ctx);
Thread *choose();
void runFrame(Thread &t, Frame &f);
// Runs guest code at ctx->pc (enter) with a stop's jump target set (jumpStops): true when the bottom function
// returned, false when guest code stopped.
bool enterCatchingStops(R5900Context *ctx);
// Runs guest code at ctx->pc until the bottom function returns.
void enter(R5900Context *ctx);
bool mustStop(const R5900Context *ctx, uint64_t cycle);
@@ -245,5 +258,6 @@ namespace bozo::ee
bool started_ = false;
uint32_t outcallDepth_ = 0;
uint32_t deferredPc_ = 0;
void **stopJump_ = nullptr; // enterCatchingStops' __builtin_setjmp buffer while guest code runs (jumpStops)
};
}