Multiprocessor stuff
Multiprocessor stuff
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
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
- Combuster
- 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
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;Re: Multiprocessor stuff
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.
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
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.
}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
Re: Multiprocessor stuff
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.
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].
Re: Multiprocessor stuff
I use this code:
Maybe I'll optimize the code writing all into an inline function. (Of course this is only a part of my SMP 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];
}
- Combuster
- 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
/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 works in all circumstances - the apic isn't always present
Re: Multiprocessor stuff
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
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
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
- Combuster
- 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
Who said you never run a MP kernel on a uniprocessor system 
Re: Multiprocessor stuff
Ah you mean legacy hardware!
Well, in that case you have a point
Well, in that case you have a point
http://j-software.dk | JPasKernel - My Object Pascal kernel
Re: Multiprocessor stuff
What do you mean legacy hardware?Laksen wrote:Ah you mean legacy hardware!![]()
Well, in that case you have a point
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
Re: Multiprocessor stuff
STR does not work when each CPU uses a different GDT. (At least it does not work without additional overhead)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
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].
Re: Multiprocessor stuff
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).
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
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).
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.

