1 /* 2 * Copyright (c) 2016 Wind River Systems, Inc. 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /* 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 20 #ifndef ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ 21 #define ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ 22 23 #if !defined(_ASMLANGUAGE) 24 #include <sys/atomic.h> 25 #include <zephyr/types.h> 26 #include <kernel/sched_priq.h> 27 #include <sys/dlist.h> 28 #include <sys/util.h> 29 #include <sys/sys_heap.h> 30 #include <arch/structs.h> 31 #endif 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 /* 38 * Bitmask definitions for the struct k_thread.thread_state field. 39 * 40 * Must be before kerneL_arch_data.h because it might need them to be already 41 * defined. 42 */ 43 44 /* states: common uses low bits, arch-specific use high bits */ 45 46 /* Not a real thread */ 47 #define _THREAD_DUMMY (BIT(0)) 48 49 /* Thread is waiting on an object */ 50 #define _THREAD_PENDING (BIT(1)) 51 52 /* Thread has not yet started */ 53 #define _THREAD_PRESTART (BIT(2)) 54 55 /* Thread has terminated */ 56 #define _THREAD_DEAD (BIT(3)) 57 58 /* Thread is suspended */ 59 #define _THREAD_SUSPENDED (BIT(4)) 60 61 /* Thread is being aborted */ 62 #define _THREAD_ABORTING (BIT(5)) 63 64 /* Thread is present in the ready queue */ 65 #define _THREAD_QUEUED (BIT(7)) 66 67 /* end - states */ 68 69 #ifdef CONFIG_STACK_SENTINEL 70 /* Magic value in lowest bytes of the stack */ 71 #define STACK_SENTINEL 0xF0F0F0F0 72 #endif 73 74 /* lowest value of _thread_base.preempt at which a thread is non-preemptible */ 75 #define _NON_PREEMPT_THRESHOLD 0x0080U 76 77 /* highest value of _thread_base.preempt at which a thread is preemptible */ 78 #define _PREEMPT_THRESHOLD (_NON_PREEMPT_THRESHOLD - 1U) 79 80 #if !defined(_ASMLANGUAGE) 81 82 struct _ready_q { 83 #ifndef CONFIG_SMP 84 /* always contains next thread to run: cannot be NULL */ 85 struct k_thread *cache; 86 #endif 87 88 #if defined(CONFIG_SCHED_DUMB) 89 sys_dlist_t runq; 90 #elif defined(CONFIG_SCHED_SCALABLE) 91 struct _priq_rb runq; 92 #elif defined(CONFIG_SCHED_MULTIQ) 93 struct _priq_mq runq; 94 #endif 95 }; 96 97 typedef struct _ready_q _ready_q_t; 98 99 struct _cpu { 100 /* nested interrupt count */ 101 uint32_t nested; 102 103 /* interrupt stack pointer base */ 104 char *irq_stack; 105 106 /* currently scheduled thread */ 107 struct k_thread *current; 108 109 /* one assigned idle thread per CPU */ 110 struct k_thread *idle_thread; 111 112 #if (CONFIG_NUM_METAIRQ_PRIORITIES > 0) && (CONFIG_NUM_COOP_PRIORITIES > 0) 113 /* Coop thread preempted by current metairq, or NULL */ 114 struct k_thread *metairq_preempted; 115 #endif 116 117 #ifdef CONFIG_TIMESLICING 118 /* number of ticks remaining in current time slice */ 119 int slice_ticks; 120 #endif 121 122 uint8_t id; 123 124 #ifdef CONFIG_SMP 125 /* True when _current is allowed to context switch */ 126 uint8_t swap_ok; 127 #endif 128 129 /* Per CPU architecture specifics */ 130 struct _cpu_arch arch; 131 }; 132 133 typedef struct _cpu _cpu_t; 134 135 struct z_kernel { 136 struct _cpu cpus[CONFIG_MP_NUM_CPUS]; 137 138 #ifdef CONFIG_PM 139 int32_t idle; /* Number of ticks for kernel idling */ 140 #endif 141 142 /* 143 * ready queue: can be big, keep after small fields, since some 144 * assembly (e.g. ARC) are limited in the encoding of the offset 145 */ 146 struct _ready_q ready_q; 147 148 #ifdef CONFIG_FPU_SHARING 149 /* 150 * A 'current_sse' field does not exist in addition to the 'current_fp' 151 * field since it's not possible to divide the IA-32 non-integer 152 * registers into 2 distinct blocks owned by differing threads. In 153 * other words, given that the 'fxnsave/fxrstor' instructions 154 * save/restore both the X87 FPU and XMM registers, it's not possible 155 * for a thread to only "own" the XMM registers. 156 */ 157 158 /* thread that owns the FP regs */ 159 struct k_thread *current_fp; 160 #endif 161 162 #if defined(CONFIG_THREAD_MONITOR) 163 struct k_thread *threads; /* singly linked list of ALL threads */ 164 #endif 165 166 #if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED) 167 /* Need to signal an IPI at the next scheduling point */ 168 bool pending_ipi; 169 #endif 170 }; 171 172 typedef struct z_kernel _kernel_t; 173 174 extern struct z_kernel _kernel; 175 176 #ifdef CONFIG_SMP 177 178 /* True if the current context can be preempted and migrated to 179 * another SMP CPU. 180 */ 181 bool z_smp_cpu_mobile(void); 182 183 #define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \ 184 arch_curr_cpu(); }) 185 #define _current z_current_get() 186 187 #else 188 #define _current_cpu (&_kernel.cpus[0]) 189 #define _current _kernel.cpus[0].current 190 #endif 191 192 /* kernel wait queue record */ 193 194 #ifdef CONFIG_WAITQ_SCALABLE 195 196 typedef struct { 197 struct _priq_rb waitq; 198 } _wait_q_t; 199 200 extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b); 201 202 #define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } } 203 204 #else 205 206 typedef struct { 207 sys_dlist_t waitq; 208 } _wait_q_t; 209 210 #define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) } 211 212 #endif 213 214 /* kernel timeout record */ 215 216 struct _timeout; 217 typedef void (*_timeout_func_t)(struct _timeout *t); 218 219 struct _timeout { 220 sys_dnode_t node; 221 _timeout_func_t fn; 222 #ifdef CONFIG_TIMEOUT_64BIT 223 /* Can't use k_ticks_t for header dependency reasons */ 224 int64_t dticks; 225 #else 226 int32_t dticks; 227 #endif 228 }; 229 230 #ifdef __cplusplus 231 } 232 #endif 233 234 #endif /* _ASMLANGUAGE */ 235 236 #endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ */ 237