1 #include <assert.h>
2 
3 #include "xc_sr_common.h"
4 
5 #include <xen-tools/libs.h>
6 
7 static const char *dhdr_types[] =
8 {
9     [DHDR_TYPE_X86_PV]  = "x86 PV",
10     [DHDR_TYPE_X86_HVM] = "x86 HVM",
11     [DHDR_TYPE_X86_PVH] = "x86 PVH",
12     [DHDR_TYPE_ARM]     = "ARM",
13 };
14 
dhdr_type_to_str(uint32_t type)15 const char *dhdr_type_to_str(uint32_t type)
16 {
17     if ( type < ARRAY_SIZE(dhdr_types) && dhdr_types[type] )
18         return dhdr_types[type];
19 
20     return "Reserved";
21 }
22 
23 static const char *mandatory_rec_types[] =
24 {
25     [REC_TYPE_END]                          = "End",
26     [REC_TYPE_PAGE_DATA]                    = "Page data",
27     [REC_TYPE_X86_PV_INFO]                  = "x86 PV info",
28     [REC_TYPE_X86_PV_P2M_FRAMES]            = "x86 PV P2M frames",
29     [REC_TYPE_X86_PV_VCPU_BASIC]            = "x86 PV vcpu basic",
30     [REC_TYPE_X86_PV_VCPU_EXTENDED]         = "x86 PV vcpu extended",
31     [REC_TYPE_X86_PV_VCPU_XSAVE]            = "x86 PV vcpu xsave",
32     [REC_TYPE_SHARED_INFO]                  = "Shared info",
33     [REC_TYPE_TSC_INFO]                     = "TSC info",
34     [REC_TYPE_HVM_CONTEXT]                  = "HVM context",
35     [REC_TYPE_HVM_PARAMS]                   = "HVM params",
36     [REC_TYPE_TOOLSTACK]                    = "Toolstack",
37     [REC_TYPE_X86_PV_VCPU_MSRS]             = "x86 PV vcpu msrs",
38     [REC_TYPE_VERIFY]                       = "Verify",
39     [REC_TYPE_CHECKPOINT]                   = "Checkpoint",
40     [REC_TYPE_CHECKPOINT_DIRTY_PFN_LIST]    = "Checkpoint dirty pfn list",
41 };
42 
rec_type_to_str(uint32_t type)43 const char *rec_type_to_str(uint32_t type)
44 {
45     if ( !(type & REC_TYPE_OPTIONAL) )
46     {
47         if ( (type < ARRAY_SIZE(mandatory_rec_types)) &&
48              (mandatory_rec_types[type]) )
49             return mandatory_rec_types[type];
50     }
51 
52     return "Reserved";
53 }
54 
write_split_record(struct xc_sr_context * ctx,struct xc_sr_record * rec,void * buf,size_t sz)55 int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec,
56                        void *buf, size_t sz)
57 {
58     static const char zeroes[(1u << REC_ALIGN_ORDER) - 1] = { 0 };
59 
60     xc_interface *xch = ctx->xch;
61     typeof(rec->length) combined_length = rec->length + sz;
62     size_t record_length = ROUNDUP(combined_length, REC_ALIGN_ORDER);
63     struct iovec parts[] =
64     {
65         { &rec->type,       sizeof(rec->type) },
66         { &combined_length, sizeof(combined_length) },
67         { rec->data,        rec->length },
68         { buf,              sz },
69         { (void*)zeroes,    record_length - combined_length },
70     };
71 
72     if ( record_length > REC_LENGTH_MAX )
73     {
74         ERROR("Record (0x%08x, %s) length %#zx exceeds max (%#x)", rec->type,
75               rec_type_to_str(rec->type), record_length, REC_LENGTH_MAX);
76         return -1;
77     }
78 
79     if ( rec->length )
80         assert(rec->data);
81     if ( sz )
82         assert(buf);
83 
84     if ( writev_exact(ctx->fd, parts, ARRAY_SIZE(parts)) )
85         goto err;
86 
87     return 0;
88 
89  err:
90     PERROR("Unable to write record to stream");
91     return -1;
92 }
93 
read_record(struct xc_sr_context * ctx,int fd,struct xc_sr_record * rec)94 int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec)
95 {
96     xc_interface *xch = ctx->xch;
97     struct xc_sr_rhdr rhdr;
98     size_t datasz;
99 
100     if ( read_exact(fd, &rhdr, sizeof(rhdr)) )
101     {
102         PERROR("Failed to read Record Header from stream");
103         return -1;
104     }
105     else if ( rhdr.length > REC_LENGTH_MAX )
106     {
107         ERROR("Record (0x%08x, %s) length %#x exceeds max (%#x)", rhdr.type,
108               rec_type_to_str(rhdr.type), rhdr.length, REC_LENGTH_MAX);
109         return -1;
110     }
111 
112     datasz = ROUNDUP(rhdr.length, REC_ALIGN_ORDER);
113 
114     if ( datasz )
115     {
116         rec->data = malloc(datasz);
117 
118         if ( !rec->data )
119         {
120             ERROR("Unable to allocate %zu bytes for record data (0x%08x, %s)",
121                   datasz, rhdr.type, rec_type_to_str(rhdr.type));
122             return -1;
123         }
124 
125         if ( read_exact(fd, rec->data, datasz) )
126         {
127             free(rec->data);
128             rec->data = NULL;
129             PERROR("Failed to read %zu bytes of data for record (0x%08x, %s)",
130                    datasz, rhdr.type, rec_type_to_str(rhdr.type));
131             return -1;
132         }
133     }
134     else
135         rec->data = NULL;
136 
137     rec->type   = rhdr.type;
138     rec->length = rhdr.length;
139 
140     return 0;
141 };
142 
build_assertions(void)143 static void __attribute__((unused)) build_assertions(void)
144 {
145     BUILD_BUG_ON(sizeof(struct xc_sr_ihdr) != 24);
146     BUILD_BUG_ON(sizeof(struct xc_sr_dhdr) != 16);
147     BUILD_BUG_ON(sizeof(struct xc_sr_rhdr) != 8);
148 
149     BUILD_BUG_ON(sizeof(struct xc_sr_rec_page_data_header)  != 8);
150     BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_info)       != 8);
151     BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_p2m_frames) != 8);
152     BUILD_BUG_ON(sizeof(struct xc_sr_rec_x86_pv_vcpu_hdr)   != 8);
153     BUILD_BUG_ON(sizeof(struct xc_sr_rec_tsc_info)          != 24);
154     BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params_entry)  != 16);
155     BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params)        != 8);
156 }
157 
158 /*
159  * Local variables:
160  * mode: C
161  * c-file-style: "BSD"
162  * c-basic-offset: 4
163  * tab-width: 4
164  * indent-tabs-mode: nil
165  * End:
166  */
167