CCF
Loading...
Searching...
No Matches
snp_ioctl6.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7#include <fcntl.h>
8#include <stdint.h>
9#include <sys/ioctl.h>
10#include <sys/types.h>
11#include <unistd.h>
12
13// Based on the SEV-SNP ABI Spec document at
14// https://www.amd.com/system/files/TechDocs/56860.pdf
15
16/* linux kernel 6.* versions of the ioctls that talk to the PSP */
17
19{
20 constexpr auto DEVICE = "/dev/sev-guest";
21
22 // Table 20
24 {
25 uint8_t report_data[snp_attestation_report_data_size];
26 uint32_t vmpl = 0;
27 uint8_t reserved[28]; // needs to be zero
28 }; // aka snp_report_req in (linux) include/uapi/linux/sev-guest.h
29
30 // Table 23
31#pragma pack(push, 1)
33 {
34 uint32_t status;
35 uint32_t report_size;
36 uint8_t reserved[0x20 - 0x8];
38 uint8_t padding[64];
39 // padding to the size of SEV_SNP_REPORT_RSP_BUF_SZ (i.e., 1280 bytes)
40 };
41
42 static_assert(sizeof(AttestationResp) < 4000);
44 {
46 uint8_t padding[4000 - sizeof(struct AttestationResp)];
47 };
48#pragma pack(pop)
49
51 {
52 uint32_t fw;
53 uint32_t vmm;
54 };
55
57 {
58 uint64_t whole;
60 };
61
62 // https://www.kernel.org/doc/html/v6.4/virt/coco/sev-guest.html#api-description
64 {
65 /* Message version number */
66 uint32_t msg_version;
67
68 /* Request and response structure address */
71
72 /* bits[63:32]: VMM error code, bits[31:0] firmware error code (see
73 * psp-sev.h) */
75 };
76
77 constexpr char SEV_GUEST_IOC_TYPE = 'S';
80
81 static inline bool is_sev_snp()
82 {
83 return access(DEVICE, W_OK) == 0;
84 }
85
87 {
88 AttestationReq req = {};
89 AttestationRespWrapper resp_wrapper = {};
90
91 public:
93 {
94 if (report_data.data.size() <= snp_attestation_report_data_size)
95 {
96 std::copy(
97 report_data.data.begin(), report_data.data.end(), req.report_data);
98 }
99 else
100 {
101 throw std::logic_error(
102 "User-defined report data is larger than available space");
103 }
104
105 int fd = open(DEVICE, O_RDWR | O_CLOEXEC);
106 if (fd < 0)
107 {
108 throw std::logic_error(fmt::format("Failed to open \"{}\"", DEVICE));
109 }
110
111 // Documented at
112 // https://www.kernel.org/doc/html/latest/virt/coco/sev-guest.html
113 GuestRequest payload = {
114 .msg_version = 1,
115 .req_data = &req,
116 .resp_wrapper = &resp_wrapper,
117 .exit_info = {0}};
118
119 int rc = ioctl(fd, SEV_SNP_GUEST_MSG_REPORT, &payload);
120 if (rc < 0)
121 {
122 CCF_APP_FAIL("IOCTL call failed: {}", strerror(errno));
124 "Exit info, fw_error: {} vmm_error: {}",
125 payload.exit_info.errors.fw,
126 payload.exit_info.errors.vmm);
127 throw std::logic_error(
128 "Failed to issue ioctl SEV_SNP_GUEST_MSG_REPORT");
129 }
130 }
131
132 const snp::Attestation& get() const override
133 {
134 return resp_wrapper.resp.report;
135 }
136
137 std::vector<uint8_t> get_raw() override
138 {
139 auto quote_bytes = reinterpret_cast<uint8_t*>(&resp_wrapper.resp.report);
140 return {quote_bytes, quote_bytes + resp_wrapper.resp.report_size};
141 }
142 };
143}
Definition attestation_sev_snp.h:289
Definition snp_ioctl6.h:87
Attestation(const PlatformAttestationReportData &report_data)
Definition snp_ioctl6.h:92
std::vector< uint8_t > get_raw() override
Definition snp_ioctl6.h:137
const snp::Attestation & get() const override
Definition snp_ioctl6.h:132
#define CCF_APP_FAIL
Definition logger.h:400
Definition snp_ioctl6.h:19
constexpr char SEV_GUEST_IOC_TYPE
Definition snp_ioctl6.h:77
constexpr int SEV_SNP_GUEST_MSG_REPORT
Definition snp_ioctl6.h:78
constexpr auto DEVICE
Definition snp_ioctl6.h:20
Definition report_data.h:51
std::vector< uint8_t > data
Definition report_data.h:52
Definition attestation_sev_snp.h:164
Definition snp_ioctl6.h:24
uint8_t report_data[snp_attestation_report_data_size]
Definition snp_ioctl6.h:25
uint8_t reserved[28]
Definition snp_ioctl6.h:27
uint32_t vmpl
Definition snp_ioctl6.h:26
uint8_t padding[4000 - sizeof(struct AttestationResp)]
Definition snp_ioctl6.h:46
struct AttestationResp resp
Definition snp_ioctl6.h:45
Definition snp_ioctl6.h:33
uint32_t status
Definition snp_ioctl6.h:34
uint32_t report_size
Definition snp_ioctl6.h:35
uint8_t padding[64]
Definition snp_ioctl6.h:38
struct Attestation report
Definition snp_ioctl6.h:37
uint8_t reserved[0x20 - 0x8]
Definition snp_ioctl6.h:36
Definition snp_ioctl6.h:51
uint32_t fw
Definition snp_ioctl6.h:52
uint32_t vmm
Definition snp_ioctl6.h:53
Definition snp_ioctl6.h:64
AttestationReq * req_data
Definition snp_ioctl6.h:69
ExitInfo exit_info
Definition snp_ioctl6.h:74
uint32_t msg_version
Definition snp_ioctl6.h:66
AttestationRespWrapper * resp_wrapper
Definition snp_ioctl6.h:70
Definition snp_ioctl6.h:57
ExitInfoErrors errors
Definition snp_ioctl6.h:59
uint64_t whole
Definition snp_ioctl6.h:58