<?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>Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/119157/secure-fault-on-i2c-in-one-thread-but-not-another</link><description>This is odd, one thread can access i2c API just fine. 
 Another tread the same API call causes Secure Fault. 
 (These threads are mutually exclusive btw), the environment is set to &amp;quot;ns&amp;quot;. 
 
 This all works as expected. 
 However, this tread causes Security</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 05 Mar 2025 22:34:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/119157/secure-fault-on-i2c-in-one-thread-but-not-another" /><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/526071?ContentTypeID=1</link><pubDate>Wed, 05 Mar 2025 22:34:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:136c4065-ed82-4a2d-ba4c-324c7a9b9dd3</guid><dc:creator>StefanG</dc:creator><description>&lt;p&gt;Ok!&lt;/p&gt;
&lt;p&gt;The problem was that the&amp;nbsp;API call i2c_reg_read_byte() was fed a SENSOR device struct, NOT the actual i2c DEVICE struct, causing the segfault.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Here is the solution:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt; // Get the I2C bus controller that the BME280 is connected to
    const struct device *i2c_dev = DEVICE_DT_GET(DT_BUS(DT_NODELABEL(bme280)));
    if (!device_is_ready(i2c_dev)) {
       LOG_ERR(&amp;quot;I2C bus not ready&amp;quot;);
       return;
    }
    // Now use the I2C controller to read the register
    ret = i2c_reg_read_byte(i2c_dev, BME280_I2C_ADDR, BME280_REG_ID, &amp;amp;bme280_chip_id);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/525333?ContentTypeID=1</link><pubDate>Fri, 28 Feb 2025 15:37:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c1d089d-6d04-4dcb-872a-f84a245881b8</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Glad to hear that things started working without it.&lt;/p&gt;
&lt;p&gt;Hope you have a wonderful weekend!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/525144?ContentTypeID=1</link><pubDate>Thu, 27 Feb 2025 18:12:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8aaa390-3b13-42d1-80fe-ce17db533b53</guid><dc:creator>StefanG</dc:creator><description>&lt;p&gt;I will check,&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Commenting out these and only keeping Sensor_API works fine.&lt;/p&gt;
&lt;p&gt;readbyte is indeed the offending line.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;/Stefan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/524516?ContentTypeID=1</link><pubDate>Tue, 25 Feb 2025 09:04:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f026bf6-f222-456a-b99e-12de9c7e8180</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Stefan,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Is this the line that causes the fault?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;ret = i2c_reg_read_byte(bme280_dev, BME280_I2C_ADDR, BME280_REG_ID, &amp;amp;bme280_chip_id);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The BME280_REG_ID define, which is a constant define, ie. located in flash. DMA requires that buffers are located in RAM, but the driver itself should provide a returned error if that is the case:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs2/drivers/i2c/i2c_nrfx_twim.c#L81-L94"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/v3.7.99-ncs2/drivers/i2c/i2c_nrfx_twim.c#L81-L94&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Will commenting out the first i2c_read_reg_byte() call will make the subsequent sensor_ API call fault as well?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/524392?ContentTypeID=1</link><pubDate>Mon, 24 Feb 2025 14:45:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:238e062b-57c6-4bc2-8bb0-0b55a49c5f49</guid><dc:creator>StefanG</dc:creator><description>&lt;p&gt;Hej,&lt;/p&gt;
&lt;p&gt;Unfortunately, these were already set to the values above. So no difference, it still crashes.&lt;/p&gt;
&lt;p&gt;The difference between these threads is that the one crashing also uses the sensor API, any chance of a security mismatch?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;/Stefan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/524272?ContentTypeID=1</link><pubDate>Mon, 24 Feb 2025 09:19:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99665f0d-fb01-4c18-8d3c-2ec0a0487340</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could this be due to the TFM logging being enabled?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Can you try to set the following in your prj.conf:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_TFM_SECURE_UART=n
CONFIG_TFM_LOG_LEVEL_SILENCE=y&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And then re-generate the build folder?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/524131?ContentTypeID=1</link><pubDate>Fri, 21 Feb 2025 15:13:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e456450f-ccd5-480b-8738-9b8d0aef891b</guid><dc:creator>StefanG</dc:creator><description>&lt;p&gt;Hej H&amp;aring;kan,&lt;/p&gt;
&lt;p&gt;The security warning threw me off, I was used to seeing segfaults :)&lt;/p&gt;
&lt;p&gt;I doubled the tread stack, same issue.&lt;/p&gt;
&lt;p&gt;Traced down the function that segfaults&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static inline int z_impl_i2c_transfer(const struct device *dev,
				      struct i2c_msg *msgs, uint8_t num_msgs,
				      uint16_t addr)
{
	const struct i2c_driver_api *api =
		(const struct i2c_driver_api *)dev-&amp;gt;api;

	if (!num_msgs) {
		return 0;
	}

	msgs[num_msgs - 1].flags |= I2C_MSG_STOP;

	int res =  api-&amp;gt;transfer(dev, msgs, num_msgs, addr);

	i2c_xfer_stats(dev, msgs, num_msgs);

	if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
		i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
	}

	return res;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and this line causes the crash and the CPU is stuck on &amp;quot;Fatal Errors&amp;quot; loop.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;	int res =  api-&amp;gt;transfer(dev, msgs, num_msgs, addr);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The thread is run in user mode, and so is the working thread.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;All calling data looks ok, and this line cannot be stepped into, it crashes immediately. To me, this does look like a security issue. The user thread is not able to call this that transfers the data.&lt;/p&gt;
&lt;p&gt;Another clue is this:&lt;/p&gt;
&lt;p&gt;[00:01:56.467,987] &amp;lt;err&amp;gt; os: ***** SECURE FAULT *****&lt;br /&gt;&lt;span style="background-color:#ffcc00;"&gt;[00:01:56.467,987] &amp;lt;err&amp;gt; os: Invalid entry point&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Secure fault on i2c in one thread, but not another</title><link>https://devzone.nordicsemi.com/thread/524076?ContentTypeID=1</link><pubDate>Fri, 21 Feb 2025 12:24:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:768327d4-8a4f-4276-80a6-d13665232d8c</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Which thread is this wrt. your code?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Current thread: 0x20008388 (bme280_tid)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Based on the faulting instruction, my first guess would be that you are executing a null pointer, which triggers a secure fault.&lt;/p&gt;
&lt;p&gt;If you use addr2line on the LR content,&amp;nbsp;it can help pin-point where the null pointer was called from.&lt;/p&gt;
&lt;p&gt;Have you ensured that your thread has enough stack allocated?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>