Equipment control method and device, computer equipment and storage medium

By listening to the preset data structures defined by the kernel-mode program through the user-mode program and dynamically implementing the reading and writing of device registers using callback functions, the high cost problem caused by modifying device drivers in the existing technology is solved, and the flexibility and efficiency of device control are achieved.

CN120849156APending Publication Date: 2025-10-28PHYTIUM TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510983216.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-16
Publication Date
2025-10-28

AI Technical Summary

Technical Problem

Existing technologies require modification of device drivers to dynamically read and write device registers when controlling external devices in user mode, resulting in high debugging costs in emulator environments or environments with low operating frequencies.

Method used

By monitoring the preset data structures defined by the kernel-mode program through user-mode programs, and using callback functions to dynamically implement read and write operations on device registers, the device driver can be avoided.

Benefits of technology

It reduces the debugging cost of device drivers and enables the flexibility and efficiency of dynamically controlling external devices in user space.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120849156A_ABST
    Figure CN120849156A_ABST
Patent Text Reader

Abstract

The invention provides an equipment control method and device, computer equipment and a storage medium, and the method comprises the steps: monitoring whether key value information of an equipment register of to-be-controlled equipment is written into a preset data structure defined by a kernel mode program or not by adopting a user mode program when an equipment drive program corresponding to the to-be-controlled equipment runs, and if the key value information of the device register exists in the preset data structure, reading the key value information of the device register by adopting a callback function defined by the kernel mode program, and executing read-write operation on the device register by adopting the callback function according to the key value information of the device register so as to control the to-be-controlled device. Once the user mode program monitors that the key value information exists in the preset data structure, the callback function defined by the kernel mode program completes reading and writing of the device register, and therefore the purposes that the device drive program does not need to be modified, register reading and writing are dynamically achieved, and the debugging cost is reduced are achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and more specifically, to a device control method, apparatus, computer device, and storage medium. Background Art

[0002] For systems that include external devices, the external devices are usually controlled through device drivers, but there are some scenarios where it is necessary to control the external devices directly in user space.

[0003] In related technologies, the kernel-based Debugfs mechanism is commonly used. Debugfs creates virtual files in the kernel's virtual file system and defines write interfaces in the device driver to implement user-defined functions, such as user-mode programs writing values ​​to the corresponding device register space to control external devices. However, this process is static; the Debugfs mechanism cannot dynamically read or write device registers at specific locations during device driver execution. To dynamically read or write device registers at specific locations during device driver execution, the device driver needs to be directly modified to read and write device registers at the execution location.

[0004] However, the above methods of modifying device drivers require recompiling and loading the device drivers. In emulator environments or environments with low operating frequencies, the device driver debugging cycle is long and the cost is high. Summary of the Invention

[0005] In view of this, embodiments of this application provide a device control method, apparatus, computer device, and storage medium to achieve the goal of dynamically reading and writing registers at specified function locations in the device driver without modifying the device driver, thereby reducing debugging costs.

[0006] In a first aspect, embodiments of this application provide a device control method applied to the operating system of a computer device, comprising: When the device driver corresponding to the device to be controlled is running, a user-mode program listens to whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program. If the key value information of the device register exists in the preset data structure, the key value information of the device register is read using the callback function defined by the kernel program; The callback function is used to perform read and write operations on the device register based on the key value information of the device register, so as to control the device to be controlled.

[0007] In an optional implementation, reading the key-value information of the device register using the callback function defined by the kernel-mode program includes: The callback function is used to write the key value information of the device register into a preset virtual file, and to read the key value information of the device register from the preset virtual file.

[0008] In an optional implementation, the step of using the callback function to write the key-value information of the device register into a preset virtual file includes: The callback function is used to write the key value information of the device register into the preset virtual file according to the write format corresponding to the write interface of the preset virtual file.

[0009] In an optional implementation, before the step of using a user-mode program to monitor whether the key value information of the device register of the device to be controlled is written into a preset data structure defined by a kernel-mode program, the method further includes: If the device driver runs to a specified function, it jumps to the entry hook function defined by the kernel program, and uses the entry hook function to write the key value information of the device register into the preset data structure.

[0010] In an optional implementation, before the step of using a user-mode program to monitor whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program during the execution of the device driver corresponding to the device to be controlled, the method further includes: A framework for building a pre-defined kernel mechanism is provided, wherein the framework is used to define an interface for reading / writing registers, and the interface is part of the device driver. Based on the interface for reading / writing registers, compile and obtain the device driver, and obtain the binary file; Load and run the binary file.

[0011] In an optional implementation, the method further includes: Jump to the exit hook function defined in the kernel-mode program, and use the exit hook function to delete the key value information of the device register from the preset data structure.

[0012] In an optional implementation, the method further includes: After the main program of the specified function is executed, the exit hook function is used to check whether the key value information of the device register exists in the preset data structure; If the key value information of the device register does not exist in the preset data structure, it is determined that the value in the key value information of the device register has been written into the device register.

[0013] In an optional implementation, the step of using the callback function to perform read / write operations on the device register based on the key value information of the device register includes: The callback function is used to perform a read operation on the device register, and a write operation is performed on the device register according to the key value information of the device register; or The callback function is used to perform a write operation on the device register and a read operation on the device register based on the key value information of the device register.

[0014] Secondly, embodiments of this application also provide a device control apparatus, including: The monitoring module is used to monitor, in user mode, whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel mode program when the device driver corresponding to the device to be controlled is running. The read / write module is used to read the key value information of the device register using a callback function defined by the kernel-mode program if the key value information of the device register exists in the preset data structure. The read / write module is further configured to use the callback function to perform read / write operations on the device register according to the key value information of the device register, so as to control the device to be controlled.

[0015] Thirdly, embodiments of this application also provide a computer device, including: a processor, a memory, and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor communicates with the memory via the bus, and the processor executes the machine-readable instructions to perform the method described in any of the first aspects.

[0016] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the method described in any of the first aspects.

[0017] This application provides a device control method, apparatus, computer device, and storage medium. The method includes: when the device driver corresponding to the device to be controlled is running, a user-mode program monitors whether key-value information of the device register of the device to be controlled is written into a preset data structure defined by a kernel-mode program. If the key-value information of the device register exists in the preset data structure, a callback function defined by the kernel-mode program reads the key-value information of the device register. The callback function then performs read / write operations on the device register based on the key-value information to control the device to be controlled. Once the user-mode program detects the existence of key-value information in the preset data structure, the callback function defined by the kernel-mode program completes the read / write operation of the device register, thereby achieving dynamic read / write of registers without modifying the device driver, thus reducing debugging costs. Attached Figure Description

[0018] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0019] Figure 1 Flowchart of the device control method provided in the embodiments of this application Figure 1 ; Figure 2 Flowchart of the device control method provided in the embodiments of this application Figure 2 ; Figure 3 Flowchart of the device control method provided in the embodiments of this application Figure 3 ; Figure 4 Flowchart of the device control method provided in the embodiments of this application Figure 4 ; Figure 5 A schematic diagram illustrating a specific device control process provided in an embodiment of this application; Figure 6 This is a schematic diagram of the structure of the device control apparatus provided in the embodiments of this application; Figure 7 A schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation

[0020] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of the embodiments of this application described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.

[0021] When directly controlling external devices in user space (such as resetting a device), the conventional approach is based on the kernel's Debugfs mechanism. Debugfs creates virtual files in the kernel's virtual file system, and users interact with device drivers through the read and write interfaces of these virtual files. Debugfs is a kernel feature that, when enabled, creates corresponding virtual files in the appropriate directories. Whether a virtual file is readable or writable depends on whether its read / write interface is defined in the device driver. If the virtual file is writable, the write interface defined in the device driver can implement user-defined functions, such as writing values ​​to the corresponding device register space, thereby controlling the device. The external devices mentioned above refer to devices such as network cards and graphics cards. These devices have corresponding device drivers, and all device drivers control device behavior by reading and writing to the device's register space. For example, if a register in the device's register space is used to enable a clock or a counter, the device driver writes a value (e.g., writes 1) to this register to enable the clock or start the counter.

[0022] However, the problem with the above process is that the behavior of reading and writing device registers is static. The Debugfs mechanism cannot dynamically read and write device registers at specific locations during device driver execution. To dynamically read and write device registers at specific locations during device driver execution, the device driver needs to be directly modified to read and write device registers at the execution location. However, this method requires recompiling and loading the device driver. In emulator environments or environments with low operating frequency, the debugging cycle of a single device driver is long, resulting in high costs.

[0023] Based on this, once the EDPF user-mode program detects key-value information in a preset data structure, a callback function defined by the EDPF kernel-mode program completes the reading and writing of device registers. This achieves the goal of dynamically reading and writing registers without modifying the device driver, thereby reducing debugging costs. It should be noted that the user-mode programs mentioned in the following embodiments are EBPF user-mode programs, and the kernel-mode programs mentioned in the following embodiments are EDPF kernel-mode programs. Figure 1 Flowchart of the device control method provided in the embodiments of this application Figure 1 In this embodiment, the executing entity can be a computer device, and the method is applied to the operating system of the computer device.

[0024] like Figure 1 As shown, the method may include: S101. When the device driver corresponding to the device to be controlled is running, a user-mode program listens to whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program.

[0025] The type of device to be controlled can include external devices such as Peripheral Component Interconnect (PCI) devices and Peripheral Component Interconnect Express (PCIe) devices, such as network cards and graphics cards. The device driver corresponding to the device to be controlled refers to the device driver of the device to be controlled in kernel mode.

[0026] Kernel-mode programs are programs written using the Extended Berkeley Packet Filter (ebpf) syntax that run in the operating system's kernel mode. User-mode programs are programs written using the ebpf syntax that run in the operating system's user mode. It's important to note that ebpf technology evolved from the BPF mechanism. While BPF was initially primarily used for filtering network packets, ebpf is no longer limited to this. It can be used to add probes to the kernel for problem tracking and analysis, and to add new kernel functionality without modifying the kernel source code.

[0027] When the device driver for the device to be controlled is running, a user-mode program monitors whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program. Specifically, when device control is required, the key value information of the device register of the device to be controlled can be written into the preset data structure. Therefore, when the device driver is running, a user-mode program can monitor whether the key value information of the device register of the device to be controlled is written into the preset data structure.

[0028] The preset data structure can be the ebpf mapping table defined by the device driver. The key-value information can be a key-value pair consisting of device register information (such as number, address, etc.) and the value to be written to the device register, that is, <register, value>. The value to be written to the device register is used to characterize the action for the device to be controlled. For example, 0 means stop counting and 1 means enable counting.

[0029] S102. If the key value information of the device register exists in the preset data structure, the key value information of the device register is read using the callback function defined by the kernel program.

[0030] S103. A callback function is used to perform read and write operations on the device register based on the key value information of the device register in order to control the device to be controlled.

[0031] If the preset data structure contains key-value information for the device register, the callback function defined by the kernel program is used to read the key-value information of the device register, and the callback function is used to perform read and write operations on the device register according to the value in the key-value information in order to control the device to be controlled.

[0032] In an optional implementation, step S103 above, which uses a callback function to perform read / write operations on the device register based on the key value information of the device register, includes: A callback function is used to perform a read operation on the device register, and then a write operation is performed on the device register based on the key value information; or, A callback function is used to perform write operations on the device register and read operations on the device register based on the key value information in the device register.

[0033] It is worth noting that, depending on the specific application scenario, reading and writing operations on the device register can be performed by using a callback function to read the device register and then writing to the device register based on the key value information. For example, if the value in the key value information is used to represent the counter enable (value is zero), then the already counted value in the device register can be read first, and then the value can be written to the device register (cleared) to start counting again.

[0034] Alternatively, a callback function can be used to perform write operations on the device register and read operations on the device register based on the key value information of the device register. For example, if the value in the key value information is used to represent reset, the value can be written to the device register first, and then the value of the device register can be read to confirm that the value has been written to the device register.

[0035] The reason for writing before reading is that the write operation may not take effect immediately. This is related to the speed of input / output (I / O), the congestion of the I / O bus, etc. Therefore, writing before reading can confirm that the written value meets expectations.

[0036] In this embodiment, once the user-mode program detects key-value information in the preset data structure, the callback function defined by the kernel-mode program completes the reading and writing of the device registers. This achieves the goal of dynamically reading and writing registers without modifying the device driver, controlling the device at the user-mode level by reading and writing device registers, thereby reducing debugging costs.

[0037] In an optional implementation, step S102 above, which involves reading the key value information of the device register using a callback function defined in the kernel-mode program, may include: A callback function is used to write the key value information of the device register to a preset virtual file, and to read the key value information of the device register from the preset virtual file.

[0038] The preset virtual file refers to the virtual file created in the kernel virtual file system by the kernel's Debugfs mechanism (i.e., the virtual file defined by Debugfs). A callback function is used to write the key value information of the device register into the preset virtual file and trigger the callback function to read the key value information of the device register from the preset virtual file.

[0039] The callback function refers to the write function when registering the preset virtual file. After the callback function is triggered, it obtains the key value information of the device register from the preset virtual file and writes the value to the corresponding device register.

[0040] In an optional implementation, writing the key-value information of the device register into a preset virtual file using a callback function may include: using a callback function to write the key-value information of the device register into the preset virtual file according to the write format corresponding to the write interface of the preset virtual file.

[0041] The write format corresponding to the write interface of the preset virtual file can be, for example, "value --> register encoding". The callback function is used to write the key value information of the device register into the preset virtual file in the format of "value --> register encoding" through the write interface.

[0042] Figure 2 Flowchart of the device control method provided in the embodiments of this application Figure 2 ,like Figure 2 As shown, in an optional implementation, before step S101, in which the user-mode program monitors whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program, the method may further include: S201. If the device driver runs to a specified function, it jumps to the entry hook function defined by the kernel program, and uses the entry hook function to write the key value information of the device register into a preset data structure.

[0043] If the device driver corresponding to the device to be controlled runs to the specified function (Target function), it will jump to the entry hook function defined in kernel mode, and use the entry hook function to write the key value information of the device register into the preset data structure.

[0044] In this embodiment, the specified function refers to the function location to be probed. By adding an entry hook function of ebpf at the function location to be probed, a probe point is added. At the probe point, the information and values ​​of the device registers to be read and written are added to the ebpf mapping table. Simultaneously, once the ebpf user-mode program detects the existence of key-value pairs in the mapping table, it writes the key-value information to the virtual file defined by Debugfs. The register read and write are completed by the callback function of the kernel-mode program, achieving the goal of dynamically reading and writing device registers at the specified function location without modifying the device driver.

[0045] In an alternative implementation, the method may further include: S202. Jump to the exit hook function defined by the kernel-mode program, so as to delete the key value information of the device register from the preset data structure using the exit hook function.

[0046] If the value read from the device register matches the expected value, it confirms that the value has been written to the device register. Then, it jumps to the exit hook function defined by the kernel program to delete the key value information of the device register from the preset data structure, so that when the kernel program executes the next specified function, the next device control flow can be executed.

[0047] It is worth noting that multiple key-value information can be written to the device register at one time into the preset data structure. Each key-value information corresponds to a specified function. Since there is a logical relationship between the calling order of different functions in the device driver, different values ​​can be combined and written to the device register in different orders according to the actual situation, thereby controlling the device to execute according to the desired control flow.

[0048] Figure 3Flowchart of the device control method provided in the embodiments of this application Figure 3 ,like Figure 3 As shown, in an optional implementation, the method may further include: S301. After the main program of the specified function is executed, the exit hook function is used to check whether the key value information of the device register exists in the preset data structure.

[0049] S302. If the key value information of the device register does not exist in the preset data structure, then it is determined that the value in the key value information of the device register has been written into the device register.

[0050] The device driver and the aforementioned listening process are executed in parallel. After the main program of the specified function is completed, before exiting the execution function, the program jumps to the exit hook function defined in the kernel mode. The exit hook function checks whether the key value information of the device register exists in the preset data structure. If the key value information of the device register does not exist in the preset data structure, it is determined that the value in the key value information of the device register has been written to the device register during the execution of the specified function. If the key value information of the device register exists in the preset data structure, it is determined that the value in the key value information of the device register has not yet been written to the device register.

[0051] In this embodiment, an exit hook function is used to check whether there is key value information of the device register in the preset data structure. This can confirm that the value in the key value information has been successfully written into the device register, thereby controlling the device to be controlled and improving the accuracy of device control.

[0052] Figure 4 Flowchart of the device control method provided in the embodiments of this application Figure 4 ,like Figure 4 As shown, in an optional implementation, before step S101, when the device driver corresponding to the device to be controlled is running and the user-mode program monitors whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program, the method may further include: S401. Build a framework for the preset kernel mechanism, whereby the framework is used to define the interface for reading / writing registers.

[0053] Among them, the default kernel mechanism can be the kernel's Debugfs mechanism. A framework based on the kernel's Debugfs mechanism is built, and an I / O interface (input / output interface) for reading / writing registers (reading registers or writing registers) is defined in this framework. This interface is referred to as A, which is part of the device driver. The device driver is compiled to obtain a binary file (B).

[0054] It should be noted that the A interface includes the I interface (write interface) and the O interface (read interface), where the write interface is the write interface of the aforementioned preset virtual file.

[0055] S402, based on the read / write register interface, compiles device drivers and obtains binary files.

[0056] S403, Load and run the binary file.

[0057] Based on the read / write register interface, the device driver is compiled and obtained into a binary file (B). After that, the device driver is not modified further. The binary file (B) is loaded and run. During runtime, a user-mode program monitors whether the key value information of the device register of the device to be controlled is written into a preset data structure defined by the kernel-mode program. The device driver mentioned in the above embodiments refers to the binary form of the device driver, that is, the binary file of the device driver.

[0058] In the binary file (B), the function to be probed is referred to as Target. In the kernel program (C), the entry hook function (hook-in) and exit hook function (hook-out) of the function Target are defined.

[0059] In some embodiments, prior to step S101 above, an ebpf environment can be set up, including tools such as the libbpf library, the bpftool tool, and the clang compiler. The libbpf library mainly provides a set of application programming interfaces (APIs) for ebpf developers to call. Developers can use these APIs to implement custom functions and logic. The bpftool tool is mainly used to load ebpf programs into the kernel and unload loaded programs. The clang compiler compiles ebpf programs written in C language into ebpf bytecode so that it can be inserted into the kernel.

[0060] Then, kernel-mode programs (C) and user-mode programs (D) can be written according to EBPF syntax, and compiled to obtain an executable program (E). The executable program (E) is loaded and run. The executable program (E) includes: the executable program corresponding to the user-mode program (D) and the executable program corresponding to the kernel-mode program (C). The user-mode program (D) and kernel-mode program (C) mentioned in the above embodiments refer to the executable program corresponding to the user-mode program (D) and the executable program corresponding to the kernel-mode program (C), respectively.

[0061] The solution provided in this application will be described below with reference to a specific embodiment. In this embodiment, the device to be controlled is an external device. Figure 5This is a schematic diagram illustrating a specific device control process provided in an embodiment of this application, such as... Figure 5 As shown, it includes the following steps: Step 1: When the device driver binary file (B) reaches the Target function, it jumps to the entry hook function (hook_in) defined by the kernel program (C) to add the key value information of the device register to the mapping table defined by the kernel program (C).

[0062] Step 2: The user-mode program (D) listens for key-value information in the mapping table. Step 3: The user-mode program (D) parses the key-value information and writes it into a preset virtual file according to the write format corresponding to the I interface (write interface) in the I / O interface (A). It then reads the key-value information of the device register from the preset virtual file based on the O interface (read interface) in the I / O interface (A) and writes the value in the key-value information into the device register of the external device (based on Debugfs write register).

[0063] Step 4: After the main program of the specified function is executed, jump to the exit hook function (hook_out) defined in the kernel mode program (C).

[0064] Step 5: Use the exit hook function (hook_out) to check whether the key-value information still exists in the mapping table to confirm that the key-value information has been successfully written to the device register.

[0065] In the above implementation process, it is desired to insert a function into the binary file (B) of the device driver, but it is not desired to modify the binary file (B). Therefore, a kernel-mode program (C) and a user-mode program (D) are required. The combination of the kernel-mode program (C) and the user-mode program (D) can achieve the following: when the binary file (B) of the device driver runs to a certain specific position, the device driver does not need to be modified, and a probe point is dynamically added at any specified function position to achieve the purpose of reading and writing registers.

[0066] Figure 6 This is a schematic diagram of the structure of the device control apparatus provided in the embodiments of this application. The apparatus can be integrated into a computer device.

[0067] like Figure 6 As shown, the device may include: The monitoring module 501 is used to monitor, in user mode, whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel mode program when the device driver corresponding to the device to be controlled is running. The read / write module 502 is used to read the key value information of the device register using a callback function defined by the kernel program if the key value information of the device register exists in the preset data structure. The read / write module 502 is also used to perform read / write operations on the device register based on the key value information of the device register using a callback function, so as to control the device to be controlled.

[0068] In an optional implementation, the read / write module 502 is specifically used for: A callback function is used to write the key value information of the device register to a preset virtual file, and to read the key value information of the device register from the preset virtual file.

[0069] In an optional implementation, the read / write module 502 is specifically used for: A callback function is used to write the key value information of the device register into the preset virtual file according to the write format corresponding to the write interface of the preset virtual file.

[0070] In an optional embodiment, the device further includes: The processing module 503 is used to jump to the entry hook function defined by the kernel program when the device driver runs to a specified function, so as to write the key value information of the device register into a preset data structure using the entry hook function.

[0071] In an optional implementation, the processing module 503 is further configured to: A framework for building a pre-defined kernel mechanism is established, in which the framework is used to define the interface for reading / writing registers, and the interface is part of the device driver; Compile device drivers and obtain binary files based on the read / write register interface; Load and run the binary file.

[0072] In an optional implementation, the processing module 503 is further configured to: Jump to the exit hook function defined in the kernel mode program to delete the key value information of the device register from the preset data structure using the exit hook function.

[0073] In an optional implementation, the processing module 503 is further configured to: After the main program of the specified function is executed, the exit hook function is used to check whether the key value information of the device register exists in the preset data structure; If the key value information of the device register does not exist in the preset data structure, it is determined that the value in the key value information of the device register has been written to the device register.

[0074] In an optional implementation, the read / write module 502 is specifically used for: A callback function is used to perform a read operation on the device register, and a write operation is performed on the device register based on the key value information of the device register; or A callback function is used to perform write operations on the device register and read operations on the device register based on the key value information in the device register.

[0075] The processing flow of each module in the device and the interaction flow between each module can be referred to the relevant descriptions in the above method embodiments, and will not be detailed here.

[0076] Figure 7 A schematic diagram of the structure of the computer device provided in the embodiments of this application, such as... Figure 7 As shown, the device may include a processor 601, a memory 602, and a bus 603. The memory 602 stores machine-readable instructions that can be executed by the processor 601. When the computer device is running, the processor 601 communicates with the memory 602 through the bus 603, and the processor 601 executes the machine-readable instructions to perform the above-described method.

[0077] This application also provides a computer-readable storage medium storing a computer program, which is executed by a processor to perform the above-described method.

[0078] In this embodiment, the computer program, when run by the processor, can also execute other machine-readable instructions to perform other methods as described in the embodiments. For details on the specific execution steps and principles, please refer to the description of the embodiments, which will not be repeated here.

[0079] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.

[0080] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0081] In addition, the functional units in the embodiments provided in this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.

[0082] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0083] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.

[0084] Finally, it should be noted that the above-described embodiments are merely specific implementations of this application, used to illustrate the technical solutions of this application, and not to limit them. The protection scope of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this application; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application. All should be covered within the protection scope of this application. Therefore, the protection scope of this application should be determined by the protection scope of the claims.

Claims

1. A device control method, characterized in that, Operating systems used in computer devices include: When the device driver corresponding to the device to be controlled is running, a user-mode program listens to whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program. If the key value information of the device register exists in the preset data structure, the key value information of the device register is read using the callback function defined by the kernel program; The callback function is used to perform read and write operations on the device register based on the key value information of the device register, so as to control the device to be controlled.

2. The method according to claim 1, characterized in that, The step of reading the key value information of the device register using the callback function defined in the kernel-mode program includes: The callback function is used to write the key value information of the device register into a preset virtual file, and to read the key value information of the device register from the preset virtual file.

3. The method according to claim 2, characterized in that, The step of writing the key-value information of the device register into a preset virtual file using the callback function includes: The callback function is used to write the key value information of the device register into the preset virtual file according to the write format corresponding to the write interface of the preset virtual file.

4. The method according to claim 1, characterized in that, Before the step of using a user-mode program to monitor whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program, the method further includes: If the device driver runs to a specified function, it jumps to the entry hook function defined by the kernel program, and uses the entry hook function to write the key value information of the device register into the preset data structure.

5. The method according to claim 1, characterized in that, Before the step of using a user-mode program to monitor whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel-mode program during the execution of the device driver corresponding to the device to be controlled, the method further includes: A framework for building a pre-defined kernel mechanism is provided, wherein the framework is used to define an interface for reading / writing registers, and the interface is part of the device driver. Based on the interface for reading / writing registers, compile the device driver to obtain a binary file; Load and run the binary file.

6. The method according to claim 4, characterized in that, The method further includes: Jump to the exit hook function defined in the kernel-mode program, and use the exit hook function to delete the key value information of the device register from the preset data structure.

7. The method according to claim 6, characterized in that, The method further includes: After the main program of the specified function is executed, the exit hook function is used to check whether the key value information of the device register exists in the preset data structure; If the key value information of the device register does not exist in the preset data structure, it is determined that the value in the key value information of the device register has been written into the device register.

8. The method according to claim 1, characterized in that, The step of using the callback function to perform read and write operations on the device register based on the key value information of the device register includes: The callback function is used to perform a read operation on the device register, and a write operation is performed on the device register according to the key value information of the device register; or The callback function is used to perform a write operation on the device register and a read operation on the device register based on the key value information of the device register.

9. A device control apparatus, characterized in that, include: The monitoring module is used to monitor, in user mode, whether the key value information of the device register of the device to be controlled is written into the preset data structure defined by the kernel mode program when the device driver corresponding to the device to be controlled is running. The read / write module is used to read the key value information of the device register using a callback function defined by the kernel-mode program if the key value information of the device register exists in the preset data structure. The read / write module is further configured to use the callback function to perform read / write operations on the device register according to the key value information of the device register, so as to control the device to be controlled.

10. A computer device, characterized in that, include: A processor, a memory, and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the computer device is running, the processor communicates with the memory via the bus, and the processor executes the machine-readable instructions to perform the method according to any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the method according to any one of claims 1 to 8.