]> git.proxmox.com Git - mirror_qemu.git/blob - HACKING
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / HACKING
1 1. Preprocessor
2
3 1.1. Variadic macros
4
5 For variadic macros, stick with this C99-like syntax:
6
7 #define DPRINTF(fmt, ...) \
8 do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
9
10 1.2. Include directives
11
12 Order include directives as follows:
13
14 #include "qemu/osdep.h" /* Always first... */
15 #include <...> /* then system headers... */
16 #include "..." /* and finally QEMU headers. */
17
18 The "qemu/osdep.h" header contains preprocessor macros that affect the behavior
19 of core system headers like <stdint.h>. It must be the first include so that
20 core system headers included by external libraries get the preprocessor macros
21 that QEMU depends on.
22
23 Do not include "qemu/osdep.h" from header files since the .c file will have
24 already included it.
25
26 2. C types
27
28 It should be common sense to use the right type, but we have collected
29 a few useful guidelines here.
30
31 2.1. Scalars
32
33 If you're using "int" or "long", odds are good that there's a better type.
34 If a variable is counting something, it should be declared with an
35 unsigned type.
36
37 If it's host memory-size related, size_t should be a good choice (use
38 ssize_t only if required). Guest RAM memory offsets must use ram_addr_t,
39 but only for RAM, it may not cover whole guest address space.
40
41 If it's file-size related, use off_t.
42 If it's file-offset related (i.e., signed), use off_t.
43 If it's just counting small numbers use "unsigned int";
44 (on all but oddball embedded systems, you can assume that that
45 type is at least four bytes wide).
46
47 In the event that you require a specific width, use a standard type
48 like int32_t, uint32_t, uint64_t, etc. The specific types are
49 mandatory for VMState fields.
50
51 Don't use Linux kernel internal types like u32, __u32 or __le32.
52
53 Use hwaddr for guest physical addresses except pcibus_t
54 for PCI addresses. In addition, ram_addr_t is a QEMU internal address
55 space that maps guest RAM physical addresses into an intermediate
56 address space that can map to host virtual address spaces. Generally
57 speaking, the size of guest memory can always fit into ram_addr_t but
58 it would not be correct to store an actual guest physical address in a
59 ram_addr_t.
60
61 For CPU virtual addresses there are several possible types.
62 vaddr is the best type to use to hold a CPU virtual address in
63 target-independent code. It is guaranteed to be large enough to hold a
64 virtual address for any target, and it does not change size from target
65 to target. It is always unsigned.
66 target_ulong is a type the size of a virtual address on the CPU; this means
67 it may be 32 or 64 bits depending on which target is being built. It should
68 therefore be used only in target-specific code, and in some
69 performance-critical built-per-target core code such as the TLB code.
70 There is also a signed version, target_long.
71 abi_ulong is for the *-user targets, and represents a type the size of
72 'void *' in that target's ABI. (This may not be the same as the size of a
73 full CPU virtual address in the case of target ABIs which use 32 bit pointers
74 on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
75 the target's ABI must use this type for anything that on the target is defined
76 to be an 'unsigned long' or a pointer type.
77 There is also a signed version, abi_long.
78
79 Of course, take all of the above with a grain of salt. If you're about
80 to use some system interface that requires a type like size_t, pid_t or
81 off_t, use matching types for any corresponding variables.
82
83 Also, if you try to use e.g., "unsigned int" as a type, and that
84 conflicts with the signedness of a related variable, sometimes
85 it's best just to use the *wrong* type, if "pulling the thread"
86 and fixing all related variables would be too invasive.
87
88 Finally, while using descriptive types is important, be careful not to
89 go overboard. If whatever you're doing causes warnings, or requires
90 casts, then reconsider or ask for help.
91
92 2.2. Pointers
93
94 Ensure that all of your pointers are "const-correct".
95 Unless a pointer is used to modify the pointed-to storage,
96 give it the "const" attribute. That way, the reader knows
97 up-front that this is a read-only pointer. Perhaps more
98 importantly, if we're diligent about this, when you see a non-const
99 pointer, you're guaranteed that it is used to modify the storage
100 it points to, or it is aliased to another pointer that is.
101
102 2.3. Typedefs
103 Typedefs are used to eliminate the redundant 'struct' keyword.
104
105 2.4. Reserved namespaces in C and POSIX
106 Underscore capital, double underscore, and underscore 't' suffixes should be
107 avoided.
108
109 3. Low level memory management
110
111 Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
112 APIs is not allowed in the QEMU codebase. Instead of these routines,
113 use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
114 g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
115 APIs.
116
117 Please note that g_malloc will exit on allocation failure, so there
118 is no need to test for failure (as you would have to with malloc).
119 Calling g_malloc with a zero size is valid and will return NULL.
120
121 Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following
122 reasons:
123
124 a. It catches multiplication overflowing size_t;
125 b. It returns T * instead of void *, letting compiler catch more type
126 errors.
127
128 Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.
129
130 Memory allocated by qemu_memalign or qemu_blockalign must be freed with
131 qemu_vfree, since breaking this will cause problems on Win32.
132
133 4. String manipulation
134
135 Do not use the strncpy function. As mentioned in the man page, it does *not*
136 guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
137 It also zeros trailing destination bytes out to the specified length. Instead,
138 use this similar function when possible, but note its different signature:
139 void pstrcpy(char *dest, int dest_buf_size, const char *src)
140
141 Don't use strcat because it can't check for buffer overflows, but:
142 char *pstrcat(char *buf, int buf_size, const char *s)
143
144 The same limitation exists with sprintf and vsprintf, so use snprintf and
145 vsnprintf.
146
147 QEMU provides other useful string functions:
148 int strstart(const char *str, const char *val, const char **ptr)
149 int stristart(const char *str, const char *val, const char **ptr)
150 int qemu_strnlen(const char *s, int max_len)
151
152 There are also replacement character processing macros for isxyz and toxyz,
153 so instead of e.g. isalnum you should use qemu_isalnum.
154
155 Because of the memory management rules, you must use g_strdup/g_strndup
156 instead of plain strdup/strndup.
157
158 5. Printf-style functions
159
160 Whenever you add a new printf-style function, i.e., one with a format
161 string argument and following "..." in its prototype, be sure to use
162 gcc's printf attribute directive in the prototype.
163
164 This makes it so gcc's -Wformat and -Wformat-security options can do
165 their jobs and cross-check format strings with the number and types
166 of arguments.
167
168 6. C standard, implementation defined and undefined behaviors
169
170 C code in QEMU should be written to the C99 language specification. A copy
171 of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
172 included, formatted as a draft, can be downloaded from:
173 http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
174
175 The C language specification defines regions of undefined behavior and
176 implementation defined behavior (to give compiler authors enough leeway to
177 produce better code). In general, code in QEMU should follow the language
178 specification and avoid both undefined and implementation defined
179 constructs. ("It works fine on the gcc I tested it with" is not a valid
180 argument...) However there are a few areas where we allow ourselves to
181 assume certain behaviors because in practice all the platforms we care about
182 behave in the same way and writing strictly conformant code would be
183 painful. These are:
184 * you may assume that integers are 2s complement representation
185 * you may assume that right shift of a signed integer duplicates
186 the sign bit (ie it is an arithmetic shift, not a logical shift)
187
188 In addition, QEMU assumes that the compiler does not use the latitude
189 given in C99 and C11 to treat aspects of signed '<<' as undefined, as
190 documented in the GNU Compiler Collection manual starting at version 4.0.
191
192 7. Error handling and reporting
193
194 7.1 Reporting errors to the human user
195
196 Do not use printf(), fprintf() or monitor_printf(). Instead, use
197 error_report() or error_vreport() from error-report.h. This ensures the
198 error is reported in the right place (current monitor or stderr), and in
199 a uniform format.
200
201 Use error_printf() & friends to print additional information.
202
203 error_report() prints the current location. In certain common cases
204 like command line parsing, the current location is tracked
205 automatically. To manipulate it manually, use the loc_*() from
206 error-report.h.
207
208 7.2 Propagating errors
209
210 An error can't always be reported to the user right where it's detected,
211 but often needs to be propagated up the call chain to a place that can
212 handle it. This can be done in various ways.
213
214 The most flexible one is Error objects. See error.h for usage
215 information.
216
217 Use the simplest suitable method to communicate success / failure to
218 callers. Stick to common methods: non-negative on success / -1 on
219 error, non-negative / -errno, non-null / null, or Error objects.
220
221 Example: when a function returns a non-null pointer on success, and it
222 can fail only in one way (as far as the caller is concerned), returning
223 null on failure is just fine, and certainly simpler and a lot easier on
224 the eyes than propagating an Error object through an Error ** parameter.
225
226 Example: when a function's callers need to report details on failure
227 only the function really knows, use Error **, and set suitable errors.
228
229 Do not report an error to the user when you're also returning an error
230 for somebody else to handle. Leave the reporting to the place that
231 consumes the error returned.
232
233 7.3 Handling errors
234
235 Calling exit() is fine when handling configuration errors during
236 startup. It's problematic during normal operation. In particular,
237 monitor commands should never exit().
238
239 Do not call exit() or abort() to handle an error that can be triggered
240 by the guest (e.g., some unimplemented corner case in guest code
241 translation or device emulation). Guests should not be able to
242 terminate QEMU.
243
244 Note that &error_fatal is just another way to exit(1), and &error_abort
245 is just another way to abort().