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
|
#include <arch/x86_64/syscalls/syscalls.hpp>
#include <generic/vfs/vfs.hpp>
#include <arch/x86_64/cpu/data.hpp>
#include <arch/x86_64/scheduling.hpp>
#include <generic/mm/pmm.hpp>
#include <generic/mm/vmm.hpp>
#include <generic/vfs/fd.hpp>
#include <etc/assembly.hpp>
#include <etc/logging.hpp>
#include <drivers/cmos.hpp>
#include <etc/libc.hpp>
#include <drivers/tsc.hpp>
#include <etc/errno.hpp>
#include <drivers/hpet.hpp>
#include <drivers/kvmtimer.hpp>
#include <generic/time.hpp>
#include <generic/vfs/vfs.hpp>
#include <etc/bootloaderinfo.hpp>
#include <generic/locks/spinlock.hpp>
syscall_ret_t sys_orangesigreturn(mcontext_t* mctx, int b, int c, int_frame_t* ctx) {
// is_sig_real
arch::x86_64::process_t* proc = CURRENT_PROC;
mcontext_to_int_frame(mctx,&proc->ctx);
proc->ctx.cs = 0x20 | 3;
proc->ctx.ss = 0x18 | 3;
proc->ctx.rflags |= (1 << 9);
DEBUG(1,"endsig from proc %d rip 0x%p",proc->id,proc->ctx.rip);
schedulingScheduleAndChangeStack(arch::x86_64::cpu::data()->timer_ist_stack,0);
__builtin_unreachable();
}
syscall_ret_t sys_orangedefaulthandler(void* handler) {
return {0,ENOSYS,0};
arch::x86_64::process_t* proc = CURRENT_PROC;
DEBUG(proc->is_debug,"Setup default handler 0x%p from proc %d\n",handler,proc->id);
return {0,0,0};
}
long long sys_pause() {
arch::x86_64::process_t* proc = CURRENT_PROC;
while(1) {
signal_ret();
yield();
}
return 0;
}
long long sys_tgkill(int tgid, int tid, int sig) {
return sys_kill(tid,sig);
}
long long sys_kill(int pid, int sig) {
arch::x86_64::process_t* proc = CURRENT_PROC;
arch::x86_64::process_t* target_proc = arch::x86_64::scheduling::by_pid(pid);
DEBUG(1,"Trying to kill %d with sig %d from proc %d\n",pid,sig,proc->id);
if(!target_proc)
return -ESRCH;
if(pid == 0 && pid < 0)
return 0; // not implemented
if(!(proc->uid == 0 || proc->uid == target_proc->uid))
return -EPERM;
switch(sig) {
case SIGKILL:
target_proc->exit_code = 137;
target_proc->_3rd_kill_lock.nowaitlock();
break;
case SIGTSTP:
case SIGTTIN:
case SIGTTOU:
case SIGSTOP:
if(target_proc->id == proc->id) // shit
yield();
return 0;
default:
pending_signal_t pend_sig;
pend_sig.sig = sig;
target_proc->sig->push(&pend_sig);
DEBUG(1,"%d 0x%p",target_proc->id == proc->id,proc->sig_handlers[sig]);
if(target_proc->id == proc->id) // shit
signal_ret();
return 0;
}
if(target_proc->id == proc->id) // shit
signal_ret();
return 0;
}
long long sys_sigaction(int signum, struct sigaction* hnd, struct sigaction* old, int_frame_t* ctx) {
arch::x86_64::process_t* proc = CURRENT_PROC;
SYSCALL_IS_SAFEZ(hnd,4096);
SYSCALL_IS_SAFEZ(old,4096);
DEBUG(proc->is_debug,"sigaction signum %d from proc %d with 0x%p",signum,proc->id,0);
if(ctx->r10 != sizeof(sigset_t)) {
DEBUG(proc->is_debug,"unsupported len %d for sigset sig %d from proc %d (linux dont support this)",ctx->r10,signum,proc->id);
return -EINVAL;
}
if(signum >= 36)
return -EINVAL;
void* old_hnd = proc->sig_handlers[signum];
void* old_rest = proc->ret_handlers[signum];
if(old) {
memset(old,0,sizeof(struct sigaction));
old->sa_handler = (void (*)(int))old_hnd;
old->sa_restorer = (void (*)())old_rest;
old->sa_flags = proc->sig_flags[signum];
memcpy(&old->sa_mask,&proc->sigsets[signum],sizeof(sigset_t));
}
if(hnd) {
DEBUG(proc->is_debug,"sigaction %d, restorer 0x%p, handler 0x%p",signum,hnd->sa_restorer,hnd->sa_handler);
proc->ret_handlers[signum] = (void*)hnd->sa_restorer;
proc->sig_handlers[signum] = (void*)hnd->sa_handler;
proc->sig_flags[signum] = hnd->sa_flags;
memcpy(&proc->sigsets[signum],&hnd->sa_mask,sizeof(sigset_t));
}
return 0;
}
#define SIG_BLOCK 0 /* Block signals. sigset_t *unewset size_t sigsetsize */
#define SIG_UNBLOCK 1 /* Unblock signals. */
#define SIG_SETMASK 2 /* Set the set of blocked signals. */
long long sys_alarm(int seconds) {
arch::x86_64::process_t* proc = CURRENT_PROC;
std::uint64_t t = proc->next_alarm;
if(seconds != 0) {
proc->next_alarm = drivers::tsc::currentus() + (seconds * (1000*1000));
DEBUG(proc->is_debug,"alarm to %lli (%d seconds)",proc->next_alarm,seconds);
} else {
proc->next_alarm = 0;
}
if(t == 0)
return 0;
else {
t -= drivers::tsc::currentus();
return (t / (1000 * 1000)) < 0 ? 0 : t / (1000 * 1000);
}
return 0;
}
long long sys_sigprocmask(int how, const sigset_t *set, sigset_t *oldset, int_frame_t* ctx) {
arch::x86_64::process_t* proc = CURRENT_PROC;
SYSCALL_IS_SAFEZ((void*)set,4096);
SYSCALL_IS_SAFEZ(oldset,4096);
if(ctx->r10 != sizeof(sigset_t)) {
DEBUG(proc->is_debug,"unsupported sigset len %d when normal is %d (even linux dont support this)",ctx->r10,sizeof(sigset_t));
return 0;
}
DEBUG(proc->is_debug,"sigprocmask 0x%p old 0x%p, size %lli",set,oldset,ctx->r10);
if(oldset) {
memcpy(oldset,&proc->current_sigset,sizeof(sigset_t));
}
if(set) {
memcpy(&proc->current_sigset,set,sizeof(sigset_t));
}
return 0;
}
long long sys_setitimer(int which, itimerval* val, itimerval* old) {
arch::x86_64::process_t* proc = CURRENT_PROC;
SYSCALL_IS_SAFEZ(val,4096);
SYSCALL_IS_SAFEZ(old,4096);
if(!val)
return -EINVAL;
itimerval oldv = {0};
std::uint64_t ns = 0;
switch(which) {
case ITIMER_REAL:
oldv = proc->itimer;
proc->itimer = *val;
arch::x86_64::update_time(&proc->itimer,&proc->next_alarm,1);
break;
case ITIMER_PROF:
oldv = proc->proftimer;
proc->proftimer = *val;
arch::x86_64::update_time(&proc->proftimer,&proc->prof_timer,0);
break;
case ITIMER_VIRTUAL:
oldv = proc->vitimer;
proc->vitimer = *val;
arch::x86_64::update_time(&proc->vitimer,&proc->virt_timer,0);
break;
default:
return -EINVAL;
}
if(old)
*old = oldv;
return 0;
}
long long sys_getitimer(int which, itimerval* val) {
arch::x86_64::process_t* proc = CURRENT_PROC;
SYSCALL_IS_SAFEZ(val,4096);
if(!val)
return -EINVAL;
std::uint64_t ns = 0;
switch(which) {
case ITIMER_REAL:
*val = proc->itimer;
ns = (proc->next_alarm - drivers::tsc::currentus()) * 1000;
val->it_value.tv_sec = ns / 1000000000;
val->it_value.tv_usec = (ns % 1000000000) / 1000;
return 0;
case ITIMER_PROF:
*val = proc->proftimer;
ns = (proc->prof_timer) * 1000;
val->it_value.tv_sec = ns / 1000000000;
val->it_value.tv_usec = (ns % 1000000000) / 1000;
return 0;
case ITIMER_VIRTUAL:
*val = proc->vitimer;
ns = (proc->virt_timer) * 1000;
val->it_value.tv_sec = ns / 1000000000;
val->it_value.tv_usec = (ns % 1000000000) / 1000;
return 0;
default:
return -EINVAL;
}
return -EINVAL;
}
// same as sys_pause but wait for signals
long long sys_sigsuspend(sigset_t* sigset, size_t size) {
if(size != sizeof(sigset_t))
return -EINVAL;
if(!sigset)
return -EINVAL;
SYSCALL_IS_SAFEZ(sigset,4096);
arch::x86_64::process_t* proc = CURRENT_PROC;
while(1) {
signal_ret_sigmask(sigset);
yield();
}
return 0;
}
long long sys_sigaltstack(stack_t* new_stack, stack_t* old) {
arch::x86_64::process_t* proc = CURRENT_PROC;
SYSCALL_IS_SAFEZ(new_stack,4096);
SYSCALL_IS_SAFEZ(old,4096);
if(old) {
*old = proc->altstack;
}
if(new_stack) {
proc->altstack = *new_stack;
}
return 0;
}
|