Geekos tour-project0 (echo your input until you press Ctrl-d)

Source: Internet
Author: User
Tags call back

What's the next step? Don't worry. Read the manual that comes with geekos.

Introduction:

Geekos is an educational operating system kernel. geekos tries to combine realism and simplicity. it is a realisticsystem because it targets a real hardware platform-the X86 PC. it strives for simplicitly in that it contains the bare minimum functionalitynecessary
To provide the services of a modern operating system, such as virtual memory, a filesystem, and interprocess communication.

Overview of geekos

1. Memory

2. interrupts and threads

3. Devices

The above three are the key points, you need to understand and explore, because the following project will certainly cover.

Project0

I have learned a little about geekos and officially enter project0 below

Project0 is relatively simple, because it only guides you through geekos!

-> Target:FirstYou need to add a kernel thread to output "hello from XXX" (XXX is your name) to the screen, and thenRead the keyboard input and press enter to display it back to the screen, unless you use c-d to end it! That's simple.

... Let's see the bochs interface before it is resolved:

To create a thread in kernel mode, read the code src/geekos/kthread. C about the thread.

I probably browsed the code of this file and did not study the details first. I directly searched for the function we needed. We needed a thread to call back our custom function. Based on this, you can find such a function.

starut Kernel_Thread* Start_Kernel_Thread(Thread_Start_Func startFunc, ulong_t arg, int priority, bool detached);

The above is a function prototype. There is a function parameter explanation on this function:

Startfunc:This is the callback function entry.

Arg:The above annotation is "pass to the new function", which is not understood first.

Priority:This can be literally understood as the thread priority. Most of them can be set to priority_normal.

Detached:For kernel threads, enter false.

In turn, the second parameter is puzzled. If it is okay, we can find out whether the function for creating the thread has been called or not. Good, it is called in Row 3.

Start_Kernel_Thread(Repear, 0, PRIORITY_NORMAL, true);

Since the second parameter is passed in 0, we also pass in 0!

Let's finish the first part of this task (output hello from crazybaby). Although it is very simple, step by step. After all, the mind stack is limited!

The key code is as follows:

 25 void GoForIt(){ 26     Print("Hello from Crazybaby\n"); 27 }

 52     Start_Kernel_Thread(GoForIt, 0, PRIORITY_NORMAL, false); 53  54    // TODO("Start a kernel thread to echo pressed keys and print counts");

Although the code is simple, there are two points to note:

1. Print is required for printing geekos because it is an independent OS and uses its own Lib!

2. the second part of the Code has a todo, although it is possible to put it, because we enable the thread to print, but so many words are printed out really indecent, so we can choose to comment it out. the first effect is as follows:

Next we will continue to complete the second part, read the entered characters and display them back!

I thought it was not easy. But I found that compilation does not support functions such as getchar.

 undefined reference to `getchar'make: *** [geekos/kernel.exe] Error 1

Alas, I just said that I forgot everything, so I should look for the functions provided by geekos!

After reading the source files under geekos, there are very few files, so you can quickly locate keyboard. C.

According to the explanation of each function header, you can find the wait_for_key function at the bottom of the file.

See the prototype:

Keycode Wait_For_Key(void)

The input parameter is void.

The output parameter is keycode. You can check the keycode declaration and use Vim to locate the keycode that is not found in this file. Then, check the header file and find it under keyboard. h.

typdef ushort_t Keycode.

It is of the unsigned short type. The following shows the simple flowchart of the program:

We also need to identify the two "signals": Carriage Return and c-d"

The rest depends on the big array below:

First Judge c-d, (key_ctrl_flag | 0x64) = kecode

Then judge whether to press ENTER 13 = keycode

You also need to determine whether the key has been release or you have already removed the screen when you press it!

The detailed code is as follows:

 25 void GoForIt(){ 26     Print("Hello from Crazybaby\n"); 27     Keycode ret; 28     while (1) {  29         ret =  Wait_For_Key(); 30         if (!(ret & KEY_RELEASE_FLAG)) { 31             if ((KEY_CTRL_FLAG | 0x64) == ret) {  32                 Print("%s\n", "GeekOS's Project0 is over!");. 33                 break; 34             } 35             if (13 != ret) { 36                 Print("%c", ret); 37             } 38             else 39             { 40                 Print("\n"); 41             }    42         }    43     }    44 }

Test results:

OK. Continue to the second project!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.