Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / arch / x86_64 / syscall.c
blob354b012fdd38fe09fe3f674032937ec53bdc67ca
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <system.h>
22 #include <arch/io.h>
23 #include <string.h>
24 #include <tty.h>
25 #include <proc.h>
26 #include <net/socket.h>
27 #include <file.h>
28 #include <signal.h>
29 #include <module.h>
30 #include <rs232.h>
32 int sattrib = 0x0F;
34 extern task_t *_curr_task;
35 void sys_exit (struct regs *r)
37 proc_t *proc = proc_find (_curr_task);
39 if (!proc)
40 return;
42 if (proc->task != _curr_task)
43 return;
45 if (proc) {
46 task_t *task = proc->tty->task;
48 proc->pid = 0;
50 /*if (int_disable ())
51 longjmp (task->state, 1);
52 else
53 while (1)
54 schedule ();*/
58 void sys_getch (struct regs *r)
60 unsigned att = sattrib << 8;
61 unsigned short *where;
63 unsigned char c = (unsigned char) getkey ();
65 if (c != 0) {
66 setkey (0);
69 unsigned short *memptr = (unsigned short *) 0x9000;
70 where = memptr;
71 *where = c | att;
75 void sys_sleep (struct regs *r)
77 timer_wait (1000 * (unsigned) r->r9);
80 void sys_putch (struct regs *r)
82 proc_t *proc = proc_find (_curr_task);
84 if (!proc)
85 return;
87 if (proc->task != _curr_task)
88 return;
90 if (proc->tty != currtty)
91 return;
93 tty_putnch (proc->tty, r->r9);
96 void sys_color (struct regs *r)
98 settextcolor (r->r9, r->r10);
102 void sys_cls (struct regs *r)
104 proc_t *proc = proc_find (_curr_task);
106 if (!proc)
107 return;
109 if (proc->task != _curr_task)
110 return;
112 if (proc->tty != currtty)
113 return;
115 int_disable ();
116 tty_cls (proc->tty);
117 int_enable ();
120 extern unsigned char scancode;
121 void sys_getkey (struct regs *r)
123 proc_t *proc = proc_find (_curr_task);
125 if (!proc)
126 return;
128 if (proc->task != _curr_task)
129 return;
131 if (proc->tty != currtty)
132 return;
134 unsigned att = sattrib << 8;
135 unsigned short *where;
136 unsigned char c = (unsigned char) scancode;
138 scancode = 0;
140 unsigned short *memptr = (unsigned short *) 0x9000;
141 where = memptr;
142 *where = c | att;
145 void sys_gotoxy (struct regs *r)
147 gotoxy (r->r9, r->r10);
150 void sys_fork (struct regs *r)
152 unsigned att = sattrib << 8;
153 unsigned short *where;
154 unsigned char c = (unsigned char) fork ();
156 unsigned short *memptr = (unsigned short *) 0x9000;
157 where = memptr;
158 *where = c | att;
161 void sys_schedule (struct regs *r)
163 schedule ();
166 extern kbd_quaue kbd_q;
167 void sys_write (struct regs *r)
169 unsigned len = r->r10;
170 unsigned fd = r->r11;
172 unsigned char *buf = (unsigned char *) r->r9;
174 if (!buf)
175 return;
177 //kprintf ("#buf: 0x%x / '%s'\n", buf, buf);
179 /*char buf[len+1];
180 if (!memcpy_to_user (&buf, (char *) r->r9, len))
181 return;*/
183 int ret = 0;
185 proc_t *proc = 0;
187 switch (fd) {
188 case 0:
189 proc = proc_find (_curr_task);
191 if (!proc)
192 return;
194 if (proc->task != _curr_task)
195 return;
197 //if (proc->tty != currtty)
198 // return;
200 //printf ("data: 0x%x | 0x%x | 0x%x\n", buf, proc->data, proc->data_off);
201 //printf ("hehe: 0x%x+0x%x / '%s' len: %d\n", buf+proc->code, proc->data_off, buf+proc->code, len);
202 //if (!memtest_data (buf, (void *) proc->data, proc->data_off))
203 tty_write (proc->tty, (char *) buf, len);
204 //else {
205 // tty_write (proc->tty, (char *) buf+proc->code, len);
207 ret = len;
208 break;
209 case 1:
210 proc = proc_find (_curr_task);
212 if (!proc)
213 return;
215 if (proc->task != _curr_task)
216 return;
218 if (len > KBD_MAX_QUAUE)
219 len = KBD_MAX_QUAUE;
221 int i = 0;
222 for (i = len; i >= 0; i --) {
223 setkey (buf[i]);
224 kbd_q.state[kbd_q.p] = 1; // down
225 kbd_q.p ++;
228 buf[len] = '\0';
230 ret = len;
231 break;
232 default:
233 ret = write (fd, (char *) buf, len);
234 break;
237 int *where;
238 int *memptr = (int *) 0x9000;
239 where = memptr;
240 *where = ret;
242 //printf ("yeah, sys_write: '%d' '%s' '0x%x'\n", len, buf, buf);
245 void sys_socket (struct regs *r)
247 int ret = socket (r->r9, r->r10, r->r11);
249 int *where;
250 int *memptr = (int *) 0x9000;
251 where = memptr;
252 *where = ret;
255 void sys_connect (struct regs *r)
257 sockaddr_in *addr = (sockaddr_in *) r->r9;
259 /*if (!memtest_data (addr, (void *) proc->data, proc->data_off))
260 tty_write (proc->tty, (char *) buf, len);
261 else {
262 // printf ("hehe: 0x%x+0x%x / '%s' len: %d\n", buf+proc->code, proc->data_off, buf+proc->code, len);
263 tty_write (proc->tty, (char *) buf+proc->code, len);
264 } */
265 //printf ("adresa mem: %s - 0x%x\n", addr->sin_addr, addr);
266 int ret = connect (r->r10, addr, r->r11);
268 int *where;
269 int *memptr = (int *) 0x9000;
270 where = memptr;
271 *where = ret;
274 void sys_malloc (struct regs *r)
276 r->r8 = (unsigned) malloc ((int) r->r9);
278 DPRINT ("malloc (): 0x%x\n", r->r8);
281 void sys_send (struct regs *r)
283 /* char msg[r->r11+1];
284 if (!memcpy_to_user (&msg, (char *) r->r9, r->r11))
285 return;*/
287 int ret = send ((int) r->r10, (char *) r->r9, (unsigned) r->r11, 0);
289 int *where;
290 int *memptr = (int *) 0x9000;
291 where = memptr;
292 *where = ret;
295 void sys_recv (struct regs *r)
297 unsigned char *msg = (unsigned char *) r->r9;
299 int ret = recv ((int) r->r10, (char *) msg, (unsigned) r->r11, 0);
301 // printf ("recv> r->r9: %d, r->r11: %u, ret: %d\n", (int) r->r9, (unsigned) r->r11, ret);
303 int *where;
304 int *memptr = (int *) 0x9000;
305 where = memptr;
306 *where = ret;
309 void sys_close (struct regs *r)
311 close (r->r9);
314 void sys_open (struct regs *r)
316 unsigned char *pathname = (unsigned char *) r->r9;
318 int ret = open ((char *) pathname, (unsigned) r->r10);
320 // printf ("%d = sys_open (%s, %u)\n", ret, (char *) pathname, (unsigned) r->r10);
322 int *where;
323 int *memptr = (int *) 0x9000;
324 where = memptr;
325 *where = ret;
328 void sys_pcspk (struct regs *r)
330 dev_t *dev = dev_find ("/dev/pcspk");
332 if (dev)
333 dev->handler (DEV_ACT_PLAY, (unsigned) r->r9);
336 void sys_usleep (struct regs *r)
338 usleep ((unsigned) r->r9);
341 void sys_read (struct regs *r)
343 unsigned fd = r->r10;
344 unsigned len = r->r11;
346 unsigned char *buf = (unsigned char *) r->r9;
348 int ret = 0;
350 proc_t *proc = 0;
352 switch (fd) {
353 case 0:
354 break;
355 case 1:
356 proc = proc_find (_curr_task);
358 if (!proc)
359 break;
361 if (proc->task != _curr_task)
362 break;
364 //if (proc->tty != currtty)
365 // return;
367 ret = tty_read (proc->tty, (char *) buf, len);
368 break;
369 default:
370 ret = read (fd, (char *) buf, len);
371 break;
374 int *where;
375 int *memptr = (int *) 0x9000;
376 where = memptr;
377 *where = ret;
380 void sys_time (struct regs *r)
382 r->r8 = (unsigned) rtc_getcurrtime ();
385 void sys_system (struct regs *r)
387 unsigned char *cmd = (unsigned char *) r->r9;
389 command_parser ((char *) cmd, strlen (cmd));
392 void sys_chdir (struct regs *r)
394 unsigned char *dir = (unsigned char *) r->r9;
396 cd ((char *) dir);
399 void sys_getdir (struct regs *r)
401 r->r8 = (unsigned) &dir;
404 void sys_procarg (struct regs *r)
406 proc_t *proc = proc_find (_curr_task);
408 if (!proc)
409 return;
411 if (proc->task != _curr_task)
412 return;
414 switch (r->r10) {
415 case 0:
416 r->r8 = (unsigned) proc->argv;
417 break;
418 case 1:
419 r->r8 = (unsigned) proc->argc;
420 break;
421 default:
422 r->r8 = 0;
426 void sys_signal (struct regs *r)
428 signal (r->r9, (sighandler_t) r->r10);
431 void sys_mount (struct regs *r)
433 partition_t *p = partition_find ((char *) r->r9);
435 if (p) {
436 mount (p, "", (char *) r->r10);
437 r->r8 = 1;
438 } else
439 r->r8 = 0;
442 void sys_kputs (struct regs *r)
444 int_disable ();
446 module_t *kmod = module_find (_curr_task);
448 if (!kmod)
449 return;
451 if (kmod->task != _curr_task)
452 return;
454 unsigned char *buf = (unsigned char *) r->r9;
455 kprintf (buf);
457 //kprintf ("yeah, sys_kputs: '%s' '0x%x'\n", buf, buf);
459 int_enable ();
462 void sys_bind (struct regs *r)
464 int ret = bind (r->r10, r->r9, r->r11);
466 int *where;
467 int *memptr = (int *) 0x9000;
468 where = memptr;
469 *where = ret;
472 void sys_listen (struct regs *r)
474 int ret = listen (r->r9, r->r10);
476 int *where;
477 int *memptr = (int *) 0x9000;
478 where = memptr;
479 *where = ret;
482 void sys_accept (struct regs *r)
484 int ret = accept (r->r10, r->r9, r->r11);
486 int *where;
487 int *memptr = (int *) 0x9000;
488 where = memptr;
489 *where = ret;
492 void sys_fcntl (struct regs *r)
494 int ret = fcntl (r->r9, r->r10, r->r11);
496 int *where;
497 int *memptr = (int *) 0x9000;
498 where = memptr;
499 *where = ret;
502 void sys_gvgafb (struct regs *r)
504 r->r8 = (unsigned) init_vgafb ();
507 void sys_gcls (struct regs *r)
509 gcls (r->r9);
512 void sys_gpixel (struct regs *r)
514 gpixel (r->r9, r->r10, r->r11);
517 void sys_rs232read (struct regs *r)
519 char *where;
520 char *memptr = (char *) 0x9000;
521 where = memptr;
522 *where = rs232_read ();
525 void sys_rs232write (struct regs *r)
527 rs232_write ((char) r->r9);
530 void sys_gttyexit (struct regs *r)
532 proc_t *proc = proc_find (_curr_task);
534 if (!proc)
535 return;
537 if (proc->task != _curr_task)
538 return;
540 tty_change (proc->tty);
543 void sys_gexit (struct regs *r)
545 gexit ();
548 extern unsigned char vgagui;
549 extern tty_t *gtty;
550 void sys_gttyinit (struct regs *r)
552 gtty_init ();
554 if (!gtty)
555 return;
557 r->r8 = (unsigned) &gtty->screen;
560 void sys_mkdir (struct regs *r)
562 unsigned char *dir = (unsigned char *) r->r9;
564 mkdir ((char *) dir);
567 void sys_free (struct regs *r)
569 free ((void *) r->r9);
571 DPRINT ("free (): 0x%x\n", r->r9);
574 void sys_realloc (struct regs *r)
576 r->r8 = (unsigned) realloc ((void *) r->r9, (size_t) r->r10);
578 DPRINT ("realloc (): 0x%x\n", r->r8);
581 void syscall_handler (struct regs *r)
583 switch (r->r8) {
584 case 1:
585 return sys_exit (r);
586 case 2:
587 return sys_getch (r);
588 case 3:
589 return sys_sleep (r);
590 case 4:
591 return sys_putch (r);
592 case 5:
593 return sys_color (r);
594 case 6:
595 return sys_cls (r);
596 case 7:
597 return sys_getkey (r);
598 case 8:
599 return sys_gotoxy (r);
600 case 9:
601 return sys_fork (r);
602 case 10:
603 return sys_schedule (r);
604 case 11:
605 return sys_write (r);
606 case 12:
607 return sys_socket (r);
608 case 13:
609 return sys_connect (r);
610 case 14:
611 return sys_malloc (r);
612 case 15:
613 return sys_send (r);
614 case 16:
615 return sys_recv (r);
616 case 17:
617 return sys_close (r);
618 case 18:
619 return sys_open (r);
620 case 19:
621 return sys_pcspk (r);
622 case 20:
623 return sys_usleep (r);
624 case 21:
625 return sys_read (r);
626 case 22:
627 return sys_time (r);
628 case 23:
629 return sys_system (r);
630 case 24:
631 return sys_chdir (r);
632 case 25:
633 return sys_getdir (r);
634 case 26:
635 return sys_procarg (r);
636 case 27:
637 return sys_signal (r);
638 case 28:
639 return sys_mount (r);
640 case 29:
641 return sys_kputs (r);
642 case 30:
643 return sys_bind (r);
644 case 31:
645 return sys_listen (r);
646 case 32:
647 return sys_accept (r);
648 case 33:
649 return sys_fcntl (r);
650 case 34:
651 return sys_gvgafb (r);
652 case 35:
653 return sys_gcls (r);
654 case 36:
655 return sys_gpixel (r);
656 case 37:
657 return sys_rs232read (r);
658 case 38:
659 return sys_rs232write (r);
660 case 39:
661 return sys_gttyexit (r);
662 case 40:
663 return sys_gexit (r);
664 case 41:
665 return sys_gttyinit (r);
666 case 42:
667 return sys_mkdir (r);
668 case 43:
669 return sys_free (r);
670 case 44:
671 return sys_realloc (r);