| 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 | // | 
|---|
| 30 | // A thread context is defined by the two (core specific) structures hal_context_t | 
|---|
| 31 | // and hal_uzone_t, defined  in hal_context.c file, that are accessed with generic | 
|---|
| 32 | // void* pointers stored in the thread descriptor. | 
|---|
| 33 | // - the "hal_context_t" structure is used to store the CPU registers values that | 
|---|
| 34 | //   have not been saved in the stack by the interrupt handler. | 
|---|
| 35 | // - the "hal_uzone_t" structure is used to save the FPU registers when required. | 
|---|
| 36 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 37 |  | 
|---|
| 38 | /**** Forward declarations ****/ | 
|---|
| 39 |  | 
|---|
| 40 | struct thread_s; | 
|---|
| 41 |  | 
|---|
| 42 | /**************************************************************************************** | 
|---|
| 43 | * Define various SR values for TSAR-MIPS32 | 
|---|
| 44 | ***************************************************************************************/ | 
|---|
| 45 |  | 
|---|
| 46 | #define SR_USR_MODE       0xFC11 | 
|---|
| 47 | #define SR_USR_MODE_FPU   0x2000FC11 | 
|---|
| 48 | #define SR_SYS_MODE       0xFC00 | 
|---|
| 49 |  | 
|---|
| 50 | /**************************************************************************************** | 
|---|
| 51 | * This structure defines the cpu_context for TSAR MIPS32. | 
|---|
| 52 | * These registers are saved/restored at each context switch. | 
|---|
| 53 | * WARNING : update the hal_cpu_context_save() and hal_cpu_context_restore() | 
|---|
| 54 | *           functions when modifying this structure. | 
|---|
| 55 | ***************************************************************************************/ | 
|---|
| 56 |  | 
|---|
| 57 | typedef struct hal_cpu_context_s | 
|---|
| 58 | { | 
|---|
| 59 | uint32_t s0_16;      // slot 0 | 
|---|
| 60 | uint32_t s1_17;      // slot 1 | 
|---|
| 61 | uint32_t s2_18;      // slot 2 | 
|---|
| 62 | uint32_t s3_19;      // slot 3 | 
|---|
| 63 | uint32_t s4_20;      // slot 4 | 
|---|
| 64 | uint32_t s5_21;      // slot 5 | 
|---|
| 65 | uint32_t s6_22;      // slot 6 | 
|---|
| 66 | uint32_t s7_23;      // slot 7 | 
|---|
| 67 | uint32_t sp_29;      // slot 8 | 
|---|
| 68 | uint32_t fp_30;      // slot 9 | 
|---|
| 69 | uint32_t ra_31;      // slot 10 | 
|---|
| 70 | uint32_t c0_sr;      // slot 11 | 
|---|
| 71 | uint32_t c0_th;      // slot 12 | 
|---|
| 72 | uint32_t c2_ptpr;    // slot 13 | 
|---|
| 73 | uint32_t c2_mode;    // slot 14 | 
|---|
| 74 | } | 
|---|
| 75 | hal_cpu_context_t; | 
|---|
| 76 |  | 
|---|
| 77 | /**************************************************************************************** | 
|---|
| 78 | * This structure defines the fpu_context for TSAR MIPS32. | 
|---|
| 79 | ***************************************************************************************/ | 
|---|
| 80 |  | 
|---|
| 81 | typedef struct hal_fpu_context_s | 
|---|
| 82 | { | 
|---|
| 83 | uint32_t   fpu_regs[32]; | 
|---|
| 84 | } | 
|---|
| 85 | hal_fpu_context_t; | 
|---|
| 86 |  | 
|---|
| 87 | /**************************************************************************************** | 
|---|
| 88 | * This function allocates, from the local cluster, the physical memory required for | 
|---|
| 89 | * the thread CPU context, initialises it, and link the context to the thread. | 
|---|
| 90 | * Seven registers are initialised: | 
|---|
| 91 | * - sp_29 / fp_30 / ra_31 | 
|---|
| 92 | * - c0_sr / c0_th | 
|---|
| 93 | * - c2_ptpr / c2_mode | 
|---|
| 94 | **************************************************************************************** | 
|---|
| 95 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 96 | * @ return 0 if success / return ENOMEM if error | 
|---|
| 97 | ***************************************************************************************/ | 
|---|
| 98 | error_t hal_cpu_context_create( struct thread_s * thread ); | 
|---|
| 99 |  | 
|---|
| 100 | /**************************************************************************************** | 
|---|
| 101 | * This function allocates, from the local cluster, the physical memory required for | 
|---|
| 102 | * a thread CPU context, initialises it from values contained in "src" thread context, | 
|---|
| 103 | * and link the context to the "dst" thread. | 
|---|
| 104 | **************************************************************************************** | 
|---|
| 105 | * @ dst  : pointer on the destination thread descriptor. | 
|---|
| 106 | * @ src  : pointer on the source thread descriptor. | 
|---|
| 107 | * @ return 0 if success / return ENOMEM if error | 
|---|
| 108 | ***************************************************************************************/ | 
|---|
| 109 | error_t hal_cpu_context_copy( struct thread_s * dst, | 
|---|
| 110 | struct thread_s * src ); | 
|---|
| 111 |  | 
|---|
| 112 | /**************************************************************************************** | 
|---|
| 113 | * This function releases the physical memory allocated for a thread CPU context. | 
|---|
| 114 | **************************************************************************************** | 
|---|
| 115 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 116 | ***************************************************************************************/ | 
|---|
| 117 | void hal_cpu_context_destroy( struct thread_s * thread ); | 
|---|
| 118 |  | 
|---|
| 119 | /**************************************************************************************** | 
|---|
| 120 | * This function save in the thread context the CPU registers values that have not | 
|---|
| 121 | * been saved in the thread stack by the exception handler: | 
|---|
| 122 | * - GRs : s0 to S7 , sp , fp, ra | 
|---|
| 123 | * - CP0 : th | 
|---|
| 124 | * - CP2 : ptpr , mode | 
|---|
| 125 | **************************************************************************************** | 
|---|
| 126 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 127 | ***************************************************************************************/ | 
|---|
| 128 | void hal_cpu_context_save( struct thread_s * thread ); | 
|---|
| 129 |  | 
|---|
| 130 | /**************************************************************************************** | 
|---|
| 131 | * This function restore from the thread context the CPU registers values that have not | 
|---|
| 132 | * been saved in the thread stack by the exception handler. | 
|---|
| 133 | * - GRs : s0 to S7 , sp , fp, ra | 
|---|
| 134 | * - CP0 : th | 
|---|
| 135 | * - CP2 : ptpr , mode | 
|---|
| 136 | **************************************************************************************** | 
|---|
| 137 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 138 | ***************************************************************************************/ | 
|---|
| 139 | void hal_cpu_context_restore( struct thread_s * thread ); | 
|---|
| 140 |  | 
|---|
| 141 | /**************************************************************************************** | 
|---|
| 142 | * This function load the relevant CPU registers from values contained in | 
|---|
| 143 | * the thread context. It should be called for a thread that has not been executed yet. | 
|---|
| 144 | * - GRs : sp , fp , a0 | 
|---|
| 145 | * - CP0 : sr , epc , th | 
|---|
| 146 | * - CP2 : ptpr , mode | 
|---|
| 147 | * It reset the loadable flag in thread descriptor. | 
|---|
| 148 | **************************************************************************************** | 
|---|
| 149 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 150 | ***************************************************************************************/ | 
|---|
| 151 | void hal_cpu_context_load( struct thread_s * thread ); | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /**************************************************************************************** | 
|---|
| 156 | * This function allocates, from the local cluster, the physical memory required for | 
|---|
| 157 | * the thread FPU context, and initialises the thread pointer. | 
|---|
| 158 | **************************************************************************************** | 
|---|
| 159 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 160 | * @ return 0 if success / return ENOMEM if error | 
|---|
| 161 | ***************************************************************************************/ | 
|---|
| 162 | error_t hal_fpu_context_create( struct thread_s * thread ); | 
|---|
| 163 |  | 
|---|
| 164 | /**************************************************************************************** | 
|---|
| 165 | * This function allocates, from the local cluster, the physical memory required for | 
|---|
| 166 | * a thread FPU context, initialises it from values contained in "src" thread context, | 
|---|
| 167 | * and link the context to the "dst" thread. | 
|---|
| 168 | **************************************************************************************** | 
|---|
| 169 | * @ dst  : pointer on the destination thread descriptor. | 
|---|
| 170 | * @ src  : pointer on the source thread descriptor. | 
|---|
| 171 | * @ return 0 if success / return ENOMEM if error | 
|---|
| 172 | ***************************************************************************************/ | 
|---|
| 173 | error_t hal_fpu_context_copy( struct thread_s * dst, | 
|---|
| 174 | struct thread_s * src ); | 
|---|
| 175 |  | 
|---|
| 176 | /**************************************************************************************** | 
|---|
| 177 | * This function releases the physical memory allocated for a FPU context. | 
|---|
| 178 | **************************************************************************************** | 
|---|
| 179 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 180 | ***************************************************************************************/ | 
|---|
| 181 | void hal_fpu_context_destroy( struct thread_s * thread ); | 
|---|
| 182 |  | 
|---|
| 183 | /**************************************************************************************** | 
|---|
| 184 | * This function save in the thread uzone the FPU registers values. | 
|---|
| 185 | **************************************************************************************** | 
|---|
| 186 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 187 | ***************************************************************************************/ | 
|---|
| 188 | void hal_fpu_context_save( struct thread_s * thread ); | 
|---|
| 189 |  | 
|---|
| 190 | /**************************************************************************************** | 
|---|
| 191 | * This function restore from the thread uzone the FPU registers values. | 
|---|
| 192 | **************************************************************************************** | 
|---|
| 193 | * @ thread  : pointer on the thread descriptor. | 
|---|
| 194 | ***************************************************************************************/ | 
|---|
| 195 | void hal_fpu_context_restore( struct thread_s * thread ); | 
|---|
| 196 |  | 
|---|
| 197 | #endif  /* _HAL_CONTEXT_H_ */ | 
|---|