Re: [seL4] Understand how TLS is setup in the root server for x64
Hi Alexandre,
I'm trying to understand how the TLS is setup for the root server on x64 (to access for example the IPC buffer) but I'm failing to connect the pieces and how the server is correctly setting the FS register.
There is detailed documentation for the TLS change on the page for RFC- 3. https://sel4.atlassian.net/browse/RFC-3 In essence, the new TLS model stores the address of the IPC buffer in a thread-local variable which must be initialised by your process or threading runtime along with general TLS intialisation. https://github.com/seL4/seL4/blob/master/libsel4/include/sel4/functions.h#L2... To assist with this there is a simple sel4runtime library which provides a minimal C-runtime and tools for initialising TLS. https://github.com/SEL4PROJ/sel4runtime On x86_64 the TLS register can be set without calling into the kernel by using the `wrfsbase` instruction or by using the `sel4runtime_set_tls_base` function from the sel4runtime library.
Now, I'm failing to find in the codebase where the ELF loader is actually detecting the TLS segment and the according setup with the TLS register.
The ELF loader is not responsible for TLS initialisation, the C-runtime is responsible for configuring TLS of the initial thread. https://bitbucket.ts.data61.csiro.au/projects/SEL4PROJ/repos/sel4runtime/bro...
Never mind, I discovered that I in order to access a TLS variable you need to call `seL4_TCB_SetTLSBase` on seL4_CapInitThreadTCB first in the root server, as I'm trying to work on a bare bone usage of seL4 to better understand it, this part was not obvious from the existing more complex samples!
In general I would recommend having the initial thread in a process initialise its own TLS via the runtime and any new thread configure its own TLS in the initialisation code before the main function call of that thread with the `sel4runtime_set_tls_base`. Each new thread will also need to use `seL4_SetIPCBuffer` to initialise the IPC buffer address in its TLS before performing any invocations or any other system calls. Cheers, Curtis Millar Systems Engineer, Trustworthy Systems, CSIRO's Data61 curtis.millar@data61.csiro.au Data61, CSIRO Level 3, K17 Building UNSW Gate 14, Barker St Kensington NSW 2033 Australia
Hi Curtis, In general I would recommend having the initial thread in a process initialise its own TLS via the runtime and any new thread configure its own TLS in the initialisation code before the main function call of that thread with the `sel4runtime_set_tls_base`. Each new thread will also need to use `seL4_SetIPCBuffer` to initialise the IPC buffer address in its TLS before performing any invocations or any other system calls. Thank you, it makes total sense now. This week-end, I discovered along the RFC-3, the SEL4PROJ/sel4runtime repository on GitHub (completely missed it as it was not part of seL4 org) and I have been able to reconnect the missing pieces. As I'm playing with a prototype language and OS while building it on top of seL4, I'm not using any intermediate C layers to connect to the seL4 API (so custom build and integration and no C runtime), while most of the samples out there are heavily relying on some foundations, which is completely understandable, considering how it can be involving to build anything relevant on top of the seL4 API. So as I'm just really getting started to integrate with seL4 (and I love❤️ it!) I have to go through all these details, but I hope that I won't be spamming unnecessarily here! 😅
participants (2)
-
Alexandre Mutel
-
Millar, Curtis (Data61, Kensington NSW)