| 1 | /* |
|---|
| 2 | * hal_context.h - Generic Thread Context Access API definition. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
|---|
| 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 from the <thread> descriptor, depending on the thread type (user / kernel). |
|---|
| 53 | * All slots required to start a new thread must be initialized in the thread context. |
|---|
| 54 | * Moreover, for an user thread, the arguments to be passed to the entry function |
|---|
| 55 | * depend on the <is_main> argument below: |
|---|
| 56 | * - a main thread, created by sys_exec(), requires two arguments, defined by the two |
|---|
| 57 | * <argc> and <argv> arguments below. |
|---|
| 58 | * define the arguments |
|---|
| 59 | * - a pthread, created by sys_thread_create() requires only one argument, defined by |
|---|
| 60 | * the "entry_args" field in the thread descriptor. |
|---|
| 61 | **************************************************************************************** |
|---|
| 62 | * @ thread : pointer on the thread descriptor. |
|---|
| 63 | * @ is_main : main thread when non zero. |
|---|
| 64 | * @ argc : actual number of arguments (for main). |
|---|
| 65 | * @ argv : user space pointer on array of pointers on strings arguments (for main). |
|---|
| 66 | ***************************************************************************************/ |
|---|
| 67 | void hal_cpu_context_init( struct thread_s * thread, |
|---|
| 68 | bool_t is_main, |
|---|
| 69 | uint32_t argc, |
|---|
| 70 | intptr_t argv ); |
|---|
| 71 | |
|---|
| 72 | /**************************************************************************************** |
|---|
| 73 | * This function is called the sys_fork() function to complete the fork mechanism. |
|---|
| 74 | * It is called by th local parent thread to initialize the CPU context of the remote |
|---|
| 75 | * child thread, identified by the <thread_xp> argument. |
|---|
| 76 | * It makes three actions: |
|---|
| 77 | * 1) It copies the current values of the CPU registers of the core running the parent |
|---|
| 78 | * thread to the remote child CPU context. |
|---|
| 79 | * 2) It patches four slots of this remote child CPU context: |
|---|
| 80 | * - the c0_th slot is set to the child thread descriptor pointer. |
|---|
| 81 | * - the sp_29 slot is set to the child kernel stack pointer. |
|---|
| 82 | * - the c0_sr slot is set to kernel mode with IRQ disabled. |
|---|
| 83 | * - the c2_ptpr slot is set to the child process GPT value. |
|---|
| 84 | * 3) It copies the content of the parent thread kernel_stack, to the child thread |
|---|
| 85 | * kernel_stack, because the COW mechanism is not available on architectures where |
|---|
| 86 | * the data MMU is de-activated in kernel mode. |
|---|
| 87 | **************************************************************************************** |
|---|
| 88 | * @ thread_xp : extended pointer on the child thread descriptor. |
|---|
| 89 | ***************************************************************************************/ |
|---|
| 90 | void hal_cpu_context_fork( xptr_t thread_xp ); |
|---|
| 91 | |
|---|
| 92 | /**************************************************************************************** |
|---|
| 93 | * This function display some slots of the CPU context. |
|---|
| 94 | **************************************************************************************** |
|---|
| 95 | * @ thread_xp : extended pointer on the thread descriptor. |
|---|
| 96 | ***************************************************************************************/ |
|---|
| 97 | void hal_cpu_context_display( xptr_t thread_xp ); |
|---|
| 98 | |
|---|
| 99 | /**************************************************************************************** |
|---|
| 100 | * This function releases the physical memory allocated for a thread CPU context. |
|---|
| 101 | **************************************************************************************** |
|---|
| 102 | * @ thread : pointer on the thread descriptor. |
|---|
| 103 | ***************************************************************************************/ |
|---|
| 104 | void hal_cpu_context_destroy( struct thread_s * thread ); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /**************************************************************************************** |
|---|
| 113 | * This function allocates memory for a FPU context, reset all entries, |
|---|
| 114 | * and links it to the thread identified by the <thread> argument. |
|---|
| 115 | **************************************************************************************** |
|---|
| 116 | * @ thread : pointer on the thread descriptor. |
|---|
| 117 | * @ return 0 if success / return -1 if failure. |
|---|
| 118 | ***************************************************************************************/ |
|---|
| 119 | error_t hal_fpu_context_alloc( struct thread_s * thread ); |
|---|
| 120 | |
|---|
| 121 | /**************************************************************************************** |
|---|
| 122 | * This function initializes a FPU context from scratch. |
|---|
| 123 | **************************************************************************************** |
|---|
| 124 | * @ thread : pointer on the thread descriptor. |
|---|
| 125 | ***************************************************************************************/ |
|---|
| 126 | void hal_fpu_context_init( struct thread_s * thread ); |
|---|
| 127 | |
|---|
| 128 | /**************************************************************************************** |
|---|
| 129 | * This function copies a FPU context defined by the <src> argument to the FPU context |
|---|
| 130 | * defined by the <dst> argument. It is used by the fork system call. |
|---|
| 131 | **************************************************************************************** |
|---|
| 132 | * @ dst : pointer on the destination thread descriptor. |
|---|
| 133 | * @ src : pointer on the source thread descriptor. |
|---|
| 134 | ***************************************************************************************/ |
|---|
| 135 | void hal_fpu_context_copy( struct thread_s * dst, |
|---|
| 136 | struct thread_s * src ); |
|---|
| 137 | |
|---|
| 138 | /**************************************************************************************** |
|---|
| 139 | * This function releases the physical memory allocated for a FPU context. |
|---|
| 140 | **************************************************************************************** |
|---|
| 141 | * @ thread : pointer on the thread descriptor. |
|---|
| 142 | ***************************************************************************************/ |
|---|
| 143 | void hal_fpu_context_destroy( struct thread_s * thread ); |
|---|
| 144 | |
|---|
| 145 | /**************************************************************************************** |
|---|
| 146 | * This function is used to implement the fork() system call. |
|---|
| 147 | * It saves in a remote thread FPU context the current FPU registers values. |
|---|
| 148 | **************************************************************************************** |
|---|
| 149 | * @ thread_xp : extended pointer on the remote thread descriptor. |
|---|
| 150 | ***************************************************************************************/ |
|---|
| 151 | void hal_fpu_context_save( xptr_t thread_xp ); |
|---|
| 152 | |
|---|
| 153 | /**************************************************************************************** |
|---|
| 154 | * This function restores from the calling thread FPU context the FPU registers values. |
|---|
| 155 | **************************************************************************************** |
|---|
| 156 | * @ thread : pointer on the thread descriptor. |
|---|
| 157 | ***************************************************************************************/ |
|---|
| 158 | void hal_fpu_context_restore( struct thread_s * thread ); |
|---|
| 159 | |
|---|
| 160 | #endif /* _HAL_CONTEXT_H_ */ |
|---|