diff --git a/include/zephyr/ipc/pbuf.h b/include/zephyr/ipc/pbuf.h index 983a0687dbb..8783cdbbf14 100644 --- a/include/zephyr/ipc/pbuf.h +++ b/include/zephyr/ipc/pbuf.h @@ -35,6 +35,13 @@ extern "C" { */ #define _PBUF_MIN_DATA_LEN ROUND_UP(PBUF_PACKET_LEN_SZ + 1 + _PBUF_IDX_SIZE, _PBUF_IDX_SIZE) +#if defined(CONFIG_ARCH_POSIX) +/* For the native simulated boards we need to modify some pointers at init */ +#define PBUF_MAYBE_CONST +#else +#define PBUF_MAYBE_CONST const +#endif + /** @brief Control block of packet buffer. * * The structure contains configuration data. @@ -87,9 +94,9 @@ struct pbuf_data { * written in a way to protect the data from being corrupted. */ struct pbuf { - const struct pbuf_cfg *const cfg; /* Configuration of the - * buffer. - */ + PBUF_MAYBE_CONST struct pbuf_cfg *const cfg; /* Configuration of the + * buffer. + */ struct pbuf_data data; /* Data used to read and write * to the buffer */ @@ -144,8 +151,7 @@ struct pbuf { "Misaligned memory."); \ BUILD_ASSERT(size >= (MAX(dcache_align, _PBUF_IDX_SIZE) + _PBUF_IDX_SIZE + \ _PBUF_MIN_DATA_LEN), "Insufficient size."); \ - \ - static const struct pbuf_cfg cfg_##name = \ + static PBUF_MAYBE_CONST struct pbuf_cfg cfg_##name = \ PBUF_CFG_INIT(mem_addr, size, dcache_align); \ static struct pbuf name = { \ .cfg = &cfg_##name, \ diff --git a/subsys/ipc/ipc_service/backends/ipc_icbmsg.c b/subsys/ipc/ipc_service/backends/ipc_icbmsg.c index 624896d657a..daf95e9a021 100644 --- a/subsys/ipc/ipc_service/backends/ipc_icbmsg.c +++ b/subsys/ipc/ipc_service/backends/ipc_icbmsg.c @@ -87,6 +87,13 @@ #include #include +#if defined(CONFIG_ARCH_POSIX) +#include +#define MAYBE_CONST +#else +#define MAYBE_CONST const +#endif + LOG_MODULE_REGISTER(ipc_icbmsg, CONFIG_IPC_SERVICE_BACKEND_ICBMSG_LOG_LEVEL); @@ -1178,12 +1185,17 @@ static int release_rx_buffer(const struct device *instance, void *token, void *d */ static int backend_init(const struct device *instance) { - const struct icbmsg_config *conf = instance->config; + MAYBE_CONST struct icbmsg_config *conf = (struct icbmsg_config *)instance->config; struct backend_data *dev_data = instance->data; #ifdef CONFIG_MULTITHREADING static K_THREAD_STACK_DEFINE(ep_bound_work_q_stack, EP_BOUND_WORK_Q_STACK_SIZE); static bool is_work_q_started; +#if defined(CONFIG_ARCH_POSIX) + native_emb_addr_remap((void **)&conf->tx.blocks_ptr); + native_emb_addr_remap((void **)&conf->rx.blocks_ptr); +#endif + if (!is_work_q_started) { k_work_queue_init(&ep_bound_work_q); k_work_queue_start(&ep_bound_work_q, ep_bound_work_q_stack, @@ -1341,7 +1353,7 @@ const static struct ipc_service_backend backend_ops = { .rx_pb = &rx_icbmsg_pb_##i, \ } \ }; \ - static const struct icbmsg_config backend_config_##i = \ + static MAYBE_CONST struct icbmsg_config backend_config_##i = \ { \ .control_config = { \ .mbox_tx = MBOX_DT_SPEC_INST_GET(i, tx), \ diff --git a/subsys/ipc/ipc_service/lib/pbuf.c b/subsys/ipc/ipc_service/lib/pbuf.c index 19ac2525c28..c744946f4ca 100644 --- a/subsys/ipc/ipc_service/lib/pbuf.c +++ b/subsys/ipc/ipc_service/lib/pbuf.c @@ -11,6 +11,10 @@ #include #include +#if defined(CONFIG_ARCH_POSIX) +#include +#endif + /* Helper funciton for getting numer of bytes being written to the bufer. */ static uint32_t idx_occupied(uint32_t len, uint32_t wr_idx, uint32_t rd_idx) { @@ -54,11 +58,23 @@ static int validate_cfg(const struct pbuf_cfg *cfg) return 0; } +#if defined(CONFIG_ARCH_POSIX) +void pbuf_native_addr_remap(struct pbuf *pb) +{ + native_emb_addr_remap((void **)&pb->cfg->rd_idx_loc); + native_emb_addr_remap((void **)&pb->cfg->wr_idx_loc); + native_emb_addr_remap((void **)&pb->cfg->data_loc); +} +#endif + int pbuf_tx_init(struct pbuf *pb) { if (validate_cfg(pb->cfg) != 0) { return -EINVAL; } +#if defined(CONFIG_ARCH_POSIX) + pbuf_native_addr_remap(pb); +#endif /* Initialize local copy of indexes. */ pb->data.wr_idx = 0; @@ -82,6 +98,10 @@ int pbuf_rx_init(struct pbuf *pb) if (validate_cfg(pb->cfg) != 0) { return -EINVAL; } +#if defined(CONFIG_ARCH_POSIX) + pbuf_native_addr_remap(pb); +#endif + /* Initialize local copy of indexes. */ pb->data.wr_idx = 0; pb->data.rd_idx = 0; diff --git a/tests/subsys/ipc/pbuf/src/main.c b/tests/subsys/ipc/pbuf/src/main.c index 9eb26695c08..4af9da68c47 100644 --- a/tests/subsys/ipc/pbuf/src/main.c +++ b/tests/subsys/ipc/pbuf/src/main.c @@ -48,7 +48,7 @@ ZTEST(test_pbuf, test_rw) * order to avoid clang complains about memory_area not being constant * expression. */ - static const struct pbuf_cfg cfg = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0); + static PBUF_MAYBE_CONST struct pbuf_cfg cfg = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0); static struct pbuf pb = { .cfg = &cfg, @@ -115,9 +115,9 @@ ZTEST(test_pbuf, test_retcodes) * order to avoid clang complains about memory_area not being constant * expression. */ - static const struct pbuf_cfg cfg0 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 32); - static const struct pbuf_cfg cfg1 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0); - static const struct pbuf_cfg cfg2 = PBUF_CFG_INIT(memory_area, 20, 4); + static PBUF_MAYBE_CONST struct pbuf_cfg cfg0 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 32); + static PBUF_MAYBE_CONST struct pbuf_cfg cfg1 = PBUF_CFG_INIT(memory_area, MEM_AREA_SZ, 0); + static PBUF_MAYBE_CONST struct pbuf_cfg cfg2 = PBUF_CFG_INIT(memory_area, 20, 4); static struct pbuf pb0 = { .cfg = &cfg0, @@ -268,7 +268,7 @@ ZTEST(test_pbuf, test_stress) * order to avoid clang complains about buffer not being constant * expression. */ - static const struct pbuf_cfg cfg = PBUF_CFG_INIT(buffer, MEM_AREA_SZ, 4); + static PBUF_MAYBE_CONST struct pbuf_cfg cfg = PBUF_CFG_INIT(buffer, MEM_AREA_SZ, 4); static struct pbuf pb = { .cfg = &cfg,