How is sleep implemented at the OS level?

前端 未结 6 1249
我在风中等你
我在风中等你 2020-12-01 11:51

I am just interested how sleep(time in ms) is implemented in a C library or basically at the OS level...

I am guessing...

  1. May be the based
6条回答
  •  长情又很酷
    2020-12-01 12:34

    cpu usage: 0%
    requirements:
    create_gate (Set up the IRQ handlers)
    pic_mask_clear (Enable specific interrupts)
    rtc_poll (Set up RTC)
    rtc_irq
    smp_wake_up

    ; In\   RAX = Time in millisecond
    ; Out\  All registers preserved
    sleep:
        push rcx
        push rax
    
        mov rcx, [rtc_irq.up_time]
        add rax, rcx
    .os_delay_loop:
        hlt
        cmp qword [rtc_irq.up_time], rax
        jle .os_delay_loop
    
        pop rax
        pop rcx
        ret
    

    smp_wake_up

    ; In\   Nothing
    ; Out\  Nohting
    smp_wakeup_all:
        push rdi
        push rax
    
        mov rdi, [os_LocalAPICAddress]
        xor eax, eax
        mov [rdi+0x0310], eax   ; Write to the high bits first
        mov eax, 0x000C0080 ; Execute interrupt 0x80
        mov [rdi+0x0300], eax   ; Then write to the low bits
    
        pop rax
        pop rdi
        ret
    

    rtc_irq:

    ; UIP (0), RTC@32.768KHz (010), Rate@1024Hz (0110)
    ; In\   Nothing
    ; Out\  Nothing
    rtc_irq:
        inc qword[.up_time]
        call smp_wakup_all
        ret
    .up_time:       dq 0
    

    usage :

    mov rax, 1000 (millisecond)
    call sleep
    

    its fine

提交回复
热议问题