Skip to content

Commit 77c3917

Browse files
Xiangyang Wulijinxia
authored andcommitted
HV:treewide:avoid using multiple # or ## in a macro
In the C99 standard, the order of evaluation associated with multiple #, multiple ## or a mix of # and ## preprocessor operator is unspecified. For this case, gcc 7.3.0 manual does not specify related implementation. So it is unsafe to use multiple # or ## in a macro. BTW, there are some macros with one or more "##" which are not used by hypervisor. Update relate codes to avoid using multiple # or ## in a macro; Remove unused macros with one or more "##"; Remove "struct __hack;" at the end of GETCC since it is useless. Note: '##' operator usage constraints: A ## preprocessing token shall not occur at the beginning or at the end of a replacement list for either form of macro definition. V1--V2: Update relate codes to avoid using multiple # or ## in a macro. V2-->V3: Remove unused macros with one or more "##"; Remove "struct __hack;" at the end of GETCC since it is useless. Signed-off-by: Xiangyang Wu <xiangyang.wu@linux.intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com>
1 parent 581a336 commit 77c3917

File tree

3 files changed

+36
-60
lines changed

3 files changed

+36
-60
lines changed

hypervisor/arch/x86/guest/instr_emul.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,21 +366,19 @@ static int vie_update_rflags(struct vcpu *vcpu, uint64_t rflags2, uint64_t psl)
366366
/*
367367
* Return the status flags that would result from doing (x - y).
368368
*/
369-
#define GETCC(sz) \
370-
static uint64_t \
371-
getcc##sz(uint##sz##_t x, uint##sz##_t y) \
372-
{ \
373-
uint64_t rflags; \
374-
\
375-
__asm __volatile("sub %2,%1; pushfq; popq %0" : \
376-
"=r" (rflags), "+r" (x) : "m" (y)); \
369+
#define build_getcc(name, type, x, y) \
370+
static uint64_t name(type x, type y) \
371+
{ \
372+
uint64_t rflags; \
373+
\
374+
__asm __volatile("sub %2,%1; pushfq; popq %0" : \
375+
"=r" (rflags), "+r" (x) : "m" (y)); \
377376
return rflags; \
378-
} struct __hack
379-
380-
GETCC(8);
381-
GETCC(16);
382-
GETCC(32);
383-
GETCC(64);
377+
}
378+
build_getcc(getcc8, uint8_t, x, y)
379+
build_getcc(getcc16, uint16_t, x, y)
380+
build_getcc(getcc32, uint32_t, x, y)
381+
build_getcc(getcc64, uint64_t, x, y)
384382

385383
static uint64_t
386384
getcc(uint8_t opsize, uint64_t x, uint64_t y)

hypervisor/arch/x86/trusty.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,20 @@ static struct trusty_key_info g_key_info = {
3333
.platform = 3U,
3434
.num_seeds = 1U
3535
};
36-
3736
#define save_segment(seg, SEG_NAME) \
3837
{ \
39-
seg.selector = exec_vmread16(VMX_GUEST_##SEG_NAME##_SEL); \
40-
seg.base = exec_vmread(VMX_GUEST_##SEG_NAME##_BASE); \
41-
seg.limit = exec_vmread32(VMX_GUEST_##SEG_NAME##_LIMIT); \
42-
seg.attr = exec_vmread32(VMX_GUEST_##SEG_NAME##_ATTR); \
38+
seg.selector = exec_vmread16(SEG_NAME##_SEL); \
39+
seg.base = exec_vmread(SEG_NAME##_BASE); \
40+
seg.limit = exec_vmread32(SEG_NAME##_LIMIT); \
41+
seg.attr = exec_vmread32(SEG_NAME##_ATTR); \
4342
}
4443

4544
#define load_segment(seg, SEG_NAME) \
4645
{ \
47-
exec_vmwrite16(VMX_GUEST_##SEG_NAME##_SEL, seg.selector); \
48-
exec_vmwrite(VMX_GUEST_##SEG_NAME##_BASE, seg.base); \
49-
exec_vmwrite32(VMX_GUEST_##SEG_NAME##_LIMIT, seg.limit); \
50-
exec_vmwrite32(VMX_GUEST_##SEG_NAME##_ATTR, seg.attr); \
46+
exec_vmwrite16(SEG_NAME##_SEL, seg.selector); \
47+
exec_vmwrite(SEG_NAME##_BASE, seg.base); \
48+
exec_vmwrite32(SEG_NAME##_LIMIT, seg.limit); \
49+
exec_vmwrite32(SEG_NAME##_ATTR, seg.attr); \
5150
}
5251

5352
#ifndef WORKAROUND_FOR_TRUSTY_4G_MEM
@@ -231,14 +230,14 @@ static void save_world_ctx(struct run_context *context)
231230
context->ia32_sysenter_esp = exec_vmread(VMX_GUEST_IA32_SYSENTER_ESP);
232231
context->ia32_sysenter_eip = exec_vmread(VMX_GUEST_IA32_SYSENTER_EIP);
233232
context->ia32_sysenter_cs = exec_vmread32(VMX_GUEST_IA32_SYSENTER_CS);
234-
save_segment(context->cs, CS);
235-
save_segment(context->ss, SS);
236-
save_segment(context->ds, DS);
237-
save_segment(context->es, ES);
238-
save_segment(context->fs, FS);
239-
save_segment(context->gs, GS);
240-
save_segment(context->tr, TR);
241-
save_segment(context->ldtr, LDTR);
233+
save_segment(context->cs, VMX_GUEST_CS);
234+
save_segment(context->ss, VMX_GUEST_SS);
235+
save_segment(context->ds, VMX_GUEST_DS);
236+
save_segment(context->es, VMX_GUEST_ES);
237+
save_segment(context->fs, VMX_GUEST_FS);
238+
save_segment(context->gs, VMX_GUEST_GS);
239+
save_segment(context->tr, VMX_GUEST_TR);
240+
save_segment(context->ldtr, VMX_GUEST_LDTR);
242241
/* Only base and limit for IDTR and GDTR */
243242
context->idtr.base = exec_vmread(VMX_GUEST_IDTR_BASE);
244243
context->gdtr.base = exec_vmread(VMX_GUEST_GDTR_BASE);
@@ -277,14 +276,14 @@ static void load_world_ctx(struct run_context *context)
277276
exec_vmwrite32(VMX_GUEST_IA32_SYSENTER_CS, context->ia32_sysenter_cs);
278277
exec_vmwrite(VMX_GUEST_IA32_SYSENTER_ESP, context->ia32_sysenter_esp);
279278
exec_vmwrite(VMX_GUEST_IA32_SYSENTER_EIP, context->ia32_sysenter_eip);
280-
load_segment(context->cs, CS);
281-
load_segment(context->ss, SS);
282-
load_segment(context->ds, DS);
283-
load_segment(context->es, ES);
284-
load_segment(context->fs, FS);
285-
load_segment(context->gs, GS);
286-
load_segment(context->tr, TR);
287-
load_segment(context->ldtr, LDTR);
279+
load_segment(context->cs, VMX_GUEST_CS);
280+
load_segment(context->ss, VMX_GUEST_SS);
281+
load_segment(context->ds, VMX_GUEST_DS);
282+
load_segment(context->es, VMX_GUEST_ES);
283+
load_segment(context->fs, VMX_GUEST_FS);
284+
load_segment(context->gs, VMX_GUEST_GS);
285+
load_segment(context->tr, VMX_GUEST_TR);
286+
load_segment(context->ldtr, VMX_GUEST_LDTR);
288287
/* Only base and limit for IDTR and GDTR */
289288
exec_vmwrite(VMX_GUEST_IDTR_BASE, context->idtr.base);
290289
exec_vmwrite(VMX_GUEST_GDTR_BASE, context->gdtr.base);

hypervisor/include/lib/macros.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@
1212
/** Replaces 'x' by its value. */
1313
#define CPP_STRING(x) __CPP_STRING(x)
1414

15-
/** Creates a bitfield mask.
16-
*
17-
* @param pos The position of the LSB within the mask.
18-
* @param width The width of the bitfield in bits.
19-
*
20-
* @return The bitfield mask.
21-
*/
22-
23-
#define BITFIELD_MASK(pos, width) (((1<<(width))-1)<<(pos))
24-
#define BITFIELD_VALUE(v, pos, width) (((v)<<(pos)) & (((1<<(width))-1)<<(pos)))
25-
26-
#define MAKE_BITFIELD_MASK(id) BITFIELD_MASK(id ## _POS, id ## _WIDTH)
27-
#define MAKE_BITFIELD_VALUE(v, id) BITFIELD_VALUE(v, id ## _POS, id ## _WIDTH)
28-
29-
/** Defines a register within a register block. */
30-
#define REGISTER(base, off) (base ## _BASE + (off))
31-
32-
#define MAKE_MMIO_REGISTER_ADDRESS(chip, module, register) \
33-
(chip ## _ ## module ## _BASE + \
34-
(chip ## _ ## module ## _ ## register ## _REGISTER))
35-
3615
/* Macro used to check if a value is aligned to the required boundary.
3716
* Returns TRUE if aligned; FALSE if not aligned
3817
* NOTE: The required alignment must be a power of 2 (2, 4, 8, 16, 32, etc)

0 commit comments

Comments
 (0)