<?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>stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/90620/stack-guard-module-raises-hard-fault-not-mmu-fault</link><description>hello, 
 we are trying to use the stack guard module - obviously we want this to give well-behaved fault handling in the event that the stack gets too close for comfort. 
 What we found when testing is that an explicit write to the guard area raises a</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 05 Aug 2022 07:31:43 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/90620/stack-guard-module-raises-hard-fault-not-mmu-fault" /><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/380280?ContentTypeID=1</link><pubDate>Fri, 05 Aug 2022 07:31:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ef4e004-7164-411e-8b80-07637ef78dd7</guid><dc:creator>danmcb</dc:creator><description>&lt;p&gt;for us it was more about understanding what was going on. We can either write the handler in asm, use the compile switch you suggested, or use the hard fault handler. Appreciate your assistance.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/380093?ContentTypeID=1</link><pubDate>Thu, 04 Aug 2022 11:25:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6f30ce0-08fc-43db-8660-5a2ca9975145</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Fair enough. I saw this as more of a problem with how the fault handling was implemented. It seems to me like it would have been easier to just use the SDK provided hardfault handler, or is there a specific need that requires you to enable the MemManage handler?&lt;/p&gt;
[quote user="danmcb"]and the stack checking it does seems to be to do with which stack is in use when the fault occurs, which is not an issue here.[/quote]
&lt;p&gt;That too. And in addition, if MSP is used, it will check if it is within the allocated stack section. If it isn&amp;#39;t, then the SP will be reset back to its initial value.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1659612055400v1.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/380084?ContentTypeID=1</link><pubDate>Thu, 04 Aug 2022 10:59:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4ada2c87-5136-4f15-a605-3ca2fedfca49</guid><dc:creator>danmcb</dc:creator><description>&lt;p&gt;the whole thing is about the stack guard module. In fact it is working as advertised, just that there are a few subtleties about making it work well when you have a real stack overflow. Didn&amp;#39;t know about that gcc flag, thanks, we will try it. not sure where the hard fault library comes into it, I am not using it in this example, and the stack checking it does seems to be to do with which stack is in use when the fault occurs, which is not an issue here.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/380073?ContentTypeID=1</link><pubDate>Thu, 04 Aug 2022 09:51:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1dbae858-58fe-49b0-a60f-c39edbc9f3cd</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;OK, I see the problem now. Unlike my FW built with Segger embedded studio, yours is pushing the frame pointer onto the stack when it enters the exception handler and therefore causing another write to the MPU protected stack region.&amp;nbsp; You can build your app with &lt;a href="https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Optimize-Options.html"&gt;-fomit-frame-pointer&lt;/a&gt; to avoid this.&lt;/p&gt;
[quote user="danmcb"]WHat this means is that really the stacj guard module doesn&amp;#39;t actually do what it should, at least if you implement the handler in C.[/quote]
&lt;p&gt;Sorry, but I&amp;#39;m not sure I understand what this has to do with the stack guard module. We already have &lt;span&gt;&lt;a title="HardFault handling library" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/lib_hardfault.html?cp=8_1_3_22"&gt;HardFault handling library&lt;/a&gt;&lt;/span&gt; which includes stack checking and parsing of MPU related faults.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/380037?ContentTypeID=1</link><pubDate>Thu, 04 Aug 2022 07:14:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fb1aee2-602f-4642-a664-74d7715fb5f5</guid><dc:creator>danmcb</dc:creator><description>&lt;p&gt;I did indeed put a breakpoint in Memory Management Handler, it doesn&amp;#39;t get there. It goes straight to hard fault. But that could be something to do with the way the debugger is working I guess.&lt;/p&gt;
&lt;p&gt;If I obj dump the handler it is like this:&lt;/p&gt;
&lt;p&gt;00026302 &amp;lt;MemoryManagement_Handler&amp;gt;:&lt;br /&gt;&amp;nbsp;&amp;nbsp; 26302:&amp;nbsp;&amp;nbsp; &amp;nbsp;b480&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;push&amp;nbsp;&amp;nbsp; &amp;nbsp;{r7}&lt;br /&gt;&amp;nbsp;&amp;nbsp; 26304:&amp;nbsp;&amp;nbsp; &amp;nbsp;af00&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;add&amp;nbsp;&amp;nbsp; &amp;nbsp;r7, sp, #0&lt;br /&gt;&amp;nbsp;&amp;nbsp; 26306:&amp;nbsp;&amp;nbsp; &amp;nbsp;e7fe&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;b.n&amp;nbsp;&amp;nbsp; &amp;nbsp;26306 &amp;lt;MemoryManagement_Handler+0x4&amp;gt;&lt;/p&gt;
&lt;p&gt;So it tries to push on the stack - which is likely what causes the hard fault.&lt;/p&gt;
&lt;p&gt;WHat this means is that really the stacj guard module doesn&amp;#39;t actually do what it should, at least if you implement the handler in C. There are two ways to work around this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;implement the handler in assembler.&lt;/li&gt;
&lt;li&gt;check the stack pointer in the hard fault handler, where you might want to turn off stack guard in order to be able to take corrective actions.&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: stack guard module raises Hard Fault not MMU Fault</title><link>https://devzone.nordicsemi.com/thread/379866?ContentTypeID=1</link><pubDate>Wed, 03 Aug 2022 10:40:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b23acaa1-ad6d-42af-92ff-cad5264be234</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I did not manage to replicate this with the code you posted. Here is the stack trace I got using your code:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1659520656313v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;And the register readout (SP is at the very beginning of my stack guard block):&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1659520766034v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;The only explanation I can think of is that the memory access violation must repeated again when you enter the MemoryManagement_Handler() as this would cause the CPU to raise a hardfault exception and preempt your memory fault handler (). You may place a breakpoint in MemoryManagement_Handler() to confirm this. &lt;/p&gt;
&lt;p&gt;I expect you would get the same issue with the trigger_stack_guard() call if you updated your stack pointer inside it:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void trigger_stack_guard(void)
{
  /* Simulate stack overflow by setting the SP to point to the guard block */
  __set_MSP(&amp;lt;stack guard start address &amp;gt;); // e.g.  __set_MSP(0x2000e400);
  static uint32_t * stack = (uint32_t *)0x2000E1FE;
  *stack = 0xDEADBEEF;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>