1、 在linux kernel中读写ARM寄存器示例
#define FUNCTION_INVARIANT(reg) \
static void get_##reg(struct kvm_vcpu *v, \
const struct sys_reg_desc *r) \
{ \
u64 val; \
\
asm volatile("mrs %0, " __stringify(reg) "\n" \
: "=r" (val)); \
((struct sys_reg_desc *)r)->val = val; \
}
FUNCTION_INVARIANT(midr_el1)
FUNCTION_INVARIANT(ctr_el0)
FUNCTION_INVARIANT(revidr_el1)
FUNCTION_INVARIANT(id_pfr0_el1)
FUNCTION_INVARIANT(id_pfr1_el1)
FUNCTION_INVARIANT(id_dfr0_el1)
FUNCTION_INVARIANT(id_afr0_el1)
FUNCTION_INVARIANT(id_mmfr0_el1)
FUNCTION_INVARIANT(id_mmfr1_el1)
FUNCTION_INVARIANT(id_mmfr2_el1)
FUNCTION_INVARIANT(id_mmfr3_el1)
FUNCTION_INVARIANT(id_isar0_el1)
FUNCTION_INVARIANT(id_isar1_el1)
FUNCTION_INVARIANT(id_isar2_el1)
FUNCTION_INVARIANT(id_isar3_el1)
FUNCTION_INVARIANT(id_isar4_el1)
FUNCTION_INVARIANT(id_isar5_el1)
FUNCTION_INVARIANT(clidr_el1)
FUNCTION_INVARIANT(aidr_el1)
2、 在optee中读写ARM寄存器示例
#define DEFINE_REG_READ_FUNC_(reg, type, asmreg) \
static inline type read_##reg(void) \
{ \
type val; \
\
asm volatile("mrs %0, " #asmreg : "=r" (val)); \
return val; \
}
#define DEFINE_REG_WRITE_FUNC_(reg, type, asmreg) \
static inline void write_##reg(type val) \
{ \
asm volatile("msr " #asmreg ", %0" : : "r" (val)); \
}
#define DEFINE_U32_REG_READ_FUNC(reg) \
DEFINE_REG_READ_FUNC_(reg, uint32_t, reg)
#define DEFINE_U32_REG_WRITE_FUNC(reg) \
DEFINE_REG_WRITE_FUNC_(reg, uint32_t, reg)
#define DEFINE_U32_REG_READWRITE_FUNCS(reg) \
DEFINE_U32_REG_READ_FUNC(reg) \
DEFINE_U32_REG_WRITE_FUNC(reg)
#define DEFINE_U64_REG_READ_FUNC(reg) \
DEFINE_REG_READ_FUNC_(reg, uint64_t, reg)
#define DEFINE_U64_REG_WRITE_FUNC(reg) \
DEFINE_REG_WRITE_FUNC_(reg, uint64_t, reg)
#define DEFINE_U64_REG_READWRITE_FUNCS(reg) \
DEFINE_U64_REG_READ_FUNC(reg) \
DEFINE_U64_REG_WRITE_FUNC(reg)
DEFINE_U32_REG_READWRITE_FUNCS(cpacr_el1)
DEFINE_U32_REG_READWRITE_FUNCS(daif)
DEFINE_U32_REG_READWRITE_FUNCS(fpcr)
DEFINE_U32_REG_READWRITE_FUNCS(fpsr)
DEFINE_U32_REG_READ_FUNC(contextidr_el1)
DEFINE_U32_REG_READ_FUNC(sctlr_el1)
DEFINE_REG_READ_FUNC_(cntfrq, uint32_t, cntfrq_el0)
DEFINE_REG_READ_FUNC_(cntpct, uint64_t, cntpct_el0)
DEFINE_REG_READ_FUNC_(cntkctl, uint32_t, cntkctl_el1)
DEFINE_REG_WRITE_FUNC_(cntkctl, uint32_t, cntkctl_el1)
DEFINE_U64_REG_READWRITE_FUNCS(ttbr0_el1)
DEFINE_U64_REG_READWRITE_FUNCS(ttbr1_el1)
DEFINE_U64_REG_READWRITE_FUNCS(tcr_el1)
DEFINE_U64_REG_READ_FUNC(esr_el1)
DEFINE_U64_REG_READ_FUNC(far_el1)
DEFINE_U64_REG_READ_FUNC(mpidr_el1)
DEFINE_U64_REG_READ_FUNC(midr_el1)
#define read_midr() read_midr_el1()
DEFINE_U64_REG_READ_FUNC(par_el1)
DEFINE_U64_REG_WRITE_FUNC(mair_el1)
DEFINE_REG_READ_FUNC_(icc_ctlr, uint32_t, S3_0_C12_C12_4)
DEFINE_REG_WRITE_FUNC_(icc_ctlr, uint32_t, S3_0_C12_C12_4)
DEFINE_REG_WRITE_FUNC_(icc_pmr, uint32_t, S3_0_C4_C6_0)
DEFINE_REG_READ_FUNC_(icc_iar0, uint32_t, S3_0_c12_c8_0)
DEFINE_REG_READ_FUNC_(icc_iar1, uint32_t, S3_0_c12_c12_0)
DEFINE_REG_WRITE_FUNC_(icc_eoir0, uint32_t, S3_0_c12_c8_1)
DEFINE_REG_WRITE_FUNC_(icc_eoir1, uint32_t, S3_0_c12_c12_1)
3、自定义log打印函数
#define XXXX_PRINTK(fmt, ...) ALOGI(fmt, ##__VA_ARGS__)
#define XXXX_ENTER() XXXX_PRINTK("%d : %s :enter",__LINE__, __func__)
#define XXXX_EXIT() XXXX_PRINTK("%d : %s :exit",__LINE__, __func__)