Skip to content

Commit 8c7aded

Browse files
cjwinklhoferkartben
authored andcommitted
logging: Assign module data to memory partition 'k_log_partition'
The logging module data is assigned to the memory partition 'k_log_partition' so that a user mode thread can access this data (see k_mem_domain_add_thread()). The 'k_log_partition' is created when: - CONFIG_USERSPACE=y - CONFIG_LOG_ALWAYS_RUNTIME=y The option CONFIG_NO_OPTIMIZATIONS=y forces the logging module to use the runtime message creation CONFIG_LOG_ALWAYS_RUNTIME=y. This raises a MPU violation when logging is used in a user mode thread since this thread is not allowed to access the module data (e.g. __log_level, __log_current_const_data). Note that the user mode thread may also require access to the partition 'z_libc_partition'. Signed-off-by: Christoph Winklhofer <[email protected]>
1 parent 7792565 commit 8c7aded

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

include/zephyr/logging/log.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <zephyr/logging/log_core.h>
1212
#include <zephyr/sys/iterable_sections.h>
1313

14+
#if CONFIG_USERSPACE && CONFIG_LOG_ALWAYS_RUNTIME
15+
#include <zephyr/app_memory/app_memdomain.h>
16+
#endif
17+
1418
#ifdef __cplusplus
1519
extern "C" {
1620
#endif
@@ -355,6 +359,16 @@ void z_log_vprintk(const char *fmt, va_list ap);
355359
(Z_LOG_EVAL(_LOG_LEVEL_RESOLVE(__VA_ARGS__), (1), (0))) \
356360
)), (0))
357361

362+
/* Determine if the data of the log module shall be in the partition
363+
* 'k_log_partition' to allow a user mode thread access to this data.
364+
*/
365+
#if CONFIG_USERSPACE && CONFIG_LOG_ALWAYS_RUNTIME
366+
extern struct k_mem_partition k_log_partition;
367+
#define Z_LOG_MODULE_PARTITION(_k_app_mem) _k_app_mem(k_log_partition)
368+
#else
369+
#define Z_LOG_MODULE_PARTITION(_k_app_mem)
370+
#endif
371+
358372
/**
359373
* @brief Create module-specific state and register the module with Logger.
360374
*
@@ -427,19 +441,22 @@ void z_log_vprintk(const char *fmt, va_list ap);
427441
extern struct log_source_dynamic_data \
428442
LOG_ITEM_DYNAMIC_DATA(GET_ARG_N(1, __VA_ARGS__)); \
429443
\
444+
Z_LOG_MODULE_PARTITION(K_APP_DMEM) \
430445
static const struct log_source_const_data * \
431446
__log_current_const_data __unused = \
432447
Z_DO_LOG_MODULE_REGISTER(__VA_ARGS__) ? \
433448
&Z_LOG_ITEM_CONST_DATA(GET_ARG_N(1, __VA_ARGS__)) : \
434449
NULL; \
435450
\
451+
Z_LOG_MODULE_PARTITION(K_APP_DMEM) \
436452
static struct log_source_dynamic_data * \
437453
__log_current_dynamic_data __unused = \
438454
(Z_DO_LOG_MODULE_REGISTER(__VA_ARGS__) && \
439455
IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) ? \
440456
&LOG_ITEM_DYNAMIC_DATA(GET_ARG_N(1, __VA_ARGS__)) : \
441457
NULL; \
442458
\
459+
Z_LOG_MODULE_PARTITION(K_APP_BMEM) \
443460
static const uint32_t __log_level __unused = \
444461
_LOG_LEVEL_RESOLVE(__VA_ARGS__)
445462

subsys/logging/log_core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include <zephyr/posix/time.h>
2828
#endif
2929

30+
#if CONFIG_USERSPACE && CONFIG_LOG_ALWAYS_RUNTIME
31+
#include <zephyr/app_memory/app_memdomain.h>
32+
K_APPMEM_PARTITION_DEFINE(k_log_partition);
33+
#endif
34+
3035
LOG_MODULE_REGISTER(log);
3136

3237
#ifndef CONFIG_LOG_PROCESS_THREAD_SLEEP_MS

0 commit comments

Comments
 (0)