From 65671d73c702eb65ca9484b6d11e57b9e9726ac7 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Fri, 26 Aug 2022 23:30:00 +0200 Subject: [PATCH] misc: Update several macros --- common/lib/part.h | 4 ++-- common/lib/print.h | 4 +++- common/protos/multiboot2.c | 4 +++- common/sys/cpu.h | 43 +++++++++++++++++++------------------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/common/lib/part.h b/common/lib/part.h index e092a17b..f5230fae 100644 --- a/common/lib/part.h +++ b/common/lib/part.h @@ -74,7 +74,7 @@ struct volume *volume_get_by_bios_drive(int drive); bool volume_read(struct volume *part, void *buffer, uint64_t loc, uint64_t count); -#define volume_iterate_parts(_VOLUME_, _BODY_) ({ \ +#define volume_iterate_parts(_VOLUME_, _BODY_) do { \ struct volume *_VOLUME = _VOLUME_; \ if (_VOLUME->pxe) { \ do { \ @@ -101,6 +101,6 @@ bool volume_read(struct volume *part, void *buffer, uint64_t loc, uint64_t count _BODY_ \ } \ } \ -}) +} while (0) #endif diff --git a/common/lib/print.h b/common/lib/print.h index 8ceb95a1..30012338 100644 --- a/common/lib/print.h +++ b/common/lib/print.h @@ -9,6 +9,8 @@ extern bool verbose; void print(const char *fmt, ...); void vprint(const char *fmt, va_list args); -#define printv(FMT, ...) ({ if (verbose) print(FMT, ##__VA_ARGS__); }) +#define printv(FMT, ...) do { \ + if (verbose) print(FMT, ##__VA_ARGS__); \ +} while (0) #endif diff --git a/common/protos/multiboot2.c b/common/protos/multiboot2.c index 4f20978d..b21b4fda 100644 --- a/common/protos/multiboot2.c +++ b/common/protos/multiboot2.c @@ -59,7 +59,9 @@ static size_t get_multiboot2_info_size( ALIGN_UP(sizeof(struct multiboot_tag), MULTIBOOT_TAG_ALIGN); // end } -#define append_tag(P, TAG) ({ (P) += ALIGN_UP((TAG)->size, MULTIBOOT_TAG_ALIGN); }) +#define append_tag(P, TAG) do { \ + (P) += ALIGN_UP((TAG)->size, MULTIBOOT_TAG_ALIGN); \ +} while (0) noreturn void multiboot2_load(char *config, char* cmdline) { struct file_handle *kernel_file; diff --git a/common/sys/cpu.h b/common/sys/cpu.h index 62847b4d..335e5556 100644 --- a/common/sys/cpu.h +++ b/common/sys/cpu.h @@ -167,58 +167,57 @@ inline void delay(uint64_t cycles) { } #define rdrand(type) ({ \ - type ret; \ + type rdrand__ret; \ asm volatile ( \ "1: " \ "rdrand %0;" \ "jnc 1b;" \ - : "=r" (ret) \ + : "=r" (rdrand__ret) \ ); \ - ret; \ + rdrand__ret; \ }) #define rdseed(type) ({ \ - type ret; \ + type rdseed__ret; \ asm volatile ( \ "1: " \ "rdseed %0;" \ "jnc 1b;" \ - : "=r" (ret) \ + : "=r" (rdseed__ret) \ ); \ - ret; \ + rdseed__ret; \ }) -#define write_cr(reg, val) ({ \ +#define write_cr(reg, val) do { \ asm volatile ("mov %0, %%cr" reg :: "r" (val) : "memory"); \ -}) +} while (0) #define read_cr(reg) ({ \ - size_t cr; \ - asm volatile ("mov %%cr" reg ", %0" : "=r" (cr) :: "memory"); \ - cr; \ + size_t read_cr__cr; \ + asm volatile ("mov %%cr" reg ", %0" : "=r" (read_cr__cr) :: "memory"); \ + read_cr__cr; \ }) #define locked_read(var) ({ \ - typeof(*var) ret = 0; \ + typeof(*var) locked_read__ret = 0; \ asm volatile ( \ "lock xadd %0, %1" \ - : "+r" (ret) \ + : "+r" (locked_read__ret) \ : "m" (*(var)) \ : "memory" \ ); \ - ret; \ + locked_read__ret; \ }) -#define locked_write(var, val) ({ \ - typeof(*var) ret = val; \ +#define locked_write(var, val) do { \ + __auto_type locked_write__ret = val; \ asm volatile ( \ "lock xchg %0, %1" \ - : "+r" ((ret)) \ + : "+r" ((locked_write__ret)) \ : "m" (*(var)) \ : "memory" \ ); \ - ret; \ -}) +} while (0) #elif defined (__aarch64__) @@ -229,14 +228,14 @@ inline uint64_t rdtsc(void) { } #define locked_read(var) ({ \ - typeof(*var) ret = 0; \ + typeof(*var) locked_read__ret = 0; \ asm volatile ( \ "ldar %0, %1" \ - : "=r" (ret) \ + : "=r" (locked_read__ret) \ : "m" (*(var)) \ : "memory" \ ); \ - ret; \ + locked_read__ret; \ }) inline size_t icache_line_size(void) {