|
[ source navigation ] [ diff markup ] [ identifier search ] [ general search ] |
|||
|
001 /* Copyright (C) 2005 David Decotigny !! 001 /* Copyright (C) 2000-2004, The KOS team 002 Copyright (C) 2000-2004, The KOS team !! 002 Copyright (C) 1999 Free Software Foundation, Inc. 003 003 004 This program is free software; you can redi 004 This program is free software; you can redistribute it and/or 005 modify it under the terms of the GNU Genera 005 modify it under the terms of the GNU General Public License 006 as published by the Free Software Foundatio 006 as published by the Free Software Foundation; either version 2 007 of the License, or (at your option) any lat 007 of the License, or (at your option) any later version. 008 008 009 This program is distributed in the hope tha 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details 012 GNU General Public License for more details. 013 013 014 You should have received a copy of the GNU 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to t 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place - Suite 3 016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 017 USA. 017 USA. 018 */ 018 */ 019 #ifndef _SOS_CPUCTXT_H_ 019 #ifndef _SOS_CPUCTXT_H_ 020 #define _SOS_CPUCTXT_H_ 020 #define _SOS_CPUCTXT_H_ 021 021 022 022 023 /** 023 /** 024 * @file cpu_context.h 024 * @file cpu_context.h 025 * 025 * 026 * Low level API to manage kernel and user thr 026 * Low level API to manage kernel and user thread CPU contexts. Should 027 * be some kind of architecture-independent. 027 * be some kind of architecture-independent. 028 */ 028 */ 029 029 030 #include <sos/types.h> 030 #include <sos/types.h> 031 #include <sos/errno.h> 031 #include <sos/errno.h> 032 032 033 033 034 /** 034 /** 035 * Prepare the system to deal with multiple CP !! 035 * Opaque structure storing the CPU context of an inactive kernel 036 */ !! 036 * thread, as saved by the low level primitives below or by the 037 sos_ret_t sos_cpu_context_subsystem_setup(); << 038 << 039 << 040 /** << 041 * Opaque structure storing the CPU context of << 042 * user thread, as saved by the low level prim << 043 * interrupt/exception handlers. 037 * interrupt/exception handlers. 044 * 038 * 045 * @note This is an (architecture-independent) 039 * @note This is an (architecture-independent) forward declaration: 046 * see cpu_context.c and the *.S files for its 040 * see cpu_context.c and the *.S files for its 047 * (architecture-dependent) definition. 041 * (architecture-dependent) definition. 048 */ 042 */ 049 struct sos_cpu_state; !! 043 struct sos_cpu_kstate; 050 044 051 045 052 /** 046 /** 053 * The type of the functions passed as argumen !! 047 * The type of the functions passed as arguments below 054 * related functions. << 055 */ 048 */ 056 typedef void (sos_cpu_kstate_function_arg1_t(s 049 typedef void (sos_cpu_kstate_function_arg1_t(sos_ui32_t arg1)); 057 050 058 051 059 /** 052 /** 060 * Function to create an initial context for a 053 * Function to create an initial context for a kernel thread starting 061 * its execution at function start_func with t 054 * its execution at function start_func with the argument initial_arg, 062 * and having the stack defined by stack_botto 055 * and having the stack defined by stack_bottom/stack_size. When the 063 * start_func function returns, the function e 056 * start_func function returns, the function exit_func is called with 064 * argument exit_arg. 057 * argument exit_arg. 065 * 058 * 066 * @param kctxt The kernel thread CPU context !! 059 * @param ctxt The kernel thread CPU context to initialize. The 067 * address of the newly-initialized struct sos !! 060 * address of the newly-initialized struct sos_cpu_kstate will be 068 * stored in this variable. The contents of th !! 061 * stored in this variable. The contents of this struct sos_cpu_kstate 069 * are actually located /inside/ the stack. 062 * are actually located /inside/ the stack. 070 * 063 * 071 * @param start_func The address of the first 064 * @param start_func The address of the first instruction that will be 072 * executed when this context will be first tr 065 * executed when this context will be first transferred on 073 * CPU. Practically speaking, this is the addr 066 * CPU. Practically speaking, this is the address of a function that 074 * is assumed to take 1 argument. 067 * is assumed to take 1 argument. 075 * 068 * 076 * @param start_arg The value that will be pas 069 * @param start_arg The value that will be passed as the argument to 077 * start_func when the thread starts. The stac 070 * start_func when the thread starts. The stack will be setup 078 * accordingly to simulate a real call to the 071 * accordingly to simulate a real call to the function and really 079 * passing this arguement. 072 * passing this arguement. 080 * 073 * 081 * @param stack_bottom The lowest address of t 074 * @param stack_bottom The lowest address of the stack. 082 * 075 * 083 * @param stack_size The size of the stack. 076 * @param stack_size The size of the stack. 084 * 077 * 085 * @param exit_func The address of the instruc 078 * @param exit_func The address of the instruction executed after the 086 * function start_func has returned. This func 079 * function start_func has returned. This function takes 1 parameter 087 * as argument: exit_arg. 080 * as argument: exit_arg. 088 * 081 * 089 * @param exit_arg The argument passed to the 082 * @param exit_arg The argument passed to the function exit_func. 090 * 083 * 091 * @note the newly created context is INTERRUP 084 * @note the newly created context is INTERRUPTIBLE by default ! 092 */ 085 */ 093 sos_ret_t sos_cpu_kstate_init(struct sos_cpu_s !! 086 sos_ret_t sos_cpu_kstate_init(struct sos_cpu_kstate **ctxt, 094 sos_cpu_kstate_f 087 sos_cpu_kstate_function_arg1_t *start_func, 095 sos_ui32_t star 088 sos_ui32_t start_arg, 096 sos_vaddr_t stac 089 sos_vaddr_t stack_bottom, 097 sos_size_t stac 090 sos_size_t stack_size, 098 sos_cpu_kstate_f 091 sos_cpu_kstate_function_arg1_t *exit_func, 099 sos_ui32_t exit 092 sos_ui32_t exit_arg); 100 093 101 094 102 /** 095 /** 103 * Function to create an initial context for a !! 096 * Function that performs an immediate context-switch from one kernel 104 * its execution at function user_start_PC wit !! 097 * thread to another one. It stores the current executing context in 105 * argument. The address of the user stack bef !! 098 * from_ctxt, and restores to_context on CPU. 106 * the ustate_init() function is given by user << 107 * thread starts in user space first and needs << 108 * the syscalls and for handling interrupts: t << 109 * kernel stack is given by the kernel_stack_* << 110 * << 111 * @param uctxt The user thread CPU context to << 112 * address of the newly-initialized struct sos << 113 * stored in this variable. The contents of th << 114 * are actually located /inside/ the kernel st << 115 * << 116 * @param user_start_PC The address of the fir << 117 * be executed in user mode when this context << 118 * transferred on CPU. Practically speaking, t << 119 * function that is assumed to take 1 argument << 120 * << 121 * @param user_start_SP The initial user stack << 122 * << 123 * @param user_start_argX The 2 parameters pas << 124 * thread function (in registers). << 125 * << 126 * @param kernel_stack_bottom The lowest addre << 127 * used to switch to user mode and to handle i << 128 * << 129 * @param kernel_stack_size The size of the ke << 130 * kernel_stack_bottom). << 131 * << 132 * @note the newly thread context is INTERRUPT << 133 */ << 134 sos_ret_t sos_cpu_ustate_init(struct sos_cpu_s << 135 sos_uaddr_t use << 136 sos_ui32_t use << 137 sos_ui32_t use << 138 sos_uaddr_t use << 139 sos_vaddr_t ker << 140 sos_size_t ker << 141 << 142 << 143 /** << 144 * Function to create an initial context for a << 145 * existing user thread context. The user thre << 146 * for the syscalls and for handling interrupt << 147 * kernel stack is given by the kernel_stack_* << 148 * << 149 * @param uctxt The user thread CPU context to << 150 * address of the newly-initialized struct sos << 151 * stored in this variable. The contents of th << 152 * are actually located /inside/ the kernel st << 153 * << 154 * @param model_uctxt The user thread context << 155 * the new user thread context << 156 * << 157 * @param user_retval The parameter passed to << 158 * thread function. << 159 * << 160 * @param kernel_stack_bottom The lowest addre << 161 * used to switch to user mode and to handle i << 162 * << 163 * @param kernel_stack_size The size of the ke << 164 * kernel_stack_bottom). << 165 * << 166 * @note the newly thread context is INTERRUPT << 167 */ << 168 sos_ret_t sos_cpu_ustate_duplicate(struct sos_ << 169 const struc << 170 sos_ui32_t << 171 sos_vaddr_t << 172 sos_size_t << 173 << 174 << 175 /** << 176 * Function that performs an immediate context << 177 * kernel/user thread to another one. It store << 178 * context in from_ctxt, and restores to_conte << 179 * 099 * 180 * @param from_ctxt The address of the struct !! 100 * @param from_ctxt The address of the struct sos_cpu_kstate will be 181 * stored in this variable. Must NOT be NULL. 101 * stored in this variable. Must NOT be NULL. 182 * 102 * 183 * @param to_ctxt The CPU will resume its exec 103 * @param to_ctxt The CPU will resume its execution with the struct 184 * sos_cpu_state located at this address. Must !! 104 * sos_cpu_kstate located at this address. Must NOT be NULL. 185 */ 105 */ 186 void sos_cpu_context_switch(struct sos_cpu_sta !! 106 void sos_cpu_kstate_switch(struct sos_cpu_kstate **from_ctxt, 187 struct sos_cpu_sta !! 107 struct sos_cpu_kstate *to_ctxt); 188 108 189 109 190 /* 110 /* 191 * Switch to the new given context (of a kerne !! 111 * Switch to the new given context (of a kernel thread) without saving 192 * saving the old context (of another kernel/u !! 112 * the old context (of another kernel thread), and call the function 193 * the function reclaiming_func passing it the !! 113 * reclaiming_func passing it the recalining_arg argument. The 194 * argument. The reclaining function is called !! 114 * reclaining function is called from within the stack of the new 195 * of the new context, so that it can (among o !! 115 * context, so that it can (among other things) safely destroy the 196 * destroy the stack of the former context. !! 116 * stack of the former context. 197 * 117 * 198 * @param switch_to_ctxt The context that will 118 * @param switch_to_ctxt The context that will be restored on the CPU 199 * 119 * 200 * @param reclaiming_func The address of the f 120 * @param reclaiming_func The address of the function that will be 201 * called after having changed the stack, but 121 * called after having changed the stack, but before restoring the CPU 202 * context to switch_to_ctxt. 122 * context to switch_to_ctxt. 203 */ 123 */ 204 void 124 void 205 sos_cpu_context_exit_to(struct sos_cpu_state * !! 125 sos_cpu_kstate_exit_to(struct sos_cpu_kstate *switch_to_ctxt, 206 sos_cpu_kstate_functio !! 126 sos_cpu_kstate_function_arg1_t *reclaiming_func, 207 sos_ui32_t reclaiming_ !! 127 sos_ui32_t reclaiming_arg) __attribute__((noreturn)); >> 128 208 129 209 /* =========================================== 130 /* ======================================================================= 210 * Public Accessor functions 131 * Public Accessor functions 211 */ 132 */ 212 133 213 << 214 /** << 215 * Return whether the saved context was in ker << 216 * << 217 * @return TRUE when context was interrupted w << 218 * when in kernel mode, < 0 on error. << 219 */ << 220 sos_ret_t << 221 sos_cpu_context_is_in_user_mode(const struct s << 222 << 223 << 224 /** 134 /** 225 * Return Program Counter stored in the saved !! 135 * Return Program Counter stored in the saved context 226 */ 136 */ 227 sos_vaddr_t sos_cpu_context_get_PC(const struc !! 137 sos_vaddr_t sos_cpu_kstate_get_PC(const struct sos_cpu_kstate *ctxt); 228 138 229 139 230 /** 140 /** 231 * Return Stack Pointer stored in the saved ke !! 141 * Return Stack Pointer stored in the saved context 232 */ 142 */ 233 sos_vaddr_t sos_cpu_context_get_SP(const struc !! 143 sos_vaddr_t sos_cpu_kstate_get_SP(const struct sos_cpu_kstate *ctxt); 234 144 235 145 236 /** 146 /** 237 * Dump the contents of the CPU context (bochs 147 * Dump the contents of the CPU context (bochs + x86_videomem) 238 */ 148 */ 239 void sos_cpu_context_dump(const struct sos_cpu !! 149 void sos_cpu_kstate_dump(const struct sos_cpu_kstate *ctxt); 240 150 241 151 242 /* =========================================== 152 /* ======================================================================= 243 * Public Accessor functions TO BE USED ONLY B 153 * Public Accessor functions TO BE USED ONLY BY Exception handlers 244 */ 154 */ 245 155 246 156 247 /** 157 /** 248 * Return the argument passed by the CPU upon 158 * Return the argument passed by the CPU upon exception, as stored in the 249 * saved context 159 * saved context 250 */ 160 */ 251 sos_ui32_t sos_cpu_context_get_EX_info(const s !! 161 sos_ui32_t sos_cpu_kstate_get_EX_info(const struct sos_cpu_kstate *ctxt); 252 162 253 163 254 /** 164 /** 255 * Return the faulting address of the exceptio 165 * Return the faulting address of the exception 256 */ 166 */ 257 sos_vaddr_t 167 sos_vaddr_t 258 sos_cpu_context_get_EX_faulting_vaddr(const st !! 168 sos_cpu_kstate_get_EX_faulting_vaddr(const struct sos_cpu_kstate *ctxt); 259 << 260 << 261 /** << 262 * Change the return address of the given cont << 263 */ << 264 sos_ret_t << 265 sos_cpu_context_set_EX_return_address(struct s << 266 sos_vadd << 267 << 268 << 269 /* =========================================== << 270 * Public Accessor functions TO BE USED ONLY B << 271 */ << 272 << 273 /** << 274 * Low-level functions used by the syscall han << 275 * responsible for retrieving the arguments pa << 276 * a user thread makes a syscall. Some of thes << 277 * available as registers' values in the user << 278 * are user-space addresses given by these reg << 279 * << 280 * @return SOS_OK on success, <0 otherwise << 281 */ << 282 sos_ret_t sos_syscall_get1arg(const struct sos << 283 /* out */unsigne << 284 << 285 sos_ret_t sos_syscall_get2args(const struct so << 286 /* out */unsign << 287 /* out */unsign << 288 << 289 sos_ret_t sos_syscall_get3args(const struct so << 290 /* out */unsign << 291 /* out */unsign << 292 /* out */unsign << 293 << 294 sos_ret_t sos_syscall_get4args(const struct so << 295 /* out */unsign << 296 /* out */unsign << 297 /* out */unsign << 298 /* out */unsign << 299 << 300 sos_ret_t sos_syscall_get5args(const struct so << 301 /* out */unsign << 302 /* out */unsign << 303 /* out */unsign << 304 /* out */unsign << 305 /* out */unsign << 306 << 307 sos_ret_t sos_syscall_get6args(const struct so << 308 /* out */unsign << 309 /* out */unsign << 310 /* out */unsign << 311 /* out */unsign << 312 /* out */unsign << 313 /* out */unsign << 314 << 315 sos_ret_t sos_syscall_get7args(const struct so << 316 /* out */unsign << 317 /* out */unsign << 318 /* out */unsign << 319 /* out */unsign << 320 /* out */unsign << 321 /* out */unsign << 322 /* out */unsign << 323 << 324 sos_ret_t sos_syscall_get8args(const struct so << 325 /* out */unsign << 326 /* out */unsign << 327 /* out */unsign << 328 /* out */unsign << 329 /* out */unsign << 330 /* out */unsign << 331 /* out */unsign << 332 /* out */unsign << 333 169 334 170 335 /* =========================================== 171 /* ======================================================================= 336 * Macros controlling stack poisoning. 172 * Macros controlling stack poisoning. 337 * Stack poisoning can be used to detect: 173 * Stack poisoning can be used to detect: 338 * - unitialized local variables 174 * - unitialized local variables 339 * - when the thread might have gone too deep 175 * - when the thread might have gone too deep in the stack 340 */ 176 */ 341 /** The signature of the poison */ 177 /** The signature of the poison */ 342 #define SOS_CPU_STATE_STACK_POISON 0xa5 !! 178 #define SOS_CPU_KSTATE_STACK_POISON 0xa5 343 179 344 /** 180 /** 345 * When set, mean that the whole stack is pois 181 * When set, mean that the whole stack is poisoned to detect use of 346 * unititialized variables 182 * unititialized variables 347 */ 183 */ 348 #define SOS_CPU_STATE_DETECT_UNINIT_KERNEL_VAR !! 184 #define SOS_CPU_KSTATE_DETECT_UNINIT_VARS 349 /* #undef SOS_CPU_STATE_DETECT_UNINIT_KERNEL_V !! 185 /* #undef SOS_CPU_KSTATE_DETECT_UNINIT_VARS */ 350 186 351 /** 187 /** 352 * When set, mean that the bottom of the stack 188 * When set, mean that the bottom of the stack is poisoned to detect 353 * probable stack overflow. Its value indicate 189 * probable stack overflow. Its value indicates the number of bytes 354 * used for this detection. 190 * used for this detection. 355 */ 191 */ 356 #define SOS_CPU_STATE_DETECT_KERNEL_STACK_OVER !! 192 #define SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW 64 357 /* #undef SOS_CPU_STATE_DETECT_KERNEL_STACK_OV !! 193 /* #undef SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW */ 358 194 359 #if defined(SOS_CPU_STATE_DETECT_KERNEL_STACK_ !! 195 #if defined(SOS_CPU_KSTATE_DETECT_STACK_OVERFLOW) 360 void 196 void 361 sos_cpu_state_prepare_detect_kernel_stack_over !! 197 sos_cpu_kstate_prepare_detect_stack_overflow(const struct sos_cpu_kstate *ctxt, 362 !! 198 sos_vaddr_t stack_bottom, 363 !! 199 sos_size_t stack_size); 364 void sos_cpu_state_detect_kernel_stack_overflo !! 200 void sos_cpu_kstate_detect_stack_overflow(const struct sos_cpu_kstate *ctxt, 365 !! 201 sos_vaddr_t stack_bottom, 366 !! 202 sos_size_t stack_size); 367 #else 203 #else 368 # define sos_cpu_state_prepare_detect_kernel_s !! 204 # define sos_cpu_kstate_prepare_detect_stack_overflow(ctxt,stkbottom,stksize) \ 369 ({ /* nop */ }) 205 ({ /* nop */ }) 370 # define sos_cpu_state_detect_kernel_stack_ove !! 206 # define sos_cpu_kstate_detect_stack_overflow(ctxt,stkbottom,stksize) \ 371 ({ /* nop */ }) 207 ({ /* nop */ }) 372 #endif 208 #endif 373 209 374 210 375 /* =========================================== 211 /* ======================================================================= 376 * Backtrace facility. To be used for DEBUGgin 212 * Backtrace facility. To be used for DEBUGging purpose ONLY. 377 */ 213 */ 378 214 379 215 380 /** 216 /** 381 * The function called at each step of the bac 217 * The function called at each step of the backtrace iterations 382 * 218 * 383 * @param PC The address of the next instructi 219 * @param PC The address of the next instruction of the function that 384 * will be executed 220 * will be executed 385 * 221 * 386 * @param params The address of the array of t 222 * @param params The address of the array of the parameteres that have 387 * been passed to the function considered 223 * been passed to the function considered 388 * 224 * 389 * @param depth The index of the iteration (ie 225 * @param depth The index of the iteration (ie the depth of the 390 * current frame into the stack) 226 * current frame into the stack) 391 * 227 * 392 * @param custom_arg Whatever you want: this i 228 * @param custom_arg Whatever you want: this is the argument passed as 393 * custom_arg to sos_backtrace() 229 * custom_arg to sos_backtrace() 394 */ 230 */ 395 typedef void (sos_backtrace_callback_t)(sos_va 231 typedef void (sos_backtrace_callback_t)(sos_vaddr_t PC, 396 sos_va 232 sos_vaddr_t params, 397 sos_ui 233 sos_ui32_t depth, 398 void * 234 void *custom_arg); 399 235 400 236 401 /** 237 /** 402 * Call the backtracer callback on each frame !! 238 * Call the backtracer callback on each frame stored in the cpu_kstate 403 * 239 * 404 * @param cpu_state The CPU context we want to !! 240 * @param cpu_kstate The CPU context we want to explore. NULL to 405 * context of a thread in Kernel mode, or NULL !! 241 * backtrace the current CPU context. 406 * the current CPU context. << 407 * 242 * 408 * @param max_depth The maximum number of fram 243 * @param max_depth The maximum number of frames to explore 409 * 244 * 410 * @param stack_bottom The lower boundary of t 245 * @param stack_bottom The lower boundary of the stack. This is used 411 * to make sure that the frame addresses fit i 246 * to make sure that the frame addresses fit inside the stack 412 * boudaries (ie are potentially correct). 247 * boudaries (ie are potentially correct). 413 * 248 * 414 * @param stack_size The size of the stack. Sa 249 * @param stack_size The size of the stack. Same comment. 415 * 250 * 416 * @param backtracer The function to call to h 251 * @param backtracer The function to call to handle the frame for each 417 * iteration 252 * iteration 418 * 253 * 419 * @param custom_arg The arg passed as custom_ 254 * @param custom_arg The arg passed as custom_arg to the backtracer 420 * 255 * 421 * @return The number of frames explored. 256 * @return The number of frames explored. 422 * 257 * 423 * @note Might be inaccurate when gcc's -fomit 258 * @note Might be inaccurate when gcc's -fomit-frame-pointer has been 424 * used. 259 * used. 425 */ 260 */ 426 sos_ui32_t sos_backtrace(const struct sos_cpu_ !! 261 sos_ui32_t sos_backtrace(const struct sos_cpu_kstate *cpu_kstate, 427 sos_ui32_t max_depth, 262 sos_ui32_t max_depth, 428 sos_vaddr_t stack_bot 263 sos_vaddr_t stack_bottom, 429 sos_size_t stack_size 264 sos_size_t stack_size, 430 sos_backtrace_callbac 265 sos_backtrace_callback_t * backtracer, 431 void *custom_arg); 266 void *custom_arg); 432 267 433 #endif /* _SOS_CPUCTXT_H_ */ 268 #endif /* _SOS_CPUCTXT_H_ */
[ source navigation ] | [ diff markup ] | [ identifier search ] | [ general search ] |