Computer device, exception processing method, and interrupt processing method
A computer device is disclosed. In a virtualization I/O procedure, a redirection apparatus in a processor is used to implement privilege level switching in a synchronization processing procedure, to directly switch from a virtualization privilege level of a VM to a user mode of a host, and perform corresponding exception processing. In an asynchronous processing procedure, an interrupt request may be asynchronously transferred by using a virtual event notification apparatus and an interrupt controller in the processor without passing through a kernel mode of the host. In both of a synchronous processing procedure and the asynchronous processing procedure of the device, overheads generated during switching are reduced, and performance of the computer device is improved.
1 . A computer device, wherein the computer device comprises a hardware layer and a host runs at the hardware layer, wherein the hardware layer comprises a first processor, and a virtual machine (VM) runs on the first processor, and wherein the first processor comprises a redirection apparatus;
the first processor is configured to obtain an exception instruction triggered by the VM;
the redirection apparatus is configured to:
switch from a virtualization privilege level of the VM to a user mode of the host, wherein the virtualization privilege level of the VM comprises a user mode of the VM and a kernel mode of the VM, wherein the host is a virtualization platform and a privilege level of the host comprises the user mode of the host and a kernel mode of the host; and
the first processor is further configured to execute, in the user mode of the host, an exception processing function corresponding to the exception instruction,
wherein the exception processing function is comprised in a thread of a virtual processor that runs in the user mode of the host, an execution environment allocated to the thread of the virtual processor comprises a main thread execution environment, an execution environment of the VM, and a shadow execution environment, wherein the shadow execution environment has user-mode memory space independent of the main thread execution environment and the execution environment of the VM, the shadow execution environment is used to execute the exception processing function, and the exception processing function is used to process an exception that triggers the exception instruction.
2 . The computer device according to claim 1 , wherein
the redirection apparatus is further configured to write an entry address of the exception processing function into a first register; and
the first processor is configured to: read the entry address of the exception processing function from the first register, invoke the exception processing function based on the entry address, and execute, in the user mode of the host, the exception processing function corresponding to the exception instruction.
3 . The computer device according to claim 1 , wherein
the exception instruction is triggered by an input/output (I/O) operation performed by the VM.
4 . The computer device according to claim 1 , wherein
the first processor is configured to: switch from a context of the VM to a context of a user-mode shadow execution environment, and the user-mode memory space of the shadow execution environment is used to store the context of the VM that is migrated out during context switching.
5 . A computer device, wherein the computer device comprises a hardware layer, a host runs at the hardware layer, wherein the host is a virtualization platform, the hardware layer comprises a first processor, a second processor, and an interrupt controller, and a first virtual machine (VM) runs on the first processor, and wherein the first processor comprises a virtual event notification apparatus;
the first processor is configured to obtain a first interrupt request triggered by the first VM, wherein the first interrupt request is used to request to execute an interrupt processing function;
the virtual event notification apparatus is configured to send interrupt information to the interrupt controller in response to the first interrupt request;
the interrupt controller is configured to: determine an identifier of the second processor based on the interrupt information, and send a second interrupt request to the second processor based on the identifier of the second processor, wherein the second interrupt request is used to request the second processor to execute the interrupt processing function; and
the second processor is configured to execute the interrupt processing function in response to the received second interrupt request, the second processor further comprises a hardware-assisted switching apparatus;
the hardware-assisted switching apparatus is configured to:
switch from a first thread to a shadow thread, wherein the first thread is a thread run by the second processor before switching, the shadow thread is a thread that runs in the host in a user mode, and the shadow thread is used to execute the interrupt processing function, wherein the host is a virtualization platform and a privilege level of the host comprises the user mode and a kernel mode; and
the second processor is configured to execute the interrupt processing function in the user mode of the host.
6 . The computer device according to claim 5 , wherein the first interrupt request comprises a virtual event interrupt request number (VEI ID), and the VEI ID identifies a virtual event that triggers the first interrupt request;
the first processor is further configured to obtain a VM ID of the first VM;
the interrupt information comprises the VEI ID and the VM ID of the first VM;
the interrupt controller is configured to: determine, based on the VEI ID and the VM ID of the first VM, an identifier of the second processor, a VEI physical number, and a privilege level of the host that processes the first interrupt request, wherein the VEI physical number identifies the virtual event in the host; and send a second interrupt request to the second processor based on the identifier of the second processor, wherein the second interrupt request comprises information indicating the privilege level of the host; and
the second processor is configured to: obtain the VEI physical number, and execute, based on a processing manner corresponding to the privilege level of the host, an interrupt processing function corresponding to the VEI physical number in response to the received second interrupt request.
7 . The computer device according to claim 6 , wherein the privilege level of the host is a user mode.
8 . The computer device according to claim 7 , wherein the second processor further comprises a redirection apparatus;
the second processor is further configured to: before executing the interrupt processing function in the user mode of the host, determine whether the first thread is at a virtualization privilege level of a second VM, wherein the second VM runs on the second processor; and
the redirection apparatus is configured to: if an interrupted thread is at the virtualization privilege level of the second VM, switch the virtualization privilege level of the second VM to the user mode of the host.
9 . The computer device according to claim 8 , wherein
the redirection apparatus is further configured to write an entry address of the interrupt processing function into a first register; and
the second processor is configured to: read the entry address of the interrupt processing function from the first register, invoke the interrupt processing function based on the entry address, and execute the interrupt processing function in the user mode of the host.
10 . The computer device according to claim 6 , wherein the privilege level of the host is a kernel mode, and
the second processor is configured to:
receive the second interrupt request that is of the kernel mode of the host and that is sent by the interrupt controller; and
switch to an entry address of an interrupt processing function of the kernel mode of the host, wherein the interrupt processing function corresponds to the second interrupt request; and
execute, in the kernel mode of the host, the interrupt processing function of the kernel mode.
11 . The computer device according to claim 5 , wherein the first interrupt request comprises a virtual event interrupt request number (VEI ID), and the VEI ID identifies a virtual event that triggers the first interrupt request;
the interrupt information comprises the VEI ID;
the interrupt controller is configured to: determine, based on the VEI ID, an identifier of the second processor and a privilege level of the host that processes the first interrupt request; and send a second interrupt request to the second processor based on the identifier of the second processor, wherein the second interrupt request comprises information indicating the privilege level of the host; and
the second processor is configured to: obtain the VEI ID, and execute, based on a processing manner corresponding to the privilege level of the host, an interrupt processing function corresponding to the VEI ID in response to the received second interrupt request.
12 . An exception processing method, wherein the method is applied to a computer device, a host and a virtual machine (VM) run on the computer device, and the method comprises:
obtaining an exception instruction triggered by the VM; and
switching from a virtualization privilege level of the VM to a user mode of the host, wherein the virtualization privilege level of the VM comprises a user mode of the VM and a kernel mode of the VM, wherein the host is a virtualization platform and a privilege level of the host comprises the user mode of the host and a kernel mode of the host; and
executing, in the user mode of the host, an exception processing function corresponding to the exception instruction, wherein the exception processing function is comprised in a thread of a virtual processor that runs in the user mode of the host, an execution environment allocated to the thread of the virtual processor comprises a main thread execution environment, an execution environment of the VM, and a shadow execution environment, wherein the shadow execution environment has user-mode memory space independent of the main thread execution environment and the execution environment of the VM, the shadow execution environment is used to execute the exception processing function, and the exception processing function is used to process an exception that triggers the exception instruction.
13 . The method according to claim 12 , wherein the method further comprises:
writing an entry address of the exception processing function into a first register; and
the executing, in the user mode of the host, an exception processing function corresponding to the exception instruction comprises:
reading the entry address of the exception processing function from the first register, invoking the exception processing function based on the entry address, and executing, in the user mode of the host, the exception processing function corresponding to the exception instruction.
14 . The method according to claim 12 , wherein the exception instruction is triggered by an input/output (I/O) operation performed by the VM.
15 . The method according to claim 12 , wherein the executing, in the user mode of the host, an exception processing function corresponding to the exception instruction comprises:
switching from a context of the VM to a context of a shadow execution environment of the user mode, and processing an exception in the user mode of the host.
16 . An interrupt processing method, wherein the method is applied to a computer device, a host and a first virtual machine (VM) run on the computer device, and the method comprises:
obtaining a first interrupt request triggered by the first VM, wherein the first interrupt request is used to request to execute an interrupt processing function;
determining interrupt information in response to the first interrupt request;
generating a second interrupt request based on the interrupt information, wherein the second interrupt request is used to request to execute the interrupt processing function; and
executing the interrupt processing function in response to the second interrupt request, comprising:
switching from a first thread to a shadow thread, wherein the first thread is a thread run by the host before switching, the shadow thread is a thread that runs in the host in a user mode, and the shadow thread is used to execute the interrupt processing function, wherein the host is a virtualization platform and a privilege level of the host comprises the user mode and a kernel mode; and
executing the interrupt processing function in the user mode of the host.
17 . The method according to claim 16 , wherein the first interrupt request comprises a virtual event interrupt request number (VEI ID), and the VEI ID identifies a virtual event that triggers the first interrupt request; and the method further comprises:
obtaining a VM identifier (ID) of the first VM, wherein the interrupt information comprises the VEI ID and the VM ID of the first VM;
determining, based on the VEI ID and the VM ID of the first VM, a corresponding VEI physical number and a privilege level of the host that processes the first interrupt request, wherein the VEI physical number identifies the virtual event in the host; and
the executing the interrupt processing function in response to the second interrupt request comprises:
obtaining the VEI physical number, and executing, based on a processing manner corresponding to the privilege level of the host, an interrupt processing function corresponding to the VEI physical number in response to the received second interrupt request.
18 . The method according to claim 17 , wherein the privilege level of the host is a user mode.
19 . The method according to claim 17 , wherein the privilege level of the host is a kernel mode, and the executing, based on a processing manner corresponding to the privilege level of the host, the interrupt processing function corresponding to the VEI ID in response to the received second interrupt request comprises:
switching to an entry address of an interrupt processing function of the kernel mode of the host, wherein the interrupt processing function corresponds to the second interrupt request; and
executing, in the kernel mode of the host, the interrupt processing function of the kernel mode.
20 . The method according to claim 16 , wherein the first interrupt request comprises a virtual event interrupt request number (VEI ID), the VEI ID identifies a virtual event that triggers the first interrupt request, and the interrupt information comprises the VEI ID; and the method further comprises:
determining, based on the VEI ID, a privilege level of the host that processes the first interrupt request; and
the executing the interrupt processing function in response to the second interrupt request comprises:
obtaining the VEI ID, and executing, based on a processing manner corresponding to the privilege level of the host, an interrupt processing function corresponding to the VEI ID in response to the received second interrupt request.