source: mainline/kernel/generic/include/proc/thread.h@ fc0de8c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc0de8c was 5a5269d, checked in by GitHub <noreply@…>, 6 years ago

Change type of uspace pointers in kernel from pointer type to numeric (#170)

From kernel's perspective, userspace addresses are not valid pointers,
and can only be used in calls to copy_to/from_uspace().
Therefore, we change the type of those arguments and variables to
uspace_addr_t which is an alias for sysarg_t.

This allows the compiler to catch accidental direct accesses to
userspace addresses.

Additionally, to avoid losing the type information in code,
a macro uspace_ptr(type) is used that translates to uspace_addr_t.
I makes no functional difference, but allows keeping the type information
in code in case we implement some sort of static checking for it in the future.

However, ccheck doesn't like that, so instead of using uspace_ptr(char),
we use uspace_ptr_char which is defined as
#define uspace_ptr_char uspace_ptr(char).

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Copyright (c) 2001-2007 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_generic_proc
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_THREAD_H_
36#define KERN_THREAD_H_
37
38#include <synch/waitq.h>
39#include <proc/task.h>
40#include <time/timeout.h>
41#include <cpu.h>
42#include <synch/spinlock.h>
43#include <adt/odict.h>
44#include <mm/slab.h>
45#include <arch/cpu.h>
46#include <mm/tlb.h>
47#include <abi/proc/uarg.h>
48#include <udebug/udebug.h>
49#include <abi/proc/thread.h>
50#include <abi/sysinfo.h>
51#include <arch.h>
52
53#define THREAD CURRENT->thread
54
55#define THREAD_NAME_BUFLEN 20
56
57extern const char *thread_states[];
58
59/* Thread flags */
60typedef enum {
61 THREAD_FLAG_NONE = 0,
62 /** Thread executes in user space. */
63 THREAD_FLAG_USPACE = (1 << 0),
64 /** Thread will be attached by the caller. */
65 THREAD_FLAG_NOATTACH = (1 << 1),
66 /** Thread accounting doesn't affect accumulated task accounting. */
67 THREAD_FLAG_UNCOUNTED = (1 << 2)
68} thread_flags_t;
69
70/** Thread structure. There is one per thread. */
71typedef struct thread {
72 link_t rq_link; /**< Run queue link. */
73 link_t wq_link; /**< Wait queue link. */
74 link_t th_link; /**< Links to threads within containing task. */
75
76 /** Link to @c threads ordered dictionary. */
77 odlink_t lthreads;
78
79 /** Lock protecting thread structure.
80 *
81 * Protects the whole thread structure except list links above.
82 */
83 IRQ_SPINLOCK_DECLARE(lock);
84
85 char name[THREAD_NAME_BUFLEN];
86
87 /** Function implementing the thread. */
88 void (*thread_code)(void *);
89 /** Argument passed to thread_code() function. */
90 void *thread_arg;
91
92 /**
93 * From here, the stored context is restored
94 * when the thread is scheduled.
95 */
96 context_t saved_context;
97
98 /**
99 * From here, the stored timeout context
100 * is restored when sleep times out.
101 */
102 context_t sleep_timeout_context;
103
104 /**
105 * From here, the stored interruption context
106 * is restored when sleep is interrupted.
107 */
108 context_t sleep_interruption_context;
109
110 /** If true, the thread can be interrupted from sleep. */
111 bool sleep_interruptible;
112
113 /**
114 * If true, and this thread's sleep returns without a wakeup
115 * (timed out or interrupted), waitq ignores the next wakeup.
116 * This is necessary for futex to be able to handle those conditions.
117 */
118 bool sleep_composable;
119
120 /** Wait queue in which this thread sleeps. */
121 waitq_t *sleep_queue;
122 /** Timeout used for timeoutable sleeping. */
123 timeout_t sleep_timeout;
124 /** Flag signalling sleep timeout in progress. */
125 volatile bool timeout_pending;
126
127 /**
128 * True if this thread is executing copy_from_uspace().
129 * False otherwise.
130 */
131 bool in_copy_from_uspace;
132
133 /**
134 * True if this thread is executing copy_to_uspace().
135 * False otherwise.
136 */
137 bool in_copy_to_uspace;
138
139 /**
140 * If true, the thread will not go to sleep at all and will call
141 * thread_exit() before returning to userspace.
142 */
143 bool interrupted;
144
145 /** If true, thread_join_timeout() cannot be used on this thread. */
146 bool detached;
147 /** Waitq for thread_join_timeout(). */
148 waitq_t join_wq;
149 /** Link used in the joiner_head list. */
150 link_t joiner_link;
151
152 fpu_context_t *saved_fpu_context;
153 bool fpu_context_exists;
154
155 /*
156 * Defined only if thread doesn't run.
157 * It means that fpu context is in CPU that last time executes this
158 * thread. This disables migration.
159 */
160 bool fpu_context_engaged;
161
162 /* The thread will not be migrated if nomigrate is non-zero. */
163 unsigned int nomigrate;
164
165 /** Thread state. */
166 state_t state;
167
168 /** Thread CPU. */
169 cpu_t *cpu;
170 /** Containing task. */
171 task_t *task;
172 /** Thread is wired to CPU. */
173 bool wired;
174 /** Thread was migrated to another CPU and has not run yet. */
175 bool stolen;
176 /** Thread is executed in user space. */
177 bool uspace;
178
179 /** Ticks before preemption. */
180 uint64_t ticks;
181
182 /** Thread accounting. */
183 uint64_t ucycles;
184 uint64_t kcycles;
185 /** Last sampled cycle. */
186 uint64_t last_cycle;
187 /** Thread doesn't affect accumulated accounting. */
188 bool uncounted;
189
190 /** Thread's priority. Implemented as index to CPU->rq */
191 int priority;
192 /** Thread ID. */
193 thread_id_t tid;
194
195 /** Architecture-specific data. */
196 thread_arch_t arch;
197
198 /** Thread's kernel stack. */
199 uint8_t *kstack;
200
201#ifdef CONFIG_UDEBUG
202 /**
203 * If true, the scheduler will print a stack trace
204 * to the kernel console upon scheduling this thread.
205 */
206 bool btrace;
207
208 /** Debugging stuff */
209 udebug_thread_t udebug;
210#endif /* CONFIG_UDEBUG */
211} thread_t;
212
213IRQ_SPINLOCK_EXTERN(threads_lock);
214extern odict_t threads;
215
216extern void thread_init(void);
217extern thread_t *thread_create(void (*)(void *), void *, task_t *,
218 thread_flags_t, const char *);
219extern void thread_wire(thread_t *, cpu_t *);
220extern void thread_attach(thread_t *, task_t *);
221extern void thread_ready(thread_t *);
222extern void thread_exit(void) __attribute__((noreturn));
223extern void thread_interrupt(thread_t *);
224extern bool thread_interrupted(thread_t *);
225
226#ifndef thread_create_arch
227extern errno_t thread_create_arch(thread_t *, thread_flags_t);
228#endif
229
230#ifndef thr_constructor_arch
231extern void thr_constructor_arch(thread_t *);
232#endif
233
234#ifndef thr_destructor_arch
235extern void thr_destructor_arch(thread_t *);
236#endif
237
238extern void thread_sleep(uint32_t);
239extern void thread_usleep(uint32_t);
240
241#define thread_join(t) \
242 thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
243
244extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
245extern void thread_detach(thread_t *);
246
247extern void thread_print_list(bool);
248extern void thread_destroy(thread_t *, bool);
249extern thread_t *thread_find_by_id(thread_id_t);
250extern size_t thread_count(void);
251extern thread_t *thread_first(void);
252extern thread_t *thread_next(thread_t *);
253extern void thread_update_accounting(bool);
254extern bool thread_exists(thread_t *);
255
256extern void thread_migration_disable(void);
257extern void thread_migration_enable(void);
258
259#ifdef CONFIG_UDEBUG
260extern void thread_stack_trace(thread_id_t);
261#endif
262
263/** Fpu context slab cache. */
264extern slab_cache_t *fpu_context_cache;
265
266/* Thread syscall prototypes. */
267extern sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t, uspace_ptr_char, size_t,
268 uspace_ptr_thread_id_t);
269extern sys_errno_t sys_thread_exit(int);
270extern sys_errno_t sys_thread_get_id(uspace_ptr_thread_id_t);
271extern sys_errno_t sys_thread_usleep(uint32_t);
272extern sys_errno_t sys_thread_udelay(uint32_t);
273
274#endif
275
276/** @}
277 */
Note: See TracBrowser for help on using the repository browser.