/* * Kernel C code entry point. * Initializes kernel subsystems, mounts filesystems, * and spawns init process. */ void Main(struct Boot_Info* bootInfo) { Init_BSS(); Init_Screen(); Init_Mem(bootInfo); Init_CRC32(); Init_TSS(); Init_Interrupts(); Init_Scheduler(); Init_Traps(); Init_Timer(); Init_Keyboard(); Set_Current_Attr(ATTRIB(BLACK, GREEN|BRIGHT)); Print("Welcome to GeekOS!\n"); Set_Current_Attr(ATTRIB(BLACK, GRAY)); TODO("Start a kernel thread to echo pressed keys and print counts"); /* Now this thread is done. */ Exit(0); }
#define TODO(message) \ do { \ Set_Current_Attr(ATTRIB(BLUE, GRAY|BRIGHT)); \ Print("Unimplemented feature: %s\n", (message)); \ while (1) \ ; \ } while (0)
/* * Start a kernel-mode-only thread, using given function as its body * and passing given argument as its parameter. Returns pointer * to the new thread if successful, null otherwise. * * startFunc - is the function to be called by the new thread * arg - is a paramter to pass to the new function * priority - the priority of this thread (use PRIORITY_NORMAL) for * most things * detached - use false for kernel threads */ struct Kernel_Thread* Start_Kernel_Thread( Thread_Start_Func startFunc, ulong_t arg, int priority, bool detached ) { struct Kernel_Thread* kthread = Create_Thread(priority, detached); if (kthread != 0) { /* * Create the initial context for the thread to make * it schedulable. */ Setup_Kernel_Thread(kthread, startFunc, arg); /* Atomically put the thread on the run queue. */ Make_Runnable_Atomic(kthread); } return kthread; }
struct Kernel_Thread* Start_Kernel_Thread( Thread_Start_Func startFunc, ulong_t arg, int priority, bool detached )
/* * Thread priorities */ #define PRIORITY_IDLE 0 #define PRIORITY_USER 1 #define PRIORITY_LOW 2 #define PRIORITY_NORMAL 5 #define PRIORITY_HIGH 10
void foo(void) { Print("Hello from Cheryl\n"); while (1) { } }
Keycode Wait_For_Key(void) { bool gotKey, iflag; Keycode keycode = KEY_UNKNOWN; iflag = Begin_Int_Atomic(); do { gotKey = !Is_Queue_Empty(); if (gotKey) keycode = Dequeue_Keycode(); else Wait(&s_waitQueue); } while (!gotKey); End_Int_Atomic(iflag); return keycode; }
/* * Flags */ #define KEY_SPECIAL_FLAG 0x0100 #define KEY_KEYPAD_FLAG 0x0200 #define KEY_SHIFT_FLAG 0x1000 #define KEY_ALT_FLAG 0x2000 #define KEY_CTRL_FLAG 0x4000 #define KEY_RELEASE_FLAG 0x8000
void foo(void) { Keycode key; Print("Hello from cheryl\n"); while (1) { key = Wait_For_Key(); if (!(key & KEY_RELEASE_FLAG)) { if ((key & KEY_CTRL_FLAG) && (key & 0xFF) == 'd') { Print("\nreceived control-d\n"); break; } else Print("%c", key); } } }
程序效果: