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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/kernel/syscall.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2010,2017 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* 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 <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>
#include <kernel/heapmgr.H>
#include <kernel/intmsghandler.H>
#include <kernel/doorbell.H>
#include <sys/sync.h>
#include <errno.h>
namespace KernelIpc
{
int send(uint64_t i_q, msg_t * i_msg);
};
extern "C"
void kernel_execute_hype_doorbell()
{
task_t* t = TaskManager::getCurrentTask();
doorbell_clear();
//Execute all work items on doorbell_actions stack
KernelWorkItem *l_work = t->cpu->doorbell_actions.pop();
while(l_work != NULL)
{
//Execute Work Item and then delete it
(*l_work)();
delete l_work;
l_work = t->cpu->doorbell_actions.pop();
}
if (t->cpu->idle_task == t)
{
t->cpu->scheduler->returnRunnable();
t->cpu->scheduler->setNextRunnable();
}
DeferredQueue::execute();
}
extern "C"
void kernel_execute_decrementer()
{
cpu_t* c = CpuManager::getCurrentCPU();
Scheduler* s = c->scheduler;
TimeManager::checkReleaseTasks(s);
task_t* current_task = TaskManager::getCurrentTask();
CpuManager::executePeriodics(c);
if (current_task == TaskManager::getCurrentTask())
{
s->returnRunnable();
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 TaskWait(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 DevMap(task_t*);
void DevUnmap(task_t*);
void TimeNanosleep(task_t*);
void Futex(task_t *t);
void Shutdown(task_t *t);
void CpuCoreType(task_t *t);
void CpuDDLevel(task_t *t);
void CpuStartCore(task_t *t);
void CpuSprValue(task_t *t);
void CpuSprSet(task_t *t);
void CpuNap(task_t *t);
void CpuWinkle(task_t *t);
void MmAllocBlock(task_t *t);
void MmRemovePages(task_t *t);
void MmSetPermission(task_t *t);
void MmAllocPages(task_t *t);
void MmVirtToPhys(task_t *t);
void MmExtend(task_t *t);
void MmLinearMap(task_t *t);
void CritAssert(task_t *t);
syscall syscalls[] =
{
&TaskYield, // TASK_YIELD
&TaskStart, // TASK_START
&TaskEnd, // TASK_END
&TaskMigrateToMaster, // TASK_MIGRATE_TO_MASTER
&TaskWait, // TASK_WAIT
&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
&DevMap, // DEV_MAP
&DevUnmap, // DEV_UNMAP
&TimeNanosleep, // TIME_NANOSLEEP
&Futex, // SYS_FUTEX operations
&Shutdown, // MISC_SHUTDOWN
&CpuCoreType, // MISC_CPUCORETYPE
&CpuDDLevel, // MISC_CPUDDLEVEL
&CpuStartCore, // MISC_CPUSTARTCORE
&CpuSprValue, // MISC_CPUSPRVALUE
&CpuSprSet, // MISC_CPUSPRSET
&CpuNap, // MISC_CPUNAP
&CpuWinkle, // MISC_CPUWINKLE
&MmAllocBlock, // MM_ALLOC_BLOCK
&MmRemovePages, // MM_REMOVE_PAGES
&MmSetPermission, // MM_SET_PERMISSION
&MmAllocPages, // MM_ALLOC_PAGES
&MmVirtToPhys, // MM_VIRT_TO_PHYS
&MmExtend, // MM_EXTEND
&MmLinearMap, // MM_LINEAR_MAP
&CritAssert, // MISC_CRITASSERT
};
};
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)
{
printk("Invalid syscall : %ld\n", syscall);
TaskManager::endTask(t, NULL, TASK_STATUS_CRASHED);
}
else
{
syscalls[syscall](t);
}
}
namespace Systemcalls
{
void TaskYield(task_t* t)
{
Scheduler* s = t->cpu->scheduler;
s->returnRunnable();
s->setNextRunnable();
// This call prevents a live-lock situation.
CpuManager::executePeriodics(CpuManager::getCurrentCPU());
}
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);
doorbell_broadcast();
}
void TaskEnd(task_t* t)
{
TaskManager::endTask(t, (void*)TASK_GETARG0(t),
TASK_STATUS_EXITED_CLEAN);
}
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();
doorbell_broadcast();
}
void TaskWait(task_t* t)
{
int64_t tid = static_cast<int64_t>(TASK_GETARG0(t));
int* status = reinterpret_cast<int*>(TASK_GETARG1(t));
void** retval = reinterpret_cast<void**>(TASK_GETARG2(t));
// Validate status address and convert to kernel address.
if (status != NULL)
{
uint64_t addr =
VmmManager::findKernelAddress(
reinterpret_cast<uint64_t>(status));
if (addr == (static_cast<uint64_t>(-EFAULT)))
{
TASK_SETRTN(t, -EFAULT);
return;
}
status = reinterpret_cast<int*>(addr);
}
// Validate retval address and convert to kernel address.
if (retval != NULL)
{
uint64_t addr =
VmmManager::findKernelAddress(
reinterpret_cast<uint64_t>(retval));
if (addr == (static_cast<uint64_t>(-EFAULT)))
{
TASK_SETRTN(t, -EFAULT);
return;
}
retval = reinterpret_cast<void**>(addr);
}
// Perform wait.
TaskManager::waitTask(t, tid, status, retval);
}
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;
static MessageQueue* msgQIntr = NULL;
void MsgQRegisterRoot(task_t* t)
{
switch(TASK_GETARG0(t))
{
case MSGQ_ROOT_VFS:
msgQRoot = (MessageQueue*) TASK_GETARG1(t);
TASK_SETRTN(t,0);
break;
case MSGQ_ROOT_INTR:
{
msgQIntr = (MessageQueue*) TASK_GETARG1(t);
uint64_t ipc_addr = (uint64_t) TASK_GETARG2(t);
InterruptMsgHdlr::create(msgQIntr,ipc_addr);
TASK_SETRTN(t,0);
}
break;
default:
printk("ERROR MsgRegisterRoot invalid type %ld\n",
TASK_GETARG0(t));
TASK_SETRTN(t,-EINVAL);
}
}
void MsgQResolveRoot(task_t* t)
{
switch(TASK_GETARG0(t))
{
case MSGQ_ROOT_VFS:
TASK_SETRTN(t, (uint64_t) msgQRoot);
break;
case MSGQ_ROOT_INTR:
TASK_SETRTN(t, (uint64_t) msgQIntr);
break;
default:
printk("ERROR MsgQResolveRoot invalid type %ld\n",
TASK_GETARG0(t));
TASK_SETRTN(t,0);
}
}
void MsgSend(task_t* t)
{
uint64_t q_handle = TASK_GETARG0(t);
msg_t* m = (msg_t*) TASK_GETARG1(t);
int rc = 0;
if(((q_handle >> 32) & MSGQ_TYPE_IPC) != 0)
{
rc = KernelIpc::send(q_handle, m);
}
else
{
MessageQueue* mq = reinterpret_cast<MessageQueue*>(q_handle);
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);
doorbell_broadcast();
}
mq->lock.unlock();
}
TASK_SETRTN(t, rc);
}
void MsgSendRecv(task_t* t)
{
MessageQueue* mq = (MessageQueue*) TASK_GETARG0(t);
msg_t* m = (msg_t*) TASK_GETARG1(t);
MessageQueue* mq2 = (MessageQueue*) TASK_GETARG2(t);
m->__reserved__async = 1; // set to sync msg.
if (NULL != mq2) // set as pseudo-sync if secondary queue given.
{
m->__reserved__pseudosync = 1;
}
if (m->type >= MSG_FIRST_SYS_TYPE)
{
printk("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;
if (!m->__reserved__pseudosync) // Normal sync, add task to pending obj.
{
mp->task = t;
t->state = TASK_STATE_BLOCK_MSG;
t->state_info = mq;
}
else // Pseudo-sync, add the secondary queue instead.
{
mp->task = reinterpret_cast<task_t*>(mq2);
TASK_SETRTN(t, 0); // Need to give good RC for the caller, since
// we are returning immediately.
}
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);
if (!m->__reserved__pseudosync)
{
// Choose next task to execute, this one is delayed.
t->cpu->scheduler->setNextRunnable();
} // For pseudo-sync, just keep running the current task.
}
else // Context switch to waiter.
{
TASK_SETRTN(waiter, (uint64_t) m);
mq->responses.insert(mp);
waiter->cpu = t->cpu;
if (m->__reserved__pseudosync) // For pseudo-sync, add this task
// back to scheduler.
{
t->cpu->scheduler->addTask(t);
doorbell_broadcast();
}
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;
// Kernel message types are handled by MessageHandler objects.
if (m->type >= MSG_FIRST_SYS_TYPE)
{
TASK_SETRTN(t,
((MessageHandler*)waiter)->recvMessage(m));
if (TaskManager::getCurrentTask() != t)
{
t->cpu->scheduler->addTask(t);
doorbell_broadcast();
}
}
// Pseudo-sync messages are handled by pushing the response onto
// a message queue.
else if (m->__reserved__pseudosync)
{
MessageQueue* mq2 = (MessageQueue*) waiter;
mq2->lock.lock();
// See if there is a waiting task (the original client).
task_t* client = mq2->waiting.remove();
if (NULL == client) // None found, add to queue.
{
MessagePending* mp2 = new MessagePending();
mp2->key = m;
mp2->task = t;
mq2->messages.insert(mp2);
}
else // Add waiting task onto its scheduler.
{
TASK_SETRTN(client, (uint64_t) m);
client->cpu->scheduler->addTask(client);
doorbell_broadcast();
}
mq2->lock.unlock();
TASK_SETRTN(t, 0);
}
// Normal-sync messages are handled by releasing the deferred task.
else
{
waiter->cpu = t->cpu;
TaskManager::setCurrentTask(waiter);
TASK_SETRTN(waiter,0);
TASK_SETRTN(t,0);
t->cpu->scheduler->addTask(t);
doorbell_broadcast();
}
}
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->state = TASK_STATE_BLOCK_MSG;
t->state_info = mq;
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();
}
/**
* 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);
uint64_t devDataSize = ALIGN_PAGE(TASK_GETARG1(t));
bool cacheable = (0 != TASK_GETARG2(t));
bool guarded = (0 != TASK_GETARG3(t));
if (TASK_GETARG0(t) & (PAGESIZE - 1)) // ensure address page alignment.
{
TASK_SETRTN(t, NULL);
}
else if (devDataSize > THIRTYTWO_GB)
{
TASK_SETRTN(t, NULL);
}
else
{
TASK_SETRTN(t,
(uint64_t)VmmManager::devMap(
ra,devDataSize,cacheable,guarded));
}
}
/**
* 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);
t->cpu->scheduler->setNextRunnable();
}
void Futex(task_t * t)
{
uint64_t op = static_cast<uint64_t>(TASK_GETARG0(t));
uint64_t futex = static_cast<uint64_t>(TASK_GETARG1(t));
uint64_t val = static_cast<uint64_t>(TASK_GETARG2(t));
uint64_t val2 = static_cast<uint64_t>(TASK_GETARG3(t));
uint64_t futex2 = static_cast<uint64_t>(TASK_GETARG4(t));
uint64_t rc = 0;
// Set RC to success initially.
TASK_SETRTN(t,0);
futex = VmmManager::findKernelAddress(futex);
if(futex == (static_cast<uint64_t>(-EFAULT)))
{
printk("Task %d terminated. No physical address found for address 0x%p",
t->tid,
reinterpret_cast<void *>(futex));
TaskManager::endTask(t, NULL, TASK_STATUS_CRASHED);
return;
}
uint64_t * futex_p = reinterpret_cast<uint64_t *>(futex);
switch(op)
{
case FUTEX_WAIT: // Put task on wait queue based on futex
rc = FutexManager::wait(t, futex_p, val);
// Can only be set rc if control of the task is still had,
// which is only, for certain, on error rc's
if(rc != 0)
{
TASK_SETRTN(t,rc);
}
break;
case FUTEX_WAKE: // Wake task(s) on the futex wait queue
rc = FutexManager::wake(futex_p, val);
TASK_SETRTN(t,rc);
break;
case FUTEX_REQUEUE:
// Wake (val) task(s) on futex && requeue remaining tasks on futex2
futex2 = VmmManager::findKernelAddress(futex2);
if(futex2 == (static_cast<uint64_t>(-EFAULT)))
{
printk("Task %d terminated. No physical address found for address 0x%p",
t->tid,
reinterpret_cast<void *>(futex2));
TaskManager::endTask(t, NULL, TASK_STATUS_CRASHED);
return;
}
rc = FutexManager::wake(futex_p, val,
reinterpret_cast<uint64_t *>(futex2),
val2);
break;
default:
printk("ERROR Futex invalid op %ld\n",op);
TASK_SETRTN(t,static_cast<uint64_t>(-EINVAL));
};
}
/**
* Shutdown all CPUs
* @param[in] t: The current task
*/
void Shutdown(task_t * t)
{
uint64_t status = static_cast<uint64_t>(TASK_GETARG0(t));
KernelMisc::g_payload_base = static_cast<uint64_t>(TASK_GETARG1(t));
KernelMisc::g_payload_entry = static_cast<uint64_t>(TASK_GETARG2(t));
KernelMisc::g_payload_data = static_cast<uint64_t>(TASK_GETARG3(t));
KernelMisc::g_masterHBInstance= static_cast<uint64_t>(TASK_GETARG4(t));
KernelMisc::g_error_data= static_cast<uint32_t>(TASK_GETARG5(t));
CpuManager::requestShutdown(status, KernelMisc::g_error_data);
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());
}
/** Prep core for activation. */
void CpuStartCore(task_t *t)
{
// This will cause another task to be scheduled in while the
// core is started.
CpuManager::startCore(static_cast<uint64_t>(TASK_GETARG0(t)),
static_cast<uint64_t>(TASK_GETARG1(t)));
};
/** Read SPR values. */
void CpuSprValue(task_t *t)
{
uint64_t spr = TASK_GETARG0(t);
switch (spr)
{
case CPU_SPR_MSR:
TASK_SETRTN(t, CpuManager::WAKEUP_MSR_VALUE);
break;
case CPU_SPR_LPCR:
TASK_SETRTN(t, CpuManager::WAKEUP_LPCR_VALUE);
break;
case CPU_SPR_HRMOR:
TASK_SETRTN(t, getHRMOR());
break;
case CPU_SPR_HID:
TASK_SETRTN(t, getHID());
break;
default:
TASK_SETRTN(t, -1);
break;
}
};
/** Set SPR values. */
void CpuSprSet(task_t *t)
{
uint64_t spr = TASK_GETARG0(t);
uint64_t newValue = TASK_GETARG1(t);
switch (spr)
{
case CPU_SPR_HID:
setHID( newValue );
TASK_SETRTN(t, true);
break;
default:
// unsupported SPR for write
TASK_SETRTN(t, false);
break;
}
};
/**
* Allow a task to request privilege escalation to execute the 'nap'
* instruction.
*
* Verifies the instruction to execute is, in fact, nap and then sets
* an MSR mask in the task structure to allow escalation on next
* execution.
*
* When 'nap' is executed the processor will eventually issue an
* SRESET exception with flags in srr1 to indication that the
* decrementer caused the wake-up. The kernel will then need to
* advance the task to the instruction after the nap and remove
* privilege escalation.
*
*/
void CpuNap(task_t *t)
{
uint32_t* instruction = static_cast<uint32_t*>(t->context.nip);
if (STOP_INSTRUCTION == (*instruction)) // Verify 'nap' instruction,
// otherwise just return.
{
// Disable EE, PR, IR, DR so 'nap' can be executed.
// (which means to stay in HV state)
t->context.msr_mask = 0xC030;
}
};
/** Winkle all the threads. */
void CpuWinkle(task_t *t)
{
cpu_t* cpu = CpuManager::getCurrentCPU();
if ((WINKLE_SCOPE_MASTER == TASK_GETARG0(t) &&
(CpuManager::getCpuCount() > CpuManager::getThreadCount())) ||
(!cpu->master))
{
TASK_SETRTN(t, -EDEADLK);
}
else
{
TASK_SETRTN(t, 0);
DeferredWork* deferred = NULL;
if (WINKLE_SCOPE_MASTER == TASK_GETARG0(t))
{
bool l_fusedCores = (bool)TASK_GETARG1(t);
deferred = new KernelMisc::WinkleCore(t, l_fusedCores);
}
else
{
deferred = new KernelMisc::WinkleAll(t);
}
t->state = TASK_STATE_BLOCK_USRSPACE;
t->state_info = deferred;
DeferredQueue::insert(deferred);
TaskManager::setCurrentTask(cpu->idle_task);
DeferredQueue::execute();
}
}
/**
* 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);
if (TASK_GETARG1(t) & (PAGESIZE - 1)) // ensure address page alignment.
{
TASK_SETRTN(t, NULL);
}
else
{
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));
}
/**
* Call PageManager to allocate a number of pages.
* @param[in] t: The task used.
*/
void MmAllocPages(task_t* t)
{
ssize_t pages = TASK_GETARG0(t);
// Attempt to allocate the page(s).
void* page = PageManager::allocatePage(pages, true);
TASK_SETRTN(t, reinterpret_cast<uint64_t>(page));
// If we are low on memory, call into the VMM to free some up.
uint64_t pcntAvail = PageManager::queryAvail();
if (pcntAvail < PageManager::LOWMEM_NORM_LIMIT)
{
static uint64_t one_at_a_time = 0;
if (!__sync_lock_test_and_set(&one_at_a_time, 1))
{
VmmManager::flushPageTable();
VmmManager::castout_t sev =
(pcntAvail < PageManager::LOWMEM_CRIT_LIMIT) ?
VmmManager::CRITICAL : VmmManager::NORMAL;
VmmManager::castOutPages(sev);
__sync_lock_release(&one_at_a_time);
}
}
else if ((page == NULL) && (pages > 1))
{
CpuManager::forceMemoryPeriodic();
}
}
/**
* Return the physical address backing a virtual address
* @param[in] t: The task used
*/
void MmVirtToPhys(task_t* t)
{
uint64_t i_vaddr = (uint64_t)TASK_GETARG0(t);
uint64_t phys = VmmManager::findPhysicalAddress(i_vaddr);
TASK_SETRTN(t, phys);
}
/**
* Extends the initial footprint of the image further into memory.
*
* Depending on the syscall parameter, we will either switch from 4MB
* to a cache-contained mode (either full 10MB or reduced 8MB) or will
* expand into 48MB of space using real system memory.
* @param[in] t: The task used to extend Memory
*/
void MmExtend(task_t* t)
{
uint64_t size = TASK_GETARG0(t);
switch (size)
{
case MM_EXTEND_REDUCED_CACHE:
TASK_SETRTN(t, KernelMisc::expand_full_cache(8*MEGABYTE));
break;
case MM_EXTEND_FULL_CACHE:
TASK_SETRTN(t,
KernelMisc::expand_full_cache(VMM_BASE_BLOCK_SIZE));
break;
case MM_EXTEND_REAL_MEMORY:
TASK_SETRTN(t, VmmManager::mmExtend());
break;
default:
TASK_SETRTN(t, -EINVAL);
break;
}
}
/**
* Allocates a block of memory of the given size
* to at a specified physical address
*/
void MmLinearMap(task_t* t)
{
void* paddr = (void *)TASK_GETARG0(t);
uint64_t size = (uint64_t)TASK_GETARG1(t);
TASK_SETRTN(t, VmmManager::mmLinearMap(paddr,size));
}
/**
* Call Crit assert to perform the terminate Immediate
* @param[in] t: the task calling the critical assert
*/
void CritAssert(task_t* t)
{
uint64_t i_failAddr = (uint64_t)(TASK_GETARG0(t));
CpuManager::critAssert(i_failAddr);
}
};
|