* libcurses++, libc++ and liblightOS++ are installed into the crosscompiler directory
[lightOS.git] / libkernel / syscall.c
blob0108db42fb685cadace206685fcfc5114e9ea45c
1 /*
2 lightOS libkernel
3 Copyright (C) 2007-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <libkernel/kernel.h>
20 #include <libkernel/syscall.h>
21 #include <libkernel/arch/syscall.h>
23 #include <libkernel/type_noprefix.h>
25 /*! External functions
26 *\note Defined in libkernel/$arch/syscall.S */
27 extern void _LIBKERNEL_thread_entry();
29 void _LIBKERNEL_kernel_version(syscall_param_t *major,
30 syscall_param_t *minor)
32 _LIBKERNEL_SYSCALL0_2(_LIBKERNEL_SYSCALL_GET_KERNEL_VERSION,
33 *major,
34 *minor);
37 void *_LIBKERNEL_extend_heap(syscall_param_t size)
39 void *address;
40 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_HEAPALLOC,
41 size,
42 address);
43 return address;
46 void _LIBKERNEL_get_memory_info(syscall_param_t *total,
47 syscall_param_t *free)
49 _LIBKERNEL_SYSCALL0_2(_LIBKERNEL_SYSCALL_GET_MEMORY_INFO,
50 *total,
51 *free);
54 _LIBKERNEL_syscall_param_t _LIBKERNEL_get_commandline_length()
56 _LIBKERNEL_syscall_param_t length;
57 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_GET_COMMANDLINE,
59 length);
60 return length;
63 void _LIBKERNEL_get_commandline(char *cmdline)
65 _LIBKERNEL_SYSCALL1_0(_LIBKERNEL_SYSCALL_GET_COMMANDLINE,
66 cmdline);
69 void _LIBKERNEL_get_time(syscall_param_t *hour,
70 syscall_param_t *minute,
71 syscall_param_t *second)
73 _LIBKERNEL_SYSCALL0_3(_LIBKERNEL_SYSCALL_GET_TIME,
74 *second,
75 *minute,
76 *hour);
79 void _LIBKERNEL_get_date(syscall_param_t *dayofweek,
80 syscall_param_t *dayofmonth,
81 syscall_param_t *month,
82 syscall_param_t *year,
83 syscall_param_t *summertime)
85 _LIBKERNEL_SYSCALL0_5(_LIBKERNEL_SYSCALL_GET_DATE,
86 *dayofweek,
87 *dayofmonth,
88 *month,
89 *year,
90 *summertime);
94 // IPC
97 port_id_t _LIBKERNEL_create_port()
99 port_id_t id;
100 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_CREATE_PORT,
101 id);
102 return id;
105 void _LIBKERNEL_destroy_port(port_id_t id)
107 _LIBKERNEL_SYSCALL1_0(_LIBKERNEL_SYSCALL_DESTROY_PORT,
108 id);
111 port_id_t _LIBKERNEL_create_specific_port(port_id_t id)
113 syscall_param_t result;
114 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_CREATE_SPECIFIC_PORT,
116 result);
117 if (result == 0)
118 return 0;
119 return id;
122 syscall_param_t _LIBKERNEL_peek_message(port_id_t id,
123 message_t *msg)
125 syscall_param_t result;
126 _LIBKERNEL_SYSCALL1_6(_LIBKERNEL_SYSCALL_PEEK_MESSAGE,
128 result,
129 msg->port,
130 msg->type,
131 msg->param1,
132 msg->param2,
133 msg->param3);
134 return result;
137 syscall_param_t _LIBKERNEL_get_message(port_id_t id,
138 message_t *msg)
140 syscall_param_t result;
141 _LIBKERNEL_SYSCALL1_6(_LIBKERNEL_SYSCALL_GET_MESSAGE,
143 result,
144 msg->port,
145 msg->type,
146 msg->param1,
147 msg->param2,
148 msg->param3);
149 return (result == id);
152 port_id_t _LIBKERNEL_wait_message(message_t *msg)
154 _LIBKERNEL_port_id_t result;
155 #ifdef X86
156 result = _LIBKERNEL_syscall_wait(_LIBKERNEL_SYSCALL_WAIT_MESSAGE, msg);
157 #endif
158 #ifdef X86_64
159 _LIBKERNEL_SYSCALL0_6(_LIBKERNEL_SYSCALL_WAIT_MESSAGE,
160 result,
161 msg->port,
162 msg->type,
163 msg->param1,
164 msg->param2,
165 msg->param3);
166 #endif
167 return result;
170 syscall_param_t _LIBKERNEL_transfer_port(port_id_t port,
171 process_id_t pid)
173 syscall_param_t result;
174 _LIBKERNEL_SYSCALL2_1(_LIBKERNEL_SYSCALL_TRANSFER_PORT,
175 port,
176 pid,
177 result);
178 return result;
181 syscall_param_t _LIBKERNEL_add_wait_message(port_id_t id,
182 const message_t *msg)
184 syscall_param_t result;
185 #ifdef X86
186 result = _LIBKERNEL_syscall_add_wait(_LIBKERNEL_SYSCALL_ADD_WAIT_MESSAGE,
188 msg);
189 #endif
190 #ifdef X86_64
191 _LIBKERNEL_SYSCALL6_1(_LIBKERNEL_SYSCALL_ADD_WAIT_MESSAGE,
193 msg->port,
194 msg->type,
195 msg->param1,
196 msg->param2,
197 msg->param3,
198 result);
199 #endif
200 return result;
203 syscall_param_t _LIBKERNEL_send_message(port_id_t id,
204 const message_t *msg)
206 syscall_param_t result;
207 #ifdef X86
208 result = _LIBKERNEL_syscall_send(_LIBKERNEL_SYSCALL_SEND_MESSAGE,
210 msg->port,
211 msg->type,
212 msg->param1,
213 msg->param2,
214 msg->param3);
215 #endif
216 #ifdef X86_64
217 _LIBKERNEL_SYSCALL6_1(_LIBKERNEL_SYSCALL_SEND_MESSAGE,
219 msg->port,
220 msg->type,
221 msg->param1,
222 msg->param2,
223 msg->param3,
224 result);
225 #endif
226 return result;
229 void _LIBKERNEL_get_port_info(port_id_t id,
230 process_id_t *owner,
231 thread_id_t *waiter)
233 syscall_param_t result;
234 _LIBKERNEL_SYSCALL1_3(_LIBKERNEL_SYSCALL_PORT_INFO,
236 result,
237 *owner,
238 *waiter);
241 syscall_param_t _LIBKERNEL_set_standard_port(process_id_t pid,
242 port_id_t stdPort,
243 port_id_t Port,
244 port_id_t fsPort)
246 syscall_param_t result;
247 _LIBKERNEL_SYSCALL5_1(_LIBKERNEL_SYSCALL_STANDARD_PORT,
248 pid,
250 stdPort,
251 Port,
252 fsPort,
253 result);
254 return result;
257 void _LIBKERNEL_get_standard_port(process_id_t pid,
258 port_id_t stdPort,
259 port_id_t *Port,
260 port_id_t *fsPort)
262 _LIBKERNEL_SYSCALL3_2(_LIBKERNEL_SYSCALL_STANDARD_PORT,
263 pid,
265 stdPort,
266 *Port,
267 *fsPort);
270 syscall_param_t _LIBKERNEL_register_event(port_id_t id,
271 syscall_param_t type,
272 syscall_param_t param1,
273 syscall_param_t param2)
275 syscall_param_t result;
276 _LIBKERNEL_SYSCALL4_1(_LIBKERNEL_SYSCALL_REGISTER_EVENT,
278 type,
279 param1,
280 param2,
281 result);
282 return result;
285 void _LIBKERNEL_acknoledge_event(port_id_t id,
286 syscall_param_t type,
287 syscall_param_t param)
289 _LIBKERNEL_SYSCALL3_0(_LIBKERNEL_SYSCALL_ACKNOLEDGE_EVENT,
291 type,
292 param);
295 syscall_param_t _LIBKERNEL_unregister_event(port_id_t id,
296 syscall_param_t type)
298 syscall_param_t result;
299 _LIBKERNEL_SYSCALL2_1(_LIBKERNEL_SYSCALL_UNREGISTER_EVENT,
301 type,
302 result);
303 return result;
306 syscall_param_t _LIBKERNEL_get_tick_count()
308 syscall_param_t count;
309 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_GET_TICK_COUNT,
310 count);
311 return count;
314 shared_memory_t _LIBKERNEL_create_shared_memory(syscall_param_t size)
316 shared_memory_t shm;
317 shm.size = size;
318 _LIBKERNEL_SYSCALL1_2(_LIBKERNEL_SYSCALL_CREATE_SHARED_MEMORY,
319 size,
320 shm.id,
321 shm.address);
322 return shm;
325 void _LIBKERNEL_destroy_shared_memory(shared_memory_t *shm)
327 if (shm->id != 0)
329 _LIBKERNEL_SYSCALL1_0(_LIBKERNEL_SYSCALL_DESTROY_SHARED_MEMORY,
330 shm->id);
332 shm->id = 0;
333 shm->address = 0;
334 shm->size = 0;
337 thread_id_t _LIBKERNEL_create_thread(void (*thread_entry)(_LIBKERNEL_syscall_param_t),
338 syscall_param_t parameter,
339 syscall_param_t flags)
341 thread_id_t id;
342 _LIBKERNEL_SYSCALL4_1(_LIBKERNEL_SYSCALL_CREATE_THREAD,
343 _LIBKERNEL_thread_entry,
344 thread_entry,
345 parameter,
346 flags,
347 id);
348 return id;
351 syscall_param_t _LIBKERNEL_suspend_thread(thread_id_t id)
353 syscall_param_t result;
354 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_SUSPEND_THREAD,
356 result);
357 return result;
360 syscall_param_t _LIBKERNEL_resume_thread(thread_id_t id)
362 syscall_param_t result;
363 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_RESUME_THREAD,
365 result);
366 return result;
369 syscall_param_t _LIBKERNEL_destroy_thread(thread_id_t id)
371 syscall_param_t result;
372 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_DESTROY_THREAD,
374 result);
375 return result;
378 syscall_param_t _LIBKERNEL_destroy_process(process_id_t id)
380 syscall_param_t result;
381 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_DESTROY_PROCESS,
383 result);
384 return result;
387 process_id_t _LIBKERNEL_get_process_id()
389 process_id_t id;
390 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_GET_PROCESSID,
391 id);
392 return id;
395 thread_id_t _LIBKERNEL_get_thread_id()
397 thread_id_t id;
398 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_GET_THREADID,
399 id);
400 return id;
403 syscall_param_t _LIBKERNEL_get_process_count()
405 syscall_param_t count;
406 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_GET_PROCESS_COUNT,
407 count);
408 return count;
411 process_id_t _LIBKERNEL_enum_processes(syscall_param_t i)
413 process_id_t id;
414 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_GET_PROCESS_LIST,
416 id);
417 return id;
420 syscall_param_t _LIBKERNEL_get_process_name_length(process_id_t id)
422 syscall_param_t length;
423 _LIBKERNEL_SYSCALL3_1(_LIBKERNEL_SYSCALL_GET_PROCESS_INFO,
427 length);
428 return length;
431 void _LIBKERNEL_get_process_name(process_id_t id,
432 char *name)
434 _LIBKERNEL_SYSCALL3_0(_LIBKERNEL_SYSCALL_GET_PROCESS_INFO,
437 name);
440 syscall_param_t _LIBKERNEL_get_process_memory_usage(process_id_t id)
442 syscall_param_t mem_usage;
443 _LIBKERNEL_SYSCALL2_1(_LIBKERNEL_SYSCALL_GET_PROCESS_INFO,
446 mem_usage);
447 return mem_usage;
450 syscall_param_t _LIBKERNEL_range_allocator_count(syscall_param_t *capacity)
452 syscall_param_t count;
453 _LIBKERNEL_SYSCALL0_2(_LIBKERNEL_SYSCALL_RANGE_ALLOCATOR_COUNT,
454 count,
455 *capacity);
456 return count;
459 void _LIBKERNEL_enum_range_allocator(syscall_param_t index,
460 syscall_param_t *address,
461 syscall_param_t *size)
463 _LIBKERNEL_SYSCALL1_2(_LIBKERNEL_SYSCALL_ENUM_RANGE_ALLOCATOR,
464 index,
465 *address,
466 *size);
470 // VBE
473 void _LIBKERNEL_vbe_get_version(syscall_param_t *major,
474 syscall_param_t *minor)
476 _LIBKERNEL_SYSCALL0_2(_LIBKERNEL_SYSCALL_VBE_GET_VERSION,
477 *major,
478 *minor);
481 syscall_param_t _LIBKERNEL_vbe_get_mode_count()
483 syscall_param_t count;
484 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_VBE_GET_MODE_COUNT,
485 count);
486 return count;
489 void _LIBKERNEL_vbe_enumerate_mode(syscall_param_t index,
490 syscall_param_t *width,
491 syscall_param_t *height,
492 syscall_param_t *bpp)
494 _LIBKERNEL_SYSCALL1_3(_LIBKERNEL_SYSCALL_VBE_GET_MODE,
495 index,
496 *width,
497 *height,
498 *bpp);
501 void *_LIBKERNEL_vbe_set_mode(syscall_param_t index)
503 void *framebuffer;
504 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_VBE_SET_MODE,
505 index,
506 framebuffer);
507 return framebuffer;
510 syscall_param_t _LIBKERNEL_allocate_io_port_range(syscall_param_t port,
511 syscall_param_t count)
513 // NOTE TODO FIXME HACK
514 syscall_param_t result;
515 _LIBKERNEL_SYSCALL0_1( _LIBKERNEL_SYSCALL_REQUEST_IO,
516 result);
517 return result;
520 void _LIBKERNEL_free_io_port_range(syscall_param_t port,
521 syscall_param_t count)
523 // NOTE TODO FIXME HACK
527 // ISA DMA
530 syscall_param_t _LIBKERNEL_setup_dma_channel(syscall_param_t channel,
531 void *address,
532 syscall_param_t count,
533 syscall_param_t mode)
535 syscall_param_t result;
536 _LIBKERNEL_SYSCALL4_1(_LIBKERNEL_SYSCALL_DMA,
537 channel,
538 address,
539 count,
540 mode,
541 result);
542 return result;
546 // Process creation
549 process_id_t _LIBKERNEL_execute(const void *data,
550 syscall_param_t size,
551 const char *cmdline,
552 syscall_param_t flags,
553 char *libName)
555 process_id_t pid;
556 _LIBKERNEL_SYSCALL5_1(_LIBKERNEL_SYSCALL_EXECUTE,
557 data,
558 size,
559 cmdline,
560 flags,
561 (syscall_param_t)libName,
562 pid);
563 return pid;
567 // Physical memory-management
570 void *_LIBKERNEL_get_physical_address(const void *address)
572 void *result;
573 _LIBKERNEL_SYSCALL1_1(_LIBKERNEL_SYSCALL_GET_PHYSICAL_ADDRESS,
574 address,
575 result);
576 return result;
579 void *_LIBKERNEL_allocate_memory_region(syscall_param_t size,
580 syscall_param_t type,
581 void *physical)
583 void *result;
584 _LIBKERNEL_SYSCALL3_1(_LIBKERNEL_SYSCALL_ALLOCATE_REGION,
585 size,
586 type,
587 physical,
588 result);
589 return result;
592 void _LIBKERNEL_free_memory_region(void *address)
594 _LIBKERNEL_SYSCALL1_0(_LIBKERNEL_SYSCALL_FREE_REGION,
595 address);
599 // System configuration
602 void _LIBKERNEL_get_system_configuration(system_config_t *config)
604 syscall_param_t ret[3];
605 _LIBKERNEL_SYSCALL0_3(_LIBKERNEL_SYSCALL_GET_SYSTEM_CONFIGURATION,
606 ret[0],
607 ret[1],
608 ret[2]);
609 config->serial[0] = ret[0] & 0xFFFF;
610 config->serial[1] = (ret[0] >> 16) & 0xFFFF;
611 config->serial[2] = ret[1] & 0xFFFF;
612 config->serial[3] = (ret[1] >> 16) & 0xFFFF;
613 config->kernel_serial = ret[2] & 0xFFFF;
617 // Libraries
620 syscall_param_t _LIBKERNEL_get_library_count()
622 syscall_param_t count;
623 _LIBKERNEL_SYSCALL0_1(_LIBKERNEL_SYSCALL_GET_LIBRARY_COUNT,
624 count);
625 return count;
628 syscall_param_t _LIBKERNEL_get_library_name_length(syscall_param_t index)
630 syscall_param_t address, size, length;
631 _LIBKERNEL_SYSCALL2_3(_LIBKERNEL_SYSCALL_GET_LIBRARY_INFO,
632 index,
634 length,
635 address,
636 size);
637 return length;
640 void _LIBKERNEL_get_library_info(syscall_param_t index,
641 char *name,
642 syscall_param_t length,
643 syscall_param_t *address,
644 syscall_param_t *size)
646 _LIBKERNEL_SYSCALL2_3(_LIBKERNEL_SYSCALL_GET_LIBRARY_INFO,
647 index,
648 name,
649 length,
650 *address,
651 *size);
655 // Signal handler
658 void _LIBKERNEL_set_signal_handler(void (*func)(int))
660 _LIBKERNEL_SYSCALL1_0(_LIBKERNEL_SYSCALL_SIGNAL_HANDLER,
661 func);
664 void _LIBKERNEL_signal_handler_end()
666 _LIBKERNEL_SYSCALL0_0(_LIBKERNEL_SYSCALL_SIGNAL_END);
669 int _LIBKERNEL_signal(process_id_t pid,
670 int sig)
672 syscall_param_t result;
673 _LIBKERNEL_SYSCALL2_1(_LIBKERNEL_SYSCALL_SIGNAL,
674 pid,
675 sig,
676 result);
677 if (result == 0)
678 return 0;
679 return -1;
682 syscall_param_t _LIBKERNEL_get_log_entry(syscall_param_t index,
683 syscall_param_t size,
684 char* address,
685 enum _LIBKERNEL_log_level* level)
687 syscall_param_t real_size;
688 _LIBKERNEL_SYSCALL3_2(_LIBKERNEL_SYSCALL_GET_LOG_ENTRY,
689 index,
690 size,
691 address,
692 real_size,
693 *level);
694 return real_size;