The Pedigree Project  0.1
pedigree-c-syscalls.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigreecSyscallNumbers.h"
21 
22 #include "pedigree-c-syscall.h"
23 
24 #include "pedigree/kernel/compiler.h"
25 #include "pedigree/kernel/processor/types.h"
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 
32 EXPORTED_PUBLIC int pedigree_load_keymap(char *buf, size_t sz);
33 
34 EXPORTED_PUBLIC void pedigree_reboot(void);
35 
36 EXPORTED_PUBLIC void pedigree_haltfs(void);
37 
39 pedigree_get_mount(char *mount_buf, char *info_buf, size_t n);
40 
41 EXPORTED_PUBLIC void *pedigree_sys_request_mem(size_t len);
42 
43 EXPORTED_PUBLIC void
44 pedigree_input_install_callback(void *p, uint32_t type, uintptr_t param);
45 
46 EXPORTED_PUBLIC void pedigree_input_remove_callback(void *p);
47 
48 EXPORTED_PUBLIC void pedigree_input_inhibit_events(int inhibit);
49 
50 EXPORTED_PUBLIC void pedigree_event_return(void);
51 
52 EXPORTED_PUBLIC void pedigree_module_load(char *file);
53 
54 EXPORTED_PUBLIC void pedigree_module_unload(char *name);
55 
56 EXPORTED_PUBLIC int pedigree_module_is_loaded(char *name);
57 
59 pedigree_module_get_depending(char *name, char *buf, size_t bufsz);
60 
61 EXPORTED_PUBLIC void
62 pedigree_config_getcolname(size_t resultIdx, size_t n, char *buf, size_t bufsz);
63 
64 EXPORTED_PUBLIC void pedigree_config_getstr_n(
65  size_t resultIdx, size_t row, size_t n, char *buf, size_t bufsz);
66 
67 EXPORTED_PUBLIC void pedigree_config_getstr_s(
68  size_t resultIdx, size_t row, const char *col, char *buf, size_t bufsz);
69 
71 pedigree_config_getnum_n(size_t resultIdx, size_t row, size_t n);
72 
74 pedigree_config_getnum_s(size_t resultIdx, size_t row, const char *col);
75 
77 pedigree_config_getbool_n(size_t resultIdx, size_t row, size_t n);
78 
80 pedigree_config_getbool_s(size_t resultIdx, size_t row, const char *col);
81 
82 EXPORTED_PUBLIC int pedigree_config_query(const char *query);
83 
84 EXPORTED_PUBLIC void pedigree_config_freeresult(size_t resultIdx);
85 
86 EXPORTED_PUBLIC int pedigree_config_numcols(size_t resultIdx);
87 
88 EXPORTED_PUBLIC int pedigree_config_numrows(size_t resultIdx);
89 
90 EXPORTED_PUBLIC int pedigree_config_was_successful(size_t resultIdx);
91 
92 EXPORTED_PUBLIC void
93 pedigree_config_get_error_message(size_t resultIdx, char *buf, int buflen);
94 
95 EXPORTED_PUBLIC char *pedigree_config_escape_string(const char *str);
96 
97 EXPORTED_PUBLIC int pedigree_login(uid_t uid, const char *password);
98 
99 EXPORTED_PUBLIC int pedigree_gfx_get_provider(void *p);
100 
101 EXPORTED_PUBLIC int pedigree_gfx_get_curr_mode(void *p, void *sm);
102 
103 EXPORTED_PUBLIC uintptr_t pedigree_gfx_get_raw_buffer(void *p);
104 
105 EXPORTED_PUBLIC int pedigree_gfx_create_buffer(void *p, void **b, void *args);
106 
107 EXPORTED_PUBLIC int pedigree_gfx_destroy_buffer(void *p, void *b);
108 
109 EXPORTED_PUBLIC void pedigree_gfx_redraw(void *p, void *args);
110 
111 EXPORTED_PUBLIC void pedigree_gfx_blit(void *p, void *args);
112 
113 EXPORTED_PUBLIC void pedigree_gfx_set_pixel(
114  void *p, uint32_t x, uint32_t y, uint32_t colour, uint32_t fmt);
115 
116 EXPORTED_PUBLIC void pedigree_gfx_rect(void *p, void *args);
117 
118 EXPORTED_PUBLIC void pedigree_gfx_copy(void *p, void *args);
119 
120 EXPORTED_PUBLIC void pedigree_gfx_line(void *p, void *args);
121 
122 EXPORTED_PUBLIC void pedigree_gfx_draw(void *p, void *args);
123 
124 EXPORTED_PUBLIC int pedigree_gfx_create_fbuffer(void *p, void *args);
125 
126 EXPORTED_PUBLIC void pedigree_gfx_delete_fbuffer(void *p);
127 
128 EXPORTED_PUBLIC void
129 pedigree_gfx_fbinfo(void *p, size_t *w, size_t *h, uint32_t *fmt, size_t *bypp);
130 
131 EXPORTED_PUBLIC void
132 pedigree_gfx_setpalette(void *p, uint32_t *data, size_t entries);
133 
134 // These are provided by libc
135 void *malloc(size_t);
136 size_t strlen(const char *);
137 
138 int pedigree_load_keymap(char *buf, size_t sz)
139 {
140  return syscall2(PEDIGREE_LOAD_KEYMAP, (long) buf, (long) sz);
141 }
142 
143 void pedigree_reboot()
144 {
145  syscall0(PEDIGREE_REBOOT);
146 }
147 
148 void pedigree_haltfs()
149 {
150  syscall0(PEDIGREE_HALTFS);
151 }
152 
153 int pedigree_get_mount(char *mount_buf, char *info_buf, size_t n)
154 {
155  return syscall3(PEDIGREE_GET_MOUNT, (long) mount_buf, (long) info_buf, n);
156 }
157 
158 void *pedigree_sys_request_mem(size_t len)
159 {
160  uintptr_t result = syscall1(PEDIGREE_SYS_REQUEST_MEM, (long) len);
161  return (void *) result;
162 }
163 
164 void pedigree_input_install_callback(void *p, uint32_t type, uintptr_t param)
165 {
166  syscall3(PEDIGREE_INPUT_INSTALL_CALLBACK, (long) p, type, param);
167 }
168 
169 void pedigree_input_remove_callback(void *p)
170 {
171  syscall1(PEDIGREE_INPUT_REMOVE_CALLBACK, (long) p);
172 }
173 
174 void pedigree_input_inhibit_events(int inhibit)
175 {
176  syscall1(PEDIGREE_INPUT_INHIBIT_EVENTS, inhibit);
177 }
178 
179 void pedigree_event_return()
180 {
181  syscall0(PEDIGREE_EVENT_RETURN);
182 }
183 
184 void pedigree_module_load(char *file)
185 {
186  syscall1(PEDIGREE_MODULE_LOAD, (long) file);
187 }
188 
189 void pedigree_module_unload(char *name)
190 {
191  syscall1(PEDIGREE_MODULE_UNLOAD, (long) name);
192 }
193 
194 int pedigree_module_is_loaded(char *name)
195 {
196  return syscall1(PEDIGREE_MODULE_IS_LOADED, (long) name);
197 }
198 
199 int pedigree_module_get_depending(char *name, char *buf, size_t bufsz)
200 {
201  return syscall3(
202  PEDIGREE_MODULE_GET_DEPENDING, (long) name, (long) buf, bufsz);
203 }
204 
205 void pedigree_config_getcolname(
206  size_t resultIdx, size_t n, char *buf, size_t bufsz)
207 {
208  syscall4(PEDIGREE_CONFIG_GETCOLNAME, resultIdx, n, (long) buf, bufsz);
209 }
210 
211 void pedigree_config_getstr_n(
212  size_t resultIdx, size_t row, size_t n, char *buf, size_t bufsz)
213 {
214  syscall5(PEDIGREE_CONFIG_GETSTR_N, resultIdx, row, n, (long) buf, bufsz);
215 }
216 void pedigree_config_getstr_s(
217  size_t resultIdx, size_t row, const char *col, char *buf, size_t bufsz)
218 {
219  syscall5(
220  PEDIGREE_CONFIG_GETSTR_S, resultIdx, row, (long) col, (long) buf,
221  bufsz);
222 }
223 
224 int pedigree_config_getnum_n(size_t resultIdx, size_t row, size_t n)
225 {
226  return syscall3(PEDIGREE_CONFIG_GETNUM_N, row, resultIdx, n);
227 }
228 int pedigree_config_getnum_s(size_t resultIdx, size_t row, const char *col)
229 {
230  return syscall3(PEDIGREE_CONFIG_GETNUM_S, row, resultIdx, (long) col);
231 }
232 
233 int pedigree_config_getbool_n(size_t resultIdx, size_t row, size_t n)
234 {
235  return syscall3(PEDIGREE_CONFIG_GETBOOL_N, resultIdx, row, n);
236 }
237 int pedigree_config_getbool_s(size_t resultIdx, size_t row, const char *col)
238 {
239  return syscall3(PEDIGREE_CONFIG_GETBOOL_S, resultIdx, row, (long) col);
240 }
241 
242 int pedigree_config_query(const char *query)
243 {
244  return syscall1(PEDIGREE_CONFIG_QUERY, (long) query);
245 }
246 
247 void pedigree_config_freeresult(size_t resultIdx)
248 {
249  syscall1(PEDIGREE_CONFIG_FREERESULT, resultIdx);
250 }
251 
252 int pedigree_config_numcols(size_t resultIdx)
253 {
254  return syscall1(PEDIGREE_CONFIG_NUMCOLS, resultIdx);
255 }
256 
257 int pedigree_config_numrows(size_t resultIdx)
258 {
259  return syscall1(PEDIGREE_CONFIG_NUMROWS, resultIdx);
260 }
261 
262 int pedigree_config_was_successful(size_t resultIdx)
263 {
264  return syscall1(PEDIGREE_CONFIG_WAS_SUCCESSFUL, resultIdx);
265 }
266 
267 void pedigree_config_get_error_message(size_t resultIdx, char *buf, int bufsz)
268 {
269  syscall3(PEDIGREE_CONFIG_GET_ERROR_MESSAGE, resultIdx, (long) buf, bufsz);
270 }
271 
272 char *pedigree_config_escape_string(const char *str)
273 {
274  // Expect the worst: every char needs to be escaped
275  char *bufferStart = (char *) malloc(strlen(str) * 2 + 1);
276  char *buffer = bufferStart;
277  while (*str)
278  {
279  if (*str == '\'')
280  {
281  *buffer++ = '\'';
282  *buffer++ = '\'';
283  }
284  else
285  *buffer++ = *str;
286 
287  str++;
288  }
289  *buffer = '\0';
290 
291  // Reallocate so we won't use more space than we need
292  bufferStart = realloc(bufferStart, strlen(bufferStart) + 1);
293 
294  return bufferStart;
295 }
296 
297 // Pedigree-specific function: login with given uid and password.
298 int pedigree_login(uid_t uid, const char *password)
299 {
300  return (long) syscall2(PEDIGREE_LOGIN, uid, (long) password);
301 }
302 
303 int pedigree_gfx_get_provider(void *p)
304 {
305  return syscall1(PEDIGREE_GFX_GET_PROVIDER, (long) p);
306 }
307 
308 int pedigree_gfx_get_curr_mode(void *p, void *sm)
309 {
310  return syscall2(PEDIGREE_GFX_GET_CURR_MODE, (long) p, (long) sm);
311 }
312 
313 uintptr_t pedigree_gfx_get_raw_buffer(void *p)
314 {
315  return (uintptr_t) syscall1(PEDIGREE_GFX_GET_RAW_BUFFER, (long) p);
316 }
317 
318 int pedigree_gfx_create_buffer(void *p, void **b, void *args)
319 {
320  return syscall3(
321  PEDIGREE_GFX_CREATE_BUFFER, (long) p, (long) b, (long) args);
322 }
323 
324 int pedigree_gfx_destroy_buffer(void *p, void *b)
325 {
326  return syscall2(PEDIGREE_GFX_DESTROY_BUFFER, (long) p, (long) b);
327 }
328 
329 void pedigree_gfx_redraw(void *p, void *args)
330 {
331  syscall2(PEDIGREE_GFX_REDRAW, (long) p, (long) args);
332 }
333 
334 void pedigree_gfx_blit(void *p, void *args)
335 {
336  syscall2(PEDIGREE_GFX_BLIT, (long) p, (long) args);
337 }
338 
339 void pedigree_gfx_set_pixel(
340  void *p, uint32_t x, uint32_t y, uint32_t colour, uint32_t fmt)
341 {
342  syscall5(PEDIGREE_GFX_SET_PIXEL, (long) p, x, y, colour, fmt);
343 }
344 
345 void pedigree_gfx_rect(void *p, void *args)
346 {
347  syscall2(PEDIGREE_GFX_RECT, (long) p, (long) args);
348 }
349 
350 void pedigree_gfx_copy(void *p, void *args)
351 {
352  syscall2(PEDIGREE_GFX_COPY, (long) p, (long) args);
353 }
354 
355 void pedigree_gfx_line(void *p, void *args)
356 {
357  syscall2(PEDIGREE_GFX_LINE, (long) p, (long) args);
358 }
359 
360 void pedigree_gfx_draw(void *p, void *args)
361 {
362  syscall2(PEDIGREE_GFX_DRAW, (long) p, (long) args);
363 }
364 
365 int pedigree_gfx_create_fbuffer(void *p, void *args)
366 {
367  return syscall2(PEDIGREE_GFX_CREATE_FBUFFER, (long) p, (long) args);
368 }
369 
370 void pedigree_gfx_delete_fbuffer(void *p)
371 {
372  syscall1(PEDIGREE_GFX_DELETE_FBUFFER, (long) p);
373 }
374 
375 void pedigree_gfx_fbinfo(
376  void *p, size_t *w, size_t *h, uint32_t *fmt, size_t *bypp)
377 {
378  syscall5(
379  PEDIGREE_GFX_FBINFO, (long) p, (long) w, (long) h, (long) fmt,
380  (long) bypp);
381 }
382 
383 void pedigree_gfx_setpalette(void *p, uint32_t *data, size_t entries)
384 {
385  syscall3(PEDIGREE_GFX_SETPALETTE, (long) p, (long) data, (long) entries);
386 }