Linux and BSD

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.
HardEnough

Linux and BSD

Post by HardEnough »

hello all
i have some question that i need to the answers for them :
1- Does linux use paging in its mm or pure segmentation with swapping ?
2- Does bsd use paging in its mm or pure segmentation ?
3- Does linux use software for taskswitching ?
4- Does bsd use software for taskswitchin ?
5- Does linux use LDTs or just GDT ?
6- Does bsd use LDTs or just GDT ?
7- What is the name of process scheduling alogorithm used in linux ?
8- What is the name of process scheduling alogorithm used in bsd ?

thanks
Mark

Re:Linux and BSD

Post by Mark »

Why not research the source and then tell us. I bet you will learn some good stuff
JoeKayzA

Re:Linux and BSD

Post by JoeKayzA »

Honestly, I beg you might find the answers pretty easily in the official docs, or via google (www.google.com is the domain, btw). No offense at all, but I would look there at first...;)

Now, AFAIK, linux uses paging only, no segmentation at all, and a low-latency software task switching mechanism (at least since 2.6). As for the algorithm, there was a posting here a few months ago, that had a link to a _very_ good guide through the linux 2.6 scheduler, I'm sure you'll find an answer there. Try searching the forum for 'linux scheduler'.

As for BSD, don't have much experience with it, but I doubt they are that different in these points.

cheers Joe
HardEnough

Re:Linux and BSD

Post by HardEnough »

ok, i will try to search both the forums and google but browsing the source is not a good idea the linux source may kill me ;-)
but how the bsd /usr/include/sys/proc.h defines PID_MAX 99999 but the maximum no of LDT the could be allocated 8192 ????
DennisCGc

Re:Linux and BSD

Post by DennisCGc »

Hi,
HardEnough wrote: but how the bsd /usr/include/sys/proc.h defines PID_MAX 99999 but the maximum no of LDT the could be allocated 8192 ????
Simple, not every OS creates a NEW LDT for a new task. An OS is not forced to do. (e.g. using different address spaces)

HTH a bit,

DennisCGc.
HardEnough

Re:Linux and BSD

Post by HardEnough »

Can the OS just use a single LDT
such that it saves the LDT for each process in the process structure and when the schedular switches another process it loads it LDT back from the process struct ?
JoeKayzA

Re:Linux and BSD

Post by JoeKayzA »

I suspect that linux/bsd don't use the LDT at all. ;) Instead it probably uses a flat address space model (look at the faq: www.mega-tokyo.com/osfaq2) with a minimal GDT, and does all the multitasking stuff in software. Thus, the max. number of processes is only limited by the amount of memory installed (it needs memory because every process is represented by a set of data structures). Note that this is quite similar for many modern x86 OS's, including MS windows, BeOS and (now also) MacOSX.

But again, google will be your best friend :)

cheers Joe
HardEnough

Re:Linux and BSD

Post by HardEnough »

Thanks JoeKayzA
but how linux doesn't use LDT for each process, if on GDT is used for all the process a process can access another process mem space if they both have the same privilage level is not am write ???
please any one make it clear for me..
Thanks alot
AR

Re:Linux and BSD

Post by AR »

That's what paging is for, it allows the segments to cover a virtual address space rather than a physical one. By removing pages belonging to other processes by swapping page directories you will prevent them from accessing each other's memory.

The LDT is optional and unnecessary, the reason you can only have 8192 of them is that is the maxiumum size of the GDT (since the LDTs must be declared in the GDT).
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Linux and BSD

Post by Brendan »

Hi,
HardEnough wrote:but how linux doesn't use LDT for each process, if on GDT is used for all the process a process can access another process mem space if they both have the same privilage level is not am write ???
please any one make it clear for me..
With paging each process has it's own address space, and no software can access anything outside of the current address space (ie. no process can access another process's memory as it's in a different address space). Normally the kernel itself is mapped into every address space and protected from CPL=3 code using paging. Segmentation isn't used at all, except for the "flat" segments, and all segment registers become constants and are never changed.

This means the GDT needs 4 descriptors (CPL=0 code, CPL=0 data, CPL=3 code, CPL=3 data) plus one barely used TSS descriptor for each CPU. This means for 255 CPUs (the maximum number theoretically possible on 80x86) you'd only need 259 GDT descriptors. Those TSS's are only needed because of the way the CPU changes from CPL=3 to CPL=0, and is rather unfortunate.

Despite this it's likely that Linux/BSD use a few extra GDT descriptors. For SYSENTER and SYSCALL the GDT descrptors need to be in a specific order, which is different for SYSENTER and SYSCALL, so some of the code/data descriptors may be duplicated to support both SYSENTER and SYSCALL at the same time. Another descriptor may be used for a call gate for the kernel API (but AFAIK Linux at least expects "int 0x80" to be used and doesn't provide a call gate option).

Another "trick" is to set an unused segment descriptor (FS or GS) to point to the current CPUs data so that it can be accessed quickly. This also becomes a constant on each CPU, but it's a different constant for each CPU. This adds another 255 descriptors.

Also (AFAIK) Windows uses the other unused descriptor (FS or GS) to access some sort of global data. DS and ES are both used for some instructions (e.g. "rep movsd") and can't be given a special purpose like FS or GS...

Adding all this up gives a maximum of 518 descriptors.

This means using an LDT (or using one per process or one per CPU) isn't necessary for most modern OS's - I doubt you'd find a modern OS that uses more than 768 descriptors, with the extra descriptors used for alignment (so that TSS descriptors and CPU specific descriptors begin at GDT entry 256 or 512, although this alignment would only be to make things more recognisable for humans).

I should point out that all of the above is derived from a knowledge of 80x86 architecture, and I have no idea what Linux, BSD or Windows actually does do. Despite this (IMHO) it is applicable to all OSs that don't use segmentation (ie. all modern OSs) - AFAIK there are no other uses for descriptors.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Linux and BSD

Post by Solar »

I know for a fact that the GCC compiler suite does not support anything else but a flat memory model, so the answer to 1) and 2) (paging or segmentation) is clear: Paging.

The answer to 7) is "changes with every other release". The latest craze is the O(1) scheduler by someone called Ingo...
Every good solution is obvious once you've found it.
HardEnough

Re:Linux and BSD

Post by HardEnough »

Thanks alot
now things become clear for me, so paging is the magic ;)
HardEnough

Re:Linux and BSD

Post by HardEnough »

i've another question here,
there is a lack of documentation about setting LDT.
i found in the intel manual that :
lgdt [mem]
lidt [mem] or lidt ax
lldt [mem] or lidt ax

i know that in case of lgdt the mem points to limit & start of gdt but in case of idt & ldt what should [mem] & ax point to ? is it a descriptor in the gdt or like lgdt (limit & start) ?

Thanks
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Linux and BSD

Post by Solar »

Brendan wrote:Also (AFAIK) Windows uses the other unused descriptor (FS or GS) to access some sort of global data.
IIRC, FS is used for SEH (structured exception handling).
Every good solution is obvious once you've found it.
HardEnough

Re:Linux and BSD

Post by HardEnough »

can any one please answer my question:
there is a lack of documentation about setting LDT.
i found in the intel manual that :
lgdt [mem]
lidt [mem] or lidt ax
lldt [mem] or lidt ax

i know that in case of lgdt the mem points to limit & start of gdt but in case of idt & ldt what should [mem] & ax point to ? is it a descriptor in the gdt or like lgdt (limit & start) ?

Thanks
Post Reply