eren-terzioglu commented on issue #15656:
URL: https://github.com/apache/nuttx/issues/15656#issuecomment-2665702034

   > Hello [@tmedicci](https://github.com/tmedicci), 
[@eren-terzioglu](https://github.com/eren-terzioglu),
   > 
   > After some analysis, I discovered that the esp_txint function exhibits 
divergent behavior depending on the RISC-V compiler optimization level.
   > 
   > I looked disassembles for both builds and cannot find a explicit reason 
for issue, but studying about I understand that the optimization removes 
implicit barriers (such as jumps and returns) that, combined with the absence 
of volatile qualifiers or explicit memory barriers, compromise hardware 
synchronization. In summary, the reordering of register accesses can affect the 
update of interrupt control bits, leading to system malfunctions.
   > 
   > Look for O0 buid:
   > 
   > ```
   > Disassembly of section .text.esp_txint:
   > 
   > 00000000 <esp_txint>:
   >    0:      1101                    add     sp,sp,-32
   >    2:      ce06                    sw      ra,28(sp)
   >    4:      c62a                    sw      a0,12(sp)
   >    6:      87ae                    mv      a5,a1
   >    8:      00f105a3                sb      a5,11(sp)
   >    c:      00b14783                lbu     a5,11(sp)
   >   10:      c799                    beqz    a5,1e <.L2>
   >   12:      4521                    li      a0,8
   >   14:      00000097                auipc   ra,0x0
   >   18:      000080e7                jalr    ra # 14 <esp_txint+0x14>
   >   1c:      a031                    j       28 <.L4>
   > 
   > 0000001e <.L2>:
   >   1e:      4521                    li      a0,8
   >   20:      00000097                auipc   ra,0x0
   >   24:      000080e7                jalr    ra # 20 <.L2+0x2>
   > 
   > 00000028 <.L4>:
   >   28:      0001                    nop
   >   2a:      40f2                    lw      ra,28(sp)
   >   2c:      6105                    add     sp,sp,32
   >   2e:      8082                    ret
   > 
   > Disassembly of section .text.usb_serial_jtag_ll_ena_intr_mask:
   > 
   > 00000000 <usb_serial_jtag_ll_ena_intr_mask>:
   >    0:      000007b7                lui     a5,0x0
   >    4:      00078793                mv      a5,a5
   >    8:      4b98                    lw      a4,16(a5)
   >    a:      8f49                    or      a4,a4,a0
   >    c:      cb98                    sw      a4,16(a5)
   >    e:      8082                    ret
   > 
   > Disassembly of section .text.usb_serial_jtag_ll_disable_intr_mask:
   > 
   > 00000000 <usb_serial_jtag_ll_disable_intr_mask>:
   >    0:      000007b7                lui     a5,0x0
   >    4:      00078793                mv      a5,a5
   >    8:      4b98                    lw      a4,16(a5)
   >    a:      fff54513                not     a0,a0
   >    e:      8d79                    and     a0,a0,a4
   >   10:      cb88                    sw      a0,16(a5)
   >   12:      8082                    ret
   > ```
   > 
   > and O2 build:
   > 
   > ```
   > Disassembly of section .text.esp_txint:
   > 
   > 00000000 <esp_txint>:
   >    0:      000007b7                lui     a5,0x0
   >    4:      00078793                mv      a5,a5
   >    8:      4b98                    lw      a4,16(a5)
   >    a:      c589                    beqz    a1,14 <.L35>
   >    c:      00876713                or      a4,a4,8
   > 
   > 00000010 <.L37>:
   >   10:      cb98                    sw      a4,16(a5)
   >   12:      8082                    ret
   > 
   > 00000014 <.L35>:
   >   14:      9b5d                    and     a4,a4,-9
   >   16:      bfed                    j       10 <.L37>
   > ```
   > 
   > I was able usbconsole work change the code for:
   > 
   > ```
   > __attribute__((optimize("O0")))
   > static void esp_txint(struct uart_dev_s *dev, bool enable)
   > {
   >   if (enable)
   >     {
   >       usb_serial_jtag_ll_ena_intr_mask(
   >         USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA);
   >     }
   >   else
   >     {
   >       usb_serial_jtag_ll_disable_intr_mask(
   >         USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA);
   >     }
   > }
   > ```
   > 
   > I am not sure this is the right way to solve it, so I want know you 
opinion.
   > 
   > [@acassis](https://github.com/acassis) Will be good listen you also.
   
   Hi @FelipeMdeO,
   We are having a look and will write an update. Thanks for your effort.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to