Repository navigation
Expand file tree
/
Copy pathsyscall.cc
More file actions
218 lines (183 loc) · 7.34 KB
/
Copy pathsyscall.cc
File metadata and controls
218 lines (183 loc) · 7.34 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
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
#include <cxx/units.h>
#include <tar/tar.h>
#include "core/event.h"
#include "core/syscalls.h"
#include "core/thread.h"
#include "libkernel/status.h"
using namespace core::thread;
using TState = core::thread::Thread::State;
using IState = core::thread::Ipc::State;
#define LOG_TRIVIAL_SYSCALL() libkernel::log(LL::SYSCALL, "%s\r\n", cur_thread()->dump())
#define LOG_SYSCALL(fmt, args...) \
libkernel::log(LL::SYSCALL, "%s: " fmt "\r\n", cur_thread()->dump(), args)
//===----------------------------------------------------------------------===//
// Static helper functions
//===----------------------------------------------------------------------===//
[[maybe_unused]]
static constexpr uint8_t clean(uint8_t c) {
if (c >= 32 && c <= 126) return c;
return '.';
}
static void hexdump(uint8_t* buf, unsigned rows) {
#ifdef NDEBUG
(void)buf;
(void)rows;
#else
for (unsigned row = 0; row < rows; row++) {
libkernel::log(
LL::VERBOSE,
"%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X | "
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\r\n",
(int)buf[0], (int)buf[1], (int)buf[2], (int)buf[3], (int)buf[4], (int)buf[5],
(int)buf[6], (int)buf[7], (int)buf[8], (int)buf[9], (int)buf[10], (int)buf[11],
(int)buf[12], (int)buf[13], (int)buf[14], (int)buf[15], clean(buf[0]), clean(buf[1]),
clean(buf[2]), clean(buf[3]), clean(buf[4]), clean(buf[5]), clean(buf[6]),
clean(buf[7]), clean(buf[8]), clean(buf[9]), clean(buf[10]), clean(buf[11]),
clean(buf[12]), clean(buf[13]), clean(buf[14]), clean(buf[15]));
buf += 16;
}
#endif
}
//===----------------------------------------------------------------------===//
// Public API
//===----------------------------------------------------------------------===//
void core::syscalls::Create(int priority, uint64_t entry) {
LOG_SYSCALL("Priority = %d, Entry = 0x%llX", priority, entry);
auto cur = cur_thread();
auto tar_header = reinterpret_cast<tar::PosixHeader const*>(entry);
auto name = tar_header->name;
auto file_ptr = reinterpret_cast<uint8_t*>(entry) + 512;
auto new_tid_r = thread::create(name, file_ptr, priority);
if (new_tid_r.status() == E::INVALID_ARGUMENT) [[unlikely]] {
cur->set_retcode(static_cast<uint64_t>(-1));
} else if (!new_tid_r) [[unlikely]] {
cur->set_retcode(static_cast<uint64_t>(-2));
} else {
cur->set_retcode(new_tid_r.unwrap());
}
}
void core::syscalls::MyTid() {
LOG_TRIVIAL_SYSCALL();
cur_thread()->set_retcode(cur_thread()->user_tid);
}
void core::syscalls::MyParentTid() {
LOG_TRIVIAL_SYSCALL();
cur_thread()->set_retcode(cur_thread()->user_pid);
}
void core::syscalls::Yield() {
LOG_TRIVIAL_SYSCALL();
auto cur = cur_thread();
cur->remove_from_scheduler();
cur->add_to_scheduler();
cur->get_runtime_perf().end_measure();
cur->get_reschedule_perf().start_measure();
}
void core::syscalls::Exit() {
LOG_TRIVIAL_SYSCALL();
cur_thread()->destory();
}
void core::syscalls::Send(tid_t tid, const void* msg, size_t msglen, void* reply, size_t replylen) {
LOG_SYSCALL("Tid = %lld, Msglen = %d, Replylen = %d", tid, msglen, replylen);
// 1a. Cannot send message to self
auto caller = cur_thread();
if (caller->user_tid == tid) {
caller->set_retcode(-1);
return;
}
// 1b. Grab the thread and check if it exists
auto recv_r = get_thread_by_tid(tid);
if (!recv_r) {
caller->set_retcode(static_cast<uint64_t>(-1));
return;
}
auto recv = recv_r.unwrap();
// 1c. Bounds sanity check
if (msglen < 0 || msglen > MaxMessageLength || replylen < 0 || replylen > MaxMessageLength) {
caller->set_retcode(static_cast<uint64_t>(-2));
return;
}
// 2. Copy the message into the current task's IPC buffer
cxx::memcpy(caller->ipc.buf, msg, msglen);
caller->ipc.send.msg_len = msglen;
caller->ipc.send.expected_reply_len = replylen;
caller->ipc.send.user_reply_buf = reinterpret_cast<char*>(reply);
caller->ipc.send.receiver = recv;
hexdump(caller->ipc.buf, cxx::align_up(msglen, 16UL) / 16);
// 3. Enqueue this thread onto the receiver's mailbox
caller->remove_from_scheduler();
caller->state = TState::IPC_WAIT;
caller->ipc.state = IState::AWAIT_RECEIVE;
recv->mailbox.push_back(caller);
// 4. Update the perf monitors
caller->get_ipc_block_perf().start_measure();
caller->get_runtime_perf().end_measure();
caller->get_reschedule_perf().start_measure();
// 5. If the receiver is waiting for a message, wake it up
if (recv->ipc.state == IState::AWAIT_SEND) {
assert(recv->state == TState::IPC_WAIT);
recv->add_to_scheduler();
recv->ipc.state = IState::SENT;
recv->state = TState::IPC_DEFERRED;
recv->get_ipc_block_perf().end_measure();
}
}
void core::syscalls::Receive(tid_t* tid, void* msg, size_t msglen) {
auto caller = cur_thread();
LOG_SYSCALL("Msglen = %d, IsWaiting = %s", msglen, caller->mailbox.empty() ? "Yes" : "No");
// 1. Set up the IPC parameters in the receiver thread
caller->ipc.receive.user_tid_buf = tid;
caller->ipc.receive.user_send_buf = reinterpret_cast<char*>(msg);
caller->ipc.receive.expected_send_len = msglen;
// 2. If there's no sender in the mailbox, put this thread into WAITING
if (caller->mailbox.empty()) {
caller->remove_from_scheduler();
caller->state = TState::IPC_WAIT;
caller->ipc.state = Ipc::State::AWAIT_SEND;
caller->get_ipc_block_perf().start_measure();
caller->get_runtime_perf().end_measure();
caller->get_reschedule_perf().start_measure();
return;
}
// 3. Otherwise, pop 1 thread from the mailbox and copy the message
const auto rc = caller->receive_one_mailbox();
caller->set_retcode(rc);
}
void core::syscalls::Reply(tid_t tid, void* reply, size_t replylen) {
LOG_SYSCALL("Tid = %lld, Replylen = %d", tid, replylen);
// 1. Grab the target (i.e., the sender of the SRR cycle)
auto caller = cur_thread();
auto sender_r = get_thread_by_tid(tid);
if (!sender_r) {
caller->set_retcode(static_cast<uint64_t>(-1));
return;
}
auto sender = sender_r.unwrap();
if (sender->ipc.state != IState::AWAIT_REPLY || sender->ipc.send.receiver != caller) {
caller->set_retcode(static_cast<uint64_t>(-2));
return;
}
assert(sender->state == TState::IPC_WAIT);
// 2. Keep the response in our own buffer because we cannot complete the
// Reply() until the sender is scheduled
int actual_size = cxx::min(replylen, sender->ipc.send.expected_reply_len);
cxx::memcpy(sender->ipc.buf, reply, actual_size);
hexdump((uint8_t*)reply, cxx::align_up(replylen, 16UL) / 16);
// 3. Set the return code for Send() and Reply()
sender->set_retcode(replylen);
caller->set_retcode(actual_size);
// 4. Let the sender know the receiver has replied
sender->ipc.send.msg_len = replylen;
sender->add_to_scheduler();
sender->state = TState::IPC_DEFERRED;
sender->ipc.state = IState::REPLIED;
// 5. Update the perf monitors
sender->get_ipc_block_perf().end_measure();
}
void core::syscalls::AwaitEvent(int event_id) {
LOG_SYSCALL("Event ID = %d", event_id);
auto cur = cur_thread();
auto result = event::install(static_cast<uint32_t>(event_id), cur);
if (result.status() != E::OK) {
cur->set_retcode(static_cast<uint64_t>(-1));
}
}