// File: /workspace/github_c_makefile_star2_O2/intel[P]pf-bb-config/bb_acc_vfio.c
// Function: vfio_get_bar0_mapping
void *vfio_get_bar0_mapping(const char *pci_addr,
			unsigned int bar_size, void *dev)
{
	int vfio_dev_fd;
	void *map = NULL;
	hw_device *hwd = (hw_device *)dev;

	struct vfio_device_info device_info = {
		.argsz = sizeof(device_info)
	};

	struct vfio_region_info region_info = {
		.argsz = sizeof(region_info)
	};

	vfio_dev_fd = hwd->vfio_dev_fd;

	if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
		LOG(ERR, "Failed to get device info, dev fd: %d",
				vfio_dev_fd);
		return NULL;
	}
	LOG(DEBUG, "Device supports %d regions, %d irqs",
			device_info.num_regions, device_info.num_irqs);
	hwd->num_regions = device_info.num_regions;

	/* Configure VFIO token */
	if (vfio_set_token(hwd, vfio_dev_fd) < 0) {
		LOG(ERR, "Fail to set VFIO_DEVICE_FEATURE with UUID token");
		return NULL;
	}

	/* Map BAR0 region */
	region_info.index = 0;
	if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO,
			&region_info)) {
		LOG(ERR, "Failed to get region(%d) info",
				region_info.index);
		return NULL;
	}

	if (region_info.flags & VFIO_REGION_INFO_FLAG_MMAP) {
		map = mmap(NULL, (size_t)region_info.size,
				PROT_READ | PROT_WRITE, MAP_SHARED, vfio_dev_fd,
				(off_t)region_info.offset);
		if (map == MAP_FAILED) {
			LOG(ERR, "mmap failed for BAR0 space");
			return NULL;
		}
	}

	/* Enable BME */
	vfio_set_bus_master(hwd);

	return map;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rdx, %rbx
movups 0x5e6a(%rip), %xmm0     # 0x1e2d0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
andl $0x0, 0x10(%rdx)
movups 0x5e47(%rip), %xmm0     # 0x1e2c0
movaps %xmm0, 0x10(%rsp)
movups 0x5e2b(%rip), %xmm0     # 0x1e2b0
movaps %xmm0, (%rsp)
movl 0xc0(%rbx), %ebp
movl $0x3b6b, %esi           # imm = 0x3B6B
movl %ebp, %edi
xorl %eax, %eax
callq 0xd1a0
movl %eax, %r14d
callq 0x18754
testl %r14d, %r14d
je 0x184d4
leaq 0x1ca1f(%rip), %rcx     # 0x34ed0
movq (%rcx), %r8
leaq 0x59de(%rip), %rdx      # 0x1de99
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
movl %ebp, %r9d
xorl %eax, %eax
callq 0x19400
jmp 0x18679
leaq 0x1c9f5(%rip), %r12     # 0x34ed0
movq 0x18(%r12), %r8
movl 0x28(%rsp), %r10d
movl 0x2c(%rsp), %r11d
leaq 0x59d5(%rip), %rdx      # 0x1dec6
leaq 0x59ff(%rip), %r9       # 0x1def7
pushq $0x3
popq %rdi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x19400
popq %rcx
popq %rdx
movl 0x28(%rsp), %eax
movl %eax, 0xcc(%rbx)
pushq $0x18
popq %rdi
callq 0xd280
movq %rax, %r14
callq 0x18754
testq %r14, %r14
je 0x1863f
movq 0x18(%r12), %r8
leaq 0x5d16(%rip), %rdx      # 0x1e252
leaq 0x5d48(%rip), %r9       # 0x1e28b
xorl %r15d, %r15d
pushq $0x3
popq %rdi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19400
movabsq $0x2000000000018, %rax  # imm = 0x2000000000018
movq %rax, (%r14)
movups 0xdc(%rbx), %xmm0
movups %xmm0, 0x8(%r14)
leaq 0x40(%rsp), %rax
cmpq $0x10, %r15
je 0x185d5
movl %r15d, %ecx
andl $0xd, %ecx
cmpl $0x8, %ecx
je 0x18589
cmpl $0x4, %ecx
jne 0x1858f
movb $0x2d, (%rax)
incq %rax
movb 0x8(%r14,%r15), %cl
movl %ecx, %edx
shrb $0x4, %dl
leal 0x30(%rdx), %esi
addb $0x57, %dl
cmpb $-0x60, %cl
movzbl %sil, %ecx
movzbl %dl, %edx
cmovbl %ecx, %edx
movb 0x8(%r14,%r15), %cl
andb $0xf, %cl
leal 0x30(%rcx), %esi
cmpb $0xa, %cl
leal 0x57(%rcx), %ecx
movzbl %sil, %esi
movzbl %cl, %ecx
cmovbl %esi, %ecx
movb %dl, (%rax)
movb %cl, 0x1(%rax)
addq $0x2, %rax
incq %r15
jmp 0x18573
movb $0x0, (%rax)
callq 0x18754
movq 0x18(%r12), %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %r10
leaq 0x5ca8(%rip), %rdx      # 0x1e29a
leaq 0x5c92(%rip), %r9       # 0x1e28b
pushq $0x3
popq %rdi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
pushq %r10
callq 0x19400
popq %rcx
popq %rdx
movl $0x3b75, %esi           # imm = 0x3B75
movl %ebp, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0xd1a0
movl %eax, %r15d
movq %r14, %rdi
callq 0xd030
callq 0x18754
testl %r15d, %r15d
je 0x18689
movq (%r12), %r8
leaq 0x58d0(%rip), %rdx      # 0x1df0d
jmp 0x1864a
movq (%r12), %r8
leaq 0x5be9(%rip), %rdx      # 0x1e233
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19400
callq 0x18754
movq (%r12), %r8
leaq 0x58a5(%rip), %rdx      # 0x1df0d
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19400
movq %r14, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r12), %r8
leaq 0x2e8f(%rip), %rdx      # 0x1b524
leaq 0x5bef(%rip), %r9       # 0x1e28b
pushq $0x3
popq %rdi
xorl %esi, %esi
movq %rax, %rcx
xorl %eax, %eax
callq 0x19400
movq %rsp, %rdx
andl $0x0, 0x8(%rdx)
movl $0x3b6c, %esi           # imm = 0x3B6C
movl %ebp, %edi
xorl %eax, %eax
callq 0xd1a0
testl %eax, %eax
je 0x186e8
callq 0x18754
movq (%r12), %r8
movl 0x8(%rsp), %r9d
leaq 0x586b(%rip), %rdx      # 0x1df44
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq %rax, %rcx
jmp 0x184c8
testb $0x4, 0x4(%rsp)
jne 0x186f4
xorl %r14d, %r14d
jmp 0x18717
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r9
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movl %ebp, %r8d
callq 0xd120
movq %rax, %r14
cmpq $-0x1, %rax
je 0x18724
movq %rbx, %rdi
callq 0x179e2
jmp 0x18679
callq 0x18754
movq (%r12), %r8
leaq 0x5835(%rip), %rdx      # 0x1df69
jmp 0x18668