Re: L4 Thread, pthread, comprehension question

Marcus Haehnel <[email protected]> Mon, 22 Sep 2025 13:59:48 +0200
Newsgroups gmane.comp.micro-kernel.l4.devel
Message-ID <[email protected]>
Hi David,

On 2025-09-22 05:37, [email protected] wrote:
> Hello L4 hackers, I hope you are all doing well today.
> 
> I am hoping to discuss the topic of threads to better my understanding 
> of it, as it seems to be a major building block in L4Re. I have the 
> following questions (using the c++ based libraries.):
> 
> 1. Is Pthread the official way to create threads in an L4Re 
> application?

That is at least the high level primitive that you should use if you 
have no good reason to use the low level L4 primitives and want to use 
any of the POSIX / libc functionality inside your thread.

> 
> 2. I have made a L4 thread (to understand how threads work in L4) 
> without the help of pthreads by setting up the Attr, which involved in 
> setting the exception handlers(using rm()), pager (using rm()), 
> instruction pointers, stack pointers, binding, ex_reg() and finally 
> running. All seems well until I try printing a message.
> It seems seems I am unable to use the printf() function in the 
> instruction pointer function of said thread. The error I get is :
>     ttest   | l4re_itas[rm]: unhandled read page fault at 0x141 
> pc=0x100f8a0
>     ttest   | l4re_itas: rom/thread_test: Unhandled exception: 
> PC=0x100f8a0 PFA=0x140 LdrFlgs=0x0
> However, if I use printf in a pthread, none of these errors pop up.
>     2a. Am I using the wrong exception handler and pager (I got both 
> from rm)?
>     2b. Also, the  "l4_vcon_send(L4_BASE_LOG_CAP,..,...) function seems 
> to work fine in either case, shouldn't printf() eventually call this 
> function as
>           after a series of indirections?
>     2c. What exc_handler and pager do pthreads use? Why are they able 
> to handle the pager faults and exceptions from printf? Can I 
> appropriate
>           them for the L4 threads I started?

What you are seeing is a side-effect of libpthread being quite tightly 
integrated with the libc. While it is true that printf will call 
l4_vcon_send somewhen down the line it will use some (potentially 
thread-local) internal buffers to print its output. While you can mix 
libpthread threads with native L4 threads you must take care to call no 
function from the libc that would make any use of TLS storage, unless 
you set this up yourself.

If you want to write normal applications std::thread and pthread threads 
are the way to go.

> 
> 3. It seems the Virtual Machines (VMs) that are on the hypervisor are 
> also closely related with threads. I assume these are Vcpu threads and 
> not pthreads.
>     3a. Am I correct in this assumption?
>     3b. How are the exception handlers and pagers set up for these 
> Vcpus?

Yes, a vCPU is in the end only a thread with some extended state. A 
thread in vcpu mode provides an additional exception execution context. 
When a trap from avcpu occurs the kernel will switch the vCPU to the 
exception entry point. See entry_ip and entry_sp here 
https://l4re.org/doc/structl4__vcpu__state__t.html#a1f45174954aa47bb2f21b7b90bed250e

These must be set as part of the vcpu state before starting the vcpu. 
The exception handler must take care of handling page-faults generated 
by threads user code.

I hope this helps :)

Best regards,

- Marcus

> 
> Thank you so much for your time,
> David
> _______________________________________________
> l4-hackers mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
_______________________________________________
l4-hackers mailing list -- [email protected]
To unsubscribe send an email to [email protected]