mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-01 23:18:21 +00:00
syscalls: syscall logging
This commit is contained in:
+1
-1
@@ -10,7 +10,7 @@ LDFLAGS=-ffreestanding -nostdlib -lgcc -Wl,-T,"$(ARCHDIR)/kernel.ld" -g
|
|||||||
PRINTFOPTS=-DPRINTF_DISABLE_SUPPORT_FLOAT
|
PRINTFOPTS=-DPRINTF_DISABLE_SUPPORT_FLOAT
|
||||||
# flanterm includes break if the headers are moved into the include dir in the tree
|
# flanterm includes break if the headers are moved into the include dir in the tree
|
||||||
FLANTERMINCDIR=$(shell pwd)/flanterm
|
FLANTERMINCDIR=$(shell pwd)/flanterm
|
||||||
CFLAGS=-g -ffreestanding -mcmodel=kernel -O2 -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -nostdlib -Wall -I "${INCDIR}" -I "$(ARCHINCDIR)" -I "$(FLANTERMINCDIR)" $(PRINTFOPTS)
|
CFLAGS=-g -ffreestanding -mcmodel=kernel -O2 -mno-red-zone -mgeneral-regs-only -mno-mmx -mno-sse -mno-sse2 -nostdlib -Wall -I "${INCDIR}" -I "$(ARCHINCDIR)" -I "$(FLANTERMINCDIR)" $(PRINTFOPTS)
|
||||||
ASFLAGS=-felf64
|
ASFLAGS=-felf64
|
||||||
|
|
||||||
astral: $(OBJS)
|
astral: $(OBJS)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ extern syscall_dup2
|
|||||||
extern syscall_dup3
|
extern syscall_dup3
|
||||||
extern syscall_fcntl
|
extern syscall_fcntl
|
||||||
extern syscall_chdir
|
extern syscall_chdir
|
||||||
|
extern syscall_pipe2
|
||||||
syscalltab:
|
syscalltab:
|
||||||
dq syscall_print
|
dq syscall_print
|
||||||
dq syscall_mmap
|
dq syscall_mmap
|
||||||
@@ -44,7 +45,8 @@ dq syscall_dup2
|
|||||||
dq syscall_dup3
|
dq syscall_dup3
|
||||||
dq syscall_fcntl
|
dq syscall_fcntl
|
||||||
dq syscall_chdir
|
dq syscall_chdir
|
||||||
syscallcount equ 22
|
dq syscall_pipe2
|
||||||
|
syscallcount equ 23
|
||||||
section .text
|
section .text
|
||||||
global arch_syscall_entry
|
global arch_syscall_entry
|
||||||
; on entry:
|
; on entry:
|
||||||
@@ -110,6 +112,14 @@ arch_syscall_entry:
|
|||||||
mov rcx, rdx
|
mov rcx, rdx
|
||||||
mov rdx, rsi
|
mov rdx, rsi
|
||||||
mov rsi, rdi
|
mov rsi, rdi
|
||||||
|
|
||||||
|
; call logging function
|
||||||
|
|
||||||
|
mov rdi, rax
|
||||||
|
extern arch_syscall_log
|
||||||
|
call arch_syscall_log ; compiled with __attribute__((no_caller_saved_registers))
|
||||||
|
|
||||||
|
; prepare context argument
|
||||||
mov rdi, rsp
|
mov rdi, rsp
|
||||||
add rdi, 8 ; context pointer is after r9 argument
|
add rdi, 8 ; context pointer is after r9 argument
|
||||||
sti
|
sti
|
||||||
@@ -123,6 +133,15 @@ arch_syscall_entry:
|
|||||||
call syscall_invalid
|
call syscall_invalid
|
||||||
.return:
|
.return:
|
||||||
cli
|
cli
|
||||||
|
|
||||||
|
; call return logging function
|
||||||
|
|
||||||
|
mov rdi, rax
|
||||||
|
mov rsi, rdx
|
||||||
|
extern arch_syscall_log_return
|
||||||
|
call arch_syscall_log_return ; compiled with __attribute__((no_caller_saved_registers))
|
||||||
|
|
||||||
|
|
||||||
; restore context
|
; restore context
|
||||||
add rsp, 32 ; r9 argument, cr2, gs, and fs are not popped.
|
add rsp, 32 ; r9 argument, cr2, gs, and fs are not popped.
|
||||||
pop rbx
|
pop rbx
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
#include <logging.h>
|
||||||
|
#include <arch/e9.h>
|
||||||
|
|
||||||
|
#define SYSCALL_COUNT 23
|
||||||
|
#define LOGSTR(x) arch_e9_puts(x)
|
||||||
|
|
||||||
|
static char *name[] = {
|
||||||
|
"print",
|
||||||
|
"mmap",
|
||||||
|
"openat",
|
||||||
|
"read",
|
||||||
|
"seek",
|
||||||
|
"close",
|
||||||
|
"archctl",
|
||||||
|
"write",
|
||||||
|
"getpid",
|
||||||
|
"fstat",
|
||||||
|
"fstatat",
|
||||||
|
"fork",
|
||||||
|
"execve",
|
||||||
|
"exit",
|
||||||
|
"waitpid",
|
||||||
|
"munmap",
|
||||||
|
"getdents",
|
||||||
|
"dup",
|
||||||
|
"dup2",
|
||||||
|
"dup3",
|
||||||
|
"fcntl",
|
||||||
|
"chdir",
|
||||||
|
"pipe2"
|
||||||
|
};
|
||||||
|
|
||||||
|
static char *args[] = {
|
||||||
|
"N/A", // print will not have its argument printed
|
||||||
|
"hint %p length %lu prot %d flags %d fd %d offset %ld", // mmap
|
||||||
|
"dirfd %d path %s flags %d mode %o", // openat
|
||||||
|
"fd %d buffer %p size %lu", // read
|
||||||
|
"fd %d offset %ld whence %d", // seek
|
||||||
|
"fd %d", // close
|
||||||
|
"func %d arg %p", // archctl
|
||||||
|
"fd %d buffer %p size %lu", // write
|
||||||
|
"N/A", // getpid
|
||||||
|
"fd %d ustat %p", // fstat
|
||||||
|
"dirfd %d path %s ustat %p flags %d", // fstatat
|
||||||
|
"N/A", // fork
|
||||||
|
"path %s uargv %p uenvp %p", // execve
|
||||||
|
"status %d", // exit
|
||||||
|
"pid %d statusp %p options %d", // waitpid
|
||||||
|
"addr %p length %lu", // munmap
|
||||||
|
"dirfd %d buffer %p readmax %lu", // getdents
|
||||||
|
"oldfd %d", // dup
|
||||||
|
"oldfd %d newfd %d", // dup2
|
||||||
|
"oldfd %d newfd %d flags %d", //dup3
|
||||||
|
"fd %d cmd %d arg %lu", // fcntl
|
||||||
|
"path %s", // chdir
|
||||||
|
"flags %d", // pipe2
|
||||||
|
};
|
||||||
|
|
||||||
|
__attribute__((no_caller_saved_registers)) void arch_syscall_log(int syscall, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6) {
|
||||||
|
char argbuff[768];
|
||||||
|
char printbuff[1024];
|
||||||
|
|
||||||
|
thread_t *thread = _cpu()->thread;
|
||||||
|
proc_t *proc = thread->proc;
|
||||||
|
|
||||||
|
snprintf(argbuff, 768, syscall < SYSCALL_COUNT ? args[syscall] : "N/A", a1, a2, a3, a4, a5, a6);
|
||||||
|
snprintf(printbuff, 1024, "\e[92msyscall: pid %d tid %d: %s: %s\n\e[0m", proc->pid, thread->tid, syscall < SYSCALL_COUNT ? name[syscall] : "invalid syscall", argbuff);
|
||||||
|
|
||||||
|
LOGSTR(printbuff);
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((no_caller_saved_registers)) void arch_syscall_log_return(int syscall, uint64_t ret, uint64_t errno) {
|
||||||
|
char printbuff[1024];
|
||||||
|
|
||||||
|
thread_t *thread = _cpu()->thread;
|
||||||
|
proc_t *proc = thread->proc;
|
||||||
|
|
||||||
|
snprintf(printbuff, 1024, "\e[94msyscall return: pid %d tid %d: %lu %lu\n\e[0m", proc->pid, thread->tid, ret, errno);
|
||||||
|
LOGSTR(printbuff);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user