1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 #ifndef PORTMACRO_H
30 #define PORTMACRO_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /******************************************************************************
37 *   Defines
38 ******************************************************************************/
39 /* Type definitions. */
40 #define portCHAR                 char
41 #define portFLOAT                float
42 #define portDOUBLE               double
43 #define portLONG                 long
44 #define portSHORT                short
45 #define portSTACK_TYPE           size_t
46 #define portPOINTER_SIZE_TYPE    size_t
47 
48 typedef portSTACK_TYPE           StackType_t;
49 
50 #if defined( __x86_64__ ) || defined( _M_X64 )
51     #define portBASE_TYPE    long long
52     typedef long long            BaseType_t;
53     typedef unsigned long long   UBaseType_t;
54 #else
55     #define portBASE_TYPE    long
56     typedef long                 BaseType_t;
57     typedef unsigned long        UBaseType_t;
58 #endif
59 
60 
61 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
62     typedef uint16_t             TickType_t;
63     #define portMAX_DELAY              ( TickType_t ) 0xffff
64 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
65     typedef uint32_t             TickType_t;
66     #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL
67 
68 /* 32-bit tick type on a 32/64-bit architecture, so reads of the tick
69  * count do not need to be guarded with a critical section. */
70     #define portTICK_TYPE_IS_ATOMIC    1
71 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
72     typedef uint64_t             TickType_t;
73     #define portMAX_DELAY              ( TickType_t ) 0xffffffffffffffffULL
74 
75 #if defined( __x86_64__ ) || defined( _M_X64 )
76 /* 64-bit tick type on a 64-bit architecture, so reads of the tick
77  * count do not need to be guarded with a critical section. */
78     #define portTICK_TYPE_IS_ATOMIC    1
79 #endif
80 #else
81     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
82 #endif
83 
84 /* Hardware specifics. */
85 #define portSTACK_GROWTH          ( -1 )
86 #define portTICK_PERIOD_MS        ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
87 #define portINLINE                __inline
88 
89 #if defined( __x86_64__ ) || defined( _M_X64 )
90     #define portBYTE_ALIGNMENT    8
91 #else
92     #define portBYTE_ALIGNMENT    4
93 #endif
94 
95 #define portYIELD()    vPortGenerateSimulatedInterrupt( portINTERRUPT_YIELD )
96 
97 
98 extern volatile BaseType_t xInsideInterrupt;
99 #define portSOFTWARE_BARRIER()    while( xInsideInterrupt != pdFALSE )
100 
101 
102 /* Simulated interrupts return pdFALSE if no context switch should be performed,
103  * or a non-zero number if a context switch should be performed. */
104 #define portYIELD_FROM_ISR( x )       return x
105 #define portEND_SWITCHING_ISR( x )    portYIELD_FROM_ISR( ( x ) )
106 
107 void vPortCloseRunningThread( void * pvTaskToDelete,
108                               volatile BaseType_t * pxPendYield );
109 void vPortDeleteThread( void * pvThreadToDelete );
110 #define portCLEAN_UP_TCB( pxTCB )                                  vPortDeleteThread( pxTCB )
111 #define portPRE_TASK_DELETE_HOOK( pvTaskToDelete, pxPendYield )    vPortCloseRunningThread( ( pvTaskToDelete ), ( pxPendYield ) )
112 #define portDISABLE_INTERRUPTS()                                   vPortEnterCritical()
113 #define portENABLE_INTERRUPTS()                                    vPortExitCritical()
114 
115 /* Critical section handling. */
116 void vPortEnterCritical( void );
117 void vPortExitCritical( void );
118 
119 #define portENTER_CRITICAL()    vPortEnterCritical()
120 #define portEXIT_CRITICAL()     vPortExitCritical()
121 
122 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
123     #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1
124 #endif
125 
126 /*-----------------------------------------------------------*/
127 
128 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
129 
130     /* Check the configuration. */
131     #if ( configMAX_PRIORITIES > 32 )
132         #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32.  It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
133     #endif
134 
135     /* Store/clear the ready priorities in a bit map. */
136     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )    ( uxReadyPriorities ) |= ( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )
137     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )     ( uxReadyPriorities ) &= ~( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )
138 
139     #ifdef __GNUC__
140 
141         #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    \
142         __asm volatile ( "bsr %1, %0\n\t"                                       \
143                          : "=r" ( uxTopPriority )                               \
144                          : "rm" ( uxReadyPriorities )                           \
145                          : "cc" )
146 
147     #else /* __GNUC__ */
148         #include <intrin.h>
149 
150         /* BitScanReverse returns the bit position of the most significant '1'
151          * in the word. */
152         #if defined( __x86_64__ ) || defined( _M_X64 )
153             #pragma intrinsic(_BitScanReverse64)
154 
155             #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    \
156             do                                                                      \
157             {                                                                       \
158                 unsigned long ulTopPriority;                                        \
159                 _BitScanReverse64( &ulTopPriority, ( uxReadyPriorities ) );         \
160                 uxTopPriority = ulTopPriority;                                      \
161             } while( 0 )
162 
163         #else /* #if defined( __x86_64__ ) || defined( _M_X64 ) */
164             #pragma intrinsic(_BitScanReverse)
165 
166             #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    _BitScanReverse( ( unsigned long * ) &( uxTopPriority ), ( uxReadyPriorities ) )
167 
168         #endif /* #if defined( __x86_64__ ) || defined( _M_X64 ) */
169 
170     #endif /* __GNUC__ */
171 
172 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
173 
174 #ifndef __GNUC__
175     __pragma( warning( disable:4211 ) ) /* Nonstandard extension used, as extern is only nonstandard to MSVC. */
176 #endif
177 
178 
179 /* Task function macros as described on the FreeRTOS.org WEB site. */
180 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
181 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
182 
183 #define portINTERRUPT_YIELD                        ( 0UL )
184 #define portINTERRUPT_TICK                         ( 1UL )
185 #define portINTERRUPT_APPLICATION_DEFINED_START    ( 2UL )
186 
187 /*
188  * Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
189  * Each bit can be used to represent an individual interrupt - with the first
190  * two bits being used for the Yield and Tick interrupts respectively.
191  */
192 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber );
193 
194 /*
195  * Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
196  * Each bit can be used to represent an individual interrupt - with the first
197  * two bits being used for the Yield and Tick interrupts respectively. This function
198  * can be called in a windows thread.
199  */
200 void vPortGenerateSimulatedInterruptFromWindowsThread( uint32_t ulInterruptNumber );
201 
202 /*
203  * Install an interrupt handler to be called by the simulated interrupt handler
204  * thread.  The interrupt number must be above any used by the kernel itself
205  * (at the time of writing the kernel was using interrupt numbers 0, 1, and 2
206  * as defined above).  The number must also be lower than 32.
207  *
208  * Interrupt handler functions must return a non-zero value if executing the
209  * handler resulted in a task switch being required.
210  */
211 void vPortSetInterruptHandler( uint32_t ulInterruptNumber,
212                                uint32_t ( * pvHandler )( void ) );
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif /* ifndef PORTMACRO_H */
219