xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/mem_high.c (revision 6e8a136ffaf56dd81447d589e81f22fc1a8cd773)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2025 Oxide Computer Company
14  */
15 
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stropts.h>
20 #include <strings.h>
21 #include <signal.h>
22 #include <setjmp.h>
23 #include <libgen.h>
24 
25 #include <sys/vmm.h>
26 #include <sys/vmm_dev.h>
27 #include <sys/mman.h>
28 #include <vmmapi.h>
29 
30 #include "common.h"
31 
32 #define	TEST_SEGID	0
33 #define	PAGE_SZ		4096
34 #define	MBYTE		(1024 * 1024)
35 #define	GBYTE		(1024 * MBYTE)
36 
37 #define	MAP_OFF		((512UL * GBYTE) - PAGE_SZ)
38 #define	SEG_SZ		(4 * MBYTE)
39 #define	PAGE_CNT	(SEG_SZ / PAGE_SZ)
40 
41 int
main(int argc,char * argv[])42 main(int argc, char *argv[])
43 {
44 	struct vmctx *ctx;
45 	int res;
46 	const char *suite_name = basename(argv[0]);
47 
48 	ctx = create_test_vm(suite_name);
49 	if (ctx == NULL) {
50 		perror("could open test VM");
51 		return (1);
52 	}
53 
54 	res = alloc_memseg(ctx, TEST_SEGID, SEG_SZ, "test_seg");
55 	if (res != 0) {
56 		perror("could not alloc memseg");
57 		goto bail;
58 	}
59 
60 	res = vm_mmap_memseg(ctx, MAP_OFF, TEST_SEGID, 0, SEG_SZ, PROT_ALL);
61 	if (res != 0) {
62 		perror("could not map memseg into vmspace");
63 		goto bail;
64 	}
65 
66 	res = vm_munmap_memseg(ctx, MAP_OFF, SEG_SZ);
67 	if (res != 0) {
68 		perror("could not unmap memseg");
69 		goto bail;
70 	}
71 
72 	/* mission accomplished */
73 	vm_destroy(ctx);
74 	(void) printf("%s\tPASS\n", suite_name);
75 	return (0);
76 
77 bail:
78 	vm_destroy(ctx);
79 	return (1);
80 }
81