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
|
#include <tce/syscall.h>
#include <tce/Object.h>
#include <stdlib.h>
#include <sched.h>
static size_t call(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f) {
size_t ret;
asm volatile("int $64" : "=a"(ret) : "a"(a), "b"(b), "c"(c), "d"(d), "S"(e), "D"(f));
return ret;
}
// Standard syscalls
void thread_exit() {
call(0, 1, 0, 0, 0, 0);
}
void schedule() {
call(0, 2, 0,0, 0, 0);
}
void thread_sleep(int time) {
call(0, 3, time, 0, 0, 0);
}
void process_exit(int retval) {
call(0, 4, retval, 0, 0, 0);
}
void printk(char* str) {
call(0, 5, (unsigned)str, 0, 0, 0);
}
//THREAD CREATION
struct thread_start_data {
void (*entry)(void*);
void *data;
void *stack;
};
void *_stack_to_free = 0;
uint32_t _stack_freeing_mutex = MUTEX_UNLOCKED;
void thread_start(void *data) {
struct thread_start_data *tsd = data;
tsd->entry(tsd->data);
mutex_lock(&_stack_freeing_mutex);
if (_stack_to_free != 0) free(_stack_to_free);
_stack_to_free = tsd->stack;
asm volatile("movl %0, (_stack_freeing_mutex); int $64;" ::
"a"(0), "b"(1), "r"(MUTEX_UNLOCKED));
}
void thread_new(void (*entry)(void*), void *data) {
struct thread_start_data *tsd = malloc(sizeof(struct thread_start_data));
tsd->entry = entry;
tsd->data = data;
tsd->stack = malloc(NEW_STACK_SIZE);
call(0, 6, (unsigned)thread_start, (unsigned)tsd, (unsigned)(tsd->stack + NEW_STACK_SIZE), 0);
}
void irq_wait(int number) {
call(0, 7, number, 0, 0, 0);
}
int proc_priv() {
return call(0, 8, 0, 0, 0, 0);
}
void* sbrk(size_t s) {
return (void*)call(0, 9, s, 0, 0, 0);
}
void brk(void* ptr) {
return call (0, 10, ptr, 0, 0, 0);
}
// Has to do with objects
Object open(char* name) {
return call(0, 20, name, 0, 0, 0);
}
Object open_relative(char* name, Object parent) {
return call(0, 21, name, parent, 0, 0);
}
void close(Object o) {
call(0, 22, o, 0, 0, 0);
}
int get_methods(char *iface, int* whereto) {
call(0, 23, iface, whereto, 0, 0);
}
int Call(int method, Object object, size_t a, size_t b, size_t c, size_t d) {
return call(object, method, a, b, c, d);
}
|