00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "core/kint.h"
00019 #include "dev/keyboard/keyboard.h"
00020 #include "core/ktime.h"
00021 #include "common/kconst.h"
00022 #include "proc/proc.h"
00023 #include "mm/mm.h"
00024
00025 #include "common/ds/queue.h"
00026 #include "dev/device.h"
00027 #include "dev/floppy/floppy.h"
00028 #include "fs/fat12.h"
00029
00030
00031 #include "core/message.h"
00032 #include "core/kstdio.h"
00033 #include "dev/mouse/mouse.h"
00034 #include "gui/svga.h"
00035
00036 #include "core/ktask.h"
00037
00038 extern void post_vm86();
00039
00040 DB which_int;
00041 DW floppy_int_pending = 0;
00042
00043 void fault(REGS *regs)
00044 {
00045 DD i;
00046 DW j;
00047 DD temp;
00048 DD cr2 = 0, cr3;
00049
00050 cli();
00051
00052
00053 cr3 = (cr3 & 0xFFF) | (((DD)_kpgde) & 0xFFFFF000);
00054 __asm__ __volatile__ ("movl %%eax,%%cr3"::"a"(cr3));
00055
00056 which_int = regs->which_int;
00057
00058 if(cur_pid != MAX_PROC+1)
00059 {
00060 _proc[cur_pid].eax = regs->eax;
00061 _proc[cur_pid].ebx = regs->ebx;
00062 _proc[cur_pid].ecx = regs->ecx;
00063 _proc[cur_pid].edx = regs->edx;
00064 _proc[cur_pid].esi = regs->esi;
00065 _proc[cur_pid].edi = regs->edi;
00066 _proc[cur_pid].ebp = regs->ebp;
00067
00068 _proc[cur_pid].eip = regs->eip;
00069
00070 _proc[cur_pid].cs = regs->cs;
00071 _proc[cur_pid].ds = regs->ds;
00072 _proc[cur_pid].ss = regs->user_ss;
00073 _proc[cur_pid].es = regs->es;
00074 _proc[cur_pid].esp = regs->user_esp;
00075
00076 _proc[cur_pid].fs = regs->fs;
00077 _proc[cur_pid].gs = regs->gs;
00078
00079 _proc[cur_pid].ldt_sel = LDT_USR_SEL * 8;
00080 _proc[cur_pid].eflags = regs->eflags;
00081
00082 if(cur_pid == GUI_PID || cur_pid == FLOPPY_PID || cur_pid == FS_PID)
00083 {
00084 _proc[cur_pid].ss = regs->_ss;
00085
00086 _proc[cur_pid].esp = regs->_esp + 4 + 4 + 4 + 4;
00087 }
00088
00089 }
00090 else
00091 {
00092 cur_pid = 0;
00093 }
00094
00095 i = regs->which_int;
00096 if(i != 0x20)
00097 {
00098 if(i == FLOPPY_INT && floppy_int_pending == 0)
00099 floppy_int_pending = 1;
00100
00101 for(j=0;j<intr_q.n;j++)
00102 {
00103 temp = get_element(&intr_q,j);
00104
00105 if(temp != E_FAILURE)
00106 {
00107 if(_proc[temp].wait_int_num == i)
00108 {
00109 printf("temp = %d",temp);
00110 remove(&intr_q,temp);
00111 remove(&timer_q,temp);
00112 enq(&ready_q,temp);
00113 _proc[temp].wait_int_num ==0;
00114 _proc[temp].eax = 0;
00115 if(i == FLOPPY_INT)
00116 floppy_int_pending = 0;
00117 }
00118 }
00119 }
00120 }
00121
00122 if(i == 0xE)
00123 {
00124 asm("movl %%cr2, %%eax":"=a"(cr2));
00125 printf("\nPg Fault: %x",cr2);
00126 }
00127
00128 if(i != 0x21 && i != 0x20)
00129 printf("\nAsm exception %x", i);
00130
00131 if(i < 0x20)
00132 {
00133 asm __volatile__("pushf");
00134 asm __volatile__("popl %%eax":"=a"(temp));
00135 printf("\nRogue pid %d eflags : %x",cur_pid,temp);
00136
00137
00138 switch(i)
00139 {
00140 case DIVIDE_ERROR:
00141 case INVALID_OPCODE:
00142 case DOUBLE_FAULT:
00143 case BAD_TSS:
00144 case GPF:
00145 case PAGE_FAULT:
00146 printf("\n_proc[].cr3 %x sys.cr3 %x",_proc[cur_pid].cr3,_system.cr3);
00147 if(graphics)
00148 {
00149 drawstring(50,200,"Sys Exception");
00150 drawstring(50,230,itoa(cur_pid,10));
00151 drawstring(50,240,itoa(i%10,10));
00152 mvBuf2Svga();
00153 }
00154
00155 if(_proc[cur_pid].eflags & (1<<17))
00156 {
00157 handleVM86(regs);
00158 }
00159 else
00160 {
00161 remove(&ready_q,cur_pid);
00162 do_exit(cur_pid);
00163 }
00164 schedule();
00165 break;
00166
00167 default:
00168 kdump();
00169 panic(itoa(which_int,10));
00170 break;
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 switch(which_int)
00184 {
00185 case 0x20:
00186 handle_timer();
00187 eoi();
00188 schedule();
00189 break;
00190
00191 case 0x21:
00192 handle_KBD();
00193 eoi();
00194 schedule();
00195 break;
00196
00197 case 0x26:
00198 printf("\FLOPPY Interrupt");
00199 eoi();
00200 schedule();
00201 break;
00202
00203 case 0x30:
00204 handle_syscall(regs);
00205 schedule();
00206 break;
00207
00208 case 0x2C:
00209 printf("\nMouse Interrupt\n");
00210 handle_mouse();
00211 schedule();
00212 break;
00213
00214 default:
00215 panic("y");
00216 }
00217
00218 sti();
00219 }
00220
00221 DD timer_i = 0;
00222
00223 extern void _Z12cursor_paintv();
00224
00225 void handle_timer()
00226 {
00227 DW i,temp;
00228 SEG_DESC *t;
00229
00230 timer_i++;
00231
00232 if(graphics)
00233 _Z12cursor_paintv();
00234
00235 if(timer_i%2000 == 0)
00236 {
00237 _Z9draw_timeh(1);
00238 }
00239
00240 if(timer_i%500 ==0)
00241 {
00242 for(i=0;i<timer_q.n;i++)
00243 {
00244 temp = get_element(&timer_q,i);
00245 if(temp != E_FAILURE)
00246 {
00247 if(_proc[temp].time_out == timer_i/500)
00248 {
00249 if(remove(&intr_q,temp) != -1)
00250 _proc[temp].eax = -1;
00251 else
00252 _proc[temp].eax = 0;
00253 remove(&timer_q,temp);
00254 enq(&ready_q,temp);
00255 }
00256 }
00257
00258 }
00259
00260 }
00261
00262 }
00263
00264 void handle_syscall(REGS * regs)
00265 {
00266 DW i,num;
00267 DD linear, phy;
00268 DD linear1, phy1;
00269 DD str_addr;
00270 DD msg_ptr;
00271 DW pid;
00272 SDW index;
00273 DD offs;
00274 DD buf;
00275 request r;
00276 fs_request fs_r;
00277 DD base;
00278 DD u_esp,u_ss;
00279 u_esp = regs->user_esp;
00280 u_ss = regs->user_ss;
00281
00282 if(cur_pid == FLOPPY_PID || cur_pid == GUI_PID || cur_pid == FS_PID)
00283 {
00284 u_esp = _proc[cur_pid].esp;
00285 u_ss = _proc[cur_pid].ss;
00286 }
00287
00288 linear = logi_to_lin(cur_pid,u_ss,u_esp);
00289 phy = lin_to_phy(cur_pid,linear);
00290
00291 switch(regs->eax)
00292 {
00293 case 0:
00294 do_exit(cur_pid);
00295 break;
00296 case 1:
00297 printf("\n\nCreate called");
00298 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00299 phy1 = lin_to_phy(cur_pid,linear1);
00300 create(phy1);
00301 break;
00302 case 2:
00303 printf("\n\nFork called");
00304 delay(0x1FF);
00305 break;
00306 case 3:
00307 pid = *(DD *) phy;
00308 msg_ptr = *(DD *) (phy + 4);
00309
00310 if(pid == GUI_PID)
00311 {
00312 printf("\nSEND:GUI:rem");
00313 remove(&ready_q,cur_pid);
00314 enq(&gui_q,cur_pid);
00315 }
00316
00317 send(pid,(MSG *) msg_ptr);
00318 break;
00319
00320 case 4:
00321 msg_ptr = *(DD *) phy;
00322 recv((MSG *) msg_ptr);
00323 break;
00324
00325 case 5:
00326 _proc[cur_pid].time_out = *(DD *) phy + timer_i/500;
00327 printf("time out %d,cur_time %d",_proc[cur_pid].time_out,timer_i);
00328 remove(&ready_q,cur_pid);
00329 enq(&timer_q,cur_pid);
00330 break;
00331
00332 case 6:
00333 _proc[cur_pid].wait_int_num = *(DD *) phy;
00334 printf("wait %d",_proc[cur_pid].wait_int_num);
00335 remove(&ready_q,cur_pid);
00336 enq(&intr_q,cur_pid);
00337 break;
00338
00339 case 7:
00340 printf("\nwaiting for both int and timer");
00341 if(*(DD *) phy == FLOPPY_INT && floppy_int_pending)
00342 {
00343 _proc[cur_pid].eax = 0;
00344 floppy_int_pending = 0;
00345 }
00346 else
00347 {
00348 _proc[cur_pid].wait_int_num = *(DD *) phy;
00349 _proc[cur_pid].time_out = *(DD *) (phy+4) + timer_i/500;
00350 remove(&ready_q,cur_pid);
00351 enq(&timer_q,cur_pid);
00352 enq(&intr_q,cur_pid);
00353 printf("\ntimer %x intr %x",_proc[cur_pid].time_out,_proc[cur_pid].wait_int_num);
00354 }
00355 break;
00356
00357 case 8:
00358 printf("floppy");
00359 offs = *(DD*)phy;
00360 buf = *(DD *) (phy + 4);
00361 r.r_count = 512;
00362 r.offset = offs*512;
00363 r.opcode = 1;
00364 r.r_dma = buf;
00365 r.pid = cur_pid;
00366
00367 if(addreq(&r) == -1)
00368 {
00369 _proc[cur_pid].eax = -1;
00370 }
00371 else
00372 {
00373 remove(&ready_q,cur_pid);
00374 enq(&device_q[FLOPPY_DEV],cur_pid);
00375 }
00376
00377 printf("offs %d buf %x no_req %d",offs,buf,no_req);
00378 break;
00379
00380 case 9:
00381 printf("floppy");
00382 offs = *(DD*)phy;
00383 buf = *(DD *) (phy + 4);
00384 r.r_count = 512;
00385 r.offset = offs*512;
00386 r.opcode = 2;
00387 r.r_dma = buf;
00388 r.pid = cur_pid;
00389
00390 if(addreq(&r) == -1)
00391 {
00392 _proc[cur_pid].eax = -1;
00393 }
00394 else
00395 {
00396 remove(&ready_q,cur_pid);
00397 enq(&device_q[FLOPPY_DEV],cur_pid);
00398 }
00399 printf("write offs %d buf %x no_req %d",offs,buf,no_req);
00400 break;
00401
00402 case 10:
00403 num = *(DD*)phy;
00404 base = *(DD *) (phy + 4);
00405 printf("\nnum : %x %d",num,base);
00406 delay(0xFFFF);
00407 break;
00408
00409 case 11:
00410 printf("\nsyscall:11");
00411 if(*(DW*)phy >= MAX_PROC)
00412 panic("y");
00413
00414 remove(&gui_q, *(DW*)phy);
00415 _proc[*(DW*)phy].eax = *(DW*)(phy+4);
00416 enq(&ready_q, *(DW*)phy);
00417 printf("\n pid %d retval %d",*(DW*)phy,*(DW*)(phy+4));
00418 break;
00419
00420 case 12:
00421 printf("\nfs processing over");
00422 if(*(DW*)phy >= MAX_PROC)
00423 panic("y");
00424 remove(&fs_q, *(DW*)phy);
00425
00426 _proc[*(DW*)phy].eax = *(DW*)(phy+4);
00427 enq(&ready_q, *(DW*)phy);
00428
00429 printf("\n pid %d retval %d",*(DW*)phy,*(DW*)(phy+4));
00430
00431 if(*(SDW*)(phy+4) == -1 && *(DW*)(phy+8) == LOAD)
00432 {
00433 printf("\n\ninside exit :");
00434 do_exit(*(DW*)phy);
00435 }
00436 break;
00437
00438 case 13:
00439 fs_r.type = OPEN;
00440 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00441 phy1 = lin_to_phy(cur_pid,linear1);
00442
00443 fs_r.op.fname = (DB *)phy1;
00444 fs_r.op.mode = *(DW *) (phy + 4);
00445 fs_r.from_pid = cur_pid;
00446
00447 if(addfsreq(&fs_r) != 0)
00448 {
00449 _proc[cur_pid].eax = -1;
00450 }
00451 else
00452 {
00453 remove(&ready_q,cur_pid);
00454 enq(&fs_q,cur_pid);
00455 }
00456
00457 printf("\n fname %s, mode %d",fs_r.op.fname,fs_r.op.mode);
00458 break;
00459
00460 case 14:
00461 fs_r.type = CLOSE;
00462
00463 fs_r.cl.fd_in = *(DW *) (phy);
00464 fs_r.from_pid = cur_pid;
00465
00466 if(addfsreq(&fs_r) != 0)
00467 {
00468 _proc[cur_pid].eax = -1;
00469 }
00470 else
00471 {
00472 remove(&ready_q,cur_pid);
00473 enq(&fs_q,cur_pid);
00474 }
00475
00476 printf("close : fd_in %d\n",fs_r.cl.fd_in);
00477 break;
00478
00479 case 15:
00480 fs_r.type = READ;
00481
00482 fs_r.from_pid = cur_pid;
00483 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)(phy+4));
00484 phy1 = lin_to_phy(cur_pid,linear1);
00485
00486 fs_r.re.buf = (DB *)phy1;
00487 fs_r.re.fd_in = *(DW *) (phy);
00488 fs_r.re.len = *(DW *) (phy+8);
00489
00490 if(addfsreq(&fs_r) != 0)
00491 {
00492 _proc[cur_pid].eax = -1;
00493 }
00494 else
00495 {
00496 remove(&ready_q,cur_pid);
00497 enq(&fs_q,cur_pid);
00498 }
00499
00500 break;
00501
00502 case 16:
00503 fs_r.type = WRITE;
00504
00505 fs_r.from_pid = cur_pid;
00506 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)(phy+4));
00507 phy1 = lin_to_phy(cur_pid,linear1);
00508
00509 fs_r.wr.buf = (DB *)phy1;
00510 fs_r.wr.fd_in = *(DW *) (phy);
00511 fs_r.wr.len = *(DW *) (phy+8);
00512
00513 if(addfsreq(&fs_r) != 0)
00514 {
00515 _proc[cur_pid].eax = -1;
00516 }
00517 else
00518 {
00519 remove(&ready_q,cur_pid);
00520 enq(&fs_q,cur_pid);
00521 }
00522 break;
00523
00524 case 17:
00525 fs_r.type = CREATE;
00526 fs_r.from_pid = cur_pid;
00527 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00528 phy1 = lin_to_phy(cur_pid,linear1);
00529
00530 fs_r.cr.name = (DB *)phy1;
00531
00532 if(addfsreq(&fs_r) != 0)
00533 {
00534 _proc[cur_pid].eax = -1;
00535 }
00536 else
00537 {
00538 remove(&ready_q,cur_pid);
00539 enq(&fs_q,cur_pid);
00540 }
00541 break;
00542
00543 case 18 :
00544 fs_r.type = OPENDIR;
00545 fs_r.from_pid = cur_pid;
00546 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00547 phy1 = lin_to_phy(cur_pid,linear1);
00548
00549 fs_r.opd.dir_name = (DB *)phy1;
00550
00551 if(addfsreq(&fs_r) != 0)
00552 {
00553 _proc[cur_pid].eax = -1;
00554 }
00555 else
00556 {
00557 remove(&ready_q,cur_pid);
00558 enq(&fs_q,cur_pid);
00559 }
00560 break;
00561
00562 case 19:
00563 fs_r.type = CREATEDIR;
00564 fs_r.from_pid = cur_pid;
00565 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00566 phy1 = lin_to_phy(cur_pid,linear1);
00567
00568 fs_r.crd.name = (DB *)phy1;
00569
00570 if(addfsreq(&fs_r) != 0)
00571 {
00572 _proc[cur_pid].eax = -1;
00573 }
00574 else
00575 {
00576 remove(&ready_q,cur_pid);
00577 enq(&fs_q,cur_pid);
00578 }
00579 break;
00580
00581 case 20:
00582 fs_r.type = CLOSEDIR;
00583 fs_r.from_pid = cur_pid;
00584 fs_r.cld.dd_in = *(DW *) (phy);
00585
00586 if(addfsreq(&fs_r) != 0)
00587 {
00588 _proc[cur_pid].eax = -1;
00589 }
00590 else
00591 {
00592 remove(&ready_q,cur_pid);
00593 enq(&fs_q,cur_pid);
00594 }
00595 break;
00596
00597 case 21:
00598 fs_r.type = READDIR;
00599 fs_r.from_pid = cur_pid;
00600 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)(phy+4));
00601 phy1 = lin_to_phy(cur_pid,linear1);
00602
00603 fs_r.red.dir = (DIRENT *)phy1;
00604 fs_r.red.dd_in = *(DW *) (phy);
00605
00606 if(addfsreq(&fs_r) != 0)
00607 {
00608 _proc[cur_pid].eax = -1;
00609 }
00610 else
00611 {
00612 remove(&ready_q,cur_pid);
00613 enq(&fs_q,cur_pid);
00614 }
00615 break;
00616
00617 case 22:
00618 fs_r.type = REMOVE;
00619 fs_r.from_pid = cur_pid;
00620
00621 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00622 phy1 = lin_to_phy(cur_pid,linear1);
00623
00624 fs_r.rem.name = (DB *)phy1;
00625
00626 if(addfsreq(&fs_r) != 0)
00627 {
00628 _proc[cur_pid].eax = -1;
00629 }
00630 else
00631 {
00632 remove(&ready_q,cur_pid);
00633 enq(&fs_q,cur_pid);
00634 }
00635 break;
00636
00637 case 23:
00638 fs_r.type = RENAME;
00639
00640 fs_r.from_pid = cur_pid;
00641 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00642 phy1 = lin_to_phy(cur_pid,linear1);
00643
00644 fs_r.ren.old_name = (DB *)phy1;
00645
00646 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)(phy+4));
00647 phy1 = lin_to_phy(cur_pid,linear1);
00648
00649 fs_r.ren.new_name = (DB *)phy1;
00650
00651 if(addfsreq(&fs_r) != 0)
00652 {
00653 _proc[cur_pid].eax = -1;
00654 }
00655 else
00656 {
00657 remove(&ready_q,cur_pid);
00658 enq(&fs_q,cur_pid);
00659 }
00660 break;
00661
00662 case 30:
00663 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00664 phy1 = lin_to_phy(cur_pid,linear1);
00665 getdate((DATE *)phy1);
00666 break;
00667
00668 case 31:
00669 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00670 phy1 = lin_to_phy(cur_pid,linear1);
00671 setdate((DATE *)phy1);
00672 break;
00673
00674 case 32:
00675 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00676 phy1 = lin_to_phy(cur_pid,linear1);
00677 gettime((TIME *)phy1);
00678 break;
00679
00680 case 33:
00681 linear1 = logi_to_lin(cur_pid,_proc[cur_pid].cs,*(DD*)phy);
00682 phy1 = lin_to_phy(cur_pid,linear1);
00683 settime((TIME *)phy1);
00684 break;
00685
00686 case 255:
00687 printf("\n2^X");
00688
00689 delay(0x1FF);
00690 linear = logi_to_lin(cur_pid,u_ss,u_esp);
00691 phy = lin_to_phy(cur_pid,linear);
00692
00693 str_addr = *(DD *)phy;
00694
00695 linear = logi_to_lin(cur_pid,_proc[cur_pid].cs,str_addr);
00696 phy = lin_to_phy(cur_pid,linear);
00697
00698 for(i = 0 ; i < 10 ; i++)
00699 {
00700 printf("%x",*(DB *) (phy + i));
00701 }
00702
00703 break;
00704
00705 default:
00706 printf("\n\nUnknown SysCall Interface invoked.");
00707 delay(0xFFF);
00708 break;
00709 }
00710 }
00711
00712 void do_pow(DW val)
00713 {
00714 _proc[cur_pid].eax = 2*val;
00715 printf("\ndo_pow %d", _proc[cur_pid].eax);
00716 }
00717
00718 void eoi()
00719 {
00720 if(which_int >= 0x28 && which_int <= 0x2F)
00721 {
00722 outb(0xA0,0x20);
00723 outb(0x20,0x20);
00724 }
00725 else if(which_int >= 0x20 && which_int < 0x28)
00726 {
00727 outb(0x20,0x20);
00728 }
00729 }