summaryrefslogtreecommitdiffstats
path: root/src/kernel/syscall.C
blob: ce26970c744de764151db2adf8f38e9f630a73d0 (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
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
#include <kernel/cpu.H>
#include <kernel/cpumgr.H>
#include <kernel/scheduler.H>
#include <kernel/taskmgr.H>
#include <kernel/task.H>
#include <kernel/syscalls.H>
#include <kernel/console.H>
#include <kernel/pagemgr.H>
#include <kernel/usermutex.H>

extern "C"
void kernel_execute_decrementer()
{
    Scheduler* s = CpuManager::getCurrentCPU()->scheduler;
    s->returnRunnable();
    s->setNextRunnable();

    // Resync decrementer.
    register uint64_t decrementer = 0x0f000000;
    asm volatile("mtdec %0" :: "r"(decrementer));
}

namespace Systemcalls
{
    typedef void(*syscall)(task_t*);
    void TaskYield(task_t*);
    void TaskStart(task_t*);
    void TaskEnd(task_t*);
    void TaskGettid(task_t*);
    void MutexCreate(task_t*);
    void MutexDestroy(task_t*);
    void MutexLockCont(task_t*);
    void MutexUnlockCont(task_t*);

    syscall syscalls[] =
	{
	    &TaskYield,
	    &TaskStart,
	    &TaskEnd,
	    &TaskGettid,

	    &MutexCreate,
	    &MutexDestroy,
	    &MutexLockCont,
	    &MutexUnlockCont,
	};
};

extern "C"
void kernel_execute_systemcall()
{
    using namespace Systemcalls;
    task_t* t = TaskManager::getCurrentTask();

    uint64_t syscall = t->context.gprs[3];
    if (syscall > SYSCALL_MAX)
    {
	// TODO : kill task.
	printk("Invalid syscall : %lld\n", syscall);
	while(1);
    }
    else
    {
	syscalls[syscall](t);
    }
}

#define TASK_GETARGN(t, n) (t->context.gprs[n+4])
#define TASK_GETARG0(t) (TASK_GETARGN(t,0))
#define TASK_GETARG1(t) (TASK_GETARGN(t,1))
#define TASK_GETARG2(t) (TASK_GETARGN(t,2))
#define TASK_GETARG3(t) (TASK_GETARGN(t,3))
#define TASK_GETARG4(t) (TASK_GETARGN(t,4))
#define TASK_GETARG5(t) (TASK_GETARGN(t,5))
#define TASK_GETARG6(t) (TASK_GETARGN(t,6))
#define TASK_GETARG7(t) (TASK_GETARGN(t,7))

#define TASK_SETRTN(t, n) (t->context.gprs[3] = (n))

namespace Systemcalls
{
    void TaskYield(task_t* t)
    {
	Scheduler* s = t->cpu->scheduler;
	s->returnRunnable();
	s->setNextRunnable();
    }

    void TaskStart(task_t* t)
    {
	task_t* newTask = 
	    TaskManager::createTask((TaskManager::task_fn_t)TASK_GETARG0(t),
				    (void*)TASK_GETARG1(t));
	newTask->cpu = t->cpu;
	t->cpu->scheduler->addTask(newTask);

	TASK_SETRTN(t, newTask->tid);
    }

    void TaskEnd(task_t* t)
    {
	// Make sure task pointers are updated before we delete this task.
	t->cpu->scheduler->setNextRunnable();
	
	// TODO: Deal with join.

	// Clean up task memory.
	PageManager::freePage(t->context.stack_ptr, TASK_DEFAULT_STACK_SIZE);
	delete t;
    }

    void TaskGettid(task_t* t)
    {
	TASK_SETRTN(t, t->tid);
    }

    void MutexCreate(task_t* t)
    {
	UserMutex * m = new UserMutex();
	TASK_SETRTN(t, (uint64_t)m);
    }

    void MutexDestroy(task_t* t)
    {
	// TODO: Extra verification of parameter and mutex state.

	delete (UserMutex*) TASK_GETARG0(t);
	TASK_SETRTN(t, 0);
    }

    void MutexLockCont(task_t* t)
    {
	// TODO: Extra verification of parameter and mutex state.
	
	UserMutex* m = (UserMutex*) TASK_GETARG0(t);
	m->lock.lock();
	if (m->unlock_pend) 
	{   
	    // We missed the unlock syscall, take lock and return to userspace.
	    m->unlock_pend = false;
	    m->lock.unlock();

	}
	else
	{
	    // Queue ourself to wait for unlock.
	    m->waiting.insert(TaskManager::getCurrentTask());
	    m->lock.unlock();
	    CpuManager::getCurrentCPU()->scheduler->setNextRunnable();
	}
	TASK_SETRTN(t, 0);	
    }

    void MutexUnlockCont(task_t* t)
    {
	// TODO: Extra verification of parameter and mutex state.

	UserMutex* m = (UserMutex*) TASK_GETARG0(t);
	m->lock.lock();
	task_t* wait_task = m->waiting.remove();
	if (NULL == wait_task)
	{
	    m->unlock_pend = true;
	}
	else
	{
	    wait_task->cpu->scheduler->addTask(wait_task);
	}
	m->lock.unlock();
	TASK_SETRTN(t, 0);
    }
};
OpenPOWER on IntegriCloud