diff --git a/grub-core/kern/emu/bhyve_hostif.c b/grub-core/kern/emu/bhyve_hostif.c index 8e03778..2cb128a 100644 --- a/grub-core/kern/emu/bhyve_hostif.c +++ b/grub-core/kern/emu/bhyve_hostif.c @@ -46,6 +46,7 @@ static struct vmctx *bhyve_ctx; static int bhyve_cinsert = 1; static int bhyve_vgainsert = 1; +static int bhyve_memwire = 0; #define BHYVE_MAXSEGS 5 struct { @@ -113,6 +114,13 @@ grub_emu_bhyve_init(const char *name, grub_uint64_t memsz) return GRUB_ERR_BUG; } +#ifdef VM_MEM_F_WIRED + if (bhyve_memwire) + { + vm_set_memflags(bhyve_ctx, VM_MEM_F_WIRED); + } +#endif + err = vm_setup_memory (bhyve_ctx, memsz, VM_MMAP_ALL); if (err) { fprintf (stderr, "Could not setup memory for VM\n"); @@ -403,3 +411,22 @@ grub_emu_bhyve_vgainsert(void) { return bhyve_vgainsert; } + +int +grub_emu_bhyve_memwire_avail(void) +{ + +#ifdef VM_MEM_F_WIRED + return (1); +#else + return (0); +#endif +} + +void +grub_emu_bhyve_set_memwire(void) +{ +#ifdef VM_MEM_F_WIRED + bhyve_memwire = 1; +#endif +} diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c index f26024e..82341e8 100644 --- a/grub-core/kern/emu/main.c +++ b/grub-core/kern/emu/main.c @@ -227,6 +227,47 @@ static struct argp argp = { NULL, help_filter, NULL }; +#ifdef BHYVE +/* + * Represent run-time conditional options as argp child options. + * The only one at this point is the "-S" option to force wiring + * of guest memory on >= 11.0-r284539. + */ +static struct argp_option bhyve_options[] = { + {0, 'S', 0, 0, N_("Force wiring of guest memory."), 0}, + { 0, 0, 0, 0, 0, 0 } +}; + +static error_t +bhyve_opt_parser (int key, char *arg, struct argp_state *state) +{ + + switch (key) + { + case 'S': + grub_emu_bhyve_set_memwire(); + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +static struct argp bhyve_opt_argp = { + bhyve_options, + bhyve_opt_parser +}; + +static struct argp_child bhyve_opt_child = { + &bhyve_opt_argp, + 0, + "", + 2 +}; + +static struct argp_child bhyve_argp_children[2]; +#endif + void grub_hostfs_init (void); @@ -255,6 +296,12 @@ main (int argc, char *argv[]) #ifdef BHYVE grub_cfg = xstrdup (DEFAULT_GRUB_CFG); + + if (grub_emu_bhyve_memwire_avail()) { + bhyve_argp_children[0] = bhyve_opt_child; + bhyve_argp_children[1].argp = NULL; + argp.children = bhyve_argp_children; + } #endif if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0) diff --git a/include/grub/emu/bhyve.h b/include/grub/emu/bhyve.h index 2662650..b2b7b2a 100644 --- a/include/grub/emu/bhyve.h +++ b/include/grub/emu/bhyve.h @@ -47,6 +47,8 @@ int grub_emu_bhyve_parse_memsize(const char *arg, grub_uint64_t *size); void grub_emu_bhyve_set_console_dev(const char *dev); void grub_emu_bhyve_unset_cinsert(void); void grub_emu_bhyve_unset_vgainsert(void); +int grub_emu_bhyve_memwire_avail(void); +void grub_emu_bhyve_set_memwire(void); int EXPORT_FUNC(grub_emu_bhyve_cinsert) (void); int EXPORT_FUNC(grub_emu_bhyve_vgainsert) (void); void EXPORT_FUNC(grub_emu_bhyve_boot32)(grub_uint32_t bootaddr,