Page 1 of 1

ASM + multitasking theory

Posted: Tue Jun 24, 2008 11:21 am
by inflater
Hi,
I'm planning multitasking in my OS, but only in the graphical user interface. My question is, how theoretically is it "enabled" and "used"? I think a timer interrupt is set to a specific frequency and holds some variables and stuff. The whole interface has the CPU halted (HLT) with enabled interrupts of course. When a application sends a signal, the timer will accept it and e.g. executes something inside the OS kernel, then returns back. But I still doesn't understand the theory in multitasking. How could I "divide" e.g. two running programs that they would work normally at the same time etc ? How can I e.g. perform programmed I/O access without halting the whole CPU? (The answer is DMA, Win95 suffered from floppy disk r/w, but when performing fdd i/o, it didnt halt to a complete stop like in MS DOS)...

Apologies that I look like Lino Commando :D...

Re: ASM + multitasking theory

Posted: Tue Jun 24, 2008 12:05 pm
by inflater
Whoah! :shock: All that sounds greatly confusing. So, i should from the irq0 execute a "scheduler" function, that'll set up a new stack and preserve the original registers (from the os shell lets say) And then, call the next task in queue, and it'll execute until another irq0 fires. Hmm, so the timer must preserve CS and EIP and all these registers. And then? It'll call another task in the queue, or return to the OS, or ... call the same task or ?

I should really start reading manuals, but GUI and multitasking thank God isn't planned in Patlock's next releases :lol: If only intel manuals were in Slovak. Multitasking, weird irq0 handlers, registers preserving, English, :lol:. Seems as the multitasking is one of the biggest obstacles in my way :(

Re: ASM + multitasking theory

Posted: Tue Jun 24, 2008 12:24 pm
by ucosty
I think you'll find it to be pretty easy.
inflater wrote:Whoah! :shock: All that sounds greatly confusing. So, i should from the irq0 execute a "scheduler" function, that'll set up a new stack and preserve the original registers (from the os shell lets say) And then, call the next task in queue, and it'll execute until another irq0 fires. Hmm, so the timer must preserve CS and EIP and all these registers. And then? It'll call another task in the queue, or return to the OS, or ... call the same task or ?
You could, I guess, do it that way. It can be a lot more simple than that, though. I do kernel stack swapping in my OS. Each process has it's own 4kb kernel stack which is used for ISR routines.

When a timer interrupt fires the ISR prologue pushes a whole bunch of registers to the stack. The last register pushed onto the stack is the ESP register. Then the timer ISR is run and it spits out a new stack address which is mov'ed into the ESP register. The registers are then popped off the new stack. It couldn't get any simpler.

Re: ASM + multitasking theory

Posted: Thu Jun 26, 2008 11:45 am
by Dex
Here is the winner of the FASM OS compo 2004, a multi-tasking demo in 512bytes (realmode):
==============
Nano OS v2.1 written by Viktor Peter Kovacs (KVP)
============== for the fasm os contest of 2004

This is a demo os, written to show some os design
methods, and to proove that it's possible to put
a simple multitasking os into 512 bytes...

Features:
-hard realtime scheduler:
-round-robin policy
-18.2 tasks/second

-i/o support:
-ascii character console
-blocking terminal i/o

-ipc support:
-copy based message passing
-blocking send/recv primitives

-standard library:
-task control: exec, exit
-ipc support: send, recv
-i/o support: getc, putc

-3 applications:
-ipc demo: glass tty with text filter, task lanuching
-ipc service: simple listener (started dynamically)
-hello world: prints 'Hello World!' on the terminal

How to test it:
-start the os from a bootsector
-type any text and it will be echoed back
-type 'H' (big h letter) and the ipc demo will lanuch
the hello world application
-it's possible to launch multiple instances of the
hello world application (and even keep typing while
they print their texts)

Remarks:
-the ipc speed is intentionally slow, so it's possible
to see the mixed output of the various tasks running
concurrently
-the demo itself is not too informative when run, but
looking at the source code reveals that it contains
most important features of a simple os (I would add
disk i/o and fs support in the next 512 bytes)
-required hardware: an i8086 compatible computer with
at least 128Kb of ram
Code

Code: Select all

;
; ==============
;  Nano OS v2.1    copyright by Viktor Peter Kovacs (KVP)
; ==============  for the fasm 512 byte os contest of 2004
;
; This is a demo os, written to show some os design
; methods, and to proove that it's possible to put
; a simple multitasking os into 512 bytes...
;

; ---- constants ----

TIMER_IRQN equ (8)                                     ; hw. timer interrupt number
IRQ_BIT    equ (512)                                   ; 'irq enable' bit in the flags register

TS_FREE    equ (0)                                     ; task states
TS_READY   equ (1)
TS_SEND    equ (2)
TS_RECV    equ (3)
TS_PUTC    equ (4)
TS_GETC    equ (5)

E_TIDLIMIT equ (0ffffh)                                ; error code for task resource limit error

TID_NUM    equ (8)                                     ; maximal number of tasks allowed
TID_STACK  equ (512)                                   ; default task stack size

; ---- startup code ----

start0:                                                ; needed for compatibility
    jmp    start1
    nop
start1:
    jmp    07c0h:start2                                ; code segment alignement
start2:
    cli
    mov    ax, cs
    mov    ds, ax
    mov    es, ax
    mov    ss, ax
    mov    sp, stack_top
    cld
    mov    di, _bss_start                              ; zero the bss
    mov    cx, _bss_end-_bss_start
    xor    ax, ax
    rep    stosb

    push   es                                          ; change the irq vector table
    xor    bx, bx
    mov    es, bx
    mov    ax, [es:TIMER_IRQN*4]
    mov    [timer0l], ax
    mov    ax, [es:TIMER_IRQN*4+2]
    mov    [timer0h], ax
    mov    ax, timer_irq
    mov    [es:TIMER_IRQN*4], ax
    mov    ax, cs
    mov    [es:TIMER_IRQN*4+2], ax
    pop    es

    mov    cx, idle_task                               ; start idle task
    call   sys_exec
    mov    cx, prg1                                    ; start program 1 (rpc demo - data source)
    call   sys_exec
    mov    cx, prg3                                    ; start program 3 (hello world)
    call   sys_exec

    mov    ax, (TID_NUM-1)*2                           ; setup and enable the scheduler
    mov    [ctid], ax
    sti
idle_task:
    jmp    idle_task                                   ; the idle task

; ---- kernel code ----

timer_irq:
    push   bp                                          ; save task context
    push   di
    push   si
    push   dx
    push   cx
    push   bx
    push   ax

    mov    ax, [irqflg]                                ; kernel lock check
    or     ax, ax
    jz     timer_irq1
timer_irq0:
    pop    ax                                          ; restore task context
    pop    bx
    pop    cx
    pop    dx
    pop    si
    pop    di
    pop    bp
           db 0eah                                     ; jmp far to old handler (for compatibility)
timer0l    dw 00000h
timer0h    dw 00000h

timer_irq1:
    mov    ax, 1                                       ; lock kernel
    mov    [irqflg], ax

    mov    bx, [ctid]
    mov    ax, [tstate+bx]
timer_cmd1:
    cmp    ax, TS_SEND                                 ; ipc message send (blocking ipc)
    jnz    timer_cmd2
    mov    si, [msgtid+bx]
    mov    ax, [tstate+si]
    cmp    ax, TS_RECV
    jnz    timer_next
    mov    ax, [msgdat+bx]
    mov    [msgdat+si], ax
    mov    [msgtid+si], bx
    mov    ax, TS_READY
    mov    [tstate+si], ax
    jmp    timer_cmd_ok
timer_cmd2:
    cmp    ax, TS_PUTC                                 ; terminal write
    jnz    timer_cmd3
    mov    ax, [msgdat+bx]
    call   bios_putc
    jmp    timer_cmd_ok
timer_cmd3:
    cmp    ax, TS_GETC                                 ; terminal read
    jnz    timer_next
    call   bios_getc
    mov    [msgdat+bx], ax
    or     ax, ax
    jz     timer_next
timer_cmd_ok:                                          ; resume task
    mov    ax, TS_READY
    mov    [tstate+bx], ax
timer_next:

;; prints task states (see:TS_* constants; red=current task)
;; (used for testing, removed to save code space)
;    push   es
;    mov    ax, 0b800h
;    mov    es, ax
;    xor    di, di
;    mov    si, tstate
;debug_print:
;    lodsw
;    mov    ah, 15
;    add    al, '0'
;    stosw
;    cmp    si, tstate+TID_NUM*2
;    jnz    debug_print
;    mov    di, [ctid]
;    inc    di
;    mov    al, 12
;    stosb
;    pop    es

timer_next0:
    mov    bx, [ctid]                                  ; hard realtime round robin scheduler
    mov    [tstack+bx], sp
timer_next1:
    add    bx, 2
    and    bx, (TID_NUM-1)*2
    mov    [ctid], bx
    mov    ax, [tstate+bx]
    cmp    ax, TS_FREE
    jz     timer_next1
    mov    sp, [tstack+bx]

    xor    ax, ax                                      ; unlock kernel
    mov    [irqflg], ax
    jmp    timer_irq0

;
; <-
;  ax : char
;
bios_putc:                                             ; device i/o function (called internally)
    push   bp
    push   bx
    mov    ah, 00eh
    mov    bx, 00007h
    int    010h
    cmp    al, 13
    jnz    bios_putc1
    mov    al, 10
    int    010h
bios_putc1:
    pop    bx
    pop    bp
    ret

;
; ->
;  ax : char (0=no char)
;
bios_getc:                                             ; device i/o function (called internally)
    mov    ax, 00100h
    int    016h
    jnz    bios_getc1
    xor    ax, ax
    ret
bios_getc1:
    mov    ax, 00000h
    int    016h
    xor    ah, ah
    ret

; ---- standard library ----

;
; EXEC: Starts a new task
;
; <-
;  cx : code offset
; (ax, si)
; ->
;  bx : new tid (E_TIDLIMIT=out of task slots)
;
sys_exec:
    pushf                                              ; task table resource lock
    cli
    xor    bx, bx
sys_exec1:
    mov    ax, [tstate+bx]                             ; looks for a free task slot
    cmp    ax, TS_FREE
    jz     sys_exec2
    add    bx, 2
    cmp    bx, TID_NUM*2
    jnz    sys_exec1
    mov    bx, E_TIDLIMIT                              ; return error code
    popf
    ret
sys_exec2:
    mov    dx, bx
    mov    dh, dl
    xor    dl, dl
    mov    si, tstacks+TID_STACK-10*2                  ; allocate task stack
    add    si, dx
    mov    [tstack+bx], si
    mov    [si+7*2], cx                                ; set task startup context (cs:ip,flg)
    mov    ax, cs
    mov    [si+8*2], ax
    pushf
    pop    ax
    or     ax, IRQ_BIT
    mov    [si+9*2], ax
    mov    ax, TS_READY
    mov    [tstate+bx], ax                             ; activate new task
    shr    bx, 1
    popf                                               ; release task table lock
    ret

;
; EXIT: Exits a task
;
sys_exit:
    mov    bx, [ctid]
    mov    ax, TS_FREE
    mov    [tstate+bx], ax
    jmp    $

;
; SEND: Sends an ipc message
;
; <-
;  ax : msg
;  bx : dst
; (cx, bp)
;
sys_send:
    shl    bx, 1
    mov    cx, TS_SEND
sys_send0:
    mov    bp, [ctid]
    mov    [msgdat+bp], ax
    mov    [msgtid+bp], bx
    mov    [tstate+bp], cx
sys_send1:
    mov    ax, [tstate+bp]
    cmp    ax, TS_READY
    jnz    sys_send1
    ret

;
; RECV: Receives an ipc message
;
; ->
;  ax : msg
;  bx : dst
; (cx, bp)
;
sys_recv:
    mov    cx, TS_RECV
sys_recv0:
    mov    bp, [ctid]
    mov    [tstate+bp], cx
sys_recv1:
    mov    ax, [tstate+bp]
    cmp    ax, TS_READY
    jnz    sys_recv1
    mov    ax, [msgdat+bp]
    mov    bx, [msgtid+bp]
    shr    bx, 1
    ret

;
; PUTC: Outputs a character to the console
;
; <-
;  ax : chr
; (bx, cx, bp)
;
sys_putc:
    mov    cx, TS_PUTC
    jmp    sys_send0

;
; GETC: Inputs a character from the console
;
; (bx, cx, bp)
; ->
;  ax : chr (0=no char)
;
sys_getc:
    mov    cx, TS_GETC
    jmp    sys_recv0

; ---- application code ----

;; prg1 v1.0
;prg1:
;    call   sys_getc
;    or     ax, ax
;    jz     prg1
;    cmp    al, 'H'
;    je     prg1_1
;    call   sys_putc    
;    jmp    prg1
;prg1_1:
;    mov    cx, prg2
;    call   sys_exec
;    jmp    prg1

;
; prg1 v2.0
;  -on startup, starts a listerner task
;  -gets a character from the console and sends it via ipc
;  -on the 'H' key, starts a new 'hello world' process
;
prg1:
    mov    cx, prg2
    call   sys_exec
    mov    di, bx
prg1_0:
    call   sys_getc
    or     ax, ax
    jz     prg1_0
    cmp    al, 'H'
    je     prg1_1
    mov    bx, di
    call   sys_send
    jmp    prg1_0
prg1_1:
    mov    cx, prg3
    call   sys_exec
    jmp    prg1_0

;
; prg2 v1.0
;  -waits for incoming messages via ipc
;  -prints the message content on the console
;
prg2:
    call   sys_recv
    call   sys_putc
    jmp    prg2

;
; hello world v1.1
;  -prints the text 'Hello World!' on the console
;  -exits on completition
;
prg3:
     mov    si, prg3_str_hello
prg3_1:
     lodsb
     or     al, al
     jz     prg3_3
     call   sys_putc
     jmp    prg3_1
prg3_3:
     call   sys_exit

prg3_str_hello db 'Hello World!',13,0

; ---- boot flags ----
rb  510-$
dw  0aa55h

; ---- boot stack ----
rb  512
stack_top:

_bss_start:                                            ; uninitialized data area (cleared to 0 on start)

; ---- kernel data ----
irqflg rw 1                                            ; kernel lock
ctid   rw 1                                            ; current task

tstate rw TID_NUM                                      ; task states
tstack rw TID_NUM                                      ; task stack pointers
msgtid rw TID_NUM                                      ; message destination task ids
msgdat rw TID_NUM                                      ; message data

; ---- application data ----


; ---- task stacks ----

rb (512*4)-$

tstacks:
rb (TID_STACK*TID_NUM)

_bss_end:
                                                                                        
PS: My entry came second :(.
PPS: We should have one (compo)for the OSDev forum.