Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

12.3 Generation of the jump_entry

As discussed in the preceding section, generating branch instructions requires the utilization of a static key to establish the instruction type, the branch target address, and the memory address of the instruction that needs to be modified. How, then, are these static keys generated? This section outlines the structural generation process of the static key.

When a specific block of code requires a dynamic toggle of its branching path, a global static key must be defined for that explicit toggle via the macro definitions:

DEFINE_STATIC_KEY_TRUE(name)

or

DEFINE_STATIC_KEY_FALSE(name)

A static key defined by the DEFINE_STATIC_KEY_TRUE macro is initialized to a default value of true, whereas a static key defined by DEFINE_STATIC_KEY_FALSE(name) is initialized to a default value of false.

The core macro blueprints for DEFINE_STATIC_KEY_TRUE and DEFINE_STATIC_KEY_FALSE are implemented as follows:

#define DEFINE_STATIC_KEY_TRUE(name) struct static_key_true name = STATIC_KEY_TRUE_INIT
#define DEFINE_STATIC_KEY_FALSE(name) struct static_key_false name = STATIC_KEY_FALSE_INIT

The finalized output of expanding these two macros defines the variable name respectively as:

struct static_key name =  {.enabled = {1}, {.entries = (void*) 1UL}}
struct static_key name = {.enabled = {0}, {.entries = (void*) 0UL}}

The functions static_branch_likely(key) and static_branch_unlikely(key) leverage the specified key to switch the execution routing direction of the branch. As established from Table 12-2, regarding the static_branch_likely() function, if the enabled field of the static key is true, static_branch_likely() returns a true value, prompting the compiler to emit a NOP instruction at compile time. Conversely, if the enabled field of the static key is false, static_branch_likely() returns a false value, prompting the compiler to emit a jump instruction at compile time. The function static_branch_unlikely() operates analogously to static_branch_likely().

The functional definitions of static_branch_likely() and static_branch_unlikely() are implemented as follows:

#define static_branch_likely(x)							\
({										\
	bool branch;								\
	if (__builtin_types_compatible_p(typeof(*x), struct static_key_true))		\
		branch = !arch_static_branch(&(x)->key, true);			\
	else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) 	\
		branch = !arch_static_branch_jump(&(x)->key, true);		\
	else									\
		branch = ____wrong_branch_error();				\
	likely(branch);								\
})
#define static_branch_unlikely(x)							\
({										\
	bool branch;								\
	if (__builtin_types_compatible_p(typeof(*x), struct static_key_true))		\
		branch = arch_static_branch_jump(&(x)->key, false);		\
	else if (__builtin_types_compatible_p(typeof(*x), struct static_key_false)) 	\
		branch = arch_static_branch(&(x)->key, false);			\
	else									\
		branch = ____wrong_branch_error();				\
	unlikely(branch);								\
})

Within these blocks, x denotes the static key controlling the execution routing direction of the branch. These two macros leverage arch_static_branch() and arch_static_branch_jump() to determine the evaluation of branch via the static key x, and subsequently employ likely(branch) and unlikely(branch) to instruct the compiler infrastructure where to position the code blocks. During compilation, if the static_branch_likely() function is utilized, the compiler places the hot code succeeding the macro on the straight-line sequential track, while shunting the code block following the else statement onto the taken side of the branch. Similarly, if static_branch_unlikely() is invoked, the compiler places the code succeeding the macro onto the taken side of the branch, while shunting the code block following the else statement onto the straight-line sequential track.

The underlying routines arch_static_branch() and arch_static_branch_jump() are highly dependent on the CPU architecture of the system. For the ARM architecture, these two functions are defined respectively as follows:

static __always_inline bool arch_static_branch(struct static_key *key, bool branch)
{
	asm_volatile_goto("1:\n\t"
		 WASM(nop) "\n\t"
		 ".pushsection __jump_table,  \"aw\"\n\t"
		 ".word 1b, %l[l_yes], %c0\n\t"	
		 ".popsection\n\t"
		 : :  "i" (&((char *)key)[branch]) :  : l_yes);
	return false;
l_yes:
	return true;
}
static __always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)
{
	asm_volatile_goto("1:\n\t"
		 WASM(b) " %l[l_yes]\n\t"
		 ".pushsection __jump_table,  \"aw\"\n\t"
		 ".word 1b, %l[l_yes], %c0\n\t"
		 ".popsection\n\t"
		 : :  "i" (&((char *)key)[branch]) :  : l_yes);
	return false;
l_yes:
	return true;
}

The composition of these two routines adopts the syntax of embedding assembly language instructions within C code. WASM is a helper macro utilized to construct assembly statements. WASM(nop) and WASM(b) define the ARM hardware instructions NOP and b (unconditional branch), respectively. The physical memory location occupied by these two instructions represents the precise location where code patching must be executed. The .pushsection and .popsection compiler directives instruct the GCC toolchain to sequentially place the enclosed code blocks into the specialized __jump_table section. Similarly, the directive .word 1b, %l[l_yes], %c0 acts as an ARM GCC compilation instruction whose purpose is to allocate a discrete jump_entry item, assigning local label 1, label l_yes, and the memory address of the static key to the code, target, and key fields of that jump_entry structure, respectively.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com