Hypercall Description

As a Hypervisor, hvisor provides a hypercall processing mechanism to the upper layer virtual machines.

How Virtual Machines Execute Hypercall

Virtual machines execute a specified assembly instruction, which is hvc on Arm64 and ecall on riscv64. When executing the assembly instruction, the parameters passed are:

  • code: hypercall id, its range and meaning are detailed in hvisor's handling of hypercalls
  • arg0: the first parameter passed by the virtual machine, type is u64
  • arg1: the second parameter passed by the virtual machine, type is u64

For example, for riscv linux:

#ifdef RISCV64

// according to the riscv sbi spec
// SBI return has the following format:
// struct sbiret
//  {
//  long error;
//  long value;
// };

// a0: error, a1: value
static inline __u64 hvisor_call(__u64 code,__u64 arg0, __u64 arg1) {
	register __u64 a0 asm("a0") = code;
	register __u64 a1 asm("a1") = arg0;
	register __u64 a2 asm("a2") = arg1;
	register __u64 a7 asm("a7") = 0x114514;
	asm volatile ("ecall"
	        : "+r" (a0), "+r" (a1)
			: "r" (a2), "r" (a7)
			: "memory");
	return a1;
}
#endif

For arm64 linux:

#ifdef ARM64
static inline __u64 hvisor_call(__u64 code, __u64 arg0, __u64 arg1) {
	register __u64 x0 asm("x0") = code;
	register __u64 x1 asm("x1") = arg0;
	register __u64 x2 asm("x2") = arg1;

	asm volatile ("hvc #0x4856"
	        : "+r" (x0)
			: "r" (x1), "r" (x2)
			: "memory");
	return x0;
}
#endif /* ARM64 */

hvisor's Handling of Hypercall

After the virtual machine executes a hypercall, the CPU enters the exception handling function specified by hvisor: hypercall. Then hvisor continues to call different processing functions based on the hypercall parameters code, arg0, arg1, which are:

codeFunction CalledParameter DescriptionFunction Summary
0hv_virtio_initarg0: start address of shared memoryUsed for root zone to initialize virtio mechanism
1hv_virtio_inject_irqNoneUsed for root zone to send virtio device interrupts to other virtual machines
2hv_zone_startarg0: virtual machine configuration file address; arg1: configuration file sizeUsed for root zone to start a virtual machine
3hv_zone_shutdownarg0: id of the virtual machine to be shut downUsed for root zone to shut down a virtual machine
4hv_zone_listarg0: address of data structure representing virtual machine information; arg1: number of virtual machine informationUsed for root zone to view information of all virtual machines in the system
5hv_ivc_infoarg0: start address of ivc informationUsed for a zone to view its own communication domain information