<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en" style='--code-editor-font: var(--default-mono-font, "GitLab Mono"), JetBrains Mono, Menlo, DejaVu Sans Mono, Liberation Mono, Consolas, Ubuntu Mono, Courier New, andale mono, lucida console, monospace;'>
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>

<style data-premailer="ignore" type="text/css">
a { color: #1068bf; }
</style>

<style>img {
max-width: 100%; height: auto;
}
body {
font-size: .875rem;
}
body {
-webkit-text-shadow: rgba(255,255,255,.01) 0 0 1px;
}
body {
font-family: "GitLab Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"; font-size: inherit;
}
</style>
</head>
<body style='font-size: inherit; -webkit-text-shadow: rgba(255,255,255,.01) 0 0 1px; font-family: "GitLab Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";'>
<div class="content">

<p class="details" style="font-style: italic; color: #737278;">
<a href="https://gitlab.rtems.org/matteo.concas">Matteo Concas</a> created an issue: <a href="https://gitlab.rtems.org/rtems/rtos/rtems/-/issues/5274">#5274</a>
</p>
<div class="md" style="position: relative; z-index: 1; color: #28272d; word-wrap: break-word;">
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: .3em; border-bottom-width: 1px; border-bottom-color: #bfbfc3; border-bottom-style: solid; color: #28272d; margin: 0 0 16px;" align="initial">
<a href="#summary" aria-hidden="true" class="anchor" id="user-content-summary" style="margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Summary</h2>
<p dir="auto" style="color: #28272d; margin: 0 0 16px;" align="initial">It seems the current RISC-V implementation isn't compatible with processors using the Smdbltrp extension. This extension introduces the MDT (M-mode-disable-trap) bit in the status register which is set when a trap is taken in M mode. If MDT is set then the MIE (Machine Interrupt Enable) bit cannot be set so the MDT bit needs to be cleared at some point.</p>
<p dir="auto" style="color: #28272d; margin: 0 0 16px;" align="initial">From section 3.6.1.2 of the RISC-V Instruction Set Manual Volume II:</p>
<blockquote dir="auto" style="font-size: inherit; color: #4c4b51; padding-top: .5rem; padding-bottom: .5rem; padding-left: 1rem; border-left-color: #dcdcde; border-left-style: solid; margin: .5rem 0; border-width: 0 0 0 4px;" align="initial">
<p style="color: inherit; line-height: 1.5; margin: 0 0 16px;">The M-mode-disable-trap (MDT) bit is a WARL field introduced by the Smdbltrp extension. Upon reset, the MDT field is set to 1. When the MDT bit is set to 1 by an explicit CSR write, the MIE (Machine Interrupt Enable) bit is cleared to 0. For RV64, this clearing occurs regardless of the value written, if any, to the MIE bit by the same write. The MIE bit can only be set to 1 by an explicit CSR write if the MDT bit is already 0 or, for RV64, is being set to 0 by the same write (For RV32, the MDT bit is in mstatush and the MIE bit in mstatus register).</p>
<p style="color: inherit; line-height: 1.5; margin: 0;">When a trap is to be taken into M-mode, if the MDT bit is currently 0, it is then set to 1, and the trap is delivered as expected. However, if MDT is already set to 1, then this is an unexpected trap. When the Smrnmi extension is implemented, a trap caused by an RNMI is not considered an unexpected trap irrespective of the state of the MDT bit. A trap caused by an RNMI does not set the MDT bit. However, a trap that occurs when executing in M-mode with mnstatus.NMIE set to 0 is an unexpected trap.</p>
</blockquote>
<p dir="auto" style="color: #28272d; margin: 0 0 16px;" align="initial">This means a call to <code style='font-size: 90%; color: #18171d; word-wrap: break-word; background-color: #ececef; border-radius: .25rem; margin-top: 0; font-weight: inherit; font-family: "GitLab Mono","JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; vertical-align: bottom; white-space: pre-wrap; overflow-wrap: break-word; word-break: keep-all; padding: 2px 4px;'>_Thread_Do_dispatch</code> might result in a <code style='font-size: 90%; color: #18171d; word-wrap: break-word; background-color: #ececef; border-radius: .25rem; font-weight: inherit; font-family: "GitLab Mono","JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; vertical-align: bottom; white-space: pre-wrap; overflow-wrap: break-word; word-break: keep-all; padding: 2px 4px;'>INTERNAL_ERROR_BAD_THREAD_DISPATCH_ENVIRONMENT</code> error because it checks if interrupts are disabled by looking at the MIE bit.</p>
<p dir="auto" style="color: #28272d; margin: 0 0 16px;" align="initial">We fixed it with this quick patch:</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre data-canonical-lang="diff" class="code highlight js-syntax-highlight language-diff" v-pre="true" style='display: block; font-size: 14px; color: #28272d; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono","JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 16px; padding: 12px; border: 1px solid #dcdcde;'><code style='font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono","JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; vertical-align: bottom; overflow-wrap: normal; padding: unset;'><span id="LC1" class="line" lang="diff" style="margin-top: 0;"><span class="gh" style="margin-top: 0;">diff --git a/cpukit/score/cpu/riscv/riscv-exception-handler.S b/cpukit/score/cpu/riscv/riscv-exception-handler.S</span></span>
<span id="LC2" class="line" lang="diff"><span class="gh" style="margin-top: 0;">index 34e7cbb0b3..8d36f36741 100644</span></span>
<span id="LC3" class="line" lang="diff"><span class="gd" style="margin-top: 0;">--- a/cpukit/score/cpu/riscv/riscv-exception-handler.S</span></span>
<span id="LC4" class="line" lang="diff"><span class="gi" style="margin-top: 0;">+++ b/cpukit/score/cpu/riscv/riscv-exception-handler.S</span></span>
<span id="LC5" class="line" lang="diff"><span class="p" style="margin-top: 0;">@@ -161,6 +161,10 @@</span> SYM(_RISCV_Exception_handler):</span>
<span id="LC6" class="line" lang="diff">     sw    t0, PER_CPU_ISR_DISPATCH_DISABLE(s0)</span>
<span id="LC7" class="line" lang="diff">     sw    t0, PER_CPU_THREAD_DISPATCH_DISABLE_LEVEL(s0)</span>
<span id="LC8" class="line" lang="diff"> </span>
<span id="LC9" class="line" lang="diff"><span class="gi" style="margin-top: 0;">+    /* Clear MDT bit */</span></span>
<span id="LC10" class="line" lang="diff"><span class="gi" style="margin-top: 0;">+    li    t0, (1 << 42)</span></span>
<span id="LC11" class="line" lang="diff"><span class="gi" style="margin-top: 0;">+    csrrc    zero, mstatus, t0</span></span>
<span id="LC12" class="line" lang="diff"><span class="gi" style="margin-top: 0;">+</span></span>
<span id="LC13" class="line" lang="diff">     /* Call _Thread_Do_dispatch(), this function will enable interrupts */</span>
<span id="LC14" class="line" lang="diff">     mv    a0, s0</span>
<span id="LC15" class="line" lang="diff">     li    a1, RISCV_MSTATUS_MIE</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<p dir="auto" style="color: #28272d; margin: 0 0 16px;" align="initial">This will clear the MDT bit before <code style='font-size: 90%; color: #18171d; word-wrap: break-word; background-color: #ececef; border-radius: .25rem; margin-top: 0; font-weight: inherit; font-family: "GitLab Mono","JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; vertical-align: bottom; white-space: pre-wrap; overflow-wrap: break-word; word-break: keep-all; padding: 2px 4px;'>_Thread_Do_dispatch()</code> is called.</p>
<p dir="auto" style="color: #28272d; margin: 0;" align="initial">It would probably be good to discuss how to properly implement this.</p>
</div>

</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #737278;">

<br>
<a href="https://gitlab.rtems.org/rtems/rtos/rtems/-/issues/5274">View it on GitLab</a>.
<br>
You're receiving this email because of your account on <a target="_blank" rel="noopener noreferrer" href="https://gitlab.rtems.org">gitlab.rtems.org</a>. <a href="https://gitlab.rtems.org/-/sent_notifications/ae1bad4809ed4464235829ffc65049fe/unsubscribe" target="_blank" rel="noopener noreferrer">Unsubscribe</a> from this thread · <a href="https://gitlab.rtems.org/-/profile/notifications" target="_blank" rel="noopener noreferrer" class="mng-notif-link">Manage all notifications</a> · <a href="https://gitlab.rtems.org/help" target="_blank" rel="noopener noreferrer" class="help-link">Help</a>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Issue","url":"https://gitlab.rtems.org/rtems/rtos/rtems/-/issues/5274"}}</script>


</p>
</div>
</body>
</html>