]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/resource.c
968ad9221e2b12c8a65a9e206e2c7216b9baead5
[arc.git] / system / kernel / resource.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 #include "Os.h"\r
17 #include "internal.h"\r
18 #include "task_i.h"\r
19 #include "resource_i.h"\r
20 #include "sys.h"\r
21 #include <assert.h>\r
22 #include <string.h>\r
23 \r
24 \r
25 #if !defined(MAX)\r
26 #define MAX(_x,_y) (((_x) > (_y)) ? (_x) : (_y))\r
27 #endif\r
28 \r
29 \r
30 /*\r
31 Resource management at interrupt level is NOT supported\r
32 \r
33 \r
34 Testing\r
35 RM:\r
36 1. Priority ceiling: Call GetResource() from preemtive\r
37    task and activate a task with higher priority than the ceiling protocol.\r
38    The higher priority task should be swapped in.\r
39 2. Verify that you cannot allocate an internal resource with\r
40    a) GetResource()\r
41    b) ReleaseResource()\r
42 3. Internal resource. Allocate 1 internal resource to 3 tasks of different\r
43    priorities. Verify that\r
44    a) Higher priority tasks than the group can preement\r
45    b) For tasks which have the same or lower priority as the highest priority within a group,\r
46       the tasks within the group behave like non preemptable tasks ( OSEK 4.6.3)\r
47 4. Attempt to release a resource which has a lower ceiling priority\r
48    than the statically assigned priority of the calling task or\r
49    interrupt routine, E_OS_ACCESS\r
50 5. The  general  restriction  on  some  system  calls  that  they  are  not  to  be  called  with  resources\r
51   occupied (chapter 8.2) does not apply to internal resources, as internal resources are handled\r
52   within  those  calls.  However,  all  standard  resources  have  to  be  released  before  the  internal\r
53   resource can be released (see chapter 8.2, \93LIFO principle\94).\r
54 6. Check LIFO order. Return E_OS_ACCESS if not in LIFO order..\r
55 7. Test Os_IsrAddResource().\r
56 \r
57 \r
58 task\r
59 - GetResource(RES_SCHEDULER) will lock the scheduler even for ISR2\r
60 \r
61 TODO:\r
62 1. task.resourceAccess is already calculated by BSW builder. This is the bitmask\r
63    of what resources is accessable by the task.\r
64 2.\r
65 \r
66   task.rsrcAccessMask & (1 << RES_SCHEDULER)\r
67 \r
68  *\r
69  */\r
70 \r
71 /* INFO\r
72  * - If OsTaskSchedule = NON, Task it not preemptable, no internal resource may be assigned to a task\r
73  *                       (cause it already have one of prio 32)\r
74  *                       FULL, Task is preemptable\r
75  * - On Schedule() .... This service has no influence on tasks with no internal resource\r
76  *                      assigned (preemptable tasks).\r
77  *\r
78  * OSEK on internal resources:\r
79  * - Non preemptable tasks are a special group with an internal resource of the\r
80  *   same priority as RES_SCHEDULER assigned\r
81  *\r
82  *\r
83  * Assign RES_SCHEDULER with prio 32.\r
84  * Assign internal resources to NON preemptable task.\r
85  *\r
86  * So that leaves us with:\r
87  * - NON\r
88  *   - Cannot assign internal resource.\r
89  *     It automatically gets internal resource with same prio as RES_SCHEDULER\r
90  *\r
91  * - FULL\r
92  *   - Assigned. Used for grouping tasks.\r
93  *   - No assigned.\r
94  *\r
95  * What does that mean?\r
96  * - It's probably OK to do a GetResource(RES_SCHEDULER) from a NON task (although pointless)\r
97  * - GetResource(<any>) from a NON task is wrong\r
98  *\r
99  * Generation/Implementation:\r
100  * - Resources to 32. Alloc with .resourceAlloc = ((1<<RES_1) |(1<<RES_2));\r
101  * - Keep allocated resources as stack to comply with LIFO order.\r
102  * - A linked resource is just another name for an existing resource. See OsResource in Autosar SWS OS.\r
103  *   This means that no resource object should be generated, just the define in Os_Cfg.h\r
104  * - A task with Scheduling=NON have priority (although it's internal priority is 32)\r
105  *\r
106  */\r
107 \r
108 #define valid_standard_id() (rPtr->nr < OS_RESOURCE_CNT) //&& !(rPtr->type == RESOURCE_TYPE_INTERNAL) )\r
109 #define valid_internal_id() (rPtr->nr < OS_RESOURCE_CNT) //&& (rPtr->type == RESOURCE_TYPE_INTERNAL) )\r
110 \r
111 \r
112 \r
113 /**\r
114  * This call serves to enter critical sections in the code that are\r
115  * assigned to the resource referenced by <ResID>. A critical\r
116  * section shall always be left using ReleaseResource.\r
117  *\r
118  * The OSEK priority ceiling protocol for resource management is described\r
119  * in chapter 8.5. Nested resource occupation is only allowed if the inner\r
120  * critical sections are completely executed within the surrounding critical\r
121  * section (strictly stacked, see chapter 8.2, Restrictions when using\r
122  * resources). Nested occupation of one and the same resource is also\r
123  * forbidden! It is recommended that corresponding calls to GetResource and\r
124  * ReleaseResource appear within the same function.\r
125  *\r
126  * It is not allowed to use services which are points of rescheduling for\r
127  * non preemptable tasks  (TerminateTask,ChainTask,  Schedule  and  WaitEvent,\r
128  * see  chapter  4.6.2)  in critical  sections.\r
129  * Additionally,  critical  sections  are  to  be  left before completion of\r
130  * an interrupt service routine.\r
131  * Generally speaking, critical sections should be short.\r
132  * The service may be called from an ISR and from task level (see Figure 12-1).\r
133  *\r
134  * @param ResID\r
135  * @return\r
136  */\r
137 \r
138 \r
139 StatusType GetResource( ResourceType ResID ) {\r
140         StatusType rv = E_OK;\r
141         OsResourceType *rPtr;\r
142         uint32_t flags;\r
143 \r
144         Irq_Save(flags);\r
145 \r
146 \r
147         if( Os_Sys.intNestCnt != 0 ) {\r
148 \r
149                 /* For interrupts to the scheduler resource seems just dumb to get */\r
150                 OsIsrVarType *isrPtr = Os_SysIsrGetCurr();\r
151 \r
152                 /* Check we can access it */\r
153                 if( ((isrPtr->constPtr->resourceMask & (1<< ResID)) == 0) ||\r
154                         ( ResID == RES_SCHEDULER )      ) {\r
155                         rv = E_OS_ID;\r
156                         goto err;\r
157                 }\r
158 \r
159                 rPtr = Os_ResourceGet(ResID);\r
160 \r
161                 /* ceiling prio for ISR seems strange...so no */\r
162                 if( rPtr->owner != NO_TASK_OWNER ) {\r
163                         rv = E_OS_ACCESS;\r
164                         Irq_Restore(flags);\r
165                         goto err;\r
166                 }\r
167                 /* Add the resource to the list of resources held by this isr */\r
168                 Os_IsrResourceAdd(rPtr,isrPtr);\r
169 \r
170         } else {\r
171                 OsTaskVarType *taskPtr = Os_SysTaskGetCurr();\r
172 \r
173                 if( ResID == RES_SCHEDULER ) {\r
174                         rPtr = &Os_Sys.resScheduler;\r
175                 } else {\r
176                         /* Check we can access it */\r
177                         if( (taskPtr->constPtr->resourceAccess & (1<< ResID)) == 0 ) {\r
178                                 rv = E_OS_ID;\r
179                                 goto err;\r
180                         }\r
181 \r
182                         rPtr = Os_ResourceGet(ResID);\r
183                 }\r
184 \r
185                 /* Check for invalid configuration */\r
186                 if( (rPtr->owner != NO_TASK_OWNER) ||\r
187                         (taskPtr->activePriority > rPtr->ceiling_priority) )\r
188                 {\r
189                         rv = E_OS_ACCESS;\r
190                         Irq_Restore(flags);\r
191                         goto err;\r
192                 }\r
193                 /* Add the resource to the list of resources held by this task */\r
194                 Os_TaskResourceAdd(rPtr,taskPtr);\r
195         }\r
196 \r
197         Irq_Restore(flags);\r
198 \r
199         if (rv != E_OK)\r
200                 goto err;\r
201 \r
202         OS_STD_END_1(OSServiceId_GetResource,ResID);\r
203 }\r
204 \r
205 /**\r
206  * ReleaseResource   is   the   counterpart   of   GetResource   and\r
207  * serves to leave critical sections in the code that are assigned to\r
208  * the resource referenced by <ResID>.\r
209  *\r
210  * For  information  on  nesting  conditions,  see  particularities  of\r
211  * GetResource. The service may be called from an ISR and from task level (see\r
212  * Figure 12-1).\r
213  *\r
214  * @param ResID\r
215  * @return\r
216  */\r
217 \r
218 StatusType ReleaseResource( ResourceType ResID) {\r
219         StatusType rv = E_OK;\r
220         OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();\r
221         OsResourceType *rPtr;\r
222         uint32_t flags;\r
223 \r
224         Irq_Save(flags);\r
225         if( ResID == RES_SCHEDULER ) {\r
226                 rPtr = &Os_Sys.resScheduler;\r
227         } else {\r
228                 /* Check we can access it */\r
229                 if( (pcbPtr->constPtr->resourceAccess & (1<< ResID)) == 0 ) {\r
230                         rv = E_OS_ID;\r
231                         goto err;\r
232                 }\r
233                 rPtr = Os_ResourceGet(ResID);\r
234         }\r
235 \r
236         /* Check for invalid configuration */\r
237         if( rPtr->owner == NO_TASK_OWNER)\r
238         {\r
239                 rv = E_OS_NOFUNC;\r
240                 Irq_Restore(flags);\r
241                 goto err;\r
242         }\r
243 \r
244         if( (pcbPtr->activePriority < rPtr->ceiling_priority))\r
245         {\r
246                 rv = E_OS_ACCESS;\r
247                 Irq_Restore(flags);\r
248                 goto err;\r
249         }\r
250 \r
251         Os_TaskResourceRemove(rPtr,pcbPtr);\r
252 \r
253         /* do a rescheduling (in some cases) (see OSEK OS 4.6.1) */\r
254         if ( (pcbPtr->constPtr->scheduling == FULL) &&\r
255                  (Os_Sys.intNestCnt == 0) &&\r
256                  (Os_SchedulerResourceIsFree()) ) {\r
257 \r
258                 OsTaskVarType* top_pcb = Os_TaskGetTop();\r
259 \r
260                 /* only dispatch if some other ready task has higher prio */\r
261                 if (top_pcb->activePriority > Os_SysTaskGetCurr()->activePriority) {\r
262                         Os_Dispatch(OP_RELEASE_RESOURCE);\r
263                 }\r
264         }\r
265         Irq_Restore(flags);\r
266 \r
267         OS_STD_END_1(OSServiceId_ReleaseResource,ResID);\r
268 }\r
269 \r
270 \r
271 void Os_ResourceGetInternal( void ) {\r
272         OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();\r
273         OsResourceType *rt = pcbPtr->constPtr->resourceIntPtr;\r
274 \r
275         if( rt != NULL ) {\r
276                 OS_DEBUG(D_RESOURCE,"Get IR proc:%s prio:%u old_task_prio:%u\n",\r
277                                 Os_SysTaskGetCurr()->name,\r
278                                 (unsigned)rt->ceiling_priority,\r
279                                 (unsigned)rt->old_task_prio);\r
280                 Os_TaskResourceAdd(rt,pcbPtr);\r
281         }\r
282 }\r
283 \r
284 void Os_ResourceReleaseInternal( void ) {\r
285         OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();\r
286         OsResourceType *rt = pcbPtr->constPtr->resourceIntPtr;\r
287 \r
288         if(  rt != NULL ) {\r
289                 OS_DEBUG(D_RESOURCE,"Rel IR proc:%s prio:%u old_task_prio:%u\n",\r
290                                 Os_SysTaskGetCurr()->name,\r
291                                 (unsigned)rt->ceiling_priority,\r
292                                 (unsigned)rt->old_task_prio);\r
293                 Os_TaskResourceRemove(rt,pcbPtr);\r
294         }\r
295 }\r
296 \r
297 \r
298 \r
299 /**\r
300  *\r
301  * @param pcb_p\r
302  * @return\r
303  */\r
304 void Os_ResourceInit( void ) {\r
305         //TAILQ_INIT(&pcb_p->resourceHead);\r
306         OsTaskVarType *pcb_p;\r
307         OsResourceType *rsrc_p;\r
308         int topPrio;\r
309 \r
310 \r
311         /* For now, assign the scheduler resource here */\r
312         Os_Sys.resScheduler.ceiling_priority = OS_RES_SCHEDULER_PRIO;\r
313         strcpy(Os_Sys.resScheduler.id,"RES_SCHEDULER");\r
314         Os_Sys.resScheduler.nr = RES_SCHEDULER;\r
315         Os_Sys.resScheduler.owner = NO_TASK_OWNER;\r
316 \r
317         /* Calculate ceiling priority\r
318          * We make this as simple as possible. The ceiling priority\r
319          * is set to the same priority as the highest priority task that\r
320          * access it.\r
321          *\r
322          * Note that this applies both internal and standard resources.\r
323          * */\r
324         for( int i=0; i < OS_RESOURCE_CNT; i++) {\r
325                 rsrc_p = Os_ResourceGet(i);\r
326                 topPrio = 0;\r
327 \r
328                 for( int pi = 0; pi < OS_TASK_CNT; pi++) {\r
329 \r
330                         pcb_p = Os_TaskGet(pi);\r
331 \r
332 \r
333                         if(pcb_p->constPtr->resourceAccess & (1<<i) ) {\r
334                                 topPrio = MAX(topPrio,pcb_p->constPtr->prio);\r
335                         }\r
336 \r
337                         /* Generator fix, add RES_SCHEDULER */\r
338 //                      pcb_p->constPtr->resourceAccess |= (1 << RES_SCHEDULER) ;\r
339                 }\r
340                 rsrc_p->ceiling_priority = topPrio;\r
341         }\r
342 }\r
343 \r