Skip to content

[ATfE] Use semihosting SYS_READC for stdin with llvm libc#667

Merged
voltur01 merged 3 commits intoarm:arm-softwarefrom
voltur01:use_sys_readc_for_stdin
Jan 15, 2026
Merged

[ATfE] Use semihosting SYS_READC for stdin with llvm libc#667
voltur01 merged 3 commits intoarm:arm-softwarefrom
voltur01:use_sys_readc_for_stdin

Conversation

@voltur01
Copy link
Contributor

Semihosting SYS_READ call on stdin just returns immediately indicating no data was read, thus rendering stdin unusable with QEMU.

SYS_READC blocks and waits for one symbol to be read from the terminal, however Enter key specifically is returned as '\r', thus the special case (that may not generalize well beyond QEMU).

Semihosting SYS_READ call on stdin just returns immediately indicating no data was read, thus rendering stdin unusable with QEMU.

SYS_READC blocks and waits for one symbol to be read from the terminal, however Enter key specifically is returned as '\r', thus the special case (that may not generalize well beyond QEMU).
@voltur01 voltur01 requested a review from a team as a code owner January 12, 2026 17:21
@smithp35
Copy link
Contributor

I found a QEMU https://gitlab.com/qemu-project/qemu/-/issues/1963 and a Picolibc issue raised from that picolibc/picolibc#697 that mention SYS_READ and SYS_READC.

If I've understood both issues, it seems like SYS_READ does not block in QEMU, but picolibc's read() may block indefinitely for an EOF in stdin. The picolibc response is that the IO implementation would need to use buffered IO to use QEMUs non-blocking SYS_READ.

Copy link
Contributor

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the Picolibc/QEMU discussion in https://gitlab.com/qemu-project/qemu/-/issues/1963, this looks good to me. It is mentioned that newlib only uses SYS_READ, which does indeed look like the case for libgloss/arm/syscalls.c however there may be higher-level code that is able to handle non-blocking IO in newlib, or it may just not work at all with QEMU!

if (retval >= 0)
retval = size - retval;
return retval;
char *buf, size_t size) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be worth a comment explaining why we are using SYS_READC. Something like:

Currently only supports reading from stdin. We use SYS_READC for reading from stdin as QEMUs SYS_READ does not block. For other files SYS_READ should be used as SYS_READC may block indefinitely in QEMU.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! A comment added including a TODO for when there is regular file support in llvm libc.

smithp35
smithp35 previously approved these changes Jan 14, 2026
Copy link
Contributor

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks.

@voltur01
Copy link
Contributor Author

Please review resolution of conflict with #665

Copy link
Contributor

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflict resolution LGTM

@voltur01 voltur01 merged commit b407fd6 into arm:arm-software Jan 15, 2026
2 checks passed
voltur01 added a commit to voltur01/arm-toolchain that referenced this pull request Jan 21, 2026
Semihosting SYS_READ call on stdin just returns immediately indicating
no data was read, thus rendering stdin unusable with QEMU.

SYS_READC blocks and waits for one symbol to be read from the terminal,
however Enter key specifically is returned as '\r', thus the special
case (that may not generalize well beyond QEMU).

(cherry picked from commit b407fd6)
voltur01 added a commit that referenced this pull request Jan 22, 2026
This applies the following changes to 22.x release branch:

#667
[ATfE] Use semihosting SYS_READC for stdin with llvm libc

#676
[ATfE] Remove __LLVM_LIBC__ define workaround for libcxx

#678
[ATfE] Replace call to abort with __llvm_libc_exit in libc startup code

#679
[ATfE] Update comment about handling cleanup for exit()

#683
[ATfE] Provide nohost init and exit in llvmlibc startup code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants