<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Usermode Thread Futex write access denied</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/107016/usermode-thread-futex-write-access-denied</link><description>Greetings, 
 fiddling around with futex and usermode threads I hit a wall when using k_futex_wait(…). The serial prompts me with 
 &amp;lt;err&amp;gt; os: syscall z_vrfy_k_futex_wait failed check: Memory region 0x20001a20 (size 4) write access denied 
 Iam creating</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 03 Jan 2024 05:44:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/107016/usermode-thread-futex-write-access-denied" /><item><title>RE: Usermode Thread Futex write access denied</title><link>https://devzone.nordicsemi.com/thread/462393?ContentTypeID=1</link><pubDate>Wed, 03 Jan 2024 05:44:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d5e1970-51ca-4624-bd5d-37b3e4588535</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Thanks for this comment and the code snippet for giving access grant to the futex object. And Yes I think it is better to take this to Zephyr.&lt;/p&gt;
[quote user="mouthbreather"]globaly declared variables are in the default memory region for user mode threads.[/quote]
&lt;p&gt;Just FYI, Reading &lt;a href="https://devzone.nordicsemi.com/support-private/support/319921/globaly%20declared%20variables%20are%20in%20the%20default%20memory%20region"&gt;setting up memory at boot&lt;/a&gt;, it says&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Global variables, by default, are not accessible to user mode threads. Refer to&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/latest/kernel/usermode/memory_domain.html#memory-domain"&gt;&lt;span&gt;Memory Domains and Partitions&lt;/span&gt;&lt;/a&gt;&amp;nbsp;on how to use global variables in user mode threads, and on how to share data between user mode threads.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Usermode Thread Futex write access denied</title><link>https://devzone.nordicsemi.com/thread/462370?ContentTypeID=1</link><pubDate>Tue, 02 Jan 2024 20:01:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d36202d0-5f61-4dd2-ac94-0809da6efd0a</guid><dc:creator>mouthbreather</dc:creator><description>&lt;p&gt;Thanks for taking a look. I did some more fiddling and found out that even the main thread does not have access permission on the futex struct.Using k_object_is_valid(&amp;amp;running_f, K_OBJ_ANY) returns false. After k_object_grant_access to the main thread k_object_is_valid returns true. But the other threads still have no write permission. This kinda contradicts the documentation on the User Mode section of the zephyr project that states that globaly declared variables are in the default memory region for user mode threads. &lt;/p&gt;
&lt;p&gt;Nonetheless I will take it to the offical zephyr discussion.&lt;/p&gt;
&lt;p&gt;For sake of completness here are the code snippets and the output regarding k_object_is_valid (Mind that this does not compile in nrf Connect SDK 2.5.1 ; I used zephyr/west 3.5.99 to compile)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*Main*/
        LOG_INF(&amp;quot;Main: in %s mode&amp;quot;, k_is_user_context() ? &amp;quot;user&amp;quot; : &amp;quot;kernel&amp;quot;);
        LOG_INF(&amp;quot;running_f is %s valid object&amp;quot;, k_object_is_valid(&amp;amp;running_f, K_OBJ_ANY) ? &amp;quot;an&amp;quot; : &amp;quot;no&amp;quot;);

        k_object_access_grant(&amp;amp;running_f, k_current_get());

        LOG_INF(&amp;quot;running_f is %s valid object&amp;quot;, k_object_is_valid(&amp;amp;running_f, K_OBJ_ANY) ? &amp;quot;an&amp;quot; : &amp;quot;no&amp;quot;);

        LOG_INF(&amp;quot;Start of main&amp;quot;);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*output*/
[00:00:00.584,564] &amp;lt;inf&amp;gt; futex: Main: in kernel mode
[00:00:00.584,625] &amp;lt;err&amp;gt; os: thread 0x20001070 (5) does not have permission on k_futex 0x200060b0
[00:00:00.584,655] &amp;lt;err&amp;gt; os: permission bitmap
                             00 00                                            |..               
[00:00:00.584,686] &amp;lt;inf&amp;gt; futex: running_f is no valid object
[00:00:00.584,716] &amp;lt;inf&amp;gt; futex: running_f is an valid object&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Usermode Thread Futex write access denied</title><link>https://devzone.nordicsemi.com/thread/462292?ContentTypeID=1</link><pubDate>Tue, 02 Jan 2024 13:09:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05ed59a6-a8e7-4551-a12c-9461360a54d1</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;It looks like you need to &lt;a href="https://docs.zephyrproject.org/latest/kernel/usermode/kernelobjects.html#c.k_object_access_grant"&gt;grant_access&lt;/a&gt;&amp;nbsp;to both parent thread objects and also the kernel resources used by the child thread that was created in user mode. The&amp;nbsp;&lt;span&gt;K_INHERIT_PERMS as you said does not seem to be inheriting the parent threads access rights to the kernel objects.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I modified&amp;nbsp;ncs\v2.5.0\zephyr\samples\userspace\prod_consumer\src\main.c to create treads in user mode like you sample and I see memory access violations here as well.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
[quote user=""]I started reading on the usermode page of zehpyr and the memory partition part. Would this be the right way to go or is there an easier approach?[/quote]
&lt;p&gt;It looks like&amp;nbsp;running_f k_futex is not accessible by guard_thread. You might have to share access by putting this the &lt;a href="https://devzone.nordicsemi.com/support-private/support/319921/running_f"&gt;sharable object&lt;/a&gt;&amp;nbsp;and share access.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Usermode Thread Futex write access denied</title><link>https://devzone.nordicsemi.com/thread/462266?ContentTypeID=1</link><pubDate>Tue, 02 Jan 2024 11:55:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70695ba5-6959-4c16-915d-a778fce29af8</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;These are very generic&amp;nbsp;Zephyr RTOS questions that could have have been better asked in Zephyr forums. I haven&amp;#39;t yet experimented with these options myself but I intend to do so this week. I will let you know with my observations.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>