Let’s talk about hooks in RTOS kernel

Follow the + star public account and never miss exciting content

e65ef62730883dca75ad13a125c07c2f.gif

Author | strongerHuang

WeChat public account | strongerHuang

Most operating systems on the market have such a type of API function interface: HOOK function (also called hook function ) .

Desktop operating systems such as Windows and Linux, as well as RT-Thread, µC/OS, and FreeRTOS real-time operating systems, can all see the existence of HOOK functions.

There must be many netizens who have this question: What is the HOOK function? What is it used for?

Let’s talk about what the HOOK function is in conjunction with µC/OS.

What is a HOOK function?

The HOOK function, also called a hook function, is a function called internally by the operating system.

If HOOK is enabled, the operating system will call the corresponding Hook function (the hook function you wrote) when a specific event arrives.

For example: if a Task is deleted in RTOS, the corresponding App_TaskIdleHook function will be called. (This "App_TaskIdleHook" function needs to be written by yourself)

When searching for HOOK content online, most of them use Windows as an example to describe the functions and examples of hook functions, such as Baidu Encyclopedia:

Hook functions are part of the Windows message processing mechanism. By setting "hooks", applications can filter all messages and events at the system level and access messages that are inaccessible under normal circumstances. The essence of a hook is a program used to process system messages. It is hooked into the system through system calls.

Why is it called "hook"?

There are many sayings on the Internet, such as being "hooked" by the operating system. Interested readers can learn more online.

The purpose and usage of HOOK function

The familiar RTOS real-time operating systems such as µC/OS and FreeRTOS all have Hook functions, which can be used by enabling the corresponding switch in the config configuration file.

Let’s talk about the purpose and usage of HOOK function in combination with µC/OS:

1. Configure HOOK

In many RTOSs, there is one (or more) configuration files to configure (tailor) the system.

For example, in µC/OS, there is the os_cfg.h system configuration file. In fact, they are some enabling switches, which turn on the required functions but do not need them, in order to achieve the function of the cutting system.

/* ---------------------- MISCELLANEOUS ----------------------- */
#define OS_APP_HOOKS_EN           1    /* Application-defined hooks are called from the uC/OS-II hooks */
#define OS_ARG_CHK_EN             0    /* Enable (1) or Disable (0) argument checking                  */
#define OS_CPU_HOOKS_EN           1    /* uC/OS-II hooks are found in the processor port files         */


#define OS_DEBUG_EN               1    /* Enable(1) debug variables                                    */

Similarly, the HOOK function is also enabled through the cfg configuration file, such as:

#define OS_APP_HOOKS_EN           1
#define OS_CPU_HOOKS_EN           1

0: closed;

1: open;

Similarly, there is also a similar configuration in FreeRTOS:

8170eb4fb5b4963a45701220d809fd2c.png

We need to use HOOK to enable it.

2. Use TaskIdleHook

TaskIdleHook, the task idle hook function , will be called when the task is idle.

This hook function is available in many operating systems. When the task is idle (all other tasks are suspended), the Hook function will be called.

For example: what we call CPU utilization is measured in idle tasks. Take µC/OS as an example:

void  OS_TaskIdle (void *p_arg)
{
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr = 0u;
#endif
    p_arg = p_arg;                               /* Prevent compiler warning for not using 'p_arg'     */
    for (;;) {
        OS_ENTER_CRITICAL();
        OSIdleCtr++;
        OS_EXIT_CRITICAL();
        OSTaskIdleHook();                        /* Call user definable HOOK                           */
    }
}

9c313540fbd01f0ec22d93f449574e53.png

System idle hook function OSTaskIdleHook:

#if OS_CPU_HOOKS_EN > 0u
void  OSTaskIdleHook (void)
{
#if OS_APP_HOOKS_EN > 0u
    App_TaskIdleHook();
#endif
}
#endif

a0c9ce5ffeed83ab0f4f96c51e74ca12.png

Application idle hook function App_TaskIdleHook:

If it is enabled, how can this function be executed? We need to implement it ourselves. For example, if the idle statistics value exceeds 50 times, we print a piece of information:

void  App_TaskIdleHook (void)
{
  UserIdleCtr++;  //¿ÕÏмÆÊý


  if(50 < UserIdleCtr)
  {
    UserIdleCtr = 0;


    printf("SYS_IDLE");
  }
}

Seeing this, do you understand the principle of HOOK function? It is actually very simple to analyze step by step from within the system.

Because the operating system is in an idle state most of the time, the IDLE idle function will be executed more frequently, which may not be understood by many people.

As mentioned above, the HOOK function will be called and executed when a specific event arrives. IDLE idle tasks are more frequent, and events such as deletion of Task tasks are rare, so this type of HOOK function is rarely generated.

Okay, I will mainly share it here. I hope it will be helpful for you to understand the HOOK function!

------------ END ------------

6c863bfd76efc6b21a5cef4b5937094e.gif

●Column "Embedded Tools "

●Column "Embedded Development"

●Column "Keil Tutorial"

●Embedded column selected tutorials

Follow the official account and reply " Add Group " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.

40d116c62b8297e524c5e70c9d9b9de1.jpeg

6803235893343da3216487cbb3ff369a.png

Click " Read the original text " to view more sharing.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/132867410