[PATCH v5 1/7] hv: change sbuf to a common infrastructure


Conghui Chen
 

sbuf is now only used for debug purpose, but later, it will be used as a
common interfaces. So, move the sbuf related code out of the debug directory.

Signed-off-by: Conghui <conghui.chen@...>
Acked-by: Eddie Dong <eddie.dong@...>
---
hypervisor/Makefile | 1 +
hypervisor/common/sbuf.c | 82 +++++++++++++++++++++++++
hypervisor/debug/sbuf.c | 64 -------------------
hypervisor/include/common/sbuf.h | 25 ++++++++
hypervisor/include/debug/sbuf.h | 71 ---------------------
hypervisor/include/public/acrn_common.h | 46 ++++++++++++++
6 files changed, 154 insertions(+), 135 deletions(-)
create mode 100644 hypervisor/common/sbuf.c
create mode 100644 hypervisor/include/common/sbuf.h
delete mode 100644 hypervisor/include/debug/sbuf.h

diff --git a/hypervisor/Makefile b/hypervisor/Makefile
index bd45b539f..b72849720 100644
--- a/hypervisor/Makefile
+++ b/hypervisor/Makefile
@@ -238,6 +238,7 @@ HW_C_SRCS += common/softirq.c
HW_C_SRCS += common/schedule.c
HW_C_SRCS += common/event.c
HW_C_SRCS += common/efi_mmap.c
+HW_C_SRCS += common/sbuf.c
ifeq ($(CONFIG_SCHED_NOOP),y)
HW_C_SRCS += common/sched_noop.c
endif
diff --git a/hypervisor/common/sbuf.c b/hypervisor/common/sbuf.c
new file mode 100644
index 000000000..533ff712c
--- /dev/null
+++ b/hypervisor/common/sbuf.c
@@ -0,0 +1,82 @@
+/*
+ * SHARED BUFFER
+ *
+ * Copyright (C) 2017-2022 Intel Corporation.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Li Fei <fei1.li@...>
+ *
+ */
+
+#include <types.h>
+#include <rtl.h>
+#include <errno.h>
+#include <asm/cpu.h>
+#include <asm/per_cpu.h>
+
+uint32_t sbuf_next_ptr(uint32_t pos_arg,
+ uint32_t span, uint32_t scope)
+{
+ uint32_t pos = pos_arg;
+ pos += span;
+ pos = (pos >= scope) ? (pos - scope) : pos;
+ return pos;
+}
+
+/**
+ * The high caller should guarantee each time there must have
+ * sbuf->ele_size data can be write form data and this function
+ * should guarantee execution atomically.
+ *
+ * flag:
+ * If OVERWRITE_EN set, buf can store (ele_num - 1) elements at most.
+ * Should use lock to guarantee that only one read or write at
+ * the same time.
+ * if OVERWRITE_EN not set, buf can store (ele_num - 1) elements
+ * at most. Shouldn't modify the sbuf->head.
+ *
+ * return:
+ * ele_size: write succeeded.
+ * 0: no write, buf is full
+ * negative: failed.
+ */
+
+uint32_t sbuf_put(struct shared_buf *sbuf, uint8_t *data)
+{
+ void *to;
+ uint32_t next_tail;
+ uint32_t ele_size;
+ bool trigger_overwrite = false;
+
+ stac();
+ next_tail = sbuf_next_ptr(sbuf->tail, sbuf->ele_size, sbuf->size);
+ /* if this write would trigger overrun */
+ if (next_tail == sbuf->head) {
+ /* accumulate overrun count if necessary */
+ sbuf->overrun_cnt += sbuf->flags & OVERRUN_CNT_EN;
+ if ((sbuf->flags & OVERWRITE_EN) == 0U) {
+ /* if not enable over write, return here. */
+ clac();
+ return 0;
+ }
+ trigger_overwrite = true;
+ }
+
+ to = (void *)sbuf + SBUF_HEAD_SIZE + sbuf->tail;
+
+ (void)memcpy_s(to, sbuf->ele_size, data, sbuf->ele_size);
+
+ if (trigger_overwrite) {
+ sbuf->head = sbuf_next_ptr(sbuf->head,
+ sbuf->ele_size, sbuf->size);
+ }
+ sbuf->tail = next_tail;
+
+ ele_size = sbuf->ele_size;
+ clac();
+
+ return ele_size;
+}
+
+
diff --git a/hypervisor/debug/sbuf.c b/hypervisor/debug/sbuf.c
index 6d0c016fc..8b8ec4605 100644
--- a/hypervisor/debug/sbuf.c
+++ b/hypervisor/debug/sbuf.c
@@ -15,70 +15,6 @@
#include <asm/cpu.h>
#include <asm/per_cpu.h>

-uint32_t sbuf_next_ptr(uint32_t pos_arg,
- uint32_t span, uint32_t scope)
-{
- uint32_t pos = pos_arg;
- pos += span;
- pos = (pos >= scope) ? (pos - scope) : pos;
- return pos;
-}
-
-/**
- * The high caller should guarantee each time there must have
- * sbuf->ele_size data can be write form data and this function
- * should guarantee execution atomically.
- *
- * flag:
- * If OVERWRITE_EN set, buf can store (ele_num - 1) elements at most.
- * Should use lock to guarantee that only one read or write at
- * the same time.
- * if OVERWRITE_EN not set, buf can store (ele_num - 1) elements
- * at most. Shouldn't modify the sbuf->head.
- *
- * return:
- * ele_size: write succeeded.
- * 0: no write, buf is full
- * negative: failed.
- */
-
-uint32_t sbuf_put(struct shared_buf *sbuf, uint8_t *data)
-{
- void *to;
- uint32_t next_tail;
- uint32_t ele_size;
- bool trigger_overwrite = false;
-
- stac();
- next_tail = sbuf_next_ptr(sbuf->tail, sbuf->ele_size, sbuf->size);
- /* if this write would trigger overrun */
- if (next_tail == sbuf->head) {
- /* accumulate overrun count if necessary */
- sbuf->overrun_cnt += sbuf->flags & OVERRUN_CNT_EN;
- if ((sbuf->flags & OVERWRITE_EN) == 0U) {
- /* if not enable over write, return here. */
- clac();
- return 0;
- }
- trigger_overwrite = true;
- }
-
- to = (void *)sbuf + SBUF_HEAD_SIZE + sbuf->tail;
-
- (void)memcpy_s(to, sbuf->ele_size, data, sbuf->ele_size);
-
- if (trigger_overwrite) {
- sbuf->head = sbuf_next_ptr(sbuf->head,
- sbuf->ele_size, sbuf->size);
- }
- sbuf->tail = next_tail;
-
- ele_size = sbuf->ele_size;
- clac();
-
- return ele_size;
-}
-
int32_t sbuf_share_setup(uint16_t pcpu_id, uint32_t sbuf_id, uint64_t *hva)
{
if ((pcpu_id >= get_pcpu_nums()) || (sbuf_id >= ACRN_SBUF_ID_MAX)) {
diff --git a/hypervisor/include/common/sbuf.h b/hypervisor/include/common/sbuf.h
new file mode 100644
index 000000000..c5440f5a4
--- /dev/null
+++ b/hypervisor/include/common/sbuf.h
@@ -0,0 +1,25 @@
+/*
+ * SHARED BUFFER
+ *
+ * Copyright (C) 2017-2022 Intel Corporation.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Li Fei <fei1.li@...>
+ *
+ */
+
+#ifndef SHARED_BUFFER_H
+#define SHARED_BUFFER_H
+#include <acrn_common.h>
+#include <asm/guest/vm.h>
+/**
+ *@pre sbuf != NULL
+ *@pre data != NULL
+ */
+uint32_t sbuf_put(struct shared_buf *sbuf, uint8_t *data);
+int32_t sbuf_share_setup(uint16_t cpu_id, uint32_t sbuf_id, uint64_t *hva);
+void sbuf_reset(void);
+uint32_t sbuf_next_ptr(uint32_t pos, uint32_t span, uint32_t scope);
+
+#endif /* SHARED_BUFFER_H */
diff --git a/hypervisor/include/debug/sbuf.h b/hypervisor/include/debug/sbuf.h
deleted file mode 100644
index 87c892c17..000000000
--- a/hypervisor/include/debug/sbuf.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * SHARED BUFFER
- *
- * Copyright (C) 2017-2022 Intel Corporation.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- * Li Fei <fei1.li@...>
- *
- */
-
-#ifndef SHARED_BUFFER_H
-#define SHARED_BUFFER_H
-
-#define SBUF_MAGIC 0x5aa57aa71aa13aa3UL
-#define SBUF_MAX_SIZE (1UL << 22U)
-#define SBUF_HEAD_SIZE 64U
-
-/* sbuf flags */
-#define OVERRUN_CNT_EN (1U << 0U) /* whether overrun counting is enabled */
-#define OVERWRITE_EN (1U << 1U) /* whether overwrite is enabled */
-
-/**
- * (sbuf) head + buf (store (ele_num - 1) elements at most)
- * buffer empty: tail == head
- * buffer full: (tail + ele_size) % size == head
- *
- * Base of memory for elements
- * |
- * |
- * ----------------------------------------------------------------------
- * | struct shared_buf | raw data (ele_size)| ... | raw data (ele_size) |
- * ----------------------------------------------------------------------
- * |
- * |
- * struct shared_buf *buf
- */
-
-enum {
- ACRN_TRACE = 0U,
- ACRN_HVLOG,
- ACRN_SEP,
- ACRN_SOCWATCH,
- ACRN_SBUF_ID_MAX,
-};
-
-/* Make sure sizeof(struct shared_buf) == SBUF_HEAD_SIZE */
-struct shared_buf {
- uint64_t magic;
- uint32_t ele_num; /* number of elements */
- uint32_t ele_size; /* sizeof of elements */
- uint32_t head; /* offset from base, to read */
- uint32_t tail; /* offset from base, to write */
- uint32_t flags;
- uint32_t reserved;
- uint32_t overrun_cnt; /* count of overrun */
- uint32_t size; /* ele_num * ele_size */
- uint32_t padding[6];
-};
-
-
-/**
- *@pre sbuf != NULL
- *@pre data != NULL
- */
-uint32_t sbuf_put(struct shared_buf *sbuf, uint8_t *data);
-int32_t sbuf_share_setup(uint16_t pcpu_id, uint32_t sbuf_id, uint64_t *hva);
-void sbuf_reset(void);
-uint32_t sbuf_next_ptr(uint32_t pos, uint32_t span, uint32_t scope);
-
-#endif /* SHARED_BUFFER_H */
diff --git a/hypervisor/include/public/acrn_common.h b/hypervisor/include/public/acrn_common.h
index fee71a655..7c73ca070 100644
--- a/hypervisor/include/public/acrn_common.h
+++ b/hypervisor/include/public/acrn_common.h
@@ -717,6 +717,52 @@ struct acrn_vdev {
uint8_t args[128];
};

+#define SBUF_MAGIC 0x5aa57aa71aa13aa3UL
+#define SBUF_MAX_SIZE (1UL << 22U)
+#define SBUF_HEAD_SIZE 64U
+
+/* sbuf flags */
+#define OVERRUN_CNT_EN (1U << 0U) /* whether overrun counting is enabled */
+#define OVERWRITE_EN (1U << 1U) /* whether overwrite is enabled */
+
+/**
+ * (sbuf) head + buf (store (ele_num - 1) elements at most)
+ * buffer empty: tail == head
+ * buffer full: (tail + ele_size) % size == head
+ *
+ * Base of memory for elements
+ * |
+ * |
+ * ----------------------------------------------------------------------
+ * | struct shared_buf | raw data (ele_size)| ... | raw data (ele_size) |
+ * ----------------------------------------------------------------------
+ * |
+ * |
+ * struct shared_buf *buf
+ */
+
+enum {
+ ACRN_TRACE = 0U,
+ ACRN_HVLOG,
+ ACRN_SEP,
+ ACRN_SOCWATCH,
+ ACRN_SBUF_ID_MAX,
+};
+
+/* Make sure sizeof(struct shared_buf) == SBUF_HEAD_SIZE */
+struct shared_buf {
+ uint64_t magic;
+ uint32_t ele_num; /* number of elements */
+ uint32_t ele_size; /* sizeof of elements */
+ uint32_t head; /* offset from base, to read */
+ uint32_t tail; /* offset from base, to write */
+ uint32_t flags;
+ uint32_t reserved;
+ uint32_t overrun_cnt; /* count of overrun */
+ uint32_t size; /* ele_num * ele_size */
+ uint32_t padding[6];
+};
+
/**
* @}
*/
--
2.25.1