flat assembler
Message board for the users of flat assembler.

Index > OS Construction > (solved) simple task switching mechanism

Author
Thread Post new topic Reply to topic
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 18 Apr 2012, 00:44
I've written a task switching mechanism which uses the LAPICs' oscillator to trigger a task switch. the code includes a Front Side Bus calculator to determine the correct value to write into the Initial Count Register.. the state machine component uses a Linked List approach. each time it is supposed to switch a task it stores the registers onto the current tasks' stack and stores the RSP register into the state machine for the current task; Then it reads in the address of the next tasks' state machine (using the linked list component) and uses it to recover RSP. The code below is on line 7485 of ORB.asm
Code:
            xor ecx, ecx
            mov[dword APIC.EOI], ecx
            
;            mov rbx,[rdi+orb.tasks];this is used as an index.
;            add rbx, rbx
;            mov ecx, 16
;            min rcx, rbx, rax
;            mov eax, NRQ.TSC, .init
;            shr eax, 4
;            mul[rcx*8+reciprocals]
;            add edx, edx
;            mov[dword APIC.LVTTimerICR], edx
            ;tsc
            ;cmp rax,[rdi+orb.timeout-128]
            ;jb .end.int
            rali ., 16
           .int.two:
                
                ;the empty condition.
                mov rsi,[rdi+orb.current-128]
                ;cmp rsi,[rsi+orb.next]
                ;je .end.int
                
                    psh r08 r09 r10 r11 r12 r13 r14 r15
                        
                        add rsp,-512-8
                        fxsave64 [rsp]
                            
                            inc[rdi+ orb.count]
                            mov[rsi+ orb.rsp], rsp
                            or [rsi+ orb.int], 1
                            
                            mov rsi,[rsi+orb.next]
                            mov rsp,[rsi+orb.rsp ]
                            mov[rdi+ orb.current-128],rsi
                            tst[rsi+ orb.int], 1
                            jz .start
                            
                        fxrstor64 [rsp]
                        add rsp, 512+8
                        
                    pop r15 r14 r13 r12 r11 r10 r09 r08
                
               .end.int:
                ret
                        rali ., line
                       .start:
                        psh NRQ.Flags, .init
                        popf
                        cal qword [rsi]
                        cli
                        lea rdi, 128 shl ORB.Multiplier + ORB.Controls, .init
                        dec[rdi+ orb.tasks]
                        mov rsi,[rdi+orb.current-128]
                        mov rdx,[rsi+orb.next]
                        mov rcx,[rsi+orb.prev]
                        mov[rdx+orb.prev], rcx
                        mov[rcx+orb.next], rdx
                        mov[rdi+orb.current-128],rdx
                        mov rax,[rsi+orb.stack]
                        psh rdx
                            
                            psh rsi
                                
                                cal ORB.Page.D, .init;deallocate the memory used for the processes' stack.
                                
                            pop rax
                            cal ORB.Line.D, .init;deallocate the memory used for the linked list block.
                            
                        pop rsi
                        mov rsp,[rsi+orb.rsp]
                        lea rdi, 128 shl ORB.Multiplier + ORB.Controls, .init
                        inc[rdi+orb.count]
                        tst[rsi+orb.int], 1
                        jz .start
                    
                    fxrstor64 [rsp]
                    add rsp, 512+8
                
                pop r15 r14 r13 r12 r11 r10 r09 r08
            
            ret
                
The included code is the current version of Mathis.


Description:
Download
Filename: Mathis.zip
Filesize: 318.93 KB
Downloaded: 414 Time(s)



Last edited by BAiC on 18 Apr 2012, 04:40; edited 1 time in total
Post 18 Apr 2012, 00:44
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 18 Apr 2012, 00:50
BTW:

psh: this is a macro for push.

.init: if you see a ".init" as the last (or near-to-last) operand then the instruction you're looking at is actually SMC. The 4byte (or 8byte) constant that is embedded within the instruction stores a small collection of information that allows a Template Engine to patch the code at runtime.

also; the code was invoked by an Interrupt Redirection function which starts on line 7639 in ORB.asm
Post 18 Apr 2012, 00:50
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 18 Apr 2012, 00:55
CAL is a macro for CALL.

rali is a macro that performs Relative Alignment. since the code is copied elsewhere prior to executing the ALIGN directive doesn't generally work.

fxsave64 is a macro for fxsave (with "db 0x48" to make it x64 compatible). FASM doesn't include this instruction in the version I'm using (I can't speak for current version).
Post 18 Apr 2012, 00:55
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 18 Apr 2012, 04:39
found it. I was using the root link as a "union" and ended up writing over rsp. The code now works as it should.
Post 18 Apr 2012, 04:39
View user's profile Send private message Visit poster's website Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach 18 Apr 2012, 20:45
Do you have a homepage, i would like to learn more about your system&goals as it looks like you are preforming some serious assembly wizardry in there.. Wink
Post 18 Apr 2012, 20:45
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 19 Apr 2012, 14:22
I will eventually make a webpage about Mathis, I'm simply not ready yet. when it comes to my goals for Mathis, they're not readily apparent: I'm actually working towards a fully integral Scripting Language.

I've been focused heavily on the algorithms needed to support a parser. my current version is nothing more than a character code loop that tokenizes the input string.
Post 19 Apr 2012, 14:22
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.