Skip to content

Commit 2da9d21

Browse files
committed
semihosting: add semihosting section to the docs
The main reason to do this is to document our O_BINARY implementation decision somewhere. However I've also moved some of the implementation details out of qemu-options and added links between the two. As a bonus I've highlighted the scary warnings about host access with the appropriate RST tags. Acked-by: Richard Henderson <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Signed-off-by: Alex Bennée <[email protected]> Message-Id: <[email protected]>
1 parent a0a6754 commit 2da9d21

File tree

2 files changed

+95
-17
lines changed

2 files changed

+95
-17
lines changed

docs/about/emulation.rst

+87
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,90 @@ depending on the guest architecture.
101101

102102
A number of features are are only available when running under
103103
emulation including :ref:`Record/Replay<replay>` and :ref:`TCG Plugins`.
104+
105+
.. _Semihosting:
106+
107+
Semihosting
108+
-----------
109+
110+
Semihosting is a feature defined by the owner of the architecture to
111+
allow programs to interact with a debugging host system. On real
112+
hardware this is usually provided by an In-circuit emulator (ICE)
113+
hooked directly to the board. QEMU's implementation allows for
114+
semihosting calls to be passed to the host system or via the
115+
``gdbstub``.
116+
117+
Generally semihosting makes it easier to bring up low level code before a
118+
more fully functional operating system has been enabled. On QEMU it
119+
also allows for embedded micro-controller code which typically doesn't
120+
have a full libc to be run as "bare-metal" code under QEMU's user-mode
121+
emulation. It is also useful for writing test cases and indeed a
122+
number of compiler suites as well as QEMU itself use semihosting calls
123+
to exit test code while reporting the success state.
124+
125+
Semihosting is only available using TCG emulation. This is because the
126+
instructions to trigger a semihosting call are typically reserved
127+
causing most hypervisors to trap and fault on them.
128+
129+
.. warning::
130+
Semihosting inherently bypasses any isolation there may be between
131+
the guest and the host. As a result a program using semihosting can
132+
happily trash your host system. You should only ever run trusted
133+
code with semihosting enabled.
134+
135+
Redirection
136+
~~~~~~~~~~~
137+
138+
Semihosting calls can be re-directed to a (potentially remote) gdb
139+
during debugging via the :ref:`gdbstub<GDB usage>`. Output to the
140+
semihosting console is configured as a ``chardev`` so can be
141+
redirected to a file, pipe or socket like any other ``chardev``
142+
device.
143+
144+
Supported Targets
145+
~~~~~~~~~~~~~~~~~
146+
147+
Most targets offer similar semihosting implementations with some
148+
minor changes to define the appropriate instruction to encode the
149+
semihosting call and which registers hold the parameters. They tend to
150+
presents a simple POSIX-like API which allows your program to read and
151+
write files, access the console and some other basic interactions.
152+
153+
For full details of the ABI for a particular target, and the set of
154+
calls it provides, you should consult the semihosting specification
155+
for that architecture.
156+
157+
.. note::
158+
QEMU makes an implementation decision to implement all file
159+
access in ``O_BINARY`` mode. The user-visible effect of this is
160+
regardless of the text/binary mode the program sets QEMU will
161+
always select a binary mode ensuring no line-terminator conversion
162+
is performed on input or output. This is because gdb semihosting
163+
support doesn't make the distinction between the modes and
164+
magically processing line endings can be confusing.
165+
166+
.. list-table:: Guest Architectures supporting Semihosting
167+
:widths: 10 10 80
168+
:header-rows: 1
169+
170+
* - Architecture
171+
- Modes
172+
- Specification
173+
* - Arm
174+
- System and User-mode
175+
- https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
176+
* - m68k
177+
- System
178+
- https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=libgloss/m68k/m68k-semi.txt;hb=HEAD
179+
* - MIPS
180+
- System
181+
- Unified Hosting Interface (MD01069)
182+
* - Nios II
183+
- System
184+
- https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/nios2/nios2-semi.txt;hb=HEAD
185+
* - RISC-V
186+
- System and User-mode
187+
- https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc
188+
* - Xtensa
189+
- System
190+
- Tensilica ISS SIMCALL

qemu-options.hx

+8-17
Original file line numberDiff line numberDiff line change
@@ -4633,10 +4633,11 @@ DEF("semihosting", 0, QEMU_OPTION_semihosting,
46334633
QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
46344634
SRST
46354635
``-semihosting``
4636-
Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V only).
4636+
Enable :ref:`Semihosting` mode (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V only).
46374637

4638-
Note that this allows guest direct access to the host filesystem, so
4639-
should only be used with a trusted guest OS.
4638+
.. warning::
4639+
Note that this allows guest direct access to the host filesystem, so
4640+
should only be used with a trusted guest OS.
46404641

46414642
See the -semihosting-config option documentation for further
46424643
information about the facilities this enables.
@@ -4648,22 +4649,12 @@ QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA |
46484649
QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
46494650
SRST
46504651
``-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,userspace=on|off][,arg=str[,...]]``
4651-
Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V
4652+
Enable and configure :ref:`Semihosting` (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V
46524653
only).
46534654

4654-
Note that this allows guest direct access to the host filesystem, so
4655-
should only be used with a trusted guest OS.
4656-
4657-
On Arm this implements the standard semihosting API, version 2.0.
4658-
4659-
On M68K this implements the "ColdFire GDB" interface used by
4660-
libgloss.
4661-
4662-
Xtensa semihosting provides basic file IO calls, such as
4663-
open/read/write/seek/select. Tensilica baremetal libc for ISS and
4664-
linux platform "sim" use this interface.
4665-
4666-
On RISC-V this implements the standard semihosting API, version 0.2.
4655+
.. warning::
4656+
Note that this allows guest direct access to the host filesystem, so
4657+
should only be used with a trusted guest OS.
46674658

46684659
``target=native|gdb|auto``
46694660
Defines where the semihosting calls will be addressed, to QEMU

0 commit comments

Comments
 (0)