162306a36Sopenharmony_ciAssembler Annotations
262306a36Sopenharmony_ci=====================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciCopyright (c) 2017-2019 Jiri Slaby
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciThis document describes the new macros for annotation of data and code in
762306a36Sopenharmony_ciassembly. In particular, it contains information about ``SYM_FUNC_START``,
862306a36Sopenharmony_ci``SYM_FUNC_END``, ``SYM_CODE_START``, and similar.
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciRationale
1162306a36Sopenharmony_ci---------
1262306a36Sopenharmony_ciSome code like entries, trampolines, or boot code needs to be written in
1362306a36Sopenharmony_ciassembly. The same as in C, such code is grouped into functions and
1462306a36Sopenharmony_ciaccompanied with data. Standard assemblers do not force users into precisely
1562306a36Sopenharmony_cimarking these pieces as code, data, or even specifying their length.
1662306a36Sopenharmony_ciNevertheless, assemblers provide developers with such annotations to aid
1762306a36Sopenharmony_cidebuggers throughout assembly. On top of that, developers also want to mark
1862306a36Sopenharmony_cisome functions as *global* in order to be visible outside of their translation
1962306a36Sopenharmony_ciunits.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciOver time, the Linux kernel has adopted macros from various projects (like
2262306a36Sopenharmony_ci``binutils``) to facilitate such annotations. So for historic reasons,
2362306a36Sopenharmony_cidevelopers have been using ``ENTRY``, ``END``, ``ENDPROC``, and other
2462306a36Sopenharmony_ciannotations in assembly.  Due to the lack of their documentation, the macros
2562306a36Sopenharmony_ciare used in rather wrong contexts at some locations. Clearly, ``ENTRY`` was
2662306a36Sopenharmony_ciintended to denote the beginning of global symbols (be it data or code).
2762306a36Sopenharmony_ci``END`` used to mark the end of data or end of special functions with
2862306a36Sopenharmony_ci*non-standard* calling convention. In contrast, ``ENDPROC`` should annotate
2962306a36Sopenharmony_cionly ends of *standard* functions.
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciWhen these macros are used correctly, they help assemblers generate a nice
3262306a36Sopenharmony_ciobject with both sizes and types set correctly. For example, the result of
3362306a36Sopenharmony_ci``arch/x86/lib/putuser.S``::
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci   Num:    Value          Size Type    Bind   Vis      Ndx Name
3662306a36Sopenharmony_ci    25: 0000000000000000    33 FUNC    GLOBAL DEFAULT    1 __put_user_1
3762306a36Sopenharmony_ci    29: 0000000000000030    37 FUNC    GLOBAL DEFAULT    1 __put_user_2
3862306a36Sopenharmony_ci    32: 0000000000000060    36 FUNC    GLOBAL DEFAULT    1 __put_user_4
3962306a36Sopenharmony_ci    35: 0000000000000090    37 FUNC    GLOBAL DEFAULT    1 __put_user_8
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThis is not only important for debugging purposes. When there are properly
4262306a36Sopenharmony_ciannotated objects like this, tools can be run on them to generate more useful
4362306a36Sopenharmony_ciinformation. In particular, on properly annotated objects, ``objtool`` can be
4462306a36Sopenharmony_cirun to check and fix the object if needed. Currently, ``objtool`` can report
4562306a36Sopenharmony_cimissing frame pointer setup/destruction in functions. It can also
4662306a36Sopenharmony_ciautomatically generate annotations for the ORC unwinder
4762306a36Sopenharmony_ci(Documentation/arch/x86/orc-unwinder.rst)
4862306a36Sopenharmony_cifor most code. Both of these are especially important to support reliable
4962306a36Sopenharmony_cistack traces which are in turn necessary for kernel live patching
5062306a36Sopenharmony_ci(Documentation/livepatch/livepatch.rst).
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciCaveat and Discussion
5362306a36Sopenharmony_ci---------------------
5462306a36Sopenharmony_ciAs one might realize, there were only three macros previously. That is indeed
5562306a36Sopenharmony_ciinsufficient to cover all the combinations of cases:
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci* standard/non-standard function
5862306a36Sopenharmony_ci* code/data
5962306a36Sopenharmony_ci* global/local symbol
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciThere was a discussion_ and instead of extending the current ``ENTRY/END*``
6262306a36Sopenharmony_cimacros, it was decided that brand new macros should be introduced instead::
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci    So how about using macro names that actually show the purpose, instead
6562306a36Sopenharmony_ci    of importing all the crappy, historic, essentially randomly chosen
6662306a36Sopenharmony_ci    debug symbol macro names from the binutils and older kernels?
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci.. _discussion: https://lore.kernel.org/r/20170217104757.28588-1-jslaby@suse.cz
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciMacros Description
7162306a36Sopenharmony_ci------------------
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciThe new macros are prefixed with the ``SYM_`` prefix and can be divided into
7462306a36Sopenharmony_cithree main groups:
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci1. ``SYM_FUNC_*`` -- to annotate C-like functions. This means functions with
7762306a36Sopenharmony_ci   standard C calling conventions. For example, on x86, this means that the
7862306a36Sopenharmony_ci   stack contains a return address at the predefined place and a return from
7962306a36Sopenharmony_ci   the function can happen in a standard way. When frame pointers are enabled,
8062306a36Sopenharmony_ci   save/restore of frame pointer shall happen at the start/end of a function,
8162306a36Sopenharmony_ci   respectively, too.
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci   Checking tools like ``objtool`` should ensure such marked functions conform
8462306a36Sopenharmony_ci   to these rules. The tools can also easily annotate these functions with
8562306a36Sopenharmony_ci   debugging information (like *ORC data*) automatically.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci2. ``SYM_CODE_*`` -- special functions called with special stack. Be it
8862306a36Sopenharmony_ci   interrupt handlers with special stack content, trampolines, or startup
8962306a36Sopenharmony_ci   functions.
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci   Checking tools mostly ignore checking of these functions. But some debug
9262306a36Sopenharmony_ci   information still can be generated automatically. For correct debug data,
9362306a36Sopenharmony_ci   this code needs hints like ``UNWIND_HINT_REGS`` provided by developers.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci3. ``SYM_DATA*`` -- obviously data belonging to ``.data`` sections and not to
9662306a36Sopenharmony_ci   ``.text``. Data do not contain instructions, so they have to be treated
9762306a36Sopenharmony_ci   specially by the tools: they should not treat the bytes as instructions,
9862306a36Sopenharmony_ci   nor assign any debug information to them.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciInstruction Macros
10162306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~
10262306a36Sopenharmony_ciThis section covers ``SYM_FUNC_*`` and ``SYM_CODE_*`` enumerated above.
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci``objtool`` requires that all code must be contained in an ELF symbol. Symbol
10562306a36Sopenharmony_cinames that have a ``.L`` prefix do not emit symbol table entries. ``.L``
10662306a36Sopenharmony_ciprefixed symbols can be used within a code region, but should be avoided for
10762306a36Sopenharmony_cidenoting a range of code via ``SYM_*_START/END`` annotations.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci* ``SYM_FUNC_START`` and ``SYM_FUNC_START_LOCAL`` are supposed to be **the
11062306a36Sopenharmony_ci  most frequent markings**. They are used for functions with standard calling
11162306a36Sopenharmony_ci  conventions -- global and local. Like in C, they both align the functions to
11262306a36Sopenharmony_ci  architecture specific ``__ALIGN`` bytes. There are also ``_NOALIGN`` variants
11362306a36Sopenharmony_ci  for special cases where developers do not want this implicit alignment.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci  ``SYM_FUNC_START_WEAK`` and ``SYM_FUNC_START_WEAK_NOALIGN`` markings are
11662306a36Sopenharmony_ci  also offered as an assembler counterpart to the *weak* attribute known from
11762306a36Sopenharmony_ci  C.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci  All of these **shall** be coupled with ``SYM_FUNC_END``. First, it marks
12062306a36Sopenharmony_ci  the sequence of instructions as a function and computes its size to the
12162306a36Sopenharmony_ci  generated object file. Second, it also eases checking and processing such
12262306a36Sopenharmony_ci  object files as the tools can trivially find exact function boundaries.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci  So in most cases, developers should write something like in the following
12562306a36Sopenharmony_ci  example, having some asm instructions in between the macros, of course::
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci    SYM_FUNC_START(memset)
12862306a36Sopenharmony_ci        ... asm insns ...
12962306a36Sopenharmony_ci    SYM_FUNC_END(memset)
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci  In fact, this kind of annotation corresponds to the now deprecated ``ENTRY``
13262306a36Sopenharmony_ci  and ``ENDPROC`` macros.
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci* ``SYM_FUNC_ALIAS``, ``SYM_FUNC_ALIAS_LOCAL``, and ``SYM_FUNC_ALIAS_WEAK`` can
13562306a36Sopenharmony_ci  be used to define multiple names for a function. The typical use is::
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci    SYM_FUNC_START(__memset)
13862306a36Sopenharmony_ci        ... asm insns ...
13962306a36Sopenharmony_ci    SYN_FUNC_END(__memset)
14062306a36Sopenharmony_ci    SYM_FUNC_ALIAS(memset, __memset)
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci  In this example, one can call ``__memset`` or ``memset`` with the same
14362306a36Sopenharmony_ci  result, except the debug information for the instructions is generated to
14462306a36Sopenharmony_ci  the object file only once -- for the non-``ALIAS`` case.
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci* ``SYM_CODE_START`` and ``SYM_CODE_START_LOCAL`` should be used only in
14762306a36Sopenharmony_ci  special cases -- if you know what you are doing. This is used exclusively
14862306a36Sopenharmony_ci  for interrupt handlers and similar where the calling convention is not the C
14962306a36Sopenharmony_ci  one. ``_NOALIGN`` variants exist too. The use is the same as for the ``FUNC``
15062306a36Sopenharmony_ci  category above::
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci    SYM_CODE_START_LOCAL(bad_put_user)
15362306a36Sopenharmony_ci        ... asm insns ...
15462306a36Sopenharmony_ci    SYM_CODE_END(bad_put_user)
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci  Again, every ``SYM_CODE_START*`` **shall** be coupled by ``SYM_CODE_END``.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci  To some extent, this category corresponds to deprecated ``ENTRY`` and
15962306a36Sopenharmony_ci  ``END``. Except ``END`` had several other meanings too.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci* ``SYM_INNER_LABEL*`` is used to denote a label inside some
16262306a36Sopenharmony_ci  ``SYM_{CODE,FUNC}_START`` and ``SYM_{CODE,FUNC}_END``.  They are very similar
16362306a36Sopenharmony_ci  to C labels, except they can be made global. An example of use::
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci    SYM_CODE_START(ftrace_caller)
16662306a36Sopenharmony_ci        /* save_mcount_regs fills in first two parameters */
16762306a36Sopenharmony_ci        ...
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci    SYM_INNER_LABEL(ftrace_caller_op_ptr, SYM_L_GLOBAL)
17062306a36Sopenharmony_ci        /* Load the ftrace_ops into the 3rd parameter */
17162306a36Sopenharmony_ci        ...
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci    SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
17462306a36Sopenharmony_ci        call ftrace_stub
17562306a36Sopenharmony_ci        ...
17662306a36Sopenharmony_ci        retq
17762306a36Sopenharmony_ci    SYM_CODE_END(ftrace_caller)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciData Macros
18062306a36Sopenharmony_ci~~~~~~~~~~~
18162306a36Sopenharmony_ciSimilar to instructions, there is a couple of macros to describe data in the
18262306a36Sopenharmony_ciassembly.
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci* ``SYM_DATA_START`` and ``SYM_DATA_START_LOCAL`` mark the start of some data
18562306a36Sopenharmony_ci  and shall be used in conjunction with either ``SYM_DATA_END``, or
18662306a36Sopenharmony_ci  ``SYM_DATA_END_LABEL``. The latter adds also a label to the end, so that
18762306a36Sopenharmony_ci  people can use ``lstack`` and (local) ``lstack_end`` in the following
18862306a36Sopenharmony_ci  example::
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci    SYM_DATA_START_LOCAL(lstack)
19162306a36Sopenharmony_ci        .skip 4096
19262306a36Sopenharmony_ci    SYM_DATA_END_LABEL(lstack, SYM_L_LOCAL, lstack_end)
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci* ``SYM_DATA`` and ``SYM_DATA_LOCAL`` are variants for simple, mostly one-line
19562306a36Sopenharmony_ci  data::
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci    SYM_DATA(HEAP,     .long rm_heap)
19862306a36Sopenharmony_ci    SYM_DATA(heap_end, .long rm_stack)
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci  In the end, they expand to ``SYM_DATA_START`` with ``SYM_DATA_END``
20162306a36Sopenharmony_ci  internally.
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciSupport Macros
20462306a36Sopenharmony_ci~~~~~~~~~~~~~~
20562306a36Sopenharmony_ciAll the above reduce themselves to some invocation of ``SYM_START``,
20662306a36Sopenharmony_ci``SYM_END``, or ``SYM_ENTRY`` at last. Normally, developers should avoid using
20762306a36Sopenharmony_cithese.
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciFurther, in the above examples, one could see ``SYM_L_LOCAL``. There are also
21062306a36Sopenharmony_ci``SYM_L_GLOBAL`` and ``SYM_L_WEAK``. All are intended to denote linkage of a
21162306a36Sopenharmony_cisymbol marked by them. They are used either in ``_LABEL`` variants of the
21262306a36Sopenharmony_ciearlier macros, or in ``SYM_START``.
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciOverriding Macros
21662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~
21762306a36Sopenharmony_ciArchitecture can also override any of the macros in their own
21862306a36Sopenharmony_ci``asm/linkage.h``, including macros specifying the type of a symbol
21962306a36Sopenharmony_ci(``SYM_T_FUNC``, ``SYM_T_OBJECT``, and ``SYM_T_NONE``).  As every macro
22062306a36Sopenharmony_cidescribed in this file is surrounded by ``#ifdef`` + ``#endif``, it is enough
22162306a36Sopenharmony_cito define the macros differently in the aforementioned architecture-dependent
22262306a36Sopenharmony_ciheader.
223