1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <kernel.h>
8 #include <ksched.h>
9
10 /* forward declaration to asm function to adjust setup the arguments
11 * to z_thread_entry() since this arch puts the first four arguments
12 * in r4-r7 and not on the stack
13 */
14 void z_thread_entry_wrapper(k_thread_entry_t, void *, void *, void *);
15
16 struct init_stack_frame {
17 /* top of the stack / most recently pushed */
18
19 /* Used by z_thread_entry_wrapper. pulls these off the stack and
20 * into argument registers before calling z_thread_entry()
21 */
22 k_thread_entry_t entry_point;
23 void *arg1;
24 void *arg2;
25 void *arg3;
26
27 /* least recently pushed */
28 };
29
30
arch_new_thread(struct k_thread * thread,k_thread_stack_t * stack,char * stack_ptr,k_thread_entry_t entry,void * arg1,void * arg2,void * arg3)31 void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
32 char *stack_ptr, k_thread_entry_t entry,
33 void *arg1, void *arg2, void *arg3)
34 {
35 struct init_stack_frame *iframe;
36
37 /* Initial stack frame data, stored at the base of the stack */
38 iframe = Z_STACK_PTR_TO_FRAME(struct init_stack_frame, stack_ptr);
39
40 /* Setup the initial stack frame */
41 iframe->entry_point = entry;
42 iframe->arg1 = arg1;
43 iframe->arg2 = arg2;
44 iframe->arg3 = arg3;
45
46 thread->callee_saved.sp = (uint32_t)iframe;
47 thread->callee_saved.ra = (uint32_t)z_thread_entry_wrapper;
48 thread->callee_saved.key = NIOS2_STATUS_PIE_MSK;
49 /* Leave the rest of thread->callee_saved junk */
50 }
51