From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 9E07620EC88 for ; Thu, 25 Apr 2024 13:48:01 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C6DAE30BB9; Thu, 25 Apr 2024 13:48:06 +0200 (CEST) Mime-Version: 1.0 Date: Thu, 25 Apr 2024 13:48:01 +0200 Message-Id: From: "Stefan Sterz" To: "Proxmox VE development discussion" X-Mailer: aerc 0.17.0-69-g65571b67d7d3-dirty References: <20240425112438.957650-1-m.frank@proxmox.com> In-Reply-To: <20240425112438.957650-1-m.frank@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL -0.116 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: Re: [pve-devel] [PATCH qemu-server v8 1/3] add C program to get hardware capabilities from CPUID X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" On Thu Apr 25, 2024 at 1:24 PM CEST, Markus Frank wrote: > Implement a systemd service that runs a C program that extracts AMD > SEV hardware information such as reduced-phys-bios and cbitpos from > CPUID at boot time, looks if SEV, SEV-ES & SEV-SNP are enabled, and > outputs these details as JSON to /run/qemu-server/hw-params.json. > > This programm can also be used to read and save other hardware > information at boot time. > > Signed-off-by: Markus Frank > Co-authored-by: Thomas Lamprecht > --- > v8: > * renamed query-machine-params to query-machine-capabilities > > v7: > * renamed amd-sev-support to query-machine-params > * mv /run/amd-sev-params to /run/qemu-server/hw-params.json > * add "mkdir /run/qemu-server" to ensure that the directory exists > * moved json content to amd-sev property inside a bigger json > so that other hardware parameters could also be read at boot time and > included in this json file. > > Makefile | 1 + > query-machine-capabilities/Makefile | 21 +++++++ > .../query-machine-capabilities.c | 55 +++++++++++++++++++ > .../query-machine-capabilities.service | 12 ++++ > 4 files changed, 89 insertions(+) > create mode 100644 query-machine-capabilities/Makefile > create mode 100644 query-machine-capabilities/query-machine-capabilities.c > create mode 100644 query-machine-capabilities/query-machine-capabilities.service > > diff --git a/Makefile b/Makefile > index 133468d..ed67fe0 100644 > --- a/Makefile > +++ b/Makefile > @@ -65,6 +65,7 @@ install: $(PKGSOURCES) > install -m 0644 -D bootsplash.jpg $(DESTDIR)/usr/share/$(PACKAGE) > $(MAKE) -C PVE install > $(MAKE) -C qmeventd install > + $(MAKE) -C query-machine-capabilities install > $(MAKE) -C qemu-configs install > $(MAKE) -C vm-network-scripts install > install -m 0755 qm $(DESTDIR)$(SBINDIR) > diff --git a/query-machine-capabilities/Makefile b/query-machine-capabilities/Makefile > new file mode 100644 > index 0000000..c5f6348 > --- /dev/null > +++ b/query-machine-capabilities/Makefile > @@ -0,0 +1,21 @@ > +DESTDIR= > +PREFIX=/usr > +SBINDIR=${PREFIX}/libexec/qemu-server > +SERVICEDIR=/lib/systemd/system > + > +CC ?= gcc > +CFLAGS += -O2 -fanalyzer -Werror -Wall -Wextra -Wpedantic -Wtype-limits -Wl,-z,relro -std=gnu11 > + > +query-machine-capabilities: query-machine-capabilities.c > + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) > + > +.PHONY: install > +install: query-machine-capabilities > + install -d ${DESTDIR}/${SBINDIR} > + install -d ${DESTDIR}${SERVICEDIR} > + install -m 0644 query-machine-capabilities.service ${DESTDIR}${SERVICEDIR} > + install -m 0755 query-machine-capabilities ${DESTDIR}${SBINDIR} > + > +.PHONY: clean > +clean: > + rm -f query-machine-capabilities > diff --git a/query-machine-capabilities/query-machine-capabilities.c b/query-machine-capabilities/query-machine-capabilities.c > new file mode 100644 > index 0000000..f4a9f9f > --- /dev/null > +++ b/query-machine-capabilities/query-machine-capabilities.c > @@ -0,0 +1,55 @@ > +#include > +#include > +#include > +#include > +#include > + > +int main() { > + uint32_t eax, ebx, ecx, edx; > + > + // query Encrypted Memory Capabilities, see: > + // https://en.wikipedia.org/wiki/CPUID#EAX=8000001Fh:_Encrypted_Memory_Capabilities > + uint32_t query_function = 0x8000001F; > + asm volatile("cpuid" > + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) > + : "0"(query_function) > + ); > + > + bool sev_support = (eax & (1<<1)) != 0; > + bool sev_es_support = (eax & (1<<3)) != 0; > + bool sev_snp_support = (eax & (1<<4)) != 0; > + > + uint8_t cbitpos = ebx & 0x3f; > + uint8_t reduced_phys_bits = (ebx >> 6) & 0x3f; > + > + FILE *file; > + char filename[] = "/run/qemu-server/host-hw-capabilities.json"; > + > + mkdir("/run/qemu-server/", 0755); > + wouldn't it make sense to check whether this call succeeded too like you do for the `fopen` below? also might be nice to use `strerror` and handle `errno` in those cases too. > + file = fopen(filename, "w"); > + if (file == NULL) { > + perror("Error opening file"); > + return 1; > + } > + > + fprintf(file, > + "{" > + " \"amd-sev\": {" > + " \"cbitpos\": %u," > + " \"reduced-phys-bits\": %u," > + " \"sev-support\": %s," > + " \"sev-support-es\": %s," > + " \"sev-support-snp\": %s" > + " }" > + " }\n", > + cbitpos, > + reduced_phys_bits, > + sev_support ? "true" : "false", > + sev_es_support ? "true" : "false", > + sev_snp_support ? "true" : "false" > + ); > + > + fclose(file); > + return 0; > +} > diff --git a/query-machine-capabilities/query-machine-capabilities.service b/query-machine-capabilities/query-machine-capabilities.service > new file mode 100644 > index 0000000..f926074 > --- /dev/null > +++ b/query-machine-capabilities/query-machine-capabilities.service > @@ -0,0 +1,12 @@ > +[Unit] > +Description=read AMD SEV parameters > +RequiresMountsFor=/run > +Before=pve-ha-lrm.service > +Before=pve-guests.service > + > +[Service] > +ExecStart=/usr/libexec/qemu-server/query-machine-capabilities > +Type=oneshot > + > +[Install] > +WantedBy=multi-user.target _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel