software task switch, mainly for Viktor.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
skoco2002

software task switch, mainly for Viktor.

Post by skoco2002 »

hello!
i was browsing alt.os.development to get that final knowledge :->> which will make me able to write software task switch and found this nice piece of code by Viktor, so thats why my questions are addressed to him, but i believe that the other guys can help me too.

consider this code :

;example switcher:
entry_XXX:
push IRQ_NUMBER_XXX ; interrupt gate number
jmp kernel_entry

kernel_entry:
push ;regs
mov eax, esp
mov esp, KERNEL_STACK
push eax
call kernel_code_in_c ; DWORD kernel(DWORD* regs);
cmp eax, 0
jz skip_switch
;switch here
mov eax, new_cr3
mov cr3, eax
;the tss is patched by the kernel (A)
;the new esp0 is set regs=new_thread->kernel_stack_ptr;(B)
skip_switch:
pop eax
mov esp, eax
pop ;regs
iret

nice, huh? i have a couple questions :
i believe that after pushing regs and saving/loading stack pointer there could be a code to set data segment regs with kernel data selector.
1.) i would like to know in which case man can skip this step, like Viktor did.
2.) my kernel is mapped to start at 3 gig (0xC0000000) loaded at 1 meg. what happened when i want to use a c function like kernel_code_in_c(), when i have mapped kernel code not in 1:1 ratio. it looks like that to have
access to my kernel data variables i have to reload cr3 with my kernel page dir. thats not very wise, it costs a lot of time. does it mean that to skip reloading cr3 with kernel page dir i cannot have kernel mapped like i have now?
3.) if the kernel is not mapped 1:1 is there any way how to skip reloading cr3 with kernel page dir if i want to work with kernel data, accessing vector of tasks and such stuff?
4.) you (Viktor) are using just one KERNEL_STACK for all threads. does it mean that to have just one kernel stack you need probably have this stack mapped in all user address spaces to point the same physical memory (i think one page is enough for the switcher), because an access to that stack is made through user page dir which is actually loaded?
5.) at line marked with (A) Viktor wrote : tss is patched by the kernel. what does it mean? esp0 in tss (that only one tss we have) set with new_thread->kernel_stack_ptr on line (B)? if so, why to do setting esp with KERNEL_STACK? when cpu detects the ring3 to ring0 transition, the stack pointer is got from tss, or not?

maybe all this questions are wrong.
i am just confused with accessing kernel data in timer_irq when cr3 has user task page dir. maybe accessing through kernel descriptors which's base starts at 3 gig? probably no, address translation will still go through user pdbr.

thanks a lot to everybody which will make this software task switch clear for me.

skoco
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:software task switch, mainly for Viktor.

Post by Pype.Clicker »

skoco2002 wrote: hello!
i was browsing alt.os.development to get that final knowledge :->>
hmm ... is there something special to configure to get access to alt.os.development ? all my previous attemps failed :(

i believe that after pushing regs and saving/loading stack pointer there could be a code to set data segment regs with kernel data selector.
1.) i would like to know in which case man can skip this step, like Viktor did.
you should *not* skip that test if you have some non-kernel code around. Think for instance at a user process that get interrupted. It will have a DPL3 selector in %ds, so you'll not be in a reliable env. for your kernel...
2.) my kernel is mapped to start at 3 gig (0xC0000000) loaded at 1 meg. what happened when i want to use a c function like kernel_code_in_c(), when i have mapped kernel code not in 1:1 ratio. it looks like that to have
access to my kernel data variables i have to reload cr3 with my kernel page dir. thats not very wise, it costs a lot of time. does it mean that to skip reloading cr3 with kernel page dir i cannot have kernel mapped like i have now?
if your kernel is at 0xc* in *every* address space and has been linked or relocated to run at that virtual address, no extra CR3 change is required, simply because your kernel variable are omnipresent ... Just consider the kernel has not a personnal address space: it just steals process address space and maps itself everywhere by hooking the last page tables for itself.
4.) you (Viktor) are using just one KERNEL_STACK for all threads. does it mean that to have just one kernel stack you need probably have this stack mapped in all user address spaces to point the same physical memory (i think one page is enough for the switcher), because an access to that stack is made through user page dir which is actually loaded?
That trick sounds weirds to me too ... when an interrupt occurs, the processor already pick up the kernel stack (from the static ss0:esp0 fields of the current TSS), so what's the point to turn from that mandatory stack to a generic stack ? ... moreover, enforcing a single kernel stack implies you cannot lock (for i/o completion, for instance) in kernel mode without locking the whole system ...
5.) at line marked with (A) Viktor wrote : tss is patched by the kernel. what does it mean? esp0 in tss (that only one tss we have) set with new_thread->kernel_stack_ptr on line (B)? if so, why to do setting esp with KERNEL_STACK? when cpu detects the ring3 to ring0 transition, the stack pointer is got from tss, or not?
yes, it does ... but if you're switching task (thus moving to another kernel & user stack) within the same TSS, you must patch manually the SS0:ESP0 fields of the TSS because the CPU doesn't do it when the interrupt returns (that's why those fields are called static).
skoco

Re:software task switch, mainly for Viktor.

Post by skoco »

You can use alt.os.development trough www.google.com,
click on ->groups<- link. I need to do that like this, since i'm not from us, and the news doesn't work outside us (i think).

and of course, thank you for reply ... skoco
grey wolf

Re:software task switch, mainly for Viktor.

Post by grey wolf »

news works outside the US, but you need a working NNTP server that can be accessed by your ISP. that can sometimes cost an extra $20USD a month for many people.
K.J.

Re:software task switch, mainly for Viktor.

Post by K.J. »

There are thousands of free NNTP servers, just search www.google.com for them.

K.J.
Post Reply