Operating system Development Series -13.G. System calls to the operating system

Source: Internet
Author: User

In our operating system, 3 processes that already exist are running on ring1, they are not able to use certain instructions arbitrarily, they cannot access some of the more privileged areas of memory, but if a task requires these usage instructions or memory regions, it can only be implemented by system calls. It is a bridge between the application and the operating system.

So, one thing may be that the application does part of the operating system to do part. In this way, the problem involves a privilege-level transformation.

Obviously, it's hard for us to do this, because the process of switching is the process of repeating such a privilege-level transformation constantly. There, the triggering of the transformation is an external interrupt, we put this incentive to change it, into an "int nnn", everything is resolved.

Let's implement a function called int get_ticks () that uses this function to get the total number of clock interrupts currently occurring. Set a global variable ticks, each time a clock interrupt, it adds 1, the process can be used at any time through the get_ticks () This system call to get this value.

See SYSCALL.ASM:

_nr_get_ticks       equ 0; correspond to the definition of sys_call_table in GLOBAL.C! Int_vector_sys_call equ 0x90globalget_ticks; Export the symbol bits 32[section. Text]get_ticks:moveax, _nr_get_ticksintint_vector_sys_callret

Here the system calls the corresponding interrupt number is set to 0x90, as long as it does not repeat the original interrupt number.

Immediately define the Int_vector_sys_call corresponding to the interrupt gate, protect.c:

public void Init_prot () {... init_idt_desc (int_vector_sys_call,da_386igate,      sys_call,privilege_user); ...}

This way we will correspond to the interruption of Int_vector_sys_call with Sys_call. Call here [Sys_call_table+eax*4] (called Sys_call_table[eax]), similar to irq_table, Sys_call_table is an array of function pointers, each of which points to a function, To handle the corresponding system call.

Externsys_call_tableglobal Sys_callsys_call:        call    save        STI        call    [sys_call_table + eax * 4]        mov     [esi + eaxreg-p_stackbase], eax        CLI        ret

The previous eax has been assigned a value of _nr_get_ticks (that is, 0), and Sys_call_table[0] has been initialized to Sys_get_ticks, so call [Sys_call_table+eax*4] is called sys_get_ Ticks

mov [esi+eaxreg-p_stackbase],eax is the position where the return value of the function Sys_call_table[eax] is placed in the process table eax so that the correct return value is loaded EAX when the process P is resumed.

You can now add code that calls Get_ticks in the process:

void TestA () {int i = 0;while (1) {get_ticks ();d isp_str ("A");d isp_int (i++);d isp_str ("."); Delay (1);}}

Don't forget to import and export the symbols in kernel.asm and syscall.asm.

Modify

void TestA () {int i = 0;while (1) {disp_str ("A");d Isp_int (Get_ticks ());d isp_str ("."); Delay (1);}}

Run as follows, the first is a0x0, the second is a0x27, two times between the "#" happens to be 39, our get_ticks everything is normal:

" Source "

Operating system Development Series -13.G. System calls to the operating system

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.