Event processing method and device based on microkernel
By pre-written the functional code of the shared code area in the microkernel system and executed by the virtual machine in kernel mode, the problem of low processing efficiency caused by frequent communication in the microkernel system is solved, and more efficient system performance is achieved.
Patent Information
- Application Number
- CN202510005439.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-02
- Publication Date
- 2025-05-06
AI Technical Summary
In microkernel systems, due to frequent communication between services, multiple inter-process communication (IPC) are caused, thus reducing processing efficiency.
By pre-write the functional code of the shared code area in user mode and execute the code in kernel mode by a virtual machine embedded in microkernel, the number of switching and communications between user mode and kernel mode is reduced.
It improves the processing efficiency of the microkernel system, reduces the number of mode switching and communication times, and improves the overall performance of the system.
Smart Images

Figure CN119938358A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a microkernel-based event processing method and device. Background Art
[0002] In the microkernel, there are address space boundaries between multiple threads generated by the target application running in user mode, so the communication between threads currently relies on IPC (Inter Process Communication). It takes three steps to complete a communication: mode switching to kernel mode, message passing process and mode switching back to user mode. And because the communication between services is very frequent in the microkernel system, it involves multiple IPCs to complete the communication between services, resulting in low processing efficiency of the microkernel system. Summary of the invention
[0003] In view of this, the embodiments of the present disclosure are directed to providing a microkernel-based event processing method and device, which can improve the processing efficiency of the microkernel system.
[0004] According to a first aspect of the present disclosure, a microkernel-based event processing method is provided, wherein the microkernel is embedded with a virtual machine, and the method comprises: in response to detecting a preset trigger event in the process of running a target application in user mode, switching from user mode to kernel mode, and obtaining bytecode associated with the trigger event; the bytecode is a function code pre-written into a shared code area in user mode, and the function code is a code corresponding to a part of the function in the target application; controlling the virtual machine to execute the bytecode, and writing the execution result of the trigger event into a shared data area, so that after switching to user mode, the execution result can be read from the shared data area in user mode.
[0005] According to a second aspect of the present disclosure, there is provided an event processing device based on a microkernel, wherein the microkernel is embedded with a virtual machine, and the device comprises: an acquisition module, configured to switch from user mode to kernel mode in response to detecting a preset trigger event in the process of running a target application in user mode, and to acquire bytecode associated with the trigger event; the bytecode is a function code pre-written into a shared code area in user mode, and the function code is a code corresponding to some functions in the target application; and a control module, configured to control the virtual machine to execute the bytecode and write the execution result of the trigger event into a shared data area, so as to read the execution result from the shared data area in user mode after switching to user mode.
[0006] According to a third aspect of the present disclosure, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the method described in the first aspect when executing the computer program.
[0007] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the method described in the first aspect is implemented.
[0008] According to a fifth aspect of the present disclosure, a computer program product is provided, including a computer program, wherein when the computer program is executed by a processor, the method described in the first aspect is implemented.
[0009] According to the microkernel-based event processing method and device of the embodiment of the present disclosure, the codes corresponding to some functions of the target application are pre-written into the shared code area, so that they are executed by the virtual machine embedded in the microkernel in kernel mode, that is, some functions originally executed in the user module are changed to be executed in kernel mode, thereby reducing the frequent switching between user mode and kernel mode and the number of communications, thereby improving the processing efficiency of the microkernel system. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0011] Figure 1 is a flow chart of a microkernel-based event processing method provided by an embodiment of the present disclosure;
[0012] Figure 2 is a system architecture diagram applicable to the embodiments of the present disclosure;
[0013] Figure 3 is an execution flow chart based on active calling interface provided by an embodiment of the present disclosure;
[0014] Figure 4 is a schematic diagram of an active call event provided by an embodiment of the present disclosure;
[0015] Figure 5 is a schematic diagram of processing a notification message provided by an embodiment of the present disclosure;
[0016] Figure 6 is a schematic diagram of processing a notification message provided by an embodiment of the present disclosure;
[0017] Figure 7 It is a schematic diagram of the driver thread optimizing the IPC communication process provided by an embodiment of the present disclosure;
[0018] Figure 8 is a flowchart of configuring a Wasm virtual machine provided by an embodiment of the present disclosure;
[0019] Fig. 9 This is a flowchart of clearing the existing configuration of the Wasm virtual machine provided by an embodiment of the present disclosure;
[0020] Fig.10 is a flow chart of a microkernel-based event processing method provided by an embodiment of the present disclosure;
[0021] Fig.11 is a schematic diagram of a microkernel-based event processing device provided in an embodiment of the present disclosure;
[0022] Fig.12 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure. DETAILED DESCRIPTION
[0023] The following will be combined with the drawings in the embodiments of the present disclosure to clearly and completely describe the technical solutions in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0024] The following is a brief description of how to reduce the microkernel scheduling time in related technologies.
[0025] To address the above issues, we migrated thread IPC. During inter-process communication, the microkernel will not block user-mode threads, but will allow user-mode threads to execute kernel-mode code. The kernel-mode thread is not awakened during the entire process, so the microkernel will not perform a complete context switch, but only switch the address space (page table) and other system states related to request processing.
[0026] Migrating thread IPC reduces the time of microkernel scheduling, that is, improving the processing efficiency of the microkernel system by optimizing the IPC processing flow, but the effect of improving efficiency is limited.
[0027] In view of this, the embodiments of the present disclosure provide a new idea to improve the processing efficiency of the microkernel system from the perspective of reducing the number of communications.
[0028] Figure 1 FIG. 1 is a flowchart of a microkernel-based event processing method provided by an embodiment of the present disclosure. Figure 1 , the microkernel-based event processing method of the embodiment of the present disclosure may include:
[0029] Step 101: In response to detecting a preset trigger event during the process of running a target application in user mode, switch from user mode to kernel mode and obtain bytecode associated with the trigger event; the bytecode is a function code pre-written into a shared code area in user mode, and the function code is a code corresponding to a part of the function in the target application.
[0030] Step 103: Control the virtual machine to execute the bytecode, and write the execution result of the triggering event into the shared data area, so that after switching to the user mode, the execution result can be read from the shared data area in the user mode.
[0031] It can be seen from the above process that the present invention pre-writes the codes corresponding to some functions of the target application into the shared code area, so that they are executed by the virtual machine embedded in the microkernel in kernel mode, that is, some functions originally executed in the user module are changed to be executed in kernel mode, thereby reducing the frequent switching between user mode and kernel mode and the number of communications, thereby improving the processing efficiency of the microkernel system.
[0032] The following describes in detail each step in the above process and the effects that can be further produced in conjunction with the embodiments.
[0033] The microkernel-based event processing method provided in the embodiment of the present disclosure is implemented by a processor. When executing a target application, the processor can be divided into a user mode and a kernel mode according to the code type and permission level it runs, that is, there are threads in the user mode and threads in the kernel mode. These two states are used to distinguish the permissions of the target user program and the microkernel.
[0034] The user state is also called user mode, which refers to the running state of the target application. In the user state, the target application requests services provided by the operating system through system calls, such as trigger event requests sent in response to trigger events.
[0035] Kernel state, also known as kernel mode, is the running state of the operating system microkernel.
[0036] However, user state and kernel state are not completely independent running spaces. The target application may need the support of the operating system (i.e., microkernel system) during operation. At this time, it is necessary to enter kernel state from user state and provide services by the operating system. The microkernel in the operating system also needs to switch back to user state as needed and return control to the target application. This switching process involves operations such as saving and restoring the processor state and switching the context, which requires the underlying support of the operating system.
[0037] The context switching includes: switching from user state to kernel state, which requires context switching; and switching from kernel state back to user state, which requires context switching.
[0038] In order to improve the processing efficiency of the microkernel system, the present invention integrates a virtual machine into the microkernel, and by extending the microkernel, moves the trigger event processed in the user mode to the microkernel for execution. After obtaining the bytecode associated with the trigger event, the virtual machine embedded in the microkernel is controlled to execute the bytecode, and the execution result of the trigger event is written into the shared data area, so that after switching to the user mode, the execution result can be read from the shared data area in the user mode. In this way, there is no need to switch between the kernel mode and the user mode multiple times when executing the trigger event, thereby improving the processing efficiency of the microkernel system.
[0039] The microkernel systems involved in the embodiments of the present disclosure include but are not limited to seL4, L3, QNX, etc.
[0040] The virtual machines involved in the embodiments of the present disclosure include but are not limited to: a virtual machine that executes ebpf (extended Berkeley Packet Filter) bytecode, a virtual machine that executes Wasm (WebAssembly, a binary instruction format designed for stack virtual machines) bytecode (hereinafter referred to as Wasm virtual machine), a virtual machine that executes lua (a lightweight scripting language) bytecode, etc.
[0041] In the following description, SeL4 is taken as the microkernel system, the Wasm virtual machine that executes Wasm bytecode is taken as an example, and the embedded environment is taken as the operating environment of the Wasm virtual machine.
[0042] First, in conjunction with the embodiment, the above step 101, namely, "in response to detecting a preset trigger event in the process of running the target application in user mode, switching from user mode to kernel mode, and obtaining the bytecode associated with the trigger event; the bytecode is a function code pre-written into the shared code area in user mode, and the function code is a code corresponding to part of the functions in the target application" is described in detail.
[0043] In the embodiment of the present disclosure, when the processor detects a preset trigger event during the process of running the target application in the user mode, it switches from the user mode to the kernel mode and obtains the Wasm bytecode associated with the preset trigger event.
[0044] In the embodiment of the present disclosure, before responding to detecting a preset trigger event during the process of running the target application in the user mode, the method further includes:
[0045] Step 1: In an embedded environment, user-mode real-time injection code is not required; therefore, in an embedded environment, the injection code can be compiled into a Wasm file, and the Wasm file can be compiled into the microkernel image.
[0046] Among them, the Wasm file is: the injection code is compiled and generated in the host computer using the SDK (Software Development Kit) of the Wasm system interface.
[0047] The second step is to read the Wasm bytecode in the Wasm file in user mode and copy the Wasm bytecode to the shared memory (such as Figure 2 Shared code area (such as Figure 2 in the Shared Code Area).
[0048] Before the first step, the method also includes: initializing the operating environment of the Wasm virtual machine; creating a shared code area for the target application, and writing the code corresponding to some functions in the target application into the shared code area; and creating a shared data area for the target application.
[0049] Among them, creating a shared code area and a shared data area for the target application may refer to a shared code area for storing Wasm bytecode allocated for normal operation of the target application, and a shared data area for data interaction between kernel mode and user mode.
[0050] After creating the shared code area and the shared data area, the capabilities of the shared code area object and the capabilities of the shared data area object are obtained respectively, wherein the capabilities here represent the access rights of the user mode and the kernel mode to access the shared code area object and the shared data area object respectively.
[0051] The disclosed embodiments set different types of trigger points in the microkernel according to actual use requirements. Trigger events refer to specific events triggered by trigger points, which require the microkernel to respond and process. Trigger events usually contain information such as event type and event parameters, which are used by the microkernel to decide how to process the event.
[0052] exist Figure 2 In the , the trigger point for the microkernel to trigger the execution of Wasm bytecode can be configured by thread, and the trigger point can include an interrupt trigger point, an IPC trigger point, and an active call trigger point. Correspondingly, the trigger event is: an interrupt event corresponding to an interrupt trigger point, an IPC event corresponding to an IPC trigger point, or an active call event corresponding to an active call trigger point.
[0053] The disclosed embodiments can be applied to various usage scenarios such as interrupt processing, IPC communication, and active user mode calls, and have flexible usage methods and strong scalability.
[0054] Wherein, obtaining the Wasm bytecode associated with the trigger event includes: obtaining the Wasm bytecode associated with the trigger event for trigger events corresponding to different types of trigger points.
[0055] After executing the second step in step 101, the method further includes: loading Wasm bytecode from the shared code area; and setting an association between the trigger event and the Wasm bytecode.
[0056] Here, setting the association between the trigger event and the Wasm bytecode may refer to setting the association between different types of trigger points and corresponding Wasm bytecodes.
[0057] In order to ensure the security of Wasm bytecode, the embodiment of the present disclosure verifies the Wasm bytecode in the shared code area, and only loads the Wasm bytecode from the shared code area after the verification is successful.
[0058] Among them, verifying the Wasm bytecode includes verifying whether Wasm meets at least one of the following:
[0059] Use native function interfaces to access shared code areas;
[0060] It is forbidden to use function pointers to access functions;
[0061] It is forbidden to inject dead loop code;
[0062] Limit the number of injected code lines.
[0063] The native function interface is used to access shared memory and objects. The native function interface includes at least one of the following:
[0064] (1) A function interface for writing bytes to the offset position of the shared data area associated with the Wasm bytecode;
[0065] Function interface for reading bytes from the shared data area offset offset associated with Wasm bytecode;
[0066] Function interface for obtaining the length of the shared data area associated with the Wasm bytecode.
[0067] (2) Function interface for serial port printing and character acquisition.
[0068] (3) Function interface used to output characters to the serial port.
[0069] (4) A function interface used to obtain the offset-th parameter of the input passed in by the active call.
[0070] After verifying the Wasm bytecode, the disclosed embodiment instantiates the shared code area written with the Wasm bytecode to obtain an instantiated shared code area object.
[0071] Continuing from the above, after instantiating the shared code area object, setting the association between the trigger event and the Wasm bytecode includes: setting the association between the trigger event and the instantiated shared code area object.
[0072] Continuing from the above, for the active call trigger point, the embodiment of the present disclosure also needs to implement user mode active control execution of Wasm bytecode through the active call interface, and the input parameters include the shared memory capability and 4 input parameters.
[0073] exist Figure 3 In the process, the user mode actively controls the Wasm virtual machine to execute Wasm bytecode by actively calling the interface, including:
[0074] (1) Get the system call parameter list; the parameter list is used to define the parameters of the active call interface; (2) Save the original shared memory capabilities of the Wasm virtual machine; (3) Set the capabilities of the shared code area for writing Wasm bytecodes; (4) Trigger the Wasm virtual machine to execute the associated Wasm bytecode; (5) Restore the original shared memory capabilities.
[0075] The above step 103, i.e., "controlling the virtual machine to execute bytecodes and writing the execution result of the triggering event into the shared data area, so as to read the execution result from the shared data area in the user mode after switching to the user mode" is described in detail below in conjunction with the embodiments.
[0076] In the disclosed embodiment, the Wasm virtual machine is controlled to execute Wasm bytecode, and the execution result is written into the shared data area, so that after switching to user mode, the execution result can be read from the shared data area in user mode.
[0077] In one example, the triggering event is an active call event corresponding to different threads in the target application program;
[0078] Control the Wasm virtual machine to execute bytecode and write the execution results of the triggering event into the shared data area, including:
[0079] For different threads, the same Wasm virtual machine is controlled to execute the same Wasm bytecode, and the execution results of the active triggering events corresponding to different threads are written into different shared data areas.
[0080] The disclosed embodiment calls the same Wasm virtual machine to execute the same Wasm bytecode for active call events corresponding to different threads in a target application, and writes the execution results of the active call events corresponding to different threads into different shared data areas; wherein the different shared data areas are configured by the above-mentioned active call interface.
[0081] When the trigger point is an active call trigger point, the Wasm bytecode associated with the active call trigger point is pre-written into the shared code area (such as Figure 2 The shared data area can be the default shared memory set during the configuration process, or it can be the shared data created by other user modes. The shared data area actually used by each language interpreter can be configured through system calls, so that the Wasm bytecode and execution results can be separated and do not affect each other.
[0082] In this scenario, Figure 4 In the example, thread 1 and thread 2 actively call the same Wasm virtual machine:
[0083] When thread 1 and thread 2 actively access the same Wasm virtual machine, the Wasm executes the same associated Wasm bytecode, and writes the execution result of the active call event corresponding to thread 1 into shared data area 1, and writes the execution result of the active call event corresponding to thread 2 into shared data area 2, so that the same Wasm bytecode can be reused; and the Wasm bytecode and the execution result are separated, so that the Wasm bytecode and the execution result do not affect each other during the reuse process. Among them, the red arrow represents the data flow; the black arrow represents the control flow.
[0084] It should be noted that active calling refers to the target application running in user mode actively calling the services or functions provided by the microkernel, including access to different shared data areas in shared memory. In kernel mode, for different threads in the target application running in user mode, the same Wasm virtual machine embedded in the microkernel is controlled to execute the same Wasm bytecode, and the execution results corresponding to different threads are written into different shared data areas. The disclosed embodiment accesses the microkernel through active calling, and can dynamically inject the code (i.e., Wasm bytecode) that the user needs to expand and execute in the microkernel. This dynamic injection method avoids the need to recompile the microkernel every time, thereby improving development efficiency and the scalability of the microkernel.
[0085] The following uses the interrupt trigger point as an example to illustrate how to improve the processing efficiency of the microkernel system:
[0086] For the interrupt trigger point, take the interrupt event driven by the bus (CAN) as an example. The read and write data operations in the interrupt event are placed in the microkernel and executed by the Wasm virtual machine, and the data is copied to the shared data area. After the Wasm virtual machine completes the interrupt event processing (such as reading data), the execution result (i.e., the read data) is written to the shared data area, and the Wasm virtual machine no longer sends notification messages to the CAN driver. The CAN driver can read the data stored in the shared data area as needed.
[0087] Among them, in the related technology, the microkernel interrupt process starts from entering the microkernel interrupt processing and ends after the user mode resumes the interrupt, including the IPC notification time, system context switching and the processing time of the CAN-driven interrupt event.
[0088] The interrupt process of the Wasm virtual machine used in the embodiment of the present disclosure starts from the time when the microkernel interrupt processing is entered, and the time until the microkernel interrupt processing ends includes the time when Wasm is ready to run and the processing time of the interrupt event driven by CAN.
[0089] It can be seen that in a single interrupt event driven by CAN, due to the reduction of IPC notification time and context switching, the use of the microkernel Wasm virtual machine saves more communication time compared to the Wasm virtual machine in related technologies, thereby improving the processing efficiency of the microkernel system. Taking this as an example, in other scenarios with frequent interrupt events, it will also bring considerable performance advantages to the microkernel system.
[0090] In one example, after writing the execution result of the triggering event into the shared data area, the method further includes:
[0091] A notification message is generated for a triggering event; the notification message is sent so that after switching to the user mode, the execution result is read from the shared data area according to the notification message in the user mode.
[0092] In the disclosed embodiment, after obtaining the execution result of the triggering event, a notification message is generally sent to the thread in user mode for the triggering event, so that after switching to the user mode, the execution result can be read from the shared data area in the user mode according to the notification message.
[0093] In one example, for a scenario where a triggering event is detected multiple times, generating a notification message for the triggering event includes: generating notification messages for each of the multiple triggering events.
[0094] Continuing from the above, for scenarios where trigger events are detected multiple times, after obtaining the execution result of the trigger event, a notification message is generally sent to the user mode thread for each trigger event; in the disclosed embodiment, the processing of the notification message originally in the user mode can be moved to the microkernel for processing, and the notification message of each trigger event is processed in the kernel mode before sending the notification message to the user mode thread.
[0095] If there are multiple triggering events, multiple notification messages are generated (one notification message is generated for each triggering event). As one achievable method, the above processing of notification messages for each triggering event may include:
[0096] Merge multiple notification messages generated into one target notification message.
[0097] The disclosed embodiment determines the target notification message based on the notification message corresponding to each trigger event among multiple trigger events, and the number of target notification messages is less than the number of notification messages corresponding to each trigger event among the multiple trigger events. Compared with sending a notification message to the user mode for each trigger event respectively, only the target notification message is sent to the user mode. In this way, there is no need to switch between the user mode and the kernel mode for the notification message corresponding to each trigger event, thereby improving the processing efficiency of the microkernel system.
[0098] In one example, the target notification message is a notification message.
[0099] Continuing from the above, in the embodiment of the present disclosure, when seL4 processes an interrupt event, seL4 sends a notification message to the user mode. Before seL4 sends the notification message to the user mode, the Wasm virtual machine pre-processes the notification message in advance, such as merging the notification messages of each interrupt event into a target notification message, so that there is no need to switch between the user mode and the kernel mode for the notification message corresponding to each trigger event, thereby improving the processing efficiency of the microkernel system.
[0100] exist Figure 5 In, the interrupt event is taken as an example;
[0101] When the interrupt trigger request (IRQ) corresponding to each interrupt event is detected during the running of the target application, the notification messages corresponding to each interrupt trigger request are merged into a target notification message, so that only one target notification message is sent to the user mode. In this way, there is no need to switch between the user mode and the kernel mode for the notification message corresponding to each trigger event, thereby improving the processing efficiency of the microkernel system.
[0102] If there are multiple triggering events, multiple notification messages are generated. As one of the implementable ways, the above-mentioned processing of notification messages of each triggering event may include:
[0103] Part of the notification messages not required by the user mode are removed from the generated multiple notification messages to obtain the target notification message.
[0104] In the disclosed embodiment, messages in the gateway message that are eliminated in the user mode are moved to the microkernel, and the user mode eliminates some notification messages from the notification messages corresponding to each trigger event in multiple trigger events to obtain the target notification messages. In this way, there is no need to switch to the user mode, and the user mode eliminates the part of the messages, thereby improving the processing efficiency of the microkernel system.
[0105] exist Figure 6 In, the interrupt event is taken as an example;
[0106] When the interrupt trigger requests corresponding to each interrupt event are detected during the running of the target application, the two yellow interrupt trigger requests are eliminated, and only the notification messages corresponding to the interrupt trigger requests corresponding to the green part are sent to the user mode. In this way, there is no need to switch between the user mode and the kernel mode for the notification messages corresponding to the yellow interrupt events, thereby improving the processing efficiency of the microkernel system.
[0107] The following uses IPC trigger points as examples to illustrate how to improve the processing efficiency of the microkernel system:
[0108] exist Figure 7 In the figure, the driver thread (DRV) is the process in the driver program; the thread is the smallest unit that can perform operation scheduling in the operating system (i.e., the microkernel system); thread 1 and thread 2 are threads running in the user mode of the target application, which are included in the process and are the actual operating units in the process; the Wasm virtual machine is embedded in the microkernel.
[0109] When the target application in user mode needs to perform low-level operations such as input and output, and apply for memory, it must call the Application Programming Interface (API) function provided by the operating system to enter kernel mode; after the operation is completed, it switches back to user mode.
[0110] exist Figure 7 (a) shows a traditional implementation method, where thread 1, thread 2 and driver thread correspond to thread 1, thread 2 and driver thread in user mode respectively.
[0111] The following uses thread 1, thread 2, and the driver thread as an example to perform input and output operations corresponding to IPC events with the microkernel. The communication process between thread 1, thread 2, and the driver thread and the microkernel includes:
[0112] (1) Before thread 2 performs output operations to the microkernel, it switches from user mode to kernel mode (corresponding to Figure 7 The “1” shown in (a) in the figure);
[0113] (2) After the microkernel processes the output operation of thread 2, the microkernel performs an input operation on the driver thread. Before performing the input operation, it switches from kernel mode to user mode (corresponding to Figure 7 "2" shown in (a));
[0114] (3) After the driver thread processes the input operation, before the driver thread performs the output operation to the microkernel, it switches from user mode to kernel mode (corresponding to Figure 7 "3" shown in (a));
[0115] (4) After the microkernel processes the output operation of the driver thread, before the microkernel performs the input operation on thread 1, it switches from kernel mode to user mode (corresponding to Figure 7 "4" shown in (a));
[0116] (5) After thread 1 processes the output operation of the microkernel, before thread 1 performs the output operation to the microkernel, it switches from user mode to kernel mode (corresponding to Figure 7 "5" shown in (a));
[0117] (6) After the microkernel processes the output operation of thread 1, before the microkernel performs the input operation on the driver thread, it switches from kernel mode to user mode (corresponding to Figure 7 "6" shown in (a));
[0118] (7) After the driver thread processes the input operation of the microkernel in (6), before the driver thread performs the output operation to the microkernel, it switches from user mode to kernel mode (corresponding to Figure 7 "7" shown in (a));
[0119] (8) The microkernel processes the output operation of the driver thread in (7) and performs an input operation on thread 2. Before performing the input operation, it switches from kernel mode to user mode (corresponding to Figure 7 The “8” shown in (a)).
[0120] The above process shows that a total of 8 switches between user mode and kernel mode were experienced.
[0121] The following is for Figure 7 (b) is used to illustrate: Figure 7 (b) shows the method provided by the embodiment of the present disclosure, which moves the operations performed by the driving thread (such as input and output operations) to the microkernel and is executed by the Wasm virtual machine embedded in the microkernel, so that the communication between threads and mode switching can be reduced from the original 8 times to 4 times.
[0122] In the above process, part of the code corresponding to the driver thread or all of the code is used as the function code input into the microkernel. The user mode moves the operation of the driver thread to the microkernel through the IPC event, reducing 4 switches between the kernel mode and the user mode, thereby improving the processing efficiency of the microkernel system.
[0123] In an embodiment of the present disclosure, before executing step 103, the method further includes: configuring the Wasm virtual machine properties through a configuration interface, such as configuring the data structure of the Wasm virtual machine, wherein the data structure of the Wasm virtual machine includes a shared data area base address, a shared code area base address, a shared data area length, and a shared code area length.
[0124] In one example, in Figure 8 , configuring the properties of the Wasm virtual machine object includes the following steps:
[0125] (1) Obtain a parameter list, which is a list of parameters related to the attributes of the virtual object; (2) Configure the structure of the Wasm virtual machine object; (3) Load the Wasm bytecode and instantiate the shared code area object.
[0126] The disclosed embodiment may also clear the properties of the Wasm virtual machine object through an interface for deconfiguration.
[0127] Among them, Fig. 9 In the configuration interface, clearing the properties of the Wasm virtual machine object includes the following steps:
[0128] (1) Destroy the Wasm virtual machine instance and unload the instantiated shared code area object; (2) Configure the structure of the Wasm virtual machine object.
[0129] Continuing from the above, the Wasm virtual machine integrated into SeL4 is used in the disclosed embodiment. The Wasm virtual machine is abstracted as an object in the microkernel, and the user mode's access to the Wasm virtual machine is controlled by the power to ensure the security of the microkernel. Any user mode that obtains the permission can use the Wasm virtual machine object. To this end, it is necessary to add a Wasm virtual machine object in the microkernel system.
[0130] Here, the association between the trigger event and the Wasm bytecode is set, including: creating a Wasm virtual machine object for the target application, configuring the Wasm virtual machine object using the information of the shared code area and the shared data area; and setting the association between the trigger event and the Wasm virtual machine object capability.
[0131] Among them, setting the association between the trigger event and the Wasm virtual machine object capability may refer to setting the association between different types of trigger points and the Wasm virtual machine object capability.
[0132] For example, configure the association between the interrupt trigger point and the specified Wasm virtual machine object capability.
[0133] For example, an association is set between an IPC trigger point and a specified Wasm virtual machine object capability.
[0134] Correspondingly, in this example, the embodiment of the present disclosure may also clear the association between the interrupt trigger point and the specified Wasm virtual machine object capability.
[0135] Correspondingly, in this example, the embodiment of the present disclosure may also clear the association between the IPC trigger point and the specified Wasm virtual machine object capability.
[0136] Fig.10 FIG. 1 is a flowchart of a microkernel-based event processing method provided by an embodiment of the present disclosure. Fig.10 Taking the interrupt trigger point as an example, the event processing method based on the microkernel of the embodiment of the present disclosure may include:
[0137] Step 1001: When the microkernel is initialized, the operating environment of the Wasm virtual machine is initialized (ie, the operating environment for running the Wasm virtual machine is initialized).
[0138] Step 1003: Create a shared code area for the target application, and write the codes corresponding to some functions in the target application into the shared code area.
[0139] Step 1005: Create a shared data area for the target application.
[0140] Step 1007: Create a Wasm virtual machine object and obtain its capabilities.
[0141] In the disclosed embodiment, the shared memory capability is configured to the Wasm virtual machine object by using the system call. The configuration process will fall into the microkernel, in which the configuration variables are stored, the Wasm bytecode in the shared code area is loaded, and the instantiation of the Wasm bytecode is completed after the verification is successful.
[0142] Step 1009: Configure the properties of the Wasm virtual machine, such as the base address and length of the shared code area, and the base address and length of the shared data area.
[0143] Step 1011: Configure the properties of the trigger point, such as the type of trigger point, such as an interrupt trigger point.
[0144] Step 1013: The Wasm virtual machine loads and instantiates the shared code area object.
[0145] Step 1015: The user mode sets trigger events corresponding to different types of trigger points, such as setting a Wasm interrupt trigger point.
[0146] Step 1017: The user mode waits for the notification message of the interrupt report.
[0147] Step 1019: When the interrupt hardware trigger is configured (i.e., the interrupt trigger point is detected), the microkernel triggers the Wasm virtual machine to execute the associated instantiated shared code area object.
[0148] Step 1021: The Wasm virtual machine executes and instantiates the shared code area object, and writes the execution result into the shared data area. The Wasm virtual machine completes the execution and exits.
[0149] Step 1023: The microkernel notifies the execution result in the shared data area to be read in user mode.
[0150] Combination of the above Figure 1 , describes in detail the method embodiment of the present disclosure, and the following is combined with Fig.11 , describes the device embodiment of the present disclosure in detail. It should be understood that the description of the method embodiment corresponds to the description of the device embodiment, so the parts not described in detail can refer to the previous method embodiment.
[0151] Fig.11 FIG. 1 is a schematic diagram of the structure of a microkernel-based event processing device provided by an embodiment of the present disclosure. Fig.11 As shown, the event processing device based on the microkernel provided by the embodiment of the present disclosure has a virtual machine embedded in the microkernel, and the device includes a code acquisition module 1101 and a control module 1102. Among them, the acquisition module 1101 is configured as an acquisition module, which is configured to switch from the user mode to the kernel mode in response to detecting a preset trigger event in the process of running the target application in the user mode, and acquire the bytecode associated with the trigger event; the bytecode is the function code pre-written into the shared code area in the user mode, and the function code is the code corresponding to some functions in the target application; the control module 1102 is configured to control the virtual machine to execute the bytecode, and write the execution result of the trigger event into the shared data area, so as to read the execution result from the shared data area in the user mode after switching to the user mode.
[0152] In some embodiments, after writing the execution result of the triggering event into the shared data area, the apparatus further comprises:
[0153] A message generation module (not shown in the figure), configured to generate a notification message for a triggering event;
[0154] The message notification module (not shown in the figure) is configured to send a notification message so that after switching to the user mode, the execution result is read from the shared data area according to the notification message in the user mode.
[0155] In some embodiments, there are multiple triggering events, and the message generation module is further configured to generate notification messages respectively for the multiple triggering events.
[0156] In some embodiments, the message notification module is further configured to:
[0157] Merging the generated multiple notification messages into a target notification message, and / or removing some notification messages not required by the user mode from the generated multiple notification messages to obtain the target notification message;
[0158] Send a target notification message.
[0159] In some embodiments, the triggering event includes: an interrupt event, an inter-process communication (IPC) event, or an active call event.
[0160] In some embodiments, if the triggering event is an active call event, and the active call event is an active call event corresponding to different threads in the target application;
[0161] The control module 1102 is further configured to:
[0162] For different threads, the same virtual machine is controlled to execute bytecodes respectively, and the execution results of active triggering events corresponding to different threads are written into different shared data areas respectively.
[0163] In some embodiments, if the triggering event is an IPC event, the function code includes the code corresponding to the driving thread portion or all of the code.
[0164] In some embodiments, the interrupt event comprises an interrupt event driven by CAN.
[0165] In some embodiments, before responding to detecting a preset trigger event during the process of running the target application in the user mode, the device further includes:
[0166] A loading module (not shown in the figure), configured to load bytecodes from the shared code area;
[0167] The setting module (not shown in the figure) is configured to set the association between the trigger event and the bytecode.
[0168] In some embodiments, the loading module is further configured to:
[0169] Verify the bytecode;
[0170] If the bytecode verification is successful, the bytecode is loaded from the shared code area.
[0171] In some embodiments, verifying the bytecode includes verifying whether the bytecode complies with at least one of the following:
[0172] Use native function interfaces to access shared code areas;
[0173] It is forbidden to use function pointers to access functions;
[0174] It is forbidden to inject dead loop code;
[0175] Limit the number of injected code lines.
[0176] In some embodiments, before responding to detecting a preset trigger event during the process of running the target application in the user mode, the device further includes:
[0177] An initialization module (not shown in the figure), configured to initialize the operating environment of the virtual machine;
[0178] A creation module (not shown in the figure) is configured to create a shared code area for the target application and write codes corresponding to some functions in the target application into the shared code area;
[0179] Create a shared data area for the target application.
[0180] In some embodiments, the setting module is further configured to:
[0181] Creating a virtual machine object for a target application, and configuring the virtual machine object using information of a shared code area and a shared data area;
[0182] Set the association between the trigger event and the virtual machine object.
[0183] In some embodiments, the bytecode is Wasm bytecode, Lua bytecode, or eBPF bytecode.
[0184] Below, reference Fig.12 An electronic device according to an embodiment of the present disclosure is described. Fig.12 Shown is a schematic structural diagram of an electronic device provided by an embodiment of the present disclosure.
[0185] like Fig.12As shown, the electronic device includes one or more processors 1201 and a memory 1202 .
[0186] The processor 1201 may be a central processing unit (CPU) or other forms of processing units having data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device to perform desired functions.
[0187] The memory 1202 may include one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. Volatile memory may include, for example, random access memory (RAM) and / or cache memory (cache), etc. Non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 1201 may run the program instructions to implement the functions of the microkernel-based event processing method and other embodiments of the present disclosure mentioned above. Various contents such as bytecodes may also be stored in the computer-readable storage medium.
[0188] In one example, the electronic device may further include: an input device 1203 and an output device 1204 , and these components are interconnected via a bus system and / or other forms of connection mechanisms (not shown).
[0189] The input device 1203 may include, for example, a keyboard, a mouse, etc.
[0190] The output device 1204 can output various information to the outside, including execution results, etc. The output device 1204 can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.
[0191] Of course, to simplify, Fig.12 Only some of the components related to the present disclosure in the electronic device are shown, and components such as a bus, an input / output interface, etc. are omitted. In addition, according to specific application situations, the electronic device may further include any other appropriate components.
[0192] In addition to the above-mentioned methods and devices, an embodiment of the present disclosure may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to execute the steps of the microkernel-based event processing method according to various embodiments of the present disclosure described above in this specification.
[0193] The computer program product may be written in any combination of one or more programming languages to write program codes for performing the operations of the disclosed embodiments, including object-oriented programming languages such as Java, C++, etc., and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user electronic device, partially on the user device, as a separate software package, partially on the user electronic device and partially on a remote electronic device, or entirely on a remote electronic device or server.
[0194] In addition, an embodiment of the present disclosure may also be a computer-readable storage medium having computer program instructions stored thereon. When the computer program instructions are executed by a processor, the processor executes the steps of the microkernel-based event processing method according to various embodiments of the present disclosure described above in this specification.
[0195] Computer readable storage media can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can include, for example, but is not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0196] The basic principles of the present disclosure are described above in conjunction with specific embodiments. However, it should be noted that the advantages, strengths, effects, etc. mentioned in the present disclosure are only examples and not limitations, and it cannot be considered that these advantages, strengths, effects, etc. are required by each embodiment of the present disclosure. In addition, the specific details disclosed above are only for the purpose of illustration and ease of understanding, and are not limitations. The above details do not limit the present disclosure to the necessity of adopting the above specific details to be implemented.
[0197] The block diagrams of the devices, apparatuses, equipment, and systems involved in this disclosure are only illustrative examples and are not intended to require or imply that they must be connected, arranged, and configured in the manner shown in the block diagrams. As will be appreciated by those skilled in the art, these devices, apparatuses, equipment, and systems can be connected, arranged, and configured in any manner. Words such as "including," "comprising," "having," and the like are open words, referring to "including but not limited to," and can be used interchangeably therewith. The words "or" and "and" used herein refer to the words "and / or," and can be used interchangeably therewith, unless the context clearly indicates otherwise. The word "such as" used herein refers to the phrase "such as but not limited to," and can be used interchangeably therewith.
[0198] It should also be noted that in the apparatus, device and method of the present disclosure, each component or each step can be decomposed and / or recombined. Such decomposition and / or recombination should be regarded as equivalent solutions of the present disclosure.
[0199] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use the present disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein may be applied to other aspects without departing from the scope of the present disclosure. Therefore, the present disclosure is not intended to be limited to the aspects shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
[0200] The above description has been given for the purpose of illustration and description. In addition, this description is not intended to limit the embodiments of the present disclosure to the forms disclosed herein. Although multiple example aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, changes, additions and sub-combinations thereof.
[0201] The above description is only a preferred embodiment of the present disclosure and is not intended to limit the present disclosure. Any modifications, equivalent substitutions, etc. made within the spirit and principles of the present disclosure should be included in the protection scope of the present disclosure.
Claims
1. A microkernel-based event processing method, characterized in that: The microkernel is embedded with a virtual machine, and the method comprises: In response to detecting a preset triggering event during the process of running the target application in the user mode, switching from the user mode to the kernel mode, and acquiring a bytecode associated with the triggering event; the bytecode is a function code pre-written into the shared code area in the user mode, and the function code is a code corresponding to a part of the functions in the target application; The control virtual machine executes the bytecode and writes the execution result of the triggering event into the shared data area, so that after switching to the user mode, the execution result can be read from the shared data area in the user mode.
2. The method according to claim 1, wherein: After writing the execution result of the trigger event into the shared data area, the method further includes: generating a notification message for the triggering event; The notification message is sent so that after switching to the user mode, the execution result is read from the shared data area according to the notification message in the user mode.
3. The method according to claim 2, characterized in that There are multiple triggering events, and generating notification messages for the triggering events includes: generating notification messages for the multiple triggering events respectively.
4. The method according to claim 3, characterized in that The sending of the notification message comprises: Merging the generated multiple notification messages into a target notification message, and / or removing some notification messages not required by the user mode from the generated multiple notification messages to obtain the target notification message; The target notification message is sent.
5. The method according to claim 1, characterized in that: The triggering event includes: an interruption event, an inter-process communication (IPC) event or an active call event.
6. The method according to claim 5, wherein: The triggering event is an active calling event corresponding to different threads in the target application program; The control virtual machine executes the bytecode and writes the execution result of the triggering event into the shared data area, including: For different threads, the same virtual machine is controlled to execute the bytecode respectively, and the execution results of the active triggering events corresponding to the different threads are written into different shared data areas respectively.
7. The method according to claim 5, characterized in that If the trigger event is an IPC event, the function code includes the code corresponding to the driving thread part or all of the code.
8. The method according to claim 5, characterized in that The interrupt event includes an interrupt event driven by CAN.
9. The method according to claim 1, characterized in that: Before responding to detecting a preset trigger event during the process of running the target application in the user mode, the method further includes: Loading the bytecode from the shared code area; An association between the trigger event and the bytecode is set.
10. The method according to claim 9, characterized in that The loading of the bytecode from the shared code area includes: Verifying the bytecode; If the bytecode is successfully verified, the bytecode is loaded from the shared code area.
11. The method according to claim 10, characterized in that Verifying the bytecode includes verifying whether the bytecode complies with at least one of the following: Use native function interfaces to access shared code areas; It is forbidden to use function pointers to access functions; It is forbidden to inject dead loop code; Limit the number of injected code lines.
12. The method according to any one of claims 1 to 11, characterized in that Before responding to detecting a preset trigger event during the process of running the target application in the user mode, the method further includes: Initializing the operating environment of the virtual machine; Creating a shared code area for the target application, and writing codes corresponding to some functions in the target application into the shared code area; A shared data area is created for the target application.
13. The method according to claim 9, characterized in that The setting of the association between the trigger event and the bytecode includes: Creating a virtual machine object for the target application, and configuring the virtual machine object using information of the shared code area and the shared data area; An association is set between the trigger event and the virtual machine object.
14. The method according to claim 1, characterized in that The bytecode is Wasm bytecode, Lua bytecode or eBPF bytecode.
15. An event processing device based on a microkernel, characterized in that: The microkernel is embedded with a virtual machine, and the device comprises: an acquisition module, configured to, in response to detecting a preset trigger event during the process of running the target application in the user mode, switch from the user mode to the kernel mode, and acquire a bytecode associated with the trigger event; the bytecode is a function code pre-written into the shared code area in the user mode, and the function code is a code corresponding to a part of the functions in the target application; The control module is configured to control the virtual machine to execute the bytecode and write the execution result of the triggering event into the shared data area, so that after switching to the user mode, the execution result can be read from the shared data area in the user mode.
16. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 14 are implemented.
17. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 14 are implemented.
18. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 14 are implemented.