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
|
#include "i386.h"
#include "Task.h"
#include "Syscall.h"
#include "Console.h"
extern "C" void syscall_entry();
extern "C" void syscall_ISR();
extern volatile RegisterDump* syscallRegDump;
asm(
".globl syscall_ISR \n"
".globl syscallRegDump \n"
"syscallRegDump: \n"
".long 0\n"
"syscall_ISR:\n"
" pusha\n"
" pushw %ds\n"
" pushw %es\n"
" pushw %fs\n"
" pushw %gs\n"
" pushw %ss\n"
" pushw %ss\n"
" pushw %ss\n"
" pushw %ss\n"
" pushw %ss\n"
" popw %ds\n"
" popw %es\n"
" popw %fs\n"
" popw %gs\n"
" mov %esp, syscallRegDump\n"
" call syscall_entry\n"
" popw %gs\n"
" popw %gs\n"
" popw %fs\n"
" popw %es\n"
" popw %ds\n"
" popa\n"
" iret\n"
);
static inline dword CAS(dword* mem, dword newval, dword oldval)
{
dword ret;
asm volatile(
"cmpxchgl %2, %1"
:"=a"(ret), "=m"(*mem)
:"r"(newval), "m"(*mem), "0"(oldval));
return ret;
}
class SpinLock {
public:
SpinLock()
{
}
~SpinLock()
{
unlock();
}
void lock()
{
volatile dword count = 0;
for (;;) {
if (CAS(&m_lock, 1, 0) == 1)
return;
++count;
}
if (count)
kprintf("waited %u in %s\n",count, current->name().characters());
}
void unlock()
{
// barrier();
m_lock = 0;
}
private:
dword m_lock { 0 };
};
class Locker {
public:
explicit Locker(SpinLock& l)
: m_lock(l)
{
m_lock.lock();
}
~Locker()
{
unlock();
}
void unlock()
{
m_lock.unlock();
}
private:
SpinLock& m_lock;
};
namespace Syscall {
static SpinLock* s_lock;
void initialize()
{
s_lock = new SpinLock;
registerUserCallableInterruptHandler(0x80, syscall_ISR);
kprintf("syscall: int 0x80 handler installed\n");
}
DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
{
Locker locker(*s_lock);
switch (function) {
case Syscall::Yield:
yield();
break;
case Syscall::PutCharacter:
Console::the().putChar(arg1 & 0xff);
break;
case Syscall::Sleep:
//kprintf("syscall: sleep(%d)\n", arg1);
current->sys$sleep(arg1);
break;
case Syscall::Spawn:
return current->sys$spawn((const char*)arg1);
case Syscall::PosixOpen:
//kprintf("syscall: open('%s', %u)\n", arg1, arg2);
return current->sys$open((const char*)arg1, (size_t)arg2);
case Syscall::PosixClose:
//kprintf("syscall: close(%d)\n", arg1);
return current->sys$close((int)arg1);
case Syscall::PosixRead:
//kprintf("syscall: read(%d, %p, %u)\n", arg1, arg2, arg3);
return current->sys$read((int)arg1, (void*)arg2, (size_t)arg3);
case Syscall::PosixSeek:
// FIXME: This has the wrong signature, should be like lseek()
kprintf("syscall: seek(%d, %d)\n", arg1, arg2);
return current->sys$seek((int)arg1, (int)arg2);
case Syscall::PosixKill:
kprintf("syscall: kill(%d, %d)\n", arg1, arg2);
return current->sys$kill((pid_t)arg1, (int)arg2);
case Syscall::PosixGetuid:
return current->sys$getuid();
case Syscall::PosixGetgid:
return current->sys$getgid();
case Syscall::PosixGetpid:
return current->sys$getpid();
case Syscall::PosixExit:
cli();
locker.unlock();
current->sys$exit((int)arg1);
ASSERT_NOT_REACHED();
return 0;
default:
kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
break;
}
return 0;
}
}
void syscall_entry()
{
auto& regs = *syscallRegDump;
DWORD function = regs.eax;
DWORD arg1 = regs.edx;
DWORD arg2 = regs.ecx;
DWORD arg3 = regs.ebx;
regs.eax = Syscall::handle(function, arg1, arg2, arg3);
}
|