mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
[libc] Created syscall trampolines in libc
Using syscalls.inc (moved to src/include) generate trampoline functions for directly calling syscalls with libc functions.
This commit is contained in:
@@ -139,6 +139,7 @@ modules:
|
|||||||
#- LACKS_TIME_H
|
#- LACKS_TIME_H
|
||||||
source:
|
source:
|
||||||
- src/libraries/libc/arch/x86_64/_Exit.s
|
- src/libraries/libc/arch/x86_64/_Exit.s
|
||||||
|
- src/libraries/libc/arch/x86_64/syscalls.s
|
||||||
- src/libraries/libc/ctype/isalnum.c
|
- src/libraries/libc/ctype/isalnum.c
|
||||||
- src/libraries/libc/ctype/isalpha.c
|
- src/libraries/libc/ctype/isalpha.c
|
||||||
- src/libraries/libc/ctype/isblank.c
|
- src/libraries/libc/ctype/isblank.c
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ asflags = $
|
|||||||
-DVERSION_MAJOR={{ version_major }} $
|
-DVERSION_MAJOR={{ version_major }} $
|
||||||
-DVERSION_MINOR={{ version_minor }} $
|
-DVERSION_MINOR={{ version_minor }} $
|
||||||
-DVERSION_PATCH={{ version_patch }} $
|
-DVERSION_PATCH={{ version_patch }} $
|
||||||
-DVERSION_GITSHA=0x{{ version_sha }}
|
-DVERSION_GITSHA=0x{{ version_sha }} $
|
||||||
|
-I${srcroot}/src/include
|
||||||
|
|
||||||
cflags = -std=c11
|
cflags = -std=c11
|
||||||
cxxflags = -std=c++14
|
cxxflags = -std=c++14
|
||||||
|
|||||||
@@ -5,53 +5,38 @@
|
|||||||
#include "j6/errors.h"
|
#include "j6/errors.h"
|
||||||
#include "j6/signals.h"
|
#include "j6/signals.h"
|
||||||
|
|
||||||
|
#include <j6libc/syscalls.h>
|
||||||
|
|
||||||
const char message[] = "Hello! This is a message being sent over a channel!";
|
const char message[] = "Hello! This is a message being sent over a channel!";
|
||||||
char inbuf[1024];
|
char inbuf[1024];
|
||||||
j6_handle_t chan = j6_handle_invalid;
|
j6_handle_t chan = j6_handle_invalid;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
j6_status_t system_log(const char *msg);
|
|
||||||
|
|
||||||
j6_status_t object_wait(j6_handle_t obj, j6_signal_t sig, j6_signal_t *out);
|
|
||||||
j6_status_t object_signal(j6_handle_t obj, j6_signal_t sig);
|
|
||||||
|
|
||||||
j6_status_t process_koid(j6_koid_t *koid);
|
|
||||||
|
|
||||||
j6_status_t thread_koid(j6_koid_t *koid);
|
|
||||||
j6_status_t thread_create(void (*koid)(), j6_handle_t *handle);
|
|
||||||
j6_status_t thread_sleep(uint64_t til);
|
|
||||||
j6_status_t thread_exit(int64_t status);
|
|
||||||
|
|
||||||
j6_status_t channel_create(j6_handle_t *handle);
|
|
||||||
j6_status_t channel_close(j6_handle_t handle);
|
|
||||||
j6_status_t channel_send(j6_handle_t handle, size_t len, void *data);
|
|
||||||
j6_status_t channel_receive(j6_handle_t handle, size_t *len, void *data);
|
|
||||||
|
|
||||||
int main(int, const char **);
|
int main(int, const char **);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
thread_proc()
|
thread_proc()
|
||||||
{
|
{
|
||||||
system_log("sub thread starting");
|
_syscall_system_log("sub thread starting");
|
||||||
|
|
||||||
j6_status_t result = object_signal(chan, j6_signal_user0);
|
j6_status_t result = _syscall_object_signal(chan, j6_signal_user0);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
thread_exit(result);
|
_syscall_thread_exit(result);
|
||||||
|
|
||||||
system_log("sub thread signaled user0");
|
_syscall_system_log("sub thread signaled user0");
|
||||||
|
|
||||||
result = channel_send(chan, sizeof(message), (void*)message);
|
result = _syscall_channel_send(chan, sizeof(message), (void*)message);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
thread_exit(result);
|
_syscall_thread_exit(result);
|
||||||
|
|
||||||
system_log("sub thread sent on channel");
|
_syscall_system_log("sub thread sent on channel");
|
||||||
|
|
||||||
for (int i = 1; i < 5; ++i)
|
for (int i = 1; i < 5; ++i)
|
||||||
thread_sleep(i*10);
|
_syscall_thread_sleep(i*10);
|
||||||
|
|
||||||
system_log("sub thread exiting");
|
_syscall_system_log("sub thread exiting");
|
||||||
thread_exit(0);
|
_syscall_thread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -60,32 +45,32 @@ main(int argc, const char **argv)
|
|||||||
j6_handle_t child = j6_handle_invalid;
|
j6_handle_t child = j6_handle_invalid;
|
||||||
j6_signal_t out = 0;
|
j6_signal_t out = 0;
|
||||||
|
|
||||||
system_log("main thread starting");
|
_syscall_system_log("main thread starting");
|
||||||
|
|
||||||
j6_status_t result = channel_create(&chan);
|
j6_status_t result = _syscall_channel_create(&chan);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
system_log("main thread created channel");
|
_syscall_system_log("main thread created channel");
|
||||||
|
|
||||||
result = thread_create(&thread_proc, &child);
|
result = _syscall_thread_create(reinterpret_cast<void*>(&thread_proc), &child);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
system_log("main thread waiting on user0");
|
_syscall_system_log("main thread waiting on user0");
|
||||||
|
|
||||||
result = object_wait(chan, j6_signal_user0, &out);
|
result = _syscall_object_wait(chan, j6_signal_user0, &out);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
system_log("main thread waiting on can_recv");
|
_syscall_system_log("main thread waiting on can_recv");
|
||||||
|
|
||||||
result = object_wait(chan, j6_signal_channel_can_recv, &out);
|
result = _syscall_object_wait(chan, j6_signal_channel_can_recv, &out);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
size_t len = sizeof(inbuf);
|
size_t len = sizeof(inbuf);
|
||||||
result = channel_receive(chan, &len, (void*)inbuf);
|
result = _syscall_channel_receive(chan, &len, (void*)inbuf);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
@@ -94,21 +79,21 @@ main(int argc, const char **argv)
|
|||||||
return 127;
|
return 127;
|
||||||
}
|
}
|
||||||
|
|
||||||
system_log("main thread received on channel");
|
_syscall_system_log("main thread received on channel");
|
||||||
|
|
||||||
system_log("main thread waiting on child");
|
_syscall_system_log("main thread waiting on child");
|
||||||
|
|
||||||
result = object_wait(child, -1ull, &out);
|
result = _syscall_object_wait(child, -1ull, &out);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
result = channel_close(chan);
|
result = _syscall_channel_close(chan);
|
||||||
if (result != j6_status_ok)
|
if (result != j6_status_ok)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
system_log("main thread closed channel");
|
_syscall_system_log("main thread closed channel");
|
||||||
|
|
||||||
system_log("main thread done, exiting");
|
_syscall_system_log("main thread done, exiting");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,35 +7,6 @@ extern exit
|
|||||||
|
|
||||||
section .text
|
section .text
|
||||||
|
|
||||||
%macro SYSCALL 2
|
|
||||||
global %1
|
|
||||||
%1:
|
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
; address of args should already be in rdi, etc
|
|
||||||
mov rax, %2
|
|
||||||
syscall
|
|
||||||
; result is now already in rax, so just return
|
|
||||||
|
|
||||||
pop rbp
|
|
||||||
ret
|
|
||||||
%endmacro
|
|
||||||
|
|
||||||
SYSCALL system_log, 0x00
|
|
||||||
SYSCALL object_wait, 0x09
|
|
||||||
SYSCALL object_signal, 0x0a
|
|
||||||
SYSCALL process_koid, 0x10
|
|
||||||
SYSCALL thread_koid, 0x18
|
|
||||||
SYSCALL thread_create, 0x19
|
|
||||||
SYSCALL thread_exit, 0x1a
|
|
||||||
SYSCALL thread_pause, 0x1b
|
|
||||||
SYSCALL thread_sleep, 0x1c
|
|
||||||
SYSCALL channel_create, 0x20
|
|
||||||
SYSCALL channel_close, 0x21
|
|
||||||
SYSCALL channel_send, 0x22
|
|
||||||
SYSCALL channel_receive, 0x23
|
|
||||||
|
|
||||||
global _start
|
global _start
|
||||||
_start:
|
_start:
|
||||||
xor rbp, rbp ; Sentinel rbp
|
xor rbp, rbp ; Sentinel rbp
|
||||||
|
|||||||
21
src/libraries/libc/arch/x86_64/syscalls.s
Normal file
21
src/libraries/libc/arch/x86_64/syscalls.s
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
%macro SYSCALL 2
|
||||||
|
global _syscall_%1
|
||||||
|
_syscall_%1:
|
||||||
|
push rbp
|
||||||
|
mov rbp, rsp
|
||||||
|
|
||||||
|
; address of args should already be in rdi, etc
|
||||||
|
mov rax, %2
|
||||||
|
syscall
|
||||||
|
; result is now already in rax, so just return
|
||||||
|
|
||||||
|
pop rbp
|
||||||
|
ret
|
||||||
|
%endmacro
|
||||||
|
|
||||||
|
%define SYSCALL(n, name) SYSCALL name, n
|
||||||
|
%define SYSCALL(n, name, a) SYSCALL name, n
|
||||||
|
%define SYSCALL(n, name, a, b) SYSCALL name, n
|
||||||
|
%define SYSCALL(n, name, a, b, c) SYSCALL name, n
|
||||||
|
|
||||||
|
%include "syscalls.inc"
|
||||||
13
src/libraries/libc/include/j6libc/syscalls.h
Normal file
13
src/libraries/libc/include/j6libc/syscalls.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define SYSCALL(n, name, ...) j6_status_t _syscall_ ## name (__VA_ARGS__);
|
||||||
|
#include "syscalls.inc"
|
||||||
|
#undef SYSCALL
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user