0.7.0 —— 两个指针槽:处理器的,和正在运行的上下文的 - #6
Merged
Merged
Conversation
riscv64 后端的注释从写下来那天起就说着: tp 在这里是约定不是架构寄存器。ABI 把它留给线程局部存储 [...] 内核可以拿它 当 per-CPU 指针 —— 但同一个 ISA 上的宿主程序会在那里找它的线程指针。[...] 这个后端不能被编进任何同时用线程指针的东西里。 最后那句是对**每一个消费者**的约束,写在注释里,没有任何东西在执行它。它成立, 是因为在这个架构上还没有别的东西想要线程指针。 ⭐ 实测 2026-08-23:有了。openkal-opensbi 的启动对象要写 tp,好让程序的 thread_local 能用 —— 在一台没有操作系统的机器上没别人会做这件事。没有它, 第一次 throw 就挂在 __cxa_get_globals 里,而诊断报的是一个异常函数和一个地址, 一个字都没提线程指针。 ## 于是槽分成两个,而且探针会说话 arch_cpu_tls / arch_cpu_set_tls 与 arch_cpu_percpu 平行:一个是**处理器**的 私有指针,一个是**正在运行的上下文**的。examples/switch 加了一条断言,而它断言 的是「这是两个槽」—— 单独 round-trip 在两者别名的后端上照样通过。 riscv64: cpu: the two slots ALIAS ← 改之前 aarch64: cpu: the two slots are distinct x86_64: cpu: the two slots are distinct ## riscv64 的 per-CPU 指针移到 mscratch 那是这个特权级为这件事提供的寄存器,而且这个后端里没有别的东西用它 —— trap.S 走栈保存恢复,不通过它交换。tp 留给它按 ABI 本来的主人。S 模式的同款 后端会用 sscratch,理由一样。⚠️ 变的是**哪个寄存器**,不是接口的含义:调用者存一个不透明指针再读回来, 这件事前后一样真。变的是调用者现在还可以用 arch_cpu_tls。 已验证:三个架构 switch ok + 两槽 distinct;宿主 3 个测试全过。
Sunrisepeak
force-pushed
the
feat/openkal-closure
branch
from
August 22, 2026 18:12
bfbbe39 to
a7c16c0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
riscv64 后端的注释从写下来那天起就说着:
最后那句是对每一个消费者的约束,写在注释里,没有任何东西在执行它。它成立,只是因为在这个架构上还没有别的东西想要线程指针。
⭐ 实测 2026-08-23:有了
openkal-opensbi的启动对象要写tp,好让程序的thread_local能用 —— 在一台没有操作系统的机器上没别人会做这件事。没有它,第一次throw就挂在__cxa_get_globals里:诊断报的是一个异常函数和一个地址,一个字都没提线程指针。
槽分成两个,而且探针会说话
arch_cpu_tls/arch_cpu_set_tls与arch_cpu_percpu平行:一个是处理器的私有指针,一个是正在运行的上下文的。examples/switch加的断言是「这是两个槽」,而不是「这个槽能 round-trip」—— 后者在两者别名的后端上照样通过。cpu: the two slots ALIAScpu: the two slots are distinctcpu: the two slots are distinctriscv64 的 per-CPU 指针移到
mscratch那是这个特权级为这件事提供的寄存器,而且这个后端里没有别的东西用它 ——
trap.S走栈保存恢复,不通过它交换。tp留给它按 ABI 本来的主人。S 模式的同款后端会用sscratch,理由一样。arch_cpu_tls。验证
switch ok+the two slots are distinct背景与设计出处:mcpplibs/openkal 的
.agents/docs/2026-08-22-ecosystem-closure-design.md§5.5 / §5.6。