Skip to content

Commit 7792565

Browse files
cjwinklhoferkartben
authored andcommitted
logging: Fix runtime message creation in user-mode thread
The runtime message creation (CONFIG_LOG_ALWAYS_RUNTIME=y) in a user- mode thread raises a MPU violation, e.g. call LOG_INF("Test") in a user-mode thread. The function 'z_log_msg_runtime_vcreate' runs in user mode but works with Kernel data. Hence, create the cbprintf package on the stack (of the user mode thread) and pass it further to the syscall 'z_log_msg_static_create'. Signed-off-by: Christoph Winklhofer <[email protected]>
1 parent 83aafaf commit 7792565

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

subsys/logging/log_msg.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ void z_log_msg_runtime_vcreate(uint8_t domain_id, const void *source,
372372
struct log_msg_desc desc =
373373
Z_LOG_MSG_DESC_INITIALIZER(domain_id, level, plen, dlen);
374374

375-
if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) && BACKENDS_IN_USE()) {
375+
if (IS_ENABLED(CONFIG_USERSPACE) && k_is_user_context()) {
376+
pkg = alloca(plen);
377+
msg = NULL;
378+
} else if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) && BACKENDS_IN_USE()) {
376379
msg = z_log_msg_alloc(msg_wlen);
377380
if (IS_ENABLED(CONFIG_LOG_FRONTEND) && msg == NULL) {
378381
pkg = alloca(plen);
@@ -389,12 +392,17 @@ void z_log_msg_runtime_vcreate(uint8_t domain_id, const void *source,
389392
__ASSERT_NO_MSG(plen >= 0);
390393
}
391394

392-
if (IS_ENABLED(CONFIG_LOG_FRONTEND) && frontend_runtime_filtering(source, desc.level)) {
393-
log_frontend_msg(source, desc, pkg, data);
394-
}
395+
if (IS_ENABLED(CONFIG_USERSPACE) && k_is_user_context()) {
396+
z_log_msg_static_create(source, desc, pkg, data);
397+
} else {
398+
if (IS_ENABLED(CONFIG_LOG_FRONTEND) &&
399+
frontend_runtime_filtering(source, desc.level)) {
400+
log_frontend_msg(source, desc, pkg, data);
401+
}
395402

396-
if (BACKENDS_IN_USE()) {
397-
z_log_msg_finalize(msg, source, desc, data);
403+
if (BACKENDS_IN_USE()) {
404+
z_log_msg_finalize(msg, source, desc, data);
405+
}
398406
}
399407
}
400408
EXPORT_SYMBOL(z_log_msg_runtime_vcreate);

0 commit comments

Comments
 (0)