[1] | 1 | /* |
---|
| 2 | * hal_context.h - Generic Thread Context Access API definition. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #ifndef _HAL_CONTEXT_H_ |
---|
| 25 | #define _HAL_CONTEXT_H_ |
---|
| 26 | |
---|
| 27 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 28 | // Generic Thread Context API definition (implementation in hal_context.c) |
---|
| 29 | // |
---|
[151] | 30 | // A thread context is defined by the two (core specific) structures hal_cpu_context_t |
---|
| 31 | // and hal_fpu_context_t, defined in hal_context.c file, that are accessed with generic |
---|
[1] | 32 | // void* pointers stored in the thread descriptor. |
---|
[337] | 33 | // - the "hal_context_t" struct is used for the CPU registers values at context switch. |
---|
| 34 | // - the "hal_fpu_context_t" struct is used for the FPU registers when required. |
---|
[1] | 35 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | |
---|
| 37 | /**** Forward declarations ****/ |
---|
| 38 | |
---|
| 39 | struct thread_s; |
---|
| 40 | |
---|
| 41 | /**************************************************************************************** |
---|
[407] | 42 | * This function allocates memory for a CPU context and links it to the thread |
---|
| 43 | * identified by the <thread> argument. The context is not initialised. |
---|
[1] | 44 | **************************************************************************************** |
---|
[407] | 45 | * @ return 0 if success / return -1 if failure. |
---|
| 46 | ***************************************************************************************/ |
---|
| 47 | error_t hal_cpu_context_alloc( struct thread_s * thread ); |
---|
| 48 | |
---|
| 49 | /**************************************************************************************** |
---|
| 50 | * This function allocates memory for a CPU context, initialize it from scratch, |
---|
| 51 | * and links it to the thread identified by the <thread> argument. |
---|
| 52 | **************************************************************************************** |
---|
[1] | 53 | * @ thread : pointer on the thread descriptor. |
---|
[407] | 54 | * @ return 0 if success / return -1 if failure. |
---|
[1] | 55 | ***************************************************************************************/ |
---|
| 56 | error_t hal_cpu_context_create( struct thread_s * thread ); |
---|
| 57 | |
---|
| 58 | /**************************************************************************************** |
---|
[408] | 59 | * This function is used to implement the fork() system call. |
---|
| 60 | * 1) It saves in a remote (child) thread CPU context the current CPU registers values. |
---|
| 61 | * Three slots are not simple copies of the parent registers values : |
---|
| 62 | * - the thread pointer is set to the child thread local pointer. |
---|
| 63 | * - the stack pointer is set to parrent SP + (child_base - parent_base). |
---|
| 64 | * - the status register is set to kernel mode with IRQ disabled. |
---|
| 65 | * 2) It copies the content of the calling (parent) thread kernel_stack, |
---|
| 66 | * to the remote (child) thread kernel_stack. |
---|
[1] | 67 | **************************************************************************************** |
---|
[408] | 68 | * @ thread_xp : extended pointer on the remote thread descriptor. |
---|
[1] | 69 | ***************************************************************************************/ |
---|
[408] | 70 | void hal_cpu_context_fork( xptr_t thread_xp ); |
---|
[1] | 71 | |
---|
| 72 | /**************************************************************************************** |
---|
[408] | 73 | * This function display some slots of the CPU context. |
---|
| 74 | * - For the MIPS32 : |
---|
| 75 | * . GPR : gp_28 , sp_29 , ra_31 |
---|
| 76 | * . CP0 : c0_sr , c0_th , c0_epc |
---|
| 77 | * . CP2 : c2_ptpr , c2-mode |
---|
| 78 | * - For X86 TODO : |
---|
| 79 | **************************************************************************************** |
---|
| 80 | * @ thread_xp : extended pointer on the thread descriptor. |
---|
| 81 | ***************************************************************************************/ |
---|
| 82 | void hal_cpu_context_display( xptr_t thread_xp ); |
---|
| 83 | |
---|
| 84 | /**************************************************************************************** |
---|
[1] | 85 | * This function releases the physical memory allocated for a thread CPU context. |
---|
| 86 | **************************************************************************************** |
---|
| 87 | * @ thread : pointer on the thread descriptor. |
---|
| 88 | ***************************************************************************************/ |
---|
| 89 | void hal_cpu_context_destroy( struct thread_s * thread ); |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
[407] | 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
[1] | 97 | /**************************************************************************************** |
---|
[407] | 98 | * This function allocates memory for a FPU context, reset all entries, |
---|
| 99 | * and links it to the thread identified by the <thread> argument. |
---|
[1] | 100 | **************************************************************************************** |
---|
| 101 | * @ thread : pointer on the thread descriptor. |
---|
[407] | 102 | * @ return 0 if success / return -1 if failure. |
---|
[1] | 103 | ***************************************************************************************/ |
---|
[407] | 104 | error_t hal_fpu_context_alloc( struct thread_s * thread ); |
---|
[1] | 105 | |
---|
| 106 | /**************************************************************************************** |
---|
[407] | 107 | * This function copies a FPU context defined by the <src> argument to the FPU context |
---|
| 108 | * defined by the <dst> argument. It is used by the fork system call. |
---|
[1] | 109 | **************************************************************************************** |
---|
| 110 | * @ dst : pointer on the destination thread descriptor. |
---|
| 111 | * @ src : pointer on the source thread descriptor. |
---|
| 112 | ***************************************************************************************/ |
---|
[407] | 113 | void hal_fpu_context_copy( struct thread_s * dst, |
---|
[1] | 114 | struct thread_s * src ); |
---|
| 115 | |
---|
| 116 | /**************************************************************************************** |
---|
| 117 | * This function releases the physical memory allocated for a FPU context. |
---|
| 118 | **************************************************************************************** |
---|
| 119 | * @ thread : pointer on the thread descriptor. |
---|
| 120 | ***************************************************************************************/ |
---|
| 121 | void hal_fpu_context_destroy( struct thread_s * thread ); |
---|
| 122 | |
---|
| 123 | /**************************************************************************************** |
---|
[408] | 124 | * This function is used to implement the fork() system call. |
---|
| 125 | * It saves in a remote thread FPU context the current FPU registers values. |
---|
[1] | 126 | **************************************************************************************** |
---|
[408] | 127 | * @ thread_xp : extended pointer on the remote thread descriptor. |
---|
[1] | 128 | ***************************************************************************************/ |
---|
[408] | 129 | void hal_fpu_context_save( xptr_t thread_xp ); |
---|
[1] | 130 | |
---|
| 131 | /**************************************************************************************** |
---|
[408] | 132 | * This function restores from the calling thread FPU context the FPU registers values. |
---|
[1] | 133 | **************************************************************************************** |
---|
| 134 | * @ thread : pointer on the thread descriptor. |
---|
| 135 | ***************************************************************************************/ |
---|
| 136 | void hal_fpu_context_restore( struct thread_s * thread ); |
---|
| 137 | |
---|
| 138 | #endif /* _HAL_CONTEXT_H_ */ |
---|