3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2009 Martin 'povik' Poviser (martin.povik@gmail.com)
7 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <net/socket.h>
39 #include <sound/audio.h>
42 void sys_exit (struct regs
*r
)
44 task_t
*taskc
= task_find (0);
49 page_dir_switch (taskc
->page_cover
->page_dir
);
51 proc_t
*proc
= proc_find (_curr_task
);
56 task_t
*task
= proc
->tty
->task
;
61 longjmp (task
->state
, 1);
67 void sys_getch (struct regs
*r
)
69 proc_t
*proc
= proc_find (_curr_task
);
74 if (proc
->tty
!= currtty
)
77 unsigned char c
= (unsigned char) getkey ();
82 *SYSV_GETCH
= (unsigned) c
;
86 void sys_sleep (struct regs
*r
)
88 unsigned long timer_start
= timer_ticks
+ ((unsigned) r
->ebx
* 1000);
90 while (timer_start
> timer_ticks
)
94 void sys_putch (struct regs
*r
)
96 //page_dir_switch (taskc->page_cover->page_dir);
98 proc_t
*proc
= proc_find (_curr_task
);
105 tty_putnch (proc
->tty
, c
);
107 //page_dir_switch (proc->task->page_cover->page_dir);
110 void sys_color (struct regs
*r
)
112 video_color (r
->ebx
, r
->ecx
);
116 void sys_cls (struct regs
*r
)
118 proc_t
*proc
= proc_find (_curr_task
);
123 if (proc
->tty
!= currtty
)
126 /* NOTE: Why we should disable interrupts ? Nobody know, but works better */
127 if (int_disable ()) {
133 void sys_getkey (struct regs
*r
)
135 proc_t
*proc
= proc_find (_curr_task
);
140 if (proc
->tty
!= currtty
)
143 *SYSV_GETKEY
= (unsigned) scancode
;
148 void sys_gotoxy (struct regs
*r
)
150 proc_t
*proc
= proc_find (_curr_task
);
155 tty_gotoxy (proc
->tty
, r
->ebx
, r
->ecx
);
158 void sys_fork (struct regs
*r
)
160 *SYSV_FORK
= fork ();
163 void sys_schedule (struct regs
*r
)
168 void sys_write (struct regs
*r
)
170 unsigned len
= r
->ecx
;
171 unsigned fd
= r
->edx
;
173 unsigned char *buf
= (unsigned char *) r
->ebx
;
185 proc
= proc_find (_curr_task
);
194 //printf ("data: %s | 0x%x | %d\n", mem, proc->data, len);
196 tty_write (proc
->tty
, (char *) mem
, len
);
202 proc
= proc_find (_curr_task
);
209 if (len
> KBD_MAX_QUAUE
)
215 for (i
= len
; i
>= 0; i
--)
225 ret
= write (fd
, (char *) mem
, len
);
232 void sys_socket (struct regs
*r
)
234 *SYSV_SOCKET
= socket (r
->ebx
, r
->ecx
, r
->edx
);
237 void sys_connect (struct regs
*r
)
239 sockaddr
*addr
= (sockaddr
*) r
->ebx
;
241 *SYSV_CONNECT
= connect (r
->ecx
, addr
, r
->edx
);
244 void sys_malloc (struct regs
*r
)
246 proc_t
*proc
= proc_find (_curr_task
);
249 r
->eax
= (unsigned) umalloc (proc
, (int) r
->ebx
);
252 DPRINT (DBG_SYSCALL
, "malloc (): 0x%x", r
->eax
);
255 void sys_send (struct regs
*r
)
257 *SYSV_SEND
= send ((int) r
->ecx
, (char *) r
->ebx
, (unsigned) r
->edx
, 0);
260 void sys_recv (struct regs
*r
)
262 unsigned char *msg
= (unsigned char *) r
->ebx
;
263 unsigned size
= (unsigned) r
->edx
;
264 int fd
= (int) r
->ecx
;
266 *SYSV_RECV
= recv (fd
, (char *) msg
, size
, 0);
269 void sys_close (struct regs
*r
)
274 void sys_open (struct regs
*r
)
276 *SYSV_OPEN
= open ((char *) r
->ebx
, (unsigned) r
->ecx
);
279 void sys_pcspk (struct regs
*r
)
281 dev_t
*dev
= dev_find ("/dev/pcspk");
284 dev
->handler (DEV_ACT_PLAY
, (unsigned) r
->ebx
);
287 void sys_usleep (struct regs
*r
)
289 //unsigned long timer_start = timer_ticks + ((unsigned) r->ebx);
291 //while (timer_start > timer_ticks)
293 usleep ((unsigned) r
->ebx
);
296 void sys_read (struct regs
*r
)
298 unsigned fd
= r
->ecx
;
299 unsigned len
= r
->edx
;
301 unsigned char *buf
= (unsigned char *) r
->ebx
;
311 proc
= proc_find (_curr_task
);
316 //if (proc->tty != currtty)
319 ret
= tty_read (proc
->tty
, (char *) buf
, len
);
322 ret
= read (fd
, (char *) buf
, len
);
329 void sys_time (struct regs
*r
)
331 time_t *memptr
= (time_t *) SYSV_TIME
;
333 tm
*t
= rtc_getcurrtime ();
336 *memptr
= (time_t) t
->__tm_gmtoff
;
339 void sys_system (struct regs
*r
)
341 proc_t
*proc
= proc_find (_curr_task
);
346 task_t
*oldtask
= _curr_task
;
348 unsigned char *cmd
= (unsigned char *) r
->ebx
;
351 page_dir_switch (task->page_cover->page_dir);
357 command_parser ((char *) cmd
, strlen (cmd
));
359 page_dir_switch (oldtask
->page_cover
->page_dir
);
361 /* needed for correct scheduling (when you execute new process, _curr_task is tasj of current tty) */
362 _curr_task
= oldtask
;
368 void sys_chdir (struct regs
*r
)
370 char *dir
= (unsigned char *) r
->ebx
;
372 *SYSV_CHDIR
= vfs_cd (dir
, strlen (dir
)) ? 0 : -1;
375 void sys_getdir (struct regs
*r
)
377 r
->eax
= (unsigned) vfs_dirent ();
380 void sys_procarg (struct regs
*r
)
382 proc_t
*proc
= proc_find (_curr_task
);
389 r
->eax
= (unsigned) proc
->argv
;
392 r
->eax
= (unsigned) proc
->argc
;
399 void sys_signal (struct regs
*r
)
401 signal (r
->ebx
, (sighandler_t
) r
->ecx
);
404 void sys_mount (struct regs
*r
)
406 partition_t
*p
= partition_find ((char *) r
->ebx
);
409 mount (p
, "", (char *) r
->ecx
);
415 void sys_kputs (struct regs
*r
)
419 module_t
*kmod
= module_find (_curr_task
);
424 if (kmod
->task
!= _curr_task
)
427 unsigned char *buf
= (unsigned char *) r
->ebx
;
430 //kprintf ("yeah, sys_kputs: '%s' '0x%x'\n", buf, buf);
435 void sys_bind (struct regs
*r
)
437 *SYSV_BIND
= bind (r
->ecx
, (sockaddr
*) r
->ebx
, r
->edx
);
440 void sys_listen (struct regs
*r
)
442 *SYSV_LISTEN
= listen (r
->ebx
, r
->ecx
);
445 void sys_accept (struct regs
*r
)
447 *SYSV_ACCEPT
= accept (r
->ecx
, r
->ebx
, r
->edx
);
450 void sys_fcntl (struct regs
*r
)
452 *SYSV_FCNTL
= fcntl (r
->ebx
, r
->ecx
, r
->edx
);
455 void sys_gvgafb (struct regs
*r
)
457 proc_t
*proc
= proc_find (_curr_task
);
463 printf ("Please start this program in graphical VESA mode\n");
468 *SYSV_GVGAFB
= (unsigned) init_vgafb ();
471 void sys_gcls (struct regs
*r
)
476 void sys_gfbswap (struct regs
*r
)
478 proc_t
*proc
= proc_find (_curr_task
);
483 if (proc
->tty
== currtty
)
489 void sys_rs232read (struct regs
*r
)
491 *SYSV_RS232READ
= (int) rs232_read ();
494 void sys_rs232write (struct regs
*r
)
496 rs232_write ((char) r
->ebx
);
499 void sys_gttyexit (struct regs
*r
)
501 proc_t
*proc
= proc_find (_curr_task
);
506 tty_change (proc
->tty
);
509 void sys_gexit (struct regs
*r
)
514 void sys_gttyinit (struct regs
*r
)
516 tty_t
*tty
= gtty_init ();
521 r
->eax
= (unsigned) &tty
->screen
;
524 void sys_getenv (struct regs
*r
)
526 char *name
= (char *) r
->ebx
;
528 char *val
= (char *) env_get (name
);
530 r
->eax
= (unsigned) val
;
533 void sys_free (struct regs
*r
)
535 proc_t
*proc
= proc_find (_curr_task
);
539 ufree (proc
, (void *) r
->ebx
);
541 DPRINT (DBG_SYSCALL
, "free (): 0x%x", r
->ebx
);
546 void sys_realloc (struct regs
*r
)
548 proc_t
*proc
= proc_find (_curr_task
);
552 r
->eax
= (unsigned) urealloc (proc
, (void *) r
->ebx
, (size_t) r
->ecx
);
554 DPRINT (DBG_SYSCALL
, "realloc (): 0x%x", r
->eax
);
559 void sys_gethostbyname (struct regs
*r
)
561 char *buf
= (char *) r
->ebx
;
568 r
->eax
= (unsigned) gethostbyname (buf
);
571 void sys_vfsent (struct regs
*r
)
573 char *pathname
= (char *) r
->ebx
;
575 r
->eax
= (unsigned) vfs_find (pathname
, strlen (pathname
));
578 void sys_sendto (struct regs
*r
)
580 /* HACK: bleeh, there is problem with fourth argument,
581 so we need to use another way to getting address */
582 sockaddr_in
*to
= (sockaddr_in
*) *SYSV_SENDTO
;
584 *SYSV_SENDTO
= sendto ((int) r
->ecx
, (char *) r
->ebx
, (unsigned) r
->edx
, 0, to
, 0);
587 void sys_recvfrom (struct regs
*r
)
589 unsigned char *msg
= (unsigned char *) r
->ebx
;
591 /* HACK: bleeh, there is problem with fourth argument,
592 so we need to use another way to getting address */
593 sockaddr_in
*from
= (sockaddr_in
*) *SYSV_RECVFROM
;
595 *SYSV_RECVFROM
= recvfrom ((int) r
->ecx
, (char *) msg
, (unsigned) r
->edx
, 0, from
, 0);
598 void sys_getchar (struct regs
*r
)
600 proc_t
*proc
= proc_find (_curr_task
);
603 unsigned char s
= '\n';
605 int *memptr
= SYSV_GETCHAR
;
607 /* non-blocking mode */
608 if (stdin
->flags
& O_NONBLOCK
) {
612 if (proc
->tty
!= currtty
)
615 unsigned char c
= getkey ();
622 tty_putnch (proc
->tty
, c
);
629 /* blocking - clasical mode */
634 if (proc
->tty
!= currtty
)
640 if (i
> 0 && c
!= '\b')
641 tty_write (proc
->tty
, &c
, 1);
651 tty_write (proc
->tty
, "\b", 1);
661 void sys_threadopen (struct regs
*r
)
663 void *ptr
= (void *) r
->ebx
;
665 unsigned m
= *SYSV_THREADOPEN
;
667 *SYSV_THREADOPEN
= proc_thread_create (ptr
, (void *) m
);
670 void sys_threadclose (struct regs
*r
)
672 proc_t
*proc
= proc_find (_curr_task
);
677 task_t
*task
= _curr_task
;
679 /* free child process thread */
680 *SYSV_THREADCLOSE
= proc_thread_destroy (proc
, task
);
682 task
= proc
->tty
->task
;
685 longjmp (task
->state
, 1);
691 void sys_ioctl (struct regs
*r
)
693 void *s
= (void *) r
->ebx
;
694 unsigned id
= r
->ecx
;
697 *SYSV_IOCTL
= ioctl_call (id
, s
, l
);
700 void sys_getpid (struct regs
*r
)
702 proc_t
*proc
= proc_find (_curr_task
);
707 r
->eax
= (int) &proc
->pid
;
710 void sys_lseek (struct regs
*r
)
713 long offset
= r
->ecx
;
716 *SYSV_LSEEK
= lseek (fd
, offset
, whence
);
719 void sys_pipe (struct regs
*r
)
721 int *p
= (int *) r
->ebx
;
728 void sys_creat (struct regs
*r
)
730 char *file
= (char *) r
->ebx
;
731 unsigned mode
= r
->ecx
; /* TODO: mode */
737 ret
= (touch (file
) == 1) ? 0 : -1;
744 void sys_mkdir (struct regs
*r
)
746 char *dir
= (char *) r
->ebx
;
747 unsigned mode
= r
->ecx
; /* TODO: mode */
753 ret
= (mkdir (dir
) == 1) ? 0 : -1;
760 void sys_rmdir (struct regs
*r
)
762 char *dir
= (char *) r
->ebx
;
767 ret
= (rm (dir
) == 1) ? 0 : -1;
774 void sys_sndopen (struct regs
*r
)
776 snd_cfg_t
*cfg
= (snd_cfg_t
*) r
->ebx
;
783 r
->eax
= (int) audio_open (cfg
);
786 void sys_sndclose (struct regs
*r
)
788 snd_audio_t
*aud
= (snd_audio_t
*) r
->ebx
;
795 r
->eax
= (int) audio_close (aud
);
798 void sys_sndwrite (struct regs
*r
)
800 snd_audio_t
*aud
= (snd_audio_t
*) r
->ebx
;
801 char *buf
= (char *) r
->ecx
;
802 unsigned len
= (unsigned) r
->edx
;
809 r
->eax
= (int) audio_write (aud
, buf
, len
);
812 void sys_remove (struct regs
*r
)
814 char *file
= (char *) r
->ebx
;
819 ret
= (vfs_rm (file
, strlen (file
)) == 1) ? 0 : -1;
826 void sys_rename (struct regs
*r
)
828 char *file
= (char *) r
->ebx
;
829 char *file2
= (char *) r
->ecx
;
834 if (cp (file
, file2
) != 1)
837 if (!ret
&& rm (file
) != 1)
845 void syscall_handler (struct regs
*r
)
851 return sys_getch (r
);
853 return sys_sleep (r
);
855 return sys_putch (r
);
857 return sys_color (r
);
861 return sys_getkey (r
);
863 return sys_gotoxy (r
);
867 return sys_schedule (r
);
869 return sys_write (r
);
871 return sys_socket (r
);
873 return sys_connect (r
);
875 return sys_malloc (r
);
881 return sys_close (r
);
885 return sys_pcspk (r
);
887 return sys_usleep (r
);
893 return sys_system (r
);
895 return sys_chdir (r
);
897 return sys_getdir (r
);
899 return sys_procarg (r
);
901 return sys_signal (r
);
903 return sys_mount (r
);
905 return sys_kputs (r
);
909 return sys_listen (r
);
911 return sys_accept (r
);
913 return sys_fcntl (r
);
915 return sys_gvgafb (r
);
919 return sys_gfbswap (r
);
921 return sys_rs232read (r
);
923 return sys_rs232write (r
);
925 return sys_gttyexit (r
);
927 return sys_gexit (r
);
929 return sys_gttyinit (r
);
931 return sys_getenv (r
);
935 return sys_realloc (r
);
937 return sys_gethostbyname (r
);
939 return sys_vfsent (r
);
941 return sys_sendto (r
);
943 return sys_recvfrom (r
);
945 return sys_getchar (r
);
947 return sys_threadopen (r
);
949 return sys_threadclose (r
);
951 return sys_ioctl (r
);
953 return sys_getpid (r
);
955 return sys_lseek (r
);
959 return sys_creat (r
);
961 return sys_mkdir (r
);
963 return sys_rmdir (r
);
965 return sys_sndopen (r
);
967 return sys_sndclose (r
);
969 return sys_sndwrite (r
);
971 return sys_remove (r
);
973 return sys_rename (r
);