blob: 7b7b748cd7e1613b68c4bcc36655804d62c28359 [file] [log] [blame]
Benjamin Walshf6ca7de2016-11-08 10:36:50 -05001/*
2 * Copyright (c) 2016 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walshf6ca7de2016-11-08 10:36:50 -05005 */
6
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09007/*
8 * The purpose of this file is to provide essential/minimal kernel structure
9 * definitions, so that they can be used without including kernel.h.
10 *
11 * The following rules must be observed:
12 * 1. kernel_structs.h shall not depend on kernel.h both directly and
13 * indirectly (i.e. it shall not include any header files that include
14 * kernel.h in their dependency chain).
15 * 2. kernel.h shall imply kernel_structs.h, such that it shall not be
16 * necessary to include kernel_structs.h explicitly when kernel.h is
17 * included.
18 */
19
Flavio Ceolina7fffa92018-09-13 15:06:35 -070020#ifndef ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_
21#define ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050022
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050023#if !defined(_ASMLANGUAGE)
Andy Ross0dd83b82020-04-03 10:01:03 -070024#include <sys/atomic.h>
Stephanos Ioannidis2d746042019-10-25 00:08:21 +090025#include <zephyr/types.h>
26#include <sched_priq.h>
Anas Nashifee9dd1a2019-06-26 10:33:41 -040027#include <sys/dlist.h>
Anas Nashifa2fd7d72019-06-26 10:33:55 -040028#include <sys/util.h>
Andy Ross0dd83b82020-04-03 10:01:03 -070029#include <sys/sys_heap.h>
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050030#endif
31
Andy Ross8ac9c082017-12-08 17:38:12 -080032#define K_NUM_PRIORITIES \
33 (CONFIG_NUM_COOP_PRIORITIES + CONFIG_NUM_PREEMPT_PRIORITIES + 1)
34
35#define K_NUM_PRIO_BITMAPS ((K_NUM_PRIORITIES + 31) >> 5)
36
Benjamin Walshb2974a62016-11-24 14:08:08 -050037/*
Benjamin Walshed240f22017-01-22 13:05:08 -050038 * Bitmask definitions for the struct k_thread.thread_state field.
Benjamin Walshb2974a62016-11-24 14:08:08 -050039 *
40 * Must be before kerneL_arch_data.h because it might need them to be already
41 * defined.
42 */
43
Benjamin Walshf9554762016-12-21 15:38:54 -050044/* states: common uses low bits, arch-specific use high bits */
45
Benjamin Walsh867f8ee2017-01-22 11:51:25 -050046/* Not a real thread */
Flavio Ceolin8aec0872018-08-15 11:52:00 -070047#define _THREAD_DUMMY (BIT(0))
Benjamin Walshb2974a62016-11-24 14:08:08 -050048
Benjamin Walshb2974a62016-11-24 14:08:08 -050049/* Thread is waiting on an object */
Flavio Ceolin8aec0872018-08-15 11:52:00 -070050#define _THREAD_PENDING (BIT(1))
Benjamin Walshb2974a62016-11-24 14:08:08 -050051
52/* Thread has not yet started */
Flavio Ceolin8aec0872018-08-15 11:52:00 -070053#define _THREAD_PRESTART (BIT(2))
Benjamin Walshb2974a62016-11-24 14:08:08 -050054
55/* Thread has terminated */
Flavio Ceolin8aec0872018-08-15 11:52:00 -070056#define _THREAD_DEAD (BIT(3))
Benjamin Walshb2974a62016-11-24 14:08:08 -050057
58/* Thread is suspended */
Flavio Ceolin8aec0872018-08-15 11:52:00 -070059#define _THREAD_SUSPENDED (BIT(4))
Benjamin Walshb2974a62016-11-24 14:08:08 -050060
Andrew Boief5a7e1a2020-09-02 09:20:38 -070061/* Thread is being aborted */
Andy Ross42ed12a2019-02-19 16:03:39 -080062#define _THREAD_ABORTING (BIT(5))
63
Andy Ross1acd8c22018-05-03 14:51:49 -070064/* Thread is present in the ready queue */
Andy Rosse06ba702020-01-14 06:26:10 -080065#define _THREAD_QUEUED (BIT(7))
Andy Ross1acd8c22018-05-03 14:51:49 -070066
Benjamin Walshf9554762016-12-21 15:38:54 -050067/* end - states */
68
Andrew Boie5dcb2792017-05-11 13:29:15 -070069#ifdef CONFIG_STACK_SENTINEL
70/* Magic value in lowest bytes of the stack */
71#define STACK_SENTINEL 0xF0F0F0F0
72#endif
Benjamin Walshf9554762016-12-21 15:38:54 -050073
Benjamin Walsh168695c2016-12-21 16:00:35 -050074/* lowest value of _thread_base.preempt at which a thread is non-preemptible */
Aastha Grover83b9f692020-08-20 16:47:11 -070075#define _NON_PREEMPT_THRESHOLD 0x0080U
Benjamin Walsh168695c2016-12-21 16:00:35 -050076
77/* highest value of _thread_base.preempt at which a thread is preemptible */
Aastha Grover83b9f692020-08-20 16:47:11 -070078#define _PREEMPT_THRESHOLD (_NON_PREEMPT_THRESHOLD - 1U)
Benjamin Walshb2974a62016-11-24 14:08:08 -050079
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050080#if !defined(_ASMLANGUAGE)
81
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082struct _ready_q {
Andy Ross2724fd12018-01-29 14:55:20 -080083#ifndef CONFIG_SMP
Benjamin Walsh04ed8602016-12-21 14:36:43 -050084 /* always contains next thread to run: cannot be NULL */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050085 struct k_thread *cache;
Andy Ross2724fd12018-01-29 14:55:20 -080086#endif
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050087
Andy Ross9f06a352018-06-28 10:38:14 -070088#if defined(CONFIG_SCHED_DUMB)
Andy Ross1acd8c22018-05-03 14:51:49 -070089 sys_dlist_t runq;
Andy Ross9f06a352018-06-28 10:38:14 -070090#elif defined(CONFIG_SCHED_SCALABLE)
Andy Ross1acd8c22018-05-03 14:51:49 -070091 struct _priq_rb runq;
Andy Ross9f06a352018-06-28 10:38:14 -070092#elif defined(CONFIG_SCHED_MULTIQ)
93 struct _priq_mq runq;
Andy Ross1acd8c22018-05-03 14:51:49 -070094#endif
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050095};
96
Benjamin Walsh88b36912016-12-02 10:37:27 -050097typedef struct _ready_q _ready_q_t;
98
Andy Rosse6946562018-01-25 16:39:35 -080099struct _cpu {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500100 /* nested interrupt count */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500101 uint32_t nested;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500102
103 /* interrupt stack pointer base */
104 char *irq_stack;
105
106 /* currently scheduled thread */
107 struct k_thread *current;
Andy Ross2724fd12018-01-29 14:55:20 -0800108
Andy Ross1acd8c22018-05-03 14:51:49 -0700109 /* one assigned idle thread per CPU */
110 struct k_thread *idle_thread;
111
Andrew Boief5a7e1a2020-09-02 09:20:38 -0700112 /* If non-null, self-aborted thread that needs cleanup */
113 struct k_thread *pending_abort;
114
Andy Ross11a050b2019-11-13 09:41:52 -0800115#if (CONFIG_NUM_METAIRQ_PRIORITIES > 0) && (CONFIG_NUM_COOP_PRIORITIES > 0)
116 /* Coop thread preempted by current metairq, or NULL */
117 struct k_thread *metairq_preempted;
118#endif
119
Andy Ross9098a452018-09-25 10:56:09 -0700120#ifdef CONFIG_TIMESLICING
121 /* number of ticks remaining in current time slice */
122 int slice_ticks;
123#endif
124
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500125 uint8_t id;
Andy Rosseace1df2018-05-30 11:23:02 -0700126
127#ifdef CONFIG_SMP
128 /* True when _current is allowed to context switch */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500129 uint8_t swap_ok;
Andy Rosseace1df2018-05-30 11:23:02 -0700130#endif
Andy Rosse6946562018-01-25 16:39:35 -0800131};
132
133typedef struct _cpu _cpu_t;
134
Flavio Ceolina406b882018-11-01 17:50:02 -0700135struct z_kernel {
Andrew Boiea203d212020-03-16 10:18:03 -0700136 struct _cpu cpus[CONFIG_MP_NUM_CPUS];
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500137
138#ifdef CONFIG_SYS_CLOCK_EXISTS
139 /* queue of timeouts */
140 sys_dlist_t timeout_q;
141#endif
142
143#ifdef CONFIG_SYS_POWER_MANAGEMENT
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500144 int32_t idle; /* Number of ticks for kernel idling */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500145#endif
146
147 /*
148 * ready queue: can be big, keep after small fields, since some
Benjamin Walshba266782016-11-14 10:17:30 -0500149 * assembly (e.g. ARC) are limited in the encoding of the offset
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500150 */
151 struct _ready_q ready_q;
152
Stephanos Ioannidisaaf93202020-05-03 18:03:19 +0900153#ifdef CONFIG_FPU_SHARING
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500154 /*
155 * A 'current_sse' field does not exist in addition to the 'current_fp'
156 * field since it's not possible to divide the IA-32 non-integer
157 * registers into 2 distinct blocks owned by differing threads. In
158 * other words, given that the 'fxnsave/fxrstor' instructions
159 * save/restore both the X87 FPU and XMM registers, it's not possible
160 * for a thread to only "own" the XMM registers.
161 */
162
Anas Nashif780324b2017-10-29 07:10:22 -0400163 /* thread that owns the FP regs */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500164 struct k_thread *current_fp;
165#endif
166
167#if defined(CONFIG_THREAD_MONITOR)
Anas Nashif780324b2017-10-29 07:10:22 -0400168 struct k_thread *threads; /* singly linked list of ALL threads */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500169#endif
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500170};
171
Flavio Ceolina406b882018-11-01 17:50:02 -0700172typedef struct z_kernel _kernel_t;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500173
Flavio Ceolina406b882018-11-01 17:50:02 -0700174extern struct z_kernel _kernel;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500175
Andy Rosse6946562018-01-25 16:39:35 -0800176#ifdef CONFIG_SMP
Andy Rosseefd3da2020-02-06 13:39:52 -0800177
178/* True if the current context can be preempted and migrated to
179 * another SMP CPU.
180 */
181bool z_smp_cpu_mobile(void);
182
183#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
184 arch_curr_cpu(); })
185#define _current k_current_get()
186
Andy Rosse6946562018-01-25 16:39:35 -0800187#else
Andy Ross1acd8c22018-05-03 14:51:49 -0700188#define _current_cpu (&_kernel.cpus[0])
Andrew Boiea203d212020-03-16 10:18:03 -0700189#define _current _kernel.cpus[0].current
Andy Rosse6946562018-01-25 16:39:35 -0800190#endif
191
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500192#define _timeout_q _kernel.timeout_q
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500193
Andy Ross41220d22020-03-31 09:17:09 -0700194/* kernel wait queue record */
195
196#ifdef CONFIG_WAITQ_SCALABLE
197
198typedef struct {
199 struct _priq_rb waitq;
200} _wait_q_t;
201
202extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
203
204#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
205
206#else
207
208typedef struct {
209 sys_dlist_t waitq;
210} _wait_q_t;
211
212#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
213
214#endif
215
216/* kernel timeout record */
217
218struct _timeout;
219typedef void (*_timeout_func_t)(struct _timeout *t);
220
221struct _timeout {
222 sys_dnode_t node;
Andy Ross41220d22020-03-31 09:17:09 -0700223 _timeout_func_t fn;
Andy Ross150e18d2020-06-17 08:56:40 -0700224#ifdef CONFIG_TIMEOUT_64BIT
225 /* Can't use k_ticks_t for header dependency reasons */
Peter Bigot5f0991d2020-09-03 14:30:04 -0500226 int64_t dticks;
Andy Ross150e18d2020-06-17 08:56:40 -0700227#else
Peter Bigot5f0991d2020-09-03 14:30:04 -0500228 int32_t dticks;
Andy Ross150e18d2020-06-17 08:56:40 -0700229#endif
Andy Ross41220d22020-03-31 09:17:09 -0700230};
231
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500232#endif /* _ASMLANGUAGE */
233
Flavio Ceolina7fffa92018-09-13 15:06:35 -0700234#endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ */