seL4 / sel4runtime

A minimal runtime for seL4 applications.
Other
12 stars 29 forks source link

Proper way to set up TLS for child thread with separate vspace #19

Open FennelFoxxo opened 1 month ago

FennelFoxxo commented 1 month ago

I'm having trouble finding information on how to configure the TLS for a new thread that should have a different vspace than the parent. This is necessary because the __sel4_ipc_buffer is stored in TLS, and so any IPC calls won't work unless it is set up. My understanding of the process is this:

  1. Map a frame into the parent's vspace at some temporary address (e.x. PARENT_VADDR)
  2. Call sel4runtime_write_tls_image(PARENT_VADDR), which copies the TLS image into this page
  3. Call sel4runtime_set_tls_variable using the TLS base returned from the previous step, and pass the IPC buffer address
  4. Remap the frame into the child thread vspace at some final address (e.x. CHILD_VADDR)
  5. Call seL4_TCB_SetTLSBase using the child thread's TCB and the TLS base (with some offset of CHILD_VADDR - PARENT_VADDR added?)

The issue is step 2 sets some other pointers (see the lookup->tls_base assignment at the end of the copy_tls_data function). The value assigned depends on the the address passed to sel4runtime_write_tls_image - in this case PARENT_VADDR. This address has no meaning in the child's vspace and will probably point to some unmapped page, and will fault as soon as __sel4_ipc_buffer is accessed from the child thread.

I can manually write to the tls_base variable within my TLS page to point to where the page will be mapped in the child vspace, but this feels prone to breaking.

As an aside, I was looking through the include/sel4runtime.h header and the sel4runtime_get_tp_offset function appears to be unimplemented. I don't see a definition for it in this repo (or anywhere on Github). Trying to call it raises an undefined reference linker error, but the other functions I've spot-checked in that header link fine.

Thank you, any help is appreciated!