From nobody Wed Apr 10 15:19:09 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VF61V2cMlz5HHKC; Wed, 10 Apr 2024 15:19:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VF61V0J5jz4n6g; Wed, 10 Apr 2024 15:19:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712762350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LZGGV0erY33YNOl/jpWkCk4Dr8zuYLgRE/kiRD/VNgc=; b=v8FpafsBKbWPzXVEo3qpfGsoRTfahosuhcUqWkN5c3kTS9hQRuuQa+Ave1Hvmq0++6SPOh odO9+EYJs9+ViQj+zOQFWpkagTabDHa7w5CZadoawQZz4Y3OpHASKCHEHPeGSt5KZ3KY6e D49itmgpqZvvmv2NxGvMMkLR17uLPBlmXG0slmTiKNo979nIluRmk27bNQGfedXt9zaO0H JG9ikpD2AnA4CafIyUUCB3/JDSGemHxwlZCoD+Y5aoUcDNjgwF718zxswFU6PyCbQnsmqs F7JTHEI8VTIywG3N2N4lqV+LwBilOLiPP5shvZs4pVF0i298x3VPAUz9+Iv4qQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712762350; a=rsa-sha256; cv=none; b=s40rRpuy80floLVTGqUdJGgOio9qqwBKzcjYZ1woByAo8o3hjsdECl9JQuSNzms/JjMl9O tK8JNYSeJAQPvgkUwfOELknQ9vm2oKy+grzpFjN48O72ig8OjD4wn0DQeMBldP/rJskB9p EXpyUPhpw+8plQk+dPqgG050I/citrXccfpamDpu1SVEd+o9GNfWPBfvTwLBG43QYC2Mlv jFt7WUVPIkfm9taEn9YZcF7H3l4XXbXLlHGWF3akbhP35KbW80As9iYh0b5SSCducQYexx 3up54qvx+V5QJ8Eea+t+D9fo2a+/9k1iOzlE/IXdVbpts/JVPy5eIrSUTUAMKw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712762350; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LZGGV0erY33YNOl/jpWkCk4Dr8zuYLgRE/kiRD/VNgc=; b=lmjjQwZUmYc0wVQi9uaY38sqsztERU3DI5s4fhy4izVzer8/b0g+rUvNWb0tBPuVq53uiW mbSGeoA+Znm+SXdHrJVfdoRqwXwi9YKXTnKfQc7v2Ge4CkrJ6eUIEzZD4kC7xzwlUDkjRU J0uIP1OHPbriPJa4WAUxRh3ZDbT6gtaPoYP4FE/cPZpAq0r7THy96TliD97owX2msSgugw iGiYzbXwEHinlAAh9a6KSdAEoK4+XGuBRml8m2kWYmkLnLeQEed6pn5NIwHkKeYXSv4D73 K2OIdyOkst9QFAUeEkTLqu7k64EWwpsdbGg0k4D0UQkmYZ44b9+43Slx7WGQKA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VF61T728tzjsK; Wed, 10 Apr 2024 15:19:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43AFJ9iS093018; Wed, 10 Apr 2024 15:19:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43AFJ9Fd093015; Wed, 10 Apr 2024 15:19:09 GMT (envelope-from git) Date: Wed, 10 Apr 2024 15:19:09 GMT Message-Id: <202404101519.43AFJ9Fd093015@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 7e0fa794123e - main - libvmmapi: Make memory segment handling a bit more abstract List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: dev-commits-src-all+owner@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7e0fa794123eb8395fffa976c47e8ba4f44f2df0 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=7e0fa794123eb8395fffa976c47e8ba4f44f2df0 commit 7e0fa794123eb8395fffa976c47e8ba4f44f2df0 Author: Mark Johnston AuthorDate: 2024-04-03 17:01:31 +0000 Commit: Mark Johnston CommitDate: 2024-04-10 15:17:56 +0000 libvmmapi: Make memory segment handling a bit more abstract libvmmapi leaves a hole at [3GB, 4GB) in the guest physical address space. This hole is not used in the arm64 port, which maps everything above 4GB. This change makes the code a bit more general to accomodate arm64 more naturally. In particular: - Remove vm_set_lowmem_limit(): it is unused and doesn't have well-defined constraints, e.g., nothing prevents a consumer from setting a lowmem limit above the highmem base. - Define a constant for the highmem base and use that everywhere that the base is currently hard-coded. - Make the lowmem limit a compile-time constant instead of a vmctx field. - Store segment info in an array. - Add vm_get_highmem_base(), for use in bhyve since the current value is hard-coded in some places. No functional change intended. Reviewed by: corvink, jhb MFC after: 2 weeks Sponsored by: Innovate UK Differential Revision: https://reviews.freebsd.org/D41004 --- lib/libvmmapi/internal.h | 13 +++++-- lib/libvmmapi/vmmapi.c | 90 ++++++++++++++++++++++++++---------------------- lib/libvmmapi/vmmapi.h | 2 +- 3 files changed, 59 insertions(+), 46 deletions(-) diff --git a/lib/libvmmapi/internal.h b/lib/libvmmapi/internal.h index 98e50f9a1bf4..2b2f9eac3757 100644 --- a/lib/libvmmapi/internal.h +++ b/lib/libvmmapi/internal.h @@ -9,12 +9,19 @@ #include +enum { + VM_MEMSEG_LOW, + VM_MEMSEG_HIGH, + VM_MEMSEG_COUNT, +}; + struct vmctx { int fd; - uint32_t lowmem_limit; + struct { + vm_paddr_t base; + vm_size_t size; + } memsegs[VM_MEMSEG_COUNT]; int memflags; - size_t lowmem; - size_t highmem; char *baseaddr; char *name; }; diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c index 63f0fe0f16fe..ab20df3f3fc9 100644 --- a/lib/libvmmapi/vmmapi.c +++ b/lib/libvmmapi/vmmapi.c @@ -59,6 +59,9 @@ #define MB (1024 * 1024UL) #define GB (1024 * 1024 * 1024UL) +#define VM_LOWMEM_LIMIT (3 * GB) +#define VM_HIGHMEM_BASE (4 * GB) + /* * Size of the guard region before and after the virtual address space * mapping the guest physical memory. This must be a multiple of the @@ -110,9 +113,9 @@ vm_open(const char *name) vm->fd = -1; vm->memflags = 0; - vm->lowmem_limit = 3 * GB; vm->name = (char *)(vm + 1); strcpy(vm->name, name); + memset(vm->memsegs, 0, sizeof(vm->memsegs)); if ((vm->fd = vm_device_open(vm->name)) < 0) goto err; @@ -194,17 +197,10 @@ vm_parse_memsize(const char *opt, size_t *ret_memsize) } uint32_t -vm_get_lowmem_limit(struct vmctx *ctx) +vm_get_lowmem_limit(struct vmctx *ctx __unused) { - return (ctx->lowmem_limit); -} - -void -vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) -{ - - ctx->lowmem_limit = limit; + return (VM_LOWMEM_LIMIT); } void @@ -266,8 +262,8 @@ vm_get_guestmem_from_ctx(struct vmctx *ctx, char **guest_baseaddr, { *guest_baseaddr = ctx->baseaddr; - *lowmem_size = ctx->lowmem; - *highmem_size = ctx->highmem; + *lowmem_size = ctx->memsegs[VM_MEMSEG_LOW].size; + *highmem_size = ctx->memsegs[VM_MEMSEG_HIGH].size; return (0); } @@ -418,17 +414,17 @@ vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) assert(vms == VM_MMAP_ALL); /* - * If 'memsize' cannot fit entirely in the 'lowmem' segment then - * create another 'highmem' segment above 4GB for the remainder. + * If 'memsize' cannot fit entirely in the 'lowmem' segment then create + * another 'highmem' segment above VM_HIGHMEM_BASE for the remainder. */ - if (memsize > ctx->lowmem_limit) { - ctx->lowmem = ctx->lowmem_limit; - ctx->highmem = memsize - ctx->lowmem_limit; - objsize = 4*GB + ctx->highmem; + if (memsize > VM_LOWMEM_LIMIT) { + ctx->memsegs[VM_MEMSEG_LOW].size = VM_LOWMEM_LIMIT; + ctx->memsegs[VM_MEMSEG_HIGH].size = memsize - VM_LOWMEM_LIMIT; + objsize = VM_HIGHMEM_BASE + ctx->memsegs[VM_MEMSEG_HIGH].size; } else { - ctx->lowmem = memsize; - ctx->highmem = 0; - objsize = ctx->lowmem; + ctx->memsegs[VM_MEMSEG_LOW].size = memsize; + ctx->memsegs[VM_MEMSEG_HIGH].size = 0; + objsize = memsize; } error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL); @@ -445,17 +441,17 @@ vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) return (-1); baseaddr = ptr + VM_MMAP_GUARD_SIZE; - if (ctx->highmem > 0) { - gpa = 4*GB; - len = ctx->highmem; + if (ctx->memsegs[VM_MEMSEG_HIGH].size > 0) { + gpa = VM_HIGHMEM_BASE; + len = ctx->memsegs[VM_MEMSEG_HIGH].size; error = setup_memory_segment(ctx, gpa, len, baseaddr); if (error) return (error); } - if (ctx->lowmem > 0) { + if (ctx->memsegs[VM_MEMSEG_LOW].size > 0) { gpa = 0; - len = ctx->lowmem; + len = ctx->memsegs[VM_MEMSEG_LOW].size; error = setup_memory_segment(ctx, gpa, len, baseaddr); if (error) return (error); @@ -476,20 +472,19 @@ vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) void * vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len) { + vm_size_t lowsize, highsize; - if (ctx->lowmem > 0) { - if (gaddr < ctx->lowmem && len <= ctx->lowmem && - gaddr + len <= ctx->lowmem) + lowsize = ctx->memsegs[VM_MEMSEG_LOW].size; + if (lowsize > 0) { + if (gaddr < lowsize && len <= lowsize && gaddr + len <= lowsize) return (ctx->baseaddr + gaddr); } - if (ctx->highmem > 0) { - if (gaddr >= 4*GB) { - if (gaddr < 4*GB + ctx->highmem && - len <= ctx->highmem && - gaddr + len <= 4*GB + ctx->highmem) - return (ctx->baseaddr + gaddr); - } + highsize = ctx->memsegs[VM_MEMSEG_HIGH].size; + if (highsize > 0 && gaddr >= VM_HIGHMEM_BASE) { + if (gaddr < VM_HIGHMEM_BASE + highsize && len <= highsize && + gaddr + len <= VM_HIGHMEM_BASE + highsize) + return (ctx->baseaddr + gaddr); } return (NULL); @@ -499,15 +494,19 @@ vm_paddr_t vm_rev_map_gpa(struct vmctx *ctx, void *addr) { vm_paddr_t offaddr; + vm_size_t lowsize, highsize; offaddr = (char *)addr - ctx->baseaddr; - if (ctx->lowmem > 0) - if (offaddr <= ctx->lowmem) + lowsize = ctx->memsegs[VM_MEMSEG_LOW].size; + if (lowsize > 0) + if (offaddr <= lowsize) return (offaddr); - if (ctx->highmem > 0) - if (offaddr >= 4*GB && offaddr < 4*GB + ctx->highmem) + highsize = ctx->memsegs[VM_MEMSEG_HIGH].size; + if (highsize > 0) + if (offaddr >= VM_HIGHMEM_BASE && + offaddr < VM_HIGHMEM_BASE + highsize) return (offaddr); return ((vm_paddr_t)-1); @@ -524,14 +523,21 @@ size_t vm_get_lowmem_size(struct vmctx *ctx) { - return (ctx->lowmem); + return (ctx->memsegs[VM_MEMSEG_LOW].size); +} + +vm_paddr_t +vm_get_highmem_base(struct vmctx *ctx __unused) +{ + + return (VM_HIGHMEM_BASE); } size_t vm_get_highmem_size(struct vmctx *ctx) { - return (ctx->highmem); + return (ctx->memsegs[VM_MEMSEG_HIGH].size); } void * diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h index b69f02cde7e4..bd182b0914fc 100644 --- a/lib/libvmmapi/vmmapi.h +++ b/lib/libvmmapi/vmmapi.h @@ -135,11 +135,11 @@ int vm_gla2gpa_nofault(struct vcpu *vcpu, struct vm_guest_paging *paging, uint64_t gla, int prot, uint64_t *gpa, int *fault); uint32_t vm_get_lowmem_limit(struct vmctx *ctx); -void vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit); void vm_set_memflags(struct vmctx *ctx, int flags); int vm_get_memflags(struct vmctx *ctx); const char *vm_get_name(struct vmctx *ctx); size_t vm_get_lowmem_size(struct vmctx *ctx); +vm_paddr_t vm_get_highmem_base(struct vmctx *ctx); size_t vm_get_highmem_size(struct vmctx *ctx); #ifdef __amd64__ int vm_set_desc(struct vcpu *vcpu, int reg,