Search code examples
unixassemblypdp-11

Panic function in UNIX


I'm currently trying to get some version of the function idle() from the UNIX OS working. I've got the source code, but I'm not good with assembly language (something I've recently been trying to change). Could somebody help me get a better understanding of how it works?

I've tried searching for it, but nothing useful comes up. I've also checked in the book "Lion's Commentary on UNIX", but I didn't find any explanation.

This is the source of the function and that's the link for the full source code.

.globl  _idle
_idle:
    mov PS,-(sp)
    bic $340,PS
    wait
    mov (sp)+,PS
    rts pc


Solution

  • Well this is PDP 11/40 assembly language, which is defined in the manual.

    Let's break it down, line by line:

    .globl  _idle    # define a global symbol called idle
    _idle:           # this is the label for the global symbol
        mov PS,-(sp) # push processor state onto stack
        bic $340,PS  # clear priority level bits - effectively enable all interrupts
        wait         # wait for an interrupt
        mov (sp)+,PS # pop processor state from stack
        rts pc       # return from function
    

    The -(sp) and (sp)+ should be read as the equivalent to the C/C++ operators --sp and sp++.

    So, it effectively saves the state, Clears the priority level bits and then waits for an interrupt. Once the interrupt arrives it restores the state and goes back to work.

    Please see the section [2.3.2 Processor Status Word] in the manual for the definition of the content of the PS register.

    Now, the wait operation will be interrupted for a variety of reasons, not the least of which is the real-time clock interrupt, so it gets woken up periodically to do some more work.

    When you look at the source, there are two places where the idle() routine is called - one from the panic handler, in an infinite loop, and the next in the swtch, which swaps between processes and when it doesn't find a runnable process enters the idle routine.