summaryrefslogtreecommitdiff
path: root/src/user/include/pland/syscall.h
blob: 01f71515bdb8808e88d98da7e8947c07c726ac75 (plain) (blame)
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
#ifndef PLAND_SYSCALL_H
#define PLAND_SYSCALL_H

#include <stdint.h>

typedef uint32_t _file_handle_t;
typedef uint32_t _task_handle_t;
typedef uint32_t _drive_number_t;
typedef enum {
  _KEY_BACKSPACE = '\b',
  _KEY_RETURN    = '\n',
  //etc.

  _KEY_LSHIFT = 0x00000100,
  _KEY_RSHIFT = 0x00000200,
  _KEY_CAPS   = 0x00000400,
  _KEY_INSERT = 0x00000800,
  _KEY_NUM    = 0x00001000,
  _KEY_SCROLL = 0x00002000,
  _KEY_LALT   = 0x00004000,
  _KEY_RALT   = 0x00008000,
  _KEY_LCTRL  = 0x00010000,
  _KEY_RCTRL  = 0x00020000,
  _KEY_LMETA  = 0x00040000,
  _KEY_RMETA  = 0x00080000,

  _KEY_SHIFT  = 0x00000300,
  _KEY_SCAPS  = 0x00000700,
  _KEY_ALT    = 0x0000c000,
  _KEY_CTRL   = 0x00030000,
  _KEY_META   = 0x000c0000,
} _key_code_t;

enum _scn {
  _SCN_OPEN_FILE,
  _SCN_CLOSE_FILE,
  _SCN_FILE_READ,
  _SCN_FILE_SIZE,
  _SCN_START_TASK,
  _SCN_LOG_STRING,
  _SCN_GET_KEY,
  _SCN_ALLOCATE_RAM
};

static inline uint32_t _sc0(enum _scn eax) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax) : "ecx", "edx");
  return out;
}

static inline uint32_t _sc1(enum _scn eax, uint32_t ebx) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax), "b" (ebx) : "ecx", "edx");
  return out;
}

static inline uint32_t _sc2(enum _scn eax, uint32_t ebx, uint32_t ecx) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx) : "edx");
  return out;
}

static inline uint32_t _sc3(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx));
  return out;
}

static inline uint32_t _sc4(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi));
  return out;
}

static inline uint32_t _sc5(enum _scn eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi) {
  volatile uint32_t out;
  asm (
    "int $0x30"
  : "=a" (out) : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));
  return out;
}

static inline void _yield_task() {
  asm (
    "int $0x39"
  );
}

__attribute__ ((noreturn))
static inline void _exit_task() {
  asm (
    "int $0x38"
  );
  __builtin_unreachable();
}

static inline _file_handle_t _open_file(_drive_number_t drive_number, const char *path) {
  return _sc2(_SCN_OPEN_FILE, drive_number, (uint32_t)path);
}

static inline void _close_file(_file_handle_t handle) {
  _sc1(_SCN_CLOSE_FILE, handle);
}

static inline uint32_t _file_read(_file_handle_t handle, uint32_t file_offset, uint32_t count, void *buffer) {
  return _sc4(_SCN_FILE_READ, handle, file_offset, count, (uint32_t)buffer);
}

static inline uint32_t _file_size(_file_handle_t handle) {
  return _sc1(_SCN_FILE_SIZE, handle);
}

static inline void _start_task(_drive_number_t drive_number, char *path) {
  _sc2(_SCN_START_TASK, drive_number, (uint32_t)path);
}

static inline void _log_string(const char *sz) {
  _sc1(_SCN_LOG_STRING, (uint32_t)sz);
}

static inline _key_code_t _get_key() {
  return _sc0(_SCN_GET_KEY);
}

static inline void *_allocate_ram(uint32_t pages) {
  return (void *)_sc1(_SCN_ALLOCATE_RAM, pages);
}

#endif