Multiprocessor stuff

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
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Multiprocessor stuff

Post by mutex »

Hi,

I have a question i wonder if someone else have figured out a way to do this.

For some functions that i use data contained in the process structure. Ex: kmalloc / kfree, copy_from_user / copy_to_user.

In a multiprocessor system there will for examle be 4 processes actually running at the same time so i found that having a "process_s *process_running[MAX_CPU];" was a good idea. Then on ISR entry the isr will patch the right struct and also set right "int process_running_pid[MAX_CPU];" to correct value..

Now this i can do by cheking the tss reg in cpu to find out what struct to patch but this really does not help because all functions do also need to know what cpu / actual process it is going to work on.. Having a "find out what running process we are actually on" function in each kmalloc/kfree/copy_from_user etc seems rather wastefull and timeconsuming.. But i can't figure out how to do it.. I could of course push the number of the cpu on the kernel stack and have a function that gets this number out, but it seems like all these ways are pretty the same.. All functions containing code that needs to know something special related to running process needs to check it?

Im not sure if this made any sense.. But problem is. How do i give functions information on what cpu we run so that right process structure is used? Of course for kernel this is simple since all kernel processes use same process struct.. but normally we run one or more different processes on the different logical cpus...

Anyway.. Would appriciate good ideas on how to solve this..

cheers
Thomas
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Multiprocessor stuff

Post by Combuster »

I use the TSS trick to get the CPU number whenever I need it, so it's not a bad idea. In fact you can make a macro or inline function that does something like the following:

Code: Select all

uint16 cpu_number;
asm { 
   STR AX
   MOV [cpu_number], AX
}
cpu_number = (cpu_number - index_of_first_tss) >> 3;
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Re: Multiprocessor stuff

Post by mutex »

It might not be such a bad idea after all since the str operation does not consume very mouch time..

Then its the design view of things. Would you use this in all functions that require some process related data, or would you let those functions have parameters to them that points to this data. So that you move the logic up, and instead give a function full parameter list.

Code: Select all

Ex pseudocode:

1)
x = from str cpuindex;
call function(x,y,z);
function(x->w,y,z)
{
Do job. Dont need to know anything about surroundings..
}

2)
call funciton(x,y)

function(y,z)
{
x = from str cpuindex;
use x->w;
Do job. Knows enough to make things work.
}
In my opinion alternative 2 would be most easy to use while coding. you call malloc(x) instead of malloc(x,running_process[cpu_index]->meminfo) for alternative 1..

This is just a matter of taste maby.. In general i like to separate code,structure and classes so that they are "self maintainable" and do not need to be aware of surrounding code..

I think i will go with alternative 1 though its a little bit more writing..

-
Thomas
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Multiprocessor stuff

Post by Korona »

I use the GS segment to store processor local information and the FS segment to store information about the current thread.
In my os GS and FS can only be changed by the kernel, so I can safely assume that their contents do not change when I don't want them to change. This assumption cannot be made for all operating systems. If you allow users to run arbitrary code it is a bad idea to assume FS and GS do not change. You can use the TSS (as Combuster described) or the local APIC id to find out on which CPU your code is running. You could use the local APIC id as an index into an array that stores a struct which contains all processor specific information. The local APIC method also works when your os does not require all processors to use the same GDT.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Multiprocessor stuff

Post by Jeko »

I use this code:

Code: Select all

#define MAX_CPU 12
unsigned int num_cpus = 1;
static unsigned int cpu_ids[MAX_CPU];
#define APIC_ID            ((unsigned int *) (apic + 0x020))

static volatile unsigned int apic_read (unsigned int *addr)

{

    return *addr;

}

unsigned ArchThisCpu(void)

{

     if (num_cpus == 1)

          return 0;

     else

          return cpu_ids[(apic_read(APIC_ID) & 0xffffffff) >> 24];

}
Maybe I'll optimize the code writing all into an inline function. (Of course this is only a part of my SMP code)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Multiprocessor stuff

Post by Combuster »

/me wonders what the advantage is of doing an mmio read over a microcoded instruction.

STR works in all circumstances - the apic isn't always present
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Laksen
Member
Member
Posts: 140
Joined: Fri Nov 09, 2007 3:30 am
Location: Aalborg, Denmark

Re: Multiprocessor stuff

Post by Laksen »

Combuster, how would you start an AP without an APIC? :)

Edit: I don't like your solution with the TSS index operation. It requires that your TSS selectors are continious in the GDT. And I don't like inline assembly in general :P
Last edited by Laksen on Tue Jun 24, 2008 4:53 pm, edited 1 time in total.
http://j-software.dk | JPasKernel - My Object Pascal kernel
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Multiprocessor stuff

Post by Combuster »

Who said you never run a MP kernel on a uniprocessor system :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Laksen
Member
Member
Posts: 140
Joined: Fri Nov 09, 2007 3:30 am
Location: Aalborg, Denmark

Re: Multiprocessor stuff

Post by Laksen »

Ah you mean legacy hardware! :P

Well, in that case you have a point :)
http://j-software.dk | JPasKernel - My Object Pascal kernel
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: Multiprocessor stuff

Post by suthers »

Laksen wrote:Ah you mean legacy hardware! :P

Well, in that case you have a point :)
What do you mean legacy hardware?
Loads of people still have and run uni- core/proc systems, my desktop is multiple core, but my laptop (on which I do most of my deving because I'm like to code outside and I move about abit (school and stuff...)), is still single core and I don't consider it legacy because of that....
Jules
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Multiprocessor stuff

Post by Korona »

Combuster wrote:/me wonders what the advantage is of doing an mmio read over a microcoded instruction.

STR works in all circumstances - the apic isn't always present
STR does not work when each CPU uses a different GDT. (At least it does not work without additional overhead)
If all CPUs use the same GDT the STR method is probably the fastest.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Multiprocessor stuff

Post by Brendan »

Hi,

Fully supporting Intel's "X2APIC" would make a complete mess of both of these ideas (STR and GS), as it makes it possible to have up to 1048560 CPUs in a single computer (while the GDT is limited to 8191 entries). :D

The local APIC ID method still works regardless of how many CPUs there are. Also...

Typically (for paging) you map the local APIC/s somewhere in kernel space, so you can read the current CPU's local APIC ID directly from the local APIC with something like "mov eax,[localAPICID]". If it's a single-CPU system with no local APIC then you could map a page of RAM at this linear address instead. In this case it doesn't really matter if a local APIC is present or not - you can still use a single instruction to get the local APIC ID (or a fake local APIC ID from RAM).

@Combuster: Reading from a local APIC register is not like a normal read from memory mapped I/O - it's done entirely within the CPU (e.g. with no front side bus activity at all) and (if you don't get a TLB miss) it's probably about as fast as accessing an MSR.

Side note: For Laksen's comment, "legacy" may just mean "no local APIC". In this case it may be worth pointing out that modern (e.g. less than 5 years old) single-CPU systems do have a local APIC. To be more accurate, all Intel CPUs since the original Pentium include local APICs but the BIOS used to disable the local APIC in single-CPU systems (but BIOSs don't disable them anymore).


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.
Post Reply