[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 | **************************************************************************************** |
---|
[457] | 45 | * @ thread : pointer on the thread descriptor. |
---|
[407] | 46 | * @ return 0 if success / return -1 if failure. |
---|
| 47 | ***************************************************************************************/ |
---|
| 48 | error_t hal_cpu_context_alloc( struct thread_s * thread ); |
---|
| 49 | |
---|
| 50 | /**************************************************************************************** |
---|
[457] | 51 | * This function initializes a CPU context from scratch. |
---|
[407] | 52 | **************************************************************************************** |
---|
[1] | 53 | * @ thread : pointer on the thread descriptor. |
---|
| 54 | ***************************************************************************************/ |
---|
[457] | 55 | void hal_cpu_context_init( struct thread_s * thread ); |
---|
[1] | 56 | |
---|
| 57 | /**************************************************************************************** |
---|
[408] | 58 | * This function is used to implement the fork() system call. |
---|
| 59 | * 1) It saves in a remote (child) thread CPU context the current CPU registers values. |
---|
| 60 | * Three slots are not simple copies of the parent registers values : |
---|
| 61 | * - the thread pointer is set to the child thread local pointer. |
---|
| 62 | * - the stack pointer is set to parrent SP + (child_base - parent_base). |
---|
| 63 | * - the status register is set to kernel mode with IRQ disabled. |
---|
| 64 | * 2) It copies the content of the calling (parent) thread kernel_stack, |
---|
| 65 | * to the remote (child) thread kernel_stack. |
---|
[1] | 66 | **************************************************************************************** |
---|
[408] | 67 | * @ thread_xp : extended pointer on the remote thread descriptor. |
---|
[1] | 68 | ***************************************************************************************/ |
---|
[408] | 69 | void hal_cpu_context_fork( xptr_t thread_xp ); |
---|
[1] | 70 | |
---|
| 71 | /**************************************************************************************** |
---|
[457] | 72 | * This function is used to implement the exec() system call. |
---|
| 73 | * 1) It initialize the relevant slots of the the calling thread CPU context. |
---|
| 74 | * 2) It call the hal_do_cpu_restore() function to return to user mode and start |
---|
| 75 | * execution of the new process. |
---|
| 76 | **************************************************************************************** |
---|
| 77 | * @ thread : pointer on the thread descriptor. |
---|
| 78 | ***************************************************************************************/ |
---|
| 79 | void hal_cpu_context_exec( struct thread_s * thread ); |
---|
| 80 | |
---|
| 81 | /**************************************************************************************** |
---|
[408] | 82 | * This function display some slots of the CPU context. |
---|
| 83 | **************************************************************************************** |
---|
| 84 | * @ thread_xp : extended pointer on the thread descriptor. |
---|
| 85 | ***************************************************************************************/ |
---|
| 86 | void hal_cpu_context_display( xptr_t thread_xp ); |
---|
| 87 | |
---|
| 88 | /**************************************************************************************** |
---|
[1] | 89 | * This function releases the physical memory allocated for a thread CPU context. |
---|
| 90 | **************************************************************************************** |
---|
| 91 | * @ thread : pointer on the thread descriptor. |
---|
| 92 | ***************************************************************************************/ |
---|
| 93 | void hal_cpu_context_destroy( struct thread_s * thread ); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
[407] | 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
[1] | 101 | /**************************************************************************************** |
---|
[407] | 102 | * This function allocates memory for a FPU context, reset all entries, |
---|
| 103 | * and links it to the thread identified by the <thread> argument. |
---|
[1] | 104 | **************************************************************************************** |
---|
| 105 | * @ thread : pointer on the thread descriptor. |
---|
[407] | 106 | * @ return 0 if success / return -1 if failure. |
---|
[1] | 107 | ***************************************************************************************/ |
---|
[407] | 108 | error_t hal_fpu_context_alloc( struct thread_s * thread ); |
---|
[1] | 109 | |
---|
| 110 | /**************************************************************************************** |
---|
[457] | 111 | * This function initializes a FPU context from scratch. |
---|
| 112 | **************************************************************************************** |
---|
| 113 | * @ thread : pointer on the thread descriptor. |
---|
| 114 | ***************************************************************************************/ |
---|
| 115 | void hal_fpu_context_init( struct thread_s * thread ); |
---|
| 116 | |
---|
| 117 | /**************************************************************************************** |
---|
[407] | 118 | * This function copies a FPU context defined by the <src> argument to the FPU context |
---|
| 119 | * defined by the <dst> argument. It is used by the fork system call. |
---|
[1] | 120 | **************************************************************************************** |
---|
| 121 | * @ dst : pointer on the destination thread descriptor. |
---|
| 122 | * @ src : pointer on the source thread descriptor. |
---|
| 123 | ***************************************************************************************/ |
---|
[407] | 124 | void hal_fpu_context_copy( struct thread_s * dst, |
---|
[1] | 125 | struct thread_s * src ); |
---|
| 126 | |
---|
| 127 | /**************************************************************************************** |
---|
| 128 | * This function releases the physical memory allocated for a FPU context. |
---|
| 129 | **************************************************************************************** |
---|
| 130 | * @ thread : pointer on the thread descriptor. |
---|
| 131 | ***************************************************************************************/ |
---|
| 132 | void hal_fpu_context_destroy( struct thread_s * thread ); |
---|
| 133 | |
---|
| 134 | /**************************************************************************************** |
---|
[408] | 135 | * This function is used to implement the fork() system call. |
---|
| 136 | * It saves in a remote thread FPU context the current FPU registers values. |
---|
[1] | 137 | **************************************************************************************** |
---|
[408] | 138 | * @ thread_xp : extended pointer on the remote thread descriptor. |
---|
[1] | 139 | ***************************************************************************************/ |
---|
[408] | 140 | void hal_fpu_context_save( xptr_t thread_xp ); |
---|
[1] | 141 | |
---|
| 142 | /**************************************************************************************** |
---|
[408] | 143 | * This function restores from the calling thread FPU context the FPU registers values. |
---|
[1] | 144 | **************************************************************************************** |
---|
| 145 | * @ thread : pointer on the thread descriptor. |
---|
| 146 | ***************************************************************************************/ |
---|
| 147 | void hal_fpu_context_restore( struct thread_s * thread ); |
---|
| 148 | |
---|
| 149 | #endif /* _HAL_CONTEXT_H_ */ |
---|