devicemodel/core/hugetlb.c
/*
 * level  : hugepage level
 * len	  : region length for mmap
 * offset : region start offset from ctx->baseaddr
 * skip   : skip offset in different level hugetlbfs fd
 */
level  : hugepage level
len    : region length for mmap
offset : region start offset from ctx->baseaddr
skip   : skip offset in different level hugetlbfs fd
level : hugepage level len : region length for mmap offset : region start offset from ctx - > baseaddr skip : skip offset in different level hugetlbfs fd
static int mmap_hugetlbfs_from_level(struct vmctx *ctx, int level, size_t len,
		size_t offset, size_t skip, char **addr_out)
{
	char *addr;
	size_t pagesz = 0;
	int fd, i;
	if (level >= HUGETLB_LV_MAX) {
		pr_err("exceed max hugetlb level");
		return -EINVAL;
	}
	if (mem_idx >= ARRAY_SIZE(mmap_mem_regions)) {
		pr_err("exceed supported regions.\n");
		return -EFAULT;
	}
	fd = hugetlb_priv[level].fd;
	addr = mmap(ctx->baseaddr + offset, len, PROT_READ | PROT_WRITE,
			MAP_SHARED | MAP_FIXED, fd, skip);
	if (addr == MAP_FAILED)
		return -ENOMEM;
	if (addr_out)
		*addr_out = addr;
	mmap_mem_regions[mem_idx].gpa_start = offset;
	mmap_mem_regions[mem_idx].gpa_end = offset + len;
	mmap_mem_regions[mem_idx].fd = fd;
	mmap_mem_regions[mem_idx].fd_offset = skip;
	mmap_mem_regions[mem_idx].hva_base = addr;
	mem_idx++;
	pr_info("mmap 0x%lx@%p\n", len, addr);
	pagesz = hugetlb_priv[level].pg_size;
	pr_info("touch %ld pages with pagesz 0x%lx\n", len/pagesz, pagesz);
	for (i = 0; i < len/pagesz; i++) {
		*(volatile char *)addr = *addr;
		addr += pagesz;
	}
	return 0;
}
static int mmap_hugetlbfs_from_level ( struct vmctx * ctx , int level , size_t len , size_t offset , size_t skip , char * * addr_out ) { char * addr ; size_t pagesz = 0 ; int fd , i ; if ( level >= HUGETLB_LV_MAX ) { pr_err ( " " ) ; return - EINVAL ; } if ( mem_idx >= ARRAY_SIZE ( mmap_mem_regions ) ) { pr_err ( " \n " ) ; return - EFAULT ; } fd = hugetlb_priv [ level ] . fd ; addr = mmap ( ctx -> baseaddr + offset , len , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_FIXED , fd , skip ) ; if ( addr == MAP_FAILED ) return - ENOMEM ; if ( addr_out ) * addr_out = addr ; mmap_mem_regions [ mem_idx ] . gpa_start = offset ; mmap_mem_regions [ mem_idx ] . gpa_end = offset + len ; mmap_mem_regions [ mem_idx ] . fd = fd ; mmap_mem_regions [ mem_idx ] . fd_offset = skip ; mmap_mem_regions [ mem_idx ] . hva_base = addr ; mem_idx ++ ; pr_info ( " \n " , len , addr ) ; pagesz = hugetlb_priv [ level ] . pg_size ; pr_info ( " \n " , len / pagesz , pagesz ) ; for ( i = 0 ; i < len / pagesz ; i ++ ) { * ( volatile char * ) addr = * addr ; addr += pagesz ; } return 0 ; }
level  : hugepage level
 len    : region length for mmap
 offset : region start offset from ctx->baseaddr
 skip   : skip offset in different level hugetlbfs fd
level : hugepage level len : region length for mmap offset : region start offset from ctx - > baseaddr skip : skip offset in different level hugetlbfs fd
/* add the mapping into mmap_mem_region */ /* pre-allocate hugepages by touch them */ /* Access to the address will trigger hugetlb_fault() in kernel,
	 * it will allocate and clear the huge page.*/