crypto/secret: fix inconsequential errors.
[qemu/ar7.git] / docs / devel / loads-stores.rst
blob0d99eb24c1bb853c44165d009afec3ffadb301ea
1 ..
2    Copyright (c) 2017 Linaro Limited
3    Written by Peter Maydell
5 ===================
6 Load and Store APIs
7 ===================
9 QEMU internally has multiple families of functions for performing
10 loads and stores. This document attempts to enumerate them all
11 and indicate when to use them. It does not provide detailed
12 documentation of each API -- for that you should look at the
13 documentation comments in the relevant header files.
16 ``ld*_p and st*_p``
17 ~~~~~~~~~~~~~~~~~~~
19 These functions operate on a host pointer, and should be used
20 when you already have a pointer into host memory (corresponding
21 to guest ram or a local buffer). They deal with doing accesses
22 with the desired endianness and with correctly handling
23 potentially unaligned pointer values.
25 Function names follow the pattern:
27 load: ``ld{type}{sign}{size}_{endian}_p(ptr)``
29 store: ``st{type}{size}_{endian}_p(ptr, val)``
31 ``type``
32  - (empty) : integer access
33  - ``f`` : float access
35 ``sign``
36  - (empty) : for 32 or 64 bit sizes (including floats and doubles)
37  - ``u`` : unsigned
38  - ``s`` : signed
40 ``size``
41  - ``b`` : 8 bits
42  - ``w`` : 16 bits
43  - ``l`` : 32 bits
44  - ``q`` : 64 bits
46 ``endian``
47  - ``he`` : host endian
48  - ``be`` : big endian
49  - ``le`` : little endian
51 The ``_{endian}`` infix is omitted for target-endian accesses.
53 The target endian accessors are only available to source
54 files which are built per-target.
56 There are also functions which take the size as an argument:
58 load: ``ldn{endian}_p(ptr, sz)``
60 which performs an unsigned load of ``sz`` bytes from ``ptr``
61 as an ``{endian}`` order value and returns it in a uint64_t.
63 store: ``stn{endian}_p(ptr, sz, val)``
65 which stores ``val`` to ``ptr`` as an ``{endian}`` order value
66 of size ``sz`` bytes.
69 Regexes for git grep
70  - ``\<ldf\?[us]\?[bwlq]\(_[hbl]e\)\?_p\>``
71  - ``\<stf\?[bwlq]\(_[hbl]e\)\?_p\>``
72  - ``\<ldn_\([hbl]e\)?_p\>``
73  - ``\<stn_\([hbl]e\)?_p\>``
75 ``cpu_{ld,st}*_mmuidx_ra``
76 ~~~~~~~~~~~~~~~~~~~~~~~~~~
78 These functions operate on a guest virtual address plus a context,
79 known as a "mmu index" or ``mmuidx``, which controls how that virtual
80 address is translated.  The meaning of the indexes are target specific,
81 but specifying a particular index might be necessary if, for instance,
82 the helper requires an "always as non-privileged" access rather that
83 the default access for the current state of the guest CPU.
85 These functions may cause a guest CPU exception to be taken
86 (e.g. for an alignment fault or MMU fault) which will result in
87 guest CPU state being updated and control longjmp'ing out of the
88 function call.  They should therefore only be used in code that is
89 implementing emulation of the guest CPU.
91 The ``retaddr`` parameter is used to control unwinding of the
92 guest CPU state in case of a guest CPU exception.  This is passed
93 to ``cpu_restore_state()``.  Therefore the value should either be 0,
94 to indicate that the guest CPU state is already synchronized, or
95 the result of ``GETPC()`` from the top level ``HELPER(foo)``
96 function, which is a return address into the generated code.
98 Function names follow the pattern:
100 load: ``cpu_ld{sign}{size}_mmuidx_ra(env, ptr, mmuidx, retaddr)``
102 store: ``cpu_st{size}_mmuidx_ra(env, ptr, val, mmuidx, retaddr)``
104 ``sign``
105  - (empty) : for 32 or 64 bit sizes
106  - ``u`` : unsigned
107  - ``s`` : signed
109 ``size``
110  - ``b`` : 8 bits
111  - ``w`` : 16 bits
112  - ``l`` : 32 bits
113  - ``q`` : 64 bits
115 Regexes for git grep:
116  - ``\<cpu_ld[us]\?[bwlq]_mmuidx_ra\>``
117  - ``\<cpu_st[bwlq]_mmuidx_ra\>``
119 ``cpu_{ld,st}*_data_ra``
120 ~~~~~~~~~~~~~~~~~~~~~~~~
122 These functions work like the ``cpu_{ld,st}_mmuidx_ra`` functions
123 except that the ``mmuidx`` parameter is taken from the current mode
124 of the guest CPU, as determined by ``cpu_mmu_index(env, false)``.
126 These are generally the preferred way to do accesses by guest
127 virtual address from helper functions, unless the access should
128 be performed with a context other than the default.
130 Function names follow the pattern:
132 load: ``cpu_ld{sign}{size}_data_ra(env, ptr, ra)``
134 store: ``cpu_st{size}_data_ra(env, ptr, val, ra)``
136 ``sign``
137  - (empty) : for 32 or 64 bit sizes
138  - ``u`` : unsigned
139  - ``s`` : signed
141 ``size``
142  - ``b`` : 8 bits
143  - ``w`` : 16 bits
144  - ``l`` : 32 bits
145  - ``q`` : 64 bits
147 Regexes for git grep:
148  - ``\<cpu_ld[us]\?[bwlq]_data_ra\>``
149  - ``\<cpu_st[bwlq]_data_ra\>``
151 ``cpu_{ld,st}*_data``
152 ~~~~~~~~~~~~~~~~~~~~~
154 These functions work like the ``cpu_{ld,st}_data_ra`` functions
155 except that the ``retaddr`` parameter is 0, and thus does not
156 unwind guest CPU state.
158 This means they must only be used from helper functions where the
159 translator has saved all necessary CPU state.  These functions are
160 the right choice for calls made from hooks like the CPU ``do_interrupt``
161 hook or when you know for certain that the translator had to save all
162 the CPU state anyway.
164 Function names follow the pattern:
166 load: ``cpu_ld{sign}{size}_data(env, ptr)``
168 store: ``cpu_st{size}_data(env, ptr, val)``
170 ``sign``
171  - (empty) : for 32 or 64 bit sizes
172  - ``u`` : unsigned
173  - ``s`` : signed
175 ``size``
176  - ``b`` : 8 bits
177  - ``w`` : 16 bits
178  - ``l`` : 32 bits
179  - ``q`` : 64 bits
181 Regexes for git grep
182  - ``\<cpu_ld[us]\?[bwlq]_data\>``
183  - ``\<cpu_st[bwlq]_data\+\>``
185 ``cpu_ld*_code``
186 ~~~~~~~~~~~~~~~~
188 These functions perform a read for instruction execution.  The ``mmuidx``
189 parameter is taken from the current mode of the guest CPU, as determined
190 by ``cpu_mmu_index(env, true)``.  The ``retaddr`` parameter is 0, and
191 thus does not unwind guest CPU state, because CPU state is always
192 synchronized while translating instructions.  Any guest CPU exception
193 that is raised will indicate an instruction execution fault rather than
194 a data read fault.
196 In general these functions should not be used directly during translation.
197 There are wrapper functions that are to be used which also take care of
198 plugins for tracing.
200 Function names follow the pattern:
202 load: ``cpu_ld{sign}{size}_code(env, ptr)``
204 ``sign``
205  - (empty) : for 32 or 64 bit sizes
206  - ``u`` : unsigned
207  - ``s`` : signed
209 ``size``
210  - ``b`` : 8 bits
211  - ``w`` : 16 bits
212  - ``l`` : 32 bits
213  - ``q`` : 64 bits
215 Regexes for git grep:
216  - ``\<cpu_ld[us]\?[bwlq]_code\>``
218 ``translator_ld*``
219 ~~~~~~~~~~~~~~~~~~
221 These functions are a wrapper for ``cpu_ld*_code`` which also perform
222 any actions required by any tracing plugins.  They are only to be
223 called during the translator callback ``translate_insn``.
225 There is a set of functions ending in ``_swap`` which, if the parameter
226 is true, returns the value in the endianness that is the reverse of
227 the guest native endianness, as determined by ``TARGET_WORDS_BIGENDIAN``.
229 Function names follow the pattern:
231 load: ``translator_ld{sign}{size}(env, ptr)``
233 swap: ``translator_ld{sign}{size}_swap(env, ptr, swap)``
235 ``sign``
236  - (empty) : for 32 or 64 bit sizes
237  - ``u`` : unsigned
238  - ``s`` : signed
240 ``size``
241  - ``b`` : 8 bits
242  - ``w`` : 16 bits
243  - ``l`` : 32 bits
244  - ``q`` : 64 bits
246 Regexes for git grep
247  - ``\<translator_ld[us]\?[bwlq]\(_swap\)\?\>``
249 ``helper_*_{ld,st}*_mmu``
250 ~~~~~~~~~~~~~~~~~~~~~~~~~
252 These functions are intended primarily to be called by the code
253 generated by the TCG backend. They may also be called by target
254 CPU helper function code. Like the ``cpu_{ld,st}_mmuidx_ra`` functions
255 they perform accesses by guest virtual address, with a given ``mmuidx``.
257 These functions specify an ``opindex`` parameter which encodes
258 (among other things) the mmu index to use for the access.  This parameter
259 should be created by calling ``make_memop_idx()``.
261 The ``retaddr`` parameter should be the result of GETPC() called directly
262 from the top level HELPER(foo) function (or 0 if no guest CPU state
263 unwinding is required).
265 **TODO** The names of these functions are a bit odd for historical
266 reasons because they were originally expected to be called only from
267 within generated code. We should rename them to bring them more in
268 line with the other memory access functions. The explicit endianness
269 is the only feature they have beyond ``*_mmuidx_ra``.
271 load: ``helper_{endian}_ld{sign}{size}_mmu(env, addr, opindex, retaddr)``
273 store: ``helper_{endian}_st{size}_mmu(env, addr, val, opindex, retaddr)``
275 ``sign``
276  - (empty) : for 32 or 64 bit sizes
277  - ``u`` : unsigned
278  - ``s`` : signed
280 ``size``
281  - ``b`` : 8 bits
282  - ``w`` : 16 bits
283  - ``l`` : 32 bits
284  - ``q`` : 64 bits
286 ``endian``
287  - ``le`` : little endian
288  - ``be`` : big endian
289  - ``ret`` : target endianness
291 Regexes for git grep
292  - ``\<helper_\(le\|be\|ret\)_ld[us]\?[bwlq]_mmu\>``
293  - ``\<helper_\(le\|be\|ret\)_st[bwlq]_mmu\>``
295 ``address_space_*``
296 ~~~~~~~~~~~~~~~~~~~
298 These functions are the primary ones to use when emulating CPU
299 or device memory accesses. They take an AddressSpace, which is the
300 way QEMU defines the view of memory that a device or CPU has.
301 (They generally correspond to being the "master" end of a hardware bus
302 or bus fabric.)
304 Each CPU has an AddressSpace. Some kinds of CPU have more than
305 one AddressSpace (for instance Arm guest CPUs have an AddressSpace
306 for the Secure world and one for NonSecure if they implement TrustZone).
307 Devices which can do DMA-type operations should generally have an
308 AddressSpace. There is also a "system address space" which typically
309 has all the devices and memory that all CPUs can see. (Some older
310 device models use the "system address space" rather than properly
311 modelling that they have an AddressSpace of their own.)
313 Functions are provided for doing byte-buffer reads and writes,
314 and also for doing one-data-item loads and stores.
316 In all cases the caller provides a MemTxAttrs to specify bus
317 transaction attributes, and can check whether the memory transaction
318 succeeded using a MemTxResult return code.
320 ``address_space_read(address_space, addr, attrs, buf, len)``
322 ``address_space_write(address_space, addr, attrs, buf, len)``
324 ``address_space_rw(address_space, addr, attrs, buf, len, is_write)``
326 ``address_space_ld{sign}{size}_{endian}(address_space, addr, attrs, txresult)``
328 ``address_space_st{size}_{endian}(address_space, addr, val, attrs, txresult)``
330 ``sign``
331  - (empty) : for 32 or 64 bit sizes
332  - ``u`` : unsigned
334 (No signed load operations are provided.)
336 ``size``
337  - ``b`` : 8 bits
338  - ``w`` : 16 bits
339  - ``l`` : 32 bits
340  - ``q`` : 64 bits
342 ``endian``
343  - ``le`` : little endian
344  - ``be`` : big endian
346 The ``_{endian}`` suffix is omitted for byte accesses.
348 Regexes for git grep
349  - ``\<address_space_\(read\|write\|rw\)\>``
350  - ``\<address_space_ldu\?[bwql]\(_[lb]e\)\?\>``
351  - ``\<address_space_st[bwql]\(_[lb]e\)\?\>``
353 ``address_space_write_rom``
354 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
356 This function performs a write by physical address like
357 ``address_space_write``, except that if the write is to a ROM then
358 the ROM contents will be modified, even though a write by the guest
359 CPU to the ROM would be ignored. This is used for non-guest writes
360 like writes from the gdb debug stub or initial loading of ROM contents.
362 Note that portions of the write which attempt to write data to a
363 device will be silently ignored -- only real RAM and ROM will
364 be written to.
366 Regexes for git grep
367  - ``address_space_write_rom``
369 ``{ld,st}*_phys``
370 ~~~~~~~~~~~~~~~~~
372 These are functions which are identical to
373 ``address_space_{ld,st}*``, except that they always pass
374 ``MEMTXATTRS_UNSPECIFIED`` for the transaction attributes, and ignore
375 whether the transaction succeeded or failed.
377 The fact that they ignore whether the transaction succeeded means
378 they should not be used in new code, unless you know for certain
379 that your code will only be used in a context where the CPU or
380 device doing the access has no way to report such an error.
382 ``load: ld{sign}{size}_{endian}_phys``
384 ``store: st{size}_{endian}_phys``
386 ``sign``
387  - (empty) : for 32 or 64 bit sizes
388  - ``u`` : unsigned
390 (No signed load operations are provided.)
392 ``size``
393  - ``b`` : 8 bits
394  - ``w`` : 16 bits
395  - ``l`` : 32 bits
396  - ``q`` : 64 bits
398 ``endian``
399  - ``le`` : little endian
400  - ``be`` : big endian
402 The ``_{endian}_`` infix is omitted for byte accesses.
404 Regexes for git grep
405  - ``\<ldu\?[bwlq]\(_[bl]e\)\?_phys\>``
406  - ``\<st[bwlq]\(_[bl]e\)\?_phys\>``
408 ``cpu_physical_memory_*``
409 ~~~~~~~~~~~~~~~~~~~~~~~~~
411 These are convenience functions which are identical to
412 ``address_space_*`` but operate specifically on the system address space,
413 always pass a ``MEMTXATTRS_UNSPECIFIED`` set of memory attributes and
414 ignore whether the memory transaction succeeded or failed.
415 For new code they are better avoided:
417 * there is likely to be behaviour you need to model correctly for a
418   failed read or write operation
419 * a device should usually perform operations on its own AddressSpace
420   rather than using the system address space
422 ``cpu_physical_memory_read``
424 ``cpu_physical_memory_write``
426 ``cpu_physical_memory_rw``
428 Regexes for git grep
429  - ``\<cpu_physical_memory_\(read\|write\|rw\)\>``
431 ``cpu_memory_rw_debug``
432 ~~~~~~~~~~~~~~~~~~~~~~~
434 Access CPU memory by virtual address for debug purposes.
436 This function is intended for use by the GDB stub and similar code.
437 It takes a virtual address, converts it to a physical address via
438 an MMU lookup using the current settings of the specified CPU,
439 and then performs the access (using ``address_space_rw`` for
440 reads or ``cpu_physical_memory_write_rom`` for writes).
441 This means that if the access is a write to a ROM then this
442 function will modify the contents (whereas a normal guest CPU access
443 would ignore the write attempt).
445 ``cpu_memory_rw_debug``
447 ``dma_memory_*``
448 ~~~~~~~~~~~~~~~~
450 These behave like ``address_space_*``, except that they perform a DMA
451 barrier operation first.
453 **TODO**: We should provide guidance on when you need the DMA
454 barrier operation and when it's OK to use ``address_space_*``, and
455 make sure our existing code is doing things correctly.
457 ``dma_memory_read``
459 ``dma_memory_write``
461 ``dma_memory_rw``
463 Regexes for git grep
464  - ``\<dma_memory_\(read\|write\|rw\)\>``
466 ``pci_dma_*`` and ``{ld,st}*_pci_dma``
467 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
469 These functions are specifically for PCI device models which need to
470 perform accesses where the PCI device is a bus master. You pass them a
471 ``PCIDevice *`` and they will do ``dma_memory_*`` operations on the
472 correct address space for that device.
474 ``pci_dma_read``
476 ``pci_dma_write``
478 ``pci_dma_rw``
480 ``load: ld{sign}{size}_{endian}_pci_dma``
482 ``store: st{size}_{endian}_pci_dma``
484 ``sign``
485  - (empty) : for 32 or 64 bit sizes
486  - ``u`` : unsigned
488 (No signed load operations are provided.)
490 ``size``
491  - ``b`` : 8 bits
492  - ``w`` : 16 bits
493  - ``l`` : 32 bits
494  - ``q`` : 64 bits
496 ``endian``
497  - ``le`` : little endian
498  - ``be`` : big endian
500 The ``_{endian}_`` infix is omitted for byte accesses.
502 Regexes for git grep
503  - ``\<pci_dma_\(read\|write\|rw\)\>``
504  - ``\<ldu\?[bwlq]\(_[bl]e\)\?_pci_dma\>``
505  - ``\<st[bwlq]\(_[bl]e\)\?_pci_dma\>``