-
Notifications
You must be signed in to change notification settings - Fork 7.6k
xtensa: support for C library provided by Xtensa toolchain #90351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
|
||
zephyr_library_sources( | ||
libc-hooks.c | ||
multithreading.c | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright (c) 2025 Cadence Design Systems, Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
char __os_flag __attribute__((retain)); /* Needed by xclib to indicate the presense of an OS */ | ||
|
||
int arch_printk_char_out(int c); | ||
static int (*_stdout_hook)(int c) = arch_printk_char_out; | ||
|
||
void __stdout_hook_install(int (*hook)(int)) | ||
{ | ||
_stdout_hook = hook; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2025 Cadence Design Systems, Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/kernel.h> | ||
#include <stdlib.h> | ||
#include <sys/reent.h> | ||
|
||
int32_t _xclib_use_mt = 1; /* Enables xclib multithread support */ | ||
|
||
typedef struct k_mutex *_Rmtx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pet peeve: you probably want a spinlock or a k_sem here and not a mutex. k_mutex is very heavyweight and the only feature it brings is priority inheritance. Semaphores are a better general purpose blocking IPC lock for almost all purposes. But given the use case, I'm thinking that the libc is probably using this to protect tight critical sections, where spinlocks are much lighter still and objectively better choices. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially used k_sem, but a recursive locking mechanism is needed to avoid hangs. |
||
|
||
/* Number of Locks needed by _Initlocks() without calling malloc() | ||
* which would infinitely call _Initlocks() recursively. | ||
*/ | ||
#define NLOCKS (_MAX_LOCK + 1) | ||
|
||
static struct k_mutex _Mtxinit_mtx[NLOCKS]; | ||
|
||
void _Mtxinit(_Rmtx *mtx) | ||
{ | ||
static int i; /* static variable initially 0 */ | ||
|
||
if (i < NLOCKS) { | ||
*mtx = &_Mtxinit_mtx[i]; | ||
++i; | ||
} else { | ||
*mtx = malloc(sizeof(struct k_mutex)); | ||
} | ||
if (!*mtx) { | ||
k_panic(); | ||
} | ||
k_mutex_init(*mtx); | ||
} | ||
|
||
void _Mtxdst(_Rmtx *mtx) | ||
{ | ||
if (mtx != NULL && *mtx != NULL && | ||
(*mtx > &_Mtxinit_mtx[NLOCKS] || *mtx < &_Mtxinit_mtx[0])) { | ||
free(*mtx); | ||
} | ||
} | ||
|
||
void _Mtxlock(_Rmtx *mtx) | ||
{ | ||
if ((mtx != NULL) && (*mtx != NULL)) { | ||
k_mutex_lock(*mtx, K_FOREVER); | ||
} | ||
} | ||
|
||
void _Mtxunlock(_Rmtx *mtx) | ||
{ | ||
if ((mtx != NULL) && (*mtx != NULL)) { | ||
k_mutex_unlock(*mtx); | ||
} | ||
} | ||
|
||
#if defined(__DYNAMIC_REENT__) | ||
struct _reent *__getreent(void) | ||
{ | ||
return &k_current_get()->arch.reent; | ||
} | ||
#else | ||
#error __DYNAMIC_REENT__ support missing | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relevant to this PR, but note that once this lands SOF will want to use it instead of its current integration for a handful of third party C++ modules, which add the libraries manually. (@lyakh @kv2019i et. al.)