From: George Popescu <georgepope@xxxxxxxxxx> Use init_kvm_debug_buffer macro to init a pointer to the kvm_debug_buffer and a pointer to the write_index. It is needed a hyp/nVHE version and a kernel version because there are required different functions to extract the per_cpu data. Iterate through the buffer using the for_each_kvm_debug_buffer_slot. The parameters are the buffer's name, the buffer's type, a pointer of the type of the buffer, which is used to iterate through it, an (unsigned long *) to compute the write index and an unsigned long iterator. Get the buffer's next empty slot using the kvm_debug_buffer_next_slot function, the required parameters are a pointer to the buffer start, a pointer to the writing index, the stored type size and the allocated size of the buffer. This function has a meaning only inside hyp/nVHE, because it shouldn't be possible to write inside the buffer from the kernel. Signed-off-by: George Popescu <georgepope@xxxxxxxxxx> --- arch/arm64/include/asm/kvm_debug_buffer.h | 31 +++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_debug_buffer.h b/arch/arm64/include/asm/kvm_debug_buffer.h index 30c9b0b1a7bf..e451c11a77a7 100644 --- a/arch/arm64/include/asm/kvm_debug_buffer.h +++ b/arch/arm64/include/asm/kvm_debug_buffer.h @@ -17,10 +17,37 @@ DECLARE_PER_CPU(type_name, buff_name)[(size)]; \ DECLARE_PER_CPU(unsigned long, buff_name##_wr_ind) +static inline void *kvm_debug_buffer_next_slot(void *buff, unsigned long *buff_ind, + unsigned int struct_size, unsigned long buff_size) +{ + void *res = NULL; + + if (*buff_ind < buff_size) { + res = buff + (*buff_ind * struct_size); + *buff_ind = *buff_ind + 1; + } + return res; +} + +#define init_kvm_debug_buffer(buff_name, buff_type, buff_pointer, write_ind) \ + do { \ + buff = (buff_type *) __hyp_this_cpu_ptr(buff_name); \ + buff_ind = (unsigned long *) __hyp_this_cpu_ptr(buff_name##_wr_ind); \ + } while (0) + #else -#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ - DECLARE_PER_CPU(type_name, kvm_nvhe_sym(buff_name))[(size)]; \ +#define init_kvm_debug_buffer(buff_name, buff_type, buff_pointer, write_ind) \ + do { \ + buff_pointer = (buff_type *) this_cpu_ptr_nvhe(buff_name); \ + write_ind = (unsigned long *) this_cpu_ptr_nvhe(buff_name##_wr_ind); \ + } while (0) + +#define for_each_kvm_debug_buffer_slot(slot, write_ind, it) \ + for ((it) = 0; (it) < *(write_ind); ++(it), ++(slot)) + +#define DECLARE_KVM_DEBUG_BUFFER(type_name, buff_name, size) \ + DECLARE_PER_CPU(type_name, kvm_nvhe_sym(buff_name))[(size)]; \ DECLARE_PER_CPU(unsigned long, kvm_nvhe_sym(buff_name##_wr_ind)) #endif //__KVM_NVHE_HYPERVISOR__ -- 2.28.0.618.gf4bc123cb7-goog