Skip to main content

Module tls

Module tls 

Source
Expand description

Guest↔host TLS: TPIDR_EL0 + per-thread host snapshot.

Guest code runs with TPIDR → freestanding GUEST_TLS_MAGIC block; every host entry restores the host TPIDR saved at prepare. Snapshots live in crate::host_slot and are mirrored into the guest block (A1) so enter can restore host TPIDR + alt SP without gettid / map locks.

Guest block layout (must match kh-libsystem):

0 magic  8 errno  16 pthread_self  24 host_tpidr  32 alt_top  40 tsd_vals

Host owns 24/32; freestanding owns tsd_vals.

Structs§

HyperEnterRet
Hypercall enter return: alt SP + guest TLS VA for leave (AAPCS64 x0/x1).

Constants§

GUEST_TLS_ALT_TOP_OFF
Hypercall alt-stack top mirror (host-owned).
GUEST_TLS_ERRNO_OFF
Offset of the errno cell inside the guest TLS block.
GUEST_TLS_HOST_TPIDR_OFF
Host TPIDR_EL0 mirror (written only by host while host TPIDR is live).
GUEST_TLS_MAGIC
Freestanding guest TLS magic ("KHTLS\x01").
GUEST_TLS_PTHREAD_OFF
Offset of pthread_self pointer storage.
GUEST_TLS_SIZE
Size of the guest TLS control block (one page is fine; we use 64 B).
GUEST_TLS_TSD_VALS_OFF
Guest-owned per-thread pthread TSD array pointer (freestanding).

Functions§

clear_guest_tls_on_exit
Restores host TLS and clears the active guest snapshot (worker exit).
enter_guest_tls
Switches TPIDR_EL0 to guest_tpidr and records it in the host slot.
enter_host_tls
Safe wrapper used from Rust trap handlers.
guest_tpidr
Current guest TPIDR snapshot (0 if none).
host_tpidr
Current host TPIDR snapshot (0 if unprepared).
init_guest_tls_block
Writes magic + errno=0 + optional pthread into a guest TLS block.
install_main_guest_tls
Allocates the main-thread guest TLS block and records it in host_slot.
is_guest_tls
True when tpidr looks like a freestanding guest TLS block.
kh_tls_enter_host⚠
Safety
kh_tls_leave_host⚠
Safety
leave_host_tls
Safe wrapper used from Rust trap handlers (map fallback).
main_guest_tls
Main thread guest TLS VA installed by install_main_guest_tls.
prepare_host_meta
Captures the current (host) TPIDR_EL0 into this thread’s host slot.
publish_alt_top_to_current_guest
Update alt_top mirror for the current thread’s guest TLS (host TPIDR live).
publish_boundary_to_guest_tls
Write host boundary fields into a freestanding guest TLS block.