-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscall.c
More file actions
175 lines (145 loc) · 4.13 KB
/
syscall.c
File metadata and controls
175 lines (145 loc) · 4.13 KB
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
#include "syscall.h"
#include "syscall_internal.h"
#include "vale_os.h"
#include <stdarg.h>
#include "context_switch.h"
#include "tcb.h"
#include <avr/interrupt.h>
extern tcb_t *current_tcb;
void (*syscall_table[SYSCALL_COUNT])(void);
syscall_tcb_t syscall_tcb;
void init_syscall_tcb(void);
void reset_syscall_tcb(void)
{
init_syscall_tcb();
}
void syscall_func(void)
{
syscall_table[current_tcb->syscall_id]();
reset_syscall_tcb();
setNewContext(current_tcb);
}
void init_syscall_tcb(void)
{
uint8_t *stack_ptr = syscall_tcb.stack + INTERRUPT_STACK_SIZE - 1;
/** write the return address of the function being called (the trampoline)*/
*stack_ptr-- = (uint8_t)((uint16_t)syscall_func & 0xFF);
*stack_ptr-- = (uint8_t)(((uint16_t)syscall_func >> 8) & 0xFF);
// this is for the devices with more than 64K of program memory
#ifdef __AVR_3_BYTE_PC__
*stack_ptr-- = 0;
#endif
/**
* Store starting register values for R2-R17, R28-R29
*/
*stack_ptr-- = 0x00; /* R2 */
*stack_ptr-- = 0x00; /* R3 */
*stack_ptr-- = 0x00; /* R4 */
*stack_ptr-- = 0x00; /* R5 */
*stack_ptr-- = 0x00; /* R6 */
*stack_ptr-- = 0x00; /* R7 */
*stack_ptr-- = 0x00; /* R8 */
*stack_ptr-- = 0x00; /* R9 */
*stack_ptr-- = 0x00; /* R10 */
*stack_ptr-- = 0x00; /* R11 */
*stack_ptr-- = 0x00; /* R12 */
*stack_ptr-- = 0x00; /* R13 */
*stack_ptr-- = 0x00; /* R14 */
*stack_ptr-- = 0x00; /* R15 */
*stack_ptr-- = 0x00; /* R16 */
*stack_ptr-- = 0x00; /* R17 */
*stack_ptr-- = 0x00; /* R28 */
*stack_ptr-- = 0x00; /* R29 */
/**
* On devices with large program space we also context switch RAMPZ, EIND.
*/
#ifdef __AVR_3_BYTE_PC__
*stack_ptr-- = 0x00; /* RAMPZ */
*stack_ptr-- = 0x00; /* EIND */
#endif
syscall_tcb.sp_save_ptr = stack_ptr;
}
void syscall_init(void)
{
syscall_table[SYSCALL_GETPID] = internal_syscall_getpid;
syscall_table[SYSCALL_SPAWN] = internal_syscall_spawn;
syscall_table[SYSCALL_GETPARENTPID] = internal_syscall_getparentpid;
syscall_table[SYSCALL_EXIT] = internal_syscall_exit;
syscall_table[SYSCALL_WAIT] = internal_syscall_wait;
syscall_table[SYSCALL_SLEEP] = internal_syscall_sleep;
syscall_table[SYSCALL_SEM_CREATE] = internal_syscall_sem_create;
syscall_table[SYSCALL_SEM_CLOSE] = internal_syscall_sem_close;
syscall_table[SYSCALL_SEM_WAIT] = internal_syscall_sem_wait;
syscall_table[SYSCALL_SEM_POST] = internal_syscall_sem_post;
syscall_table[SYSCALL_SEM_OPEN] = internal_syscall_sem_open;
syscall_table[SYSCALL_SEM_UNLINK] = internal_syscall_sem_unlink;
init_syscall_tcb();
}
int syscall(int syscall_id, int arg_count, ...)
{
current_tcb->syscall_id = syscall_id;
va_list args;
va_start(args, arg_count);
for (int i = 0; i < arg_count; i++)
{
current_tcb->syscall_args[i] = va_arg(args, int);
}
va_end(args);
cli();
contextSwitch(current_tcb, &syscall_tcb);
sei();
current_tcb->syscall_id = -1;
return current_tcb->syscall_result;
}
int8_t syscall_getpid(void)
{
return (int8_t)syscall(SYSCALL_GETPID, 0);
}
int8_t syscall_spawn(void (*entry_point)(void))
{
return (int8_t)syscall(SYSCALL_SPAWN, 1, entry_point);
}
int8_t syscall_getparentpid(void)
{
return (int8_t)syscall(SYSCALL_GETPARENTPID, 0);
}
void syscall_exit(int exit_code)
{
syscall(SYSCALL_EXIT, 1, exit_code);
}
int syscall_wait(int8_t pid, int *exit_code)
{
return syscall(SYSCALL_WAIT, 2, pid, exit_code);
}
int syscall_wait_any(int *exit_code)
{
return syscall_wait(0, exit_code);
}
void syscall_sleep(int ms)
{
syscall(SYSCALL_SLEEP, 1, ms);
}
int syscall_sem_create(int id, int initial_count)
{
return syscall(SYSCALL_SEM_CREATE, 2, id, initial_count);
}
int syscall_sem_close(int id)
{
return syscall(SYSCALL_SEM_CLOSE, 1, id);
}
int syscall_sem_wait(int id)
{
return syscall(SYSCALL_SEM_WAIT, 1, id);
}
int syscall_sem_post(int id)
{
return syscall(SYSCALL_SEM_POST, 1, id);
}
int syscall_sem_open(int id)
{
return syscall(SYSCALL_SEM_OPEN, 1, id);
}
int syscall_sem_unlink(int id)
{
return syscall(SYSCALL_SEM_UNLINK, 1, id);
}