• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef OBJECT_THREAD_H
13 #define OBJECT_THREAD_H
14 
15 #include <common/list.h>
16 #include <mm/vmspace.h>
17 #include <sched/sched.h>
18 #include <object/cap_group.h>
19 #include <arch/machine/smp.h>
20 #include <ipc/connection.h>
21 #include <irq/timer.h>
22 #include <common/debug.h>
23 
24 /* Per-CPU variable current_thread is only accessed by its owner CPU. */
25 #define current_thread (current_threads[smp_get_cpu_id()])
26 #ifdef CHCORE_KERNEL_RT
27 /* RT (kernel PREEMT): allocate the stack for each thread  */
28 #define DEFAULT_KERNEL_STACK_SZ (0x1000)
29 #else
30 /* No kernel PREEMT: no stack for each thread, instead, using a per-cpu stack */
31 #define DEFAULT_KERNEL_STACK_SZ (0)
32 #endif
33 
34 #define THREAD_ITSELF ((void *)(-1))
35 
36 struct thread {
37     struct list_head node; // link threads in a same cap_group
38     struct list_head ready_queue_node; // link threads in a ready queue
39     struct list_head
40         notification_queue_node; // link threads in a notification waiting queue
41     struct thread_ctx *thread_ctx; // thread control block
42 
43     /*
44      * prev_thread switch CPU to this_thread
45      *
46      * When previous thread is the thread itself,
47      * prev_thread will be set to THREAD_ITSELF.
48      */
49     struct thread *prev_thread;
50 
51     /*
52      * vmspace: virtual memory address space.
53      * vmspace is also stored in the 2nd slot of capability
54      */
55     struct vmspace *vmspace;
56 
57     struct cap_group *cap_group;
58 
59     /* Record the thread cap for quick thread recycle. */
60     cap_t cap;
61 
62     /*
63      * Only exists for threads in a server process.
64      * If not NULL, it points to one of the three config types.
65      */
66     void *general_ipc_config;
67 
68     struct sleep_state sleep_state;
69 };
70 
71 extern struct thread *current_threads[PLAT_CPU_NUM];
72 extern struct thread idle_threads[PLAT_CPU_NUM];
73 
74 /* Used for creating the root thread */
75 extern const char binary_procmgr_bin_start;
76 extern const unsigned long binary_procmgr_bin_size;
77 
78 void create_root_thread(void);
79 void switch_thread_vmspace_to(struct thread *);
80 void thread_deinit(void *thread_ptr);
81 
82 /* Syscalls */
83 cap_t sys_create_thread(unsigned long thread_args_p);
84 void sys_thread_exit(void);
85 int sys_set_affinity(cap_t thread_cap, s32 aff);
86 s32 sys_get_affinity(cap_t thread_cap);
87 int sys_set_prio(cap_t thread_cap, int prio);
88 int sys_get_prio(cap_t thread_cap);
89 
90 #ifdef CHCORE_OH_TEE
91 cap_t sys_get_thread_id(cap_t thread_cap);
92 int sys_terminate_thread(cap_t thread_cap);
93 #endif /* CHCORE_OH_TEE */
94 
95 void sys_disable_local_irq(void);
96 void sys_enable_local_irq(void);
97 
98 #endif /* OBJECT_THREAD_H */
99