syscall.c

Commit: 5ef818f0 Author: jokerz Raw Copy
1
/* syscall.c - System call interface for Pactor64 */
2
#include "syscall.h"
3
4
/* Syscall dispatch table */
5
typedef uint64_t (*syscall_fn_t)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
6
static syscall_fn_t syscall_table[MAX_SYSCALL];
7
8
/* Current heap break for brk() */
9
static uint64_t current_brk = 0;
10
11
/* === Individual syscall implementations === */
12
13
static uint64_t sys_read(uint64_t fd, uint64_t buf, uint64_t count,
14
                          uint64_t a3, uint64_t a4) {
15
    (void)buf; (void)count; (void)a3; (void)a4;
16
    if (fd == 0) return 0; /* stdin: no input yet */
17
    return (uint64_t)-1;
18
}
19
20
static uint64_t sys_write(uint64_t fd, uint64_t buf, uint64_t count,
21
                           uint64_t a3, uint64_t a4) {
22
    (void)a3; (void)a4;
23
    if (fd == 1 || fd == 2) {
24
        const char *str = (const char *)(uintptr_t)buf;
25
        for (uint64_t i = 0; i < count; i++) serial_putc(str[i]);
26
        return count;
27
    }
28
    return (uint64_t)-1;
29
}
30
31
static uint64_t sys_open(uint64_t path, uint64_t flags, uint64_t mode,
32
                          uint64_t a3, uint64_t a4) {
33
    (void)path; (void)flags; (void)mode; (void)a3; (void)a4;
34
    kprintf("[syscall] open: not yet implemented\n");
35
    return (uint64_t)-1;
36
}
37
38
static uint64_t sys_close(uint64_t fd, uint64_t a1, uint64_t a2,
39
                           uint64_t a3, uint64_t a4) {
40
    (void)a1; (void)a2; (void)a3; (void)a4;
41
    if (fd <= 2) return 0;
42
    return (uint64_t)-1;
43
}
44
45
static uint64_t sys_exit(uint64_t status, uint64_t a1, uint64_t a2,
46
                          uint64_t a3, uint64_t a4) {
47
    (void)a1; (void)a2; (void)a3; (void)a4;
48
    kprintf("\n[syscall] Process exited with status %d\n", status);
49
    /* Return to kernel shell instead of hanging */
50
    return (uint64_t)-999;
51
    return 0; /* unreachable */
52
}
53
54
static uint64_t sys_brk(uint64_t addr, uint64_t a1, uint64_t a2,
55
                         uint64_t a3, uint64_t a4) {
56
    (void)a1; (void)a2; (void)a3; (void)a4;
57
    if (current_brk == 0) { current_brk = addr; return current_brk; }
58
    if (addr == 0) return current_brk;
59
    if (addr > current_brk) current_brk = addr;
60
    return current_brk;
61
}
62
63
/* Initialize the syscall interface */
64
void syscall_init(void) {
65
    for (int i = 0; i < MAX_SYSCALL; i++) syscall_table[i] = 0;
66
67
    syscall_table[SYS_READ]  = sys_read;
68
    syscall_table[SYS_WRITE] = sys_write;
69
    syscall_table[SYS_OPEN]  = sys_open;
70
    syscall_table[SYS_CLOSE] = sys_close;
71
    syscall_table[SYS_EXIT]  = sys_exit;
72
    syscall_table[SYS_BRK]   = sys_brk;
73
74
    /* STAR: kernel CS=0x08 (SS=0x10), user CS base=0x18
75
     * SYSCALL: CS=STAR[63:48]=0x08, SS=STAR[63:48]+8=0x10
76
     * SYSRET:  CS=STAR[47:32]|3=0x1B, SS=(STAR[47:32]+8)|3=0x23 */
77
    uint64_t star = ((uint64_t)0x0008 << 48) | ((uint64_t)0x0018 << 32);
78
    write_msr(MSR_STAR, star);
79
    write_msr(MSR_LSTAR, (uint64_t)(uintptr_t)syscall_entry);
80
    /* FMASK: clear IF and DF on SYSCALL entry */
81
    write_msr(MSR_FMASK, (1 << 9) | (1 << 10));
82
83
    kprintf("[syscall] Initialized SYSCALL interface\n");
84
    kprintf("[syscall] STAR=0x%x LSTAR=0x%x\n", star, (uint64_t)(uintptr_t)syscall_entry);
85
}
86
87
/* Main syscall dispatcher - called from assembly */
88
uint64_t syscall_dispatch(uint64_t num, uint64_t arg1, uint64_t arg2,
89
                          uint64_t arg3, uint64_t arg4, uint64_t arg5) {
90
    if (num >= MAX_SYSCALL || syscall_table[num] == 0) {
91
        kprintf("[syscall] Unimplemented syscall %d\n", num);
92
        return (uint64_t)-38; /* ENOSYS */
93
    }
94
    return syscall_table[num](arg1, arg2, arg3, arg4, arg5);
95
}
96