blob: 50215a111ead9fbbe6551e2f9999e66fd4289b5d [file] [log] [blame] [view]
Andrew Walbrand0d0e342019-10-17 14:11:42 +01001# Style guide
2
3Hafnium's coding style has been based on the
4[Linux style](https://www.kernel.org/doc/html/v4.17/process/coding-style.html)
5with explicit modifications:
6
7* Always use braces for conditionals and loops. (No SSL `goto fail;`, thanks.)
8
9Following this, we generally fall back to the subset of the
10[Google C++ style guide](https://google.github.io/styleguide/cppguide.html) that
11is applicable to C.
12
13We try to automate this where possible with clang-format and clang-tidy but that
14doesn't capture everything we'd like today. Where the style enforced by this
15tooling conflicts with what is in this document we accept what the tooling
16requires, and try to improve it if possible.
17
Andrew Walbrand0d0e342019-10-17 14:11:42 +010018## Clarifications
19
20* Yes, it does mean all variables are declared, C90-style, at the top of
21 scope, even those loop induction variables.
22* Linux encourages no braces around single-statement branches. We follow
23 Google and require braces around all scope blocks.
24
25## Naming symbols
26
27* Arch-specific functions should start with `arch_`.
28* Platform-specific functions should start with `plat_`.
Shaked Flurb06f12f2019-12-11 14:49:14 +000029* Non-static functions should generally start with the name of the file they
Andrew Walbrand0d0e342019-10-17 14:11:42 +010030 are declared in (after the `arch_` or `plat_` prefix if appropriate), though
31 there are quite a few exceptions to this rule.
32* Prefer `x_count` over `num_x`.
33
34## Prose
35
36These rules apply to comments and other natural language text.
37
38* Capitalize acronyms.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010039 * CPU, vCPU, VM, EL2, FF-A, QEMU
Andrew Walbrand0d0e342019-10-17 14:11:42 +010040* Spell out Hafnium in full, not Hf.
41* Use single spaces.
42* Sentences end with full stops.
43* If the comment fits on one line use `/* */`, otherwise space it out:
44
45 ```
46 /*
47 * Informative long comment
48 * with extra information.
49 */
50 ```
51
52* Doc-ish comments start with `/**`.
53
54 * Use for:
55 * Function definitions (not declarations)
56 * Struct declarations
57 * Enum values
58 * Do not use for:
59 * Macros
60 * Definitions of globals
61
62* References to code symbols use backticks, e.g. `` `my_symbol` ``.
63
64## Coding practices
65
66* Function macros should be functions instead, that way you get types.
Olivier Deprez70128812023-05-22 12:12:03 +020067* Lock ordering is described at the top of [`api.c`](../../src/api.c).
Andrew Walbrand0d0e342019-10-17 14:11:42 +010068* Use opaque types to avoid implicit casts when it will help avoid mistakes.
Olivier Deprez70128812023-05-22 12:12:03 +020069 e.g. [`addr.h`](../../inc/hf/addr.h)
Andrew Walbrand0d0e342019-10-17 14:11:42 +010070* Avoid inline casting. C doesn't give much protection so be formal about the
Olivier Deprez70128812023-05-22 12:12:03 +020071 transformations. e.g. [`addr.h`](../../inc/hf/addr.h)
Andrew Walbrand0d0e342019-10-17 14:11:42 +010072* If a function acquires a resource, there must be a single exit path to free
73 the resource. Tracking down multiple exit points is hard and requires
74 duplicated code which is harder. This may require splitting functions into
75 subfunctions. Early exit is okay if there aren't any clean up tasks.
76* Don't use function pointers. It makes analysis hard and is often a target of
77 attacks.
Andrew Walbran5c38fb72020-02-10 15:00:20 +000078* Be liberal with `CHECK`. Use it to assert pre-/post- conditions.
Andrew Walbrand0d0e342019-10-17 14:11:42 +010079* No self-modifying code.
80* Build targets should include all the direct dependencies for their sources,
81 where possible, rather than relying on transitive dependencies.
Andrew Walbran17eebf92020-02-05 16:35:49 +000082
83## Logging
84
85Hafnium uses the same log levels as Arm Trusted Firmware. There are 5 log
86levels, in order of severity:
87
881. `ERROR`
89
90 Use this only for cases that there is an error in the hypervisor itself,
91 perhaps caused by a coding error, bad configuration, unexpected hardware
92 behaviour or a malformed manifest. Errors should not be logged during normal
93 operation, even in case of a buggy or malicious VM.
94
952. `NOTICE`
96
97 Use this sparingly for important messages which should be logged even in
98 production builds because they will be useful for debugging. This is a
99 suitable level to use for events which may indicate a bug in a VM.
100
1013. `WARNING`
102
103 Use this for warnings which are important to developers but can generally be
104 ignored in production.
105
1064. `INFO`
107
108 Use this to provide extra information that is helpful for developers.
109
1105. `VERBOSE`
111
112 Use this to provide even more information which may be helpful when tracing
113 through execution in detail, such as when debugging test failures. This is
114 the only level which should include any sensitive data.
115
116Logging is done with the `dlog_*` macros, e.g. `dlog_info`. These accept
117printf-style format strings and arguments.
118
119The log level of a build is controlled by the `log_level` argument defined in
Olivier Deprez70128812023-05-22 12:12:03 +0200120[`BUILDCONFIG.gn`](../../build/BUILDCONFIG.gn). This defaults to `INFO` for debug
Andrew Walbran17eebf92020-02-05 16:35:49 +0000121builds and tests, meaning that all levels except `VERBOSE` will be logged. It is
122recommended to set the log level to `NOTICE` for production builds, to reduce
123binary size and log spam.