1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/kernel/taskmgr.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* COPYRIGHT International Business Machines Corp. 2010,2014 */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#include <util/singleton.H>
#include <kernel/taskmgr.H>
#include <kernel/task.H>
#include <kernel/pagemgr.H>
#include <kernel/cpumgr.H>
#include <kernel/stacksegment.H>
#include <kernel/stacksegment.H>
#include <kernel/cpu.H>
#include <kernel/scheduler.H>
#include <sys/task.h>
#include <arch/ppc.H>
#include <string.h>
#include <limits.h>
#include <assert.h>
extern "C" void userspace_task_entry();
extern "C" void task_end_stub();
task_t* TaskManager::getCurrentTask()
{
register task_t* current_task = (task_t*) getSPRG3();
return current_task;
}
void TaskManager::setCurrentTask(task_t* t)
{
t->cpu = CpuManager::getCurrentCPU();
t->state = TASK_STATE_RUNNING;
setSPRG3((uint64_t)t);
return;
}
TaskManager::TaskManager() : iv_nextTid()
{
}
task_t* TaskManager::createIdleTask()
{
return Singleton<TaskManager>::instance()._createIdleTask();
}
task_t* TaskManager::createTask(TaskManager::task_fn_t t, void* p,
bool kernelParent)
{
return Singleton<TaskManager>::instance()._createTask(t, p, true,
kernelParent);
}
void TaskManager::endTask(task_t* t, void* retval, int status)
{
Singleton<TaskManager>::instance()._endTask(t,retval,status);
}
void TaskManager::waitTask(task_t* t, int64_t tid, int* status, void** retval)
{
Singleton<TaskManager>::instance()._waitTask(t,tid,status,retval);
}
task_t* TaskManager::_createIdleTask()
{
return this->_createTask(&TaskManager::idleTaskLoop, NULL, false, true);
}
task_t* TaskManager::_createTask(TaskManager::task_fn_t t,
void* p, bool withStack, bool kernelParent)
{
task_t* task = new task_t();
task->tid = this->getNextTid();
// Set NIP to be userspace_task_entry stub and GPR3 to be the
// function pointer for the desired task entry point.
task->context.nip = reinterpret_cast<void*>(&userspace_task_entry);
task->context.gprs[4] = reinterpret_cast<uint64_t>(t);
// Set up LR to be the entry point for task_end_stub in case a task
// 'returns' from its entry point.
task->context.lr = reinterpret_cast<uint64_t>(&task_end_stub);
// Set up GRP[13] as task structure reserved.
task->context.gprs[13] = (uint64_t)task;
// Set up argument.
task->context.gprs[3] = (uint64_t) p;
// Setup stack.
if (withStack)
{
task->context.stack_ptr = StackSegment::createStack(task->tid);
task->context.gprs[1] =
reinterpret_cast<uint64_t>(task->context.stack_ptr);
}
else
{
task->context.stack_ptr = NULL;
task->context.gprs[1] = NULL;
}
// Clear FP context (start with FP disabled on all tasks).
task->fp_context = NULL;
// Clear task state info.
task->state = TASK_STATE_READY;
task->state_info = NULL;
// Create tracker instance for this task.
task_tracking_t* tracker = new task_tracking_t();
tracker->key = task->tid;
tracker->task = task;
tracker->status = -1;
tracker->retval = NULL;
tracker->wait_info = NULL;
tracker->entry_point = reinterpret_cast<void*>(t);
task->tracker = tracker;
// Assign parent for tracker instance, add to task tree.
iv_spinlock.lock();
task_t* parent = kernelParent ? NULL : getCurrentTask();
if (NULL == parent)
{
tracker->parent = NULL;
iv_taskList.insert(tracker);
}
else
{
tracker->parent = parent->tracker;
parent->tracker->children.insert(tracker);
}
iv_spinlock.unlock();
return task;
}
void TaskManager::_endTask(task_t* t, void* retval, int status)
{
// Update task state.
t->state = TASK_STATE_ENDED;
// Make sure task pointers are updated before we delete this task.
if (getCurrentTask() == t)
t->cpu->scheduler->setNextRunnable();
iv_spinlock.lock();
// Update status in tracker.
t->tracker->status = status;
t->tracker->retval = retval;
t->tracker->task = NULL; // NULL signifies task is complete for now.
if (t->detached) // If detached, just clean up the tracker.
{
removeTracker(t->tracker);
}
else // If not detached, do join.
{
if (t->tracker->parent && t->tracker->parent->wait_info)
{
task_tracking_t* parent = t->tracker->parent;
if ((parent->wait_info->tid < 0) ||
(parent->wait_info->tid == t->tid))
{
if (parent->wait_info->status)
{
*(parent->wait_info->status) = status;
}
if (parent->wait_info->retval)
{
*(parent->wait_info->retval) = retval;
}
delete parent->wait_info;
parent->wait_info = NULL;
lwsync(); // Ensure status is pushed to memory before parent
// task begins execution.
TASK_SETRTN(parent->task, t->tid);
removeTracker(t->tracker);
parent->task->cpu->scheduler->addTask(parent->task);
}
}
else if (!t->tracker->parent) // Parented by kernel.
{
if (status == TASK_STATUS_CRASHED)
{
printk("Critical: Parentless task %d crashed.\n",
t->tid);
kassert(status != TASK_STATUS_CRASHED);
}
else
{
removeTracker(t->tracker);
}
}
}
iv_spinlock.unlock();
// Clean up task memory.
// Delete FP context.
if (t->fp_context)
delete t->fp_context;
// Delete stack.
StackSegment::deleteStack(t->tid);
// Delete task struct.
delete t;
}
void TaskManager::_waitTask(task_t* t, int64_t tid, int* status, void** retval)
{
iv_spinlock.lock();
do
{
// Search for a candidate completed child task.
task_tracking_t* child_task = NULL;
if (tid < 0) // -1 => Look for any task.
{
task_tracking_t* children = t->tracker->children.begin();
if (!children)
{
// No children at all, this is a deadlock.
TASK_SETRTN(t, -EDEADLK);
break;
}
while(children)
{
if (!children->task)
{
child_task = children;
children = NULL;
}
else
{
children = children->next;
}
}
}
else // Look for a specific task.
{
// This copy is needed to create a reference of the appropriate
// type to pass into the 'find' function. Otherwise, you get
// type-punned reference errors.
task_tracking_t::key_type _tid = tid;
child_task = t->tracker->children.find(_tid);
// Check that we aren't asking to wait on a task that isn't our
// child, potential deadlock.
if (NULL == child_task)
{
TASK_SETRTN(t, -EDEADLK);
break;
}
}
// If there was a finished task found return information to caller.
if (child_task && (child_task->task == NULL))
{
TASK_SETRTN(t, child_task->key);
if (status)
{
*status = child_task->status;
}
if (retval)
{
*retval = child_task->retval;
}
removeTracker(child_task);
}
else // Otherwise, create wait-info to defer task.
{
task_wait_t* tj = t->tracker->wait_info = new task_wait_t();
tj->tid = tid;
tj->status = status;
tj->retval = retval;
t->state = TASK_STATE_BLOCK_JOIN;
t->state_info = reinterpret_cast<void*>(tid);
t->cpu->scheduler->setNextRunnable();
}
} while(0);
iv_spinlock.unlock();
return;
}
void TaskManager::removeTracker(task_tracking_t* t)
{
task_tracking_list_t* trackingList = NULL;
task_tracking_t* parent = NULL;
if (t->parent)
{
trackingList = &t->parent->children;
parent = t->parent;
}
else // Parent is kernel.
{
trackingList = &iv_taskList;
}
// Remove tracker from parent list.
trackingList->erase(t);
// Add children to parent.
while(task_tracking_t* child = t->children.remove())
{
child->parent = parent;
if ((!parent) && (!child->task)) // Deal with finished children
// becoming parented by the kernel.
{
if (child->status == TASK_STATUS_CRASHED)
{
trackingList->insert(child); // Insert into kernel list so it
// is there for debug.
printk("Critical: Parentless task %d crashed.\n",
child->key);
kassert(child->status != TASK_STATUS_CRASHED);
}
else
{
removeTracker(child);
}
}
else
{
trackingList->insert(child);
}
}
// Delete tracker object.
delete t;
}
|