target/i386/sev: Housekeeping helping using SEV-disabled binaries
cover target/i386/sev: Housekeeping helping using SEV-disabled binaries
Hi,
I wasted some time trying to figure out how OVMF was supposed to behave until realizing the binary I was using was built without SEV support... Then wrote this series to help other developers to not hit the same problem.
Some SEV patches I was following have been queued on Eduardo's 'x86-next' tree, so I used his tree as base, and included David and Connor patches to reduce merge conflicts.
Based-on: https://gitlab.com/ehabkost/qemu/-/commits/x86-next/
Connor Kuehl (1):
MAINTAINERS: Add Connor Kuehl as reviewer for AMD SEV
Dr. David Alan Gilbert (1):
target/i386/sev: sev_get_attestation_report use g_autofree
Philippe Mathieu-Daudé (9):
qapi/misc-target: Group SEV QAPI definitions target/i386/monitor: Return QMP error when SEV is disabled in build target/i386/cpu: Add missing 'qapi/error.h' header target/i386/sev_i386.h: Remove unused headers target/i386/sev: Remove sev_get_me_mask() target/i386/sev: Mark unreachable code with g_assert_not_reached() target/i386/sev: Restrict SEV to system emulation target/i386/monitor: Move SEV specific commands to sev.c
monitor: Restrict 'info sev' to x86 targets
qapi/misc-target.json | 75 +++++++++++----------- include/monitor/hmp-target.h | 1 + include/monitor/hmp.h | 1 - target/i386/sev_i386.h | 5 -- target/i386/cpu.c | 1 + target/i386/monitor.c | 91 -------------------------- target/i386/sev-stub.c | 49 +------------- target/i386/sev-sysemu-stub.c | 69 ++++++++++++++++++++ target/i386/sev.c | 117 +++++++++++++++++++++++++++++----- MAINTAINERS | 7 ++ target/i386/meson.build | 4 +- 11 files changed, 219 insertions(+), 201 deletions(-) create mode 100644 target/i386/sev-sysemu-stub.c
Signature · 1 line
2.31.1
PATCH 1/11 MAINTAINERS: Add Connor Kuehl as reviewer for AMD SEV
From: Connor Kuehl <[email protected]>
It may not be appropriate for me to take over as a maintainer at this time, but I would consider myself familiar with AMD SEV and what this code is meant to be doing as part of a VMM for launching SEV-protected guests.
To that end, I would be happy to volunteer as a reviewer for SEV-related changes so that I am CC'd on them and can help share the review burden with whoever does maintain this code.
Message-Id: <[email protected]>
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -2938,6 +2938,13 @@ F: hw/core/clock-vmstate.c F: hw/core/qdev-clock.c F: docs/devel/clocks.rst +AMD Secure Encrypted Virtualization (SEV) +R: Connor Kuehl <[email protected]> +F: docs/amd-memory-encryption.txt +F: accel/kvm/sev-stub.c +F: target/i386/sev* +F: include/sysemu/sev.h + Usermode Emulation ------------------ Overall usermode emulation
Signature · 1 line
2.31.1
Re: [PATCH 01/11] MAINTAINERS: Add Connor Kuehl as reviewer for AMD SEV
Quoted 10 lines · Philippe Mathieu-Daudé
From: Connor Kuehl <[email protected]>
It may not be appropriate for me to take over as a maintainer at this time, but I would consider myself familiar with AMD SEV and what this code is meant to be doing as part of a VMM for launching SEV-protected guests.
To that end, I would be happy to volunteer as a reviewer for SEV-related changes so that I am CC'd on them and can help share the review burden with whoever does maintain this code.
Quoted 7 lines
Signed-off-by Connor Kuehl <[email protected]>Message-Id: <[email protected]>
[PMD: Cover more files]Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
Thanks for stepping in!
PATCH 2/11 qapi/misc-target: Group SEV QAPI definitions
In reply to Philippe Mathieu-DaudéThere is already a section with various SEV commands / types, so move the SEV guest attestation together.
--- qapi/misc-target.json | 75 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 38 deletions(-)
qapi/misc-target.json +37 -38
@@ -219,6 +219,43 @@ 'data': { 'packet-header': 'str', 'secret': 'str', '*gpa': 'uint64' }, 'if': 'defined(TARGET_I386)' } +## +# @SevAttestationReport: +# +# The struct describes attestation report for a Secure Encrypted Virtualization +# feature. +# +# @data: guest attestation report (base64 encoded) +# +# +# Since: 6.1 +## +{ 'struct': 'SevAttestationReport', + 'data': { 'data': 'str'}, + 'if': 'defined(TARGET_I386)' } + +## +# @query-sev-attestation-report: +# +# This command is used to get the SEV attestation report, and is supported on AMD +# X86 platforms only. +# +# @mnonce: a random 16 bytes value encoded in base64 (it will be included in report) +# +# Returns: SevAttestationReport objects. +# +# Since: 6.1 +# +# Example: +# +# -> { "execute" : "query-sev-attestation-report", "arguments": { "mnonce": "aaaaaaa" } } +# <- { "return" : { "data": "aaaaaaaabbbddddd"} } +# +## +{ 'command': 'query-sev-attestation-report', 'data': { 'mnonce': 'str' }, + 'returns': 'SevAttestationReport', + 'if': 'defined(TARGET_I386)' } + ## # @dump-skeys: #
@@ -285,41 +322,3 @@ ## { 'command': 'query-gic-capabilities', 'returns': ['GICCapability'], 'if': 'defined(TARGET_ARM)' } - - -## -# @SevAttestationReport: -# -# The struct describes attestation report for a Secure Encrypted Virtualization -# feature. -# -# @data: guest attestation report (base64 encoded) -# -# -# Since: 6.1 -## -{ 'struct': 'SevAttestationReport', - 'data': { 'data': 'str'}, - 'if': 'defined(TARGET_I386)' } - -## -# @query-sev-attestation-report: -# -# This command is used to get the SEV attestation report, and is supported on AMD -# X86 platforms only. -# -# @mnonce: a random 16 bytes value encoded in base64 (it will be included in report) -# -# Returns: SevAttestationReport objects. -# -# Since: 6.1 -# -# Example: -# -# -> { "execute" : "query-sev-attestation-report", "arguments": { "mnonce": "aaaaaaa" } } -# <- { "return" : { "data": "aaaaaaaabbbddddd"} } -# -## -{ 'command': 'query-sev-attestation-report', 'data': { 'mnonce': 'str' }, - 'returns': 'SevAttestationReport', - 'if': 'defined(TARGET_I386)' }
Signature · 1 line
2.31.1
Re: [PATCH 02/11] qapi/misc-target: Group SEV QAPI definitions
Quoted 46 lines · Philippe Mathieu-Daudé
There is already a section with various SEV commands / types, so move the SEV guest attestation together.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- qapi/misc-target.json | 75 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 38 deletions(-)qapi/misc-target.json +30
-0@@ -219,6 +219,43 @@ 'data': { 'packet-header': 'str', 'secret': 'str', '*gpa': 'uint64' }, 'if': 'defined(TARGET_I386)' } +## +# @SevAttestationReport: +# +# The struct describes attestation report for a Secure Encrypted Virtualization +# feature. +# +# @data: guest attestation report (base64 encoded) +# +# +# Since: 6.1 +## +{ 'struct': 'SevAttestationReport', + 'data': { 'data': 'str'}, + 'if': 'defined(TARGET_I386)' } + +## +# @query-sev-attestation-report: +# +# This command is used to get the SEV attestation report, and is supported on AMD +# X86 platforms only. +# +# @mnonce: a random 16 bytes value encoded in base64 (it will be included in report) +# +# Returns: SevAttestationReport objects. +# +# Since: 6.1 +# +# Example: +# +# -> { "execute" : "query-sev-attestation-report", "arguments": { "mnonce": "aaaaaaa" } }
qapi/misc-target.json line 261
Quoted 10 lines
qapi/misc-target.json +7
-0+# <- { "return" : { "data": "aaaaaaaabbbddddd"} } +# +## +{ 'command': 'query-sev-attestation-report', 'data': { 'mnonce': 'str' }, + 'returns': 'SevAttestationReport', + 'if': 'defined(TARGET_I386)' } + ## # @dump-skeys: #
Just code motion, so
Opportunity to wrap the long doc comment lines. Should be kept under 70 or so.
[...]
Re: [PATCH 02/11] qapi/misc-target: Group SEV QAPI definitions
Quoted 61 lines · Markus Armbruster
Quoted 46 lines · Philippe Mathieu-Daudé
There is already a section with various SEV commands / types, so move the SEV guest attestation together.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- qapi/misc-target.json | 75 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 38 deletions(-)qapi/misc-target.json +30
-0@@ -219,6 +219,43 @@ 'data': { 'packet-header': 'str', 'secret': 'str', '*gpa': 'uint64' }, 'if': 'defined(TARGET_I386)' } +## +# @SevAttestationReport: +# +# The struct describes attestation report for a Secure Encrypted Virtualization +# feature. +# +# @data: guest attestation report (base64 encoded) +# +# +# Since: 6.1 +## +{ 'struct': 'SevAttestationReport', + 'data': { 'data': 'str'}, + 'if': 'defined(TARGET_I386)' } + +## +# @query-sev-attestation-report: +# +# This command is used to get the SEV attestation report, and is supported on AMD +# X86 platforms only. +# +# @mnonce: a random 16 bytes value encoded in base64 (it will be included in report) +# +# Returns: SevAttestationReport objects. +# +# Since: 6.1 +# +# Example: +# +# -> { "execute" : "query-sev-attestation-report", "arguments": { "mnonce": "aaaaaaa" } }qapi/misc-target.json line 261
Quoted 10 lines
qapi/misc-target.json +7
-0+# <- { "return" : { "data": "aaaaaaaabbbddddd"} } +# +## +{ 'command': 'query-sev-attestation-report', 'data': { 'mnonce': 'str' }, + 'returns': 'SevAttestationReport', + 'if': 'defined(TARGET_I386)' } + ## # @dump-skeys: #Just code motion, so
Quoted 4 lines
Opportunity to wrap the long doc comment lines. Should be kept under 70 or so.
Hmm is that a QAPI specific requirement? It is not enforced by checkpatch, and still in the 80-90 grey area:
https://www.mail-archive.com/[email protected]/msg763806.html
(I can do if respin required, but I'd rather have this catch earlier, not at code movement).
Thanks for the A-b!
Re: [PATCH 02/11] qapi/misc-target: Group SEV QAPI definitions
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Markus Armbruster
Just code motion, so
Opportunity to wrap the long doc comment lines. Should be kept under 70 or so.
Hmm is that a QAPI specific requirement? It is not enforced by checkpatch, and still in the 80-90 grey area:
https://www.mail-archive.com/[email protected]/msg763806.html
Consider why we limit line length at all: it's for *legibility*. Humans tend to have trouble following long lines with our eyes (I sure do). Typographic manuals suggest to limit columns to roughly 60 characters for exactly that reason[1]. Four levels of indentation plus 60 characters of actual text yields 76.
We add a grey area to provide for the occasional case where deeper indentation pushes code of reasonable width beyond the "white" area, and breaking these lines would be less legible than making use of the grey area.
The lines I referred to are long for no good reason. Wrapping them will improve legibility.
Quoted 2 lines
(I can do if respin required, but I'd rather have this catch earlier, not at code movement).
Before, during, after, or even not at all *clank*[2], your choice.
Quoted 1 line
Thanks for the A-b!
[1] https://en.wikipedia.org/wiki/Column_(typography)#Typographic_style
[2] Sad sound of a can being kicked down the road
PATCH 3/11 target/i386/monitor: Return QMP error when SEV is disabled in build
In reply to Philippe Mathieu-DaudéIf the management layer tries to inject a secret, it gets an empty response in case the binary built without SEV:
{ "execute": "sev-inject-launch-secret",
"arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 }
}
{
"return": {
}
}Make it clearer by returning an error, mentioning the feature is disabled:
{ "execute": "sev-inject-launch-secret",
"arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 }
}
{
"error": {
"class": "GenericError",
"desc": "this feature or command is not currently supported"
}
}--- target/i386/monitor.c | 5 +++++ 1 file changed, 5 insertions(+)
target/i386/monitor.c +5 -0
@@ -28,6 +28,7 @@#include "monitor/hmp-target.h" #include "monitor/hmp.h" #include "qapi/qmp/qdict.h" +#include "qapi/qmp/qerror.h" #include "sysemu/kvm.h" #include "sysemu/sev.h" #include "qapi/error.h"
@@ -742,6 +743,10 @@ void qmp_sev_inject_launch_secret(const char *packet_hdr, bool has_gpa, uint64_t gpa, Error **errp) { + if (!sev_enabled()) { +error_setg (errp ,QERR_UNSUPPORTED ); +return ; + }if (!has_gpa) {uint8_t *data;struct sev_secret_area *area;
Signature · 1 line
2.31.1
Re: [PATCH 03/11] target/i386/monitor: Return QMP error when SEV is disabled in build
Quoted 24 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
If the management layer tries to inject a secret, it gets an empty response in case the binary built without SEV:
{ "execute": "sev-inject-launch-secret", "arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 } } { "return": { } }Make it clearer by returning an error, mentioning the feature is disabled:
{ "execute": "sev-inject-launch-secret", "arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 } } { "error": { "class": "GenericError", "desc": "this feature or command is not currently supported" } }
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 30 lines
--- target/i386/monitor.c | 5 +++++ 1 file changed, 5 insertions(+)target/i386/monitor.c +5
-0@@ -28,6 +28,7 @@#include "monitor/hmp-target.h" #include "monitor/hmp.h" #include "qapi/qmp/qdict.h" +#include "qapi/qmp/qerror.h" #include "sysemu/kvm.h" #include "sysemu/sev.h" #include "qapi/error.h" @@ -742,6 +743,10 @@ void qmp_sev_inject_launch_secret(const char *packet_hdr, bool has_gpa, uint64_t gpa, Error **errp) { + if (!sev_enabled()) { +error_setg (errp ,QERR_UNSUPPORTED ); +return ; + }if (!has_gpa) {uint8_t *data;struct sev_secret_area *area;Signature · 1 line
2.31.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH 03/11] target/i386/monitor: Return QMP error when SEV is disabled in build
In reply to Philippe Mathieu-DaudéQuoted 24 lines · Philippe Mathieu-Daudé
If the management layer tries to inject a secret, it gets an empty response in case the binary built without SEV:
{ "execute": "sev-inject-launch-secret", "arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 } } { "return": { } }Make it clearer by returning an error, mentioning the feature is disabled:
{ "execute": "sev-inject-launch-secret", "arguments": { "packet-header": "mypkt", "secret": "mypass", "gpa": 4294959104 } } { "error": { "class": "GenericError", "desc": "this feature or command is not currently supported" } }
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
PATCH 4/11 target/i386/cpu: Add missing 'qapi/error.h' header
In reply to Philippe Mathieu-DaudéCommit 00b81053244 ("target-i386: Remove assert_no_error usage") forgot to add the "qapi/error.h", add it now.
--- target/i386/cpu.c | 1 + 1 file changed, 1 insertion(+)
target/i386/cpu.c +1 -0
@@ -27,6 +27,7 @@#include "sysemu/hvf.h" #include "kvm/kvm_i386.h" #include "sev_i386.h" +#include "qapi/error.h" #include "qapi/qapi-visit-machine.h" #include "qapi/qmp/qerror.h" #include "qapi/qapi-commands-machine-target.h"
Signature · 1 line
2.31.1
Re: [PATCH 04/11] target/i386/cpu: Add missing 'qapi/error.h' header
Quoted 2 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Commit
00b81053244("target-i386: Removeassert_no_errorusage") forgot to add the "qapi/error.h", add it now.
for the error_abort I guess
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 19 lines
--- target/i386/cpu.c | 1 + 1 file changed, 1 insertion(+)target/i386/cpu.c +1
-0@@ -27,6 +27,7 @@#include "sysemu/hvf.h" #include "kvm/kvm_i386.h" #include "sev_i386.h" +#include "qapi/error.h" #include "qapi/qapi-visit-machine.h" #include "qapi/qmp/qerror.h" #include "qapi/qapi-commands-machine-target.h" Signature · 1 line
2.31.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH 04/11] target/i386/cpu: Add missing 'qapi/error.h' header
In reply to Philippe Mathieu-DaudéQuoted 4 lines · Philippe Mathieu-Daudé
Commit
00b81053244("target-i386: Removeassert_no_errorusage") forgot to add the "qapi/error.h", add it now.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
PATCH 5/11 target/i386/sev_i386.h: Remove unused headers
In reply to Philippe Mathieu-DaudéDeclarations don't require these headers, remove them.
--- target/i386/sev_i386.h | 4 ---- target/i386/sev-stub.c | 1 + 2 files changed, 1 insertion(+), 4 deletions(-)
target/i386/sev_i386.h +0 -4
@@ -14,11 +14,7 @@#ifndef QEMU_SEV_I386_H #define QEMU_SEV_I386_H -#include "qom/object.h" -#include "qapi/error.h" -#include "sysemu/kvm.h" #include "sysemu/sev.h" -#include "qemu/error-report.h" #include "qapi/qapi-types-misc-target.h" #define SEV_POLICY_NODBG 0x1
target/i386/sev-stub.c +1 -0
@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qapi/error.h" #include "sev_i386.h" SevInfo *sev_get_info (void )
Signature · 1 line
2.31.1
Re: [PATCH 05/11] target/i386/sev_i386.h: Remove unused headers
Quoted 3 lines · Philippe Mathieu-Daudé
Declarations don't require these headers, remove them.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
PATCH 6/11 target/i386/sev: Remove sev_get_me_mask()
In reply to Philippe Mathieu-DaudéUnused dead code makes review harder, so remove it.
--- target/i386/sev_i386.h | 1 - target/i386/sev-stub.c | 5 ----- target/i386/sev.c | 9 --------- 3 files changed, 15 deletions(-)
target/i386/sev_i386.h +0 -1
@@ -25,7 +25,6 @@#define SEV_POLICY_SEV 0x20extern bool sev_es_enabled (void ); -extern uint64_t sev_get_me_mask (void );extern SevInfo *sev_get_info (void );extern uint32_t sev_get_cbit_position (void );extern uint32_t sev_get_reduced_phys_bits (void );
target/i386/sev-stub.c +0 -5
@@ -25,11 +25,6 @@ bool sev_enabled(void)return false; } -uint64_t sev_get_me_mask (void ) -{ -return ~0 ; -} -uint32_t sev_get_cbit_position (void ) {return 0 ;
target/i386/sev.c +0 -9
@@ -64,7 +64,6 @@ struct SevGuestState {uint8_t api_major;uint8_t api_minor;uint8_t build_id; -uint64_t me_mask;int sev_fd;SevState state;gchar *measurement;
@@ -362,12 +361,6 @@ sev_es_enabled(void)return sev_enabled () && (sev_guest->policy &SEV_POLICY_ES ); } -uint64_t -sev_get_me_mask (void ) -{ -return sev_guest ? sev_guest->me_mask : ~0 ; -} -uint32_t sev_get_cbit_position (void ) {
@@ -810,8 +803,6 @@ int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) goto err; } - sev->me_mask = ~(1UL << sev->cbitpos); - devname =object_property_get_str (OBJECT (sev),"sev-device" ,NULL ); sev->sev_fd =open (devname,O_RDWR );if (sev->sev_fd <0 ) {
Signature · 1 line
2.31.1
Re: [PATCH 06/11] target/i386/sev: Remove sev_get_me_mask()
Quoted 3 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Unused dead code makes review harder, so remove it.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Yes, it doesn't seem to have been used since it was added.
Quoted 71 lines
--- target/i386/sev_i386.h | 1 - target/i386/sev-stub.c | 5 ----- target/i386/sev.c | 9 --------- 3 files changed, 15 deletions(-)target/i386/sev_i386.h +0
-1@@ -25,7 +25,6 @@#define SEV_POLICY_SEV 0x20extern bool sev_es_enabled (void ); -extern uint64_t sev_get_me_mask (void );extern SevInfo *sev_get_info (void );extern uint32_t sev_get_cbit_position (void );extern uint32_t sev_get_reduced_phys_bits (void );target/i386/sev-stub.c +0
-5@@ -25,11 +25,6 @@ bool sev_enabled(void)return false; } -uint64_t sev_get_me_mask (void ) -{ -return ~0 ; -} -uint32_t sev_get_cbit_position (void ) {return 0 ;target/i386/sev.c +0
-9@@ -64,7 +64,6 @@ struct SevGuestState {uint8_t api_major;uint8_t api_minor;uint8_t build_id; -uint64_t me_mask;int sev_fd;SevState state;gchar *measurement;@@ -362,12 +361,6 @@ sev_es_enabled(void)return sev_enabled () && (sev_guest->policy &SEV_POLICY_ES ); } -uint64_t -sev_get_me_mask (void ) -{ -return sev_guest ? sev_guest->me_mask : ~0 ; -} -uint32_t sev_get_cbit_position (void ) {@@ -810,8 +803,6 @@ int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) goto err; } - sev->me_mask = ~(1UL << sev->cbitpos); - devname =object_property_get_str (OBJECT (sev),"sev-device" ,NULL ); sev->sev_fd =open (devname,O_RDWR );if (sev->sev_fd <0 ) {Signature · 1 line
2.31.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH 06/11] target/i386/sev: Remove sev_get_me_mask()
In reply to Philippe Mathieu-DaudéQuoted 11 lines · Philippe Mathieu-Daudé
target/i386/sev.c +0
-6-uint64_t -sev_get_me_mask (void ) -{ -return sev_guest ? sev_guest->me_mask : ~0 ; -} -uint32_t sev_get_cbit_position (void ) {
target/i386/sev.c int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) · line 808
Quoted 8 lines
target/i386/sev.c +0
-2@@ -810,8 +803,6 @@ int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) goto err; } - sev->me_mask = ~(1UL << sev->cbitpos); - devname =object_property_get_str (OBJECT (sev),"sev-device" ,NULL ); sev->sev_fd =open (devname,O_RDWR );if (sev->sev_fd <0 ) {
Brijesh, do you remember if this was added with the intent that it would be useful in a future series?
Otherwise:
PATCH 7/11 target/i386/sev: Mark unreachable code with g_assert_not_reached()
In reply to Philippe Mathieu-DaudéThe unique sev_encrypt_flash() invocation (in pc_system_flash_map) is protected by the "if (sev_enabled())" check, so is not reacheable.
Replace the abort() call in sev_es_save_reset_vector() by g_assert_not_reached() which meaning is clearer.
--- target/i386/sev-stub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/sev-stub.c +2 -2
@@ -54,7 +54,7 @@ int sev_inject_launch_secret(const char *hdr, const char *secret,int sev_encrypt_flash (uint8_t *ptr,uint64_t len,Error **errp) { -return 0 ; +g_assert_not_reached (); }bool sev_es_enabled (void )
@@ -68,7 +68,7 @@ void sev_es_set_reset_vector(CPUState *cpu)int sev_es_save_reset_vector (void *flash_ptr,uint64_t flash_size) { -abort (); +g_assert_not_reached (); } SevAttestationReport *
Signature · 1 line
2.31.1
Re: [PATCH 07/11] target/i386/sev: Mark unreachable code with g_assert_not_reached()
Quoted 7 lines · Philippe Mathieu-Daudé
The unique
sev_encrypt_flash()invocation (inpc_system_flash_map) is protected by the "if (sev_enabled())" check, so is not reacheable.
Replace theabort()call insev_es_save_reset_vector()byg_assert_not_reached()which meaning is clearer.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
PATCH 8/11 target/i386/sev: sev_get_attestation_report use g_autofree
In reply to Philippe Mathieu-DaudéFrom: "Dr. David Alan Gilbert" <[email protected]>
Removes a whole bunch of g_free's and a goto.
Message-Id: <[email protected]>
--- target/i386/sev.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
target/i386/sev.c +3 -8
@@ -493,8 +493,8 @@ sev_get_attestation_report(const char *mnonce, Error **errp)struct kvm_sev_attestation_report input = {};SevAttestationReport *report =NULL ;SevGuestState *sev = sev_guest; -guchar *data; -guchar *buf; + g_autofree guchar *data = NULL; + g_autofree guchar *buf = NULL;gsize len;int err =0 , ret;
@@ -514,7 +514,6 @@ sev_get_attestation_report(const char *mnonce, Error **errp)if (len !=sizeof (input.mnonce)) {error_setg (errp,"SEV: mnonce must be %zu bytes (got %" G_GSIZE_FORMAT ")" ,sizeof (input.mnonce), len); -g_free (buf);return NULL ; }
@@ -525,7 +524,6 @@ sev_get_attestation_report(const char *mnonce, Error **errp)if (err !=SEV_RET_INVALID_LEN ) {error_setg (errp,"failed to query the attestation report length " "ret=%d fw_err=%d (%s)" , ret, err,fw_error_to_str (err)); -g_free (buf);return NULL ; } }
@@ -540,7 +538,7 @@ sev_get_attestation_report(const char *mnonce, Error **errp)if (ret) {error_setg_errno (errp, errno,"Failed to get attestation report" " ret=%d fw_err=%d (%s)" , ret, err,fw_error_to_str (err)); - goto e_free_data; +return NULL ; } report =g_new0 (SevAttestationReport,1 );
@@ -548,9 +546,6 @@ sev_get_attestation_report(const char *mnonce, Error **errp)trace_kvm_sev_attestation_report (mnonce, report->data); -e_free_data: -g_free (data); -g_free (buf);return report; }
Signature · 1 line
2.31.1
PATCH 9/11 target/i386/sev: Restrict SEV to system emulation
In reply to Philippe Mathieu-DaudéSEV is irrelevant on user emulation, so restrict it to sysemu. Some stubs are still required because used in cpu.c by x86_register_cpudef_types(), so move the sysemu specific stubs to sev-sysemu-stub.c instead. This will allow us to simplify monitor.c (which is not available in user emulation) in the next commit.
--- target/i386/sev-stub.c | 43 ------------------------- target/i386/sev-sysemu-stub.c | 60 +++++++++++++++++++++++++++++++++++ target/i386/meson.build | 4 ++- 3 files changed, 63 insertions(+), 44 deletions(-) create mode 100644 target/i386/sev-sysemu-stub.c
target/i386/sev-stub.c +0 -43
@@ -15,11 +15,6 @@#include "qapi/error.h" #include "sev_i386.h" -SevInfo *sev_get_info (void ) -{ -return NULL ; -} -bool sev_enabled (void ) {return false;
@@ -35,45 +30,7 @@ uint32_t sev_get_reduced_phys_bits(void)return 0 ; } -char *sev_get_launch_measurement (void ) -{ -return NULL ; -} - -SevCapability *sev_get_capabilities (Error **errp) -{ -error_setg (errp,"SEV is not available in this QEMU" ); -return NULL ; -} - -int sev_inject_launch_secret (const char *hdr,const char *secret, -uint64_t gpa,Error **errp) -{ -return 1 ; -} - -int sev_encrypt_flash (uint8_t *ptr,uint64_t len,Error **errp) -{ -g_assert_not_reached (); -} -bool sev_es_enabled (void ) {return false; } - -void sev_es_set_reset_vector (CPUState *cpu) -{ -} - -int sev_es_save_reset_vector (void *flash_ptr,uint64_t flash_size) -{ -g_assert_not_reached (); -} - -SevAttestationReport * -sev_get_attestation_report (const char *mnonce,Error **errp) -{ -error_setg (errp,"SEV is not available in this QEMU" ); -return NULL ; -}
target/i386/sev-sysemu-stub.c new file +60 -0
60 lines not on this page · raw message
target/i386/meson.build +3 -1
@@ -6,7 +6,7 @@ 'xsave_helper.c', 'cpu-dump.c', )) -i386_ss.add(when: 'CONFIG_SEV', if_true: files('host-cpu.c', 'sev.c'), if_false: files('sev-stub.c')) +i386_ss.add(when: 'CONFIG_SEV', if_true: files('host-cpu.c'), if_false: files('sev-stub.c')) # x86 cpu type i386_ss.add(when: 'CONFIG_KVM', if_true: files('host-cpu.c'))
@@ -20,6 +20,8 @@ 'monitor.c', 'cpu-sysemu.c', )) +i386_softmmu_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-sysemu-stub.c')) + i386_user_ss = ss.source_set() subdir('kvm')
Signature · 1 line
2.31.1
PATCH 10/11 target/i386/monitor: Move SEV specific commands to sev.c
In reply to Philippe Mathieu-DaudéHaving the HMP/QMP commands defined in monitor.c makes the stubs rather complicated when SEV is not built in. To simplify, move the SEV functions to sev.c, and remove a layer of stubs.
Also make it clearer when SEV is not built in, so developers don't try to enable it when it is not enablable:
- before:
(qemu) info sev
SEV is not enabled
- after:
(qemu) info sev
SEV is not available in this QEMU
--- target/i386/monitor.c | 96 ---------------------------------- target/i386/sev-sysemu-stub.c | 29 +++++++---- target/i386/sev.c | 97 +++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 106 deletions(-)
target/i386/monitor.c +0 -96
@@ -28,11 +28,8 @@#include "monitor/hmp-target.h" #include "monitor/hmp.h" #include "qapi/qmp/qdict.h" -#include "qapi/qmp/qerror.h" #include "sysemu/kvm.h" -#include "sysemu/sev.h" #include "qapi/error.h" -#include "sev_i386.h" #include "qapi/qapi-commands-misc-target.h" #include "qapi/qapi-commands-misc.h" #include "hw/i386/pc.h"
@@ -675,96 +672,3 @@ void hmp_info_io_apic(Monitor *mon, const QDict *qdict)monitor_printf (mon,"This command is obsolete and will be " "removed soon. Please use 'info pic' instead.\n" ); } - -SevInfo *qmp_query_sev (Error **errp) -{ -SevInfo *info; - - info =sev_get_info (); -if (!info) { -error_setg (errp,"SEV feature is not available" ); -return NULL ; - } - -return info; -} - -void hmp_info_sev (Monitor *mon,const QDict *qdict) -{ -SevInfo *info =sev_get_info (); - -if (info && info->enabled) { -monitor_printf (mon,"handle: %d\n" , info->handle); -monitor_printf (mon,"state: %s\n" ,SevState_str (info->state)); -monitor_printf (mon,"build: %d\n" , info->build_id); -monitor_printf (mon,"api version: %d.%d\n" , - info->api_major, info->api_minor); -monitor_printf (mon,"debug: %s\n" , - info->policy &SEV_POLICY_NODBG ?"off" :"on" ); -monitor_printf (mon,"key-sharing: %s\n" , - info->policy &SEV_POLICY_NOKS ?"off" :"on" ); - }else { -monitor_printf (mon,"SEV is not enabled\n" ); - } - -qapi_free_SevInfo (info); -} - -SevLaunchMeasureInfo *qmp_query_sev_launch_measure (Error **errp) -{ -char *data; -SevLaunchMeasureInfo *info; - - data =sev_get_launch_measurement (); -if (!data) { -error_setg (errp,"Measurement is not available" ); -return NULL ; - } - - info =g_malloc0 (sizeof (*info)); - info->data = data; - -return info; -} - -SevCapability *qmp_query_sev_capabilities (Error **errp) -{ -return sev_get_capabilities (errp); -} - -#define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294" -struct sev_secret_area { -uint32_t base; -uint32_t size; -}; - -void qmp_sev_inject_launch_secret (const char *packet_hdr, -const char *secret, -bool has_gpa,uint64_t gpa, -Error **errp) -{ -if (!sev_enabled ()) { -error_setg (errp,QERR_UNSUPPORTED ); -return ; - } -if (!has_gpa) { -uint8_t *data; -struct sev_secret_area *area; - -if (!pc_system_ovmf_table_find (SEV_SECRET_GUID , &data,NULL )) { -error_setg (errp,"SEV: no secret area found in OVMF," -" gpa must be specified." ); -return ; - } - area = (struct sev_secret_area *)data; - gpa = area->base; - } - -sev_inject_launch_secret (packet_hdr, secret, gpa, errp); -} - -SevAttestationReport * -qmp_query_sev_attestation_report (const char *mnonce,Error **errp) -{ -return sev_get_attestation_report (mnonce, errp); -}
target/i386/sev-sysemu-stub.c +19 -10
@@ -12,30 +12,35 @@*/ #include "qemu/osdep.h" +#include "monitor/monitor.h" +#include "monitor/hmp.h" #include "qapi/qapi-commands-misc-target.h" +#include "qapi/qmp/qerror.h" #include "qapi/error.h" #include "sev_i386.h" -SevInfo *sev_get_info (void ) +SevInfo *qmp_query_sev (Error **errp) { +error_setg (errp,QERR_UNSUPPORTED );return NULL ; } -char *sev_get_launch_measurement (void ) +SevLaunchMeasureInfo *qmp_query_sev_launch_measure (Error **errp) { +error_setg (errp,QERR_UNSUPPORTED );return NULL ; } -SevCapability *sev_get_capabilities (Error **errp) +SevCapability *qmp_query_sev_capabilities (Error **errp) { -error_setg (errp,"SEV is not available in this QEMU" ); +error_setg (errp,QERR_UNSUPPORTED );return NULL ; } -int sev_inject_launch_secret (const char *hdr,const char *secret, -uint64_t gpa,Error **errp) +void qmp_sev_inject_launch_secret (const char *packet_header,const char *secret, +bool has_gpa,uint64_t gpa,Error **errp) { -return 1 ; +error_setg (errp,QERR_UNSUPPORTED ); }int sev_encrypt_flash (uint8_t *ptr,uint64_t len,Error **errp)
@@ -52,9 +57,13 @@ int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size)g_assert_not_reached (); } -SevAttestationReport *sev_get_attestation_report (const char *mnonce, -Error **errp) +SevAttestationReport *qmp_query_sev_attestation_report (const char *mnonce,Error **errp) { -error_setg (errp,"SEV is not available in this QEMU" ); +error_setg (errp,QERR_UNSUPPORTED );return NULL ; } + +void hmp_info_sev (Monitor *mon,const QDict *qdict) +{ +monitor_printf (mon,"SEV is not available in this QEMU\n" ); +}
target/i386/sev.c +97 -0
110 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH 10/11] target/i386/monitor: Move SEV specific commands to sev.c
Quoted 14 lines · Philippe Mathieu-Daudé
Having the HMP/QMP commands defined in
monitor.cmakes the stubs rather complicated when SEV is not built in. To simplify, move the SEV functions tosev.c, and remove a layer of stubs.Also make it clearer when SEV is not built in, so developers don't try to enable it when it is not enablable:
- before:
(qemu) info sev
SEV is not enabled- after:
Quoted 8 lines
(qemu) info sev
SEV is not available in this QEMUSigned-off-by Philippe Mathieu-Daudé <[email protected]>--- target/i386/monitor.c | 96 ---------------------------------- target/i386/sev-sysemu-stub.c | 29 +++++++---- target/i386/sev.c | 97 +++++++++++++++++++++++++++++++++++
Hi Philippe,
I agree that the split from monitor.c makes it easier to follow. Instead of putting the QMP entry points in sev-sysemu-stub. and sev.c, what do you think of placing them in sev-qmp-stub.c and sev-qmp.c, respectively?
I find that appealing from a code organization/module boundary perspective.
Connor
Re: [PATCH 10/11] target/i386/monitor: Move SEV specific commands to sev.c
Quoted 24 lines · Connor Kuehl
Quoted 14 lines · Philippe Mathieu-Daudé
Having the HMP/QMP commands defined in
monitor.cmakes the stubs rather complicated when SEV is not built in. To simplify, move the SEV functions tosev.c, and remove a layer of stubs.Also make it clearer when SEV is not built in, so developers don't try to enable it when it is not enablable:
- before:
(qemu) info sev
SEV is not enabled- after:
Quoted 8 lines
(qemu) info sev
SEV is not available in this QEMUSigned-off-by Philippe Mathieu-Daudé <[email protected]>--- target/i386/monitor.c | 96 ---------------------------------- target/i386/sev-sysemu-stub.c | 29 +++++++---- target/i386/sev.c | 97 +++++++++++++++++++++++++++++++++++
Quoted 8 lines
Hi Philippe,
I agree that the split from
monitor.cmakes it easier to follow. Instead of putting the QMP entry points in sev-sysemu-stub. andsev.c, what do you think of placing them insev-qmp-stub.candsev-qmp.c, respectively?I find that appealing from a code organization/module boundary perspective.
I tried but this makes the code more complex since sev-qmp.c calls functions defined in sev.c, we have to keep them declared in a local header (sev_i386.h). So for v2 I choose to keep the same organization but exploded the patch for easier review.
PATCH 11/11 monitor: Restrict 'info sev' to x86 targets
In reply to Philippe Mathieu-Daudé--- include/monitor/hmp-target.h | 1 + include/monitor/hmp.h | 1 - target/i386/sev-sysemu-stub.c | 2 +- target/i386/sev.c | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-)
include/monitor/hmp-target.h +1 -0
@@ -49,5 +49,6 @@ void hmp_info_tlb(Monitor *mon, const QDict *qdict);void hmp_mce (Monitor *mon,const QDict *qdict);void hmp_info_local_apic (Monitor *mon,const QDict *qdict);void hmp_info_io_apic (Monitor *mon,const QDict *qdict); +void hmp_info_sev (Monitor *mon,const QDict *qdict);#endif /* MONITOR_HMP_TARGET_H */
include/monitor/hmp.h +0 -1
@@ -124,7 +124,6 @@ void hmp_info_ramblock(Monitor *mon, const QDict *qdict);void hmp_hotpluggable_cpus (Monitor *mon,const QDict *qdict);void hmp_info_vm_generation_id (Monitor *mon,const QDict *qdict);void hmp_info_memory_size_summary (Monitor *mon,const QDict *qdict); -void hmp_info_sev (Monitor *mon,const QDict *qdict);void hmp_info_replay (Monitor *mon,const QDict *qdict);void hmp_replay_break (Monitor *mon,const QDict *qdict);void hmp_replay_delete_break (Monitor *mon,const QDict *qdict);
target/i386/sev-sysemu-stub.c +1 -1
@@ -13,7 +13,7 @@#include "qemu/osdep.h" #include "monitor/monitor.h" -#include "monitor/hmp.h" +#include "monitor/hmp-target.h" #include "qapi/qapi-commands-misc-target.h" #include "qapi/qmp/qerror.h" #include "qapi/error.h"
target/i386/sev.c +1 -1
@@ -32,7 +32,7 @@#include "migration/blocker.h" #include "qom/object.h" #include "monitor/monitor.h" -#include "monitor/hmp.h" +#include "monitor/hmp-target.h" #include "qapi/qapi-commands-misc-target.h" #include "qapi/qmp/qerror.h" #include "exec/confidential-guest-support.h"
Signature · 1 line
2.31.1