samples: fix thread function signatures

Fix thread function signatures to avoid stack corruption on thread exit.

Signed-off-by: Benedikt Schmidt <benedikt.schmidt@embedded-solutions.at>
This commit is contained in:
Benedikt Schmidt 2023-10-04 08:58:03 +02:00 committed by Carles Cufí
commit 6202459d9f
22 changed files with 124 additions and 61 deletions

View file

@ -90,7 +90,7 @@ int main(void)
for (i = 0; i < THREADS_NUM; i++) {
k_thread_create(&tthread[i], tstack[i], STACK_SIZE,
(k_thread_entry_t)test_thread,
test_thread,
(void *)&th_counter, (void *)th_buffer[i], NULL,
K_PRIO_COOP(10), 0, K_NO_WAIT);
}

View file

@ -101,7 +101,6 @@ void test_thread(void *arg1, void *arg2, void *arg3)
/* Thread that processes one pair of sender/receiver queue */
void queue_thread(void *arg1, void *arg2, void *arg3)
{
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
@ -117,7 +116,7 @@ void queue_thread(void *arg1, void *arg2, void *arg3)
for (int i = 0; i < THREADS_NUM; i++)
k_thread_create(&tthread[i+THREADS_NUM*queue_num],
tstack[i+THREADS_NUM*queue_num], STACK_SIZE,
(k_thread_entry_t)test_thread,
test_thread,
(void *)&sender[queue_num],
(void *)&receiver[queue_num], (void *)&queue_num,
K_PRIO_PREEMPT(10), 0, K_NO_WAIT);
@ -156,7 +155,7 @@ int main(void)
for (int i = 0; i < QUEUE_NUM; i++)
k_thread_create(&qthread[i], qstack[i], STACK_SIZE,
(k_thread_entry_t)queue_thread,
queue_thread,
(void *)&sender[i], (void *)&receiver[i],
(void *)&i, K_PRIO_PREEMPT(11), 0, K_NO_WAIT);

View file

@ -285,8 +285,12 @@ static void setup_tcp_accept(struct net_context *tcp_recv6)
}
}
static void listen(void)
static void listen(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
struct net_context *udp_recv6 = { 0 };
struct net_context *tcp_recv6 = { 0 };
@ -313,7 +317,7 @@ int main(void)
init_app();
k_thread_create(&thread_data, thread_stack, STACKSIZE,
(k_thread_entry_t)listen,
listen,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
return 0;
}

View file

@ -23,4 +23,4 @@ extern struct k_mem_domain app_domain;
#define THREAD_PRIORITY K_PRIO_PREEMPT(8)
#endif
int start_thread(void);
void start_thread(void);

View file

@ -83,12 +83,16 @@ static void init_app(void)
}
}
static int start_client(void)
static void start_client(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
/* Wait for connection */
k_sem_take(&run_app, K_FOREVER);
return start_thread();
start_thread();
}
int main(void)
@ -107,9 +111,9 @@ int main(void)
k_thread_access_grant(k_current_get(), &run_app);
k_mem_domain_add_thread(&app_domain, k_current_get());
k_thread_user_mode_enter((k_thread_entry_t)start_client, NULL, NULL, NULL);
k_thread_user_mode_enter(start_client, NULL, NULL, NULL);
#else
exit(start_client());
start_client(NULL, NULL, NULL);
#endif
return 0;
}

View file

@ -141,23 +141,28 @@ static void process_thread(void)
LOG_ERR("Exiting thread: %d", err);
}
int start_thread(void)
void start_thread(void)
{
int rc;
#if defined(CONFIG_USERSPACE)
rc = k_mem_domain_add_thread(&app_domain, udp_thread_id);
if (rc < 0) {
return rc;
LOG_ERR("Failed: k_mem_domain_add_thread() %d", rc);
return;
}
#endif
rc = k_thread_name_set(udp_thread_id, "udp");
if (rc < 0 && rc != -ENOSYS) {
LOG_ERR("Failed: k_thread_name_set() %d", rc);
return rc;
return;
}
k_thread_start(udp_thread_id);
return k_thread_join(udp_thread_id, K_FOREVER);
rc = k_thread_join(udp_thread_id, K_FOREVER);
if (rc != 0) {
LOG_ERR("Failed: k_thread_join() %d", rc);
}
}

View file

@ -39,8 +39,12 @@ static const struct can_filter zfilter = {
static struct socketcan_filter sock_filter;
static void tx(int *can_fd)
static void tx(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p2);
ARG_UNUSED(p3);
int *can_fd = p1;
int fd = POINTER_TO_INT(can_fd);
struct can_frame zframe = {0};
struct socketcan_frame sframe = {0};
@ -95,9 +99,11 @@ static int create_socket(const struct socketcan_filter *sfilter)
return fd;
}
static void rx(int *can_fd, int *do_close_period,
const struct socketcan_filter *sfilter)
static void rx(void *p1, void *p2, void *p3)
{
int *can_fd = p1;
int *do_close_period = p2;
const struct socketcan_filter *sfilter = p3;
int close_period = POINTER_TO_INT(do_close_period);
int fd = POINTER_TO_INT(can_fd);
struct sockaddr_can can_addr;
@ -205,7 +211,7 @@ static int setup_socket(void)
/* Delay TX startup so that RX is ready to receive */
tx_tid = k_thread_create(&tx_data, tx_stack,
K_THREAD_STACK_SIZEOF(tx_stack),
(k_thread_entry_t)tx, INT_TO_POINTER(fd),
tx, INT_TO_POINTER(fd),
NULL, NULL, PRIORITY, 0, K_SECONDS(1));
if (!tx_tid) {
ret = -ENOENT;
@ -225,7 +231,7 @@ static int setup_socket(void)
if (fd >= 0) {
rx_tid = k_thread_create(&rx_data, rx_stack,
K_THREAD_STACK_SIZEOF(rx_stack),
(k_thread_entry_t)rx,
rx,
INT_TO_POINTER(fd),
INT_TO_POINTER(CLOSE_PERIOD),
&sock_filter, PRIORITY, 0, K_NO_WAIT);

View file

@ -287,7 +287,7 @@ static int process_tcp(int *sock, int *accepted)
&tcp6_handler_thread[slot],
tcp6_handler_stack[slot],
K_THREAD_STACK_SIZEOF(tcp6_handler_stack[slot]),
(k_thread_entry_t)client_conn_handler,
client_conn_handler,
INT_TO_POINTER(slot),
&accepted[slot],
&tcp6_handler_tid[slot],
@ -302,7 +302,7 @@ static int process_tcp(int *sock, int *accepted)
&tcp4_handler_thread[slot],
tcp4_handler_stack[slot],
K_THREAD_STACK_SIZEOF(tcp4_handler_stack[slot]),
(k_thread_entry_t)client_conn_handler,
client_conn_handler,
INT_TO_POINTER(slot),
&accepted[slot],
&tcp4_handler_tid[slot],

View file

@ -279,8 +279,12 @@ static void init_app(void)
init_udp();
}
static int start_client(void)
static void start_client(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
int iterations = CONFIG_NET_SAMPLE_SEND_ITERATIONS;
int i = 0;
int ret;
@ -305,8 +309,6 @@ static int start_client(void)
stop_udp_and_tcp();
}
return ret;
}
int main(void)
@ -327,10 +329,9 @@ int main(void)
k_thread_access_grant(k_current_get(), &run_app);
k_mem_domain_add_thread(&app_domain, k_current_get());
k_thread_user_mode_enter((k_thread_entry_t)start_client, NULL, NULL,
NULL);
k_thread_user_mode_enter(start_client, NULL, NULL, NULL);
#else
exit(start_client());
start_client(NULL, NULL, NULL);
#endif
return 0;
}

View file

@ -40,8 +40,12 @@ static int send_udp_data(struct data *data);
static void wait_reply(struct k_timer *timer);
static void wait_transmit(struct k_timer *timer);
static void process_udp_tx(void)
static void process_udp_tx(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
struct k_poll_event events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
K_POLL_MODE_NOTIFY_ONLY,
@ -317,7 +321,7 @@ int start_udp(void)
k_thread_create(&udp_tx_thread, udp_tx_thread_stack,
K_THREAD_STACK_SIZEOF(udp_tx_thread_stack),
(k_thread_entry_t)process_udp_tx,
process_udp_tx,
NULL, NULL, NULL, THREAD_PRIORITY,
IS_ENABLED(CONFIG_USERSPACE) ?
K_USER | K_INHERIT_PERMS : 0,

View file

@ -243,7 +243,7 @@ static int process_tcp(struct data *data)
&tcp6_handler_thread[slot],
tcp6_handler_stack[slot],
K_THREAD_STACK_SIZEOF(tcp6_handler_stack[slot]),
(k_thread_entry_t)handle_data,
handle_data,
INT_TO_POINTER(slot), data, &tcp6_handler_in_use[slot],
THREAD_PRIORITY,
IS_ENABLED(CONFIG_USERSPACE) ? K_USER |
@ -267,7 +267,7 @@ static int process_tcp(struct data *data)
&tcp4_handler_thread[slot],
tcp4_handler_stack[slot],
K_THREAD_STACK_SIZEOF(tcp4_handler_stack[slot]),
(k_thread_entry_t)handle_data,
handle_data,
INT_TO_POINTER(slot), data, &tcp4_handler_in_use[slot],
THREAD_PRIORITY,
IS_ENABLED(CONFIG_USERSPACE) ? K_USER |

View file

@ -82,8 +82,12 @@ static char *get_ip_addr(char *ipaddr, size_t len, sa_family_t family,
return buf;
}
static void listener(void)
static void listener(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
struct sockaddr_nm sockaddr;
struct sockaddr_nm event_addr;
socklen_t event_addr_len;
@ -163,10 +167,10 @@ int main(void)
k_thread_start(trigger_events_thread_id);
if (IS_ENABLED(CONFIG_USERSPACE)) {
k_thread_user_mode_enter((k_thread_entry_t)listener,
k_thread_user_mode_enter(listener,
NULL, NULL, NULL);
} else {
listener();
listener(NULL, NULL, NULL);
}
return 0;
}

View file

@ -127,8 +127,12 @@ static void event_handler(struct net_mgmt_event_callback *cb,
}
}
static void rx(struct app_data *data)
static void rx(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p2);
ARG_UNUSED(p3);
struct app_data *data = p1;
static uint8_t recv_buf[sizeof(txtime_str)];
struct sockaddr src;
socklen_t addr_len = data->peer_addr_len;
@ -143,8 +147,12 @@ static void rx(struct app_data *data)
}
}
static void tx(struct app_data *data)
static void tx(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p2);
ARG_UNUSED(p3);
struct app_data *data = p1;
struct net_ptp_time time;
struct msghdr msg;
struct cmsghdr *cmsg;
@ -620,7 +628,7 @@ int main(void)
tx_tid = k_thread_create(&tx_thread, tx_stack,
K_THREAD_STACK_SIZEOF(tx_stack),
(k_thread_entry_t)tx, &peer_data,
tx, &peer_data,
NULL, NULL, THREAD_PRIORITY, 0,
K_FOREVER);
if (!tx_tid) {
@ -632,7 +640,7 @@ int main(void)
rx_tid = k_thread_create(&rx_thread, rx_stack,
K_THREAD_STACK_SIZEOF(rx_stack),
(k_thread_entry_t)rx, &peer_data,
rx, &peer_data,
NULL, NULL, THREAD_PRIORITY, 0,
K_FOREVER);
if (!rx_tid) {

View file

@ -299,8 +299,12 @@ static void process_config(struct net_pkt *pkt)
}
}
static void rx_thread(void)
static void rx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
LOG_DBG("RX thread started");
while (true) {
@ -386,8 +390,12 @@ static int try_write(uint8_t *data, uint16_t len)
/**
* TX - transmit to SLIP interface
*/
static void tx_thread(void)
static void tx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
LOG_DBG("TX thread started");
while (true) {
@ -421,7 +429,7 @@ static void init_rx_queue(void)
k_thread_create(&rx_thread_data, rx_stack,
K_THREAD_STACK_SIZEOF(rx_stack),
(k_thread_entry_t)rx_thread,
rx_thread,
NULL, NULL, NULL, THREAD_PRIORITY, 0, K_NO_WAIT);
}
@ -431,7 +439,7 @@ static void init_tx_queue(void)
k_thread_create(&tx_thread_data, tx_stack,
K_THREAD_STACK_SIZEOF(tx_stack),
(k_thread_entry_t)tx_thread,
tx_thread,
NULL, NULL, NULL, THREAD_PRIORITY, 0, K_NO_WAIT);
}

View file

@ -295,8 +295,12 @@ static int tx(struct net_pkt *pkt)
return ret;
}
static void tx_thread(void)
static void tx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
LOG_DBG("Tx thread started");
while (1) {
@ -353,7 +357,7 @@ static void init_tx_queue(void)
k_thread_create(&tx_thread_data, tx_stack,
K_THREAD_STACK_SIZEOF(tx_stack),
(k_thread_entry_t)tx_thread,
tx_thread,
NULL, NULL, NULL, THREAD_PRIORITY, 0, K_NO_WAIT);
}

View file

@ -144,6 +144,7 @@ void app_task(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
int status = 0;
unsigned int message = 0U;
struct metal_device *device;
@ -259,7 +260,7 @@ int main(void)
{
printk("Starting application thread!\n");
k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)app_task,
app_task,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
return 0;
}

View file

@ -168,6 +168,7 @@ void app_task(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
int status = 0;
unsigned int message = 0U;
struct metal_device *device;
@ -284,7 +285,7 @@ int main(void)
{
printk("Starting application thread!\n");
k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)app_task,
app_task,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
#if defined(CONFIG_SOC_MPS2_AN521) || \

View file

@ -276,6 +276,7 @@ void app_rpmsg_client_sample(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
unsigned int msg_cnt = 0;
int ret = 0;
@ -302,6 +303,7 @@ void app_rpmsg_tty(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
unsigned char tx_buff[512];
int ret = 0;
@ -335,6 +337,7 @@ void rpmsg_mng_task(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
unsigned char *msg;
unsigned int len;
int ret = 0;
@ -375,13 +378,13 @@ int main(void)
{
printk("Starting application threads!\n");
k_thread_create(&thread_mng_data, thread_mng_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)rpmsg_mng_task,
rpmsg_mng_task,
NULL, NULL, NULL, K_PRIO_COOP(8), 0, K_NO_WAIT);
k_thread_create(&thread_rp__client_data, thread_rp__client_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)app_rpmsg_client_sample,
app_rpmsg_client_sample,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
k_thread_create(&thread_tty_data, thread_tty_stack, APP_TTY_TASK_STACK_SIZE,
(k_thread_entry_t)app_rpmsg_tty,
app_rpmsg_tty,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
return 0;
}

View file

@ -54,6 +54,7 @@ void app_task(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
int status = 0;
unsigned int message = 0U;
@ -79,7 +80,7 @@ int main(void)
{
printk("Starting application thread!\n");
k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)app_task,
app_task,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
return 0;
}

View file

@ -55,6 +55,7 @@ void app_task(void *arg1, void *arg2, void *arg3)
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
ARG_UNUSED(arg3);
int status = 0;
unsigned int message = 0U;
@ -88,7 +89,7 @@ int main(void)
{
printk("Starting application thread!\n");
k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE,
(k_thread_entry_t)app_task,
app_task,
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
#if defined(CONFIG_SOC_MPS2_AN521) || \

View file

@ -133,7 +133,7 @@ int main(void)
* then add the thread to the domain.
*/
tENC = k_thread_create(&enc_thread, enc_stack, STACKSIZE,
(k_thread_entry_t)enc, NULL, NULL, NULL,
enc, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tENC, &allforone);
@ -150,7 +150,7 @@ int main(void)
tPT = k_thread_create(&pt_thread, pt_stack, STACKSIZE,
(k_thread_entry_t)pt, NULL, NULL, NULL,
pt, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tPT, &allforone);
@ -163,7 +163,7 @@ int main(void)
printk("pt_domain Created\n");
tCT = k_thread_create(&ct_thread, ct_stack, STACKSIZE,
(k_thread_entry_t)ct, NULL, NULL, NULL,
ct, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tCT, &allforone);
@ -204,8 +204,11 @@ int main(void)
* Copy memory from pt thread and encrypt to a local buffer
* then copy to the ct thread.
*/
void enc(void)
void enc(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
int index, index_out;
@ -252,8 +255,11 @@ void enc(void)
* It can be extended to receive data from a serial port
* and pass the data to enc
*/
void pt(void)
void pt(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
k_sleep(K_MSEC(20));
while (1) {
@ -282,8 +288,11 @@ void pt(void)
* CT waits for fBUFOUT = 1 then copies
* the message clears the flag and prints
*/
void ct(void)
void ct(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
char tbuf[60];

View file

@ -20,9 +20,9 @@
#include <zephyr/arch/arc/v2/mpu/arc_core_mpu.h>
#endif
void enc(void);
void pt(void);
void ct(void);
void enc(void *p1, void *p2, void *p3);
void pt(void *p1, void *p2, void *p3);
void ct(void *p1, void *p2, void *p3);
#define _app_user_d K_APP_DMEM(user_part)
#define _app_user_b K_APP_BMEM(user_part)