SYSCALL naked assembly working
[cortex-from-scratch] / syscall.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/9/20 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * 
9  * |----------------------------|
10  * | SYSTEM CALL        | #     |
11  * |--------------------|-------|
12  * | THEOS_getenv       |       |
13  * | THEOS_killenv      |       |
14  * | THEOS_setenv       |       |
15  * | THEOS_newenv       |       |
16  * | THEOS_cputs        |       |
17  * | THEOS_omnigod      |       |
18  * | THEOS_brk          |       |
19  * | THEOS_time         |       |
20  * | THEOS_magic        |       |
21  * |----------------------------|
22  *
23  * TODO: include in header enum
24  * */
25
26
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30
31 #include <sys/robsys.h>
32 #include <sys/mmap.h>
33
34 #include <lib/stdio.h>
35 #include <lib/string.h>
36 #include <lib/regfunc.h>
37 #include <lib/tinyprintf.h>
38
39
40 /* the function gets called for 
41  * */
42 __attribute__ ((naked))
43 void * __svc_handler__(int x) {
44
45         uint8_t svc_nr;
46
47         asm volatile (
48         "tst lr, #4" "\n\t"
49         "ite eq" "\n\t"
50         "mrseq r0, msp" "\n\t"
51         "mrsne r0, psp" "\n\t"
52         "ldr r0, [r0, #24]" "\n\t"
53         "ldrb %0, [r0, #-2]" : "=r" (svc_nr) );
54
55         printf("SYSTEM CALL NR: %d", svc_nr);
56
57         for(;;);
58
59         volatile uint32_t * sp;
60         
61         asm volatile (
62         "tst lr, #4" "\n\t" 
63         "ite eq" "\n\t"
64         "mrseq %0, msp" "\n\t" 
65         "mrsne r0, psp" : "=r" (sp) );
66
67
68         for (int i = 0; (sp + i) < 0x20010000; i++) {
69                 printf("ADDRESS: %p, VALUE: %x\n", (sp + i), *(sp + i));
70         }
71
72         for (;;);       
73         //asm ("mov %0, pc" : "=r" (link_register));
74         //printf("%x\n", link_register);
75         
76         volatile uint32_t * svc_number = (uint32_t *) 0x20022222;
77         *svc_number = sp[-6];
78         //              = sp[6];
79         //uint8_t tmp  = link_register[-2];
80         printf("SVC nr: %x", *svc_number);
81         //printf("%d, %d, %d, %d, %d, %d\n", msp[6], msp[0], msp[1], msp[2], msp[3], msp[4]);
82         
83         for (;;);
84         //return NULL;  
85         return 0;
86
87 }
88
89 void syscall(unsigned int * args) {
90
91         uint32_t svc_number = 99;
92         printf("SYSCALL NR: %x", svc_number);
93
94         for(;;);
95         /* switch(SYSCALL_NO) {
96                 case THEOS_cputs:
97                         kernel_cputs(a1, a2);
98                         break;
99                 default:
100                         for (;;);
101         } */
102
103 }
104
105 void syscall_init() {
106
107
108         /* SVC is located at position 11 in the interrupt vector table  */
109 //      extern void * _syscall;
110         extern void * hardfault;
111
112         ivt_set_gate(11, __svc_handler__, 0);
113
114
115 }
116
117 static void kernel_cputs(char * s, size_t l) {
118
119         // TODO
120 }
121
122
123 void kernel_omnigod() {
124
125         /* */
126
127 }
128
129
130