Started trying to get the syscall forwading stuff working between BOCHS and my native...
[akaros.git] / tools / syscall_server / syscall_server.h
1 #ifndef SERIAL_SERVER_H
2 #define SERIAL_SERVER_H
3
4 #include <stdint.h>
5 #include <newlib_stat.h>
6
7 #define ROS_SLAVE_PTY ".ros_slave_pty"
8
9 #define OPEN_ID                 0
10 #define CLOSE_ID                1
11 #define READ_ID                 2
12 #define WRITE_ID                3
13 #define LINK_ID                 4
14 #define UNLINK_ID               5
15 #define LSEEK_ID                6
16 #define FSTAT_ID                7
17 #define ISATTY_ID               8
18 #define STAT_ID                 9
19 #define NUM_SYSCALLS    10
20
21 typedef uint32_t syscall_id_t;
22
23 typedef struct open_subheader {
24         uint32_t flags;
25         uint32_t mode;
26         uint32_t len;
27 } open_subheader_t;
28
29 typedef struct close_subheader {
30         uint32_t fd;
31         uint32_t FILL1;
32         uint32_t FILL2;
33 } close_subheader_t;
34
35 typedef struct read_subheader {
36         uint32_t fd;
37         uint32_t len;
38         uint32_t FILL1;
39 } read_subheader_t;
40
41 typedef struct write_subheader {
42         uint32_t fd;
43         uint32_t len;
44         uint32_t FILL1;
45 } write_subheader_t;
46
47 typedef struct lseek_subheader {
48         uint32_t fd;
49         uint32_t ptr;
50         uint32_t dir;
51 } lseek_subheader_t;
52
53 typedef struct isatty_subheader {
54         uint32_t fd;
55         uint32_t FILL1;
56         uint32_t FILL2;
57 } isatty_subheader_t;
58
59 typedef struct link_subheader {
60         uint32_t old_len;
61         uint32_t new_len;
62         uint32_t FILL1;
63 } link_subheader_t;
64
65 typedef struct unlink_subheader {
66         uint32_t len;
67         uint32_t FILL1;
68         uint32_t FILL2;
69 } unlink_subheader_t;
70
71 typedef struct fstat_subheader {
72         uint32_t fd;
73         uint32_t FILL1;
74         uint32_t FILL2;
75 } fstat_subheader_t;
76
77 typedef struct stat_subheader {
78         uint32_t len;
79         uint32_t FILL1;
80         uint32_t FILL2;
81 } stat_subheader_t;
82
83 typedef struct syscall_req_header {
84         syscall_id_t id;
85         union {
86                 open_subheader_t open;
87                 close_subheader_t close;
88                 read_subheader_t read;
89                 write_subheader_t write;
90                 lseek_subheader_t lseek;
91                 isatty_subheader_t isatty;
92                 link_subheader_t link;
93                 unlink_subheader_t unlink;
94                 fstat_subheader_t fstat;
95                 stat_subheader_t stat;  
96         } subheader;
97 } syscall_req_header_t;
98
99 typedef struct syscall_req {
100         syscall_req_header_t header;
101         uint32_t payload_len;
102         void* payload;
103 } syscall_req_t;
104
105 typedef struct syscall_rsp_header {
106         int32_t return_val;
107         uint32_t return_errno;
108 } syscall_rsp_header_t;
109
110 typedef struct syscall_rsp {
111         syscall_rsp_header_t header;
112         uint32_t payload_len;
113         void* payload;
114 } syscall_rsp_t;
115
116 void run_server();
117 void translate_stat(struct stat* native, struct newlib_stat* newlib);
118 void translate_flags(int native, int newlib);
119 void translate_mode(int native, int newlib);
120 void translate_dir(int native, int newlib);
121 void translate_errno(int native, int newlib);
122 void set_syscall_req_payload_len(syscall_req_t* req);
123 void read_syscall_req(int fd, syscall_req_t* req);
124 void read_syscall_req_header(int fd, syscall_req_t* req);
125 void read_syscall_req_payload(int fd, syscall_req_t* req);
126 void write_syscall_rsp(int fd, syscall_rsp_t* rsp);
127 void write_syscall_rsp_header(int fd, syscall_rsp_t* rsp);
128 void write_syscall_rsp_payload(int fd, syscall_rsp_t* rsp);
129 int read_from_serial(int fd, void* buf, int len, int peek); 
130 void error(int fd, const char* s);
131
132 void handle_syscall(syscall_req_t* req, syscall_rsp_t* rsp);
133 void handle_open(syscall_req_t* req, syscall_rsp_t* rsp);
134 void handle_close(syscall_req_t* req, syscall_rsp_t* rsp);
135 void handle_read(syscall_req_t* req, syscall_rsp_t* rsp);
136 void handle_write(syscall_req_t* req, syscall_rsp_t* rsp);
137 void handle_link(syscall_req_t* req, syscall_rsp_t* rsp);
138 void handle_unlink(syscall_req_t* req, syscall_rsp_t* rsp);
139 void handle_lseek(syscall_req_t* req, syscall_rsp_t* rsp);
140 void handle_fstat(syscall_req_t* req, syscall_rsp_t* rsp);
141 void handle_isatty(syscall_req_t* req, syscall_rsp_t* rsp);
142 void handle_stat(syscall_req_t* req, syscall_rsp_t* rsp);
143
144 #endif //SERIAL_SERVER_H