summaryrefslogtreecommitdiffstats
path: root/src/kernel/taskmgr.C
blob: 00bb1d2c9e1a79907885dc00fa3b86e90a58d583 (plain)
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
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/kernel/taskmgr.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2010 - 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END
#include <util/singleton.H>
#include <kernel/taskmgr.H>
#include <kernel/task.H>
#include <kernel/pagemgr.H>
#include <kernel/cpumgr.H>
#include <sys/task.h>
#include <arch/ppc.H>
#include <string.h>
#include <limits.h>

extern "C" void userspace_task_entry();

void TaskManager::idleTaskLoop(void* unused)
{
    while(1)
    {
	setThreadPriorityLow();
    }
}

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();
    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)
{
    return Singleton<TaskManager>::instance()._createTask(t, p, true);
}

task_t* TaskManager::_createIdleTask()
{
    return this->_createTask(&TaskManager::idleTaskLoop, NULL, false);
}

task_t* TaskManager::_createTask(TaskManager::task_fn_t t,
				 void* p, bool withStack)
{
    task_t* task = new task_t;
    memset(task, '\0', sizeof(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 in case a task
    // 'returns' from its entry point.  By the Power ABI, the entry
    // point address is in (func_ptr)[0].
    task->context.lr = reinterpret_cast<uint64_t*>(&task_end)[0];

    // 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 =
	    PageManager::allocatePage(TASK_DEFAULT_STACK_SIZE);
        memset(task->context.stack_ptr, '\0',
               TASK_DEFAULT_STACK_SIZE * PAGESIZE);
	task->context.gprs[1] = ((uint64_t)task->context.stack_ptr) +
               TASK_DEFAULT_STACK_SIZE * PAGESIZE - 8;
    }
    else
    {
	task->context.stack_ptr = NULL;
        task->context.gprs[1] = NULL;
    }

    return task;
}

OpenPOWER on IntegriCloud