1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <kernel.h>
#include <nano_internal.h>
#include <kernel_structs.h>
#include <wait_q.h>
#include <string.h>
#if defined(CONFIG_THREAD_MONITOR)
/*
* Add a thread to the kernel's list of active threads.
*/
static ALWAYS_INLINE void thread_monitor_init(struct k_thread *thread)
{
unsigned int key;
key = irq_lock();
thread->next_thread = _kernel.threads;
_kernel.threads = thread;
irq_unlock(key);
}
#else
#define thread_monitor_init(thread) \
do {/* do nothing */ \
} while ((0))
#endif /* CONFIG_THREAD_MONITOR */
/* forward declaration to asm function to adjust setup the arguments
* to _thread_entry() since this arch puts the first four arguments
* in r4-r7 and not on the stack
*/
void _thread_entry_wrapper(_thread_entry_t, void *, void *, void *);
struct init_stack_frame {
/* top of the stack / most recently pushed */
/* Used by _thread_entry_wrapper. pulls these off the stack and
* into argument registers before calling _thread_entry()
*/
_thread_entry_t entry_point;
void *arg1;
void *arg2;
void *arg3;
/* least recently pushed */
};
void _new_thread(char *stack_memory, unsigned stack_size,
void *uk_task_ptr, _thread_entry_t thread_func,
void *arg1, void *arg2, void *arg3,
int priority, unsigned options)
{
_ASSERT_VALID_PRIO(priority, thread_func);
struct k_thread *thread;
struct init_stack_frame *iframe;
#ifdef CONFIG_INIT_STACKS
memset(stack_memory, 0xaa, stack_size);
#endif
/* Initial stack frame data, stored at the base of the stack */
iframe = (struct init_stack_frame *)
STACK_ROUND_DOWN(stack_memory + stack_size - sizeof(*iframe));
/* Setup the initial stack frame */
iframe->entry_point = thread_func;
iframe->arg1 = arg1;
iframe->arg2 = arg2;
iframe->arg3 = arg3;
/* Initialize various struct k_thread members */
thread = (struct k_thread *)stack_memory;
thread->base.prio = priority;
/* k_q_node initialized upon first insertion in a list */
thread->base.flags = options | K_PRESTART;
thread->base.sched_locked = 0;
/* static threads overwrite it afterwards with real value */
thread->init_data = NULL;
thread->fn_abort = NULL;
#ifdef CONFIG_THREAD_CUSTOM_DATA
/* Initialize custom data field (value is opaque to kernel) */
thread->custom_data = NULL;
#endif
ARG_UNUSED(uk_task_ptr);
thread->callee_saved.sp = (uint32_t)iframe;
thread->callee_saved.ra = (uint32_t)_thread_entry_wrapper;
thread->callee_saved.key = NIOS2_STATUS_PIE_MSK;
/* Leave the rest of thread->callee_saved junk */
#ifdef CONFIG_NANO_TIMEOUTS
_nano_timeout_thread_init(thread);
#endif
thread_monitor_init(thread);
}
|