comparison src/gpf2/osl/os_pro_fl.c @ 487:91e8dac34ada

src/gpf2/osl: initial import from old freecalypso-sw tree
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 22 Jun 2018 05:56:16 +0000
parents
children bc12e6de8a07
comparison
equal deleted inserted replaced
486:c433cca731a3 487:91e8dac34ada
1 /*
2 * This C module is a reconstruction based on the disassembly of
3 * os_pro.obj in frame_na7_db_fl.lib from the Leonardo package.
4 */
5
6 /* set of included headers from COFF symtab: */
7 #include <stdio.h>
8 #include <string.h>
9 #include "gpfconf.h" /* FreeCalypso addition */
10 #include "../../nucleus/nucleus.h"
11 #include "typedefs.h"
12 #include "os.h"
13 #include "gdi.h"
14 #include "os_types.h"
15 #include "os_glob.h"
16
17 typedef unsigned char u_char;
18
19 extern VOID *TCD_Current_Thread;
20 extern T_OS_TASK_TABLE_ENTRY TaskTable[];
21 extern VOID os_TaskEntry(UNSIGNED, VOID *);
22
23 static NU_SEMAPHORE ProSemCB;
24
25 #define OS_NU_TASK_MAGIC 0xdeafbeef
26
27 static int
28 os_GetTaskEntry(USHORT Index, OS_HANDLE *Handle)
29 {
30 static USHORT Idx;
31
32 if (Index == FIRST_ENTRY)
33 Idx = 0;
34 if (Index == FIRST_ENTRY || Index == NEXT_ENTRY) {
35 while (++Idx <= MaxTasks && !TaskTable[Idx].Name[0])
36 ;
37 } else
38 Idx = Index;
39 if (Idx <= MaxTasks && TaskTable[Idx].Name[0]) {
40 *Handle = Idx;
41 return(0);
42 } else
43 return(-1);
44 }
45
46 GLOBAL LONG
47 os_TaskInformation(USHORT Index, char *Buffer)
48 {
49 DATA_ELEMENT TaskStatus;
50 OPTION Prio, Preempt;
51 UNSIGNED Count, TimeSlice, Size, MinStack;
52 OS_HANDLE Handle;
53 CHAR Name[NU_MAX_NAME];
54 u_char *StackBase, *sp;
55 USHORT Untouched;
56
57 if (os_GetTaskEntry(Index, &Handle) < 0)
58 return(OS_ERROR);
59 if (NU_Task_Information(&TaskTable[Handle].TaskCB.TCB, Name,
60 &TaskStatus, &Count, &Prio, &Preempt,
61 &TimeSlice, (VOID **) &StackBase,
62 &Size, &MinStack) != NU_SUCCESS)
63 return(OS_ERROR);
64 Untouched = 0;
65 for (sp = StackBase; sp < StackBase + Size; sp++) {
66 if (*sp != INITIAL_STACK_VALUE)
67 break;
68 Untouched++;
69 }
70 sprintf(Buffer,
71 "Name:%s Stat:%d Count:%ld Prio:%d Stack:%lx Size:%ld Untouched:%d",
72 Name, TaskStatus, Count, 255 - Prio, (ULONG) StackBase,
73 (LONG) Size, Untouched);
74 return(OS_OK);
75 }
76
77 GLOBAL LONG
78 os_StopTask(OS_HANDLE Caller, OS_HANDLE TaskHandle)
79 {
80 if (NU_Suspend_Task(&TaskTable[TaskHandle].TaskCB.TCB) == NU_SUCCESS)
81 return(OS_OK);
82 else
83 return(OS_ERROR);
84 }
85
86 GLOBAL LONG
87 os_StartTask(OS_HANDLE Caller, OS_HANDLE TaskHandle, ULONG Value)
88 {
89 if (NU_Resume_Task(&TaskTable[TaskHandle].TaskCB.TCB) == NU_SUCCESS)
90 return(OS_OK);
91 else
92 return(OS_ERROR);
93 }
94
95 GLOBAL LONG
96 os_ProInit(void)
97 {
98 USHORT i;
99
100 if (NU_Create_Semaphore(&ProSemCB, "PROSEM", 1, NU_PRIORITY)
101 != NU_SUCCESS)
102 return(OS_ERROR);
103 for (i = 1; i <= MaxTasks; i++)
104 bzero(&TaskTable[i], sizeof(T_OS_TASK_TABLE_ENTRY));
105 return(OS_OK);
106 }
107
108 GLOBAL unsigned char
109 os_GetTaskState(OS_HANDLE Caller, OS_HANDLE Handle)
110 {
111 if (TaskTable[Handle].Name[0])
112 return(TaskTable[Handle].TaskCB.TCB.tc_status);
113 else
114 return(255);
115 }
116
117 GLOBAL LONG
118 os_GetTaskHandle(OS_HANDLE Caller, char *Name, OS_HANDLE *TaskHandle)
119 {
120 USHORT i;
121
122 if (!Name) {
123 OS_NU_TASK *os_nu_task = (OS_NU_TASK *) TCD_Current_Thread;
124
125 if (os_nu_task && os_nu_task->magic_nr == OS_NU_TASK_MAGIC)
126 *TaskHandle = os_nu_task->handle;
127 else
128 *TaskHandle = OS_NOTASK;
129 return(OS_OK);
130 }
131 for (i = 1; i <= MaxTasks; i++)
132 if (TaskTable[i].Name[0] &&
133 !strncmp(Name, TaskTable[i].Name, RESOURCE_NAMELEN - 1)) {
134 *TaskHandle = i;
135 return(OS_OK);
136 }
137 return(OS_ERROR);
138 }
139
140 GLOBAL LONG
141 os_GetTaskData(OS_HANDLE Handle, unsigned **tcb,
142 u_char **stackbegin, u_char **stackend)
143 {
144 NU_TASK *task;
145
146 if (!TaskTable[Handle].Name[0])
147 return(OS_ERROR);
148 task = &TaskTable[Handle].TaskCB.TCB;
149 *tcb = (unsigned *) task;
150 *stackbegin = (u_char *) task->tc_stack_start;
151 *stackend = (u_char *) task->tc_stack_end;
152 return(OS_OK);
153 }
154
155 GLOBAL LONG
156 os_GetScheduleCount(OS_HANDLE task_handle, int *schedule_count)
157 {
158 NU_TASK *task_cb;
159
160 if (task_handle > MaxTasks)
161 return(OS_ERROR);
162 if (!TaskTable[task_handle].Name[0])
163 return(OS_ERROR);
164 if (task_handle == OS_NOTASK)
165 task_cb = (NU_TASK *) TCD_Current_Thread;
166 else
167 task_cb = &TaskTable[task_handle].TaskCB.TCB;
168 *schedule_count = task_cb->tc_scheduled;
169 return(OS_OK);
170 }
171
172 GLOBAL LONG
173 os_DestroyTask(OS_HANDLE Caller, OS_HANDLE TaskHandle)
174 {
175 STATUS sts;
176
177 if (NU_Terminate_Task(&TaskTable[TaskHandle].TaskCB.TCB) != NU_SUCCESS)
178 return(OS_ERROR);
179 if (NU_Delete_Task(&TaskTable[TaskHandle].TaskCB.TCB) != NU_SUCCESS)
180 return(OS_ERROR);
181 if (os_DeallocateMemory(Caller, TaskTable[TaskHandle].Stack) != OS_OK)
182 return(OS_ERROR);
183 sts = NU_Obtain_Semaphore(&ProSemCB, NU_SUSPEND);
184 TaskTable[TaskHandle].Name[0] = 0;
185 if (sts == NU_SUCCESS)
186 NU_Release_Semaphore(&ProSemCB);
187 return(OS_OK);
188 }
189
190 GLOBAL LONG
191 os_CreateTask(OS_HANDLE Caller, char *Name,
192 void (*TaskEntry)(OS_HANDLE, ULONG), ULONG StackSize,
193 USHORT Priority, OS_HANDLE *TaskHandle, OS_HANDLE MemPoolHandle)
194 {
195 STATUS sem_sts;
196 USHORT Handle;
197 u_char *stack;
198
199 sem_sts = NU_Obtain_Semaphore(&ProSemCB, NU_SUSPEND);
200 for (Handle = 1; Handle <= MaxTasks; Handle++)
201 if (!TaskTable[Handle].Name[0])
202 break;
203 if (Handle > MaxTasks) {
204 return_error: if (sem_sts == NU_SUCCESS)
205 NU_Release_Semaphore(&ProSemCB);
206 return(OS_ERROR);
207 }
208 if (os_AllocateMemory(Handle, &TaskTable[Handle].Stack, StackSize + 4,
209 0, MemPoolHandle) != OS_OK)
210 goto return_error;
211 stack = (u_char *)TaskTable[Handle].Stack + 4;
212 memset(stack, INITIAL_STACK_VALUE, StackSize);
213 *TaskTable[Handle].Stack = GUARD_PATTERN;
214 if (NU_Create_Task(&TaskTable[Handle].TaskCB.TCB, Name, os_TaskEntry,
215 Handle, 0, stack, StackSize,
216 255 - Priority, 0, NU_PREEMPT, NU_NO_START)
217 != NU_SUCCESS)
218 goto return_error;
219 strncpy(TaskTable[Handle].Name, Name, RESOURCE_NAMELEN);
220 TaskTable[Handle].Name[RESOURCE_NAMELEN-1] = 0;
221 TaskTable[Handle].TaskEntry = TaskEntry;
222 TaskTable[Handle].TaskCB.magic_nr = OS_NU_TASK_MAGIC;
223 TaskTable[Handle].TaskCB.handle = Handle;
224 *TaskHandle = Handle;
225 if (sem_sts == NU_SUCCESS)
226 NU_Release_Semaphore(&ProSemCB);
227 return(OS_OK);
228 }
229
230 GLOBAL LONG
231 os_ChangePreemption(char preempt)
232 {
233 if (NU_Change_Preemption(preempt) == NU_SUCCESS)
234 return(OS_OK);
235 else
236 return(OS_ERROR);
237 }