| 1 | /* |
|---|
| 2 | * hal_context.h - Generic Thread Context Access API definition. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017,2018,2019) |
|---|
| 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_cpu_context_t |
|---|
| 31 | // and hal_fpu_context_t, defined in hal_context.c file, that are accessed with generic |
|---|
| 32 | // void* pointers stored in the thread descriptor. |
|---|
| 33 | // - the "hal_cpu_context_t" struct saves the CPU registers values at context switch. |
|---|
| 34 | // - the "hal_fpu_context_t" struct saves the FPU registers values at FPU switch. |
|---|
| 35 | ////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | /**** Forward declarations ****/ |
|---|
| 38 | |
|---|
| 39 | struct thread_s; |
|---|
| 40 | |
|---|
| 41 | /**************************************************************************************** |
|---|
| 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. |
|---|
| 44 | **************************************************************************************** |
|---|
| 45 | * @ thread : pointer on the thread descriptor. |
|---|
| 46 | * @ return 0 if success / return -1 if failure. |
|---|
| 47 | ***************************************************************************************/ |
|---|
| 48 | error_t hal_cpu_context_alloc( struct thread_s * thread ); |
|---|
| 49 | |
|---|
| 50 | /**************************************************************************************** |
|---|
| 51 | * This function initializes the CPU context of the thread identified by the <thread> |
|---|
| 52 | * argument. All slots required to start a new thread must be initialized. |
|---|
| 53 | **************************************************************************************** |
|---|
| 54 | * @ thread : pointer on the thread descriptor. |
|---|
| 55 | ***************************************************************************************/ |
|---|
| 56 | void hal_cpu_context_init( struct thread_s * thread ); |
|---|
| 57 | |
|---|
| 58 | /**************************************************************************************** |
|---|
| 59 | * This function is called the sys_fork() function to complete the fork mechanism. |
|---|
| 60 | * It is called by th local parent thread to initialize the CPU context of the remote |
|---|
| 61 | * child thread, identified by the <thread_xp> argument. |
|---|
| 62 | * It makes three actions: |
|---|
| 63 | * 1) It copies the current values of the CPU registers of the core running the parent |
|---|
| 64 | * thread to the remote child CPU context. |
|---|
| 65 | * 2) It patches four slots of this remote child CPU context: |
|---|
| 66 | * - the c0_th slot is set to the child thread descriptor pointer. |
|---|
| 67 | * - the sp_29 slot is set to the child kernel stack pointer. |
|---|
| 68 | * - the c0_sr slot is set to kernel mode with IRQ disabled. |
|---|
| 69 | * - the c2_ptpr slot is set to the child process GPT value. |
|---|
| 70 | * 3) It copies the content of the parent thread kernel_stack, to the child thread |
|---|
| 71 | * kernel_stack, because the COW mechanism is not available on architectures where |
|---|
| 72 | * the data MMU is de-activated in kernel mode. |
|---|
| 73 | **************************************************************************************** |
|---|
| 74 | * @ thread_xp : extended pointer on the child thread descriptor. |
|---|
| 75 | ***************************************************************************************/ |
|---|
| 76 | void hal_cpu_context_fork( xptr_t thread_xp ); |
|---|
| 77 | |
|---|
| 78 | /**************************************************************************************** |
|---|
| 79 | * This function is used to implement the exec() system call. |
|---|
| 80 | * 1) It initialize the relevant slots of the the calling thread CPU context. |
|---|
| 81 | * 2) It call the hal_do_cpu_restore() function to return to user mode and start |
|---|
| 82 | * execution of the new process. |
|---|
| 83 | **************************************************************************************** |
|---|
| 84 | * @ thread : pointer on the thread descriptor. |
|---|
| 85 | ***************************************************************************************/ |
|---|
| 86 | void hal_cpu_context_exec( struct thread_s * thread ); |
|---|
| 87 | |
|---|
| 88 | /**************************************************************************************** |
|---|
| 89 | * This function display some slots of the CPU context. |
|---|
| 90 | **************************************************************************************** |
|---|
| 91 | * @ thread_xp : extended pointer on the thread descriptor. |
|---|
| 92 | ***************************************************************************************/ |
|---|
| 93 | void hal_cpu_context_display( xptr_t thread_xp ); |
|---|
| 94 | |
|---|
| 95 | /**************************************************************************************** |
|---|
| 96 | * This function releases the physical memory allocated for a thread CPU context. |
|---|
| 97 | **************************************************************************************** |
|---|
| 98 | * @ thread : pointer on the thread descriptor. |
|---|
| 99 | ***************************************************************************************/ |
|---|
| 100 | void hal_cpu_context_destroy( struct thread_s * thread ); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /**************************************************************************************** |
|---|
| 109 | * This function allocates memory for a FPU context, reset all entries, |
|---|
| 110 | * and links it to the thread identified by the <thread> argument. |
|---|
| 111 | **************************************************************************************** |
|---|
| 112 | * @ thread : pointer on the thread descriptor. |
|---|
| 113 | * @ return 0 if success / return -1 if failure. |
|---|
| 114 | ***************************************************************************************/ |
|---|
| 115 | error_t hal_fpu_context_alloc( struct thread_s * thread ); |
|---|
| 116 | |
|---|
| 117 | /**************************************************************************************** |
|---|
| 118 | * This function initializes a FPU context from scratch. |
|---|
| 119 | **************************************************************************************** |
|---|
| 120 | * @ thread : pointer on the thread descriptor. |
|---|
| 121 | ***************************************************************************************/ |
|---|
| 122 | void hal_fpu_context_init( struct thread_s * thread ); |
|---|
| 123 | |
|---|
| 124 | /**************************************************************************************** |
|---|
| 125 | * This function copies a FPU context defined by the <src> argument to the FPU context |
|---|
| 126 | * defined by the <dst> argument. It is used by the fork system call. |
|---|
| 127 | **************************************************************************************** |
|---|
| 128 | * @ dst : pointer on the destination thread descriptor. |
|---|
| 129 | * @ src : pointer on the source thread descriptor. |
|---|
| 130 | ***************************************************************************************/ |
|---|
| 131 | void hal_fpu_context_copy( struct thread_s * dst, |
|---|
| 132 | struct thread_s * src ); |
|---|
| 133 | |
|---|
| 134 | /**************************************************************************************** |
|---|
| 135 | * This function releases the physical memory allocated for a FPU context. |
|---|
| 136 | **************************************************************************************** |
|---|
| 137 | * @ thread : pointer on the thread descriptor. |
|---|
| 138 | ***************************************************************************************/ |
|---|
| 139 | void hal_fpu_context_destroy( struct thread_s * thread ); |
|---|
| 140 | |
|---|
| 141 | /**************************************************************************************** |
|---|
| 142 | * This function is used to implement the fork() system call. |
|---|
| 143 | * It saves in a remote thread FPU context the current FPU registers values. |
|---|
| 144 | **************************************************************************************** |
|---|
| 145 | * @ thread_xp : extended pointer on the remote thread descriptor. |
|---|
| 146 | ***************************************************************************************/ |
|---|
| 147 | void hal_fpu_context_save( xptr_t thread_xp ); |
|---|
| 148 | |
|---|
| 149 | /**************************************************************************************** |
|---|
| 150 | * This function restores from the calling thread FPU context the FPU registers values. |
|---|
| 151 | **************************************************************************************** |
|---|
| 152 | * @ thread : pointer on the thread descriptor. |
|---|
| 153 | ***************************************************************************************/ |
|---|
| 154 | void hal_fpu_context_restore( struct thread_s * thread ); |
|---|
| 155 | |
|---|
| 156 | #endif /* _HAL_CONTEXT_H_ */ |
|---|