//IDLE 쓰레드의 핸들러 함수 //계속해서 태스크 스위칭(HalTaskSwitch)을 시도 static DWORD PspIdleThread(PVOID StartContext) { while(1) { HalTaskSwitch(); } return 0; }
//키보드로부터 입력받은 데이터를 가공하는 함수(raw_keydata_q -> user_keydata_q) static DWORD KbdpTranslatorThread(PVOID StartContext) { KBD_KEY_DATA key_data; BYTE raw_key, indicator, key_family; while(1) { if(!KbdpPopRawKeyData(&(m_KbdData.raw_keydata_q), &raw_key)){ HalTaskSwitch(); continue; } if(raw_key & 0x80) { if(raw_key == 0xaa || raw_key == 0xb6) m_KbdData.shift_key_pressed = FALSE; else if(raw_key == 0xb8) m_KbdData.alt_key_pressed = FALSE; else if(raw_key == 0x9d) m_KbdData.ctrl_key_pressed = FALSE; continue; } if(raw_key == 0x45) { indicator = KBD_LED_NUMLOCK; goto $reset; } else if (raw_key == 0x3a) { indicator = KBD_LED_CAPSLOCK; goto $reset; } else if(raw_key == 0x46) { indicator = KBD_LED_SCROLLLOCK; $reset: if(m_KbdData.indicator_status & indicator) { m_KbdData.indicator_status &= (~indicator); } else { m_KbdData.indicator_status |= indicator; } KbdpResetIndicator(); continue; } if(raw_key == 0x2a || raw_key == 0x36) { m_KbdData.shift_key_pressed = TRUE; } else if(raw_key == 0x1d) { m_KbdData.ctrl_key_pressed = TRUE; } else if(raw_key == 0x38) { m_KbdData.alt_key_pressed = TRUE; } key_family = KBD_ASCII_ORG; key_family |= (m_KbdData.shift_key_pressed ? KBD_ASCII_WITH_SHIFT : 0); key_family |= (m_KbdData.indicator_status&KBD_LED_CAPSLOCK ? KBD_ASCII_WITH_CAPSLOCK : 0); key_data.type = KBD_KTYPE_GENERAL; key_data.key = m_AsciiCode[key_family][raw_key]; KbdpPushUserKeyData(&(m_KbdData.user_keydata_q), &key_data); } return 0; }
static DWORD PspThreadCutterThread(PVOID StartContext) { HANDLE ProcessHandle, ThreadHandle; PTHREAD_CONTROL_BLOCK *pt_prev_thread, *pt_cur_thread; while(1) { /* check thread cutting list */ if(!PspPopCuttingItem(&m_ThreadCuttingList, &ThreadHandle)) { HalTaskSwitch(); continue; } ENTER_CRITICAL_SECTION(); ProcessHandle = PsGetThreadPtr(ThreadHandle)->parent_process_handle; /* if requsted thread's parent process handle is same with system process handle, then do nothing!! protect system thread */ if(ProcessHandle == PsGetThreadPtr(PsGetCurrentThread())->parent_process_handle) { goto $exit; } /* invalid thread */ if(PsGetProcessPtr(ProcessHandle)->thread_count == 0) { goto $exit; } /* check whether there is only one thread exist in the parent process */ else if(PsGetProcessPtr(ProcessHandle)->thread_count == 1) { PsGetProcessPtr(ProcessHandle)->pt_head_thread = NULL; } /* more than 2 threads exist */ else { pt_prev_thread = pt_cur_thread = &(PsGetProcessPtr(ProcessHandle)->pt_head_thread); while(*pt_cur_thread != PsGetThreadPtr(ThreadHandle)) { /* there is no requested thread in the parent process */ if((*pt_cur_thread)->pt_next_thread == NULL) { goto $exit; } pt_prev_thread = pt_cur_thread; pt_cur_thread = &((*pt_cur_thread)->pt_next_thread); } /* change next thread pointer */ (*pt_prev_thread)->pt_next_thread = (*pt_cur_thread)->pt_next_thread; } PsGetProcessPtr(ProcessHandle)->thread_count--; /* except user mode program's stack */ if(PsGetThreadPtr(ThreadHandle)->pt_stack_base_address >= (int *)0x00200000) MmFreeNonCachedMemory((PVOID)(PsGetThreadPtr(ThreadHandle)->pt_stack_base_address)); /* dealloc stack */ MmFreeNonCachedMemory((PVOID)(PsGetThreadPtr(ThreadHandle))); /* dealloc thread */ $exit: EXIT_CRITICAL_SECTION(); } return 0; }
static DWORD PspProcessCutterThread(PVOID StartContext) { HANDLE ProcessHandle; PPROCESS_CONTROL_BLOCK *pt_prev_process, *pt_cur_process; PTHREAD_CONTROL_BLOCK *pt_cur_thread; while(1) { /* check process cutting list */ if(!PspPopCuttingItem(&m_ProcessCuttingList, &ProcessHandle)) { HalTaskSwitch(); continue; } ENTER_CRITICAL_SECTION(); /* if requsted process handle is same with system process handle, then do nothing!! protect system process */ if(ProcessHandle == PsGetThreadPtr(PsGetCurrentThread())->parent_process_handle) { goto $exit; } /* find requested process position in the process list */ pt_prev_process = pt_cur_process = &(m_ProcMgrBlk.pt_head_process); while(*pt_cur_process != PsGetProcessPtr(ProcessHandle)) { /* there is no requested process in the list */ if((*pt_cur_process)->pt_next_process == NULL) { goto $exit; } pt_prev_process = pt_cur_process; pt_cur_process = &((*pt_cur_process)->pt_next_process); } /* change next process pointer */ (*pt_prev_process)->pt_next_process = (*pt_cur_process)->pt_next_process; m_ProcMgrBlk.process_count--; /* dealloc all threads belonged to the requested process */ pt_cur_thread = &(PsGetProcessPtr(ProcessHandle)->pt_head_thread); while(*pt_cur_thread != NULL) { MmFreeNonCachedMemory((PVOID)((*pt_cur_thread)->pt_stack_base_address)); MmFreeNonCachedMemory((PVOID)(*pt_cur_thread)); pt_cur_thread = &((*pt_cur_thread)->pt_next_thread); } /* dealloc the requested process memory */ MmFreeNonCachedMemory((PVOID)ProcessHandle); $exit: EXIT_CRITICAL_SECTION(); } return 0; }
//Init 쓰레드의 핸들러 함수 static void PspTaskEntryPoint(void) { PKSTART_ROUTINE start_routine; HANDLE current_thread; DWORD ret_value; current_thread = PsGetCurrentThread(); start_routine = PsGetThreadPtr(current_thread)->start_routine; ret_value = start_routine(PsGetThreadPtr(current_thread)->start_context); PsGetThreadPtr(current_thread)->thread_status = THREAD_STATUS_TERMINATED; HalTaskSwitch(); while(1) ; }
/********************************************************************************************************** * JOB_ITEM & THREAD * **********************************************************************************************************/ static DWORD FddpJobProcessThread(PVOID StartContext) { FDD_JOB_ITEM job_item; while(1) { if(!FddpPopJobItem(&job_item)) { HalTaskSwitch(); continue; } FddpReadWriteSector(job_item.type, job_item.sector, job_item.numbers_of_sectors, job_item.pt_data); PsSetThreadStatus(job_item.thread, THREAD_STATUS_READY); } return 0; }
//종료된 쓰레드의 삭제 static DWORD PspThreadCutterThread(PVOID StartContext) { HANDLE ProcessHandle, ThreadHandle; PTHREAD_CONTROL_BLOCK *pt_prev_thread, *pt_cur_thread; while(1) { if(!PspPopCuttingItem(&m_ThreadCuttingList, &ThreadHandle)) { HalTaskSwitch(); continue; } ENTER_CRITICAL_SECTION(); ProcessHandle = PsGetThreadPtr(ThreadHandle)->parent_process_handle; if(ProcessHandle == PsGetThreadPtr(PsGetCurrentThread())->parent_process_handle) { goto $exit; } if(PsGetProcessPtr(ProcessHandle)->thread_count == 0) { goto $exit; } else if(PsGetProcessPtr(ProcessHandle)->thread_count == 1) { PsGetProcessPtr(ProcessHandle)->pt_head_thread = NULL; } else { pt_prev_thread = pt_cur_thread = &(PsGetProcessPtr(ProcessHandle)->pt_head_thread); while(*pt_cur_thread != PsGetThreadPtr(ThreadHandle)) { if((*pt_cur_thread)->pt_next_thread == NULL) { goto $exit; } pt_prev_thread = pt_cur_thread; pt_cur_thread = &((*pt_cur_thread)->pt_next_thread); } (*pt_prev_thread)->pt_next_thread = (*pt_cur_thread)->pt_next_thread; } PsGetProcessPtr(ProcessHandle)->thread_count--; if(PsGetThreadPtr(ThreadHandle)->pt_stack_base_address >= (int *)0x00200000) MmFreeNonCachedMemory((PVOID)(PsGetThreadPtr(ThreadHandle)->pt_stack_base_address)); MmFreeNonCachedMemory((PVOID)(PsGetThreadPtr(ThreadHandle))); $exit: EXIT_CRITICAL_SECTION(); } }
//종료된 프로세스의 삭제 static DWORD PspProcessCutterThread(PVOID StartContext) { HANDLE ProcessHandle; PPROCESS_CONTROL_BLOCK *pt_prev_process, *pt_cur_process; PTHREAD_CONTROL_BLOCK *pt_cur_thread; while(1) { if(!PspPopCuttingItem(&m_ProcessCuttingList, &ProcessHandle)) { HalTaskSwitch(); continue; } ENTER_CRITICAL_SECTION(); if(ProcessHandle == PsGetThreadPtr(PsGetCurrentThread())->parent_process_handle) { goto $exit; } pt_prev_process = pt_cur_process = &(m_ProcMgrBlk.pt_head_process); while(*pt_cur_process != PsGetProcessPtr(ProcessHandle)) { if((*pt_cur_process)->pt_next_process == NULL) { goto $exit; } pt_prev_process = pt_cur_process; pt_cur_process = &((*pt_cur_process)->pt_next_process); } (*pt_prev_process)->pt_next_process = (*pt_cur_process)->pt_next_process; m_ProcMgrBlk.process_count--; pt_cur_thread = &(PsGetProcessPtr(ProcessHandle)->pt_head_thread); while(*pt_cur_thread != NULL) { MmFreeNonCachedMemory((PVOID)((*pt_cur_thread)->pt_stack_base_address)); MmFreeNonCachedMemory((PVOID)(*pt_cur_thread)); pt_cur_thread = &((*pt_cur_thread)->pt_next_thread); } MmFreeNonCachedMemory((PVOID)ProcessHandle); $exit: EXIT_CRITICAL_SECTION(); } return 0; }
/* * LED ASSOCIATED KEYs * 45-c5 : num-lock 3a-ba : capslock 46-c6 : scroll-lock * MAIN KEYs (LEFT-SIDED AREA) * 2a-aa : left-shift 36-b6 : right-shift 1d-9d : left-ctrl e0-1d-e0-9d : right-ctrl 38-b8 : left-alt e0-38-e0-b8 : right-alt 1c-9c : enter e-8e : back-space f-8f : tab e0-71-e0-f1 : chinese e0-5c-e0-dc : right-windows-key e0-72 : kor/eng 1-81 : esc 3b-bb : f1 3c-bc : f2 3d-bd : f3 3e-be : f4 3f-bf : f5 40-c0 : f6 41-c1 : f7 42-c2 : f8 43-c3 : f9 44-c4 : f10 57-d7 : f11 58-d8 : f12 * MIDDLE SIDE * e0-2a-e0-37-e0-b7-e0-aa : print screen e1-1d-45-e1-9d-c5 : pause e0-2a-e0-52-e0-d2-e0-aa : insert e0-2a-e0-47-e0-c7-e0-aa : home e0-2a-e0-49-e0-c9-e0-aa : page up e0-2a-e0-53-e0-d3-e0-aa : delete e0-2a-e0-4f-e0-cf-e0-aa : end e0-2a-e0-51-e0-d1-e0-aa : page down e0-2a-e0-48-e0-c8-e0-aa : up arrow e0-2a-e0-50-e0-d0-e0-aa : down arrow e0-2a-e0-4b-e0-cb-e0-aa : left arrow e0-2a-e0-4d-e0-cd-e0-aa : right arrow * NUM PAD * e0-35-e0-b5 : / 37-b7 : * 4a-ca : - 4e-ce : + e0-1c-e0-9c : enter 53-d3 : . (or Del) 52-d2 : 0 (or Ins) 4f-cf : 1 50-d0 : 2 51-d1 : 3 4b-cb : 4 4c-cc : 5 4d-cd : 6 47-c7 : 7 48-c8 : 8 c9-49 : 9 e0-21-e0-a1 : calculator e0-18-e0-98 : X'fer */ static DWORD KbdpTranslatorThread(PVOID StartContext) { KBD_KEY_DATA key_data; BYTE raw_key, indicator, key_family; while(1) { if(!KbdpPopRawKeyData(&(m_KbdData.raw_keydata_q), &raw_key)) { HalTaskSwitch(); /* if there is no remained job to do, then call task-swithing task in order to run other taks. */ continue; } /* is this special key or function key started with '0xe0'? */ /* do something */ /* dont process such "break key" */ if(raw_key & 0x80) { if(raw_key == 0xaa || raw_key == 0xb6) /* shift key breaked */ m_KbdData.shift_key_pressed = FALSE; else if(raw_key == 0xb8) /* alt key breaked */ m_KbdData.alt_key_pressed = FALSE; else if(raw_key == 0x9d) /* alt key breaked */ m_KbdData.ctrl_key_pressed = FALSE; continue; } /* RESET INDICATORs */ if(raw_key == 0x45) { /* num lock */ indicator = KBD_LED_NUMLOCK; goto $reset_indicator; } else if(raw_key == 0x3a) { /* capslock */ indicator = KBD_LED_CAPSLOCK; goto $reset_indicator; } else if(raw_key == 0x46) { /* scroll lock */ indicator = KBD_LED_SCROLLLOCK; $reset_indicator: if(m_KbdData.indicator_status & indicator) { m_KbdData.indicator_status &= (~indicator); } else { m_KbdData.indicator_status |= indicator; } KbdpResetIndicator(); continue; } /* SHIFT, ALT, CTRL */ if(raw_key == 0x2a || raw_key == 0x36) { /* shift */ m_KbdData.shift_key_pressed = TRUE; continue; } else if(raw_key == 0x1d) { m_KbdData.ctrl_key_pressed = TRUE; continue; } else if(raw_key == 0x38) { m_KbdData.alt_key_pressed = TRUE; continue; } /* GENERAL KEYs */ key_family = KBD_ASCII_ORG; key_family |= (m_KbdData.shift_key_pressed ? KBD_ASCII_WITH_SHIFT : 0); key_family |= (m_KbdData.indicator_status&KBD_LED_CAPSLOCK ? KBD_ASCII_WITH_CAPSLOCK : 0); key_data.type = KBD_KTYPE_GENERAL; key_data.key = m_AsciiCode[key_family][raw_key]; KbdpPushUserKeyData(&(m_KbdData.user_keydata_q), &key_data); } return 0; }