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