|
[ source navigation ] [ diff markup ] [ identifier search ] [ general search ] |
|||
|
001 /* Copyright (C) 2000-2004, The KOS team 002 Copyright (C) 1999 Free Software Foundation, Inc. 003 004 This program is free software; you can redistribute it and/or 005 modify it under the terms of the GNU General Public License 006 as published by the Free Software Foundation; either version 2 007 of the License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 017 USA. 018 */ 019 #ifndef _SOS_CPUCTXT_H_ 020 #define _SOS_CPUCTXT_H_ 021 022 023 /** 024 * @file cpu_context.h 025 * 026 * Low level API to manage kernel and user thread CPU contexts. Should 027 * be some kind of architecture-independent. 028 */ 029 030 #include <sos/types.h> 031 #include <sos/errno.h> 032 033 034 /** 035 * Opaque structure storing the CPU context of an inactive kernel 036 * thread, as saved by the low level primitives below or by the 037 * interrupt/exception handlers. 038 * 039 * @note This is an (architecture-independent) forward declaration: 040 * see cpu_context.c and the *.S files for its 041 * (architecture-dependent) definition. 042 */ 043 struct sos_cpu_kstate; 044 045 046 /** 047 * The type of the functions passed as arguments below 048 */ 049 typedef void (sos_cpu_kstate_function_arg1_t(sos_ui32_t arg1)); 050 051 052 /** 053 * Function to create an initial context for a kernel thread starting 054 * its execution at function start_func with the argument initial_arg, 055 * and having the stack defined by stack_bottom/stack_size. When the 056 * start_func function returns, the function exit_func is called with 057 * argument exit_arg. 058 * 059 * @param ctxt The kernel thread CPU context to initialize. The 060 * address of the newly-initialized struct sos_cpu_kstate will be 061 * stored in this variable. The contents of this struct sos_cpu_kstate 062 * are actually located /inside/ the stack. 063 * 064 * @param start_func The address of the first instruction that will be 065 * executed when this context will be first transferred on 066 * CPU. Practically speaking, this is the address of a function that 067 * is assumed to take 1 argument. 068 * 069 * @param start_arg The value that will be passed as the argument to 070 * start_func when the thread starts. The stack will be setup 071 * accordingly to simulate a real call to the function and really 072 * passing this arguement. 073 * 074 * @param stack_bottom The lowest address of the stack. 075 * 076 * @param stack_size The size of the stack. 077 * 078 * @param exit_func The address of the instruction executed after the 079 * function start_func has returned. This function takes 1 parameter 080 * as argument: exit_arg. 081 * 082 * @param exit_arg The argument passed to the function exit_func. 083 * 084 * @note the newly created context is INTERRUPTIBLE by default ! 085 */ 086 sos_ret_t sos_cpu_kstate_init(struct sos_cpu_kstate **ctxt, 087 sos_cpu_kstate_function_arg1_t *start_func, 088 sos_ui32_t start_arg, 089 sos_vaddr_t stack_bottom, 090 sos_size_t stack_size, 091 sos_cpu_kstate_function_arg1_t *exit_func, 092 sos_ui32_t exit_arg); 093 094 095 /** 096 * Function that performs an immediate context-switch from one kernel 097 * thread to another one. It stores the current executing context in 098 * from_ctxt, and restores to_context on CPU. 099 * 100 * @param from_ctxt The address of the struct sos_cpu_kstate will be 101 * stored in this variable. Must NOT be NULL. 102 * 103 * @param to_ctxt The CPU will resume its execution with the struct 104 * sos_cpu_kstate located at this address. Must NOT be NULL. 105 */ 106 void sos_cpu_kstate_switch(struct sos_cpu_kstate **from_ctxt, 107 struct sos_cpu_kstate *to_ctxt); 108 109 110 /* 111 * Switch to the new given context (of a kernel thread) without saving 112 * the old context (of another kernel thread), and call the function 113 * reclaiming_func passing it the recalining_arg argument. The 114 * reclaining function is called from within the stack of the new 115 * context, so that it can (among other things) safely destroy the 116 * stack of the former context. 117 * 118 * @param switch_to_ctxt The context that will be restored on the CPU 119 * 120 * @param reclaiming_func The address of the function that will be 121 * called after having changed the stack, but before restoring the CPU 122 * context to switch_to_ctxt. 123 */ 124 void 125 sos_cpu_kstate_exit_to(struct sos_cpu_kstate *switch_to_ctxt, 126 sos_cpu_kstate_function_arg1_t *reclaiming_func, 127 sos_ui32_t reclaiming_arg) __attribute__((noreturn)); 128 129 130 /* ======================================================================= 131 * Public Accessor functions 132 */ 133 134 /** 135 * Return Program Counter stored in the saved context 136 */ 137 sos_vaddr_t sos_cpu_kstate_get_PC(const struct sos_cpu_kstate *ctxt); 138 139 140 /** 141 * Return Stack Pointer stored in the saved context 142 */ 143 sos_vaddr_t sos_cpu_kstate_get_SP(const struct sos_cpu_kstate *ctxt); 144 145 146 /** 147 * Dump the contents of the CPU context (bochs + x86_videomem) 148 */ 149 void sos_cpu_kstate_dump(const struct sos_cpu_kstate *ctxt); 150 151 152 /* ======================================================================= 153 * Public Accessor functions TO BE USED ONLY BY Exception handlers 154 */ 155 156 157 /** 158 * Return the argument passed by the CPU upon exception, as stored in the 159 * saved context 160 */ 161 sos_ui32_t sos_cpu_kstate_get_EX_info(const struct sos_cpu_kstate *ctxt); 162 163 164 /** 165 * Return the faulting address of the exception 166 */ 167 sos_vaddr_t 168 sos_cpu_kstate_get_EX_faulting_vaddr(const struct sos_cpu_kstate *ctxt); 169 170 171 /* ======================================================================= 172 * Macros controlling stack poisoning. 173 * Stack poisoning can be used to detect: 174 * - unitialized local variables 175 * - when the thread might have gone too deep in the stack 176 */ 177 /** The signature of the poison */ 178 #define SOS_CPU_KSTATE_STACK_POISON 0xa5 179 180 /** 181 * When set, mean that the whole stack is poisoned to detect use of 182 * unititialized variables 183 */ 184 #define SOS_CPU_KSTATE_DETECT_UNINIT_VARS 185 /* #undef SOS_CPU_KSTATE_DETECT_UNINIT_VARS */ 186 187 /** 188 * When set, mean that the bottom of the stack is poisoned to detect 189 * probable stack overflow. Its value indicates the number of bytes 190 * used for this detection. 191 */ 192 #define SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW 64 193 /* #undef SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW */ 194 195 #if defined(SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW) 196 void 197 sos_cpu_kstate_prepare_detect_stack_overflow(const struct sos_cpu_kstate *ctxt, 198 sos_vaddr_t stack_bottom, 199 sos_size_t stack_size); 200 void sos_cpu_kstate_detect_stack_overflow(const struct sos_cpu_kstate *ctxt, 201 sos_vaddr_t stack_bottom, 202 sos_size_t stack_size); 203 #else 204 # define sos_cpu_kstate_prepare_detect_stack_overflow(ctxt,stkbottom,stksize) \ 205 ({ /* nop */ }) 206 # define sos_cpu_kstate_detect_stack_overflow(ctxt,stkbottom,stksize) \ 207 ({ /* nop */ }) 208 #endif 209 210 211 /* ======================================================================= 212 * Backtrace facility. To be used for DEBUGging purpose ONLY. 213 */ 214 215 216 /** 217 * The function called at each step of the backtrace iterations 218 * 219 * @param PC The address of the next instruction of the function that 220 * will be executed 221 * 222 * @param params The address of the array of the parameteres that have 223 * been passed to the function considered 224 * 225 * @param depth The index of the iteration (ie the depth of the 226 * current frame into the stack) 227 * 228 * @param custom_arg Whatever you want: this is the argument passed as 229 * custom_arg to sos_backtrace() 230 */ 231 typedef void (sos_backtrace_callback_t)(sos_vaddr_t PC, 232 sos_vaddr_t params, 233 sos_ui32_t depth, 234 void *custom_arg); 235 236 237 /** 238 * Call the backtracer callback on each frame stored in the cpu_kstate 239 * 240 * @param cpu_kstate The CPU context we want to explore. NULL to 241 * backtrace the current CPU context. 242 * 243 * @param max_depth The maximum number of frames to explore 244 * 245 * @param stack_bottom The lower boundary of the stack. This is used 246 * to make sure that the frame addresses fit inside the stack 247 * boudaries (ie are potentially correct). 248 * 249 * @param stack_size The size of the stack. Same comment. 250 * 251 * @param backtracer The function to call to handle the frame for each 252 * iteration 253 * 254 * @param custom_arg The arg passed as custom_arg to the backtracer 255 * 256 * @return The number of frames explored. 257 * 258 * @note Might be inaccurate when gcc's -fomit-frame-pointer has been 259 * used. 260 */ 261 sos_ui32_t sos_backtrace(const struct sos_cpu_kstate *cpu_kstate, 262 sos_ui32_t max_depth, 263 sos_vaddr_t stack_bottom, 264 sos_size_t stack_size, 265 sos_backtrace_callback_t * backtracer, 266 void *custom_arg); 267 268 #endif /* _SOS_CPUCTXT_H_ */
[ source navigation ] | [ diff markup ] | [ identifier search ] | [ general search ] |