Re: L4 Thread, pthread, comprehension question
Hello Paul, Thank you for your thoughtful comments and thoughts. I have read your blogs about your explorations into L4 and they certainly were helpful for my understanding. Also, thank you for suggesting tools for further investigation. If I find anything intriguing, I will for sure report back. Thank you, David ___ l4-hackers mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: L4 Thread, pthread, comprehension question
Hello Marcus, Thank you so much for answering my questions. These concepts are much clearer to me now. :) Best, David ___ l4-hackers mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: L4 Thread, pthread, comprehension question
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]
Re: L4 Thread, pthread, comprehension question
Welcome to the list, David! > 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? Presuming that the situation hasn't changed since I asked a similar question some time ago, although I cannot actually find that message at the moment, I think that the pthread library is the preferred way. There was a message in January 2024 from someone who was wondering about a different pthread library implementation, but I don't know if that went any further than an initial enquiry. I did consider implementing my own threading library when expanding support for Newlib as the C library in L4Re. However, since I didn't really want yet another subproject to work on, I ended up customising the existing pthread library to work with my Newlib implementation. > 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. I wonder whether the underlying C library might be relying on specific structures provided by libpthread. This may well have been another reason why I decided not to make my own independent threading library. > 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? It has to get to that invocation first, and that journey may be the problem. I would recommend investigating the location of the page fault using the appropriate tools like addr2line and objdump. > 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? Without revisiting my own work and the existing implementation too thoroughly, I would say that threads will share the same exception handlers and pagers, these defined in the task's capability slots. (The unusual case would be the region mapper thread which has a particular purpose.) You do need to make sure that the thread's environment is suitably populated, which you seem to have done. This, of course, involves populating the thread stack with the environment definitions. In my own framework, I do this for the main thread of a task when creating a new task, but it would obviously need doing for every new thread as well. So, I think that your problems may be rooted in the dependencies of the C library on the pthread library, but I welcome any corrections and clarifications, not least because I am not exactly offering a comprehensive answer here! Regards, Paul ___ l4-hackers mailing list -- [email protected] To unsubscribe send an email to [email protected]
Re: L4 Thread, pthread, comprehension question
Also, if there was a way to not display my email address as my username, that would be great. ___ l4-hackers mailing list -- [email protected] To unsubscribe send an email to [email protected]
