forked from higepon/mona
/
syscalls.cpp
executable file
·961 lines (818 loc) · 28.1 KB
/
syscalls.cpp
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
/*!
\file syscalls.cpp
\brief syscalls
Copyright (c) 2002,2003 Higepon and the individuals listed on the ChangeLog entries.
All rights reserved.
License=MIT/X License
\author Higepon
\version $Revision$
\date create:2003/03/22 update:$Date$
*/
#include "syscalls.h"
#include "global.h"
#include "io.h"
#include "Loader.h"
#include "RTC.h"
#include "apm.h"
#include "shutdown.h"
#include "sys/error.h"
#include "Condition.h"
extern const char* version;
extern uint32_t version_number;
extern mones::FrameNode* g_frames;
extern mones::Nic* g_nic;
inline intptr_t syscall1(intptr_t syscall_number, intptr_t arg1)
{
intptr_t ret = 0;
asm volatile("movl $%c1, %%ebx \n"
"movl %2 , %%esi \n"
"int $0x80 \n"
"movl %%eax, %0 \n"
:"=m"(ret)
:"g"(syscall_number), "m"(arg1)
:"ebx", "esi"
);
return ret;
}
inline intptr_t syscall2(intptr_t syscall_number, intptr_t arg1, intptr_t arg2)
{
intptr_t ret = 0;
asm volatile("movl $%c1, %%ebx \n"
"movl %2 , %%esi \n"
"movl %3 , %%ecx \n"
"int $0x80 \n"
"movl %%eax, %0 \n"
:"=m"(ret)
:"g"(syscall_number), "m"(arg1), "m"(arg2)
:"ebx", "esi", "ecx"
);
return ret;
}
inline void setReturnValue(ArchThreadInfo* info, intptr_t value)
{
info->eax = value;
}
uint32_t systemcall_mutex_create()
{
KMutex* mutex = new KMutex();
return g_id->allocateID(mutex);
}
uint32_t systemcall_mutex_lock(uint32_t id)
{
uint32_t result;
int noTimeout = 0;
return syscall2(SYSTEM_CALL_MUTEX_LOCK, id, noTimeout);
}
uint32_t systemcall_mutex_unlock(uint32_t id)
{
return syscall1(SYSTEM_CALL_MUTEX_UNLOCK, id);
}
// don't call without systemcall
// this has context change
// use systemcall_mutex_lock()
static intptr_t systemcall_mutex_lock2(intptr_t id, intptr_t timeoutTick)
{
KObject* object = g_id->get(id, g_currentThread->thread, KObject::KMUTEX);
if (NULL == object) {
return M_BAD_MUTEX_ID;
}
KMutex* mutex = (KMutex*)object;
return mutex->lock(g_currentThread->thread, timeoutTick);
}
// don't call without systemcall
// this has context change
// use systemcall_mutex_unlock()
static intptr_t systemcall_mutex_unlock2(intptr_t id)
{
KObject* object = g_id->get(id, g_currentThread->thread, KObject::KMUTEX);
if (object == NULL) {
return M_BAD_MUTEX_ID;
}
KMutex* mutex = (KMutex*)object;
return mutex->unlock();
}
void syscall_entrance()
{
ScreenInfo* screenInfo;
ArchThreadInfo* info = g_currentThread->archinfo;
/* result normal */
setReturnValue(info, M_OK);
#define SYSTEM_CALL_ARG_1 (info->esi)
#define SYSTEM_CALL_ARG_2 (info->ecx)
#define SYSTEM_CALL_ARG_3 (info->edi)
#define SYSTEM_CALL_ARG_4 (info->edx)
switch(info->ebx)
{
case SYSTEM_CALL_PRINT:
//enableInterrupt();
g_console->printf("%s", (char*)(SYSTEM_CALL_ARG_1));
break;
case SYSTEM_CALL_ALLOCATE_CONTIGUOUS:
{
LinearAddress laddress = SYSTEM_CALL_ARG_1;
int pageNum = SYSTEM_CALL_ARG_2;
setReturnValue(info, g_page_manager->allocateContiguous(g_currentThread->process->getPageDirectory(),
laddress,
pageNum));
break;
}
case SYSTEM_CALL_DEALLOCATE_CONTIGUOUS:
{
LinearAddress laddress = SYSTEM_CALL_ARG_1;
int pageNum = SYSTEM_CALL_ARG_2;
g_page_manager->deallocateContiguous(g_currentThread->process->getPageDirectory(),
laddress,
pageNum);
break;
}
case SYSTEM_CALL_SET_TIMER:
setReturnValue(info, g_scheduler->SetTimer(g_currentThread->thread, SYSTEM_CALL_ARG_1));
break;
case SYSTEM_CALL_KILL_TIMER:
setReturnValue(info, g_scheduler->KillTimer(SYSTEM_CALL_ARG_1, g_currentThread->thread));
break;
case SYSTEM_CALL_MTHREAD_SLEEP:
g_scheduler->Sleep(g_currentThread->thread, SYSTEM_CALL_ARG_1);
g_scheduler->SwitchToNext();
break;
case SYSTEM_CALL_MTHREAD_SELF:
{
setReturnValue(info, g_currentThread->thread->id);
break;
}
case SYSTEM_CALL_KILL:
ThreadOperation::kill();
g_scheduler->SwitchToNext();
break;
case SYSTEM_CALL_KILL_THREAD:
{
uint32_t tid = SYSTEM_CALL_ARG_1;
intptr_t ret = ThreadOperation::kill(tid);
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
break;
}
case SYSTEM_CALL_SEND:
{
uintptr_t id = (uintptr_t)(SYSTEM_CALL_ARG_1);
Thread* thread = g_scheduler->Find(id);
if (NULL == thread) {
setReturnValue(info, -1);
break;
} else {
intptr_t ret = g_messenger->send(thread, (MessageInfo*)(SYSTEM_CALL_ARG_2));
setReturnValue(info, ret);
if (ret == M_OK) {
g_scheduler->SwitchToNext();
/* not reached */
} else {
break;
}
}
}
case SYSTEM_CALL_RECEIVE:
setReturnValue(info, g_messenger->receive(g_currentThread->thread, (MessageInfo*)(SYSTEM_CALL_ARG_1)));
break;
case SYSTEM_CALL_EXIST_MESSAGE:
{
bool existMessage = !(g_currentThread->thread->messageList->isEmpty());
setReturnValue(info, existMessage ? 1 : 0);
break;
}
case SYSTEM_CALL_GET_PHYSICAL_ADDRESS:
{
PhysicalAddress ret = 0;
g_page_manager->getPhysicalAddress((PageEntry*)g_currentThread->archinfo->cr3, (uint32_t)(SYSTEM_CALL_ARG_1), &ret);
setReturnValue(info, ret);
break;
}
case SYSTEM_CALL_MTHREAD_CREATE:
{
uint32_t arg = SYSTEM_CALL_ARG_2;
Thread* thread = ThreadOperation::create(g_currentThread->process, SYSTEM_CALL_ARG_1);
thread->tinfo->archinfo->ecx = arg;
g_scheduler->Join(thread);
setReturnValue(info, thread->id);
break;
}
case SYSTEM_CALL_MTHREAD_KILL:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::THREAD);
if (object == NULL) {
setReturnValue(info, M_BAD_THREAD_ID);
} else {
Thread* t = (Thread*)object;
intptr_t ret = ThreadOperation::kill(t->id);
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
}
break;
}
case SYSTEM_CALL_CONDITION_CREATE:
{
Condition* condition = new Condition;
ASSERT(condition != NULL);
setReturnValue(info, g_id->allocateID(condition));
break;
}
case SYSTEM_CALL_CONDITION_DESTROY:
{
const intptr_t condition_id = SYSTEM_CALL_ARG_1;
KObject* object = g_id->get(condition_id, g_currentThread->thread, KObject::CONDITION);
if (object == NULL) {
setReturnValue(info, M_BAD_CONDITION_ID);
} else {
Condition* condition = (Condition*)object;
delete condition;
setReturnValue(info, M_OK);
}
break;
}
case SYSTEM_CALL_CONDITION_NOTIFY_ALL:
{
const intptr_t condition_id = SYSTEM_CALL_ARG_1;
KObject* object = g_id->get(condition_id, g_currentThread->thread, KObject::CONDITION);
if (object == NULL) {
setReturnValue(info, M_BAD_CONDITION_ID);
} else {
Condition* condition = (Condition*)object;
intptr_t ret = condition->notifyAll();
setReturnValue(info, M_OK);
ASSERT(ret == Scheduler::YIELD);
g_scheduler->SwitchToNext();
/* Not reached */
}
break;
}
case SYSTEM_CALL_CONDITION_WAIT:
{
const intptr_t condition_id = SYSTEM_CALL_ARG_1;
KObject* condObject = g_id->get(condition_id, g_currentThread->thread, KObject::CONDITION);
const intptr_t mutex_id = SYSTEM_CALL_ARG_2;
KObject* mutexObject = g_id->get(mutex_id, g_currentThread->thread, KObject::KMUTEX);
if (condObject == NULL) {
setReturnValue(info, M_BAD_CONDITION_ID);
} else if (mutexObject == NULL) {
setReturnValue(info, M_BAD_MUTEX_ID);
} else {
Condition* condition = (Condition*)condObject;
KMutex* mutex = (KMutex*)mutexObject;
// unlock and wait should be atomic.
mutex->unlock();
intptr_t ret = condition->wait(g_currentThread->thread);
ASSERT(ret == Scheduler::YIELD);
g_scheduler->SwitchToNext();
setReturnValue(info, ret);
}
break;
}
case SYSTEM_CALL_CONDITION_WAIT_TIMEOUT:
{
const intptr_t condition_id = SYSTEM_CALL_ARG_1;
KObject* condObject = g_id->get(condition_id, g_currentThread->thread, KObject::CONDITION);
const intptr_t mutex_id = SYSTEM_CALL_ARG_2;
KObject* mutexObject = g_id->get(mutex_id, g_currentThread->thread, KObject::KMUTEX);
const intptr_t timeoutTick = SYSTEM_CALL_ARG_3;
if (condObject == NULL) {
setReturnValue(info, M_BAD_CONDITION_ID);
} else if (mutexObject == NULL) {
setReturnValue(info, M_BAD_MUTEX_ID);
} else {
Condition* condition = (Condition*)condObject;
KMutex* mutex = (KMutex*)mutexObject;
// unlock and wait should be atomic.
mutex->unlock();
intptr_t ret = condition->waitTimeout(g_currentThread->thread, timeoutTick);
ASSERT(ret == Scheduler::YIELD);
g_scheduler->SwitchToNext();
setReturnValue(info, ret);
}
break;
}
case SYSTEM_CALL_MUTEX_CREATE:
if (SYSTEM_CALL_ARG_1 == MUTEX_CREATE_NEW) {
intptr_t mutexid = systemcall_mutex_create();
ASSERT(mutexid > 0);
setReturnValue(info, systemcall_mutex_create());
} else {
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::KMUTEX);
if (object == NULL) {
setReturnValue(info, M_BAD_MUTEX_ID);
} else {
KMutex* mutex = (KMutex*)object;
mutex->addRef();
setReturnValue(info, SYSTEM_CALL_ARG_1);
}
}
break;
case SYSTEM_CALL_SEMAPHORE_CREATE:
if (SYSTEM_CALL_ARG_1 == 0) {
KObject* object = g_id->get(SYSTEM_CALL_ARG_2, g_currentThread->thread, KObject::USER_SEMAPHORE);
if (object == NULL) {
setReturnValue(info, M_BAD_SEMAPHORE_ID);
} else {
UserSemaphore* semaphore = (UserSemaphore*)object;
semaphore->addRef();
setReturnValue(info, SYSTEM_CALL_ARG_2);
}
} else {
UserSemaphore* semaphore = new UserSemaphore(SYSTEM_CALL_ARG_1);
setReturnValue(info, g_id->allocateID(semaphore));
}
break;
case SYSTEM_CALL_MUTEX_LOCK:
{
intptr_t ret = systemcall_mutex_lock2(SYSTEM_CALL_ARG_1, SYSTEM_CALL_ARG_2);
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
break;
}
case SYSTEM_CALL_SEMAPHORE_DOWN:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::USER_SEMAPHORE);
if (object == NULL) {
setReturnValue(info, M_BAD_SEMAPHORE_ID);
} else {
UserSemaphore* semaphore = (UserSemaphore*)object;
intptr_t ret = semaphore->down(g_currentThread->thread);
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
}
break;
}
case SYSTEM_CALL_MUTEX_TRY_LOCK:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::KMUTEX);
if (object == NULL) {
setReturnValue(info, M_BAD_MUTEX_ID);
} else {
setReturnValue(info, ((KMutex*)object)->tryLock(g_currentThread->thread));
}
break;
}
case SYSTEM_CALL_SEMAPHORE_TRYDOWN:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::USER_SEMAPHORE);
if (object == NULL) {
setReturnValue(info, M_BAD_SEMAPHORE_ID);
} else {
setReturnValue(info, ((UserSemaphore*)object)->tryDown(g_currentThread->thread));
}
break;
}
case SYSTEM_CALL_MUTEX_UNLOCK:
{
intptr_t ret = systemcall_mutex_unlock2(SYSTEM_CALL_ARG_1);
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
break;
}
case SYSTEM_CALL_SEMAPHORE_UP:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::USER_SEMAPHORE);
if (object == NULL) {
setReturnValue(info, M_BAD_SEMAPHORE_ID);
} else {
UserSemaphore* semaphore = (UserSemaphore*)object;
intptr_t ret = semaphore->up();
if (ret == Scheduler::YIELD) {
g_scheduler->SwitchToNext();
} else {
setReturnValue(info, ret);
}
}
break;
}
case SYSTEM_CALL_MUTEX_DESTROY:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::KMUTEX);
if (object == NULL) {
setReturnValue(info, M_BAD_MUTEX_ID);
} else {
KMutex* mutex = (KMutex*)object;
mutex->releaseRef();
setReturnValue(info, M_OK);
}
break;
}
case SYSTEM_CALL_SEMAPHORE_DESTROY:
{
KObject* object = g_id->get(SYSTEM_CALL_ARG_1, g_currentThread->thread, KObject::USER_SEMAPHORE);
if (object == NULL) {
setReturnValue(info, M_BAD_SEMAPHORE_ID);
} else {
UserSemaphore* semaphore = (UserSemaphore*)object;
semaphore->releaseRef();
setReturnValue(info, 0);
}
break;
}
case SYSTEM_CALL_LOOKUP:
setReturnValue(info, g_scheduler->Lookup((char*)(SYSTEM_CALL_ARG_1)));
break;
case SYSTEM_CALL_GET_VRAM_INFO:
screenInfo = (ScreenInfo*)(SYSTEM_CALL_ARG_1);
screenInfo->vram = (uint32_t)(g_vesaDetail->physBasePtr);
screenInfo->bpp = (uint32_t)(g_vesaDetail->bitsPerPixel);
screenInfo->x = (uint32_t)(g_vesaDetail->xResolution);
screenInfo->y = (uint32_t)(g_vesaDetail->yResolution);
break;
case SYSTEM_CALL_LOAD_PROCESS:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_SET_CURSOR:
g_console->setCursor((int)(SYSTEM_CALL_ARG_1), (int)(SYSTEM_CALL_ARG_2));
break;
case SYSTEM_CALL_GET_CURSOR:
g_console->getCursor((int*)(SYSTEM_CALL_ARG_1), (int*)(SYSTEM_CALL_ARG_2));
break;
case SYSTEM_CALL_FDC_OPEN:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FDC_CLOSE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FDC_READ:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FDC_WRITE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_OPEN:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_READ:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_WRITE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_CREATE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_CLOSE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_GET_PID:
setReturnValue(info, g_currentThread->process->getPid());
break;
case SYSTEM_CALL_GET_TID:
setReturnValue(info, g_currentThread->thread->id);
break;
case SYSTEM_CALL_ARGUMENTS_NUM:
setReturnValue(info, g_currentThread->process->getArguments()->size());
break;
case SYSTEM_CALL_GET_ARGUMENTS:
{
List<char*>* list = g_currentThread->process->getArguments();
char* buf = (char*)(SYSTEM_CALL_ARG_1);
int index = (int)(SYSTEM_CALL_ARG_2);
if (index - 1 > list->size())
{
setReturnValue(info, 1);
break;
}
strncpy(buf, list->get(index), MAX_PROCESS_ARGUMENT_LENGTH);
setReturnValue(info, 0);
break;
}
case SYSTEM_CALL_MTHREAD_YIELD_MESSAGE:
/* message has come. after your last peek or receive */
if (g_currentThread->thread->flags & MEvent::MESSAGE)
{
break;
}
g_scheduler->WaitEvent(g_currentThread->thread, MEvent::MESSAGE);
g_scheduler->SwitchToNext();
/* not reached */
break;
case SYSTEM_CALL_DATE:
{
KDate* date = (KDate*)(SYSTEM_CALL_ARG_1);
RTC::getDate(date);
setReturnValue(info, 0);
break;
}
case SYSTEM_CALL_GET_IO:
info->eflags = info->eflags | 0x3000;
setReturnValue(info, 0);
{
bool isProcessChange = g_scheduler->SetNextThread();
ThreadOperation::switchThread(isProcessChange, 17);
}
break;
case SYSTEM_CALL_FDC_DISK_CHANGED:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_LOOKUP_MAIN_THREAD:
if (SYSTEM_CALL_ARG_1 == NULL)
{
setReturnValue(info, g_scheduler->LookupMainThread(g_currentThread->process));
}
else
{
setReturnValue(info, g_scheduler->LookupMainThread((char*)(SYSTEM_CALL_ARG_1)));
}
break;
case SYSTEM_CALL_MEMORY_MAP_CREATE:
{
static uint32_t sharedId = 0x9000;
sharedId++;
uint32_t size = SYSTEM_CALL_ARG_1;
while (Semaphore::down(&g_semaphore_shared));
bool isOpen = SharedMemoryObject::open(sharedId, size);
Semaphore::up(&g_semaphore_shared);
if (!isOpen)
{
logprintf("%s(%s):%d\n", __FILE__, __func__, __LINE__);
setReturnValue(info, 0);
break;
}
setReturnValue(info, sharedId);
break;
}
case SYSTEM_CALL_MEMORY_MAP_GET_SIZE:
{
uint32_t id = SYSTEM_CALL_ARG_1;
SharedMemoryObject* object = SharedMemoryObject::find(id);
if (object == NULL) {
logprintf("error map_get_size id = %x %s(%s):%d\n", id, __FILE__, __func__, __LINE__);
setReturnValue(info, 0);
} else {
setReturnValue(info, object->getSize());
}
break;
}
case SYSTEM_CALL_MEMORY_MAP_MAP:
{
uint32_t id = SYSTEM_CALL_ARG_1;
uint32_t address = SYSTEM_CALL_ARG_2;
while (Semaphore::down(&g_semaphore_shared));
bool isAttached = SharedMemoryObject::attach(id, g_currentThread->process, address);
Semaphore::up(&g_semaphore_shared);
Semaphore::up(&g_semaphore_shared);
if (!isAttached)
{
setReturnValue(info, 1);
break;
}
break;
}
case SYSTEM_CALL_MEMORY_MAP_UNMAP:
{
uint32_t id = SYSTEM_CALL_ARG_1;
while (Semaphore::down(&g_semaphore_shared));
bool isDetached = SharedMemoryObject::detach(id, g_currentThread->process);
Semaphore::up(&g_semaphore_shared);
setReturnValue(info, isDetached ? 0 : 1);
break;
}
case SYSTEM_CALL_CD:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_DIR_OPEN:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_DIR_CLOSE:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_DIR_READ:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_PS_DUMP_SET:
g_scheduler->SetDump();
break;
case SYSTEM_CALL_PS_DUMP_READ:
{
PsInfo* p = (PsInfo*)(SYSTEM_CALL_ARG_1);
PsInfo* q = g_scheduler->ReadDump();
if (q == NULL)
{
setReturnValue(info, 1);
break;
}
*p = *q;
delete q;
setReturnValue(info, 0);
break;
}
case SYSTEM_CALL_GET_TICK:
setReturnValue(info, g_scheduler->GetTick());
break;
case SYSTEM_CALL_FILE_POSITION:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_FILE_SEEK:
g_console->printf("this systemcall not supported %s:%d\n", __FILE__, __LINE__);
break;
case SYSTEM_CALL_GET_KERNEL_VERSION:
strncpy((char*)SYSTEM_CALL_ARG_1, version, SYSTEM_CALL_ARG_2);
setReturnValue(info, version_number);
break;
case SYSTEM_CALL_LOAD_PROCESS_IMAGE:
{
LoadProcessInfo* p = (LoadProcessInfo*)(SYSTEM_CALL_ARG_1);
setReturnValue(info, Loader::Load(p->image, p->size, p->entrypoint, p->name, true, p->list));
break;
}
case SYSTEM_CALL_CLEAR_SCREEN:
g_console->clearScreen();
setReturnValue(info, 0);
break;
case SYSTEM_CALL_PEEK:
setReturnValue(info, g_messenger->peek(g_currentThread->thread
, (MessageInfo*)(SYSTEM_CALL_ARG_1)
, (int)(SYSTEM_CALL_ARG_2)
, (int)(SYSTEM_CALL_ARG_3)
));
break;
case SYSTEM_CALL_REMOVE_IRQ_RECEIVER:
{
int irq = (int)SYSTEM_CALL_ARG_1;
/* out of range */
if (irq > 15 || irq < 0)
{
setReturnValue(info, 1);
break;
}
g_irqInfo[irq].hasReceiver = false;
g_irqInfo[irq].maskInterrupt = false;
break;
}
case SYSTEM_CALL_SET_IRQ_RECEIVER:
{
int irq = (int)SYSTEM_CALL_ARG_1;
bool maskInterrupt = SYSTEM_CALL_ARG_2 != 0;
/* out of range */
if (irq > 15 || irq < 0)
{
break;
}
g_irqInfo[irq].hasReceiver = true;
g_irqInfo[irq].thread = g_currentThread;
g_irqInfo[irq].maskInterrupt = maskInterrupt;
break;
}
case SYSTEM_CALL_HAS_IRQ_RECEIVER:
{
int irq = (int)SYSTEM_CALL_ARG_1;
/* out of range */
if (irq > 15 || irq < 0)
{
break;
}
setReturnValue(info, g_irqInfo[irq].hasReceiver ? 1 : 0);
break;
}
case SYSTEM_CALL_FREE_PAGES:
{
uint32_t address = SYSTEM_CALL_ARG_1;
uint32_t size = SYSTEM_CALL_ARG_2;
g_page_manager->returnPages(g_currentThread->process->getPageDirectory(), address, size);
break;
}
case SYSTEM_CALL_GET_MEMORY_INFO:
{
MemoryInfo* i = (MemoryInfo*)(SYSTEM_CALL_ARG_1);
i->totalMemoryL = g_total_system_memory;
g_page_manager->getPagePoolInfo(&(i->freePageNum), &(i->totalPageNum), &(i->pageSize));
break;
}
case SYSTEM_CALL_LOG_PRINT:
{
logprintf("%s", (const char*)(SYSTEM_CALL_ARG_1));
break;
}
case SYSTEM_CALL_ALLOCATE_DMA_MEMORY:
{
uint32_t size = SYSTEM_CALL_ARG_1;
setReturnValue(info, (uint32_t)g_page_manager->allocateDMAMemory(g_currentThread->process->getPageDirectory(), size, true));
break;
}
case SYSTEM_CALL_DEALLOCATE_DMA_MEMORY:
g_page_manager->deallocateDMAMemory(g_currentThread->process->getPageDirectory(), SYSTEM_CALL_ARG_1, SYSTEM_CALL_ARG_2);
break;
case SYSTEM_CALL_CHANGE_BASE_PRIORITY:
g_scheduler->ChangeBasePriority(g_currentThread->thread, SYSTEM_CALL_ARG_1);
break;
case SYSTEM_CALL_SET_DLL_SEGMENT_WRITABLE:
g_currentThread->process->getDllSegment()->setWritable(true);
break;
case SYSTEM_CALL_SET_DLL_SEGMENT_NOTSHARED:
g_dllSharedObject->setPageFlag(SYSTEM_CALL_ARG_1, SharedMemoryObject::FLAG_NOT_SHARED);
break;
case SYSTEM_CALL_APM_BIOS:
setReturnValue(info, (uint32_t)apm_bios((uint16_t)SYSTEM_CALL_ARG_1, (apm_bios_regs*)SYSTEM_CALL_ARG_2));
break;
case SYSTEM_CALL_SHUTDOWN:
setReturnValue(info, shutdown(SYSTEM_CALL_ARG_1, SYSTEM_CALL_ARG_2));
break;
case SYSTEM_CALL_RECEIVE_PACKET:
if (g_frames->IsEmpty())
{
setReturnValue(info, 1);
}
else
{
mones::FrameNode* node = (mones::FrameNode*)(g_frames->RemoveNext());
memcpy((uint8_t*)SYSTEM_CALL_ARG_1, node->frame, sizeof(mones::Ether::Frame));
setReturnValue(info, 0);
}
break;
case SYSTEM_CALL_SEND_PACKET:
g_nic->outputFrame((uint8_t*)SYSTEM_CALL_ARG_1, (uint8_t*)SYSTEM_CALL_ARG_2, SYSTEM_CALL_ARG_3, SYSTEM_CALL_ARG_4);
break;
case SYSTEM_CALL_SET_WATCH_POINT:
{
#define B4(a,b,c,d) ((a)*8+(b)*4+(c)*2+(d))
#define B8(a,b,c,d,e,f,g,h) (B4(a,b,c,d)*16+B4(e,f,g,h))
uint32_t address = SYSTEM_CALL_ARG_1;
uint32_t breakflag = SYSTEM_CALL_ARG_2;
uint32_t flag = B8(0,0,0,0,0,0,0,0) << 24
| B8(0,0,0,0,1,1, (breakflag & 2) >> 1 ,(breakflag & 1)) << 16
| B8(0,0,0,0,0,1,0,1) << 8
| B8(0,0,0,0,0,0,1,1);
uint32_t dr0, dr7;
asm volatile("movl %2, %%eax \n"
"movl %%eax, %%dr0 \n"
"movl %3, %%eax \n"
"movl %%eax, %%dr7 \n"
"movl %%dr0, %%eax \n"
"movl %%eax, %0 \n"
"movl %%dr7, %%eax \n"
"movl %%eax, %1 \n"
: "=m"(dr0), "=m"(dr7) : "m"(address), "m"(flag): "eax");
#if 0
g_console->printf("dr0=%x dr7=%x\n", dr0, dr7);
#endif
break;
}
case SYSTEM_CALL_REMOVE_WATCH_POINT:
{
uint32_t flag = B8(0,0,0,0,0,0,0,0) << 24
| B8(0,0,0,0,1,1,1,1) << 16
| B8(0,0,0,0,0,1,0,1) << 8
| B8(0,0,0,0,0,0,0,0);
asm volatile("movl %0, %%eax \n"
"movl %%eax, %%dr0 \n"
: : "m"(flag): "eax");
break;
}
case SYSTEM_CALL_STACKTRACE_ENABLE:
{
uint32_t pid = SYSTEM_CALL_ARG_1;
uint8_t* data = (uint8_t*)SYSTEM_CALL_ARG_2;
uint32_t size = SYSTEM_CALL_ARG_3;
bool res = g_page_manager->enableStackTrace(pid, data, size);
setReturnValue(info, res? 0 : 1);
break;
}
case SYSTEM_CALL_STACKTRACE_DISABLE:
{
uint32_t pid = SYSTEM_CALL_ARG_1;
g_page_manager->disableStackTrace(pid);
setReturnValue(info, 0);
break;
}
case SYSTEM_CALL_STACKTRACE_DUMP:
{
uint32_t pid = SYSTEM_CALL_ARG_1;
g_scheduler->ReserveStackDump(pid);
setReturnValue(info, 0);
break;
}
case SYSTEM_CALL_NOW_IN_NANOSEC:
{
union {
struct {
uint32_t l;
uint32_t h;
} u32;
uint64_t u64;
} n;
n.u64 = RTC::epochNanoseconds();
*((uint32_t*)SYSTEM_CALL_ARG_1) = n.u32.l;
*((uint32_t*)SYSTEM_CALL_ARG_2) = n.u32.h;
break;
}
default:
g_console->printf("syscall:default");
break;
}
return;
}