summaryrefslogtreecommitdiffstats
path: root/src/kernel/syscall.C
blob: cb4650c7a3092d307758f55239c2acbc1ceab8b2 (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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/kernel/syscall.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 <assert.h>
#include <errno.h>
#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/msg.H>
#include <kernel/timemgr.H>
#include <kernel/futexmgr.H>
#include <kernel/cpuid.H>
#include <kernel/misc.H>
#include <kernel/msghandler.H>
#include <kernel/vmmmgr.H>
#include <kernel/stacksegment.H>

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

    if (CpuManager::isShutdownRequested())
    {
        KernelMisc::shutdown();
    }

    s->setNextRunnable();
}

namespace Systemcalls
{
    typedef void(*syscall)(task_t*);
    void TaskYield(task_t*);
    void TaskStart(task_t*);
    void TaskEnd(task_t*);
    void TaskMigrateToMaster(task_t*);
    void MsgQCreate(task_t*);
    void MsgQDestroy(task_t*);
    void MsgQRegisterRoot(task_t*);
    void MsgQResolveRoot(task_t*);
    void MsgSend(task_t*);
    void MsgSendRecv(task_t*);
    void MsgRespond(task_t*);
    void MsgWait(task_t*);
    void MmioMap(task_t*);
    void MmioUnmap(task_t*);
    void DevMap(task_t*);
    void DevUnmap(task_t*);
    void TimeNanosleep(task_t*);
    void FutexWait(task_t *t);
    void FutexWake(task_t *t);
    void Shutdown(task_t *t);
    void CpuCoreType(task_t *t);
    void CpuDDLevel(task_t *t);
    void MmAllocBlock(task_t *t);
    void MmRemovePages(task_t *t);
    void MmSetPermission(task_t *t);


    syscall syscalls[] =
    {
        &TaskYield,  // TASK_YIELD
        &TaskStart,  // TASK_START
        &TaskEnd,  // TASK_END
        &TaskMigrateToMaster, // TASK_MIGRATE_TO_MASTER

        &MsgQCreate,  // MSGQ_CREATE
        &MsgQDestroy,  // MSGQ_DESTROY
        &MsgQRegisterRoot,  // MSGQ_REGISTER_ROOT
        &MsgQResolveRoot,  // MSGQ_RESOLVE_ROOT

        &MsgSend,  // MSG_SEND
        &MsgSendRecv,  // MSG_SENDRECV
        &MsgRespond,  // MSG_RESPOND
        &MsgWait,  // MSG_WAIT

        &MmioMap,  // MMIO_MAP
        &MmioUnmap,  // MMIO_UNMAP
        &DevMap,  // DEV_MAP
        &DevUnmap,  // DEV_UNMAP

        &TimeNanosleep,  // TIME_NANOSLEEP

        &FutexWait,  // FUTEX_WAIT
        &FutexWake,  // FUTEX_WAKE

        &Shutdown,  // MISC_SHUTDOWN
        &CpuCoreType,  // MISC_CPUCORETYPE
        &CpuDDLevel,  // MISC_CPUDDLEVEL

        &MmAllocBlock, // MM_ALLOC_BLOCK
        &MmRemovePages, // MM_REMOVE_PAGES
        &MmSetPermission, // MM_SET_PERMISSION
        };
};

extern "C"
void kernel_execute_system_call()
{
    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 : %ld\n", syscall);
        while(1);
    }
    else
    {
        syscalls[syscall](t);
    }
}

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.
            // Delete FP context.
        if (t->fp_context)
            delete t->fp_context;
            // Delete stack.
        StackSegment::deleteStack(t->tid);
            // Delete task struct.
        delete t;
    }

    void TaskMigrateToMaster(task_t* t)
    {
        // Move r6 to r3.
        //     This is needed so that this system call can be called from
        //     within a "fast" system call in start.S.  The fast system call
        //     will populate r6 with it's own syscall number.  When we return
        //     from this system call, on the master processor, we'll be back
        //     at the 'sc' instruction with r3 back to the fast syscall, and
        //     the fast syscall will be executed on the master processor.
        TASK_SETRTN(t, TASK_GETARG2(t));

        // Move task to master CPU and pick a new task.
        t->cpu->scheduler->addTaskMasterCPU(t);
        t->cpu->scheduler->setNextRunnable();
    }

    void MsgQCreate(task_t* t)
    {
        TASK_SETRTN(t, (uint64_t) new MessageQueue());
    }

    void MsgQDestroy(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);
        if (NULL != mq)
            delete mq;
        TASK_SETRTN(t, 0);
    }

    static MessageQueue* msgQRoot = NULL;

    void MsgQRegisterRoot(task_t* t)
    {
        msgQRoot = (MessageQueue*) TASK_GETARG0(t);
        TASK_SETRTN(t, 0);
    }

    void MsgQResolveRoot(task_t* t)
    {
        TASK_SETRTN(t, (uint64_t) msgQRoot);
    }

    void MsgSend(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);
        msg_t* m = (msg_t*) TASK_GETARG1(t);

        if ((NULL == mq) || (NULL == m))
        {
            printkd("NULL pointer for message queue (%p) or message (%p).\n",
                    mq, m);
            TASK_SETRTN(t, -EINVAL);
            return;
        }

        m->__reserved__async = 0; // set to async msg.

        if (m->type >= MSG_FIRST_SYS_TYPE)
        {
            printkd("Invalid type for msg_send, type=%d.\n", m->type);
            TASK_SETRTN(t, -EINVAL);
            return;
        }

        mq->lock.lock();

        // Get waiting (server) task.
        task_t* waiter = mq->waiting.remove();
        if (NULL == waiter) // None found, add to 'messages' queue.
        {
            MessagePending* mp = new MessagePending();
            mp->key = m;
            mp->task = t;
            mq->messages.insert(mp);
        }
        else // Add waiter back to its scheduler.
        {
            TASK_SETRTN(waiter, (uint64_t) m);
            waiter->cpu->scheduler->addTask(waiter);
        }

        mq->lock.unlock();
        TASK_SETRTN(t, 0);
    }

    void MsgSendRecv(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);
        msg_t* m = (msg_t*) TASK_GETARG1(t);
        m->__reserved__async = 1; // set to sync msg.

        if (m->type >= MSG_FIRST_SYS_TYPE)
        {
            printkd("Invalid message type for msg_sendrecv, type=%d.\n",
                    m->type);
            TASK_SETRTN(t, -EINVAL);
            return;
        }

        // Create pending response object.
        MessagePending* mp = new MessagePending();
        mp->key = m;
        mp->task = t;

        mq->lock.lock();

        // Get waiting (server) task.
        task_t* waiter = mq->waiting.remove();
        if (NULL == waiter) // None found, add to 'messages' queue.
        {
            mq->messages.insert(mp);
            // Choose next thread to execute, this one is delayed.
            t->cpu->scheduler->setNextRunnable();
        }
        else // Context switch to waiter.
        {
            TASK_SETRTN(waiter, (uint64_t) m);
            mq->responses.insert(mp);
            waiter->cpu = t->cpu;
            TaskManager::setCurrentTask(waiter);
        }

        mq->lock.unlock();
    }

    void MsgRespond(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);
        msg_t* m = (msg_t*) TASK_GETARG1(t);

        mq->lock.lock();
        MessagePending* mp = mq->responses.find(m);
        if (NULL != mp)
        {
            task_t* waiter = mp->task;

            mq->responses.erase(mp);
            mq->lock.unlock();
            delete mp;

            if (m->type >= MSG_FIRST_SYS_TYPE)
            {
                TASK_SETRTN(t,
                            ((MessageHandler*)waiter)->recvMessage(m));

                if (TaskManager::getCurrentTask() != t)
                {
                    t->cpu->scheduler->addTask(t);
                }
            }
            else
            {
                waiter->cpu = t->cpu;
                TaskManager::setCurrentTask(waiter);
                TASK_SETRTN(waiter,0);

                TASK_SETRTN(t,0);
                t->cpu->scheduler->addTask(t);
            }
        }
        else
        {
            TASK_SETRTN(t, -EBADF);
            mq->lock.unlock();
        }
    }

    void MsgWait(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);

        mq->lock.lock();
        MessagePending* mp = mq->messages.remove();

        if (NULL == mp)
        {
            mq->waiting.insert(t);
            t->cpu->scheduler->setNextRunnable();
        }
        else
        {
            msg_t* m = mp->key;
            if (m->__reserved__async)
                mq->responses.insert(mp);
            else
                delete mp;
            TASK_SETRTN(t, (uint64_t) m);
        }
        mq->lock.unlock();
    }

    void MmioMap(task_t* t)
    {
        void* ra = (void*)TASK_GETARG0(t);
        size_t pages = TASK_GETARG1(t);

        TASK_SETRTN(t, (uint64_t) VmmManager::mmioMap(ra,pages));
    }

    void MmioUnmap(task_t* t)
    {
        void* ea = (void*)TASK_GETARG0(t);
        size_t pages = TASK_GETARG1(t);

        TASK_SETRTN(t, VmmManager::mmioUnmap(ea,pages));
    }

    /**
     * Map a device into virtual memory
     * @param[in] t:  The task used to map a device
     */
    void DevMap(task_t *t)
    {
        void *ra = (void*)TASK_GETARG0(t);
        SEG_DATA_SIZES devDataSize = (SEG_DATA_SIZES)TASK_GETARG1(t);

        kassert(TASK_SETRTN(t, (uint64_t)VmmManager::devMap(ra,devDataSize)) !=
                NULL);
    }

    /**
     * Unmap a device from virtual memory
     * @param[in] t:  The task used to unmap a device
     */
    void DevUnmap(task_t *t)
    {
        void *ea = (void*)TASK_GETARG0(t);

        TASK_SETRTN(t, VmmManager::devUnmap(ea));
    }

    void TimeNanosleep(task_t* t)
    {
        TimeManager::delayTask(t, TASK_GETARG0(t), TASK_GETARG1(t));
        TASK_SETRTN(t, 0);

        Scheduler* s = t->cpu->scheduler;
        s->setNextRunnable();
    }

    /**
     * Put task on wait queue based on futex
     * @param[in] t:  The task to block
     */
    void FutexWait(task_t * t)
    {
        uint64_t uaddr = (uint64_t) TASK_GETARG0(t);
        uint64_t val   = (uint64_t) TASK_GETARG1(t);

        // Set RC to success initially.
        TASK_SETRTN(t,0);

        //translate uaddr from user space to kernel space
        uaddr = VmmManager::findPhysicalAddress(uaddr);
        if(uaddr != (uint64_t)(-EFAULT))
        {
            uint64_t rc = FutexManager::wait(t,(uint64_t *)uaddr,val);
            if (rc != 0) // Can only set rc if we still have control of the task,
                         // which is only (for certain) on error rc's.
            {
                TASK_SETRTN(t,rc);
            }
        }
        else
        {
            printk("Task %d terminated. No physical address found for address 0x%p",
                   t->tid, (void *) uaddr);
            TaskEnd(t);
        }
    }

    /**
     * Wake tasks on futex wait queue
     * @param[in] t:  The current task
     */
    void FutexWake(task_t * t)
    {
        uint64_t uaddr = (uint64_t) TASK_GETARG0(t);
        uint64_t count = (uint64_t) TASK_GETARG1(t);

        // translate uaddr from user space to kernel space
        uaddr = VmmManager::findPhysicalAddress(uaddr);
        if(uaddr != (uint64_t)(-EFAULT))
        {
            uint64_t started = FutexManager::wake((uint64_t *)uaddr,count);

            TASK_SETRTN(t,started);
        }
        else
        {
            printk("Task %d terminated. No physical address found for address 0x%p",
                   t->tid, (void *) uaddr);
            TaskEnd(t);
        }
    }

    /**
     * Shutdown all CPUs
     * @param[in] t:  The current task
     */
    void Shutdown(task_t * t)
    {
        uint64_t status = static_cast<uint64_t>(TASK_GETARG0(t));
        CpuManager::requestShutdown(status);
        TASK_SETRTN(t, 0);
    }

    /** Read CPU Core type using CpuID interfaces. */
    void CpuCoreType(task_t *t)
    {
        TASK_SETRTN(t, CpuID::getCpuType());
    }

    /** Read CPU DD level using CpuID interfaces. */
    void CpuDDLevel(task_t *t)
    {
        TASK_SETRTN(t, CpuID::getCpuDD());
    }

    /**
     * Allocate a block of virtual memory within the base segment
     * @param[in] t: The task used to allocate a block in the base segment
     */
    void MmAllocBlock(task_t* t)
    {
        MessageQueue* mq = (MessageQueue*)TASK_GETARG0(t);
        void* va = (void*)TASK_GETARG1(t);
        uint64_t size = (uint64_t)TASK_GETARG2(t);

        TASK_SETRTN(t, VmmManager::mmAllocBlock(mq,va,size));
    }

    /**
     * Remove pages from virtual memory
     * @param[in] t: The task used to remove pages
     */
    void MmRemovePages(task_t* t)
    {
        VmmManager::PAGE_REMOVAL_OPS oper =
                (VmmManager::PAGE_REMOVAL_OPS)TASK_GETARG0(t);
        void* vaddr = (void*)TASK_GETARG1(t);
        uint64_t size = (uint64_t)TASK_GETARG2(t);

        TASK_SETRTN(t, VmmManager::mmRemovePages(oper,vaddr,size,t));
    }

     /**
     * Set the Permissions on a block containing the virtual address passed in.
     * @param[in] t: The task used to set Page Permissions for a given block
     */
    void MmSetPermission(task_t* t)
    {   
        void* va = (void*)TASK_GETARG0(t);
        uint64_t size = (uint64_t)TASK_GETARG1(t);
        PAGE_PERMISSIONS access_type = (PAGE_PERMISSIONS)TASK_GETARG2(t);
        
        TASK_SETRTN(t, VmmManager::mmSetPermission(va,size, access_type));
    }
    
};
OpenPOWER on IntegriCloud